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/vlan/if_vlan_var.h>
66 #include <net/vlan/if_vlan_ether.h>
68 #include <dev/netif/mii_layer/mii.h>
69 #include <dev/netif/mii_layer/miivar.h>
70 #include <dev/netif/mii_layer/brgphyreg.h>
72 #include <bus/pci/pcidevs.h>
73 #include <bus/pci/pcireg.h>
74 #include <bus/pci/pcivar.h>
76 #include <dev/netif/bge/if_bgereg.h>
77 #include <dev/netif/bnx/if_bnxvar.h>
79 /* "device miibus" required. See GENERIC if you get errors here. */
80 #include "miibus_if.h"
82 #define BNX_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
84 #define BNX_INTR_CKINTVL ((10 * hz) / 1000) /* 10ms */
86 static const struct bnx_type {
91 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717,
92 "Broadcom BCM5717 Gigabit Ethernet" },
93 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717C,
94 "Broadcom BCM5717C Gigabit Ethernet" },
95 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5718,
96 "Broadcom BCM5718 Gigabit Ethernet" },
97 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5719,
98 "Broadcom BCM5719 Gigabit Ethernet" },
99 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5720_ALT,
100 "Broadcom BCM5720 Gigabit Ethernet" },
102 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
103 "Broadcom BCM57761 Gigabit Ethernet" },
104 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
105 "Broadcom BCM57762 Gigabit Ethernet" },
106 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
107 "Broadcom BCM57765 Gigabit Ethernet" },
108 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
109 "Broadcom BCM57766 Gigabit Ethernet" },
110 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
111 "Broadcom BCM57781 Gigabit Ethernet" },
112 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
113 "Broadcom BCM57782 Gigabit Ethernet" },
114 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
115 "Broadcom BCM57785 Gigabit Ethernet" },
116 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
117 "Broadcom BCM57786 Gigabit Ethernet" },
118 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
119 "Broadcom BCM57791 Fast Ethernet" },
120 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
121 "Broadcom BCM57795 Fast Ethernet" },
126 #define BNX_IS_JUMBO_CAPABLE(sc) ((sc)->bnx_flags & BNX_FLAG_JUMBO)
127 #define BNX_IS_5717_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
128 #define BNX_IS_57765_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
129 #define BNX_IS_57765_FAMILY(sc) \
130 ((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
132 typedef int (*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
134 static int bnx_probe(device_t);
135 static int bnx_attach(device_t);
136 static int bnx_detach(device_t);
137 static void bnx_shutdown(device_t);
138 static int bnx_suspend(device_t);
139 static int bnx_resume(device_t);
140 static int bnx_miibus_readreg(device_t, int, int);
141 static int bnx_miibus_writereg(device_t, int, int, int);
142 static void bnx_miibus_statchg(device_t);
145 static void bnx_npoll(struct ifnet *, struct ifpoll_info *);
146 static void bnx_npoll_compat(struct ifnet *, void *, int);
148 static void bnx_intr_legacy(void *);
149 static void bnx_msi(void *);
150 static void bnx_msi_oneshot(void *);
151 static void bnx_intr(struct bnx_softc *);
152 static void bnx_enable_intr(struct bnx_softc *);
153 static void bnx_disable_intr(struct bnx_softc *);
154 static void bnx_txeof(struct bnx_softc *, uint16_t);
155 static void bnx_rxeof(struct bnx_softc *, uint16_t, int);
157 static void bnx_start(struct ifnet *, struct ifaltq_subque *);
158 static int bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
159 static void bnx_init(void *);
160 static void bnx_stop(struct bnx_softc *);
161 static void bnx_watchdog(struct ifnet *);
162 static int bnx_ifmedia_upd(struct ifnet *);
163 static void bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
164 static void bnx_tick(void *);
166 static int bnx_alloc_jumbo_mem(struct bnx_softc *);
167 static void bnx_free_jumbo_mem(struct bnx_softc *);
168 static struct bnx_jslot
169 *bnx_jalloc(struct bnx_softc *);
170 static void bnx_jfree(void *);
171 static void bnx_jref(void *);
172 static int bnx_newbuf_std(struct bnx_softc *, int, int);
173 static int bnx_newbuf_jumbo(struct bnx_softc *, int, int);
174 static void bnx_setup_rxdesc_std(struct bnx_softc *, int);
175 static void bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
176 static int bnx_init_rx_ring_std(struct bnx_softc *);
177 static void bnx_free_rx_ring_std(struct bnx_softc *);
178 static int bnx_init_rx_ring_jumbo(struct bnx_softc *);
179 static void bnx_free_rx_ring_jumbo(struct bnx_softc *);
180 static void bnx_free_tx_ring(struct bnx_softc *);
181 static int bnx_init_tx_ring(struct bnx_softc *);
182 static int bnx_dma_alloc(struct bnx_softc *);
183 static void bnx_dma_free(struct bnx_softc *);
184 static int bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
185 bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
186 static void bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
188 bnx_defrag_shortdma(struct mbuf *);
189 static int bnx_encap(struct bnx_softc *, struct mbuf **,
191 static int bnx_setup_tso(struct bnx_softc *, struct mbuf **,
192 uint16_t *, uint16_t *);
194 static void bnx_reset(struct bnx_softc *);
195 static int bnx_chipinit(struct bnx_softc *);
196 static int bnx_blockinit(struct bnx_softc *);
197 static void bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
198 static void bnx_enable_msi(struct bnx_softc *sc);
199 static void bnx_setmulti(struct bnx_softc *);
200 static void bnx_setpromisc(struct bnx_softc *);
201 static void bnx_stats_update_regs(struct bnx_softc *);
202 static uint32_t bnx_dma_swap_options(struct bnx_softc *);
204 static uint32_t bnx_readmem_ind(struct bnx_softc *, uint32_t);
205 static void bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
207 static uint32_t bnx_readreg_ind(struct bnx_softc *, uint32_t);
209 static void bnx_writereg_ind(struct bnx_softc *, uint32_t, uint32_t);
210 static void bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
211 static void bnx_writembx(struct bnx_softc *, int, int);
212 static uint8_t bnx_nvram_getbyte(struct bnx_softc *, int, uint8_t *);
213 static int bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
214 static uint8_t bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
215 static int bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
217 static void bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
218 static void bnx_copper_link_upd(struct bnx_softc *, uint32_t);
219 static void bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
220 static void bnx_link_poll(struct bnx_softc *);
222 static int bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
223 static int bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
224 static int bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
225 static int bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
227 static void bnx_coal_change(struct bnx_softc *);
228 static int bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
229 static int bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
230 static int bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
231 static int bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
232 static int bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
233 static int bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
234 static int bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
237 static int bnx_msi_enable = 1;
238 TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
240 static device_method_t bnx_methods[] = {
241 /* Device interface */
242 DEVMETHOD(device_probe, bnx_probe),
243 DEVMETHOD(device_attach, bnx_attach),
244 DEVMETHOD(device_detach, bnx_detach),
245 DEVMETHOD(device_shutdown, bnx_shutdown),
246 DEVMETHOD(device_suspend, bnx_suspend),
247 DEVMETHOD(device_resume, bnx_resume),
250 DEVMETHOD(bus_print_child, bus_generic_print_child),
251 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
254 DEVMETHOD(miibus_readreg, bnx_miibus_readreg),
255 DEVMETHOD(miibus_writereg, bnx_miibus_writereg),
256 DEVMETHOD(miibus_statchg, bnx_miibus_statchg),
261 static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
262 static devclass_t bnx_devclass;
264 DECLARE_DUMMY_MODULE(if_bnx);
265 DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
266 DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
269 bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
271 device_t dev = sc->bnx_dev;
274 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
275 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
278 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
279 val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
280 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
285 bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
287 device_t dev = sc->bnx_dev;
289 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
290 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
293 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
294 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
295 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
300 bnx_readreg_ind(struct bnx_softc *sc, uin32_t off)
302 device_t dev = sc->bnx_dev;
304 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
305 return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
310 bnx_writereg_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
312 device_t dev = sc->bnx_dev;
314 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
315 pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
319 bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
321 CSR_WRITE_4(sc, off, val);
325 bnx_writembx(struct bnx_softc *sc, int off, int val)
327 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906)
328 off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
330 CSR_WRITE_4(sc, off, val);
334 bnx_nvram_getbyte(struct bnx_softc *sc, int addr, uint8_t *dest)
336 uint32_t access, byte = 0;
340 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
341 for (i = 0; i < 8000; i++) {
342 if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
350 access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
351 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
353 CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
354 CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
355 for (i = 0; i < BNX_TIMEOUT * 10; i++) {
357 if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
363 if (i == BNX_TIMEOUT * 10) {
364 if_printf(&sc->arpcom.ac_if, "nvram read timed out\n");
369 byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
371 *dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
373 /* Disable access. */
374 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
377 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
378 CSR_READ_4(sc, BGE_NVRAM_SWARB);
384 * Read a sequence of bytes from NVRAM.
387 bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
392 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
395 for (i = 0; i < cnt; i++) {
396 err = bnx_nvram_getbyte(sc, off + i, &byte);
402 return (err ? 1 : 0);
406 * Read a byte of data stored in the EEPROM at address 'addr.' The
407 * BCM570x supports both the traditional bitbang interface and an
408 * auto access interface for reading the EEPROM. We use the auto
412 bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
418 * Enable use of auto EEPROM access so we can avoid
419 * having to use the bitbang method.
421 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
423 /* Reset the EEPROM, load the clock period. */
424 CSR_WRITE_4(sc, BGE_EE_ADDR,
425 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
428 /* Issue the read EEPROM command. */
429 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
431 /* Wait for completion */
432 for(i = 0; i < BNX_TIMEOUT * 10; i++) {
434 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
438 if (i == BNX_TIMEOUT) {
439 if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
444 byte = CSR_READ_4(sc, BGE_EE_DATA);
446 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
452 * Read a sequence of bytes from the EEPROM.
455 bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
461 for (byte = 0, err = 0, i = 0; i < len; i++) {
462 err = bnx_eeprom_getbyte(sc, off + i, &byte);
472 bnx_miibus_readreg(device_t dev, int phy, int reg)
474 struct bnx_softc *sc = device_get_softc(dev);
478 KASSERT(phy == sc->bnx_phyno,
479 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
481 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
482 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
483 CSR_WRITE_4(sc, BGE_MI_MODE,
484 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
488 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
489 BGE_MIPHY(phy) | BGE_MIREG(reg));
491 /* Poll for the PHY register access to complete. */
492 for (i = 0; i < BNX_TIMEOUT; i++) {
494 val = CSR_READ_4(sc, BGE_MI_COMM);
495 if ((val & BGE_MICOMM_BUSY) == 0) {
497 val = CSR_READ_4(sc, BGE_MI_COMM);
501 if (i == BNX_TIMEOUT) {
502 if_printf(&sc->arpcom.ac_if, "PHY read timed out "
503 "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
507 /* Restore the autopoll bit if necessary. */
508 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
509 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
513 if (val & BGE_MICOMM_READFAIL)
516 return (val & 0xFFFF);
520 bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
522 struct bnx_softc *sc = device_get_softc(dev);
525 KASSERT(phy == sc->bnx_phyno,
526 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
528 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
529 (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
532 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
533 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
534 CSR_WRITE_4(sc, BGE_MI_MODE,
535 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
539 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
540 BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
542 for (i = 0; i < BNX_TIMEOUT; i++) {
544 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
546 CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
550 if (i == BNX_TIMEOUT) {
551 if_printf(&sc->arpcom.ac_if, "PHY write timed out "
552 "(phy %d, reg %d, val %d)\n", phy, reg, val);
555 /* Restore the autopoll bit if necessary. */
556 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
557 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
565 bnx_miibus_statchg(device_t dev)
567 struct bnx_softc *sc;
568 struct mii_data *mii;
570 sc = device_get_softc(dev);
571 mii = device_get_softc(sc->bnx_miibus);
573 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
574 (IFM_ACTIVE | IFM_AVALID)) {
575 switch (IFM_SUBTYPE(mii->mii_media_active)) {
583 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
595 if (sc->bnx_link == 0)
598 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
599 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
600 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
601 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
603 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
606 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
607 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
609 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
614 * Memory management for jumbo frames.
617 bnx_alloc_jumbo_mem(struct bnx_softc *sc)
619 struct ifnet *ifp = &sc->arpcom.ac_if;
620 struct bnx_jslot *entry;
626 * Create tag for jumbo mbufs.
627 * This is really a bit of a kludge. We allocate a special
628 * jumbo buffer pool which (thanks to the way our DMA
629 * memory allocation works) will consist of contiguous
630 * pages. This means that even though a jumbo buffer might
631 * be larger than a page size, we don't really need to
632 * map it into more than one DMA segment. However, the
633 * default mbuf tag will result in multi-segment mappings,
634 * so we have to create a special jumbo mbuf tag that
635 * lets us get away with mapping the jumbo buffers as
636 * a single segment. I think eventually the driver should
637 * be changed so that it uses ordinary mbufs and cluster
638 * buffers, i.e. jumbo frames can span multiple DMA
639 * descriptors. But that's a project for another day.
643 * Create DMA stuffs for jumbo RX ring.
645 error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
646 &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
647 &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
648 (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
649 &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
651 if_printf(ifp, "could not create jumbo RX ring\n");
656 * Create DMA stuffs for jumbo buffer block.
658 error = bnx_dma_block_alloc(sc, BNX_JMEM,
659 &sc->bnx_cdata.bnx_jumbo_tag,
660 &sc->bnx_cdata.bnx_jumbo_map,
661 (void **)&sc->bnx_ldata.bnx_jumbo_buf,
664 if_printf(ifp, "could not create jumbo buffer\n");
668 SLIST_INIT(&sc->bnx_jfree_listhead);
671 * Now divide it up into 9K pieces and save the addresses
672 * in an array. Note that we play an evil trick here by using
673 * the first few bytes in the buffer to hold the the address
674 * of the softc structure for this interface. This is because
675 * bnx_jfree() needs it, but it is called by the mbuf management
676 * code which will not pass it to us explicitly.
678 for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
679 entry = &sc->bnx_cdata.bnx_jslots[i];
681 entry->bnx_buf = ptr;
682 entry->bnx_paddr = paddr;
683 entry->bnx_inuse = 0;
685 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
694 bnx_free_jumbo_mem(struct bnx_softc *sc)
696 /* Destroy jumbo RX ring. */
697 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
698 sc->bnx_cdata.bnx_rx_jumbo_ring_map,
699 sc->bnx_ldata.bnx_rx_jumbo_ring);
701 /* Destroy jumbo buffer block. */
702 bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
703 sc->bnx_cdata.bnx_jumbo_map,
704 sc->bnx_ldata.bnx_jumbo_buf);
708 * Allocate a jumbo buffer.
710 static struct bnx_jslot *
711 bnx_jalloc(struct bnx_softc *sc)
713 struct bnx_jslot *entry;
715 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
716 entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
718 SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
719 entry->bnx_inuse = 1;
721 if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
723 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
728 * Adjust usage count on a jumbo buffer.
733 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
734 struct bnx_softc *sc = entry->bnx_sc;
737 panic("bnx_jref: can't find softc pointer!");
739 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
740 panic("bnx_jref: asked to reference buffer "
741 "that we don't manage!");
742 } else if (entry->bnx_inuse == 0) {
743 panic("bnx_jref: buffer already free!");
745 atomic_add_int(&entry->bnx_inuse, 1);
750 * Release a jumbo buffer.
755 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
756 struct bnx_softc *sc = entry->bnx_sc;
759 panic("bnx_jfree: can't find softc pointer!");
761 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
762 panic("bnx_jfree: asked to free buffer that we don't manage!");
763 } else if (entry->bnx_inuse == 0) {
764 panic("bnx_jfree: buffer already free!");
767 * Possible MP race to 0, use the serializer. The atomic insn
768 * is still needed for races against bnx_jref().
770 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
771 atomic_subtract_int(&entry->bnx_inuse, 1);
772 if (entry->bnx_inuse == 0) {
773 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
776 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
782 * Intialize a standard receive ring descriptor.
785 bnx_newbuf_std(struct bnx_softc *sc, int i, int init)
787 struct mbuf *m_new = NULL;
788 bus_dma_segment_t seg;
792 m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
795 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
796 m_adj(m_new, ETHER_ALIGN);
798 error = bus_dmamap_load_mbuf_segment(sc->bnx_cdata.bnx_rx_mtag,
799 sc->bnx_cdata.bnx_rx_tmpmap, m_new,
800 &seg, 1, &nsegs, BUS_DMA_NOWAIT);
807 bus_dmamap_sync(sc->bnx_cdata.bnx_rx_mtag,
808 sc->bnx_cdata.bnx_rx_std_dmamap[i],
809 BUS_DMASYNC_POSTREAD);
810 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
811 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
814 map = sc->bnx_cdata.bnx_rx_tmpmap;
815 sc->bnx_cdata.bnx_rx_tmpmap = sc->bnx_cdata.bnx_rx_std_dmamap[i];
816 sc->bnx_cdata.bnx_rx_std_dmamap[i] = map;
818 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_mbuf = m_new;
819 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_paddr = seg.ds_addr;
821 bnx_setup_rxdesc_std(sc, i);
826 bnx_setup_rxdesc_std(struct bnx_softc *sc, int i)
828 struct bnx_rxchain *rc;
831 rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
832 r = &sc->bnx_ldata.bnx_rx_std_ring[i];
834 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
835 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
836 r->bge_len = rc->bnx_mbuf->m_len;
838 r->bge_flags = BGE_RXBDFLAG_END;
842 * Initialize a jumbo receive ring descriptor. This allocates
843 * a jumbo buffer from the pool managed internally by the driver.
846 bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
848 struct mbuf *m_new = NULL;
849 struct bnx_jslot *buf;
852 /* Allocate the mbuf. */
853 MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
857 /* Allocate the jumbo buffer */
858 buf = bnx_jalloc(sc);
864 /* Attach the buffer to the mbuf. */
865 m_new->m_ext.ext_arg = buf;
866 m_new->m_ext.ext_buf = buf->bnx_buf;
867 m_new->m_ext.ext_free = bnx_jfree;
868 m_new->m_ext.ext_ref = bnx_jref;
869 m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
871 m_new->m_flags |= M_EXT;
873 m_new->m_data = m_new->m_ext.ext_buf;
874 m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
876 paddr = buf->bnx_paddr;
877 m_adj(m_new, ETHER_ALIGN);
878 paddr += ETHER_ALIGN;
880 /* Save necessary information */
881 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_mbuf = m_new;
882 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_paddr = paddr;
884 /* Set up the descriptor. */
885 bnx_setup_rxdesc_jumbo(sc, i);
890 bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
893 struct bnx_rxchain *rc;
895 r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
896 rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
898 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
899 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
900 r->bge_len = rc->bnx_mbuf->m_len;
902 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
906 bnx_init_rx_ring_std(struct bnx_softc *sc)
910 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
911 error = bnx_newbuf_std(sc, i, 1);
916 sc->bnx_std = BGE_STD_RX_RING_CNT - 1;
917 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
923 bnx_free_rx_ring_std(struct bnx_softc *sc)
927 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
928 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
930 if (rc->bnx_mbuf != NULL) {
931 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
932 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
933 m_freem(rc->bnx_mbuf);
936 bzero(&sc->bnx_ldata.bnx_rx_std_ring[i],
937 sizeof(struct bge_rx_bd));
942 bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
947 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
948 error = bnx_newbuf_jumbo(sc, i, 1);
953 sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
955 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
956 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
957 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
959 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
965 bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
969 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
970 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
972 if (rc->bnx_mbuf != NULL) {
973 m_freem(rc->bnx_mbuf);
976 bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
977 sizeof(struct bge_rx_bd));
982 bnx_free_tx_ring(struct bnx_softc *sc)
986 for (i = 0; i < BGE_TX_RING_CNT; i++) {
987 if (sc->bnx_cdata.bnx_tx_chain[i] != NULL) {
988 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
989 sc->bnx_cdata.bnx_tx_dmamap[i]);
990 m_freem(sc->bnx_cdata.bnx_tx_chain[i]);
991 sc->bnx_cdata.bnx_tx_chain[i] = NULL;
993 bzero(&sc->bnx_ldata.bnx_tx_ring[i],
994 sizeof(struct bge_tx_bd));
999 bnx_init_tx_ring(struct bnx_softc *sc)
1002 sc->bnx_tx_saved_considx = 0;
1003 sc->bnx_tx_prodidx = 0;
1005 /* Initialize transmit producer index for host-memory send ring. */
1006 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bnx_tx_prodidx);
1007 bnx_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1013 bnx_setmulti(struct bnx_softc *sc)
1016 struct ifmultiaddr *ifma;
1017 uint32_t hashes[4] = { 0, 0, 0, 0 };
1020 ifp = &sc->arpcom.ac_if;
1022 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1023 for (i = 0; i < 4; i++)
1024 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1028 /* First, zot all the existing filters. */
1029 for (i = 0; i < 4; i++)
1030 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1032 /* Now program new ones. */
1033 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1034 if (ifma->ifma_addr->sa_family != AF_LINK)
1037 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1038 ETHER_ADDR_LEN) & 0x7f;
1039 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1042 for (i = 0; i < 4; i++)
1043 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1047 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1048 * self-test results.
1051 bnx_chipinit(struct bnx_softc *sc)
1053 uint32_t dma_rw_ctl, mode_ctl;
1056 /* Set endian type before we access any non-PCI registers. */
1057 pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
1058 BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
1060 /* Clear the MAC control register */
1061 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1064 * Clear the MAC statistics block in the NIC's
1067 for (i = BGE_STATS_BLOCK;
1068 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1069 BNX_MEMWIN_WRITE(sc, i, 0);
1071 for (i = BGE_STATUS_BLOCK;
1072 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1073 BNX_MEMWIN_WRITE(sc, i, 0);
1075 if (BNX_IS_57765_FAMILY(sc)) {
1078 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1079 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1080 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1082 /* Access the lower 1K of PL PCI-E block registers. */
1083 CSR_WRITE_4(sc, BGE_MODE_CTL,
1084 val | BGE_MODECTL_PCIE_PL_SEL);
1086 val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1087 val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1088 CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1090 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1092 if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
1093 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1094 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1096 /* Access the lower 1K of DL PCI-E block registers. */
1097 CSR_WRITE_4(sc, BGE_MODE_CTL,
1098 val | BGE_MODECTL_PCIE_DL_SEL);
1100 val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1101 val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1102 val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1103 CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1105 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1108 val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1109 val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1110 val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1111 CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1115 * Set up the PCI DMA control register.
1117 dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
1119 * Disable 32bytes cache alignment for DMA write to host memory
1122 * 64bytes cache alignment for DMA write to host memory is still
1125 dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1126 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
1127 dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1129 * Enable HW workaround for controllers that misinterpret
1130 * a status tag update and leave interrupts permanently
1133 if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1134 !BNX_IS_57765_FAMILY(sc))
1135 dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1137 if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
1140 pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1143 * Set up general mode register.
1145 mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
1146 BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
1147 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1150 * Disable memory write invalidate. Apparently it is not supported
1151 * properly by these devices. Also ensure that INTx isn't disabled,
1152 * as these chips need it even when using MSI.
1154 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
1155 (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
1157 /* Set the timer prescaler (always 66Mhz) */
1158 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1160 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
1161 DELAY(40); /* XXX */
1163 /* Put PHY into ready state */
1164 BNX_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1165 CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1173 bnx_blockinit(struct bnx_softc *sc)
1175 struct bge_rcb *rcb;
1182 * Initialize the memory window pointer register so that
1183 * we can access the first 32K of internal NIC RAM. This will
1184 * allow us to set up the TX send ring RCBs and the RX return
1185 * ring RCBs, plus other things which live in NIC memory.
1187 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1189 /* Configure mbuf pool watermarks */
1190 if (BNX_IS_57765_PLUS(sc)) {
1191 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1192 if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
1193 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
1194 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
1196 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
1197 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
1199 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
1200 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1201 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
1202 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
1204 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1205 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1206 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1209 /* Configure DMA resource watermarks */
1210 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1211 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1213 /* Enable buffer manager */
1214 val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1216 * Change the arbitration algorithm of TXMBUF read request to
1217 * round-robin instead of priority based for BCM5719. When
1218 * TXFIFO is almost empty, RDMA will hold its request until
1219 * TXFIFO is not almost empty.
1221 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
1222 val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1223 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1224 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1225 sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1226 val |= BGE_BMANMODE_LOMBUF_ATTN;
1227 CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
1229 /* Poll for buffer manager start indication */
1230 for (i = 0; i < BNX_TIMEOUT; i++) {
1231 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1236 if (i == BNX_TIMEOUT) {
1237 if_printf(&sc->arpcom.ac_if,
1238 "buffer manager failed to start\n");
1242 /* Enable flow-through queues */
1243 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1244 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1246 /* Wait until queue initialization is complete */
1247 for (i = 0; i < BNX_TIMEOUT; i++) {
1248 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1253 if (i == BNX_TIMEOUT) {
1254 if_printf(&sc->arpcom.ac_if,
1255 "flow-through queue init failed\n");
1260 * Summary of rings supported by the controller:
1262 * Standard Receive Producer Ring
1263 * - This ring is used to feed receive buffers for "standard"
1264 * sized frames (typically 1536 bytes) to the controller.
1266 * Jumbo Receive Producer Ring
1267 * - This ring is used to feed receive buffers for jumbo sized
1268 * frames (i.e. anything bigger than the "standard" frames)
1269 * to the controller.
1271 * Mini Receive Producer Ring
1272 * - This ring is used to feed receive buffers for "mini"
1273 * sized frames to the controller.
1274 * - This feature required external memory for the controller
1275 * but was never used in a production system. Should always
1278 * Receive Return Ring
1279 * - After the controller has placed an incoming frame into a
1280 * receive buffer that buffer is moved into a receive return
1281 * ring. The driver is then responsible to passing the
1282 * buffer up to the stack. Many versions of the controller
1283 * support multiple RR rings.
1286 * - This ring is used for outgoing frames. Many versions of
1287 * the controller support multiple send rings.
1290 /* Initialize the standard receive producer ring control block. */
1291 rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
1292 rcb->bge_hostaddr.bge_addr_lo =
1293 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1294 rcb->bge_hostaddr.bge_addr_hi =
1295 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1296 if (BNX_IS_57765_PLUS(sc)) {
1298 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
1299 * Bits 15-2 : Maximum RX frame size
1300 * Bit 1 : 1 = Ring Disabled, 0 = Ring ENabled
1303 rcb->bge_maxlen_flags =
1304 BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
1307 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
1308 * Bits 15-2 : Reserved (should be 0)
1309 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
1312 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1314 if (BNX_IS_5717_PLUS(sc))
1315 rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
1317 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1318 /* Write the standard receive producer ring control block. */
1319 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1320 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1321 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1322 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1323 /* Reset the standard receive producer ring producer index. */
1324 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1327 * Initialize the jumbo RX producer ring control
1328 * block. We set the 'ring disabled' bit in the
1329 * flags field until we're actually ready to start
1330 * using this ring (i.e. once we set the MTU
1331 * high enough to require it).
1333 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1334 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
1335 /* Get the jumbo receive producer ring RCB parameters. */
1336 rcb->bge_hostaddr.bge_addr_lo =
1337 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1338 rcb->bge_hostaddr.bge_addr_hi =
1339 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1340 rcb->bge_maxlen_flags =
1341 BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
1342 BGE_RCB_FLAG_RING_DISABLED);
1343 if (BNX_IS_5717_PLUS(sc))
1344 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
1346 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1347 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1348 rcb->bge_hostaddr.bge_addr_hi);
1349 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1350 rcb->bge_hostaddr.bge_addr_lo);
1351 /* Program the jumbo receive producer ring RCB parameters. */
1352 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1353 rcb->bge_maxlen_flags);
1354 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1355 /* Reset the jumbo receive producer ring producer index. */
1356 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1359 /* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1360 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
1361 (sc->bnx_chipid == BGE_CHIPID_BCM5906_A0 ||
1362 sc->bnx_chipid == BGE_CHIPID_BCM5906_A1 ||
1363 sc->bnx_chipid == BGE_CHIPID_BCM5906_A2)) {
1364 CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
1365 (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1369 * The BD ring replenish thresholds control how often the
1370 * hardware fetches new BD's from the producer rings in host
1371 * memory. Setting the value too low on a busy system can
1372 * starve the hardware and recue the throughpout.
1374 * Set the BD ring replentish thresholds. The recommended
1375 * values are 1/8th the number of descriptors allocated to
1379 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
1380 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1381 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
1382 BGE_JUMBO_RX_RING_CNT/8);
1384 if (BNX_IS_57765_PLUS(sc)) {
1385 CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
1386 CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
1390 * Disable all send rings by setting the 'ring disabled' bit
1391 * in the flags field of all the TX send ring control blocks,
1392 * located in NIC memory.
1394 if (BNX_IS_5717_PLUS(sc))
1396 else if (BNX_IS_57765_FAMILY(sc))
1400 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1401 for (i = 0; i < limit; i++) {
1402 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1403 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1404 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1405 vrcb += sizeof(struct bge_rcb);
1408 /* Configure send ring RCB 0 (we use only the first ring) */
1409 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1410 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_tx_ring_paddr);
1411 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1412 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1413 if (BNX_IS_5717_PLUS(sc)) {
1414 RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
1416 RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1417 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1419 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1420 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1423 * Disable all receive return rings by setting the
1424 * 'ring disabled' bit in the flags field of all the receive
1425 * return ring control blocks, located in NIC memory.
1427 if (BNX_IS_5717_PLUS(sc)) {
1428 /* Should be 17, use 16 until we get an SRAM map. */
1430 } else if (BNX_IS_57765_FAMILY(sc)) {
1435 /* Disable all receive return rings. */
1436 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1437 for (i = 0; i < limit; i++) {
1438 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1439 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1440 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1441 BGE_RCB_FLAG_RING_DISABLED);
1442 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1443 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
1444 (i * (sizeof(uint64_t))), 0);
1445 vrcb += sizeof(struct bge_rcb);
1449 * Set up receive return ring 0. Note that the NIC address
1450 * for RX return rings is 0x0. The return rings live entirely
1451 * within the host, so the nicaddr field in the RCB isn't used.
1453 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1454 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_rx_return_ring_paddr);
1455 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1456 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1457 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1458 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1459 BGE_RCB_MAXLEN_FLAGS(sc->bnx_return_ring_cnt, 0));
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 val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
1472 (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
1474 CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
1477 * Specify which ring to use for packets that don't match
1480 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1483 * Configure number of RX lists. One interrupt distribution
1484 * list, sixteen active lists, one bad frames class.
1486 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1488 /* Inialize RX list placement stats mask. */
1489 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1490 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1492 /* Disable host coalescing until we get it set up */
1493 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1495 /* Poll to make sure it's shut down. */
1496 for (i = 0; i < BNX_TIMEOUT; i++) {
1497 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1502 if (i == BNX_TIMEOUT) {
1503 if_printf(&sc->arpcom.ac_if,
1504 "host coalescing engine failed to idle\n");
1508 /* Set up host coalescing defaults */
1509 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bnx_rx_coal_ticks);
1510 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bnx_tx_coal_ticks);
1511 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bnx_rx_coal_bds);
1512 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bnx_tx_coal_bds);
1513 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, sc->bnx_rx_coal_bds_int);
1514 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, sc->bnx_tx_coal_bds_int);
1516 /* Set up address of status block */
1517 bzero(sc->bnx_ldata.bnx_status_block, BGE_STATUS_BLK_SZ);
1518 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1519 BGE_ADDR_HI(sc->bnx_ldata.bnx_status_block_paddr));
1520 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1521 BGE_ADDR_LO(sc->bnx_ldata.bnx_status_block_paddr));
1523 /* Set up status block partail update size. */
1524 val = BGE_STATBLKSZ_32BYTE;
1527 * Does not seem to have visible effect in both
1528 * bulk data (1472B UDP datagram) and tiny data
1529 * (18B UDP datagram) TX tests.
1531 val |= BGE_HCCMODE_CLRTICK_TX;
1533 /* Turn on host coalescing state machine */
1534 CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
1536 /* Turn on RX BD completion state machine and enable attentions */
1537 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1538 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1540 /* Turn on RX list placement state machine */
1541 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1543 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1544 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1545 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1546 BGE_MACMODE_FRMHDR_DMA_ENB;
1548 if (sc->bnx_flags & BNX_FLAG_TBI)
1549 val |= BGE_PORTMODE_TBI;
1550 else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
1551 val |= BGE_PORTMODE_GMII;
1553 val |= BGE_PORTMODE_MII;
1555 /* Turn on DMA, clear stats */
1556 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
1558 /* Set misc. local control, enable interrupts on attentions */
1559 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1562 /* Assert GPIO pins for PHY reset */
1563 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1564 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1565 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1566 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1569 /* Turn on write DMA state machine */
1570 val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
1571 /* Enable host coalescing bug fix. */
1572 val |= BGE_WDMAMODE_STATUS_TAG_FIX;
1573 if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
1574 /* Request larger DMA burst size to get better performance. */
1575 val |= BGE_WDMAMODE_BURST_ALL_DATA;
1577 CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
1580 if (BNX_IS_57765_PLUS(sc)) {
1583 dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
1585 * Adjust tx margin to prevent TX data corruption and
1586 * fix internal FIFO overflow.
1588 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1589 sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
1590 dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
1591 BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
1592 BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
1593 dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
1594 BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
1595 BGE_RDMA_RSRVCTRL_TXMRGN_320B;
1598 * Enable fix for read DMA FIFO overruns.
1599 * The fix is to limit the number of RX BDs
1600 * the hardware would fetch at a fime.
1602 CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
1603 dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1606 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
1607 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
1608 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
1609 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
1610 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1611 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
1613 * Allow 4KB burst length reads for non-LSO frames.
1614 * Enable 512B burst length reads for buffer descriptors.
1616 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
1617 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
1618 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
1619 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1622 /* Turn on read DMA state machine */
1623 val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1624 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
1625 val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
1626 if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
1627 sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
1628 sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
1629 val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1630 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1631 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
1633 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
1634 val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
1635 BGE_RDMAMODE_H2BNC_VLAN_DET;
1637 * Allow multiple outstanding read requests from
1638 * non-LSO read DMA engine.
1640 val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
1642 if (sc->bnx_flags & BNX_FLAG_TSO)
1643 val |= BGE_RDMAMODE_TSO4_ENABLE;
1644 val |= BGE_RDMAMODE_FIFO_LONG_BURST;
1645 CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
1648 /* Turn on RX data completion state machine */
1649 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1651 /* Turn on RX BD initiator state machine */
1652 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1654 /* Turn on RX data and RX BD initiator state machine */
1655 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1657 /* Turn on send BD completion state machine */
1658 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1660 /* Turn on send data completion state machine */
1661 val = BGE_SDCMODE_ENABLE;
1662 if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
1663 val |= BGE_SDCMODE_CDELAY;
1664 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
1666 /* Turn on send data initiator state machine */
1667 if (sc->bnx_flags & BNX_FLAG_TSO) {
1668 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
1669 BGE_SDIMODE_HW_LSO_PRE_DMA);
1671 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1674 /* Turn on send BD initiator state machine */
1675 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1677 /* Turn on send BD selector state machine */
1678 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1680 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1681 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1682 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1684 /* ack/clear link change events */
1685 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1686 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1687 BGE_MACSTAT_LINK_CHANGED);
1688 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1691 * Enable attention when the link has changed state for
1692 * devices that use auto polling.
1694 if (sc->bnx_flags & BNX_FLAG_TBI) {
1695 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1697 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
1698 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
1704 * Clear any pending link state attention.
1705 * Otherwise some link state change events may be lost until attention
1706 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
1707 * It's not necessary on newer BCM chips - perhaps enabling link
1708 * state change attentions implies clearing pending attention.
1710 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1711 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1712 BGE_MACSTAT_LINK_CHANGED);
1714 /* Enable link state change attentions. */
1715 BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1721 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1722 * against our list and return its name if we find a match. Note
1723 * that since the Broadcom controller contains VPD support, we
1724 * can get the device name string from the controller itself instead
1725 * of the compiled-in string. This is a little slow, but it guarantees
1726 * we'll always announce the right product name.
1729 bnx_probe(device_t dev)
1731 const struct bnx_type *t;
1732 uint16_t product, vendor;
1734 if (!pci_is_pcie(dev))
1737 product = pci_get_device(dev);
1738 vendor = pci_get_vendor(dev);
1740 for (t = bnx_devs; t->bnx_name != NULL; t++) {
1741 if (vendor == t->bnx_vid && product == t->bnx_did)
1744 if (t->bnx_name == NULL)
1747 device_set_desc(dev, t->bnx_name);
1752 bnx_attach(device_t dev)
1755 struct bnx_softc *sc;
1757 int error = 0, rid, capmask;
1758 uint8_t ether_addr[ETHER_ADDR_LEN];
1760 driver_intr_t *intr_func;
1761 uintptr_t mii_priv = 0;
1763 #ifdef BNX_TSO_DEBUG
1768 sc = device_get_softc(dev);
1770 callout_init_mp(&sc->bnx_stat_timer);
1771 callout_init_mp(&sc->bnx_intr_timer);
1772 lwkt_serialize_init(&sc->bnx_jslot_serializer);
1774 product = pci_get_device(dev);
1776 #ifndef BURN_BRIDGES
1777 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1780 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1781 mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
1783 device_printf(dev, "chip is in D%d power mode "
1784 "-- setting to D0\n", pci_get_powerstate(dev));
1786 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1788 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1789 pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
1791 #endif /* !BURN_BRIDGE */
1794 * Map control/status registers.
1796 pci_enable_busmaster(dev);
1799 sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1802 if (sc->bnx_res == NULL) {
1803 device_printf(dev, "couldn't map memory\n");
1807 sc->bnx_btag = rman_get_bustag(sc->bnx_res);
1808 sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
1810 /* Save various chip information */
1812 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
1813 BGE_PCIMISCCTL_ASICREV_SHIFT;
1814 if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
1815 /* All chips having dedicated ASICREV register have CPMU */
1816 sc->bnx_flags |= BNX_FLAG_CPMU;
1819 case PCI_PRODUCT_BROADCOM_BCM5717:
1820 case PCI_PRODUCT_BROADCOM_BCM5717C:
1821 case PCI_PRODUCT_BROADCOM_BCM5718:
1822 case PCI_PRODUCT_BROADCOM_BCM5719:
1823 case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1824 sc->bnx_chipid = pci_read_config(dev,
1825 BGE_PCI_GEN2_PRODID_ASICREV, 4);
1828 case PCI_PRODUCT_BROADCOM_BCM57761:
1829 case PCI_PRODUCT_BROADCOM_BCM57762:
1830 case PCI_PRODUCT_BROADCOM_BCM57765:
1831 case PCI_PRODUCT_BROADCOM_BCM57766:
1832 case PCI_PRODUCT_BROADCOM_BCM57781:
1833 case PCI_PRODUCT_BROADCOM_BCM57782:
1834 case PCI_PRODUCT_BROADCOM_BCM57785:
1835 case PCI_PRODUCT_BROADCOM_BCM57786:
1836 case PCI_PRODUCT_BROADCOM_BCM57791:
1837 case PCI_PRODUCT_BROADCOM_BCM57795:
1838 sc->bnx_chipid = pci_read_config(dev,
1839 BGE_PCI_GEN15_PRODID_ASICREV, 4);
1843 sc->bnx_chipid = pci_read_config(dev,
1844 BGE_PCI_PRODID_ASICREV, 4);
1848 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1849 sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1851 sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
1852 sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
1854 switch (sc->bnx_asicrev) {
1855 case BGE_ASICREV_BCM5717:
1856 case BGE_ASICREV_BCM5719:
1857 case BGE_ASICREV_BCM5720:
1858 sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1861 case BGE_ASICREV_BCM57765:
1862 case BGE_ASICREV_BCM57766:
1863 sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
1866 sc->bnx_flags |= BNX_FLAG_SHORTDMA;
1868 sc->bnx_flags |= BNX_FLAG_TSO;
1869 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
1870 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
1871 sc->bnx_flags &= ~BNX_FLAG_TSO;
1873 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1874 BNX_IS_57765_FAMILY(sc)) {
1876 * All BCM57785 and BCM5718 families chips have a bug that
1877 * under certain situation interrupt will not be enabled
1878 * even if status tag is written to BGE_MBX_IRQ0_LO mailbox.
1880 * While BCM5719 and BCM5720 have a hardware workaround
1881 * which could fix the above bug.
1882 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1885 * For the rest of the chips in these two families, we will
1886 * have to poll the status block at high rate (10ms currently)
1887 * to check whether the interrupt is hosed or not.
1888 * See bnx_intr_check() for details.
1890 sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1893 sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
1894 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1895 sc->bnx_asicrev == BGE_ASICREV_BCM5720)
1896 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
1898 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
1899 device_printf(dev, "CHIP ID 0x%08x; "
1900 "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
1901 sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
1904 * Set various PHY quirk flags.
1907 capmask = MII_CAPMASK_DEFAULT;
1908 if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
1909 product == PCI_PRODUCT_BROADCOM_BCM57795) {
1911 capmask &= ~BMSR_EXTSTAT;
1914 mii_priv |= BRGPHY_FLAG_WIRESPEED;
1917 * Allocate interrupt
1919 sc->bnx_irq_type = pci_alloc_1intr(dev, bnx_msi_enable, &sc->bnx_irq_rid,
1922 sc->bnx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->bnx_irq_rid,
1924 if (sc->bnx_irq == NULL) {
1925 device_printf(dev, "couldn't map interrupt\n");
1930 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
1931 sc->bnx_flags |= BNX_FLAG_ONESHOT_MSI;
1935 /* Initialize if_name earlier, so if_printf could be used */
1936 ifp = &sc->arpcom.ac_if;
1937 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1939 /* Try to reset the chip. */
1942 if (bnx_chipinit(sc)) {
1943 device_printf(dev, "chip initialization failed\n");
1949 * Get station address
1951 error = bnx_get_eaddr(sc, ether_addr);
1953 device_printf(dev, "failed to read station address\n");
1957 if (BNX_IS_57765_PLUS(sc)) {
1958 sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT;
1960 /* 5705/5750 limits RX return ring to 512 entries. */
1961 sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
1964 error = bnx_dma_alloc(sc);
1968 /* Set default tuneable values. */
1969 sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
1970 sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
1971 sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
1972 sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
1973 sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
1974 sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
1975 sc->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
1977 /* Set up ifnet structure */
1979 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1980 ifp->if_ioctl = bnx_ioctl;
1981 ifp->if_start = bnx_start;
1982 #ifdef IFPOLL_ENABLE
1983 ifp->if_npoll = bnx_npoll;
1985 ifp->if_watchdog = bnx_watchdog;
1986 ifp->if_init = bnx_init;
1987 ifp->if_mtu = ETHERMTU;
1988 ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1989 ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
1990 ifq_set_ready(&ifp->if_snd);
1992 ifp->if_capabilities |= IFCAP_HWCSUM;
1993 ifp->if_hwassist = BNX_CSUM_FEATURES;
1994 if (sc->bnx_flags & BNX_FLAG_TSO) {
1995 ifp->if_capabilities |= IFCAP_TSO;
1996 ifp->if_hwassist |= CSUM_TSO;
1998 ifp->if_capenable = ifp->if_capabilities;
2001 * Figure out what sort of media we have by checking the
2002 * hardware config word in the first 32k of NIC internal memory,
2003 * or fall back to examining the EEPROM if necessary.
2004 * Note: on some BCM5700 cards, this value appears to be unset.
2005 * If that's the case, we have to rely on identifying the NIC
2006 * by its PCI subsystem ID, as we do below for the SysKonnect
2009 if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2010 hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2012 if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2014 device_printf(dev, "failed to read EEPROM\n");
2018 hwcfg = ntohl(hwcfg);
2021 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
2022 if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
2023 (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2024 sc->bnx_flags |= BNX_FLAG_TBI;
2027 if (sc->bnx_flags & BNX_FLAG_CPMU)
2028 sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
2030 sc->bnx_mi_mode = BGE_MIMODE_BASE;
2032 /* Setup link status update stuffs */
2033 if (sc->bnx_flags & BNX_FLAG_TBI) {
2034 sc->bnx_link_upd = bnx_tbi_link_upd;
2035 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2036 } else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
2037 sc->bnx_link_upd = bnx_autopoll_link_upd;
2038 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2040 sc->bnx_link_upd = bnx_copper_link_upd;
2041 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2044 /* Set default PHY address */
2048 * PHY address mapping for various devices.
2050 * | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
2051 * ---------+-------+-------+-------+-------+
2052 * BCM57XX | 1 | X | X | X |
2053 * BCM5704 | 1 | X | 1 | X |
2054 * BCM5717 | 1 | 8 | 2 | 9 |
2055 * BCM5719 | 1 | 8 | 2 | 9 |
2056 * BCM5720 | 1 | 8 | 2 | 9 |
2058 * Other addresses may respond but they are not
2059 * IEEE compliant PHYs and should be ignored.
2061 if (BNX_IS_5717_PLUS(sc)) {
2064 f = pci_get_function(dev);
2065 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
2066 if (CSR_READ_4(sc, BGE_SGDIG_STS) &
2067 BGE_SGDIGSTS_IS_SERDES)
2068 sc->bnx_phyno = f + 8;
2070 sc->bnx_phyno = f + 1;
2072 if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
2073 BGE_CPMU_PHY_STRAP_IS_SERDES)
2074 sc->bnx_phyno = f + 8;
2076 sc->bnx_phyno = f + 1;
2080 if (sc->bnx_flags & BNX_FLAG_TBI) {
2081 ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
2082 bnx_ifmedia_upd, bnx_ifmedia_sts);
2083 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2084 ifmedia_add(&sc->bnx_ifmedia,
2085 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2086 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2087 ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
2088 sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
2090 struct mii_probe_args mii_args;
2092 mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
2093 mii_args.mii_probemask = 1 << sc->bnx_phyno;
2094 mii_args.mii_capmask = capmask;
2095 mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
2096 mii_args.mii_priv = mii_priv;
2098 error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
2100 device_printf(dev, "MII without any PHY!\n");
2106 * Create sysctl nodes.
2108 sysctl_ctx_init(&sc->bnx_sysctl_ctx);
2109 sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
2110 SYSCTL_STATIC_CHILDREN(_hw),
2112 device_get_nameunit(dev),
2114 if (sc->bnx_sysctl_tree == NULL) {
2115 device_printf(dev, "can't add sysctl node\n");
2120 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2121 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2122 OID_AUTO, "rx_coal_ticks",
2123 CTLTYPE_INT | CTLFLAG_RW,
2124 sc, 0, bnx_sysctl_rx_coal_ticks, "I",
2125 "Receive coalescing ticks (usec).");
2126 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2127 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2128 OID_AUTO, "tx_coal_ticks",
2129 CTLTYPE_INT | CTLFLAG_RW,
2130 sc, 0, bnx_sysctl_tx_coal_ticks, "I",
2131 "Transmit coalescing ticks (usec).");
2132 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2133 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2134 OID_AUTO, "rx_coal_bds",
2135 CTLTYPE_INT | CTLFLAG_RW,
2136 sc, 0, bnx_sysctl_rx_coal_bds, "I",
2137 "Receive max coalesced BD count.");
2138 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2139 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2140 OID_AUTO, "tx_coal_bds",
2141 CTLTYPE_INT | CTLFLAG_RW,
2142 sc, 0, bnx_sysctl_tx_coal_bds, "I",
2143 "Transmit max coalesced BD count.");
2145 * A common design characteristic for many Broadcom
2146 * client controllers is that they only support a
2147 * single outstanding DMA read operation on the PCIe
2148 * bus. This means that it will take twice as long to
2149 * fetch a TX frame that is split into header and
2150 * payload buffers as it does to fetch a single,
2151 * contiguous TX frame (2 reads vs. 1 read). For these
2152 * controllers, coalescing buffers to reduce the number
2153 * of memory reads is effective way to get maximum
2154 * performance(about 940Mbps). Without collapsing TX
2155 * buffers the maximum TCP bulk transfer performance
2156 * is about 850Mbps. However forcing coalescing mbufs
2157 * consumes a lot of CPU cycles, so leave it off by
2160 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2161 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2162 "force_defrag", CTLFLAG_RW, &sc->bnx_force_defrag, 0,
2163 "Force defragment on TX path");
2165 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2166 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2167 "tx_wreg", CTLFLAG_RW, &sc->bnx_tx_wreg, 0,
2168 "# of segments before writing to hardware register");
2170 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2171 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2172 "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2173 sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
2174 "Receive max coalesced BD count during interrupt.");
2175 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2176 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2177 "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2178 sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
2179 "Transmit max coalesced BD count during interrupt.");
2181 #ifdef BNX_TSO_DEBUG
2182 for (i = 0; i < BNX_TSO_NSTATS; ++i) {
2183 ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
2184 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2185 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2186 desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
2191 * Call MI attach routine.
2193 ether_ifattach(ifp, ether_addr, NULL);
2195 #ifdef IFPOLL_ENABLE
2196 ifpoll_compat_setup(&sc->bnx_npoll,
2197 &sc->bnx_sysctl_ctx, sc->bnx_sysctl_tree,
2198 device_get_unit(dev), ifp->if_serializer);
2201 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
2202 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
2203 intr_func = bnx_msi_oneshot;
2205 device_printf(dev, "oneshot MSI\n");
2207 intr_func = bnx_msi;
2210 intr_func = bnx_intr_legacy;
2212 error = bus_setup_intr(dev, sc->bnx_irq, INTR_MPSAFE, intr_func, sc,
2213 &sc->bnx_intrhand, ifp->if_serializer);
2215 ether_ifdetach(ifp);
2216 device_printf(dev, "couldn't set up irq\n");
2220 sc->bnx_intr_cpuid = rman_get_cpuid(sc->bnx_irq);
2221 sc->bnx_stat_cpuid = sc->bnx_intr_cpuid;
2222 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2231 bnx_detach(device_t dev)
2233 struct bnx_softc *sc = device_get_softc(dev);
2235 if (device_is_attached(dev)) {
2236 struct ifnet *ifp = &sc->arpcom.ac_if;
2238 lwkt_serialize_enter(ifp->if_serializer);
2241 bus_teardown_intr(dev, sc->bnx_irq, sc->bnx_intrhand);
2242 lwkt_serialize_exit(ifp->if_serializer);
2244 ether_ifdetach(ifp);
2247 if (sc->bnx_flags & BNX_FLAG_TBI)
2248 ifmedia_removeall(&sc->bnx_ifmedia);
2250 device_delete_child(dev, sc->bnx_miibus);
2251 bus_generic_detach(dev);
2253 if (sc->bnx_irq != NULL) {
2254 bus_release_resource(dev, SYS_RES_IRQ, sc->bnx_irq_rid,
2257 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI)
2258 pci_release_msi(dev);
2260 if (sc->bnx_res != NULL) {
2261 bus_release_resource(dev, SYS_RES_MEMORY,
2262 BGE_PCI_BAR0, sc->bnx_res);
2265 if (sc->bnx_sysctl_tree != NULL)
2266 sysctl_ctx_free(&sc->bnx_sysctl_ctx);
2274 bnx_reset(struct bnx_softc *sc)
2277 uint32_t cachesize, command, pcistate, reset;
2278 void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
2284 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
2285 write_op = bnx_writemem_direct;
2287 write_op = bnx_writereg_ind;
2289 /* Save some important PCI state. */
2290 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2291 command = pci_read_config(dev, BGE_PCI_CMD, 4);
2292 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2294 pci_write_config(dev, BGE_PCI_MISC_CTL,
2295 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2296 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2297 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2299 /* Disable fastboot on controllers that support it. */
2301 if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
2302 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
2305 * Write the magic number to SRAM at offset 0xB50.
2306 * When firmware finishes its initialization it will
2307 * write ~BGE_MAGIC_NUMBER to the same location.
2309 bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2311 reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2313 /* XXX: Broadcom Linux driver. */
2314 /* Force PCI-E 1.0a mode */
2315 if (!BNX_IS_57765_PLUS(sc) &&
2316 CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
2317 (BGE_PCIE_PHY_TSTCTL_PSCRAM |
2318 BGE_PCIE_PHY_TSTCTL_PCIE10)) {
2319 CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
2320 BGE_PCIE_PHY_TSTCTL_PSCRAM);
2322 if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
2323 /* Prevent PCIE link training during global reset */
2324 CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2329 * Set GPHY Power Down Override to leave GPHY
2330 * powered up in D0 uninitialized.
2332 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
2333 reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
2335 /* Issue global reset */
2336 write_op(sc, BGE_MISC_CFG, reset);
2338 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
2339 uint32_t status, ctrl;
2341 status = CSR_READ_4(sc, BGE_VCPU_STATUS);
2342 CSR_WRITE_4(sc, BGE_VCPU_STATUS,
2343 status | BGE_VCPU_STATUS_DRV_RESET);
2344 ctrl = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
2345 CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
2346 ctrl & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
2351 /* XXX: Broadcom Linux driver. */
2352 if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
2355 DELAY(500000); /* wait for link training to complete */
2356 v = pci_read_config(dev, 0xc4, 4);
2357 pci_write_config(dev, 0xc4, v | (1<<15), 4);
2360 devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
2362 /* Disable no snoop and disable relaxed ordering. */
2363 devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
2365 /* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
2366 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
2367 devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
2368 devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
2371 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
2374 /* Clear error status. */
2375 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
2376 PCIEM_DEVSTS_CORR_ERR |
2377 PCIEM_DEVSTS_NFATAL_ERR |
2378 PCIEM_DEVSTS_FATAL_ERR |
2379 PCIEM_DEVSTS_UNSUPP_REQ, 2);
2381 /* Reset some of the PCI state that got zapped by reset */
2382 pci_write_config(dev, BGE_PCI_MISC_CTL,
2383 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2384 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2385 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2386 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2387 pci_write_config(dev, BGE_PCI_CMD, command, 4);
2388 write_op(sc, BGE_MISC_CFG, (65 << 1));
2390 /* Enable memory arbiter */
2391 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2393 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
2394 for (i = 0; i < BNX_TIMEOUT; i++) {
2395 val = CSR_READ_4(sc, BGE_VCPU_STATUS);
2396 if (val & BGE_VCPU_STATUS_INIT_DONE)
2400 if (i == BNX_TIMEOUT) {
2401 if_printf(&sc->arpcom.ac_if, "reset timed out\n");
2406 * Poll until we see the 1's complement of the magic number.
2407 * This indicates that the firmware initialization
2410 for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
2411 val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2412 if (val == ~BGE_MAGIC_NUMBER)
2416 if (i == BNX_FIRMWARE_TIMEOUT) {
2417 if_printf(&sc->arpcom.ac_if, "firmware handshake "
2418 "timed out, found 0x%08x\n", val);
2421 /* BCM57765 A0 needs additional time before accessing. */
2422 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
2427 * XXX Wait for the value of the PCISTATE register to
2428 * return to its original pre-reset state. This is a
2429 * fairly good indicator of reset completion. If we don't
2430 * wait for the reset to fully complete, trying to read
2431 * from the device's non-PCI registers may yield garbage
2434 for (i = 0; i < BNX_TIMEOUT; i++) {
2435 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2440 /* Fix up byte swapping */
2441 CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
2443 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2446 * The 5704 in TBI mode apparently needs some special
2447 * adjustment to insure the SERDES drive level is set
2450 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
2451 (sc->bnx_flags & BNX_FLAG_TBI)) {
2454 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2455 serdescfg = (serdescfg & ~0xFFF) | 0x880;
2456 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2459 CSR_WRITE_4(sc, BGE_MI_MODE,
2460 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
2463 /* XXX: Broadcom Linux driver. */
2464 if (!BNX_IS_57765_PLUS(sc)) {
2467 /* Enable Data FIFO protection. */
2468 v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2469 CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
2474 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
2475 BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
2476 CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
2481 * Frame reception handling. This is called if there's a frame
2482 * on the receive return list.
2484 * Note: we have to be able to handle two possibilities here:
2485 * 1) the frame is from the jumbo recieve ring
2486 * 2) the frame is from the standard receive ring
2490 bnx_rxeof(struct bnx_softc *sc, uint16_t rx_prod, int count)
2493 int stdcnt = 0, jumbocnt = 0;
2495 ifp = &sc->arpcom.ac_if;
2497 while (sc->bnx_rx_saved_considx != rx_prod && count != 0) {
2498 struct bge_rx_bd *cur_rx;
2500 struct mbuf *m = NULL;
2501 uint16_t vlan_tag = 0;
2507 &sc->bnx_ldata.bnx_rx_return_ring[sc->bnx_rx_saved_considx];
2509 rxidx = cur_rx->bge_idx;
2510 BNX_INC(sc->bnx_rx_saved_considx, sc->bnx_return_ring_cnt);
2512 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2514 vlan_tag = cur_rx->bge_vlan_tag;
2517 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2518 BNX_INC(sc->bnx_jumbo, BGE_JUMBO_RX_RING_CNT);
2521 if (rxidx != sc->bnx_jumbo) {
2522 IFNET_STAT_INC(ifp, ierrors, 1);
2523 if_printf(ifp, "sw jumbo index(%d) "
2524 "and hw jumbo index(%d) mismatch, drop!\n",
2525 sc->bnx_jumbo, rxidx);
2526 bnx_setup_rxdesc_jumbo(sc, rxidx);
2530 m = sc->bnx_cdata.bnx_rx_jumbo_chain[rxidx].bnx_mbuf;
2531 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2532 IFNET_STAT_INC(ifp, ierrors, 1);
2533 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2536 if (bnx_newbuf_jumbo(sc, sc->bnx_jumbo, 0)) {
2537 IFNET_STAT_INC(ifp, ierrors, 1);
2538 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2542 BNX_INC(sc->bnx_std, BGE_STD_RX_RING_CNT);
2545 if (rxidx != sc->bnx_std) {
2546 IFNET_STAT_INC(ifp, ierrors, 1);
2547 if_printf(ifp, "sw std index(%d) "
2548 "and hw std index(%d) mismatch, drop!\n",
2549 sc->bnx_std, rxidx);
2550 bnx_setup_rxdesc_std(sc, rxidx);
2554 m = sc->bnx_cdata.bnx_rx_std_chain[rxidx].bnx_mbuf;
2555 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2556 IFNET_STAT_INC(ifp, ierrors, 1);
2557 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2560 if (bnx_newbuf_std(sc, sc->bnx_std, 0)) {
2561 IFNET_STAT_INC(ifp, ierrors, 1);
2562 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2567 IFNET_STAT_INC(ifp, ipackets, 1);
2568 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2569 m->m_pkthdr.rcvif = ifp;
2571 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2572 (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
2573 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
2574 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2575 if ((cur_rx->bge_error_flag &
2576 BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
2577 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2579 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2580 m->m_pkthdr.csum_data =
2581 cur_rx->bge_tcp_udp_csum;
2582 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
2588 * If we received a packet with a vlan tag, pass it
2589 * to vlan_input() instead of ether_input().
2592 m->m_flags |= M_VLANTAG;
2593 m->m_pkthdr.ether_vlantag = vlan_tag;
2595 ifp->if_input(ifp, m);
2598 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bnx_rx_saved_considx);
2600 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
2602 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
2606 bnx_txeof(struct bnx_softc *sc, uint16_t tx_cons)
2610 ifp = &sc->arpcom.ac_if;
2613 * Go through our tx ring and free mbufs for those
2614 * frames that have been sent.
2616 while (sc->bnx_tx_saved_considx != tx_cons) {
2619 idx = sc->bnx_tx_saved_considx;
2620 if (sc->bnx_cdata.bnx_tx_chain[idx] != NULL) {
2621 IFNET_STAT_INC(ifp, opackets, 1);
2622 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
2623 sc->bnx_cdata.bnx_tx_dmamap[idx]);
2624 m_freem(sc->bnx_cdata.bnx_tx_chain[idx]);
2625 sc->bnx_cdata.bnx_tx_chain[idx] = NULL;
2628 BNX_INC(sc->bnx_tx_saved_considx, BGE_TX_RING_CNT);
2631 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) >=
2632 (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
2633 ifq_clr_oactive(&ifp->if_snd);
2635 if (sc->bnx_txcnt == 0)
2638 if (!ifq_is_empty(&ifp->if_snd))
2642 #ifdef IFPOLL_ENABLE
2645 bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2647 struct bnx_softc *sc = ifp->if_softc;
2649 ASSERT_SERIALIZED(ifp->if_serializer);
2652 int cpuid = sc->bnx_npoll.ifpc_cpuid;
2654 info->ifpi_rx[cpuid].poll_func = bnx_npoll_compat;
2655 info->ifpi_rx[cpuid].arg = NULL;
2656 info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
2658 if (ifp->if_flags & IFF_RUNNING)
2659 bnx_disable_intr(sc);
2660 ifq_set_cpuid(&ifp->if_snd, cpuid);
2662 if (ifp->if_flags & IFF_RUNNING)
2663 bnx_enable_intr(sc);
2664 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2669 bnx_npoll_compat(struct ifnet *ifp, void *arg __unused, int cycle)
2671 struct bnx_softc *sc = ifp->if_softc;
2672 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2673 uint16_t rx_prod, tx_cons;
2675 ASSERT_SERIALIZED(ifp->if_serializer);
2677 if (sc->bnx_npoll.ifpc_stcount-- == 0) {
2678 sc->bnx_npoll.ifpc_stcount = sc->bnx_npoll.ifpc_stfrac;
2680 * Process link state changes.
2685 sc->bnx_status_tag = sblk->bge_status_tag;
2688 * Use a load fence to ensure that status_tag is saved
2689 * before rx_prod and tx_cons.
2693 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2694 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2696 if (sc->bnx_rx_saved_considx != rx_prod)
2697 bnx_rxeof(sc, rx_prod, cycle);
2699 if (sc->bnx_tx_saved_considx != tx_cons)
2700 bnx_txeof(sc, tx_cons);
2702 if (sc->bnx_coal_chg)
2703 bnx_coal_change(sc);
2706 #endif /* IFPOLL_ENABLE */
2709 bnx_intr_legacy(void *xsc)
2711 struct bnx_softc *sc = xsc;
2712 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2714 if (sc->bnx_status_tag == sblk->bge_status_tag) {
2717 val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
2718 if (val & BGE_PCISTAT_INTR_NOTACT)
2724 * Interrupt will have to be disabled if tagged status
2725 * is used, else interrupt will always be asserted on
2726 * certain chips (at least on BCM5750 AX/BX).
2728 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2736 struct bnx_softc *sc = xsc;
2738 /* Disable interrupt first */
2739 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2744 bnx_msi_oneshot(void *xsc)
2750 bnx_intr(struct bnx_softc *sc)
2752 struct ifnet *ifp = &sc->arpcom.ac_if;
2753 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2754 uint16_t rx_prod, tx_cons;
2757 sc->bnx_status_tag = sblk->bge_status_tag;
2759 * Use a load fence to ensure that status_tag is saved
2760 * before rx_prod, tx_cons and status.
2764 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2765 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2766 status = sblk->bge_status;
2768 if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
2771 if (ifp->if_flags & IFF_RUNNING) {
2772 if (sc->bnx_rx_saved_considx != rx_prod)
2773 bnx_rxeof(sc, rx_prod, -1);
2775 if (sc->bnx_tx_saved_considx != tx_cons)
2776 bnx_txeof(sc, tx_cons);
2779 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
2781 if (sc->bnx_coal_chg)
2782 bnx_coal_change(sc);
2788 struct bnx_softc *sc = xsc;
2789 struct ifnet *ifp = &sc->arpcom.ac_if;
2791 lwkt_serialize_enter(ifp->if_serializer);
2793 KKASSERT(mycpuid == sc->bnx_stat_cpuid);
2795 bnx_stats_update_regs(sc);
2797 if (sc->bnx_flags & BNX_FLAG_TBI) {
2799 * Since in TBI mode auto-polling can't be used we should poll
2800 * link status manually. Here we register pending link event
2801 * and trigger interrupt.
2804 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
2805 } else if (!sc->bnx_link) {
2806 mii_tick(device_get_softc(sc->bnx_miibus));
2809 callout_reset(&sc->bnx_stat_timer, hz, bnx_tick, sc);
2811 lwkt_serialize_exit(ifp->if_serializer);
2815 bnx_stats_update_regs(struct bnx_softc *sc)
2817 struct ifnet *ifp = &sc->arpcom.ac_if;
2818 struct bge_mac_stats_regs stats;
2822 s = (uint32_t *)&stats;
2823 for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
2824 *s = CSR_READ_4(sc, BGE_RX_STATS + i);
2828 IFNET_STAT_SET(ifp, collisions,
2829 (stats.dot3StatsSingleCollisionFrames +
2830 stats.dot3StatsMultipleCollisionFrames +
2831 stats.dot3StatsExcessiveCollisions +
2832 stats.dot3StatsLateCollisions));
2836 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
2837 * pointers to descriptors.
2840 bnx_encap(struct bnx_softc *sc, struct mbuf **m_head0, uint32_t *txidx,
2843 struct bge_tx_bd *d = NULL;
2844 uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
2845 bus_dma_segment_t segs[BNX_NSEG_NEW];
2847 int error, maxsegs, nsegs, idx, i;
2848 struct mbuf *m_head = *m_head0, *m_new;
2850 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
2851 #ifdef BNX_TSO_DEBUG
2855 error = bnx_setup_tso(sc, m_head0, &mss, &csum_flags);
2860 #ifdef BNX_TSO_DEBUG
2861 tso_nsegs = (m_head->m_pkthdr.len /
2862 m_head->m_pkthdr.tso_segsz) - 1;
2863 if (tso_nsegs > (BNX_TSO_NSTATS - 1))
2864 tso_nsegs = BNX_TSO_NSTATS - 1;
2865 else if (tso_nsegs < 0)
2867 sc->bnx_tsosegs[tso_nsegs]++;
2869 } else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
2870 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2871 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2872 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2873 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2874 if (m_head->m_flags & M_LASTFRAG)
2875 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
2876 else if (m_head->m_flags & M_FRAG)
2877 csum_flags |= BGE_TXBDFLAG_IP_FRAG;
2879 if (m_head->m_flags & M_VLANTAG) {
2880 csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
2881 vlan_tag = m_head->m_pkthdr.ether_vlantag;
2885 map = sc->bnx_cdata.bnx_tx_dmamap[idx];
2887 maxsegs = (BGE_TX_RING_CNT - sc->bnx_txcnt) - BNX_NSEG_RSVD;
2888 KASSERT(maxsegs >= BNX_NSEG_SPARE,
2889 ("not enough segments %d", maxsegs));
2891 if (maxsegs > BNX_NSEG_NEW)
2892 maxsegs = BNX_NSEG_NEW;
2895 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
2896 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
2897 * but when such padded frames employ the bge IP/TCP checksum
2898 * offload, the hardware checksum assist gives incorrect results
2899 * (possibly from incorporating its own padding into the UDP/TCP
2900 * checksum; who knows). If we pad such runts with zeros, the
2901 * onboard checksum comes out correct.
2903 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
2904 m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
2905 error = m_devpad(m_head, BNX_MIN_FRAMELEN);
2910 if ((sc->bnx_flags & BNX_FLAG_SHORTDMA) && m_head->m_next != NULL) {
2911 m_new = bnx_defrag_shortdma(m_head);
2912 if (m_new == NULL) {
2916 *m_head0 = m_head = m_new;
2918 if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
2919 sc->bnx_force_defrag && m_head->m_next != NULL) {
2921 * Forcefully defragment mbuf chain to overcome hardware
2922 * limitation which only support a single outstanding
2923 * DMA read operation. If it fails, keep moving on using
2924 * the original mbuf chain.
2926 m_new = m_defrag(m_head, MB_DONTWAIT);
2928 *m_head0 = m_head = m_new;
2931 error = bus_dmamap_load_mbuf_defrag(sc->bnx_cdata.bnx_tx_mtag, map,
2932 m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
2935 *segs_used += nsegs;
2938 bus_dmamap_sync(sc->bnx_cdata.bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
2940 for (i = 0; ; i++) {
2941 d = &sc->bnx_ldata.bnx_tx_ring[idx];
2943 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
2944 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
2945 d->bge_len = segs[i].ds_len;
2946 d->bge_flags = csum_flags;
2947 d->bge_vlan_tag = vlan_tag;
2952 BNX_INC(idx, BGE_TX_RING_CNT);
2954 /* Mark the last segment as end of packet... */
2955 d->bge_flags |= BGE_TXBDFLAG_END;
2958 * Insure that the map for this transmission is placed at
2959 * the array index of the last descriptor in this chain.
2961 sc->bnx_cdata.bnx_tx_dmamap[*txidx] = sc->bnx_cdata.bnx_tx_dmamap[idx];
2962 sc->bnx_cdata.bnx_tx_dmamap[idx] = map;
2963 sc->bnx_cdata.bnx_tx_chain[idx] = m_head;
2964 sc->bnx_txcnt += nsegs;
2966 BNX_INC(idx, BGE_TX_RING_CNT);
2977 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2978 * to the mbuf data regions directly in the transmit descriptors.
2981 bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2983 struct bnx_softc *sc = ifp->if_softc;
2984 struct mbuf *m_head = NULL;
2988 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2990 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
2993 prodidx = sc->bnx_tx_prodidx;
2995 while (sc->bnx_cdata.bnx_tx_chain[prodidx] == NULL) {
2997 * Sanity check: avoid coming within BGE_NSEG_RSVD
2998 * descriptors of the end of the ring. Also make
2999 * sure there are BGE_NSEG_SPARE descriptors for
3000 * jumbo buffers' or TSO segments' defragmentation.
3002 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) <
3003 (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
3004 ifq_set_oactive(&ifp->if_snd);
3008 m_head = ifq_dequeue(&ifp->if_snd, NULL);
3013 * Pack the data into the transmit ring. If we
3014 * don't have room, set the OACTIVE flag and wait
3015 * for the NIC to drain the ring.
3017 if (bnx_encap(sc, &m_head, &prodidx, &nsegs)) {
3018 ifq_set_oactive(&ifp->if_snd);
3019 IFNET_STAT_INC(ifp, oerrors, 1);
3023 if (nsegs >= sc->bnx_tx_wreg) {
3025 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3029 ETHER_BPF_MTAP(ifp, m_head);
3032 * Set a timeout in case the chip goes out to lunch.
3039 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3041 sc->bnx_tx_prodidx = prodidx;
3047 struct bnx_softc *sc = xsc;
3048 struct ifnet *ifp = &sc->arpcom.ac_if;
3052 ASSERT_SERIALIZED(ifp->if_serializer);
3054 /* Cancel pending I/O and flush buffers. */
3060 * Init the various state machines, ring
3061 * control blocks and firmware.
3063 if (bnx_blockinit(sc)) {
3064 if_printf(ifp, "initialization failure\n");
3070 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3071 ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
3073 /* Load our MAC address. */
3074 m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
3075 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3076 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3078 /* Enable or disable promiscuous mode as needed. */
3081 /* Program multicast filter. */
3085 if (bnx_init_rx_ring_std(sc)) {
3086 if_printf(ifp, "RX ring initialization failed\n");
3091 /* Init jumbo RX ring. */
3092 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
3093 if (bnx_init_rx_ring_jumbo(sc)) {
3094 if_printf(ifp, "Jumbo RX ring initialization failed\n");
3100 /* Init our RX return ring index */
3101 sc->bnx_rx_saved_considx = 0;
3104 bnx_init_tx_ring(sc);
3106 /* Enable TX MAC state machine lockup fix. */
3107 mode = CSR_READ_4(sc, BGE_TX_MODE);
3108 mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
3109 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
3110 mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3111 mode |= CSR_READ_4(sc, BGE_TX_MODE) &
3112 (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3114 /* Turn on transmitter */
3115 CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
3117 /* Turn on receiver */
3118 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3121 * Set the number of good frames to receive after RX MBUF
3122 * Low Watermark has been reached. After the RX MAC receives
3123 * this number of frames, it will drop subsequent incoming
3124 * frames until the MBUF High Watermark is reached.
3126 if (BNX_IS_57765_FAMILY(sc))
3127 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
3129 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
3131 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
3133 if_printf(ifp, "MSI_MODE: %#x\n",
3134 CSR_READ_4(sc, BGE_MSI_MODE));
3138 /* Tell firmware we're alive. */
3139 BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3141 /* Enable host interrupts if polling(4) is not enabled. */
3142 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
3143 #ifdef IFPOLL_ENABLE
3144 if (ifp->if_flags & IFF_NPOLLING)
3145 bnx_disable_intr(sc);
3148 bnx_enable_intr(sc);
3150 bnx_ifmedia_upd(ifp);
3152 ifp->if_flags |= IFF_RUNNING;
3153 ifq_clr_oactive(&ifp->if_snd);
3155 callout_reset_bycpu(&sc->bnx_stat_timer, hz, bnx_tick, sc,
3156 sc->bnx_stat_cpuid);
3160 * Set media options.
3163 bnx_ifmedia_upd(struct ifnet *ifp)
3165 struct bnx_softc *sc = ifp->if_softc;
3167 /* If this is a 1000baseX NIC, enable the TBI port. */
3168 if (sc->bnx_flags & BNX_FLAG_TBI) {
3169 struct ifmedia *ifm = &sc->bnx_ifmedia;
3171 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3174 switch(IFM_SUBTYPE(ifm->ifm_media)) {
3179 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3180 BNX_CLRBIT(sc, BGE_MAC_MODE,
3181 BGE_MACMODE_HALF_DUPLEX);
3183 BNX_SETBIT(sc, BGE_MAC_MODE,
3184 BGE_MACMODE_HALF_DUPLEX);
3191 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3195 if (mii->mii_instance) {
3196 struct mii_softc *miisc;
3198 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3199 mii_phy_reset(miisc);
3204 * Force an interrupt so that we will call bnx_link_upd
3205 * if needed and clear any pending link state attention.
3206 * Without this we are not getting any further interrupts
3207 * for link state changes and thus will not UP the link and
3208 * not be able to send in bnx_start. The only way to get
3209 * things working was to receive a packet and get an RX
3212 * bnx_tick should help for fiber cards and we might not
3213 * need to do this here if BNX_FLAG_TBI is set but as
3214 * we poll for fiber anyway it should not harm.
3216 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3222 * Report current media status.
3225 bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3227 struct bnx_softc *sc = ifp->if_softc;
3229 if (sc->bnx_flags & BNX_FLAG_TBI) {
3230 ifmr->ifm_status = IFM_AVALID;
3231 ifmr->ifm_active = IFM_ETHER;
3232 if (CSR_READ_4(sc, BGE_MAC_STS) &
3233 BGE_MACSTAT_TBI_PCS_SYNCHED) {
3234 ifmr->ifm_status |= IFM_ACTIVE;
3236 ifmr->ifm_active |= IFM_NONE;
3240 ifmr->ifm_active |= IFM_1000_SX;
3241 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3242 ifmr->ifm_active |= IFM_HDX;
3244 ifmr->ifm_active |= IFM_FDX;
3246 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3249 ifmr->ifm_active = mii->mii_media_active;
3250 ifmr->ifm_status = mii->mii_media_status;
3255 bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
3257 struct bnx_softc *sc = ifp->if_softc;
3258 struct ifreq *ifr = (struct ifreq *)data;
3259 int mask, error = 0;
3261 ASSERT_SERIALIZED(ifp->if_serializer);
3265 if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
3266 (BNX_IS_JUMBO_CAPABLE(sc) &&
3267 ifr->ifr_mtu > BNX_JUMBO_MTU)) {
3269 } else if (ifp->if_mtu != ifr->ifr_mtu) {
3270 ifp->if_mtu = ifr->ifr_mtu;
3271 if (ifp->if_flags & IFF_RUNNING)
3276 if (ifp->if_flags & IFF_UP) {
3277 if (ifp->if_flags & IFF_RUNNING) {
3278 mask = ifp->if_flags ^ sc->bnx_if_flags;
3281 * If only the state of the PROMISC flag
3282 * changed, then just use the 'set promisc
3283 * mode' command instead of reinitializing
3284 * the entire NIC. Doing a full re-init
3285 * means reloading the firmware and waiting
3286 * for it to start up, which may take a
3287 * second or two. Similarly for ALLMULTI.
3289 if (mask & IFF_PROMISC)
3291 if (mask & IFF_ALLMULTI)
3296 } else if (ifp->if_flags & IFF_RUNNING) {
3299 sc->bnx_if_flags = ifp->if_flags;
3303 if (ifp->if_flags & IFF_RUNNING)
3308 if (sc->bnx_flags & BNX_FLAG_TBI) {
3309 error = ifmedia_ioctl(ifp, ifr,
3310 &sc->bnx_ifmedia, command);
3312 struct mii_data *mii;
3314 mii = device_get_softc(sc->bnx_miibus);
3315 error = ifmedia_ioctl(ifp, ifr,
3316 &mii->mii_media, command);
3320 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3321 if (mask & IFCAP_HWCSUM) {
3322 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
3323 if (ifp->if_capenable & IFCAP_TXCSUM)
3324 ifp->if_hwassist |= BNX_CSUM_FEATURES;
3326 ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
3328 if (mask & IFCAP_TSO) {
3329 ifp->if_capenable ^= (mask & IFCAP_TSO);
3330 if (ifp->if_capenable & IFCAP_TSO)
3331 ifp->if_hwassist |= CSUM_TSO;
3333 ifp->if_hwassist &= ~CSUM_TSO;
3337 error = ether_ioctl(ifp, command, data);
3344 bnx_watchdog(struct ifnet *ifp)
3346 struct bnx_softc *sc = ifp->if_softc;
3348 if_printf(ifp, "watchdog timeout -- resetting\n");
3352 IFNET_STAT_INC(ifp, oerrors, 1);
3354 if (!ifq_is_empty(&ifp->if_snd))
3359 * Stop the adapter and free any mbufs allocated to the
3363 bnx_stop(struct bnx_softc *sc)
3365 struct ifnet *ifp = &sc->arpcom.ac_if;
3367 ASSERT_SERIALIZED(ifp->if_serializer);
3369 callout_stop(&sc->bnx_stat_timer);
3372 * Disable all of the receiver blocks
3374 bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3375 bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3376 bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3377 bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3378 bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3379 bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3382 * Disable all of the transmit blocks
3384 bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3385 bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3386 bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3387 bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3388 bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3389 bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3392 * Shut down all of the memory managers and related
3395 bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3396 bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3397 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3398 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3400 /* Disable host interrupts. */
3401 bnx_disable_intr(sc);
3404 * Tell firmware we're shutting down.
3406 BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3408 /* Free the RX lists. */
3409 bnx_free_rx_ring_std(sc);
3411 /* Free jumbo RX list. */
3412 if (BNX_IS_JUMBO_CAPABLE(sc))
3413 bnx_free_rx_ring_jumbo(sc);
3415 /* Free TX buffers. */
3416 bnx_free_tx_ring(sc);
3418 sc->bnx_status_tag = 0;
3420 sc->bnx_coal_chg = 0;
3422 sc->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
3424 ifp->if_flags &= ~IFF_RUNNING;
3425 ifq_clr_oactive(&ifp->if_snd);
3430 * Stop all chip I/O so that the kernel's probe routines don't
3431 * get confused by errant DMAs when rebooting.
3434 bnx_shutdown(device_t dev)
3436 struct bnx_softc *sc = device_get_softc(dev);
3437 struct ifnet *ifp = &sc->arpcom.ac_if;
3439 lwkt_serialize_enter(ifp->if_serializer);
3442 lwkt_serialize_exit(ifp->if_serializer);
3446 bnx_suspend(device_t dev)
3448 struct bnx_softc *sc = device_get_softc(dev);
3449 struct ifnet *ifp = &sc->arpcom.ac_if;
3451 lwkt_serialize_enter(ifp->if_serializer);
3453 lwkt_serialize_exit(ifp->if_serializer);
3459 bnx_resume(device_t dev)
3461 struct bnx_softc *sc = device_get_softc(dev);
3462 struct ifnet *ifp = &sc->arpcom.ac_if;
3464 lwkt_serialize_enter(ifp->if_serializer);
3466 if (ifp->if_flags & IFF_UP) {
3469 if (!ifq_is_empty(&ifp->if_snd))
3473 lwkt_serialize_exit(ifp->if_serializer);
3479 bnx_setpromisc(struct bnx_softc *sc)
3481 struct ifnet *ifp = &sc->arpcom.ac_if;
3483 if (ifp->if_flags & IFF_PROMISC)
3484 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3486 BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3490 bnx_dma_free(struct bnx_softc *sc)
3494 /* Destroy RX mbuf DMA stuffs. */
3495 if (sc->bnx_cdata.bnx_rx_mtag != NULL) {
3496 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3497 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3498 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3500 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3501 sc->bnx_cdata.bnx_rx_tmpmap);
3502 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3505 /* Destroy TX mbuf DMA stuffs. */
3506 if (sc->bnx_cdata.bnx_tx_mtag != NULL) {
3507 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3508 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3509 sc->bnx_cdata.bnx_tx_dmamap[i]);
3511 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3514 /* Destroy standard RX ring */
3515 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_std_ring_tag,
3516 sc->bnx_cdata.bnx_rx_std_ring_map,
3517 sc->bnx_ldata.bnx_rx_std_ring);
3519 if (BNX_IS_JUMBO_CAPABLE(sc))
3520 bnx_free_jumbo_mem(sc);
3522 /* Destroy RX return ring */
3523 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_return_ring_tag,
3524 sc->bnx_cdata.bnx_rx_return_ring_map,
3525 sc->bnx_ldata.bnx_rx_return_ring);
3527 /* Destroy TX ring */
3528 bnx_dma_block_free(sc->bnx_cdata.bnx_tx_ring_tag,
3529 sc->bnx_cdata.bnx_tx_ring_map,
3530 sc->bnx_ldata.bnx_tx_ring);
3532 /* Destroy status block */
3533 bnx_dma_block_free(sc->bnx_cdata.bnx_status_tag,
3534 sc->bnx_cdata.bnx_status_map,
3535 sc->bnx_ldata.bnx_status_block);
3537 /* Destroy the parent tag */
3538 if (sc->bnx_cdata.bnx_parent_tag != NULL)
3539 bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
3543 bnx_dma_alloc(struct bnx_softc *sc)
3545 struct ifnet *ifp = &sc->arpcom.ac_if;
3550 * Allocate the parent bus DMA tag appropriate for PCI.
3552 * All of the NetExtreme/NetLink controllers have 4GB boundary
3554 * Whenever an address crosses a multiple of the 4GB boundary
3555 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3556 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3557 * state machine will lockup and cause the device to hang.
3559 error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
3560 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3562 BUS_SPACE_MAXSIZE_32BIT, 0,
3563 BUS_SPACE_MAXSIZE_32BIT,
3564 0, &sc->bnx_cdata.bnx_parent_tag);
3566 if_printf(ifp, "could not allocate parent dma tag\n");
3571 * Create DMA tag and maps for RX mbufs.
3573 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3574 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3575 NULL, NULL, MCLBYTES, 1, MCLBYTES,
3576 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK,
3577 &sc->bnx_cdata.bnx_rx_mtag);
3579 if_printf(ifp, "could not allocate RX mbuf dma tag\n");
3583 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3584 BUS_DMA_WAITOK, &sc->bnx_cdata.bnx_rx_tmpmap);
3586 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3587 sc->bnx_cdata.bnx_rx_mtag = NULL;
3591 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3592 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3594 &sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3598 for (j = 0; j < i; ++j) {
3599 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3600 sc->bnx_cdata.bnx_rx_std_dmamap[j]);
3602 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3603 sc->bnx_cdata.bnx_rx_mtag = NULL;
3605 if_printf(ifp, "could not create DMA map for RX\n");
3611 * Create DMA tag and maps for TX mbufs.
3613 if (sc->bnx_flags & BNX_FLAG_TSO)
3614 txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
3616 txmaxsz = BNX_JUMBO_FRAMELEN;
3617 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3618 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3620 txmaxsz, BNX_NSEG_NEW, PAGE_SIZE,
3621 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK |
3623 &sc->bnx_cdata.bnx_tx_mtag);
3625 if_printf(ifp, "could not allocate TX mbuf dma tag\n");
3629 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3630 error = bus_dmamap_create(sc->bnx_cdata.bnx_tx_mtag,
3631 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
3632 &sc->bnx_cdata.bnx_tx_dmamap[i]);
3636 for (j = 0; j < i; ++j) {
3637 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3638 sc->bnx_cdata.bnx_tx_dmamap[j]);
3640 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3641 sc->bnx_cdata.bnx_tx_mtag = NULL;
3643 if_printf(ifp, "could not create DMA map for TX\n");
3649 * Create DMA stuffs for standard RX ring.
3651 error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
3652 &sc->bnx_cdata.bnx_rx_std_ring_tag,
3653 &sc->bnx_cdata.bnx_rx_std_ring_map,
3654 (void *)&sc->bnx_ldata.bnx_rx_std_ring,
3655 &sc->bnx_ldata.bnx_rx_std_ring_paddr);
3657 if_printf(ifp, "could not create std RX ring\n");
3662 * Create jumbo buffer pool.
3664 if (BNX_IS_JUMBO_CAPABLE(sc)) {
3665 error = bnx_alloc_jumbo_mem(sc);
3667 if_printf(ifp, "could not create jumbo buffer pool\n");
3673 * Create DMA stuffs for RX return ring.
3675 error = bnx_dma_block_alloc(sc,
3676 BGE_RX_RTN_RING_SZ(sc->bnx_return_ring_cnt),
3677 &sc->bnx_cdata.bnx_rx_return_ring_tag,
3678 &sc->bnx_cdata.bnx_rx_return_ring_map,
3679 (void *)&sc->bnx_ldata.bnx_rx_return_ring,
3680 &sc->bnx_ldata.bnx_rx_return_ring_paddr);
3682 if_printf(ifp, "could not create RX ret ring\n");
3687 * Create DMA stuffs for TX ring.
3689 error = bnx_dma_block_alloc(sc, BGE_TX_RING_SZ,
3690 &sc->bnx_cdata.bnx_tx_ring_tag,
3691 &sc->bnx_cdata.bnx_tx_ring_map,
3692 (void *)&sc->bnx_ldata.bnx_tx_ring,
3693 &sc->bnx_ldata.bnx_tx_ring_paddr);
3695 if_printf(ifp, "could not create TX ring\n");
3700 * Create DMA stuffs for status block.
3702 error = bnx_dma_block_alloc(sc, BGE_STATUS_BLK_SZ,
3703 &sc->bnx_cdata.bnx_status_tag,
3704 &sc->bnx_cdata.bnx_status_map,
3705 (void *)&sc->bnx_ldata.bnx_status_block,
3706 &sc->bnx_ldata.bnx_status_block_paddr);
3708 if_printf(ifp, "could not create status block\n");
3716 bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
3717 bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
3722 error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
3723 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3724 size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3728 *tag = dmem.dmem_tag;
3729 *map = dmem.dmem_map;
3730 *addr = dmem.dmem_addr;
3731 *paddr = dmem.dmem_busaddr;
3737 bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
3740 bus_dmamap_unload(tag, map);
3741 bus_dmamem_free(tag, addr, map);
3742 bus_dma_tag_destroy(tag);
3747 bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
3749 struct ifnet *ifp = &sc->arpcom.ac_if;
3751 #define PCS_ENCODE_ERR (BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
3754 * Sometimes PCS encoding errors are detected in
3755 * TBI mode (on fiber NICs), and for some reason
3756 * the chip will signal them as link changes.
3757 * If we get a link change event, but the 'PCS
3758 * encoding error' bit in the MAC status register
3759 * is set, don't bother doing a link check.
3760 * This avoids spurious "gigabit link up" messages
3761 * that sometimes appear on fiber NICs during
3762 * periods of heavy traffic.
3764 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
3765 if (!sc->bnx_link) {
3767 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
3768 BNX_CLRBIT(sc, BGE_MAC_MODE,
3769 BGE_MACMODE_TBI_SEND_CFGS);
3771 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
3774 if_printf(ifp, "link UP\n");
3776 ifp->if_link_state = LINK_STATE_UP;
3777 if_link_state_change(ifp);
3779 } else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
3784 if_printf(ifp, "link DOWN\n");
3786 ifp->if_link_state = LINK_STATE_DOWN;
3787 if_link_state_change(ifp);
3791 #undef PCS_ENCODE_ERR
3793 /* Clear the attention. */
3794 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3795 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3796 BGE_MACSTAT_LINK_CHANGED);
3800 bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3802 struct ifnet *ifp = &sc->arpcom.ac_if;
3803 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3806 bnx_miibus_statchg(sc->bnx_dev);
3810 if_printf(ifp, "link UP\n");
3812 if_printf(ifp, "link DOWN\n");
3815 /* Clear the attention. */
3816 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3817 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3818 BGE_MACSTAT_LINK_CHANGED);
3822 bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3824 struct ifnet *ifp = &sc->arpcom.ac_if;
3825 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3829 if (!sc->bnx_link &&
3830 (mii->mii_media_status & IFM_ACTIVE) &&
3831 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
3834 if_printf(ifp, "link UP\n");
3835 } else if (sc->bnx_link &&
3836 (!(mii->mii_media_status & IFM_ACTIVE) ||
3837 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
3840 if_printf(ifp, "link DOWN\n");
3843 /* Clear the attention. */
3844 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3845 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3846 BGE_MACSTAT_LINK_CHANGED);
3850 bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
3852 struct bnx_softc *sc = arg1;
3854 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3855 &sc->bnx_rx_coal_ticks,
3856 BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
3857 BNX_RX_COAL_TICKS_CHG);
3861 bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
3863 struct bnx_softc *sc = arg1;
3865 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3866 &sc->bnx_tx_coal_ticks,
3867 BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
3868 BNX_TX_COAL_TICKS_CHG);
3872 bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
3874 struct bnx_softc *sc = arg1;
3876 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3877 &sc->bnx_rx_coal_bds,
3878 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3879 BNX_RX_COAL_BDS_CHG);
3883 bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
3885 struct bnx_softc *sc = arg1;
3887 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3888 &sc->bnx_tx_coal_bds,
3889 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3890 BNX_TX_COAL_BDS_CHG);
3894 bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3896 struct bnx_softc *sc = arg1;
3898 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3899 &sc->bnx_rx_coal_bds_int,
3900 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3901 BNX_RX_COAL_BDS_INT_CHG);
3905 bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3907 struct bnx_softc *sc = arg1;
3909 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3910 &sc->bnx_tx_coal_bds_int,
3911 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3912 BNX_TX_COAL_BDS_INT_CHG);
3916 bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
3917 int coal_min, int coal_max, uint32_t coal_chg_mask)
3919 struct bnx_softc *sc = arg1;
3920 struct ifnet *ifp = &sc->arpcom.ac_if;
3923 lwkt_serialize_enter(ifp->if_serializer);
3926 error = sysctl_handle_int(oidp, &v, 0, req);
3927 if (!error && req->newptr != NULL) {
3928 if (v < coal_min || v > coal_max) {
3932 sc->bnx_coal_chg |= coal_chg_mask;
3936 lwkt_serialize_exit(ifp->if_serializer);
3941 bnx_coal_change(struct bnx_softc *sc)
3943 struct ifnet *ifp = &sc->arpcom.ac_if;
3945 ASSERT_SERIALIZED(ifp->if_serializer);
3947 if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
3948 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
3949 sc->bnx_rx_coal_ticks);
3951 CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
3954 if_printf(ifp, "rx_coal_ticks -> %u\n",
3955 sc->bnx_rx_coal_ticks);
3959 if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
3960 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
3961 sc->bnx_tx_coal_ticks);
3963 CSR_READ_4(sc, BGE_HCC_TX_COAL_TICKS);
3966 if_printf(ifp, "tx_coal_ticks -> %u\n",
3967 sc->bnx_tx_coal_ticks);
3971 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
3972 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
3973 sc->bnx_rx_coal_bds);
3975 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
3978 if_printf(ifp, "rx_coal_bds -> %u\n",
3979 sc->bnx_rx_coal_bds);
3983 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
3984 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS,
3985 sc->bnx_tx_coal_bds);
3987 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS);
3990 if_printf(ifp, "tx_coal_bds -> %u\n",
3991 sc->bnx_tx_coal_bds);
3995 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
3996 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
3997 sc->bnx_rx_coal_bds_int);
3999 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT);
4002 if_printf(ifp, "rx_coal_bds_int -> %u\n",
4003 sc->bnx_rx_coal_bds_int);
4007 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
4008 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
4009 sc->bnx_tx_coal_bds_int);
4011 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT);
4014 if_printf(ifp, "tx_coal_bds_int -> %u\n",
4015 sc->bnx_tx_coal_bds_int);
4019 sc->bnx_coal_chg = 0;
4023 bnx_intr_check(void *xsc)
4025 struct bnx_softc *sc = xsc;
4026 struct ifnet *ifp = &sc->arpcom.ac_if;
4027 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
4029 lwkt_serialize_enter(ifp->if_serializer);
4031 KKASSERT(mycpuid == sc->bnx_intr_cpuid);
4033 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4034 lwkt_serialize_exit(ifp->if_serializer);
4038 if (sblk->bge_idx[0].bge_rx_prod_idx != sc->bnx_rx_saved_considx ||
4039 sblk->bge_idx[0].bge_tx_cons_idx != sc->bnx_tx_saved_considx) {
4040 if (sc->bnx_rx_check_considx == sc->bnx_rx_saved_considx &&
4041 sc->bnx_tx_check_considx == sc->bnx_tx_saved_considx) {
4042 if (!sc->bnx_intr_maylose) {
4043 sc->bnx_intr_maylose = TRUE;
4047 if_printf(ifp, "lost interrupt\n");
4051 sc->bnx_intr_maylose = FALSE;
4052 sc->bnx_rx_check_considx = sc->bnx_rx_saved_considx;
4053 sc->bnx_tx_check_considx = sc->bnx_tx_saved_considx;
4056 callout_reset(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4057 bnx_intr_check, sc);
4058 lwkt_serialize_exit(ifp->if_serializer);
4062 bnx_enable_intr(struct bnx_softc *sc)
4064 struct ifnet *ifp = &sc->arpcom.ac_if;
4066 lwkt_serialize_handler_enable(ifp->if_serializer);
4071 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
4072 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4073 /* XXX Linux driver */
4074 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
4078 * Unmask the interrupt when we stop polling.
4080 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4081 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4084 * Trigger another interrupt, since above writing
4085 * to interrupt mailbox0 may acknowledge pending
4088 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4090 if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4091 sc->bnx_intr_maylose = FALSE;
4092 sc->bnx_rx_check_considx = 0;
4093 sc->bnx_tx_check_considx = 0;
4096 if_printf(ifp, "status tag bug workaround\n");
4098 /* 10ms check interval */
4099 callout_reset_bycpu(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4100 bnx_intr_check, sc, sc->bnx_intr_cpuid);
4105 bnx_disable_intr(struct bnx_softc *sc)
4107 struct ifnet *ifp = &sc->arpcom.ac_if;
4110 * Mask the interrupt when we start polling.
4112 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4113 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4116 * Acknowledge possible asserted interrupt.
4118 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4120 callout_stop(&sc->bnx_intr_timer);
4121 sc->bnx_intr_maylose = FALSE;
4122 sc->bnx_rx_check_considx = 0;
4123 sc->bnx_tx_check_considx = 0;
4125 sc->bnx_npoll.ifpc_stcount = 0;
4127 lwkt_serialize_handler_disable(ifp->if_serializer);
4131 bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
4136 mac_addr = bnx_readmem_ind(sc, 0x0c14);
4137 if ((mac_addr >> 16) == 0x484b) {
4138 ether_addr[0] = (uint8_t)(mac_addr >> 8);
4139 ether_addr[1] = (uint8_t)mac_addr;
4140 mac_addr = bnx_readmem_ind(sc, 0x0c18);
4141 ether_addr[2] = (uint8_t)(mac_addr >> 24);
4142 ether_addr[3] = (uint8_t)(mac_addr >> 16);
4143 ether_addr[4] = (uint8_t)(mac_addr >> 8);
4144 ether_addr[5] = (uint8_t)mac_addr;
4151 bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
4153 int mac_offset = BGE_EE_MAC_OFFSET;
4155 if (BNX_IS_5717_PLUS(sc)) {
4158 f = pci_get_function(sc->bnx_dev);
4160 mac_offset = BGE_EE_MAC_OFFSET_5717;
4162 mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
4163 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
4164 mac_offset = BGE_EE_MAC_OFFSET_5906;
4167 return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
4171 bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
4173 if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
4176 return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4181 bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
4183 static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
4184 /* NOTE: Order is critical */
4186 bnx_get_eaddr_nvram,
4187 bnx_get_eaddr_eeprom,
4190 const bnx_eaddr_fcn_t *func;
4192 for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
4193 if ((*func)(sc, eaddr) == 0)
4196 return (*func == NULL ? ENXIO : 0);
4200 * NOTE: 'm' is not freed upon failure
4203 bnx_defrag_shortdma(struct mbuf *m)
4209 * If device receive two back-to-back send BDs with less than
4210 * or equal to 8 total bytes then the device may hang. The two
4211 * back-to-back send BDs must in the same frame for this failure
4212 * to occur. Scan mbuf chains and see whether two back-to-back
4213 * send BDs are there. If this is the case, allocate new mbuf
4214 * and copy the frame to workaround the silicon bug.
4216 for (n = m, found = 0; n != NULL; n = n->m_next) {
4227 n = m_defrag(m, MB_DONTWAIT);
4234 bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
4238 BNX_CLRBIT(sc, reg, bit);
4239 for (i = 0; i < BNX_TIMEOUT; i++) {
4240 if ((CSR_READ_4(sc, reg) & bit) == 0)
4247 bnx_link_poll(struct bnx_softc *sc)
4251 status = CSR_READ_4(sc, BGE_MAC_STS);
4252 if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
4253 sc->bnx_link_evt = 0;
4254 sc->bnx_link_upd(sc, status);
4259 bnx_enable_msi(struct bnx_softc *sc)
4263 msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
4264 msi_mode |= BGE_MSIMODE_ENABLE;
4265 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4268 * 5718-PG105-R says that "one shot" mode
4269 * does not work if MSI is used, however,
4270 * it obviously works.
4272 msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
4274 CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
4278 bnx_dma_swap_options(struct bnx_softc *sc)
4280 uint32_t dma_options;
4282 dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
4283 BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
4284 #if BYTE_ORDER == BIG_ENDIAN
4285 dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
4287 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
4288 dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
4289 BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
4290 BGE_MODECTL_HTX2B_ENABLE;
4296 bnx_setup_tso(struct bnx_softc *sc, struct mbuf **mp,
4297 uint16_t *mss0, uint16_t *flags0)
4302 int thoff, iphlen, hoff, hlen;
4303 uint16_t flags, mss;
4306 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4308 hoff = m->m_pkthdr.csum_lhlen;
4309 iphlen = m->m_pkthdr.csum_iphlen;
4310 thoff = m->m_pkthdr.csum_thlen;
4312 KASSERT(hoff > 0, ("invalid ether header len"));
4313 KASSERT(iphlen > 0, ("invalid ip header len"));
4314 KASSERT(thoff > 0, ("invalid tcp header len"));
4316 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4317 m = m_pullup(m, hoff + iphlen + thoff);
4324 ip = mtodoff(m, struct ip *, hoff);
4325 th = mtodoff(m, struct tcphdr *, hoff + iphlen);
4327 mss = m->m_pkthdr.tso_segsz;
4328 flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
4330 ip->ip_len = htons(mss + iphlen + thoff);
4333 hlen = (iphlen + thoff) >> 2;
4334 mss |= ((hlen & 0x3) << 14);
4335 flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);