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_BCM5725,
103 "Broadcom BCM5725 Gigabit Ethernet" },
104 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5727,
105 "Broadcom BCM5727 Gigabit Ethernet" },
106 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5762,
107 "Broadcom BCM5762 Gigabit Ethernet" },
109 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
110 "Broadcom BCM57761 Gigabit Ethernet" },
111 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
112 "Broadcom BCM57762 Gigabit Ethernet" },
113 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
114 "Broadcom BCM57765 Gigabit Ethernet" },
115 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
116 "Broadcom BCM57766 Gigabit Ethernet" },
117 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
118 "Broadcom BCM57781 Gigabit Ethernet" },
119 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
120 "Broadcom BCM57782 Gigabit Ethernet" },
121 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
122 "Broadcom BCM57785 Gigabit Ethernet" },
123 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
124 "Broadcom BCM57786 Gigabit Ethernet" },
125 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
126 "Broadcom BCM57791 Fast Ethernet" },
127 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
128 "Broadcom BCM57795 Fast Ethernet" },
133 #define BNX_IS_JUMBO_CAPABLE(sc) ((sc)->bnx_flags & BNX_FLAG_JUMBO)
134 #define BNX_IS_5717_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
135 #define BNX_IS_57765_PLUS(sc) ((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
136 #define BNX_IS_57765_FAMILY(sc) \
137 ((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
139 typedef int (*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
141 static int bnx_probe(device_t);
142 static int bnx_attach(device_t);
143 static int bnx_detach(device_t);
144 static void bnx_shutdown(device_t);
145 static int bnx_suspend(device_t);
146 static int bnx_resume(device_t);
147 static int bnx_miibus_readreg(device_t, int, int);
148 static int bnx_miibus_writereg(device_t, int, int, int);
149 static void bnx_miibus_statchg(device_t);
152 static void bnx_npoll(struct ifnet *, struct ifpoll_info *);
153 static void bnx_npoll_compat(struct ifnet *, void *, int);
155 static void bnx_intr_legacy(void *);
156 static void bnx_msi(void *);
157 static void bnx_msi_oneshot(void *);
158 static void bnx_intr(struct bnx_softc *);
159 static void bnx_enable_intr(struct bnx_softc *);
160 static void bnx_disable_intr(struct bnx_softc *);
161 static void bnx_txeof(struct bnx_softc *, uint16_t);
162 static void bnx_rxeof(struct bnx_softc *, uint16_t, int);
164 static void bnx_start(struct ifnet *, struct ifaltq_subque *);
165 static int bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
166 static void bnx_init(void *);
167 static void bnx_stop(struct bnx_softc *);
168 static void bnx_watchdog(struct ifnet *);
169 static int bnx_ifmedia_upd(struct ifnet *);
170 static void bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
171 static void bnx_tick(void *);
173 static int bnx_alloc_jumbo_mem(struct bnx_softc *);
174 static void bnx_free_jumbo_mem(struct bnx_softc *);
175 static struct bnx_jslot
176 *bnx_jalloc(struct bnx_softc *);
177 static void bnx_jfree(void *);
178 static void bnx_jref(void *);
179 static int bnx_newbuf_std(struct bnx_softc *, int, int);
180 static int bnx_newbuf_jumbo(struct bnx_softc *, int, int);
181 static void bnx_setup_rxdesc_std(struct bnx_softc *, int);
182 static void bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
183 static int bnx_init_rx_ring_std(struct bnx_softc *);
184 static void bnx_free_rx_ring_std(struct bnx_softc *);
185 static int bnx_init_rx_ring_jumbo(struct bnx_softc *);
186 static void bnx_free_rx_ring_jumbo(struct bnx_softc *);
187 static void bnx_free_tx_ring(struct bnx_softc *);
188 static int bnx_init_tx_ring(struct bnx_softc *);
189 static int bnx_dma_alloc(struct bnx_softc *);
190 static void bnx_dma_free(struct bnx_softc *);
191 static int bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
192 bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
193 static void bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
195 bnx_defrag_shortdma(struct mbuf *);
196 static int bnx_encap(struct bnx_softc *, struct mbuf **,
198 static int bnx_setup_tso(struct bnx_softc *, struct mbuf **,
199 uint16_t *, uint16_t *);
201 static void bnx_reset(struct bnx_softc *);
202 static int bnx_chipinit(struct bnx_softc *);
203 static int bnx_blockinit(struct bnx_softc *);
204 static void bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
205 static void bnx_enable_msi(struct bnx_softc *sc);
206 static void bnx_setmulti(struct bnx_softc *);
207 static void bnx_setpromisc(struct bnx_softc *);
208 static void bnx_stats_update_regs(struct bnx_softc *);
209 static uint32_t bnx_dma_swap_options(struct bnx_softc *);
211 static uint32_t bnx_readmem_ind(struct bnx_softc *, uint32_t);
212 static void bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
214 static uint32_t bnx_readreg_ind(struct bnx_softc *, uint32_t);
216 static void bnx_writereg_ind(struct bnx_softc *, uint32_t, uint32_t);
217 static void bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
218 static void bnx_writembx(struct bnx_softc *, int, int);
219 static uint8_t bnx_nvram_getbyte(struct bnx_softc *, int, uint8_t *);
220 static int bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
221 static uint8_t bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
222 static int bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
224 static void bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
225 static void bnx_copper_link_upd(struct bnx_softc *, uint32_t);
226 static void bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
227 static void bnx_link_poll(struct bnx_softc *);
229 static int bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
230 static int bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
231 static int bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
232 static int bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
234 static void bnx_coal_change(struct bnx_softc *);
235 static int bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
236 static int bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
237 static int bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
238 static int bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
239 static int bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
240 static int bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
241 static int bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
244 static int bnx_msi_enable = 1;
245 TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
247 static device_method_t bnx_methods[] = {
248 /* Device interface */
249 DEVMETHOD(device_probe, bnx_probe),
250 DEVMETHOD(device_attach, bnx_attach),
251 DEVMETHOD(device_detach, bnx_detach),
252 DEVMETHOD(device_shutdown, bnx_shutdown),
253 DEVMETHOD(device_suspend, bnx_suspend),
254 DEVMETHOD(device_resume, bnx_resume),
257 DEVMETHOD(bus_print_child, bus_generic_print_child),
258 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
261 DEVMETHOD(miibus_readreg, bnx_miibus_readreg),
262 DEVMETHOD(miibus_writereg, bnx_miibus_writereg),
263 DEVMETHOD(miibus_statchg, bnx_miibus_statchg),
268 static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
269 static devclass_t bnx_devclass;
271 DECLARE_DUMMY_MODULE(if_bnx);
272 DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
273 DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
276 bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
278 device_t dev = sc->bnx_dev;
281 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
282 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
285 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
286 val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
287 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
292 bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
294 device_t dev = sc->bnx_dev;
296 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
297 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
300 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
301 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
302 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
307 bnx_readreg_ind(struct bnx_softc *sc, uin32_t off)
309 device_t dev = sc->bnx_dev;
311 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
312 return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
317 bnx_writereg_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
319 device_t dev = sc->bnx_dev;
321 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
322 pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
326 bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
328 CSR_WRITE_4(sc, off, val);
332 bnx_writembx(struct bnx_softc *sc, int off, int val)
334 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906)
335 off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
337 CSR_WRITE_4(sc, off, val);
341 bnx_nvram_getbyte(struct bnx_softc *sc, int addr, uint8_t *dest)
343 uint32_t access, byte = 0;
347 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
348 for (i = 0; i < 8000; i++) {
349 if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
357 access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
358 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
360 CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
361 CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
362 for (i = 0; i < BNX_TIMEOUT * 10; i++) {
364 if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
370 if (i == BNX_TIMEOUT * 10) {
371 if_printf(&sc->arpcom.ac_if, "nvram read timed out\n");
376 byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
378 *dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
380 /* Disable access. */
381 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
384 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
385 CSR_READ_4(sc, BGE_NVRAM_SWARB);
391 * Read a sequence of bytes from NVRAM.
394 bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
399 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
402 for (i = 0; i < cnt; i++) {
403 err = bnx_nvram_getbyte(sc, off + i, &byte);
409 return (err ? 1 : 0);
413 * Read a byte of data stored in the EEPROM at address 'addr.' The
414 * BCM570x supports both the traditional bitbang interface and an
415 * auto access interface for reading the EEPROM. We use the auto
419 bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
425 * Enable use of auto EEPROM access so we can avoid
426 * having to use the bitbang method.
428 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
430 /* Reset the EEPROM, load the clock period. */
431 CSR_WRITE_4(sc, BGE_EE_ADDR,
432 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
435 /* Issue the read EEPROM command. */
436 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
438 /* Wait for completion */
439 for(i = 0; i < BNX_TIMEOUT * 10; i++) {
441 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
445 if (i == BNX_TIMEOUT) {
446 if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
451 byte = CSR_READ_4(sc, BGE_EE_DATA);
453 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
459 * Read a sequence of bytes from the EEPROM.
462 bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
468 for (byte = 0, err = 0, i = 0; i < len; i++) {
469 err = bnx_eeprom_getbyte(sc, off + i, &byte);
479 bnx_miibus_readreg(device_t dev, int phy, int reg)
481 struct bnx_softc *sc = device_get_softc(dev);
485 KASSERT(phy == sc->bnx_phyno,
486 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
488 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
489 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
490 CSR_WRITE_4(sc, BGE_MI_MODE,
491 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
495 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
496 BGE_MIPHY(phy) | BGE_MIREG(reg));
498 /* Poll for the PHY register access to complete. */
499 for (i = 0; i < BNX_TIMEOUT; i++) {
501 val = CSR_READ_4(sc, BGE_MI_COMM);
502 if ((val & BGE_MICOMM_BUSY) == 0) {
504 val = CSR_READ_4(sc, BGE_MI_COMM);
508 if (i == BNX_TIMEOUT) {
509 if_printf(&sc->arpcom.ac_if, "PHY read timed out "
510 "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
514 /* Restore the autopoll bit if necessary. */
515 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
516 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
520 if (val & BGE_MICOMM_READFAIL)
523 return (val & 0xFFFF);
527 bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
529 struct bnx_softc *sc = device_get_softc(dev);
532 KASSERT(phy == sc->bnx_phyno,
533 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
535 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
536 (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
539 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
540 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
541 CSR_WRITE_4(sc, BGE_MI_MODE,
542 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
546 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
547 BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
549 for (i = 0; i < BNX_TIMEOUT; i++) {
551 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
553 CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
557 if (i == BNX_TIMEOUT) {
558 if_printf(&sc->arpcom.ac_if, "PHY write timed out "
559 "(phy %d, reg %d, val %d)\n", phy, reg, val);
562 /* Restore the autopoll bit if necessary. */
563 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
564 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
572 bnx_miibus_statchg(device_t dev)
574 struct bnx_softc *sc;
575 struct mii_data *mii;
577 sc = device_get_softc(dev);
578 mii = device_get_softc(sc->bnx_miibus);
580 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
581 (IFM_ACTIVE | IFM_AVALID)) {
582 switch (IFM_SUBTYPE(mii->mii_media_active)) {
590 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
602 if (sc->bnx_link == 0)
605 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
606 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
607 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
608 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
610 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
613 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
614 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
616 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
621 * Memory management for jumbo frames.
624 bnx_alloc_jumbo_mem(struct bnx_softc *sc)
626 struct ifnet *ifp = &sc->arpcom.ac_if;
627 struct bnx_jslot *entry;
633 * Create tag for jumbo mbufs.
634 * This is really a bit of a kludge. We allocate a special
635 * jumbo buffer pool which (thanks to the way our DMA
636 * memory allocation works) will consist of contiguous
637 * pages. This means that even though a jumbo buffer might
638 * be larger than a page size, we don't really need to
639 * map it into more than one DMA segment. However, the
640 * default mbuf tag will result in multi-segment mappings,
641 * so we have to create a special jumbo mbuf tag that
642 * lets us get away with mapping the jumbo buffers as
643 * a single segment. I think eventually the driver should
644 * be changed so that it uses ordinary mbufs and cluster
645 * buffers, i.e. jumbo frames can span multiple DMA
646 * descriptors. But that's a project for another day.
650 * Create DMA stuffs for jumbo RX ring.
652 error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
653 &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
654 &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
655 (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
656 &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
658 if_printf(ifp, "could not create jumbo RX ring\n");
663 * Create DMA stuffs for jumbo buffer block.
665 error = bnx_dma_block_alloc(sc, BNX_JMEM,
666 &sc->bnx_cdata.bnx_jumbo_tag,
667 &sc->bnx_cdata.bnx_jumbo_map,
668 (void **)&sc->bnx_ldata.bnx_jumbo_buf,
671 if_printf(ifp, "could not create jumbo buffer\n");
675 SLIST_INIT(&sc->bnx_jfree_listhead);
678 * Now divide it up into 9K pieces and save the addresses
679 * in an array. Note that we play an evil trick here by using
680 * the first few bytes in the buffer to hold the the address
681 * of the softc structure for this interface. This is because
682 * bnx_jfree() needs it, but it is called by the mbuf management
683 * code which will not pass it to us explicitly.
685 for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
686 entry = &sc->bnx_cdata.bnx_jslots[i];
688 entry->bnx_buf = ptr;
689 entry->bnx_paddr = paddr;
690 entry->bnx_inuse = 0;
692 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
701 bnx_free_jumbo_mem(struct bnx_softc *sc)
703 /* Destroy jumbo RX ring. */
704 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
705 sc->bnx_cdata.bnx_rx_jumbo_ring_map,
706 sc->bnx_ldata.bnx_rx_jumbo_ring);
708 /* Destroy jumbo buffer block. */
709 bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
710 sc->bnx_cdata.bnx_jumbo_map,
711 sc->bnx_ldata.bnx_jumbo_buf);
715 * Allocate a jumbo buffer.
717 static struct bnx_jslot *
718 bnx_jalloc(struct bnx_softc *sc)
720 struct bnx_jslot *entry;
722 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
723 entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
725 SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
726 entry->bnx_inuse = 1;
728 if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
730 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
735 * Adjust usage count on a jumbo buffer.
740 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
741 struct bnx_softc *sc = entry->bnx_sc;
744 panic("bnx_jref: can't find softc pointer!");
746 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
747 panic("bnx_jref: asked to reference buffer "
748 "that we don't manage!");
749 } else if (entry->bnx_inuse == 0) {
750 panic("bnx_jref: buffer already free!");
752 atomic_add_int(&entry->bnx_inuse, 1);
757 * Release a jumbo buffer.
762 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
763 struct bnx_softc *sc = entry->bnx_sc;
766 panic("bnx_jfree: can't find softc pointer!");
768 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
769 panic("bnx_jfree: asked to free buffer that we don't manage!");
770 } else if (entry->bnx_inuse == 0) {
771 panic("bnx_jfree: buffer already free!");
774 * Possible MP race to 0, use the serializer. The atomic insn
775 * is still needed for races against bnx_jref().
777 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
778 atomic_subtract_int(&entry->bnx_inuse, 1);
779 if (entry->bnx_inuse == 0) {
780 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
783 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
789 * Intialize a standard receive ring descriptor.
792 bnx_newbuf_std(struct bnx_softc *sc, int i, int init)
794 struct mbuf *m_new = NULL;
795 bus_dma_segment_t seg;
799 m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
802 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
803 m_adj(m_new, ETHER_ALIGN);
805 error = bus_dmamap_load_mbuf_segment(sc->bnx_cdata.bnx_rx_mtag,
806 sc->bnx_cdata.bnx_rx_tmpmap, m_new,
807 &seg, 1, &nsegs, BUS_DMA_NOWAIT);
814 bus_dmamap_sync(sc->bnx_cdata.bnx_rx_mtag,
815 sc->bnx_cdata.bnx_rx_std_dmamap[i],
816 BUS_DMASYNC_POSTREAD);
817 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
818 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
821 map = sc->bnx_cdata.bnx_rx_tmpmap;
822 sc->bnx_cdata.bnx_rx_tmpmap = sc->bnx_cdata.bnx_rx_std_dmamap[i];
823 sc->bnx_cdata.bnx_rx_std_dmamap[i] = map;
825 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_mbuf = m_new;
826 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_paddr = seg.ds_addr;
828 bnx_setup_rxdesc_std(sc, i);
833 bnx_setup_rxdesc_std(struct bnx_softc *sc, int i)
835 struct bnx_rxchain *rc;
838 rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
839 r = &sc->bnx_ldata.bnx_rx_std_ring[i];
841 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
842 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
843 r->bge_len = rc->bnx_mbuf->m_len;
845 r->bge_flags = BGE_RXBDFLAG_END;
849 * Initialize a jumbo receive ring descriptor. This allocates
850 * a jumbo buffer from the pool managed internally by the driver.
853 bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
855 struct mbuf *m_new = NULL;
856 struct bnx_jslot *buf;
859 /* Allocate the mbuf. */
860 MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
864 /* Allocate the jumbo buffer */
865 buf = bnx_jalloc(sc);
871 /* Attach the buffer to the mbuf. */
872 m_new->m_ext.ext_arg = buf;
873 m_new->m_ext.ext_buf = buf->bnx_buf;
874 m_new->m_ext.ext_free = bnx_jfree;
875 m_new->m_ext.ext_ref = bnx_jref;
876 m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
878 m_new->m_flags |= M_EXT;
880 m_new->m_data = m_new->m_ext.ext_buf;
881 m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
883 paddr = buf->bnx_paddr;
884 m_adj(m_new, ETHER_ALIGN);
885 paddr += ETHER_ALIGN;
887 /* Save necessary information */
888 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_mbuf = m_new;
889 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_paddr = paddr;
891 /* Set up the descriptor. */
892 bnx_setup_rxdesc_jumbo(sc, i);
897 bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
900 struct bnx_rxchain *rc;
902 r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
903 rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
905 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
906 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
907 r->bge_len = rc->bnx_mbuf->m_len;
909 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
913 bnx_init_rx_ring_std(struct bnx_softc *sc)
917 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
918 error = bnx_newbuf_std(sc, i, 1);
923 sc->bnx_std = BGE_STD_RX_RING_CNT - 1;
924 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
930 bnx_free_rx_ring_std(struct bnx_softc *sc)
934 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
935 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
937 if (rc->bnx_mbuf != NULL) {
938 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
939 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
940 m_freem(rc->bnx_mbuf);
943 bzero(&sc->bnx_ldata.bnx_rx_std_ring[i],
944 sizeof(struct bge_rx_bd));
949 bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
954 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
955 error = bnx_newbuf_jumbo(sc, i, 1);
960 sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
962 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
963 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
964 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
966 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
972 bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
976 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
977 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
979 if (rc->bnx_mbuf != NULL) {
980 m_freem(rc->bnx_mbuf);
983 bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
984 sizeof(struct bge_rx_bd));
989 bnx_free_tx_ring(struct bnx_softc *sc)
993 for (i = 0; i < BGE_TX_RING_CNT; i++) {
994 if (sc->bnx_cdata.bnx_tx_chain[i] != NULL) {
995 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
996 sc->bnx_cdata.bnx_tx_dmamap[i]);
997 m_freem(sc->bnx_cdata.bnx_tx_chain[i]);
998 sc->bnx_cdata.bnx_tx_chain[i] = NULL;
1000 bzero(&sc->bnx_ldata.bnx_tx_ring[i],
1001 sizeof(struct bge_tx_bd));
1006 bnx_init_tx_ring(struct bnx_softc *sc)
1009 sc->bnx_tx_saved_considx = 0;
1010 sc->bnx_tx_prodidx = 0;
1012 /* Initialize transmit producer index for host-memory send ring. */
1013 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bnx_tx_prodidx);
1014 bnx_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1020 bnx_setmulti(struct bnx_softc *sc)
1023 struct ifmultiaddr *ifma;
1024 uint32_t hashes[4] = { 0, 0, 0, 0 };
1027 ifp = &sc->arpcom.ac_if;
1029 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1030 for (i = 0; i < 4; i++)
1031 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1035 /* First, zot all the existing filters. */
1036 for (i = 0; i < 4; i++)
1037 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1039 /* Now program new ones. */
1040 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1041 if (ifma->ifma_addr->sa_family != AF_LINK)
1044 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1045 ETHER_ADDR_LEN) & 0x7f;
1046 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1049 for (i = 0; i < 4; i++)
1050 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1054 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1055 * self-test results.
1058 bnx_chipinit(struct bnx_softc *sc)
1060 uint32_t dma_rw_ctl, mode_ctl;
1063 /* Set endian type before we access any non-PCI registers. */
1064 pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
1065 BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
1067 /* Clear the MAC control register */
1068 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1071 * Clear the MAC statistics block in the NIC's
1074 for (i = BGE_STATS_BLOCK;
1075 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1076 BNX_MEMWIN_WRITE(sc, i, 0);
1078 for (i = BGE_STATUS_BLOCK;
1079 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1080 BNX_MEMWIN_WRITE(sc, i, 0);
1082 if (BNX_IS_57765_FAMILY(sc)) {
1085 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1086 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1087 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1089 /* Access the lower 1K of PL PCI-E block registers. */
1090 CSR_WRITE_4(sc, BGE_MODE_CTL,
1091 val | BGE_MODECTL_PCIE_PL_SEL);
1093 val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1094 val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1095 CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1097 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1099 if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
1100 /* Fix transmit hangs */
1101 val = CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL);
1102 val |= BGE_CPMU_PADRNG_CTL_RDIV2;
1103 CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL, val);
1105 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1106 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1108 /* Access the lower 1K of DL PCI-E block registers. */
1109 CSR_WRITE_4(sc, BGE_MODE_CTL,
1110 val | BGE_MODECTL_PCIE_DL_SEL);
1112 val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1113 val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1114 val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1115 CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1117 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1120 val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1121 val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1122 val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1123 CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1127 * Set up the PCI DMA control register.
1129 dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
1131 * Disable 32bytes cache alignment for DMA write to host memory
1134 * 64bytes cache alignment for DMA write to host memory is still
1137 dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1138 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
1139 dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1141 * Enable HW workaround for controllers that misinterpret
1142 * a status tag update and leave interrupts permanently
1145 if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1146 sc->bnx_asicrev != BGE_ASICREV_BCM5762 &&
1147 !BNX_IS_57765_FAMILY(sc))
1148 dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1150 if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
1153 pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1156 * Set up general mode register.
1158 mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
1159 BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
1160 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1163 * Disable memory write invalidate. Apparently it is not supported
1164 * properly by these devices. Also ensure that INTx isn't disabled,
1165 * as these chips need it even when using MSI.
1167 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
1168 (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
1170 /* Set the timer prescaler (always 66Mhz) */
1171 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1173 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
1174 DELAY(40); /* XXX */
1176 /* Put PHY into ready state */
1177 BNX_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1178 CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1186 bnx_blockinit(struct bnx_softc *sc)
1188 struct bge_rcb *rcb;
1195 * Initialize the memory window pointer register so that
1196 * we can access the first 32K of internal NIC RAM. This will
1197 * allow us to set up the TX send ring RCBs and the RX return
1198 * ring RCBs, plus other things which live in NIC memory.
1200 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1202 /* Configure mbuf pool watermarks */
1203 if (BNX_IS_57765_PLUS(sc)) {
1204 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1205 if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
1206 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
1207 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
1209 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
1210 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
1212 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
1213 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1214 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
1215 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
1217 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1218 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1219 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1222 /* Configure DMA resource watermarks */
1223 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1224 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1226 /* Enable buffer manager */
1227 val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1229 * Change the arbitration algorithm of TXMBUF read request to
1230 * round-robin instead of priority based for BCM5719. When
1231 * TXFIFO is almost empty, RDMA will hold its request until
1232 * TXFIFO is not almost empty.
1234 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
1235 val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1236 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1237 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1238 sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1239 val |= BGE_BMANMODE_LOMBUF_ATTN;
1240 CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
1242 /* Poll for buffer manager start indication */
1243 for (i = 0; i < BNX_TIMEOUT; i++) {
1244 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1249 if (i == BNX_TIMEOUT) {
1250 if_printf(&sc->arpcom.ac_if,
1251 "buffer manager failed to start\n");
1255 /* Enable flow-through queues */
1256 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1257 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1259 /* Wait until queue initialization is complete */
1260 for (i = 0; i < BNX_TIMEOUT; i++) {
1261 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1266 if (i == BNX_TIMEOUT) {
1267 if_printf(&sc->arpcom.ac_if,
1268 "flow-through queue init failed\n");
1273 * Summary of rings supported by the controller:
1275 * Standard Receive Producer Ring
1276 * - This ring is used to feed receive buffers for "standard"
1277 * sized frames (typically 1536 bytes) to the controller.
1279 * Jumbo Receive Producer Ring
1280 * - This ring is used to feed receive buffers for jumbo sized
1281 * frames (i.e. anything bigger than the "standard" frames)
1282 * to the controller.
1284 * Mini Receive Producer Ring
1285 * - This ring is used to feed receive buffers for "mini"
1286 * sized frames to the controller.
1287 * - This feature required external memory for the controller
1288 * but was never used in a production system. Should always
1291 * Receive Return Ring
1292 * - After the controller has placed an incoming frame into a
1293 * receive buffer that buffer is moved into a receive return
1294 * ring. The driver is then responsible to passing the
1295 * buffer up to the stack. Many versions of the controller
1296 * support multiple RR rings.
1299 * - This ring is used for outgoing frames. Many versions of
1300 * the controller support multiple send rings.
1303 /* Initialize the standard receive producer ring control block. */
1304 rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
1305 rcb->bge_hostaddr.bge_addr_lo =
1306 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1307 rcb->bge_hostaddr.bge_addr_hi =
1308 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1309 if (BNX_IS_57765_PLUS(sc)) {
1311 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
1312 * Bits 15-2 : Maximum RX frame size
1313 * Bit 1 : 1 = Ring Disabled, 0 = Ring ENabled
1316 rcb->bge_maxlen_flags =
1317 BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
1320 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
1321 * Bits 15-2 : Reserved (should be 0)
1322 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
1325 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1327 if (BNX_IS_5717_PLUS(sc))
1328 rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
1330 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1331 /* Write the standard receive producer ring control block. */
1332 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1333 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1334 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1335 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1336 /* Reset the standard receive producer ring producer index. */
1337 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1340 * Initialize the jumbo RX producer ring control
1341 * block. We set the 'ring disabled' bit in the
1342 * flags field until we're actually ready to start
1343 * using this ring (i.e. once we set the MTU
1344 * high enough to require it).
1346 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1347 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
1348 /* Get the jumbo receive producer ring RCB parameters. */
1349 rcb->bge_hostaddr.bge_addr_lo =
1350 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1351 rcb->bge_hostaddr.bge_addr_hi =
1352 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1353 rcb->bge_maxlen_flags =
1354 BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
1355 BGE_RCB_FLAG_RING_DISABLED);
1356 if (BNX_IS_5717_PLUS(sc))
1357 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
1359 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1360 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1361 rcb->bge_hostaddr.bge_addr_hi);
1362 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1363 rcb->bge_hostaddr.bge_addr_lo);
1364 /* Program the jumbo receive producer ring RCB parameters. */
1365 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1366 rcb->bge_maxlen_flags);
1367 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1368 /* Reset the jumbo receive producer ring producer index. */
1369 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1372 /* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1373 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
1374 (sc->bnx_chipid == BGE_CHIPID_BCM5906_A0 ||
1375 sc->bnx_chipid == BGE_CHIPID_BCM5906_A1 ||
1376 sc->bnx_chipid == BGE_CHIPID_BCM5906_A2)) {
1377 CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
1378 (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1382 * The BD ring replenish thresholds control how often the
1383 * hardware fetches new BD's from the producer rings in host
1384 * memory. Setting the value too low on a busy system can
1385 * starve the hardware and recue the throughpout.
1387 * Set the BD ring replentish thresholds. The recommended
1388 * values are 1/8th the number of descriptors allocated to
1392 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
1393 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1394 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
1395 BGE_JUMBO_RX_RING_CNT/8);
1397 if (BNX_IS_57765_PLUS(sc)) {
1398 CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
1399 CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
1403 * Disable all send rings by setting the 'ring disabled' bit
1404 * in the flags field of all the TX send ring control blocks,
1405 * located in NIC memory.
1407 if (BNX_IS_5717_PLUS(sc))
1409 else if (BNX_IS_57765_FAMILY(sc) ||
1410 sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1414 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1415 for (i = 0; i < limit; i++) {
1416 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1417 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1418 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1419 vrcb += sizeof(struct bge_rcb);
1422 /* Configure send ring RCB 0 (we use only the first ring) */
1423 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1424 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_tx_ring_paddr);
1425 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1426 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1427 if (BNX_IS_5717_PLUS(sc)) {
1428 RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
1430 RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1431 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1433 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1434 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1437 * Disable all receive return rings by setting the
1438 * 'ring disabled' bit in the flags field of all the receive
1439 * return ring control blocks, located in NIC memory.
1441 if (BNX_IS_5717_PLUS(sc)) {
1442 /* Should be 17, use 16 until we get an SRAM map. */
1444 } else if (BNX_IS_57765_FAMILY(sc) ||
1445 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1450 /* Disable all receive return rings. */
1451 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1452 for (i = 0; i < limit; i++) {
1453 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1454 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1455 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1456 BGE_RCB_FLAG_RING_DISABLED);
1457 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1458 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
1459 (i * (sizeof(uint64_t))), 0);
1460 vrcb += sizeof(struct bge_rcb);
1464 * Set up receive return ring 0. Note that the NIC address
1465 * for RX return rings is 0x0. The return rings live entirely
1466 * within the host, so the nicaddr field in the RCB isn't used.
1468 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1469 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_rx_return_ring_paddr);
1470 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1471 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1472 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1473 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1474 BGE_RCB_MAXLEN_FLAGS(sc->bnx_return_ring_cnt, 0));
1476 /* Set random backoff seed for TX */
1477 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1478 sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
1479 sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
1480 sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
1481 BGE_TX_BACKOFF_SEED_MASK);
1483 /* Set inter-packet gap */
1485 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1486 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1487 val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
1488 (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
1490 CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
1493 * Specify which ring to use for packets that don't match
1496 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1499 * Configure number of RX lists. One interrupt distribution
1500 * list, sixteen active lists, one bad frames class.
1502 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1504 /* Inialize RX list placement stats mask. */
1505 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1506 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1508 /* Disable host coalescing until we get it set up */
1509 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1511 /* Poll to make sure it's shut down. */
1512 for (i = 0; i < BNX_TIMEOUT; i++) {
1513 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1518 if (i == BNX_TIMEOUT) {
1519 if_printf(&sc->arpcom.ac_if,
1520 "host coalescing engine failed to idle\n");
1524 /* Set up host coalescing defaults */
1525 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bnx_rx_coal_ticks);
1526 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bnx_tx_coal_ticks);
1527 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bnx_rx_coal_bds);
1528 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bnx_tx_coal_bds);
1529 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, sc->bnx_rx_coal_bds_int);
1530 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, sc->bnx_tx_coal_bds_int);
1532 /* Set up address of status block */
1533 bzero(sc->bnx_ldata.bnx_status_block, BGE_STATUS_BLK_SZ);
1534 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1535 BGE_ADDR_HI(sc->bnx_ldata.bnx_status_block_paddr));
1536 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1537 BGE_ADDR_LO(sc->bnx_ldata.bnx_status_block_paddr));
1539 /* Set up status block partail update size. */
1540 val = BGE_STATBLKSZ_32BYTE;
1543 * Does not seem to have visible effect in both
1544 * bulk data (1472B UDP datagram) and tiny data
1545 * (18B UDP datagram) TX tests.
1547 val |= BGE_HCCMODE_CLRTICK_TX;
1549 /* Turn on host coalescing state machine */
1550 CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
1552 /* Turn on RX BD completion state machine and enable attentions */
1553 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1554 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1556 /* Turn on RX list placement state machine */
1557 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1559 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1560 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1561 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1562 BGE_MACMODE_FRMHDR_DMA_ENB;
1564 if (sc->bnx_flags & BNX_FLAG_TBI)
1565 val |= BGE_PORTMODE_TBI;
1566 else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
1567 val |= BGE_PORTMODE_GMII;
1569 val |= BGE_PORTMODE_MII;
1571 /* Turn on DMA, clear stats */
1572 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
1574 /* Set misc. local control, enable interrupts on attentions */
1575 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1578 /* Assert GPIO pins for PHY reset */
1579 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1580 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1581 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1582 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1585 /* Turn on write DMA state machine */
1586 val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
1587 /* Enable host coalescing bug fix. */
1588 val |= BGE_WDMAMODE_STATUS_TAG_FIX;
1589 if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
1590 /* Request larger DMA burst size to get better performance. */
1591 val |= BGE_WDMAMODE_BURST_ALL_DATA;
1593 CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
1596 if (BNX_IS_57765_PLUS(sc)) {
1597 uint32_t dmactl, dmactl_reg;
1599 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1600 dmactl_reg = BGE_RDMA_RSRVCTRL2;
1602 dmactl_reg = BGE_RDMA_RSRVCTRL;
1604 dmactl = CSR_READ_4(sc, dmactl_reg);
1606 * Adjust tx margin to prevent TX data corruption and
1607 * fix internal FIFO overflow.
1609 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1610 sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1611 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1612 dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
1613 BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
1614 BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
1615 dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
1616 BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
1617 BGE_RDMA_RSRVCTRL_TXMRGN_320B;
1620 * Enable fix for read DMA FIFO overruns.
1621 * The fix is to limit the number of RX BDs
1622 * the hardware would fetch at a fime.
1624 CSR_WRITE_4(sc, dmactl_reg,
1625 dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1628 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
1629 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
1630 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
1631 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
1632 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1633 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1634 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1637 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1638 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL2;
1640 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL;
1643 * Allow 4KB burst length reads for non-LSO frames.
1644 * Enable 512B burst length reads for buffer descriptors.
1646 CSR_WRITE_4(sc, ctrl_reg,
1647 CSR_READ_4(sc, ctrl_reg) |
1648 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
1649 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1652 /* Turn on read DMA state machine */
1653 val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1654 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
1655 val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
1656 if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
1657 sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
1658 sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
1659 val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1660 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1661 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
1663 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1664 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1665 val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
1666 BGE_RDMAMODE_H2BNC_VLAN_DET;
1668 * Allow multiple outstanding read requests from
1669 * non-LSO read DMA engine.
1671 val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
1673 if (sc->bnx_flags & BNX_FLAG_TSO)
1674 val |= BGE_RDMAMODE_TSO4_ENABLE;
1675 val |= BGE_RDMAMODE_FIFO_LONG_BURST;
1676 CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
1679 /* Turn on RX data completion state machine */
1680 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1682 /* Turn on RX BD initiator state machine */
1683 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1685 /* Turn on RX data and RX BD initiator state machine */
1686 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1688 /* Turn on send BD completion state machine */
1689 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1691 /* Turn on send data completion state machine */
1692 val = BGE_SDCMODE_ENABLE;
1693 if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
1694 val |= BGE_SDCMODE_CDELAY;
1695 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
1697 /* Turn on send data initiator state machine */
1698 if (sc->bnx_flags & BNX_FLAG_TSO) {
1699 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
1700 BGE_SDIMODE_HW_LSO_PRE_DMA);
1702 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1705 /* Turn on send BD initiator state machine */
1706 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1708 /* Turn on send BD selector state machine */
1709 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1711 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1712 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1713 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1715 /* ack/clear link change events */
1716 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1717 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1718 BGE_MACSTAT_LINK_CHANGED);
1719 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1722 * Enable attention when the link has changed state for
1723 * devices that use auto polling.
1725 if (sc->bnx_flags & BNX_FLAG_TBI) {
1726 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1728 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
1729 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
1735 * Clear any pending link state attention.
1736 * Otherwise some link state change events may be lost until attention
1737 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
1738 * It's not necessary on newer BCM chips - perhaps enabling link
1739 * state change attentions implies clearing pending attention.
1741 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1742 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1743 BGE_MACSTAT_LINK_CHANGED);
1745 /* Enable link state change attentions. */
1746 BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1752 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1753 * against our list and return its name if we find a match. Note
1754 * that since the Broadcom controller contains VPD support, we
1755 * can get the device name string from the controller itself instead
1756 * of the compiled-in string. This is a little slow, but it guarantees
1757 * we'll always announce the right product name.
1760 bnx_probe(device_t dev)
1762 const struct bnx_type *t;
1763 uint16_t product, vendor;
1765 if (!pci_is_pcie(dev))
1768 product = pci_get_device(dev);
1769 vendor = pci_get_vendor(dev);
1771 for (t = bnx_devs; t->bnx_name != NULL; t++) {
1772 if (vendor == t->bnx_vid && product == t->bnx_did)
1775 if (t->bnx_name == NULL)
1778 device_set_desc(dev, t->bnx_name);
1783 bnx_attach(device_t dev)
1786 struct bnx_softc *sc;
1788 int error = 0, rid, capmask;
1789 uint8_t ether_addr[ETHER_ADDR_LEN];
1791 driver_intr_t *intr_func;
1792 uintptr_t mii_priv = 0;
1794 #ifdef BNX_TSO_DEBUG
1799 sc = device_get_softc(dev);
1801 callout_init_mp(&sc->bnx_stat_timer);
1802 callout_init_mp(&sc->bnx_intr_timer);
1803 lwkt_serialize_init(&sc->bnx_jslot_serializer);
1805 product = pci_get_device(dev);
1807 #ifndef BURN_BRIDGES
1808 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1811 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1812 mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
1814 device_printf(dev, "chip is in D%d power mode "
1815 "-- setting to D0\n", pci_get_powerstate(dev));
1817 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1819 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1820 pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
1822 #endif /* !BURN_BRIDGE */
1825 * Map control/status registers.
1827 pci_enable_busmaster(dev);
1830 sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1833 if (sc->bnx_res == NULL) {
1834 device_printf(dev, "couldn't map memory\n");
1838 sc->bnx_btag = rman_get_bustag(sc->bnx_res);
1839 sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
1841 /* Save various chip information */
1843 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
1844 BGE_PCIMISCCTL_ASICREV_SHIFT;
1845 if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
1846 /* All chips having dedicated ASICREV register have CPMU */
1847 sc->bnx_flags |= BNX_FLAG_CPMU;
1850 case PCI_PRODUCT_BROADCOM_BCM5717:
1851 case PCI_PRODUCT_BROADCOM_BCM5717C:
1852 case PCI_PRODUCT_BROADCOM_BCM5718:
1853 case PCI_PRODUCT_BROADCOM_BCM5719:
1854 case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1855 case PCI_PRODUCT_BROADCOM_BCM5725:
1856 case PCI_PRODUCT_BROADCOM_BCM5727:
1857 case PCI_PRODUCT_BROADCOM_BCM5762:
1858 sc->bnx_chipid = pci_read_config(dev,
1859 BGE_PCI_GEN2_PRODID_ASICREV, 4);
1862 case PCI_PRODUCT_BROADCOM_BCM57761:
1863 case PCI_PRODUCT_BROADCOM_BCM57762:
1864 case PCI_PRODUCT_BROADCOM_BCM57765:
1865 case PCI_PRODUCT_BROADCOM_BCM57766:
1866 case PCI_PRODUCT_BROADCOM_BCM57781:
1867 case PCI_PRODUCT_BROADCOM_BCM57782:
1868 case PCI_PRODUCT_BROADCOM_BCM57785:
1869 case PCI_PRODUCT_BROADCOM_BCM57786:
1870 case PCI_PRODUCT_BROADCOM_BCM57791:
1871 case PCI_PRODUCT_BROADCOM_BCM57795:
1872 sc->bnx_chipid = pci_read_config(dev,
1873 BGE_PCI_GEN15_PRODID_ASICREV, 4);
1877 sc->bnx_chipid = pci_read_config(dev,
1878 BGE_PCI_PRODID_ASICREV, 4);
1882 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1883 sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1885 sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
1886 sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
1888 switch (sc->bnx_asicrev) {
1889 case BGE_ASICREV_BCM5717:
1890 case BGE_ASICREV_BCM5719:
1891 case BGE_ASICREV_BCM5720:
1892 sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1895 case BGE_ASICREV_BCM5762:
1896 sc->bnx_flags |= BNX_FLAG_57765_PLUS;
1899 case BGE_ASICREV_BCM57765:
1900 case BGE_ASICREV_BCM57766:
1901 sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
1904 sc->bnx_flags |= BNX_FLAG_SHORTDMA;
1906 sc->bnx_flags |= BNX_FLAG_TSO;
1907 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
1908 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
1909 sc->bnx_flags &= ~BNX_FLAG_TSO;
1911 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1912 BNX_IS_57765_FAMILY(sc)) {
1914 * All BCM57785 and BCM5718 families chips have a bug that
1915 * under certain situation interrupt will not be enabled
1916 * even if status tag is written to BGE_MBX_IRQ0_LO mailbox.
1918 * While BCM5719 and BCM5720 have a hardware workaround
1919 * which could fix the above bug.
1920 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1923 * For the rest of the chips in these two families, we will
1924 * have to poll the status block at high rate (10ms currently)
1925 * to check whether the interrupt is hosed or not.
1926 * See bnx_intr_check() for details.
1928 sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1931 sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
1932 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1933 sc->bnx_asicrev == BGE_ASICREV_BCM5720)
1934 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
1936 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
1937 device_printf(dev, "CHIP ID 0x%08x; "
1938 "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
1939 sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
1942 * Set various PHY quirk flags.
1945 capmask = MII_CAPMASK_DEFAULT;
1946 if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
1947 product == PCI_PRODUCT_BROADCOM_BCM57795) {
1949 capmask &= ~BMSR_EXTSTAT;
1952 mii_priv |= BRGPHY_FLAG_WIRESPEED;
1953 if (sc->bnx_chipid == BGE_CHIPID_BCM5762_A0)
1954 mii_priv |= BRGPHY_FLAG_5762_A0;
1957 * Allocate interrupt
1959 sc->bnx_irq_type = pci_alloc_1intr(dev, bnx_msi_enable, &sc->bnx_irq_rid,
1962 sc->bnx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->bnx_irq_rid,
1964 if (sc->bnx_irq == NULL) {
1965 device_printf(dev, "couldn't map interrupt\n");
1970 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
1971 sc->bnx_flags |= BNX_FLAG_ONESHOT_MSI;
1975 /* Initialize if_name earlier, so if_printf could be used */
1976 ifp = &sc->arpcom.ac_if;
1977 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1979 /* Try to reset the chip. */
1982 if (bnx_chipinit(sc)) {
1983 device_printf(dev, "chip initialization failed\n");
1989 * Get station address
1991 error = bnx_get_eaddr(sc, ether_addr);
1993 device_printf(dev, "failed to read station address\n");
1997 if (BNX_IS_57765_PLUS(sc)) {
1998 sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT;
2000 /* 5705/5750 limits RX return ring to 512 entries. */
2001 sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2004 error = bnx_dma_alloc(sc);
2008 /* Set default tuneable values. */
2009 sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
2010 sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
2011 sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
2012 sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
2013 sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
2014 sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
2015 sc->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
2017 /* Set up ifnet structure */
2019 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2020 ifp->if_ioctl = bnx_ioctl;
2021 ifp->if_start = bnx_start;
2022 #ifdef IFPOLL_ENABLE
2023 ifp->if_npoll = bnx_npoll;
2025 ifp->if_watchdog = bnx_watchdog;
2026 ifp->if_init = bnx_init;
2027 ifp->if_mtu = ETHERMTU;
2028 ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2029 ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
2030 ifq_set_ready(&ifp->if_snd);
2032 ifp->if_capabilities |= IFCAP_HWCSUM;
2033 ifp->if_hwassist = BNX_CSUM_FEATURES;
2034 if (sc->bnx_flags & BNX_FLAG_TSO) {
2035 ifp->if_capabilities |= IFCAP_TSO;
2036 ifp->if_hwassist |= CSUM_TSO;
2038 ifp->if_capenable = ifp->if_capabilities;
2041 * Figure out what sort of media we have by checking the
2042 * hardware config word in the first 32k of NIC internal memory,
2043 * or fall back to examining the EEPROM if necessary.
2044 * Note: on some BCM5700 cards, this value appears to be unset.
2045 * If that's the case, we have to rely on identifying the NIC
2046 * by its PCI subsystem ID, as we do below for the SysKonnect
2049 if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2050 hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2052 if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2054 device_printf(dev, "failed to read EEPROM\n");
2058 hwcfg = ntohl(hwcfg);
2061 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
2062 if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
2063 (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2064 sc->bnx_flags |= BNX_FLAG_TBI;
2067 if (sc->bnx_flags & BNX_FLAG_CPMU)
2068 sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
2070 sc->bnx_mi_mode = BGE_MIMODE_BASE;
2072 /* Setup link status update stuffs */
2073 if (sc->bnx_flags & BNX_FLAG_TBI) {
2074 sc->bnx_link_upd = bnx_tbi_link_upd;
2075 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2076 } else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
2077 sc->bnx_link_upd = bnx_autopoll_link_upd;
2078 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2080 sc->bnx_link_upd = bnx_copper_link_upd;
2081 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
2084 /* Set default PHY address */
2088 * PHY address mapping for various devices.
2090 * | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
2091 * ---------+-------+-------+-------+-------+
2092 * BCM57XX | 1 | X | X | X |
2093 * BCM5704 | 1 | X | 1 | X |
2094 * BCM5717 | 1 | 8 | 2 | 9 |
2095 * BCM5719 | 1 | 8 | 2 | 9 |
2096 * BCM5720 | 1 | 8 | 2 | 9 |
2098 * Other addresses may respond but they are not
2099 * IEEE compliant PHYs and should be ignored.
2101 if (BNX_IS_5717_PLUS(sc)) {
2104 f = pci_get_function(dev);
2105 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
2106 if (CSR_READ_4(sc, BGE_SGDIG_STS) &
2107 BGE_SGDIGSTS_IS_SERDES)
2108 sc->bnx_phyno = f + 8;
2110 sc->bnx_phyno = f + 1;
2112 if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
2113 BGE_CPMU_PHY_STRAP_IS_SERDES)
2114 sc->bnx_phyno = f + 8;
2116 sc->bnx_phyno = f + 1;
2120 if (sc->bnx_flags & BNX_FLAG_TBI) {
2121 ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
2122 bnx_ifmedia_upd, bnx_ifmedia_sts);
2123 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2124 ifmedia_add(&sc->bnx_ifmedia,
2125 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2126 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2127 ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
2128 sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
2130 struct mii_probe_args mii_args;
2132 mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
2133 mii_args.mii_probemask = 1 << sc->bnx_phyno;
2134 mii_args.mii_capmask = capmask;
2135 mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
2136 mii_args.mii_priv = mii_priv;
2138 error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
2140 device_printf(dev, "MII without any PHY!\n");
2146 * Create sysctl nodes.
2148 sysctl_ctx_init(&sc->bnx_sysctl_ctx);
2149 sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
2150 SYSCTL_STATIC_CHILDREN(_hw),
2152 device_get_nameunit(dev),
2154 if (sc->bnx_sysctl_tree == NULL) {
2155 device_printf(dev, "can't add sysctl node\n");
2160 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2161 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2162 OID_AUTO, "rx_coal_ticks",
2163 CTLTYPE_INT | CTLFLAG_RW,
2164 sc, 0, bnx_sysctl_rx_coal_ticks, "I",
2165 "Receive coalescing ticks (usec).");
2166 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2167 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2168 OID_AUTO, "tx_coal_ticks",
2169 CTLTYPE_INT | CTLFLAG_RW,
2170 sc, 0, bnx_sysctl_tx_coal_ticks, "I",
2171 "Transmit coalescing ticks (usec).");
2172 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2173 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2174 OID_AUTO, "rx_coal_bds",
2175 CTLTYPE_INT | CTLFLAG_RW,
2176 sc, 0, bnx_sysctl_rx_coal_bds, "I",
2177 "Receive max coalesced BD count.");
2178 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2179 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2180 OID_AUTO, "tx_coal_bds",
2181 CTLTYPE_INT | CTLFLAG_RW,
2182 sc, 0, bnx_sysctl_tx_coal_bds, "I",
2183 "Transmit max coalesced BD count.");
2185 * A common design characteristic for many Broadcom
2186 * client controllers is that they only support a
2187 * single outstanding DMA read operation on the PCIe
2188 * bus. This means that it will take twice as long to
2189 * fetch a TX frame that is split into header and
2190 * payload buffers as it does to fetch a single,
2191 * contiguous TX frame (2 reads vs. 1 read). For these
2192 * controllers, coalescing buffers to reduce the number
2193 * of memory reads is effective way to get maximum
2194 * performance(about 940Mbps). Without collapsing TX
2195 * buffers the maximum TCP bulk transfer performance
2196 * is about 850Mbps. However forcing coalescing mbufs
2197 * consumes a lot of CPU cycles, so leave it off by
2200 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2201 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2202 "force_defrag", CTLFLAG_RW, &sc->bnx_force_defrag, 0,
2203 "Force defragment on TX path");
2205 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2206 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2207 "tx_wreg", CTLFLAG_RW, &sc->bnx_tx_wreg, 0,
2208 "# of segments before writing to hardware register");
2210 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2211 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2212 "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2213 sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
2214 "Receive max coalesced BD count during interrupt.");
2215 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2216 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2217 "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2218 sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
2219 "Transmit max coalesced BD count during interrupt.");
2221 #ifdef BNX_TSO_DEBUG
2222 for (i = 0; i < BNX_TSO_NSTATS; ++i) {
2223 ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
2224 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2225 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2226 desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
2231 * Call MI attach routine.
2233 ether_ifattach(ifp, ether_addr, NULL);
2235 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2237 #ifdef IFPOLL_ENABLE
2238 ifpoll_compat_setup(&sc->bnx_npoll,
2239 &sc->bnx_sysctl_ctx, sc->bnx_sysctl_tree,
2240 device_get_unit(dev), ifp->if_serializer);
2243 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
2244 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
2245 intr_func = bnx_msi_oneshot;
2247 device_printf(dev, "oneshot MSI\n");
2249 intr_func = bnx_msi;
2252 intr_func = bnx_intr_legacy;
2254 error = bus_setup_intr(dev, sc->bnx_irq, INTR_MPSAFE, intr_func, sc,
2255 &sc->bnx_intrhand, ifp->if_serializer);
2257 ether_ifdetach(ifp);
2258 device_printf(dev, "couldn't set up irq\n");
2262 sc->bnx_intr_cpuid = rman_get_cpuid(sc->bnx_irq);
2263 sc->bnx_stat_cpuid = sc->bnx_intr_cpuid;
2272 bnx_detach(device_t dev)
2274 struct bnx_softc *sc = device_get_softc(dev);
2276 if (device_is_attached(dev)) {
2277 struct ifnet *ifp = &sc->arpcom.ac_if;
2279 lwkt_serialize_enter(ifp->if_serializer);
2282 bus_teardown_intr(dev, sc->bnx_irq, sc->bnx_intrhand);
2283 lwkt_serialize_exit(ifp->if_serializer);
2285 ether_ifdetach(ifp);
2288 if (sc->bnx_flags & BNX_FLAG_TBI)
2289 ifmedia_removeall(&sc->bnx_ifmedia);
2291 device_delete_child(dev, sc->bnx_miibus);
2292 bus_generic_detach(dev);
2294 if (sc->bnx_irq != NULL) {
2295 bus_release_resource(dev, SYS_RES_IRQ, sc->bnx_irq_rid,
2298 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI)
2299 pci_release_msi(dev);
2301 if (sc->bnx_res != NULL) {
2302 bus_release_resource(dev, SYS_RES_MEMORY,
2303 BGE_PCI_BAR0, sc->bnx_res);
2306 if (sc->bnx_sysctl_tree != NULL)
2307 sysctl_ctx_free(&sc->bnx_sysctl_ctx);
2315 bnx_reset(struct bnx_softc *sc)
2318 uint32_t cachesize, command, pcistate, reset;
2319 void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
2325 if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
2326 write_op = bnx_writemem_direct;
2328 write_op = bnx_writereg_ind;
2330 /* Save some important PCI state. */
2331 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2332 command = pci_read_config(dev, BGE_PCI_CMD, 4);
2333 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2335 pci_write_config(dev, BGE_PCI_MISC_CTL,
2336 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2337 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2338 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2340 /* Disable fastboot on controllers that support it. */
2342 if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
2343 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
2346 * Write the magic number to SRAM at offset 0xB50.
2347 * When firmware finishes its initialization it will
2348 * write ~BGE_MAGIC_NUMBER to the same location.
2350 bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2352 reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2354 /* XXX: Broadcom Linux driver. */
2355 /* Force PCI-E 1.0a mode */
2356 if (!BNX_IS_57765_PLUS(sc) &&
2357 CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
2358 (BGE_PCIE_PHY_TSTCTL_PSCRAM |
2359 BGE_PCIE_PHY_TSTCTL_PCIE10)) {
2360 CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
2361 BGE_PCIE_PHY_TSTCTL_PSCRAM);
2363 if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
2364 /* Prevent PCIE link training during global reset */
2365 CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2370 * Set GPHY Power Down Override to leave GPHY
2371 * powered up in D0 uninitialized.
2373 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
2374 reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
2376 /* Issue global reset */
2377 write_op(sc, BGE_MISC_CFG, reset);
2379 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
2380 uint32_t status, ctrl;
2382 status = CSR_READ_4(sc, BGE_VCPU_STATUS);
2383 CSR_WRITE_4(sc, BGE_VCPU_STATUS,
2384 status | BGE_VCPU_STATUS_DRV_RESET);
2385 ctrl = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
2386 CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
2387 ctrl & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
2392 /* XXX: Broadcom Linux driver. */
2393 if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
2396 DELAY(500000); /* wait for link training to complete */
2397 v = pci_read_config(dev, 0xc4, 4);
2398 pci_write_config(dev, 0xc4, v | (1<<15), 4);
2401 devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
2403 /* Disable no snoop and disable relaxed ordering. */
2404 devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
2406 /* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
2407 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
2408 devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
2409 devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
2412 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
2415 /* Clear error status. */
2416 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
2417 PCIEM_DEVSTS_CORR_ERR |
2418 PCIEM_DEVSTS_NFATAL_ERR |
2419 PCIEM_DEVSTS_FATAL_ERR |
2420 PCIEM_DEVSTS_UNSUPP_REQ, 2);
2422 /* Reset some of the PCI state that got zapped by reset */
2423 pci_write_config(dev, BGE_PCI_MISC_CTL,
2424 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2425 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2426 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2427 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2428 pci_write_config(dev, BGE_PCI_CMD, command, 4);
2429 write_op(sc, BGE_MISC_CFG, (65 << 1));
2431 /* Enable memory arbiter */
2432 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2434 if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
2435 for (i = 0; i < BNX_TIMEOUT; i++) {
2436 val = CSR_READ_4(sc, BGE_VCPU_STATUS);
2437 if (val & BGE_VCPU_STATUS_INIT_DONE)
2441 if (i == BNX_TIMEOUT) {
2442 if_printf(&sc->arpcom.ac_if, "reset timed out\n");
2447 * Poll until we see the 1's complement of the magic number.
2448 * This indicates that the firmware initialization
2451 for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
2452 val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2453 if (val == ~BGE_MAGIC_NUMBER)
2457 if (i == BNX_FIRMWARE_TIMEOUT) {
2458 if_printf(&sc->arpcom.ac_if, "firmware handshake "
2459 "timed out, found 0x%08x\n", val);
2462 /* BCM57765 A0 needs additional time before accessing. */
2463 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
2468 * XXX Wait for the value of the PCISTATE register to
2469 * return to its original pre-reset state. This is a
2470 * fairly good indicator of reset completion. If we don't
2471 * wait for the reset to fully complete, trying to read
2472 * from the device's non-PCI registers may yield garbage
2475 for (i = 0; i < BNX_TIMEOUT; i++) {
2476 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2481 /* Fix up byte swapping */
2482 CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
2484 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2487 * The 5704 in TBI mode apparently needs some special
2488 * adjustment to insure the SERDES drive level is set
2491 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
2492 (sc->bnx_flags & BNX_FLAG_TBI)) {
2495 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2496 serdescfg = (serdescfg & ~0xFFF) | 0x880;
2497 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2500 CSR_WRITE_4(sc, BGE_MI_MODE,
2501 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
2504 /* XXX: Broadcom Linux driver. */
2505 if (!BNX_IS_57765_PLUS(sc)) {
2508 /* Enable Data FIFO protection. */
2509 v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2510 CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
2515 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
2516 BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
2517 CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
2522 * Frame reception handling. This is called if there's a frame
2523 * on the receive return list.
2525 * Note: we have to be able to handle two possibilities here:
2526 * 1) the frame is from the jumbo recieve ring
2527 * 2) the frame is from the standard receive ring
2531 bnx_rxeof(struct bnx_softc *sc, uint16_t rx_prod, int count)
2534 int stdcnt = 0, jumbocnt = 0;
2536 ifp = &sc->arpcom.ac_if;
2538 while (sc->bnx_rx_saved_considx != rx_prod && count != 0) {
2539 struct bge_rx_bd *cur_rx;
2541 struct mbuf *m = NULL;
2542 uint16_t vlan_tag = 0;
2548 &sc->bnx_ldata.bnx_rx_return_ring[sc->bnx_rx_saved_considx];
2550 rxidx = cur_rx->bge_idx;
2551 BNX_INC(sc->bnx_rx_saved_considx, sc->bnx_return_ring_cnt);
2553 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2555 vlan_tag = cur_rx->bge_vlan_tag;
2558 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2559 BNX_INC(sc->bnx_jumbo, BGE_JUMBO_RX_RING_CNT);
2562 if (rxidx != sc->bnx_jumbo) {
2563 IFNET_STAT_INC(ifp, ierrors, 1);
2564 if_printf(ifp, "sw jumbo index(%d) "
2565 "and hw jumbo index(%d) mismatch, drop!\n",
2566 sc->bnx_jumbo, rxidx);
2567 bnx_setup_rxdesc_jumbo(sc, rxidx);
2571 m = sc->bnx_cdata.bnx_rx_jumbo_chain[rxidx].bnx_mbuf;
2572 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2573 IFNET_STAT_INC(ifp, ierrors, 1);
2574 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2577 if (bnx_newbuf_jumbo(sc, sc->bnx_jumbo, 0)) {
2578 IFNET_STAT_INC(ifp, ierrors, 1);
2579 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2583 BNX_INC(sc->bnx_std, BGE_STD_RX_RING_CNT);
2586 if (rxidx != sc->bnx_std) {
2587 IFNET_STAT_INC(ifp, ierrors, 1);
2588 if_printf(ifp, "sw std index(%d) "
2589 "and hw std index(%d) mismatch, drop!\n",
2590 sc->bnx_std, rxidx);
2591 bnx_setup_rxdesc_std(sc, rxidx);
2595 m = sc->bnx_cdata.bnx_rx_std_chain[rxidx].bnx_mbuf;
2596 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2597 IFNET_STAT_INC(ifp, ierrors, 1);
2598 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2601 if (bnx_newbuf_std(sc, sc->bnx_std, 0)) {
2602 IFNET_STAT_INC(ifp, ierrors, 1);
2603 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2608 IFNET_STAT_INC(ifp, ipackets, 1);
2609 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2610 m->m_pkthdr.rcvif = ifp;
2612 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2613 (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
2614 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
2615 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2616 if ((cur_rx->bge_error_flag &
2617 BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
2618 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2620 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2621 m->m_pkthdr.csum_data =
2622 cur_rx->bge_tcp_udp_csum;
2623 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
2629 * If we received a packet with a vlan tag, pass it
2630 * to vlan_input() instead of ether_input().
2633 m->m_flags |= M_VLANTAG;
2634 m->m_pkthdr.ether_vlantag = vlan_tag;
2636 ifp->if_input(ifp, m);
2639 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bnx_rx_saved_considx);
2641 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
2643 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
2647 bnx_txeof(struct bnx_softc *sc, uint16_t tx_cons)
2651 ifp = &sc->arpcom.ac_if;
2654 * Go through our tx ring and free mbufs for those
2655 * frames that have been sent.
2657 while (sc->bnx_tx_saved_considx != tx_cons) {
2660 idx = sc->bnx_tx_saved_considx;
2661 if (sc->bnx_cdata.bnx_tx_chain[idx] != NULL) {
2662 IFNET_STAT_INC(ifp, opackets, 1);
2663 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
2664 sc->bnx_cdata.bnx_tx_dmamap[idx]);
2665 m_freem(sc->bnx_cdata.bnx_tx_chain[idx]);
2666 sc->bnx_cdata.bnx_tx_chain[idx] = NULL;
2669 BNX_INC(sc->bnx_tx_saved_considx, BGE_TX_RING_CNT);
2672 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) >=
2673 (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
2674 ifq_clr_oactive(&ifp->if_snd);
2676 if (sc->bnx_txcnt == 0)
2679 if (!ifq_is_empty(&ifp->if_snd))
2683 #ifdef IFPOLL_ENABLE
2686 bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2688 struct bnx_softc *sc = ifp->if_softc;
2690 ASSERT_SERIALIZED(ifp->if_serializer);
2693 int cpuid = sc->bnx_npoll.ifpc_cpuid;
2695 info->ifpi_rx[cpuid].poll_func = bnx_npoll_compat;
2696 info->ifpi_rx[cpuid].arg = NULL;
2697 info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
2699 if (ifp->if_flags & IFF_RUNNING)
2700 bnx_disable_intr(sc);
2701 ifq_set_cpuid(&ifp->if_snd, cpuid);
2703 if (ifp->if_flags & IFF_RUNNING)
2704 bnx_enable_intr(sc);
2705 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2710 bnx_npoll_compat(struct ifnet *ifp, void *arg __unused, int cycle)
2712 struct bnx_softc *sc = ifp->if_softc;
2713 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2714 uint16_t rx_prod, tx_cons;
2716 ASSERT_SERIALIZED(ifp->if_serializer);
2718 if (sc->bnx_npoll.ifpc_stcount-- == 0) {
2719 sc->bnx_npoll.ifpc_stcount = sc->bnx_npoll.ifpc_stfrac;
2721 * Process link state changes.
2726 sc->bnx_status_tag = sblk->bge_status_tag;
2729 * Use a load fence to ensure that status_tag is saved
2730 * before rx_prod and tx_cons.
2734 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2735 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2737 if (sc->bnx_rx_saved_considx != rx_prod)
2738 bnx_rxeof(sc, rx_prod, cycle);
2740 if (sc->bnx_tx_saved_considx != tx_cons)
2741 bnx_txeof(sc, tx_cons);
2743 if (sc->bnx_coal_chg)
2744 bnx_coal_change(sc);
2747 #endif /* IFPOLL_ENABLE */
2750 bnx_intr_legacy(void *xsc)
2752 struct bnx_softc *sc = xsc;
2753 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2755 if (sc->bnx_status_tag == sblk->bge_status_tag) {
2758 val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
2759 if (val & BGE_PCISTAT_INTR_NOTACT)
2765 * Interrupt will have to be disabled if tagged status
2766 * is used, else interrupt will always be asserted on
2767 * certain chips (at least on BCM5750 AX/BX).
2769 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2777 struct bnx_softc *sc = xsc;
2779 /* Disable interrupt first */
2780 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2785 bnx_msi_oneshot(void *xsc)
2791 bnx_intr(struct bnx_softc *sc)
2793 struct ifnet *ifp = &sc->arpcom.ac_if;
2794 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2795 uint16_t rx_prod, tx_cons;
2798 sc->bnx_status_tag = sblk->bge_status_tag;
2800 * Use a load fence to ensure that status_tag is saved
2801 * before rx_prod, tx_cons and status.
2805 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2806 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2807 status = sblk->bge_status;
2809 if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
2812 if (ifp->if_flags & IFF_RUNNING) {
2813 if (sc->bnx_rx_saved_considx != rx_prod)
2814 bnx_rxeof(sc, rx_prod, -1);
2816 if (sc->bnx_tx_saved_considx != tx_cons)
2817 bnx_txeof(sc, tx_cons);
2820 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
2822 if (sc->bnx_coal_chg)
2823 bnx_coal_change(sc);
2829 struct bnx_softc *sc = xsc;
2830 struct ifnet *ifp = &sc->arpcom.ac_if;
2832 lwkt_serialize_enter(ifp->if_serializer);
2834 KKASSERT(mycpuid == sc->bnx_stat_cpuid);
2836 bnx_stats_update_regs(sc);
2838 if (sc->bnx_flags & BNX_FLAG_TBI) {
2840 * Since in TBI mode auto-polling can't be used we should poll
2841 * link status manually. Here we register pending link event
2842 * and trigger interrupt.
2845 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
2846 } else if (!sc->bnx_link) {
2847 mii_tick(device_get_softc(sc->bnx_miibus));
2850 callout_reset(&sc->bnx_stat_timer, hz, bnx_tick, sc);
2852 lwkt_serialize_exit(ifp->if_serializer);
2856 bnx_stats_update_regs(struct bnx_softc *sc)
2858 struct ifnet *ifp = &sc->arpcom.ac_if;
2859 struct bge_mac_stats_regs stats;
2863 s = (uint32_t *)&stats;
2864 for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
2865 *s = CSR_READ_4(sc, BGE_RX_STATS + i);
2869 IFNET_STAT_SET(ifp, collisions,
2870 (stats.dot3StatsSingleCollisionFrames +
2871 stats.dot3StatsMultipleCollisionFrames +
2872 stats.dot3StatsExcessiveCollisions +
2873 stats.dot3StatsLateCollisions));
2877 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
2878 * pointers to descriptors.
2881 bnx_encap(struct bnx_softc *sc, struct mbuf **m_head0, uint32_t *txidx,
2884 struct bge_tx_bd *d = NULL;
2885 uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
2886 bus_dma_segment_t segs[BNX_NSEG_NEW];
2888 int error, maxsegs, nsegs, idx, i;
2889 struct mbuf *m_head = *m_head0, *m_new;
2891 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
2892 #ifdef BNX_TSO_DEBUG
2896 error = bnx_setup_tso(sc, m_head0, &mss, &csum_flags);
2901 #ifdef BNX_TSO_DEBUG
2902 tso_nsegs = (m_head->m_pkthdr.len /
2903 m_head->m_pkthdr.tso_segsz) - 1;
2904 if (tso_nsegs > (BNX_TSO_NSTATS - 1))
2905 tso_nsegs = BNX_TSO_NSTATS - 1;
2906 else if (tso_nsegs < 0)
2908 sc->bnx_tsosegs[tso_nsegs]++;
2910 } else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
2911 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2912 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2913 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2914 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2915 if (m_head->m_flags & M_LASTFRAG)
2916 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
2917 else if (m_head->m_flags & M_FRAG)
2918 csum_flags |= BGE_TXBDFLAG_IP_FRAG;
2920 if (m_head->m_flags & M_VLANTAG) {
2921 csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
2922 vlan_tag = m_head->m_pkthdr.ether_vlantag;
2926 map = sc->bnx_cdata.bnx_tx_dmamap[idx];
2928 maxsegs = (BGE_TX_RING_CNT - sc->bnx_txcnt) - BNX_NSEG_RSVD;
2929 KASSERT(maxsegs >= BNX_NSEG_SPARE,
2930 ("not enough segments %d", maxsegs));
2932 if (maxsegs > BNX_NSEG_NEW)
2933 maxsegs = BNX_NSEG_NEW;
2936 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
2937 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
2938 * but when such padded frames employ the bge IP/TCP checksum
2939 * offload, the hardware checksum assist gives incorrect results
2940 * (possibly from incorporating its own padding into the UDP/TCP
2941 * checksum; who knows). If we pad such runts with zeros, the
2942 * onboard checksum comes out correct.
2944 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
2945 m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
2946 error = m_devpad(m_head, BNX_MIN_FRAMELEN);
2951 if ((sc->bnx_flags & BNX_FLAG_SHORTDMA) && m_head->m_next != NULL) {
2952 m_new = bnx_defrag_shortdma(m_head);
2953 if (m_new == NULL) {
2957 *m_head0 = m_head = m_new;
2959 if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
2960 sc->bnx_force_defrag && m_head->m_next != NULL) {
2962 * Forcefully defragment mbuf chain to overcome hardware
2963 * limitation which only support a single outstanding
2964 * DMA read operation. If it fails, keep moving on using
2965 * the original mbuf chain.
2967 m_new = m_defrag(m_head, MB_DONTWAIT);
2969 *m_head0 = m_head = m_new;
2972 error = bus_dmamap_load_mbuf_defrag(sc->bnx_cdata.bnx_tx_mtag, map,
2973 m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
2976 *segs_used += nsegs;
2979 bus_dmamap_sync(sc->bnx_cdata.bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
2981 for (i = 0; ; i++) {
2982 d = &sc->bnx_ldata.bnx_tx_ring[idx];
2984 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
2985 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
2986 d->bge_len = segs[i].ds_len;
2987 d->bge_flags = csum_flags;
2988 d->bge_vlan_tag = vlan_tag;
2993 BNX_INC(idx, BGE_TX_RING_CNT);
2995 /* Mark the last segment as end of packet... */
2996 d->bge_flags |= BGE_TXBDFLAG_END;
2999 * Insure that the map for this transmission is placed at
3000 * the array index of the last descriptor in this chain.
3002 sc->bnx_cdata.bnx_tx_dmamap[*txidx] = sc->bnx_cdata.bnx_tx_dmamap[idx];
3003 sc->bnx_cdata.bnx_tx_dmamap[idx] = map;
3004 sc->bnx_cdata.bnx_tx_chain[idx] = m_head;
3005 sc->bnx_txcnt += nsegs;
3007 BNX_INC(idx, BGE_TX_RING_CNT);
3018 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3019 * to the mbuf data regions directly in the transmit descriptors.
3022 bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3024 struct bnx_softc *sc = ifp->if_softc;
3025 struct mbuf *m_head = NULL;
3029 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
3031 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
3034 prodidx = sc->bnx_tx_prodidx;
3036 while (sc->bnx_cdata.bnx_tx_chain[prodidx] == NULL) {
3038 * Sanity check: avoid coming within BGE_NSEG_RSVD
3039 * descriptors of the end of the ring. Also make
3040 * sure there are BGE_NSEG_SPARE descriptors for
3041 * jumbo buffers' or TSO segments' defragmentation.
3043 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) <
3044 (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
3045 ifq_set_oactive(&ifp->if_snd);
3049 m_head = ifq_dequeue(&ifp->if_snd, NULL);
3054 * Pack the data into the transmit ring. If we
3055 * don't have room, set the OACTIVE flag and wait
3056 * for the NIC to drain the ring.
3058 if (bnx_encap(sc, &m_head, &prodidx, &nsegs)) {
3059 ifq_set_oactive(&ifp->if_snd);
3060 IFNET_STAT_INC(ifp, oerrors, 1);
3064 if (nsegs >= sc->bnx_tx_wreg) {
3066 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3070 ETHER_BPF_MTAP(ifp, m_head);
3073 * Set a timeout in case the chip goes out to lunch.
3080 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3082 sc->bnx_tx_prodidx = prodidx;
3088 struct bnx_softc *sc = xsc;
3089 struct ifnet *ifp = &sc->arpcom.ac_if;
3093 ASSERT_SERIALIZED(ifp->if_serializer);
3095 /* Cancel pending I/O and flush buffers. */
3101 * Init the various state machines, ring
3102 * control blocks and firmware.
3104 if (bnx_blockinit(sc)) {
3105 if_printf(ifp, "initialization failure\n");
3111 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3112 ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
3114 /* Load our MAC address. */
3115 m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
3116 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3117 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3119 /* Enable or disable promiscuous mode as needed. */
3122 /* Program multicast filter. */
3126 if (bnx_init_rx_ring_std(sc)) {
3127 if_printf(ifp, "RX ring initialization failed\n");
3132 /* Init jumbo RX ring. */
3133 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
3134 if (bnx_init_rx_ring_jumbo(sc)) {
3135 if_printf(ifp, "Jumbo RX ring initialization failed\n");
3141 /* Init our RX return ring index */
3142 sc->bnx_rx_saved_considx = 0;
3145 bnx_init_tx_ring(sc);
3147 /* Enable TX MAC state machine lockup fix. */
3148 mode = CSR_READ_4(sc, BGE_TX_MODE);
3149 mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
3150 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
3151 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
3152 mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3153 mode |= CSR_READ_4(sc, BGE_TX_MODE) &
3154 (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
3156 /* Turn on transmitter */
3157 CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
3159 /* Turn on receiver */
3160 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3163 * Set the number of good frames to receive after RX MBUF
3164 * Low Watermark has been reached. After the RX MAC receives
3165 * this number of frames, it will drop subsequent incoming
3166 * frames until the MBUF High Watermark is reached.
3168 if (BNX_IS_57765_FAMILY(sc))
3169 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
3171 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
3173 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
3175 if_printf(ifp, "MSI_MODE: %#x\n",
3176 CSR_READ_4(sc, BGE_MSI_MODE));
3180 /* Tell firmware we're alive. */
3181 BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3183 /* Enable host interrupts if polling(4) is not enabled. */
3184 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
3185 #ifdef IFPOLL_ENABLE
3186 if (ifp->if_flags & IFF_NPOLLING)
3187 bnx_disable_intr(sc);
3190 bnx_enable_intr(sc);
3192 bnx_ifmedia_upd(ifp);
3194 ifp->if_flags |= IFF_RUNNING;
3195 ifq_clr_oactive(&ifp->if_snd);
3197 callout_reset_bycpu(&sc->bnx_stat_timer, hz, bnx_tick, sc,
3198 sc->bnx_stat_cpuid);
3202 * Set media options.
3205 bnx_ifmedia_upd(struct ifnet *ifp)
3207 struct bnx_softc *sc = ifp->if_softc;
3209 /* If this is a 1000baseX NIC, enable the TBI port. */
3210 if (sc->bnx_flags & BNX_FLAG_TBI) {
3211 struct ifmedia *ifm = &sc->bnx_ifmedia;
3213 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3216 switch(IFM_SUBTYPE(ifm->ifm_media)) {
3221 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3222 BNX_CLRBIT(sc, BGE_MAC_MODE,
3223 BGE_MACMODE_HALF_DUPLEX);
3225 BNX_SETBIT(sc, BGE_MAC_MODE,
3226 BGE_MACMODE_HALF_DUPLEX);
3233 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3237 if (mii->mii_instance) {
3238 struct mii_softc *miisc;
3240 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3241 mii_phy_reset(miisc);
3246 * Force an interrupt so that we will call bnx_link_upd
3247 * if needed and clear any pending link state attention.
3248 * Without this we are not getting any further interrupts
3249 * for link state changes and thus will not UP the link and
3250 * not be able to send in bnx_start. The only way to get
3251 * things working was to receive a packet and get an RX
3254 * bnx_tick should help for fiber cards and we might not
3255 * need to do this here if BNX_FLAG_TBI is set but as
3256 * we poll for fiber anyway it should not harm.
3258 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3264 * Report current media status.
3267 bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3269 struct bnx_softc *sc = ifp->if_softc;
3271 if (sc->bnx_flags & BNX_FLAG_TBI) {
3272 ifmr->ifm_status = IFM_AVALID;
3273 ifmr->ifm_active = IFM_ETHER;
3274 if (CSR_READ_4(sc, BGE_MAC_STS) &
3275 BGE_MACSTAT_TBI_PCS_SYNCHED) {
3276 ifmr->ifm_status |= IFM_ACTIVE;
3278 ifmr->ifm_active |= IFM_NONE;
3282 ifmr->ifm_active |= IFM_1000_SX;
3283 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3284 ifmr->ifm_active |= IFM_HDX;
3286 ifmr->ifm_active |= IFM_FDX;
3288 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3291 ifmr->ifm_active = mii->mii_media_active;
3292 ifmr->ifm_status = mii->mii_media_status;
3297 bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
3299 struct bnx_softc *sc = ifp->if_softc;
3300 struct ifreq *ifr = (struct ifreq *)data;
3301 int mask, error = 0;
3303 ASSERT_SERIALIZED(ifp->if_serializer);
3307 if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
3308 (BNX_IS_JUMBO_CAPABLE(sc) &&
3309 ifr->ifr_mtu > BNX_JUMBO_MTU)) {
3311 } else if (ifp->if_mtu != ifr->ifr_mtu) {
3312 ifp->if_mtu = ifr->ifr_mtu;
3313 if (ifp->if_flags & IFF_RUNNING)
3318 if (ifp->if_flags & IFF_UP) {
3319 if (ifp->if_flags & IFF_RUNNING) {
3320 mask = ifp->if_flags ^ sc->bnx_if_flags;
3323 * If only the state of the PROMISC flag
3324 * changed, then just use the 'set promisc
3325 * mode' command instead of reinitializing
3326 * the entire NIC. Doing a full re-init
3327 * means reloading the firmware and waiting
3328 * for it to start up, which may take a
3329 * second or two. Similarly for ALLMULTI.
3331 if (mask & IFF_PROMISC)
3333 if (mask & IFF_ALLMULTI)
3338 } else if (ifp->if_flags & IFF_RUNNING) {
3341 sc->bnx_if_flags = ifp->if_flags;
3345 if (ifp->if_flags & IFF_RUNNING)
3350 if (sc->bnx_flags & BNX_FLAG_TBI) {
3351 error = ifmedia_ioctl(ifp, ifr,
3352 &sc->bnx_ifmedia, command);
3354 struct mii_data *mii;
3356 mii = device_get_softc(sc->bnx_miibus);
3357 error = ifmedia_ioctl(ifp, ifr,
3358 &mii->mii_media, command);
3362 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3363 if (mask & IFCAP_HWCSUM) {
3364 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
3365 if (ifp->if_capenable & IFCAP_TXCSUM)
3366 ifp->if_hwassist |= BNX_CSUM_FEATURES;
3368 ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
3370 if (mask & IFCAP_TSO) {
3371 ifp->if_capenable ^= (mask & IFCAP_TSO);
3372 if (ifp->if_capenable & IFCAP_TSO)
3373 ifp->if_hwassist |= CSUM_TSO;
3375 ifp->if_hwassist &= ~CSUM_TSO;
3379 error = ether_ioctl(ifp, command, data);
3386 bnx_watchdog(struct ifnet *ifp)
3388 struct bnx_softc *sc = ifp->if_softc;
3390 if_printf(ifp, "watchdog timeout -- resetting\n");
3394 IFNET_STAT_INC(ifp, oerrors, 1);
3396 if (!ifq_is_empty(&ifp->if_snd))
3401 * Stop the adapter and free any mbufs allocated to the
3405 bnx_stop(struct bnx_softc *sc)
3407 struct ifnet *ifp = &sc->arpcom.ac_if;
3409 ASSERT_SERIALIZED(ifp->if_serializer);
3411 callout_stop(&sc->bnx_stat_timer);
3414 * Disable all of the receiver blocks
3416 bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3417 bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3418 bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3419 bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3420 bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3421 bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3424 * Disable all of the transmit blocks
3426 bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3427 bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3428 bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3429 bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3430 bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3431 bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3434 * Shut down all of the memory managers and related
3437 bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3438 bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3439 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3440 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3442 /* Disable host interrupts. */
3443 bnx_disable_intr(sc);
3446 * Tell firmware we're shutting down.
3448 BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3450 /* Free the RX lists. */
3451 bnx_free_rx_ring_std(sc);
3453 /* Free jumbo RX list. */
3454 if (BNX_IS_JUMBO_CAPABLE(sc))
3455 bnx_free_rx_ring_jumbo(sc);
3457 /* Free TX buffers. */
3458 bnx_free_tx_ring(sc);
3460 sc->bnx_status_tag = 0;
3462 sc->bnx_coal_chg = 0;
3464 sc->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
3466 ifp->if_flags &= ~IFF_RUNNING;
3467 ifq_clr_oactive(&ifp->if_snd);
3472 * Stop all chip I/O so that the kernel's probe routines don't
3473 * get confused by errant DMAs when rebooting.
3476 bnx_shutdown(device_t dev)
3478 struct bnx_softc *sc = device_get_softc(dev);
3479 struct ifnet *ifp = &sc->arpcom.ac_if;
3481 lwkt_serialize_enter(ifp->if_serializer);
3484 lwkt_serialize_exit(ifp->if_serializer);
3488 bnx_suspend(device_t dev)
3490 struct bnx_softc *sc = device_get_softc(dev);
3491 struct ifnet *ifp = &sc->arpcom.ac_if;
3493 lwkt_serialize_enter(ifp->if_serializer);
3495 lwkt_serialize_exit(ifp->if_serializer);
3501 bnx_resume(device_t dev)
3503 struct bnx_softc *sc = device_get_softc(dev);
3504 struct ifnet *ifp = &sc->arpcom.ac_if;
3506 lwkt_serialize_enter(ifp->if_serializer);
3508 if (ifp->if_flags & IFF_UP) {
3511 if (!ifq_is_empty(&ifp->if_snd))
3515 lwkt_serialize_exit(ifp->if_serializer);
3521 bnx_setpromisc(struct bnx_softc *sc)
3523 struct ifnet *ifp = &sc->arpcom.ac_if;
3525 if (ifp->if_flags & IFF_PROMISC)
3526 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3528 BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3532 bnx_dma_free(struct bnx_softc *sc)
3536 /* Destroy RX mbuf DMA stuffs. */
3537 if (sc->bnx_cdata.bnx_rx_mtag != NULL) {
3538 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3539 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3540 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3542 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3543 sc->bnx_cdata.bnx_rx_tmpmap);
3544 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3547 /* Destroy TX mbuf DMA stuffs. */
3548 if (sc->bnx_cdata.bnx_tx_mtag != NULL) {
3549 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3550 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3551 sc->bnx_cdata.bnx_tx_dmamap[i]);
3553 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3556 /* Destroy standard RX ring */
3557 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_std_ring_tag,
3558 sc->bnx_cdata.bnx_rx_std_ring_map,
3559 sc->bnx_ldata.bnx_rx_std_ring);
3561 if (BNX_IS_JUMBO_CAPABLE(sc))
3562 bnx_free_jumbo_mem(sc);
3564 /* Destroy RX return ring */
3565 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_return_ring_tag,
3566 sc->bnx_cdata.bnx_rx_return_ring_map,
3567 sc->bnx_ldata.bnx_rx_return_ring);
3569 /* Destroy TX ring */
3570 bnx_dma_block_free(sc->bnx_cdata.bnx_tx_ring_tag,
3571 sc->bnx_cdata.bnx_tx_ring_map,
3572 sc->bnx_ldata.bnx_tx_ring);
3574 /* Destroy status block */
3575 bnx_dma_block_free(sc->bnx_cdata.bnx_status_tag,
3576 sc->bnx_cdata.bnx_status_map,
3577 sc->bnx_ldata.bnx_status_block);
3579 /* Destroy the parent tag */
3580 if (sc->bnx_cdata.bnx_parent_tag != NULL)
3581 bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
3585 bnx_dma_alloc(struct bnx_softc *sc)
3587 struct ifnet *ifp = &sc->arpcom.ac_if;
3592 * Allocate the parent bus DMA tag appropriate for PCI.
3594 * All of the NetExtreme/NetLink controllers have 4GB boundary
3596 * Whenever an address crosses a multiple of the 4GB boundary
3597 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3598 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3599 * state machine will lockup and cause the device to hang.
3601 error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
3602 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3604 BUS_SPACE_MAXSIZE_32BIT, 0,
3605 BUS_SPACE_MAXSIZE_32BIT,
3606 0, &sc->bnx_cdata.bnx_parent_tag);
3608 if_printf(ifp, "could not allocate parent dma tag\n");
3613 * Create DMA tag and maps for RX mbufs.
3615 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3616 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3617 NULL, NULL, MCLBYTES, 1, MCLBYTES,
3618 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK,
3619 &sc->bnx_cdata.bnx_rx_mtag);
3621 if_printf(ifp, "could not allocate RX mbuf dma tag\n");
3625 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3626 BUS_DMA_WAITOK, &sc->bnx_cdata.bnx_rx_tmpmap);
3628 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3629 sc->bnx_cdata.bnx_rx_mtag = NULL;
3633 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3634 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3636 &sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3640 for (j = 0; j < i; ++j) {
3641 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3642 sc->bnx_cdata.bnx_rx_std_dmamap[j]);
3644 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3645 sc->bnx_cdata.bnx_rx_mtag = NULL;
3647 if_printf(ifp, "could not create DMA map for RX\n");
3653 * Create DMA tag and maps for TX mbufs.
3655 if (sc->bnx_flags & BNX_FLAG_TSO)
3656 txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
3658 txmaxsz = BNX_JUMBO_FRAMELEN;
3659 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3660 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3662 txmaxsz, BNX_NSEG_NEW, PAGE_SIZE,
3663 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK |
3665 &sc->bnx_cdata.bnx_tx_mtag);
3667 if_printf(ifp, "could not allocate TX mbuf dma tag\n");
3671 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3672 error = bus_dmamap_create(sc->bnx_cdata.bnx_tx_mtag,
3673 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
3674 &sc->bnx_cdata.bnx_tx_dmamap[i]);
3678 for (j = 0; j < i; ++j) {
3679 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3680 sc->bnx_cdata.bnx_tx_dmamap[j]);
3682 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3683 sc->bnx_cdata.bnx_tx_mtag = NULL;
3685 if_printf(ifp, "could not create DMA map for TX\n");
3691 * Create DMA stuffs for standard RX ring.
3693 error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
3694 &sc->bnx_cdata.bnx_rx_std_ring_tag,
3695 &sc->bnx_cdata.bnx_rx_std_ring_map,
3696 (void *)&sc->bnx_ldata.bnx_rx_std_ring,
3697 &sc->bnx_ldata.bnx_rx_std_ring_paddr);
3699 if_printf(ifp, "could not create std RX ring\n");
3704 * Create jumbo buffer pool.
3706 if (BNX_IS_JUMBO_CAPABLE(sc)) {
3707 error = bnx_alloc_jumbo_mem(sc);
3709 if_printf(ifp, "could not create jumbo buffer pool\n");
3715 * Create DMA stuffs for RX return ring.
3717 error = bnx_dma_block_alloc(sc,
3718 BGE_RX_RTN_RING_SZ(sc->bnx_return_ring_cnt),
3719 &sc->bnx_cdata.bnx_rx_return_ring_tag,
3720 &sc->bnx_cdata.bnx_rx_return_ring_map,
3721 (void *)&sc->bnx_ldata.bnx_rx_return_ring,
3722 &sc->bnx_ldata.bnx_rx_return_ring_paddr);
3724 if_printf(ifp, "could not create RX ret ring\n");
3729 * Create DMA stuffs for TX ring.
3731 error = bnx_dma_block_alloc(sc, BGE_TX_RING_SZ,
3732 &sc->bnx_cdata.bnx_tx_ring_tag,
3733 &sc->bnx_cdata.bnx_tx_ring_map,
3734 (void *)&sc->bnx_ldata.bnx_tx_ring,
3735 &sc->bnx_ldata.bnx_tx_ring_paddr);
3737 if_printf(ifp, "could not create TX ring\n");
3742 * Create DMA stuffs for status block.
3744 error = bnx_dma_block_alloc(sc, BGE_STATUS_BLK_SZ,
3745 &sc->bnx_cdata.bnx_status_tag,
3746 &sc->bnx_cdata.bnx_status_map,
3747 (void *)&sc->bnx_ldata.bnx_status_block,
3748 &sc->bnx_ldata.bnx_status_block_paddr);
3750 if_printf(ifp, "could not create status block\n");
3758 bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
3759 bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
3764 error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
3765 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3766 size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3770 *tag = dmem.dmem_tag;
3771 *map = dmem.dmem_map;
3772 *addr = dmem.dmem_addr;
3773 *paddr = dmem.dmem_busaddr;
3779 bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
3782 bus_dmamap_unload(tag, map);
3783 bus_dmamem_free(tag, addr, map);
3784 bus_dma_tag_destroy(tag);
3789 bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
3791 struct ifnet *ifp = &sc->arpcom.ac_if;
3793 #define PCS_ENCODE_ERR (BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
3796 * Sometimes PCS encoding errors are detected in
3797 * TBI mode (on fiber NICs), and for some reason
3798 * the chip will signal them as link changes.
3799 * If we get a link change event, but the 'PCS
3800 * encoding error' bit in the MAC status register
3801 * is set, don't bother doing a link check.
3802 * This avoids spurious "gigabit link up" messages
3803 * that sometimes appear on fiber NICs during
3804 * periods of heavy traffic.
3806 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
3807 if (!sc->bnx_link) {
3809 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
3810 BNX_CLRBIT(sc, BGE_MAC_MODE,
3811 BGE_MACMODE_TBI_SEND_CFGS);
3813 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
3816 if_printf(ifp, "link UP\n");
3818 ifp->if_link_state = LINK_STATE_UP;
3819 if_link_state_change(ifp);
3821 } else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
3826 if_printf(ifp, "link DOWN\n");
3828 ifp->if_link_state = LINK_STATE_DOWN;
3829 if_link_state_change(ifp);
3833 #undef PCS_ENCODE_ERR
3835 /* Clear the attention. */
3836 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3837 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3838 BGE_MACSTAT_LINK_CHANGED);
3842 bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3844 struct ifnet *ifp = &sc->arpcom.ac_if;
3845 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3848 bnx_miibus_statchg(sc->bnx_dev);
3852 if_printf(ifp, "link UP\n");
3854 if_printf(ifp, "link DOWN\n");
3857 /* Clear the attention. */
3858 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3859 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3860 BGE_MACSTAT_LINK_CHANGED);
3864 bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3866 struct ifnet *ifp = &sc->arpcom.ac_if;
3867 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3871 if (!sc->bnx_link &&
3872 (mii->mii_media_status & IFM_ACTIVE) &&
3873 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
3876 if_printf(ifp, "link UP\n");
3877 } else if (sc->bnx_link &&
3878 (!(mii->mii_media_status & IFM_ACTIVE) ||
3879 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
3882 if_printf(ifp, "link DOWN\n");
3885 /* Clear the attention. */
3886 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3887 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3888 BGE_MACSTAT_LINK_CHANGED);
3892 bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
3894 struct bnx_softc *sc = arg1;
3896 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3897 &sc->bnx_rx_coal_ticks,
3898 BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
3899 BNX_RX_COAL_TICKS_CHG);
3903 bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
3905 struct bnx_softc *sc = arg1;
3907 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3908 &sc->bnx_tx_coal_ticks,
3909 BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
3910 BNX_TX_COAL_TICKS_CHG);
3914 bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
3916 struct bnx_softc *sc = arg1;
3918 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3919 &sc->bnx_rx_coal_bds,
3920 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3921 BNX_RX_COAL_BDS_CHG);
3925 bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
3927 struct bnx_softc *sc = arg1;
3929 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3930 &sc->bnx_tx_coal_bds,
3931 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3932 BNX_TX_COAL_BDS_CHG);
3936 bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3938 struct bnx_softc *sc = arg1;
3940 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3941 &sc->bnx_rx_coal_bds_int,
3942 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3943 BNX_RX_COAL_BDS_INT_CHG);
3947 bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3949 struct bnx_softc *sc = arg1;
3951 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3952 &sc->bnx_tx_coal_bds_int,
3953 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3954 BNX_TX_COAL_BDS_INT_CHG);
3958 bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
3959 int coal_min, int coal_max, uint32_t coal_chg_mask)
3961 struct bnx_softc *sc = arg1;
3962 struct ifnet *ifp = &sc->arpcom.ac_if;
3965 lwkt_serialize_enter(ifp->if_serializer);
3968 error = sysctl_handle_int(oidp, &v, 0, req);
3969 if (!error && req->newptr != NULL) {
3970 if (v < coal_min || v > coal_max) {
3974 sc->bnx_coal_chg |= coal_chg_mask;
3978 lwkt_serialize_exit(ifp->if_serializer);
3983 bnx_coal_change(struct bnx_softc *sc)
3985 struct ifnet *ifp = &sc->arpcom.ac_if;
3987 ASSERT_SERIALIZED(ifp->if_serializer);
3989 if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
3990 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
3991 sc->bnx_rx_coal_ticks);
3993 CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
3996 if_printf(ifp, "rx_coal_ticks -> %u\n",
3997 sc->bnx_rx_coal_ticks);
4001 if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
4002 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
4003 sc->bnx_tx_coal_ticks);
4005 CSR_READ_4(sc, BGE_HCC_TX_COAL_TICKS);
4008 if_printf(ifp, "tx_coal_ticks -> %u\n",
4009 sc->bnx_tx_coal_ticks);
4013 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
4014 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
4015 sc->bnx_rx_coal_bds);
4017 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
4020 if_printf(ifp, "rx_coal_bds -> %u\n",
4021 sc->bnx_rx_coal_bds);
4025 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
4026 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS,
4027 sc->bnx_tx_coal_bds);
4029 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS);
4032 if_printf(ifp, "tx_coal_bds -> %u\n",
4033 sc->bnx_tx_coal_bds);
4037 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
4038 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
4039 sc->bnx_rx_coal_bds_int);
4041 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT);
4044 if_printf(ifp, "rx_coal_bds_int -> %u\n",
4045 sc->bnx_rx_coal_bds_int);
4049 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
4050 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
4051 sc->bnx_tx_coal_bds_int);
4053 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT);
4056 if_printf(ifp, "tx_coal_bds_int -> %u\n",
4057 sc->bnx_tx_coal_bds_int);
4061 sc->bnx_coal_chg = 0;
4065 bnx_intr_check(void *xsc)
4067 struct bnx_softc *sc = xsc;
4068 struct ifnet *ifp = &sc->arpcom.ac_if;
4069 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
4071 lwkt_serialize_enter(ifp->if_serializer);
4073 KKASSERT(mycpuid == sc->bnx_intr_cpuid);
4075 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4076 lwkt_serialize_exit(ifp->if_serializer);
4080 if (sblk->bge_idx[0].bge_rx_prod_idx != sc->bnx_rx_saved_considx ||
4081 sblk->bge_idx[0].bge_tx_cons_idx != sc->bnx_tx_saved_considx) {
4082 if (sc->bnx_rx_check_considx == sc->bnx_rx_saved_considx &&
4083 sc->bnx_tx_check_considx == sc->bnx_tx_saved_considx) {
4084 if (!sc->bnx_intr_maylose) {
4085 sc->bnx_intr_maylose = TRUE;
4089 if_printf(ifp, "lost interrupt\n");
4093 sc->bnx_intr_maylose = FALSE;
4094 sc->bnx_rx_check_considx = sc->bnx_rx_saved_considx;
4095 sc->bnx_tx_check_considx = sc->bnx_tx_saved_considx;
4098 callout_reset(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4099 bnx_intr_check, sc);
4100 lwkt_serialize_exit(ifp->if_serializer);
4104 bnx_enable_intr(struct bnx_softc *sc)
4106 struct ifnet *ifp = &sc->arpcom.ac_if;
4108 lwkt_serialize_handler_enable(ifp->if_serializer);
4113 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
4114 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4115 /* XXX Linux driver */
4116 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
4120 * Unmask the interrupt when we stop polling.
4122 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4123 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4126 * Trigger another interrupt, since above writing
4127 * to interrupt mailbox0 may acknowledge pending
4130 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4132 if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4133 sc->bnx_intr_maylose = FALSE;
4134 sc->bnx_rx_check_considx = 0;
4135 sc->bnx_tx_check_considx = 0;
4138 if_printf(ifp, "status tag bug workaround\n");
4140 /* 10ms check interval */
4141 callout_reset_bycpu(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4142 bnx_intr_check, sc, sc->bnx_intr_cpuid);
4147 bnx_disable_intr(struct bnx_softc *sc)
4149 struct ifnet *ifp = &sc->arpcom.ac_if;
4152 * Mask the interrupt when we start polling.
4154 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4155 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4158 * Acknowledge possible asserted interrupt.
4160 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4162 callout_stop(&sc->bnx_intr_timer);
4163 sc->bnx_intr_maylose = FALSE;
4164 sc->bnx_rx_check_considx = 0;
4165 sc->bnx_tx_check_considx = 0;
4167 sc->bnx_npoll.ifpc_stcount = 0;
4169 lwkt_serialize_handler_disable(ifp->if_serializer);
4173 bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
4178 mac_addr = bnx_readmem_ind(sc, 0x0c14);
4179 if ((mac_addr >> 16) == 0x484b) {
4180 ether_addr[0] = (uint8_t)(mac_addr >> 8);
4181 ether_addr[1] = (uint8_t)mac_addr;
4182 mac_addr = bnx_readmem_ind(sc, 0x0c18);
4183 ether_addr[2] = (uint8_t)(mac_addr >> 24);
4184 ether_addr[3] = (uint8_t)(mac_addr >> 16);
4185 ether_addr[4] = (uint8_t)(mac_addr >> 8);
4186 ether_addr[5] = (uint8_t)mac_addr;
4193 bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
4195 int mac_offset = BGE_EE_MAC_OFFSET;
4197 if (BNX_IS_5717_PLUS(sc)) {
4200 f = pci_get_function(sc->bnx_dev);
4202 mac_offset = BGE_EE_MAC_OFFSET_5717;
4204 mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
4205 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
4206 mac_offset = BGE_EE_MAC_OFFSET_5906;
4209 return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
4213 bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
4215 if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
4218 return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4223 bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
4225 static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
4226 /* NOTE: Order is critical */
4228 bnx_get_eaddr_nvram,
4229 bnx_get_eaddr_eeprom,
4232 const bnx_eaddr_fcn_t *func;
4234 for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
4235 if ((*func)(sc, eaddr) == 0)
4238 return (*func == NULL ? ENXIO : 0);
4242 * NOTE: 'm' is not freed upon failure
4245 bnx_defrag_shortdma(struct mbuf *m)
4251 * If device receive two back-to-back send BDs with less than
4252 * or equal to 8 total bytes then the device may hang. The two
4253 * back-to-back send BDs must in the same frame for this failure
4254 * to occur. Scan mbuf chains and see whether two back-to-back
4255 * send BDs are there. If this is the case, allocate new mbuf
4256 * and copy the frame to workaround the silicon bug.
4258 for (n = m, found = 0; n != NULL; n = n->m_next) {
4269 n = m_defrag(m, MB_DONTWAIT);
4276 bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
4280 BNX_CLRBIT(sc, reg, bit);
4281 for (i = 0; i < BNX_TIMEOUT; i++) {
4282 if ((CSR_READ_4(sc, reg) & bit) == 0)
4289 bnx_link_poll(struct bnx_softc *sc)
4293 status = CSR_READ_4(sc, BGE_MAC_STS);
4294 if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
4295 sc->bnx_link_evt = 0;
4296 sc->bnx_link_upd(sc, status);
4301 bnx_enable_msi(struct bnx_softc *sc)
4305 msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
4306 msi_mode |= BGE_MSIMODE_ENABLE;
4307 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4310 * 5718-PG105-R says that "one shot" mode
4311 * does not work if MSI is used, however,
4312 * it obviously works.
4314 msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
4316 CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
4320 bnx_dma_swap_options(struct bnx_softc *sc)
4322 uint32_t dma_options;
4324 dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
4325 BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
4326 #if BYTE_ORDER == BIG_ENDIAN
4327 dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
4329 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
4330 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
4331 dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
4332 BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
4333 BGE_MODECTL_HTX2B_ENABLE;
4339 bnx_setup_tso(struct bnx_softc *sc, struct mbuf **mp,
4340 uint16_t *mss0, uint16_t *flags0)
4345 int thoff, iphlen, hoff, hlen;
4346 uint16_t flags, mss;
4349 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4351 hoff = m->m_pkthdr.csum_lhlen;
4352 iphlen = m->m_pkthdr.csum_iphlen;
4353 thoff = m->m_pkthdr.csum_thlen;
4355 KASSERT(hoff > 0, ("invalid ether header len"));
4356 KASSERT(iphlen > 0, ("invalid ip header len"));
4357 KASSERT(thoff > 0, ("invalid tcp header len"));
4359 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4360 m = m_pullup(m, hoff + iphlen + thoff);
4367 ip = mtodoff(m, struct ip *, hoff);
4368 th = mtodoff(m, struct tcphdr *, hoff + iphlen);
4370 mss = m->m_pkthdr.tso_segsz;
4371 flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
4373 ip->ip_len = htons(mss + iphlen + thoff);
4376 hlen = (iphlen + thoff) >> 2;
4377 mss |= ((hlen & 0x3) << 14);
4378 flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);