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.18 2008/03/10 10:47:57 sephe Exp $ */
5 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.
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
40 * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
41 * Copyright (c) 2005, 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
56 /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */
58 #include "opt_polling.h"
60 #include <sys/param.h>
61 #include <sys/endian.h>
62 #include <sys/kernel.h>
66 #include <sys/serialize.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69 #include <sys/sysctl.h>
71 #include <net/ethernet.h>
74 #include <net/if_arp.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/ifq_var.h>
78 #include <net/if_types.h>
79 #include <net/if_var.h>
80 #include <net/vlan/if_vlan_var.h>
82 #include <bus/pci/pcireg.h>
83 #include <bus/pci/pcivar.h>
84 #include <bus/pci/pcidevs.h>
86 #include <dev/netif/mii_layer/mii.h>
87 #include <dev/netif/mii_layer/miivar.h>
89 #include "miibus_if.h"
91 #include <dev/netif/nfe/if_nfereg.h>
92 #include <dev/netif/nfe/if_nfevar.h>
95 #define NFE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
97 static int nfe_probe(device_t
);
98 static int nfe_attach(device_t
);
99 static int nfe_detach(device_t
);
100 static void nfe_shutdown(device_t
);
101 static int nfe_resume(device_t
);
102 static int nfe_suspend(device_t
);
104 static int nfe_miibus_readreg(device_t
, int, int);
105 static void nfe_miibus_writereg(device_t
, int, int, int);
106 static void nfe_miibus_statchg(device_t
);
108 #ifdef DEVICE_POLLING
109 static void nfe_poll(struct ifnet
*, enum poll_cmd
, int);
111 static void nfe_intr(void *);
112 static int nfe_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
113 static void nfe_rxeof(struct nfe_softc
*);
114 static void nfe_txeof(struct nfe_softc
*);
115 static int nfe_encap(struct nfe_softc
*, struct nfe_tx_ring
*,
117 static void nfe_start(struct ifnet
*);
118 static void nfe_watchdog(struct ifnet
*);
119 static void nfe_init(void *);
120 static void nfe_stop(struct nfe_softc
*);
121 static struct nfe_jbuf
*nfe_jalloc(struct nfe_softc
*);
122 static void nfe_jfree(void *);
123 static void nfe_jref(void *);
124 static int nfe_jpool_alloc(struct nfe_softc
*, struct nfe_rx_ring
*);
125 static void nfe_jpool_free(struct nfe_softc
*, struct nfe_rx_ring
*);
126 static int nfe_alloc_rx_ring(struct nfe_softc
*, struct nfe_rx_ring
*);
127 static void nfe_reset_rx_ring(struct nfe_softc
*, struct nfe_rx_ring
*);
128 static int nfe_init_rx_ring(struct nfe_softc
*, struct nfe_rx_ring
*);
129 static void nfe_free_rx_ring(struct nfe_softc
*, struct nfe_rx_ring
*);
130 static int nfe_alloc_tx_ring(struct nfe_softc
*, struct nfe_tx_ring
*);
131 static void nfe_reset_tx_ring(struct nfe_softc
*, struct nfe_tx_ring
*);
132 static int nfe_init_tx_ring(struct nfe_softc
*, struct nfe_tx_ring
*);
133 static void nfe_free_tx_ring(struct nfe_softc
*, struct nfe_tx_ring
*);
134 static int nfe_ifmedia_upd(struct ifnet
*);
135 static void nfe_ifmedia_sts(struct ifnet
*, struct ifmediareq
*);
136 static void nfe_setmulti(struct nfe_softc
*);
137 static void nfe_get_macaddr(struct nfe_softc
*, uint8_t *);
138 static void nfe_set_macaddr(struct nfe_softc
*, const uint8_t *);
139 static void nfe_tick(void *);
140 static void nfe_ring_dma_addr(void *, bus_dma_segment_t
*, int, int);
141 static void nfe_buf_dma_addr(void *, bus_dma_segment_t
*, int, bus_size_t
,
143 static void nfe_set_paddr_rxdesc(struct nfe_softc
*, struct nfe_rx_ring
*,
145 static void nfe_set_ready_rxdesc(struct nfe_softc
*, struct nfe_rx_ring
*,
147 static int nfe_newbuf_std(struct nfe_softc
*, struct nfe_rx_ring
*, int,
149 static int nfe_newbuf_jumbo(struct nfe_softc
*, struct nfe_rx_ring
*, int,
152 static int nfe_sysctl_imtime(SYSCTL_HANDLER_ARGS
);
157 static int nfe_debug
= 0;
158 static int nfe_rx_ring_count
= NFE_RX_RING_DEF_COUNT
;
159 static int nfe_imtime
= -1;
161 TUNABLE_INT("hw.nfe.rx_ring_count", &nfe_rx_ring_count
);
162 TUNABLE_INT("hw.nfe.imtime", &nfe_imtime
);
163 TUNABLE_INT("hw.nfe.debug", &nfe_debug
);
165 #define DPRINTF(sc, fmt, ...) do { \
166 if ((sc)->sc_debug) { \
167 if_printf(&(sc)->arpcom.ac_if, \
172 #define DPRINTFN(sc, lv, fmt, ...) do { \
173 if ((sc)->sc_debug >= (lv)) { \
174 if_printf(&(sc)->arpcom.ac_if, \
179 #else /* !NFE_DEBUG */
181 #define DPRINTF(sc, fmt, ...)
182 #define DPRINTFN(sc, lv, fmt, ...)
184 #endif /* NFE_DEBUG */
188 bus_dma_segment_t
*segs
;
191 static const struct nfe_dev
{
196 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE_LAN
,
197 "NVIDIA nForce Fast Ethernet" },
199 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE2_LAN
,
200 "NVIDIA nForce2 Fast Ethernet" },
202 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE3_LAN1
,
203 "NVIDIA nForce3 Gigabit Ethernet" },
205 /* XXX TGEN the next chip can also be found in the nForce2 Ultra 400Gb
206 chipset, and possibly also the 400R; it might be both nForce2- and
207 nForce3-based boards can use the same MCPs (= southbridges) */
208 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE3_LAN2
,
209 "NVIDIA nForce3 Gigabit Ethernet" },
211 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE3_LAN3
,
212 "NVIDIA nForce3 Gigabit Ethernet" },
214 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE3_LAN4
,
215 "NVIDIA nForce3 Gigabit Ethernet" },
217 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_NFORCE3_LAN5
,
218 "NVIDIA nForce3 Gigabit Ethernet" },
220 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_CK804_LAN1
,
221 "NVIDIA CK804 Gigabit Ethernet" },
223 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_CK804_LAN2
,
224 "NVIDIA CK804 Gigabit Ethernet" },
226 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP04_LAN1
,
227 "NVIDIA MCP04 Gigabit Ethernet" },
229 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP04_LAN2
,
230 "NVIDIA MCP04 Gigabit Ethernet" },
232 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP51_LAN1
,
233 "NVIDIA MCP51 Gigabit Ethernet" },
235 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP51_LAN2
,
236 "NVIDIA MCP51 Gigabit Ethernet" },
238 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP55_LAN1
,
239 "NVIDIA MCP55 Gigabit Ethernet" },
241 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP55_LAN2
,
242 "NVIDIA MCP55 Gigabit Ethernet" },
244 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP61_LAN1
,
245 "NVIDIA MCP61 Gigabit Ethernet" },
247 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP61_LAN2
,
248 "NVIDIA MCP61 Gigabit Ethernet" },
250 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP61_LAN3
,
251 "NVIDIA MCP61 Gigabit Ethernet" },
253 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP61_LAN4
,
254 "NVIDIA MCP61 Gigabit Ethernet" },
256 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP65_LAN1
,
257 "NVIDIA MCP65 Gigabit Ethernet" },
259 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP65_LAN2
,
260 "NVIDIA MCP65 Gigabit Ethernet" },
262 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP65_LAN3
,
263 "NVIDIA MCP65 Gigabit Ethernet" },
265 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP65_LAN4
,
266 "NVIDIA MCP65 Gigabit Ethernet" },
268 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP67_LAN1
,
269 "NVIDIA MCP67 Gigabit Ethernet" },
271 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP67_LAN2
,
272 "NVIDIA MCP67 Gigabit Ethernet" },
274 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP67_LAN3
,
275 "NVIDIA MCP67 Gigabit Ethernet" },
277 { PCI_VENDOR_NVIDIA
, PCI_PRODUCT_NVIDIA_MCP67_LAN4
,
278 "NVIDIA MCP67 Gigabit Ethernet" }
281 static device_method_t nfe_methods
[] = {
282 /* Device interface */
283 DEVMETHOD(device_probe
, nfe_probe
),
284 DEVMETHOD(device_attach
, nfe_attach
),
285 DEVMETHOD(device_detach
, nfe_detach
),
286 DEVMETHOD(device_suspend
, nfe_suspend
),
287 DEVMETHOD(device_resume
, nfe_resume
),
288 DEVMETHOD(device_shutdown
, nfe_shutdown
),
291 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
292 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
295 DEVMETHOD(miibus_readreg
, nfe_miibus_readreg
),
296 DEVMETHOD(miibus_writereg
, nfe_miibus_writereg
),
297 DEVMETHOD(miibus_statchg
, nfe_miibus_statchg
),
302 static driver_t nfe_driver
= {
305 sizeof(struct nfe_softc
)
308 static devclass_t nfe_devclass
;
310 DECLARE_DUMMY_MODULE(if_nfe
);
311 MODULE_DEPEND(if_nfe
, miibus
, 1, 1, 1);
312 DRIVER_MODULE(if_nfe
, pci
, nfe_driver
, nfe_devclass
, 0, 0);
313 DRIVER_MODULE(miibus
, nfe
, miibus_driver
, miibus_devclass
, 0, 0);
316 nfe_probe(device_t dev
)
318 const struct nfe_dev
*n
;
321 vid
= pci_get_vendor(dev
);
322 did
= pci_get_device(dev
);
323 for (n
= nfe_devices
; n
->desc
!= NULL
; ++n
) {
324 if (vid
== n
->vid
&& did
== n
->did
) {
325 struct nfe_softc
*sc
= device_get_softc(dev
);
328 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN2
:
329 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN3
:
330 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN4
:
331 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN5
:
332 sc
->sc_flags
= NFE_JUMBO_SUP
|
335 case PCI_PRODUCT_NVIDIA_MCP51_LAN1
:
336 case PCI_PRODUCT_NVIDIA_MCP51_LAN2
:
337 case PCI_PRODUCT_NVIDIA_MCP61_LAN1
:
338 case PCI_PRODUCT_NVIDIA_MCP61_LAN2
:
339 case PCI_PRODUCT_NVIDIA_MCP61_LAN3
:
340 case PCI_PRODUCT_NVIDIA_MCP61_LAN4
:
341 case PCI_PRODUCT_NVIDIA_MCP67_LAN1
:
342 case PCI_PRODUCT_NVIDIA_MCP67_LAN2
:
343 case PCI_PRODUCT_NVIDIA_MCP67_LAN3
:
344 case PCI_PRODUCT_NVIDIA_MCP67_LAN4
:
345 sc
->sc_flags
= NFE_40BIT_ADDR
;
347 case PCI_PRODUCT_NVIDIA_CK804_LAN1
:
348 case PCI_PRODUCT_NVIDIA_CK804_LAN2
:
349 case PCI_PRODUCT_NVIDIA_MCP04_LAN1
:
350 case PCI_PRODUCT_NVIDIA_MCP04_LAN2
:
351 case PCI_PRODUCT_NVIDIA_MCP65_LAN1
:
352 case PCI_PRODUCT_NVIDIA_MCP65_LAN2
:
353 case PCI_PRODUCT_NVIDIA_MCP65_LAN3
:
354 case PCI_PRODUCT_NVIDIA_MCP65_LAN4
:
355 sc
->sc_flags
= NFE_JUMBO_SUP
|
359 case PCI_PRODUCT_NVIDIA_MCP55_LAN1
:
360 case PCI_PRODUCT_NVIDIA_MCP55_LAN2
:
361 sc
->sc_flags
= NFE_JUMBO_SUP
|
368 device_set_desc(dev
, n
->desc
);
369 device_set_async_attach(dev
, TRUE
);
377 nfe_attach(device_t dev
)
379 struct nfe_softc
*sc
= device_get_softc(dev
);
380 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
381 uint8_t eaddr
[ETHER_ADDR_LEN
];
384 if_initname(ifp
, device_get_name(dev
), device_get_unit(dev
));
385 lwkt_serialize_init(&sc
->sc_jbuf_serializer
);
388 * Initialize sysctl variables
390 sc
->sc_imtime
= nfe_imtime
;
391 sc
->sc_irq_enable
= NFE_IRQ_ENABLE(sc
);
392 sc
->sc_rx_ring_count
= nfe_rx_ring_count
;
393 sc
->sc_debug
= nfe_debug
;
395 sc
->sc_mem_rid
= PCIR_BAR(0);
398 if (pci_get_powerstate(dev
) != PCI_POWERSTATE_D0
) {
401 mem
= pci_read_config(dev
, sc
->sc_mem_rid
, 4);
402 irq
= pci_read_config(dev
, PCIR_INTLINE
, 4);
404 device_printf(dev
, "chip is in D%d power mode "
405 "-- setting to D0\n", pci_get_powerstate(dev
));
407 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
409 pci_write_config(dev
, sc
->sc_mem_rid
, mem
, 4);
410 pci_write_config(dev
, PCIR_INTLINE
, irq
, 4);
412 #endif /* !BURN_BRIDGE */
414 /* Enable bus mastering */
415 pci_enable_busmaster(dev
);
417 /* Allocate IO memory */
418 sc
->sc_mem_res
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
,
419 &sc
->sc_mem_rid
, RF_ACTIVE
);
420 if (sc
->sc_mem_res
== NULL
) {
421 device_printf(dev
, "cound not allocate io memory\n");
424 sc
->sc_memh
= rman_get_bushandle(sc
->sc_mem_res
);
425 sc
->sc_memt
= rman_get_bustag(sc
->sc_mem_res
);
429 sc
->sc_irq_res
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
,
431 RF_SHAREABLE
| RF_ACTIVE
);
432 if (sc
->sc_irq_res
== NULL
) {
433 device_printf(dev
, "could not allocate irq\n");
438 nfe_get_macaddr(sc
, eaddr
);
441 * Allocate Tx and Rx rings.
443 error
= nfe_alloc_tx_ring(sc
, &sc
->txq
);
445 device_printf(dev
, "could not allocate Tx ring\n");
449 error
= nfe_alloc_rx_ring(sc
, &sc
->rxq
);
451 device_printf(dev
, "could not allocate Rx ring\n");
458 sysctl_ctx_init(&sc
->sc_sysctl_ctx
);
459 sc
->sc_sysctl_tree
= SYSCTL_ADD_NODE(&sc
->sc_sysctl_ctx
,
460 SYSCTL_STATIC_CHILDREN(_hw
),
462 device_get_nameunit(dev
),
464 if (sc
->sc_sysctl_tree
== NULL
) {
465 device_printf(dev
, "can't add sysctl node\n");
469 SYSCTL_ADD_PROC(&sc
->sc_sysctl_ctx
,
470 SYSCTL_CHILDREN(sc
->sc_sysctl_tree
),
471 OID_AUTO
, "imtimer", CTLTYPE_INT
| CTLFLAG_RW
,
472 sc
, 0, nfe_sysctl_imtime
, "I",
473 "Interrupt moderation time (usec). "
474 "-1 to disable interrupt moderation.");
475 SYSCTL_ADD_INT(NULL
, SYSCTL_CHILDREN(sc
->sc_sysctl_tree
), OID_AUTO
,
476 "rx_ring_count", CTLFLAG_RD
, &sc
->sc_rx_ring_count
,
478 SYSCTL_ADD_INT(NULL
, SYSCTL_CHILDREN(sc
->sc_sysctl_tree
), OID_AUTO
,
479 "debug", CTLFLAG_RW
, &sc
->sc_debug
,
480 0, "control debugging printfs");
482 error
= mii_phy_probe(dev
, &sc
->sc_miibus
, nfe_ifmedia_upd
,
485 device_printf(dev
, "MII without any phy\n");
490 ifp
->if_mtu
= ETHERMTU
;
491 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
492 ifp
->if_ioctl
= nfe_ioctl
;
493 ifp
->if_start
= nfe_start
;
494 #ifdef DEVICE_POLLING
495 ifp
->if_poll
= nfe_poll
;
497 ifp
->if_watchdog
= nfe_watchdog
;
498 ifp
->if_init
= nfe_init
;
499 ifq_set_maxlen(&ifp
->if_snd
, NFE_IFQ_MAXLEN
);
500 ifq_set_ready(&ifp
->if_snd
);
502 ifp
->if_capabilities
= IFCAP_VLAN_MTU
;
504 if (sc
->sc_flags
& NFE_HW_VLAN
)
505 ifp
->if_capabilities
|= IFCAP_VLAN_HWTAGGING
;
508 if (sc
->sc_flags
& NFE_HW_CSUM
) {
509 ifp
->if_capabilities
|= IFCAP_HWCSUM
;
510 ifp
->if_hwassist
= NFE_CSUM_FEATURES
;
513 sc
->sc_flags
&= ~NFE_HW_CSUM
;
515 ifp
->if_capenable
= ifp
->if_capabilities
;
517 callout_init(&sc
->sc_tick_ch
);
519 ether_ifattach(ifp
, eaddr
, NULL
);
521 error
= bus_setup_intr(dev
, sc
->sc_irq_res
, INTR_MPSAFE
, nfe_intr
, sc
,
522 &sc
->sc_ih
, ifp
->if_serializer
);
524 device_printf(dev
, "could not setup intr\n");
536 nfe_detach(device_t dev
)
538 struct nfe_softc
*sc
= device_get_softc(dev
);
540 if (device_is_attached(dev
)) {
541 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
543 lwkt_serialize_enter(ifp
->if_serializer
);
545 bus_teardown_intr(dev
, sc
->sc_irq_res
, sc
->sc_ih
);
546 lwkt_serialize_exit(ifp
->if_serializer
);
551 if (sc
->sc_miibus
!= NULL
)
552 device_delete_child(dev
, sc
->sc_miibus
);
553 bus_generic_detach(dev
);
555 if (sc
->sc_sysctl_tree
!= NULL
)
556 sysctl_ctx_free(&sc
->sc_sysctl_ctx
);
558 if (sc
->sc_irq_res
!= NULL
) {
559 bus_release_resource(dev
, SYS_RES_IRQ
, sc
->sc_irq_rid
,
563 if (sc
->sc_mem_res
!= NULL
) {
564 bus_release_resource(dev
, SYS_RES_MEMORY
, sc
->sc_mem_rid
,
568 nfe_free_tx_ring(sc
, &sc
->txq
);
569 nfe_free_rx_ring(sc
, &sc
->rxq
);
575 nfe_shutdown(device_t dev
)
577 struct nfe_softc
*sc
= device_get_softc(dev
);
578 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
580 lwkt_serialize_enter(ifp
->if_serializer
);
582 lwkt_serialize_exit(ifp
->if_serializer
);
586 nfe_suspend(device_t dev
)
588 struct nfe_softc
*sc
= device_get_softc(dev
);
589 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
591 lwkt_serialize_enter(ifp
->if_serializer
);
593 lwkt_serialize_exit(ifp
->if_serializer
);
599 nfe_resume(device_t dev
)
601 struct nfe_softc
*sc
= device_get_softc(dev
);
602 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
604 lwkt_serialize_enter(ifp
->if_serializer
);
605 if (ifp
->if_flags
& IFF_UP
)
607 lwkt_serialize_exit(ifp
->if_serializer
);
613 nfe_miibus_statchg(device_t dev
)
615 struct nfe_softc
*sc
= device_get_softc(dev
);
616 struct mii_data
*mii
= device_get_softc(sc
->sc_miibus
);
617 uint32_t phy
, seed
, misc
= NFE_MISC1_MAGIC
, link
= NFE_MEDIA_SET
;
619 phy
= NFE_READ(sc
, NFE_PHY_IFACE
);
620 phy
&= ~(NFE_PHY_HDX
| NFE_PHY_100TX
| NFE_PHY_1000T
);
622 seed
= NFE_READ(sc
, NFE_RNDSEED
);
623 seed
&= ~NFE_SEED_MASK
;
625 if ((mii
->mii_media_active
& IFM_GMASK
) == IFM_HDX
) {
626 phy
|= NFE_PHY_HDX
; /* half-duplex */
627 misc
|= NFE_MISC1_HDX
;
630 switch (IFM_SUBTYPE(mii
->mii_media_active
)) {
631 case IFM_1000_T
: /* full-duplex only */
632 link
|= NFE_MEDIA_1000T
;
633 seed
|= NFE_SEED_1000T
;
634 phy
|= NFE_PHY_1000T
;
637 link
|= NFE_MEDIA_100TX
;
638 seed
|= NFE_SEED_100TX
;
639 phy
|= NFE_PHY_100TX
;
642 link
|= NFE_MEDIA_10T
;
643 seed
|= NFE_SEED_10T
;
647 NFE_WRITE(sc
, NFE_RNDSEED
, seed
); /* XXX: gigabit NICs only? */
649 NFE_WRITE(sc
, NFE_PHY_IFACE
, phy
);
650 NFE_WRITE(sc
, NFE_MISC1
, misc
);
651 NFE_WRITE(sc
, NFE_LINKSPEED
, link
);
655 nfe_miibus_readreg(device_t dev
, int phy
, int reg
)
657 struct nfe_softc
*sc
= device_get_softc(dev
);
661 NFE_WRITE(sc
, NFE_PHY_STATUS
, 0xf);
663 if (NFE_READ(sc
, NFE_PHY_CTL
) & NFE_PHY_BUSY
) {
664 NFE_WRITE(sc
, NFE_PHY_CTL
, NFE_PHY_BUSY
);
668 NFE_WRITE(sc
, NFE_PHY_CTL
, (phy
<< NFE_PHYADD_SHIFT
) | reg
);
670 for (ntries
= 0; ntries
< 1000; ntries
++) {
672 if (!(NFE_READ(sc
, NFE_PHY_CTL
) & NFE_PHY_BUSY
))
675 if (ntries
== 1000) {
676 DPRINTFN(sc
, 2, "timeout waiting for PHY %s\n", "");
680 if (NFE_READ(sc
, NFE_PHY_STATUS
) & NFE_PHY_ERROR
) {
681 DPRINTFN(sc
, 2, "could not read PHY %s\n", "");
685 val
= NFE_READ(sc
, NFE_PHY_DATA
);
686 if (val
!= 0xffffffff && val
!= 0)
687 sc
->mii_phyaddr
= phy
;
689 DPRINTFN(sc
, 2, "mii read phy %d reg 0x%x ret 0x%x\n", phy
, reg
, val
);
695 nfe_miibus_writereg(device_t dev
, int phy
, int reg
, int val
)
697 struct nfe_softc
*sc
= device_get_softc(dev
);
701 NFE_WRITE(sc
, NFE_PHY_STATUS
, 0xf);
703 if (NFE_READ(sc
, NFE_PHY_CTL
) & NFE_PHY_BUSY
) {
704 NFE_WRITE(sc
, NFE_PHY_CTL
, NFE_PHY_BUSY
);
708 NFE_WRITE(sc
, NFE_PHY_DATA
, val
);
709 ctl
= NFE_PHY_WRITE
| (phy
<< NFE_PHYADD_SHIFT
) | reg
;
710 NFE_WRITE(sc
, NFE_PHY_CTL
, ctl
);
712 for (ntries
= 0; ntries
< 1000; ntries
++) {
714 if (!(NFE_READ(sc
, NFE_PHY_CTL
) & NFE_PHY_BUSY
))
720 DPRINTFN(sc
, 2, "could not write to PHY %s\n", "");
724 #ifdef DEVICE_POLLING
727 nfe_poll(struct ifnet
*ifp
, enum poll_cmd cmd
, int count
)
729 struct nfe_softc
*sc
= ifp
->if_softc
;
731 ASSERT_SERIALIZED(ifp
->if_serializer
);
735 /* Disable interrupts */
736 NFE_WRITE(sc
, NFE_IRQ_MASK
, 0);
738 case POLL_DEREGISTER
:
739 /* enable interrupts */
740 NFE_WRITE(sc
, NFE_IRQ_MASK
, sc
->sc_irq_enable
);
742 case POLL_AND_CHECK_STATUS
:
745 if (ifp
->if_flags
& IFF_RUNNING
) {
758 struct nfe_softc
*sc
= arg
;
759 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
762 r
= NFE_READ(sc
, NFE_IRQ_STATUS
);
764 return; /* not for us */
765 NFE_WRITE(sc
, NFE_IRQ_STATUS
, r
);
767 DPRINTFN(sc
, 5, "%s: interrupt register %x\n", __func__
, r
);
769 if (r
& NFE_IRQ_LINK
) {
770 NFE_READ(sc
, NFE_PHY_STATUS
);
771 NFE_WRITE(sc
, NFE_PHY_STATUS
, 0xf);
772 DPRINTF(sc
, "link state changed %s\n", "");
775 if (ifp
->if_flags
& IFF_RUNNING
) {
785 nfe_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
787 struct nfe_softc
*sc
= ifp
->if_softc
;
788 struct ifreq
*ifr
= (struct ifreq
*)data
;
789 struct mii_data
*mii
;
794 if (((sc
->sc_flags
& NFE_JUMBO_SUP
) &&
795 ifr
->ifr_mtu
> NFE_JUMBO_MTU
) ||
796 ((sc
->sc_flags
& NFE_JUMBO_SUP
) == 0 &&
797 ifr
->ifr_mtu
> ETHERMTU
)) {
799 } else if (ifp
->if_mtu
!= ifr
->ifr_mtu
) {
800 ifp
->if_mtu
= ifr
->ifr_mtu
;
805 if (ifp
->if_flags
& IFF_UP
) {
807 * If only the PROMISC or ALLMULTI flag changes, then
808 * don't do a full re-init of the chip, just update
811 if ((ifp
->if_flags
& IFF_RUNNING
) &&
812 ((ifp
->if_flags
^ sc
->sc_if_flags
) &
813 (IFF_ALLMULTI
| IFF_PROMISC
)) != 0) {
816 if (!(ifp
->if_flags
& IFF_RUNNING
))
820 if (ifp
->if_flags
& IFF_RUNNING
)
823 sc
->sc_if_flags
= ifp
->if_flags
;
827 if (ifp
->if_flags
& IFF_RUNNING
)
832 mii
= device_get_softc(sc
->sc_miibus
);
833 error
= ifmedia_ioctl(ifp
, ifr
, &mii
->mii_media
, cmd
);
836 mask
= (ifr
->ifr_reqcap
^ ifp
->if_capenable
) & IFCAP_HWCSUM
;
837 if (mask
&& (ifp
->if_capabilities
& IFCAP_HWCSUM
)) {
838 ifp
->if_capenable
^= mask
;
839 if (IFCAP_TXCSUM
& ifp
->if_capenable
)
840 ifp
->if_hwassist
= NFE_CSUM_FEATURES
;
842 ifp
->if_hwassist
= 0;
844 if (ifp
->if_flags
& IFF_RUNNING
)
849 error
= ether_ioctl(ifp
, cmd
, data
);
856 nfe_rxeof(struct nfe_softc
*sc
)
858 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
859 struct nfe_rx_ring
*ring
= &sc
->rxq
;
863 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_POSTREAD
);
866 struct nfe_rx_data
*data
= &ring
->data
[ring
->cur
];
871 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
872 struct nfe_desc64
*desc64
= &ring
->desc64
[ring
->cur
];
874 flags
= le16toh(desc64
->flags
);
875 len
= le16toh(desc64
->length
) & 0x3fff;
877 struct nfe_desc32
*desc32
= &ring
->desc32
[ring
->cur
];
879 flags
= le16toh(desc32
->flags
);
880 len
= le16toh(desc32
->length
) & 0x3fff;
883 if (flags
& NFE_RX_READY
)
888 if ((sc
->sc_flags
& (NFE_JUMBO_SUP
| NFE_40BIT_ADDR
)) == 0) {
889 if (!(flags
& NFE_RX_VALID_V1
))
892 if ((flags
& NFE_RX_FIXME_V1
) == NFE_RX_FIXME_V1
) {
893 flags
&= ~NFE_RX_ERROR
;
894 len
--; /* fix buffer length */
897 if (!(flags
& NFE_RX_VALID_V2
))
900 if ((flags
& NFE_RX_FIXME_V2
) == NFE_RX_FIXME_V2
) {
901 flags
&= ~NFE_RX_ERROR
;
902 len
--; /* fix buffer length */
906 if (flags
& NFE_RX_ERROR
) {
913 if (sc
->sc_flags
& NFE_USE_JUMBO
)
914 error
= nfe_newbuf_jumbo(sc
, ring
, ring
->cur
, 0);
916 error
= nfe_newbuf_std(sc
, ring
, ring
->cur
, 0);
923 m
->m_pkthdr
.len
= m
->m_len
= len
;
924 m
->m_pkthdr
.rcvif
= ifp
;
926 if ((ifp
->if_capenable
& IFCAP_RXCSUM
) &&
927 (flags
& NFE_RX_CSUMOK
)) {
928 if (flags
& NFE_RX_IP_CSUMOK_V2
) {
929 m
->m_pkthdr
.csum_flags
|= CSUM_IP_CHECKED
|
934 (NFE_RX_UDP_CSUMOK_V2
| NFE_RX_TCP_CSUMOK_V2
)) {
935 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
|
937 CSUM_FRAG_NOT_CHECKED
;
938 m
->m_pkthdr
.csum_data
= 0xffff;
943 ifp
->if_input(ifp
, m
);
945 nfe_set_ready_rxdesc(sc
, ring
, ring
->cur
);
946 sc
->rxq
.cur
= (sc
->rxq
.cur
+ 1) % sc
->sc_rx_ring_count
;
950 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_PREWRITE
);
954 nfe_txeof(struct nfe_softc
*sc
)
956 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
957 struct nfe_tx_ring
*ring
= &sc
->txq
;
958 struct nfe_tx_data
*data
= NULL
;
960 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_POSTREAD
);
961 while (ring
->next
!= ring
->cur
) {
964 if (sc
->sc_flags
& NFE_40BIT_ADDR
)
965 flags
= le16toh(ring
->desc64
[ring
->next
].flags
);
967 flags
= le16toh(ring
->desc32
[ring
->next
].flags
);
969 if (flags
& NFE_TX_VALID
)
972 data
= &ring
->data
[ring
->next
];
974 if ((sc
->sc_flags
& (NFE_JUMBO_SUP
| NFE_40BIT_ADDR
)) == 0) {
975 if (!(flags
& NFE_TX_LASTFRAG_V1
) && data
->m
== NULL
)
978 if ((flags
& NFE_TX_ERROR_V1
) != 0) {
979 if_printf(ifp
, "tx v1 error 0x%4b\n", flags
,
986 if (!(flags
& NFE_TX_LASTFRAG_V2
) && data
->m
== NULL
)
989 if ((flags
& NFE_TX_ERROR_V2
) != 0) {
990 if_printf(ifp
, "tx v2 error 0x%4b\n", flags
,
998 if (data
->m
== NULL
) { /* should not get there */
1000 "last fragment bit w/o associated mbuf!\n");
1004 /* last fragment of the mbuf chain transmitted */
1005 bus_dmamap_sync(ring
->data_tag
, data
->map
,
1006 BUS_DMASYNC_POSTWRITE
);
1007 bus_dmamap_unload(ring
->data_tag
, data
->map
);
1014 KKASSERT(ring
->queued
>= 0);
1015 ring
->next
= (ring
->next
+ 1) % NFE_TX_RING_COUNT
;
1018 if (data
!= NULL
) { /* at least one slot freed */
1019 ifp
->if_flags
&= ~IFF_OACTIVE
;
1025 nfe_encap(struct nfe_softc
*sc
, struct nfe_tx_ring
*ring
, struct mbuf
*m0
)
1027 struct nfe_dma_ctx ctx
;
1028 bus_dma_segment_t segs
[NFE_MAX_SCATTER
];
1029 struct nfe_tx_data
*data
, *data_map
;
1031 struct nfe_desc64
*desc64
= NULL
;
1032 struct nfe_desc32
*desc32
= NULL
;
1037 data
= &ring
->data
[ring
->cur
];
1039 data_map
= data
; /* Remember who owns the DMA map */
1041 ctx
.nsegs
= NFE_MAX_SCATTER
;
1043 error
= bus_dmamap_load_mbuf(ring
->data_tag
, map
, m0
,
1044 nfe_buf_dma_addr
, &ctx
, BUS_DMA_NOWAIT
);
1045 if (error
&& error
!= EFBIG
) {
1046 if_printf(&sc
->arpcom
.ac_if
, "could not map TX mbuf\n");
1050 if (error
) { /* error == EFBIG */
1053 m_new
= m_defrag(m0
, MB_DONTWAIT
);
1054 if (m_new
== NULL
) {
1055 if_printf(&sc
->arpcom
.ac_if
,
1056 "could not defrag TX mbuf\n");
1063 ctx
.nsegs
= NFE_MAX_SCATTER
;
1065 error
= bus_dmamap_load_mbuf(ring
->data_tag
, map
, m0
,
1066 nfe_buf_dma_addr
, &ctx
,
1069 if_printf(&sc
->arpcom
.ac_if
,
1070 "could not map defraged TX mbuf\n");
1077 if (ring
->queued
+ ctx
.nsegs
>= NFE_TX_RING_COUNT
- 1) {
1078 bus_dmamap_unload(ring
->data_tag
, map
);
1083 /* setup h/w VLAN tagging */
1084 if (m0
->m_flags
& M_VLANTAG
)
1085 vtag
= m0
->m_pkthdr
.ether_vlantag
;
1087 if (sc
->arpcom
.ac_if
.if_capenable
& IFCAP_TXCSUM
) {
1088 if (m0
->m_pkthdr
.csum_flags
& CSUM_IP
)
1089 flags
|= NFE_TX_IP_CSUM
;
1090 if (m0
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))
1091 flags
|= NFE_TX_TCP_CSUM
;
1095 * XXX urm. somebody is unaware of how hardware works. You
1096 * absolutely CANNOT set NFE_TX_VALID on the next descriptor in
1097 * the ring until the entire chain is actually *VALID*. Otherwise
1098 * the hardware may encounter a partially initialized chain that
1099 * is marked as being ready to go when it in fact is not ready to
1103 for (i
= 0; i
< ctx
.nsegs
; i
++) {
1104 j
= (ring
->cur
+ i
) % NFE_TX_RING_COUNT
;
1105 data
= &ring
->data
[j
];
1107 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
1108 desc64
= &ring
->desc64
[j
];
1109 #if defined(__LP64__)
1110 desc64
->physaddr
[0] =
1111 htole32(segs
[i
].ds_addr
>> 32);
1113 desc64
->physaddr
[1] =
1114 htole32(segs
[i
].ds_addr
& 0xffffffff);
1115 desc64
->length
= htole16(segs
[i
].ds_len
- 1);
1116 desc64
->vtag
= htole32(vtag
);
1117 desc64
->flags
= htole16(flags
);
1119 desc32
= &ring
->desc32
[j
];
1120 desc32
->physaddr
= htole32(segs
[i
].ds_addr
);
1121 desc32
->length
= htole16(segs
[i
].ds_len
- 1);
1122 desc32
->flags
= htole16(flags
);
1125 /* csum flags and vtag belong to the first fragment only */
1126 flags
&= ~(NFE_TX_IP_CSUM
| NFE_TX_TCP_CSUM
);
1130 KKASSERT(ring
->queued
<= NFE_TX_RING_COUNT
);
1133 /* the whole mbuf chain has been DMA mapped, fix last descriptor */
1134 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
1135 desc64
->flags
|= htole16(NFE_TX_LASTFRAG_V2
);
1137 if (sc
->sc_flags
& NFE_JUMBO_SUP
)
1138 flags
= NFE_TX_LASTFRAG_V2
;
1140 flags
= NFE_TX_LASTFRAG_V1
;
1141 desc32
->flags
|= htole16(flags
);
1145 * Set NFE_TX_VALID backwards so the hardware doesn't see the
1146 * whole mess until the first descriptor in the map is flagged.
1148 for (i
= ctx
.nsegs
- 1; i
>= 0; --i
) {
1149 j
= (ring
->cur
+ i
) % NFE_TX_RING_COUNT
;
1150 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
1151 desc64
= &ring
->desc64
[j
];
1152 desc64
->flags
|= htole16(NFE_TX_VALID
);
1154 desc32
= &ring
->desc32
[j
];
1155 desc32
->flags
|= htole16(NFE_TX_VALID
);
1158 ring
->cur
= (ring
->cur
+ ctx
.nsegs
) % NFE_TX_RING_COUNT
;
1160 /* Exchange DMA map */
1161 data_map
->map
= data
->map
;
1165 bus_dmamap_sync(ring
->data_tag
, map
, BUS_DMASYNC_PREWRITE
);
1173 nfe_start(struct ifnet
*ifp
)
1175 struct nfe_softc
*sc
= ifp
->if_softc
;
1176 struct nfe_tx_ring
*ring
= &sc
->txq
;
1180 if (ifp
->if_flags
& IFF_OACTIVE
)
1183 if (ifq_is_empty(&ifp
->if_snd
))
1187 m0
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1193 if (nfe_encap(sc
, ring
, m0
) != 0) {
1194 ifp
->if_flags
|= IFF_OACTIVE
;
1201 * `m0' may be freed in nfe_encap(), so
1202 * it should not be touched any more.
1205 if (count
== 0) /* nothing sent */
1208 /* Sync TX descriptor ring */
1209 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_PREWRITE
);
1212 NFE_WRITE(sc
, NFE_RXTX_CTL
, NFE_RXTX_KICKTX
| sc
->rxtxctl
);
1215 * Set a timeout in case the chip goes out to lunch.
1221 nfe_watchdog(struct ifnet
*ifp
)
1223 struct nfe_softc
*sc
= ifp
->if_softc
;
1225 if (ifp
->if_flags
& IFF_RUNNING
) {
1226 if_printf(ifp
, "watchdog timeout - lost interrupt recovered\n");
1231 if_printf(ifp
, "watchdog timeout\n");
1233 nfe_init(ifp
->if_softc
);
1241 struct nfe_softc
*sc
= xsc
;
1242 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1250 * Switching between jumbo frames and normal frames should
1251 * be done _after_ nfe_stop() but _before_ nfe_init_rx_ring().
1253 if (ifp
->if_mtu
> ETHERMTU
) {
1254 sc
->sc_flags
|= NFE_USE_JUMBO
;
1255 sc
->rxq
.bufsz
= NFE_JBYTES
;
1257 if_printf(ifp
, "use jumbo frames\n");
1259 sc
->sc_flags
&= ~NFE_USE_JUMBO
;
1260 sc
->rxq
.bufsz
= MCLBYTES
;
1262 if_printf(ifp
, "use non-jumbo frames\n");
1265 error
= nfe_init_tx_ring(sc
, &sc
->txq
);
1271 error
= nfe_init_rx_ring(sc
, &sc
->rxq
);
1277 NFE_WRITE(sc
, NFE_TX_UNK
, 0);
1278 NFE_WRITE(sc
, NFE_STATUS
, 0);
1280 sc
->rxtxctl
= NFE_RXTX_BIT2
;
1281 if (sc
->sc_flags
& NFE_40BIT_ADDR
)
1282 sc
->rxtxctl
|= NFE_RXTX_V3MAGIC
;
1283 else if (sc
->sc_flags
& NFE_JUMBO_SUP
)
1284 sc
->rxtxctl
|= NFE_RXTX_V2MAGIC
;
1286 if (ifp
->if_capenable
& IFCAP_RXCSUM
)
1287 sc
->rxtxctl
|= NFE_RXTX_RXCSUM
;
1290 * Although the adapter is capable of stripping VLAN tags from received
1291 * frames (NFE_RXTX_VTAG_STRIP), we do not enable this functionality on
1292 * purpose. This will be done in software by our network stack.
1294 if (sc
->sc_flags
& NFE_HW_VLAN
)
1295 sc
->rxtxctl
|= NFE_RXTX_VTAG_INSERT
;
1297 NFE_WRITE(sc
, NFE_RXTX_CTL
, NFE_RXTX_RESET
| sc
->rxtxctl
);
1299 NFE_WRITE(sc
, NFE_RXTX_CTL
, sc
->rxtxctl
);
1301 if (sc
->sc_flags
& NFE_HW_VLAN
)
1302 NFE_WRITE(sc
, NFE_VTAG_CTL
, NFE_VTAG_ENABLE
);
1304 NFE_WRITE(sc
, NFE_SETUP_R6
, 0);
1306 /* set MAC address */
1307 nfe_set_macaddr(sc
, sc
->arpcom
.ac_enaddr
);
1309 /* tell MAC where rings are in memory */
1311 NFE_WRITE(sc
, NFE_RX_RING_ADDR_HI
, sc
->rxq
.physaddr
>> 32);
1313 NFE_WRITE(sc
, NFE_RX_RING_ADDR_LO
, sc
->rxq
.physaddr
& 0xffffffff);
1315 NFE_WRITE(sc
, NFE_TX_RING_ADDR_HI
, sc
->txq
.physaddr
>> 32);
1317 NFE_WRITE(sc
, NFE_TX_RING_ADDR_LO
, sc
->txq
.physaddr
& 0xffffffff);
1319 NFE_WRITE(sc
, NFE_RING_SIZE
,
1320 (sc
->sc_rx_ring_count
- 1) << 16 |
1321 (NFE_TX_RING_COUNT
- 1));
1323 NFE_WRITE(sc
, NFE_RXBUFSZ
, sc
->rxq
.bufsz
);
1325 /* force MAC to wakeup */
1326 tmp
= NFE_READ(sc
, NFE_PWR_STATE
);
1327 NFE_WRITE(sc
, NFE_PWR_STATE
, tmp
| NFE_PWR_WAKEUP
);
1329 tmp
= NFE_READ(sc
, NFE_PWR_STATE
);
1330 NFE_WRITE(sc
, NFE_PWR_STATE
, tmp
| NFE_PWR_VALID
);
1333 * NFE_IMTIMER generates a periodic interrupt via NFE_IRQ_TIMER.
1334 * It is unclear how wide the timer is. Base programming does
1335 * not seem to effect NFE_IRQ_TX_DONE or NFE_IRQ_RX_DONE so
1336 * we don't get any interrupt moderation. TX moderation is
1337 * possible by using the timer interrupt instead of TX_DONE.
1339 * It is unclear whether there are other bits that can be
1340 * set to make the NFE device actually do interrupt moderation
1343 * For now set a 128uS interval as a placemark, but don't use
1346 if (sc
->sc_imtime
< 0)
1347 NFE_WRITE(sc
, NFE_IMTIMER
, NFE_IMTIME_DEFAULT
);
1349 NFE_WRITE(sc
, NFE_IMTIMER
, NFE_IMTIME(sc
->sc_imtime
));
1351 NFE_WRITE(sc
, NFE_SETUP_R1
, NFE_R1_MAGIC
);
1352 NFE_WRITE(sc
, NFE_SETUP_R2
, NFE_R2_MAGIC
);
1353 NFE_WRITE(sc
, NFE_SETUP_R6
, NFE_R6_MAGIC
);
1355 /* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */
1356 NFE_WRITE(sc
, NFE_STATUS
, sc
->mii_phyaddr
<< 24 | NFE_STATUS_MAGIC
);
1358 NFE_WRITE(sc
, NFE_SETUP_R4
, NFE_R4_MAGIC
);
1359 NFE_WRITE(sc
, NFE_WOL_CTL
, NFE_WOL_MAGIC
);
1361 sc
->rxtxctl
&= ~NFE_RXTX_BIT2
;
1362 NFE_WRITE(sc
, NFE_RXTX_CTL
, sc
->rxtxctl
);
1364 NFE_WRITE(sc
, NFE_RXTX_CTL
, NFE_RXTX_BIT1
| sc
->rxtxctl
);
1369 nfe_ifmedia_upd(ifp
);
1372 NFE_WRITE(sc
, NFE_RX_CTL
, NFE_RX_START
);
1375 NFE_WRITE(sc
, NFE_TX_CTL
, NFE_TX_START
);
1377 NFE_WRITE(sc
, NFE_PHY_STATUS
, 0xf);
1379 #ifdef DEVICE_POLLING
1380 if ((ifp
->if_flags
& IFF_POLLING
) == 0)
1382 /* enable interrupts */
1383 NFE_WRITE(sc
, NFE_IRQ_MASK
, sc
->sc_irq_enable
);
1385 callout_reset(&sc
->sc_tick_ch
, hz
, nfe_tick
, sc
);
1387 ifp
->if_flags
|= IFF_RUNNING
;
1388 ifp
->if_flags
&= ~IFF_OACTIVE
;
1391 * If we had stuff in the tx ring before its all cleaned out now
1392 * so we are not going to get an interrupt, jump-start any pending
1399 nfe_stop(struct nfe_softc
*sc
)
1401 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1403 callout_stop(&sc
->sc_tick_ch
);
1406 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
1409 * Are NFE_TX_CTL and NFE_RX_CTL polled by the chip microcontroller
1410 * or do they directly reset/terminate the DMA hardware? Nobody
1415 * (1) Delay before zeroing out NFE_TX_CTL. This seems to help a
1416 * watchdog timeout that occurs after a stop/init sequence. I am
1417 * theorizing that a TX KICK occuring just prior to a reinit (e.g.
1418 * due to dhclient) is queueing an interrupt to the microcontroller
1419 * which gets delayed until after we clear the control registers
1420 * down below, resulting in mass confusion. TX KICK is clearly
1421 * hardware aided whereas the other bits in the control register
1422 * are more likely to be polled by the microcontroller.
1424 * (2) Delay after zeroing out TX and RX CTL registers, under the
1425 * assumption that primary DMA is initiated and terminated by
1426 * the microcontroller and not hardware (and anyway, one can hardly
1427 * expect the DMA engine to just instantly stop!). We don't want
1428 * to rip the rings out from under it before it has had a chance to
1434 NFE_WRITE(sc
, NFE_TX_CTL
, 0);
1437 NFE_WRITE(sc
, NFE_RX_CTL
, 0);
1439 /* Disable interrupts */
1440 NFE_WRITE(sc
, NFE_IRQ_MASK
, 0);
1444 /* Reset Tx and Rx rings */
1445 nfe_reset_tx_ring(sc
, &sc
->txq
);
1446 nfe_reset_rx_ring(sc
, &sc
->rxq
);
1450 nfe_alloc_rx_ring(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1452 int i
, j
, error
, descsize
;
1455 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
1456 desc
= (void **)&ring
->desc64
;
1457 descsize
= sizeof(struct nfe_desc64
);
1459 desc
= (void **)&ring
->desc32
;
1460 descsize
= sizeof(struct nfe_desc32
);
1463 ring
->jbuf
= kmalloc(sizeof(struct nfe_jbuf
) * NFE_JPOOL_COUNT
,
1464 M_DEVBUF
, M_WAITOK
| M_ZERO
);
1465 ring
->data
= kmalloc(sizeof(struct nfe_rx_data
) * sc
->sc_rx_ring_count
,
1466 M_DEVBUF
, M_WAITOK
| M_ZERO
);
1468 ring
->bufsz
= MCLBYTES
;
1469 ring
->cur
= ring
->next
= 0;
1471 error
= bus_dma_tag_create(NULL
, PAGE_SIZE
, 0,
1472 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
,
1474 sc
->sc_rx_ring_count
* descsize
, 1,
1475 sc
->sc_rx_ring_count
* descsize
,
1478 if_printf(&sc
->arpcom
.ac_if
,
1479 "could not create desc RX DMA tag\n");
1483 error
= bus_dmamem_alloc(ring
->tag
, desc
, BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1486 if_printf(&sc
->arpcom
.ac_if
,
1487 "could not allocate RX desc DMA memory\n");
1488 bus_dma_tag_destroy(ring
->tag
);
1493 error
= bus_dmamap_load(ring
->tag
, ring
->map
, *desc
,
1494 sc
->sc_rx_ring_count
* descsize
,
1495 nfe_ring_dma_addr
, &ring
->physaddr
,
1498 if_printf(&sc
->arpcom
.ac_if
,
1499 "could not load RX desc DMA map\n");
1500 bus_dmamem_free(ring
->tag
, *desc
, ring
->map
);
1501 bus_dma_tag_destroy(ring
->tag
);
1506 if (sc
->sc_flags
& NFE_JUMBO_SUP
) {
1507 error
= nfe_jpool_alloc(sc
, ring
);
1509 if_printf(&sc
->arpcom
.ac_if
,
1510 "could not allocate jumbo frames\n");
1515 error
= bus_dma_tag_create(NULL
, 1, 0,
1516 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
,
1518 MCLBYTES
, 1, MCLBYTES
,
1519 0, &ring
->data_tag
);
1521 if_printf(&sc
->arpcom
.ac_if
,
1522 "could not create RX mbuf DMA tag\n");
1526 /* Create a spare RX mbuf DMA map */
1527 error
= bus_dmamap_create(ring
->data_tag
, 0, &ring
->data_tmpmap
);
1529 if_printf(&sc
->arpcom
.ac_if
,
1530 "could not create spare RX mbuf DMA map\n");
1531 bus_dma_tag_destroy(ring
->data_tag
);
1532 ring
->data_tag
= NULL
;
1536 for (i
= 0; i
< sc
->sc_rx_ring_count
; i
++) {
1537 error
= bus_dmamap_create(ring
->data_tag
, 0,
1538 &ring
->data
[i
].map
);
1540 if_printf(&sc
->arpcom
.ac_if
,
1541 "could not create %dth RX mbuf DMA mapn", i
);
1547 for (j
= 0; j
< i
; ++j
)
1548 bus_dmamap_destroy(ring
->data_tag
, ring
->data
[i
].map
);
1549 bus_dmamap_destroy(ring
->data_tag
, ring
->data_tmpmap
);
1550 bus_dma_tag_destroy(ring
->data_tag
);
1551 ring
->data_tag
= NULL
;
1556 nfe_reset_rx_ring(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1560 for (i
= 0; i
< sc
->sc_rx_ring_count
; i
++) {
1561 struct nfe_rx_data
*data
= &ring
->data
[i
];
1563 if (data
->m
!= NULL
) {
1564 if ((sc
->sc_flags
& NFE_USE_JUMBO
) == 0)
1565 bus_dmamap_unload(ring
->data_tag
, data
->map
);
1570 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_PREWRITE
);
1572 ring
->cur
= ring
->next
= 0;
1576 nfe_init_rx_ring(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1580 for (i
= 0; i
< sc
->sc_rx_ring_count
; ++i
) {
1583 /* XXX should use a function pointer */
1584 if (sc
->sc_flags
& NFE_USE_JUMBO
)
1585 error
= nfe_newbuf_jumbo(sc
, ring
, i
, 1);
1587 error
= nfe_newbuf_std(sc
, ring
, i
, 1);
1589 if_printf(&sc
->arpcom
.ac_if
,
1590 "could not allocate RX buffer\n");
1594 nfe_set_ready_rxdesc(sc
, ring
, i
);
1596 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_PREWRITE
);
1602 nfe_free_rx_ring(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1604 if (ring
->data_tag
!= NULL
) {
1605 struct nfe_rx_data
*data
;
1608 for (i
= 0; i
< sc
->sc_rx_ring_count
; i
++) {
1609 data
= &ring
->data
[i
];
1611 if (data
->m
!= NULL
) {
1612 bus_dmamap_unload(ring
->data_tag
, data
->map
);
1615 bus_dmamap_destroy(ring
->data_tag
, data
->map
);
1617 bus_dmamap_destroy(ring
->data_tag
, ring
->data_tmpmap
);
1618 bus_dma_tag_destroy(ring
->data_tag
);
1621 nfe_jpool_free(sc
, ring
);
1623 if (ring
->jbuf
!= NULL
)
1624 kfree(ring
->jbuf
, M_DEVBUF
);
1625 if (ring
->data
!= NULL
)
1626 kfree(ring
->data
, M_DEVBUF
);
1628 if (ring
->tag
!= NULL
) {
1631 if (sc
->sc_flags
& NFE_40BIT_ADDR
)
1632 desc
= ring
->desc64
;
1634 desc
= ring
->desc32
;
1636 bus_dmamap_unload(ring
->tag
, ring
->map
);
1637 bus_dmamem_free(ring
->tag
, desc
, ring
->map
);
1638 bus_dma_tag_destroy(ring
->tag
);
1642 static struct nfe_jbuf
*
1643 nfe_jalloc(struct nfe_softc
*sc
)
1645 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1646 struct nfe_jbuf
*jbuf
;
1648 lwkt_serialize_enter(&sc
->sc_jbuf_serializer
);
1650 jbuf
= SLIST_FIRST(&sc
->rxq
.jfreelist
);
1652 SLIST_REMOVE_HEAD(&sc
->rxq
.jfreelist
, jnext
);
1655 if_printf(ifp
, "no free jumbo buffer\n");
1658 lwkt_serialize_exit(&sc
->sc_jbuf_serializer
);
1664 nfe_jfree(void *arg
)
1666 struct nfe_jbuf
*jbuf
= arg
;
1667 struct nfe_softc
*sc
= jbuf
->sc
;
1668 struct nfe_rx_ring
*ring
= jbuf
->ring
;
1670 if (&ring
->jbuf
[jbuf
->slot
] != jbuf
)
1671 panic("%s: free wrong jumbo buffer\n", __func__
);
1672 else if (jbuf
->inuse
== 0)
1673 panic("%s: jumbo buffer already freed\n", __func__
);
1675 lwkt_serialize_enter(&sc
->sc_jbuf_serializer
);
1676 atomic_subtract_int(&jbuf
->inuse
, 1);
1677 if (jbuf
->inuse
== 0)
1678 SLIST_INSERT_HEAD(&ring
->jfreelist
, jbuf
, jnext
);
1679 lwkt_serialize_exit(&sc
->sc_jbuf_serializer
);
1685 struct nfe_jbuf
*jbuf
= arg
;
1686 struct nfe_rx_ring
*ring
= jbuf
->ring
;
1688 if (&ring
->jbuf
[jbuf
->slot
] != jbuf
)
1689 panic("%s: ref wrong jumbo buffer\n", __func__
);
1690 else if (jbuf
->inuse
== 0)
1691 panic("%s: jumbo buffer already freed\n", __func__
);
1693 atomic_add_int(&jbuf
->inuse
, 1);
1697 nfe_jpool_alloc(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1699 struct nfe_jbuf
*jbuf
;
1700 bus_addr_t physaddr
;
1705 * Allocate a big chunk of DMA'able memory.
1707 error
= bus_dma_tag_create(NULL
, PAGE_SIZE
, 0,
1708 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
,
1710 NFE_JPOOL_SIZE
, 1, NFE_JPOOL_SIZE
,
1713 if_printf(&sc
->arpcom
.ac_if
,
1714 "could not create jumbo DMA tag\n");
1718 error
= bus_dmamem_alloc(ring
->jtag
, (void **)&ring
->jpool
,
1719 BUS_DMA_WAITOK
, &ring
->jmap
);
1721 if_printf(&sc
->arpcom
.ac_if
,
1722 "could not allocate jumbo DMA memory\n");
1723 bus_dma_tag_destroy(ring
->jtag
);
1728 error
= bus_dmamap_load(ring
->jtag
, ring
->jmap
, ring
->jpool
,
1729 NFE_JPOOL_SIZE
, nfe_ring_dma_addr
, &physaddr
,
1732 if_printf(&sc
->arpcom
.ac_if
,
1733 "could not load jumbo DMA map\n");
1734 bus_dmamem_free(ring
->jtag
, ring
->jpool
, ring
->jmap
);
1735 bus_dma_tag_destroy(ring
->jtag
);
1740 /* ..and split it into 9KB chunks */
1741 SLIST_INIT(&ring
->jfreelist
);
1744 for (i
= 0; i
< NFE_JPOOL_COUNT
; i
++) {
1745 jbuf
= &ring
->jbuf
[i
];
1752 jbuf
->physaddr
= physaddr
;
1754 SLIST_INSERT_HEAD(&ring
->jfreelist
, jbuf
, jnext
);
1757 physaddr
+= NFE_JBYTES
;
1764 nfe_jpool_free(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
)
1766 if (ring
->jtag
!= NULL
) {
1767 bus_dmamap_unload(ring
->jtag
, ring
->jmap
);
1768 bus_dmamem_free(ring
->jtag
, ring
->jpool
, ring
->jmap
);
1769 bus_dma_tag_destroy(ring
->jtag
);
1774 nfe_alloc_tx_ring(struct nfe_softc
*sc
, struct nfe_tx_ring
*ring
)
1776 int i
, j
, error
, descsize
;
1779 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
1780 desc
= (void **)&ring
->desc64
;
1781 descsize
= sizeof(struct nfe_desc64
);
1783 desc
= (void **)&ring
->desc32
;
1784 descsize
= sizeof(struct nfe_desc32
);
1788 ring
->cur
= ring
->next
= 0;
1790 error
= bus_dma_tag_create(NULL
, PAGE_SIZE
, 0,
1791 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
,
1793 NFE_TX_RING_COUNT
* descsize
, 1,
1794 NFE_TX_RING_COUNT
* descsize
,
1797 if_printf(&sc
->arpcom
.ac_if
,
1798 "could not create TX desc DMA map\n");
1802 error
= bus_dmamem_alloc(ring
->tag
, desc
, BUS_DMA_WAITOK
| BUS_DMA_ZERO
,
1805 if_printf(&sc
->arpcom
.ac_if
,
1806 "could not allocate TX desc DMA memory\n");
1807 bus_dma_tag_destroy(ring
->tag
);
1812 error
= bus_dmamap_load(ring
->tag
, ring
->map
, *desc
,
1813 NFE_TX_RING_COUNT
* descsize
,
1814 nfe_ring_dma_addr
, &ring
->physaddr
,
1817 if_printf(&sc
->arpcom
.ac_if
,
1818 "could not load TX desc DMA map\n");
1819 bus_dmamem_free(ring
->tag
, *desc
, ring
->map
);
1820 bus_dma_tag_destroy(ring
->tag
);
1825 error
= bus_dma_tag_create(NULL
, PAGE_SIZE
, 0,
1826 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
,
1828 NFE_JBYTES
* NFE_MAX_SCATTER
,
1829 NFE_MAX_SCATTER
, NFE_JBYTES
,
1830 0, &ring
->data_tag
);
1832 if_printf(&sc
->arpcom
.ac_if
,
1833 "could not create TX buf DMA tag\n");
1837 for (i
= 0; i
< NFE_TX_RING_COUNT
; i
++) {
1838 error
= bus_dmamap_create(ring
->data_tag
, 0,
1839 &ring
->data
[i
].map
);
1841 if_printf(&sc
->arpcom
.ac_if
,
1842 "could not create %dth TX buf DMA map\n", i
);
1849 for (j
= 0; j
< i
; ++j
)
1850 bus_dmamap_destroy(ring
->data_tag
, ring
->data
[i
].map
);
1851 bus_dma_tag_destroy(ring
->data_tag
);
1852 ring
->data_tag
= NULL
;
1857 nfe_reset_tx_ring(struct nfe_softc
*sc
, struct nfe_tx_ring
*ring
)
1861 for (i
= 0; i
< NFE_TX_RING_COUNT
; i
++) {
1862 struct nfe_tx_data
*data
= &ring
->data
[i
];
1864 if (sc
->sc_flags
& NFE_40BIT_ADDR
)
1865 ring
->desc64
[i
].flags
= 0;
1867 ring
->desc32
[i
].flags
= 0;
1869 if (data
->m
!= NULL
) {
1870 bus_dmamap_sync(ring
->data_tag
, data
->map
,
1871 BUS_DMASYNC_POSTWRITE
);
1872 bus_dmamap_unload(ring
->data_tag
, data
->map
);
1877 bus_dmamap_sync(ring
->tag
, ring
->map
, BUS_DMASYNC_PREWRITE
);
1880 ring
->cur
= ring
->next
= 0;
1884 nfe_init_tx_ring(struct nfe_softc
*sc __unused
,
1885 struct nfe_tx_ring
*ring __unused
)
1891 nfe_free_tx_ring(struct nfe_softc
*sc
, struct nfe_tx_ring
*ring
)
1893 if (ring
->data_tag
!= NULL
) {
1894 struct nfe_tx_data
*data
;
1897 for (i
= 0; i
< NFE_TX_RING_COUNT
; ++i
) {
1898 data
= &ring
->data
[i
];
1900 if (data
->m
!= NULL
) {
1901 bus_dmamap_unload(ring
->data_tag
, data
->map
);
1904 bus_dmamap_destroy(ring
->data_tag
, data
->map
);
1907 bus_dma_tag_destroy(ring
->data_tag
);
1910 if (ring
->tag
!= NULL
) {
1913 if (sc
->sc_flags
& NFE_40BIT_ADDR
)
1914 desc
= ring
->desc64
;
1916 desc
= ring
->desc32
;
1918 bus_dmamap_unload(ring
->tag
, ring
->map
);
1919 bus_dmamem_free(ring
->tag
, desc
, ring
->map
);
1920 bus_dma_tag_destroy(ring
->tag
);
1925 nfe_ifmedia_upd(struct ifnet
*ifp
)
1927 struct nfe_softc
*sc
= ifp
->if_softc
;
1928 struct mii_data
*mii
= device_get_softc(sc
->sc_miibus
);
1930 if (mii
->mii_instance
!= 0) {
1931 struct mii_softc
*miisc
;
1933 LIST_FOREACH(miisc
, &mii
->mii_phys
, mii_list
)
1934 mii_phy_reset(miisc
);
1942 nfe_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
1944 struct nfe_softc
*sc
= ifp
->if_softc
;
1945 struct mii_data
*mii
= device_get_softc(sc
->sc_miibus
);
1948 ifmr
->ifm_status
= mii
->mii_media_status
;
1949 ifmr
->ifm_active
= mii
->mii_media_active
;
1953 nfe_setmulti(struct nfe_softc
*sc
)
1955 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1956 struct ifmultiaddr
*ifma
;
1957 uint8_t addr
[ETHER_ADDR_LEN
], mask
[ETHER_ADDR_LEN
];
1958 uint32_t filter
= NFE_RXFILTER_MAGIC
;
1961 if ((ifp
->if_flags
& (IFF_ALLMULTI
| IFF_PROMISC
)) != 0) {
1962 bzero(addr
, ETHER_ADDR_LEN
);
1963 bzero(mask
, ETHER_ADDR_LEN
);
1967 bcopy(etherbroadcastaddr
, addr
, ETHER_ADDR_LEN
);
1968 bcopy(etherbroadcastaddr
, mask
, ETHER_ADDR_LEN
);
1970 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
1973 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
1976 maddr
= LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
);
1977 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++) {
1978 addr
[i
] &= maddr
[i
];
1979 mask
[i
] &= ~maddr
[i
];
1983 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++)
1987 addr
[0] |= 0x01; /* make sure multicast bit is set */
1989 NFE_WRITE(sc
, NFE_MULTIADDR_HI
,
1990 addr
[3] << 24 | addr
[2] << 16 | addr
[1] << 8 | addr
[0]);
1991 NFE_WRITE(sc
, NFE_MULTIADDR_LO
,
1992 addr
[5] << 8 | addr
[4]);
1993 NFE_WRITE(sc
, NFE_MULTIMASK_HI
,
1994 mask
[3] << 24 | mask
[2] << 16 | mask
[1] << 8 | mask
[0]);
1995 NFE_WRITE(sc
, NFE_MULTIMASK_LO
,
1996 mask
[5] << 8 | mask
[4]);
1998 filter
|= (ifp
->if_flags
& IFF_PROMISC
) ? NFE_PROMISC
: NFE_U2M
;
1999 NFE_WRITE(sc
, NFE_RXFILTER
, filter
);
2003 nfe_get_macaddr(struct nfe_softc
*sc
, uint8_t *addr
)
2007 tmp
= NFE_READ(sc
, NFE_MACADDR_LO
);
2008 addr
[0] = (tmp
>> 8) & 0xff;
2009 addr
[1] = (tmp
& 0xff);
2011 tmp
= NFE_READ(sc
, NFE_MACADDR_HI
);
2012 addr
[2] = (tmp
>> 24) & 0xff;
2013 addr
[3] = (tmp
>> 16) & 0xff;
2014 addr
[4] = (tmp
>> 8) & 0xff;
2015 addr
[5] = (tmp
& 0xff);
2019 nfe_set_macaddr(struct nfe_softc
*sc
, const uint8_t *addr
)
2021 NFE_WRITE(sc
, NFE_MACADDR_LO
,
2022 addr
[5] << 8 | addr
[4]);
2023 NFE_WRITE(sc
, NFE_MACADDR_HI
,
2024 addr
[3] << 24 | addr
[2] << 16 | addr
[1] << 8 | addr
[0]);
2030 struct nfe_softc
*sc
= arg
;
2031 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2032 struct mii_data
*mii
= device_get_softc(sc
->sc_miibus
);
2034 lwkt_serialize_enter(ifp
->if_serializer
);
2037 callout_reset(&sc
->sc_tick_ch
, hz
, nfe_tick
, sc
);
2039 lwkt_serialize_exit(ifp
->if_serializer
);
2043 nfe_ring_dma_addr(void *arg
, bus_dma_segment_t
*seg
, int nseg
, int error
)
2048 KASSERT(nseg
== 1, ("too many segments, should be 1\n"));
2050 *((uint32_t *)arg
) = seg
->ds_addr
;
2054 nfe_buf_dma_addr(void *arg
, bus_dma_segment_t
*segs
, int nsegs
,
2055 bus_size_t mapsz __unused
, int error
)
2057 struct nfe_dma_ctx
*ctx
= arg
;
2063 KASSERT(nsegs
<= ctx
->nsegs
,
2064 ("too many segments(%d), should be <= %d\n",
2065 nsegs
, ctx
->nsegs
));
2068 for (i
= 0; i
< nsegs
; ++i
)
2069 ctx
->segs
[i
] = segs
[i
];
2073 nfe_newbuf_std(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
, int idx
,
2076 struct nfe_rx_data
*data
= &ring
->data
[idx
];
2077 struct nfe_dma_ctx ctx
;
2078 bus_dma_segment_t seg
;
2083 m
= m_getcl(wait
? MB_WAIT
: MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
2086 m
->m_len
= m
->m_pkthdr
.len
= MCLBYTES
;
2090 error
= bus_dmamap_load_mbuf(ring
->data_tag
, ring
->data_tmpmap
,
2091 m
, nfe_buf_dma_addr
, &ctx
,
2092 wait
? BUS_DMA_WAITOK
: BUS_DMA_NOWAIT
);
2095 if_printf(&sc
->arpcom
.ac_if
, "could map RX mbuf %d\n", error
);
2099 /* Unload originally mapped mbuf */
2100 bus_dmamap_unload(ring
->data_tag
, data
->map
);
2102 /* Swap this DMA map with tmp DMA map */
2104 data
->map
= ring
->data_tmpmap
;
2105 ring
->data_tmpmap
= map
;
2107 /* Caller is assumed to have collected the old mbuf */
2110 nfe_set_paddr_rxdesc(sc
, ring
, idx
, seg
.ds_addr
);
2112 bus_dmamap_sync(ring
->data_tag
, data
->map
, BUS_DMASYNC_PREREAD
);
2117 nfe_newbuf_jumbo(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
, int idx
,
2120 struct nfe_rx_data
*data
= &ring
->data
[idx
];
2121 struct nfe_jbuf
*jbuf
;
2124 MGETHDR(m
, wait
? MB_WAIT
: MB_DONTWAIT
, MT_DATA
);
2128 jbuf
= nfe_jalloc(sc
);
2131 if_printf(&sc
->arpcom
.ac_if
, "jumbo allocation failed "
2132 "-- packet dropped!\n");
2136 m
->m_ext
.ext_arg
= jbuf
;
2137 m
->m_ext
.ext_buf
= jbuf
->buf
;
2138 m
->m_ext
.ext_free
= nfe_jfree
;
2139 m
->m_ext
.ext_ref
= nfe_jref
;
2140 m
->m_ext
.ext_size
= NFE_JBYTES
;
2142 m
->m_data
= m
->m_ext
.ext_buf
;
2143 m
->m_flags
|= M_EXT
;
2144 m
->m_len
= m
->m_pkthdr
.len
= m
->m_ext
.ext_size
;
2146 /* Caller is assumed to have collected the old mbuf */
2149 nfe_set_paddr_rxdesc(sc
, ring
, idx
, jbuf
->physaddr
);
2151 bus_dmamap_sync(ring
->jtag
, ring
->jmap
, BUS_DMASYNC_PREREAD
);
2156 nfe_set_paddr_rxdesc(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
, int idx
,
2157 bus_addr_t physaddr
)
2159 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
2160 struct nfe_desc64
*desc64
= &ring
->desc64
[idx
];
2162 #if defined(__LP64__)
2163 desc64
->physaddr
[0] = htole32(physaddr
>> 32);
2165 desc64
->physaddr
[1] = htole32(physaddr
& 0xffffffff);
2167 struct nfe_desc32
*desc32
= &ring
->desc32
[idx
];
2169 desc32
->physaddr
= htole32(physaddr
);
2174 nfe_set_ready_rxdesc(struct nfe_softc
*sc
, struct nfe_rx_ring
*ring
, int idx
)
2176 if (sc
->sc_flags
& NFE_40BIT_ADDR
) {
2177 struct nfe_desc64
*desc64
= &ring
->desc64
[idx
];
2179 desc64
->length
= htole16(ring
->bufsz
);
2180 desc64
->flags
= htole16(NFE_RX_READY
);
2182 struct nfe_desc32
*desc32
= &ring
->desc32
[idx
];
2184 desc32
->length
= htole16(ring
->bufsz
);
2185 desc32
->flags
= htole16(NFE_RX_READY
);
2190 nfe_sysctl_imtime(SYSCTL_HANDLER_ARGS
)
2192 struct nfe_softc
*sc
= arg1
;
2193 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
2196 lwkt_serialize_enter(ifp
->if_serializer
);
2199 error
= sysctl_handle_int(oidp
, &v
, 0, req
);
2200 if (error
|| req
->newptr
== NULL
)
2207 if (sc
->sc_imtime
!= v
) {
2208 int old_imtime
= sc
->sc_imtime
;
2211 sc
->sc_irq_enable
= NFE_IRQ_ENABLE(sc
);
2213 if ((ifp
->if_flags
& (IFF_POLLING
| IFF_RUNNING
))
2215 if (old_imtime
> 0 && sc
->sc_imtime
> 0) {
2216 NFE_WRITE(sc
, NFE_IMTIMER
,
2217 NFE_IMTIME(sc
->sc_imtime
));
2218 } else if ((old_imtime
* sc
->sc_imtime
) < 0) {
2224 lwkt_serialize_exit(ifp
->if_serializer
);