Introduce two delays in nfe_stop().
[dragonfly.git] / sys / dev / netif / nfe / if_nfe.c
1 /*      $OpenBSD: if_nfe.c,v 1.63 2006/06/17 18:00:43 brad Exp $        */
2 /*      $DragonFly: src/sys/dev/netif/nfe/if_nfe.c,v 1.13 2007/08/09 07:24:50 dillon Exp $      */
3
4 /*
5  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
6  * 
7  * This code is derived from software contributed to The DragonFly Project
8  * by Sepherosa Ziehau <sepherosa@gmail.com> and
9  * Matthew Dillon <dillon@apollo.backplane.com>
10  * 
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  * 3. Neither the name of The DragonFly Project nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific, prior written permission.
24  * 
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
29  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 /*
40  * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
41  * Copyright (c) 2005, 2006 Jonathan Gray <jsg@openbsd.org>
42  *
43  * Permission to use, copy, modify, and distribute this software for any
44  * purpose with or without fee is hereby granted, provided that the above
45  * copyright notice and this permission notice appear in all copies.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54  */
55
56 /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */
57
58 #include "opt_polling.h"
59
60 #include <sys/param.h>
61 #include <sys/endian.h>
62 #include <sys/kernel.h>
63 #include <sys/bus.h>
64 #include <sys/proc.h>
65 #include <sys/rman.h>
66 #include <sys/serialize.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69 #include <sys/sysctl.h>
70
71 #include <net/ethernet.h>
72 #include <net/if.h>
73 #include <net/bpf.h>
74 #include <net/if_arp.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/ifq_var.h>
78 #include <net/if_types.h>
79 #include <net/if_var.h>
80 #include <net/vlan/if_vlan_var.h>
81
82 #include <bus/pci/pcireg.h>
83 #include <bus/pci/pcivar.h>
84 #include <bus/pci/pcidevs.h>
85
86 #include <dev/netif/mii_layer/mii.h>
87 #include <dev/netif/mii_layer/miivar.h>
88
89 #include "miibus_if.h"
90
91 #include <dev/netif/nfe/if_nfereg.h>
92 #include <dev/netif/nfe/if_nfevar.h>
93
94 #define NFE_CSUM
95 #define NFE_CSUM_FEATURES       (CSUM_IP | CSUM_TCP | CSUM_UDP)
96
97 static int      nfe_probe(device_t);
98 static int      nfe_attach(device_t);
99 static int      nfe_detach(device_t);
100 static void     nfe_shutdown(device_t);
101 static int      nfe_resume(device_t);
102 static int      nfe_suspend(device_t);
103
104 static int      nfe_miibus_readreg(device_t, int, int);
105 static void     nfe_miibus_writereg(device_t, int, int, int);
106 static void     nfe_miibus_statchg(device_t);
107
108 #ifdef DEVICE_POLLING
109 static void     nfe_poll(struct ifnet *, enum poll_cmd, int);
110 #endif
111 static void     nfe_intr(void *);
112 static int      nfe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
113 static void     nfe_rxeof(struct nfe_softc *);
114 static void     nfe_txeof(struct nfe_softc *);
115 static int      nfe_encap(struct nfe_softc *, struct nfe_tx_ring *,
116                           struct mbuf *);
117 static void     nfe_start(struct ifnet *);
118 static void     nfe_watchdog(struct ifnet *);
119 static void     nfe_init(void *);
120 static void     nfe_stop(struct nfe_softc *);
121 static struct nfe_jbuf *nfe_jalloc(struct nfe_softc *);
122 static void     nfe_jfree(void *);
123 static void     nfe_jref(void *);
124 static int      nfe_jpool_alloc(struct nfe_softc *, struct nfe_rx_ring *);
125 static void     nfe_jpool_free(struct nfe_softc *, struct nfe_rx_ring *);
126 static int      nfe_alloc_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
127 static void     nfe_reset_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
128 static int      nfe_init_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
129 static void     nfe_free_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
130 static int      nfe_alloc_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
131 static void     nfe_reset_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
132 static int      nfe_init_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
133 static void     nfe_free_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
134 static int      nfe_ifmedia_upd(struct ifnet *);
135 static void     nfe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
136 static void     nfe_setmulti(struct nfe_softc *);
137 static void     nfe_get_macaddr(struct nfe_softc *, uint8_t *);
138 static void     nfe_set_macaddr(struct nfe_softc *, const uint8_t *);
139 static void     nfe_tick(void *);
140 static void     nfe_ring_dma_addr(void *, bus_dma_segment_t *, int, int);
141 static void     nfe_buf_dma_addr(void *, bus_dma_segment_t *, int, bus_size_t,
142                                  int);
143 static void     nfe_set_paddr_rxdesc(struct nfe_softc *, struct nfe_rx_ring *,
144                                      int, bus_addr_t);
145 static void     nfe_set_ready_rxdesc(struct nfe_softc *, struct nfe_rx_ring *,
146                                      int);
147 static int      nfe_newbuf_std(struct nfe_softc *, struct nfe_rx_ring *, int,
148                                int);
149 static int      nfe_newbuf_jumbo(struct nfe_softc *, struct nfe_rx_ring *, int,
150                                  int);
151
152 #define NFE_DEBUG
153 #ifdef NFE_DEBUG
154
155 static int      nfe_debug = 0;
156 static int      nfe_rx_ring_count = NFE_RX_RING_DEF_COUNT;
157
158 TUNABLE_INT("hw.nfe.rx_ring_count", &nfe_rx_ring_count);
159
160 SYSCTL_NODE(_hw, OID_AUTO, nfe, CTLFLAG_RD, 0, "nVidia GigE parameters");
161 SYSCTL_INT(_hw_nfe, OID_AUTO, rx_ring_count, CTLFLAG_RD, &nfe_rx_ring_count,
162            NFE_RX_RING_DEF_COUNT, "rx ring count");
163 SYSCTL_INT(_hw_nfe, OID_AUTO, debug, CTLFLAG_RW, &nfe_debug, 0,
164            "control debugging printfs");
165
166 #define DPRINTF(sc, fmt, ...) do {              \
167         if (nfe_debug) {                        \
168                 if_printf(&(sc)->arpcom.ac_if,  \
169                           fmt, __VA_ARGS__);    \
170         }                                       \
171 } while (0)
172
173 #define DPRINTFN(sc, lv, fmt, ...) do {         \
174         if (nfe_debug >= (lv)) {                \
175                 if_printf(&(sc)->arpcom.ac_if,  \
176                           fmt, __VA_ARGS__);    \
177         }                                       \
178 } while (0)
179
180 #else   /* !NFE_DEBUG */
181
182 #define DPRINTF(sc, fmt, ...)
183 #define DPRINTFN(sc, lv, fmt, ...)
184
185 #endif  /* NFE_DEBUG */
186
187 struct nfe_dma_ctx {
188         int                     nsegs;
189         bus_dma_segment_t       *segs;
190 };
191
192 static const struct nfe_dev {
193         uint16_t        vid;
194         uint16_t        did;
195         const char      *desc;
196 } nfe_devices[] = {
197         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_LAN,
198           "NVIDIA nForce Fast Ethernet" },
199
200         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_LAN,
201           "NVIDIA nForce2 Fast Ethernet" },
202
203         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN1,
204           "NVIDIA nForce3 Gigabit Ethernet" },
205
206         /* XXX TGEN the next chip can also be found in the nForce2 Ultra 400Gb
207            chipset, and possibly also the 400R; it might be both nForce2- and
208            nForce3-based boards can use the same MCPs (= southbridges) */
209         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN2,
210           "NVIDIA nForce3 Gigabit Ethernet" },
211
212         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN3,
213           "NVIDIA nForce3 Gigabit Ethernet" },
214
215         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN4,
216           "NVIDIA nForce3 Gigabit Ethernet" },
217
218         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN5,
219           "NVIDIA nForce3 Gigabit Ethernet" },
220
221         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN1,
222           "NVIDIA CK804 Gigabit Ethernet" },
223
224         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN2,
225           "NVIDIA CK804 Gigabit Ethernet" },
226
227         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN1,
228           "NVIDIA MCP04 Gigabit Ethernet" },
229
230         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN2,
231           "NVIDIA MCP04 Gigabit Ethernet" },
232
233         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN1,
234           "NVIDIA MCP51 Gigabit Ethernet" },
235
236         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN2,
237           "NVIDIA MCP51 Gigabit Ethernet" },
238
239         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN1,
240           "NVIDIA MCP55 Gigabit Ethernet" },
241
242         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN2,
243           "NVIDIA MCP55 Gigabit Ethernet" },
244
245         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN1,
246           "NVIDIA MCP61 Gigabit Ethernet" },
247
248         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN2,
249           "NVIDIA MCP61 Gigabit Ethernet" },
250
251         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN3,
252           "NVIDIA MCP61 Gigabit Ethernet" },
253
254         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN4,
255           "NVIDIA MCP61 Gigabit Ethernet" },
256
257         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN1,
258           "NVIDIA MCP65 Gigabit Ethernet" },
259
260         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN2,
261           "NVIDIA MCP65 Gigabit Ethernet" },
262
263         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN3,
264           "NVIDIA MCP65 Gigabit Ethernet" },
265
266         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN4,
267           "NVIDIA MCP65 Gigabit Ethernet" },
268
269         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN1,
270           "NVIDIA MCP67 Gigabit Ethernet" },
271
272         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN2,
273           "NVIDIA MCP67 Gigabit Ethernet" },
274
275         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN3,
276           "NVIDIA MCP67 Gigabit Ethernet" },
277
278         { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN4,
279           "NVIDIA MCP67 Gigabit Ethernet" }
280 };
281
282 static device_method_t nfe_methods[] = {
283         /* Device interface */
284         DEVMETHOD(device_probe,         nfe_probe),
285         DEVMETHOD(device_attach,        nfe_attach),
286         DEVMETHOD(device_detach,        nfe_detach),
287         DEVMETHOD(device_suspend,       nfe_suspend),
288         DEVMETHOD(device_resume,        nfe_resume),
289         DEVMETHOD(device_shutdown,      nfe_shutdown),
290
291         /* Bus interface */
292         DEVMETHOD(bus_print_child,      bus_generic_print_child),
293         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
294
295         /* MII interface */
296         DEVMETHOD(miibus_readreg,       nfe_miibus_readreg),
297         DEVMETHOD(miibus_writereg,      nfe_miibus_writereg),
298         DEVMETHOD(miibus_statchg,       nfe_miibus_statchg),
299
300         { 0, 0 }
301 };
302
303 static driver_t nfe_driver = {
304         "nfe",
305         nfe_methods,
306         sizeof(struct nfe_softc)
307 };
308
309 static devclass_t       nfe_devclass;
310
311 DECLARE_DUMMY_MODULE(if_nfe);
312 MODULE_DEPEND(if_nfe, miibus, 1, 1, 1);
313 DRIVER_MODULE(if_nfe, pci, nfe_driver, nfe_devclass, 0, 0);
314 DRIVER_MODULE(miibus, nfe, miibus_driver, miibus_devclass, 0, 0);
315
316 static int
317 nfe_probe(device_t dev)
318 {
319         const struct nfe_dev *n;
320         uint16_t vid, did;
321
322         vid = pci_get_vendor(dev);
323         did = pci_get_device(dev);
324         for (n = nfe_devices; n->desc != NULL; ++n) {
325                 if (vid == n->vid && did == n->did) {
326                         struct nfe_softc *sc = device_get_softc(dev);
327
328                         switch (did) {
329                         case PCI_PRODUCT_NVIDIA_NFORCE3_LAN2:
330                         case PCI_PRODUCT_NVIDIA_NFORCE3_LAN3:
331                         case PCI_PRODUCT_NVIDIA_NFORCE3_LAN4:
332                         case PCI_PRODUCT_NVIDIA_NFORCE3_LAN5:
333                                 sc->sc_flags = NFE_JUMBO_SUP |
334                                                NFE_HW_CSUM;
335                                 break;
336                         case PCI_PRODUCT_NVIDIA_MCP51_LAN1:
337                         case PCI_PRODUCT_NVIDIA_MCP51_LAN2:
338                         case PCI_PRODUCT_NVIDIA_MCP61_LAN1:
339                         case PCI_PRODUCT_NVIDIA_MCP61_LAN2:
340                         case PCI_PRODUCT_NVIDIA_MCP61_LAN3:
341                         case PCI_PRODUCT_NVIDIA_MCP61_LAN4:
342                         case PCI_PRODUCT_NVIDIA_MCP67_LAN1:
343                         case PCI_PRODUCT_NVIDIA_MCP67_LAN2:
344                         case PCI_PRODUCT_NVIDIA_MCP67_LAN3:
345                         case PCI_PRODUCT_NVIDIA_MCP67_LAN4:
346                                 sc->sc_flags = NFE_40BIT_ADDR;
347                                 break;
348                         case PCI_PRODUCT_NVIDIA_CK804_LAN1:
349                         case PCI_PRODUCT_NVIDIA_CK804_LAN2:
350                         case PCI_PRODUCT_NVIDIA_MCP04_LAN1:
351                         case PCI_PRODUCT_NVIDIA_MCP04_LAN2:
352                         case PCI_PRODUCT_NVIDIA_MCP65_LAN1:
353                         case PCI_PRODUCT_NVIDIA_MCP65_LAN2:
354                         case PCI_PRODUCT_NVIDIA_MCP65_LAN3:
355                         case PCI_PRODUCT_NVIDIA_MCP65_LAN4:
356                                 sc->sc_flags = NFE_JUMBO_SUP |
357                                                NFE_40BIT_ADDR |
358                                                NFE_HW_CSUM;
359                                 break;
360                         case PCI_PRODUCT_NVIDIA_MCP55_LAN1:
361                         case PCI_PRODUCT_NVIDIA_MCP55_LAN2:
362                                 sc->sc_flags = NFE_JUMBO_SUP |
363                                                NFE_40BIT_ADDR |
364                                                NFE_HW_CSUM |
365                                                NFE_HW_VLAN;
366                                 break;
367                         }
368
369                         device_set_desc(dev, n->desc);
370                         device_set_async_attach(dev, TRUE);
371                         return 0;
372                 }
373         }
374         return ENXIO;
375 }
376
377 static int
378 nfe_attach(device_t dev)
379 {
380         struct nfe_softc *sc = device_get_softc(dev);
381         struct ifnet *ifp = &sc->arpcom.ac_if;
382         uint8_t eaddr[ETHER_ADDR_LEN];
383         int error;
384
385         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
386         lwkt_serialize_init(&sc->sc_jbuf_serializer);
387
388         sc->sc_mem_rid = PCIR_BAR(0);
389
390 #ifndef BURN_BRIDGES
391         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
392                 uint32_t mem, irq;
393
394                 mem = pci_read_config(dev, sc->sc_mem_rid, 4);
395                 irq = pci_read_config(dev, PCIR_INTLINE, 4);
396
397                 device_printf(dev, "chip is in D%d power mode "
398                     "-- setting to D0\n", pci_get_powerstate(dev));
399
400                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
401
402                 pci_write_config(dev, sc->sc_mem_rid, mem, 4);
403                 pci_write_config(dev, PCIR_INTLINE, irq, 4);
404         }
405 #endif  /* !BURN_BRIDGE */
406
407         /* Enable bus mastering */
408         pci_enable_busmaster(dev);
409
410         /* Allocate IO memory */
411         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
412                                                 &sc->sc_mem_rid, RF_ACTIVE);
413         if (sc->sc_mem_res == NULL) {
414                 device_printf(dev, "cound not allocate io memory\n");
415                 return ENXIO;
416         }
417         sc->sc_memh = rman_get_bushandle(sc->sc_mem_res);
418         sc->sc_memt = rman_get_bustag(sc->sc_mem_res);
419
420         /* Allocate IRQ */
421         sc->sc_irq_rid = 0;
422         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
423                                                 &sc->sc_irq_rid,
424                                                 RF_SHAREABLE | RF_ACTIVE);
425         if (sc->sc_irq_res == NULL) {
426                 device_printf(dev, "could not allocate irq\n");
427                 error = ENXIO;
428                 goto fail;
429         }
430
431         nfe_get_macaddr(sc, eaddr);
432
433         /*
434          * Allocate Tx and Rx rings.
435          */
436         error = nfe_alloc_tx_ring(sc, &sc->txq);
437         if (error) {
438                 device_printf(dev, "could not allocate Tx ring\n");
439                 goto fail;
440         }
441
442         error = nfe_alloc_rx_ring(sc, &sc->rxq);
443         if (error) {
444                 device_printf(dev, "could not allocate Rx ring\n");
445                 goto fail;
446         }
447
448         error = mii_phy_probe(dev, &sc->sc_miibus, nfe_ifmedia_upd,
449                               nfe_ifmedia_sts);
450         if (error) {
451                 device_printf(dev, "MII without any phy\n");
452                 goto fail;
453         }
454
455         ifp->if_softc = sc;
456         ifp->if_mtu = ETHERMTU;
457         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
458         ifp->if_ioctl = nfe_ioctl;
459         ifp->if_start = nfe_start;
460 #ifdef DEVICE_POLLING
461         ifp->if_poll = nfe_poll;
462 #endif
463         ifp->if_watchdog = nfe_watchdog;
464         ifp->if_init = nfe_init;
465         ifq_set_maxlen(&ifp->if_snd, NFE_IFQ_MAXLEN);
466         ifq_set_ready(&ifp->if_snd);
467
468         ifp->if_capabilities = IFCAP_VLAN_MTU;
469
470         if (sc->sc_flags & NFE_HW_VLAN)
471                 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
472
473 #ifdef NFE_CSUM
474         if (sc->sc_flags & NFE_HW_CSUM) {
475                 ifp->if_capabilities |= IFCAP_HWCSUM;
476                 ifp->if_hwassist = NFE_CSUM_FEATURES;
477         }
478 #else
479         sc->sc_flags &= ~NFE_HW_CSUM;
480 #endif
481         ifp->if_capenable = ifp->if_capabilities;
482
483         callout_init(&sc->sc_tick_ch);
484
485         ether_ifattach(ifp, eaddr, NULL);
486
487         error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, nfe_intr, sc,
488                                &sc->sc_ih, ifp->if_serializer);
489         if (error) {
490                 device_printf(dev, "could not setup intr\n");
491                 ether_ifdetach(ifp);
492                 goto fail;
493         }
494
495         return 0;
496 fail:
497         nfe_detach(dev);
498         return error;
499 }
500
501 static int
502 nfe_detach(device_t dev)
503 {
504         struct nfe_softc *sc = device_get_softc(dev);
505
506         if (device_is_attached(dev)) {
507                 struct ifnet *ifp = &sc->arpcom.ac_if;
508
509                 lwkt_serialize_enter(ifp->if_serializer);
510                 nfe_stop(sc);
511                 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_ih);
512                 lwkt_serialize_exit(ifp->if_serializer);
513
514                 ether_ifdetach(ifp);
515         }
516
517         if (sc->sc_miibus != NULL)
518                 device_delete_child(dev, sc->sc_miibus);
519         bus_generic_detach(dev);
520
521         if (sc->sc_irq_res != NULL) {
522                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
523                                      sc->sc_irq_res);
524         }
525
526         if (sc->sc_mem_res != NULL) {
527                 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
528                                      sc->sc_mem_res);
529         }
530
531         nfe_free_tx_ring(sc, &sc->txq);
532         nfe_free_rx_ring(sc, &sc->rxq);
533
534         return 0;
535 }
536
537 static void
538 nfe_shutdown(device_t dev)
539 {
540         struct nfe_softc *sc = device_get_softc(dev);
541         struct ifnet *ifp = &sc->arpcom.ac_if;
542
543         lwkt_serialize_enter(ifp->if_serializer);
544         nfe_stop(sc);
545         lwkt_serialize_exit(ifp->if_serializer);
546 }
547
548 static int
549 nfe_suspend(device_t dev)
550 {
551         struct nfe_softc *sc = device_get_softc(dev);
552         struct ifnet *ifp = &sc->arpcom.ac_if;
553
554         lwkt_serialize_enter(ifp->if_serializer);
555         nfe_stop(sc);
556         lwkt_serialize_exit(ifp->if_serializer);
557
558         return 0;
559 }
560
561 static int
562 nfe_resume(device_t dev)
563 {
564         struct nfe_softc *sc = device_get_softc(dev);
565         struct ifnet *ifp = &sc->arpcom.ac_if;
566
567         lwkt_serialize_enter(ifp->if_serializer);
568         if (ifp->if_flags & IFF_UP)
569                 nfe_init(sc);
570         lwkt_serialize_exit(ifp->if_serializer);
571
572         return 0;
573 }
574
575 static void
576 nfe_miibus_statchg(device_t dev)
577 {
578         struct nfe_softc *sc = device_get_softc(dev);
579         struct mii_data *mii = device_get_softc(sc->sc_miibus);
580         uint32_t phy, seed, misc = NFE_MISC1_MAGIC, link = NFE_MEDIA_SET;
581
582         phy = NFE_READ(sc, NFE_PHY_IFACE);
583         phy &= ~(NFE_PHY_HDX | NFE_PHY_100TX | NFE_PHY_1000T);
584
585         seed = NFE_READ(sc, NFE_RNDSEED);
586         seed &= ~NFE_SEED_MASK;
587
588         if ((mii->mii_media_active & IFM_GMASK) == IFM_HDX) {
589                 phy  |= NFE_PHY_HDX;    /* half-duplex */
590                 misc |= NFE_MISC1_HDX;
591         }
592
593         switch (IFM_SUBTYPE(mii->mii_media_active)) {
594         case IFM_1000_T:        /* full-duplex only */
595                 link |= NFE_MEDIA_1000T;
596                 seed |= NFE_SEED_1000T;
597                 phy  |= NFE_PHY_1000T;
598                 break;
599         case IFM_100_TX:
600                 link |= NFE_MEDIA_100TX;
601                 seed |= NFE_SEED_100TX;
602                 phy  |= NFE_PHY_100TX;
603                 break;
604         case IFM_10_T:
605                 link |= NFE_MEDIA_10T;
606                 seed |= NFE_SEED_10T;
607                 break;
608         }
609
610         NFE_WRITE(sc, NFE_RNDSEED, seed);       /* XXX: gigabit NICs only? */
611
612         NFE_WRITE(sc, NFE_PHY_IFACE, phy);
613         NFE_WRITE(sc, NFE_MISC1, misc);
614         NFE_WRITE(sc, NFE_LINKSPEED, link);
615 }
616
617 static int
618 nfe_miibus_readreg(device_t dev, int phy, int reg)
619 {
620         struct nfe_softc *sc = device_get_softc(dev);
621         uint32_t val;
622         int ntries;
623
624         NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
625
626         if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
627                 NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
628                 DELAY(100);
629         }
630
631         NFE_WRITE(sc, NFE_PHY_CTL, (phy << NFE_PHYADD_SHIFT) | reg);
632
633         for (ntries = 0; ntries < 1000; ntries++) {
634                 DELAY(100);
635                 if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
636                         break;
637         }
638         if (ntries == 1000) {
639                 DPRINTFN(sc, 2, "timeout waiting for PHY %s\n", "");
640                 return 0;
641         }
642
643         if (NFE_READ(sc, NFE_PHY_STATUS) & NFE_PHY_ERROR) {
644                 DPRINTFN(sc, 2, "could not read PHY %s\n", "");
645                 return 0;
646         }
647
648         val = NFE_READ(sc, NFE_PHY_DATA);
649         if (val != 0xffffffff && val != 0)
650                 sc->mii_phyaddr = phy;
651
652         DPRINTFN(sc, 2, "mii read phy %d reg 0x%x ret 0x%x\n", phy, reg, val);
653
654         return val;
655 }
656
657 static void
658 nfe_miibus_writereg(device_t dev, int phy, int reg, int val)
659 {
660         struct nfe_softc *sc = device_get_softc(dev);
661         uint32_t ctl;
662         int ntries;
663
664         NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
665
666         if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
667                 NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
668                 DELAY(100);
669         }
670
671         NFE_WRITE(sc, NFE_PHY_DATA, val);
672         ctl = NFE_PHY_WRITE | (phy << NFE_PHYADD_SHIFT) | reg;
673         NFE_WRITE(sc, NFE_PHY_CTL, ctl);
674
675         for (ntries = 0; ntries < 1000; ntries++) {
676                 DELAY(100);
677                 if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
678                         break;
679         }
680
681 #ifdef NFE_DEBUG
682         if (ntries == 1000)
683                 DPRINTFN(sc, 2, "could not write to PHY %s\n", "");
684 #endif
685 }
686
687 #ifdef DEVICE_POLLING
688
689 static void
690 nfe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
691 {
692         struct nfe_softc *sc = ifp->if_softc;
693
694         switch(cmd) {
695         case POLL_REGISTER:
696                 /* Disable interrupts */
697                 NFE_WRITE(sc, NFE_IRQ_MASK, 0);
698                 break;
699         case POLL_DEREGISTER:
700                 /* enable interrupts */
701                 NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED);
702                 break;
703         case POLL_AND_CHECK_STATUS:
704                 /* fall through */
705         case POLL_ONLY:
706                 if (ifp->if_flags & IFF_RUNNING) {
707                         nfe_rxeof(sc);
708                         nfe_txeof(sc);
709                 }
710                 break;
711         }
712 }
713
714 #endif
715
716 static void
717 nfe_intr(void *arg)
718 {
719         struct nfe_softc *sc = arg;
720         struct ifnet *ifp = &sc->arpcom.ac_if;
721         uint32_t r;
722
723         r = NFE_READ(sc, NFE_IRQ_STATUS);
724         if (r == 0)
725                 return; /* not for us */
726         NFE_WRITE(sc, NFE_IRQ_STATUS, r);
727
728         DPRINTFN(sc, 5, "%s: interrupt register %x\n", __func__, r);
729
730         if (r & NFE_IRQ_LINK) {
731                 NFE_READ(sc, NFE_PHY_STATUS);
732                 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
733                 DPRINTF(sc, "link state changed %s\n", "");
734         }
735
736         if (ifp->if_flags & IFF_RUNNING) {
737                 /* check Rx ring */
738                 nfe_rxeof(sc);
739
740                 /* check Tx ring */
741                 nfe_txeof(sc);
742         }
743 }
744
745 static int
746 nfe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
747 {
748         struct nfe_softc *sc = ifp->if_softc;
749         struct ifreq *ifr = (struct ifreq *)data;
750         struct mii_data *mii;
751         int error = 0, mask;
752
753         switch (cmd) {
754         case SIOCSIFMTU:
755                 if (((sc->sc_flags & NFE_JUMBO_SUP) &&
756                      ifr->ifr_mtu > NFE_JUMBO_MTU) ||
757                     ((sc->sc_flags & NFE_JUMBO_SUP) == 0 &&
758                      ifr->ifr_mtu > ETHERMTU)) {
759                         return EINVAL;
760                 } else if (ifp->if_mtu != ifr->ifr_mtu) {
761                         ifp->if_mtu = ifr->ifr_mtu;
762                         nfe_init(sc);
763                 }
764                 break;
765         case SIOCSIFFLAGS:
766                 if (ifp->if_flags & IFF_UP) {
767                         /*
768                          * If only the PROMISC or ALLMULTI flag changes, then
769                          * don't do a full re-init of the chip, just update
770                          * the Rx filter.
771                          */
772                         if ((ifp->if_flags & IFF_RUNNING) &&
773                             ((ifp->if_flags ^ sc->sc_if_flags) &
774                              (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
775                                 nfe_setmulti(sc);
776                         } else {
777                                 if (!(ifp->if_flags & IFF_RUNNING))
778                                         nfe_init(sc);
779                         }
780                 } else {
781                         if (ifp->if_flags & IFF_RUNNING)
782                                 nfe_stop(sc);
783                 }
784                 sc->sc_if_flags = ifp->if_flags;
785                 break;
786         case SIOCADDMULTI:
787         case SIOCDELMULTI:
788                 if (ifp->if_flags & IFF_RUNNING)
789                         nfe_setmulti(sc);
790                 break;
791         case SIOCSIFMEDIA:
792         case SIOCGIFMEDIA:
793                 mii = device_get_softc(sc->sc_miibus);
794                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
795                 break;
796         case SIOCSIFCAP:
797                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
798                 if ((mask & IFCAP_HWCSUM) &&
799                     (ifp->if_capabilities & IFCAP_HWCSUM)) {
800                         if (IFCAP_HWCSUM & ifp->if_capenable) {
801                                 ifp->if_capenable &= ~IFCAP_HWCSUM;
802                                 ifp->if_hwassist = 0;
803                         } else {
804                                 ifp->if_capenable |= IFCAP_HWCSUM;
805                                 ifp->if_hwassist = NFE_CSUM_FEATURES;
806                         }
807
808                         if (ifp->if_flags & IFF_RUNNING)
809                                 nfe_init(sc);
810                 }
811                 break;
812         default:
813                 error = ether_ioctl(ifp, cmd, data);
814                 break;
815         }
816         return error;
817 }
818
819 static void
820 nfe_rxeof(struct nfe_softc *sc)
821 {
822         struct ifnet *ifp = &sc->arpcom.ac_if;
823         struct nfe_rx_ring *ring = &sc->rxq;
824         int reap;
825
826         reap = 0;
827         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_POSTREAD);
828
829         for (;;) {
830                 struct nfe_rx_data *data = &ring->data[ring->cur];
831                 struct mbuf *m;
832                 uint16_t flags;
833                 int len, error;
834
835                 if (sc->sc_flags & NFE_40BIT_ADDR) {
836                         struct nfe_desc64 *desc64 = &ring->desc64[ring->cur];
837
838                         flags = le16toh(desc64->flags);
839                         len = le16toh(desc64->length) & 0x3fff;
840                 } else {
841                         struct nfe_desc32 *desc32 = &ring->desc32[ring->cur];
842
843                         flags = le16toh(desc32->flags);
844                         len = le16toh(desc32->length) & 0x3fff;
845                 }
846
847                 if (flags & NFE_RX_READY)
848                         break;
849
850                 reap = 1;
851
852                 if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
853                         if (!(flags & NFE_RX_VALID_V1))
854                                 goto skip;
855
856                         if ((flags & NFE_RX_FIXME_V1) == NFE_RX_FIXME_V1) {
857                                 flags &= ~NFE_RX_ERROR;
858                                 len--;  /* fix buffer length */
859                         }
860                 } else {
861                         if (!(flags & NFE_RX_VALID_V2))
862                                 goto skip;
863
864                         if ((flags & NFE_RX_FIXME_V2) == NFE_RX_FIXME_V2) {
865                                 flags &= ~NFE_RX_ERROR;
866                                 len--;  /* fix buffer length */
867                         }
868                 }
869
870                 if (flags & NFE_RX_ERROR) {
871                         ifp->if_ierrors++;
872                         goto skip;
873                 }
874
875                 m = data->m;
876
877                 if (sc->sc_flags & NFE_USE_JUMBO)
878                         error = nfe_newbuf_jumbo(sc, ring, ring->cur, 0);
879                 else
880                         error = nfe_newbuf_std(sc, ring, ring->cur, 0);
881                 if (error) {
882                         ifp->if_ierrors++;
883                         goto skip;
884                 }
885
886                 /* finalize mbuf */
887                 m->m_pkthdr.len = m->m_len = len;
888                 m->m_pkthdr.rcvif = ifp;
889
890                 if ((ifp->if_capenable & IFCAP_HWCSUM) &&
891                     (flags & NFE_RX_CSUMOK)) {
892                         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
893
894                         if (flags & NFE_RX_IP_CSUMOK_V2)
895                                 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
896
897                         if (flags &
898                             (NFE_RX_UDP_CSUMOK_V2 | NFE_RX_TCP_CSUMOK_V2)) {
899                                 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
900                                                           CSUM_PSEUDO_HDR;
901                                 m->m_pkthdr.csum_data = 0xffff;
902                         }
903                 }
904
905                 ifp->if_ipackets++;
906                 ifp->if_input(ifp, m);
907 skip:
908                 nfe_set_ready_rxdesc(sc, ring, ring->cur);
909                 sc->rxq.cur = (sc->rxq.cur + 1) % nfe_rx_ring_count;
910         }
911
912         if (reap)
913                 bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_PREWRITE);
914 }
915
916 static void
917 nfe_txeof(struct nfe_softc *sc)
918 {
919         struct ifnet *ifp = &sc->arpcom.ac_if;
920         struct nfe_tx_ring *ring = &sc->txq;
921         struct nfe_tx_data *data = NULL;
922
923         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_POSTREAD);
924         while (ring->next != ring->cur) {
925                 uint16_t flags;
926
927                 if (sc->sc_flags & NFE_40BIT_ADDR)
928                         flags = le16toh(ring->desc64[ring->next].flags);
929                 else
930                         flags = le16toh(ring->desc32[ring->next].flags);
931
932                 if (flags & NFE_TX_VALID)
933                         break;
934
935                 data = &ring->data[ring->next];
936
937                 if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
938                         if (!(flags & NFE_TX_LASTFRAG_V1) && data->m == NULL)
939                                 goto skip;
940
941                         if ((flags & NFE_TX_ERROR_V1) != 0) {
942                                 if_printf(ifp, "tx v1 error 0x%4b\n", flags,
943                                           NFE_V1_TXERR);
944                                 ifp->if_oerrors++;
945                         } else {
946                                 ifp->if_opackets++;
947                         }
948                 } else {
949                         if (!(flags & NFE_TX_LASTFRAG_V2) && data->m == NULL)
950                                 goto skip;
951
952                         if ((flags & NFE_TX_ERROR_V2) != 0) {
953                                 if_printf(ifp, "tx v2 error 0x%4b\n", flags,
954                                           NFE_V2_TXERR);
955                                 ifp->if_oerrors++;
956                         } else {
957                                 ifp->if_opackets++;
958                         }
959                 }
960
961                 if (data->m == NULL) {  /* should not get there */
962                         if_printf(ifp,
963                                   "last fragment bit w/o associated mbuf!\n");
964                         goto skip;
965                 }
966
967                 /* last fragment of the mbuf chain transmitted */
968                 bus_dmamap_sync(ring->data_tag, data->map,
969                                 BUS_DMASYNC_POSTWRITE);
970                 bus_dmamap_unload(ring->data_tag, data->map);
971                 m_freem(data->m);
972                 data->m = NULL;
973
974                 ifp->if_timer = 0;
975 skip:
976                 ring->queued--;
977                 KKASSERT(ring->queued >= 0);
978                 ring->next = (ring->next + 1) % NFE_TX_RING_COUNT;
979         }
980
981         if (data != NULL) {     /* at least one slot freed */
982                 ifp->if_flags &= ~IFF_OACTIVE;
983                 ifp->if_start(ifp);
984         }
985 }
986
987 static int
988 nfe_encap(struct nfe_softc *sc, struct nfe_tx_ring *ring, struct mbuf *m0)
989 {
990         struct nfe_dma_ctx ctx;
991         bus_dma_segment_t segs[NFE_MAX_SCATTER];
992         struct nfe_tx_data *data, *data_map;
993         bus_dmamap_t map;
994         struct nfe_desc64 *desc64 = NULL;
995         struct nfe_desc32 *desc32 = NULL;
996         uint16_t flags = 0;
997         uint32_t vtag = 0;
998         int error, i, j;
999
1000         data = &ring->data[ring->cur];
1001         map = data->map;
1002         data_map = data;        /* Remember who owns the DMA map */
1003
1004         ctx.nsegs = NFE_MAX_SCATTER;
1005         ctx.segs = segs;
1006         error = bus_dmamap_load_mbuf(ring->data_tag, map, m0,
1007                                      nfe_buf_dma_addr, &ctx, BUS_DMA_NOWAIT);
1008         if (error && error != EFBIG) {
1009                 if_printf(&sc->arpcom.ac_if, "could not map TX mbuf\n");
1010                 goto back;
1011         }
1012
1013         if (error) {    /* error == EFBIG */
1014                 struct mbuf *m_new;
1015
1016                 m_new = m_defrag(m0, MB_DONTWAIT);
1017                 if (m_new == NULL) {
1018                         if_printf(&sc->arpcom.ac_if,
1019                                   "could not defrag TX mbuf\n");
1020                         error = ENOBUFS;
1021                         goto back;
1022                 } else {
1023                         m0 = m_new;
1024                 }
1025
1026                 ctx.nsegs = NFE_MAX_SCATTER;
1027                 ctx.segs = segs;
1028                 error = bus_dmamap_load_mbuf(ring->data_tag, map, m0,
1029                                              nfe_buf_dma_addr, &ctx,
1030                                              BUS_DMA_NOWAIT);
1031                 if (error) {
1032                         if_printf(&sc->arpcom.ac_if,
1033                                   "could not map defraged TX mbuf\n");
1034                         goto back;
1035                 }
1036         }
1037
1038         error = 0;
1039
1040         if (ring->queued + ctx.nsegs >= NFE_TX_RING_COUNT - 1) {
1041                 bus_dmamap_unload(ring->data_tag, map);
1042                 error = ENOBUFS;
1043                 goto back;
1044         }
1045
1046         /* setup h/w VLAN tagging */
1047         if ((m0->m_flags & (M_PROTO1 | M_PKTHDR)) == (M_PROTO1 | M_PKTHDR) &&
1048             m0->m_pkthdr.rcvif != NULL &&
1049             m0->m_pkthdr.rcvif->if_type == IFT_L2VLAN) {
1050                 struct ifvlan *ifv = m0->m_pkthdr.rcvif->if_softc;
1051
1052                 if (ifv != NULL)
1053                         vtag = NFE_TX_VTAG | htons(ifv->ifv_tag);
1054         }
1055
1056         if (sc->arpcom.ac_if.if_capenable & IFCAP_HWCSUM) {
1057                 if (m0->m_pkthdr.csum_flags & CSUM_IP)
1058                         flags |= NFE_TX_IP_CSUM;
1059                 if (m0->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
1060                         flags |= NFE_TX_TCP_CSUM;
1061         }
1062
1063         /*
1064          * XXX urm. somebody is unaware of how hardware works.  You 
1065          * absolutely CANNOT set NFE_TX_VALID on the next descriptor in
1066          * the ring until the entire chain is actually *VALID*.  Otherwise
1067          * the hardware may encounter a partially initialized chain that
1068          * is marked as being ready to go when it in fact is not ready to
1069          * go.
1070          */
1071
1072         for (i = 0; i < ctx.nsegs; i++) {
1073                 j = (ring->cur + i) % NFE_TX_RING_COUNT;
1074                 data = &ring->data[j];
1075
1076                 if (sc->sc_flags & NFE_40BIT_ADDR) {
1077                         desc64 = &ring->desc64[j];
1078 #if defined(__LP64__)
1079                         desc64->physaddr[0] =
1080                             htole32(segs[i].ds_addr >> 32);
1081 #endif
1082                         desc64->physaddr[1] =
1083                             htole32(segs[i].ds_addr & 0xffffffff);
1084                         desc64->length = htole16(segs[i].ds_len - 1);
1085                         desc64->vtag = htole32(vtag);
1086                         desc64->flags = htole16(flags);
1087                 } else {
1088                         desc32 = &ring->desc32[j];
1089                         desc32->physaddr = htole32(segs[i].ds_addr);
1090                         desc32->length = htole16(segs[i].ds_len - 1);
1091                         desc32->flags = htole16(flags);
1092                 }
1093
1094                 /* csum flags and vtag belong to the first fragment only */
1095                 flags &= ~(NFE_TX_IP_CSUM | NFE_TX_TCP_CSUM);
1096                 vtag = 0;
1097
1098                 ring->queued++;
1099                 KKASSERT(ring->queued <= NFE_TX_RING_COUNT);
1100         }
1101
1102         /* the whole mbuf chain has been DMA mapped, fix last descriptor */
1103         if (sc->sc_flags & NFE_40BIT_ADDR) {
1104                 desc64->flags |= htole16(NFE_TX_LASTFRAG_V2);
1105         } else {
1106                 if (sc->sc_flags & NFE_JUMBO_SUP)
1107                         flags = NFE_TX_LASTFRAG_V2;
1108                 else
1109                         flags = NFE_TX_LASTFRAG_V1;
1110                 desc32->flags |= htole16(flags);
1111         }
1112
1113         /*
1114          * Set NFE_TX_VALID backwards so the hardware doesn't see the
1115          * whole mess until the first descriptor in the map is flagged.
1116          */
1117         for (i = ctx.nsegs - 1; i >= 0; --i) {
1118                 j = (ring->cur + i) % NFE_TX_RING_COUNT;
1119                 if (sc->sc_flags & NFE_40BIT_ADDR) {
1120                         desc64 = &ring->desc64[j];
1121                         desc64->flags |= htole16(NFE_TX_VALID);
1122                 } else {
1123                         desc32 = &ring->desc32[j];
1124                         desc32->flags |= htole16(NFE_TX_VALID);
1125                 }
1126         }
1127         ring->cur = (ring->cur + ctx.nsegs) % NFE_TX_RING_COUNT;
1128
1129         /* Exchange DMA map */
1130         data_map->map = data->map;
1131         data->map = map;
1132         data->m = m0;
1133
1134         bus_dmamap_sync(ring->data_tag, map, BUS_DMASYNC_PREWRITE);
1135 back:
1136         if (error)
1137                 m_freem(m0);
1138         return error;
1139 }
1140
1141 static void
1142 nfe_start(struct ifnet *ifp)
1143 {
1144         struct nfe_softc *sc = ifp->if_softc;
1145         struct nfe_tx_ring *ring = &sc->txq;
1146         int count = 0;
1147         struct mbuf *m0;
1148
1149         if (ifp->if_flags & IFF_OACTIVE)
1150                 return;
1151
1152         if (ifq_is_empty(&ifp->if_snd))
1153                 return;
1154
1155         for (;;) {
1156                 m0 = ifq_dequeue(&ifp->if_snd, NULL);
1157                 if (m0 == NULL)
1158                         break;
1159
1160                 BPF_MTAP(ifp, m0);
1161
1162                 if (nfe_encap(sc, ring, m0) != 0) {
1163                         ifp->if_flags |= IFF_OACTIVE;
1164                         break;
1165                 }
1166                 ++count;
1167
1168                 /*
1169                  * NOTE:
1170                  * `m0' may be freed in nfe_encap(), so
1171                  * it should not be touched any more.
1172                  */
1173         }
1174         if (count == 0) /* nothing sent */
1175                 return;
1176
1177         /* Sync TX descriptor ring */
1178         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_PREWRITE);
1179
1180         /* Kick Tx */
1181         NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl);
1182
1183         /*
1184          * Set a timeout in case the chip goes out to lunch.
1185          */
1186         ifp->if_timer = 5;
1187 }
1188
1189 static void
1190 nfe_watchdog(struct ifnet *ifp)
1191 {
1192         struct nfe_softc *sc = ifp->if_softc;
1193
1194         if (ifp->if_flags & IFF_RUNNING) {
1195                 if_printf(ifp, "watchdog timeout - lost interrupt recovered\n");
1196                 nfe_txeof(sc);
1197                 return;
1198         }
1199
1200         if_printf(ifp, "watchdog timeout\n");
1201
1202         nfe_init(ifp->if_softc);
1203
1204         ifp->if_oerrors++;
1205 }
1206
1207 static void
1208 nfe_init(void *xsc)
1209 {
1210         struct nfe_softc *sc = xsc;
1211         struct ifnet *ifp = &sc->arpcom.ac_if;
1212         uint32_t tmp;
1213         int error;
1214
1215         nfe_stop(sc);
1216
1217         /*
1218          * NOTE:
1219          * Switching between jumbo frames and normal frames should
1220          * be done _after_ nfe_stop() but _before_ nfe_init_rx_ring().
1221          */
1222         if (ifp->if_mtu > ETHERMTU) {
1223                 sc->sc_flags |= NFE_USE_JUMBO;
1224                 sc->rxq.bufsz = NFE_JBYTES;
1225                 if (bootverbose)
1226                         if_printf(ifp, "use jumbo frames\n");
1227         } else {
1228                 sc->sc_flags &= ~NFE_USE_JUMBO;
1229                 sc->rxq.bufsz = MCLBYTES;
1230                 if (bootverbose)
1231                         if_printf(ifp, "use non-jumbo frames\n");
1232         }
1233
1234         error = nfe_init_tx_ring(sc, &sc->txq);
1235         if (error) {
1236                 nfe_stop(sc);
1237                 return;
1238         }
1239
1240         error = nfe_init_rx_ring(sc, &sc->rxq);
1241         if (error) {
1242                 nfe_stop(sc);
1243                 return;
1244         }
1245
1246         NFE_WRITE(sc, NFE_TX_UNK, 0);
1247         NFE_WRITE(sc, NFE_STATUS, 0);
1248
1249         sc->rxtxctl = NFE_RXTX_BIT2;
1250         if (sc->sc_flags & NFE_40BIT_ADDR)
1251                 sc->rxtxctl |= NFE_RXTX_V3MAGIC;
1252         else if (sc->sc_flags & NFE_JUMBO_SUP)
1253                 sc->rxtxctl |= NFE_RXTX_V2MAGIC;
1254
1255         if (ifp->if_capenable & IFCAP_HWCSUM)
1256                 sc->rxtxctl |= NFE_RXTX_RXCSUM;
1257
1258         /*
1259          * Although the adapter is capable of stripping VLAN tags from received
1260          * frames (NFE_RXTX_VTAG_STRIP), we do not enable this functionality on
1261          * purpose.  This will be done in software by our network stack.
1262          */
1263         if (sc->sc_flags & NFE_HW_VLAN)
1264                 sc->rxtxctl |= NFE_RXTX_VTAG_INSERT;
1265
1266         NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | sc->rxtxctl);
1267         DELAY(10);
1268         NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1269
1270         if (sc->sc_flags & NFE_HW_VLAN)
1271                 NFE_WRITE(sc, NFE_VTAG_CTL, NFE_VTAG_ENABLE);
1272
1273         NFE_WRITE(sc, NFE_SETUP_R6, 0);
1274
1275         /* set MAC address */
1276         nfe_set_macaddr(sc, sc->arpcom.ac_enaddr);
1277
1278         /* tell MAC where rings are in memory */
1279 #ifdef __LP64__
1280         NFE_WRITE(sc, NFE_RX_RING_ADDR_HI, sc->rxq.physaddr >> 32);
1281 #endif
1282         NFE_WRITE(sc, NFE_RX_RING_ADDR_LO, sc->rxq.physaddr & 0xffffffff);
1283 #ifdef __LP64__
1284         NFE_WRITE(sc, NFE_TX_RING_ADDR_HI, sc->txq.physaddr >> 32);
1285 #endif
1286         NFE_WRITE(sc, NFE_TX_RING_ADDR_LO, sc->txq.physaddr & 0xffffffff);
1287
1288         NFE_WRITE(sc, NFE_RING_SIZE,
1289             (nfe_rx_ring_count - 1) << 16 |
1290             (NFE_TX_RING_COUNT - 1));
1291
1292         NFE_WRITE(sc, NFE_RXBUFSZ, sc->rxq.bufsz);
1293
1294         /* force MAC to wakeup */
1295         tmp = NFE_READ(sc, NFE_PWR_STATE);
1296         NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_WAKEUP);
1297         DELAY(10);
1298         tmp = NFE_READ(sc, NFE_PWR_STATE);
1299         NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_VALID);
1300
1301         /*
1302          * NFE_IMTIMER generates a periodic interrupt via NFE_IRQ_TIMER.
1303          * It is unclear how wide the timer is.  Base programming does
1304          * not seem to effect NFE_IRQ_TX_DONE or NFE_IRQ_RX_DONE so
1305          * we don't get any interrupt moderation.  TX moderation is
1306          * possible by using the timer interrupt instead of TX_DONE.
1307          *
1308          * It is unclear whether there are other bits that can be
1309          * set to make the NFE device actually do interrupt moderation
1310          * on the RX side.
1311          *
1312          * For now set a 128uS interval as a placemark, but don't use
1313          * the timer.
1314          */
1315         NFE_WRITE(sc, NFE_IMTIMER, NFE_IM_DEFAULT);
1316
1317         NFE_WRITE(sc, NFE_SETUP_R1, NFE_R1_MAGIC);
1318         NFE_WRITE(sc, NFE_SETUP_R2, NFE_R2_MAGIC);
1319         NFE_WRITE(sc, NFE_SETUP_R6, NFE_R6_MAGIC);
1320
1321         /* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */
1322         NFE_WRITE(sc, NFE_STATUS, sc->mii_phyaddr << 24 | NFE_STATUS_MAGIC);
1323
1324         NFE_WRITE(sc, NFE_SETUP_R4, NFE_R4_MAGIC);
1325         NFE_WRITE(sc, NFE_WOL_CTL, NFE_WOL_MAGIC);
1326
1327         sc->rxtxctl &= ~NFE_RXTX_BIT2;
1328         NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1329         DELAY(10);
1330         NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT1 | sc->rxtxctl);
1331
1332         /* set Rx filter */
1333         nfe_setmulti(sc);
1334
1335         nfe_ifmedia_upd(ifp);
1336
1337         /* enable Rx */
1338         NFE_WRITE(sc, NFE_RX_CTL, NFE_RX_START);
1339
1340         /* enable Tx */
1341         NFE_WRITE(sc, NFE_TX_CTL, NFE_TX_START);
1342
1343         NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1344
1345 #ifdef DEVICE_POLLING
1346         if ((ifp->if_flags & IFF_POLLING) == 0)
1347 #endif
1348         /* enable interrupts */
1349         NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED);
1350
1351         callout_reset(&sc->sc_tick_ch, hz, nfe_tick, sc);
1352
1353         ifp->if_flags |= IFF_RUNNING;
1354         ifp->if_flags &= ~IFF_OACTIVE;
1355
1356         /*
1357          * If we had stuff in the tx ring before its all cleaned out now
1358          * so we are not going to get an interrupt, jump-start any pending
1359          * output.
1360          */
1361         ifp->if_start(ifp);
1362 }
1363
1364 static void
1365 nfe_stop(struct nfe_softc *sc)
1366 {
1367         struct ifnet *ifp = &sc->arpcom.ac_if;
1368
1369         callout_stop(&sc->sc_tick_ch);
1370
1371         ifp->if_timer = 0;
1372         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1373
1374         /*
1375          * Are NFE_TX_CTL and NFE_RX_CTL polled by the chip microcontroller
1376          * or do they directly reset/terminate the DMA hardware?  Nobody
1377          * knows.
1378          *
1379          * Add two delays:
1380          *
1381          * (1) Delay before zeroing out NFE_TX_CTL.  This seems to help a
1382          * watchdog timeout that occurs after a stop/init sequence.  I am
1383          * theorizing that a TX KICK occuring just prior to a reinit (e.g.
1384          * due to dhclient) is queueing an interrupt to the microcontroller
1385          * which gets delayed until after we clear the control registers
1386          * down below, resulting in mass confusion.  TX KICK is clearly
1387          * hardware aided whereas the other bits in the control register
1388          * are more likely to be polled by the microcontroller.
1389          *
1390          * (2) Delay after zeroing out TX and RX CTL registers, under the
1391          * assumption that primary DMA is initiated and terminated by
1392          * the microcontroller and not hardware (and anyway, one can hardly
1393          * expect the DMA engine to just instantly stop!).  We don't want
1394          * to rip the rings out from under it before it has had a chance to
1395          * actually stop!
1396          */
1397         DELAY(1000);
1398
1399         /* Abort Tx */
1400         NFE_WRITE(sc, NFE_TX_CTL, 0);
1401
1402         /* Disable Rx */
1403         NFE_WRITE(sc, NFE_RX_CTL, 0);
1404
1405         /* Disable interrupts */
1406         NFE_WRITE(sc, NFE_IRQ_MASK, 0);
1407
1408         DELAY(1000);
1409
1410         /* Reset Tx and Rx rings */
1411         nfe_reset_tx_ring(sc, &sc->txq);
1412         nfe_reset_rx_ring(sc, &sc->rxq);
1413 }
1414
1415 static int
1416 nfe_alloc_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1417 {
1418         int i, j, error, descsize;
1419         void **desc;
1420
1421         if (sc->sc_flags & NFE_40BIT_ADDR) {
1422                 desc = (void **)&ring->desc64;
1423                 descsize = sizeof(struct nfe_desc64);
1424         } else {
1425                 desc = (void **)&ring->desc32;
1426                 descsize = sizeof(struct nfe_desc32);
1427         }
1428
1429         ring->jbuf = kmalloc(sizeof(struct nfe_jbuf) * NFE_JPOOL_COUNT,
1430                              M_DEVBUF, M_WAITOK | M_ZERO);
1431         ring->data = kmalloc(sizeof(struct nfe_rx_data) * nfe_rx_ring_count,
1432                              M_DEVBUF, M_WAITOK | M_ZERO);
1433
1434         ring->bufsz = MCLBYTES;
1435         ring->cur = ring->next = 0;
1436
1437         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1438                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1439                                    NULL, NULL,
1440                                    nfe_rx_ring_count * descsize, 1,
1441                                    nfe_rx_ring_count * descsize,
1442                                    0, &ring->tag);
1443         if (error) {
1444                 if_printf(&sc->arpcom.ac_if,
1445                           "could not create desc RX DMA tag\n");
1446                 return error;
1447         }
1448
1449         error = bus_dmamem_alloc(ring->tag, desc, BUS_DMA_WAITOK | BUS_DMA_ZERO,
1450                                  &ring->map);
1451         if (error) {
1452                 if_printf(&sc->arpcom.ac_if,
1453                           "could not allocate RX desc DMA memory\n");
1454                 bus_dma_tag_destroy(ring->tag);
1455                 ring->tag = NULL;
1456                 return error;
1457         }
1458
1459         error = bus_dmamap_load(ring->tag, ring->map, *desc,
1460                                 nfe_rx_ring_count * descsize,
1461                                 nfe_ring_dma_addr, &ring->physaddr,
1462                                 BUS_DMA_WAITOK);
1463         if (error) {
1464                 if_printf(&sc->arpcom.ac_if,
1465                           "could not load RX desc DMA map\n");
1466                 bus_dmamem_free(ring->tag, *desc, ring->map);
1467                 bus_dma_tag_destroy(ring->tag);
1468                 ring->tag = NULL;
1469                 return error;
1470         }
1471
1472         if (sc->sc_flags & NFE_JUMBO_SUP) {
1473                 error = nfe_jpool_alloc(sc, ring);
1474                 if (error) {
1475                         if_printf(&sc->arpcom.ac_if,
1476                                   "could not allocate jumbo frames\n");
1477                         return error;
1478                 }
1479         }
1480
1481         error = bus_dma_tag_create(NULL, 1, 0,
1482                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1483                                    NULL, NULL,
1484                                    MCLBYTES, 1, MCLBYTES,
1485                                    0, &ring->data_tag);
1486         if (error) {
1487                 if_printf(&sc->arpcom.ac_if,
1488                           "could not create RX mbuf DMA tag\n");
1489                 return error;
1490         }
1491
1492         /* Create a spare RX mbuf DMA map */
1493         error = bus_dmamap_create(ring->data_tag, 0, &ring->data_tmpmap);
1494         if (error) {
1495                 if_printf(&sc->arpcom.ac_if,
1496                           "could not create spare RX mbuf DMA map\n");
1497                 bus_dma_tag_destroy(ring->data_tag);
1498                 ring->data_tag = NULL;
1499                 return error;
1500         }
1501
1502         for (i = 0; i < nfe_rx_ring_count; i++) {
1503                 error = bus_dmamap_create(ring->data_tag, 0,
1504                                           &ring->data[i].map);
1505                 if (error) {
1506                         if_printf(&sc->arpcom.ac_if,
1507                                   "could not create %dth RX mbuf DMA mapn", i);
1508                         goto fail;
1509                 }
1510         }
1511         return 0;
1512 fail:
1513         for (j = 0; j < i; ++j)
1514                 bus_dmamap_destroy(ring->data_tag, ring->data[i].map);
1515         bus_dmamap_destroy(ring->data_tag, ring->data_tmpmap);
1516         bus_dma_tag_destroy(ring->data_tag);
1517         ring->data_tag = NULL;
1518         return error;
1519 }
1520
1521 static void
1522 nfe_reset_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1523 {
1524         int i;
1525
1526         for (i = 0; i < nfe_rx_ring_count; i++) {
1527                 struct nfe_rx_data *data = &ring->data[i];
1528
1529                 if (data->m != NULL) {
1530                         if ((sc->sc_flags & NFE_USE_JUMBO) == 0)
1531                                 bus_dmamap_unload(ring->data_tag, data->map);
1532                         m_freem(data->m);
1533                         data->m = NULL;
1534                 }
1535         }
1536         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_PREWRITE);
1537
1538         ring->cur = ring->next = 0;
1539 }
1540
1541 static int
1542 nfe_init_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1543 {
1544         int i;
1545
1546         for (i = 0; i < nfe_rx_ring_count; ++i) {
1547                 int error;
1548
1549                 /* XXX should use a function pointer */
1550                 if (sc->sc_flags & NFE_USE_JUMBO)
1551                         error = nfe_newbuf_jumbo(sc, ring, i, 1);
1552                 else
1553                         error = nfe_newbuf_std(sc, ring, i, 1);
1554                 if (error) {
1555                         if_printf(&sc->arpcom.ac_if,
1556                                   "could not allocate RX buffer\n");
1557                         return error;
1558                 }
1559
1560                 nfe_set_ready_rxdesc(sc, ring, i);
1561         }
1562         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_PREWRITE);
1563
1564         return 0;
1565 }
1566
1567 static void
1568 nfe_free_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1569 {
1570         if (ring->data_tag != NULL) {
1571                 struct nfe_rx_data *data;
1572                 int i;
1573
1574                 for (i = 0; i < nfe_rx_ring_count; i++) {
1575                         data = &ring->data[i];
1576
1577                         if (data->m != NULL) {
1578                                 bus_dmamap_unload(ring->data_tag, data->map);
1579                                 m_freem(data->m);
1580                         }
1581                         bus_dmamap_destroy(ring->data_tag, data->map);
1582                 }
1583                 bus_dmamap_destroy(ring->data_tag, ring->data_tmpmap);
1584                 bus_dma_tag_destroy(ring->data_tag);
1585         }
1586
1587         nfe_jpool_free(sc, ring);
1588         
1589         if (ring->jbuf != NULL)
1590                 kfree(ring->jbuf, M_DEVBUF);
1591         if (ring->data != NULL)
1592                 kfree(ring->data, M_DEVBUF);
1593
1594         if (ring->tag != NULL) {
1595                 void *desc;
1596
1597                 if (sc->sc_flags & NFE_40BIT_ADDR)
1598                         desc = ring->desc64;
1599                 else
1600                         desc = ring->desc32;
1601
1602                 bus_dmamap_unload(ring->tag, ring->map);
1603                 bus_dmamem_free(ring->tag, desc, ring->map);
1604                 bus_dma_tag_destroy(ring->tag);
1605         }
1606 }
1607
1608 static struct nfe_jbuf *
1609 nfe_jalloc(struct nfe_softc *sc)
1610 {
1611         struct ifnet *ifp = &sc->arpcom.ac_if;
1612         struct nfe_jbuf *jbuf;
1613
1614         lwkt_serialize_enter(&sc->sc_jbuf_serializer);
1615
1616         jbuf = SLIST_FIRST(&sc->rxq.jfreelist);
1617         if (jbuf != NULL) {
1618                 SLIST_REMOVE_HEAD(&sc->rxq.jfreelist, jnext);
1619                 jbuf->inuse = 1;
1620         } else {
1621                 if_printf(ifp, "no free jumbo buffer\n");
1622         }
1623
1624         lwkt_serialize_exit(&sc->sc_jbuf_serializer);
1625
1626         return jbuf;
1627 }
1628
1629 static void
1630 nfe_jfree(void *arg)
1631 {
1632         struct nfe_jbuf *jbuf = arg;
1633         struct nfe_softc *sc = jbuf->sc;
1634         struct nfe_rx_ring *ring = jbuf->ring;
1635
1636         if (&ring->jbuf[jbuf->slot] != jbuf)
1637                 panic("%s: free wrong jumbo buffer\n", __func__);
1638         else if (jbuf->inuse == 0)
1639                 panic("%s: jumbo buffer already freed\n", __func__);
1640
1641         lwkt_serialize_enter(&sc->sc_jbuf_serializer);
1642         atomic_subtract_int(&jbuf->inuse, 1);
1643         if (jbuf->inuse == 0)
1644                 SLIST_INSERT_HEAD(&ring->jfreelist, jbuf, jnext);
1645         lwkt_serialize_exit(&sc->sc_jbuf_serializer);
1646 }
1647
1648 static void
1649 nfe_jref(void *arg)
1650 {
1651         struct nfe_jbuf *jbuf = arg;
1652         struct nfe_rx_ring *ring = jbuf->ring;
1653
1654         if (&ring->jbuf[jbuf->slot] != jbuf)
1655                 panic("%s: ref wrong jumbo buffer\n", __func__);
1656         else if (jbuf->inuse == 0)
1657                 panic("%s: jumbo buffer already freed\n", __func__);
1658
1659         atomic_add_int(&jbuf->inuse, 1);
1660 }
1661
1662 static int
1663 nfe_jpool_alloc(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1664 {
1665         struct nfe_jbuf *jbuf;
1666         bus_addr_t physaddr;
1667         caddr_t buf;
1668         int i, error;
1669
1670         /*
1671          * Allocate a big chunk of DMA'able memory.
1672          */
1673         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1674                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1675                                    NULL, NULL,
1676                                    NFE_JPOOL_SIZE, 1, NFE_JPOOL_SIZE,
1677                                    0, &ring->jtag);
1678         if (error) {
1679                 if_printf(&sc->arpcom.ac_if,
1680                           "could not create jumbo DMA tag\n");
1681                 return error;
1682         }
1683
1684         error = bus_dmamem_alloc(ring->jtag, (void **)&ring->jpool,
1685                                  BUS_DMA_WAITOK, &ring->jmap);
1686         if (error) {
1687                 if_printf(&sc->arpcom.ac_if,
1688                           "could not allocate jumbo DMA memory\n");
1689                 bus_dma_tag_destroy(ring->jtag);
1690                 ring->jtag = NULL;
1691                 return error;
1692         }
1693
1694         error = bus_dmamap_load(ring->jtag, ring->jmap, ring->jpool,
1695                                 NFE_JPOOL_SIZE, nfe_ring_dma_addr, &physaddr,
1696                                 BUS_DMA_WAITOK);
1697         if (error) {
1698                 if_printf(&sc->arpcom.ac_if,
1699                           "could not load jumbo DMA map\n");
1700                 bus_dmamem_free(ring->jtag, ring->jpool, ring->jmap);
1701                 bus_dma_tag_destroy(ring->jtag);
1702                 ring->jtag = NULL;
1703                 return error;
1704         }
1705
1706         /* ..and split it into 9KB chunks */
1707         SLIST_INIT(&ring->jfreelist);
1708
1709         buf = ring->jpool;
1710         for (i = 0; i < NFE_JPOOL_COUNT; i++) {
1711                 jbuf = &ring->jbuf[i];
1712
1713                 jbuf->sc = sc;
1714                 jbuf->ring = ring;
1715                 jbuf->inuse = 0;
1716                 jbuf->slot = i;
1717                 jbuf->buf = buf;
1718                 jbuf->physaddr = physaddr;
1719
1720                 SLIST_INSERT_HEAD(&ring->jfreelist, jbuf, jnext);
1721
1722                 buf += NFE_JBYTES;
1723                 physaddr += NFE_JBYTES;
1724         }
1725
1726         return 0;
1727 }
1728
1729 static void
1730 nfe_jpool_free(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1731 {
1732         if (ring->jtag != NULL) {
1733                 bus_dmamap_unload(ring->jtag, ring->jmap);
1734                 bus_dmamem_free(ring->jtag, ring->jpool, ring->jmap);
1735                 bus_dma_tag_destroy(ring->jtag);
1736         }
1737 }
1738
1739 static int
1740 nfe_alloc_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1741 {
1742         int i, j, error, descsize;
1743         void **desc;
1744
1745         if (sc->sc_flags & NFE_40BIT_ADDR) {
1746                 desc = (void **)&ring->desc64;
1747                 descsize = sizeof(struct nfe_desc64);
1748         } else {
1749                 desc = (void **)&ring->desc32;
1750                 descsize = sizeof(struct nfe_desc32);
1751         }
1752
1753         ring->queued = 0;
1754         ring->cur = ring->next = 0;
1755
1756         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1757                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1758                                    NULL, NULL,
1759                                    NFE_TX_RING_COUNT * descsize, 1,
1760                                    NFE_TX_RING_COUNT * descsize,
1761                                    0, &ring->tag);
1762         if (error) {
1763                 if_printf(&sc->arpcom.ac_if,
1764                           "could not create TX desc DMA map\n");
1765                 return error;
1766         }
1767
1768         error = bus_dmamem_alloc(ring->tag, desc, BUS_DMA_WAITOK | BUS_DMA_ZERO,
1769                                  &ring->map);
1770         if (error) {
1771                 if_printf(&sc->arpcom.ac_if,
1772                           "could not allocate TX desc DMA memory\n");
1773                 bus_dma_tag_destroy(ring->tag);
1774                 ring->tag = NULL;
1775                 return error;
1776         }
1777
1778         error = bus_dmamap_load(ring->tag, ring->map, *desc,
1779                                 NFE_TX_RING_COUNT * descsize,
1780                                 nfe_ring_dma_addr, &ring->physaddr,
1781                                 BUS_DMA_WAITOK);
1782         if (error) {
1783                 if_printf(&sc->arpcom.ac_if,
1784                           "could not load TX desc DMA map\n");
1785                 bus_dmamem_free(ring->tag, *desc, ring->map);
1786                 bus_dma_tag_destroy(ring->tag);
1787                 ring->tag = NULL;
1788                 return error;
1789         }
1790
1791         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1792                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1793                                    NULL, NULL,
1794                                    NFE_JBYTES * NFE_MAX_SCATTER,
1795                                    NFE_MAX_SCATTER, NFE_JBYTES,
1796                                    0, &ring->data_tag);
1797         if (error) {
1798                 if_printf(&sc->arpcom.ac_if,
1799                           "could not create TX buf DMA tag\n");
1800                 return error;
1801         }
1802
1803         for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1804                 error = bus_dmamap_create(ring->data_tag, 0,
1805                                           &ring->data[i].map);
1806                 if (error) {
1807                         if_printf(&sc->arpcom.ac_if,
1808                                   "could not create %dth TX buf DMA map\n", i);
1809                         goto fail;
1810                 }
1811         }
1812
1813         return 0;
1814 fail:
1815         for (j = 0; j < i; ++j)
1816                 bus_dmamap_destroy(ring->data_tag, ring->data[i].map);
1817         bus_dma_tag_destroy(ring->data_tag);
1818         ring->data_tag = NULL;
1819         return error;
1820 }
1821
1822 static void
1823 nfe_reset_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1824 {
1825         int i;
1826
1827         for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1828                 struct nfe_tx_data *data = &ring->data[i];
1829
1830                 if (sc->sc_flags & NFE_40BIT_ADDR)
1831                         ring->desc64[i].flags = 0;
1832                 else
1833                         ring->desc32[i].flags = 0;
1834
1835                 if (data->m != NULL) {
1836                         bus_dmamap_sync(ring->data_tag, data->map,
1837                                         BUS_DMASYNC_POSTWRITE);
1838                         bus_dmamap_unload(ring->data_tag, data->map);
1839                         m_freem(data->m);
1840                         data->m = NULL;
1841                 }
1842         }
1843         bus_dmamap_sync(ring->tag, ring->map, BUS_DMASYNC_PREWRITE);
1844
1845         ring->queued = 0;
1846         ring->cur = ring->next = 0;
1847 }
1848
1849 static int
1850 nfe_init_tx_ring(struct nfe_softc *sc __unused,
1851                  struct nfe_tx_ring *ring __unused)
1852 {
1853         return 0;
1854 }
1855
1856 static void
1857 nfe_free_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1858 {
1859         if (ring->data_tag != NULL) {
1860                 struct nfe_tx_data *data;
1861                 int i;
1862
1863                 for (i = 0; i < NFE_TX_RING_COUNT; ++i) {
1864                         data = &ring->data[i];
1865
1866                         if (data->m != NULL) {
1867                                 bus_dmamap_unload(ring->data_tag, data->map);
1868                                 m_freem(data->m);
1869                         }
1870                         bus_dmamap_destroy(ring->data_tag, data->map);
1871                 }
1872
1873                 bus_dma_tag_destroy(ring->data_tag);
1874         }
1875
1876         if (ring->tag != NULL) {
1877                 void *desc;
1878
1879                 if (sc->sc_flags & NFE_40BIT_ADDR)
1880                         desc = ring->desc64;
1881                 else
1882                         desc = ring->desc32;
1883
1884                 bus_dmamap_unload(ring->tag, ring->map);
1885                 bus_dmamem_free(ring->tag, desc, ring->map);
1886                 bus_dma_tag_destroy(ring->tag);
1887         }
1888 }
1889
1890 static int
1891 nfe_ifmedia_upd(struct ifnet *ifp)
1892 {
1893         struct nfe_softc *sc = ifp->if_softc;
1894         struct mii_data *mii = device_get_softc(sc->sc_miibus);
1895
1896         if (mii->mii_instance != 0) {
1897                 struct mii_softc *miisc;
1898
1899                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1900                         mii_phy_reset(miisc);
1901         }
1902         mii_mediachg(mii);
1903
1904         return 0;
1905 }
1906
1907 static void
1908 nfe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1909 {
1910         struct nfe_softc *sc = ifp->if_softc;
1911         struct mii_data *mii = device_get_softc(sc->sc_miibus);
1912
1913         mii_pollstat(mii);
1914         ifmr->ifm_status = mii->mii_media_status;
1915         ifmr->ifm_active = mii->mii_media_active;
1916 }
1917
1918 static void
1919 nfe_setmulti(struct nfe_softc *sc)
1920 {
1921         struct ifnet *ifp = &sc->arpcom.ac_if;
1922         struct ifmultiaddr *ifma;
1923         uint8_t addr[ETHER_ADDR_LEN], mask[ETHER_ADDR_LEN];
1924         uint32_t filter = NFE_RXFILTER_MAGIC;
1925         int i;
1926
1927         if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1928                 bzero(addr, ETHER_ADDR_LEN);
1929                 bzero(mask, ETHER_ADDR_LEN);
1930                 goto done;
1931         }
1932
1933         bcopy(etherbroadcastaddr, addr, ETHER_ADDR_LEN);
1934         bcopy(etherbroadcastaddr, mask, ETHER_ADDR_LEN);
1935
1936         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1937                 caddr_t maddr;
1938
1939                 if (ifma->ifma_addr->sa_family != AF_LINK)
1940                         continue;
1941
1942                 maddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
1943                 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1944                         addr[i] &= maddr[i];
1945                         mask[i] &= ~maddr[i];
1946                 }
1947         }
1948
1949         for (i = 0; i < ETHER_ADDR_LEN; i++)
1950                 mask[i] |= addr[i];
1951
1952 done:
1953         addr[0] |= 0x01;        /* make sure multicast bit is set */
1954
1955         NFE_WRITE(sc, NFE_MULTIADDR_HI,
1956             addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1957         NFE_WRITE(sc, NFE_MULTIADDR_LO,
1958             addr[5] <<  8 | addr[4]);
1959         NFE_WRITE(sc, NFE_MULTIMASK_HI,
1960             mask[3] << 24 | mask[2] << 16 | mask[1] << 8 | mask[0]);
1961         NFE_WRITE(sc, NFE_MULTIMASK_LO,
1962             mask[5] <<  8 | mask[4]);
1963
1964         filter |= (ifp->if_flags & IFF_PROMISC) ? NFE_PROMISC : NFE_U2M;
1965         NFE_WRITE(sc, NFE_RXFILTER, filter);
1966 }
1967
1968 static void
1969 nfe_get_macaddr(struct nfe_softc *sc, uint8_t *addr)
1970 {
1971         uint32_t tmp;
1972
1973         tmp = NFE_READ(sc, NFE_MACADDR_LO);
1974         addr[0] = (tmp >> 8) & 0xff;
1975         addr[1] = (tmp & 0xff);
1976
1977         tmp = NFE_READ(sc, NFE_MACADDR_HI);
1978         addr[2] = (tmp >> 24) & 0xff;
1979         addr[3] = (tmp >> 16) & 0xff;
1980         addr[4] = (tmp >>  8) & 0xff;
1981         addr[5] = (tmp & 0xff);
1982 }
1983
1984 static void
1985 nfe_set_macaddr(struct nfe_softc *sc, const uint8_t *addr)
1986 {
1987         NFE_WRITE(sc, NFE_MACADDR_LO,
1988             addr[5] <<  8 | addr[4]);
1989         NFE_WRITE(sc, NFE_MACADDR_HI,
1990             addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1991 }
1992
1993 static void
1994 nfe_tick(void *arg)
1995 {
1996         struct nfe_softc *sc = arg;
1997         struct ifnet *ifp = &sc->arpcom.ac_if;
1998         struct mii_data *mii = device_get_softc(sc->sc_miibus);
1999
2000         lwkt_serialize_enter(ifp->if_serializer);
2001
2002         mii_tick(mii);
2003         callout_reset(&sc->sc_tick_ch, hz, nfe_tick, sc);
2004
2005         lwkt_serialize_exit(ifp->if_serializer);
2006 }
2007
2008 static void
2009 nfe_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2010 {
2011         if (error)
2012                 return;
2013
2014         KASSERT(nseg == 1, ("too many segments, should be 1\n"));
2015
2016         *((uint32_t *)arg) = seg->ds_addr;
2017 }
2018
2019 static void
2020 nfe_buf_dma_addr(void *arg, bus_dma_segment_t *segs, int nsegs,
2021                  bus_size_t mapsz __unused, int error)
2022 {
2023         struct nfe_dma_ctx *ctx = arg;
2024         int i;
2025
2026         if (error)
2027                 return;
2028
2029         KASSERT(nsegs <= ctx->nsegs,
2030                 ("too many segments(%d), should be <= %d\n",
2031                  nsegs, ctx->nsegs));
2032
2033         ctx->nsegs = nsegs;
2034         for (i = 0; i < nsegs; ++i)
2035                 ctx->segs[i] = segs[i];
2036 }
2037
2038 static int
2039 nfe_newbuf_std(struct nfe_softc *sc, struct nfe_rx_ring *ring, int idx,
2040                int wait)
2041 {
2042         struct nfe_rx_data *data = &ring->data[idx];
2043         struct nfe_dma_ctx ctx;
2044         bus_dma_segment_t seg;
2045         bus_dmamap_t map;
2046         struct mbuf *m;
2047         int error;
2048
2049         m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2050         if (m == NULL)
2051                 return ENOBUFS;
2052         m->m_len = m->m_pkthdr.len = MCLBYTES;
2053
2054         ctx.nsegs = 1;
2055         ctx.segs = &seg;
2056         error = bus_dmamap_load_mbuf(ring->data_tag, ring->data_tmpmap,
2057                                      m, nfe_buf_dma_addr, &ctx,
2058                                      wait ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2059         if (error) {
2060                 m_freem(m);
2061                 if_printf(&sc->arpcom.ac_if, "could map RX mbuf %d\n", error);
2062                 return error;
2063         }
2064
2065         /* Unload originally mapped mbuf */
2066         bus_dmamap_unload(ring->data_tag, data->map);
2067
2068         /* Swap this DMA map with tmp DMA map */
2069         map = data->map;
2070         data->map = ring->data_tmpmap;
2071         ring->data_tmpmap = map;
2072
2073         /* Caller is assumed to have collected the old mbuf */
2074         data->m = m;
2075
2076         nfe_set_paddr_rxdesc(sc, ring, idx, seg.ds_addr);
2077
2078         bus_dmamap_sync(ring->data_tag, data->map, BUS_DMASYNC_PREREAD);
2079         return 0;
2080 }
2081
2082 static int
2083 nfe_newbuf_jumbo(struct nfe_softc *sc, struct nfe_rx_ring *ring, int idx,
2084                  int wait)
2085 {
2086         struct nfe_rx_data *data = &ring->data[idx];
2087         struct nfe_jbuf *jbuf;
2088         struct mbuf *m;
2089
2090         MGETHDR(m, wait ? MB_WAIT : MB_DONTWAIT, MT_DATA);
2091         if (m == NULL)
2092                 return ENOBUFS;
2093
2094         jbuf = nfe_jalloc(sc);
2095         if (jbuf == NULL) {
2096                 m_freem(m);
2097                 if_printf(&sc->arpcom.ac_if, "jumbo allocation failed "
2098                     "-- packet dropped!\n");
2099                 return ENOBUFS;
2100         }
2101
2102         m->m_ext.ext_arg = jbuf;
2103         m->m_ext.ext_buf = jbuf->buf;
2104         m->m_ext.ext_free = nfe_jfree;
2105         m->m_ext.ext_ref = nfe_jref;
2106         m->m_ext.ext_size = NFE_JBYTES;
2107
2108         m->m_data = m->m_ext.ext_buf;
2109         m->m_flags |= M_EXT;
2110         m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
2111
2112         /* Caller is assumed to have collected the old mbuf */
2113         data->m = m;
2114
2115         nfe_set_paddr_rxdesc(sc, ring, idx, jbuf->physaddr);
2116
2117         bus_dmamap_sync(ring->jtag, ring->jmap, BUS_DMASYNC_PREREAD);
2118         return 0;
2119 }
2120
2121 static void
2122 nfe_set_paddr_rxdesc(struct nfe_softc *sc, struct nfe_rx_ring *ring, int idx,
2123                      bus_addr_t physaddr)
2124 {
2125         if (sc->sc_flags & NFE_40BIT_ADDR) {
2126                 struct nfe_desc64 *desc64 = &ring->desc64[idx];
2127
2128 #if defined(__LP64__)
2129                 desc64->physaddr[0] = htole32(physaddr >> 32);
2130 #endif
2131                 desc64->physaddr[1] = htole32(physaddr & 0xffffffff);
2132         } else {
2133                 struct nfe_desc32 *desc32 = &ring->desc32[idx];
2134
2135                 desc32->physaddr = htole32(physaddr);
2136         }
2137 }
2138
2139 static void
2140 nfe_set_ready_rxdesc(struct nfe_softc *sc, struct nfe_rx_ring *ring, int idx)
2141 {
2142         if (sc->sc_flags & NFE_40BIT_ADDR) {
2143                 struct nfe_desc64 *desc64 = &ring->desc64[idx];
2144
2145                 desc64->length = htole16(ring->bufsz);
2146                 desc64->flags = htole16(NFE_RX_READY);
2147         } else {
2148                 struct nfe_desc32 *desc32 = &ring->desc32[idx];
2149
2150                 desc32->length = htole16(ring->bufsz);
2151                 desc32->flags = htole16(NFE_RX_READY);
2152         }
2153 }