2 * Copyright (c) 2001 Wind River Systems
3 * Copyright (c) 1997, 1998, 1999, 2001
4 * Bill Paul <wpaul@windriver.com>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/sys/dev/bge/if_bge.c,v 1.3.2.39 2005/07/03 03:41:18 silby Exp $
37 #include "opt_ifpoll.h"
39 #include <sys/param.h>
41 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/interrupt.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
48 #include <sys/serialize.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
53 #include <netinet/ip.h>
54 #include <netinet/tcp.h>
57 #include <net/ethernet.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_poll.h>
63 #include <net/if_types.h>
64 #include <net/ifq_var.h>
65 #include <net/toeplitz.h>
66 #include <net/toeplitz2.h>
67 #include <net/vlan/if_vlan_var.h>
68 #include <net/vlan/if_vlan_ether.h>
70 #include <dev/netif/mii_layer/mii.h>
71 #include <dev/netif/mii_layer/miivar.h>
72 #include <dev/netif/mii_layer/brgphyreg.h>
74 #include <bus/pci/pcidevs.h>
75 #include <bus/pci/pcireg.h>
76 #include <bus/pci/pcivar.h>
78 #include <dev/netif/bge/if_bgereg.h>
79 #include <dev/netif/bnx/if_bnxvar.h>
81 /* "device miibus" required. See GENERIC if you get errors here. */
82 #include "miibus_if.h"
84 #define BNX_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
86 #define BNX_INTR_CKINTVL ((10 * hz) / 1000) /* 10ms */
89 #define BNX_RSS_DPRINTF(sc, lvl, fmt, ...) \
91 if (sc->bnx_rss_debug >= lvl) \
92 if_printf(&sc->arpcom.ac_if, fmt, __VA_ARGS__); \
94 #else /* !BNX_RSS_DEBUG */
95 #define BNX_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
96 #endif /* BNX_RSS_DEBUG */
98 static const struct bnx_type {
103 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717,
104 "Broadcom BCM5717 Gigabit Ethernet" },
105 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717C,
106 "Broadcom BCM5717C Gigabit Ethernet" },
107 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5718,
108 "Broadcom BCM5718 Gigabit Ethernet" },
109 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5719,
110 "Broadcom BCM5719 Gigabit Ethernet" },
111 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5720_ALT,
112 "Broadcom BCM5720 Gigabit Ethernet" },
114 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5725,
115 "Broadcom BCM5725 Gigabit Ethernet" },
116 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5727,
117 "Broadcom BCM5727 Gigabit Ethernet" },
118 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5762,
119 "Broadcom BCM5762 Gigabit Ethernet" },
121 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
122 "Broadcom BCM57761 Gigabit Ethernet" },
123 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
124 "Broadcom BCM57762 Gigabit Ethernet" },
125 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
126 "Broadcom BCM57765 Gigabit Ethernet" },
127 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
128 "Broadcom BCM57766 Gigabit Ethernet" },
129 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
130 "Broadcom BCM57781 Gigabit Ethernet" },
131 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
132 "Broadcom BCM57782 Gigabit Ethernet" },
133 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
134 "Broadcom BCM57785 Gigabit Ethernet" },
135 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
136 "Broadcom BCM57786 Gigabit Ethernet" },
137 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
138 "Broadcom BCM57791 Fast Ethernet" },
139 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
140 "Broadcom BCM57795 Fast Ethernet" },
145 static const int bnx_tx_mailbox[BNX_TX_RING_MAX] = {
146 BGE_MBX_TX_HOST_PROD0_LO,
147 BGE_MBX_TX_HOST_PROD0_HI,
148 BGE_MBX_TX_HOST_PROD1_LO,
149 BGE_MBX_TX_HOST_PROD1_HI
152 #define BNX_IS_JUMBO_CAPABLE(sc) ((sc)->bnx_flags & BNX_FLAG_JUMBO)
153 #define BNX_IS_5717_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
154 #define BNX_IS_57765_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
155 #define BNX_IS_57765_FAMILY(sc) \
156 ((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
158 typedef int (*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
160 static int bnx_probe(device_t);
161 static int bnx_attach(device_t);
162 static int bnx_detach(device_t);
163 static void bnx_shutdown(device_t);
164 static int bnx_suspend(device_t);
165 static int bnx_resume(device_t);
166 static int bnx_miibus_readreg(device_t, int, int);
167 static int bnx_miibus_writereg(device_t, int, int, int);
168 static void bnx_miibus_statchg(device_t);
170 static void bnx_handle_status(struct bnx_softc *);
172 static void bnx_npoll(struct ifnet *, struct ifpoll_info *);
173 static void bnx_npoll_rx(struct ifnet *, void *, int);
174 static void bnx_npoll_tx(struct ifnet *, void *, int);
175 static void bnx_npoll_tx_notag(struct ifnet *, void *, int);
176 static void bnx_npoll_status(struct ifnet *);
177 static void bnx_npoll_status_notag(struct ifnet *);
179 static void bnx_intr_legacy(void *);
180 static void bnx_msi(void *);
181 static void bnx_intr(struct bnx_softc *);
182 static void bnx_msix_status(void *);
183 static void bnx_msix_tx_status(void *);
184 static void bnx_msix_rx(void *);
185 static void bnx_msix_rxtx(void *);
186 static void bnx_enable_intr(struct bnx_softc *);
187 static void bnx_disable_intr(struct bnx_softc *);
188 static void bnx_txeof(struct bnx_tx_ring *, uint16_t);
189 static void bnx_rxeof(struct bnx_rx_ret_ring *, uint16_t, int);
190 static int bnx_alloc_intr(struct bnx_softc *);
191 static int bnx_setup_intr(struct bnx_softc *);
192 static void bnx_free_intr(struct bnx_softc *);
193 static void bnx_teardown_intr(struct bnx_softc *, int);
194 static int bnx_alloc_msix(struct bnx_softc *);
195 static void bnx_free_msix(struct bnx_softc *, boolean_t);
196 static void bnx_check_intr_rxtx(void *);
197 static void bnx_check_intr_rx(void *);
198 static void bnx_check_intr_tx(void *);
199 static void bnx_rx_std_refill_ithread(void *);
200 static void bnx_rx_std_refill(void *, void *);
201 static void bnx_rx_std_refill_sched_ipi(void *);
202 static void bnx_rx_std_refill_stop(void *);
203 static void bnx_rx_std_refill_sched(struct bnx_rx_ret_ring *,
204 struct bnx_rx_std_ring *);
206 static void bnx_start(struct ifnet *, struct ifaltq_subque *);
207 static int bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
208 static void bnx_init(void *);
209 static void bnx_stop(struct bnx_softc *);
210 static void bnx_watchdog(struct ifaltq_subque *);
211 static int bnx_ifmedia_upd(struct ifnet *);
212 static void bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
213 static void bnx_tick(void *);
214 static void bnx_serialize(struct ifnet *, enum ifnet_serialize);
215 static void bnx_deserialize(struct ifnet *, enum ifnet_serialize);
216 static int bnx_tryserialize(struct ifnet *, enum ifnet_serialize);
218 static void bnx_serialize_assert(struct ifnet *, enum ifnet_serialize,
221 static void bnx_serialize_skipmain(struct bnx_softc *);
222 static void bnx_deserialize_skipmain(struct bnx_softc *sc);
224 static int bnx_alloc_jumbo_mem(struct bnx_softc *);
225 static void bnx_free_jumbo_mem(struct bnx_softc *);
226 static struct bnx_jslot
227 *bnx_jalloc(struct bnx_softc *);
228 static void bnx_jfree(void *);
229 static void bnx_jref(void *);
230 static int bnx_newbuf_std(struct bnx_rx_ret_ring *, int, int);
231 static int bnx_newbuf_jumbo(struct bnx_softc *, int, int);
232 static void bnx_setup_rxdesc_std(struct bnx_rx_std_ring *, int);
233 static void bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
234 static int bnx_init_rx_ring_std(struct bnx_rx_std_ring *);
235 static void bnx_free_rx_ring_std(struct bnx_rx_std_ring *);
236 static int bnx_init_rx_ring_jumbo(struct bnx_softc *);
237 static void bnx_free_rx_ring_jumbo(struct bnx_softc *);
238 static void bnx_free_tx_ring(struct bnx_tx_ring *);
239 static int bnx_init_tx_ring(struct bnx_tx_ring *);
240 static int bnx_create_tx_ring(struct bnx_tx_ring *);
241 static void bnx_destroy_tx_ring(struct bnx_tx_ring *);
242 static int bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *);
243 static void bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *);
244 static int bnx_dma_alloc(device_t);
245 static void bnx_dma_free(struct bnx_softc *);
246 static int bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
247 bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
248 static void bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
250 bnx_defrag_shortdma(struct mbuf *);
251 static int bnx_encap(struct bnx_tx_ring *, struct mbuf **,
253 static int bnx_setup_tso(struct bnx_tx_ring *, struct mbuf **,
254 uint16_t *, uint16_t *);
255 static void bnx_setup_serialize(struct bnx_softc *);
256 static void bnx_set_tick_cpuid(struct bnx_softc *, boolean_t);
257 static void bnx_setup_ring_cnt(struct bnx_softc *);
259 static struct pktinfo *bnx_rss_info(struct pktinfo *,
260 const struct bge_rx_bd *);
261 static void bnx_init_rss(struct bnx_softc *);
262 static void bnx_reset(struct bnx_softc *);
263 static int bnx_chipinit(struct bnx_softc *);
264 static int bnx_blockinit(struct bnx_softc *);
265 static void bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
266 static void bnx_enable_msi(struct bnx_softc *, boolean_t);
267 static void bnx_setmulti(struct bnx_softc *);
268 static void bnx_setpromisc(struct bnx_softc *);
269 static void bnx_stats_update_regs(struct bnx_softc *);
270 static uint32_t bnx_dma_swap_options(struct bnx_softc *);
272 static uint32_t bnx_readmem_ind(struct bnx_softc *, uint32_t);
273 static void bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
275 static uint32_t bnx_readreg_ind(struct bnx_softc *, uint32_t);
277 static void bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
278 static void bnx_writembx(struct bnx_softc *, int, int);
279 static int bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
280 static uint8_t bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
281 static int bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
283 static void bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
284 static void bnx_copper_link_upd(struct bnx_softc *, uint32_t);
285 static void bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
286 static void bnx_link_poll(struct bnx_softc *);
288 static int bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
289 static int bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
290 static int bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
291 static int bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
293 static void bnx_coal_change(struct bnx_softc *);
294 static int bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS);
295 static int bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS);
296 static int bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
297 static int bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
298 static int bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
299 static int bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
300 static int bnx_sysctl_tx_coal_bds_poll(SYSCTL_HANDLER_ARGS);
301 static int bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
302 static int bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
303 static int bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
306 static int bnx_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS);
307 static int bnx_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS);
308 static int bnx_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS);
310 static int bnx_sysctl_std_refill(SYSCTL_HANDLER_ARGS);
312 static int bnx_msi_enable = 1;
313 static int bnx_msix_enable = 1;
315 static int bnx_rx_rings = 0; /* auto */
316 static int bnx_tx_rings = 0; /* auto */
318 TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
319 TUNABLE_INT("hw.bnx.msix.enable", &bnx_msix_enable);
320 TUNABLE_INT("hw.bnx.rx_rings", &bnx_rx_rings);
321 TUNABLE_INT("hw.bnx.tx_rings", &bnx_tx_rings);
323 static device_method_t bnx_methods[] = {
324 /* Device interface */
325 DEVMETHOD(device_probe, bnx_probe),
326 DEVMETHOD(device_attach, bnx_attach),
327 DEVMETHOD(device_detach, bnx_detach),
328 DEVMETHOD(device_shutdown, bnx_shutdown),
329 DEVMETHOD(device_suspend, bnx_suspend),
330 DEVMETHOD(device_resume, bnx_resume),
333 DEVMETHOD(bus_print_child, bus_generic_print_child),
334 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
337 DEVMETHOD(miibus_readreg, bnx_miibus_readreg),
338 DEVMETHOD(miibus_writereg, bnx_miibus_writereg),
339 DEVMETHOD(miibus_statchg, bnx_miibus_statchg),
344 static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
345 static devclass_t bnx_devclass;
347 DECLARE_DUMMY_MODULE(if_bnx);
348 DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
349 DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
352 bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
354 device_t dev = sc->bnx_dev;
357 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
358 val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
359 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
364 bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
366 device_t dev = sc->bnx_dev;
368 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
369 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
370 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
374 bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
376 CSR_WRITE_4(sc, off, val);
380 bnx_writembx(struct bnx_softc *sc, int off, int val)
382 CSR_WRITE_4(sc, off, val);
386 * Read a sequence of bytes from NVRAM.
389 bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
395 * Read a byte of data stored in the EEPROM at address 'addr.' The
396 * BCM570x supports both the traditional bitbang interface and an
397 * auto access interface for reading the EEPROM. We use the auto
401 bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
407 * Enable use of auto EEPROM access so we can avoid
408 * having to use the bitbang method.
410 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
412 /* Reset the EEPROM, load the clock period. */
413 CSR_WRITE_4(sc, BGE_EE_ADDR,
414 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
417 /* Issue the read EEPROM command. */
418 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
420 /* Wait for completion */
421 for(i = 0; i < BNX_TIMEOUT * 10; i++) {
423 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
427 if (i == BNX_TIMEOUT) {
428 if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
433 byte = CSR_READ_4(sc, BGE_EE_DATA);
435 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
441 * Read a sequence of bytes from the EEPROM.
444 bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
450 for (byte = 0, err = 0, i = 0; i < len; i++) {
451 err = bnx_eeprom_getbyte(sc, off + i, &byte);
461 bnx_miibus_readreg(device_t dev, int phy, int reg)
463 struct bnx_softc *sc = device_get_softc(dev);
467 KASSERT(phy == sc->bnx_phyno,
468 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
470 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
471 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
472 CSR_WRITE_4(sc, BGE_MI_MODE,
473 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
477 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
478 BGE_MIPHY(phy) | BGE_MIREG(reg));
480 /* Poll for the PHY register access to complete. */
481 for (i = 0; i < BNX_TIMEOUT; i++) {
483 val = CSR_READ_4(sc, BGE_MI_COMM);
484 if ((val & BGE_MICOMM_BUSY) == 0) {
486 val = CSR_READ_4(sc, BGE_MI_COMM);
490 if (i == BNX_TIMEOUT) {
491 if_printf(&sc->arpcom.ac_if, "PHY read timed out "
492 "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
496 /* Restore the autopoll bit if necessary. */
497 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
498 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
502 if (val & BGE_MICOMM_READFAIL)
505 return (val & 0xFFFF);
509 bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
511 struct bnx_softc *sc = device_get_softc(dev);
514 KASSERT(phy == sc->bnx_phyno,
515 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
517 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
518 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
519 CSR_WRITE_4(sc, BGE_MI_MODE,
520 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
524 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
525 BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
527 for (i = 0; i < BNX_TIMEOUT; i++) {
529 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
531 CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
535 if (i == BNX_TIMEOUT) {
536 if_printf(&sc->arpcom.ac_if, "PHY write timed out "
537 "(phy %d, reg %d, val %d)\n", phy, reg, val);
540 /* Restore the autopoll bit if necessary. */
541 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
542 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
550 bnx_miibus_statchg(device_t dev)
552 struct bnx_softc *sc;
553 struct mii_data *mii;
555 sc = device_get_softc(dev);
556 mii = device_get_softc(sc->bnx_miibus);
558 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
559 (IFM_ACTIVE | IFM_AVALID)) {
560 switch (IFM_SUBTYPE(mii->mii_media_active)) {
577 if (sc->bnx_link == 0)
580 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
581 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
582 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
583 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
585 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
588 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
589 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
591 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
596 * Memory management for jumbo frames.
599 bnx_alloc_jumbo_mem(struct bnx_softc *sc)
601 struct ifnet *ifp = &sc->arpcom.ac_if;
602 struct bnx_jslot *entry;
608 * Create tag for jumbo mbufs.
609 * This is really a bit of a kludge. We allocate a special
610 * jumbo buffer pool which (thanks to the way our DMA
611 * memory allocation works) will consist of contiguous
612 * pages. This means that even though a jumbo buffer might
613 * be larger than a page size, we don't really need to
614 * map it into more than one DMA segment. However, the
615 * default mbuf tag will result in multi-segment mappings,
616 * so we have to create a special jumbo mbuf tag that
617 * lets us get away with mapping the jumbo buffers as
618 * a single segment. I think eventually the driver should
619 * be changed so that it uses ordinary mbufs and cluster
620 * buffers, i.e. jumbo frames can span multiple DMA
621 * descriptors. But that's a project for another day.
625 * Create DMA stuffs for jumbo RX ring.
627 error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
628 &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
629 &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
630 (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
631 &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
633 if_printf(ifp, "could not create jumbo RX ring\n");
638 * Create DMA stuffs for jumbo buffer block.
640 error = bnx_dma_block_alloc(sc, BNX_JMEM,
641 &sc->bnx_cdata.bnx_jumbo_tag,
642 &sc->bnx_cdata.bnx_jumbo_map,
643 (void **)&sc->bnx_ldata.bnx_jumbo_buf,
646 if_printf(ifp, "could not create jumbo buffer\n");
650 SLIST_INIT(&sc->bnx_jfree_listhead);
653 * Now divide it up into 9K pieces and save the addresses
654 * in an array. Note that we play an evil trick here by using
655 * the first few bytes in the buffer to hold the the address
656 * of the softc structure for this interface. This is because
657 * bnx_jfree() needs it, but it is called by the mbuf management
658 * code which will not pass it to us explicitly.
660 for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
661 entry = &sc->bnx_cdata.bnx_jslots[i];
663 entry->bnx_buf = ptr;
664 entry->bnx_paddr = paddr;
665 entry->bnx_inuse = 0;
667 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
676 bnx_free_jumbo_mem(struct bnx_softc *sc)
678 /* Destroy jumbo RX ring. */
679 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
680 sc->bnx_cdata.bnx_rx_jumbo_ring_map,
681 sc->bnx_ldata.bnx_rx_jumbo_ring);
683 /* Destroy jumbo buffer block. */
684 bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
685 sc->bnx_cdata.bnx_jumbo_map,
686 sc->bnx_ldata.bnx_jumbo_buf);
690 * Allocate a jumbo buffer.
692 static struct bnx_jslot *
693 bnx_jalloc(struct bnx_softc *sc)
695 struct bnx_jslot *entry;
697 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
698 entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
700 SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
701 entry->bnx_inuse = 1;
703 if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
705 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
710 * Adjust usage count on a jumbo buffer.
715 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
716 struct bnx_softc *sc = entry->bnx_sc;
719 panic("bnx_jref: can't find softc pointer!");
721 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
722 panic("bnx_jref: asked to reference buffer "
723 "that we don't manage!");
724 } else if (entry->bnx_inuse == 0) {
725 panic("bnx_jref: buffer already free!");
727 atomic_add_int(&entry->bnx_inuse, 1);
732 * Release a jumbo buffer.
737 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
738 struct bnx_softc *sc = entry->bnx_sc;
741 panic("bnx_jfree: can't find softc pointer!");
743 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
744 panic("bnx_jfree: asked to free buffer that we don't manage!");
745 } else if (entry->bnx_inuse == 0) {
746 panic("bnx_jfree: buffer already free!");
749 * Possible MP race to 0, use the serializer. The atomic insn
750 * is still needed for races against bnx_jref().
752 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
753 atomic_subtract_int(&entry->bnx_inuse, 1);
754 if (entry->bnx_inuse == 0) {
755 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
758 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
764 * Intialize a standard receive ring descriptor.
767 bnx_newbuf_std(struct bnx_rx_ret_ring *ret, int i, int init)
769 struct mbuf *m_new = NULL;
770 bus_dma_segment_t seg;
773 struct bnx_rx_buf *rb;
775 rb = &ret->bnx_std->bnx_rx_std_buf[i];
776 KASSERT(!rb->bnx_rx_refilled, ("RX buf %dth has been refilled", i));
778 m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
783 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
784 m_adj(m_new, ETHER_ALIGN);
786 error = bus_dmamap_load_mbuf_segment(ret->bnx_rx_mtag,
787 ret->bnx_rx_tmpmap, m_new, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
794 bus_dmamap_sync(ret->bnx_rx_mtag, rb->bnx_rx_dmamap,
795 BUS_DMASYNC_POSTREAD);
796 bus_dmamap_unload(ret->bnx_rx_mtag, rb->bnx_rx_dmamap);
799 map = ret->bnx_rx_tmpmap;
800 ret->bnx_rx_tmpmap = rb->bnx_rx_dmamap;
802 rb->bnx_rx_dmamap = map;
803 rb->bnx_rx_mbuf = m_new;
804 rb->bnx_rx_paddr = seg.ds_addr;
805 rb->bnx_rx_len = m_new->m_len;
808 rb->bnx_rx_refilled = 1;
813 bnx_setup_rxdesc_std(struct bnx_rx_std_ring *std, int i)
815 struct bnx_rx_buf *rb;
820 rb = &std->bnx_rx_std_buf[i];
821 KASSERT(rb->bnx_rx_refilled, ("RX buf %dth is not refilled", i));
823 paddr = rb->bnx_rx_paddr;
824 len = rb->bnx_rx_len;
828 rb->bnx_rx_refilled = 0;
830 r = &std->bnx_rx_std_ring[i];
831 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(paddr);
832 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(paddr);
835 r->bge_flags = BGE_RXBDFLAG_END;
839 * Initialize a jumbo receive ring descriptor. This allocates
840 * a jumbo buffer from the pool managed internally by the driver.
843 bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
845 struct mbuf *m_new = NULL;
846 struct bnx_jslot *buf;
849 /* Allocate the mbuf. */
850 MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
854 /* Allocate the jumbo buffer */
855 buf = bnx_jalloc(sc);
861 /* Attach the buffer to the mbuf. */
862 m_new->m_ext.ext_arg = buf;
863 m_new->m_ext.ext_buf = buf->bnx_buf;
864 m_new->m_ext.ext_free = bnx_jfree;
865 m_new->m_ext.ext_ref = bnx_jref;
866 m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
868 m_new->m_flags |= M_EXT;
870 m_new->m_data = m_new->m_ext.ext_buf;
871 m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
873 paddr = buf->bnx_paddr;
874 m_adj(m_new, ETHER_ALIGN);
875 paddr += ETHER_ALIGN;
877 /* Save necessary information */
878 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_mbuf = m_new;
879 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_paddr = paddr;
881 /* Set up the descriptor. */
882 bnx_setup_rxdesc_jumbo(sc, i);
887 bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
890 struct bnx_rx_buf *rc;
892 r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
893 rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
895 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_rx_paddr);
896 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_rx_paddr);
897 r->bge_len = rc->bnx_rx_mbuf->m_len;
899 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
903 bnx_init_rx_ring_std(struct bnx_rx_std_ring *std)
907 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
908 /* Use the first RX return ring's tmp RX mbuf DMA map */
909 error = bnx_newbuf_std(&std->bnx_sc->bnx_rx_ret_ring[0], i, 1);
912 bnx_setup_rxdesc_std(std, i);
915 std->bnx_rx_std_used = 0;
916 std->bnx_rx_std_refill = 0;
917 std->bnx_rx_std_running = 0;
919 lwkt_serialize_handler_enable(&std->bnx_rx_std_serialize);
921 std->bnx_rx_std = BGE_STD_RX_RING_CNT - 1;
922 bnx_writembx(std->bnx_sc, BGE_MBX_RX_STD_PROD_LO, std->bnx_rx_std);
928 bnx_free_rx_ring_std(struct bnx_rx_std_ring *std)
932 lwkt_serialize_handler_disable(&std->bnx_rx_std_serialize);
934 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
935 struct bnx_rx_buf *rb = &std->bnx_rx_std_buf[i];
937 rb->bnx_rx_refilled = 0;
938 if (rb->bnx_rx_mbuf != NULL) {
939 bus_dmamap_unload(std->bnx_rx_mtag, rb->bnx_rx_dmamap);
940 m_freem(rb->bnx_rx_mbuf);
941 rb->bnx_rx_mbuf = NULL;
943 bzero(&std->bnx_rx_std_ring[i], sizeof(struct bge_rx_bd));
948 bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
953 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
954 error = bnx_newbuf_jumbo(sc, i, 1);
959 sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
961 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
962 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
963 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
965 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
971 bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
975 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
976 struct bnx_rx_buf *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
978 if (rc->bnx_rx_mbuf != NULL) {
979 m_freem(rc->bnx_rx_mbuf);
980 rc->bnx_rx_mbuf = NULL;
982 bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
983 sizeof(struct bge_rx_bd));
988 bnx_free_tx_ring(struct bnx_tx_ring *txr)
992 for (i = 0; i < BGE_TX_RING_CNT; i++) {
993 struct bnx_tx_buf *buf = &txr->bnx_tx_buf[i];
995 if (buf->bnx_tx_mbuf != NULL) {
996 bus_dmamap_unload(txr->bnx_tx_mtag,
998 m_freem(buf->bnx_tx_mbuf);
999 buf->bnx_tx_mbuf = NULL;
1001 bzero(&txr->bnx_tx_ring[i], sizeof(struct bge_tx_bd));
1003 txr->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
1007 bnx_init_tx_ring(struct bnx_tx_ring *txr)
1009 txr->bnx_tx_cnt = 0;
1010 txr->bnx_tx_saved_considx = 0;
1011 txr->bnx_tx_prodidx = 0;
1013 /* Initialize transmit producer index for host-memory send ring. */
1014 bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, txr->bnx_tx_prodidx);
1020 bnx_setmulti(struct bnx_softc *sc)
1023 struct ifmultiaddr *ifma;
1024 uint32_t hashes[4] = { 0, 0, 0, 0 };
1027 ifp = &sc->arpcom.ac_if;
1029 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1030 for (i = 0; i < 4; i++)
1031 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1035 /* First, zot all the existing filters. */
1036 for (i = 0; i < 4; i++)
1037 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1039 /* Now program new ones. */
1040 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1041 if (ifma->ifma_addr->sa_family != AF_LINK)
1044 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1045 ETHER_ADDR_LEN) & 0x7f;
1046 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1049 for (i = 0; i < 4; i++)
1050 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1054 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1055 * self-test results.
1058 bnx_chipinit(struct bnx_softc *sc)
1060 uint32_t dma_rw_ctl, mode_ctl;
1063 /* Set endian type before we access any non-PCI registers. */
1064 pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
1065 BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
1067 /* Clear the MAC control register */
1068 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1071 * Clear the MAC statistics block in the NIC's
1074 for (i = BGE_STATS_BLOCK;
1075 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1076 BNX_MEMWIN_WRITE(sc, i, 0);
1078 for (i = BGE_STATUS_BLOCK;
1079 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1080 BNX_MEMWIN_WRITE(sc, i, 0);
1082 if (BNX_IS_57765_FAMILY(sc)) {
1085 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1086 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1087 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1089 /* Access the lower 1K of PL PCI-E block registers. */
1090 CSR_WRITE_4(sc, BGE_MODE_CTL,
1091 val | BGE_MODECTL_PCIE_PL_SEL);
1093 val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1094 val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1095 CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1097 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1099 if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
1100 /* Fix transmit hangs */
1101 val = CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL);
1102 val |= BGE_CPMU_PADRNG_CTL_RDIV2;
1103 CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL, val);
1105 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1106 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1108 /* Access the lower 1K of DL PCI-E block registers. */
1109 CSR_WRITE_4(sc, BGE_MODE_CTL,
1110 val | BGE_MODECTL_PCIE_DL_SEL);
1112 val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1113 val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1114 val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1115 CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1117 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1120 val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1121 val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1122 val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1123 CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1127 * Set up the PCI DMA control register.
1129 dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
1131 * Disable 32bytes cache alignment for DMA write to host memory
1134 * 64bytes cache alignment for DMA write to host memory is still
1137 dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1138 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
1139 dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1141 * Enable HW workaround for controllers that misinterpret
1142 * a status tag update and leave interrupts permanently
1145 if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1146 sc->bnx_asicrev != BGE_ASICREV_BCM5762 &&
1147 !BNX_IS_57765_FAMILY(sc))
1148 dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1150 if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
1153 pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1156 * Set up general mode register.
1158 mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
1159 BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
1160 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1163 * Disable memory write invalidate. Apparently it is not supported
1164 * properly by these devices. Also ensure that INTx isn't disabled,
1165 * as these chips need it even when using MSI.
1167 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
1168 (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
1170 /* Set the timer prescaler (always 66Mhz) */
1171 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1177 bnx_blockinit(struct bnx_softc *sc)
1179 struct bnx_intr_data *intr;
1180 struct bge_rcb *rcb;
1187 * Initialize the memory window pointer register so that
1188 * we can access the first 32K of internal NIC RAM. This will
1189 * allow us to set up the TX send ring RCBs and the RX return
1190 * ring RCBs, plus other things which live in NIC memory.
1192 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1194 /* Configure mbuf pool watermarks */
1195 if (BNX_IS_57765_PLUS(sc)) {
1196 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1197 if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
1198 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
1199 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
1201 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
1202 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
1205 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1206 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1207 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1210 /* Configure DMA resource watermarks */
1211 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1212 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1214 /* Enable buffer manager */
1215 val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1217 * Change the arbitration algorithm of TXMBUF read request to
1218 * round-robin instead of priority based for BCM5719. When
1219 * TXFIFO is almost empty, RDMA will hold its request until
1220 * TXFIFO is not almost empty.
1222 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
1223 val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1224 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1225 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1226 sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1227 val |= BGE_BMANMODE_LOMBUF_ATTN;
1228 CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
1230 /* Poll for buffer manager start indication */
1231 for (i = 0; i < BNX_TIMEOUT; i++) {
1232 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1237 if (i == BNX_TIMEOUT) {
1238 if_printf(&sc->arpcom.ac_if,
1239 "buffer manager failed to start\n");
1243 /* Enable flow-through queues */
1244 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1245 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1247 /* Wait until queue initialization is complete */
1248 for (i = 0; i < BNX_TIMEOUT; i++) {
1249 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1254 if (i == BNX_TIMEOUT) {
1255 if_printf(&sc->arpcom.ac_if,
1256 "flow-through queue init failed\n");
1261 * Summary of rings supported by the controller:
1263 * Standard Receive Producer Ring
1264 * - This ring is used to feed receive buffers for "standard"
1265 * sized frames (typically 1536 bytes) to the controller.
1267 * Jumbo Receive Producer Ring
1268 * - This ring is used to feed receive buffers for jumbo sized
1269 * frames (i.e. anything bigger than the "standard" frames)
1270 * to the controller.
1272 * Mini Receive Producer Ring
1273 * - This ring is used to feed receive buffers for "mini"
1274 * sized frames to the controller.
1275 * - This feature required external memory for the controller
1276 * but was never used in a production system. Should always
1279 * Receive Return Ring
1280 * - After the controller has placed an incoming frame into a
1281 * receive buffer that buffer is moved into a receive return
1282 * ring. The driver is then responsible to passing the
1283 * buffer up to the stack. BCM5718/BCM57785 families support
1284 * multiple receive return rings.
1287 * - This ring is used for outgoing frames. BCM5719/BCM5720
1288 * support multiple send rings.
1291 /* Initialize the standard receive producer ring control block. */
1292 rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
1293 rcb->bge_hostaddr.bge_addr_lo =
1294 BGE_ADDR_LO(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
1295 rcb->bge_hostaddr.bge_addr_hi =
1296 BGE_ADDR_HI(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
1297 if (BNX_IS_57765_PLUS(sc)) {
1299 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
1300 * Bits 15-2 : Maximum RX frame size
1301 * Bit 1 : 1 = Ring Disabled, 0 = Ring ENabled
1304 rcb->bge_maxlen_flags =
1305 BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
1308 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
1309 * Bits 15-2 : Reserved (should be 0)
1310 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
1313 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1315 if (BNX_IS_5717_PLUS(sc))
1316 rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
1318 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1319 /* Write the standard receive producer ring control block. */
1320 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1321 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1322 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1323 if (!BNX_IS_5717_PLUS(sc))
1324 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1325 /* Reset the standard receive producer ring producer index. */
1326 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1329 * Initialize the jumbo RX producer ring control
1330 * block. We set the 'ring disabled' bit in the
1331 * flags field until we're actually ready to start
1332 * using this ring (i.e. once we set the MTU
1333 * high enough to require it).
1335 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1336 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
1337 /* Get the jumbo receive producer ring RCB parameters. */
1338 rcb->bge_hostaddr.bge_addr_lo =
1339 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1340 rcb->bge_hostaddr.bge_addr_hi =
1341 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1342 rcb->bge_maxlen_flags =
1343 BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
1344 BGE_RCB_FLAG_RING_DISABLED);
1345 if (BNX_IS_5717_PLUS(sc))
1346 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
1348 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1349 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1350 rcb->bge_hostaddr.bge_addr_hi);
1351 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1352 rcb->bge_hostaddr.bge_addr_lo);
1353 /* Program the jumbo receive producer ring RCB parameters. */
1354 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1355 rcb->bge_maxlen_flags);
1356 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1357 /* Reset the jumbo receive producer ring producer index. */
1358 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1362 * The BD ring replenish thresholds control how often the
1363 * hardware fetches new BD's from the producer rings in host
1364 * memory. Setting the value too low on a busy system can
1365 * starve the hardware and recue the throughpout.
1367 * Set the BD ring replentish thresholds. The recommended
1368 * values are 1/8th the number of descriptors allocated to
1372 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
1373 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1374 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
1375 BGE_JUMBO_RX_RING_CNT/8);
1377 if (BNX_IS_57765_PLUS(sc)) {
1378 CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
1379 CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
1383 * Disable all send rings by setting the 'ring disabled' bit
1384 * in the flags field of all the TX send ring control blocks,
1385 * located in NIC memory.
1387 if (BNX_IS_5717_PLUS(sc))
1389 else if (BNX_IS_57765_FAMILY(sc) ||
1390 sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1394 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1395 for (i = 0; i < limit; i++) {
1396 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1397 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1398 vrcb += sizeof(struct bge_rcb);
1402 * Configure send ring RCBs
1404 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1405 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
1406 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
1408 BGE_HOSTADDR(taddr, txr->bnx_tx_ring_paddr);
1409 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi,
1411 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo,
1413 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1414 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1415 vrcb += sizeof(struct bge_rcb);
1419 * Disable all receive return rings by setting the
1420 * 'ring disabled' bit in the flags field of all the receive
1421 * return ring control blocks, located in NIC memory.
1423 if (BNX_IS_5717_PLUS(sc)) {
1424 /* Should be 17, use 16 until we get an SRAM map. */
1426 } else if (BNX_IS_57765_FAMILY(sc) ||
1427 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1432 /* Disable all receive return rings. */
1433 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1434 for (i = 0; i < limit; i++) {
1435 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1436 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1437 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1438 BGE_RCB_FLAG_RING_DISABLED);
1439 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
1440 (i * (sizeof(uint64_t))), 0);
1441 vrcb += sizeof(struct bge_rcb);
1445 * Set up receive return rings.
1447 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1448 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
1449 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
1451 BGE_HOSTADDR(taddr, ret->bnx_rx_ret_ring_paddr);
1452 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi,
1454 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo,
1456 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1457 BGE_RCB_MAXLEN_FLAGS(BNX_RETURN_RING_CNT, 0));
1458 vrcb += sizeof(struct bge_rcb);
1461 /* Set random backoff seed for TX */
1462 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1463 sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
1464 sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
1465 sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
1466 BGE_TX_BACKOFF_SEED_MASK);
1468 /* Set inter-packet gap */
1470 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1471 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1472 val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
1473 (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
1475 CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
1478 * Specify which ring to use for packets that don't match
1481 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1484 * Configure number of RX lists. One interrupt distribution
1485 * list, sixteen active lists, one bad frames class.
1487 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1489 /* Inialize RX list placement stats mask. */
1490 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1491 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1493 /* Disable host coalescing until we get it set up */
1494 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1496 /* Poll to make sure it's shut down. */
1497 for (i = 0; i < BNX_TIMEOUT; i++) {
1498 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1503 if (i == BNX_TIMEOUT) {
1504 if_printf(&sc->arpcom.ac_if,
1505 "host coalescing engine failed to idle\n");
1509 /* Set up host coalescing defaults */
1510 sc->bnx_coal_chg = BNX_RX_COAL_TICKS_CHG |
1511 BNX_TX_COAL_TICKS_CHG |
1512 BNX_RX_COAL_BDS_CHG |
1513 BNX_TX_COAL_BDS_CHG |
1514 BNX_RX_COAL_BDS_INT_CHG |
1515 BNX_TX_COAL_BDS_INT_CHG;
1516 bnx_coal_change(sc);
1519 * Set up addresses of status blocks
1521 intr = &sc->bnx_intr_data[0];
1522 bzero(intr->bnx_status_block, BGE_STATUS_BLK_SZ);
1523 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1524 BGE_ADDR_HI(intr->bnx_status_block_paddr));
1525 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1526 BGE_ADDR_LO(intr->bnx_status_block_paddr));
1527 for (i = 1; i < sc->bnx_intr_cnt; ++i) {
1528 intr = &sc->bnx_intr_data[i];
1529 bzero(intr->bnx_status_block, BGE_STATUS_BLK_SZ);
1530 CSR_WRITE_4(sc, BGE_VEC1_STATUSBLK_ADDR_HI + ((i - 1) * 8),
1531 BGE_ADDR_HI(intr->bnx_status_block_paddr));
1532 CSR_WRITE_4(sc, BGE_VEC1_STATUSBLK_ADDR_LO + ((i - 1) * 8),
1533 BGE_ADDR_LO(intr->bnx_status_block_paddr));
1536 /* Set up status block partail update size. */
1537 val = BGE_STATBLKSZ_32BYTE;
1540 * Does not seem to have visible effect in both
1541 * bulk data (1472B UDP datagram) and tiny data
1542 * (18B UDP datagram) TX tests.
1544 val |= BGE_HCCMODE_CLRTICK_TX;
1546 /* Turn on host coalescing state machine */
1547 CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
1549 /* Turn on RX BD completion state machine and enable attentions */
1550 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1551 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1553 /* Turn on RX list placement state machine */
1554 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1556 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1557 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1558 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1559 BGE_MACMODE_FRMHDR_DMA_ENB;
1561 if (sc->bnx_flags & BNX_FLAG_TBI)
1562 val |= BGE_PORTMODE_TBI;
1563 else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
1564 val |= BGE_PORTMODE_GMII;
1566 val |= BGE_PORTMODE_MII;
1568 /* Turn on DMA, clear stats */
1569 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
1571 /* Set misc. local control, enable interrupts on attentions */
1572 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1575 /* Assert GPIO pins for PHY reset */
1576 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1577 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1578 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1579 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1582 if (sc->bnx_intr_type == PCI_INTR_TYPE_MSIX)
1583 bnx_enable_msi(sc, TRUE);
1585 /* Turn on write DMA state machine */
1586 val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
1587 /* Enable host coalescing bug fix. */
1588 val |= BGE_WDMAMODE_STATUS_TAG_FIX;
1589 if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
1590 /* Request larger DMA burst size to get better performance. */
1591 val |= BGE_WDMAMODE_BURST_ALL_DATA;
1593 CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
1596 if (BNX_IS_57765_PLUS(sc)) {
1597 uint32_t dmactl, dmactl_reg;
1599 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1600 dmactl_reg = BGE_RDMA_RSRVCTRL2;
1602 dmactl_reg = BGE_RDMA_RSRVCTRL;
1604 dmactl = CSR_READ_4(sc, dmactl_reg);
1606 * Adjust tx margin to prevent TX data corruption and
1607 * fix internal FIFO overflow.
1609 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1610 sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1611 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1612 dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
1613 BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
1614 BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
1615 dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
1616 BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
1617 BGE_RDMA_RSRVCTRL_TXMRGN_320B;
1620 * Enable fix for read DMA FIFO overruns.
1621 * The fix is to limit the number of RX BDs
1622 * the hardware would fetch at a fime.
1624 CSR_WRITE_4(sc, dmactl_reg,
1625 dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1628 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
1629 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
1630 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
1631 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
1632 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1633 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1634 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1637 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1638 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL2;
1640 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL;
1643 * Allow 4KB burst length reads for non-LSO frames.
1644 * Enable 512B burst length reads for buffer descriptors.
1646 CSR_WRITE_4(sc, ctrl_reg,
1647 CSR_READ_4(sc, ctrl_reg) |
1648 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
1649 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1652 /* Turn on read DMA state machine */
1653 val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1654 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
1655 val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
1656 if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
1657 sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
1658 sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
1659 val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1660 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1661 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
1663 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1664 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1665 val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
1666 BGE_RDMAMODE_H2BNC_VLAN_DET;
1668 * Allow multiple outstanding read requests from
1669 * non-LSO read DMA engine.
1671 val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
1673 if (sc->bnx_asicrev == BGE_ASICREV_BCM57766)
1674 val |= BGE_RDMAMODE_JMB_2K_MMRR;
1675 if (sc->bnx_flags & BNX_FLAG_TSO)
1676 val |= BGE_RDMAMODE_TSO4_ENABLE;
1677 val |= BGE_RDMAMODE_FIFO_LONG_BURST;
1678 CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
1681 /* Turn on RX data completion state machine */
1682 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1684 /* Turn on RX BD initiator state machine */
1685 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1687 /* Turn on RX data and RX BD initiator state machine */
1688 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1690 /* Turn on send BD completion state machine */
1691 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1693 /* Turn on send data completion state machine */
1694 val = BGE_SDCMODE_ENABLE;
1695 if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
1696 val |= BGE_SDCMODE_CDELAY;
1697 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
1699 /* Turn on send data initiator state machine */
1700 if (sc->bnx_flags & BNX_FLAG_TSO) {
1701 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
1702 BGE_SDIMODE_HW_LSO_PRE_DMA);
1704 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1707 /* Turn on send BD initiator state machine */
1708 val = BGE_SBDIMODE_ENABLE;
1709 if (sc->bnx_tx_ringcnt > 1)
1710 val |= BGE_SBDIMODE_MULTI_TXR;
1711 CSR_WRITE_4(sc, BGE_SBDI_MODE, val);
1713 /* Turn on send BD selector state machine */
1714 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1716 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1717 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1718 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1720 /* ack/clear link change events */
1721 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1722 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1723 BGE_MACSTAT_LINK_CHANGED);
1724 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1727 * Enable attention when the link has changed state for
1728 * devices that use auto polling.
1730 if (sc->bnx_flags & BNX_FLAG_TBI) {
1731 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1733 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
1734 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
1740 * Clear any pending link state attention.
1741 * Otherwise some link state change events may be lost until attention
1742 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
1743 * It's not necessary on newer BCM chips - perhaps enabling link
1744 * state change attentions implies clearing pending attention.
1746 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1747 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1748 BGE_MACSTAT_LINK_CHANGED);
1750 /* Enable link state change attentions. */
1751 BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1757 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1758 * against our list and return its name if we find a match. Note
1759 * that since the Broadcom controller contains VPD support, we
1760 * can get the device name string from the controller itself instead
1761 * of the compiled-in string. This is a little slow, but it guarantees
1762 * we'll always announce the right product name.
1765 bnx_probe(device_t dev)
1767 const struct bnx_type *t;
1768 uint16_t product, vendor;
1770 if (!pci_is_pcie(dev))
1773 product = pci_get_device(dev);
1774 vendor = pci_get_vendor(dev);
1776 for (t = bnx_devs; t->bnx_name != NULL; t++) {
1777 if (vendor == t->bnx_vid && product == t->bnx_did)
1780 if (t->bnx_name == NULL)
1783 device_set_desc(dev, t->bnx_name);
1788 bnx_attach(device_t dev)
1791 struct bnx_softc *sc;
1792 struct bnx_rx_std_ring *std;
1794 int error = 0, rid, capmask, i, std_cpuid, std_cpuid_def;
1795 uint8_t ether_addr[ETHER_ADDR_LEN];
1797 uintptr_t mii_priv = 0;
1798 #if defined(BNX_TSO_DEBUG) || defined(BNX_RSS_DEBUG) || defined(BNX_TSS_DEBUG)
1801 #ifdef IFPOLL_ENABLE
1802 int offset, offset_def;
1805 sc = device_get_softc(dev);
1807 callout_init_mp(&sc->bnx_tick_timer);
1808 lwkt_serialize_init(&sc->bnx_jslot_serializer);
1809 lwkt_serialize_init(&sc->bnx_main_serialize);
1811 /* Always setup interrupt mailboxes */
1812 for (i = 0; i < BNX_INTR_MAX; ++i) {
1813 callout_init_mp(&sc->bnx_intr_data[i].bnx_intr_timer);
1814 sc->bnx_intr_data[i].bnx_sc = sc;
1815 sc->bnx_intr_data[i].bnx_intr_mbx = BGE_MBX_IRQ0_LO + (i * 8);
1816 sc->bnx_intr_data[i].bnx_intr_rid = -1;
1817 sc->bnx_intr_data[i].bnx_intr_cpuid = -1;
1820 product = pci_get_device(dev);
1822 #ifndef BURN_BRIDGES
1823 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1826 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1827 mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
1829 device_printf(dev, "chip is in D%d power mode "
1830 "-- setting to D0\n", pci_get_powerstate(dev));
1832 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1834 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1835 pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
1837 #endif /* !BURN_BRIDGE */
1840 * Map control/status registers.
1842 pci_enable_busmaster(dev);
1845 sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1848 if (sc->bnx_res == NULL) {
1849 device_printf(dev, "couldn't map memory\n");
1853 sc->bnx_btag = rman_get_bustag(sc->bnx_res);
1854 sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
1856 /* Save various chip information */
1858 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
1859 BGE_PCIMISCCTL_ASICREV_SHIFT;
1860 if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
1861 /* All chips having dedicated ASICREV register have CPMU */
1862 sc->bnx_flags |= BNX_FLAG_CPMU;
1865 case PCI_PRODUCT_BROADCOM_BCM5717:
1866 case PCI_PRODUCT_BROADCOM_BCM5717C:
1867 case PCI_PRODUCT_BROADCOM_BCM5718:
1868 case PCI_PRODUCT_BROADCOM_BCM5719:
1869 case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1870 case PCI_PRODUCT_BROADCOM_BCM5725:
1871 case PCI_PRODUCT_BROADCOM_BCM5727:
1872 case PCI_PRODUCT_BROADCOM_BCM5762:
1873 sc->bnx_chipid = pci_read_config(dev,
1874 BGE_PCI_GEN2_PRODID_ASICREV, 4);
1877 case PCI_PRODUCT_BROADCOM_BCM57761:
1878 case PCI_PRODUCT_BROADCOM_BCM57762:
1879 case PCI_PRODUCT_BROADCOM_BCM57765:
1880 case PCI_PRODUCT_BROADCOM_BCM57766:
1881 case PCI_PRODUCT_BROADCOM_BCM57781:
1882 case PCI_PRODUCT_BROADCOM_BCM57782:
1883 case PCI_PRODUCT_BROADCOM_BCM57785:
1884 case PCI_PRODUCT_BROADCOM_BCM57786:
1885 case PCI_PRODUCT_BROADCOM_BCM57791:
1886 case PCI_PRODUCT_BROADCOM_BCM57795:
1887 sc->bnx_chipid = pci_read_config(dev,
1888 BGE_PCI_GEN15_PRODID_ASICREV, 4);
1892 sc->bnx_chipid = pci_read_config(dev,
1893 BGE_PCI_PRODID_ASICREV, 4);
1897 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1898 sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1900 sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
1901 sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
1903 switch (sc->bnx_asicrev) {
1904 case BGE_ASICREV_BCM5717:
1905 case BGE_ASICREV_BCM5719:
1906 case BGE_ASICREV_BCM5720:
1907 sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1910 case BGE_ASICREV_BCM5762:
1911 sc->bnx_flags |= BNX_FLAG_57765_PLUS;
1914 case BGE_ASICREV_BCM57765:
1915 case BGE_ASICREV_BCM57766:
1916 sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
1920 sc->bnx_flags |= BNX_FLAG_TSO;
1921 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
1922 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
1923 sc->bnx_flags &= ~BNX_FLAG_TSO;
1925 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1926 BNX_IS_57765_FAMILY(sc)) {
1928 * All BCM57785 and BCM5718 families chips have a bug that
1929 * under certain situation interrupt will not be enabled
1930 * even if status tag is written to interrupt mailbox.
1932 * While BCM5719 and BCM5720 have a hardware workaround
1933 * which could fix the above bug.
1934 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1937 * For the rest of the chips in these two families, we will
1938 * have to poll the status block at high rate (10ms currently)
1939 * to check whether the interrupt is hosed or not.
1940 * See bnx_check_intr_*() for details.
1942 sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1945 sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
1946 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1947 sc->bnx_asicrev == BGE_ASICREV_BCM5720)
1948 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
1950 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
1951 device_printf(dev, "CHIP ID 0x%08x; "
1952 "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
1953 sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
1956 * Set various PHY quirk flags.
1959 capmask = MII_CAPMASK_DEFAULT;
1960 if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
1961 product == PCI_PRODUCT_BROADCOM_BCM57795) {
1963 capmask &= ~BMSR_EXTSTAT;
1966 mii_priv |= BRGPHY_FLAG_WIRESPEED;
1967 if (sc->bnx_chipid == BGE_CHIPID_BCM5762_A0)
1968 mii_priv |= BRGPHY_FLAG_5762_A0;
1970 /* Initialize if_name earlier, so if_printf could be used */
1971 ifp = &sc->arpcom.ac_if;
1972 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1974 /* Try to reset the chip. */
1977 if (bnx_chipinit(sc)) {
1978 device_printf(dev, "chip initialization failed\n");
1984 * Get station address
1986 error = bnx_get_eaddr(sc, ether_addr);
1988 device_printf(dev, "failed to read station address\n");
1992 /* Setup RX/TX and interrupt count */
1993 bnx_setup_ring_cnt(sc);
1995 if ((sc->bnx_rx_retcnt == 1 && sc->bnx_tx_ringcnt == 1) ||
1996 (sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt > 1)) {
1998 * The RX ring and the corresponding TX ring processing
1999 * should be on the same CPU, since they share the same
2002 sc->bnx_flags |= BNX_FLAG_RXTX_BUNDLE;
2004 device_printf(dev, "RX/TX bundle\n");
2005 if (sc->bnx_tx_ringcnt > 1) {
2007 * Multiple TX rings do not share status block
2008 * with link status, so link status will have
2009 * to save its own status_tag.
2011 sc->bnx_flags |= BNX_FLAG_STATUS_HASTAG;
2013 device_printf(dev, "status needs tag\n");
2016 KKASSERT(sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt == 1);
2018 device_printf(dev, "RX/TX not bundled\n");
2021 error = bnx_dma_alloc(dev);
2025 #ifdef IFPOLL_ENABLE
2026 if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
2028 * NPOLLING RX/TX CPU offset
2030 if (sc->bnx_rx_retcnt == ncpus2) {
2034 (sc->bnx_rx_retcnt * device_get_unit(dev)) % ncpus2;
2035 offset = device_getenv_int(dev, "npoll.offset",
2037 if (offset >= ncpus2 ||
2038 offset % sc->bnx_rx_retcnt != 0) {
2039 device_printf(dev, "invalid npoll.offset %d, "
2040 "use %d\n", offset, offset_def);
2041 offset = offset_def;
2044 sc->bnx_npoll_rxoff = offset;
2045 sc->bnx_npoll_txoff = offset;
2048 * NPOLLING RX CPU offset
2050 if (sc->bnx_rx_retcnt == ncpus2) {
2054 (sc->bnx_rx_retcnt * device_get_unit(dev)) % ncpus2;
2055 offset = device_getenv_int(dev, "npoll.rxoff",
2057 if (offset >= ncpus2 ||
2058 offset % sc->bnx_rx_retcnt != 0) {
2059 device_printf(dev, "invalid npoll.rxoff %d, "
2060 "use %d\n", offset, offset_def);
2061 offset = offset_def;
2064 sc->bnx_npoll_rxoff = offset;
2067 * NPOLLING TX CPU offset
2069 offset_def = device_get_unit(dev) % ncpus2;
2070 offset = device_getenv_int(dev, "npoll.txoff", offset_def);
2071 if (offset >= ncpus2) {
2072 device_printf(dev, "invalid npoll.txoff %d, use %d\n",
2073 offset, offset_def);
2074 offset = offset_def;
2076 sc->bnx_npoll_txoff = offset;
2078 #endif /* IFPOLL_ENABLE */
2081 * Allocate interrupt
2083 error = bnx_alloc_intr(sc);
2087 /* Setup serializers */
2088 bnx_setup_serialize(sc);
2090 /* Set default tuneable values. */
2091 sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
2092 sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
2093 sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
2094 sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
2095 sc->bnx_tx_coal_bds_poll = BNX_TX_COAL_BDS_POLL_DEF;
2096 sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
2097 sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
2099 /* Set up ifnet structure */
2101 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2102 ifp->if_ioctl = bnx_ioctl;
2103 ifp->if_start = bnx_start;
2104 #ifdef IFPOLL_ENABLE
2105 ifp->if_npoll = bnx_npoll;
2107 ifp->if_init = bnx_init;
2108 ifp->if_serialize = bnx_serialize;
2109 ifp->if_deserialize = bnx_deserialize;
2110 ifp->if_tryserialize = bnx_tryserialize;
2112 ifp->if_serialize_assert = bnx_serialize_assert;
2114 ifp->if_mtu = ETHERMTU;
2115 ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2117 ifp->if_capabilities |= IFCAP_HWCSUM;
2118 ifp->if_hwassist = BNX_CSUM_FEATURES;
2119 if (sc->bnx_flags & BNX_FLAG_TSO) {
2120 ifp->if_capabilities |= IFCAP_TSO;
2121 ifp->if_hwassist |= CSUM_TSO;
2123 if (BNX_RSS_ENABLED(sc))
2124 ifp->if_capabilities |= IFCAP_RSS;
2125 ifp->if_capenable = ifp->if_capabilities;
2127 ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
2128 ifq_set_ready(&ifp->if_snd);
2129 ifq_set_subq_cnt(&ifp->if_snd, sc->bnx_tx_ringcnt);
2131 if (sc->bnx_tx_ringcnt > 1) {
2132 ifp->if_mapsubq = ifq_mapsubq_mask;
2133 ifq_set_subq_mask(&ifp->if_snd, sc->bnx_tx_ringcnt - 1);
2137 * Figure out what sort of media we have by checking the
2138 * hardware config word in the first 32k of NIC internal memory,
2139 * or fall back to examining the EEPROM if necessary.
2140 * Note: on some BCM5700 cards, this value appears to be unset.
2141 * If that's the case, we have to rely on identifying the NIC
2142 * by its PCI subsystem ID, as we do below for the SysKonnect
2145 if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2146 hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2148 if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2150 device_printf(dev, "failed to read EEPROM\n");
2154 hwcfg = ntohl(hwcfg);
2157 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
2158 if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
2159 (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2160 sc->bnx_flags |= BNX_FLAG_TBI;
2163 if (sc->bnx_flags & BNX_FLAG_CPMU)
2164 sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
2166 sc->bnx_mi_mode = BGE_MIMODE_BASE;
2168 /* Setup link status update stuffs */
2169 if (sc->bnx_flags & BNX_FLAG_TBI) {
2170 sc->bnx_link_upd = bnx_tbi_link_upd;
2171 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2172 } else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
2173 sc->bnx_link_upd = bnx_autopoll_link_upd;
2174 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2176 sc->bnx_link_upd = bnx_copper_link_upd;
2177 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2180 /* Set default PHY address */
2184 * PHY address mapping for various devices.
2186 * | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
2187 * ---------+-------+-------+-------+-------+
2188 * BCM57XX | 1 | X | X | X |
2189 * BCM5704 | 1 | X | 1 | X |
2190 * BCM5717 | 1 | 8 | 2 | 9 |
2191 * BCM5719 | 1 | 8 | 2 | 9 |
2192 * BCM5720 | 1 | 8 | 2 | 9 |
2194 * Other addresses may respond but they are not
2195 * IEEE compliant PHYs and should be ignored.
2197 if (BNX_IS_5717_PLUS(sc)) {
2200 f = pci_get_function(dev);
2201 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
2202 if (CSR_READ_4(sc, BGE_SGDIG_STS) &
2203 BGE_SGDIGSTS_IS_SERDES)
2204 sc->bnx_phyno = f + 8;
2206 sc->bnx_phyno = f + 1;
2208 if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
2209 BGE_CPMU_PHY_STRAP_IS_SERDES)
2210 sc->bnx_phyno = f + 8;
2212 sc->bnx_phyno = f + 1;
2216 if (sc->bnx_flags & BNX_FLAG_TBI) {
2217 ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
2218 bnx_ifmedia_upd, bnx_ifmedia_sts);
2219 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2220 ifmedia_add(&sc->bnx_ifmedia,
2221 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2222 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2223 ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
2224 sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
2226 struct mii_probe_args mii_args;
2228 mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
2229 mii_args.mii_probemask = 1 << sc->bnx_phyno;
2230 mii_args.mii_capmask = capmask;
2231 mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
2232 mii_args.mii_priv = mii_priv;
2234 error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
2236 device_printf(dev, "MII without any PHY!\n");
2242 * Create sysctl nodes.
2244 sysctl_ctx_init(&sc->bnx_sysctl_ctx);
2245 sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
2246 SYSCTL_STATIC_CHILDREN(_hw),
2248 device_get_nameunit(dev),
2250 if (sc->bnx_sysctl_tree == NULL) {
2251 device_printf(dev, "can't add sysctl node\n");
2256 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2257 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2258 "rx_rings", CTLFLAG_RD, &sc->bnx_rx_retcnt, 0, "# of RX rings");
2259 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2260 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2261 "tx_rings", CTLFLAG_RD, &sc->bnx_tx_ringcnt, 0, "# of TX rings");
2263 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2264 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2265 OID_AUTO, "rx_coal_ticks",
2266 CTLTYPE_INT | CTLFLAG_RW,
2267 sc, 0, bnx_sysctl_rx_coal_ticks, "I",
2268 "Receive coalescing ticks (usec).");
2269 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2270 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2271 OID_AUTO, "tx_coal_ticks",
2272 CTLTYPE_INT | CTLFLAG_RW,
2273 sc, 0, bnx_sysctl_tx_coal_ticks, "I",
2274 "Transmit coalescing ticks (usec).");
2275 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2276 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2277 OID_AUTO, "rx_coal_bds",
2278 CTLTYPE_INT | CTLFLAG_RW,
2279 sc, 0, bnx_sysctl_rx_coal_bds, "I",
2280 "Receive max coalesced BD count.");
2281 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2282 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2283 OID_AUTO, "tx_coal_bds",
2284 CTLTYPE_INT | CTLFLAG_RW,
2285 sc, 0, bnx_sysctl_tx_coal_bds, "I",
2286 "Transmit max coalesced BD count.");
2287 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2288 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2289 OID_AUTO, "tx_coal_bds_poll",
2290 CTLTYPE_INT | CTLFLAG_RW,
2291 sc, 0, bnx_sysctl_tx_coal_bds_poll, "I",
2292 "Transmit max coalesced BD count in polling.");
2294 * A common design characteristic for many Broadcom
2295 * client controllers is that they only support a
2296 * single outstanding DMA read operation on the PCIe
2297 * bus. This means that it will take twice as long to
2298 * fetch a TX frame that is split into header and
2299 * payload buffers as it does to fetch a single,
2300 * contiguous TX frame (2 reads vs. 1 read). For these
2301 * controllers, coalescing buffers to reduce the number
2302 * of memory reads is effective way to get maximum
2303 * performance(about 940Mbps). Without collapsing TX
2304 * buffers the maximum TCP bulk transfer performance
2305 * is about 850Mbps. However forcing coalescing mbufs
2306 * consumes a lot of CPU cycles, so leave it off by
2309 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2310 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2311 "force_defrag", CTLTYPE_INT | CTLFLAG_RW,
2312 sc, 0, bnx_sysctl_force_defrag, "I",
2313 "Force defragment on TX path");
2315 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2316 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2317 "tx_wreg", CTLTYPE_INT | CTLFLAG_RW,
2318 sc, 0, bnx_sysctl_tx_wreg, "I",
2319 "# of segments before writing to hardware register");
2321 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2322 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2323 "std_refill", CTLTYPE_INT | CTLFLAG_RW,
2324 sc, 0, bnx_sysctl_std_refill, "I",
2325 "# of packets received before scheduling standard refilling");
2327 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2328 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2329 "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2330 sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
2331 "Receive max coalesced BD count during interrupt.");
2332 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2333 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2334 "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2335 sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
2336 "Transmit max coalesced BD count during interrupt.");
2338 #ifdef IFPOLL_ENABLE
2339 if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
2340 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2341 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2342 "npoll_offset", CTLTYPE_INT | CTLFLAG_RW,
2343 sc, 0, bnx_sysctl_npoll_offset, "I",
2344 "NPOLLING cpu offset");
2346 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2347 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2348 "npoll_rxoff", CTLTYPE_INT | CTLFLAG_RW,
2349 sc, 0, bnx_sysctl_npoll_rxoff, "I",
2350 "NPOLLING RX cpu offset");
2351 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2352 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2353 "npoll_txoff", CTLTYPE_INT | CTLFLAG_RW,
2354 sc, 0, bnx_sysctl_npoll_txoff, "I",
2355 "NPOLLING TX cpu offset");
2359 #ifdef BNX_RSS_DEBUG
2360 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2361 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2362 "std_refill_mask", CTLFLAG_RD,
2363 &sc->bnx_rx_std_ring.bnx_rx_std_refill, 0, "");
2364 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2365 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2366 "std_used", CTLFLAG_RD,
2367 &sc->bnx_rx_std_ring.bnx_rx_std_used, 0, "");
2368 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2369 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2370 "rss_debug", CTLFLAG_RW, &sc->bnx_rss_debug, 0, "");
2371 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
2372 ksnprintf(desc, sizeof(desc), "rx_pkt%d", i);
2373 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2374 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2375 desc, CTLFLAG_RW, &sc->bnx_rx_ret_ring[i].bnx_rx_pkt, "");
2377 ksnprintf(desc, sizeof(desc), "rx_force_sched%d", i);
2378 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2379 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2381 &sc->bnx_rx_ret_ring[i].bnx_rx_force_sched, "");
2384 #ifdef BNX_TSS_DEBUG
2385 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
2386 ksnprintf(desc, sizeof(desc), "tx_pkt%d", i);
2387 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2388 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2389 desc, CTLFLAG_RW, &sc->bnx_tx_ring[i].bnx_tx_pkt, "");
2393 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2394 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2395 "norxbds", CTLFLAG_RW, &sc->bnx_norxbds, "");
2397 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2398 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2399 "errors", CTLFLAG_RW, &sc->bnx_errors, "");
2401 #ifdef BNX_TSO_DEBUG
2402 for (i = 0; i < BNX_TSO_NSTATS; ++i) {
2403 ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
2404 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2405 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2406 desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
2411 * Call MI attach routine.
2413 ether_ifattach(ifp, ether_addr, NULL);
2415 /* Setup TX rings and subqueues */
2416 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
2417 struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
2418 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
2420 ifsq_set_cpuid(ifsq, txr->bnx_tx_cpuid);
2421 ifsq_set_hw_serialize(ifsq, &txr->bnx_tx_serialize);
2422 ifsq_set_priv(ifsq, txr);
2423 txr->bnx_ifsq = ifsq;
2425 ifsq_watchdog_init(&txr->bnx_tx_watchdog, ifsq, bnx_watchdog);
2428 device_printf(dev, "txr %d -> cpu%d\n", i,
2433 error = bnx_setup_intr(sc);
2435 ether_ifdetach(ifp);
2438 bnx_set_tick_cpuid(sc, FALSE);
2441 * Create RX standard ring refilling thread
2443 std_cpuid_def = device_get_unit(dev) % ncpus;
2444 std_cpuid = device_getenv_int(dev, "std.cpuid", std_cpuid_def);
2445 if (std_cpuid < 0 || std_cpuid >= ncpus) {
2446 device_printf(dev, "invalid std.cpuid %d, use %d\n",
2447 std_cpuid, std_cpuid_def);
2448 std_cpuid = std_cpuid_def;
2451 std = &sc->bnx_rx_std_ring;
2452 lwkt_create(bnx_rx_std_refill_ithread, std, NULL,
2453 &std->bnx_rx_std_ithread, TDF_NOSTART | TDF_INTTHREAD, std_cpuid,
2454 "%s std", device_get_nameunit(dev));
2455 lwkt_setpri(&std->bnx_rx_std_ithread, TDPRI_INT_MED);
2456 std->bnx_rx_std_ithread.td_preemptable = lwkt_preempt;
2457 sc->bnx_flags |= BNX_FLAG_STD_THREAD;
2466 bnx_detach(device_t dev)
2468 struct bnx_softc *sc = device_get_softc(dev);
2470 if (device_is_attached(dev)) {
2471 struct ifnet *ifp = &sc->arpcom.ac_if;
2473 ifnet_serialize_all(ifp);
2476 bnx_teardown_intr(sc, sc->bnx_intr_cnt);
2477 ifnet_deserialize_all(ifp);
2479 ether_ifdetach(ifp);
2482 if (sc->bnx_flags & BNX_FLAG_STD_THREAD) {
2483 struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
2485 tsleep_interlock(std, 0);
2487 if (std->bnx_rx_std_ithread.td_gd == mycpu) {
2488 bnx_rx_std_refill_stop(std);
2490 lwkt_send_ipiq(std->bnx_rx_std_ithread.td_gd,
2491 bnx_rx_std_refill_stop, std);
2494 tsleep(std, PINTERLOCKED, "bnx_detach", 0);
2496 device_printf(dev, "RX std ithread exited\n");
2498 lwkt_synchronize_ipiqs("bnx_detach_ipiq");
2501 if (sc->bnx_flags & BNX_FLAG_TBI)
2502 ifmedia_removeall(&sc->bnx_ifmedia);
2504 device_delete_child(dev, sc->bnx_miibus);
2505 bus_generic_detach(dev);
2509 if (sc->bnx_msix_mem_res != NULL) {
2510 bus_release_resource(dev, SYS_RES_MEMORY, sc->bnx_msix_mem_rid,
2511 sc->bnx_msix_mem_res);
2513 if (sc->bnx_res != NULL) {
2514 bus_release_resource(dev, SYS_RES_MEMORY,
2515 BGE_PCI_BAR0, sc->bnx_res);
2518 if (sc->bnx_sysctl_tree != NULL)
2519 sysctl_ctx_free(&sc->bnx_sysctl_ctx);
2523 if (sc->bnx_serialize != NULL)
2524 kfree(sc->bnx_serialize, M_DEVBUF);
2530 bnx_reset(struct bnx_softc *sc)
2533 uint32_t cachesize, command, pcistate, reset;
2534 void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
2540 write_op = bnx_writemem_direct;
2542 /* Save some important PCI state. */
2543 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2544 command = pci_read_config(dev, BGE_PCI_CMD, 4);
2545 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2547 pci_write_config(dev, BGE_PCI_MISC_CTL,
2548 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2549 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2550 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2552 /* Disable fastboot on controllers that support it. */
2554 if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
2555 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
2558 * Write the magic number to SRAM at offset 0xB50.
2559 * When firmware finishes its initialization it will
2560 * write ~BGE_MAGIC_NUMBER to the same location.
2562 bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2564 reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2566 /* XXX: Broadcom Linux driver. */
2567 /* Force PCI-E 1.0a mode */
2568 if (!BNX_IS_57765_PLUS(sc) &&
2569 CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
2570 (BGE_PCIE_PHY_TSTCTL_PSCRAM |
2571 BGE_PCIE_PHY_TSTCTL_PCIE10)) {
2572 CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
2573 BGE_PCIE_PHY_TSTCTL_PSCRAM);
2575 if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
2576 /* Prevent PCIE link training during global reset */
2577 CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2582 * Set GPHY Power Down Override to leave GPHY
2583 * powered up in D0 uninitialized.
2585 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
2586 reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
2588 /* Issue global reset */
2589 write_op(sc, BGE_MISC_CFG, reset);
2593 /* XXX: Broadcom Linux driver. */
2594 if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
2597 DELAY(500000); /* wait for link training to complete */
2598 v = pci_read_config(dev, 0xc4, 4);
2599 pci_write_config(dev, 0xc4, v | (1<<15), 4);
2602 devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
2604 /* Disable no snoop and disable relaxed ordering. */
2605 devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
2607 /* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
2608 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
2609 devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
2610 devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
2613 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
2616 /* Clear error status. */
2617 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
2618 PCIEM_DEVSTS_CORR_ERR |
2619 PCIEM_DEVSTS_NFATAL_ERR |
2620 PCIEM_DEVSTS_FATAL_ERR |
2621 PCIEM_DEVSTS_UNSUPP_REQ, 2);
2623 /* Reset some of the PCI state that got zapped by reset */
2624 pci_write_config(dev, BGE_PCI_MISC_CTL,
2625 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2626 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2627 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2628 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2629 pci_write_config(dev, BGE_PCI_CMD, command, 4);
2630 write_op(sc, BGE_MISC_CFG, (65 << 1));
2632 /* Enable memory arbiter */
2633 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2636 * Poll until we see the 1's complement of the magic number.
2637 * This indicates that the firmware initialization is complete.
2639 for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
2640 val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2641 if (val == ~BGE_MAGIC_NUMBER)
2645 if (i == BNX_FIRMWARE_TIMEOUT) {
2646 if_printf(&sc->arpcom.ac_if, "firmware handshake "
2647 "timed out, found 0x%08x\n", val);
2650 /* BCM57765 A0 needs additional time before accessing. */
2651 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
2655 * XXX Wait for the value of the PCISTATE register to
2656 * return to its original pre-reset state. This is a
2657 * fairly good indicator of reset completion. If we don't
2658 * wait for the reset to fully complete, trying to read
2659 * from the device's non-PCI registers may yield garbage
2662 for (i = 0; i < BNX_TIMEOUT; i++) {
2663 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2668 /* Fix up byte swapping */
2669 CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
2671 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2674 * The 5704 in TBI mode apparently needs some special
2675 * adjustment to insure the SERDES drive level is set
2678 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
2679 (sc->bnx_flags & BNX_FLAG_TBI)) {
2682 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2683 serdescfg = (serdescfg & ~0xFFF) | 0x880;
2684 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2687 CSR_WRITE_4(sc, BGE_MI_MODE,
2688 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
2691 /* XXX: Broadcom Linux driver. */
2692 if (!BNX_IS_57765_PLUS(sc)) {
2695 /* Enable Data FIFO protection. */
2696 v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2697 CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
2702 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
2703 BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
2704 CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
2709 * Frame reception handling. This is called if there's a frame
2710 * on the receive return list.
2712 * Note: we have to be able to handle two possibilities here:
2713 * 1) the frame is from the jumbo recieve ring
2714 * 2) the frame is from the standard receive ring
2718 bnx_rxeof(struct bnx_rx_ret_ring *ret, uint16_t rx_prod, int count)
2720 struct bnx_softc *sc = ret->bnx_sc;
2721 struct bnx_rx_std_ring *std = ret->bnx_std;
2722 struct ifnet *ifp = &sc->arpcom.ac_if;
2725 while (ret->bnx_rx_saved_considx != rx_prod && count != 0) {
2726 struct pktinfo pi0, *pi = NULL;
2727 struct bge_rx_bd *cur_rx;
2728 struct bnx_rx_buf *rb;
2730 struct mbuf *m = NULL;
2731 uint16_t vlan_tag = 0;
2736 cur_rx = &ret->bnx_rx_ret_ring[ret->bnx_rx_saved_considx];
2738 rxidx = cur_rx->bge_idx;
2739 KKASSERT(rxidx < BGE_STD_RX_RING_CNT);
2741 BNX_INC(ret->bnx_rx_saved_considx, BNX_RETURN_RING_CNT);
2742 #ifdef BNX_RSS_DEBUG
2746 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2748 vlan_tag = cur_rx->bge_vlan_tag;
2751 if (ret->bnx_rx_cnt >= ret->bnx_rx_cntmax) {
2752 atomic_add_int(&std->bnx_rx_std_used, std_used);
2755 bnx_rx_std_refill_sched(ret, std);
2760 rb = &std->bnx_rx_std_buf[rxidx];
2761 m = rb->bnx_rx_mbuf;
2762 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2763 IFNET_STAT_INC(ifp, ierrors, 1);
2765 rb->bnx_rx_refilled = 1;
2768 if (bnx_newbuf_std(ret, rxidx, 0)) {
2769 IFNET_STAT_INC(ifp, ierrors, 1);
2773 IFNET_STAT_INC(ifp, ipackets, 1);
2774 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2775 m->m_pkthdr.rcvif = ifp;
2777 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2778 (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
2779 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
2780 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2781 if ((cur_rx->bge_error_flag &
2782 BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
2783 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2785 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2786 m->m_pkthdr.csum_data =
2787 cur_rx->bge_tcp_udp_csum;
2788 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
2792 if (ifp->if_capenable & IFCAP_RSS) {
2793 pi = bnx_rss_info(&pi0, cur_rx);
2795 (cur_rx->bge_flags & BGE_RXBDFLAG_RSS_HASH)) {
2796 m->m_flags |= M_HASH;
2798 toeplitz_hash(cur_rx->bge_hash);
2803 * If we received a packet with a vlan tag, pass it
2804 * to vlan_input() instead of ether_input().
2807 m->m_flags |= M_VLANTAG;
2808 m->m_pkthdr.ether_vlantag = vlan_tag;
2810 ether_input_pkt(ifp, m, pi);
2812 bnx_writembx(sc, ret->bnx_rx_mbx, ret->bnx_rx_saved_considx);
2817 cur_std_used = atomic_fetchadd_int(&std->bnx_rx_std_used,
2819 if (cur_std_used + std_used >= (BGE_STD_RX_RING_CNT / 2)) {
2820 #ifdef BNX_RSS_DEBUG
2821 ret->bnx_rx_force_sched++;
2823 bnx_rx_std_refill_sched(ret, std);
2829 bnx_txeof(struct bnx_tx_ring *txr, uint16_t tx_cons)
2831 struct ifnet *ifp = &txr->bnx_sc->arpcom.ac_if;
2834 * Go through our tx ring and free mbufs for those
2835 * frames that have been sent.
2837 while (txr->bnx_tx_saved_considx != tx_cons) {
2838 struct bnx_tx_buf *buf;
2841 idx = txr->bnx_tx_saved_considx;
2842 buf = &txr->bnx_tx_buf[idx];
2843 if (buf->bnx_tx_mbuf != NULL) {
2844 IFNET_STAT_INC(ifp, opackets, 1);
2845 #ifdef BNX_TSS_DEBUG
2848 bus_dmamap_unload(txr->bnx_tx_mtag,
2849 buf->bnx_tx_dmamap);
2850 m_freem(buf->bnx_tx_mbuf);
2851 buf->bnx_tx_mbuf = NULL;
2854 BNX_INC(txr->bnx_tx_saved_considx, BGE_TX_RING_CNT);
2857 if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) >=
2858 (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
2859 ifsq_clr_oactive(txr->bnx_ifsq);
2861 if (txr->bnx_tx_cnt == 0)
2862 txr->bnx_tx_watchdog.wd_timer = 0;
2864 if (!ifsq_is_empty(txr->bnx_ifsq))
2865 ifsq_devstart(txr->bnx_ifsq);
2869 bnx_handle_status(struct bnx_softc *sc)
2873 status = *sc->bnx_hw_status;
2875 if (status & BGE_STATFLAG_ERROR) {
2881 val = CSR_READ_4(sc, BGE_FLOW_ATTN);
2882 if (val & ~BGE_FLOWATTN_MB_LOWAT) {
2883 if_printf(&sc->arpcom.ac_if,
2884 "flow attn 0x%08x\n", val);
2888 val = CSR_READ_4(sc, BGE_MSI_STATUS);
2889 if (val & ~BGE_MSISTAT_MSI_PCI_REQ) {
2890 if_printf(&sc->arpcom.ac_if,
2891 "msi status 0x%08x\n", val);
2895 val = CSR_READ_4(sc, BGE_RDMA_STATUS);
2897 if_printf(&sc->arpcom.ac_if,
2898 "rmda status 0x%08x\n", val);
2902 val = CSR_READ_4(sc, BGE_WDMA_STATUS);
2904 if_printf(&sc->arpcom.ac_if,
2905 "wdma status 0x%08x\n", val);
2910 bnx_serialize_skipmain(sc);
2912 bnx_deserialize_skipmain(sc);
2916 if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
2920 #ifdef IFPOLL_ENABLE
2923 bnx_npoll_rx(struct ifnet *ifp __unused, void *xret, int cycle)
2925 struct bnx_rx_ret_ring *ret = xret;
2928 ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
2930 ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
2933 rx_prod = *ret->bnx_rx_considx;
2934 if (ret->bnx_rx_saved_considx != rx_prod)
2935 bnx_rxeof(ret, rx_prod, cycle);
2939 bnx_npoll_tx_notag(struct ifnet *ifp __unused, void *xtxr, int cycle __unused)
2941 struct bnx_tx_ring *txr = xtxr;
2944 ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
2946 tx_cons = *txr->bnx_tx_considx;
2947 if (txr->bnx_tx_saved_considx != tx_cons)
2948 bnx_txeof(txr, tx_cons);
2952 bnx_npoll_tx(struct ifnet *ifp, void *xtxr, int cycle)
2954 struct bnx_tx_ring *txr = xtxr;
2956 ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
2958 txr->bnx_saved_status_tag = *txr->bnx_hw_status_tag;
2960 bnx_npoll_tx_notag(ifp, txr, cycle);
2964 bnx_npoll_status_notag(struct ifnet *ifp)
2966 struct bnx_softc *sc = ifp->if_softc;
2968 ASSERT_SERIALIZED(&sc->bnx_main_serialize);
2970 bnx_handle_status(sc);
2974 bnx_npoll_status(struct ifnet *ifp)
2976 struct bnx_softc *sc = ifp->if_softc;
2978 ASSERT_SERIALIZED(&sc->bnx_main_serialize);
2980 sc->bnx_saved_status_tag = *sc->bnx_hw_status_tag;
2982 bnx_npoll_status_notag(ifp);
2986 bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2988 struct bnx_softc *sc = ifp->if_softc;
2991 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2994 if (sc->bnx_flags & BNX_FLAG_STATUS_HASTAG)
2995 info->ifpi_status.status_func = bnx_npoll_status;
2997 info->ifpi_status.status_func = bnx_npoll_status_notag;
2998 info->ifpi_status.serializer = &sc->bnx_main_serialize;
3000 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
3001 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
3002 int idx = i + sc->bnx_npoll_txoff;
3004 KKASSERT(idx < ncpus2);
3005 if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
3006 info->ifpi_tx[idx].poll_func =
3009 info->ifpi_tx[idx].poll_func = bnx_npoll_tx;
3011 info->ifpi_tx[idx].arg = txr;
3012 info->ifpi_tx[idx].serializer = &txr->bnx_tx_serialize;
3013 ifsq_set_cpuid(txr->bnx_ifsq, idx);
3016 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
3017 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
3018 int idx = i + sc->bnx_npoll_rxoff;
3020 KKASSERT(idx < ncpus2);
3021 info->ifpi_rx[idx].poll_func = bnx_npoll_rx;
3022 info->ifpi_rx[idx].arg = ret;
3023 info->ifpi_rx[idx].serializer =
3024 &ret->bnx_rx_ret_serialize;
3027 if (ifp->if_flags & IFF_RUNNING) {
3028 bnx_disable_intr(sc);
3029 bnx_set_tick_cpuid(sc, TRUE);
3031 sc->bnx_coal_chg = BNX_TX_COAL_BDS_CHG;
3032 bnx_coal_change(sc);
3035 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
3036 ifsq_set_cpuid(sc->bnx_tx_ring[i].bnx_ifsq,
3037 sc->bnx_tx_ring[i].bnx_tx_cpuid);
3039 if (ifp->if_flags & IFF_RUNNING) {
3040 sc->bnx_coal_chg = BNX_TX_COAL_BDS_CHG;
3041 bnx_coal_change(sc);
3043 bnx_enable_intr(sc);
3044 bnx_set_tick_cpuid(sc, FALSE);
3049 #endif /* IFPOLL_ENABLE */
3052 bnx_intr_legacy(void *xsc)
3054 struct bnx_softc *sc = xsc;
3055 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
3057 if (ret->bnx_saved_status_tag == *ret->bnx_hw_status_tag) {
3060 val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
3061 if (val & BGE_PCISTAT_INTR_NOTACT)
3067 * Interrupt will have to be disabled if tagged status
3068 * is used, else interrupt will always be asserted on
3069 * certain chips (at least on BCM5750 AX/BX).
3071 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
3083 bnx_intr(struct bnx_softc *sc)
3085 struct ifnet *ifp = &sc->arpcom.ac_if;
3086 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
3088 ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3090 ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
3092 * Use a load fence to ensure that status_tag is saved
3093 * before rx_prod, tx_cons and status.
3097 bnx_handle_status(sc);
3099 if (ifp->if_flags & IFF_RUNNING) {
3100 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
3101 uint16_t rx_prod, tx_cons;
3103 lwkt_serialize_enter(&ret->bnx_rx_ret_serialize);
3104 rx_prod = *ret->bnx_rx_considx;
3105 if (ret->bnx_rx_saved_considx != rx_prod)
3106 bnx_rxeof(ret, rx_prod, -1);
3107 lwkt_serialize_exit(&ret->bnx_rx_ret_serialize);
3109 lwkt_serialize_enter(&txr->bnx_tx_serialize);
3110 tx_cons = *txr->bnx_tx_considx;
3111 if (txr->bnx_tx_saved_considx != tx_cons)
3112 bnx_txeof(txr, tx_cons);
3113 lwkt_serialize_exit(&txr->bnx_tx_serialize);
3116 bnx_writembx(sc, BGE_MBX_IRQ0_LO, ret->bnx_saved_status_tag << 24);
3120 bnx_msix_tx_status(void *xtxr)
3122 struct bnx_tx_ring *txr = xtxr;
3123 struct bnx_softc *sc = txr->bnx_sc;
3124 struct ifnet *ifp = &sc->arpcom.ac_if;
3126 ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3128 txr->bnx_saved_status_tag = *txr->bnx_hw_status_tag;
3130 * Use a load fence to ensure that status_tag is saved
3131 * before tx_cons and status.
3135 bnx_handle_status(sc);
3137 if (ifp->if_flags & IFF_RUNNING) {
3140 lwkt_serialize_enter(&txr->bnx_tx_serialize);
3141 tx_cons = *txr->bnx_tx_considx;
3142 if (txr->bnx_tx_saved_considx != tx_cons)
3143 bnx_txeof(txr, tx_cons);
3144 lwkt_serialize_exit(&txr->bnx_tx_serialize);
3147 bnx_writembx(sc, BGE_MBX_IRQ0_LO, txr->bnx_saved_status_tag << 24);
3151 bnx_msix_rx(void *xret)
3153 struct bnx_rx_ret_ring *ret = xret;
3156 ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
3158 ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
3160 * Use a load fence to ensure that status_tag is saved
3165 rx_prod = *ret->bnx_rx_considx;
3166 if (ret->bnx_rx_saved_considx != rx_prod)
3167 bnx_rxeof(ret, rx_prod, -1);
3169 bnx_writembx(ret->bnx_sc, ret->bnx_msix_mbx,
3170 ret->bnx_saved_status_tag << 24);
3174 bnx_msix_rxtx(void *xret)
3176 struct bnx_rx_ret_ring *ret = xret;
3177 struct bnx_tx_ring *txr = ret->bnx_txr;
3178 uint16_t rx_prod, tx_cons;
3180 ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
3182 ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
3184 * Use a load fence to ensure that status_tag is saved
3185 * before rx_prod and tx_cons.
3189 rx_prod = *ret->bnx_rx_considx;
3190 if (ret->bnx_rx_saved_considx != rx_prod)
3191 bnx_rxeof(ret, rx_prod, -1);
3193 lwkt_serialize_enter(&txr->bnx_tx_serialize);
3194 tx_cons = *txr->bnx_tx_considx;
3195 if (txr->bnx_tx_saved_considx != tx_cons)
3196 bnx_txeof(txr, tx_cons);
3197 lwkt_serialize_exit(&txr->bnx_tx_serialize);
3199 bnx_writembx(ret->bnx_sc, ret->bnx_msix_mbx,
3200 ret->bnx_saved_status_tag << 24);
3204 bnx_msix_status(void *xsc)
3206 struct bnx_softc *sc = xsc;
3208 ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3210 sc->bnx_saved_status_tag = *sc->bnx_hw_status_tag;
3212 * Use a load fence to ensure that status_tag is saved
3217 bnx_handle_status(sc);
3219 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_saved_status_tag << 24);
3225 struct bnx_softc *sc = xsc;
3227 lwkt_serialize_enter(&sc->bnx_main_serialize);
3229 bnx_stats_update_regs(sc);
3231 if (sc->bnx_flags & BNX_FLAG_TBI) {
3233 * Since in TBI mode auto-polling can't be used we should poll
3234 * link status manually. Here we register pending link event
3235 * and trigger interrupt.
3238 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3239 } else if (!sc->bnx_link) {
3240 mii_tick(device_get_softc(sc->bnx_miibus));
3243 callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
3244 sc->bnx_tick_cpuid);
3246 lwkt_serialize_exit(&sc->bnx_main_serialize);
3250 bnx_stats_update_regs(struct bnx_softc *sc)
3252 struct ifnet *ifp = &sc->arpcom.ac_if;
3253 struct bge_mac_stats_regs stats;
3257 s = (uint32_t *)&stats;
3258 for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
3259 *s = CSR_READ_4(sc, BGE_RX_STATS + i);
3263 IFNET_STAT_SET(ifp, collisions,
3264 (stats.dot3StatsSingleCollisionFrames +
3265 stats.dot3StatsMultipleCollisionFrames +
3266 stats.dot3StatsExcessiveCollisions +
3267 stats.dot3StatsLateCollisions));
3269 val = CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
3270 sc->bnx_norxbds += val;
3274 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
3275 * pointers to descriptors.
3278 bnx_encap(struct bnx_tx_ring *txr, struct mbuf **m_head0, uint32_t *txidx,
3281 struct bge_tx_bd *d = NULL;
3282 uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
3283 bus_dma_segment_t segs[BNX_NSEG_NEW];
3285 int error, maxsegs, nsegs, idx, i;
3286 struct mbuf *m_head = *m_head0, *m_new;
3288 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3289 #ifdef BNX_TSO_DEBUG
3293 error = bnx_setup_tso(txr, m_head0, &mss, &csum_flags);
3298 #ifdef BNX_TSO_DEBUG
3299 tso_nsegs = (m_head->m_pkthdr.len /
3300 m_head->m_pkthdr.tso_segsz) - 1;
3301 if (tso_nsegs > (BNX_TSO_NSTATS - 1))
3302 tso_nsegs = BNX_TSO_NSTATS - 1;
3303 else if (tso_nsegs < 0)
3305 txr->bnx_sc->bnx_tsosegs[tso_nsegs]++;
3307 } else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
3308 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
3309 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3310 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
3311 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3312 if (m_head->m_flags & M_LASTFRAG)
3313 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
3314 else if (m_head->m_flags & M_FRAG)
3315 csum_flags |= BGE_TXBDFLAG_IP_FRAG;
3317 if (m_head->m_flags & M_VLANTAG) {
3318 csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
3319 vlan_tag = m_head->m_pkthdr.ether_vlantag;
3323 map = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
3325 maxsegs = (BGE_TX_RING_CNT - txr->bnx_tx_cnt) - BNX_NSEG_RSVD;
3326 KASSERT(maxsegs >= BNX_NSEG_SPARE,
3327 ("not enough segments %d", maxsegs));
3329 if (maxsegs > BNX_NSEG_NEW)
3330 maxsegs = BNX_NSEG_NEW;
3333 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
3334 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
3335 * but when such padded frames employ the bge IP/TCP checksum
3336 * offload, the hardware checksum assist gives incorrect results
3337 * (possibly from incorporating its own padding into the UDP/TCP
3338 * checksum; who knows). If we pad such runts with zeros, the
3339 * onboard checksum comes out correct.
3341 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
3342 m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
3343 error = m_devpad(m_head, BNX_MIN_FRAMELEN);
3348 if ((txr->bnx_tx_flags & BNX_TX_FLAG_SHORTDMA) &&
3349 m_head->m_next != NULL) {
3350 m_new = bnx_defrag_shortdma(m_head);
3351 if (m_new == NULL) {
3355 *m_head0 = m_head = m_new;
3357 if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
3358 (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG) &&
3359 m_head->m_next != NULL) {
3361 * Forcefully defragment mbuf chain to overcome hardware
3362 * limitation which only support a single outstanding
3363 * DMA read operation. If it fails, keep moving on using
3364 * the original mbuf chain.
3366 m_new = m_defrag(m_head, MB_DONTWAIT);
3368 *m_head0 = m_head = m_new;
3371 error = bus_dmamap_load_mbuf_defrag(txr->bnx_tx_mtag, map,
3372 m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
3375 *segs_used += nsegs;
3378 bus_dmamap_sync(txr->bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
3380 for (i = 0; ; i++) {
3381 d = &txr->bnx_tx_ring[idx];
3383 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
3384 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
3385 d->bge_len = segs[i].ds_len;
3386 d->bge_flags = csum_flags;
3387 d->bge_vlan_tag = vlan_tag;
3392 BNX_INC(idx, BGE_TX_RING_CNT);
3394 /* Mark the last segment as end of packet... */
3395 d->bge_flags |= BGE_TXBDFLAG_END;
3398 * Insure that the map for this transmission is placed at
3399 * the array index of the last descriptor in this chain.
3401 txr->bnx_tx_buf[*txidx].bnx_tx_dmamap = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
3402 txr->bnx_tx_buf[idx].bnx_tx_dmamap = map;
3403 txr->bnx_tx_buf[idx].bnx_tx_mbuf = m_head;
3404 txr->bnx_tx_cnt += nsegs;
3406 BNX_INC(idx, BGE_TX_RING_CNT);
3417 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3418 * to the mbuf data regions directly in the transmit descriptors.
3421 bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3423 struct bnx_tx_ring *txr = ifsq_get_priv(ifsq);
3424 struct mbuf *m_head = NULL;
3428 KKASSERT(txr->bnx_ifsq == ifsq);
3429 ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
3431 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
3434 prodidx = txr->bnx_tx_prodidx;
3436 while (txr->bnx_tx_buf[prodidx].bnx_tx_mbuf == NULL) {
3438 * Sanity check: avoid coming within BGE_NSEG_RSVD
3439 * descriptors of the end of the ring. Also make
3440 * sure there are BGE_NSEG_SPARE descriptors for
3441 * jumbo buffers' or TSO segments' defragmentation.
3443 if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) <
3444 (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
3445 ifsq_set_oactive(ifsq);
3449 m_head = ifsq_dequeue(ifsq, NULL);
3454 * Pack the data into the transmit ring. If we
3455 * don't have room, set the OACTIVE flag and wait
3456 * for the NIC to drain the ring.
3458 if (bnx_encap(txr, &m_head, &prodidx, &nsegs)) {
3459 ifsq_set_oactive(ifsq);
3460 IFNET_STAT_INC(ifp, oerrors, 1);
3464 if (nsegs >= txr->bnx_tx_wreg) {
3466 bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3470 ETHER_BPF_MTAP(ifp, m_head);
3473 * Set a timeout in case the chip goes out to lunch.
3475 txr->bnx_tx_watchdog.wd_timer = 5;
3480 bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3482 txr->bnx_tx_prodidx = prodidx;
3488 struct bnx_softc *sc = xsc;
3489 struct ifnet *ifp = &sc->arpcom.ac_if;
3495 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3497 /* Cancel pending I/O and flush buffers. */
3503 * Init the various state machines, ring
3504 * control blocks and firmware.
3506 if (bnx_blockinit(sc)) {
3507 if_printf(ifp, "initialization failure\n");
3513 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3514 ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
3516 /* Load our MAC address. */
3517 m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
3518 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3519 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3521 /* Enable or disable promiscuous mode as needed. */
3524 /* Program multicast filter. */
3528 if (bnx_init_rx_ring_std(&sc->bnx_rx_std_ring)) {
3529 if_printf(ifp, "RX ring initialization failed\n");
3534 /* Init jumbo RX ring. */
3535 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
3536 if (bnx_init_rx_ring_jumbo(sc)) {
3537 if_printf(ifp, "Jumbo RX ring initialization failed\n");
3543 /* Init our RX return ring index */
3544 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
3545 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
3547 ret->bnx_rx_saved_considx = 0;
3548 ret->bnx_rx_cnt = 0;
3552 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
3553 bnx_init_tx_ring(&sc->bnx_tx_ring[i]);
3555 /* Enable TX MAC state machine lockup fix. */
3556 mode = CSR_READ_4(sc, BGE_TX_MODE);
3557 mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
3558 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
3559 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
3560 mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3561 mode |= CSR_READ_4(sc, BGE_TX_MODE) &
3562 (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3564 /* Turn on transmitter */
3565 CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
3567 /* Initialize RSS */
3568 mode = BGE_RXMODE_ENABLE;
3569 if (BNX_RSS_ENABLED(sc)) {
3571 mode |= BGE_RXMODE_RSS_ENABLE |
3572 BGE_RXMODE_RSS_HASH_MASK_BITS |
3573 BGE_RXMODE_RSS_IPV4_HASH |
3574 BGE_RXMODE_RSS_TCP_IPV4_HASH;
3576 /* Turn on receiver */
3577 BNX_SETBIT(sc, BGE_RX_MODE, mode);
3580 * Set the number of good frames to receive after RX MBUF
3581 * Low Watermark has been reached. After the RX MAC receives
3582 * this number of frames, it will drop subsequent incoming
3583 * frames until the MBUF High Watermark is reached.
3585 if (BNX_IS_57765_FAMILY(sc))
3586 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
3588 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
3590 if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI ||
3591 sc->bnx_intr_type == PCI_INTR_TYPE_MSIX) {
3593 if_printf(ifp, "MSI_MODE: %#x\n",
3594 CSR_READ_4(sc, BGE_MSI_MODE));
3598 /* Tell firmware we're alive. */
3599 BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3601 /* Enable host interrupts if polling(4) is not enabled. */
3602 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
3605 #ifdef IFPOLL_ENABLE
3606 if (ifp->if_flags & IFF_NPOLLING)
3610 bnx_disable_intr(sc);
3612 bnx_enable_intr(sc);
3613 bnx_set_tick_cpuid(sc, polling);
3615 bnx_ifmedia_upd(ifp);
3617 ifp->if_flags |= IFF_RUNNING;
3618 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
3619 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
3621 ifsq_clr_oactive(txr->bnx_ifsq);
3622 ifsq_watchdog_start(&txr->bnx_tx_watchdog);
3625 callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
3626 sc->bnx_tick_cpuid);
3630 * Set media options.
3633 bnx_ifmedia_upd(struct ifnet *ifp)
3635 struct bnx_softc *sc = ifp->if_softc;
3637 /* If this is a 1000baseX NIC, enable the TBI port. */
3638 if (sc->bnx_flags & BNX_FLAG_TBI) {
3639 struct ifmedia *ifm = &sc->bnx_ifmedia;
3641 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3644 switch(IFM_SUBTYPE(ifm->ifm_media)) {
3649 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3650 BNX_CLRBIT(sc, BGE_MAC_MODE,
3651 BGE_MACMODE_HALF_DUPLEX);
3653 BNX_SETBIT(sc, BGE_MAC_MODE,
3654 BGE_MACMODE_HALF_DUPLEX);
3661 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3665 if (mii->mii_instance) {
3666 struct mii_softc *miisc;
3668 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3669 mii_phy_reset(miisc);
3674 * Force an interrupt so that we will call bnx_link_upd
3675 * if needed and clear any pending link state attention.
3676 * Without this we are not getting any further interrupts
3677 * for link state changes and thus will not UP the link and
3678 * not be able to send in bnx_start. The only way to get
3679 * things working was to receive a packet and get an RX
3682 * bnx_tick should help for fiber cards and we might not
3683 * need to do this here if BNX_FLAG_TBI is set but as
3684 * we poll for fiber anyway it should not harm.
3686 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3692 * Report current media status.
3695 bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3697 struct bnx_softc *sc = ifp->if_softc;
3699 if (sc->bnx_flags & BNX_FLAG_TBI) {
3700 ifmr->ifm_status = IFM_AVALID;
3701 ifmr->ifm_active = IFM_ETHER;
3702 if (CSR_READ_4(sc, BGE_MAC_STS) &
3703 BGE_MACSTAT_TBI_PCS_SYNCHED) {
3704 ifmr->ifm_status |= IFM_ACTIVE;
3706 ifmr->ifm_active |= IFM_NONE;
3710 ifmr->ifm_active |= IFM_1000_SX;
3711 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3712 ifmr->ifm_active |= IFM_HDX;
3714 ifmr->ifm_active |= IFM_FDX;
3716 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3719 ifmr->ifm_active = mii->mii_media_active;
3720 ifmr->ifm_status = mii->mii_media_status;
3725 bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
3727 struct bnx_softc *sc = ifp->if_softc;
3728 struct ifreq *ifr = (struct ifreq *)data;
3729 int mask, error = 0;
3731 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3735 if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
3736 (BNX_IS_JUMBO_CAPABLE(sc) &&
3737 ifr->ifr_mtu > BNX_JUMBO_MTU)) {
3739 } else if (ifp->if_mtu != ifr->ifr_mtu) {
3740 ifp->if_mtu = ifr->ifr_mtu;
3741 if (ifp->if_flags & IFF_RUNNING)
3746 if (ifp->if_flags & IFF_UP) {
3747 if (ifp->if_flags & IFF_RUNNING) {
3748 mask = ifp->if_flags ^ sc->bnx_if_flags;
3751 * If only the state of the PROMISC flag
3752 * changed, then just use the 'set promisc
3753 * mode' command instead of reinitializing
3754 * the entire NIC. Doing a full re-init
3755 * means reloading the firmware and waiting
3756 * for it to start up, which may take a
3757 * second or two. Similarly for ALLMULTI.
3759 if (mask & IFF_PROMISC)
3761 if (mask & IFF_ALLMULTI)
3766 } else if (ifp->if_flags & IFF_RUNNING) {
3769 sc->bnx_if_flags = ifp->if_flags;
3773 if (ifp->if_flags & IFF_RUNNING)
3778 if (sc->bnx_flags & BNX_FLAG_TBI) {
3779 error = ifmedia_ioctl(ifp, ifr,
3780 &sc->bnx_ifmedia, command);
3782 struct mii_data *mii;
3784 mii = device_get_softc(sc->bnx_miibus);
3785 error = ifmedia_ioctl(ifp, ifr,
3786 &mii->mii_media, command);
3790 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3791 if (mask & IFCAP_HWCSUM) {
3792 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
3793 if (ifp->if_capenable & IFCAP_TXCSUM)
3794 ifp->if_hwassist |= BNX_CSUM_FEATURES;
3796 ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
3798 if (mask & IFCAP_TSO) {
3799 ifp->if_capenable ^= (mask & IFCAP_TSO);
3800 if (ifp->if_capenable & IFCAP_TSO)
3801 ifp->if_hwassist |= CSUM_TSO;
3803 ifp->if_hwassist &= ~CSUM_TSO;
3805 if (mask & IFCAP_RSS)
3806 ifp->if_capenable ^= IFCAP_RSS;
3809 error = ether_ioctl(ifp, command, data);
3816 bnx_watchdog(struct ifaltq_subque *ifsq)
3818 struct ifnet *ifp = ifsq_get_ifp(ifsq);
3819 struct bnx_softc *sc = ifp->if_softc;
3822 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3824 if_printf(ifp, "watchdog timeout -- resetting\n");
3828 IFNET_STAT_INC(ifp, oerrors, 1);
3830 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
3831 ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
3835 * Stop the adapter and free any mbufs allocated to the
3839 bnx_stop(struct bnx_softc *sc)
3841 struct ifnet *ifp = &sc->arpcom.ac_if;
3844 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3846 callout_stop(&sc->bnx_tick_timer);
3849 * Disable all of the receiver blocks
3851 bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3852 bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3853 bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3854 bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3855 bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3856 bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3859 * Disable all of the transmit blocks
3861 bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3862 bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3863 bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3864 bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3865 bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3866 bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3869 * Shut down all of the memory managers and related
3872 bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3873 bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3874 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3875 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3877 /* Disable host interrupts. */
3878 bnx_disable_intr(sc);
3881 * Tell firmware we're shutting down.
3883 BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3885 /* Free the RX lists. */
3886 bnx_free_rx_ring_std(&sc->bnx_rx_std_ring);
3888 /* Free jumbo RX list. */
3889 if (BNX_IS_JUMBO_CAPABLE(sc))
3890 bnx_free_rx_ring_jumbo(sc);
3892 /* Free TX buffers. */
3893 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
3894 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
3896 txr->bnx_saved_status_tag = 0;
3897 bnx_free_tx_ring(txr);
3900 /* Clear saved status tag */
3901 for (i = 0; i < sc->bnx_rx_retcnt; ++i)
3902 sc->bnx_rx_ret_ring[i].bnx_saved_status_tag = 0;
3905 sc->bnx_coal_chg = 0;
3907 ifp->if_flags &= ~IFF_RUNNING;
3908 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
3909 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
3911 ifsq_clr_oactive(txr->bnx_ifsq);
3912 ifsq_watchdog_stop(&txr->bnx_tx_watchdog);
3917 * Stop all chip I/O so that the kernel's probe routines don't
3918 * get confused by errant DMAs when rebooting.
3921 bnx_shutdown(device_t dev)
3923 struct bnx_softc *sc = device_get_softc(dev);
3924 struct ifnet *ifp = &sc->arpcom.ac_if;
3926 ifnet_serialize_all(ifp);
3929 ifnet_deserialize_all(ifp);
3933 bnx_suspend(device_t dev)
3935 struct bnx_softc *sc = device_get_softc(dev);
3936 struct ifnet *ifp = &sc->arpcom.ac_if;
3938 ifnet_serialize_all(ifp);
3940 ifnet_deserialize_all(ifp);
3946 bnx_resume(device_t dev)
3948 struct bnx_softc *sc = device_get_softc(dev);
3949 struct ifnet *ifp = &sc->arpcom.ac_if;
3951 ifnet_serialize_all(ifp);
3953 if (ifp->if_flags & IFF_UP) {
3957 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
3958 ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
3961 ifnet_deserialize_all(ifp);
3967 bnx_setpromisc(struct bnx_softc *sc)
3969 struct ifnet *ifp = &sc->arpcom.ac_if;
3971 if (ifp->if_flags & IFF_PROMISC)
3972 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3974 BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3978 bnx_dma_free(struct bnx_softc *sc)
3980 struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
3983 /* Destroy RX return rings */
3984 if (sc->bnx_rx_ret_ring != NULL) {
3985 for (i = 0; i < sc->bnx_rx_retcnt; ++i)
3986 bnx_destroy_rx_ret_ring(&sc->bnx_rx_ret_ring[i]);
3987 kfree(sc->bnx_rx_ret_ring, M_DEVBUF);
3990 /* Destroy RX mbuf DMA stuffs. */
3991 if (std->bnx_rx_mtag != NULL) {
3992 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3993 KKASSERT(std->bnx_rx_std_buf[i].bnx_rx_mbuf == NULL);
3994 bus_dmamap_destroy(std->bnx_rx_mtag,
3995 std->bnx_rx_std_buf[i].bnx_rx_dmamap);
3997 bus_dma_tag_destroy(std->bnx_rx_mtag);
4000 /* Destroy standard RX ring */
4001 bnx_dma_block_free(std->bnx_rx_std_ring_tag,
4002 std->bnx_rx_std_ring_map, std->bnx_rx_std_ring);
4004 /* Destroy TX rings */
4005 if (sc->bnx_tx_ring != NULL) {
4006 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
4007 bnx_destroy_tx_ring(&sc->bnx_tx_ring[i]);
4008 kfree(sc->bnx_tx_ring, M_DEVBUF);
4011 if (BNX_IS_JUMBO_CAPABLE(sc))
4012 bnx_free_jumbo_mem(sc);
4014 /* Destroy status blocks */
4015 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4016 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4018 bnx_dma_block_free(intr->bnx_status_tag,
4019 intr->bnx_status_map, intr->bnx_status_block);
4022 /* Destroy the parent tag */
4023 if (sc->bnx_cdata.bnx_parent_tag != NULL)
4024 bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
4028 bnx_dma_alloc(device_t dev)
4030 struct bnx_softc *sc = device_get_softc(dev);
4031 struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
4035 * Allocate the parent bus DMA tag appropriate for PCI.
4037 * All of the NetExtreme/NetLink controllers have 4GB boundary
4039 * Whenever an address crosses a multiple of the 4GB boundary
4040 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
4041 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
4042 * state machine will lockup and cause the device to hang.
4044 error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
4045 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
4046 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
4047 0, &sc->bnx_cdata.bnx_parent_tag);
4049 device_printf(dev, "could not create parent DMA tag\n");
4054 * Create DMA stuffs for status blocks.
4056 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4057 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4059 error = bnx_dma_block_alloc(sc,
4060 __VM_CACHELINE_ALIGN(BGE_STATUS_BLK_SZ),
4061 &intr->bnx_status_tag, &intr->bnx_status_map,
4062 (void *)&intr->bnx_status_block,
4063 &intr->bnx_status_block_paddr);
4066 "could not create %dth status block\n", i);
4070 sc->bnx_hw_status = &sc->bnx_intr_data[0].bnx_status_block->bge_status;
4071 if (sc->bnx_flags & BNX_FLAG_STATUS_HASTAG) {
4072 sc->bnx_hw_status_tag =
4073 &sc->bnx_intr_data[0].bnx_status_block->bge_status_tag;
4077 * Create DMA tag and maps for RX mbufs.
4080 lwkt_serialize_init(&std->bnx_rx_std_serialize);
4081 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
4082 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
4083 NULL, NULL, MCLBYTES, 1, MCLBYTES,
4084 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK, &std->bnx_rx_mtag);
4086 device_printf(dev, "could not create RX mbuf DMA tag\n");
4090 for (i = 0; i < BGE_STD_RX_RING_CNT; ++i) {
4091 error = bus_dmamap_create(std->bnx_rx_mtag, BUS_DMA_WAITOK,
4092 &std->bnx_rx_std_buf[i].bnx_rx_dmamap);
4096 for (j = 0; j < i; ++j) {
4097 bus_dmamap_destroy(std->bnx_rx_mtag,
4098 std->bnx_rx_std_buf[j].bnx_rx_dmamap);
4100 bus_dma_tag_destroy(std->bnx_rx_mtag);
4101 std->bnx_rx_mtag = NULL;
4104 "could not create %dth RX mbuf DMA map\n", i);
4110 * Create DMA stuffs for standard RX ring.
4112 error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
4113 &std->bnx_rx_std_ring_tag,
4114 &std->bnx_rx_std_ring_map,
4115 (void *)&std->bnx_rx_std_ring,
4116 &std->bnx_rx_std_ring_paddr);
4118 device_printf(dev, "could not create std RX ring\n");
4123 * Create RX return rings
4125 mbx = BGE_MBX_RX_CONS0_LO;
4126 sc->bnx_rx_ret_ring = kmalloc_cachealign(
4127 sizeof(struct bnx_rx_ret_ring) * sc->bnx_rx_retcnt, M_DEVBUF,
4129 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4130 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
4131 struct bnx_intr_data *intr;
4135 ret->bnx_rx_mbx = mbx;
4136 ret->bnx_rx_cntmax = (BGE_STD_RX_RING_CNT / 4) /
4138 ret->bnx_rx_mask = 1 << i;
4140 if (!BNX_RSS_ENABLED(sc)) {
4141 intr = &sc->bnx_intr_data[0];
4143 KKASSERT(i + 1 < sc->bnx_intr_cnt);
4144 intr = &sc->bnx_intr_data[i + 1];
4148 ret->bnx_rx_considx =
4149 &intr->bnx_status_block->bge_idx[0].bge_rx_prod_idx;
4150 } else if (i == 1) {
4151 ret->bnx_rx_considx =
4152 &intr->bnx_status_block->bge_rx_jumbo_cons_idx;
4153 } else if (i == 2) {
4154 ret->bnx_rx_considx =
4155 &intr->bnx_status_block->bge_rsvd1;
4156 } else if (i == 3) {
4157 ret->bnx_rx_considx =
4158 &intr->bnx_status_block->bge_rx_mini_cons_idx;
4160 panic("unknown RX return ring %d\n", i);
4162 ret->bnx_hw_status_tag =
4163 &intr->bnx_status_block->bge_status_tag;
4165 error = bnx_create_rx_ret_ring(ret);
4168 "could not create %dth RX ret ring\n", i);
4177 sc->bnx_tx_ring = kmalloc_cachealign(
4178 sizeof(struct bnx_tx_ring) * sc->bnx_tx_ringcnt, M_DEVBUF,
4180 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4181 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
4182 struct bnx_intr_data *intr;
4185 txr->bnx_tx_mbx = bnx_tx_mailbox[i];
4187 if (sc->bnx_tx_ringcnt == 1) {
4188 intr = &sc->bnx_intr_data[0];
4190 KKASSERT(i + 1 < sc->bnx_intr_cnt);
4191 intr = &sc->bnx_intr_data[i + 1];
4194 if ((sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) == 0) {
4195 txr->bnx_hw_status_tag =
4196 &intr->bnx_status_block->bge_status_tag;
4198 txr->bnx_tx_considx =
4199 &intr->bnx_status_block->bge_idx[0].bge_tx_cons_idx;
4201 error = bnx_create_tx_ring(txr);
4204 "could not create %dth TX ring\n", i);
4210 * Create jumbo buffer pool.
4212 if (BNX_IS_JUMBO_CAPABLE(sc)) {
4213 error = bnx_alloc_jumbo_mem(sc);
4216 "could not create jumbo buffer pool\n");
4225 bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
4226 bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
4231 error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
4232 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
4233 size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
4237 *tag = dmem.dmem_tag;
4238 *map = dmem.dmem_map;
4239 *addr = dmem.dmem_addr;
4240 *paddr = dmem.dmem_busaddr;
4246 bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
4249 bus_dmamap_unload(tag, map);
4250 bus_dmamem_free(tag, addr, map);
4251 bus_dma_tag_destroy(tag);
4256 bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
4258 struct ifnet *ifp = &sc->arpcom.ac_if;
4260 #define PCS_ENCODE_ERR (BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
4263 * Sometimes PCS encoding errors are detected in
4264 * TBI mode (on fiber NICs), and for some reason
4265 * the chip will signal them as link changes.
4266 * If we get a link change event, but the 'PCS
4267 * encoding error' bit in the MAC status register
4268 * is set, don't bother doing a link check.
4269 * This avoids spurious "gigabit link up" messages
4270 * that sometimes appear on fiber NICs during
4271 * periods of heavy traffic.
4273 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
4274 if (!sc->bnx_link) {
4276 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
4277 BNX_CLRBIT(sc, BGE_MAC_MODE,
4278 BGE_MACMODE_TBI_SEND_CFGS);
4280 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
4283 if_printf(ifp, "link UP\n");
4285 ifp->if_link_state = LINK_STATE_UP;
4286 if_link_state_change(ifp);
4288 } else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
4293 if_printf(ifp, "link DOWN\n");
4295 ifp->if_link_state = LINK_STATE_DOWN;
4296 if_link_state_change(ifp);
4300 #undef PCS_ENCODE_ERR
4302 /* Clear the attention. */
4303 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4304 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4305 BGE_MACSTAT_LINK_CHANGED);
4309 bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
4311 struct ifnet *ifp = &sc->arpcom.ac_if;
4312 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
4315 bnx_miibus_statchg(sc->bnx_dev);
4319 if_printf(ifp, "link UP\n");
4321 if_printf(ifp, "link DOWN\n");
4324 /* Clear the attention. */
4325 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4326 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4327 BGE_MACSTAT_LINK_CHANGED);
4331 bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
4333 struct ifnet *ifp = &sc->arpcom.ac_if;
4334 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
4338 if (!sc->bnx_link &&
4339 (mii->mii_media_status & IFM_ACTIVE) &&
4340 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
4343 if_printf(ifp, "link UP\n");
4344 } else if (sc->bnx_link &&
4345 (!(mii->mii_media_status & IFM_ACTIVE) ||
4346 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
4349 if_printf(ifp, "link DOWN\n");
4352 /* Clear the attention. */
4353 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4354 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4355 BGE_MACSTAT_LINK_CHANGED);
4359 bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
4361 struct bnx_softc *sc = arg1;
4363 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4364 &sc->bnx_rx_coal_ticks,
4365 BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
4366 BNX_RX_COAL_TICKS_CHG);
4370 bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
4372 struct bnx_softc *sc = arg1;
4374 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4375 &sc->bnx_tx_coal_ticks,
4376 BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
4377 BNX_TX_COAL_TICKS_CHG);
4381 bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
4383 struct bnx_softc *sc = arg1;
4385 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4386 &sc->bnx_rx_coal_bds,
4387 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
4388 BNX_RX_COAL_BDS_CHG);
4392 bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
4394 struct bnx_softc *sc = arg1;
4396 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4397 &sc->bnx_tx_coal_bds,
4398 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
4399 BNX_TX_COAL_BDS_CHG);
4403 bnx_sysctl_tx_coal_bds_poll(SYSCTL_HANDLER_ARGS)
4405 struct bnx_softc *sc = arg1;
4407 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4408 &sc->bnx_tx_coal_bds_poll,
4409 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
4410 BNX_TX_COAL_BDS_CHG);
4414 bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
4416 struct bnx_softc *sc = arg1;
4418 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4419 &sc->bnx_rx_coal_bds_int,
4420 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
4421 BNX_RX_COAL_BDS_INT_CHG);
4425 bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
4427 struct bnx_softc *sc = arg1;
4429 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4430 &sc->bnx_tx_coal_bds_int,
4431 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
4432 BNX_TX_COAL_BDS_INT_CHG);
4436 bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
4437 int coal_min, int coal_max, uint32_t coal_chg_mask)
4439 struct bnx_softc *sc = arg1;
4440 struct ifnet *ifp = &sc->arpcom.ac_if;
4443 ifnet_serialize_all(ifp);
4446 error = sysctl_handle_int(oidp, &v, 0, req);
4447 if (!error && req->newptr != NULL) {
4448 if (v < coal_min || v > coal_max) {
4452 sc->bnx_coal_chg |= coal_chg_mask;
4454 /* Commit changes */
4455 bnx_coal_change(sc);
4459 ifnet_deserialize_all(ifp);
4464 bnx_coal_change(struct bnx_softc *sc)
4466 struct ifnet *ifp = &sc->arpcom.ac_if;
4469 ASSERT_IFNET_SERIALIZED_ALL(ifp);
4471 if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
4472 if (sc->bnx_rx_retcnt == 1) {
4473 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
4474 sc->bnx_rx_coal_ticks);
4477 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, 0);
4478 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4479 CSR_WRITE_4(sc, BGE_VEC1_RX_COAL_TICKS +
4480 (i * BGE_VEC_COALSET_SIZE),
4481 sc->bnx_rx_coal_ticks);
4484 for (; i < BNX_INTR_MAX - 1; ++i) {
4485 CSR_WRITE_4(sc, BGE_VEC1_RX_COAL_TICKS +
4486 (i * BGE_VEC_COALSET_SIZE), 0);
4489 if_printf(ifp, "rx_coal_ticks -> %u\n",
4490 sc->bnx_rx_coal_ticks);
4494 if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
4495 if (sc->bnx_tx_ringcnt == 1) {
4496 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
4497 sc->bnx_tx_coal_ticks);
4500 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, 0);
4501 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4502 CSR_WRITE_4(sc, BGE_VEC1_TX_COAL_TICKS +
4503 (i * BGE_VEC_COALSET_SIZE),
4504 sc->bnx_tx_coal_ticks);
4507 for (; i < BNX_INTR_MAX - 1; ++i) {
4508 CSR_WRITE_4(sc, BGE_VEC1_TX_COAL_TICKS +
4509 (i * BGE_VEC_COALSET_SIZE), 0);
4512 if_printf(ifp, "tx_coal_ticks -> %u\n",
4513 sc->bnx_tx_coal_ticks);
4517 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
4518 if (sc->bnx_rx_retcnt == 1) {
4519 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
4520 sc->bnx_rx_coal_bds);
4523 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, 0);
4524 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4525 CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS +
4526 (i * BGE_VEC_COALSET_SIZE),
4527 sc->bnx_rx_coal_bds);
4530 for (; i < BNX_INTR_MAX - 1; ++i) {
4531 CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS +
4532 (i * BGE_VEC_COALSET_SIZE), 0);
4535 if_printf(ifp, "rx_coal_bds -> %u\n",
4536 sc->bnx_rx_coal_bds);
4540 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
4541 uint32_t tx_coal_bds;
4543 if (ifp->if_flags & IFF_NPOLLING)
4544 tx_coal_bds = sc->bnx_tx_coal_bds_poll;
4546 tx_coal_bds = sc->bnx_tx_coal_bds;
4548 if (sc->bnx_tx_ringcnt == 1) {
4549 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, tx_coal_bds);
4552 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, 0);
4553 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4554 CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS +
4555 (i * BGE_VEC_COALSET_SIZE), tx_coal_bds);
4558 for (; i < BNX_INTR_MAX - 1; ++i) {
4559 CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS +
4560 (i * BGE_VEC_COALSET_SIZE), 0);
4563 if_printf(ifp, "%stx_coal_bds -> %u\n",
4564 (ifp->if_flags & IFF_NPOLLING) ? "polling " : "",
4569 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
4570 if (sc->bnx_rx_retcnt == 1) {
4571 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
4572 sc->bnx_rx_coal_bds_int);
4575 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
4576 for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4577 CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS_INT +
4578 (i * BGE_VEC_COALSET_SIZE),
4579 sc->bnx_rx_coal_bds_int);
4582 for (; i < BNX_INTR_MAX - 1; ++i) {
4583 CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS_INT +
4584 (i * BGE_VEC_COALSET_SIZE), 0);
4587 if_printf(ifp, "rx_coal_bds_int -> %u\n",
4588 sc->bnx_rx_coal_bds_int);
4592 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
4593 if (sc->bnx_tx_ringcnt == 1) {
4594 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
4595 sc->bnx_tx_coal_bds_int);
4598 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
4599 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4600 CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS_INT +
4601 (i * BGE_VEC_COALSET_SIZE),
4602 sc->bnx_tx_coal_bds_int);
4605 for (; i < BNX_INTR_MAX - 1; ++i) {
4606 CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS_INT +
4607 (i * BGE_VEC_COALSET_SIZE), 0);
4610 if_printf(ifp, "tx_coal_bds_int -> %u\n",
4611 sc->bnx_tx_coal_bds_int);
4615 sc->bnx_coal_chg = 0;
4619 bnx_check_intr_rxtx(void *xintr)
4621 struct bnx_intr_data *intr = xintr;
4622 struct bnx_rx_ret_ring *ret;
4623 struct bnx_tx_ring *txr;
4626 lwkt_serialize_enter(intr->bnx_intr_serialize);
4628 KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4630 ifp = &intr->bnx_sc->arpcom.ac_if;
4631 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4632 lwkt_serialize_exit(intr->bnx_intr_serialize);
4636 txr = intr->bnx_txr;
4637 ret = intr->bnx_ret;
4639 if (*ret->bnx_rx_considx != ret->bnx_rx_saved_considx ||
4640 *txr->bnx_tx_considx != txr->bnx_tx_saved_considx) {
4641 if (intr->bnx_rx_check_considx == ret->bnx_rx_saved_considx &&
4642 intr->bnx_tx_check_considx == txr->bnx_tx_saved_considx) {
4643 if (!intr->bnx_intr_maylose) {
4644 intr->bnx_intr_maylose = TRUE;
4648 if_printf(ifp, "lost interrupt\n");
4649 intr->bnx_intr_func(intr->bnx_intr_arg);
4652 intr->bnx_intr_maylose = FALSE;
4653 intr->bnx_rx_check_considx = ret->bnx_rx_saved_considx;
4654 intr->bnx_tx_check_considx = txr->bnx_tx_saved_considx;
4657 callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4658 intr->bnx_intr_check, intr);
4659 lwkt_serialize_exit(intr->bnx_intr_serialize);
4663 bnx_check_intr_tx(void *xintr)
4665 struct bnx_intr_data *intr = xintr;
4666 struct bnx_tx_ring *txr;
4669 lwkt_serialize_enter(intr->bnx_intr_serialize);
4671 KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4673 ifp = &intr->bnx_sc->arpcom.ac_if;
4674 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4675 lwkt_serialize_exit(intr->bnx_intr_serialize);
4679 txr = intr->bnx_txr;
4681 if (*txr->bnx_tx_considx != txr->bnx_tx_saved_considx) {
4682 if (intr->bnx_tx_check_considx == txr->bnx_tx_saved_considx) {
4683 if (!intr->bnx_intr_maylose) {
4684 intr->bnx_intr_maylose = TRUE;
4688 if_printf(ifp, "lost interrupt\n");
4689 intr->bnx_intr_func(intr->bnx_intr_arg);
4692 intr->bnx_intr_maylose = FALSE;
4693 intr->bnx_tx_check_considx = txr->bnx_tx_saved_considx;
4696 callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4697 intr->bnx_intr_check, intr);
4698 lwkt_serialize_exit(intr->bnx_intr_serialize);
4702 bnx_check_intr_rx(void *xintr)
4704 struct bnx_intr_data *intr = xintr;
4705 struct bnx_rx_ret_ring *ret;
4708 lwkt_serialize_enter(intr->bnx_intr_serialize);
4710 KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4712 ifp = &intr->bnx_sc->arpcom.ac_if;
4713 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4714 lwkt_serialize_exit(intr->bnx_intr_serialize);
4718 ret = intr->bnx_ret;
4720 if (*ret->bnx_rx_considx != ret->bnx_rx_saved_considx) {
4721 if (intr->bnx_rx_check_considx == ret->bnx_rx_saved_considx) {
4722 if (!intr->bnx_intr_maylose) {
4723 intr->bnx_intr_maylose = TRUE;
4727 if_printf(ifp, "lost interrupt\n");
4728 intr->bnx_intr_func(intr->bnx_intr_arg);
4731 intr->bnx_intr_maylose = FALSE;
4732 intr->bnx_rx_check_considx = ret->bnx_rx_saved_considx;
4735 callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4736 intr->bnx_intr_check, intr);
4737 lwkt_serialize_exit(intr->bnx_intr_serialize);
4741 bnx_enable_intr(struct bnx_softc *sc)
4743 struct ifnet *ifp = &sc->arpcom.ac_if;
4746 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4747 lwkt_serialize_handler_enable(
4748 sc->bnx_intr_data[i].bnx_intr_serialize);
4754 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4755 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4757 bnx_writembx(sc, intr->bnx_intr_mbx,
4758 (*intr->bnx_saved_status_tag) << 24);
4759 /* XXX Linux driver */
4760 bnx_writembx(sc, intr->bnx_intr_mbx,
4761 (*intr->bnx_saved_status_tag) << 24);
4765 * Unmask the interrupt when we stop polling.
4767 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4768 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4771 * Trigger another interrupt, since above writing
4772 * to interrupt mailbox0 may acknowledge pending
4775 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4777 if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4779 if_printf(ifp, "status tag bug workaround\n");
4781 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4782 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4784 if (intr->bnx_intr_check == NULL)
4786 intr->bnx_intr_maylose = FALSE;
4787 intr->bnx_rx_check_considx = 0;
4788 intr->bnx_tx_check_considx = 0;
4789 callout_reset_bycpu(&intr->bnx_intr_timer,
4790 BNX_INTR_CKINTVL, intr->bnx_intr_check, intr,
4791 intr->bnx_intr_cpuid);
4797 bnx_disable_intr(struct bnx_softc *sc)
4801 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4802 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4804 callout_stop(&intr->bnx_intr_timer);
4805 intr->bnx_intr_maylose = FALSE;
4806 intr->bnx_rx_check_considx = 0;
4807 intr->bnx_tx_check_considx = 0;
4811 * Mask the interrupt when we start polling.
4813 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4814 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4817 * Acknowledge possible asserted interrupt.
4819 for (i = 0; i < BNX_INTR_MAX; ++i)
4820 bnx_writembx(sc, sc->bnx_intr_data[i].bnx_intr_mbx, 1);
4822 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4823 lwkt_serialize_handler_disable(
4824 sc->bnx_intr_data[i].bnx_intr_serialize);
4829 bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
4834 mac_addr = bnx_readmem_ind(sc, 0x0c14);
4835 if ((mac_addr >> 16) == 0x484b) {
4836 ether_addr[0] = (uint8_t)(mac_addr >> 8);
4837 ether_addr[1] = (uint8_t)mac_addr;
4838 mac_addr = bnx_readmem_ind(sc, 0x0c18);
4839 ether_addr[2] = (uint8_t)(mac_addr >> 24);
4840 ether_addr[3] = (uint8_t)(mac_addr >> 16);
4841 ether_addr[4] = (uint8_t)(mac_addr >> 8);
4842 ether_addr[5] = (uint8_t)mac_addr;
4849 bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
4851 int mac_offset = BGE_EE_MAC_OFFSET;
4853 if (BNX_IS_5717_PLUS(sc)) {
4856 f = pci_get_function(sc->bnx_dev);
4858 mac_offset = BGE_EE_MAC_OFFSET_5717;
4860 mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
4863 return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
4867 bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
4869 if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
4872 return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4877 bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
4879 static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
4880 /* NOTE: Order is critical */
4882 bnx_get_eaddr_nvram,
4883 bnx_get_eaddr_eeprom,
4886 const bnx_eaddr_fcn_t *func;
4888 for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
4889 if ((*func)(sc, eaddr) == 0)
4892 return (*func == NULL ? ENXIO : 0);
4896 * NOTE: 'm' is not freed upon failure
4899 bnx_defrag_shortdma(struct mbuf *m)
4905 * If device receive two back-to-back send BDs with less than
4906 * or equal to 8 total bytes then the device may hang. The two
4907 * back-to-back send BDs must in the same frame for this failure
4908 * to occur. Scan mbuf chains and see whether two back-to-back
4909 * send BDs are there. If this is the case, allocate new mbuf
4910 * and copy the frame to workaround the silicon bug.
4912 for (n = m, found = 0; n != NULL; n = n->m_next) {
4923 n = m_defrag(m, MB_DONTWAIT);
4930 bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
4934 BNX_CLRBIT(sc, reg, bit);
4935 for (i = 0; i < BNX_TIMEOUT; i++) {
4936 if ((CSR_READ_4(sc, reg) & bit) == 0)
4943 bnx_link_poll(struct bnx_softc *sc)
4947 status = CSR_READ_4(sc, BGE_MAC_STS);
4948 if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
4949 sc->bnx_link_evt = 0;
4950 sc->bnx_link_upd(sc, status);
4955 bnx_enable_msi(struct bnx_softc *sc, boolean_t is_msix)
4959 msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
4960 msi_mode |= BGE_MSIMODE_ENABLE;
4963 * 5718-PG105-R says that "one shot" mode does not work
4964 * if MSI is used, however, it obviously works.
4966 msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
4968 msi_mode |= BGE_MSIMODE_MSIX_MULTIMODE;
4970 msi_mode &= ~BGE_MSIMODE_MSIX_MULTIMODE;
4971 CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
4975 bnx_dma_swap_options(struct bnx_softc *sc)
4977 uint32_t dma_options;
4979 dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
4980 BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
4981 #if BYTE_ORDER == BIG_ENDIAN
4982 dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
4984 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
4985 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
4986 dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
4987 BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
4988 BGE_MODECTL_HTX2B_ENABLE;
4994 bnx_setup_tso(struct bnx_tx_ring *txr, struct mbuf **mp,
4995 uint16_t *mss0, uint16_t *flags0)
5000 int thoff, iphlen, hoff, hlen;
5001 uint16_t flags, mss;
5004 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
5006 hoff = m->m_pkthdr.csum_lhlen;
5007 iphlen = m->m_pkthdr.csum_iphlen;
5008 thoff = m->m_pkthdr.csum_thlen;
5010 KASSERT(hoff > 0, ("invalid ether header len"));
5011 KASSERT(iphlen > 0, ("invalid ip header len"));
5012 KASSERT(thoff > 0, ("invalid tcp header len"));
5014 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
5015 m = m_pullup(m, hoff + iphlen + thoff);
5022 ip = mtodoff(m, struct ip *, hoff);
5023 th = mtodoff(m, struct tcphdr *, hoff + iphlen);
5025 mss = m->m_pkthdr.tso_segsz;
5026 flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
5028 ip->ip_len = htons(mss + iphlen + thoff);
5031 hlen = (iphlen + thoff) >> 2;
5032 mss |= ((hlen & 0x3) << 14);
5033 flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);
5042 bnx_create_tx_ring(struct bnx_tx_ring *txr)
5044 bus_size_t txmaxsz, txmaxsegsz;
5047 lwkt_serialize_init(&txr->bnx_tx_serialize);
5050 * Create DMA tag and maps for TX mbufs.
5052 if (txr->bnx_sc->bnx_flags & BNX_FLAG_TSO)
5053 txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
5055 txmaxsz = BNX_JUMBO_FRAMELEN;
5056 if (txr->bnx_sc->bnx_asicrev == BGE_ASICREV_BCM57766)
5057 txmaxsegsz = MCLBYTES;
5059 txmaxsegsz = PAGE_SIZE;
5060 error = bus_dma_tag_create(txr->bnx_sc->bnx_cdata.bnx_parent_tag,
5061 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
5062 txmaxsz, BNX_NSEG_NEW, txmaxsegsz,
5063 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
5066 device_printf(txr->bnx_sc->bnx_dev,
5067 "could not create TX mbuf DMA tag\n");
5071 for (i = 0; i < BGE_TX_RING_CNT; i++) {
5072 error = bus_dmamap_create(txr->bnx_tx_mtag,
5073 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
5074 &txr->bnx_tx_buf[i].bnx_tx_dmamap);
5078 for (j = 0; j < i; ++j) {
5079 bus_dmamap_destroy(txr->bnx_tx_mtag,
5080 txr->bnx_tx_buf[j].bnx_tx_dmamap);
5082 bus_dma_tag_destroy(txr->bnx_tx_mtag);
5083 txr->bnx_tx_mtag = NULL;
5085 device_printf(txr->bnx_sc->bnx_dev,
5086 "could not create TX mbuf DMA map\n");
5092 * Create DMA stuffs for TX ring.
5094 error = bnx_dma_block_alloc(txr->bnx_sc, BGE_TX_RING_SZ,
5095 &txr->bnx_tx_ring_tag,
5096 &txr->bnx_tx_ring_map,
5097 (void *)&txr->bnx_tx_ring,
5098 &txr->bnx_tx_ring_paddr);
5100 device_printf(txr->bnx_sc->bnx_dev,
5101 "could not create TX ring\n");
5105 txr->bnx_tx_flags |= BNX_TX_FLAG_SHORTDMA;
5106 txr->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
5112 bnx_destroy_tx_ring(struct bnx_tx_ring *txr)
5114 /* Destroy TX mbuf DMA stuffs. */
5115 if (txr->bnx_tx_mtag != NULL) {
5118 for (i = 0; i < BGE_TX_RING_CNT; i++) {
5119 KKASSERT(txr->bnx_tx_buf[i].bnx_tx_mbuf == NULL);
5120 bus_dmamap_destroy(txr->bnx_tx_mtag,
5121 txr->bnx_tx_buf[i].bnx_tx_dmamap);
5123 bus_dma_tag_destroy(txr->bnx_tx_mtag);
5126 /* Destroy TX ring */
5127 bnx_dma_block_free(txr->bnx_tx_ring_tag,
5128 txr->bnx_tx_ring_map, txr->bnx_tx_ring);
5132 bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS)
5134 struct bnx_softc *sc = (void *)arg1;
5135 struct ifnet *ifp = &sc->arpcom.ac_if;
5136 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
5137 int error, defrag, i;
5139 if (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG)
5144 error = sysctl_handle_int(oidp, &defrag, 0, req);
5145 if (error || req->newptr == NULL)
5148 ifnet_serialize_all(ifp);
5149 for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
5150 txr = &sc->bnx_tx_ring[i];
5152 txr->bnx_tx_flags |= BNX_TX_FLAG_FORCE_DEFRAG;
5154 txr->bnx_tx_flags &= ~BNX_TX_FLAG_FORCE_DEFRAG;
5156 ifnet_deserialize_all(ifp);
5162 bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS)
5164 struct bnx_softc *sc = (void *)arg1;
5165 struct ifnet *ifp = &sc->arpcom.ac_if;
5166 struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
5167 int error, tx_wreg, i;
5169 tx_wreg = txr->bnx_tx_wreg;
5170 error = sysctl_handle_int(oidp, &tx_wreg, 0, req);
5171 if (error || req->newptr == NULL)
5174 ifnet_serialize_all(ifp);
5175 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
5176 sc->bnx_tx_ring[i].bnx_tx_wreg = tx_wreg;
5177 ifnet_deserialize_all(ifp);
5183 bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *ret)
5187 lwkt_serialize_init(&ret->bnx_rx_ret_serialize);
5190 * Create DMA stuffs for RX return ring.
5192 error = bnx_dma_block_alloc(ret->bnx_sc,
5193 BGE_RX_RTN_RING_SZ(BNX_RETURN_RING_CNT),
5194 &ret->bnx_rx_ret_ring_tag,
5195 &ret->bnx_rx_ret_ring_map,
5196 (void *)&ret->bnx_rx_ret_ring,
5197 &ret->bnx_rx_ret_ring_paddr);
5199 device_printf(ret->bnx_sc->bnx_dev,
5200 "could not create RX ret ring\n");
5204 /* Shadow standard ring's RX mbuf DMA tag */
5205 ret->bnx_rx_mtag = ret->bnx_std->bnx_rx_mtag;
5208 * Create tmp DMA map for RX mbufs.
5210 error = bus_dmamap_create(ret->bnx_rx_mtag, BUS_DMA_WAITOK,
5211 &ret->bnx_rx_tmpmap);
5213 device_printf(ret->bnx_sc->bnx_dev,
5214 "could not create tmp RX mbuf DMA map\n");
5215 ret->bnx_rx_mtag = NULL;
5222 bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *ret)
5224 /* Destroy tmp RX mbuf DMA map */
5225 if (ret->bnx_rx_mtag != NULL)
5226 bus_dmamap_destroy(ret->bnx_rx_mtag, ret->bnx_rx_tmpmap);
5228 /* Destroy RX return ring */
5229 bnx_dma_block_free(ret->bnx_rx_ret_ring_tag,
5230 ret->bnx_rx_ret_ring_map, ret->bnx_rx_ret_ring);
5234 bnx_alloc_intr(struct bnx_softc *sc)
5236 struct bnx_intr_data *intr;
5240 if (sc->bnx_intr_cnt > 1) {
5241 error = bnx_alloc_msix(sc);
5244 KKASSERT(sc->bnx_intr_type == PCI_INTR_TYPE_MSIX);
5248 KKASSERT(sc->bnx_intr_cnt == 1);
5250 intr = &sc->bnx_intr_data[0];
5251 intr->bnx_ret = &sc->bnx_rx_ret_ring[0];
5252 intr->bnx_txr = &sc->bnx_tx_ring[0];
5253 intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5254 intr->bnx_intr_check = bnx_check_intr_rxtx;
5255 intr->bnx_saved_status_tag = &intr->bnx_ret->bnx_saved_status_tag;
5257 sc->bnx_intr_type = pci_alloc_1intr(sc->bnx_dev, bnx_msi_enable,
5258 &intr->bnx_intr_rid, &intr_flags);
5260 intr->bnx_intr_res = bus_alloc_resource_any(sc->bnx_dev, SYS_RES_IRQ,
5261 &intr->bnx_intr_rid, intr_flags);
5262 if (intr->bnx_intr_res == NULL) {
5263 device_printf(sc->bnx_dev, "could not alloc interrupt\n");
5267 if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI) {
5268 bnx_enable_msi(sc, FALSE);
5269 intr->bnx_intr_func = bnx_msi;
5271 device_printf(sc->bnx_dev, "oneshot MSI\n");
5273 intr->bnx_intr_func = bnx_intr_legacy;
5275 intr->bnx_intr_arg = sc;
5276 intr->bnx_intr_cpuid = rman_get_cpuid(intr->bnx_intr_res);
5278 intr->bnx_txr->bnx_tx_cpuid = intr->bnx_intr_cpuid;
5284 bnx_setup_intr(struct bnx_softc *sc)
5288 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
5289 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
5291 error = bus_setup_intr_descr(sc->bnx_dev, intr->bnx_intr_res,
5292 INTR_MPSAFE, intr->bnx_intr_func, intr->bnx_intr_arg,
5293 &intr->bnx_intr_hand, intr->bnx_intr_serialize,
5294 intr->bnx_intr_desc);
5296 device_printf(sc->bnx_dev,
5297 "could not set up %dth intr\n", i);
5298 bnx_teardown_intr(sc, i);
5306 bnx_teardown_intr(struct bnx_softc *sc, int cnt)
5310 for (i = 0; i < cnt; ++i) {
5311 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
5313 bus_teardown_intr(sc->bnx_dev, intr->bnx_intr_res,
5314 intr->bnx_intr_hand);
5319 bnx_free_intr(struct bnx_softc *sc)
5321 if (sc->bnx_intr_type != PCI_INTR_TYPE_MSIX) {
5322 struct bnx_intr_data *intr;
5324 KKASSERT(sc->bnx_intr_cnt <= 1);
5325 intr = &sc->bnx_intr_data[0];
5327 if (intr->bnx_intr_res != NULL) {
5328 bus_release_resource(sc->bnx_dev, SYS_RES_IRQ,
5329 intr->bnx_intr_rid, intr->bnx_intr_res);
5331 if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI)
5332 pci_release_msi(sc->bnx_dev);
5334 bnx_free_msix(sc, TRUE);
5339 bnx_setup_serialize(struct bnx_softc *sc)
5344 * Allocate serializer array
5347 /* Main + RX STD + TX + RX RET */
5348 sc->bnx_serialize_cnt = 1 + 1 + sc->bnx_tx_ringcnt + sc->bnx_rx_retcnt;
5351 kmalloc(sc->bnx_serialize_cnt * sizeof(struct lwkt_serialize *),
5352 M_DEVBUF, M_WAITOK | M_ZERO);
5357 * NOTE: Order is critical
5362 KKASSERT(i < sc->bnx_serialize_cnt);
5363 sc->bnx_serialize[i++] = &sc->bnx_main_serialize;
5365 KKASSERT(i < sc->bnx_serialize_cnt);
5366 sc->bnx_serialize[i++] = &sc->bnx_rx_std_ring.bnx_rx_std_serialize;
5368 for (j = 0; j < sc->bnx_rx_retcnt; ++j) {
5369 KKASSERT(i < sc->bnx_serialize_cnt);
5370 sc->bnx_serialize[i++] =
5371 &sc->bnx_rx_ret_ring[j].bnx_rx_ret_serialize;
5374 for (j = 0; j < sc->bnx_tx_ringcnt; ++j) {
5375 KKASSERT(i < sc->bnx_serialize_cnt);
5376 sc->bnx_serialize[i++] =
5377 &sc->bnx_tx_ring[j].bnx_tx_serialize;
5380 KKASSERT(i == sc->bnx_serialize_cnt);
5384 bnx_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
5386 struct bnx_softc *sc = ifp->if_softc;
5388 ifnet_serialize_array_enter(sc->bnx_serialize,
5389 sc->bnx_serialize_cnt, slz);
5393 bnx_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
5395 struct bnx_softc *sc = ifp->if_softc;
5397 ifnet_serialize_array_exit(sc->bnx_serialize,
5398 sc->bnx_serialize_cnt, slz);
5402 bnx_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
5404 struct bnx_softc *sc = ifp->if_softc;
5406 return ifnet_serialize_array_try(sc->bnx_serialize,
5407 sc->bnx_serialize_cnt, slz);
5413 bnx_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
5414 boolean_t serialized)
5416 struct bnx_softc *sc = ifp->if_softc;
5418 ifnet_serialize_array_assert(sc->bnx_serialize, sc->bnx_serialize_cnt,
5422 #endif /* INVARIANTS */
5424 #ifdef IFPOLL_ENABLE
5427 bnx_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS)
5429 struct bnx_softc *sc = (void *)arg1;
5430 struct ifnet *ifp = &sc->arpcom.ac_if;
5433 off = sc->bnx_npoll_rxoff;
5434 error = sysctl_handle_int(oidp, &off, 0, req);
5435 if (error || req->newptr == NULL)
5440 ifnet_serialize_all(ifp);
5441 if (off >= ncpus2 || off % sc->bnx_rx_retcnt != 0) {
5445 sc->bnx_npoll_txoff = off;
5446 sc->bnx_npoll_rxoff = off;
5448 ifnet_deserialize_all(ifp);
5454 bnx_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
5456 struct bnx_softc *sc = (void *)arg1;
5457 struct ifnet *ifp = &sc->arpcom.ac_if;
5460 off = sc->bnx_npoll_rxoff;
5461 error = sysctl_handle_int(oidp, &off, 0, req);
5462 if (error || req->newptr == NULL)
5467 ifnet_serialize_all(ifp);
5468 if (off >= ncpus2 || off % sc->bnx_rx_retcnt != 0) {
5472 sc->bnx_npoll_rxoff = off;
5474 ifnet_deserialize_all(ifp);
5480 bnx_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
5482 struct bnx_softc *sc = (void *)arg1;
5483 struct ifnet *ifp = &sc->arpcom.ac_if;
5486 off = sc->bnx_npoll_txoff;
5487 error = sysctl_handle_int(oidp, &off, 0, req);
5488 if (error || req->newptr == NULL)
5493 ifnet_serialize_all(ifp);
5494 if (off >= ncpus2) {
5498 sc->bnx_npoll_txoff = off;
5500 ifnet_deserialize_all(ifp);
5505 #endif /* IFPOLL_ENABLE */
5508 bnx_set_tick_cpuid(struct bnx_softc *sc, boolean_t polling)
5511 sc->bnx_tick_cpuid = 0; /* XXX */
5513 sc->bnx_tick_cpuid = sc->bnx_intr_data[0].bnx_intr_cpuid;
5517 bnx_rx_std_refill_ithread(void *xstd)
5519 struct bnx_rx_std_ring *std = xstd;
5520 struct globaldata *gd = mycpu;
5524 while (!std->bnx_rx_std_stop) {
5525 if (std->bnx_rx_std_refill) {
5526 lwkt_serialize_handler_call(
5527 &std->bnx_rx_std_serialize,
5528 bnx_rx_std_refill, std, NULL);
5534 atomic_poll_release_int(&std->bnx_rx_std_running);
5537 if (!std->bnx_rx_std_refill && !std->bnx_rx_std_stop) {
5538 lwkt_deschedule_self(gd->gd_curthread);
5551 bnx_rx_std_refill(void *xstd, void *frame __unused)
5553 struct bnx_rx_std_ring *std = xstd;
5554 int cnt, refill_mask;
5560 refill_mask = std->bnx_rx_std_refill;
5561 atomic_clear_int(&std->bnx_rx_std_refill, refill_mask);
5563 while (refill_mask) {
5564 uint16_t check_idx = std->bnx_rx_std;
5567 ret_idx = bsfl(refill_mask);
5569 struct bnx_rx_buf *rb;
5572 BNX_INC(check_idx, BGE_STD_RX_RING_CNT);
5573 rb = &std->bnx_rx_std_buf[check_idx];
5574 refilled = rb->bnx_rx_refilled;
5577 bnx_setup_rxdesc_std(std, check_idx);
5578 std->bnx_rx_std = check_idx;
5581 atomic_subtract_int(
5582 &std->bnx_rx_std_used, cnt);
5583 bnx_writembx(std->bnx_sc,
5584 BGE_MBX_RX_STD_PROD_LO,
5592 refill_mask &= ~(1 << ret_idx);
5596 atomic_subtract_int(&std->bnx_rx_std_used, cnt);
5597 bnx_writembx(std->bnx_sc, BGE_MBX_RX_STD_PROD_LO,
5601 if (std->bnx_rx_std_refill)
5604 atomic_poll_release_int(&std->bnx_rx_std_running);
5607 if (std->bnx_rx_std_refill)
5612 bnx_sysctl_std_refill(SYSCTL_HANDLER_ARGS)
5614 struct bnx_softc *sc = (void *)arg1;
5615 struct ifnet *ifp = &sc->arpcom.ac_if;
5616 struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
5617 int error, cntmax, i;
5619 cntmax = ret->bnx_rx_cntmax;
5620 error = sysctl_handle_int(oidp, &cntmax, 0, req);
5621 if (error || req->newptr == NULL)
5624 ifnet_serialize_all(ifp);
5626 if ((cntmax * sc->bnx_rx_retcnt) >= BGE_STD_RX_RING_CNT / 2) {
5631 for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
5632 sc->bnx_rx_ret_ring[i].bnx_rx_cntmax = cntmax;
5636 ifnet_deserialize_all(ifp);
5642 bnx_init_rss(struct bnx_softc *sc)
5644 uint8_t key[BGE_RSS_KEYREG_CNT * BGE_RSS_KEYREG_SIZE];
5647 KKASSERT(BNX_RSS_ENABLED(sc));
5650 for (j = 0; j < BGE_RSS_INDIR_TBL_CNT; ++j) {
5653 for (i = 0; i < BGE_RSS_INDIR_TBLENT_CNT; ++i) {
5656 q = r % sc->bnx_rx_retcnt;
5657 tbl |= q << (BGE_RSS_INDIR_TBLENT_SHIFT *
5658 (BGE_RSS_INDIR_TBLENT_CNT - i - 1));
5662 BNX_RSS_DPRINTF(sc, 1, "tbl%d %08x\n", j, tbl);
5663 CSR_WRITE_4(sc, BGE_RSS_INDIR_TBL(j), tbl);
5666 toeplitz_get_key(key, sizeof(key));
5667 for (i = 0; i < BGE_RSS_KEYREG_CNT; ++i) {
5670 keyreg = BGE_RSS_KEYREG_VAL(key, i);
5672 BNX_RSS_DPRINTF(sc, 1, "key%d %08x\n", i, keyreg);
5673 CSR_WRITE_4(sc, BGE_RSS_KEYREG(i), keyreg);
5678 bnx_setup_ring_cnt(struct bnx_softc *sc)
5680 int msix_enable, i, msix_cnt, msix_cnt2, ring_max;
5682 sc->bnx_tx_ringcnt = 1;
5683 sc->bnx_rx_retcnt = 1;
5684 sc->bnx_intr_cnt = 1;
5686 msix_enable = device_getenv_int(sc->bnx_dev, "msix.enable",
5694 msix_cnt = pci_msix_count(sc->bnx_dev);
5699 while ((1 << (i + 1)) <= msix_cnt)
5704 * One MSI-X vector is dedicated to status or single TX queue,
5705 * so make sure that there are enough MSI-X vectors.
5707 if (msix_cnt == msix_cnt2) {
5710 * This probably will not happen; 57785/5718 families
5711 * come with at least 5 MSI-X vectors.
5714 if (msix_cnt2 <= 1) {
5715 device_printf(sc->bnx_dev,
5716 "MSI-X count %d could not be used\n", msix_cnt);
5719 device_printf(sc->bnx_dev, "MSI-X count %d is power of 2\n",
5724 * Setup RX ring count
5726 ring_max = BNX_RX_RING_MAX;
5727 if (ring_max > msix_cnt2)
5728 ring_max = msix_cnt2;
5729 sc->bnx_rx_retcnt = device_getenv_int(sc->bnx_dev, "rx_rings",
5731 sc->bnx_rx_retcnt = if_ring_count2(sc->bnx_rx_retcnt, ring_max);
5733 if (sc->bnx_rx_retcnt == 1)
5737 * We need one extra MSI-X vector for link status or
5738 * TX ring (if only one TX ring is enabled).
5740 sc->bnx_intr_cnt = sc->bnx_rx_retcnt + 1;
5743 * Setup TX ring count
5745 * Currently only BCM5719 and BCM5720 support multiple TX rings
5746 * and the TX ring count must be less than the RX ring count.
5748 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
5749 sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
5750 ring_max = BNX_TX_RING_MAX;
5751 if (ring_max > msix_cnt2)
5752 ring_max = msix_cnt2;
5753 if (ring_max > sc->bnx_rx_retcnt)
5754 ring_max = sc->bnx_rx_retcnt;
5755 sc->bnx_tx_ringcnt = device_getenv_int(sc->bnx_dev, "tx_rings",
5757 sc->bnx_tx_ringcnt = if_ring_count2(sc->bnx_tx_ringcnt,
5763 bnx_alloc_msix(struct bnx_softc *sc)
5765 struct bnx_intr_data *intr;
5766 boolean_t setup = FALSE;
5767 int error, i, offset, offset_def;
5769 KKASSERT(sc->bnx_intr_cnt > 1);
5770 KKASSERT(sc->bnx_intr_cnt == sc->bnx_rx_retcnt + 1);
5772 if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
5776 intr = &sc->bnx_intr_data[0];
5778 intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5779 intr->bnx_saved_status_tag = &sc->bnx_saved_status_tag;
5781 intr->bnx_intr_func = bnx_msix_status;
5782 intr->bnx_intr_arg = sc;
5783 intr->bnx_intr_cpuid = 0; /* XXX */
5785 ksnprintf(intr->bnx_intr_desc0, sizeof(intr->bnx_intr_desc0),
5786 "%s sts", device_get_nameunit(sc->bnx_dev));
5787 intr->bnx_intr_desc = intr->bnx_intr_desc0;
5792 if (sc->bnx_rx_retcnt == ncpus2) {
5795 offset_def = (sc->bnx_rx_retcnt *
5796 device_get_unit(sc->bnx_dev)) % ncpus2;
5798 offset = device_getenv_int(sc->bnx_dev,
5799 "msix.offset", offset_def);
5800 if (offset >= ncpus2 ||
5801 offset % sc->bnx_rx_retcnt != 0) {
5802 device_printf(sc->bnx_dev,
5803 "invalid msix.offset %d, use %d\n",
5804 offset, offset_def);
5805 offset = offset_def;
5809 for (i = 1; i < sc->bnx_intr_cnt; ++i) {
5812 intr = &sc->bnx_intr_data[i];
5814 KKASSERT(idx < sc->bnx_rx_retcnt);
5815 intr->bnx_ret = &sc->bnx_rx_ret_ring[idx];
5816 if (idx < sc->bnx_tx_ringcnt) {
5817 intr->bnx_txr = &sc->bnx_tx_ring[idx];
5818 intr->bnx_ret->bnx_txr = intr->bnx_txr;
5821 intr->bnx_intr_serialize =
5822 &intr->bnx_ret->bnx_rx_ret_serialize;
5823 intr->bnx_saved_status_tag =
5824 &intr->bnx_ret->bnx_saved_status_tag;
5826 intr->bnx_intr_arg = intr->bnx_ret;
5827 KKASSERT(idx + offset < ncpus2);
5828 intr->bnx_intr_cpuid = idx + offset;
5830 if (intr->bnx_txr == NULL) {
5831 intr->bnx_intr_check = bnx_check_intr_rx;
5832 intr->bnx_intr_func = bnx_msix_rx;
5833 ksnprintf(intr->bnx_intr_desc0,
5834 sizeof(intr->bnx_intr_desc0), "%s rx%d",
5835 device_get_nameunit(sc->bnx_dev), idx);
5837 intr->bnx_intr_check = bnx_check_intr_rxtx;
5838 intr->bnx_intr_func = bnx_msix_rxtx;
5839 ksnprintf(intr->bnx_intr_desc0,
5840 sizeof(intr->bnx_intr_desc0), "%s rxtx%d",
5841 device_get_nameunit(sc->bnx_dev), idx);
5843 intr->bnx_txr->bnx_tx_cpuid =
5844 intr->bnx_intr_cpuid;
5846 intr->bnx_intr_desc = intr->bnx_intr_desc0;
5848 intr->bnx_ret->bnx_msix_mbx = intr->bnx_intr_mbx;
5852 * TX ring and link status
5854 offset_def = device_get_unit(sc->bnx_dev) % ncpus2;
5855 offset = device_getenv_int(sc->bnx_dev, "msix.txoff",
5857 if (offset >= ncpus2) {
5858 device_printf(sc->bnx_dev,
5859 "invalid msix.txoff %d, use %d\n",
5860 offset, offset_def);
5861 offset = offset_def;
5864 intr = &sc->bnx_intr_data[0];
5866 intr->bnx_txr = &sc->bnx_tx_ring[0];
5867 intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5868 intr->bnx_intr_check = bnx_check_intr_tx;
5869 intr->bnx_saved_status_tag =
5870 &intr->bnx_txr->bnx_saved_status_tag;
5872 intr->bnx_intr_func = bnx_msix_tx_status;
5873 intr->bnx_intr_arg = intr->bnx_txr;
5874 intr->bnx_intr_cpuid = offset;
5876 ksnprintf(intr->bnx_intr_desc0, sizeof(intr->bnx_intr_desc0),
5877 "%s ststx", device_get_nameunit(sc->bnx_dev));
5878 intr->bnx_intr_desc = intr->bnx_intr_desc0;
5880 intr->bnx_txr->bnx_tx_cpuid = intr->bnx_intr_cpuid;
5885 if (sc->bnx_rx_retcnt == ncpus2) {
5888 offset_def = (sc->bnx_rx_retcnt *
5889 device_get_unit(sc->bnx_dev)) % ncpus2;
5891 offset = device_getenv_int(sc->bnx_dev,
5892 "msix.rxoff", offset_def);
5893 if (offset >= ncpus2 ||
5894 offset % sc->bnx_rx_retcnt != 0) {
5895 device_printf(sc->bnx_dev,
5896 "invalid msix.rxoff %d, use %d\n",
5897 offset, offset_def);
5898 offset = offset_def;
5902 for (i = 1; i < sc->bnx_intr_cnt; ++i) {
5905 intr = &sc->bnx_intr_data[i];
5907 KKASSERT(idx < sc->bnx_rx_retcnt);
5908 intr->bnx_ret = &sc->bnx_rx_ret_ring[idx];
5909 intr->bnx_intr_serialize =
5910 &intr->bnx_ret->bnx_rx_ret_serialize;
5911 intr->bnx_intr_check = bnx_check_intr_rx;
5912 intr->bnx_saved_status_tag =
5913 &intr->bnx_ret->bnx_saved_status_tag;
5915 intr->bnx_intr_func = bnx_msix_rx;
5916 intr->bnx_intr_arg = intr->bnx_ret;
5917 KKASSERT(idx + offset < ncpus2);
5918 intr->bnx_intr_cpuid = idx + offset;
5920 ksnprintf(intr->bnx_intr_desc0,
5921 sizeof(intr->bnx_intr_desc0), "%s rx%d",
5922 device_get_nameunit(sc->bnx_dev), idx);
5923 intr->bnx_intr_desc = intr->bnx_intr_desc0;
5925 intr->bnx_ret->bnx_msix_mbx = intr->bnx_intr_mbx;
5929 sc->bnx_msix_mem_rid = PCIR_BAR(4);
5930 sc->bnx_msix_mem_res = bus_alloc_resource_any(sc->bnx_dev,
5931 SYS_RES_MEMORY, &sc->bnx_msix_mem_rid, RF_ACTIVE);
5932 if (sc->bnx_msix_mem_res == NULL) {
5933 device_printf(sc->bnx_dev, "could not alloc MSI-X table\n");
5937 bnx_enable_msi(sc, TRUE);
5939 error = pci_setup_msix(sc->bnx_dev);
5941 device_printf(sc->bnx_dev, "could not setup MSI-X\n");
5946 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
5947 intr = &sc->bnx_intr_data[i];
5949 error = pci_alloc_msix_vector(sc->bnx_dev, i,
5950 &intr->bnx_intr_rid, intr->bnx_intr_cpuid);
5952 device_printf(sc->bnx_dev,
5953 "could not alloc MSI-X %d on cpu%d\n",
5954 i, intr->bnx_intr_cpuid);
5958 intr->bnx_intr_res = bus_alloc_resource_any(sc->bnx_dev,
5959 SYS_RES_IRQ, &intr->bnx_intr_rid, RF_ACTIVE);
5960 if (intr->bnx_intr_res == NULL) {
5961 device_printf(sc->bnx_dev,
5962 "could not alloc MSI-X %d resource\n", i);
5968 pci_enable_msix(sc->bnx_dev);
5969 sc->bnx_intr_type = PCI_INTR_TYPE_MSIX;
5972 bnx_free_msix(sc, setup);
5977 bnx_free_msix(struct bnx_softc *sc, boolean_t setup)
5981 KKASSERT(sc->bnx_intr_cnt > 1);
5983 for (i = 0; i < sc->bnx_intr_cnt; ++i) {
5984 struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
5986 if (intr->bnx_intr_res != NULL) {
5987 bus_release_resource(sc->bnx_dev, SYS_RES_IRQ,
5988 intr->bnx_intr_rid, intr->bnx_intr_res);
5990 if (intr->bnx_intr_rid >= 0) {
5991 pci_release_msix_vector(sc->bnx_dev,
5992 intr->bnx_intr_rid);
5996 pci_teardown_msix(sc->bnx_dev);
6000 bnx_rx_std_refill_sched_ipi(void *xret)
6002 struct bnx_rx_ret_ring *ret = xret;
6003 struct bnx_rx_std_ring *std = ret->bnx_std;
6004 struct globaldata *gd = mycpu;
6008 atomic_set_int(&std->bnx_rx_std_refill, ret->bnx_rx_mask);
6011 KKASSERT(std->bnx_rx_std_ithread.td_gd == gd);
6012 lwkt_schedule(&std->bnx_rx_std_ithread);
6018 bnx_rx_std_refill_stop(void *xstd)
6020 struct bnx_rx_std_ring *std = xstd;
6021 struct globaldata *gd = mycpu;
6025 std->bnx_rx_std_stop = 1;
6028 KKASSERT(std->bnx_rx_std_ithread.td_gd == gd);
6029 lwkt_schedule(&std->bnx_rx_std_ithread);
6035 bnx_serialize_skipmain(struct bnx_softc *sc)
6037 lwkt_serialize_array_enter(sc->bnx_serialize,
6038 sc->bnx_serialize_cnt, 1);
6042 bnx_deserialize_skipmain(struct bnx_softc *sc)
6044 lwkt_serialize_array_exit(sc->bnx_serialize,
6045 sc->bnx_serialize_cnt, 1);
6049 bnx_rx_std_refill_sched(struct bnx_rx_ret_ring *ret,
6050 struct bnx_rx_std_ring *std)
6052 struct globaldata *gd = mycpu;
6054 ret->bnx_rx_cnt = 0;
6059 atomic_set_int(&std->bnx_rx_std_refill, ret->bnx_rx_mask);
6061 if (atomic_poll_acquire_int(&std->bnx_rx_std_running)) {
6062 if (std->bnx_rx_std_ithread.td_gd == gd) {
6063 lwkt_schedule(&std->bnx_rx_std_ithread);
6066 std->bnx_rx_std_ithread.td_gd,
6067 bnx_rx_std_refill_sched_ipi, ret);
6074 static struct pktinfo *
6075 bnx_rss_info(struct pktinfo *pi, const struct bge_rx_bd *cur_rx)
6077 /* Don't pick up IPv6 packet */
6078 if (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6)
6081 /* Don't pick up IP packet w/o IP checksum */
6082 if ((cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) == 0 ||
6083 (cur_rx->bge_error_flag & BGE_RXERRFLAG_IP_CSUM_NOK))
6086 /* Don't pick up IP packet w/o TCP/UDP checksum */
6087 if ((cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) == 0)
6090 /* May be IP fragment */
6091 if (cur_rx->bge_tcp_udp_csum != 0xffff)
6094 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_IS_TCP)
6095 pi->pi_l3proto = IPPROTO_TCP;
6097 pi->pi_l3proto = IPPROTO_UDP;
6098 pi->pi_netisr = NETISR_IP;