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_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
217 static void bnx_writembx(struct bnx_softc *, int, int);
218 static int bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
219 static uint8_t bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
220 static int bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
222 static void bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
223 static void bnx_copper_link_upd(struct bnx_softc *, uint32_t);
224 static void bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
225 static void bnx_link_poll(struct bnx_softc *);
227 static int bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
228 static int bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
229 static int bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
230 static int bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
232 static void bnx_coal_change(struct bnx_softc *);
233 static int bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
234 static int bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
235 static int bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
236 static int bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
237 static int bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
238 static int bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
239 static int bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
242 static int bnx_msi_enable = 1;
243 TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
245 static device_method_t bnx_methods[] = {
246 /* Device interface */
247 DEVMETHOD(device_probe, bnx_probe),
248 DEVMETHOD(device_attach, bnx_attach),
249 DEVMETHOD(device_detach, bnx_detach),
250 DEVMETHOD(device_shutdown, bnx_shutdown),
251 DEVMETHOD(device_suspend, bnx_suspend),
252 DEVMETHOD(device_resume, bnx_resume),
255 DEVMETHOD(bus_print_child, bus_generic_print_child),
256 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
259 DEVMETHOD(miibus_readreg, bnx_miibus_readreg),
260 DEVMETHOD(miibus_writereg, bnx_miibus_writereg),
261 DEVMETHOD(miibus_statchg, bnx_miibus_statchg),
266 static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
267 static devclass_t bnx_devclass;
269 DECLARE_DUMMY_MODULE(if_bnx);
270 DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
271 DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
274 bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
276 device_t dev = sc->bnx_dev;
279 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
280 val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
281 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
286 bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
288 device_t dev = sc->bnx_dev;
290 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
291 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
292 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
296 bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
298 CSR_WRITE_4(sc, off, val);
302 bnx_writembx(struct bnx_softc *sc, int off, int val)
304 CSR_WRITE_4(sc, off, val);
308 * Read a sequence of bytes from NVRAM.
311 bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
317 * Read a byte of data stored in the EEPROM at address 'addr.' The
318 * BCM570x supports both the traditional bitbang interface and an
319 * auto access interface for reading the EEPROM. We use the auto
323 bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
329 * Enable use of auto EEPROM access so we can avoid
330 * having to use the bitbang method.
332 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
334 /* Reset the EEPROM, load the clock period. */
335 CSR_WRITE_4(sc, BGE_EE_ADDR,
336 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
339 /* Issue the read EEPROM command. */
340 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
342 /* Wait for completion */
343 for(i = 0; i < BNX_TIMEOUT * 10; i++) {
345 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
349 if (i == BNX_TIMEOUT) {
350 if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
355 byte = CSR_READ_4(sc, BGE_EE_DATA);
357 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
363 * Read a sequence of bytes from the EEPROM.
366 bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
372 for (byte = 0, err = 0, i = 0; i < len; i++) {
373 err = bnx_eeprom_getbyte(sc, off + i, &byte);
383 bnx_miibus_readreg(device_t dev, int phy, int reg)
385 struct bnx_softc *sc = device_get_softc(dev);
389 KASSERT(phy == sc->bnx_phyno,
390 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
392 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
393 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
394 CSR_WRITE_4(sc, BGE_MI_MODE,
395 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
399 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
400 BGE_MIPHY(phy) | BGE_MIREG(reg));
402 /* Poll for the PHY register access to complete. */
403 for (i = 0; i < BNX_TIMEOUT; i++) {
405 val = CSR_READ_4(sc, BGE_MI_COMM);
406 if ((val & BGE_MICOMM_BUSY) == 0) {
408 val = CSR_READ_4(sc, BGE_MI_COMM);
412 if (i == BNX_TIMEOUT) {
413 if_printf(&sc->arpcom.ac_if, "PHY read timed out "
414 "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
418 /* Restore the autopoll bit if necessary. */
419 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
420 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
424 if (val & BGE_MICOMM_READFAIL)
427 return (val & 0xFFFF);
431 bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
433 struct bnx_softc *sc = device_get_softc(dev);
436 KASSERT(phy == sc->bnx_phyno,
437 ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
439 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
440 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
441 CSR_WRITE_4(sc, BGE_MI_MODE,
442 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
446 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
447 BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
449 for (i = 0; i < BNX_TIMEOUT; i++) {
451 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
453 CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
457 if (i == BNX_TIMEOUT) {
458 if_printf(&sc->arpcom.ac_if, "PHY write timed out "
459 "(phy %d, reg %d, val %d)\n", phy, reg, val);
462 /* Restore the autopoll bit if necessary. */
463 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
464 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
472 bnx_miibus_statchg(device_t dev)
474 struct bnx_softc *sc;
475 struct mii_data *mii;
477 sc = device_get_softc(dev);
478 mii = device_get_softc(sc->bnx_miibus);
480 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
481 (IFM_ACTIVE | IFM_AVALID)) {
482 switch (IFM_SUBTYPE(mii->mii_media_active)) {
499 if (sc->bnx_link == 0)
502 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
503 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
504 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
505 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
507 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
510 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
511 BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
513 BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
518 * Memory management for jumbo frames.
521 bnx_alloc_jumbo_mem(struct bnx_softc *sc)
523 struct ifnet *ifp = &sc->arpcom.ac_if;
524 struct bnx_jslot *entry;
530 * Create tag for jumbo mbufs.
531 * This is really a bit of a kludge. We allocate a special
532 * jumbo buffer pool which (thanks to the way our DMA
533 * memory allocation works) will consist of contiguous
534 * pages. This means that even though a jumbo buffer might
535 * be larger than a page size, we don't really need to
536 * map it into more than one DMA segment. However, the
537 * default mbuf tag will result in multi-segment mappings,
538 * so we have to create a special jumbo mbuf tag that
539 * lets us get away with mapping the jumbo buffers as
540 * a single segment. I think eventually the driver should
541 * be changed so that it uses ordinary mbufs and cluster
542 * buffers, i.e. jumbo frames can span multiple DMA
543 * descriptors. But that's a project for another day.
547 * Create DMA stuffs for jumbo RX ring.
549 error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
550 &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
551 &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
552 (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
553 &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
555 if_printf(ifp, "could not create jumbo RX ring\n");
560 * Create DMA stuffs for jumbo buffer block.
562 error = bnx_dma_block_alloc(sc, BNX_JMEM,
563 &sc->bnx_cdata.bnx_jumbo_tag,
564 &sc->bnx_cdata.bnx_jumbo_map,
565 (void **)&sc->bnx_ldata.bnx_jumbo_buf,
568 if_printf(ifp, "could not create jumbo buffer\n");
572 SLIST_INIT(&sc->bnx_jfree_listhead);
575 * Now divide it up into 9K pieces and save the addresses
576 * in an array. Note that we play an evil trick here by using
577 * the first few bytes in the buffer to hold the the address
578 * of the softc structure for this interface. This is because
579 * bnx_jfree() needs it, but it is called by the mbuf management
580 * code which will not pass it to us explicitly.
582 for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
583 entry = &sc->bnx_cdata.bnx_jslots[i];
585 entry->bnx_buf = ptr;
586 entry->bnx_paddr = paddr;
587 entry->bnx_inuse = 0;
589 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
598 bnx_free_jumbo_mem(struct bnx_softc *sc)
600 /* Destroy jumbo RX ring. */
601 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
602 sc->bnx_cdata.bnx_rx_jumbo_ring_map,
603 sc->bnx_ldata.bnx_rx_jumbo_ring);
605 /* Destroy jumbo buffer block. */
606 bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
607 sc->bnx_cdata.bnx_jumbo_map,
608 sc->bnx_ldata.bnx_jumbo_buf);
612 * Allocate a jumbo buffer.
614 static struct bnx_jslot *
615 bnx_jalloc(struct bnx_softc *sc)
617 struct bnx_jslot *entry;
619 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
620 entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
622 SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
623 entry->bnx_inuse = 1;
625 if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
627 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
632 * Adjust usage count on a jumbo buffer.
637 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
638 struct bnx_softc *sc = entry->bnx_sc;
641 panic("bnx_jref: can't find softc pointer!");
643 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
644 panic("bnx_jref: asked to reference buffer "
645 "that we don't manage!");
646 } else if (entry->bnx_inuse == 0) {
647 panic("bnx_jref: buffer already free!");
649 atomic_add_int(&entry->bnx_inuse, 1);
654 * Release a jumbo buffer.
659 struct bnx_jslot *entry = (struct bnx_jslot *)arg;
660 struct bnx_softc *sc = entry->bnx_sc;
663 panic("bnx_jfree: can't find softc pointer!");
665 if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
666 panic("bnx_jfree: asked to free buffer that we don't manage!");
667 } else if (entry->bnx_inuse == 0) {
668 panic("bnx_jfree: buffer already free!");
671 * Possible MP race to 0, use the serializer. The atomic insn
672 * is still needed for races against bnx_jref().
674 lwkt_serialize_enter(&sc->bnx_jslot_serializer);
675 atomic_subtract_int(&entry->bnx_inuse, 1);
676 if (entry->bnx_inuse == 0) {
677 SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
680 lwkt_serialize_exit(&sc->bnx_jslot_serializer);
686 * Intialize a standard receive ring descriptor.
689 bnx_newbuf_std(struct bnx_softc *sc, int i, int init)
691 struct mbuf *m_new = NULL;
692 bus_dma_segment_t seg;
696 m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
699 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
700 m_adj(m_new, ETHER_ALIGN);
702 error = bus_dmamap_load_mbuf_segment(sc->bnx_cdata.bnx_rx_mtag,
703 sc->bnx_cdata.bnx_rx_tmpmap, m_new,
704 &seg, 1, &nsegs, BUS_DMA_NOWAIT);
711 bus_dmamap_sync(sc->bnx_cdata.bnx_rx_mtag,
712 sc->bnx_cdata.bnx_rx_std_dmamap[i],
713 BUS_DMASYNC_POSTREAD);
714 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
715 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
718 map = sc->bnx_cdata.bnx_rx_tmpmap;
719 sc->bnx_cdata.bnx_rx_tmpmap = sc->bnx_cdata.bnx_rx_std_dmamap[i];
720 sc->bnx_cdata.bnx_rx_std_dmamap[i] = map;
722 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_mbuf = m_new;
723 sc->bnx_cdata.bnx_rx_std_chain[i].bnx_paddr = seg.ds_addr;
725 bnx_setup_rxdesc_std(sc, i);
730 bnx_setup_rxdesc_std(struct bnx_softc *sc, int i)
732 struct bnx_rxchain *rc;
735 rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
736 r = &sc->bnx_ldata.bnx_rx_std_ring[i];
738 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
739 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
740 r->bge_len = rc->bnx_mbuf->m_len;
742 r->bge_flags = BGE_RXBDFLAG_END;
746 * Initialize a jumbo receive ring descriptor. This allocates
747 * a jumbo buffer from the pool managed internally by the driver.
750 bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
752 struct mbuf *m_new = NULL;
753 struct bnx_jslot *buf;
756 /* Allocate the mbuf. */
757 MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
761 /* Allocate the jumbo buffer */
762 buf = bnx_jalloc(sc);
768 /* Attach the buffer to the mbuf. */
769 m_new->m_ext.ext_arg = buf;
770 m_new->m_ext.ext_buf = buf->bnx_buf;
771 m_new->m_ext.ext_free = bnx_jfree;
772 m_new->m_ext.ext_ref = bnx_jref;
773 m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
775 m_new->m_flags |= M_EXT;
777 m_new->m_data = m_new->m_ext.ext_buf;
778 m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
780 paddr = buf->bnx_paddr;
781 m_adj(m_new, ETHER_ALIGN);
782 paddr += ETHER_ALIGN;
784 /* Save necessary information */
785 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_mbuf = m_new;
786 sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_paddr = paddr;
788 /* Set up the descriptor. */
789 bnx_setup_rxdesc_jumbo(sc, i);
794 bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
797 struct bnx_rxchain *rc;
799 r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
800 rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
802 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
803 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
804 r->bge_len = rc->bnx_mbuf->m_len;
806 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
810 bnx_init_rx_ring_std(struct bnx_softc *sc)
814 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
815 error = bnx_newbuf_std(sc, i, 1);
820 sc->bnx_std = BGE_STD_RX_RING_CNT - 1;
821 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
827 bnx_free_rx_ring_std(struct bnx_softc *sc)
831 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
832 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
834 if (rc->bnx_mbuf != NULL) {
835 bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
836 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
837 m_freem(rc->bnx_mbuf);
840 bzero(&sc->bnx_ldata.bnx_rx_std_ring[i],
841 sizeof(struct bge_rx_bd));
846 bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
851 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
852 error = bnx_newbuf_jumbo(sc, i, 1);
857 sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
859 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
860 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
861 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
863 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
869 bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
873 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
874 struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
876 if (rc->bnx_mbuf != NULL) {
877 m_freem(rc->bnx_mbuf);
880 bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
881 sizeof(struct bge_rx_bd));
886 bnx_free_tx_ring(struct bnx_softc *sc)
890 for (i = 0; i < BGE_TX_RING_CNT; i++) {
891 if (sc->bnx_cdata.bnx_tx_chain[i] != NULL) {
892 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
893 sc->bnx_cdata.bnx_tx_dmamap[i]);
894 m_freem(sc->bnx_cdata.bnx_tx_chain[i]);
895 sc->bnx_cdata.bnx_tx_chain[i] = NULL;
897 bzero(&sc->bnx_ldata.bnx_tx_ring[i],
898 sizeof(struct bge_tx_bd));
903 bnx_init_tx_ring(struct bnx_softc *sc)
906 sc->bnx_tx_saved_considx = 0;
907 sc->bnx_tx_prodidx = 0;
909 /* Initialize transmit producer index for host-memory send ring. */
910 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bnx_tx_prodidx);
911 bnx_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
917 bnx_setmulti(struct bnx_softc *sc)
920 struct ifmultiaddr *ifma;
921 uint32_t hashes[4] = { 0, 0, 0, 0 };
924 ifp = &sc->arpcom.ac_if;
926 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
927 for (i = 0; i < 4; i++)
928 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
932 /* First, zot all the existing filters. */
933 for (i = 0; i < 4; i++)
934 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
936 /* Now program new ones. */
937 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
938 if (ifma->ifma_addr->sa_family != AF_LINK)
941 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
942 ETHER_ADDR_LEN) & 0x7f;
943 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
946 for (i = 0; i < 4; i++)
947 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
951 * Do endian, PCI and DMA initialization. Also check the on-board ROM
955 bnx_chipinit(struct bnx_softc *sc)
957 uint32_t dma_rw_ctl, mode_ctl;
960 /* Set endian type before we access any non-PCI registers. */
961 pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
962 BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
964 /* Clear the MAC control register */
965 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
968 * Clear the MAC statistics block in the NIC's
971 for (i = BGE_STATS_BLOCK;
972 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
973 BNX_MEMWIN_WRITE(sc, i, 0);
975 for (i = BGE_STATUS_BLOCK;
976 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
977 BNX_MEMWIN_WRITE(sc, i, 0);
979 if (BNX_IS_57765_FAMILY(sc)) {
982 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
983 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
984 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
986 /* Access the lower 1K of PL PCI-E block registers. */
987 CSR_WRITE_4(sc, BGE_MODE_CTL,
988 val | BGE_MODECTL_PCIE_PL_SEL);
990 val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
991 val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
992 CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
994 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
996 if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
997 /* Fix transmit hangs */
998 val = CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL);
999 val |= BGE_CPMU_PADRNG_CTL_RDIV2;
1000 CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL, val);
1002 mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1003 val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1005 /* Access the lower 1K of DL PCI-E block registers. */
1006 CSR_WRITE_4(sc, BGE_MODE_CTL,
1007 val | BGE_MODECTL_PCIE_DL_SEL);
1009 val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1010 val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1011 val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1012 CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1014 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1017 val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1018 val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1019 val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1020 CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1024 * Set up the PCI DMA control register.
1026 dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
1028 * Disable 32bytes cache alignment for DMA write to host memory
1031 * 64bytes cache alignment for DMA write to host memory is still
1034 dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1035 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
1036 dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1038 * Enable HW workaround for controllers that misinterpret
1039 * a status tag update and leave interrupts permanently
1042 if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1043 sc->bnx_asicrev != BGE_ASICREV_BCM5762 &&
1044 !BNX_IS_57765_FAMILY(sc))
1045 dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1047 if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
1050 pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1053 * Set up general mode register.
1055 mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
1056 BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
1057 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1060 * Disable memory write invalidate. Apparently it is not supported
1061 * properly by these devices. Also ensure that INTx isn't disabled,
1062 * as these chips need it even when using MSI.
1064 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
1065 (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
1067 /* Set the timer prescaler (always 66Mhz) */
1068 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1074 bnx_blockinit(struct bnx_softc *sc)
1076 struct bge_rcb *rcb;
1083 * Initialize the memory window pointer register so that
1084 * we can access the first 32K of internal NIC RAM. This will
1085 * allow us to set up the TX send ring RCBs and the RX return
1086 * ring RCBs, plus other things which live in NIC memory.
1088 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1090 /* Configure mbuf pool watermarks */
1091 if (BNX_IS_57765_PLUS(sc)) {
1092 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1093 if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
1094 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
1095 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
1097 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
1098 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
1101 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1102 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1103 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1106 /* Configure DMA resource watermarks */
1107 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1108 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1110 /* Enable buffer manager */
1111 val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1113 * Change the arbitration algorithm of TXMBUF read request to
1114 * round-robin instead of priority based for BCM5719. When
1115 * TXFIFO is almost empty, RDMA will hold its request until
1116 * TXFIFO is not almost empty.
1118 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
1119 val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1120 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1121 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1122 sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1123 val |= BGE_BMANMODE_LOMBUF_ATTN;
1124 CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
1126 /* Poll for buffer manager start indication */
1127 for (i = 0; i < BNX_TIMEOUT; i++) {
1128 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1133 if (i == BNX_TIMEOUT) {
1134 if_printf(&sc->arpcom.ac_if,
1135 "buffer manager failed to start\n");
1139 /* Enable flow-through queues */
1140 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1141 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1143 /* Wait until queue initialization is complete */
1144 for (i = 0; i < BNX_TIMEOUT; i++) {
1145 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1150 if (i == BNX_TIMEOUT) {
1151 if_printf(&sc->arpcom.ac_if,
1152 "flow-through queue init failed\n");
1157 * Summary of rings supported by the controller:
1159 * Standard Receive Producer Ring
1160 * - This ring is used to feed receive buffers for "standard"
1161 * sized frames (typically 1536 bytes) to the controller.
1163 * Jumbo Receive Producer Ring
1164 * - This ring is used to feed receive buffers for jumbo sized
1165 * frames (i.e. anything bigger than the "standard" frames)
1166 * to the controller.
1168 * Mini Receive Producer Ring
1169 * - This ring is used to feed receive buffers for "mini"
1170 * sized frames to the controller.
1171 * - This feature required external memory for the controller
1172 * but was never used in a production system. Should always
1175 * Receive Return Ring
1176 * - After the controller has placed an incoming frame into a
1177 * receive buffer that buffer is moved into a receive return
1178 * ring. The driver is then responsible to passing the
1179 * buffer up to the stack. Many versions of the controller
1180 * support multiple RR rings.
1183 * - This ring is used for outgoing frames. Many versions of
1184 * the controller support multiple send rings.
1187 /* Initialize the standard receive producer ring control block. */
1188 rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
1189 rcb->bge_hostaddr.bge_addr_lo =
1190 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1191 rcb->bge_hostaddr.bge_addr_hi =
1192 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1193 if (BNX_IS_57765_PLUS(sc)) {
1195 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
1196 * Bits 15-2 : Maximum RX frame size
1197 * Bit 1 : 1 = Ring Disabled, 0 = Ring ENabled
1200 rcb->bge_maxlen_flags =
1201 BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
1204 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
1205 * Bits 15-2 : Reserved (should be 0)
1206 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
1209 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1211 if (BNX_IS_5717_PLUS(sc))
1212 rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
1214 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1215 /* Write the standard receive producer ring control block. */
1216 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1217 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1218 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1219 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1220 /* Reset the standard receive producer ring producer index. */
1221 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1224 * Initialize the jumbo RX producer ring control
1225 * block. We set the 'ring disabled' bit in the
1226 * flags field until we're actually ready to start
1227 * using this ring (i.e. once we set the MTU
1228 * high enough to require it).
1230 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1231 rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
1232 /* Get the jumbo receive producer ring RCB parameters. */
1233 rcb->bge_hostaddr.bge_addr_lo =
1234 BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1235 rcb->bge_hostaddr.bge_addr_hi =
1236 BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
1237 rcb->bge_maxlen_flags =
1238 BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
1239 BGE_RCB_FLAG_RING_DISABLED);
1240 if (BNX_IS_5717_PLUS(sc))
1241 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
1243 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1244 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1245 rcb->bge_hostaddr.bge_addr_hi);
1246 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1247 rcb->bge_hostaddr.bge_addr_lo);
1248 /* Program the jumbo receive producer ring RCB parameters. */
1249 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1250 rcb->bge_maxlen_flags);
1251 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1252 /* Reset the jumbo receive producer ring producer index. */
1253 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1257 * The BD ring replenish thresholds control how often the
1258 * hardware fetches new BD's from the producer rings in host
1259 * memory. Setting the value too low on a busy system can
1260 * starve the hardware and recue the throughpout.
1262 * Set the BD ring replentish thresholds. The recommended
1263 * values are 1/8th the number of descriptors allocated to
1267 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
1268 if (BNX_IS_JUMBO_CAPABLE(sc)) {
1269 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
1270 BGE_JUMBO_RX_RING_CNT/8);
1272 if (BNX_IS_57765_PLUS(sc)) {
1273 CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
1274 CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
1278 * Disable all send rings by setting the 'ring disabled' bit
1279 * in the flags field of all the TX send ring control blocks,
1280 * located in NIC memory.
1282 if (BNX_IS_5717_PLUS(sc))
1284 else if (BNX_IS_57765_FAMILY(sc) ||
1285 sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1289 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1290 for (i = 0; i < limit; i++) {
1291 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1292 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1293 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1294 vrcb += sizeof(struct bge_rcb);
1297 /* Configure send ring RCB 0 (we use only the first ring) */
1298 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1299 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_tx_ring_paddr);
1300 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1301 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1302 if (BNX_IS_5717_PLUS(sc)) {
1303 RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
1305 RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1306 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1308 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1309 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1312 * Disable all receive return rings by setting the
1313 * 'ring disabled' bit in the flags field of all the receive
1314 * return ring control blocks, located in NIC memory.
1316 if (BNX_IS_5717_PLUS(sc)) {
1317 /* Should be 17, use 16 until we get an SRAM map. */
1319 } else if (BNX_IS_57765_FAMILY(sc) ||
1320 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1325 /* Disable all receive return rings. */
1326 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1327 for (i = 0; i < limit; i++) {
1328 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1329 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1330 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1331 BGE_RCB_FLAG_RING_DISABLED);
1332 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1333 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
1334 (i * (sizeof(uint64_t))), 0);
1335 vrcb += sizeof(struct bge_rcb);
1339 * Set up receive return ring 0. Note that the NIC address
1340 * for RX return rings is 0x0. The return rings live entirely
1341 * within the host, so the nicaddr field in the RCB isn't used.
1343 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1344 BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_rx_return_ring_paddr);
1345 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1346 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1347 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1348 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1349 BGE_RCB_MAXLEN_FLAGS(BNX_RETURN_RING_CNT, 0));
1351 /* Set random backoff seed for TX */
1352 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1353 sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
1354 sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
1355 sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
1356 BGE_TX_BACKOFF_SEED_MASK);
1358 /* Set inter-packet gap */
1360 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1361 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1362 val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
1363 (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
1365 CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
1368 * Specify which ring to use for packets that don't match
1371 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1374 * Configure number of RX lists. One interrupt distribution
1375 * list, sixteen active lists, one bad frames class.
1377 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1379 /* Inialize RX list placement stats mask. */
1380 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1381 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1383 /* Disable host coalescing until we get it set up */
1384 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1386 /* Poll to make sure it's shut down. */
1387 for (i = 0; i < BNX_TIMEOUT; i++) {
1388 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1393 if (i == BNX_TIMEOUT) {
1394 if_printf(&sc->arpcom.ac_if,
1395 "host coalescing engine failed to idle\n");
1399 /* Set up host coalescing defaults */
1400 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bnx_rx_coal_ticks);
1401 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bnx_tx_coal_ticks);
1402 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bnx_rx_coal_bds);
1403 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bnx_tx_coal_bds);
1404 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, sc->bnx_rx_coal_bds_int);
1405 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, sc->bnx_tx_coal_bds_int);
1407 /* Set up address of status block */
1408 bzero(sc->bnx_ldata.bnx_status_block, BGE_STATUS_BLK_SZ);
1409 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1410 BGE_ADDR_HI(sc->bnx_ldata.bnx_status_block_paddr));
1411 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1412 BGE_ADDR_LO(sc->bnx_ldata.bnx_status_block_paddr));
1414 /* Set up status block partail update size. */
1415 val = BGE_STATBLKSZ_32BYTE;
1418 * Does not seem to have visible effect in both
1419 * bulk data (1472B UDP datagram) and tiny data
1420 * (18B UDP datagram) TX tests.
1422 val |= BGE_HCCMODE_CLRTICK_TX;
1424 /* Turn on host coalescing state machine */
1425 CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
1427 /* Turn on RX BD completion state machine and enable attentions */
1428 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1429 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1431 /* Turn on RX list placement state machine */
1432 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1434 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1435 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1436 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1437 BGE_MACMODE_FRMHDR_DMA_ENB;
1439 if (sc->bnx_flags & BNX_FLAG_TBI)
1440 val |= BGE_PORTMODE_TBI;
1441 else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
1442 val |= BGE_PORTMODE_GMII;
1444 val |= BGE_PORTMODE_MII;
1446 /* Turn on DMA, clear stats */
1447 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
1449 /* Set misc. local control, enable interrupts on attentions */
1450 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1453 /* Assert GPIO pins for PHY reset */
1454 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1455 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1456 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1457 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1460 /* Turn on write DMA state machine */
1461 val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
1462 /* Enable host coalescing bug fix. */
1463 val |= BGE_WDMAMODE_STATUS_TAG_FIX;
1464 if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
1465 /* Request larger DMA burst size to get better performance. */
1466 val |= BGE_WDMAMODE_BURST_ALL_DATA;
1468 CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
1471 if (BNX_IS_57765_PLUS(sc)) {
1472 uint32_t dmactl, dmactl_reg;
1474 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1475 dmactl_reg = BGE_RDMA_RSRVCTRL2;
1477 dmactl_reg = BGE_RDMA_RSRVCTRL;
1479 dmactl = CSR_READ_4(sc, dmactl_reg);
1481 * Adjust tx margin to prevent TX data corruption and
1482 * fix internal FIFO overflow.
1484 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1485 sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1486 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1487 dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
1488 BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
1489 BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
1490 dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
1491 BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
1492 BGE_RDMA_RSRVCTRL_TXMRGN_320B;
1495 * Enable fix for read DMA FIFO overruns.
1496 * The fix is to limit the number of RX BDs
1497 * the hardware would fetch at a fime.
1499 CSR_WRITE_4(sc, dmactl_reg,
1500 dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1503 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
1504 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
1505 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
1506 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
1507 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1508 } else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1509 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1512 if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1513 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL2;
1515 ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL;
1518 * Allow 4KB burst length reads for non-LSO frames.
1519 * Enable 512B burst length reads for buffer descriptors.
1521 CSR_WRITE_4(sc, ctrl_reg,
1522 CSR_READ_4(sc, ctrl_reg) |
1523 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
1524 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1527 /* Turn on read DMA state machine */
1528 val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1529 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
1530 val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
1531 if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
1532 sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
1533 sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
1534 val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1535 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1536 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
1538 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1539 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1540 val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
1541 BGE_RDMAMODE_H2BNC_VLAN_DET;
1543 * Allow multiple outstanding read requests from
1544 * non-LSO read DMA engine.
1546 val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
1548 if (sc->bnx_asicrev == BGE_ASICREV_BCM57766)
1549 val |= BGE_RDMAMODE_JMB_2K_MMRR;
1550 if (sc->bnx_flags & BNX_FLAG_TSO)
1551 val |= BGE_RDMAMODE_TSO4_ENABLE;
1552 val |= BGE_RDMAMODE_FIFO_LONG_BURST;
1553 CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
1556 /* Turn on RX data completion state machine */
1557 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1559 /* Turn on RX BD initiator state machine */
1560 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1562 /* Turn on RX data and RX BD initiator state machine */
1563 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1565 /* Turn on send BD completion state machine */
1566 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1568 /* Turn on send data completion state machine */
1569 val = BGE_SDCMODE_ENABLE;
1570 if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
1571 val |= BGE_SDCMODE_CDELAY;
1572 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
1574 /* Turn on send data initiator state machine */
1575 if (sc->bnx_flags & BNX_FLAG_TSO) {
1576 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
1577 BGE_SDIMODE_HW_LSO_PRE_DMA);
1579 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1582 /* Turn on send BD initiator state machine */
1583 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1585 /* Turn on send BD selector state machine */
1586 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1588 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1589 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1590 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1592 /* ack/clear link change events */
1593 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1594 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1595 BGE_MACSTAT_LINK_CHANGED);
1596 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1599 * Enable attention when the link has changed state for
1600 * devices that use auto polling.
1602 if (sc->bnx_flags & BNX_FLAG_TBI) {
1603 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1605 if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
1606 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
1612 * Clear any pending link state attention.
1613 * Otherwise some link state change events may be lost until attention
1614 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
1615 * It's not necessary on newer BCM chips - perhaps enabling link
1616 * state change attentions implies clearing pending attention.
1618 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1619 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1620 BGE_MACSTAT_LINK_CHANGED);
1622 /* Enable link state change attentions. */
1623 BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1629 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1630 * against our list and return its name if we find a match. Note
1631 * that since the Broadcom controller contains VPD support, we
1632 * can get the device name string from the controller itself instead
1633 * of the compiled-in string. This is a little slow, but it guarantees
1634 * we'll always announce the right product name.
1637 bnx_probe(device_t dev)
1639 const struct bnx_type *t;
1640 uint16_t product, vendor;
1642 if (!pci_is_pcie(dev))
1645 product = pci_get_device(dev);
1646 vendor = pci_get_vendor(dev);
1648 for (t = bnx_devs; t->bnx_name != NULL; t++) {
1649 if (vendor == t->bnx_vid && product == t->bnx_did)
1652 if (t->bnx_name == NULL)
1655 device_set_desc(dev, t->bnx_name);
1660 bnx_attach(device_t dev)
1663 struct bnx_softc *sc;
1665 int error = 0, rid, capmask;
1666 uint8_t ether_addr[ETHER_ADDR_LEN];
1668 driver_intr_t *intr_func;
1669 uintptr_t mii_priv = 0;
1671 #ifdef BNX_TSO_DEBUG
1676 sc = device_get_softc(dev);
1678 callout_init_mp(&sc->bnx_stat_timer);
1679 callout_init_mp(&sc->bnx_intr_timer);
1680 lwkt_serialize_init(&sc->bnx_jslot_serializer);
1682 product = pci_get_device(dev);
1684 #ifndef BURN_BRIDGES
1685 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1688 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1689 mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
1691 device_printf(dev, "chip is in D%d power mode "
1692 "-- setting to D0\n", pci_get_powerstate(dev));
1694 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1696 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1697 pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
1699 #endif /* !BURN_BRIDGE */
1702 * Map control/status registers.
1704 pci_enable_busmaster(dev);
1707 sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1710 if (sc->bnx_res == NULL) {
1711 device_printf(dev, "couldn't map memory\n");
1715 sc->bnx_btag = rman_get_bustag(sc->bnx_res);
1716 sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
1718 /* Save various chip information */
1720 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
1721 BGE_PCIMISCCTL_ASICREV_SHIFT;
1722 if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
1723 /* All chips having dedicated ASICREV register have CPMU */
1724 sc->bnx_flags |= BNX_FLAG_CPMU;
1727 case PCI_PRODUCT_BROADCOM_BCM5717:
1728 case PCI_PRODUCT_BROADCOM_BCM5717C:
1729 case PCI_PRODUCT_BROADCOM_BCM5718:
1730 case PCI_PRODUCT_BROADCOM_BCM5719:
1731 case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1732 case PCI_PRODUCT_BROADCOM_BCM5725:
1733 case PCI_PRODUCT_BROADCOM_BCM5727:
1734 case PCI_PRODUCT_BROADCOM_BCM5762:
1735 sc->bnx_chipid = pci_read_config(dev,
1736 BGE_PCI_GEN2_PRODID_ASICREV, 4);
1739 case PCI_PRODUCT_BROADCOM_BCM57761:
1740 case PCI_PRODUCT_BROADCOM_BCM57762:
1741 case PCI_PRODUCT_BROADCOM_BCM57765:
1742 case PCI_PRODUCT_BROADCOM_BCM57766:
1743 case PCI_PRODUCT_BROADCOM_BCM57781:
1744 case PCI_PRODUCT_BROADCOM_BCM57782:
1745 case PCI_PRODUCT_BROADCOM_BCM57785:
1746 case PCI_PRODUCT_BROADCOM_BCM57786:
1747 case PCI_PRODUCT_BROADCOM_BCM57791:
1748 case PCI_PRODUCT_BROADCOM_BCM57795:
1749 sc->bnx_chipid = pci_read_config(dev,
1750 BGE_PCI_GEN15_PRODID_ASICREV, 4);
1754 sc->bnx_chipid = pci_read_config(dev,
1755 BGE_PCI_PRODID_ASICREV, 4);
1759 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1760 sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1762 sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
1763 sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
1765 switch (sc->bnx_asicrev) {
1766 case BGE_ASICREV_BCM5717:
1767 case BGE_ASICREV_BCM5719:
1768 case BGE_ASICREV_BCM5720:
1769 sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1772 case BGE_ASICREV_BCM5762:
1773 sc->bnx_flags |= BNX_FLAG_57765_PLUS;
1776 case BGE_ASICREV_BCM57765:
1777 case BGE_ASICREV_BCM57766:
1778 sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
1781 sc->bnx_flags |= BNX_FLAG_SHORTDMA;
1783 sc->bnx_flags |= BNX_FLAG_TSO;
1784 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
1785 sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
1786 sc->bnx_flags &= ~BNX_FLAG_TSO;
1788 if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1789 BNX_IS_57765_FAMILY(sc)) {
1791 * All BCM57785 and BCM5718 families chips have a bug that
1792 * under certain situation interrupt will not be enabled
1793 * even if status tag is written to BGE_MBX_IRQ0_LO mailbox.
1795 * While BCM5719 and BCM5720 have a hardware workaround
1796 * which could fix the above bug.
1797 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1800 * For the rest of the chips in these two families, we will
1801 * have to poll the status block at high rate (10ms currently)
1802 * to check whether the interrupt is hosed or not.
1803 * See bnx_intr_check() for details.
1805 sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1808 sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
1809 if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1810 sc->bnx_asicrev == BGE_ASICREV_BCM5720)
1811 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
1813 pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
1814 device_printf(dev, "CHIP ID 0x%08x; "
1815 "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
1816 sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
1819 * Set various PHY quirk flags.
1822 capmask = MII_CAPMASK_DEFAULT;
1823 if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
1824 product == PCI_PRODUCT_BROADCOM_BCM57795) {
1826 capmask &= ~BMSR_EXTSTAT;
1829 mii_priv |= BRGPHY_FLAG_WIRESPEED;
1830 if (sc->bnx_chipid == BGE_CHIPID_BCM5762_A0)
1831 mii_priv |= BRGPHY_FLAG_5762_A0;
1833 /* Initialize if_name earlier, so if_printf could be used */
1834 ifp = &sc->arpcom.ac_if;
1835 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1837 /* Try to reset the chip. */
1840 if (bnx_chipinit(sc)) {
1841 device_printf(dev, "chip initialization failed\n");
1847 * Get station address
1849 error = bnx_get_eaddr(sc, ether_addr);
1851 device_printf(dev, "failed to read station address\n");
1855 error = bnx_dma_alloc(sc);
1860 * Allocate interrupt
1862 sc->bnx_irq_type = pci_alloc_1intr(dev, bnx_msi_enable, &sc->bnx_irq_rid,
1865 sc->bnx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->bnx_irq_rid,
1867 if (sc->bnx_irq == NULL) {
1868 device_printf(dev, "couldn't map interrupt\n");
1873 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
1874 sc->bnx_flags |= BNX_FLAG_ONESHOT_MSI;
1878 /* Set default tuneable values. */
1879 sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
1880 sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
1881 sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
1882 sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
1883 sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
1884 sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
1885 sc->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
1887 /* Set up ifnet structure */
1889 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1890 ifp->if_ioctl = bnx_ioctl;
1891 ifp->if_start = bnx_start;
1892 #ifdef IFPOLL_ENABLE
1893 ifp->if_npoll = bnx_npoll;
1895 ifp->if_watchdog = bnx_watchdog;
1896 ifp->if_init = bnx_init;
1897 ifp->if_mtu = ETHERMTU;
1898 ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1899 ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
1900 ifq_set_ready(&ifp->if_snd);
1902 ifp->if_capabilities |= IFCAP_HWCSUM;
1903 ifp->if_hwassist = BNX_CSUM_FEATURES;
1904 if (sc->bnx_flags & BNX_FLAG_TSO) {
1905 ifp->if_capabilities |= IFCAP_TSO;
1906 ifp->if_hwassist |= CSUM_TSO;
1908 ifp->if_capenable = ifp->if_capabilities;
1911 * Figure out what sort of media we have by checking the
1912 * hardware config word in the first 32k of NIC internal memory,
1913 * or fall back to examining the EEPROM if necessary.
1914 * Note: on some BCM5700 cards, this value appears to be unset.
1915 * If that's the case, we have to rely on identifying the NIC
1916 * by its PCI subsystem ID, as we do below for the SysKonnect
1919 if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
1920 hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
1922 if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
1924 device_printf(dev, "failed to read EEPROM\n");
1928 hwcfg = ntohl(hwcfg);
1931 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
1932 if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
1933 (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
1934 sc->bnx_flags |= BNX_FLAG_TBI;
1937 if (sc->bnx_flags & BNX_FLAG_CPMU)
1938 sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
1940 sc->bnx_mi_mode = BGE_MIMODE_BASE;
1942 /* Setup link status update stuffs */
1943 if (sc->bnx_flags & BNX_FLAG_TBI) {
1944 sc->bnx_link_upd = bnx_tbi_link_upd;
1945 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
1946 } else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
1947 sc->bnx_link_upd = bnx_autopoll_link_upd;
1948 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
1950 sc->bnx_link_upd = bnx_copper_link_upd;
1951 sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
1954 /* Set default PHY address */
1958 * PHY address mapping for various devices.
1960 * | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
1961 * ---------+-------+-------+-------+-------+
1962 * BCM57XX | 1 | X | X | X |
1963 * BCM5704 | 1 | X | 1 | X |
1964 * BCM5717 | 1 | 8 | 2 | 9 |
1965 * BCM5719 | 1 | 8 | 2 | 9 |
1966 * BCM5720 | 1 | 8 | 2 | 9 |
1968 * Other addresses may respond but they are not
1969 * IEEE compliant PHYs and should be ignored.
1971 if (BNX_IS_5717_PLUS(sc)) {
1974 f = pci_get_function(dev);
1975 if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
1976 if (CSR_READ_4(sc, BGE_SGDIG_STS) &
1977 BGE_SGDIGSTS_IS_SERDES)
1978 sc->bnx_phyno = f + 8;
1980 sc->bnx_phyno = f + 1;
1982 if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
1983 BGE_CPMU_PHY_STRAP_IS_SERDES)
1984 sc->bnx_phyno = f + 8;
1986 sc->bnx_phyno = f + 1;
1990 if (sc->bnx_flags & BNX_FLAG_TBI) {
1991 ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
1992 bnx_ifmedia_upd, bnx_ifmedia_sts);
1993 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
1994 ifmedia_add(&sc->bnx_ifmedia,
1995 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
1996 ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
1997 ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
1998 sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
2000 struct mii_probe_args mii_args;
2002 mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
2003 mii_args.mii_probemask = 1 << sc->bnx_phyno;
2004 mii_args.mii_capmask = capmask;
2005 mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
2006 mii_args.mii_priv = mii_priv;
2008 error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
2010 device_printf(dev, "MII without any PHY!\n");
2016 * Create sysctl nodes.
2018 sysctl_ctx_init(&sc->bnx_sysctl_ctx);
2019 sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
2020 SYSCTL_STATIC_CHILDREN(_hw),
2022 device_get_nameunit(dev),
2024 if (sc->bnx_sysctl_tree == NULL) {
2025 device_printf(dev, "can't add sysctl node\n");
2030 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2031 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2032 OID_AUTO, "rx_coal_ticks",
2033 CTLTYPE_INT | CTLFLAG_RW,
2034 sc, 0, bnx_sysctl_rx_coal_ticks, "I",
2035 "Receive coalescing ticks (usec).");
2036 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2037 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2038 OID_AUTO, "tx_coal_ticks",
2039 CTLTYPE_INT | CTLFLAG_RW,
2040 sc, 0, bnx_sysctl_tx_coal_ticks, "I",
2041 "Transmit coalescing ticks (usec).");
2042 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2043 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2044 OID_AUTO, "rx_coal_bds",
2045 CTLTYPE_INT | CTLFLAG_RW,
2046 sc, 0, bnx_sysctl_rx_coal_bds, "I",
2047 "Receive max coalesced BD count.");
2048 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2049 SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
2050 OID_AUTO, "tx_coal_bds",
2051 CTLTYPE_INT | CTLFLAG_RW,
2052 sc, 0, bnx_sysctl_tx_coal_bds, "I",
2053 "Transmit max coalesced BD count.");
2055 * A common design characteristic for many Broadcom
2056 * client controllers is that they only support a
2057 * single outstanding DMA read operation on the PCIe
2058 * bus. This means that it will take twice as long to
2059 * fetch a TX frame that is split into header and
2060 * payload buffers as it does to fetch a single,
2061 * contiguous TX frame (2 reads vs. 1 read). For these
2062 * controllers, coalescing buffers to reduce the number
2063 * of memory reads is effective way to get maximum
2064 * performance(about 940Mbps). Without collapsing TX
2065 * buffers the maximum TCP bulk transfer performance
2066 * is about 850Mbps. However forcing coalescing mbufs
2067 * consumes a lot of CPU cycles, so leave it off by
2070 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2071 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2072 "force_defrag", CTLFLAG_RW, &sc->bnx_force_defrag, 0,
2073 "Force defragment on TX path");
2075 SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
2076 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2077 "tx_wreg", CTLFLAG_RW, &sc->bnx_tx_wreg, 0,
2078 "# of segments before writing to hardware register");
2080 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2081 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2082 "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2083 sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
2084 "Receive max coalesced BD count during interrupt.");
2085 SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2086 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2087 "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
2088 sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
2089 "Transmit max coalesced BD count during interrupt.");
2091 #ifdef BNX_TSO_DEBUG
2092 for (i = 0; i < BNX_TSO_NSTATS; ++i) {
2093 ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
2094 SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
2095 SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2096 desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
2101 * Call MI attach routine.
2103 ether_ifattach(ifp, ether_addr, NULL);
2105 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2107 #ifdef IFPOLL_ENABLE
2108 ifpoll_compat_setup(&sc->bnx_npoll,
2109 &sc->bnx_sysctl_ctx, sc->bnx_sysctl_tree,
2110 device_get_unit(dev), ifp->if_serializer);
2113 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
2114 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
2115 intr_func = bnx_msi_oneshot;
2117 device_printf(dev, "oneshot MSI\n");
2119 intr_func = bnx_msi;
2122 intr_func = bnx_intr_legacy;
2124 error = bus_setup_intr(dev, sc->bnx_irq, INTR_MPSAFE, intr_func, sc,
2125 &sc->bnx_intrhand, ifp->if_serializer);
2127 ether_ifdetach(ifp);
2128 device_printf(dev, "couldn't set up irq\n");
2132 sc->bnx_intr_cpuid = rman_get_cpuid(sc->bnx_irq);
2133 sc->bnx_stat_cpuid = sc->bnx_intr_cpuid;
2142 bnx_detach(device_t dev)
2144 struct bnx_softc *sc = device_get_softc(dev);
2146 if (device_is_attached(dev)) {
2147 struct ifnet *ifp = &sc->arpcom.ac_if;
2149 lwkt_serialize_enter(ifp->if_serializer);
2152 bus_teardown_intr(dev, sc->bnx_irq, sc->bnx_intrhand);
2153 lwkt_serialize_exit(ifp->if_serializer);
2155 ether_ifdetach(ifp);
2158 if (sc->bnx_flags & BNX_FLAG_TBI)
2159 ifmedia_removeall(&sc->bnx_ifmedia);
2161 device_delete_child(dev, sc->bnx_miibus);
2162 bus_generic_detach(dev);
2164 if (sc->bnx_irq != NULL) {
2165 bus_release_resource(dev, SYS_RES_IRQ, sc->bnx_irq_rid,
2168 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI)
2169 pci_release_msi(dev);
2171 if (sc->bnx_res != NULL) {
2172 bus_release_resource(dev, SYS_RES_MEMORY,
2173 BGE_PCI_BAR0, sc->bnx_res);
2176 if (sc->bnx_sysctl_tree != NULL)
2177 sysctl_ctx_free(&sc->bnx_sysctl_ctx);
2185 bnx_reset(struct bnx_softc *sc)
2188 uint32_t cachesize, command, pcistate, reset;
2189 void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
2195 write_op = bnx_writemem_direct;
2197 /* Save some important PCI state. */
2198 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2199 command = pci_read_config(dev, BGE_PCI_CMD, 4);
2200 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2202 pci_write_config(dev, BGE_PCI_MISC_CTL,
2203 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2204 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2205 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2207 /* Disable fastboot on controllers that support it. */
2209 if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
2210 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
2213 * Write the magic number to SRAM at offset 0xB50.
2214 * When firmware finishes its initialization it will
2215 * write ~BGE_MAGIC_NUMBER to the same location.
2217 bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2219 reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2221 /* XXX: Broadcom Linux driver. */
2222 /* Force PCI-E 1.0a mode */
2223 if (!BNX_IS_57765_PLUS(sc) &&
2224 CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
2225 (BGE_PCIE_PHY_TSTCTL_PSCRAM |
2226 BGE_PCIE_PHY_TSTCTL_PCIE10)) {
2227 CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
2228 BGE_PCIE_PHY_TSTCTL_PSCRAM);
2230 if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
2231 /* Prevent PCIE link training during global reset */
2232 CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2237 * Set GPHY Power Down Override to leave GPHY
2238 * powered up in D0 uninitialized.
2240 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
2241 reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
2243 /* Issue global reset */
2244 write_op(sc, BGE_MISC_CFG, reset);
2248 /* XXX: Broadcom Linux driver. */
2249 if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
2252 DELAY(500000); /* wait for link training to complete */
2253 v = pci_read_config(dev, 0xc4, 4);
2254 pci_write_config(dev, 0xc4, v | (1<<15), 4);
2257 devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
2259 /* Disable no snoop and disable relaxed ordering. */
2260 devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
2262 /* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
2263 if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
2264 devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
2265 devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
2268 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
2271 /* Clear error status. */
2272 pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
2273 PCIEM_DEVSTS_CORR_ERR |
2274 PCIEM_DEVSTS_NFATAL_ERR |
2275 PCIEM_DEVSTS_FATAL_ERR |
2276 PCIEM_DEVSTS_UNSUPP_REQ, 2);
2278 /* Reset some of the PCI state that got zapped by reset */
2279 pci_write_config(dev, BGE_PCI_MISC_CTL,
2280 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2281 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
2282 BGE_PCIMISCCTL_TAGGED_STATUS, 4);
2283 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2284 pci_write_config(dev, BGE_PCI_CMD, command, 4);
2285 write_op(sc, BGE_MISC_CFG, (65 << 1));
2287 /* Enable memory arbiter */
2288 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2291 * Poll until we see the 1's complement of the magic number.
2292 * This indicates that the firmware initialization is complete.
2294 for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
2295 val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2296 if (val == ~BGE_MAGIC_NUMBER)
2300 if (i == BNX_FIRMWARE_TIMEOUT) {
2301 if_printf(&sc->arpcom.ac_if, "firmware handshake "
2302 "timed out, found 0x%08x\n", val);
2305 /* BCM57765 A0 needs additional time before accessing. */
2306 if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
2310 * XXX Wait for the value of the PCISTATE register to
2311 * return to its original pre-reset state. This is a
2312 * fairly good indicator of reset completion. If we don't
2313 * wait for the reset to fully complete, trying to read
2314 * from the device's non-PCI registers may yield garbage
2317 for (i = 0; i < BNX_TIMEOUT; i++) {
2318 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2323 /* Fix up byte swapping */
2324 CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
2326 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2329 * The 5704 in TBI mode apparently needs some special
2330 * adjustment to insure the SERDES drive level is set
2333 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
2334 (sc->bnx_flags & BNX_FLAG_TBI)) {
2337 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2338 serdescfg = (serdescfg & ~0xFFF) | 0x880;
2339 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2342 CSR_WRITE_4(sc, BGE_MI_MODE,
2343 sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
2346 /* XXX: Broadcom Linux driver. */
2347 if (!BNX_IS_57765_PLUS(sc)) {
2350 /* Enable Data FIFO protection. */
2351 v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2352 CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
2357 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
2358 BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
2359 CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
2364 * Frame reception handling. This is called if there's a frame
2365 * on the receive return list.
2367 * Note: we have to be able to handle two possibilities here:
2368 * 1) the frame is from the jumbo recieve ring
2369 * 2) the frame is from the standard receive ring
2373 bnx_rxeof(struct bnx_softc *sc, uint16_t rx_prod, int count)
2376 int stdcnt = 0, jumbocnt = 0;
2378 ifp = &sc->arpcom.ac_if;
2380 while (sc->bnx_rx_saved_considx != rx_prod && count != 0) {
2381 struct bge_rx_bd *cur_rx;
2383 struct mbuf *m = NULL;
2384 uint16_t vlan_tag = 0;
2390 &sc->bnx_ldata.bnx_rx_return_ring[sc->bnx_rx_saved_considx];
2392 rxidx = cur_rx->bge_idx;
2393 BNX_INC(sc->bnx_rx_saved_considx, BNX_RETURN_RING_CNT);
2395 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2397 vlan_tag = cur_rx->bge_vlan_tag;
2400 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2401 BNX_INC(sc->bnx_jumbo, BGE_JUMBO_RX_RING_CNT);
2404 if (rxidx != sc->bnx_jumbo) {
2405 IFNET_STAT_INC(ifp, ierrors, 1);
2406 if_printf(ifp, "sw jumbo index(%d) "
2407 "and hw jumbo index(%d) mismatch, drop!\n",
2408 sc->bnx_jumbo, rxidx);
2409 bnx_setup_rxdesc_jumbo(sc, rxidx);
2413 m = sc->bnx_cdata.bnx_rx_jumbo_chain[rxidx].bnx_mbuf;
2414 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2415 IFNET_STAT_INC(ifp, ierrors, 1);
2416 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2419 if (bnx_newbuf_jumbo(sc, sc->bnx_jumbo, 0)) {
2420 IFNET_STAT_INC(ifp, ierrors, 1);
2421 bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
2425 BNX_INC(sc->bnx_std, BGE_STD_RX_RING_CNT);
2428 if (rxidx != sc->bnx_std) {
2429 IFNET_STAT_INC(ifp, ierrors, 1);
2430 if_printf(ifp, "sw std index(%d) "
2431 "and hw std index(%d) mismatch, drop!\n",
2432 sc->bnx_std, rxidx);
2433 bnx_setup_rxdesc_std(sc, rxidx);
2437 m = sc->bnx_cdata.bnx_rx_std_chain[rxidx].bnx_mbuf;
2438 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2439 IFNET_STAT_INC(ifp, ierrors, 1);
2440 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2443 if (bnx_newbuf_std(sc, sc->bnx_std, 0)) {
2444 IFNET_STAT_INC(ifp, ierrors, 1);
2445 bnx_setup_rxdesc_std(sc, sc->bnx_std);
2450 IFNET_STAT_INC(ifp, ipackets, 1);
2451 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2452 m->m_pkthdr.rcvif = ifp;
2454 if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2455 (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
2456 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
2457 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2458 if ((cur_rx->bge_error_flag &
2459 BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
2460 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2462 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2463 m->m_pkthdr.csum_data =
2464 cur_rx->bge_tcp_udp_csum;
2465 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
2471 * If we received a packet with a vlan tag, pass it
2472 * to vlan_input() instead of ether_input().
2475 m->m_flags |= M_VLANTAG;
2476 m->m_pkthdr.ether_vlantag = vlan_tag;
2478 ifp->if_input(ifp, m);
2481 bnx_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bnx_rx_saved_considx);
2483 bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
2485 bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
2489 bnx_txeof(struct bnx_softc *sc, uint16_t tx_cons)
2493 ifp = &sc->arpcom.ac_if;
2496 * Go through our tx ring and free mbufs for those
2497 * frames that have been sent.
2499 while (sc->bnx_tx_saved_considx != tx_cons) {
2502 idx = sc->bnx_tx_saved_considx;
2503 if (sc->bnx_cdata.bnx_tx_chain[idx] != NULL) {
2504 IFNET_STAT_INC(ifp, opackets, 1);
2505 bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
2506 sc->bnx_cdata.bnx_tx_dmamap[idx]);
2507 m_freem(sc->bnx_cdata.bnx_tx_chain[idx]);
2508 sc->bnx_cdata.bnx_tx_chain[idx] = NULL;
2511 BNX_INC(sc->bnx_tx_saved_considx, BGE_TX_RING_CNT);
2514 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) >=
2515 (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
2516 ifq_clr_oactive(&ifp->if_snd);
2518 if (sc->bnx_txcnt == 0)
2521 if (!ifq_is_empty(&ifp->if_snd))
2525 #ifdef IFPOLL_ENABLE
2528 bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2530 struct bnx_softc *sc = ifp->if_softc;
2532 ASSERT_SERIALIZED(ifp->if_serializer);
2535 int cpuid = sc->bnx_npoll.ifpc_cpuid;
2537 info->ifpi_rx[cpuid].poll_func = bnx_npoll_compat;
2538 info->ifpi_rx[cpuid].arg = NULL;
2539 info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
2541 if (ifp->if_flags & IFF_RUNNING)
2542 bnx_disable_intr(sc);
2543 ifq_set_cpuid(&ifp->if_snd, cpuid);
2545 if (ifp->if_flags & IFF_RUNNING)
2546 bnx_enable_intr(sc);
2547 ifq_set_cpuid(&ifp->if_snd, sc->bnx_intr_cpuid);
2552 bnx_npoll_compat(struct ifnet *ifp, void *arg __unused, int cycle)
2554 struct bnx_softc *sc = ifp->if_softc;
2555 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2556 uint16_t rx_prod, tx_cons;
2558 ASSERT_SERIALIZED(ifp->if_serializer);
2560 if (sc->bnx_npoll.ifpc_stcount-- == 0) {
2561 sc->bnx_npoll.ifpc_stcount = sc->bnx_npoll.ifpc_stfrac;
2563 * Process link state changes.
2568 sc->bnx_status_tag = sblk->bge_status_tag;
2571 * Use a load fence to ensure that status_tag is saved
2572 * before rx_prod and tx_cons.
2576 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2577 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2579 if (sc->bnx_rx_saved_considx != rx_prod)
2580 bnx_rxeof(sc, rx_prod, cycle);
2582 if (sc->bnx_tx_saved_considx != tx_cons)
2583 bnx_txeof(sc, tx_cons);
2585 if (sc->bnx_coal_chg)
2586 bnx_coal_change(sc);
2589 #endif /* IFPOLL_ENABLE */
2592 bnx_intr_legacy(void *xsc)
2594 struct bnx_softc *sc = xsc;
2595 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2597 if (sc->bnx_status_tag == sblk->bge_status_tag) {
2600 val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
2601 if (val & BGE_PCISTAT_INTR_NOTACT)
2607 * Interrupt will have to be disabled if tagged status
2608 * is used, else interrupt will always be asserted on
2609 * certain chips (at least on BCM5750 AX/BX).
2611 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2619 struct bnx_softc *sc = xsc;
2621 /* Disable interrupt first */
2622 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
2627 bnx_msi_oneshot(void *xsc)
2633 bnx_intr(struct bnx_softc *sc)
2635 struct ifnet *ifp = &sc->arpcom.ac_if;
2636 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
2637 uint16_t rx_prod, tx_cons;
2640 sc->bnx_status_tag = sblk->bge_status_tag;
2642 * Use a load fence to ensure that status_tag is saved
2643 * before rx_prod, tx_cons and status.
2647 rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
2648 tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
2649 status = sblk->bge_status;
2651 if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
2654 if (ifp->if_flags & IFF_RUNNING) {
2655 if (sc->bnx_rx_saved_considx != rx_prod)
2656 bnx_rxeof(sc, rx_prod, -1);
2658 if (sc->bnx_tx_saved_considx != tx_cons)
2659 bnx_txeof(sc, tx_cons);
2662 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
2664 if (sc->bnx_coal_chg)
2665 bnx_coal_change(sc);
2671 struct bnx_softc *sc = xsc;
2672 struct ifnet *ifp = &sc->arpcom.ac_if;
2674 lwkt_serialize_enter(ifp->if_serializer);
2676 KKASSERT(mycpuid == sc->bnx_stat_cpuid);
2678 bnx_stats_update_regs(sc);
2680 if (sc->bnx_flags & BNX_FLAG_TBI) {
2682 * Since in TBI mode auto-polling can't be used we should poll
2683 * link status manually. Here we register pending link event
2684 * and trigger interrupt.
2687 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
2688 } else if (!sc->bnx_link) {
2689 mii_tick(device_get_softc(sc->bnx_miibus));
2692 callout_reset(&sc->bnx_stat_timer, hz, bnx_tick, sc);
2694 lwkt_serialize_exit(ifp->if_serializer);
2698 bnx_stats_update_regs(struct bnx_softc *sc)
2700 struct ifnet *ifp = &sc->arpcom.ac_if;
2701 struct bge_mac_stats_regs stats;
2705 s = (uint32_t *)&stats;
2706 for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
2707 *s = CSR_READ_4(sc, BGE_RX_STATS + i);
2711 IFNET_STAT_SET(ifp, collisions,
2712 (stats.dot3StatsSingleCollisionFrames +
2713 stats.dot3StatsMultipleCollisionFrames +
2714 stats.dot3StatsExcessiveCollisions +
2715 stats.dot3StatsLateCollisions));
2719 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
2720 * pointers to descriptors.
2723 bnx_encap(struct bnx_softc *sc, struct mbuf **m_head0, uint32_t *txidx,
2726 struct bge_tx_bd *d = NULL;
2727 uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
2728 bus_dma_segment_t segs[BNX_NSEG_NEW];
2730 int error, maxsegs, nsegs, idx, i;
2731 struct mbuf *m_head = *m_head0, *m_new;
2733 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
2734 #ifdef BNX_TSO_DEBUG
2738 error = bnx_setup_tso(sc, m_head0, &mss, &csum_flags);
2743 #ifdef BNX_TSO_DEBUG
2744 tso_nsegs = (m_head->m_pkthdr.len /
2745 m_head->m_pkthdr.tso_segsz) - 1;
2746 if (tso_nsegs > (BNX_TSO_NSTATS - 1))
2747 tso_nsegs = BNX_TSO_NSTATS - 1;
2748 else if (tso_nsegs < 0)
2750 sc->bnx_tsosegs[tso_nsegs]++;
2752 } else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
2753 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2754 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2755 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2756 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2757 if (m_head->m_flags & M_LASTFRAG)
2758 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
2759 else if (m_head->m_flags & M_FRAG)
2760 csum_flags |= BGE_TXBDFLAG_IP_FRAG;
2762 if (m_head->m_flags & M_VLANTAG) {
2763 csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
2764 vlan_tag = m_head->m_pkthdr.ether_vlantag;
2768 map = sc->bnx_cdata.bnx_tx_dmamap[idx];
2770 maxsegs = (BGE_TX_RING_CNT - sc->bnx_txcnt) - BNX_NSEG_RSVD;
2771 KASSERT(maxsegs >= BNX_NSEG_SPARE,
2772 ("not enough segments %d", maxsegs));
2774 if (maxsegs > BNX_NSEG_NEW)
2775 maxsegs = BNX_NSEG_NEW;
2778 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
2779 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
2780 * but when such padded frames employ the bge IP/TCP checksum
2781 * offload, the hardware checksum assist gives incorrect results
2782 * (possibly from incorporating its own padding into the UDP/TCP
2783 * checksum; who knows). If we pad such runts with zeros, the
2784 * onboard checksum comes out correct.
2786 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
2787 m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
2788 error = m_devpad(m_head, BNX_MIN_FRAMELEN);
2793 if ((sc->bnx_flags & BNX_FLAG_SHORTDMA) && m_head->m_next != NULL) {
2794 m_new = bnx_defrag_shortdma(m_head);
2795 if (m_new == NULL) {
2799 *m_head0 = m_head = m_new;
2801 if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
2802 sc->bnx_force_defrag && m_head->m_next != NULL) {
2804 * Forcefully defragment mbuf chain to overcome hardware
2805 * limitation which only support a single outstanding
2806 * DMA read operation. If it fails, keep moving on using
2807 * the original mbuf chain.
2809 m_new = m_defrag(m_head, MB_DONTWAIT);
2811 *m_head0 = m_head = m_new;
2814 error = bus_dmamap_load_mbuf_defrag(sc->bnx_cdata.bnx_tx_mtag, map,
2815 m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
2818 *segs_used += nsegs;
2821 bus_dmamap_sync(sc->bnx_cdata.bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
2823 for (i = 0; ; i++) {
2824 d = &sc->bnx_ldata.bnx_tx_ring[idx];
2826 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
2827 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
2828 d->bge_len = segs[i].ds_len;
2829 d->bge_flags = csum_flags;
2830 d->bge_vlan_tag = vlan_tag;
2835 BNX_INC(idx, BGE_TX_RING_CNT);
2837 /* Mark the last segment as end of packet... */
2838 d->bge_flags |= BGE_TXBDFLAG_END;
2841 * Insure that the map for this transmission is placed at
2842 * the array index of the last descriptor in this chain.
2844 sc->bnx_cdata.bnx_tx_dmamap[*txidx] = sc->bnx_cdata.bnx_tx_dmamap[idx];
2845 sc->bnx_cdata.bnx_tx_dmamap[idx] = map;
2846 sc->bnx_cdata.bnx_tx_chain[idx] = m_head;
2847 sc->bnx_txcnt += nsegs;
2849 BNX_INC(idx, BGE_TX_RING_CNT);
2860 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2861 * to the mbuf data regions directly in the transmit descriptors.
2864 bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2866 struct bnx_softc *sc = ifp->if_softc;
2867 struct mbuf *m_head = NULL;
2871 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2873 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
2876 prodidx = sc->bnx_tx_prodidx;
2878 while (sc->bnx_cdata.bnx_tx_chain[prodidx] == NULL) {
2880 * Sanity check: avoid coming within BGE_NSEG_RSVD
2881 * descriptors of the end of the ring. Also make
2882 * sure there are BGE_NSEG_SPARE descriptors for
2883 * jumbo buffers' or TSO segments' defragmentation.
2885 if ((BGE_TX_RING_CNT - sc->bnx_txcnt) <
2886 (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
2887 ifq_set_oactive(&ifp->if_snd);
2891 m_head = ifq_dequeue(&ifp->if_snd, NULL);
2896 * Pack the data into the transmit ring. If we
2897 * don't have room, set the OACTIVE flag and wait
2898 * for the NIC to drain the ring.
2900 if (bnx_encap(sc, &m_head, &prodidx, &nsegs)) {
2901 ifq_set_oactive(&ifp->if_snd);
2902 IFNET_STAT_INC(ifp, oerrors, 1);
2906 if (nsegs >= sc->bnx_tx_wreg) {
2908 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
2912 ETHER_BPF_MTAP(ifp, m_head);
2915 * Set a timeout in case the chip goes out to lunch.
2922 bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
2924 sc->bnx_tx_prodidx = prodidx;
2930 struct bnx_softc *sc = xsc;
2931 struct ifnet *ifp = &sc->arpcom.ac_if;
2935 ASSERT_SERIALIZED(ifp->if_serializer);
2937 /* Cancel pending I/O and flush buffers. */
2943 * Init the various state machines, ring
2944 * control blocks and firmware.
2946 if (bnx_blockinit(sc)) {
2947 if_printf(ifp, "initialization failure\n");
2953 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
2954 ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
2956 /* Load our MAC address. */
2957 m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
2958 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
2959 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
2961 /* Enable or disable promiscuous mode as needed. */
2964 /* Program multicast filter. */
2968 if (bnx_init_rx_ring_std(sc)) {
2969 if_printf(ifp, "RX ring initialization failed\n");
2974 /* Init jumbo RX ring. */
2975 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
2976 if (bnx_init_rx_ring_jumbo(sc)) {
2977 if_printf(ifp, "Jumbo RX ring initialization failed\n");
2983 /* Init our RX return ring index */
2984 sc->bnx_rx_saved_considx = 0;
2987 bnx_init_tx_ring(sc);
2989 /* Enable TX MAC state machine lockup fix. */
2990 mode = CSR_READ_4(sc, BGE_TX_MODE);
2991 mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
2992 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
2993 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
2994 mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
2995 mode |= CSR_READ_4(sc, BGE_TX_MODE) &
2996 (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
2998 /* Turn on transmitter */
2999 CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
3001 /* Turn on receiver */
3002 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3005 * Set the number of good frames to receive after RX MBUF
3006 * Low Watermark has been reached. After the RX MAC receives
3007 * this number of frames, it will drop subsequent incoming
3008 * frames until the MBUF High Watermark is reached.
3010 if (BNX_IS_57765_FAMILY(sc))
3011 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
3013 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
3015 if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
3017 if_printf(ifp, "MSI_MODE: %#x\n",
3018 CSR_READ_4(sc, BGE_MSI_MODE));
3022 /* Tell firmware we're alive. */
3023 BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3025 /* Enable host interrupts if polling(4) is not enabled. */
3026 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
3027 #ifdef IFPOLL_ENABLE
3028 if (ifp->if_flags & IFF_NPOLLING)
3029 bnx_disable_intr(sc);
3032 bnx_enable_intr(sc);
3034 bnx_ifmedia_upd(ifp);
3036 ifp->if_flags |= IFF_RUNNING;
3037 ifq_clr_oactive(&ifp->if_snd);
3039 callout_reset_bycpu(&sc->bnx_stat_timer, hz, bnx_tick, sc,
3040 sc->bnx_stat_cpuid);
3044 * Set media options.
3047 bnx_ifmedia_upd(struct ifnet *ifp)
3049 struct bnx_softc *sc = ifp->if_softc;
3051 /* If this is a 1000baseX NIC, enable the TBI port. */
3052 if (sc->bnx_flags & BNX_FLAG_TBI) {
3053 struct ifmedia *ifm = &sc->bnx_ifmedia;
3055 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3058 switch(IFM_SUBTYPE(ifm->ifm_media)) {
3063 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3064 BNX_CLRBIT(sc, BGE_MAC_MODE,
3065 BGE_MACMODE_HALF_DUPLEX);
3067 BNX_SETBIT(sc, BGE_MAC_MODE,
3068 BGE_MACMODE_HALF_DUPLEX);
3075 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3079 if (mii->mii_instance) {
3080 struct mii_softc *miisc;
3082 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3083 mii_phy_reset(miisc);
3088 * Force an interrupt so that we will call bnx_link_upd
3089 * if needed and clear any pending link state attention.
3090 * Without this we are not getting any further interrupts
3091 * for link state changes and thus will not UP the link and
3092 * not be able to send in bnx_start. The only way to get
3093 * things working was to receive a packet and get an RX
3096 * bnx_tick should help for fiber cards and we might not
3097 * need to do this here if BNX_FLAG_TBI is set but as
3098 * we poll for fiber anyway it should not harm.
3100 BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3106 * Report current media status.
3109 bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3111 struct bnx_softc *sc = ifp->if_softc;
3113 if (sc->bnx_flags & BNX_FLAG_TBI) {
3114 ifmr->ifm_status = IFM_AVALID;
3115 ifmr->ifm_active = IFM_ETHER;
3116 if (CSR_READ_4(sc, BGE_MAC_STS) &
3117 BGE_MACSTAT_TBI_PCS_SYNCHED) {
3118 ifmr->ifm_status |= IFM_ACTIVE;
3120 ifmr->ifm_active |= IFM_NONE;
3124 ifmr->ifm_active |= IFM_1000_SX;
3125 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3126 ifmr->ifm_active |= IFM_HDX;
3128 ifmr->ifm_active |= IFM_FDX;
3130 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3133 ifmr->ifm_active = mii->mii_media_active;
3134 ifmr->ifm_status = mii->mii_media_status;
3139 bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
3141 struct bnx_softc *sc = ifp->if_softc;
3142 struct ifreq *ifr = (struct ifreq *)data;
3143 int mask, error = 0;
3145 ASSERT_SERIALIZED(ifp->if_serializer);
3149 if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
3150 (BNX_IS_JUMBO_CAPABLE(sc) &&
3151 ifr->ifr_mtu > BNX_JUMBO_MTU)) {
3153 } else if (ifp->if_mtu != ifr->ifr_mtu) {
3154 ifp->if_mtu = ifr->ifr_mtu;
3155 if (ifp->if_flags & IFF_RUNNING)
3160 if (ifp->if_flags & IFF_UP) {
3161 if (ifp->if_flags & IFF_RUNNING) {
3162 mask = ifp->if_flags ^ sc->bnx_if_flags;
3165 * If only the state of the PROMISC flag
3166 * changed, then just use the 'set promisc
3167 * mode' command instead of reinitializing
3168 * the entire NIC. Doing a full re-init
3169 * means reloading the firmware and waiting
3170 * for it to start up, which may take a
3171 * second or two. Similarly for ALLMULTI.
3173 if (mask & IFF_PROMISC)
3175 if (mask & IFF_ALLMULTI)
3180 } else if (ifp->if_flags & IFF_RUNNING) {
3183 sc->bnx_if_flags = ifp->if_flags;
3187 if (ifp->if_flags & IFF_RUNNING)
3192 if (sc->bnx_flags & BNX_FLAG_TBI) {
3193 error = ifmedia_ioctl(ifp, ifr,
3194 &sc->bnx_ifmedia, command);
3196 struct mii_data *mii;
3198 mii = device_get_softc(sc->bnx_miibus);
3199 error = ifmedia_ioctl(ifp, ifr,
3200 &mii->mii_media, command);
3204 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3205 if (mask & IFCAP_HWCSUM) {
3206 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
3207 if (ifp->if_capenable & IFCAP_TXCSUM)
3208 ifp->if_hwassist |= BNX_CSUM_FEATURES;
3210 ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
3212 if (mask & IFCAP_TSO) {
3213 ifp->if_capenable ^= (mask & IFCAP_TSO);
3214 if (ifp->if_capenable & IFCAP_TSO)
3215 ifp->if_hwassist |= CSUM_TSO;
3217 ifp->if_hwassist &= ~CSUM_TSO;
3221 error = ether_ioctl(ifp, command, data);
3228 bnx_watchdog(struct ifnet *ifp)
3230 struct bnx_softc *sc = ifp->if_softc;
3232 if_printf(ifp, "watchdog timeout -- resetting\n");
3236 IFNET_STAT_INC(ifp, oerrors, 1);
3238 if (!ifq_is_empty(&ifp->if_snd))
3243 * Stop the adapter and free any mbufs allocated to the
3247 bnx_stop(struct bnx_softc *sc)
3249 struct ifnet *ifp = &sc->arpcom.ac_if;
3251 ASSERT_SERIALIZED(ifp->if_serializer);
3253 callout_stop(&sc->bnx_stat_timer);
3256 * Disable all of the receiver blocks
3258 bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3259 bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3260 bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3261 bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3262 bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3263 bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3266 * Disable all of the transmit blocks
3268 bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3269 bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3270 bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3271 bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3272 bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3273 bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3276 * Shut down all of the memory managers and related
3279 bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3280 bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3281 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3282 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3284 /* Disable host interrupts. */
3285 bnx_disable_intr(sc);
3288 * Tell firmware we're shutting down.
3290 BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3292 /* Free the RX lists. */
3293 bnx_free_rx_ring_std(sc);
3295 /* Free jumbo RX list. */
3296 if (BNX_IS_JUMBO_CAPABLE(sc))
3297 bnx_free_rx_ring_jumbo(sc);
3299 /* Free TX buffers. */
3300 bnx_free_tx_ring(sc);
3302 sc->bnx_status_tag = 0;
3304 sc->bnx_coal_chg = 0;
3306 sc->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
3308 ifp->if_flags &= ~IFF_RUNNING;
3309 ifq_clr_oactive(&ifp->if_snd);
3314 * Stop all chip I/O so that the kernel's probe routines don't
3315 * get confused by errant DMAs when rebooting.
3318 bnx_shutdown(device_t dev)
3320 struct bnx_softc *sc = device_get_softc(dev);
3321 struct ifnet *ifp = &sc->arpcom.ac_if;
3323 lwkt_serialize_enter(ifp->if_serializer);
3326 lwkt_serialize_exit(ifp->if_serializer);
3330 bnx_suspend(device_t dev)
3332 struct bnx_softc *sc = device_get_softc(dev);
3333 struct ifnet *ifp = &sc->arpcom.ac_if;
3335 lwkt_serialize_enter(ifp->if_serializer);
3337 lwkt_serialize_exit(ifp->if_serializer);
3343 bnx_resume(device_t dev)
3345 struct bnx_softc *sc = device_get_softc(dev);
3346 struct ifnet *ifp = &sc->arpcom.ac_if;
3348 lwkt_serialize_enter(ifp->if_serializer);
3350 if (ifp->if_flags & IFF_UP) {
3353 if (!ifq_is_empty(&ifp->if_snd))
3357 lwkt_serialize_exit(ifp->if_serializer);
3363 bnx_setpromisc(struct bnx_softc *sc)
3365 struct ifnet *ifp = &sc->arpcom.ac_if;
3367 if (ifp->if_flags & IFF_PROMISC)
3368 BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3370 BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3374 bnx_dma_free(struct bnx_softc *sc)
3378 /* Destroy RX mbuf DMA stuffs. */
3379 if (sc->bnx_cdata.bnx_rx_mtag != NULL) {
3380 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3381 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3382 sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3384 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3385 sc->bnx_cdata.bnx_rx_tmpmap);
3386 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3389 /* Destroy TX mbuf DMA stuffs. */
3390 if (sc->bnx_cdata.bnx_tx_mtag != NULL) {
3391 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3392 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3393 sc->bnx_cdata.bnx_tx_dmamap[i]);
3395 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3398 /* Destroy standard RX ring */
3399 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_std_ring_tag,
3400 sc->bnx_cdata.bnx_rx_std_ring_map,
3401 sc->bnx_ldata.bnx_rx_std_ring);
3403 if (BNX_IS_JUMBO_CAPABLE(sc))
3404 bnx_free_jumbo_mem(sc);
3406 /* Destroy RX return ring */
3407 bnx_dma_block_free(sc->bnx_cdata.bnx_rx_return_ring_tag,
3408 sc->bnx_cdata.bnx_rx_return_ring_map,
3409 sc->bnx_ldata.bnx_rx_return_ring);
3411 /* Destroy TX ring */
3412 bnx_dma_block_free(sc->bnx_cdata.bnx_tx_ring_tag,
3413 sc->bnx_cdata.bnx_tx_ring_map,
3414 sc->bnx_ldata.bnx_tx_ring);
3416 /* Destroy status block */
3417 bnx_dma_block_free(sc->bnx_cdata.bnx_status_tag,
3418 sc->bnx_cdata.bnx_status_map,
3419 sc->bnx_ldata.bnx_status_block);
3421 /* Destroy the parent tag */
3422 if (sc->bnx_cdata.bnx_parent_tag != NULL)
3423 bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
3427 bnx_dma_alloc(struct bnx_softc *sc)
3429 struct ifnet *ifp = &sc->arpcom.ac_if;
3430 bus_size_t txmaxsz, txmaxsegsz;
3434 * Allocate the parent bus DMA tag appropriate for PCI.
3436 * All of the NetExtreme/NetLink controllers have 4GB boundary
3438 * Whenever an address crosses a multiple of the 4GB boundary
3439 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3440 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3441 * state machine will lockup and cause the device to hang.
3443 error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
3444 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3446 BUS_SPACE_MAXSIZE_32BIT, 0,
3447 BUS_SPACE_MAXSIZE_32BIT,
3448 0, &sc->bnx_cdata.bnx_parent_tag);
3450 if_printf(ifp, "could not allocate parent dma tag\n");
3455 * Create DMA tag and maps for RX mbufs.
3457 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3458 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3459 NULL, NULL, MCLBYTES, 1, MCLBYTES,
3460 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK,
3461 &sc->bnx_cdata.bnx_rx_mtag);
3463 if_printf(ifp, "could not allocate RX mbuf dma tag\n");
3467 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3468 BUS_DMA_WAITOK, &sc->bnx_cdata.bnx_rx_tmpmap);
3470 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3471 sc->bnx_cdata.bnx_rx_mtag = NULL;
3475 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3476 error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
3478 &sc->bnx_cdata.bnx_rx_std_dmamap[i]);
3482 for (j = 0; j < i; ++j) {
3483 bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
3484 sc->bnx_cdata.bnx_rx_std_dmamap[j]);
3486 bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
3487 sc->bnx_cdata.bnx_rx_mtag = NULL;
3489 if_printf(ifp, "could not create DMA map for RX\n");
3495 * Create DMA tag and maps for TX mbufs.
3497 if (sc->bnx_flags & BNX_FLAG_TSO)
3498 txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
3500 txmaxsz = BNX_JUMBO_FRAMELEN;
3501 if (sc->bnx_asicrev == BGE_ASICREV_BCM57766)
3502 txmaxsegsz = MCLBYTES;
3504 txmaxsegsz = PAGE_SIZE;
3505 error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3506 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3508 txmaxsz, BNX_NSEG_NEW, txmaxsegsz,
3509 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK |
3511 &sc->bnx_cdata.bnx_tx_mtag);
3513 if_printf(ifp, "could not allocate TX mbuf dma tag\n");
3517 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3518 error = bus_dmamap_create(sc->bnx_cdata.bnx_tx_mtag,
3519 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
3520 &sc->bnx_cdata.bnx_tx_dmamap[i]);
3524 for (j = 0; j < i; ++j) {
3525 bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
3526 sc->bnx_cdata.bnx_tx_dmamap[j]);
3528 bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
3529 sc->bnx_cdata.bnx_tx_mtag = NULL;
3531 if_printf(ifp, "could not create DMA map for TX\n");
3537 * Create DMA stuffs for standard RX ring.
3539 error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
3540 &sc->bnx_cdata.bnx_rx_std_ring_tag,
3541 &sc->bnx_cdata.bnx_rx_std_ring_map,
3542 (void *)&sc->bnx_ldata.bnx_rx_std_ring,
3543 &sc->bnx_ldata.bnx_rx_std_ring_paddr);
3545 if_printf(ifp, "could not create std RX ring\n");
3550 * Create jumbo buffer pool.
3552 if (BNX_IS_JUMBO_CAPABLE(sc)) {
3553 error = bnx_alloc_jumbo_mem(sc);
3555 if_printf(ifp, "could not create jumbo buffer pool\n");
3561 * Create DMA stuffs for RX return ring.
3563 error = bnx_dma_block_alloc(sc,
3564 BGE_RX_RTN_RING_SZ(BNX_RETURN_RING_CNT),
3565 &sc->bnx_cdata.bnx_rx_return_ring_tag,
3566 &sc->bnx_cdata.bnx_rx_return_ring_map,
3567 (void *)&sc->bnx_ldata.bnx_rx_return_ring,
3568 &sc->bnx_ldata.bnx_rx_return_ring_paddr);
3570 if_printf(ifp, "could not create RX ret ring\n");
3575 * Create DMA stuffs for TX ring.
3577 error = bnx_dma_block_alloc(sc, BGE_TX_RING_SZ,
3578 &sc->bnx_cdata.bnx_tx_ring_tag,
3579 &sc->bnx_cdata.bnx_tx_ring_map,
3580 (void *)&sc->bnx_ldata.bnx_tx_ring,
3581 &sc->bnx_ldata.bnx_tx_ring_paddr);
3583 if_printf(ifp, "could not create TX ring\n");
3588 * Create DMA stuffs for status block.
3590 error = bnx_dma_block_alloc(sc, BGE_STATUS_BLK_SZ,
3591 &sc->bnx_cdata.bnx_status_tag,
3592 &sc->bnx_cdata.bnx_status_map,
3593 (void *)&sc->bnx_ldata.bnx_status_block,
3594 &sc->bnx_ldata.bnx_status_block_paddr);
3596 if_printf(ifp, "could not create status block\n");
3604 bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
3605 bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
3610 error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
3611 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3612 size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
3616 *tag = dmem.dmem_tag;
3617 *map = dmem.dmem_map;
3618 *addr = dmem.dmem_addr;
3619 *paddr = dmem.dmem_busaddr;
3625 bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
3628 bus_dmamap_unload(tag, map);
3629 bus_dmamem_free(tag, addr, map);
3630 bus_dma_tag_destroy(tag);
3635 bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
3637 struct ifnet *ifp = &sc->arpcom.ac_if;
3639 #define PCS_ENCODE_ERR (BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
3642 * Sometimes PCS encoding errors are detected in
3643 * TBI mode (on fiber NICs), and for some reason
3644 * the chip will signal them as link changes.
3645 * If we get a link change event, but the 'PCS
3646 * encoding error' bit in the MAC status register
3647 * is set, don't bother doing a link check.
3648 * This avoids spurious "gigabit link up" messages
3649 * that sometimes appear on fiber NICs during
3650 * periods of heavy traffic.
3652 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
3653 if (!sc->bnx_link) {
3655 if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
3656 BNX_CLRBIT(sc, BGE_MAC_MODE,
3657 BGE_MACMODE_TBI_SEND_CFGS);
3659 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
3662 if_printf(ifp, "link UP\n");
3664 ifp->if_link_state = LINK_STATE_UP;
3665 if_link_state_change(ifp);
3667 } else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
3672 if_printf(ifp, "link DOWN\n");
3674 ifp->if_link_state = LINK_STATE_DOWN;
3675 if_link_state_change(ifp);
3679 #undef PCS_ENCODE_ERR
3681 /* Clear the attention. */
3682 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3683 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3684 BGE_MACSTAT_LINK_CHANGED);
3688 bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3690 struct ifnet *ifp = &sc->arpcom.ac_if;
3691 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3694 bnx_miibus_statchg(sc->bnx_dev);
3698 if_printf(ifp, "link UP\n");
3700 if_printf(ifp, "link DOWN\n");
3703 /* Clear the attention. */
3704 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3705 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3706 BGE_MACSTAT_LINK_CHANGED);
3710 bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
3712 struct ifnet *ifp = &sc->arpcom.ac_if;
3713 struct mii_data *mii = device_get_softc(sc->bnx_miibus);
3717 if (!sc->bnx_link &&
3718 (mii->mii_media_status & IFM_ACTIVE) &&
3719 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
3722 if_printf(ifp, "link UP\n");
3723 } else if (sc->bnx_link &&
3724 (!(mii->mii_media_status & IFM_ACTIVE) ||
3725 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
3728 if_printf(ifp, "link DOWN\n");
3731 /* Clear the attention. */
3732 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
3733 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
3734 BGE_MACSTAT_LINK_CHANGED);
3738 bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
3740 struct bnx_softc *sc = arg1;
3742 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3743 &sc->bnx_rx_coal_ticks,
3744 BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
3745 BNX_RX_COAL_TICKS_CHG);
3749 bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
3751 struct bnx_softc *sc = arg1;
3753 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3754 &sc->bnx_tx_coal_ticks,
3755 BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
3756 BNX_TX_COAL_TICKS_CHG);
3760 bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
3762 struct bnx_softc *sc = arg1;
3764 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3765 &sc->bnx_rx_coal_bds,
3766 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3767 BNX_RX_COAL_BDS_CHG);
3771 bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
3773 struct bnx_softc *sc = arg1;
3775 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3776 &sc->bnx_tx_coal_bds,
3777 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3778 BNX_TX_COAL_BDS_CHG);
3782 bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3784 struct bnx_softc *sc = arg1;
3786 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3787 &sc->bnx_rx_coal_bds_int,
3788 BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
3789 BNX_RX_COAL_BDS_INT_CHG);
3793 bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
3795 struct bnx_softc *sc = arg1;
3797 return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
3798 &sc->bnx_tx_coal_bds_int,
3799 BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
3800 BNX_TX_COAL_BDS_INT_CHG);
3804 bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
3805 int coal_min, int coal_max, uint32_t coal_chg_mask)
3807 struct bnx_softc *sc = arg1;
3808 struct ifnet *ifp = &sc->arpcom.ac_if;
3811 lwkt_serialize_enter(ifp->if_serializer);
3814 error = sysctl_handle_int(oidp, &v, 0, req);
3815 if (!error && req->newptr != NULL) {
3816 if (v < coal_min || v > coal_max) {
3820 sc->bnx_coal_chg |= coal_chg_mask;
3824 lwkt_serialize_exit(ifp->if_serializer);
3829 bnx_coal_change(struct bnx_softc *sc)
3831 struct ifnet *ifp = &sc->arpcom.ac_if;
3833 ASSERT_SERIALIZED(ifp->if_serializer);
3835 if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
3836 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
3837 sc->bnx_rx_coal_ticks);
3839 CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
3842 if_printf(ifp, "rx_coal_ticks -> %u\n",
3843 sc->bnx_rx_coal_ticks);
3847 if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
3848 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
3849 sc->bnx_tx_coal_ticks);
3851 CSR_READ_4(sc, BGE_HCC_TX_COAL_TICKS);
3854 if_printf(ifp, "tx_coal_ticks -> %u\n",
3855 sc->bnx_tx_coal_ticks);
3859 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
3860 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
3861 sc->bnx_rx_coal_bds);
3863 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
3866 if_printf(ifp, "rx_coal_bds -> %u\n",
3867 sc->bnx_rx_coal_bds);
3871 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
3872 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS,
3873 sc->bnx_tx_coal_bds);
3875 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS);
3878 if_printf(ifp, "tx_coal_bds -> %u\n",
3879 sc->bnx_tx_coal_bds);
3883 if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
3884 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
3885 sc->bnx_rx_coal_bds_int);
3887 CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT);
3890 if_printf(ifp, "rx_coal_bds_int -> %u\n",
3891 sc->bnx_rx_coal_bds_int);
3895 if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
3896 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
3897 sc->bnx_tx_coal_bds_int);
3899 CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT);
3902 if_printf(ifp, "tx_coal_bds_int -> %u\n",
3903 sc->bnx_tx_coal_bds_int);
3907 sc->bnx_coal_chg = 0;
3911 bnx_intr_check(void *xsc)
3913 struct bnx_softc *sc = xsc;
3914 struct ifnet *ifp = &sc->arpcom.ac_if;
3915 struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
3917 lwkt_serialize_enter(ifp->if_serializer);
3919 KKASSERT(mycpuid == sc->bnx_intr_cpuid);
3921 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
3922 lwkt_serialize_exit(ifp->if_serializer);
3926 if (sblk->bge_idx[0].bge_rx_prod_idx != sc->bnx_rx_saved_considx ||
3927 sblk->bge_idx[0].bge_tx_cons_idx != sc->bnx_tx_saved_considx) {
3928 if (sc->bnx_rx_check_considx == sc->bnx_rx_saved_considx &&
3929 sc->bnx_tx_check_considx == sc->bnx_tx_saved_considx) {
3930 if (!sc->bnx_intr_maylose) {
3931 sc->bnx_intr_maylose = TRUE;
3935 if_printf(ifp, "lost interrupt\n");
3939 sc->bnx_intr_maylose = FALSE;
3940 sc->bnx_rx_check_considx = sc->bnx_rx_saved_considx;
3941 sc->bnx_tx_check_considx = sc->bnx_tx_saved_considx;
3944 callout_reset(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
3945 bnx_intr_check, sc);
3946 lwkt_serialize_exit(ifp->if_serializer);
3950 bnx_enable_intr(struct bnx_softc *sc)
3952 struct ifnet *ifp = &sc->arpcom.ac_if;
3954 lwkt_serialize_handler_enable(ifp->if_serializer);
3959 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
3960 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
3961 /* XXX Linux driver */
3962 bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
3966 * Unmask the interrupt when we stop polling.
3968 PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
3969 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
3972 * Trigger another interrupt, since above writing
3973 * to interrupt mailbox0 may acknowledge pending
3976 BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
3978 if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
3979 sc->bnx_intr_maylose = FALSE;
3980 sc->bnx_rx_check_considx = 0;
3981 sc->bnx_tx_check_considx = 0;
3984 if_printf(ifp, "status tag bug workaround\n");
3986 /* 10ms check interval */
3987 callout_reset_bycpu(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
3988 bnx_intr_check, sc, sc->bnx_intr_cpuid);
3993 bnx_disable_intr(struct bnx_softc *sc)
3995 struct ifnet *ifp = &sc->arpcom.ac_if;
3998 * Mask the interrupt when we start polling.
4000 PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
4001 BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
4004 * Acknowledge possible asserted interrupt.
4006 bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4008 callout_stop(&sc->bnx_intr_timer);
4009 sc->bnx_intr_maylose = FALSE;
4010 sc->bnx_rx_check_considx = 0;
4011 sc->bnx_tx_check_considx = 0;
4013 sc->bnx_npoll.ifpc_stcount = 0;
4015 lwkt_serialize_handler_disable(ifp->if_serializer);
4019 bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
4024 mac_addr = bnx_readmem_ind(sc, 0x0c14);
4025 if ((mac_addr >> 16) == 0x484b) {
4026 ether_addr[0] = (uint8_t)(mac_addr >> 8);
4027 ether_addr[1] = (uint8_t)mac_addr;
4028 mac_addr = bnx_readmem_ind(sc, 0x0c18);
4029 ether_addr[2] = (uint8_t)(mac_addr >> 24);
4030 ether_addr[3] = (uint8_t)(mac_addr >> 16);
4031 ether_addr[4] = (uint8_t)(mac_addr >> 8);
4032 ether_addr[5] = (uint8_t)mac_addr;
4039 bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
4041 int mac_offset = BGE_EE_MAC_OFFSET;
4043 if (BNX_IS_5717_PLUS(sc)) {
4046 f = pci_get_function(sc->bnx_dev);
4048 mac_offset = BGE_EE_MAC_OFFSET_5717;
4050 mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
4053 return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
4057 bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
4059 if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
4062 return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4067 bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
4069 static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
4070 /* NOTE: Order is critical */
4072 bnx_get_eaddr_nvram,
4073 bnx_get_eaddr_eeprom,
4076 const bnx_eaddr_fcn_t *func;
4078 for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
4079 if ((*func)(sc, eaddr) == 0)
4082 return (*func == NULL ? ENXIO : 0);
4086 * NOTE: 'm' is not freed upon failure
4089 bnx_defrag_shortdma(struct mbuf *m)
4095 * If device receive two back-to-back send BDs with less than
4096 * or equal to 8 total bytes then the device may hang. The two
4097 * back-to-back send BDs must in the same frame for this failure
4098 * to occur. Scan mbuf chains and see whether two back-to-back
4099 * send BDs are there. If this is the case, allocate new mbuf
4100 * and copy the frame to workaround the silicon bug.
4102 for (n = m, found = 0; n != NULL; n = n->m_next) {
4113 n = m_defrag(m, MB_DONTWAIT);
4120 bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
4124 BNX_CLRBIT(sc, reg, bit);
4125 for (i = 0; i < BNX_TIMEOUT; i++) {
4126 if ((CSR_READ_4(sc, reg) & bit) == 0)
4133 bnx_link_poll(struct bnx_softc *sc)
4137 status = CSR_READ_4(sc, BGE_MAC_STS);
4138 if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
4139 sc->bnx_link_evt = 0;
4140 sc->bnx_link_upd(sc, status);
4145 bnx_enable_msi(struct bnx_softc *sc)
4149 msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
4150 msi_mode |= BGE_MSIMODE_ENABLE;
4151 if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4154 * 5718-PG105-R says that "one shot" mode
4155 * does not work if MSI is used, however,
4156 * it obviously works.
4158 msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
4160 CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
4164 bnx_dma_swap_options(struct bnx_softc *sc)
4166 uint32_t dma_options;
4168 dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
4169 BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
4170 #if BYTE_ORDER == BIG_ENDIAN
4171 dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
4173 if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
4174 sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
4175 dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
4176 BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
4177 BGE_MODECTL_HTX2B_ENABLE;
4183 bnx_setup_tso(struct bnx_softc *sc, struct mbuf **mp,
4184 uint16_t *mss0, uint16_t *flags0)
4189 int thoff, iphlen, hoff, hlen;
4190 uint16_t flags, mss;
4193 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4195 hoff = m->m_pkthdr.csum_lhlen;
4196 iphlen = m->m_pkthdr.csum_iphlen;
4197 thoff = m->m_pkthdr.csum_thlen;
4199 KASSERT(hoff > 0, ("invalid ether header len"));
4200 KASSERT(iphlen > 0, ("invalid ip header len"));
4201 KASSERT(thoff > 0, ("invalid tcp header len"));
4203 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4204 m = m_pullup(m, hoff + iphlen + thoff);
4211 ip = mtodoff(m, struct ip *, hoff);
4212 th = mtodoff(m, struct tcphdr *, hoff + iphlen);
4214 mss = m->m_pkthdr.tso_segsz;
4215 flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
4217 ip->ip_len = htons(mss + iphlen + thoff);
4220 hlen = (iphlen + thoff) >> 2;
4221 mss |= ((hlen & 0x3) << 14);
4222 flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);