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