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