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