xref: /dflybsd-src/sys/dev/netif/bnx/if_bnx.c (revision a1bd58c979c634722a526d69e53b2db2f9eebf47)
16c8d8eccSSepherosa Ziehau /*
26c8d8eccSSepherosa Ziehau  * Copyright (c) 2001 Wind River Systems
36c8d8eccSSepherosa Ziehau  * Copyright (c) 1997, 1998, 1999, 2001
46c8d8eccSSepherosa Ziehau  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
56c8d8eccSSepherosa Ziehau  *
66c8d8eccSSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
76c8d8eccSSepherosa Ziehau  * modification, are permitted provided that the following conditions
86c8d8eccSSepherosa Ziehau  * are met:
96c8d8eccSSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
106c8d8eccSSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
116c8d8eccSSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
126c8d8eccSSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in the
136c8d8eccSSepherosa Ziehau  *    documentation and/or other materials provided with the distribution.
146c8d8eccSSepherosa Ziehau  * 3. All advertising materials mentioning features or use of this software
156c8d8eccSSepherosa Ziehau  *    must display the following acknowledgement:
166c8d8eccSSepherosa Ziehau  *	This product includes software developed by Bill Paul.
176c8d8eccSSepherosa Ziehau  * 4. Neither the name of the author nor the names of any co-contributors
186c8d8eccSSepherosa Ziehau  *    may be used to endorse or promote products derived from this software
196c8d8eccSSepherosa Ziehau  *    without specific prior written permission.
206c8d8eccSSepherosa Ziehau  *
216c8d8eccSSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
226c8d8eccSSepherosa Ziehau  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236c8d8eccSSepherosa Ziehau  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246c8d8eccSSepherosa Ziehau  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
256c8d8eccSSepherosa Ziehau  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
266c8d8eccSSepherosa Ziehau  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
276c8d8eccSSepherosa Ziehau  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
286c8d8eccSSepherosa Ziehau  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
296c8d8eccSSepherosa Ziehau  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
306c8d8eccSSepherosa Ziehau  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
316c8d8eccSSepherosa Ziehau  * THE POSSIBILITY OF SUCH DAMAGE.
326c8d8eccSSepherosa Ziehau  *
336c8d8eccSSepherosa Ziehau  * $FreeBSD: src/sys/dev/bge/if_bge.c,v 1.3.2.39 2005/07/03 03:41:18 silby Exp $
346c8d8eccSSepherosa Ziehau  */
356c8d8eccSSepherosa Ziehau 
3666deb1c1SSepherosa Ziehau #include "opt_bnx.h"
376c8d8eccSSepherosa Ziehau #include "opt_polling.h"
386c8d8eccSSepherosa Ziehau 
396c8d8eccSSepherosa Ziehau #include <sys/param.h>
406c8d8eccSSepherosa Ziehau #include <sys/bus.h>
416c8d8eccSSepherosa Ziehau #include <sys/endian.h>
426c8d8eccSSepherosa Ziehau #include <sys/kernel.h>
436c8d8eccSSepherosa Ziehau #include <sys/interrupt.h>
446c8d8eccSSepherosa Ziehau #include <sys/mbuf.h>
456c8d8eccSSepherosa Ziehau #include <sys/malloc.h>
466c8d8eccSSepherosa Ziehau #include <sys/queue.h>
476c8d8eccSSepherosa Ziehau #include <sys/rman.h>
486c8d8eccSSepherosa Ziehau #include <sys/serialize.h>
496c8d8eccSSepherosa Ziehau #include <sys/socket.h>
506c8d8eccSSepherosa Ziehau #include <sys/sockio.h>
516c8d8eccSSepherosa Ziehau #include <sys/sysctl.h>
526c8d8eccSSepherosa Ziehau 
5366deb1c1SSepherosa Ziehau #include <netinet/ip.h>
5466deb1c1SSepherosa Ziehau #include <netinet/tcp.h>
5566deb1c1SSepherosa Ziehau 
566c8d8eccSSepherosa Ziehau #include <net/bpf.h>
576c8d8eccSSepherosa Ziehau #include <net/ethernet.h>
586c8d8eccSSepherosa Ziehau #include <net/if.h>
596c8d8eccSSepherosa Ziehau #include <net/if_arp.h>
606c8d8eccSSepherosa Ziehau #include <net/if_dl.h>
616c8d8eccSSepherosa Ziehau #include <net/if_media.h>
626c8d8eccSSepherosa Ziehau #include <net/if_types.h>
636c8d8eccSSepherosa Ziehau #include <net/ifq_var.h>
646c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_var.h>
656c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_ether.h>
666c8d8eccSSepherosa Ziehau 
676c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/mii.h>
686c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/miivar.h>
696c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/brgphyreg.h>
706c8d8eccSSepherosa Ziehau 
716c8d8eccSSepherosa Ziehau #include <bus/pci/pcidevs.h>
726c8d8eccSSepherosa Ziehau #include <bus/pci/pcireg.h>
736c8d8eccSSepherosa Ziehau #include <bus/pci/pcivar.h>
746c8d8eccSSepherosa Ziehau 
756c8d8eccSSepherosa Ziehau #include <dev/netif/bge/if_bgereg.h>
766c8d8eccSSepherosa Ziehau #include <dev/netif/bnx/if_bnxvar.h>
776c8d8eccSSepherosa Ziehau 
786c8d8eccSSepherosa Ziehau /* "device miibus" required.  See GENERIC if you get errors here. */
796c8d8eccSSepherosa Ziehau #include "miibus_if.h"
806c8d8eccSSepherosa Ziehau 
813b18363fSSepherosa Ziehau #define BNX_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
826c8d8eccSSepherosa Ziehau 
83df9ccc98SSepherosa Ziehau #define BNX_INTR_CKINTVL	((10 * hz) / 1000)	/* 10ms */
84df9ccc98SSepherosa Ziehau 
856c8d8eccSSepherosa Ziehau static const struct bnx_type {
866c8d8eccSSepherosa Ziehau 	uint16_t		bnx_vid;
876c8d8eccSSepherosa Ziehau 	uint16_t		bnx_did;
886c8d8eccSSepherosa Ziehau 	char			*bnx_name;
896c8d8eccSSepherosa Ziehau } bnx_devs[] = {
906c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717,
916c8d8eccSSepherosa Ziehau 		"Broadcom BCM5717 Gigabit Ethernet" },
926c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5718,
936c8d8eccSSepherosa Ziehau 		"Broadcom BCM5718 Gigabit Ethernet" },
946c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5719,
956c8d8eccSSepherosa Ziehau 		"Broadcom BCM5719 Gigabit Ethernet" },
966c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5720_ALT,
976c8d8eccSSepherosa Ziehau 		"Broadcom BCM5720 Gigabit Ethernet" },
986c8d8eccSSepherosa Ziehau 
996c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
1006c8d8eccSSepherosa Ziehau 		"Broadcom BCM57761 Gigabit Ethernet" },
10132ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
10232ff3c80SSepherosa Ziehau 		"Broadcom BCM57762 Gigabit Ethernet" },
1036c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
1046c8d8eccSSepherosa Ziehau 		"Broadcom BCM57765 Gigabit Ethernet" },
10532ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
10632ff3c80SSepherosa Ziehau 		"Broadcom BCM57766 Gigabit Ethernet" },
10732ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
10832ff3c80SSepherosa Ziehau 		"Broadcom BCM57781 Gigabit Ethernet" },
10932ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
11032ff3c80SSepherosa Ziehau 		"Broadcom BCM57782 Gigabit Ethernet" },
1116c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
1126c8d8eccSSepherosa Ziehau 		"Broadcom BCM57785 Gigabit Ethernet" },
11332ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
11432ff3c80SSepherosa Ziehau 		"Broadcom BCM57786 Gigabit Ethernet" },
11532ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
11632ff3c80SSepherosa Ziehau 		"Broadcom BCM57791 Fast Ethernet" },
1176c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
1186c8d8eccSSepherosa Ziehau 		"Broadcom BCM57795 Fast Ethernet" },
1196c8d8eccSSepherosa Ziehau 
1206c8d8eccSSepherosa Ziehau 	{ 0, 0, NULL }
1216c8d8eccSSepherosa Ziehau };
1226c8d8eccSSepherosa Ziehau 
1236c8d8eccSSepherosa Ziehau #define BNX_IS_JUMBO_CAPABLE(sc)	((sc)->bnx_flags & BNX_FLAG_JUMBO)
1246c8d8eccSSepherosa Ziehau #define BNX_IS_5717_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
125f368d0d9SSepherosa Ziehau #define BNX_IS_57765_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
126f368d0d9SSepherosa Ziehau #define BNX_IS_57765_FAMILY(sc)	 \
127f368d0d9SSepherosa Ziehau 	((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
1286c8d8eccSSepherosa Ziehau 
1296c8d8eccSSepherosa Ziehau typedef int	(*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
1306c8d8eccSSepherosa Ziehau 
1316c8d8eccSSepherosa Ziehau static int	bnx_probe(device_t);
1326c8d8eccSSepherosa Ziehau static int	bnx_attach(device_t);
1336c8d8eccSSepherosa Ziehau static int	bnx_detach(device_t);
1346c8d8eccSSepherosa Ziehau static void	bnx_shutdown(device_t);
1356c8d8eccSSepherosa Ziehau static int	bnx_suspend(device_t);
1366c8d8eccSSepherosa Ziehau static int	bnx_resume(device_t);
1376c8d8eccSSepherosa Ziehau static int	bnx_miibus_readreg(device_t, int, int);
1386c8d8eccSSepherosa Ziehau static int	bnx_miibus_writereg(device_t, int, int, int);
1396c8d8eccSSepherosa Ziehau static void	bnx_miibus_statchg(device_t);
1406c8d8eccSSepherosa Ziehau 
1416c8d8eccSSepherosa Ziehau #ifdef DEVICE_POLLING
1426c8d8eccSSepherosa Ziehau static void	bnx_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
1436c8d8eccSSepherosa Ziehau #endif
1446c8d8eccSSepherosa Ziehau static void	bnx_intr_legacy(void *);
1456c8d8eccSSepherosa Ziehau static void	bnx_msi(void *);
1466c8d8eccSSepherosa Ziehau static void	bnx_msi_oneshot(void *);
1476c8d8eccSSepherosa Ziehau static void	bnx_intr(struct bnx_softc *);
1486c8d8eccSSepherosa Ziehau static void	bnx_enable_intr(struct bnx_softc *);
1496c8d8eccSSepherosa Ziehau static void	bnx_disable_intr(struct bnx_softc *);
1506c8d8eccSSepherosa Ziehau static void	bnx_txeof(struct bnx_softc *, uint16_t);
1516c8d8eccSSepherosa Ziehau static void	bnx_rxeof(struct bnx_softc *, uint16_t);
1526c8d8eccSSepherosa Ziehau 
1536c8d8eccSSepherosa Ziehau static void	bnx_start(struct ifnet *);
1546c8d8eccSSepherosa Ziehau static int	bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
1556c8d8eccSSepherosa Ziehau static void	bnx_init(void *);
1566c8d8eccSSepherosa Ziehau static void	bnx_stop(struct bnx_softc *);
1576c8d8eccSSepherosa Ziehau static void	bnx_watchdog(struct ifnet *);
1586c8d8eccSSepherosa Ziehau static int	bnx_ifmedia_upd(struct ifnet *);
1596c8d8eccSSepherosa Ziehau static void	bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
1606c8d8eccSSepherosa Ziehau static void	bnx_tick(void *);
1616c8d8eccSSepherosa Ziehau 
1626c8d8eccSSepherosa Ziehau static int	bnx_alloc_jumbo_mem(struct bnx_softc *);
1636c8d8eccSSepherosa Ziehau static void	bnx_free_jumbo_mem(struct bnx_softc *);
1646c8d8eccSSepherosa Ziehau static struct bnx_jslot
1656c8d8eccSSepherosa Ziehau 		*bnx_jalloc(struct bnx_softc *);
1666c8d8eccSSepherosa Ziehau static void	bnx_jfree(void *);
1676c8d8eccSSepherosa Ziehau static void	bnx_jref(void *);
1686c8d8eccSSepherosa Ziehau static int	bnx_newbuf_std(struct bnx_softc *, int, int);
1696c8d8eccSSepherosa Ziehau static int	bnx_newbuf_jumbo(struct bnx_softc *, int, int);
1706c8d8eccSSepherosa Ziehau static void	bnx_setup_rxdesc_std(struct bnx_softc *, int);
1716c8d8eccSSepherosa Ziehau static void	bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
1726c8d8eccSSepherosa Ziehau static int	bnx_init_rx_ring_std(struct bnx_softc *);
1736c8d8eccSSepherosa Ziehau static void	bnx_free_rx_ring_std(struct bnx_softc *);
1746c8d8eccSSepherosa Ziehau static int	bnx_init_rx_ring_jumbo(struct bnx_softc *);
1756c8d8eccSSepherosa Ziehau static void	bnx_free_rx_ring_jumbo(struct bnx_softc *);
1766c8d8eccSSepherosa Ziehau static void	bnx_free_tx_ring(struct bnx_softc *);
1776c8d8eccSSepherosa Ziehau static int	bnx_init_tx_ring(struct bnx_softc *);
1786c8d8eccSSepherosa Ziehau static int	bnx_dma_alloc(struct bnx_softc *);
1796c8d8eccSSepherosa Ziehau static void	bnx_dma_free(struct bnx_softc *);
1806c8d8eccSSepherosa Ziehau static int	bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
1816c8d8eccSSepherosa Ziehau 		    bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
1826c8d8eccSSepherosa Ziehau static void	bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
1836c8d8eccSSepherosa Ziehau static struct mbuf *
1846c8d8eccSSepherosa Ziehau 		bnx_defrag_shortdma(struct mbuf *);
1856c8d8eccSSepherosa Ziehau static int	bnx_encap(struct bnx_softc *, struct mbuf **, uint32_t *);
18666deb1c1SSepherosa Ziehau static int	bnx_setup_tso(struct bnx_softc *, struct mbuf **,
18766deb1c1SSepherosa Ziehau 		    uint16_t *, uint16_t *);
1886c8d8eccSSepherosa Ziehau 
1896c8d8eccSSepherosa Ziehau static void	bnx_reset(struct bnx_softc *);
1906c8d8eccSSepherosa Ziehau static int	bnx_chipinit(struct bnx_softc *);
1916c8d8eccSSepherosa Ziehau static int	bnx_blockinit(struct bnx_softc *);
1926c8d8eccSSepherosa Ziehau static void	bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
1936c8d8eccSSepherosa Ziehau static void	bnx_enable_msi(struct bnx_softc *sc);
1946c8d8eccSSepherosa Ziehau static void	bnx_setmulti(struct bnx_softc *);
1956c8d8eccSSepherosa Ziehau static void	bnx_setpromisc(struct bnx_softc *);
1966c8d8eccSSepherosa Ziehau static void	bnx_stats_update_regs(struct bnx_softc *);
1976c8d8eccSSepherosa Ziehau static uint32_t	bnx_dma_swap_options(struct bnx_softc *);
1986c8d8eccSSepherosa Ziehau 
1996c8d8eccSSepherosa Ziehau static uint32_t	bnx_readmem_ind(struct bnx_softc *, uint32_t);
2006c8d8eccSSepherosa Ziehau static void	bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
2016c8d8eccSSepherosa Ziehau #ifdef notdef
2026c8d8eccSSepherosa Ziehau static uint32_t	bnx_readreg_ind(struct bnx_softc *, uint32_t);
2036c8d8eccSSepherosa Ziehau #endif
2046c8d8eccSSepherosa Ziehau static void	bnx_writereg_ind(struct bnx_softc *, uint32_t, uint32_t);
2056c8d8eccSSepherosa Ziehau static void	bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
2066c8d8eccSSepherosa Ziehau static void	bnx_writembx(struct bnx_softc *, int, int);
2076c8d8eccSSepherosa Ziehau static uint8_t	bnx_nvram_getbyte(struct bnx_softc *, int, uint8_t *);
2086c8d8eccSSepherosa Ziehau static int	bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
2096c8d8eccSSepherosa Ziehau static uint8_t	bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
2106c8d8eccSSepherosa Ziehau static int	bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
2116c8d8eccSSepherosa Ziehau 
2126c8d8eccSSepherosa Ziehau static void	bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
2136c8d8eccSSepherosa Ziehau static void	bnx_copper_link_upd(struct bnx_softc *, uint32_t);
2146c8d8eccSSepherosa Ziehau static void	bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
2156c8d8eccSSepherosa Ziehau static void	bnx_link_poll(struct bnx_softc *);
2166c8d8eccSSepherosa Ziehau 
2176c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
2186c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
2196c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
2206c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
2216c8d8eccSSepherosa Ziehau 
2226c8d8eccSSepherosa Ziehau static void	bnx_coal_change(struct bnx_softc *);
2236c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
2246c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
2256c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
2266c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
2276c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
2286c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
2296c8d8eccSSepherosa Ziehau static int	bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
2306c8d8eccSSepherosa Ziehau 		    int, int, uint32_t);
2316c8d8eccSSepherosa Ziehau 
2326c8d8eccSSepherosa Ziehau static int	bnx_msi_enable = 1;
2336c8d8eccSSepherosa Ziehau TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
2346c8d8eccSSepherosa Ziehau 
2356c8d8eccSSepherosa Ziehau static device_method_t bnx_methods[] = {
2366c8d8eccSSepherosa Ziehau 	/* Device interface */
2376c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_probe,		bnx_probe),
2386c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_attach,	bnx_attach),
2396c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_detach,	bnx_detach),
2406c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_shutdown,	bnx_shutdown),
2416c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_suspend,	bnx_suspend),
2426c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_resume,	bnx_resume),
2436c8d8eccSSepherosa Ziehau 
2446c8d8eccSSepherosa Ziehau 	/* bus interface */
2456c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
2466c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
2476c8d8eccSSepherosa Ziehau 
2486c8d8eccSSepherosa Ziehau 	/* MII interface */
2496c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_readreg,	bnx_miibus_readreg),
2506c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_writereg,	bnx_miibus_writereg),
2516c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_statchg,	bnx_miibus_statchg),
2526c8d8eccSSepherosa Ziehau 
2536c8d8eccSSepherosa Ziehau 	{ 0, 0 }
2546c8d8eccSSepherosa Ziehau };
2556c8d8eccSSepherosa Ziehau 
2566c8d8eccSSepherosa Ziehau static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
2576c8d8eccSSepherosa Ziehau static devclass_t bnx_devclass;
2586c8d8eccSSepherosa Ziehau 
2596c8d8eccSSepherosa Ziehau DECLARE_DUMMY_MODULE(if_bnx);
2606c8d8eccSSepherosa Ziehau DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
2616c8d8eccSSepherosa Ziehau DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
2626c8d8eccSSepherosa Ziehau 
2636c8d8eccSSepherosa Ziehau static uint32_t
2646c8d8eccSSepherosa Ziehau bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
2656c8d8eccSSepherosa Ziehau {
2666c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
2676c8d8eccSSepherosa Ziehau 	uint32_t val;
2686c8d8eccSSepherosa Ziehau 
2696c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
2706c8d8eccSSepherosa Ziehau 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
2716c8d8eccSSepherosa Ziehau 		return 0;
2726c8d8eccSSepherosa Ziehau 
2736c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
2746c8d8eccSSepherosa Ziehau 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
2756c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
2766c8d8eccSSepherosa Ziehau 	return (val);
2776c8d8eccSSepherosa Ziehau }
2786c8d8eccSSepherosa Ziehau 
2796c8d8eccSSepherosa Ziehau static void
2806c8d8eccSSepherosa Ziehau bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
2816c8d8eccSSepherosa Ziehau {
2826c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
2836c8d8eccSSepherosa Ziehau 
2846c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
2856c8d8eccSSepherosa Ziehau 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
2866c8d8eccSSepherosa Ziehau 		return;
2876c8d8eccSSepherosa Ziehau 
2886c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
2896c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
2906c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
2916c8d8eccSSepherosa Ziehau }
2926c8d8eccSSepherosa Ziehau 
2936c8d8eccSSepherosa Ziehau #ifdef notdef
2946c8d8eccSSepherosa Ziehau static uint32_t
2956c8d8eccSSepherosa Ziehau bnx_readreg_ind(struct bnx_softc *sc, uin32_t off)
2966c8d8eccSSepherosa Ziehau {
2976c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
2986c8d8eccSSepherosa Ziehau 
2996c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
3006c8d8eccSSepherosa Ziehau 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
3016c8d8eccSSepherosa Ziehau }
3026c8d8eccSSepherosa Ziehau #endif
3036c8d8eccSSepherosa Ziehau 
3046c8d8eccSSepherosa Ziehau static void
3056c8d8eccSSepherosa Ziehau bnx_writereg_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
3066c8d8eccSSepherosa Ziehau {
3076c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
3086c8d8eccSSepherosa Ziehau 
3096c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
3106c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
3116c8d8eccSSepherosa Ziehau }
3126c8d8eccSSepherosa Ziehau 
3136c8d8eccSSepherosa Ziehau static void
3146c8d8eccSSepherosa Ziehau bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
3156c8d8eccSSepherosa Ziehau {
3166c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
3176c8d8eccSSepherosa Ziehau }
3186c8d8eccSSepherosa Ziehau 
3196c8d8eccSSepherosa Ziehau static void
3206c8d8eccSSepherosa Ziehau bnx_writembx(struct bnx_softc *sc, int off, int val)
3216c8d8eccSSepherosa Ziehau {
3226c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906)
3236c8d8eccSSepherosa Ziehau 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
3246c8d8eccSSepherosa Ziehau 
3256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
3266c8d8eccSSepherosa Ziehau }
3276c8d8eccSSepherosa Ziehau 
3286c8d8eccSSepherosa Ziehau static uint8_t
3296c8d8eccSSepherosa Ziehau bnx_nvram_getbyte(struct bnx_softc *sc, int addr, uint8_t *dest)
3306c8d8eccSSepherosa Ziehau {
3316c8d8eccSSepherosa Ziehau 	uint32_t access, byte = 0;
3326c8d8eccSSepherosa Ziehau 	int i;
3336c8d8eccSSepherosa Ziehau 
3346c8d8eccSSepherosa Ziehau 	/* Lock. */
3356c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
3366c8d8eccSSepherosa Ziehau 	for (i = 0; i < 8000; i++) {
3376c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
3386c8d8eccSSepherosa Ziehau 			break;
3396c8d8eccSSepherosa Ziehau 		DELAY(20);
3406c8d8eccSSepherosa Ziehau 	}
3416c8d8eccSSepherosa Ziehau 	if (i == 8000)
3426c8d8eccSSepherosa Ziehau 		return (1);
3436c8d8eccSSepherosa Ziehau 
3446c8d8eccSSepherosa Ziehau 	/* Enable access. */
3456c8d8eccSSepherosa Ziehau 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
3466c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
3476c8d8eccSSepherosa Ziehau 
3486c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
3496c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
3506c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT * 10; i++) {
3516c8d8eccSSepherosa Ziehau 		DELAY(10);
3526c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
3536c8d8eccSSepherosa Ziehau 			DELAY(10);
3546c8d8eccSSepherosa Ziehau 			break;
3556c8d8eccSSepherosa Ziehau 		}
3566c8d8eccSSepherosa Ziehau 	}
3576c8d8eccSSepherosa Ziehau 
3586c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT * 10) {
3596c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "nvram read timed out\n");
3606c8d8eccSSepherosa Ziehau 		return (1);
3616c8d8eccSSepherosa Ziehau 	}
3626c8d8eccSSepherosa Ziehau 
3636c8d8eccSSepherosa Ziehau 	/* Get result. */
3646c8d8eccSSepherosa Ziehau 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
3656c8d8eccSSepherosa Ziehau 
3666c8d8eccSSepherosa Ziehau 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
3676c8d8eccSSepherosa Ziehau 
3686c8d8eccSSepherosa Ziehau 	/* Disable access. */
3696c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
3706c8d8eccSSepherosa Ziehau 
3716c8d8eccSSepherosa Ziehau 	/* Unlock. */
3726c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
3736c8d8eccSSepherosa Ziehau 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
3746c8d8eccSSepherosa Ziehau 
3756c8d8eccSSepherosa Ziehau 	return (0);
3766c8d8eccSSepherosa Ziehau }
3776c8d8eccSSepherosa Ziehau 
3786c8d8eccSSepherosa Ziehau /*
3796c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from NVRAM.
3806c8d8eccSSepherosa Ziehau  */
3816c8d8eccSSepherosa Ziehau static int
3826c8d8eccSSepherosa Ziehau bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
3836c8d8eccSSepherosa Ziehau {
3846c8d8eccSSepherosa Ziehau 	int err = 0, i;
3856c8d8eccSSepherosa Ziehau 	uint8_t byte = 0;
3866c8d8eccSSepherosa Ziehau 
3876c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
3886c8d8eccSSepherosa Ziehau 		return (1);
3896c8d8eccSSepherosa Ziehau 
3906c8d8eccSSepherosa Ziehau 	for (i = 0; i < cnt; i++) {
3916c8d8eccSSepherosa Ziehau 		err = bnx_nvram_getbyte(sc, off + i, &byte);
3926c8d8eccSSepherosa Ziehau 		if (err)
3936c8d8eccSSepherosa Ziehau 			break;
3946c8d8eccSSepherosa Ziehau 		*(dest + i) = byte;
3956c8d8eccSSepherosa Ziehau 	}
3966c8d8eccSSepherosa Ziehau 
3976c8d8eccSSepherosa Ziehau 	return (err ? 1 : 0);
3986c8d8eccSSepherosa Ziehau }
3996c8d8eccSSepherosa Ziehau 
4006c8d8eccSSepherosa Ziehau /*
4016c8d8eccSSepherosa Ziehau  * Read a byte of data stored in the EEPROM at address 'addr.' The
4026c8d8eccSSepherosa Ziehau  * BCM570x supports both the traditional bitbang interface and an
4036c8d8eccSSepherosa Ziehau  * auto access interface for reading the EEPROM. We use the auto
4046c8d8eccSSepherosa Ziehau  * access method.
4056c8d8eccSSepherosa Ziehau  */
4066c8d8eccSSepherosa Ziehau static uint8_t
4076c8d8eccSSepherosa Ziehau bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
4086c8d8eccSSepherosa Ziehau {
4096c8d8eccSSepherosa Ziehau 	int i;
4106c8d8eccSSepherosa Ziehau 	uint32_t byte = 0;
4116c8d8eccSSepherosa Ziehau 
4126c8d8eccSSepherosa Ziehau 	/*
4136c8d8eccSSepherosa Ziehau 	 * Enable use of auto EEPROM access so we can avoid
4146c8d8eccSSepherosa Ziehau 	 * having to use the bitbang method.
4156c8d8eccSSepherosa Ziehau 	 */
4166c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
4176c8d8eccSSepherosa Ziehau 
4186c8d8eccSSepherosa Ziehau 	/* Reset the EEPROM, load the clock period. */
4196c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR,
4206c8d8eccSSepherosa Ziehau 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
4216c8d8eccSSepherosa Ziehau 	DELAY(20);
4226c8d8eccSSepherosa Ziehau 
4236c8d8eccSSepherosa Ziehau 	/* Issue the read EEPROM command. */
4246c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
4256c8d8eccSSepherosa Ziehau 
4266c8d8eccSSepherosa Ziehau 	/* Wait for completion */
4276c8d8eccSSepherosa Ziehau 	for(i = 0; i < BNX_TIMEOUT * 10; i++) {
4286c8d8eccSSepherosa Ziehau 		DELAY(10);
4296c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
4306c8d8eccSSepherosa Ziehau 			break;
4316c8d8eccSSepherosa Ziehau 	}
4326c8d8eccSSepherosa Ziehau 
4336c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
4346c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
4356c8d8eccSSepherosa Ziehau 		return(1);
4366c8d8eccSSepherosa Ziehau 	}
4376c8d8eccSSepherosa Ziehau 
4386c8d8eccSSepherosa Ziehau 	/* Get result. */
4396c8d8eccSSepherosa Ziehau 	byte = CSR_READ_4(sc, BGE_EE_DATA);
4406c8d8eccSSepherosa Ziehau 
4416c8d8eccSSepherosa Ziehau         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
4426c8d8eccSSepherosa Ziehau 
4436c8d8eccSSepherosa Ziehau 	return(0);
4446c8d8eccSSepherosa Ziehau }
4456c8d8eccSSepherosa Ziehau 
4466c8d8eccSSepherosa Ziehau /*
4476c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from the EEPROM.
4486c8d8eccSSepherosa Ziehau  */
4496c8d8eccSSepherosa Ziehau static int
4506c8d8eccSSepherosa Ziehau bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
4516c8d8eccSSepherosa Ziehau {
4526c8d8eccSSepherosa Ziehau 	size_t i;
4536c8d8eccSSepherosa Ziehau 	int err;
4546c8d8eccSSepherosa Ziehau 	uint8_t byte;
4556c8d8eccSSepherosa Ziehau 
4566c8d8eccSSepherosa Ziehau 	for (byte = 0, err = 0, i = 0; i < len; i++) {
4576c8d8eccSSepherosa Ziehau 		err = bnx_eeprom_getbyte(sc, off + i, &byte);
4586c8d8eccSSepherosa Ziehau 		if (err)
4596c8d8eccSSepherosa Ziehau 			break;
4606c8d8eccSSepherosa Ziehau 		*(dest + i) = byte;
4616c8d8eccSSepherosa Ziehau 	}
4626c8d8eccSSepherosa Ziehau 
4636c8d8eccSSepherosa Ziehau 	return(err ? 1 : 0);
4646c8d8eccSSepherosa Ziehau }
4656c8d8eccSSepherosa Ziehau 
4666c8d8eccSSepherosa Ziehau static int
4676c8d8eccSSepherosa Ziehau bnx_miibus_readreg(device_t dev, int phy, int reg)
4686c8d8eccSSepherosa Ziehau {
4696c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
4706c8d8eccSSepherosa Ziehau 	uint32_t val;
4716c8d8eccSSepherosa Ziehau 	int i;
4726c8d8eccSSepherosa Ziehau 
4736c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
4746c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
4756c8d8eccSSepherosa Ziehau 
4766c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
4776c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4786c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
4796c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
4806c8d8eccSSepherosa Ziehau 		DELAY(80);
4816c8d8eccSSepherosa Ziehau 	}
4826c8d8eccSSepherosa Ziehau 
4836c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
4846c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
4856c8d8eccSSepherosa Ziehau 
4866c8d8eccSSepherosa Ziehau 	/* Poll for the PHY register access to complete. */
4876c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
4886c8d8eccSSepherosa Ziehau 		DELAY(10);
4896c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_MI_COMM);
4906c8d8eccSSepherosa Ziehau 		if ((val & BGE_MICOMM_BUSY) == 0) {
4916c8d8eccSSepherosa Ziehau 			DELAY(5);
4926c8d8eccSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_MI_COMM);
4936c8d8eccSSepherosa Ziehau 			break;
4946c8d8eccSSepherosa Ziehau 		}
4956c8d8eccSSepherosa Ziehau 	}
4966c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
4976c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY read timed out "
4986c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
4996c8d8eccSSepherosa Ziehau 		val = 0;
5006c8d8eccSSepherosa Ziehau 	}
5016c8d8eccSSepherosa Ziehau 
5026c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
5036c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5046c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
5056c8d8eccSSepherosa Ziehau 		DELAY(80);
5066c8d8eccSSepherosa Ziehau 	}
5076c8d8eccSSepherosa Ziehau 
5086c8d8eccSSepherosa Ziehau 	if (val & BGE_MICOMM_READFAIL)
5096c8d8eccSSepherosa Ziehau 		return 0;
5106c8d8eccSSepherosa Ziehau 
5116c8d8eccSSepherosa Ziehau 	return (val & 0xFFFF);
5126c8d8eccSSepherosa Ziehau }
5136c8d8eccSSepherosa Ziehau 
5146c8d8eccSSepherosa Ziehau static int
5156c8d8eccSSepherosa Ziehau bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
5166c8d8eccSSepherosa Ziehau {
5176c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
5186c8d8eccSSepherosa Ziehau 	int i;
5196c8d8eccSSepherosa Ziehau 
5206c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
5216c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
5226c8d8eccSSepherosa Ziehau 
5236c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
5246c8d8eccSSepherosa Ziehau 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
5256c8d8eccSSepherosa Ziehau 	       return 0;
5266c8d8eccSSepherosa Ziehau 
5276c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
5286c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5296c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
5306c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
5316c8d8eccSSepherosa Ziehau 		DELAY(80);
5326c8d8eccSSepherosa Ziehau 	}
5336c8d8eccSSepherosa Ziehau 
5346c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
5356c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
5366c8d8eccSSepherosa Ziehau 
5376c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
5386c8d8eccSSepherosa Ziehau 		DELAY(10);
5396c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
5406c8d8eccSSepherosa Ziehau 			DELAY(5);
5416c8d8eccSSepherosa Ziehau 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
5426c8d8eccSSepherosa Ziehau 			break;
5436c8d8eccSSepherosa Ziehau 		}
5446c8d8eccSSepherosa Ziehau 	}
5456c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
5466c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY write timed out "
5476c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val %d)\n", phy, reg, val);
5486c8d8eccSSepherosa Ziehau 	}
5496c8d8eccSSepherosa Ziehau 
5506c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
5516c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5526c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
5536c8d8eccSSepherosa Ziehau 		DELAY(80);
5546c8d8eccSSepherosa Ziehau 	}
5556c8d8eccSSepherosa Ziehau 
5566c8d8eccSSepherosa Ziehau 	return 0;
5576c8d8eccSSepherosa Ziehau }
5586c8d8eccSSepherosa Ziehau 
5596c8d8eccSSepherosa Ziehau static void
5606c8d8eccSSepherosa Ziehau bnx_miibus_statchg(device_t dev)
5616c8d8eccSSepherosa Ziehau {
5626c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
5636c8d8eccSSepherosa Ziehau 	struct mii_data *mii;
5646c8d8eccSSepherosa Ziehau 
5656c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
5666c8d8eccSSepherosa Ziehau 	mii = device_get_softc(sc->bnx_miibus);
5676c8d8eccSSepherosa Ziehau 
5686c8d8eccSSepherosa Ziehau 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
5696c8d8eccSSepherosa Ziehau 	    (IFM_ACTIVE | IFM_AVALID)) {
5706c8d8eccSSepherosa Ziehau 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5716c8d8eccSSepherosa Ziehau 		case IFM_10_T:
5726c8d8eccSSepherosa Ziehau 		case IFM_100_TX:
5736c8d8eccSSepherosa Ziehau 			sc->bnx_link = 1;
5746c8d8eccSSepherosa Ziehau 			break;
5756c8d8eccSSepherosa Ziehau 		case IFM_1000_T:
5766c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
5776c8d8eccSSepherosa Ziehau 		case IFM_2500_SX:
5786c8d8eccSSepherosa Ziehau 			if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
5796c8d8eccSSepherosa Ziehau 				sc->bnx_link = 1;
5806c8d8eccSSepherosa Ziehau 			else
5816c8d8eccSSepherosa Ziehau 				sc->bnx_link = 0;
5826c8d8eccSSepherosa Ziehau 			break;
5836c8d8eccSSepherosa Ziehau 		default:
5846c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
5856c8d8eccSSepherosa Ziehau 			break;
5866c8d8eccSSepherosa Ziehau 		}
5876c8d8eccSSepherosa Ziehau 	} else {
5886c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
5896c8d8eccSSepherosa Ziehau 	}
5906c8d8eccSSepherosa Ziehau 	if (sc->bnx_link == 0)
5916c8d8eccSSepherosa Ziehau 		return;
5926c8d8eccSSepherosa Ziehau 
5936c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
5946c8d8eccSSepherosa Ziehau 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
5956c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
5966c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
5976c8d8eccSSepherosa Ziehau 	} else {
5986c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
5996c8d8eccSSepherosa Ziehau 	}
6006c8d8eccSSepherosa Ziehau 
6016c8d8eccSSepherosa Ziehau 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
6026c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6036c8d8eccSSepherosa Ziehau 	} else {
6046c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6056c8d8eccSSepherosa Ziehau 	}
6066c8d8eccSSepherosa Ziehau }
6076c8d8eccSSepherosa Ziehau 
6086c8d8eccSSepherosa Ziehau /*
6096c8d8eccSSepherosa Ziehau  * Memory management for jumbo frames.
6106c8d8eccSSepherosa Ziehau  */
6116c8d8eccSSepherosa Ziehau static int
6126c8d8eccSSepherosa Ziehau bnx_alloc_jumbo_mem(struct bnx_softc *sc)
6136c8d8eccSSepherosa Ziehau {
6146c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
6156c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
6166c8d8eccSSepherosa Ziehau 	uint8_t *ptr;
6176c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
6186c8d8eccSSepherosa Ziehau 	int i, error;
6196c8d8eccSSepherosa Ziehau 
6206c8d8eccSSepherosa Ziehau 	/*
6216c8d8eccSSepherosa Ziehau 	 * Create tag for jumbo mbufs.
6226c8d8eccSSepherosa Ziehau 	 * This is really a bit of a kludge. We allocate a special
6236c8d8eccSSepherosa Ziehau 	 * jumbo buffer pool which (thanks to the way our DMA
6246c8d8eccSSepherosa Ziehau 	 * memory allocation works) will consist of contiguous
6256c8d8eccSSepherosa Ziehau 	 * pages. This means that even though a jumbo buffer might
6266c8d8eccSSepherosa Ziehau 	 * be larger than a page size, we don't really need to
6276c8d8eccSSepherosa Ziehau 	 * map it into more than one DMA segment. However, the
6286c8d8eccSSepherosa Ziehau 	 * default mbuf tag will result in multi-segment mappings,
6296c8d8eccSSepherosa Ziehau 	 * so we have to create a special jumbo mbuf tag that
6306c8d8eccSSepherosa Ziehau 	 * lets us get away with mapping the jumbo buffers as
6316c8d8eccSSepherosa Ziehau 	 * a single segment. I think eventually the driver should
6326c8d8eccSSepherosa Ziehau 	 * be changed so that it uses ordinary mbufs and cluster
6336c8d8eccSSepherosa Ziehau 	 * buffers, i.e. jumbo frames can span multiple DMA
6346c8d8eccSSepherosa Ziehau 	 * descriptors. But that's a project for another day.
6356c8d8eccSSepherosa Ziehau 	 */
6366c8d8eccSSepherosa Ziehau 
6376c8d8eccSSepherosa Ziehau 	/*
6386c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo RX ring.
6396c8d8eccSSepherosa Ziehau 	 */
6406c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
6416c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
6426c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
6436c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
6446c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
6456c8d8eccSSepherosa Ziehau 	if (error) {
6466c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo RX ring\n");
6476c8d8eccSSepherosa Ziehau 		return error;
6486c8d8eccSSepherosa Ziehau 	}
6496c8d8eccSSepherosa Ziehau 
6506c8d8eccSSepherosa Ziehau 	/*
6516c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo buffer block.
6526c8d8eccSSepherosa Ziehau 	 */
6536c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BNX_JMEM,
6546c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_tag,
6556c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_map,
6566c8d8eccSSepherosa Ziehau 				    (void **)&sc->bnx_ldata.bnx_jumbo_buf,
6576c8d8eccSSepherosa Ziehau 				    &paddr);
6586c8d8eccSSepherosa Ziehau 	if (error) {
6596c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo buffer\n");
6606c8d8eccSSepherosa Ziehau 		return error;
6616c8d8eccSSepherosa Ziehau 	}
6626c8d8eccSSepherosa Ziehau 
6636c8d8eccSSepherosa Ziehau 	SLIST_INIT(&sc->bnx_jfree_listhead);
6646c8d8eccSSepherosa Ziehau 
6656c8d8eccSSepherosa Ziehau 	/*
6666c8d8eccSSepherosa Ziehau 	 * Now divide it up into 9K pieces and save the addresses
6676c8d8eccSSepherosa Ziehau 	 * in an array. Note that we play an evil trick here by using
6686c8d8eccSSepherosa Ziehau 	 * the first few bytes in the buffer to hold the the address
6696c8d8eccSSepherosa Ziehau 	 * of the softc structure for this interface. This is because
6706c8d8eccSSepherosa Ziehau 	 * bnx_jfree() needs it, but it is called by the mbuf management
6716c8d8eccSSepherosa Ziehau 	 * code which will not pass it to us explicitly.
6726c8d8eccSSepherosa Ziehau 	 */
6736c8d8eccSSepherosa Ziehau 	for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
6746c8d8eccSSepherosa Ziehau 		entry = &sc->bnx_cdata.bnx_jslots[i];
6756c8d8eccSSepherosa Ziehau 		entry->bnx_sc = sc;
6766c8d8eccSSepherosa Ziehau 		entry->bnx_buf = ptr;
6776c8d8eccSSepherosa Ziehau 		entry->bnx_paddr = paddr;
6786c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 0;
6796c8d8eccSSepherosa Ziehau 		entry->bnx_slot = i;
6806c8d8eccSSepherosa Ziehau 		SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
6816c8d8eccSSepherosa Ziehau 
6826c8d8eccSSepherosa Ziehau 		ptr += BNX_JLEN;
6836c8d8eccSSepherosa Ziehau 		paddr += BNX_JLEN;
6846c8d8eccSSepherosa Ziehau 	}
6856c8d8eccSSepherosa Ziehau 	return 0;
6866c8d8eccSSepherosa Ziehau }
6876c8d8eccSSepherosa Ziehau 
6886c8d8eccSSepherosa Ziehau static void
6896c8d8eccSSepherosa Ziehau bnx_free_jumbo_mem(struct bnx_softc *sc)
6906c8d8eccSSepherosa Ziehau {
6916c8d8eccSSepherosa Ziehau 	/* Destroy jumbo RX ring. */
6926c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
6936c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_rx_jumbo_ring_map,
6946c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_rx_jumbo_ring);
6956c8d8eccSSepherosa Ziehau 
6966c8d8eccSSepherosa Ziehau 	/* Destroy jumbo buffer block. */
6976c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
6986c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_jumbo_map,
6996c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_jumbo_buf);
7006c8d8eccSSepherosa Ziehau }
7016c8d8eccSSepherosa Ziehau 
7026c8d8eccSSepherosa Ziehau /*
7036c8d8eccSSepherosa Ziehau  * Allocate a jumbo buffer.
7046c8d8eccSSepherosa Ziehau  */
7056c8d8eccSSepherosa Ziehau static struct bnx_jslot *
7066c8d8eccSSepherosa Ziehau bnx_jalloc(struct bnx_softc *sc)
7076c8d8eccSSepherosa Ziehau {
7086c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
7096c8d8eccSSepherosa Ziehau 
7106c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(&sc->bnx_jslot_serializer);
7116c8d8eccSSepherosa Ziehau 	entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
7126c8d8eccSSepherosa Ziehau 	if (entry) {
7136c8d8eccSSepherosa Ziehau 		SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
7146c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 1;
7156c8d8eccSSepherosa Ziehau 	} else {
7166c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
7176c8d8eccSSepherosa Ziehau 	}
7186c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(&sc->bnx_jslot_serializer);
7196c8d8eccSSepherosa Ziehau 	return(entry);
7206c8d8eccSSepherosa Ziehau }
7216c8d8eccSSepherosa Ziehau 
7226c8d8eccSSepherosa Ziehau /*
7236c8d8eccSSepherosa Ziehau  * Adjust usage count on a jumbo buffer.
7246c8d8eccSSepherosa Ziehau  */
7256c8d8eccSSepherosa Ziehau static void
7266c8d8eccSSepherosa Ziehau bnx_jref(void *arg)
7276c8d8eccSSepherosa Ziehau {
7286c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
7296c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
7306c8d8eccSSepherosa Ziehau 
7316c8d8eccSSepherosa Ziehau 	if (sc == NULL)
7326c8d8eccSSepherosa Ziehau 		panic("bnx_jref: can't find softc pointer!");
7336c8d8eccSSepherosa Ziehau 
7346c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
7356c8d8eccSSepherosa Ziehau 		panic("bnx_jref: asked to reference buffer "
7366c8d8eccSSepherosa Ziehau 		    "that we don't manage!");
7376c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
7386c8d8eccSSepherosa Ziehau 		panic("bnx_jref: buffer already free!");
7396c8d8eccSSepherosa Ziehau 	} else {
7406c8d8eccSSepherosa Ziehau 		atomic_add_int(&entry->bnx_inuse, 1);
7416c8d8eccSSepherosa Ziehau 	}
7426c8d8eccSSepherosa Ziehau }
7436c8d8eccSSepherosa Ziehau 
7446c8d8eccSSepherosa Ziehau /*
7456c8d8eccSSepherosa Ziehau  * Release a jumbo buffer.
7466c8d8eccSSepherosa Ziehau  */
7476c8d8eccSSepherosa Ziehau static void
7486c8d8eccSSepherosa Ziehau bnx_jfree(void *arg)
7496c8d8eccSSepherosa Ziehau {
7506c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
7516c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
7526c8d8eccSSepherosa Ziehau 
7536c8d8eccSSepherosa Ziehau 	if (sc == NULL)
7546c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: can't find softc pointer!");
7556c8d8eccSSepherosa Ziehau 
7566c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
7576c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: asked to free buffer that we don't manage!");
7586c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
7596c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: buffer already free!");
7606c8d8eccSSepherosa Ziehau 	} else {
7616c8d8eccSSepherosa Ziehau 		/*
7626c8d8eccSSepherosa Ziehau 		 * Possible MP race to 0, use the serializer.  The atomic insn
7636c8d8eccSSepherosa Ziehau 		 * is still needed for races against bnx_jref().
7646c8d8eccSSepherosa Ziehau 		 */
7656c8d8eccSSepherosa Ziehau 		lwkt_serialize_enter(&sc->bnx_jslot_serializer);
7666c8d8eccSSepherosa Ziehau 		atomic_subtract_int(&entry->bnx_inuse, 1);
7676c8d8eccSSepherosa Ziehau 		if (entry->bnx_inuse == 0) {
7686c8d8eccSSepherosa Ziehau 			SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
7696c8d8eccSSepherosa Ziehau 					  entry, jslot_link);
7706c8d8eccSSepherosa Ziehau 		}
7716c8d8eccSSepherosa Ziehau 		lwkt_serialize_exit(&sc->bnx_jslot_serializer);
7726c8d8eccSSepherosa Ziehau 	}
7736c8d8eccSSepherosa Ziehau }
7746c8d8eccSSepherosa Ziehau 
7756c8d8eccSSepherosa Ziehau 
7766c8d8eccSSepherosa Ziehau /*
7776c8d8eccSSepherosa Ziehau  * Intialize a standard receive ring descriptor.
7786c8d8eccSSepherosa Ziehau  */
7796c8d8eccSSepherosa Ziehau static int
7806c8d8eccSSepherosa Ziehau bnx_newbuf_std(struct bnx_softc *sc, int i, int init)
7816c8d8eccSSepherosa Ziehau {
7826c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
7836c8d8eccSSepherosa Ziehau 	bus_dma_segment_t seg;
7846c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
7856c8d8eccSSepherosa Ziehau 	int error, nsegs;
7866c8d8eccSSepherosa Ziehau 
7876c8d8eccSSepherosa Ziehau 	m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
7886c8d8eccSSepherosa Ziehau 	if (m_new == NULL)
7896c8d8eccSSepherosa Ziehau 		return ENOBUFS;
7906c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
7916c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
7926c8d8eccSSepherosa Ziehau 
7936c8d8eccSSepherosa Ziehau 	error = bus_dmamap_load_mbuf_segment(sc->bnx_cdata.bnx_rx_mtag,
7946c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_rx_tmpmap, m_new,
7956c8d8eccSSepherosa Ziehau 			&seg, 1, &nsegs, BUS_DMA_NOWAIT);
7966c8d8eccSSepherosa Ziehau 	if (error) {
7976c8d8eccSSepherosa Ziehau 		m_freem(m_new);
7986c8d8eccSSepherosa Ziehau 		return error;
7996c8d8eccSSepherosa Ziehau 	}
8006c8d8eccSSepherosa Ziehau 
8016c8d8eccSSepherosa Ziehau 	if (!init) {
8026c8d8eccSSepherosa Ziehau 		bus_dmamap_sync(sc->bnx_cdata.bnx_rx_mtag,
8036c8d8eccSSepherosa Ziehau 				sc->bnx_cdata.bnx_rx_std_dmamap[i],
8046c8d8eccSSepherosa Ziehau 				BUS_DMASYNC_POSTREAD);
8056c8d8eccSSepherosa Ziehau 		bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
8066c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_rx_std_dmamap[i]);
8076c8d8eccSSepherosa Ziehau 	}
8086c8d8eccSSepherosa Ziehau 
8096c8d8eccSSepherosa Ziehau 	map = sc->bnx_cdata.bnx_rx_tmpmap;
8106c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_tmpmap = sc->bnx_cdata.bnx_rx_std_dmamap[i];
8116c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_std_dmamap[i] = map;
8126c8d8eccSSepherosa Ziehau 
8136c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_std_chain[i].bnx_mbuf = m_new;
8146c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_std_chain[i].bnx_paddr = seg.ds_addr;
8156c8d8eccSSepherosa Ziehau 
8166c8d8eccSSepherosa Ziehau 	bnx_setup_rxdesc_std(sc, i);
8176c8d8eccSSepherosa Ziehau 	return 0;
8186c8d8eccSSepherosa Ziehau }
8196c8d8eccSSepherosa Ziehau 
8206c8d8eccSSepherosa Ziehau static void
8216c8d8eccSSepherosa Ziehau bnx_setup_rxdesc_std(struct bnx_softc *sc, int i)
8226c8d8eccSSepherosa Ziehau {
8236c8d8eccSSepherosa Ziehau 	struct bnx_rxchain *rc;
8246c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
8256c8d8eccSSepherosa Ziehau 
8266c8d8eccSSepherosa Ziehau 	rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
8276c8d8eccSSepherosa Ziehau 	r = &sc->bnx_ldata.bnx_rx_std_ring[i];
8286c8d8eccSSepherosa Ziehau 
8296c8d8eccSSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
8306c8d8eccSSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
8316c8d8eccSSepherosa Ziehau 	r->bge_len = rc->bnx_mbuf->m_len;
8326c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
8336c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END;
8346c8d8eccSSepherosa Ziehau }
8356c8d8eccSSepherosa Ziehau 
8366c8d8eccSSepherosa Ziehau /*
8376c8d8eccSSepherosa Ziehau  * Initialize a jumbo receive ring descriptor. This allocates
8386c8d8eccSSepherosa Ziehau  * a jumbo buffer from the pool managed internally by the driver.
8396c8d8eccSSepherosa Ziehau  */
8406c8d8eccSSepherosa Ziehau static int
8416c8d8eccSSepherosa Ziehau bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
8426c8d8eccSSepherosa Ziehau {
8436c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
8446c8d8eccSSepherosa Ziehau 	struct bnx_jslot *buf;
8456c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
8466c8d8eccSSepherosa Ziehau 
8476c8d8eccSSepherosa Ziehau 	/* Allocate the mbuf. */
8486c8d8eccSSepherosa Ziehau 	MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
8496c8d8eccSSepherosa Ziehau 	if (m_new == NULL)
8506c8d8eccSSepherosa Ziehau 		return ENOBUFS;
8516c8d8eccSSepherosa Ziehau 
8526c8d8eccSSepherosa Ziehau 	/* Allocate the jumbo buffer */
8536c8d8eccSSepherosa Ziehau 	buf = bnx_jalloc(sc);
8546c8d8eccSSepherosa Ziehau 	if (buf == NULL) {
8556c8d8eccSSepherosa Ziehau 		m_freem(m_new);
8566c8d8eccSSepherosa Ziehau 		return ENOBUFS;
8576c8d8eccSSepherosa Ziehau 	}
8586c8d8eccSSepherosa Ziehau 
8596c8d8eccSSepherosa Ziehau 	/* Attach the buffer to the mbuf. */
8606c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_arg = buf;
8616c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_buf = buf->bnx_buf;
8626c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_free = bnx_jfree;
8636c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_ref = bnx_jref;
8646c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
8656c8d8eccSSepherosa Ziehau 
8666c8d8eccSSepherosa Ziehau 	m_new->m_flags |= M_EXT;
8676c8d8eccSSepherosa Ziehau 
8686c8d8eccSSepherosa Ziehau 	m_new->m_data = m_new->m_ext.ext_buf;
8696c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
8706c8d8eccSSepherosa Ziehau 
8716c8d8eccSSepherosa Ziehau 	paddr = buf->bnx_paddr;
8726c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
8736c8d8eccSSepherosa Ziehau 	paddr += ETHER_ALIGN;
8746c8d8eccSSepherosa Ziehau 
8756c8d8eccSSepherosa Ziehau 	/* Save necessary information */
8766c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_mbuf = m_new;
8776c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_paddr = paddr;
8786c8d8eccSSepherosa Ziehau 
8796c8d8eccSSepherosa Ziehau 	/* Set up the descriptor. */
8806c8d8eccSSepherosa Ziehau 	bnx_setup_rxdesc_jumbo(sc, i);
8816c8d8eccSSepherosa Ziehau 	return 0;
8826c8d8eccSSepherosa Ziehau }
8836c8d8eccSSepherosa Ziehau 
8846c8d8eccSSepherosa Ziehau static void
8856c8d8eccSSepherosa Ziehau bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
8866c8d8eccSSepherosa Ziehau {
8876c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
8886c8d8eccSSepherosa Ziehau 	struct bnx_rxchain *rc;
8896c8d8eccSSepherosa Ziehau 
8906c8d8eccSSepherosa Ziehau 	r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
8916c8d8eccSSepherosa Ziehau 	rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
8926c8d8eccSSepherosa Ziehau 
8936c8d8eccSSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_paddr);
8946c8d8eccSSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_paddr);
8956c8d8eccSSepherosa Ziehau 	r->bge_len = rc->bnx_mbuf->m_len;
8966c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
8976c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
8986c8d8eccSSepherosa Ziehau }
8996c8d8eccSSepherosa Ziehau 
9006c8d8eccSSepherosa Ziehau static int
9016c8d8eccSSepherosa Ziehau bnx_init_rx_ring_std(struct bnx_softc *sc)
9026c8d8eccSSepherosa Ziehau {
9036c8d8eccSSepherosa Ziehau 	int i, error;
9046c8d8eccSSepherosa Ziehau 
9056c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
9066c8d8eccSSepherosa Ziehau 		error = bnx_newbuf_std(sc, i, 1);
9076c8d8eccSSepherosa Ziehau 		if (error)
9086c8d8eccSSepherosa Ziehau 			return error;
9096c8d8eccSSepherosa Ziehau 	};
9106c8d8eccSSepherosa Ziehau 
9116c8d8eccSSepherosa Ziehau 	sc->bnx_std = BGE_STD_RX_RING_CNT - 1;
9126c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
9136c8d8eccSSepherosa Ziehau 
9146c8d8eccSSepherosa Ziehau 	return(0);
9156c8d8eccSSepherosa Ziehau }
9166c8d8eccSSepherosa Ziehau 
9176c8d8eccSSepherosa Ziehau static void
9186c8d8eccSSepherosa Ziehau bnx_free_rx_ring_std(struct bnx_softc *sc)
9196c8d8eccSSepherosa Ziehau {
9206c8d8eccSSepherosa Ziehau 	int i;
9216c8d8eccSSepherosa Ziehau 
9226c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
9236c8d8eccSSepherosa Ziehau 		struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_std_chain[i];
9246c8d8eccSSepherosa Ziehau 
9256c8d8eccSSepherosa Ziehau 		if (rc->bnx_mbuf != NULL) {
9266c8d8eccSSepherosa Ziehau 			bus_dmamap_unload(sc->bnx_cdata.bnx_rx_mtag,
9276c8d8eccSSepherosa Ziehau 					  sc->bnx_cdata.bnx_rx_std_dmamap[i]);
9286c8d8eccSSepherosa Ziehau 			m_freem(rc->bnx_mbuf);
9296c8d8eccSSepherosa Ziehau 			rc->bnx_mbuf = NULL;
9306c8d8eccSSepherosa Ziehau 		}
9316c8d8eccSSepherosa Ziehau 		bzero(&sc->bnx_ldata.bnx_rx_std_ring[i],
9326c8d8eccSSepherosa Ziehau 		    sizeof(struct bge_rx_bd));
9336c8d8eccSSepherosa Ziehau 	}
9346c8d8eccSSepherosa Ziehau }
9356c8d8eccSSepherosa Ziehau 
9366c8d8eccSSepherosa Ziehau static int
9376c8d8eccSSepherosa Ziehau bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
9386c8d8eccSSepherosa Ziehau {
9396c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
9406c8d8eccSSepherosa Ziehau 	int i, error;
9416c8d8eccSSepherosa Ziehau 
9426c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
9436c8d8eccSSepherosa Ziehau 		error = bnx_newbuf_jumbo(sc, i, 1);
9446c8d8eccSSepherosa Ziehau 		if (error)
9456c8d8eccSSepherosa Ziehau 			return error;
9466c8d8eccSSepherosa Ziehau 	};
9476c8d8eccSSepherosa Ziehau 
9486c8d8eccSSepherosa Ziehau 	sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
9496c8d8eccSSepherosa Ziehau 
9506c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
9516c8d8eccSSepherosa Ziehau 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
9526c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
9536c8d8eccSSepherosa Ziehau 
9546c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
9556c8d8eccSSepherosa Ziehau 
9566c8d8eccSSepherosa Ziehau 	return(0);
9576c8d8eccSSepherosa Ziehau }
9586c8d8eccSSepherosa Ziehau 
9596c8d8eccSSepherosa Ziehau static void
9606c8d8eccSSepherosa Ziehau bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
9616c8d8eccSSepherosa Ziehau {
9626c8d8eccSSepherosa Ziehau 	int i;
9636c8d8eccSSepherosa Ziehau 
9646c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
9656c8d8eccSSepherosa Ziehau 		struct bnx_rxchain *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
9666c8d8eccSSepherosa Ziehau 
9676c8d8eccSSepherosa Ziehau 		if (rc->bnx_mbuf != NULL) {
9686c8d8eccSSepherosa Ziehau 			m_freem(rc->bnx_mbuf);
9696c8d8eccSSepherosa Ziehau 			rc->bnx_mbuf = NULL;
9706c8d8eccSSepherosa Ziehau 		}
9716c8d8eccSSepherosa Ziehau 		bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
9726c8d8eccSSepherosa Ziehau 		    sizeof(struct bge_rx_bd));
9736c8d8eccSSepherosa Ziehau 	}
9746c8d8eccSSepherosa Ziehau }
9756c8d8eccSSepherosa Ziehau 
9766c8d8eccSSepherosa Ziehau static void
9776c8d8eccSSepherosa Ziehau bnx_free_tx_ring(struct bnx_softc *sc)
9786c8d8eccSSepherosa Ziehau {
9796c8d8eccSSepherosa Ziehau 	int i;
9806c8d8eccSSepherosa Ziehau 
9816c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
9826c8d8eccSSepherosa Ziehau 		if (sc->bnx_cdata.bnx_tx_chain[i] != NULL) {
9836c8d8eccSSepherosa Ziehau 			bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
9846c8d8eccSSepherosa Ziehau 					  sc->bnx_cdata.bnx_tx_dmamap[i]);
9856c8d8eccSSepherosa Ziehau 			m_freem(sc->bnx_cdata.bnx_tx_chain[i]);
9866c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_tx_chain[i] = NULL;
9876c8d8eccSSepherosa Ziehau 		}
9886c8d8eccSSepherosa Ziehau 		bzero(&sc->bnx_ldata.bnx_tx_ring[i],
9896c8d8eccSSepherosa Ziehau 		    sizeof(struct bge_tx_bd));
9906c8d8eccSSepherosa Ziehau 	}
9916c8d8eccSSepherosa Ziehau }
9926c8d8eccSSepherosa Ziehau 
9936c8d8eccSSepherosa Ziehau static int
9946c8d8eccSSepherosa Ziehau bnx_init_tx_ring(struct bnx_softc *sc)
9956c8d8eccSSepherosa Ziehau {
9966c8d8eccSSepherosa Ziehau 	sc->bnx_txcnt = 0;
9976c8d8eccSSepherosa Ziehau 	sc->bnx_tx_saved_considx = 0;
9986c8d8eccSSepherosa Ziehau 	sc->bnx_tx_prodidx = 0;
9996c8d8eccSSepherosa Ziehau 
10006c8d8eccSSepherosa Ziehau 	/* Initialize transmit producer index for host-memory send ring. */
10016c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bnx_tx_prodidx);
10026c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
10036c8d8eccSSepherosa Ziehau 
10046c8d8eccSSepherosa Ziehau 	return(0);
10056c8d8eccSSepherosa Ziehau }
10066c8d8eccSSepherosa Ziehau 
10076c8d8eccSSepherosa Ziehau static void
10086c8d8eccSSepherosa Ziehau bnx_setmulti(struct bnx_softc *sc)
10096c8d8eccSSepherosa Ziehau {
10106c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
10116c8d8eccSSepherosa Ziehau 	struct ifmultiaddr *ifma;
10126c8d8eccSSepherosa Ziehau 	uint32_t hashes[4] = { 0, 0, 0, 0 };
10136c8d8eccSSepherosa Ziehau 	int h, i;
10146c8d8eccSSepherosa Ziehau 
10156c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
10166c8d8eccSSepherosa Ziehau 
10176c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
10186c8d8eccSSepherosa Ziehau 		for (i = 0; i < 4; i++)
10196c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
10206c8d8eccSSepherosa Ziehau 		return;
10216c8d8eccSSepherosa Ziehau 	}
10226c8d8eccSSepherosa Ziehau 
10236c8d8eccSSepherosa Ziehau 	/* First, zot all the existing filters. */
10246c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
10256c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
10266c8d8eccSSepherosa Ziehau 
10276c8d8eccSSepherosa Ziehau 	/* Now program new ones. */
10286c8d8eccSSepherosa Ziehau 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
10296c8d8eccSSepherosa Ziehau 		if (ifma->ifma_addr->sa_family != AF_LINK)
10306c8d8eccSSepherosa Ziehau 			continue;
10316c8d8eccSSepherosa Ziehau 		h = ether_crc32_le(
10326c8d8eccSSepherosa Ziehau 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
10336c8d8eccSSepherosa Ziehau 		    ETHER_ADDR_LEN) & 0x7f;
10346c8d8eccSSepherosa Ziehau 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
10356c8d8eccSSepherosa Ziehau 	}
10366c8d8eccSSepherosa Ziehau 
10376c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
10386c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
10396c8d8eccSSepherosa Ziehau }
10406c8d8eccSSepherosa Ziehau 
10416c8d8eccSSepherosa Ziehau /*
10426c8d8eccSSepherosa Ziehau  * Do endian, PCI and DMA initialization. Also check the on-board ROM
10436c8d8eccSSepherosa Ziehau  * self-test results.
10446c8d8eccSSepherosa Ziehau  */
10456c8d8eccSSepherosa Ziehau static int
10466c8d8eccSSepherosa Ziehau bnx_chipinit(struct bnx_softc *sc)
10476c8d8eccSSepherosa Ziehau {
10486c8d8eccSSepherosa Ziehau 	uint32_t dma_rw_ctl, mode_ctl;
10496c8d8eccSSepherosa Ziehau 	int i;
10506c8d8eccSSepherosa Ziehau 
10516c8d8eccSSepherosa Ziehau 	/* Set endian type before we access any non-PCI registers. */
10526c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
10536c8d8eccSSepherosa Ziehau 	    BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
10546c8d8eccSSepherosa Ziehau 
10556c8d8eccSSepherosa Ziehau 	/* Clear the MAC control register */
10566c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
10576c8d8eccSSepherosa Ziehau 
10586c8d8eccSSepherosa Ziehau 	/*
10596c8d8eccSSepherosa Ziehau 	 * Clear the MAC statistics block in the NIC's
10606c8d8eccSSepherosa Ziehau 	 * internal memory.
10616c8d8eccSSepherosa Ziehau 	 */
10626c8d8eccSSepherosa Ziehau 	for (i = BGE_STATS_BLOCK;
10636c8d8eccSSepherosa Ziehau 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
10646c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
10656c8d8eccSSepherosa Ziehau 
10666c8d8eccSSepherosa Ziehau 	for (i = BGE_STATUS_BLOCK;
10676c8d8eccSSepherosa Ziehau 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
10686c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
10696c8d8eccSSepherosa Ziehau 
1070d7872545SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc)) {
1071d7872545SSepherosa Ziehau 		uint32_t val;
1072d7872545SSepherosa Ziehau 
1073d7872545SSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1074d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1075d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1076d7872545SSepherosa Ziehau 
1077d7872545SSepherosa Ziehau 			/* Access the lower 1K of PL PCI-E block registers. */
1078d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1079d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_PL_SEL);
1080d7872545SSepherosa Ziehau 
1081d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1082d7872545SSepherosa Ziehau 			val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1083d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1084d7872545SSepherosa Ziehau 
1085d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1086d7872545SSepherosa Ziehau 		}
1087d7872545SSepherosa Ziehau 		if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
1088d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1089d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1090d7872545SSepherosa Ziehau 
1091d7872545SSepherosa Ziehau 			/* Access the lower 1K of DL PCI-E block registers. */
1092d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1093d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_DL_SEL);
1094d7872545SSepherosa Ziehau 
1095d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1096d7872545SSepherosa Ziehau 			val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1097d7872545SSepherosa Ziehau 			val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1098d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1099d7872545SSepherosa Ziehau 
1100d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1101d7872545SSepherosa Ziehau 		}
1102d7872545SSepherosa Ziehau 
1103d7872545SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1104d7872545SSepherosa Ziehau 		val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1105d7872545SSepherosa Ziehau 		val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1106d7872545SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1107d7872545SSepherosa Ziehau 	}
1108d7872545SSepherosa Ziehau 
11092890cca3SSepherosa Ziehau 	/*
11102890cca3SSepherosa Ziehau 	 * Set up the PCI DMA control register.
11112890cca3SSepherosa Ziehau 	 */
11122890cca3SSepherosa Ziehau 	dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
11132890cca3SSepherosa Ziehau 	/*
11142890cca3SSepherosa Ziehau 	 * Disable 32bytes cache alignment for DMA write to host memory
11152890cca3SSepherosa Ziehau 	 *
11162890cca3SSepherosa Ziehau 	 * NOTE:
11172890cca3SSepherosa Ziehau 	 * 64bytes cache alignment for DMA write to host memory is still
11182890cca3SSepherosa Ziehau 	 * enabled.
11192890cca3SSepherosa Ziehau 	 */
11202890cca3SSepherosa Ziehau 	dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
11216c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
11226c8d8eccSSepherosa Ziehau 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
11236c8d8eccSSepherosa Ziehau 	/*
11246c8d8eccSSepherosa Ziehau 	 * Enable HW workaround for controllers that misinterpret
11256c8d8eccSSepherosa Ziehau 	 * a status tag update and leave interrupts permanently
11266c8d8eccSSepherosa Ziehau 	 * disabled.
11276c8d8eccSSepherosa Ziehau 	 */
11286c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
11292890cca3SSepherosa Ziehau 	    !BNX_IS_57765_FAMILY(sc))
11306c8d8eccSSepherosa Ziehau 		dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
11312890cca3SSepherosa Ziehau 	if (bootverbose) {
11322890cca3SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
11332890cca3SSepherosa Ziehau 		    dma_rw_ctl);
11346c8d8eccSSepherosa Ziehau 	}
11356c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
11366c8d8eccSSepherosa Ziehau 
11376c8d8eccSSepherosa Ziehau 	/*
11386c8d8eccSSepherosa Ziehau 	 * Set up general mode register.
11396c8d8eccSSepherosa Ziehau 	 */
11406c8d8eccSSepherosa Ziehau 	mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
11416c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
11426c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
11436c8d8eccSSepherosa Ziehau 
11446c8d8eccSSepherosa Ziehau 	/*
11456c8d8eccSSepherosa Ziehau 	 * Disable memory write invalidate.  Apparently it is not supported
11466c8d8eccSSepherosa Ziehau 	 * properly by these devices.  Also ensure that INTx isn't disabled,
11476c8d8eccSSepherosa Ziehau 	 * as these chips need it even when using MSI.
11486c8d8eccSSepherosa Ziehau 	 */
11496c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
11506c8d8eccSSepherosa Ziehau 	    (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
11516c8d8eccSSepherosa Ziehau 
11526c8d8eccSSepherosa Ziehau 	/* Set the timer prescaler (always 66Mhz) */
11536c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
11546c8d8eccSSepherosa Ziehau 
11556c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
11566c8d8eccSSepherosa Ziehau 		DELAY(40);	/* XXX */
11576c8d8eccSSepherosa Ziehau 
11586c8d8eccSSepherosa Ziehau 		/* Put PHY into ready state */
11596c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
11606c8d8eccSSepherosa Ziehau 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
11616c8d8eccSSepherosa Ziehau 		DELAY(40);
11626c8d8eccSSepherosa Ziehau 	}
11636c8d8eccSSepherosa Ziehau 
11646c8d8eccSSepherosa Ziehau 	return(0);
11656c8d8eccSSepherosa Ziehau }
11666c8d8eccSSepherosa Ziehau 
11676c8d8eccSSepherosa Ziehau static int
11686c8d8eccSSepherosa Ziehau bnx_blockinit(struct bnx_softc *sc)
11696c8d8eccSSepherosa Ziehau {
11706c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
11716c8d8eccSSepherosa Ziehau 	bus_size_t vrcb;
11726c8d8eccSSepherosa Ziehau 	bge_hostaddr taddr;
11736c8d8eccSSepherosa Ziehau 	uint32_t val;
11746c8d8eccSSepherosa Ziehau 	int i, limit;
11756c8d8eccSSepherosa Ziehau 
11766c8d8eccSSepherosa Ziehau 	/*
11776c8d8eccSSepherosa Ziehau 	 * Initialize the memory window pointer register so that
11786c8d8eccSSepherosa Ziehau 	 * we can access the first 32K of internal NIC RAM. This will
11796c8d8eccSSepherosa Ziehau 	 * allow us to set up the TX send ring RCBs and the RX return
11806c8d8eccSSepherosa Ziehau 	 * ring RCBs, plus other things which live in NIC memory.
11816c8d8eccSSepherosa Ziehau 	 */
11826c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
11836c8d8eccSSepherosa Ziehau 
11846c8d8eccSSepherosa Ziehau 	/* Configure mbuf pool watermarks */
1185f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
11866c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11876c8d8eccSSepherosa Ziehau 		if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
11886c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
11896c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
11906c8d8eccSSepherosa Ziehau 		} else {
11916c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
11926c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
11936c8d8eccSSepherosa Ziehau 		}
11946c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
11956c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11966c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
11976c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
11986c8d8eccSSepherosa Ziehau 	} else {
11996c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12006c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12016c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
12026c8d8eccSSepherosa Ziehau 	}
12036c8d8eccSSepherosa Ziehau 
12046c8d8eccSSepherosa Ziehau 	/* Configure DMA resource watermarks */
12056c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
12066c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
12076c8d8eccSSepherosa Ziehau 
12086c8d8eccSSepherosa Ziehau 	/* Enable buffer manager */
12096c8d8eccSSepherosa Ziehau 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
12106c8d8eccSSepherosa Ziehau 	/*
12116c8d8eccSSepherosa Ziehau 	 * Change the arbitration algorithm of TXMBUF read request to
12126c8d8eccSSepherosa Ziehau 	 * round-robin instead of priority based for BCM5719.  When
12136c8d8eccSSepherosa Ziehau 	 * TXFIFO is almost empty, RDMA will hold its request until
12146c8d8eccSSepherosa Ziehau 	 * TXFIFO is not almost empty.
12156c8d8eccSSepherosa Ziehau 	 */
12166c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
12176c8d8eccSSepherosa Ziehau 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1218e5eebe34SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1219e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1220e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1221e5eebe34SSepherosa Ziehau 		val |= BGE_BMANMODE_LOMBUF_ATTN;
12226c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
12236c8d8eccSSepherosa Ziehau 
12246c8d8eccSSepherosa Ziehau 	/* Poll for buffer manager start indication */
12256c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
12266c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
12276c8d8eccSSepherosa Ziehau 			break;
12286c8d8eccSSepherosa Ziehau 		DELAY(10);
12296c8d8eccSSepherosa Ziehau 	}
12306c8d8eccSSepherosa Ziehau 
12316c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
12326c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
12336c8d8eccSSepherosa Ziehau 			  "buffer manager failed to start\n");
12346c8d8eccSSepherosa Ziehau 		return(ENXIO);
12356c8d8eccSSepherosa Ziehau 	}
12366c8d8eccSSepherosa Ziehau 
12376c8d8eccSSepherosa Ziehau 	/* Enable flow-through queues */
12386c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
12396c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
12406c8d8eccSSepherosa Ziehau 
12416c8d8eccSSepherosa Ziehau 	/* Wait until queue initialization is complete */
12426c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
12436c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
12446c8d8eccSSepherosa Ziehau 			break;
12456c8d8eccSSepherosa Ziehau 		DELAY(10);
12466c8d8eccSSepherosa Ziehau 	}
12476c8d8eccSSepherosa Ziehau 
12486c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
12496c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
12506c8d8eccSSepherosa Ziehau 			  "flow-through queue init failed\n");
12516c8d8eccSSepherosa Ziehau 		return(ENXIO);
12526c8d8eccSSepherosa Ziehau 	}
12536c8d8eccSSepherosa Ziehau 
12546c8d8eccSSepherosa Ziehau 	/*
12556c8d8eccSSepherosa Ziehau 	 * Summary of rings supported by the controller:
12566c8d8eccSSepherosa Ziehau 	 *
12576c8d8eccSSepherosa Ziehau 	 * Standard Receive Producer Ring
12586c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "standard"
12596c8d8eccSSepherosa Ziehau 	 *   sized frames (typically 1536 bytes) to the controller.
12606c8d8eccSSepherosa Ziehau 	 *
12616c8d8eccSSepherosa Ziehau 	 * Jumbo Receive Producer Ring
12626c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for jumbo sized
12636c8d8eccSSepherosa Ziehau 	 *   frames (i.e. anything bigger than the "standard" frames)
12646c8d8eccSSepherosa Ziehau 	 *   to the controller.
12656c8d8eccSSepherosa Ziehau 	 *
12666c8d8eccSSepherosa Ziehau 	 * Mini Receive Producer Ring
12676c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "mini"
12686c8d8eccSSepherosa Ziehau 	 *   sized frames to the controller.
12696c8d8eccSSepherosa Ziehau 	 * - This feature required external memory for the controller
12706c8d8eccSSepherosa Ziehau 	 *   but was never used in a production system.  Should always
12716c8d8eccSSepherosa Ziehau 	 *   be disabled.
12726c8d8eccSSepherosa Ziehau 	 *
12736c8d8eccSSepherosa Ziehau 	 * Receive Return Ring
12746c8d8eccSSepherosa Ziehau 	 * - After the controller has placed an incoming frame into a
12756c8d8eccSSepherosa Ziehau 	 *   receive buffer that buffer is moved into a receive return
12766c8d8eccSSepherosa Ziehau 	 *   ring.  The driver is then responsible to passing the
12776c8d8eccSSepherosa Ziehau 	 *   buffer up to the stack.  Many versions of the controller
12786c8d8eccSSepherosa Ziehau 	 *   support multiple RR rings.
12796c8d8eccSSepherosa Ziehau 	 *
12806c8d8eccSSepherosa Ziehau 	 * Send Ring
12816c8d8eccSSepherosa Ziehau 	 * - This ring is used for outgoing frames.  Many versions of
12826c8d8eccSSepherosa Ziehau 	 *   the controller support multiple send rings.
12836c8d8eccSSepherosa Ziehau 	 */
12846c8d8eccSSepherosa Ziehau 
12856c8d8eccSSepherosa Ziehau 	/* Initialize the standard receive producer ring control block. */
12866c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
12876c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_lo =
12886c8d8eccSSepherosa Ziehau 	    BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_std_ring_paddr);
12896c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_hi =
12906c8d8eccSSepherosa Ziehau 	    BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_std_ring_paddr);
1291f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
12926c8d8eccSSepherosa Ziehau 		/*
12936c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
12946c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Maximum RX frame size
12956c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
12966c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
12976c8d8eccSSepherosa Ziehau 		 */
12986c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
12996c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
13006c8d8eccSSepherosa Ziehau 	} else {
13016c8d8eccSSepherosa Ziehau 		/*
13026c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
13036c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Reserved (should be 0)
13046c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
13056c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
13066c8d8eccSSepherosa Ziehau 		 */
13076c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13086c8d8eccSSepherosa Ziehau 	}
1309303fdc72SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
13106c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
13116c8d8eccSSepherosa Ziehau 	else
13126c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
13136c8d8eccSSepherosa Ziehau 	/* Write the standard receive producer ring control block. */
13146c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
13156c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
13166c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
13176c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
13186c8d8eccSSepherosa Ziehau 	/* Reset the standard receive producer ring producer index. */
13196c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
13206c8d8eccSSepherosa Ziehau 
13216c8d8eccSSepherosa Ziehau 	/*
13226c8d8eccSSepherosa Ziehau 	 * Initialize the jumbo RX producer ring control
13236c8d8eccSSepherosa Ziehau 	 * block.  We set the 'ring disabled' bit in the
13246c8d8eccSSepherosa Ziehau 	 * flags field until we're actually ready to start
13256c8d8eccSSepherosa Ziehau 	 * using this ring (i.e. once we set the MTU
13266c8d8eccSSepherosa Ziehau 	 * high enough to require it).
13276c8d8eccSSepherosa Ziehau 	 */
13286c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
13296c8d8eccSSepherosa Ziehau 		rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
13306c8d8eccSSepherosa Ziehau 		/* Get the jumbo receive producer ring RCB parameters. */
13316c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_lo =
13326c8d8eccSSepherosa Ziehau 		    BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
13336c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_hi =
13346c8d8eccSSepherosa Ziehau 		    BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
13356c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
13366c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
13376c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
1338303fdc72SSepherosa Ziehau 		if (BNX_IS_5717_PLUS(sc))
13396c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
13406c8d8eccSSepherosa Ziehau 		else
13416c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
13426c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
13436c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_hi);
13446c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
13456c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_lo);
13466c8d8eccSSepherosa Ziehau 		/* Program the jumbo receive producer ring RCB parameters. */
13476c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13486c8d8eccSSepherosa Ziehau 		    rcb->bge_maxlen_flags);
13496c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
13506c8d8eccSSepherosa Ziehau 		/* Reset the jumbo receive producer ring producer index. */
13516c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
13526c8d8eccSSepherosa Ziehau 	}
13536c8d8eccSSepherosa Ziehau 
13546c8d8eccSSepherosa Ziehau 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
13556c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906 &&
13566c8d8eccSSepherosa Ziehau 	    (sc->bnx_chipid == BGE_CHIPID_BCM5906_A0 ||
13576c8d8eccSSepherosa Ziehau 	     sc->bnx_chipid == BGE_CHIPID_BCM5906_A1 ||
13586c8d8eccSSepherosa Ziehau 	     sc->bnx_chipid == BGE_CHIPID_BCM5906_A2)) {
13596c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
13606c8d8eccSSepherosa Ziehau 		    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
13616c8d8eccSSepherosa Ziehau 	}
13626c8d8eccSSepherosa Ziehau 
13636c8d8eccSSepherosa Ziehau 	/*
13646c8d8eccSSepherosa Ziehau 	 * The BD ring replenish thresholds control how often the
13656c8d8eccSSepherosa Ziehau 	 * hardware fetches new BD's from the producer rings in host
13666c8d8eccSSepherosa Ziehau 	 * memory.  Setting the value too low on a busy system can
13676c8d8eccSSepherosa Ziehau 	 * starve the hardware and recue the throughpout.
13686c8d8eccSSepherosa Ziehau 	 *
13696c8d8eccSSepherosa Ziehau 	 * Set the BD ring replentish thresholds. The recommended
13706c8d8eccSSepherosa Ziehau 	 * values are 1/8th the number of descriptors allocated to
13716c8d8eccSSepherosa Ziehau 	 * each ring.
13726c8d8eccSSepherosa Ziehau 	 */
13736c8d8eccSSepherosa Ziehau 	val = 8;
13746c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
13756c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
13766c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
13776c8d8eccSSepherosa Ziehau 		    BGE_JUMBO_RX_RING_CNT/8);
13786c8d8eccSSepherosa Ziehau 	}
1379f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
13806c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
13816c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
13826c8d8eccSSepherosa Ziehau 	}
13836c8d8eccSSepherosa Ziehau 
13846c8d8eccSSepherosa Ziehau 	/*
13856c8d8eccSSepherosa Ziehau 	 * Disable all send rings by setting the 'ring disabled' bit
13866c8d8eccSSepherosa Ziehau 	 * in the flags field of all the TX send ring control blocks,
13876c8d8eccSSepherosa Ziehau 	 * located in NIC memory.
13886c8d8eccSSepherosa Ziehau 	 */
138980969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
139080969639SSepherosa Ziehau 		limit = 4;
13914f23029eSSepherosa Ziehau 	else if (BNX_IS_57765_FAMILY(sc))
13924f23029eSSepherosa Ziehau 		limit = 2;
139380969639SSepherosa Ziehau 	else
13946c8d8eccSSepherosa Ziehau 		limit = 1;
13956c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
13966c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
13976c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13986c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
13996c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
14006c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
14016c8d8eccSSepherosa Ziehau 	}
14026c8d8eccSSepherosa Ziehau 
14036c8d8eccSSepherosa Ziehau 	/* Configure send ring RCB 0 (we use only the first ring) */
14046c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
14056c8d8eccSSepherosa Ziehau 	BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_tx_ring_paddr);
14066c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
14076c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1408303fdc72SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
14096c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
14106c8d8eccSSepherosa Ziehau 	} else {
14116c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
14126c8d8eccSSepherosa Ziehau 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
14136c8d8eccSSepherosa Ziehau 	}
14146c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14156c8d8eccSSepherosa Ziehau 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
14166c8d8eccSSepherosa Ziehau 
14176c8d8eccSSepherosa Ziehau 	/*
14186c8d8eccSSepherosa Ziehau 	 * Disable all receive return rings by setting the
14196c8d8eccSSepherosa Ziehau 	 * 'ring disabled' bit in the flags field of all the receive
14206c8d8eccSSepherosa Ziehau 	 * return ring control blocks, located in NIC memory.
14216c8d8eccSSepherosa Ziehau 	 */
142280969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
14236c8d8eccSSepherosa Ziehau 		/* Should be 17, use 16 until we get an SRAM map. */
14246c8d8eccSSepherosa Ziehau 		limit = 16;
14254f23029eSSepherosa Ziehau 	} else if (BNX_IS_57765_FAMILY(sc)) {
14266c8d8eccSSepherosa Ziehau 		limit = 4;
14276c8d8eccSSepherosa Ziehau 	} else {
14286c8d8eccSSepherosa Ziehau 		limit = 1;
14296c8d8eccSSepherosa Ziehau 	}
14306c8d8eccSSepherosa Ziehau 	/* Disable all receive return rings. */
14316c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
14326c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
14336c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
14346c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
14356c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14366c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
14376c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
14386c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
14396c8d8eccSSepherosa Ziehau 		    (i * (sizeof(uint64_t))), 0);
14406c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
14416c8d8eccSSepherosa Ziehau 	}
14426c8d8eccSSepherosa Ziehau 
14436c8d8eccSSepherosa Ziehau 	/*
14446c8d8eccSSepherosa Ziehau 	 * Set up receive return ring 0.  Note that the NIC address
14456c8d8eccSSepherosa Ziehau 	 * for RX return rings is 0x0.  The return rings live entirely
14466c8d8eccSSepherosa Ziehau 	 * within the host, so the nicaddr field in the RCB isn't used.
14476c8d8eccSSepherosa Ziehau 	 */
14486c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
14496c8d8eccSSepherosa Ziehau 	BGE_HOSTADDR(taddr, sc->bnx_ldata.bnx_rx_return_ring_paddr);
14506c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
14516c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
14526c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
14536c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14546c8d8eccSSepherosa Ziehau 	    BGE_RCB_MAXLEN_FLAGS(sc->bnx_return_ring_cnt, 0));
14556c8d8eccSSepherosa Ziehau 
14566c8d8eccSSepherosa Ziehau 	/* Set random backoff seed for TX */
14576c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14586c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
14596c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
14606c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
14616c8d8eccSSepherosa Ziehau 	    BGE_TX_BACKOFF_SEED_MASK);
14626c8d8eccSSepherosa Ziehau 
14636c8d8eccSSepherosa Ziehau 	/* Set inter-packet gap */
14646c8d8eccSSepherosa Ziehau 	val = 0x2620;
14656c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
14666c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
14676c8d8eccSSepherosa Ziehau 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
14686c8d8eccSSepherosa Ziehau 	}
14696c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
14706c8d8eccSSepherosa Ziehau 
14716c8d8eccSSepherosa Ziehau 	/*
14726c8d8eccSSepherosa Ziehau 	 * Specify which ring to use for packets that don't match
14736c8d8eccSSepherosa Ziehau 	 * any RX rules.
14746c8d8eccSSepherosa Ziehau 	 */
14756c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
14766c8d8eccSSepherosa Ziehau 
14776c8d8eccSSepherosa Ziehau 	/*
14786c8d8eccSSepherosa Ziehau 	 * Configure number of RX lists. One interrupt distribution
14796c8d8eccSSepherosa Ziehau 	 * list, sixteen active lists, one bad frames class.
14806c8d8eccSSepherosa Ziehau 	 */
14816c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
14826c8d8eccSSepherosa Ziehau 
14836c8d8eccSSepherosa Ziehau 	/* Inialize RX list placement stats mask. */
14846c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
14856c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
14866c8d8eccSSepherosa Ziehau 
14876c8d8eccSSepherosa Ziehau 	/* Disable host coalescing until we get it set up */
14886c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
14896c8d8eccSSepherosa Ziehau 
14906c8d8eccSSepherosa Ziehau 	/* Poll to make sure it's shut down. */
14916c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
14926c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
14936c8d8eccSSepherosa Ziehau 			break;
14946c8d8eccSSepherosa Ziehau 		DELAY(10);
14956c8d8eccSSepherosa Ziehau 	}
14966c8d8eccSSepherosa Ziehau 
14976c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
14986c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
14996c8d8eccSSepherosa Ziehau 			  "host coalescing engine failed to idle\n");
15006c8d8eccSSepherosa Ziehau 		return(ENXIO);
15016c8d8eccSSepherosa Ziehau 	}
15026c8d8eccSSepherosa Ziehau 
15036c8d8eccSSepherosa Ziehau 	/* Set up host coalescing defaults */
15046c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bnx_rx_coal_ticks);
15056c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bnx_tx_coal_ticks);
15066c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bnx_rx_coal_bds);
15076c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bnx_tx_coal_bds);
15086c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, sc->bnx_rx_coal_bds_int);
15096c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, sc->bnx_tx_coal_bds_int);
15106c8d8eccSSepherosa Ziehau 
15116c8d8eccSSepherosa Ziehau 	/* Set up address of status block */
15126c8d8eccSSepherosa Ziehau 	bzero(sc->bnx_ldata.bnx_status_block, BGE_STATUS_BLK_SZ);
15136c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
15146c8d8eccSSepherosa Ziehau 	    BGE_ADDR_HI(sc->bnx_ldata.bnx_status_block_paddr));
15156c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
15166c8d8eccSSepherosa Ziehau 	    BGE_ADDR_LO(sc->bnx_ldata.bnx_status_block_paddr));
15176c8d8eccSSepherosa Ziehau 
15186c8d8eccSSepherosa Ziehau 	/* Set up status block partail update size. */
15196c8d8eccSSepherosa Ziehau 	val = BGE_STATBLKSZ_32BYTE;
15206c8d8eccSSepherosa Ziehau #if 0
15216c8d8eccSSepherosa Ziehau 	/*
15226c8d8eccSSepherosa Ziehau 	 * Does not seem to have visible effect in both
15236c8d8eccSSepherosa Ziehau 	 * bulk data (1472B UDP datagram) and tiny data
15246c8d8eccSSepherosa Ziehau 	 * (18B UDP datagram) TX tests.
15256c8d8eccSSepherosa Ziehau 	 */
15266c8d8eccSSepherosa Ziehau 	val |= BGE_HCCMODE_CLRTICK_TX;
15276c8d8eccSSepherosa Ziehau #endif
15286c8d8eccSSepherosa Ziehau 	/* Turn on host coalescing state machine */
15296c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
15306c8d8eccSSepherosa Ziehau 
15316c8d8eccSSepherosa Ziehau 	/* Turn on RX BD completion state machine and enable attentions */
15326c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
15336c8d8eccSSepherosa Ziehau 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
15346c8d8eccSSepherosa Ziehau 
15356c8d8eccSSepherosa Ziehau 	/* Turn on RX list placement state machine */
15366c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
15376c8d8eccSSepherosa Ziehau 
15386c8d8eccSSepherosa Ziehau 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
15396c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
15406c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
15416c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_FRMHDR_DMA_ENB;
15426c8d8eccSSepherosa Ziehau 
15436c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
15446c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_TBI;
15456c8d8eccSSepherosa Ziehau 	else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
15466c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_GMII;
15476c8d8eccSSepherosa Ziehau 	else
15486c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_MII;
15496c8d8eccSSepherosa Ziehau 
15506c8d8eccSSepherosa Ziehau 	/* Turn on DMA, clear stats */
15516c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
15526c8d8eccSSepherosa Ziehau 
15536c8d8eccSSepherosa Ziehau 	/* Set misc. local control, enable interrupts on attentions */
15546c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
15556c8d8eccSSepherosa Ziehau 
15566c8d8eccSSepherosa Ziehau #ifdef notdef
15576c8d8eccSSepherosa Ziehau 	/* Assert GPIO pins for PHY reset */
15586c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
15596c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
15606c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
15616c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
15626c8d8eccSSepherosa Ziehau #endif
15636c8d8eccSSepherosa Ziehau 
15646c8d8eccSSepherosa Ziehau 	/* Turn on write DMA state machine */
15656c8d8eccSSepherosa Ziehau 	val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
15666c8d8eccSSepherosa Ziehau 	/* Enable host coalescing bug fix. */
15676c8d8eccSSepherosa Ziehau 	val |= BGE_WDMAMODE_STATUS_TAG_FIX;
15686c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
15696c8d8eccSSepherosa Ziehau 		/* Request larger DMA burst size to get better performance. */
15706c8d8eccSSepherosa Ziehau 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
15716c8d8eccSSepherosa Ziehau 	}
15726c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
15736c8d8eccSSepherosa Ziehau 	DELAY(40);
15746c8d8eccSSepherosa Ziehau 
15753730a14dSSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
15766c8d8eccSSepherosa Ziehau 		uint32_t dmactl;
15776c8d8eccSSepherosa Ziehau 
15786c8d8eccSSepherosa Ziehau 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
15796c8d8eccSSepherosa Ziehau 		/*
15806c8d8eccSSepherosa Ziehau 		 * Adjust tx margin to prevent TX data corruption and
15816c8d8eccSSepherosa Ziehau 		 * fix internal FIFO overflow.
15826c8d8eccSSepherosa Ziehau 		 */
15836c8d8eccSSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
15846c8d8eccSSepherosa Ziehau 		    sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
15856c8d8eccSSepherosa Ziehau 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
15866c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
15876c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
15886c8d8eccSSepherosa Ziehau 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
15896c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
15906c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
15916c8d8eccSSepherosa Ziehau 		}
15926c8d8eccSSepherosa Ziehau 		/*
15936c8d8eccSSepherosa Ziehau 		 * Enable fix for read DMA FIFO overruns.
15946c8d8eccSSepherosa Ziehau 		 * The fix is to limit the number of RX BDs
15956c8d8eccSSepherosa Ziehau 		 * the hardware would fetch at a fime.
15966c8d8eccSSepherosa Ziehau 		 */
15976c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
15986c8d8eccSSepherosa Ziehau 		    dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
15996c8d8eccSSepherosa Ziehau 	}
16006c8d8eccSSepherosa Ziehau 
16016c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
16026c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
16036c8d8eccSSepherosa Ziehau 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
16046c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
16056c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
16066c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
16076c8d8eccSSepherosa Ziehau 		/*
16086c8d8eccSSepherosa Ziehau 		 * Allow 4KB burst length reads for non-LSO frames.
16096c8d8eccSSepherosa Ziehau 		 * Enable 512B burst length reads for buffer descriptors.
16106c8d8eccSSepherosa Ziehau 		 */
16116c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
16126c8d8eccSSepherosa Ziehau 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
16136c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
16146c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
16156c8d8eccSSepherosa Ziehau 	}
16166c8d8eccSSepherosa Ziehau 
16176c8d8eccSSepherosa Ziehau 	/* Turn on read DMA state machine */
16186c8d8eccSSepherosa Ziehau 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
16196c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
16206c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
16216c8d8eccSSepherosa Ziehau         if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
16226c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
16236c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
16246c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
16256c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
16266c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
16276c8d8eccSSepherosa Ziehau 	}
16286c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
16296c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
16306c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_H2BNC_VLAN_DET;
16316c8d8eccSSepherosa Ziehau 		/*
16326c8d8eccSSepherosa Ziehau 		 * Allow multiple outstanding read requests from
16336c8d8eccSSepherosa Ziehau 		 * non-LSO read DMA engine.
16346c8d8eccSSepherosa Ziehau 		 */
16356c8d8eccSSepherosa Ziehau 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
16366c8d8eccSSepherosa Ziehau 	}
163766deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO)
163866deb1c1SSepherosa Ziehau 		val |= BGE_RDMAMODE_TSO4_ENABLE;
16396c8d8eccSSepherosa Ziehau 	val |= BGE_RDMAMODE_FIFO_LONG_BURST;
16406c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
16416c8d8eccSSepherosa Ziehau 	DELAY(40);
16426c8d8eccSSepherosa Ziehau 
16436c8d8eccSSepherosa Ziehau 	/* Turn on RX data completion state machine */
16446c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
16456c8d8eccSSepherosa Ziehau 
16466c8d8eccSSepherosa Ziehau 	/* Turn on RX BD initiator state machine */
16476c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
16486c8d8eccSSepherosa Ziehau 
16496c8d8eccSSepherosa Ziehau 	/* Turn on RX data and RX BD initiator state machine */
16506c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
16516c8d8eccSSepherosa Ziehau 
16526c8d8eccSSepherosa Ziehau 	/* Turn on send BD completion state machine */
16536c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
16546c8d8eccSSepherosa Ziehau 
16556c8d8eccSSepherosa Ziehau 	/* Turn on send data completion state machine */
16566c8d8eccSSepherosa Ziehau 	val = BGE_SDCMODE_ENABLE;
16576c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
16586c8d8eccSSepherosa Ziehau 		val |= BGE_SDCMODE_CDELAY;
16596c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
16606c8d8eccSSepherosa Ziehau 
16616c8d8eccSSepherosa Ziehau 	/* Turn on send data initiator state machine */
166266deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
166366deb1c1SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
166466deb1c1SSepherosa Ziehau 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
166566deb1c1SSepherosa Ziehau 	} else {
16666c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
166766deb1c1SSepherosa Ziehau 	}
16686c8d8eccSSepherosa Ziehau 
16696c8d8eccSSepherosa Ziehau 	/* Turn on send BD initiator state machine */
16706c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
16716c8d8eccSSepherosa Ziehau 
16726c8d8eccSSepherosa Ziehau 	/* Turn on send BD selector state machine */
16736c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
16746c8d8eccSSepherosa Ziehau 
16756c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
16766c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
16776c8d8eccSSepherosa Ziehau 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
16786c8d8eccSSepherosa Ziehau 
16796c8d8eccSSepherosa Ziehau 	/* ack/clear link change events */
16806c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16816c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16826c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
16836c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
16846c8d8eccSSepherosa Ziehau 
16856c8d8eccSSepherosa Ziehau 	/*
16866c8d8eccSSepherosa Ziehau 	 * Enable attention when the link has changed state for
16876c8d8eccSSepherosa Ziehau 	 * devices that use auto polling.
16886c8d8eccSSepherosa Ziehau 	 */
16896c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
16906c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
16916c8d8eccSSepherosa Ziehau  	} else {
16926c8d8eccSSepherosa Ziehau 		if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
16936c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
16946c8d8eccSSepherosa Ziehau 			DELAY(80);
16956c8d8eccSSepherosa Ziehau 		}
16966c8d8eccSSepherosa Ziehau 	}
16976c8d8eccSSepherosa Ziehau 
16986c8d8eccSSepherosa Ziehau 	/*
16996c8d8eccSSepherosa Ziehau 	 * Clear any pending link state attention.
17006c8d8eccSSepherosa Ziehau 	 * Otherwise some link state change events may be lost until attention
17016c8d8eccSSepherosa Ziehau 	 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
17026c8d8eccSSepherosa Ziehau 	 * It's not necessary on newer BCM chips - perhaps enabling link
17036c8d8eccSSepherosa Ziehau 	 * state change attentions implies clearing pending attention.
17046c8d8eccSSepherosa Ziehau 	 */
17056c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
17066c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
17076c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
17086c8d8eccSSepherosa Ziehau 
17096c8d8eccSSepherosa Ziehau 	/* Enable link state change attentions. */
17106c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
17116c8d8eccSSepherosa Ziehau 
17126c8d8eccSSepherosa Ziehau 	return(0);
17136c8d8eccSSepherosa Ziehau }
17146c8d8eccSSepherosa Ziehau 
17156c8d8eccSSepherosa Ziehau /*
17166c8d8eccSSepherosa Ziehau  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
17176c8d8eccSSepherosa Ziehau  * against our list and return its name if we find a match. Note
17186c8d8eccSSepherosa Ziehau  * that since the Broadcom controller contains VPD support, we
17196c8d8eccSSepherosa Ziehau  * can get the device name string from the controller itself instead
17206c8d8eccSSepherosa Ziehau  * of the compiled-in string. This is a little slow, but it guarantees
17216c8d8eccSSepherosa Ziehau  * we'll always announce the right product name.
17226c8d8eccSSepherosa Ziehau  */
17236c8d8eccSSepherosa Ziehau static int
17246c8d8eccSSepherosa Ziehau bnx_probe(device_t dev)
17256c8d8eccSSepherosa Ziehau {
17266c8d8eccSSepherosa Ziehau 	const struct bnx_type *t;
17276c8d8eccSSepherosa Ziehau 	uint16_t product, vendor;
17286c8d8eccSSepherosa Ziehau 
17296c8d8eccSSepherosa Ziehau 	if (!pci_is_pcie(dev))
17306c8d8eccSSepherosa Ziehau 		return ENXIO;
17316c8d8eccSSepherosa Ziehau 
17326c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
17336c8d8eccSSepherosa Ziehau 	vendor = pci_get_vendor(dev);
17346c8d8eccSSepherosa Ziehau 
17356c8d8eccSSepherosa Ziehau 	for (t = bnx_devs; t->bnx_name != NULL; t++) {
17366c8d8eccSSepherosa Ziehau 		if (vendor == t->bnx_vid && product == t->bnx_did)
17376c8d8eccSSepherosa Ziehau 			break;
17386c8d8eccSSepherosa Ziehau 	}
17396c8d8eccSSepherosa Ziehau 	if (t->bnx_name == NULL)
17406c8d8eccSSepherosa Ziehau 		return ENXIO;
17416c8d8eccSSepherosa Ziehau 
17426c8d8eccSSepherosa Ziehau 	device_set_desc(dev, t->bnx_name);
17436c8d8eccSSepherosa Ziehau 	return 0;
17446c8d8eccSSepherosa Ziehau }
17456c8d8eccSSepherosa Ziehau 
17466c8d8eccSSepherosa Ziehau static int
17476c8d8eccSSepherosa Ziehau bnx_attach(device_t dev)
17486c8d8eccSSepherosa Ziehau {
17496c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
17506c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
17516c8d8eccSSepherosa Ziehau 	uint32_t hwcfg = 0, misccfg;
17526c8d8eccSSepherosa Ziehau 	int error = 0, rid, capmask;
17536c8d8eccSSepherosa Ziehau 	uint8_t ether_addr[ETHER_ADDR_LEN];
17546c8d8eccSSepherosa Ziehau 	uint16_t product, vendor;
17556c8d8eccSSepherosa Ziehau 	driver_intr_t *intr_func;
17566c8d8eccSSepherosa Ziehau 	uintptr_t mii_priv = 0;
17576c8d8eccSSepherosa Ziehau 	u_int intr_flags;
175866deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
175966deb1c1SSepherosa Ziehau 	char desc[32];
176066deb1c1SSepherosa Ziehau 	int i;
176166deb1c1SSepherosa Ziehau #endif
17626c8d8eccSSepherosa Ziehau 
17636c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
17646c8d8eccSSepherosa Ziehau 	sc->bnx_dev = dev;
176550668ed5SSepherosa Ziehau 	callout_init_mp(&sc->bnx_stat_timer);
1766df9ccc98SSepherosa Ziehau 	callout_init_mp(&sc->bnx_intr_timer);
17676c8d8eccSSepherosa Ziehau 	lwkt_serialize_init(&sc->bnx_jslot_serializer);
17686c8d8eccSSepherosa Ziehau 
17696c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
17706c8d8eccSSepherosa Ziehau 	vendor = pci_get_vendor(dev);
17716c8d8eccSSepherosa Ziehau 
17726c8d8eccSSepherosa Ziehau #ifndef BURN_BRIDGES
17736c8d8eccSSepherosa Ziehau 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
17746c8d8eccSSepherosa Ziehau 		uint32_t irq, mem;
17756c8d8eccSSepherosa Ziehau 
17766c8d8eccSSepherosa Ziehau 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
17776c8d8eccSSepherosa Ziehau 		mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
17786c8d8eccSSepherosa Ziehau 
17796c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip is in D%d power mode "
17806c8d8eccSSepherosa Ziehau 		    "-- setting to D0\n", pci_get_powerstate(dev));
17816c8d8eccSSepherosa Ziehau 
17826c8d8eccSSepherosa Ziehau 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
17836c8d8eccSSepherosa Ziehau 
17846c8d8eccSSepherosa Ziehau 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
17856c8d8eccSSepherosa Ziehau 		pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
17866c8d8eccSSepherosa Ziehau 	}
17876c8d8eccSSepherosa Ziehau #endif	/* !BURN_BRIDGE */
17886c8d8eccSSepherosa Ziehau 
17896c8d8eccSSepherosa Ziehau 	/*
17906c8d8eccSSepherosa Ziehau 	 * Map control/status registers.
17916c8d8eccSSepherosa Ziehau 	 */
17926c8d8eccSSepherosa Ziehau 	pci_enable_busmaster(dev);
17936c8d8eccSSepherosa Ziehau 
17946c8d8eccSSepherosa Ziehau 	rid = BGE_PCI_BAR0;
17956c8d8eccSSepherosa Ziehau 	sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
17966c8d8eccSSepherosa Ziehau 	    RF_ACTIVE);
17976c8d8eccSSepherosa Ziehau 
17986c8d8eccSSepherosa Ziehau 	if (sc->bnx_res == NULL) {
17996c8d8eccSSepherosa Ziehau 		device_printf(dev, "couldn't map memory\n");
18006c8d8eccSSepherosa Ziehau 		return ENXIO;
18016c8d8eccSSepherosa Ziehau 	}
18026c8d8eccSSepherosa Ziehau 
18036c8d8eccSSepherosa Ziehau 	sc->bnx_btag = rman_get_bustag(sc->bnx_res);
18046c8d8eccSSepherosa Ziehau 	sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
18056c8d8eccSSepherosa Ziehau 
18066c8d8eccSSepherosa Ziehau 	/* Save various chip information */
18076c8d8eccSSepherosa Ziehau 	sc->bnx_chipid =
18086c8d8eccSSepherosa Ziehau 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
18096c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
18106c8d8eccSSepherosa Ziehau 	if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
18116c8d8eccSSepherosa Ziehau 		/* All chips having dedicated ASICREV register have CPMU */
18126c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_CPMU;
18136c8d8eccSSepherosa Ziehau 
18146c8d8eccSSepherosa Ziehau 		switch (product) {
18156c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5717:
18166c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5718:
18176c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5719:
18186c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
18196c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
18206c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
18216c8d8eccSSepherosa Ziehau 			break;
18226c8d8eccSSepherosa Ziehau 
18236c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57761:
182432ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57762:
18256c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57765:
182632ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57766:
18276c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57781:
182832ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57782:
18296c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57785:
183032ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57786:
18316c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57791:
18326c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57795:
18336c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
18346c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
18356c8d8eccSSepherosa Ziehau 			break;
18366c8d8eccSSepherosa Ziehau 
18376c8d8eccSSepherosa Ziehau 		default:
18386c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
18396c8d8eccSSepherosa Ziehau 			    BGE_PCI_PRODID_ASICREV, 4);
18406c8d8eccSSepherosa Ziehau 			break;
18416c8d8eccSSepherosa Ziehau 		}
18426c8d8eccSSepherosa Ziehau 	}
18436c8d8eccSSepherosa Ziehau 	sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
18446c8d8eccSSepherosa Ziehau 	sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
18456c8d8eccSSepherosa Ziehau 
18466c8d8eccSSepherosa Ziehau 	switch (sc->bnx_asicrev) {
18476c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5717:
18486c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5719:
18496c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5720:
1850f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1851f368d0d9SSepherosa Ziehau 		break;
1852f368d0d9SSepherosa Ziehau 
18536c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM57765:
185432ff3c80SSepherosa Ziehau 	case BGE_ASICREV_BCM57766:
1855f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
18566c8d8eccSSepherosa Ziehau 		break;
18576c8d8eccSSepherosa Ziehau 	}
18586c8d8eccSSepherosa Ziehau 	sc->bnx_flags |= BNX_FLAG_SHORTDMA;
18596c8d8eccSSepherosa Ziehau 
186066deb1c1SSepherosa Ziehau 	sc->bnx_flags |= BNX_FLAG_TSO;
186166deb1c1SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
186266deb1c1SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
186366deb1c1SSepherosa Ziehau 		sc->bnx_flags &= ~BNX_FLAG_TSO;
186466deb1c1SSepherosa Ziehau 
1865df9ccc98SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1866df9ccc98SSepherosa Ziehau 	    BNX_IS_57765_FAMILY(sc)) {
1867df9ccc98SSepherosa Ziehau 		/*
1868df9ccc98SSepherosa Ziehau 		 * All BCM57785 and BCM5718 families chips have a bug that
1869df9ccc98SSepherosa Ziehau 		 * under certain situation interrupt will not be enabled
1870df9ccc98SSepherosa Ziehau 		 * even if status tag is written to BGE_MBX_IRQ0_LO mailbox.
1871df9ccc98SSepherosa Ziehau 		 *
1872df9ccc98SSepherosa Ziehau 		 * While BCM5719 and BCM5720 have a hardware workaround
1873df9ccc98SSepherosa Ziehau 		 * which could fix the above bug.
1874df9ccc98SSepherosa Ziehau 		 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1875df9ccc98SSepherosa Ziehau 		 * bnx_chipinit().
1876df9ccc98SSepherosa Ziehau 		 *
1877df9ccc98SSepherosa Ziehau 		 * For the rest of the chips in these two families, we will
1878df9ccc98SSepherosa Ziehau 		 * have to poll the status block at high rate (10ms currently)
1879df9ccc98SSepherosa Ziehau 		 * to check whether the interrupt is hosed or not.
1880df9ccc98SSepherosa Ziehau 		 * See bnx_intr_check() for details.
1881df9ccc98SSepherosa Ziehau 		 */
1882df9ccc98SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1883df9ccc98SSepherosa Ziehau 	}
1884df9ccc98SSepherosa Ziehau 
18856c8d8eccSSepherosa Ziehau 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
18866c8d8eccSSepherosa Ziehau 
18876c8d8eccSSepherosa Ziehau 	sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
18886c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
18896c8d8eccSSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720)
18906c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
18916c8d8eccSSepherosa Ziehau 	else
18926c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
18936c8d8eccSSepherosa Ziehau 	device_printf(dev, "CHIP ID 0x%08x; "
18946c8d8eccSSepherosa Ziehau 		      "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
18956c8d8eccSSepherosa Ziehau 		      sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
18966c8d8eccSSepherosa Ziehau 
18976c8d8eccSSepherosa Ziehau 	/*
18986c8d8eccSSepherosa Ziehau 	 * Set various PHY quirk flags.
18996c8d8eccSSepherosa Ziehau 	 */
19006c8d8eccSSepherosa Ziehau 
19016c8d8eccSSepherosa Ziehau 	capmask = MII_CAPMASK_DEFAULT;
190246283a40SSepherosa Ziehau 	if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
190346283a40SSepherosa Ziehau 	    product == PCI_PRODUCT_BROADCOM_BCM57795) {
19046c8d8eccSSepherosa Ziehau 		/* 10/100 only */
19056c8d8eccSSepherosa Ziehau 		capmask &= ~BMSR_EXTSTAT;
19066c8d8eccSSepherosa Ziehau 	}
19076c8d8eccSSepherosa Ziehau 
19086c8d8eccSSepherosa Ziehau 	mii_priv |= BRGPHY_FLAG_WIRESPEED;
19096c8d8eccSSepherosa Ziehau 
19106c8d8eccSSepherosa Ziehau 	/*
19116c8d8eccSSepherosa Ziehau 	 * Allocate interrupt
19126c8d8eccSSepherosa Ziehau 	 */
19136c8d8eccSSepherosa Ziehau 	sc->bnx_irq_type = pci_alloc_1intr(dev, bnx_msi_enable, &sc->bnx_irq_rid,
19146c8d8eccSSepherosa Ziehau 	    &intr_flags);
19156c8d8eccSSepherosa Ziehau 
19166c8d8eccSSepherosa Ziehau 	sc->bnx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->bnx_irq_rid,
19176c8d8eccSSepherosa Ziehau 	    intr_flags);
19186c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq == NULL) {
19196c8d8eccSSepherosa Ziehau 		device_printf(dev, "couldn't map interrupt\n");
19206c8d8eccSSepherosa Ziehau 		error = ENXIO;
19216c8d8eccSSepherosa Ziehau 		goto fail;
19226c8d8eccSSepherosa Ziehau 	}
19236c8d8eccSSepherosa Ziehau 
19246c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
19256c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_ONESHOT_MSI;
19266c8d8eccSSepherosa Ziehau 		bnx_enable_msi(sc);
19276c8d8eccSSepherosa Ziehau 	}
19286c8d8eccSSepherosa Ziehau 
19296c8d8eccSSepherosa Ziehau 	/* Initialize if_name earlier, so if_printf could be used */
19306c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
19316c8d8eccSSepherosa Ziehau 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
19326c8d8eccSSepherosa Ziehau 
19336c8d8eccSSepherosa Ziehau 	/* Try to reset the chip. */
19346c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
19356c8d8eccSSepherosa Ziehau 
19366c8d8eccSSepherosa Ziehau 	if (bnx_chipinit(sc)) {
19376c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip initialization failed\n");
19386c8d8eccSSepherosa Ziehau 		error = ENXIO;
19396c8d8eccSSepherosa Ziehau 		goto fail;
19406c8d8eccSSepherosa Ziehau 	}
19416c8d8eccSSepherosa Ziehau 
19426c8d8eccSSepherosa Ziehau 	/*
19436c8d8eccSSepherosa Ziehau 	 * Get station address
19446c8d8eccSSepherosa Ziehau 	 */
19456c8d8eccSSepherosa Ziehau 	error = bnx_get_eaddr(sc, ether_addr);
19466c8d8eccSSepherosa Ziehau 	if (error) {
19476c8d8eccSSepherosa Ziehau 		device_printf(dev, "failed to read station address\n");
19486c8d8eccSSepherosa Ziehau 		goto fail;
19496c8d8eccSSepherosa Ziehau 	}
19506c8d8eccSSepherosa Ziehau 
1951f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
19526c8d8eccSSepherosa Ziehau 		sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT;
19536c8d8eccSSepherosa Ziehau 	} else {
19546c8d8eccSSepherosa Ziehau 		/* 5705/5750 limits RX return ring to 512 entries. */
19556c8d8eccSSepherosa Ziehau 		sc->bnx_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
19566c8d8eccSSepherosa Ziehau 	}
19576c8d8eccSSepherosa Ziehau 
19586c8d8eccSSepherosa Ziehau 	error = bnx_dma_alloc(sc);
19596c8d8eccSSepherosa Ziehau 	if (error)
19606c8d8eccSSepherosa Ziehau 		goto fail;
19616c8d8eccSSepherosa Ziehau 
19626c8d8eccSSepherosa Ziehau 	/* Set default tuneable values. */
19636c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
19646c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
19656c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
19666c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
19676c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_DEF;
19686c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_DEF;
19696c8d8eccSSepherosa Ziehau 
19706c8d8eccSSepherosa Ziehau 	/* Set up ifnet structure */
19716c8d8eccSSepherosa Ziehau 	ifp->if_softc = sc;
19726c8d8eccSSepherosa Ziehau 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
19736c8d8eccSSepherosa Ziehau 	ifp->if_ioctl = bnx_ioctl;
19746c8d8eccSSepherosa Ziehau 	ifp->if_start = bnx_start;
19756c8d8eccSSepherosa Ziehau #ifdef DEVICE_POLLING
19766c8d8eccSSepherosa Ziehau 	ifp->if_poll = bnx_poll;
19776c8d8eccSSepherosa Ziehau #endif
19786c8d8eccSSepherosa Ziehau 	ifp->if_watchdog = bnx_watchdog;
19796c8d8eccSSepherosa Ziehau 	ifp->if_init = bnx_init;
19806c8d8eccSSepherosa Ziehau 	ifp->if_mtu = ETHERMTU;
19816c8d8eccSSepherosa Ziehau 	ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
19826c8d8eccSSepherosa Ziehau 	ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
19836c8d8eccSSepherosa Ziehau 	ifq_set_ready(&ifp->if_snd);
19846c8d8eccSSepherosa Ziehau 
19856c8d8eccSSepherosa Ziehau 	ifp->if_capabilities |= IFCAP_HWCSUM;
19866c8d8eccSSepherosa Ziehau 	ifp->if_hwassist = BNX_CSUM_FEATURES;
198766deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
198866deb1c1SSepherosa Ziehau 		ifp->if_capabilities |= IFCAP_TSO;
198966deb1c1SSepherosa Ziehau 		ifp->if_hwassist |= CSUM_TSO;
199066deb1c1SSepherosa Ziehau 	}
19916c8d8eccSSepherosa Ziehau 	ifp->if_capenable = ifp->if_capabilities;
19926c8d8eccSSepherosa Ziehau 
19936c8d8eccSSepherosa Ziehau 	/*
19946c8d8eccSSepherosa Ziehau 	 * Figure out what sort of media we have by checking the
19956c8d8eccSSepherosa Ziehau 	 * hardware config word in the first 32k of NIC internal memory,
19966c8d8eccSSepherosa Ziehau 	 * or fall back to examining the EEPROM if necessary.
19976c8d8eccSSepherosa Ziehau 	 * Note: on some BCM5700 cards, this value appears to be unset.
19986c8d8eccSSepherosa Ziehau 	 * If that's the case, we have to rely on identifying the NIC
19996c8d8eccSSepherosa Ziehau 	 * by its PCI subsystem ID, as we do below for the SysKonnect
20006c8d8eccSSepherosa Ziehau 	 * SK-9D41.
20016c8d8eccSSepherosa Ziehau 	 */
20026c8d8eccSSepherosa Ziehau 	if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
20036c8d8eccSSepherosa Ziehau 		hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
20046c8d8eccSSepherosa Ziehau 	} else {
20056c8d8eccSSepherosa Ziehau 		if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
20066c8d8eccSSepherosa Ziehau 				    sizeof(hwcfg))) {
20076c8d8eccSSepherosa Ziehau 			device_printf(dev, "failed to read EEPROM\n");
20086c8d8eccSSepherosa Ziehau 			error = ENXIO;
20096c8d8eccSSepherosa Ziehau 			goto fail;
20106c8d8eccSSepherosa Ziehau 		}
20116c8d8eccSSepherosa Ziehau 		hwcfg = ntohl(hwcfg);
20126c8d8eccSSepherosa Ziehau 	}
20136c8d8eccSSepherosa Ziehau 
20146c8d8eccSSepherosa Ziehau 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
20156c8d8eccSSepherosa Ziehau 	if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
20166c8d8eccSSepherosa Ziehau 	    (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
20176c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_TBI;
20186c8d8eccSSepherosa Ziehau 
20196c8d8eccSSepherosa Ziehau 	/* Setup MI MODE */
20206c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_CPMU)
20216c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
20226c8d8eccSSepherosa Ziehau 	else
20236c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_BASE;
20246c8d8eccSSepherosa Ziehau 
20256c8d8eccSSepherosa Ziehau 	/* Setup link status update stuffs */
20266c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
20276c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_tbi_link_upd;
20286c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20296c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
20306c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_autopoll_link_upd;
20316c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20326c8d8eccSSepherosa Ziehau 	} else {
20336c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_copper_link_upd;
20346c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20356c8d8eccSSepherosa Ziehau 	}
20366c8d8eccSSepherosa Ziehau 
20376c8d8eccSSepherosa Ziehau 	/* Set default PHY address */
20386c8d8eccSSepherosa Ziehau 	sc->bnx_phyno = 1;
20396c8d8eccSSepherosa Ziehau 
20406c8d8eccSSepherosa Ziehau 	/*
20416c8d8eccSSepherosa Ziehau 	 * PHY address mapping for various devices.
20426c8d8eccSSepherosa Ziehau 	 *
20436c8d8eccSSepherosa Ziehau 	 *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
20446c8d8eccSSepherosa Ziehau 	 * ---------+-------+-------+-------+-------+
20456c8d8eccSSepherosa Ziehau 	 * BCM57XX  |   1   |   X   |   X   |   X   |
20466c8d8eccSSepherosa Ziehau 	 * BCM5704  |   1   |   X   |   1   |   X   |
20476c8d8eccSSepherosa Ziehau 	 * BCM5717  |   1   |   8   |   2   |   9   |
20486c8d8eccSSepherosa Ziehau 	 * BCM5719  |   1   |   8   |   2   |   9   |
20496c8d8eccSSepherosa Ziehau 	 * BCM5720  |   1   |   8   |   2   |   9   |
20506c8d8eccSSepherosa Ziehau 	 *
20516c8d8eccSSepherosa Ziehau 	 * Other addresses may respond but they are not
20526c8d8eccSSepherosa Ziehau 	 * IEEE compliant PHYs and should be ignored.
20536c8d8eccSSepherosa Ziehau 	 */
205480969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
20556c8d8eccSSepherosa Ziehau 		int f;
20566c8d8eccSSepherosa Ziehau 
20576c8d8eccSSepherosa Ziehau 		f = pci_get_function(dev);
20586c8d8eccSSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
20596c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
20606c8d8eccSSepherosa Ziehau 			    BGE_SGDIGSTS_IS_SERDES)
20616c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 8;
20626c8d8eccSSepherosa Ziehau 			else
20636c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 1;
20646c8d8eccSSepherosa Ziehau 		} else {
20656c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
20666c8d8eccSSepherosa Ziehau 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
20676c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 8;
20686c8d8eccSSepherosa Ziehau 			else
20696c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 1;
20706c8d8eccSSepherosa Ziehau 		}
20716c8d8eccSSepherosa Ziehau 	}
20726c8d8eccSSepherosa Ziehau 
20736c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
20746c8d8eccSSepherosa Ziehau 		ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
20756c8d8eccSSepherosa Ziehau 		    bnx_ifmedia_upd, bnx_ifmedia_sts);
20766c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
20776c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia,
20786c8d8eccSSepherosa Ziehau 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
20796c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
20806c8d8eccSSepherosa Ziehau 		ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
20816c8d8eccSSepherosa Ziehau 		sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
20826c8d8eccSSepherosa Ziehau 	} else {
20836c8d8eccSSepherosa Ziehau 		struct mii_probe_args mii_args;
20846c8d8eccSSepherosa Ziehau 
20856c8d8eccSSepherosa Ziehau 		mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
20866c8d8eccSSepherosa Ziehau 		mii_args.mii_probemask = 1 << sc->bnx_phyno;
20876c8d8eccSSepherosa Ziehau 		mii_args.mii_capmask = capmask;
20886c8d8eccSSepherosa Ziehau 		mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
20896c8d8eccSSepherosa Ziehau 		mii_args.mii_priv = mii_priv;
20906c8d8eccSSepherosa Ziehau 
20916c8d8eccSSepherosa Ziehau 		error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
20926c8d8eccSSepherosa Ziehau 		if (error) {
20936c8d8eccSSepherosa Ziehau 			device_printf(dev, "MII without any PHY!\n");
20946c8d8eccSSepherosa Ziehau 			goto fail;
20956c8d8eccSSepherosa Ziehau 		}
20966c8d8eccSSepherosa Ziehau 	}
20976c8d8eccSSepherosa Ziehau 
20986c8d8eccSSepherosa Ziehau 	/*
20996c8d8eccSSepherosa Ziehau 	 * Create sysctl nodes.
21006c8d8eccSSepherosa Ziehau 	 */
21016c8d8eccSSepherosa Ziehau 	sysctl_ctx_init(&sc->bnx_sysctl_ctx);
21026c8d8eccSSepherosa Ziehau 	sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
21036c8d8eccSSepherosa Ziehau 					      SYSCTL_STATIC_CHILDREN(_hw),
21046c8d8eccSSepherosa Ziehau 					      OID_AUTO,
21056c8d8eccSSepherosa Ziehau 					      device_get_nameunit(dev),
21066c8d8eccSSepherosa Ziehau 					      CTLFLAG_RD, 0, "");
21076c8d8eccSSepherosa Ziehau 	if (sc->bnx_sysctl_tree == NULL) {
21086c8d8eccSSepherosa Ziehau 		device_printf(dev, "can't add sysctl node\n");
21096c8d8eccSSepherosa Ziehau 		error = ENXIO;
21106c8d8eccSSepherosa Ziehau 		goto fail;
21116c8d8eccSSepherosa Ziehau 	}
21126c8d8eccSSepherosa Ziehau 
21136c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21146c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21156c8d8eccSSepherosa Ziehau 			OID_AUTO, "rx_coal_ticks",
21166c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21176c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_ticks, "I",
21186c8d8eccSSepherosa Ziehau 			"Receive coalescing ticks (usec).");
21196c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21206c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21216c8d8eccSSepherosa Ziehau 			OID_AUTO, "tx_coal_ticks",
21226c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21236c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_ticks, "I",
21246c8d8eccSSepherosa Ziehau 			"Transmit coalescing ticks (usec).");
21256c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21266c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21276c8d8eccSSepherosa Ziehau 			OID_AUTO, "rx_coal_bds",
21286c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21296c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_bds, "I",
21306c8d8eccSSepherosa Ziehau 			"Receive max coalesced BD count.");
21316c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21326c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21336c8d8eccSSepherosa Ziehau 			OID_AUTO, "tx_coal_bds",
21346c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21356c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_bds, "I",
21366c8d8eccSSepherosa Ziehau 			"Transmit max coalesced BD count.");
21376c8d8eccSSepherosa Ziehau 	/*
21386c8d8eccSSepherosa Ziehau 	 * A common design characteristic for many Broadcom
21396c8d8eccSSepherosa Ziehau 	 * client controllers is that they only support a
21406c8d8eccSSepherosa Ziehau 	 * single outstanding DMA read operation on the PCIe
21416c8d8eccSSepherosa Ziehau 	 * bus. This means that it will take twice as long to
21426c8d8eccSSepherosa Ziehau 	 * fetch a TX frame that is split into header and
21436c8d8eccSSepherosa Ziehau 	 * payload buffers as it does to fetch a single,
21446c8d8eccSSepherosa Ziehau 	 * contiguous TX frame (2 reads vs. 1 read). For these
21456c8d8eccSSepherosa Ziehau 	 * controllers, coalescing buffers to reduce the number
21466c8d8eccSSepherosa Ziehau 	 * of memory reads is effective way to get maximum
21476c8d8eccSSepherosa Ziehau 	 * performance(about 940Mbps).  Without collapsing TX
21486c8d8eccSSepherosa Ziehau 	 * buffers the maximum TCP bulk transfer performance
21496c8d8eccSSepherosa Ziehau 	 * is about 850Mbps. However forcing coalescing mbufs
21506c8d8eccSSepherosa Ziehau 	 * consumes a lot of CPU cycles, so leave it off by
21516c8d8eccSSepherosa Ziehau 	 * default.
21526c8d8eccSSepherosa Ziehau 	 */
21536c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
21546c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
21556c8d8eccSSepherosa Ziehau 	    "force_defrag", CTLFLAG_RW, &sc->bnx_force_defrag, 0,
21566c8d8eccSSepherosa Ziehau 	    "Force defragment on TX path");
21576c8d8eccSSepherosa Ziehau 
21586c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21596c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
21606c8d8eccSSepherosa Ziehau 	    "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
21616c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
21626c8d8eccSSepherosa Ziehau 	    "Receive max coalesced BD count during interrupt.");
21636c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21646c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
21656c8d8eccSSepherosa Ziehau 	    "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
21666c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
21676c8d8eccSSepherosa Ziehau 	    "Transmit max coalesced BD count during interrupt.");
21686c8d8eccSSepherosa Ziehau 
216966deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
217066deb1c1SSepherosa Ziehau 	for (i = 0; i < BNX_TSO_NSTATS; ++i) {
217166deb1c1SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
217266deb1c1SSepherosa Ziehau 		SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
217366deb1c1SSepherosa Ziehau 		    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
217466deb1c1SSepherosa Ziehau 		    desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
217566deb1c1SSepherosa Ziehau 	}
217666deb1c1SSepherosa Ziehau #endif
217766deb1c1SSepherosa Ziehau 
21786c8d8eccSSepherosa Ziehau 	/*
21796c8d8eccSSepherosa Ziehau 	 * Call MI attach routine.
21806c8d8eccSSepherosa Ziehau 	 */
21816c8d8eccSSepherosa Ziehau 	ether_ifattach(ifp, ether_addr, NULL);
21826c8d8eccSSepherosa Ziehau 
21836c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
21846c8d8eccSSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
21856c8d8eccSSepherosa Ziehau 			intr_func = bnx_msi_oneshot;
21866c8d8eccSSepherosa Ziehau 			if (bootverbose)
21876c8d8eccSSepherosa Ziehau 				device_printf(dev, "oneshot MSI\n");
21886c8d8eccSSepherosa Ziehau 		} else {
21896c8d8eccSSepherosa Ziehau 			intr_func = bnx_msi;
21906c8d8eccSSepherosa Ziehau 		}
21916c8d8eccSSepherosa Ziehau 	} else {
21926c8d8eccSSepherosa Ziehau 		intr_func = bnx_intr_legacy;
21936c8d8eccSSepherosa Ziehau 	}
21946c8d8eccSSepherosa Ziehau 	error = bus_setup_intr(dev, sc->bnx_irq, INTR_MPSAFE, intr_func, sc,
21956c8d8eccSSepherosa Ziehau 	    &sc->bnx_intrhand, ifp->if_serializer);
21966c8d8eccSSepherosa Ziehau 	if (error) {
21976c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
21986c8d8eccSSepherosa Ziehau 		device_printf(dev, "couldn't set up irq\n");
21996c8d8eccSSepherosa Ziehau 		goto fail;
22006c8d8eccSSepherosa Ziehau 	}
22016c8d8eccSSepherosa Ziehau 
22026c8d8eccSSepherosa Ziehau 	ifp->if_cpuid = rman_get_cpuid(sc->bnx_irq);
22036c8d8eccSSepherosa Ziehau 	KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
22046c8d8eccSSepherosa Ziehau 
22058ca0f604SSepherosa Ziehau 	sc->bnx_stat_cpuid = ifp->if_cpuid;
2206df9ccc98SSepherosa Ziehau 	sc->bnx_intr_cpuid = ifp->if_cpuid;
22078ca0f604SSepherosa Ziehau 
22086c8d8eccSSepherosa Ziehau 	return(0);
22096c8d8eccSSepherosa Ziehau fail:
22106c8d8eccSSepherosa Ziehau 	bnx_detach(dev);
22116c8d8eccSSepherosa Ziehau 	return(error);
22126c8d8eccSSepherosa Ziehau }
22136c8d8eccSSepherosa Ziehau 
22146c8d8eccSSepherosa Ziehau static int
22156c8d8eccSSepherosa Ziehau bnx_detach(device_t dev)
22166c8d8eccSSepherosa Ziehau {
22176c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
22186c8d8eccSSepherosa Ziehau 
22196c8d8eccSSepherosa Ziehau 	if (device_is_attached(dev)) {
22206c8d8eccSSepherosa Ziehau 		struct ifnet *ifp = &sc->arpcom.ac_if;
22216c8d8eccSSepherosa Ziehau 
22226c8d8eccSSepherosa Ziehau 		lwkt_serialize_enter(ifp->if_serializer);
22236c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
22246c8d8eccSSepherosa Ziehau 		bnx_reset(sc);
22256c8d8eccSSepherosa Ziehau 		bus_teardown_intr(dev, sc->bnx_irq, sc->bnx_intrhand);
22266c8d8eccSSepherosa Ziehau 		lwkt_serialize_exit(ifp->if_serializer);
22276c8d8eccSSepherosa Ziehau 
22286c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
22296c8d8eccSSepherosa Ziehau 	}
22306c8d8eccSSepherosa Ziehau 
22316c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
22326c8d8eccSSepherosa Ziehau 		ifmedia_removeall(&sc->bnx_ifmedia);
22336c8d8eccSSepherosa Ziehau 	if (sc->bnx_miibus)
22346c8d8eccSSepherosa Ziehau 		device_delete_child(dev, sc->bnx_miibus);
22356c8d8eccSSepherosa Ziehau 	bus_generic_detach(dev);
22366c8d8eccSSepherosa Ziehau 
22376c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq != NULL) {
22386c8d8eccSSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_IRQ, sc->bnx_irq_rid,
22396c8d8eccSSepherosa Ziehau 		    sc->bnx_irq);
22406c8d8eccSSepherosa Ziehau 	}
22416c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI)
22426c8d8eccSSepherosa Ziehau 		pci_release_msi(dev);
22436c8d8eccSSepherosa Ziehau 
22446c8d8eccSSepherosa Ziehau 	if (sc->bnx_res != NULL) {
22456c8d8eccSSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_MEMORY,
22466c8d8eccSSepherosa Ziehau 		    BGE_PCI_BAR0, sc->bnx_res);
22476c8d8eccSSepherosa Ziehau 	}
22486c8d8eccSSepherosa Ziehau 
22496c8d8eccSSepherosa Ziehau 	if (sc->bnx_sysctl_tree != NULL)
22506c8d8eccSSepherosa Ziehau 		sysctl_ctx_free(&sc->bnx_sysctl_ctx);
22516c8d8eccSSepherosa Ziehau 
22526c8d8eccSSepherosa Ziehau 	bnx_dma_free(sc);
22536c8d8eccSSepherosa Ziehau 
22546c8d8eccSSepherosa Ziehau 	return 0;
22556c8d8eccSSepherosa Ziehau }
22566c8d8eccSSepherosa Ziehau 
22576c8d8eccSSepherosa Ziehau static void
22586c8d8eccSSepherosa Ziehau bnx_reset(struct bnx_softc *sc)
22596c8d8eccSSepherosa Ziehau {
22606c8d8eccSSepherosa Ziehau 	device_t dev;
22616c8d8eccSSepherosa Ziehau 	uint32_t cachesize, command, pcistate, reset;
22626c8d8eccSSepherosa Ziehau 	void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
22636c8d8eccSSepherosa Ziehau 	int i, val = 0;
22646c8d8eccSSepherosa Ziehau 	uint16_t devctl;
22656c8d8eccSSepherosa Ziehau 
22666c8d8eccSSepherosa Ziehau 	dev = sc->bnx_dev;
22676c8d8eccSSepherosa Ziehau 
22686c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev != BGE_ASICREV_BCM5906)
22696c8d8eccSSepherosa Ziehau 		write_op = bnx_writemem_direct;
22706c8d8eccSSepherosa Ziehau 	else
22716c8d8eccSSepherosa Ziehau 		write_op = bnx_writereg_ind;
22726c8d8eccSSepherosa Ziehau 
22736c8d8eccSSepherosa Ziehau 	/* Save some important PCI state. */
22746c8d8eccSSepherosa Ziehau 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
22756c8d8eccSSepherosa Ziehau 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
22766c8d8eccSSepherosa Ziehau 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
22776c8d8eccSSepherosa Ziehau 
22786c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
22796c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
22806c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
22816c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
22826c8d8eccSSepherosa Ziehau 
22836c8d8eccSSepherosa Ziehau 	/* Disable fastboot on controllers that support it. */
22846c8d8eccSSepherosa Ziehau 	if (bootverbose)
22856c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
22866c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
22876c8d8eccSSepherosa Ziehau 
22886c8d8eccSSepherosa Ziehau 	/*
22896c8d8eccSSepherosa Ziehau 	 * Write the magic number to SRAM at offset 0xB50.
22906c8d8eccSSepherosa Ziehau 	 * When firmware finishes its initialization it will
22916c8d8eccSSepherosa Ziehau 	 * write ~BGE_MAGIC_NUMBER to the same location.
22926c8d8eccSSepherosa Ziehau 	 */
22936c8d8eccSSepherosa Ziehau 	bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
22946c8d8eccSSepherosa Ziehau 
22956c8d8eccSSepherosa Ziehau 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
22966c8d8eccSSepherosa Ziehau 
22976c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
22986c8d8eccSSepherosa Ziehau 	/* Force PCI-E 1.0a mode */
22993730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc) &&
23006c8d8eccSSepherosa Ziehau 	    CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
23016c8d8eccSSepherosa Ziehau 	    (BGE_PCIE_PHY_TSTCTL_PSCRAM |
23026c8d8eccSSepherosa Ziehau 	     BGE_PCIE_PHY_TSTCTL_PCIE10)) {
23036c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
23046c8d8eccSSepherosa Ziehau 		    BGE_PCIE_PHY_TSTCTL_PSCRAM);
23056c8d8eccSSepherosa Ziehau 	}
23066c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
23076c8d8eccSSepherosa Ziehau 		/* Prevent PCIE link training during global reset */
23086c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
23096c8d8eccSSepherosa Ziehau 		reset |= (1<<29);
23106c8d8eccSSepherosa Ziehau 	}
23116c8d8eccSSepherosa Ziehau 
23126c8d8eccSSepherosa Ziehau 	/*
23136c8d8eccSSepherosa Ziehau 	 * Set GPHY Power Down Override to leave GPHY
23146c8d8eccSSepherosa Ziehau 	 * powered up in D0 uninitialized.
23156c8d8eccSSepherosa Ziehau 	 */
23166c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
23176c8d8eccSSepherosa Ziehau 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
23186c8d8eccSSepherosa Ziehau 
23196c8d8eccSSepherosa Ziehau 	/* Issue global reset */
23206c8d8eccSSepherosa Ziehau 	write_op(sc, BGE_MISC_CFG, reset);
23216c8d8eccSSepherosa Ziehau 
23226c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
23236c8d8eccSSepherosa Ziehau 		uint32_t status, ctrl;
23246c8d8eccSSepherosa Ziehau 
23256c8d8eccSSepherosa Ziehau 		status = CSR_READ_4(sc, BGE_VCPU_STATUS);
23266c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
23276c8d8eccSSepherosa Ziehau 		    status | BGE_VCPU_STATUS_DRV_RESET);
23286c8d8eccSSepherosa Ziehau 		ctrl = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
23296c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
23306c8d8eccSSepherosa Ziehau 		    ctrl & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
23316c8d8eccSSepherosa Ziehau 	}
23326c8d8eccSSepherosa Ziehau 
23336c8d8eccSSepherosa Ziehau 	DELAY(1000);
23346c8d8eccSSepherosa Ziehau 
23356c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
23366c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
23376c8d8eccSSepherosa Ziehau 		uint32_t v;
23386c8d8eccSSepherosa Ziehau 
23396c8d8eccSSepherosa Ziehau 		DELAY(500000); /* wait for link training to complete */
23406c8d8eccSSepherosa Ziehau 		v = pci_read_config(dev, 0xc4, 4);
23416c8d8eccSSepherosa Ziehau 		pci_write_config(dev, 0xc4, v | (1<<15), 4);
23426c8d8eccSSepherosa Ziehau 	}
23436c8d8eccSSepherosa Ziehau 
23446c8d8eccSSepherosa Ziehau 	devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
23456c8d8eccSSepherosa Ziehau 
23466c8d8eccSSepherosa Ziehau 	/* Disable no snoop and disable relaxed ordering. */
23476c8d8eccSSepherosa Ziehau 	devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
23486c8d8eccSSepherosa Ziehau 
23496c8d8eccSSepherosa Ziehau 	/* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
23506c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
23516c8d8eccSSepherosa Ziehau 		devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
23526c8d8eccSSepherosa Ziehau 		devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
23536c8d8eccSSepherosa Ziehau 	}
23546c8d8eccSSepherosa Ziehau 
23556c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
23566c8d8eccSSepherosa Ziehau 	    devctl, 2);
23576c8d8eccSSepherosa Ziehau 
23586c8d8eccSSepherosa Ziehau 	/* Clear error status. */
23596c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
23606c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_CORR_ERR |
23616c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_NFATAL_ERR |
23626c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_FATAL_ERR |
23636c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_UNSUPP_REQ, 2);
23646c8d8eccSSepherosa Ziehau 
23656c8d8eccSSepherosa Ziehau 	/* Reset some of the PCI state that got zapped by reset */
23666c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
23676c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
23686c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
23696c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
23706c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
23716c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
23726c8d8eccSSepherosa Ziehau 	write_op(sc, BGE_MISC_CFG, (65 << 1));
23736c8d8eccSSepherosa Ziehau 
23746c8d8eccSSepherosa Ziehau 	/* Enable memory arbiter */
23756c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
23766c8d8eccSSepherosa Ziehau 
23776c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
23786c8d8eccSSepherosa Ziehau 		for (i = 0; i < BNX_TIMEOUT; i++) {
23796c8d8eccSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
23806c8d8eccSSepherosa Ziehau 			if (val & BGE_VCPU_STATUS_INIT_DONE)
23816c8d8eccSSepherosa Ziehau 				break;
23826c8d8eccSSepherosa Ziehau 			DELAY(100);
23836c8d8eccSSepherosa Ziehau 		}
23846c8d8eccSSepherosa Ziehau 		if (i == BNX_TIMEOUT) {
23856c8d8eccSSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if, "reset timed out\n");
23866c8d8eccSSepherosa Ziehau 			return;
23876c8d8eccSSepherosa Ziehau 		}
23886c8d8eccSSepherosa Ziehau 	} else {
23896c8d8eccSSepherosa Ziehau 		/*
23906c8d8eccSSepherosa Ziehau 		 * Poll until we see the 1's complement of the magic number.
23916c8d8eccSSepherosa Ziehau 		 * This indicates that the firmware initialization
23926c8d8eccSSepherosa Ziehau 		 * is complete.
23936c8d8eccSSepherosa Ziehau 		 */
23946c8d8eccSSepherosa Ziehau 		for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
23956c8d8eccSSepherosa Ziehau 			val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
23966c8d8eccSSepherosa Ziehau 			if (val == ~BGE_MAGIC_NUMBER)
23976c8d8eccSSepherosa Ziehau 				break;
23986c8d8eccSSepherosa Ziehau 			DELAY(10);
23996c8d8eccSSepherosa Ziehau 		}
24006c8d8eccSSepherosa Ziehau 		if (i == BNX_FIRMWARE_TIMEOUT) {
24016c8d8eccSSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if, "firmware handshake "
24026c8d8eccSSepherosa Ziehau 				  "timed out, found 0x%08x\n", val);
24036c8d8eccSSepherosa Ziehau 		}
24046c8d8eccSSepherosa Ziehau 
24056c8d8eccSSepherosa Ziehau 		/* BCM57765 A0 needs additional time before accessing. */
24066c8d8eccSSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
24076c8d8eccSSepherosa Ziehau 			DELAY(10 * 1000);
24086c8d8eccSSepherosa Ziehau 	}
24096c8d8eccSSepherosa Ziehau 
24106c8d8eccSSepherosa Ziehau 	/*
24116c8d8eccSSepherosa Ziehau 	 * XXX Wait for the value of the PCISTATE register to
24126c8d8eccSSepherosa Ziehau 	 * return to its original pre-reset state. This is a
24136c8d8eccSSepherosa Ziehau 	 * fairly good indicator of reset completion. If we don't
24146c8d8eccSSepherosa Ziehau 	 * wait for the reset to fully complete, trying to read
24156c8d8eccSSepherosa Ziehau 	 * from the device's non-PCI registers may yield garbage
24166c8d8eccSSepherosa Ziehau 	 * results.
24176c8d8eccSSepherosa Ziehau 	 */
24186c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
24196c8d8eccSSepherosa Ziehau 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
24206c8d8eccSSepherosa Ziehau 			break;
24216c8d8eccSSepherosa Ziehau 		DELAY(10);
24226c8d8eccSSepherosa Ziehau 	}
24236c8d8eccSSepherosa Ziehau 
24246c8d8eccSSepherosa Ziehau 	/* Fix up byte swapping */
24256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
24266c8d8eccSSepherosa Ziehau 
24276c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
24286c8d8eccSSepherosa Ziehau 
24296c8d8eccSSepherosa Ziehau 	/*
24306c8d8eccSSepherosa Ziehau 	 * The 5704 in TBI mode apparently needs some special
24316c8d8eccSSepherosa Ziehau 	 * adjustment to insure the SERDES drive level is set
24326c8d8eccSSepherosa Ziehau 	 * to 1.2V.
24336c8d8eccSSepherosa Ziehau 	 */
24346c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
24356c8d8eccSSepherosa Ziehau 	    (sc->bnx_flags & BNX_FLAG_TBI)) {
24366c8d8eccSSepherosa Ziehau 		uint32_t serdescfg;
24376c8d8eccSSepherosa Ziehau 
24386c8d8eccSSepherosa Ziehau 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
24396c8d8eccSSepherosa Ziehau 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
24406c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
24416c8d8eccSSepherosa Ziehau 	}
24426c8d8eccSSepherosa Ziehau 
24436c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
24443730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc)) {
24456c8d8eccSSepherosa Ziehau 		uint32_t v;
24466c8d8eccSSepherosa Ziehau 
24476c8d8eccSSepherosa Ziehau 		/* Enable Data FIFO protection. */
2448f1f34fc4SSepherosa Ziehau 		v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2449f1f34fc4SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
24506c8d8eccSSepherosa Ziehau 	}
24516c8d8eccSSepherosa Ziehau 
24526c8d8eccSSepherosa Ziehau 	DELAY(10000);
24536c8d8eccSSepherosa Ziehau 
24546c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
24556c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
24566c8d8eccSSepherosa Ziehau 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
24576c8d8eccSSepherosa Ziehau 	}
24586c8d8eccSSepherosa Ziehau }
24596c8d8eccSSepherosa Ziehau 
24606c8d8eccSSepherosa Ziehau /*
24616c8d8eccSSepherosa Ziehau  * Frame reception handling. This is called if there's a frame
24626c8d8eccSSepherosa Ziehau  * on the receive return list.
24636c8d8eccSSepherosa Ziehau  *
24646c8d8eccSSepherosa Ziehau  * Note: we have to be able to handle two possibilities here:
24656c8d8eccSSepherosa Ziehau  * 1) the frame is from the jumbo recieve ring
24666c8d8eccSSepherosa Ziehau  * 2) the frame is from the standard receive ring
24676c8d8eccSSepherosa Ziehau  */
24686c8d8eccSSepherosa Ziehau 
24696c8d8eccSSepherosa Ziehau static void
24706c8d8eccSSepherosa Ziehau bnx_rxeof(struct bnx_softc *sc, uint16_t rx_prod)
24716c8d8eccSSepherosa Ziehau {
24726c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
24736c8d8eccSSepherosa Ziehau 	int stdcnt = 0, jumbocnt = 0;
24746c8d8eccSSepherosa Ziehau 
24756c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
24766c8d8eccSSepherosa Ziehau 
24776c8d8eccSSepherosa Ziehau 	while (sc->bnx_rx_saved_considx != rx_prod) {
24786c8d8eccSSepherosa Ziehau 		struct bge_rx_bd	*cur_rx;
24796c8d8eccSSepherosa Ziehau 		uint32_t		rxidx;
24806c8d8eccSSepherosa Ziehau 		struct mbuf		*m = NULL;
24816c8d8eccSSepherosa Ziehau 		uint16_t		vlan_tag = 0;
24826c8d8eccSSepherosa Ziehau 		int			have_tag = 0;
24836c8d8eccSSepherosa Ziehau 
24846c8d8eccSSepherosa Ziehau 		cur_rx =
24856c8d8eccSSepherosa Ziehau 	    &sc->bnx_ldata.bnx_rx_return_ring[sc->bnx_rx_saved_considx];
24866c8d8eccSSepherosa Ziehau 
24876c8d8eccSSepherosa Ziehau 		rxidx = cur_rx->bge_idx;
24886c8d8eccSSepherosa Ziehau 		BNX_INC(sc->bnx_rx_saved_considx, sc->bnx_return_ring_cnt);
24896c8d8eccSSepherosa Ziehau 
24906c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
24916c8d8eccSSepherosa Ziehau 			have_tag = 1;
24926c8d8eccSSepherosa Ziehau 			vlan_tag = cur_rx->bge_vlan_tag;
24936c8d8eccSSepherosa Ziehau 		}
24946c8d8eccSSepherosa Ziehau 
24956c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
24966c8d8eccSSepherosa Ziehau 			BNX_INC(sc->bnx_jumbo, BGE_JUMBO_RX_RING_CNT);
24976c8d8eccSSepherosa Ziehau 			jumbocnt++;
24986c8d8eccSSepherosa Ziehau 
24996c8d8eccSSepherosa Ziehau 			if (rxidx != sc->bnx_jumbo) {
25006c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25016c8d8eccSSepherosa Ziehau 				if_printf(ifp, "sw jumbo index(%d) "
25026c8d8eccSSepherosa Ziehau 				    "and hw jumbo index(%d) mismatch, drop!\n",
25036c8d8eccSSepherosa Ziehau 				    sc->bnx_jumbo, rxidx);
25046c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, rxidx);
25056c8d8eccSSepherosa Ziehau 				continue;
25066c8d8eccSSepherosa Ziehau 			}
25076c8d8eccSSepherosa Ziehau 
25086c8d8eccSSepherosa Ziehau 			m = sc->bnx_cdata.bnx_rx_jumbo_chain[rxidx].bnx_mbuf;
25096c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
25106c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25116c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
25126c8d8eccSSepherosa Ziehau 				continue;
25136c8d8eccSSepherosa Ziehau 			}
25146c8d8eccSSepherosa Ziehau 			if (bnx_newbuf_jumbo(sc, sc->bnx_jumbo, 0)) {
25156c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25166c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
25176c8d8eccSSepherosa Ziehau 				continue;
25186c8d8eccSSepherosa Ziehau 			}
25196c8d8eccSSepherosa Ziehau 		} else {
25206c8d8eccSSepherosa Ziehau 			BNX_INC(sc->bnx_std, BGE_STD_RX_RING_CNT);
25216c8d8eccSSepherosa Ziehau 			stdcnt++;
25226c8d8eccSSepherosa Ziehau 
25236c8d8eccSSepherosa Ziehau 			if (rxidx != sc->bnx_std) {
25246c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25256c8d8eccSSepherosa Ziehau 				if_printf(ifp, "sw std index(%d) "
25266c8d8eccSSepherosa Ziehau 				    "and hw std index(%d) mismatch, drop!\n",
25276c8d8eccSSepherosa Ziehau 				    sc->bnx_std, rxidx);
25286c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_std(sc, rxidx);
25296c8d8eccSSepherosa Ziehau 				continue;
25306c8d8eccSSepherosa Ziehau 			}
25316c8d8eccSSepherosa Ziehau 
25326c8d8eccSSepherosa Ziehau 			m = sc->bnx_cdata.bnx_rx_std_chain[rxidx].bnx_mbuf;
25336c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
25346c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25356c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_std(sc, sc->bnx_std);
25366c8d8eccSSepherosa Ziehau 				continue;
25376c8d8eccSSepherosa Ziehau 			}
25386c8d8eccSSepherosa Ziehau 			if (bnx_newbuf_std(sc, sc->bnx_std, 0)) {
25396c8d8eccSSepherosa Ziehau 				ifp->if_ierrors++;
25406c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_std(sc, sc->bnx_std);
25416c8d8eccSSepherosa Ziehau 				continue;
25426c8d8eccSSepherosa Ziehau 			}
25436c8d8eccSSepherosa Ziehau 		}
25446c8d8eccSSepherosa Ziehau 
25456c8d8eccSSepherosa Ziehau 		ifp->if_ipackets++;
25466c8d8eccSSepherosa Ziehau 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
25476c8d8eccSSepherosa Ziehau 		m->m_pkthdr.rcvif = ifp;
25486c8d8eccSSepherosa Ziehau 
25496c8d8eccSSepherosa Ziehau 		if ((ifp->if_capenable & IFCAP_RXCSUM) &&
25506c8d8eccSSepherosa Ziehau 		    (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
25516c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
25526c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
25536c8d8eccSSepherosa Ziehau 				if ((cur_rx->bge_error_flag &
25546c8d8eccSSepherosa Ziehau 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
25556c8d8eccSSepherosa Ziehau 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
25566c8d8eccSSepherosa Ziehau 			}
25576c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
25586c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_data =
25596c8d8eccSSepherosa Ziehau 				    cur_rx->bge_tcp_udp_csum;
25606c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
25616c8d8eccSSepherosa Ziehau 				    CSUM_PSEUDO_HDR;
25626c8d8eccSSepherosa Ziehau 			}
25636c8d8eccSSepherosa Ziehau 		}
25646c8d8eccSSepherosa Ziehau 
25656c8d8eccSSepherosa Ziehau 		/*
25666c8d8eccSSepherosa Ziehau 		 * If we received a packet with a vlan tag, pass it
25676c8d8eccSSepherosa Ziehau 		 * to vlan_input() instead of ether_input().
25686c8d8eccSSepherosa Ziehau 		 */
25696c8d8eccSSepherosa Ziehau 		if (have_tag) {
25706c8d8eccSSepherosa Ziehau 			m->m_flags |= M_VLANTAG;
25716c8d8eccSSepherosa Ziehau 			m->m_pkthdr.ether_vlantag = vlan_tag;
25726c8d8eccSSepherosa Ziehau 			have_tag = vlan_tag = 0;
25736c8d8eccSSepherosa Ziehau 		}
25746c8d8eccSSepherosa Ziehau 		ifp->if_input(ifp, m);
25756c8d8eccSSepherosa Ziehau 	}
25766c8d8eccSSepherosa Ziehau 
25776c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bnx_rx_saved_considx);
25786c8d8eccSSepherosa Ziehau 	if (stdcnt)
25796c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bnx_std);
25806c8d8eccSSepherosa Ziehau 	if (jumbocnt)
25816c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
25826c8d8eccSSepherosa Ziehau }
25836c8d8eccSSepherosa Ziehau 
25846c8d8eccSSepherosa Ziehau static void
25856c8d8eccSSepherosa Ziehau bnx_txeof(struct bnx_softc *sc, uint16_t tx_cons)
25866c8d8eccSSepherosa Ziehau {
25876c8d8eccSSepherosa Ziehau 	struct bge_tx_bd *cur_tx = NULL;
25886c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
25896c8d8eccSSepherosa Ziehau 
25906c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
25916c8d8eccSSepherosa Ziehau 
25926c8d8eccSSepherosa Ziehau 	/*
25936c8d8eccSSepherosa Ziehau 	 * Go through our tx ring and free mbufs for those
25946c8d8eccSSepherosa Ziehau 	 * frames that have been sent.
25956c8d8eccSSepherosa Ziehau 	 */
25966c8d8eccSSepherosa Ziehau 	while (sc->bnx_tx_saved_considx != tx_cons) {
25976c8d8eccSSepherosa Ziehau 		uint32_t idx = 0;
25986c8d8eccSSepherosa Ziehau 
25996c8d8eccSSepherosa Ziehau 		idx = sc->bnx_tx_saved_considx;
26006c8d8eccSSepherosa Ziehau 		cur_tx = &sc->bnx_ldata.bnx_tx_ring[idx];
26016c8d8eccSSepherosa Ziehau 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
26026c8d8eccSSepherosa Ziehau 			ifp->if_opackets++;
26036c8d8eccSSepherosa Ziehau 		if (sc->bnx_cdata.bnx_tx_chain[idx] != NULL) {
26046c8d8eccSSepherosa Ziehau 			bus_dmamap_unload(sc->bnx_cdata.bnx_tx_mtag,
26056c8d8eccSSepherosa Ziehau 			    sc->bnx_cdata.bnx_tx_dmamap[idx]);
26066c8d8eccSSepherosa Ziehau 			m_freem(sc->bnx_cdata.bnx_tx_chain[idx]);
26076c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_tx_chain[idx] = NULL;
26086c8d8eccSSepherosa Ziehau 		}
26096c8d8eccSSepherosa Ziehau 		sc->bnx_txcnt--;
26106c8d8eccSSepherosa Ziehau 		BNX_INC(sc->bnx_tx_saved_considx, BGE_TX_RING_CNT);
26116c8d8eccSSepherosa Ziehau 	}
26126c8d8eccSSepherosa Ziehau 
26136c8d8eccSSepherosa Ziehau 	if (cur_tx != NULL &&
26146c8d8eccSSepherosa Ziehau 	    (BGE_TX_RING_CNT - sc->bnx_txcnt) >=
26156c8d8eccSSepherosa Ziehau 	    (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
26166c8d8eccSSepherosa Ziehau 		ifp->if_flags &= ~IFF_OACTIVE;
26176c8d8eccSSepherosa Ziehau 
26186c8d8eccSSepherosa Ziehau 	if (sc->bnx_txcnt == 0)
26196c8d8eccSSepherosa Ziehau 		ifp->if_timer = 0;
26206c8d8eccSSepherosa Ziehau 
26216c8d8eccSSepherosa Ziehau 	if (!ifq_is_empty(&ifp->if_snd))
26226c8d8eccSSepherosa Ziehau 		if_devstart(ifp);
26236c8d8eccSSepherosa Ziehau }
26246c8d8eccSSepherosa Ziehau 
26256c8d8eccSSepherosa Ziehau #ifdef DEVICE_POLLING
26266c8d8eccSSepherosa Ziehau 
26276c8d8eccSSepherosa Ziehau static void
26286c8d8eccSSepherosa Ziehau bnx_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
26296c8d8eccSSepherosa Ziehau {
26306c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
26316c8d8eccSSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
26326c8d8eccSSepherosa Ziehau 	uint16_t rx_prod, tx_cons;
26336c8d8eccSSepherosa Ziehau 
26346c8d8eccSSepherosa Ziehau 	switch(cmd) {
26356c8d8eccSSepherosa Ziehau 	case POLL_REGISTER:
26366c8d8eccSSepherosa Ziehau 		bnx_disable_intr(sc);
26376c8d8eccSSepherosa Ziehau 		break;
26386c8d8eccSSepherosa Ziehau 	case POLL_DEREGISTER:
26396c8d8eccSSepherosa Ziehau 		bnx_enable_intr(sc);
26406c8d8eccSSepherosa Ziehau 		break;
26416c8d8eccSSepherosa Ziehau 	case POLL_AND_CHECK_STATUS:
26426c8d8eccSSepherosa Ziehau 		/*
26436c8d8eccSSepherosa Ziehau 		 * Process link state changes.
26446c8d8eccSSepherosa Ziehau 		 */
26456c8d8eccSSepherosa Ziehau 		bnx_link_poll(sc);
26466c8d8eccSSepherosa Ziehau 		/* Fall through */
26476c8d8eccSSepherosa Ziehau 	case POLL_ONLY:
26486c8d8eccSSepherosa Ziehau 		sc->bnx_status_tag = sblk->bge_status_tag;
26496c8d8eccSSepherosa Ziehau 		/*
26506c8d8eccSSepherosa Ziehau 		 * Use a load fence to ensure that status_tag
26516c8d8eccSSepherosa Ziehau 		 * is saved  before rx_prod and tx_cons.
26526c8d8eccSSepherosa Ziehau 		 */
26536c8d8eccSSepherosa Ziehau 		cpu_lfence();
26546c8d8eccSSepherosa Ziehau 
26556c8d8eccSSepherosa Ziehau 		rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
26566c8d8eccSSepherosa Ziehau 		tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
26576c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING) {
26586c8d8eccSSepherosa Ziehau 			rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
26596c8d8eccSSepherosa Ziehau 			if (sc->bnx_rx_saved_considx != rx_prod)
26606c8d8eccSSepherosa Ziehau 				bnx_rxeof(sc, rx_prod);
26616c8d8eccSSepherosa Ziehau 
26626c8d8eccSSepherosa Ziehau 			tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
26636c8d8eccSSepherosa Ziehau 			if (sc->bnx_tx_saved_considx != tx_cons)
26646c8d8eccSSepherosa Ziehau 				bnx_txeof(sc, tx_cons);
26656c8d8eccSSepherosa Ziehau 		}
26666c8d8eccSSepherosa Ziehau 		break;
26676c8d8eccSSepherosa Ziehau 	}
26686c8d8eccSSepherosa Ziehau }
26696c8d8eccSSepherosa Ziehau 
26706c8d8eccSSepherosa Ziehau #endif
26716c8d8eccSSepherosa Ziehau 
26726c8d8eccSSepherosa Ziehau static void
26736c8d8eccSSepherosa Ziehau bnx_intr_legacy(void *xsc)
26746c8d8eccSSepherosa Ziehau {
26756c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
26766c8d8eccSSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
26776c8d8eccSSepherosa Ziehau 
26786c8d8eccSSepherosa Ziehau 	if (sc->bnx_status_tag == sblk->bge_status_tag) {
26796c8d8eccSSepherosa Ziehau 		uint32_t val;
26806c8d8eccSSepherosa Ziehau 
26816c8d8eccSSepherosa Ziehau 		val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
26826c8d8eccSSepherosa Ziehau 		if (val & BGE_PCISTAT_INTR_NOTACT)
26836c8d8eccSSepherosa Ziehau 			return;
26846c8d8eccSSepherosa Ziehau 	}
26856c8d8eccSSepherosa Ziehau 
26866c8d8eccSSepherosa Ziehau 	/*
26876c8d8eccSSepherosa Ziehau 	 * NOTE:
26886c8d8eccSSepherosa Ziehau 	 * Interrupt will have to be disabled if tagged status
26896c8d8eccSSepherosa Ziehau 	 * is used, else interrupt will always be asserted on
26906c8d8eccSSepherosa Ziehau 	 * certain chips (at least on BCM5750 AX/BX).
26916c8d8eccSSepherosa Ziehau 	 */
26926c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
26936c8d8eccSSepherosa Ziehau 
26946c8d8eccSSepherosa Ziehau 	bnx_intr(sc);
26956c8d8eccSSepherosa Ziehau }
26966c8d8eccSSepherosa Ziehau 
26976c8d8eccSSepherosa Ziehau static void
26986c8d8eccSSepherosa Ziehau bnx_msi(void *xsc)
26996c8d8eccSSepherosa Ziehau {
27006c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
27016c8d8eccSSepherosa Ziehau 
27026c8d8eccSSepherosa Ziehau 	/* Disable interrupt first */
27036c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
27046c8d8eccSSepherosa Ziehau 	bnx_intr(sc);
27056c8d8eccSSepherosa Ziehau }
27066c8d8eccSSepherosa Ziehau 
27076c8d8eccSSepherosa Ziehau static void
27086c8d8eccSSepherosa Ziehau bnx_msi_oneshot(void *xsc)
27096c8d8eccSSepherosa Ziehau {
27106c8d8eccSSepherosa Ziehau 	bnx_intr(xsc);
27116c8d8eccSSepherosa Ziehau }
27126c8d8eccSSepherosa Ziehau 
27136c8d8eccSSepherosa Ziehau static void
27146c8d8eccSSepherosa Ziehau bnx_intr(struct bnx_softc *sc)
27156c8d8eccSSepherosa Ziehau {
27166c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
27176c8d8eccSSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
27186c8d8eccSSepherosa Ziehau 	uint16_t rx_prod, tx_cons;
27196c8d8eccSSepherosa Ziehau 	uint32_t status;
27206c8d8eccSSepherosa Ziehau 
27216c8d8eccSSepherosa Ziehau 	sc->bnx_status_tag = sblk->bge_status_tag;
27226c8d8eccSSepherosa Ziehau 	/*
27236c8d8eccSSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
27246c8d8eccSSepherosa Ziehau 	 * before rx_prod, tx_cons and status.
27256c8d8eccSSepherosa Ziehau 	 */
27266c8d8eccSSepherosa Ziehau 	cpu_lfence();
27276c8d8eccSSepherosa Ziehau 
27286c8d8eccSSepherosa Ziehau 	rx_prod = sblk->bge_idx[0].bge_rx_prod_idx;
27296c8d8eccSSepherosa Ziehau 	tx_cons = sblk->bge_idx[0].bge_tx_cons_idx;
27306c8d8eccSSepherosa Ziehau 	status = sblk->bge_status;
27316c8d8eccSSepherosa Ziehau 
27326c8d8eccSSepherosa Ziehau 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
27336c8d8eccSSepherosa Ziehau 		bnx_link_poll(sc);
27346c8d8eccSSepherosa Ziehau 
27356c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_RUNNING) {
27366c8d8eccSSepherosa Ziehau 		if (sc->bnx_rx_saved_considx != rx_prod)
27376c8d8eccSSepherosa Ziehau 			bnx_rxeof(sc, rx_prod);
27386c8d8eccSSepherosa Ziehau 
27396c8d8eccSSepherosa Ziehau 		if (sc->bnx_tx_saved_considx != tx_cons)
27406c8d8eccSSepherosa Ziehau 			bnx_txeof(sc, tx_cons);
27416c8d8eccSSepherosa Ziehau 	}
27426c8d8eccSSepherosa Ziehau 
27436c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
27446c8d8eccSSepherosa Ziehau 
27456c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg)
27466c8d8eccSSepherosa Ziehau 		bnx_coal_change(sc);
27476c8d8eccSSepherosa Ziehau }
27486c8d8eccSSepherosa Ziehau 
27496c8d8eccSSepherosa Ziehau static void
27506c8d8eccSSepherosa Ziehau bnx_tick(void *xsc)
27516c8d8eccSSepherosa Ziehau {
27526c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
27536c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
27546c8d8eccSSepherosa Ziehau 
27556c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
27566c8d8eccSSepherosa Ziehau 
27578ca0f604SSepherosa Ziehau 	KKASSERT(mycpuid == sc->bnx_stat_cpuid);
27588ca0f604SSepherosa Ziehau 
27596c8d8eccSSepherosa Ziehau 	bnx_stats_update_regs(sc);
27606c8d8eccSSepherosa Ziehau 
27616c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
27626c8d8eccSSepherosa Ziehau 		/*
27636c8d8eccSSepherosa Ziehau 		 * Since in TBI mode auto-polling can't be used we should poll
27646c8d8eccSSepherosa Ziehau 		 * link status manually. Here we register pending link event
27656c8d8eccSSepherosa Ziehau 		 * and trigger interrupt.
27666c8d8eccSSepherosa Ziehau 		 */
27676c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
27686c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
27696c8d8eccSSepherosa Ziehau 	} else if (!sc->bnx_link) {
27706c8d8eccSSepherosa Ziehau 		mii_tick(device_get_softc(sc->bnx_miibus));
27716c8d8eccSSepherosa Ziehau 	}
27726c8d8eccSSepherosa Ziehau 
27736c8d8eccSSepherosa Ziehau 	callout_reset(&sc->bnx_stat_timer, hz, bnx_tick, sc);
27746c8d8eccSSepherosa Ziehau 
27756c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
27766c8d8eccSSepherosa Ziehau }
27776c8d8eccSSepherosa Ziehau 
27786c8d8eccSSepherosa Ziehau static void
27796c8d8eccSSepherosa Ziehau bnx_stats_update_regs(struct bnx_softc *sc)
27806c8d8eccSSepherosa Ziehau {
27816c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
27826c8d8eccSSepherosa Ziehau 	struct bge_mac_stats_regs stats;
27836c8d8eccSSepherosa Ziehau 	uint32_t *s;
27846c8d8eccSSepherosa Ziehau 	int i;
27856c8d8eccSSepherosa Ziehau 
27866c8d8eccSSepherosa Ziehau 	s = (uint32_t *)&stats;
27876c8d8eccSSepherosa Ziehau 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
27886c8d8eccSSepherosa Ziehau 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
27896c8d8eccSSepherosa Ziehau 		s++;
27906c8d8eccSSepherosa Ziehau 	}
27916c8d8eccSSepherosa Ziehau 
27926c8d8eccSSepherosa Ziehau 	ifp->if_collisions +=
27936c8d8eccSSepherosa Ziehau 	   (stats.dot3StatsSingleCollisionFrames +
27946c8d8eccSSepherosa Ziehau 	   stats.dot3StatsMultipleCollisionFrames +
27956c8d8eccSSepherosa Ziehau 	   stats.dot3StatsExcessiveCollisions +
27966c8d8eccSSepherosa Ziehau 	   stats.dot3StatsLateCollisions) -
27976c8d8eccSSepherosa Ziehau 	   ifp->if_collisions;
27986c8d8eccSSepherosa Ziehau }
27996c8d8eccSSepherosa Ziehau 
28006c8d8eccSSepherosa Ziehau /*
28016c8d8eccSSepherosa Ziehau  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
28026c8d8eccSSepherosa Ziehau  * pointers to descriptors.
28036c8d8eccSSepherosa Ziehau  */
28046c8d8eccSSepherosa Ziehau static int
28056c8d8eccSSepherosa Ziehau bnx_encap(struct bnx_softc *sc, struct mbuf **m_head0, uint32_t *txidx)
28066c8d8eccSSepherosa Ziehau {
28076c8d8eccSSepherosa Ziehau 	struct bge_tx_bd *d = NULL;
280866deb1c1SSepherosa Ziehau 	uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
28096c8d8eccSSepherosa Ziehau 	bus_dma_segment_t segs[BNX_NSEG_NEW];
28106c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
28116c8d8eccSSepherosa Ziehau 	int error, maxsegs, nsegs, idx, i;
28126c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = *m_head0, *m_new;
28136c8d8eccSSepherosa Ziehau 
281466deb1c1SSepherosa Ziehau 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
281566deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
281666deb1c1SSepherosa Ziehau 		int tso_nsegs;
281766deb1c1SSepherosa Ziehau #endif
281866deb1c1SSepherosa Ziehau 
281966deb1c1SSepherosa Ziehau 		error = bnx_setup_tso(sc, m_head0, &mss, &csum_flags);
282066deb1c1SSepherosa Ziehau 		if (error)
282166deb1c1SSepherosa Ziehau 			return error;
282266deb1c1SSepherosa Ziehau 		m_head = *m_head0;
282366deb1c1SSepherosa Ziehau 
282466deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
2825f0336d39SSepherosa Ziehau 		tso_nsegs = (m_head->m_pkthdr.len /
2826f0336d39SSepherosa Ziehau 		    m_head->m_pkthdr.tso_segsz) - 1;
282766deb1c1SSepherosa Ziehau 		if (tso_nsegs > (BNX_TSO_NSTATS - 1))
282866deb1c1SSepherosa Ziehau 			tso_nsegs = BNX_TSO_NSTATS - 1;
282966deb1c1SSepherosa Ziehau 		else if (tso_nsegs < 0)
283066deb1c1SSepherosa Ziehau 			tso_nsegs = 0;
283166deb1c1SSepherosa Ziehau 		sc->bnx_tsosegs[tso_nsegs]++;
283266deb1c1SSepherosa Ziehau #endif
283366deb1c1SSepherosa Ziehau 	} else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
28346c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
28356c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
28366c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
28376c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
28386c8d8eccSSepherosa Ziehau 		if (m_head->m_flags & M_LASTFRAG)
28396c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
28406c8d8eccSSepherosa Ziehau 		else if (m_head->m_flags & M_FRAG)
28416c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
28426c8d8eccSSepherosa Ziehau 	}
284366deb1c1SSepherosa Ziehau 	if (m_head->m_flags & M_VLANTAG) {
284466deb1c1SSepherosa Ziehau 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
284566deb1c1SSepherosa Ziehau 		vlan_tag = m_head->m_pkthdr.ether_vlantag;
284666deb1c1SSepherosa Ziehau 	}
28476c8d8eccSSepherosa Ziehau 
28486c8d8eccSSepherosa Ziehau 	idx = *txidx;
28496c8d8eccSSepherosa Ziehau 	map = sc->bnx_cdata.bnx_tx_dmamap[idx];
28506c8d8eccSSepherosa Ziehau 
28516c8d8eccSSepherosa Ziehau 	maxsegs = (BGE_TX_RING_CNT - sc->bnx_txcnt) - BNX_NSEG_RSVD;
28526c8d8eccSSepherosa Ziehau 	KASSERT(maxsegs >= BNX_NSEG_SPARE,
28536c8d8eccSSepherosa Ziehau 		("not enough segments %d", maxsegs));
28546c8d8eccSSepherosa Ziehau 
28556c8d8eccSSepherosa Ziehau 	if (maxsegs > BNX_NSEG_NEW)
28566c8d8eccSSepherosa Ziehau 		maxsegs = BNX_NSEG_NEW;
28576c8d8eccSSepherosa Ziehau 
28586c8d8eccSSepherosa Ziehau 	/*
28596c8d8eccSSepherosa Ziehau 	 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
28606c8d8eccSSepherosa Ziehau 	 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
28616c8d8eccSSepherosa Ziehau 	 * but when such padded frames employ the bge IP/TCP checksum
28626c8d8eccSSepherosa Ziehau 	 * offload, the hardware checksum assist gives incorrect results
28636c8d8eccSSepherosa Ziehau 	 * (possibly from incorporating its own padding into the UDP/TCP
28646c8d8eccSSepherosa Ziehau 	 * checksum; who knows).  If we pad such runts with zeros, the
28656c8d8eccSSepherosa Ziehau 	 * onboard checksum comes out correct.
28666c8d8eccSSepherosa Ziehau 	 */
28676c8d8eccSSepherosa Ziehau 	if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
28686c8d8eccSSepherosa Ziehau 	    m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
28696c8d8eccSSepherosa Ziehau 		error = m_devpad(m_head, BNX_MIN_FRAMELEN);
28706c8d8eccSSepherosa Ziehau 		if (error)
28716c8d8eccSSepherosa Ziehau 			goto back;
28726c8d8eccSSepherosa Ziehau 	}
28736c8d8eccSSepherosa Ziehau 
28746c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_SHORTDMA) && m_head->m_next != NULL) {
28756c8d8eccSSepherosa Ziehau 		m_new = bnx_defrag_shortdma(m_head);
28766c8d8eccSSepherosa Ziehau 		if (m_new == NULL) {
28776c8d8eccSSepherosa Ziehau 			error = ENOBUFS;
28786c8d8eccSSepherosa Ziehau 			goto back;
28796c8d8eccSSepherosa Ziehau 		}
28806c8d8eccSSepherosa Ziehau 		*m_head0 = m_head = m_new;
28816c8d8eccSSepherosa Ziehau 	}
288266deb1c1SSepherosa Ziehau 	if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
288366deb1c1SSepherosa Ziehau 	    sc->bnx_force_defrag && m_head->m_next != NULL) {
28846c8d8eccSSepherosa Ziehau 		/*
28856c8d8eccSSepherosa Ziehau 		 * Forcefully defragment mbuf chain to overcome hardware
28866c8d8eccSSepherosa Ziehau 		 * limitation which only support a single outstanding
28876c8d8eccSSepherosa Ziehau 		 * DMA read operation.  If it fails, keep moving on using
28886c8d8eccSSepherosa Ziehau 		 * the original mbuf chain.
28896c8d8eccSSepherosa Ziehau 		 */
28906c8d8eccSSepherosa Ziehau 		m_new = m_defrag(m_head, MB_DONTWAIT);
28916c8d8eccSSepherosa Ziehau 		if (m_new != NULL)
28926c8d8eccSSepherosa Ziehau 			*m_head0 = m_head = m_new;
28936c8d8eccSSepherosa Ziehau 	}
28946c8d8eccSSepherosa Ziehau 
28956c8d8eccSSepherosa Ziehau 	error = bus_dmamap_load_mbuf_defrag(sc->bnx_cdata.bnx_tx_mtag, map,
28966c8d8eccSSepherosa Ziehau 			m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
28976c8d8eccSSepherosa Ziehau 	if (error)
28986c8d8eccSSepherosa Ziehau 		goto back;
28996c8d8eccSSepherosa Ziehau 
29006c8d8eccSSepherosa Ziehau 	m_head = *m_head0;
29016c8d8eccSSepherosa Ziehau 	bus_dmamap_sync(sc->bnx_cdata.bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
29026c8d8eccSSepherosa Ziehau 
29036c8d8eccSSepherosa Ziehau 	for (i = 0; ; i++) {
29046c8d8eccSSepherosa Ziehau 		d = &sc->bnx_ldata.bnx_tx_ring[idx];
29056c8d8eccSSepherosa Ziehau 
29066c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
29076c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
29086c8d8eccSSepherosa Ziehau 		d->bge_len = segs[i].ds_len;
29096c8d8eccSSepherosa Ziehau 		d->bge_flags = csum_flags;
291066deb1c1SSepherosa Ziehau 		d->bge_vlan_tag = vlan_tag;
291166deb1c1SSepherosa Ziehau 		d->bge_mss = mss;
29126c8d8eccSSepherosa Ziehau 
29136c8d8eccSSepherosa Ziehau 		if (i == nsegs - 1)
29146c8d8eccSSepherosa Ziehau 			break;
29156c8d8eccSSepherosa Ziehau 		BNX_INC(idx, BGE_TX_RING_CNT);
29166c8d8eccSSepherosa Ziehau 	}
29176c8d8eccSSepherosa Ziehau 	/* Mark the last segment as end of packet... */
29186c8d8eccSSepherosa Ziehau 	d->bge_flags |= BGE_TXBDFLAG_END;
29196c8d8eccSSepherosa Ziehau 
29206c8d8eccSSepherosa Ziehau 	/*
29216c8d8eccSSepherosa Ziehau 	 * Insure that the map for this transmission is placed at
29226c8d8eccSSepherosa Ziehau 	 * the array index of the last descriptor in this chain.
29236c8d8eccSSepherosa Ziehau 	 */
29246c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_tx_dmamap[*txidx] = sc->bnx_cdata.bnx_tx_dmamap[idx];
29256c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_tx_dmamap[idx] = map;
29266c8d8eccSSepherosa Ziehau 	sc->bnx_cdata.bnx_tx_chain[idx] = m_head;
29276c8d8eccSSepherosa Ziehau 	sc->bnx_txcnt += nsegs;
29286c8d8eccSSepherosa Ziehau 
29296c8d8eccSSepherosa Ziehau 	BNX_INC(idx, BGE_TX_RING_CNT);
29306c8d8eccSSepherosa Ziehau 	*txidx = idx;
29316c8d8eccSSepherosa Ziehau back:
29326c8d8eccSSepherosa Ziehau 	if (error) {
29336c8d8eccSSepherosa Ziehau 		m_freem(*m_head0);
29346c8d8eccSSepherosa Ziehau 		*m_head0 = NULL;
29356c8d8eccSSepherosa Ziehau 	}
29366c8d8eccSSepherosa Ziehau 	return error;
29376c8d8eccSSepherosa Ziehau }
29386c8d8eccSSepherosa Ziehau 
29396c8d8eccSSepherosa Ziehau /*
29406c8d8eccSSepherosa Ziehau  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
29416c8d8eccSSepherosa Ziehau  * to the mbuf data regions directly in the transmit descriptors.
29426c8d8eccSSepherosa Ziehau  */
29436c8d8eccSSepherosa Ziehau static void
29446c8d8eccSSepherosa Ziehau bnx_start(struct ifnet *ifp)
29456c8d8eccSSepherosa Ziehau {
29466c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
29476c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = NULL;
29486c8d8eccSSepherosa Ziehau 	uint32_t prodidx;
29496c8d8eccSSepherosa Ziehau 	int need_trans;
29506c8d8eccSSepherosa Ziehau 
29516c8d8eccSSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
29526c8d8eccSSepherosa Ziehau 		return;
29536c8d8eccSSepherosa Ziehau 
29546c8d8eccSSepherosa Ziehau 	prodidx = sc->bnx_tx_prodidx;
29556c8d8eccSSepherosa Ziehau 
29566c8d8eccSSepherosa Ziehau 	need_trans = 0;
29576c8d8eccSSepherosa Ziehau 	while (sc->bnx_cdata.bnx_tx_chain[prodidx] == NULL) {
29586c8d8eccSSepherosa Ziehau 		/*
29596c8d8eccSSepherosa Ziehau 		 * Sanity check: avoid coming within BGE_NSEG_RSVD
29606c8d8eccSSepherosa Ziehau 		 * descriptors of the end of the ring.  Also make
29616c8d8eccSSepherosa Ziehau 		 * sure there are BGE_NSEG_SPARE descriptors for
2962*a1bd58c9SSepherosa Ziehau 		 * jumbo buffers' or TSO segments' defragmentation.
29636c8d8eccSSepherosa Ziehau 		 */
29646c8d8eccSSepherosa Ziehau 		if ((BGE_TX_RING_CNT - sc->bnx_txcnt) <
29656c8d8eccSSepherosa Ziehau 		    (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
29666c8d8eccSSepherosa Ziehau 			ifp->if_flags |= IFF_OACTIVE;
29676c8d8eccSSepherosa Ziehau 			break;
29686c8d8eccSSepherosa Ziehau 		}
29696c8d8eccSSepherosa Ziehau 
2970*a1bd58c9SSepherosa Ziehau 		m_head = ifq_dequeue(&ifp->if_snd, NULL);
2971*a1bd58c9SSepherosa Ziehau 		if (m_head == NULL)
2972*a1bd58c9SSepherosa Ziehau 			break;
2973*a1bd58c9SSepherosa Ziehau 
29746c8d8eccSSepherosa Ziehau 		/*
29756c8d8eccSSepherosa Ziehau 		 * Pack the data into the transmit ring. If we
29766c8d8eccSSepherosa Ziehau 		 * don't have room, set the OACTIVE flag and wait
29776c8d8eccSSepherosa Ziehau 		 * for the NIC to drain the ring.
29786c8d8eccSSepherosa Ziehau 		 */
29796c8d8eccSSepherosa Ziehau 		if (bnx_encap(sc, &m_head, &prodidx)) {
29806c8d8eccSSepherosa Ziehau 			ifp->if_flags |= IFF_OACTIVE;
29816c8d8eccSSepherosa Ziehau 			ifp->if_oerrors++;
29826c8d8eccSSepherosa Ziehau 			break;
29836c8d8eccSSepherosa Ziehau 		}
29846c8d8eccSSepherosa Ziehau 		need_trans = 1;
29856c8d8eccSSepherosa Ziehau 
29866c8d8eccSSepherosa Ziehau 		ETHER_BPF_MTAP(ifp, m_head);
29876c8d8eccSSepherosa Ziehau 	}
29886c8d8eccSSepherosa Ziehau 
29896c8d8eccSSepherosa Ziehau 	if (!need_trans)
29906c8d8eccSSepherosa Ziehau 		return;
29916c8d8eccSSepherosa Ziehau 
29926c8d8eccSSepherosa Ziehau 	/* Transmit */
29936c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
29946c8d8eccSSepherosa Ziehau 
29956c8d8eccSSepherosa Ziehau 	sc->bnx_tx_prodidx = prodidx;
29966c8d8eccSSepherosa Ziehau 
29976c8d8eccSSepherosa Ziehau 	/*
29986c8d8eccSSepherosa Ziehau 	 * Set a timeout in case the chip goes out to lunch.
29996c8d8eccSSepherosa Ziehau 	 */
30006c8d8eccSSepherosa Ziehau 	ifp->if_timer = 5;
30016c8d8eccSSepherosa Ziehau }
30026c8d8eccSSepherosa Ziehau 
30036c8d8eccSSepherosa Ziehau static void
30046c8d8eccSSepherosa Ziehau bnx_init(void *xsc)
30056c8d8eccSSepherosa Ziehau {
30066c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
30076c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
30086c8d8eccSSepherosa Ziehau 	uint16_t *m;
30096c8d8eccSSepherosa Ziehau 	uint32_t mode;
30106c8d8eccSSepherosa Ziehau 
30116c8d8eccSSepherosa Ziehau 	ASSERT_SERIALIZED(ifp->if_serializer);
30126c8d8eccSSepherosa Ziehau 
30136c8d8eccSSepherosa Ziehau 	/* Cancel pending I/O and flush buffers. */
30146c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
30156c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
30166c8d8eccSSepherosa Ziehau 	bnx_chipinit(sc);
30176c8d8eccSSepherosa Ziehau 
30186c8d8eccSSepherosa Ziehau 	/*
30196c8d8eccSSepherosa Ziehau 	 * Init the various state machines, ring
30206c8d8eccSSepherosa Ziehau 	 * control blocks and firmware.
30216c8d8eccSSepherosa Ziehau 	 */
30226c8d8eccSSepherosa Ziehau 	if (bnx_blockinit(sc)) {
30236c8d8eccSSepherosa Ziehau 		if_printf(ifp, "initialization failure\n");
30246c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
30256c8d8eccSSepherosa Ziehau 		return;
30266c8d8eccSSepherosa Ziehau 	}
30276c8d8eccSSepherosa Ziehau 
30286c8d8eccSSepherosa Ziehau 	/* Specify MTU. */
30296c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
30306c8d8eccSSepherosa Ziehau 	    ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
30316c8d8eccSSepherosa Ziehau 
30326c8d8eccSSepherosa Ziehau 	/* Load our MAC address. */
30336c8d8eccSSepherosa Ziehau 	m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
30346c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
30356c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
30366c8d8eccSSepherosa Ziehau 
30376c8d8eccSSepherosa Ziehau 	/* Enable or disable promiscuous mode as needed. */
30386c8d8eccSSepherosa Ziehau 	bnx_setpromisc(sc);
30396c8d8eccSSepherosa Ziehau 
30406c8d8eccSSepherosa Ziehau 	/* Program multicast filter. */
30416c8d8eccSSepherosa Ziehau 	bnx_setmulti(sc);
30426c8d8eccSSepherosa Ziehau 
30436c8d8eccSSepherosa Ziehau 	/* Init RX ring. */
30446c8d8eccSSepherosa Ziehau 	if (bnx_init_rx_ring_std(sc)) {
30456c8d8eccSSepherosa Ziehau 		if_printf(ifp, "RX ring initialization failed\n");
30466c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
30476c8d8eccSSepherosa Ziehau 		return;
30486c8d8eccSSepherosa Ziehau 	}
30496c8d8eccSSepherosa Ziehau 
30506c8d8eccSSepherosa Ziehau 	/* Init jumbo RX ring. */
30516c8d8eccSSepherosa Ziehau 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
30526c8d8eccSSepherosa Ziehau 		if (bnx_init_rx_ring_jumbo(sc)) {
30536c8d8eccSSepherosa Ziehau 			if_printf(ifp, "Jumbo RX ring initialization failed\n");
30546c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
30556c8d8eccSSepherosa Ziehau 			return;
30566c8d8eccSSepherosa Ziehau 		}
30576c8d8eccSSepherosa Ziehau 	}
30586c8d8eccSSepherosa Ziehau 
30596c8d8eccSSepherosa Ziehau 	/* Init our RX return ring index */
30606c8d8eccSSepherosa Ziehau 	sc->bnx_rx_saved_considx = 0;
30616c8d8eccSSepherosa Ziehau 
30626c8d8eccSSepherosa Ziehau 	/* Init TX ring. */
30636c8d8eccSSepherosa Ziehau 	bnx_init_tx_ring(sc);
30646c8d8eccSSepherosa Ziehau 
30656c8d8eccSSepherosa Ziehau 	/* Enable TX MAC state machine lockup fix. */
30666c8d8eccSSepherosa Ziehau 	mode = CSR_READ_4(sc, BGE_TX_MODE);
30676c8d8eccSSepherosa Ziehau 	mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
30686c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
30696c8d8eccSSepherosa Ziehau 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
30706c8d8eccSSepherosa Ziehau 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
30716c8d8eccSSepherosa Ziehau 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
30726c8d8eccSSepherosa Ziehau 	}
30736c8d8eccSSepherosa Ziehau 	/* Turn on transmitter */
30746c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
30756c8d8eccSSepherosa Ziehau 
30766c8d8eccSSepherosa Ziehau 	/* Turn on receiver */
30776c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
30786c8d8eccSSepherosa Ziehau 
30796c8d8eccSSepherosa Ziehau 	/*
30806c8d8eccSSepherosa Ziehau 	 * Set the number of good frames to receive after RX MBUF
30816c8d8eccSSepherosa Ziehau 	 * Low Watermark has been reached.  After the RX MAC receives
30826c8d8eccSSepherosa Ziehau 	 * this number of frames, it will drop subsequent incoming
30836c8d8eccSSepherosa Ziehau 	 * frames until the MBUF High Watermark is reached.
30846c8d8eccSSepherosa Ziehau 	 */
3085bcb29629SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc))
30866c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
30876c8d8eccSSepherosa Ziehau 	else
30886c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
30896c8d8eccSSepherosa Ziehau 
30906c8d8eccSSepherosa Ziehau 	if (sc->bnx_irq_type == PCI_INTR_TYPE_MSI) {
30916c8d8eccSSepherosa Ziehau 		if (bootverbose) {
30926c8d8eccSSepherosa Ziehau 			if_printf(ifp, "MSI_MODE: %#x\n",
30936c8d8eccSSepherosa Ziehau 			    CSR_READ_4(sc, BGE_MSI_MODE));
30946c8d8eccSSepherosa Ziehau 		}
30956c8d8eccSSepherosa Ziehau 	}
30966c8d8eccSSepherosa Ziehau 
30976c8d8eccSSepherosa Ziehau 	/* Tell firmware we're alive. */
30986c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
30996c8d8eccSSepherosa Ziehau 
31006c8d8eccSSepherosa Ziehau 	/* Enable host interrupts if polling(4) is not enabled. */
31016c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
31026c8d8eccSSepherosa Ziehau #ifdef DEVICE_POLLING
31036c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_POLLING)
31046c8d8eccSSepherosa Ziehau 		bnx_disable_intr(sc);
31056c8d8eccSSepherosa Ziehau 	else
31066c8d8eccSSepherosa Ziehau #endif
31076c8d8eccSSepherosa Ziehau 	bnx_enable_intr(sc);
31086c8d8eccSSepherosa Ziehau 
31096c8d8eccSSepherosa Ziehau 	bnx_ifmedia_upd(ifp);
31106c8d8eccSSepherosa Ziehau 
31116c8d8eccSSepherosa Ziehau 	ifp->if_flags |= IFF_RUNNING;
31126c8d8eccSSepherosa Ziehau 	ifp->if_flags &= ~IFF_OACTIVE;
31136c8d8eccSSepherosa Ziehau 
31148ca0f604SSepherosa Ziehau 	callout_reset_bycpu(&sc->bnx_stat_timer, hz, bnx_tick, sc,
31158ca0f604SSepherosa Ziehau 	    sc->bnx_stat_cpuid);
31166c8d8eccSSepherosa Ziehau }
31176c8d8eccSSepherosa Ziehau 
31186c8d8eccSSepherosa Ziehau /*
31196c8d8eccSSepherosa Ziehau  * Set media options.
31206c8d8eccSSepherosa Ziehau  */
31216c8d8eccSSepherosa Ziehau static int
31226c8d8eccSSepherosa Ziehau bnx_ifmedia_upd(struct ifnet *ifp)
31236c8d8eccSSepherosa Ziehau {
31246c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
31256c8d8eccSSepherosa Ziehau 
31266c8d8eccSSepherosa Ziehau 	/* If this is a 1000baseX NIC, enable the TBI port. */
31276c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
31286c8d8eccSSepherosa Ziehau 		struct ifmedia *ifm = &sc->bnx_ifmedia;
31296c8d8eccSSepherosa Ziehau 
31306c8d8eccSSepherosa Ziehau 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
31316c8d8eccSSepherosa Ziehau 			return(EINVAL);
31326c8d8eccSSepherosa Ziehau 
31336c8d8eccSSepherosa Ziehau 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
31346c8d8eccSSepherosa Ziehau 		case IFM_AUTO:
31356c8d8eccSSepherosa Ziehau 			break;
31366c8d8eccSSepherosa Ziehau 
31376c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
31386c8d8eccSSepherosa Ziehau 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
31396c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
31406c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
31416c8d8eccSSepherosa Ziehau 			} else {
31426c8d8eccSSepherosa Ziehau 				BNX_SETBIT(sc, BGE_MAC_MODE,
31436c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
31446c8d8eccSSepherosa Ziehau 			}
31456c8d8eccSSepherosa Ziehau 			break;
31466c8d8eccSSepherosa Ziehau 		default:
31476c8d8eccSSepherosa Ziehau 			return(EINVAL);
31486c8d8eccSSepherosa Ziehau 		}
31496c8d8eccSSepherosa Ziehau 	} else {
31506c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
31516c8d8eccSSepherosa Ziehau 
31526c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
31536c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
31546c8d8eccSSepherosa Ziehau 		if (mii->mii_instance) {
31556c8d8eccSSepherosa Ziehau 			struct mii_softc *miisc;
31566c8d8eccSSepherosa Ziehau 
31576c8d8eccSSepherosa Ziehau 			LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
31586c8d8eccSSepherosa Ziehau 				mii_phy_reset(miisc);
31596c8d8eccSSepherosa Ziehau 		}
31606c8d8eccSSepherosa Ziehau 		mii_mediachg(mii);
31616c8d8eccSSepherosa Ziehau 
31626c8d8eccSSepherosa Ziehau 		/*
31636c8d8eccSSepherosa Ziehau 		 * Force an interrupt so that we will call bnx_link_upd
31646c8d8eccSSepherosa Ziehau 		 * if needed and clear any pending link state attention.
31656c8d8eccSSepherosa Ziehau 		 * Without this we are not getting any further interrupts
31666c8d8eccSSepherosa Ziehau 		 * for link state changes and thus will not UP the link and
31676c8d8eccSSepherosa Ziehau 		 * not be able to send in bnx_start.  The only way to get
31686c8d8eccSSepherosa Ziehau 		 * things working was to receive a packet and get an RX
31696c8d8eccSSepherosa Ziehau 		 * intr.
31706c8d8eccSSepherosa Ziehau 		 *
31716c8d8eccSSepherosa Ziehau 		 * bnx_tick should help for fiber cards and we might not
31726c8d8eccSSepherosa Ziehau 		 * need to do this here if BNX_FLAG_TBI is set but as
31736c8d8eccSSepherosa Ziehau 		 * we poll for fiber anyway it should not harm.
31746c8d8eccSSepherosa Ziehau 		 */
31756c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
31766c8d8eccSSepherosa Ziehau 	}
31776c8d8eccSSepherosa Ziehau 	return(0);
31786c8d8eccSSepherosa Ziehau }
31796c8d8eccSSepherosa Ziehau 
31806c8d8eccSSepherosa Ziehau /*
31816c8d8eccSSepherosa Ziehau  * Report current media status.
31826c8d8eccSSepherosa Ziehau  */
31836c8d8eccSSepherosa Ziehau static void
31846c8d8eccSSepherosa Ziehau bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
31856c8d8eccSSepherosa Ziehau {
31866c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
31876c8d8eccSSepherosa Ziehau 
31886c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
31896c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = IFM_AVALID;
31906c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = IFM_ETHER;
31916c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_STS) &
31926c8d8eccSSepherosa Ziehau 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
31936c8d8eccSSepherosa Ziehau 			ifmr->ifm_status |= IFM_ACTIVE;
31946c8d8eccSSepherosa Ziehau 		} else {
31956c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_NONE;
31966c8d8eccSSepherosa Ziehau 			return;
31976c8d8eccSSepherosa Ziehau 		}
31986c8d8eccSSepherosa Ziehau 
31996c8d8eccSSepherosa Ziehau 		ifmr->ifm_active |= IFM_1000_SX;
32006c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
32016c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_HDX;
32026c8d8eccSSepherosa Ziehau 		else
32036c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_FDX;
32046c8d8eccSSepherosa Ziehau 	} else {
32056c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
32066c8d8eccSSepherosa Ziehau 
32076c8d8eccSSepherosa Ziehau 		mii_pollstat(mii);
32086c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = mii->mii_media_active;
32096c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = mii->mii_media_status;
32106c8d8eccSSepherosa Ziehau 	}
32116c8d8eccSSepherosa Ziehau }
32126c8d8eccSSepherosa Ziehau 
32136c8d8eccSSepherosa Ziehau static int
32146c8d8eccSSepherosa Ziehau bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
32156c8d8eccSSepherosa Ziehau {
32166c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
32176c8d8eccSSepherosa Ziehau 	struct ifreq *ifr = (struct ifreq *)data;
32186c8d8eccSSepherosa Ziehau 	int mask, error = 0;
32196c8d8eccSSepherosa Ziehau 
32206c8d8eccSSepherosa Ziehau 	ASSERT_SERIALIZED(ifp->if_serializer);
32216c8d8eccSSepherosa Ziehau 
32226c8d8eccSSepherosa Ziehau 	switch (command) {
32236c8d8eccSSepherosa Ziehau 	case SIOCSIFMTU:
32246c8d8eccSSepherosa Ziehau 		if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
32256c8d8eccSSepherosa Ziehau 		    (BNX_IS_JUMBO_CAPABLE(sc) &&
32266c8d8eccSSepherosa Ziehau 		     ifr->ifr_mtu > BNX_JUMBO_MTU)) {
32276c8d8eccSSepherosa Ziehau 			error = EINVAL;
32286c8d8eccSSepherosa Ziehau 		} else if (ifp->if_mtu != ifr->ifr_mtu) {
32296c8d8eccSSepherosa Ziehau 			ifp->if_mtu = ifr->ifr_mtu;
32306c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING)
32316c8d8eccSSepherosa Ziehau 				bnx_init(sc);
32326c8d8eccSSepherosa Ziehau 		}
32336c8d8eccSSepherosa Ziehau 		break;
32346c8d8eccSSepherosa Ziehau 	case SIOCSIFFLAGS:
32356c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_UP) {
32366c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING) {
32376c8d8eccSSepherosa Ziehau 				mask = ifp->if_flags ^ sc->bnx_if_flags;
32386c8d8eccSSepherosa Ziehau 
32396c8d8eccSSepherosa Ziehau 				/*
32406c8d8eccSSepherosa Ziehau 				 * If only the state of the PROMISC flag
32416c8d8eccSSepherosa Ziehau 				 * changed, then just use the 'set promisc
32426c8d8eccSSepherosa Ziehau 				 * mode' command instead of reinitializing
32436c8d8eccSSepherosa Ziehau 				 * the entire NIC. Doing a full re-init
32446c8d8eccSSepherosa Ziehau 				 * means reloading the firmware and waiting
32456c8d8eccSSepherosa Ziehau 				 * for it to start up, which may take a
32466c8d8eccSSepherosa Ziehau 				 * second or two.  Similarly for ALLMULTI.
32476c8d8eccSSepherosa Ziehau 				 */
32486c8d8eccSSepherosa Ziehau 				if (mask & IFF_PROMISC)
32496c8d8eccSSepherosa Ziehau 					bnx_setpromisc(sc);
32506c8d8eccSSepherosa Ziehau 				if (mask & IFF_ALLMULTI)
32516c8d8eccSSepherosa Ziehau 					bnx_setmulti(sc);
32526c8d8eccSSepherosa Ziehau 			} else {
32536c8d8eccSSepherosa Ziehau 				bnx_init(sc);
32546c8d8eccSSepherosa Ziehau 			}
32556c8d8eccSSepherosa Ziehau 		} else if (ifp->if_flags & IFF_RUNNING) {
32566c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
32576c8d8eccSSepherosa Ziehau 		}
32586c8d8eccSSepherosa Ziehau 		sc->bnx_if_flags = ifp->if_flags;
32596c8d8eccSSepherosa Ziehau 		break;
32606c8d8eccSSepherosa Ziehau 	case SIOCADDMULTI:
32616c8d8eccSSepherosa Ziehau 	case SIOCDELMULTI:
32626c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING)
32636c8d8eccSSepherosa Ziehau 			bnx_setmulti(sc);
32646c8d8eccSSepherosa Ziehau 		break;
32656c8d8eccSSepherosa Ziehau 	case SIOCSIFMEDIA:
32666c8d8eccSSepherosa Ziehau 	case SIOCGIFMEDIA:
32676c8d8eccSSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_TBI) {
32686c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
32696c8d8eccSSepherosa Ziehau 			    &sc->bnx_ifmedia, command);
32706c8d8eccSSepherosa Ziehau 		} else {
32716c8d8eccSSepherosa Ziehau 			struct mii_data *mii;
32726c8d8eccSSepherosa Ziehau 
32736c8d8eccSSepherosa Ziehau 			mii = device_get_softc(sc->bnx_miibus);
32746c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
32756c8d8eccSSepherosa Ziehau 					      &mii->mii_media, command);
32766c8d8eccSSepherosa Ziehau 		}
32776c8d8eccSSepherosa Ziehau 		break;
32786c8d8eccSSepherosa Ziehau         case SIOCSIFCAP:
32796c8d8eccSSepherosa Ziehau 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
32806c8d8eccSSepherosa Ziehau 		if (mask & IFCAP_HWCSUM) {
32816c8d8eccSSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
328266deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TXCSUM)
328366deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= BNX_CSUM_FEATURES;
32846c8d8eccSSepherosa Ziehau 			else
328566deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
328666deb1c1SSepherosa Ziehau 		}
328766deb1c1SSepherosa Ziehau 		if (mask & IFCAP_TSO) {
328866deb1c1SSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_TSO);
328966deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TSO)
329066deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= CSUM_TSO;
329166deb1c1SSepherosa Ziehau 			else
329266deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~CSUM_TSO;
32936c8d8eccSSepherosa Ziehau 		}
32946c8d8eccSSepherosa Ziehau 		break;
32956c8d8eccSSepherosa Ziehau 	default:
32966c8d8eccSSepherosa Ziehau 		error = ether_ioctl(ifp, command, data);
32976c8d8eccSSepherosa Ziehau 		break;
32986c8d8eccSSepherosa Ziehau 	}
32996c8d8eccSSepherosa Ziehau 	return error;
33006c8d8eccSSepherosa Ziehau }
33016c8d8eccSSepherosa Ziehau 
33026c8d8eccSSepherosa Ziehau static void
33036c8d8eccSSepherosa Ziehau bnx_watchdog(struct ifnet *ifp)
33046c8d8eccSSepherosa Ziehau {
33056c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
33066c8d8eccSSepherosa Ziehau 
33076c8d8eccSSepherosa Ziehau 	if_printf(ifp, "watchdog timeout -- resetting\n");
33086c8d8eccSSepherosa Ziehau 
33096c8d8eccSSepherosa Ziehau 	bnx_init(sc);
33106c8d8eccSSepherosa Ziehau 
33116c8d8eccSSepherosa Ziehau 	ifp->if_oerrors++;
33126c8d8eccSSepherosa Ziehau 
33136c8d8eccSSepherosa Ziehau 	if (!ifq_is_empty(&ifp->if_snd))
33146c8d8eccSSepherosa Ziehau 		if_devstart(ifp);
33156c8d8eccSSepherosa Ziehau }
33166c8d8eccSSepherosa Ziehau 
33176c8d8eccSSepherosa Ziehau /*
33186c8d8eccSSepherosa Ziehau  * Stop the adapter and free any mbufs allocated to the
33196c8d8eccSSepherosa Ziehau  * RX and TX lists.
33206c8d8eccSSepherosa Ziehau  */
33216c8d8eccSSepherosa Ziehau static void
33226c8d8eccSSepherosa Ziehau bnx_stop(struct bnx_softc *sc)
33236c8d8eccSSepherosa Ziehau {
33246c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
33256c8d8eccSSepherosa Ziehau 
33266c8d8eccSSepherosa Ziehau 	ASSERT_SERIALIZED(ifp->if_serializer);
33276c8d8eccSSepherosa Ziehau 
33286c8d8eccSSepherosa Ziehau 	callout_stop(&sc->bnx_stat_timer);
33296c8d8eccSSepherosa Ziehau 
33306c8d8eccSSepherosa Ziehau 	/*
33316c8d8eccSSepherosa Ziehau 	 * Disable all of the receiver blocks
33326c8d8eccSSepherosa Ziehau 	 */
33336c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
33346c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
33356c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
33366c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
33376c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
33386c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
33396c8d8eccSSepherosa Ziehau 
33406c8d8eccSSepherosa Ziehau 	/*
33416c8d8eccSSepherosa Ziehau 	 * Disable all of the transmit blocks
33426c8d8eccSSepherosa Ziehau 	 */
33436c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
33446c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
33456c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
33466c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
33476c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
33486c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
33496c8d8eccSSepherosa Ziehau 
33506c8d8eccSSepherosa Ziehau 	/*
33516c8d8eccSSepherosa Ziehau 	 * Shut down all of the memory managers and related
33526c8d8eccSSepherosa Ziehau 	 * state machines.
33536c8d8eccSSepherosa Ziehau 	 */
33546c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
33556c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
33566c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
33576c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
33586c8d8eccSSepherosa Ziehau 
33596c8d8eccSSepherosa Ziehau 	/* Disable host interrupts. */
33606c8d8eccSSepherosa Ziehau 	bnx_disable_intr(sc);
33616c8d8eccSSepherosa Ziehau 
33626c8d8eccSSepherosa Ziehau 	/*
33636c8d8eccSSepherosa Ziehau 	 * Tell firmware we're shutting down.
33646c8d8eccSSepherosa Ziehau 	 */
33656c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
33666c8d8eccSSepherosa Ziehau 
33676c8d8eccSSepherosa Ziehau 	/* Free the RX lists. */
33686c8d8eccSSepherosa Ziehau 	bnx_free_rx_ring_std(sc);
33696c8d8eccSSepherosa Ziehau 
33706c8d8eccSSepherosa Ziehau 	/* Free jumbo RX list. */
33716c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
33726c8d8eccSSepherosa Ziehau 		bnx_free_rx_ring_jumbo(sc);
33736c8d8eccSSepherosa Ziehau 
33746c8d8eccSSepherosa Ziehau 	/* Free TX buffers. */
33756c8d8eccSSepherosa Ziehau 	bnx_free_tx_ring(sc);
33766c8d8eccSSepherosa Ziehau 
33776c8d8eccSSepherosa Ziehau 	sc->bnx_status_tag = 0;
33786c8d8eccSSepherosa Ziehau 	sc->bnx_link = 0;
33796c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
33806c8d8eccSSepherosa Ziehau 
33816c8d8eccSSepherosa Ziehau 	sc->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
33826c8d8eccSSepherosa Ziehau 
33836c8d8eccSSepherosa Ziehau 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
33846c8d8eccSSepherosa Ziehau 	ifp->if_timer = 0;
33856c8d8eccSSepherosa Ziehau }
33866c8d8eccSSepherosa Ziehau 
33876c8d8eccSSepherosa Ziehau /*
33886c8d8eccSSepherosa Ziehau  * Stop all chip I/O so that the kernel's probe routines don't
33896c8d8eccSSepherosa Ziehau  * get confused by errant DMAs when rebooting.
33906c8d8eccSSepherosa Ziehau  */
33916c8d8eccSSepherosa Ziehau static void
33926c8d8eccSSepherosa Ziehau bnx_shutdown(device_t dev)
33936c8d8eccSSepherosa Ziehau {
33946c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
33956c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
33966c8d8eccSSepherosa Ziehau 
33976c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
33986c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
33996c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
34006c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
34016c8d8eccSSepherosa Ziehau }
34026c8d8eccSSepherosa Ziehau 
34036c8d8eccSSepherosa Ziehau static int
34046c8d8eccSSepherosa Ziehau bnx_suspend(device_t dev)
34056c8d8eccSSepherosa Ziehau {
34066c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
34076c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
34086c8d8eccSSepherosa Ziehau 
34096c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
34106c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
34116c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
34126c8d8eccSSepherosa Ziehau 
34136c8d8eccSSepherosa Ziehau 	return 0;
34146c8d8eccSSepherosa Ziehau }
34156c8d8eccSSepherosa Ziehau 
34166c8d8eccSSepherosa Ziehau static int
34176c8d8eccSSepherosa Ziehau bnx_resume(device_t dev)
34186c8d8eccSSepherosa Ziehau {
34196c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
34206c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
34216c8d8eccSSepherosa Ziehau 
34226c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
34236c8d8eccSSepherosa Ziehau 
34246c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_UP) {
34256c8d8eccSSepherosa Ziehau 		bnx_init(sc);
34266c8d8eccSSepherosa Ziehau 
34276c8d8eccSSepherosa Ziehau 		if (!ifq_is_empty(&ifp->if_snd))
34286c8d8eccSSepherosa Ziehau 			if_devstart(ifp);
34296c8d8eccSSepherosa Ziehau 	}
34306c8d8eccSSepherosa Ziehau 
34316c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
34326c8d8eccSSepherosa Ziehau 
34336c8d8eccSSepherosa Ziehau 	return 0;
34346c8d8eccSSepherosa Ziehau }
34356c8d8eccSSepherosa Ziehau 
34366c8d8eccSSepherosa Ziehau static void
34376c8d8eccSSepherosa Ziehau bnx_setpromisc(struct bnx_softc *sc)
34386c8d8eccSSepherosa Ziehau {
34396c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
34406c8d8eccSSepherosa Ziehau 
34416c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_PROMISC)
34426c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
34436c8d8eccSSepherosa Ziehau 	else
34446c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
34456c8d8eccSSepherosa Ziehau }
34466c8d8eccSSepherosa Ziehau 
34476c8d8eccSSepherosa Ziehau static void
34486c8d8eccSSepherosa Ziehau bnx_dma_free(struct bnx_softc *sc)
34496c8d8eccSSepherosa Ziehau {
34506c8d8eccSSepherosa Ziehau 	int i;
34516c8d8eccSSepherosa Ziehau 
34526c8d8eccSSepherosa Ziehau 	/* Destroy RX mbuf DMA stuffs. */
34536c8d8eccSSepherosa Ziehau 	if (sc->bnx_cdata.bnx_rx_mtag != NULL) {
34546c8d8eccSSepherosa Ziehau 		for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
34556c8d8eccSSepherosa Ziehau 			bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
34566c8d8eccSSepherosa Ziehau 			    sc->bnx_cdata.bnx_rx_std_dmamap[i]);
34576c8d8eccSSepherosa Ziehau 		}
34586c8d8eccSSepherosa Ziehau 		bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
34596c8d8eccSSepherosa Ziehau 				   sc->bnx_cdata.bnx_rx_tmpmap);
34606c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
34616c8d8eccSSepherosa Ziehau 	}
34626c8d8eccSSepherosa Ziehau 
34636c8d8eccSSepherosa Ziehau 	/* Destroy TX mbuf DMA stuffs. */
34646c8d8eccSSepherosa Ziehau 	if (sc->bnx_cdata.bnx_tx_mtag != NULL) {
34656c8d8eccSSepherosa Ziehau 		for (i = 0; i < BGE_TX_RING_CNT; i++) {
34666c8d8eccSSepherosa Ziehau 			bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
34676c8d8eccSSepherosa Ziehau 			    sc->bnx_cdata.bnx_tx_dmamap[i]);
34686c8d8eccSSepherosa Ziehau 		}
34696c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
34706c8d8eccSSepherosa Ziehau 	}
34716c8d8eccSSepherosa Ziehau 
34726c8d8eccSSepherosa Ziehau 	/* Destroy standard RX ring */
34736c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_rx_std_ring_tag,
34746c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_rx_std_ring_map,
34756c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_rx_std_ring);
34766c8d8eccSSepherosa Ziehau 
34776c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
34786c8d8eccSSepherosa Ziehau 		bnx_free_jumbo_mem(sc);
34796c8d8eccSSepherosa Ziehau 
34806c8d8eccSSepherosa Ziehau 	/* Destroy RX return ring */
34816c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_rx_return_ring_tag,
34826c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_rx_return_ring_map,
34836c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_rx_return_ring);
34846c8d8eccSSepherosa Ziehau 
34856c8d8eccSSepherosa Ziehau 	/* Destroy TX ring */
34866c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_tx_ring_tag,
34876c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_tx_ring_map,
34886c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_tx_ring);
34896c8d8eccSSepherosa Ziehau 
34906c8d8eccSSepherosa Ziehau 	/* Destroy status block */
34916c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_status_tag,
34926c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_status_map,
34936c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_status_block);
34946c8d8eccSSepherosa Ziehau 
34956c8d8eccSSepherosa Ziehau 	/* Destroy the parent tag */
34966c8d8eccSSepherosa Ziehau 	if (sc->bnx_cdata.bnx_parent_tag != NULL)
34976c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
34986c8d8eccSSepherosa Ziehau }
34996c8d8eccSSepherosa Ziehau 
35006c8d8eccSSepherosa Ziehau static int
35016c8d8eccSSepherosa Ziehau bnx_dma_alloc(struct bnx_softc *sc)
35026c8d8eccSSepherosa Ziehau {
35036c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
350466deb1c1SSepherosa Ziehau 	bus_size_t txmaxsz;
35056c8d8eccSSepherosa Ziehau 	int i, error;
35066c8d8eccSSepherosa Ziehau 
35076c8d8eccSSepherosa Ziehau 	/*
35086c8d8eccSSepherosa Ziehau 	 * Allocate the parent bus DMA tag appropriate for PCI.
35096c8d8eccSSepherosa Ziehau 	 *
35106c8d8eccSSepherosa Ziehau 	 * All of the NetExtreme/NetLink controllers have 4GB boundary
35116c8d8eccSSepherosa Ziehau 	 * DMA bug.
35126c8d8eccSSepherosa Ziehau 	 * Whenever an address crosses a multiple of the 4GB boundary
35136c8d8eccSSepherosa Ziehau 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
35146c8d8eccSSepherosa Ziehau 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
35156c8d8eccSSepherosa Ziehau 	 * state machine will lockup and cause the device to hang.
35166c8d8eccSSepherosa Ziehau 	 */
35176c8d8eccSSepherosa Ziehau 	error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
35186c8d8eccSSepherosa Ziehau 				   BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
35196c8d8eccSSepherosa Ziehau 				   NULL, NULL,
35206c8d8eccSSepherosa Ziehau 				   BUS_SPACE_MAXSIZE_32BIT, 0,
35216c8d8eccSSepherosa Ziehau 				   BUS_SPACE_MAXSIZE_32BIT,
35226c8d8eccSSepherosa Ziehau 				   0, &sc->bnx_cdata.bnx_parent_tag);
35236c8d8eccSSepherosa Ziehau 	if (error) {
35246c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not allocate parent dma tag\n");
35256c8d8eccSSepherosa Ziehau 		return error;
35266c8d8eccSSepherosa Ziehau 	}
35276c8d8eccSSepherosa Ziehau 
35286c8d8eccSSepherosa Ziehau 	/*
35296c8d8eccSSepherosa Ziehau 	 * Create DMA tag and maps for RX mbufs.
35306c8d8eccSSepherosa Ziehau 	 */
35316c8d8eccSSepherosa Ziehau 	error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
35326c8d8eccSSepherosa Ziehau 				   BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
35336c8d8eccSSepherosa Ziehau 				   NULL, NULL, MCLBYTES, 1, MCLBYTES,
35346c8d8eccSSepherosa Ziehau 				   BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK,
35356c8d8eccSSepherosa Ziehau 				   &sc->bnx_cdata.bnx_rx_mtag);
35366c8d8eccSSepherosa Ziehau 	if (error) {
35376c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not allocate RX mbuf dma tag\n");
35386c8d8eccSSepherosa Ziehau 		return error;
35396c8d8eccSSepherosa Ziehau 	}
35406c8d8eccSSepherosa Ziehau 
35416c8d8eccSSepherosa Ziehau 	error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
35426c8d8eccSSepherosa Ziehau 				  BUS_DMA_WAITOK, &sc->bnx_cdata.bnx_rx_tmpmap);
35436c8d8eccSSepherosa Ziehau 	if (error) {
35446c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
35456c8d8eccSSepherosa Ziehau 		sc->bnx_cdata.bnx_rx_mtag = NULL;
35466c8d8eccSSepherosa Ziehau 		return error;
35476c8d8eccSSepherosa Ziehau 	}
35486c8d8eccSSepherosa Ziehau 
35496c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
35506c8d8eccSSepherosa Ziehau 		error = bus_dmamap_create(sc->bnx_cdata.bnx_rx_mtag,
35516c8d8eccSSepherosa Ziehau 					  BUS_DMA_WAITOK,
35526c8d8eccSSepherosa Ziehau 					  &sc->bnx_cdata.bnx_rx_std_dmamap[i]);
35536c8d8eccSSepherosa Ziehau 		if (error) {
35546c8d8eccSSepherosa Ziehau 			int j;
35556c8d8eccSSepherosa Ziehau 
35566c8d8eccSSepherosa Ziehau 			for (j = 0; j < i; ++j) {
35576c8d8eccSSepherosa Ziehau 				bus_dmamap_destroy(sc->bnx_cdata.bnx_rx_mtag,
35586c8d8eccSSepherosa Ziehau 					sc->bnx_cdata.bnx_rx_std_dmamap[j]);
35596c8d8eccSSepherosa Ziehau 			}
35606c8d8eccSSepherosa Ziehau 			bus_dma_tag_destroy(sc->bnx_cdata.bnx_rx_mtag);
35616c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_rx_mtag = NULL;
35626c8d8eccSSepherosa Ziehau 
35636c8d8eccSSepherosa Ziehau 			if_printf(ifp, "could not create DMA map for RX\n");
35646c8d8eccSSepherosa Ziehau 			return error;
35656c8d8eccSSepherosa Ziehau 		}
35666c8d8eccSSepherosa Ziehau 	}
35676c8d8eccSSepherosa Ziehau 
35686c8d8eccSSepherosa Ziehau 	/*
35696c8d8eccSSepherosa Ziehau 	 * Create DMA tag and maps for TX mbufs.
35706c8d8eccSSepherosa Ziehau 	 */
357166deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO)
357266deb1c1SSepherosa Ziehau 		txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
357366deb1c1SSepherosa Ziehau 	else
357466deb1c1SSepherosa Ziehau 		txmaxsz = BNX_JUMBO_FRAMELEN;
35756c8d8eccSSepherosa Ziehau 	error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
35766c8d8eccSSepherosa Ziehau 				   BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
35776c8d8eccSSepherosa Ziehau 				   NULL, NULL,
357866deb1c1SSepherosa Ziehau 				   txmaxsz, BNX_NSEG_NEW, PAGE_SIZE,
35796c8d8eccSSepherosa Ziehau 				   BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK |
35806c8d8eccSSepherosa Ziehau 				   BUS_DMA_ONEBPAGE,
35816c8d8eccSSepherosa Ziehau 				   &sc->bnx_cdata.bnx_tx_mtag);
35826c8d8eccSSepherosa Ziehau 	if (error) {
35836c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not allocate TX mbuf dma tag\n");
35846c8d8eccSSepherosa Ziehau 		return error;
35856c8d8eccSSepherosa Ziehau 	}
35866c8d8eccSSepherosa Ziehau 
35876c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
35886c8d8eccSSepherosa Ziehau 		error = bus_dmamap_create(sc->bnx_cdata.bnx_tx_mtag,
35896c8d8eccSSepherosa Ziehau 					  BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
35906c8d8eccSSepherosa Ziehau 					  &sc->bnx_cdata.bnx_tx_dmamap[i]);
35916c8d8eccSSepherosa Ziehau 		if (error) {
35926c8d8eccSSepherosa Ziehau 			int j;
35936c8d8eccSSepherosa Ziehau 
35946c8d8eccSSepherosa Ziehau 			for (j = 0; j < i; ++j) {
35956c8d8eccSSepherosa Ziehau 				bus_dmamap_destroy(sc->bnx_cdata.bnx_tx_mtag,
35966c8d8eccSSepherosa Ziehau 					sc->bnx_cdata.bnx_tx_dmamap[j]);
35976c8d8eccSSepherosa Ziehau 			}
35986c8d8eccSSepherosa Ziehau 			bus_dma_tag_destroy(sc->bnx_cdata.bnx_tx_mtag);
35996c8d8eccSSepherosa Ziehau 			sc->bnx_cdata.bnx_tx_mtag = NULL;
36006c8d8eccSSepherosa Ziehau 
36016c8d8eccSSepherosa Ziehau 			if_printf(ifp, "could not create DMA map for TX\n");
36026c8d8eccSSepherosa Ziehau 			return error;
36036c8d8eccSSepherosa Ziehau 		}
36046c8d8eccSSepherosa Ziehau 	}
36056c8d8eccSSepherosa Ziehau 
36066c8d8eccSSepherosa Ziehau 	/*
36076c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for standard RX ring.
36086c8d8eccSSepherosa Ziehau 	 */
36096c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
36106c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_std_ring_tag,
36116c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_std_ring_map,
36126c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_rx_std_ring,
36136c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_rx_std_ring_paddr);
36146c8d8eccSSepherosa Ziehau 	if (error) {
36156c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create std RX ring\n");
36166c8d8eccSSepherosa Ziehau 		return error;
36176c8d8eccSSepherosa Ziehau 	}
36186c8d8eccSSepherosa Ziehau 
36196c8d8eccSSepherosa Ziehau 	/*
36206c8d8eccSSepherosa Ziehau 	 * Create jumbo buffer pool.
36216c8d8eccSSepherosa Ziehau 	 */
36226c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
36236c8d8eccSSepherosa Ziehau 		error = bnx_alloc_jumbo_mem(sc);
36246c8d8eccSSepherosa Ziehau 		if (error) {
36256c8d8eccSSepherosa Ziehau 			if_printf(ifp, "could not create jumbo buffer pool\n");
36266c8d8eccSSepherosa Ziehau 			return error;
36276c8d8eccSSepherosa Ziehau 		}
36286c8d8eccSSepherosa Ziehau 	}
36296c8d8eccSSepherosa Ziehau 
36306c8d8eccSSepherosa Ziehau 	/*
36316c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for RX return ring.
36326c8d8eccSSepherosa Ziehau 	 */
36336c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc,
36346c8d8eccSSepherosa Ziehau 	    BGE_RX_RTN_RING_SZ(sc->bnx_return_ring_cnt),
36356c8d8eccSSepherosa Ziehau 	    &sc->bnx_cdata.bnx_rx_return_ring_tag,
36366c8d8eccSSepherosa Ziehau 	    &sc->bnx_cdata.bnx_rx_return_ring_map,
36376c8d8eccSSepherosa Ziehau 	    (void *)&sc->bnx_ldata.bnx_rx_return_ring,
36386c8d8eccSSepherosa Ziehau 	    &sc->bnx_ldata.bnx_rx_return_ring_paddr);
36396c8d8eccSSepherosa Ziehau 	if (error) {
36406c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create RX ret ring\n");
36416c8d8eccSSepherosa Ziehau 		return error;
36426c8d8eccSSepherosa Ziehau 	}
36436c8d8eccSSepherosa Ziehau 
36446c8d8eccSSepherosa Ziehau 	/*
36456c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for TX ring.
36466c8d8eccSSepherosa Ziehau 	 */
36476c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_TX_RING_SZ,
36486c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_tx_ring_tag,
36496c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_tx_ring_map,
36506c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_tx_ring,
36516c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_tx_ring_paddr);
36526c8d8eccSSepherosa Ziehau 	if (error) {
36536c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create TX ring\n");
36546c8d8eccSSepherosa Ziehau 		return error;
36556c8d8eccSSepherosa Ziehau 	}
36566c8d8eccSSepherosa Ziehau 
36576c8d8eccSSepherosa Ziehau 	/*
36586c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for status block.
36596c8d8eccSSepherosa Ziehau 	 */
36606c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_STATUS_BLK_SZ,
36616c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_status_tag,
36626c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_status_map,
36636c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_status_block,
36646c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_status_block_paddr);
36656c8d8eccSSepherosa Ziehau 	if (error) {
36666c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create status block\n");
36676c8d8eccSSepherosa Ziehau 		return error;
36686c8d8eccSSepherosa Ziehau 	}
36696c8d8eccSSepherosa Ziehau 
36706c8d8eccSSepherosa Ziehau 	return 0;
36716c8d8eccSSepherosa Ziehau }
36726c8d8eccSSepherosa Ziehau 
36736c8d8eccSSepherosa Ziehau static int
36746c8d8eccSSepherosa Ziehau bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
36756c8d8eccSSepherosa Ziehau 		    bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
36766c8d8eccSSepherosa Ziehau {
36776c8d8eccSSepherosa Ziehau 	bus_dmamem_t dmem;
36786c8d8eccSSepherosa Ziehau 	int error;
36796c8d8eccSSepherosa Ziehau 
36806c8d8eccSSepherosa Ziehau 	error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
36816c8d8eccSSepherosa Ziehau 				    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
36826c8d8eccSSepherosa Ziehau 				    size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
36836c8d8eccSSepherosa Ziehau 	if (error)
36846c8d8eccSSepherosa Ziehau 		return error;
36856c8d8eccSSepherosa Ziehau 
36866c8d8eccSSepherosa Ziehau 	*tag = dmem.dmem_tag;
36876c8d8eccSSepherosa Ziehau 	*map = dmem.dmem_map;
36886c8d8eccSSepherosa Ziehau 	*addr = dmem.dmem_addr;
36896c8d8eccSSepherosa Ziehau 	*paddr = dmem.dmem_busaddr;
36906c8d8eccSSepherosa Ziehau 
36916c8d8eccSSepherosa Ziehau 	return 0;
36926c8d8eccSSepherosa Ziehau }
36936c8d8eccSSepherosa Ziehau 
36946c8d8eccSSepherosa Ziehau static void
36956c8d8eccSSepherosa Ziehau bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
36966c8d8eccSSepherosa Ziehau {
36976c8d8eccSSepherosa Ziehau 	if (tag != NULL) {
36986c8d8eccSSepherosa Ziehau 		bus_dmamap_unload(tag, map);
36996c8d8eccSSepherosa Ziehau 		bus_dmamem_free(tag, addr, map);
37006c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(tag);
37016c8d8eccSSepherosa Ziehau 	}
37026c8d8eccSSepherosa Ziehau }
37036c8d8eccSSepherosa Ziehau 
37046c8d8eccSSepherosa Ziehau static void
37056c8d8eccSSepherosa Ziehau bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
37066c8d8eccSSepherosa Ziehau {
37076c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
37086c8d8eccSSepherosa Ziehau 
37096c8d8eccSSepherosa Ziehau #define PCS_ENCODE_ERR	(BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
37106c8d8eccSSepherosa Ziehau 
37116c8d8eccSSepherosa Ziehau 	/*
37126c8d8eccSSepherosa Ziehau 	 * Sometimes PCS encoding errors are detected in
37136c8d8eccSSepherosa Ziehau 	 * TBI mode (on fiber NICs), and for some reason
37146c8d8eccSSepherosa Ziehau 	 * the chip will signal them as link changes.
37156c8d8eccSSepherosa Ziehau 	 * If we get a link change event, but the 'PCS
37166c8d8eccSSepherosa Ziehau 	 * encoding error' bit in the MAC status register
37176c8d8eccSSepherosa Ziehau 	 * is set, don't bother doing a link check.
37186c8d8eccSSepherosa Ziehau 	 * This avoids spurious "gigabit link up" messages
37196c8d8eccSSepherosa Ziehau 	 * that sometimes appear on fiber NICs during
37206c8d8eccSSepherosa Ziehau 	 * periods of heavy traffic.
37216c8d8eccSSepherosa Ziehau 	 */
37226c8d8eccSSepherosa Ziehau 	if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
37236c8d8eccSSepherosa Ziehau 		if (!sc->bnx_link) {
37246c8d8eccSSepherosa Ziehau 			sc->bnx_link++;
37256c8d8eccSSepherosa Ziehau 			if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
37266c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
37276c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_TBI_SEND_CFGS);
37286c8d8eccSSepherosa Ziehau 			}
37296c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
37306c8d8eccSSepherosa Ziehau 
37316c8d8eccSSepherosa Ziehau 			if (bootverbose)
37326c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link UP\n");
37336c8d8eccSSepherosa Ziehau 
37346c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_UP;
37356c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
37366c8d8eccSSepherosa Ziehau 		}
37376c8d8eccSSepherosa Ziehau 	} else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
37386c8d8eccSSepherosa Ziehau 		if (sc->bnx_link) {
37396c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
37406c8d8eccSSepherosa Ziehau 
37416c8d8eccSSepherosa Ziehau 			if (bootverbose)
37426c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link DOWN\n");
37436c8d8eccSSepherosa Ziehau 
37446c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_DOWN;
37456c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
37466c8d8eccSSepherosa Ziehau 		}
37476c8d8eccSSepherosa Ziehau 	}
37486c8d8eccSSepherosa Ziehau 
37496c8d8eccSSepherosa Ziehau #undef PCS_ENCODE_ERR
37506c8d8eccSSepherosa Ziehau 
37516c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
37526c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
37536c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
37546c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
37556c8d8eccSSepherosa Ziehau }
37566c8d8eccSSepherosa Ziehau 
37576c8d8eccSSepherosa Ziehau static void
37586c8d8eccSSepherosa Ziehau bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
37596c8d8eccSSepherosa Ziehau {
37606c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
37616c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
37626c8d8eccSSepherosa Ziehau 
37636c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
37646c8d8eccSSepherosa Ziehau 	bnx_miibus_statchg(sc->bnx_dev);
37656c8d8eccSSepherosa Ziehau 
37666c8d8eccSSepherosa Ziehau 	if (bootverbose) {
37676c8d8eccSSepherosa Ziehau 		if (sc->bnx_link)
37686c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
37696c8d8eccSSepherosa Ziehau 		else
37706c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
37716c8d8eccSSepherosa Ziehau 	}
37726c8d8eccSSepherosa Ziehau 
37736c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
37746c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
37756c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
37766c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
37776c8d8eccSSepherosa Ziehau }
37786c8d8eccSSepherosa Ziehau 
37796c8d8eccSSepherosa Ziehau static void
37806c8d8eccSSepherosa Ziehau bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
37816c8d8eccSSepherosa Ziehau {
37826c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
37836c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
37846c8d8eccSSepherosa Ziehau 
37856c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
37866c8d8eccSSepherosa Ziehau 
37876c8d8eccSSepherosa Ziehau 	if (!sc->bnx_link &&
37886c8d8eccSSepherosa Ziehau 	    (mii->mii_media_status & IFM_ACTIVE) &&
37896c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37906c8d8eccSSepherosa Ziehau 		sc->bnx_link++;
37916c8d8eccSSepherosa Ziehau 		if (bootverbose)
37926c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
37936c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_link &&
37946c8d8eccSSepherosa Ziehau 	    (!(mii->mii_media_status & IFM_ACTIVE) ||
37956c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37966c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
37976c8d8eccSSepherosa Ziehau 		if (bootverbose)
37986c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
37996c8d8eccSSepherosa Ziehau 	}
38006c8d8eccSSepherosa Ziehau 
38016c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
38026c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
38036c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
38046c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
38056c8d8eccSSepherosa Ziehau }
38066c8d8eccSSepherosa Ziehau 
38076c8d8eccSSepherosa Ziehau static int
38086c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
38096c8d8eccSSepherosa Ziehau {
38106c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38116c8d8eccSSepherosa Ziehau 
38126c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38136c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_ticks,
38146c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
38156c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_CHG);
38166c8d8eccSSepherosa Ziehau }
38176c8d8eccSSepherosa Ziehau 
38186c8d8eccSSepherosa Ziehau static int
38196c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
38206c8d8eccSSepherosa Ziehau {
38216c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38226c8d8eccSSepherosa Ziehau 
38236c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38246c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_ticks,
38256c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
38266c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_CHG);
38276c8d8eccSSepherosa Ziehau }
38286c8d8eccSSepherosa Ziehau 
38296c8d8eccSSepherosa Ziehau static int
38306c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
38316c8d8eccSSepherosa Ziehau {
38326c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38336c8d8eccSSepherosa Ziehau 
38346c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38356c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds,
38366c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
38376c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_CHG);
38386c8d8eccSSepherosa Ziehau }
38396c8d8eccSSepherosa Ziehau 
38406c8d8eccSSepherosa Ziehau static int
38416c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
38426c8d8eccSSepherosa Ziehau {
38436c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38446c8d8eccSSepherosa Ziehau 
38456c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38466c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds,
38476c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
38486c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_CHG);
38496c8d8eccSSepherosa Ziehau }
38506c8d8eccSSepherosa Ziehau 
38516c8d8eccSSepherosa Ziehau static int
38526c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
38536c8d8eccSSepherosa Ziehau {
38546c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38556c8d8eccSSepherosa Ziehau 
38566c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38576c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds_int,
38586c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
38596c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_INT_CHG);
38606c8d8eccSSepherosa Ziehau }
38616c8d8eccSSepherosa Ziehau 
38626c8d8eccSSepherosa Ziehau static int
38636c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
38646c8d8eccSSepherosa Ziehau {
38656c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38666c8d8eccSSepherosa Ziehau 
38676c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
38686c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds_int,
38696c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
38706c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_INT_CHG);
38716c8d8eccSSepherosa Ziehau }
38726c8d8eccSSepherosa Ziehau 
38736c8d8eccSSepherosa Ziehau static int
38746c8d8eccSSepherosa Ziehau bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
38756c8d8eccSSepherosa Ziehau     int coal_min, int coal_max, uint32_t coal_chg_mask)
38766c8d8eccSSepherosa Ziehau {
38776c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38786c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
38796c8d8eccSSepherosa Ziehau 	int error = 0, v;
38806c8d8eccSSepherosa Ziehau 
38816c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
38826c8d8eccSSepherosa Ziehau 
38836c8d8eccSSepherosa Ziehau 	v = *coal;
38846c8d8eccSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &v, 0, req);
38856c8d8eccSSepherosa Ziehau 	if (!error && req->newptr != NULL) {
38866c8d8eccSSepherosa Ziehau 		if (v < coal_min || v > coal_max) {
38876c8d8eccSSepherosa Ziehau 			error = EINVAL;
38886c8d8eccSSepherosa Ziehau 		} else {
38896c8d8eccSSepherosa Ziehau 			*coal = v;
38906c8d8eccSSepherosa Ziehau 			sc->bnx_coal_chg |= coal_chg_mask;
38916c8d8eccSSepherosa Ziehau 		}
38926c8d8eccSSepherosa Ziehau 	}
38936c8d8eccSSepherosa Ziehau 
38946c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
38956c8d8eccSSepherosa Ziehau 	return error;
38966c8d8eccSSepherosa Ziehau }
38976c8d8eccSSepherosa Ziehau 
38986c8d8eccSSepherosa Ziehau static void
38996c8d8eccSSepherosa Ziehau bnx_coal_change(struct bnx_softc *sc)
39006c8d8eccSSepherosa Ziehau {
39016c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
39026c8d8eccSSepherosa Ziehau 	uint32_t val;
39036c8d8eccSSepherosa Ziehau 
39046c8d8eccSSepherosa Ziehau 	ASSERT_SERIALIZED(ifp->if_serializer);
39056c8d8eccSSepherosa Ziehau 
39066c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
39076c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
39086c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_ticks);
39096c8d8eccSSepherosa Ziehau 		DELAY(10);
39106c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
39116c8d8eccSSepherosa Ziehau 
39126c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39136c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_ticks -> %u\n",
39146c8d8eccSSepherosa Ziehau 				  sc->bnx_rx_coal_ticks);
39156c8d8eccSSepherosa Ziehau 		}
39166c8d8eccSSepherosa Ziehau 	}
39176c8d8eccSSepherosa Ziehau 
39186c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
39196c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
39206c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_ticks);
39216c8d8eccSSepherosa Ziehau 		DELAY(10);
39226c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_TX_COAL_TICKS);
39236c8d8eccSSepherosa Ziehau 
39246c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39256c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_ticks -> %u\n",
39266c8d8eccSSepherosa Ziehau 				  sc->bnx_tx_coal_ticks);
39276c8d8eccSSepherosa Ziehau 		}
39286c8d8eccSSepherosa Ziehau 	}
39296c8d8eccSSepherosa Ziehau 
39306c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
39316c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
39326c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds);
39336c8d8eccSSepherosa Ziehau 		DELAY(10);
39346c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
39356c8d8eccSSepherosa Ziehau 
39366c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39376c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_bds -> %u\n",
39386c8d8eccSSepherosa Ziehau 				  sc->bnx_rx_coal_bds);
39396c8d8eccSSepherosa Ziehau 		}
39406c8d8eccSSepherosa Ziehau 	}
39416c8d8eccSSepherosa Ziehau 
39426c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
39436c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS,
39446c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds);
39456c8d8eccSSepherosa Ziehau 		DELAY(10);
39466c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS);
39476c8d8eccSSepherosa Ziehau 
39486c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39496c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_max_coal_bds -> %u\n",
39506c8d8eccSSepherosa Ziehau 				  sc->bnx_tx_coal_bds);
39516c8d8eccSSepherosa Ziehau 		}
39526c8d8eccSSepherosa Ziehau 	}
39536c8d8eccSSepherosa Ziehau 
39546c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
39556c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
39566c8d8eccSSepherosa Ziehau 		    sc->bnx_rx_coal_bds_int);
39576c8d8eccSSepherosa Ziehau 		DELAY(10);
39586c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT);
39596c8d8eccSSepherosa Ziehau 
39606c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39616c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_bds_int -> %u\n",
39626c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds_int);
39636c8d8eccSSepherosa Ziehau 		}
39646c8d8eccSSepherosa Ziehau 	}
39656c8d8eccSSepherosa Ziehau 
39666c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
39676c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
39686c8d8eccSSepherosa Ziehau 		    sc->bnx_tx_coal_bds_int);
39696c8d8eccSSepherosa Ziehau 		DELAY(10);
39706c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT);
39716c8d8eccSSepherosa Ziehau 
39726c8d8eccSSepherosa Ziehau 		if (bootverbose) {
39736c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_bds_int -> %u\n",
39746c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds_int);
39756c8d8eccSSepherosa Ziehau 		}
39766c8d8eccSSepherosa Ziehau 	}
39776c8d8eccSSepherosa Ziehau 
39786c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
39796c8d8eccSSepherosa Ziehau }
39806c8d8eccSSepherosa Ziehau 
39816c8d8eccSSepherosa Ziehau static void
3982df9ccc98SSepherosa Ziehau bnx_intr_check(void *xsc)
3983df9ccc98SSepherosa Ziehau {
3984df9ccc98SSepherosa Ziehau 	struct bnx_softc *sc = xsc;
3985df9ccc98SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
3986df9ccc98SSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
3987df9ccc98SSepherosa Ziehau 
3988df9ccc98SSepherosa Ziehau 	lwkt_serialize_enter(ifp->if_serializer);
3989df9ccc98SSepherosa Ziehau 
3990df9ccc98SSepherosa Ziehau 	KKASSERT(mycpuid == sc->bnx_intr_cpuid);
3991df9ccc98SSepherosa Ziehau 
3992df9ccc98SSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_POLLING)) != IFF_RUNNING) {
3993df9ccc98SSepherosa Ziehau 		lwkt_serialize_exit(ifp->if_serializer);
3994df9ccc98SSepherosa Ziehau 		return;
3995df9ccc98SSepherosa Ziehau 	}
3996df9ccc98SSepherosa Ziehau 
3997df9ccc98SSepherosa Ziehau 	if (sblk->bge_idx[0].bge_rx_prod_idx != sc->bnx_rx_saved_considx ||
3998df9ccc98SSepherosa Ziehau 	    sblk->bge_idx[0].bge_tx_cons_idx != sc->bnx_tx_saved_considx) {
3999df9ccc98SSepherosa Ziehau 		if (sc->bnx_rx_check_considx == sc->bnx_rx_saved_considx &&
4000df9ccc98SSepherosa Ziehau 		    sc->bnx_tx_check_considx == sc->bnx_tx_saved_considx) {
4001df9ccc98SSepherosa Ziehau 			if (!sc->bnx_intr_maylose) {
4002df9ccc98SSepherosa Ziehau 				sc->bnx_intr_maylose = TRUE;
4003df9ccc98SSepherosa Ziehau 				goto done;
4004df9ccc98SSepherosa Ziehau 			}
4005df9ccc98SSepherosa Ziehau 			if (bootverbose)
4006df9ccc98SSepherosa Ziehau 				if_printf(ifp, "lost interrupt\n");
4007df9ccc98SSepherosa Ziehau 			bnx_msi(sc);
4008df9ccc98SSepherosa Ziehau 		}
4009df9ccc98SSepherosa Ziehau 	}
4010df9ccc98SSepherosa Ziehau 	sc->bnx_intr_maylose = FALSE;
4011df9ccc98SSepherosa Ziehau 	sc->bnx_rx_check_considx = sc->bnx_rx_saved_considx;
4012df9ccc98SSepherosa Ziehau 	sc->bnx_tx_check_considx = sc->bnx_tx_saved_considx;
4013df9ccc98SSepherosa Ziehau 
4014df9ccc98SSepherosa Ziehau done:
4015df9ccc98SSepherosa Ziehau 	callout_reset(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4016df9ccc98SSepherosa Ziehau 	    bnx_intr_check, sc);
4017df9ccc98SSepherosa Ziehau 	lwkt_serialize_exit(ifp->if_serializer);
4018df9ccc98SSepherosa Ziehau }
4019df9ccc98SSepherosa Ziehau 
4020df9ccc98SSepherosa Ziehau static void
40216c8d8eccSSepherosa Ziehau bnx_enable_intr(struct bnx_softc *sc)
40226c8d8eccSSepherosa Ziehau {
40236c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40246c8d8eccSSepherosa Ziehau 
40256c8d8eccSSepherosa Ziehau 	lwkt_serialize_handler_enable(ifp->if_serializer);
40266c8d8eccSSepherosa Ziehau 
40276c8d8eccSSepherosa Ziehau 	/*
40286c8d8eccSSepherosa Ziehau 	 * Enable interrupt.
40296c8d8eccSSepherosa Ziehau 	 */
40306c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
40316c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
40326c8d8eccSSepherosa Ziehau 		/* XXX Linux driver */
40336c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_status_tag << 24);
40346c8d8eccSSepherosa Ziehau 	}
40356c8d8eccSSepherosa Ziehau 
40366c8d8eccSSepherosa Ziehau 	/*
40376c8d8eccSSepherosa Ziehau 	 * Unmask the interrupt when we stop polling.
40386c8d8eccSSepherosa Ziehau 	 */
40396c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
40406c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
40416c8d8eccSSepherosa Ziehau 
40426c8d8eccSSepherosa Ziehau 	/*
40436c8d8eccSSepherosa Ziehau 	 * Trigger another interrupt, since above writing
40446c8d8eccSSepherosa Ziehau 	 * to interrupt mailbox0 may acknowledge pending
40456c8d8eccSSepherosa Ziehau 	 * interrupt.
40466c8d8eccSSepherosa Ziehau 	 */
40476c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4048df9ccc98SSepherosa Ziehau 
4049df9ccc98SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4050df9ccc98SSepherosa Ziehau 		sc->bnx_intr_maylose = FALSE;
4051df9ccc98SSepherosa Ziehau 		sc->bnx_rx_check_considx = 0;
4052df9ccc98SSepherosa Ziehau 		sc->bnx_tx_check_considx = 0;
4053df9ccc98SSepherosa Ziehau 
4054df9ccc98SSepherosa Ziehau 		if (bootverbose)
4055df9ccc98SSepherosa Ziehau 			if_printf(ifp, "status tag bug workaround\n");
4056df9ccc98SSepherosa Ziehau 
4057df9ccc98SSepherosa Ziehau 		/* 10ms check interval */
4058df9ccc98SSepherosa Ziehau 		callout_reset_bycpu(&sc->bnx_intr_timer, BNX_INTR_CKINTVL,
4059df9ccc98SSepherosa Ziehau 		    bnx_intr_check, sc, sc->bnx_intr_cpuid);
4060df9ccc98SSepherosa Ziehau 	}
40616c8d8eccSSepherosa Ziehau }
40626c8d8eccSSepherosa Ziehau 
40636c8d8eccSSepherosa Ziehau static void
40646c8d8eccSSepherosa Ziehau bnx_disable_intr(struct bnx_softc *sc)
40656c8d8eccSSepherosa Ziehau {
40666c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40676c8d8eccSSepherosa Ziehau 
40686c8d8eccSSepherosa Ziehau 	/*
40696c8d8eccSSepherosa Ziehau 	 * Mask the interrupt when we start polling.
40706c8d8eccSSepherosa Ziehau 	 */
40716c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
40726c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
40736c8d8eccSSepherosa Ziehau 
40746c8d8eccSSepherosa Ziehau 	/*
40756c8d8eccSSepherosa Ziehau 	 * Acknowledge possible asserted interrupt.
40766c8d8eccSSepherosa Ziehau 	 */
40776c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
40786c8d8eccSSepherosa Ziehau 
4079df9ccc98SSepherosa Ziehau 	callout_stop(&sc->bnx_intr_timer);
4080df9ccc98SSepherosa Ziehau 	sc->bnx_intr_maylose = FALSE;
4081df9ccc98SSepherosa Ziehau 	sc->bnx_rx_check_considx = 0;
4082df9ccc98SSepherosa Ziehau 	sc->bnx_tx_check_considx = 0;
4083df9ccc98SSepherosa Ziehau 
40846c8d8eccSSepherosa Ziehau 	lwkt_serialize_handler_disable(ifp->if_serializer);
40856c8d8eccSSepherosa Ziehau }
40866c8d8eccSSepherosa Ziehau 
40876c8d8eccSSepherosa Ziehau static int
40886c8d8eccSSepherosa Ziehau bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
40896c8d8eccSSepherosa Ziehau {
40906c8d8eccSSepherosa Ziehau 	uint32_t mac_addr;
40916c8d8eccSSepherosa Ziehau 	int ret = 1;
40926c8d8eccSSepherosa Ziehau 
40936c8d8eccSSepherosa Ziehau 	mac_addr = bnx_readmem_ind(sc, 0x0c14);
40946c8d8eccSSepherosa Ziehau 	if ((mac_addr >> 16) == 0x484b) {
40956c8d8eccSSepherosa Ziehau 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
40966c8d8eccSSepherosa Ziehau 		ether_addr[1] = (uint8_t)mac_addr;
40976c8d8eccSSepherosa Ziehau 		mac_addr = bnx_readmem_ind(sc, 0x0c18);
40986c8d8eccSSepherosa Ziehau 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
40996c8d8eccSSepherosa Ziehau 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
41006c8d8eccSSepherosa Ziehau 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
41016c8d8eccSSepherosa Ziehau 		ether_addr[5] = (uint8_t)mac_addr;
41026c8d8eccSSepherosa Ziehau 		ret = 0;
41036c8d8eccSSepherosa Ziehau 	}
41046c8d8eccSSepherosa Ziehau 	return ret;
41056c8d8eccSSepherosa Ziehau }
41066c8d8eccSSepherosa Ziehau 
41076c8d8eccSSepherosa Ziehau static int
41086c8d8eccSSepherosa Ziehau bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
41096c8d8eccSSepherosa Ziehau {
41106c8d8eccSSepherosa Ziehau 	int mac_offset = BGE_EE_MAC_OFFSET;
41116c8d8eccSSepherosa Ziehau 
411280969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
411380969639SSepherosa Ziehau 		int f;
411480969639SSepherosa Ziehau 
411580969639SSepherosa Ziehau 		f = pci_get_function(sc->bnx_dev);
411680969639SSepherosa Ziehau 		if (f & 1)
411780969639SSepherosa Ziehau 			mac_offset = BGE_EE_MAC_OFFSET_5717;
411880969639SSepherosa Ziehau 		if (f > 1)
411980969639SSepherosa Ziehau 			mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
412080969639SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5906) {
41216c8d8eccSSepherosa Ziehau 		mac_offset = BGE_EE_MAC_OFFSET_5906;
412280969639SSepherosa Ziehau 	}
41236c8d8eccSSepherosa Ziehau 
41246c8d8eccSSepherosa Ziehau 	return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
41256c8d8eccSSepherosa Ziehau }
41266c8d8eccSSepherosa Ziehau 
41276c8d8eccSSepherosa Ziehau static int
41286c8d8eccSSepherosa Ziehau bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
41296c8d8eccSSepherosa Ziehau {
41306c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
41316c8d8eccSSepherosa Ziehau 		return 1;
41326c8d8eccSSepherosa Ziehau 
41336c8d8eccSSepherosa Ziehau 	return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
41346c8d8eccSSepherosa Ziehau 			       ETHER_ADDR_LEN);
41356c8d8eccSSepherosa Ziehau }
41366c8d8eccSSepherosa Ziehau 
41376c8d8eccSSepherosa Ziehau static int
41386c8d8eccSSepherosa Ziehau bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
41396c8d8eccSSepherosa Ziehau {
41406c8d8eccSSepherosa Ziehau 	static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
41416c8d8eccSSepherosa Ziehau 		/* NOTE: Order is critical */
41426c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_mem,
41436c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_nvram,
41446c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_eeprom,
41456c8d8eccSSepherosa Ziehau 		NULL
41466c8d8eccSSepherosa Ziehau 	};
41476c8d8eccSSepherosa Ziehau 	const bnx_eaddr_fcn_t *func;
41486c8d8eccSSepherosa Ziehau 
41496c8d8eccSSepherosa Ziehau 	for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
41506c8d8eccSSepherosa Ziehau 		if ((*func)(sc, eaddr) == 0)
41516c8d8eccSSepherosa Ziehau 			break;
41526c8d8eccSSepherosa Ziehau 	}
41536c8d8eccSSepherosa Ziehau 	return (*func == NULL ? ENXIO : 0);
41546c8d8eccSSepherosa Ziehau }
41556c8d8eccSSepherosa Ziehau 
41566c8d8eccSSepherosa Ziehau /*
41576c8d8eccSSepherosa Ziehau  * NOTE: 'm' is not freed upon failure
41586c8d8eccSSepherosa Ziehau  */
41596c8d8eccSSepherosa Ziehau struct mbuf *
41606c8d8eccSSepherosa Ziehau bnx_defrag_shortdma(struct mbuf *m)
41616c8d8eccSSepherosa Ziehau {
41626c8d8eccSSepherosa Ziehau 	struct mbuf *n;
41636c8d8eccSSepherosa Ziehau 	int found;
41646c8d8eccSSepherosa Ziehau 
41656c8d8eccSSepherosa Ziehau 	/*
41666c8d8eccSSepherosa Ziehau 	 * If device receive two back-to-back send BDs with less than
41676c8d8eccSSepherosa Ziehau 	 * or equal to 8 total bytes then the device may hang.  The two
41686c8d8eccSSepherosa Ziehau 	 * back-to-back send BDs must in the same frame for this failure
41696c8d8eccSSepherosa Ziehau 	 * to occur.  Scan mbuf chains and see whether two back-to-back
41706c8d8eccSSepherosa Ziehau 	 * send BDs are there.  If this is the case, allocate new mbuf
41716c8d8eccSSepherosa Ziehau 	 * and copy the frame to workaround the silicon bug.
41726c8d8eccSSepherosa Ziehau 	 */
41736c8d8eccSSepherosa Ziehau 	for (n = m, found = 0; n != NULL; n = n->m_next) {
41746c8d8eccSSepherosa Ziehau 		if (n->m_len < 8) {
41756c8d8eccSSepherosa Ziehau 			found++;
41766c8d8eccSSepherosa Ziehau 			if (found > 1)
41776c8d8eccSSepherosa Ziehau 				break;
41786c8d8eccSSepherosa Ziehau 			continue;
41796c8d8eccSSepherosa Ziehau 		}
41806c8d8eccSSepherosa Ziehau 		found = 0;
41816c8d8eccSSepherosa Ziehau 	}
41826c8d8eccSSepherosa Ziehau 
41836c8d8eccSSepherosa Ziehau 	if (found > 1)
41846c8d8eccSSepherosa Ziehau 		n = m_defrag(m, MB_DONTWAIT);
41856c8d8eccSSepherosa Ziehau 	else
41866c8d8eccSSepherosa Ziehau 		n = m;
41876c8d8eccSSepherosa Ziehau 	return n;
41886c8d8eccSSepherosa Ziehau }
41896c8d8eccSSepherosa Ziehau 
41906c8d8eccSSepherosa Ziehau static void
41916c8d8eccSSepherosa Ziehau bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
41926c8d8eccSSepherosa Ziehau {
41936c8d8eccSSepherosa Ziehau 	int i;
41946c8d8eccSSepherosa Ziehau 
41956c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, reg, bit);
41966c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
41976c8d8eccSSepherosa Ziehau 		if ((CSR_READ_4(sc, reg) & bit) == 0)
41986c8d8eccSSepherosa Ziehau 			return;
41996c8d8eccSSepherosa Ziehau 		DELAY(100);
42006c8d8eccSSepherosa Ziehau 	}
42016c8d8eccSSepherosa Ziehau }
42026c8d8eccSSepherosa Ziehau 
42036c8d8eccSSepherosa Ziehau static void
42046c8d8eccSSepherosa Ziehau bnx_link_poll(struct bnx_softc *sc)
42056c8d8eccSSepherosa Ziehau {
42066c8d8eccSSepherosa Ziehau 	uint32_t status;
42076c8d8eccSSepherosa Ziehau 
42086c8d8eccSSepherosa Ziehau 	status = CSR_READ_4(sc, BGE_MAC_STS);
42096c8d8eccSSepherosa Ziehau 	if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
42106c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt = 0;
42116c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd(sc, status);
42126c8d8eccSSepherosa Ziehau 	}
42136c8d8eccSSepherosa Ziehau }
42146c8d8eccSSepherosa Ziehau 
42156c8d8eccSSepherosa Ziehau static void
42166c8d8eccSSepherosa Ziehau bnx_enable_msi(struct bnx_softc *sc)
42176c8d8eccSSepherosa Ziehau {
42186c8d8eccSSepherosa Ziehau 	uint32_t msi_mode;
42196c8d8eccSSepherosa Ziehau 
42206c8d8eccSSepherosa Ziehau 	msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
42216c8d8eccSSepherosa Ziehau 	msi_mode |= BGE_MSIMODE_ENABLE;
42226c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
42236c8d8eccSSepherosa Ziehau 		/*
42246c8d8eccSSepherosa Ziehau 		 * NOTE:
42256c8d8eccSSepherosa Ziehau 		 * 5718-PG105-R says that "one shot" mode
42266c8d8eccSSepherosa Ziehau 		 * does not work if MSI is used, however,
42276c8d8eccSSepherosa Ziehau 		 * it obviously works.
42286c8d8eccSSepherosa Ziehau 		 */
42296c8d8eccSSepherosa Ziehau 		msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
42306c8d8eccSSepherosa Ziehau 	}
42316c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
42326c8d8eccSSepherosa Ziehau }
42336c8d8eccSSepherosa Ziehau 
42346c8d8eccSSepherosa Ziehau static uint32_t
42356c8d8eccSSepherosa Ziehau bnx_dma_swap_options(struct bnx_softc *sc)
42366c8d8eccSSepherosa Ziehau {
42376c8d8eccSSepherosa Ziehau 	uint32_t dma_options;
42386c8d8eccSSepherosa Ziehau 
42396c8d8eccSSepherosa Ziehau 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
42406c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
42416c8d8eccSSepherosa Ziehau #if BYTE_ORDER == BIG_ENDIAN
42426c8d8eccSSepherosa Ziehau 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
42436c8d8eccSSepherosa Ziehau #endif
42446c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
42456c8d8eccSSepherosa Ziehau 		dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
42466c8d8eccSSepherosa Ziehau 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
42476c8d8eccSSepherosa Ziehau 		    BGE_MODECTL_HTX2B_ENABLE;
42486c8d8eccSSepherosa Ziehau 	}
42496c8d8eccSSepherosa Ziehau 	return dma_options;
42506c8d8eccSSepherosa Ziehau }
425166deb1c1SSepherosa Ziehau 
425266deb1c1SSepherosa Ziehau static int
425366deb1c1SSepherosa Ziehau bnx_setup_tso(struct bnx_softc *sc, struct mbuf **mp,
425466deb1c1SSepherosa Ziehau     uint16_t *mss0, uint16_t *flags0)
425566deb1c1SSepherosa Ziehau {
425666deb1c1SSepherosa Ziehau 	struct mbuf *m;
425766deb1c1SSepherosa Ziehau 	struct ip *ip;
425866deb1c1SSepherosa Ziehau 	struct tcphdr *th;
425966deb1c1SSepherosa Ziehau 	int thoff, iphlen, hoff, hlen;
426066deb1c1SSepherosa Ziehau 	uint16_t flags, mss;
426166deb1c1SSepherosa Ziehau 
4262f7a2269aSSepherosa Ziehau 	m = *mp;
4263f7a2269aSSepherosa Ziehau 	KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4264f7a2269aSSepherosa Ziehau 
4265f7a2269aSSepherosa Ziehau 	hoff = m->m_pkthdr.csum_lhlen;
4266f7a2269aSSepherosa Ziehau 	iphlen = m->m_pkthdr.csum_iphlen;
4267f7a2269aSSepherosa Ziehau 	thoff = m->m_pkthdr.csum_thlen;
4268f7a2269aSSepherosa Ziehau 
4269f7a2269aSSepherosa Ziehau 	KASSERT(hoff > 0, ("invalid ether header len"));
4270f7a2269aSSepherosa Ziehau 	KASSERT(iphlen > 0, ("invalid ip header len"));
4271f7a2269aSSepherosa Ziehau 	KASSERT(thoff > 0, ("invalid tcp header len"));
4272f7a2269aSSepherosa Ziehau 
4273f7a2269aSSepherosa Ziehau 	if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4274f7a2269aSSepherosa Ziehau 		m = m_pullup(m, hoff + iphlen + thoff);
4275f7a2269aSSepherosa Ziehau 		if (m == NULL) {
4276f7a2269aSSepherosa Ziehau 			*mp = NULL;
4277f7a2269aSSepherosa Ziehau 			return ENOBUFS;
4278f7a2269aSSepherosa Ziehau 		}
4279f7a2269aSSepherosa Ziehau 		*mp = m;
4280f7a2269aSSepherosa Ziehau 	}
4281f7a2269aSSepherosa Ziehau 	ip = mtodoff(m, struct ip *, hoff);
4282f7a2269aSSepherosa Ziehau 	th = mtodoff(m, struct tcphdr *, hoff + iphlen);
4283f7a2269aSSepherosa Ziehau 
4284f0336d39SSepherosa Ziehau 	mss = m->m_pkthdr.tso_segsz;
428566deb1c1SSepherosa Ziehau 	flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
428666deb1c1SSepherosa Ziehau 
428766deb1c1SSepherosa Ziehau 	ip->ip_len = htons(mss + iphlen + thoff);
428866deb1c1SSepherosa Ziehau 	th->th_sum = 0;
428966deb1c1SSepherosa Ziehau 
429066deb1c1SSepherosa Ziehau 	hlen = (iphlen + thoff) >> 2;
429166deb1c1SSepherosa Ziehau 	mss |= ((hlen & 0x3) << 14);
429266deb1c1SSepherosa Ziehau 	flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);
429366deb1c1SSepherosa Ziehau 
429466deb1c1SSepherosa Ziehau 	*mss0 = mss;
429566deb1c1SSepherosa Ziehau 	*flags0 = flags;
429666deb1c1SSepherosa Ziehau 
429766deb1c1SSepherosa Ziehau 	return 0;
429866deb1c1SSepherosa Ziehau }
4299