xref: /dflybsd-src/sys/dev/netif/bnx/if_bnx.c (revision 7dbaa83315a858292dc0b796696c25427ee57802)
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"
3739a8d43aSSepherosa Ziehau #include "opt_ifpoll.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>
6239a8d43aSSepherosa Ziehau #include <net/if_poll.h>
636c8d8eccSSepherosa Ziehau #include <net/if_types.h>
646c8d8eccSSepherosa Ziehau #include <net/ifq_var.h>
656c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_var.h>
666c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_ether.h>
676c8d8eccSSepherosa Ziehau 
686c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/mii.h>
696c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/miivar.h>
706c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/brgphyreg.h>
716c8d8eccSSepherosa Ziehau 
726c8d8eccSSepherosa Ziehau #include <bus/pci/pcidevs.h>
736c8d8eccSSepherosa Ziehau #include <bus/pci/pcireg.h>
746c8d8eccSSepherosa Ziehau #include <bus/pci/pcivar.h>
756c8d8eccSSepherosa Ziehau 
766c8d8eccSSepherosa Ziehau #include <dev/netif/bge/if_bgereg.h>
776c8d8eccSSepherosa Ziehau #include <dev/netif/bnx/if_bnxvar.h>
786c8d8eccSSepherosa Ziehau 
796c8d8eccSSepherosa Ziehau /* "device miibus" required.  See GENERIC if you get errors here. */
806c8d8eccSSepherosa Ziehau #include "miibus_if.h"
816c8d8eccSSepherosa Ziehau 
823b18363fSSepherosa Ziehau #define BNX_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
836c8d8eccSSepherosa Ziehau 
84df9ccc98SSepherosa Ziehau #define BNX_INTR_CKINTVL	((10 * hz) / 1000)	/* 10ms */
85df9ccc98SSepherosa Ziehau 
866c8d8eccSSepherosa Ziehau static const struct bnx_type {
876c8d8eccSSepherosa Ziehau 	uint16_t		bnx_vid;
886c8d8eccSSepherosa Ziehau 	uint16_t		bnx_did;
896c8d8eccSSepherosa Ziehau 	char			*bnx_name;
906c8d8eccSSepherosa Ziehau } bnx_devs[] = {
916c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717,
926c8d8eccSSepherosa Ziehau 		"Broadcom BCM5717 Gigabit Ethernet" },
93d79f5d8fSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717C,
94d79f5d8fSSepherosa Ziehau 		"Broadcom BCM5717C Gigabit Ethernet" },
956c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5718,
966c8d8eccSSepherosa Ziehau 		"Broadcom BCM5718 Gigabit Ethernet" },
976c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5719,
986c8d8eccSSepherosa Ziehau 		"Broadcom BCM5719 Gigabit Ethernet" },
996c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5720_ALT,
1006c8d8eccSSepherosa Ziehau 		"Broadcom BCM5720 Gigabit Ethernet" },
1016c8d8eccSSepherosa Ziehau 
102b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5725,
103b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5725 Gigabit Ethernet" },
104b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5727,
105b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5727 Gigabit Ethernet" },
106b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5762,
107b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5762 Gigabit Ethernet" },
108b96cbbb6SSepherosa Ziehau 
1096c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
1106c8d8eccSSepherosa Ziehau 		"Broadcom BCM57761 Gigabit Ethernet" },
11132ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
11232ff3c80SSepherosa Ziehau 		"Broadcom BCM57762 Gigabit Ethernet" },
1136c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
1146c8d8eccSSepherosa Ziehau 		"Broadcom BCM57765 Gigabit Ethernet" },
11532ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
11632ff3c80SSepherosa Ziehau 		"Broadcom BCM57766 Gigabit Ethernet" },
11732ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
11832ff3c80SSepherosa Ziehau 		"Broadcom BCM57781 Gigabit Ethernet" },
11932ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
12032ff3c80SSepherosa Ziehau 		"Broadcom BCM57782 Gigabit Ethernet" },
1216c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
1226c8d8eccSSepherosa Ziehau 		"Broadcom BCM57785 Gigabit Ethernet" },
12332ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
12432ff3c80SSepherosa Ziehau 		"Broadcom BCM57786 Gigabit Ethernet" },
12532ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
12632ff3c80SSepherosa Ziehau 		"Broadcom BCM57791 Fast Ethernet" },
1276c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
1286c8d8eccSSepherosa Ziehau 		"Broadcom BCM57795 Fast Ethernet" },
1296c8d8eccSSepherosa Ziehau 
1306c8d8eccSSepherosa Ziehau 	{ 0, 0, NULL }
1316c8d8eccSSepherosa Ziehau };
1326c8d8eccSSepherosa Ziehau 
1336c8d8eccSSepherosa Ziehau #define BNX_IS_JUMBO_CAPABLE(sc)	((sc)->bnx_flags & BNX_FLAG_JUMBO)
1346c8d8eccSSepherosa Ziehau #define BNX_IS_5717_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
135f368d0d9SSepherosa Ziehau #define BNX_IS_57765_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
136f368d0d9SSepherosa Ziehau #define BNX_IS_57765_FAMILY(sc)	 \
137f368d0d9SSepherosa Ziehau 	((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
1386c8d8eccSSepherosa Ziehau 
1396c8d8eccSSepherosa Ziehau typedef int	(*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
1406c8d8eccSSepherosa Ziehau 
1416c8d8eccSSepherosa Ziehau static int	bnx_probe(device_t);
1426c8d8eccSSepherosa Ziehau static int	bnx_attach(device_t);
1436c8d8eccSSepherosa Ziehau static int	bnx_detach(device_t);
1446c8d8eccSSepherosa Ziehau static void	bnx_shutdown(device_t);
1456c8d8eccSSepherosa Ziehau static int	bnx_suspend(device_t);
1466c8d8eccSSepherosa Ziehau static int	bnx_resume(device_t);
1476c8d8eccSSepherosa Ziehau static int	bnx_miibus_readreg(device_t, int, int);
1486c8d8eccSSepherosa Ziehau static int	bnx_miibus_writereg(device_t, int, int, int);
1496c8d8eccSSepherosa Ziehau static void	bnx_miibus_statchg(device_t);
1506c8d8eccSSepherosa Ziehau 
15139a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
15239a8d43aSSepherosa Ziehau static void	bnx_npoll(struct ifnet *, struct ifpoll_info *);
1534fa38985SSepherosa Ziehau static void	bnx_npoll_rx(struct ifnet *, void *, int);
1544fa38985SSepherosa Ziehau static void	bnx_npoll_tx(struct ifnet *, void *, int);
1554fa38985SSepherosa Ziehau static void	bnx_npoll_status(struct ifnet *);
1566c8d8eccSSepherosa Ziehau #endif
1576c8d8eccSSepherosa Ziehau static void	bnx_intr_legacy(void *);
1586c8d8eccSSepherosa Ziehau static void	bnx_msi(void *);
1596c8d8eccSSepherosa Ziehau static void	bnx_msi_oneshot(void *);
1606c8d8eccSSepherosa Ziehau static void	bnx_intr(struct bnx_softc *);
1616c8d8eccSSepherosa Ziehau static void	bnx_enable_intr(struct bnx_softc *);
1626c8d8eccSSepherosa Ziehau static void	bnx_disable_intr(struct bnx_softc *);
16333a04907SSepherosa Ziehau static void	bnx_txeof(struct bnx_tx_ring *, uint16_t);
164beedf5beSSepherosa Ziehau static void	bnx_rxeof(struct bnx_rx_ret_ring *, uint16_t, int);
1650c7da01dSSepherosa Ziehau static int	bnx_alloc_intr(struct bnx_softc *);
1660c7da01dSSepherosa Ziehau static int	bnx_setup_intr(struct bnx_softc *);
1670c7da01dSSepherosa Ziehau static void	bnx_free_intr(struct bnx_softc *);
168f33ac8a4SSepherosa Ziehau static void	bnx_teardown_intr(struct bnx_softc *, int);
169f33ac8a4SSepherosa Ziehau static void	bnx_check_intr(void *);
1706c8d8eccSSepherosa Ziehau 
171f0a26983SSepherosa Ziehau static void	bnx_start(struct ifnet *, struct ifaltq_subque *);
1726c8d8eccSSepherosa Ziehau static int	bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
1736c8d8eccSSepherosa Ziehau static void	bnx_init(void *);
1746c8d8eccSSepherosa Ziehau static void	bnx_stop(struct bnx_softc *);
1753397dea6SSepherosa Ziehau static void	bnx_watchdog(struct ifaltq_subque *);
1766c8d8eccSSepherosa Ziehau static int	bnx_ifmedia_upd(struct ifnet *);
1776c8d8eccSSepherosa Ziehau static void	bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
1786c8d8eccSSepherosa Ziehau static void	bnx_tick(void *);
179329f9016SSepherosa Ziehau static void	bnx_serialize(struct ifnet *, enum ifnet_serialize);
180329f9016SSepherosa Ziehau static void	bnx_deserialize(struct ifnet *, enum ifnet_serialize);
181329f9016SSepherosa Ziehau static int	bnx_tryserialize(struct ifnet *, enum ifnet_serialize);
182329f9016SSepherosa Ziehau #ifdef INVARIANTS
183329f9016SSepherosa Ziehau static void	bnx_serialize_assert(struct ifnet *, enum ifnet_serialize,
184329f9016SSepherosa Ziehau 		    boolean_t);
185329f9016SSepherosa Ziehau #endif
1866c8d8eccSSepherosa Ziehau 
1876c8d8eccSSepherosa Ziehau static int	bnx_alloc_jumbo_mem(struct bnx_softc *);
1886c8d8eccSSepherosa Ziehau static void	bnx_free_jumbo_mem(struct bnx_softc *);
1896c8d8eccSSepherosa Ziehau static struct bnx_jslot
1906c8d8eccSSepherosa Ziehau 		*bnx_jalloc(struct bnx_softc *);
1916c8d8eccSSepherosa Ziehau static void	bnx_jfree(void *);
1926c8d8eccSSepherosa Ziehau static void	bnx_jref(void *);
193beedf5beSSepherosa Ziehau static int	bnx_newbuf_std(struct bnx_rx_ret_ring *, int, int);
1946c8d8eccSSepherosa Ziehau static int	bnx_newbuf_jumbo(struct bnx_softc *, int, int);
195beedf5beSSepherosa Ziehau static void	bnx_setup_rxdesc_std(struct bnx_rx_std_ring *, int);
1966c8d8eccSSepherosa Ziehau static void	bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
197beedf5beSSepherosa Ziehau static int	bnx_init_rx_ring_std(struct bnx_rx_std_ring *);
198beedf5beSSepherosa Ziehau static void	bnx_free_rx_ring_std(struct bnx_rx_std_ring *);
1996c8d8eccSSepherosa Ziehau static int	bnx_init_rx_ring_jumbo(struct bnx_softc *);
2006c8d8eccSSepherosa Ziehau static void	bnx_free_rx_ring_jumbo(struct bnx_softc *);
20133a04907SSepherosa Ziehau static void	bnx_free_tx_ring(struct bnx_tx_ring *);
20233a04907SSepherosa Ziehau static int	bnx_init_tx_ring(struct bnx_tx_ring *);
20333a04907SSepherosa Ziehau static int	bnx_create_tx_ring(struct bnx_tx_ring *);
20433a04907SSepherosa Ziehau static void	bnx_destroy_tx_ring(struct bnx_tx_ring *);
205beedf5beSSepherosa Ziehau static int	bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *);
206beedf5beSSepherosa Ziehau static void	bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *);
207beedf5beSSepherosa Ziehau static int	bnx_dma_alloc(device_t);
2086c8d8eccSSepherosa Ziehau static void	bnx_dma_free(struct bnx_softc *);
2096c8d8eccSSepherosa Ziehau static int	bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
2106c8d8eccSSepherosa Ziehau 		    bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
2116c8d8eccSSepherosa Ziehau static void	bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
2126c8d8eccSSepherosa Ziehau static struct mbuf *
2136c8d8eccSSepherosa Ziehau 		bnx_defrag_shortdma(struct mbuf *);
21433a04907SSepherosa Ziehau static int	bnx_encap(struct bnx_tx_ring *, struct mbuf **,
215c9b7f592SSepherosa Ziehau 		    uint32_t *, int *);
21633a04907SSepherosa Ziehau static int	bnx_setup_tso(struct bnx_tx_ring *, struct mbuf **,
21766deb1c1SSepherosa Ziehau 		    uint16_t *, uint16_t *);
218329f9016SSepherosa Ziehau static void	bnx_setup_serialize(struct bnx_softc *);
219*7dbaa833SSepherosa Ziehau static void	bnx_set_tick_cpuid(struct bnx_softc *, boolean_t);
2206c8d8eccSSepherosa Ziehau 
2216c8d8eccSSepherosa Ziehau static void	bnx_reset(struct bnx_softc *);
2226c8d8eccSSepherosa Ziehau static int	bnx_chipinit(struct bnx_softc *);
2236c8d8eccSSepherosa Ziehau static int	bnx_blockinit(struct bnx_softc *);
2246c8d8eccSSepherosa Ziehau static void	bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
2256c8d8eccSSepherosa Ziehau static void	bnx_enable_msi(struct bnx_softc *sc);
2266c8d8eccSSepherosa Ziehau static void	bnx_setmulti(struct bnx_softc *);
2276c8d8eccSSepherosa Ziehau static void	bnx_setpromisc(struct bnx_softc *);
2286c8d8eccSSepherosa Ziehau static void	bnx_stats_update_regs(struct bnx_softc *);
2296c8d8eccSSepherosa Ziehau static uint32_t	bnx_dma_swap_options(struct bnx_softc *);
2306c8d8eccSSepherosa Ziehau 
2316c8d8eccSSepherosa Ziehau static uint32_t	bnx_readmem_ind(struct bnx_softc *, uint32_t);
2326c8d8eccSSepherosa Ziehau static void	bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
2336c8d8eccSSepherosa Ziehau #ifdef notdef
2346c8d8eccSSepherosa Ziehau static uint32_t	bnx_readreg_ind(struct bnx_softc *, uint32_t);
2356c8d8eccSSepherosa Ziehau #endif
2366c8d8eccSSepherosa Ziehau static void	bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
2376c8d8eccSSepherosa Ziehau static void	bnx_writembx(struct bnx_softc *, int, int);
2386c8d8eccSSepherosa Ziehau static int	bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
2396c8d8eccSSepherosa Ziehau static uint8_t	bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
2406c8d8eccSSepherosa Ziehau static int	bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
2416c8d8eccSSepherosa Ziehau 
2426c8d8eccSSepherosa Ziehau static void	bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
2436c8d8eccSSepherosa Ziehau static void	bnx_copper_link_upd(struct bnx_softc *, uint32_t);
2446c8d8eccSSepherosa Ziehau static void	bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
2456c8d8eccSSepherosa Ziehau static void	bnx_link_poll(struct bnx_softc *);
2466c8d8eccSSepherosa Ziehau 
2476c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
2486c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
2496c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
2506c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
2516c8d8eccSSepherosa Ziehau 
2526c8d8eccSSepherosa Ziehau static void	bnx_coal_change(struct bnx_softc *);
253aad4de2bSSepherosa Ziehau static int	bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS);
254472c99c8SSepherosa Ziehau static int	bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS);
2556c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
2566c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
2576c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
2586c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
2596c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
2606c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
2616c8d8eccSSepherosa Ziehau static int	bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
2626c8d8eccSSepherosa Ziehau 		    int, int, uint32_t);
2634fa38985SSepherosa Ziehau #ifdef IFPOLL_ENABLE
2644fa38985SSepherosa Ziehau static int	bnx_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS);
2654fa38985SSepherosa Ziehau static int	bnx_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS);
2664fa38985SSepherosa Ziehau static int	bnx_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS);
2674fa38985SSepherosa Ziehau #endif
2686c8d8eccSSepherosa Ziehau 
2696c8d8eccSSepherosa Ziehau static int	bnx_msi_enable = 1;
2706c8d8eccSSepherosa Ziehau TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
2716c8d8eccSSepherosa Ziehau 
2726c8d8eccSSepherosa Ziehau static device_method_t bnx_methods[] = {
2736c8d8eccSSepherosa Ziehau 	/* Device interface */
2746c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_probe,		bnx_probe),
2756c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_attach,	bnx_attach),
2766c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_detach,	bnx_detach),
2776c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_shutdown,	bnx_shutdown),
2786c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_suspend,	bnx_suspend),
2796c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_resume,	bnx_resume),
2806c8d8eccSSepherosa Ziehau 
2816c8d8eccSSepherosa Ziehau 	/* bus interface */
2826c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
2836c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
2846c8d8eccSSepherosa Ziehau 
2856c8d8eccSSepherosa Ziehau 	/* MII interface */
2866c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_readreg,	bnx_miibus_readreg),
2876c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_writereg,	bnx_miibus_writereg),
2886c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_statchg,	bnx_miibus_statchg),
2896c8d8eccSSepherosa Ziehau 
290d3c9c58eSSascha Wildner 	DEVMETHOD_END
2916c8d8eccSSepherosa Ziehau };
2926c8d8eccSSepherosa Ziehau 
2936c8d8eccSSepherosa Ziehau static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
2946c8d8eccSSepherosa Ziehau static devclass_t bnx_devclass;
2956c8d8eccSSepherosa Ziehau 
2966c8d8eccSSepherosa Ziehau DECLARE_DUMMY_MODULE(if_bnx);
2976c8d8eccSSepherosa Ziehau DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
2986c8d8eccSSepherosa Ziehau DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
2996c8d8eccSSepherosa Ziehau 
3006c8d8eccSSepherosa Ziehau static uint32_t
3016c8d8eccSSepherosa Ziehau bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
3026c8d8eccSSepherosa Ziehau {
3036c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
3046c8d8eccSSepherosa Ziehau 	uint32_t val;
3056c8d8eccSSepherosa Ziehau 
3066c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
3076c8d8eccSSepherosa Ziehau 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
3086c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
3096c8d8eccSSepherosa Ziehau 	return (val);
3106c8d8eccSSepherosa Ziehau }
3116c8d8eccSSepherosa Ziehau 
3126c8d8eccSSepherosa Ziehau static void
3136c8d8eccSSepherosa Ziehau bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
3146c8d8eccSSepherosa Ziehau {
3156c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
3166c8d8eccSSepherosa Ziehau 
3176c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
3186c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
3196c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
3206c8d8eccSSepherosa Ziehau }
3216c8d8eccSSepherosa Ziehau 
3226c8d8eccSSepherosa Ziehau static void
3236c8d8eccSSepherosa Ziehau bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
3246c8d8eccSSepherosa Ziehau {
3256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
3266c8d8eccSSepherosa Ziehau }
3276c8d8eccSSepherosa Ziehau 
3286c8d8eccSSepherosa Ziehau static void
3296c8d8eccSSepherosa Ziehau bnx_writembx(struct bnx_softc *sc, int off, int val)
3306c8d8eccSSepherosa Ziehau {
3316c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
3326c8d8eccSSepherosa Ziehau }
3336c8d8eccSSepherosa Ziehau 
3346c8d8eccSSepherosa Ziehau /*
3356c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from NVRAM.
3366c8d8eccSSepherosa Ziehau  */
3376c8d8eccSSepherosa Ziehau static int
3386c8d8eccSSepherosa Ziehau bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
3396c8d8eccSSepherosa Ziehau {
3406c8d8eccSSepherosa Ziehau 	return (1);
3416c8d8eccSSepherosa Ziehau }
3426c8d8eccSSepherosa Ziehau 
3436c8d8eccSSepherosa Ziehau /*
3446c8d8eccSSepherosa Ziehau  * Read a byte of data stored in the EEPROM at address 'addr.' The
3456c8d8eccSSepherosa Ziehau  * BCM570x supports both the traditional bitbang interface and an
3466c8d8eccSSepherosa Ziehau  * auto access interface for reading the EEPROM. We use the auto
3476c8d8eccSSepherosa Ziehau  * access method.
3486c8d8eccSSepherosa Ziehau  */
3496c8d8eccSSepherosa Ziehau static uint8_t
3506c8d8eccSSepherosa Ziehau bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
3516c8d8eccSSepherosa Ziehau {
3526c8d8eccSSepherosa Ziehau 	int i;
3536c8d8eccSSepherosa Ziehau 	uint32_t byte = 0;
3546c8d8eccSSepherosa Ziehau 
3556c8d8eccSSepherosa Ziehau 	/*
3566c8d8eccSSepherosa Ziehau 	 * Enable use of auto EEPROM access so we can avoid
3576c8d8eccSSepherosa Ziehau 	 * having to use the bitbang method.
3586c8d8eccSSepherosa Ziehau 	 */
3596c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
3606c8d8eccSSepherosa Ziehau 
3616c8d8eccSSepherosa Ziehau 	/* Reset the EEPROM, load the clock period. */
3626c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR,
3636c8d8eccSSepherosa Ziehau 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
3646c8d8eccSSepherosa Ziehau 	DELAY(20);
3656c8d8eccSSepherosa Ziehau 
3666c8d8eccSSepherosa Ziehau 	/* Issue the read EEPROM command. */
3676c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
3686c8d8eccSSepherosa Ziehau 
3696c8d8eccSSepherosa Ziehau 	/* Wait for completion */
3706c8d8eccSSepherosa Ziehau 	for(i = 0; i < BNX_TIMEOUT * 10; i++) {
3716c8d8eccSSepherosa Ziehau 		DELAY(10);
3726c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
3736c8d8eccSSepherosa Ziehau 			break;
3746c8d8eccSSepherosa Ziehau 	}
3756c8d8eccSSepherosa Ziehau 
3766c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
3776c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
3786c8d8eccSSepherosa Ziehau 		return(1);
3796c8d8eccSSepherosa Ziehau 	}
3806c8d8eccSSepherosa Ziehau 
3816c8d8eccSSepherosa Ziehau 	/* Get result. */
3826c8d8eccSSepherosa Ziehau 	byte = CSR_READ_4(sc, BGE_EE_DATA);
3836c8d8eccSSepherosa Ziehau 
3846c8d8eccSSepherosa Ziehau         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
3856c8d8eccSSepherosa Ziehau 
3866c8d8eccSSepherosa Ziehau 	return(0);
3876c8d8eccSSepherosa Ziehau }
3886c8d8eccSSepherosa Ziehau 
3896c8d8eccSSepherosa Ziehau /*
3906c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from the EEPROM.
3916c8d8eccSSepherosa Ziehau  */
3926c8d8eccSSepherosa Ziehau static int
3936c8d8eccSSepherosa Ziehau bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
3946c8d8eccSSepherosa Ziehau {
3956c8d8eccSSepherosa Ziehau 	size_t i;
3966c8d8eccSSepherosa Ziehau 	int err;
3976c8d8eccSSepherosa Ziehau 	uint8_t byte;
3986c8d8eccSSepherosa Ziehau 
3996c8d8eccSSepherosa Ziehau 	for (byte = 0, err = 0, i = 0; i < len; i++) {
4006c8d8eccSSepherosa Ziehau 		err = bnx_eeprom_getbyte(sc, off + i, &byte);
4016c8d8eccSSepherosa Ziehau 		if (err)
4026c8d8eccSSepherosa Ziehau 			break;
4036c8d8eccSSepherosa Ziehau 		*(dest + i) = byte;
4046c8d8eccSSepherosa Ziehau 	}
4056c8d8eccSSepherosa Ziehau 
4066c8d8eccSSepherosa Ziehau 	return(err ? 1 : 0);
4076c8d8eccSSepherosa Ziehau }
4086c8d8eccSSepherosa Ziehau 
4096c8d8eccSSepherosa Ziehau static int
4106c8d8eccSSepherosa Ziehau bnx_miibus_readreg(device_t dev, int phy, int reg)
4116c8d8eccSSepherosa Ziehau {
4126c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
4136c8d8eccSSepherosa Ziehau 	uint32_t val;
4146c8d8eccSSepherosa Ziehau 	int i;
4156c8d8eccSSepherosa Ziehau 
4166c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
4176c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
4186c8d8eccSSepherosa Ziehau 
4196c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
4206c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4216c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
4226c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
4236c8d8eccSSepherosa Ziehau 		DELAY(80);
4246c8d8eccSSepherosa Ziehau 	}
4256c8d8eccSSepherosa Ziehau 
4266c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
4276c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
4286c8d8eccSSepherosa Ziehau 
4296c8d8eccSSepherosa Ziehau 	/* Poll for the PHY register access to complete. */
4306c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
4316c8d8eccSSepherosa Ziehau 		DELAY(10);
4326c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_MI_COMM);
4336c8d8eccSSepherosa Ziehau 		if ((val & BGE_MICOMM_BUSY) == 0) {
4346c8d8eccSSepherosa Ziehau 			DELAY(5);
4356c8d8eccSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_MI_COMM);
4366c8d8eccSSepherosa Ziehau 			break;
4376c8d8eccSSepherosa Ziehau 		}
4386c8d8eccSSepherosa Ziehau 	}
4396c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
4406c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY read timed out "
4416c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
4426c8d8eccSSepherosa Ziehau 		val = 0;
4436c8d8eccSSepherosa Ziehau 	}
4446c8d8eccSSepherosa Ziehau 
4456c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
4466c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4476c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
4486c8d8eccSSepherosa Ziehau 		DELAY(80);
4496c8d8eccSSepherosa Ziehau 	}
4506c8d8eccSSepherosa Ziehau 
4516c8d8eccSSepherosa Ziehau 	if (val & BGE_MICOMM_READFAIL)
4526c8d8eccSSepherosa Ziehau 		return 0;
4536c8d8eccSSepherosa Ziehau 
4546c8d8eccSSepherosa Ziehau 	return (val & 0xFFFF);
4556c8d8eccSSepherosa Ziehau }
4566c8d8eccSSepherosa Ziehau 
4576c8d8eccSSepherosa Ziehau static int
4586c8d8eccSSepherosa Ziehau bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
4596c8d8eccSSepherosa Ziehau {
4606c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
4616c8d8eccSSepherosa Ziehau 	int i;
4626c8d8eccSSepherosa Ziehau 
4636c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
4646c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
4656c8d8eccSSepherosa Ziehau 
4666c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
4676c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4686c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
4696c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
4706c8d8eccSSepherosa Ziehau 		DELAY(80);
4716c8d8eccSSepherosa Ziehau 	}
4726c8d8eccSSepherosa Ziehau 
4736c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
4746c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
4756c8d8eccSSepherosa Ziehau 
4766c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
4776c8d8eccSSepherosa Ziehau 		DELAY(10);
4786c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
4796c8d8eccSSepherosa Ziehau 			DELAY(5);
4806c8d8eccSSepherosa Ziehau 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
4816c8d8eccSSepherosa Ziehau 			break;
4826c8d8eccSSepherosa Ziehau 		}
4836c8d8eccSSepherosa Ziehau 	}
4846c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
4856c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY write timed out "
4866c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val %d)\n", phy, reg, val);
4876c8d8eccSSepherosa Ziehau 	}
4886c8d8eccSSepherosa Ziehau 
4896c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
4906c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4916c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
4926c8d8eccSSepherosa Ziehau 		DELAY(80);
4936c8d8eccSSepherosa Ziehau 	}
4946c8d8eccSSepherosa Ziehau 
4956c8d8eccSSepherosa Ziehau 	return 0;
4966c8d8eccSSepherosa Ziehau }
4976c8d8eccSSepherosa Ziehau 
4986c8d8eccSSepherosa Ziehau static void
4996c8d8eccSSepherosa Ziehau bnx_miibus_statchg(device_t dev)
5006c8d8eccSSepherosa Ziehau {
5016c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
5026c8d8eccSSepherosa Ziehau 	struct mii_data *mii;
5036c8d8eccSSepherosa Ziehau 
5046c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
5056c8d8eccSSepherosa Ziehau 	mii = device_get_softc(sc->bnx_miibus);
5066c8d8eccSSepherosa Ziehau 
5076c8d8eccSSepherosa Ziehau 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
5086c8d8eccSSepherosa Ziehau 	    (IFM_ACTIVE | IFM_AVALID)) {
5096c8d8eccSSepherosa Ziehau 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5106c8d8eccSSepherosa Ziehau 		case IFM_10_T:
5116c8d8eccSSepherosa Ziehau 		case IFM_100_TX:
5126c8d8eccSSepherosa Ziehau 			sc->bnx_link = 1;
5136c8d8eccSSepherosa Ziehau 			break;
5146c8d8eccSSepherosa Ziehau 		case IFM_1000_T:
5156c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
5166c8d8eccSSepherosa Ziehau 		case IFM_2500_SX:
5176c8d8eccSSepherosa Ziehau 			sc->bnx_link = 1;
5186c8d8eccSSepherosa Ziehau 			break;
5196c8d8eccSSepherosa Ziehau 		default:
5206c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
5216c8d8eccSSepherosa Ziehau 			break;
5226c8d8eccSSepherosa Ziehau 		}
5236c8d8eccSSepherosa Ziehau 	} else {
5246c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
5256c8d8eccSSepherosa Ziehau 	}
5266c8d8eccSSepherosa Ziehau 	if (sc->bnx_link == 0)
5276c8d8eccSSepherosa Ziehau 		return;
5286c8d8eccSSepherosa Ziehau 
5296c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
5306c8d8eccSSepherosa Ziehau 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
5316c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
5326c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
5336c8d8eccSSepherosa Ziehau 	} else {
5346c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
5356c8d8eccSSepherosa Ziehau 	}
5366c8d8eccSSepherosa Ziehau 
5376c8d8eccSSepherosa Ziehau 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
5386c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
5396c8d8eccSSepherosa Ziehau 	} else {
5406c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
5416c8d8eccSSepherosa Ziehau 	}
5426c8d8eccSSepherosa Ziehau }
5436c8d8eccSSepherosa Ziehau 
5446c8d8eccSSepherosa Ziehau /*
5456c8d8eccSSepherosa Ziehau  * Memory management for jumbo frames.
5466c8d8eccSSepherosa Ziehau  */
5476c8d8eccSSepherosa Ziehau static int
5486c8d8eccSSepherosa Ziehau bnx_alloc_jumbo_mem(struct bnx_softc *sc)
5496c8d8eccSSepherosa Ziehau {
5506c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
5516c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
5526c8d8eccSSepherosa Ziehau 	uint8_t *ptr;
5536c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
5546c8d8eccSSepherosa Ziehau 	int i, error;
5556c8d8eccSSepherosa Ziehau 
5566c8d8eccSSepherosa Ziehau 	/*
5576c8d8eccSSepherosa Ziehau 	 * Create tag for jumbo mbufs.
5586c8d8eccSSepherosa Ziehau 	 * This is really a bit of a kludge. We allocate a special
5596c8d8eccSSepherosa Ziehau 	 * jumbo buffer pool which (thanks to the way our DMA
5606c8d8eccSSepherosa Ziehau 	 * memory allocation works) will consist of contiguous
5616c8d8eccSSepherosa Ziehau 	 * pages. This means that even though a jumbo buffer might
5626c8d8eccSSepherosa Ziehau 	 * be larger than a page size, we don't really need to
5636c8d8eccSSepherosa Ziehau 	 * map it into more than one DMA segment. However, the
5646c8d8eccSSepherosa Ziehau 	 * default mbuf tag will result in multi-segment mappings,
5656c8d8eccSSepherosa Ziehau 	 * so we have to create a special jumbo mbuf tag that
5666c8d8eccSSepherosa Ziehau 	 * lets us get away with mapping the jumbo buffers as
5676c8d8eccSSepherosa Ziehau 	 * a single segment. I think eventually the driver should
5686c8d8eccSSepherosa Ziehau 	 * be changed so that it uses ordinary mbufs and cluster
5696c8d8eccSSepherosa Ziehau 	 * buffers, i.e. jumbo frames can span multiple DMA
5706c8d8eccSSepherosa Ziehau 	 * descriptors. But that's a project for another day.
5716c8d8eccSSepherosa Ziehau 	 */
5726c8d8eccSSepherosa Ziehau 
5736c8d8eccSSepherosa Ziehau 	/*
5746c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo RX ring.
5756c8d8eccSSepherosa Ziehau 	 */
5766c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
5776c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
5786c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
5796c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
5806c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
5816c8d8eccSSepherosa Ziehau 	if (error) {
5826c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo RX ring\n");
5836c8d8eccSSepherosa Ziehau 		return error;
5846c8d8eccSSepherosa Ziehau 	}
5856c8d8eccSSepherosa Ziehau 
5866c8d8eccSSepherosa Ziehau 	/*
5876c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo buffer block.
5886c8d8eccSSepherosa Ziehau 	 */
5896c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BNX_JMEM,
5906c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_tag,
5916c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_map,
5926c8d8eccSSepherosa Ziehau 				    (void **)&sc->bnx_ldata.bnx_jumbo_buf,
5936c8d8eccSSepherosa Ziehau 				    &paddr);
5946c8d8eccSSepherosa Ziehau 	if (error) {
5956c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo buffer\n");
5966c8d8eccSSepherosa Ziehau 		return error;
5976c8d8eccSSepherosa Ziehau 	}
5986c8d8eccSSepherosa Ziehau 
5996c8d8eccSSepherosa Ziehau 	SLIST_INIT(&sc->bnx_jfree_listhead);
6006c8d8eccSSepherosa Ziehau 
6016c8d8eccSSepherosa Ziehau 	/*
6026c8d8eccSSepherosa Ziehau 	 * Now divide it up into 9K pieces and save the addresses
6036c8d8eccSSepherosa Ziehau 	 * in an array. Note that we play an evil trick here by using
6046c8d8eccSSepherosa Ziehau 	 * the first few bytes in the buffer to hold the the address
6056c8d8eccSSepherosa Ziehau 	 * of the softc structure for this interface. This is because
6066c8d8eccSSepherosa Ziehau 	 * bnx_jfree() needs it, but it is called by the mbuf management
6076c8d8eccSSepherosa Ziehau 	 * code which will not pass it to us explicitly.
6086c8d8eccSSepherosa Ziehau 	 */
6096c8d8eccSSepherosa Ziehau 	for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
6106c8d8eccSSepherosa Ziehau 		entry = &sc->bnx_cdata.bnx_jslots[i];
6116c8d8eccSSepherosa Ziehau 		entry->bnx_sc = sc;
6126c8d8eccSSepherosa Ziehau 		entry->bnx_buf = ptr;
6136c8d8eccSSepherosa Ziehau 		entry->bnx_paddr = paddr;
6146c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 0;
6156c8d8eccSSepherosa Ziehau 		entry->bnx_slot = i;
6166c8d8eccSSepherosa Ziehau 		SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
6176c8d8eccSSepherosa Ziehau 
6186c8d8eccSSepherosa Ziehau 		ptr += BNX_JLEN;
6196c8d8eccSSepherosa Ziehau 		paddr += BNX_JLEN;
6206c8d8eccSSepherosa Ziehau 	}
6216c8d8eccSSepherosa Ziehau 	return 0;
6226c8d8eccSSepherosa Ziehau }
6236c8d8eccSSepherosa Ziehau 
6246c8d8eccSSepherosa Ziehau static void
6256c8d8eccSSepherosa Ziehau bnx_free_jumbo_mem(struct bnx_softc *sc)
6266c8d8eccSSepherosa Ziehau {
6276c8d8eccSSepherosa Ziehau 	/* Destroy jumbo RX ring. */
6286c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
6296c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_rx_jumbo_ring_map,
6306c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_rx_jumbo_ring);
6316c8d8eccSSepherosa Ziehau 
6326c8d8eccSSepherosa Ziehau 	/* Destroy jumbo buffer block. */
6336c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
6346c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_jumbo_map,
6356c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_jumbo_buf);
6366c8d8eccSSepherosa Ziehau }
6376c8d8eccSSepherosa Ziehau 
6386c8d8eccSSepherosa Ziehau /*
6396c8d8eccSSepherosa Ziehau  * Allocate a jumbo buffer.
6406c8d8eccSSepherosa Ziehau  */
6416c8d8eccSSepherosa Ziehau static struct bnx_jslot *
6426c8d8eccSSepherosa Ziehau bnx_jalloc(struct bnx_softc *sc)
6436c8d8eccSSepherosa Ziehau {
6446c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
6456c8d8eccSSepherosa Ziehau 
6466c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(&sc->bnx_jslot_serializer);
6476c8d8eccSSepherosa Ziehau 	entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
6486c8d8eccSSepherosa Ziehau 	if (entry) {
6496c8d8eccSSepherosa Ziehau 		SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
6506c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 1;
6516c8d8eccSSepherosa Ziehau 	} else {
6526c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
6536c8d8eccSSepherosa Ziehau 	}
6546c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(&sc->bnx_jslot_serializer);
6556c8d8eccSSepherosa Ziehau 	return(entry);
6566c8d8eccSSepherosa Ziehau }
6576c8d8eccSSepherosa Ziehau 
6586c8d8eccSSepherosa Ziehau /*
6596c8d8eccSSepherosa Ziehau  * Adjust usage count on a jumbo buffer.
6606c8d8eccSSepherosa Ziehau  */
6616c8d8eccSSepherosa Ziehau static void
6626c8d8eccSSepherosa Ziehau bnx_jref(void *arg)
6636c8d8eccSSepherosa Ziehau {
6646c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
6656c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
6666c8d8eccSSepherosa Ziehau 
6676c8d8eccSSepherosa Ziehau 	if (sc == NULL)
6686c8d8eccSSepherosa Ziehau 		panic("bnx_jref: can't find softc pointer!");
6696c8d8eccSSepherosa Ziehau 
6706c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
6716c8d8eccSSepherosa Ziehau 		panic("bnx_jref: asked to reference buffer "
6726c8d8eccSSepherosa Ziehau 		    "that we don't manage!");
6736c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
6746c8d8eccSSepherosa Ziehau 		panic("bnx_jref: buffer already free!");
6756c8d8eccSSepherosa Ziehau 	} else {
6766c8d8eccSSepherosa Ziehau 		atomic_add_int(&entry->bnx_inuse, 1);
6776c8d8eccSSepherosa Ziehau 	}
6786c8d8eccSSepherosa Ziehau }
6796c8d8eccSSepherosa Ziehau 
6806c8d8eccSSepherosa Ziehau /*
6816c8d8eccSSepherosa Ziehau  * Release a jumbo buffer.
6826c8d8eccSSepherosa Ziehau  */
6836c8d8eccSSepherosa Ziehau static void
6846c8d8eccSSepherosa Ziehau bnx_jfree(void *arg)
6856c8d8eccSSepherosa Ziehau {
6866c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
6876c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
6886c8d8eccSSepherosa Ziehau 
6896c8d8eccSSepherosa Ziehau 	if (sc == NULL)
6906c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: can't find softc pointer!");
6916c8d8eccSSepherosa Ziehau 
6926c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
6936c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: asked to free buffer that we don't manage!");
6946c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
6956c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: buffer already free!");
6966c8d8eccSSepherosa Ziehau 	} else {
6976c8d8eccSSepherosa Ziehau 		/*
6986c8d8eccSSepherosa Ziehau 		 * Possible MP race to 0, use the serializer.  The atomic insn
6996c8d8eccSSepherosa Ziehau 		 * is still needed for races against bnx_jref().
7006c8d8eccSSepherosa Ziehau 		 */
7016c8d8eccSSepherosa Ziehau 		lwkt_serialize_enter(&sc->bnx_jslot_serializer);
7026c8d8eccSSepherosa Ziehau 		atomic_subtract_int(&entry->bnx_inuse, 1);
7036c8d8eccSSepherosa Ziehau 		if (entry->bnx_inuse == 0) {
7046c8d8eccSSepherosa Ziehau 			SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
7056c8d8eccSSepherosa Ziehau 					  entry, jslot_link);
7066c8d8eccSSepherosa Ziehau 		}
7076c8d8eccSSepherosa Ziehau 		lwkt_serialize_exit(&sc->bnx_jslot_serializer);
7086c8d8eccSSepherosa Ziehau 	}
7096c8d8eccSSepherosa Ziehau }
7106c8d8eccSSepherosa Ziehau 
7116c8d8eccSSepherosa Ziehau 
7126c8d8eccSSepherosa Ziehau /*
7136c8d8eccSSepherosa Ziehau  * Intialize a standard receive ring descriptor.
7146c8d8eccSSepherosa Ziehau  */
7156c8d8eccSSepherosa Ziehau static int
716beedf5beSSepherosa Ziehau bnx_newbuf_std(struct bnx_rx_ret_ring *ret, int i, int init)
7176c8d8eccSSepherosa Ziehau {
7186c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
7196c8d8eccSSepherosa Ziehau 	bus_dma_segment_t seg;
7206c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
7216c8d8eccSSepherosa Ziehau 	int error, nsegs;
722beedf5beSSepherosa Ziehau 	struct bnx_rx_buf *rb;
7236c8d8eccSSepherosa Ziehau 
7246c8d8eccSSepherosa Ziehau 	m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
7256c8d8eccSSepherosa Ziehau 	if (m_new == NULL)
7266c8d8eccSSepherosa Ziehau 		return ENOBUFS;
7276c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
7286c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
7296c8d8eccSSepherosa Ziehau 
730beedf5beSSepherosa Ziehau 	error = bus_dmamap_load_mbuf_segment(ret->bnx_rx_mtag,
731beedf5beSSepherosa Ziehau 	    ret->bnx_rx_tmpmap, m_new, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
7326c8d8eccSSepherosa Ziehau 	if (error) {
7336c8d8eccSSepherosa Ziehau 		m_freem(m_new);
7346c8d8eccSSepherosa Ziehau 		return error;
7356c8d8eccSSepherosa Ziehau 	}
7366c8d8eccSSepherosa Ziehau 
737beedf5beSSepherosa Ziehau 	rb = &ret->bnx_std->bnx_rx_std_buf[i];
738beedf5beSSepherosa Ziehau 
7396c8d8eccSSepherosa Ziehau 	if (!init) {
740beedf5beSSepherosa Ziehau 		bus_dmamap_sync(ret->bnx_rx_mtag, rb->bnx_rx_dmamap,
7416c8d8eccSSepherosa Ziehau 		    BUS_DMASYNC_POSTREAD);
742beedf5beSSepherosa Ziehau 		bus_dmamap_unload(ret->bnx_rx_mtag, rb->bnx_rx_dmamap);
7436c8d8eccSSepherosa Ziehau 	}
7446c8d8eccSSepherosa Ziehau 
745beedf5beSSepherosa Ziehau 	map = ret->bnx_rx_tmpmap;
746beedf5beSSepherosa Ziehau 	ret->bnx_rx_tmpmap = rb->bnx_rx_dmamap;
747beedf5beSSepherosa Ziehau 	rb->bnx_rx_dmamap = map;
7486c8d8eccSSepherosa Ziehau 
749beedf5beSSepherosa Ziehau 	rb->bnx_rx_mbuf = m_new;
750beedf5beSSepherosa Ziehau 	rb->bnx_rx_paddr = seg.ds_addr;
7516c8d8eccSSepherosa Ziehau 
752beedf5beSSepherosa Ziehau 	bnx_setup_rxdesc_std(ret->bnx_std, i);
7536c8d8eccSSepherosa Ziehau 	return 0;
7546c8d8eccSSepherosa Ziehau }
7556c8d8eccSSepherosa Ziehau 
7566c8d8eccSSepherosa Ziehau static void
757beedf5beSSepherosa Ziehau bnx_setup_rxdesc_std(struct bnx_rx_std_ring *std, int i)
7586c8d8eccSSepherosa Ziehau {
759beedf5beSSepherosa Ziehau 	const struct bnx_rx_buf *rb;
7606c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
7616c8d8eccSSepherosa Ziehau 
762beedf5beSSepherosa Ziehau 	rb = &std->bnx_rx_std_buf[i];
763beedf5beSSepherosa Ziehau 	r = &std->bnx_rx_std_ring[i];
7646c8d8eccSSepherosa Ziehau 
765beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rb->bnx_rx_paddr);
766beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rb->bnx_rx_paddr);
767beedf5beSSepherosa Ziehau 	r->bge_len = rb->bnx_rx_mbuf->m_len;
7686c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
7696c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END;
7706c8d8eccSSepherosa Ziehau }
7716c8d8eccSSepherosa Ziehau 
7726c8d8eccSSepherosa Ziehau /*
7736c8d8eccSSepherosa Ziehau  * Initialize a jumbo receive ring descriptor. This allocates
7746c8d8eccSSepherosa Ziehau  * a jumbo buffer from the pool managed internally by the driver.
7756c8d8eccSSepherosa Ziehau  */
7766c8d8eccSSepherosa Ziehau static int
7776c8d8eccSSepherosa Ziehau bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
7786c8d8eccSSepherosa Ziehau {
7796c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
7806c8d8eccSSepherosa Ziehau 	struct bnx_jslot *buf;
7816c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
7826c8d8eccSSepherosa Ziehau 
7836c8d8eccSSepherosa Ziehau 	/* Allocate the mbuf. */
7846c8d8eccSSepherosa Ziehau 	MGETHDR(m_new, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
7856c8d8eccSSepherosa Ziehau 	if (m_new == NULL)
7866c8d8eccSSepherosa Ziehau 		return ENOBUFS;
7876c8d8eccSSepherosa Ziehau 
7886c8d8eccSSepherosa Ziehau 	/* Allocate the jumbo buffer */
7896c8d8eccSSepherosa Ziehau 	buf = bnx_jalloc(sc);
7906c8d8eccSSepherosa Ziehau 	if (buf == NULL) {
7916c8d8eccSSepherosa Ziehau 		m_freem(m_new);
7926c8d8eccSSepherosa Ziehau 		return ENOBUFS;
7936c8d8eccSSepherosa Ziehau 	}
7946c8d8eccSSepherosa Ziehau 
7956c8d8eccSSepherosa Ziehau 	/* Attach the buffer to the mbuf. */
7966c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_arg = buf;
7976c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_buf = buf->bnx_buf;
7986c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_free = bnx_jfree;
7996c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_ref = bnx_jref;
8006c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
8016c8d8eccSSepherosa Ziehau 
8026c8d8eccSSepherosa Ziehau 	m_new->m_flags |= M_EXT;
8036c8d8eccSSepherosa Ziehau 
8046c8d8eccSSepherosa Ziehau 	m_new->m_data = m_new->m_ext.ext_buf;
8056c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
8066c8d8eccSSepherosa Ziehau 
8076c8d8eccSSepherosa Ziehau 	paddr = buf->bnx_paddr;
8086c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
8096c8d8eccSSepherosa Ziehau 	paddr += ETHER_ALIGN;
8106c8d8eccSSepherosa Ziehau 
8116c8d8eccSSepherosa Ziehau 	/* Save necessary information */
812beedf5beSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_mbuf = m_new;
813beedf5beSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_paddr = paddr;
8146c8d8eccSSepherosa Ziehau 
8156c8d8eccSSepherosa Ziehau 	/* Set up the descriptor. */
8166c8d8eccSSepherosa Ziehau 	bnx_setup_rxdesc_jumbo(sc, i);
8176c8d8eccSSepherosa Ziehau 	return 0;
8186c8d8eccSSepherosa Ziehau }
8196c8d8eccSSepherosa Ziehau 
8206c8d8eccSSepherosa Ziehau static void
8216c8d8eccSSepherosa Ziehau bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
8226c8d8eccSSepherosa Ziehau {
8236c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
824beedf5beSSepherosa Ziehau 	struct bnx_rx_buf *rc;
8256c8d8eccSSepherosa Ziehau 
8266c8d8eccSSepherosa Ziehau 	r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
8276c8d8eccSSepherosa Ziehau 	rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
8286c8d8eccSSepherosa Ziehau 
829beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_rx_paddr);
830beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_rx_paddr);
831beedf5beSSepherosa Ziehau 	r->bge_len = rc->bnx_rx_mbuf->m_len;
8326c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
8336c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
8346c8d8eccSSepherosa Ziehau }
8356c8d8eccSSepherosa Ziehau 
8366c8d8eccSSepherosa Ziehau static int
837beedf5beSSepherosa Ziehau bnx_init_rx_ring_std(struct bnx_rx_std_ring *std)
8386c8d8eccSSepherosa Ziehau {
8396c8d8eccSSepherosa Ziehau 	int i, error;
8406c8d8eccSSepherosa Ziehau 
8416c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
842beedf5beSSepherosa Ziehau 		/* Use the first RX return ring's tmp RX mbuf DMA map */
843beedf5beSSepherosa Ziehau 		error = bnx_newbuf_std(&std->bnx_sc->bnx_rx_ret_ring[0], i, 1);
8446c8d8eccSSepherosa Ziehau 		if (error)
8456c8d8eccSSepherosa Ziehau 			return error;
84687c7a7cfSSascha Wildner 	}
8476c8d8eccSSepherosa Ziehau 
848beedf5beSSepherosa Ziehau 	std->bnx_rx_std = BGE_STD_RX_RING_CNT - 1;
849beedf5beSSepherosa Ziehau 	bnx_writembx(std->bnx_sc, BGE_MBX_RX_STD_PROD_LO, std->bnx_rx_std);
8506c8d8eccSSepherosa Ziehau 
8516c8d8eccSSepherosa Ziehau 	return(0);
8526c8d8eccSSepherosa Ziehau }
8536c8d8eccSSepherosa Ziehau 
8546c8d8eccSSepherosa Ziehau static void
855beedf5beSSepherosa Ziehau bnx_free_rx_ring_std(struct bnx_rx_std_ring *std)
8566c8d8eccSSepherosa Ziehau {
8576c8d8eccSSepherosa Ziehau 	int i;
8586c8d8eccSSepherosa Ziehau 
8596c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
860beedf5beSSepherosa Ziehau 		struct bnx_rx_buf *rb = &std->bnx_rx_std_buf[i];
8616c8d8eccSSepherosa Ziehau 
862beedf5beSSepherosa Ziehau 		if (rb->bnx_rx_mbuf != NULL) {
863beedf5beSSepherosa Ziehau 			bus_dmamap_unload(std->bnx_rx_mtag, rb->bnx_rx_dmamap);
864beedf5beSSepherosa Ziehau 			m_freem(rb->bnx_rx_mbuf);
865beedf5beSSepherosa Ziehau 			rb->bnx_rx_mbuf = NULL;
8666c8d8eccSSepherosa Ziehau 		}
867beedf5beSSepherosa Ziehau 		bzero(&std->bnx_rx_std_ring[i], sizeof(struct bge_rx_bd));
8686c8d8eccSSepherosa Ziehau 	}
8696c8d8eccSSepherosa Ziehau }
8706c8d8eccSSepherosa Ziehau 
8716c8d8eccSSepherosa Ziehau static int
8726c8d8eccSSepherosa Ziehau bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
8736c8d8eccSSepherosa Ziehau {
8746c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
8756c8d8eccSSepherosa Ziehau 	int i, error;
8766c8d8eccSSepherosa Ziehau 
8776c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
8786c8d8eccSSepherosa Ziehau 		error = bnx_newbuf_jumbo(sc, i, 1);
8796c8d8eccSSepherosa Ziehau 		if (error)
8806c8d8eccSSepherosa Ziehau 			return error;
88187c7a7cfSSascha Wildner 	}
8826c8d8eccSSepherosa Ziehau 
8836c8d8eccSSepherosa Ziehau 	sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
8846c8d8eccSSepherosa Ziehau 
8856c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
8866c8d8eccSSepherosa Ziehau 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
8876c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
8886c8d8eccSSepherosa Ziehau 
8896c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
8906c8d8eccSSepherosa Ziehau 
8916c8d8eccSSepherosa Ziehau 	return(0);
8926c8d8eccSSepherosa Ziehau }
8936c8d8eccSSepherosa Ziehau 
8946c8d8eccSSepherosa Ziehau static void
8956c8d8eccSSepherosa Ziehau bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
8966c8d8eccSSepherosa Ziehau {
8976c8d8eccSSepherosa Ziehau 	int i;
8986c8d8eccSSepherosa Ziehau 
8996c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
900beedf5beSSepherosa Ziehau 		struct bnx_rx_buf *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
9016c8d8eccSSepherosa Ziehau 
902beedf5beSSepherosa Ziehau 		if (rc->bnx_rx_mbuf != NULL) {
903beedf5beSSepherosa Ziehau 			m_freem(rc->bnx_rx_mbuf);
904beedf5beSSepherosa Ziehau 			rc->bnx_rx_mbuf = NULL;
9056c8d8eccSSepherosa Ziehau 		}
9066c8d8eccSSepherosa Ziehau 		bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
9076c8d8eccSSepherosa Ziehau 		    sizeof(struct bge_rx_bd));
9086c8d8eccSSepherosa Ziehau 	}
9096c8d8eccSSepherosa Ziehau }
9106c8d8eccSSepherosa Ziehau 
9116c8d8eccSSepherosa Ziehau static void
91233a04907SSepherosa Ziehau bnx_free_tx_ring(struct bnx_tx_ring *txr)
9136c8d8eccSSepherosa Ziehau {
9146c8d8eccSSepherosa Ziehau 	int i;
9156c8d8eccSSepherosa Ziehau 
9166c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
917fa4b1067SSepherosa Ziehau 		struct bnx_tx_buf *buf = &txr->bnx_tx_buf[i];
918fa4b1067SSepherosa Ziehau 
919fa4b1067SSepherosa Ziehau 		if (buf->bnx_tx_mbuf != NULL) {
92033a04907SSepherosa Ziehau 			bus_dmamap_unload(txr->bnx_tx_mtag,
921fa4b1067SSepherosa Ziehau 			    buf->bnx_tx_dmamap);
922fa4b1067SSepherosa Ziehau 			m_freem(buf->bnx_tx_mbuf);
923fa4b1067SSepherosa Ziehau 			buf->bnx_tx_mbuf = NULL;
9246c8d8eccSSepherosa Ziehau 		}
92533a04907SSepherosa Ziehau 		bzero(&txr->bnx_tx_ring[i], sizeof(struct bge_tx_bd));
9266c8d8eccSSepherosa Ziehau 	}
92733a04907SSepherosa Ziehau 	txr->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
9286c8d8eccSSepherosa Ziehau }
9296c8d8eccSSepherosa Ziehau 
9306c8d8eccSSepherosa Ziehau static int
93133a04907SSepherosa Ziehau bnx_init_tx_ring(struct bnx_tx_ring *txr)
9326c8d8eccSSepherosa Ziehau {
933fa639b88SSepherosa Ziehau 	txr->bnx_tx_cnt = 0;
93433a04907SSepherosa Ziehau 	txr->bnx_tx_saved_considx = 0;
93533a04907SSepherosa Ziehau 	txr->bnx_tx_prodidx = 0;
9366c8d8eccSSepherosa Ziehau 
9376c8d8eccSSepherosa Ziehau 	/* Initialize transmit producer index for host-memory send ring. */
9388bd43d5dSSepherosa Ziehau 	bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, txr->bnx_tx_prodidx);
9396c8d8eccSSepherosa Ziehau 
9406c8d8eccSSepherosa Ziehau 	return(0);
9416c8d8eccSSepherosa Ziehau }
9426c8d8eccSSepherosa Ziehau 
9436c8d8eccSSepherosa Ziehau static void
9446c8d8eccSSepherosa Ziehau bnx_setmulti(struct bnx_softc *sc)
9456c8d8eccSSepherosa Ziehau {
9466c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
9476c8d8eccSSepherosa Ziehau 	struct ifmultiaddr *ifma;
9486c8d8eccSSepherosa Ziehau 	uint32_t hashes[4] = { 0, 0, 0, 0 };
9496c8d8eccSSepherosa Ziehau 	int h, i;
9506c8d8eccSSepherosa Ziehau 
9516c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
9526c8d8eccSSepherosa Ziehau 
9536c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
9546c8d8eccSSepherosa Ziehau 		for (i = 0; i < 4; i++)
9556c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
9566c8d8eccSSepherosa Ziehau 		return;
9576c8d8eccSSepherosa Ziehau 	}
9586c8d8eccSSepherosa Ziehau 
9596c8d8eccSSepherosa Ziehau 	/* First, zot all the existing filters. */
9606c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
9616c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
9626c8d8eccSSepherosa Ziehau 
9636c8d8eccSSepherosa Ziehau 	/* Now program new ones. */
9646c8d8eccSSepherosa Ziehau 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
9656c8d8eccSSepherosa Ziehau 		if (ifma->ifma_addr->sa_family != AF_LINK)
9666c8d8eccSSepherosa Ziehau 			continue;
9676c8d8eccSSepherosa Ziehau 		h = ether_crc32_le(
9686c8d8eccSSepherosa Ziehau 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
9696c8d8eccSSepherosa Ziehau 		    ETHER_ADDR_LEN) & 0x7f;
9706c8d8eccSSepherosa Ziehau 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
9716c8d8eccSSepherosa Ziehau 	}
9726c8d8eccSSepherosa Ziehau 
9736c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
9746c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
9756c8d8eccSSepherosa Ziehau }
9766c8d8eccSSepherosa Ziehau 
9776c8d8eccSSepherosa Ziehau /*
9786c8d8eccSSepherosa Ziehau  * Do endian, PCI and DMA initialization. Also check the on-board ROM
9796c8d8eccSSepherosa Ziehau  * self-test results.
9806c8d8eccSSepherosa Ziehau  */
9816c8d8eccSSepherosa Ziehau static int
9826c8d8eccSSepherosa Ziehau bnx_chipinit(struct bnx_softc *sc)
9836c8d8eccSSepherosa Ziehau {
9846c8d8eccSSepherosa Ziehau 	uint32_t dma_rw_ctl, mode_ctl;
9856c8d8eccSSepherosa Ziehau 	int i;
9866c8d8eccSSepherosa Ziehau 
9876c8d8eccSSepherosa Ziehau 	/* Set endian type before we access any non-PCI registers. */
9886c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
9896c8d8eccSSepherosa Ziehau 	    BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
9906c8d8eccSSepherosa Ziehau 
9916c8d8eccSSepherosa Ziehau 	/* Clear the MAC control register */
9926c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
9936c8d8eccSSepherosa Ziehau 
9946c8d8eccSSepherosa Ziehau 	/*
9956c8d8eccSSepherosa Ziehau 	 * Clear the MAC statistics block in the NIC's
9966c8d8eccSSepherosa Ziehau 	 * internal memory.
9976c8d8eccSSepherosa Ziehau 	 */
9986c8d8eccSSepherosa Ziehau 	for (i = BGE_STATS_BLOCK;
9996c8d8eccSSepherosa Ziehau 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
10006c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
10016c8d8eccSSepherosa Ziehau 
10026c8d8eccSSepherosa Ziehau 	for (i = BGE_STATUS_BLOCK;
10036c8d8eccSSepherosa Ziehau 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
10046c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
10056c8d8eccSSepherosa Ziehau 
1006d7872545SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc)) {
1007d7872545SSepherosa Ziehau 		uint32_t val;
1008d7872545SSepherosa Ziehau 
1009d7872545SSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1010d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1011d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1012d7872545SSepherosa Ziehau 
1013d7872545SSepherosa Ziehau 			/* Access the lower 1K of PL PCI-E block registers. */
1014d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1015d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_PL_SEL);
1016d7872545SSepherosa Ziehau 
1017d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1018d7872545SSepherosa Ziehau 			val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1019d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1020d7872545SSepherosa Ziehau 
1021d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1022d7872545SSepherosa Ziehau 		}
1023d7872545SSepherosa Ziehau 		if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
10241749651bSSepherosa Ziehau 			/* Fix transmit hangs */
10251749651bSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL);
10261749651bSSepherosa Ziehau 			val |= BGE_CPMU_PADRNG_CTL_RDIV2;
10271749651bSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL, val);
10281749651bSSepherosa Ziehau 
1029d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1030d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1031d7872545SSepherosa Ziehau 
1032d7872545SSepherosa Ziehau 			/* Access the lower 1K of DL PCI-E block registers. */
1033d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1034d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_DL_SEL);
1035d7872545SSepherosa Ziehau 
1036d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1037d7872545SSepherosa Ziehau 			val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1038d7872545SSepherosa Ziehau 			val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1039d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1040d7872545SSepherosa Ziehau 
1041d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1042d7872545SSepherosa Ziehau 		}
1043d7872545SSepherosa Ziehau 
1044d7872545SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1045d7872545SSepherosa Ziehau 		val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1046d7872545SSepherosa Ziehau 		val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1047d7872545SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1048d7872545SSepherosa Ziehau 	}
1049d7872545SSepherosa Ziehau 
10502890cca3SSepherosa Ziehau 	/*
10512890cca3SSepherosa Ziehau 	 * Set up the PCI DMA control register.
10522890cca3SSepherosa Ziehau 	 */
10532890cca3SSepherosa Ziehau 	dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
10542890cca3SSepherosa Ziehau 	/*
10552890cca3SSepherosa Ziehau 	 * Disable 32bytes cache alignment for DMA write to host memory
10562890cca3SSepherosa Ziehau 	 *
10572890cca3SSepherosa Ziehau 	 * NOTE:
10582890cca3SSepherosa Ziehau 	 * 64bytes cache alignment for DMA write to host memory is still
10592890cca3SSepherosa Ziehau 	 * enabled.
10602890cca3SSepherosa Ziehau 	 */
10612890cca3SSepherosa Ziehau 	dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
10626c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
10636c8d8eccSSepherosa Ziehau 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
10646c8d8eccSSepherosa Ziehau 	/*
10656c8d8eccSSepherosa Ziehau 	 * Enable HW workaround for controllers that misinterpret
10666c8d8eccSSepherosa Ziehau 	 * a status tag update and leave interrupts permanently
10676c8d8eccSSepherosa Ziehau 	 * disabled.
10686c8d8eccSSepherosa Ziehau 	 */
10696c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1070b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev != BGE_ASICREV_BCM5762 &&
10712890cca3SSepherosa Ziehau 	    !BNX_IS_57765_FAMILY(sc))
10726c8d8eccSSepherosa Ziehau 		dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
10732890cca3SSepherosa Ziehau 	if (bootverbose) {
10742890cca3SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
10752890cca3SSepherosa Ziehau 		    dma_rw_ctl);
10766c8d8eccSSepherosa Ziehau 	}
10776c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
10786c8d8eccSSepherosa Ziehau 
10796c8d8eccSSepherosa Ziehau 	/*
10806c8d8eccSSepherosa Ziehau 	 * Set up general mode register.
10816c8d8eccSSepherosa Ziehau 	 */
10826c8d8eccSSepherosa Ziehau 	mode_ctl = bnx_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
10836c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
10846c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
10856c8d8eccSSepherosa Ziehau 
10866c8d8eccSSepherosa Ziehau 	/*
10876c8d8eccSSepherosa Ziehau 	 * Disable memory write invalidate.  Apparently it is not supported
10886c8d8eccSSepherosa Ziehau 	 * properly by these devices.  Also ensure that INTx isn't disabled,
10896c8d8eccSSepherosa Ziehau 	 * as these chips need it even when using MSI.
10906c8d8eccSSepherosa Ziehau 	 */
10916c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
10926c8d8eccSSepherosa Ziehau 	    (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
10936c8d8eccSSepherosa Ziehau 
10946c8d8eccSSepherosa Ziehau 	/* Set the timer prescaler (always 66Mhz) */
10956c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
10966c8d8eccSSepherosa Ziehau 
10976c8d8eccSSepherosa Ziehau 	return(0);
10986c8d8eccSSepherosa Ziehau }
10996c8d8eccSSepherosa Ziehau 
11006c8d8eccSSepherosa Ziehau static int
11016c8d8eccSSepherosa Ziehau bnx_blockinit(struct bnx_softc *sc)
11026c8d8eccSSepherosa Ziehau {
110333a04907SSepherosa Ziehau 	struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
1104beedf5beSSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
11056c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
11066c8d8eccSSepherosa Ziehau 	bus_size_t vrcb;
11076c8d8eccSSepherosa Ziehau 	bge_hostaddr taddr;
11086c8d8eccSSepherosa Ziehau 	uint32_t val;
11096c8d8eccSSepherosa Ziehau 	int i, limit;
11106c8d8eccSSepherosa Ziehau 
11116c8d8eccSSepherosa Ziehau 	/*
11126c8d8eccSSepherosa Ziehau 	 * Initialize the memory window pointer register so that
11136c8d8eccSSepherosa Ziehau 	 * we can access the first 32K of internal NIC RAM. This will
11146c8d8eccSSepherosa Ziehau 	 * allow us to set up the TX send ring RCBs and the RX return
11156c8d8eccSSepherosa Ziehau 	 * ring RCBs, plus other things which live in NIC memory.
11166c8d8eccSSepherosa Ziehau 	 */
11176c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
11186c8d8eccSSepherosa Ziehau 
11196c8d8eccSSepherosa Ziehau 	/* Configure mbuf pool watermarks */
1120f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
11216c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11226c8d8eccSSepherosa Ziehau 		if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
11236c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
11246c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
11256c8d8eccSSepherosa Ziehau 		} else {
11266c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
11276c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
11286c8d8eccSSepherosa Ziehau 		}
11296c8d8eccSSepherosa Ziehau 	} else {
11306c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11316c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11326c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
11336c8d8eccSSepherosa Ziehau 	}
11346c8d8eccSSepherosa Ziehau 
11356c8d8eccSSepherosa Ziehau 	/* Configure DMA resource watermarks */
11366c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
11376c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
11386c8d8eccSSepherosa Ziehau 
11396c8d8eccSSepherosa Ziehau 	/* Enable buffer manager */
11406c8d8eccSSepherosa Ziehau 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
11416c8d8eccSSepherosa Ziehau 	/*
11426c8d8eccSSepherosa Ziehau 	 * Change the arbitration algorithm of TXMBUF read request to
11436c8d8eccSSepherosa Ziehau 	 * round-robin instead of priority based for BCM5719.  When
11446c8d8eccSSepherosa Ziehau 	 * TXFIFO is almost empty, RDMA will hold its request until
11456c8d8eccSSepherosa Ziehau 	 * TXFIFO is not almost empty.
11466c8d8eccSSepherosa Ziehau 	 */
11476c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
11486c8d8eccSSepherosa Ziehau 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1149e5eebe34SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1150e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1151e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1152e5eebe34SSepherosa Ziehau 		val |= BGE_BMANMODE_LOMBUF_ATTN;
11536c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
11546c8d8eccSSepherosa Ziehau 
11556c8d8eccSSepherosa Ziehau 	/* Poll for buffer manager start indication */
11566c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
11576c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
11586c8d8eccSSepherosa Ziehau 			break;
11596c8d8eccSSepherosa Ziehau 		DELAY(10);
11606c8d8eccSSepherosa Ziehau 	}
11616c8d8eccSSepherosa Ziehau 
11626c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
11636c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
11646c8d8eccSSepherosa Ziehau 			  "buffer manager failed to start\n");
11656c8d8eccSSepherosa Ziehau 		return(ENXIO);
11666c8d8eccSSepherosa Ziehau 	}
11676c8d8eccSSepherosa Ziehau 
11686c8d8eccSSepherosa Ziehau 	/* Enable flow-through queues */
11696c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
11706c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
11716c8d8eccSSepherosa Ziehau 
11726c8d8eccSSepherosa Ziehau 	/* Wait until queue initialization is complete */
11736c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
11746c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
11756c8d8eccSSepherosa Ziehau 			break;
11766c8d8eccSSepherosa Ziehau 		DELAY(10);
11776c8d8eccSSepherosa Ziehau 	}
11786c8d8eccSSepherosa Ziehau 
11796c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
11806c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
11816c8d8eccSSepherosa Ziehau 			  "flow-through queue init failed\n");
11826c8d8eccSSepherosa Ziehau 		return(ENXIO);
11836c8d8eccSSepherosa Ziehau 	}
11846c8d8eccSSepherosa Ziehau 
11856c8d8eccSSepherosa Ziehau 	/*
11866c8d8eccSSepherosa Ziehau 	 * Summary of rings supported by the controller:
11876c8d8eccSSepherosa Ziehau 	 *
11886c8d8eccSSepherosa Ziehau 	 * Standard Receive Producer Ring
11896c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "standard"
11906c8d8eccSSepherosa Ziehau 	 *   sized frames (typically 1536 bytes) to the controller.
11916c8d8eccSSepherosa Ziehau 	 *
11926c8d8eccSSepherosa Ziehau 	 * Jumbo Receive Producer Ring
11936c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for jumbo sized
11946c8d8eccSSepherosa Ziehau 	 *   frames (i.e. anything bigger than the "standard" frames)
11956c8d8eccSSepherosa Ziehau 	 *   to the controller.
11966c8d8eccSSepherosa Ziehau 	 *
11976c8d8eccSSepherosa Ziehau 	 * Mini Receive Producer Ring
11986c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "mini"
11996c8d8eccSSepherosa Ziehau 	 *   sized frames to the controller.
12006c8d8eccSSepherosa Ziehau 	 * - This feature required external memory for the controller
12016c8d8eccSSepherosa Ziehau 	 *   but was never used in a production system.  Should always
12026c8d8eccSSepherosa Ziehau 	 *   be disabled.
12036c8d8eccSSepherosa Ziehau 	 *
12046c8d8eccSSepherosa Ziehau 	 * Receive Return Ring
12056c8d8eccSSepherosa Ziehau 	 * - After the controller has placed an incoming frame into a
12066c8d8eccSSepherosa Ziehau 	 *   receive buffer that buffer is moved into a receive return
12076c8d8eccSSepherosa Ziehau 	 *   ring.  The driver is then responsible to passing the
12086c8d8eccSSepherosa Ziehau 	 *   buffer up to the stack.  Many versions of the controller
12096c8d8eccSSepherosa Ziehau 	 *   support multiple RR rings.
12106c8d8eccSSepherosa Ziehau 	 *
12116c8d8eccSSepherosa Ziehau 	 * Send Ring
12126c8d8eccSSepherosa Ziehau 	 * - This ring is used for outgoing frames.  Many versions of
12136c8d8eccSSepherosa Ziehau 	 *   the controller support multiple send rings.
12146c8d8eccSSepherosa Ziehau 	 */
12156c8d8eccSSepherosa Ziehau 
12166c8d8eccSSepherosa Ziehau 	/* Initialize the standard receive producer ring control block. */
12176c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
12186c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_lo =
1219beedf5beSSepherosa Ziehau 	    BGE_ADDR_LO(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
12206c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_hi =
1221beedf5beSSepherosa Ziehau 	    BGE_ADDR_HI(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
1222f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
12236c8d8eccSSepherosa Ziehau 		/*
12246c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
12256c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Maximum RX frame size
12266c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
12276c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
12286c8d8eccSSepherosa Ziehau 		 */
12296c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
12306c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
12316c8d8eccSSepherosa Ziehau 	} else {
12326c8d8eccSSepherosa Ziehau 		/*
12336c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
12346c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Reserved (should be 0)
12356c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
12366c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
12376c8d8eccSSepherosa Ziehau 		 */
12386c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12396c8d8eccSSepherosa Ziehau 	}
1240303fdc72SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
12416c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
12426c8d8eccSSepherosa Ziehau 	else
12436c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
12446c8d8eccSSepherosa Ziehau 	/* Write the standard receive producer ring control block. */
12456c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
12466c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
12476c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
12486c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
12496c8d8eccSSepherosa Ziehau 	/* Reset the standard receive producer ring producer index. */
12506c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
12516c8d8eccSSepherosa Ziehau 
12526c8d8eccSSepherosa Ziehau 	/*
12536c8d8eccSSepherosa Ziehau 	 * Initialize the jumbo RX producer ring control
12546c8d8eccSSepherosa Ziehau 	 * block.  We set the 'ring disabled' bit in the
12556c8d8eccSSepherosa Ziehau 	 * flags field until we're actually ready to start
12566c8d8eccSSepherosa Ziehau 	 * using this ring (i.e. once we set the MTU
12576c8d8eccSSepherosa Ziehau 	 * high enough to require it).
12586c8d8eccSSepherosa Ziehau 	 */
12596c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
12606c8d8eccSSepherosa Ziehau 		rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
12616c8d8eccSSepherosa Ziehau 		/* Get the jumbo receive producer ring RCB parameters. */
12626c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_lo =
12636c8d8eccSSepherosa Ziehau 		    BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
12646c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_hi =
12656c8d8eccSSepherosa Ziehau 		    BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
12666c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
12676c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
12686c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
1269303fdc72SSepherosa Ziehau 		if (BNX_IS_5717_PLUS(sc))
12706c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
12716c8d8eccSSepherosa Ziehau 		else
12726c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
12736c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
12746c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_hi);
12756c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
12766c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_lo);
12776c8d8eccSSepherosa Ziehau 		/* Program the jumbo receive producer ring RCB parameters. */
12786c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12796c8d8eccSSepherosa Ziehau 		    rcb->bge_maxlen_flags);
12806c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
12816c8d8eccSSepherosa Ziehau 		/* Reset the jumbo receive producer ring producer index. */
12826c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
12836c8d8eccSSepherosa Ziehau 	}
12846c8d8eccSSepherosa Ziehau 
12856c8d8eccSSepherosa Ziehau 	/*
12866c8d8eccSSepherosa Ziehau 	 * The BD ring replenish thresholds control how often the
12876c8d8eccSSepherosa Ziehau 	 * hardware fetches new BD's from the producer rings in host
12886c8d8eccSSepherosa Ziehau 	 * memory.  Setting the value too low on a busy system can
12896c8d8eccSSepherosa Ziehau 	 * starve the hardware and recue the throughpout.
12906c8d8eccSSepherosa Ziehau 	 *
12916c8d8eccSSepherosa Ziehau 	 * Set the BD ring replentish thresholds. The recommended
12926c8d8eccSSepherosa Ziehau 	 * values are 1/8th the number of descriptors allocated to
12936c8d8eccSSepherosa Ziehau 	 * each ring.
12946c8d8eccSSepherosa Ziehau 	 */
12956c8d8eccSSepherosa Ziehau 	val = 8;
12966c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
12976c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
12986c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
12996c8d8eccSSepherosa Ziehau 		    BGE_JUMBO_RX_RING_CNT/8);
13006c8d8eccSSepherosa Ziehau 	}
1301f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
13026c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
13036c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
13046c8d8eccSSepherosa Ziehau 	}
13056c8d8eccSSepherosa Ziehau 
13066c8d8eccSSepherosa Ziehau 	/*
13076c8d8eccSSepherosa Ziehau 	 * Disable all send rings by setting the 'ring disabled' bit
13086c8d8eccSSepherosa Ziehau 	 * in the flags field of all the TX send ring control blocks,
13096c8d8eccSSepherosa Ziehau 	 * located in NIC memory.
13106c8d8eccSSepherosa Ziehau 	 */
131180969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
131280969639SSepherosa Ziehau 		limit = 4;
1313b96cbbb6SSepherosa Ziehau 	else if (BNX_IS_57765_FAMILY(sc) ||
1314b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762)
13154f23029eSSepherosa Ziehau 		limit = 2;
131680969639SSepherosa Ziehau 	else
13176c8d8eccSSepherosa Ziehau 		limit = 1;
13186c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
13196c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
13206c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13216c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
13226c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
13236c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
13246c8d8eccSSepherosa Ziehau 	}
13256c8d8eccSSepherosa Ziehau 
13266c8d8eccSSepherosa Ziehau 	/* Configure send ring RCB 0 (we use only the first ring) */
13276c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
132833a04907SSepherosa Ziehau 	BGE_HOSTADDR(taddr, txr->bnx_tx_ring_paddr);
13296c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
13306c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1331303fdc72SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
13326c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
13336c8d8eccSSepherosa Ziehau 	} else {
13346c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
13356c8d8eccSSepherosa Ziehau 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13366c8d8eccSSepherosa Ziehau 	}
13376c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13386c8d8eccSSepherosa Ziehau 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
13396c8d8eccSSepherosa Ziehau 
13406c8d8eccSSepherosa Ziehau 	/*
13416c8d8eccSSepherosa Ziehau 	 * Disable all receive return rings by setting the
13426c8d8eccSSepherosa Ziehau 	 * 'ring disabled' bit in the flags field of all the receive
13436c8d8eccSSepherosa Ziehau 	 * return ring control blocks, located in NIC memory.
13446c8d8eccSSepherosa Ziehau 	 */
134580969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
13466c8d8eccSSepherosa Ziehau 		/* Should be 17, use 16 until we get an SRAM map. */
13476c8d8eccSSepherosa Ziehau 		limit = 16;
1348b96cbbb6SSepherosa Ziehau 	} else if (BNX_IS_57765_FAMILY(sc) ||
1349b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
13506c8d8eccSSepherosa Ziehau 		limit = 4;
13516c8d8eccSSepherosa Ziehau 	} else {
13526c8d8eccSSepherosa Ziehau 		limit = 1;
13536c8d8eccSSepherosa Ziehau 	}
13546c8d8eccSSepherosa Ziehau 	/* Disable all receive return rings. */
13556c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
13566c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
13576c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
13586c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
13596c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13606c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
13616c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
13626c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
13636c8d8eccSSepherosa Ziehau 		    (i * (sizeof(uint64_t))), 0);
13646c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
13656c8d8eccSSepherosa Ziehau 	}
13666c8d8eccSSepherosa Ziehau 
13676c8d8eccSSepherosa Ziehau 	/*
13686c8d8eccSSepherosa Ziehau 	 * Set up receive return ring 0.  Note that the NIC address
13696c8d8eccSSepherosa Ziehau 	 * for RX return rings is 0x0.  The return rings live entirely
13706c8d8eccSSepherosa Ziehau 	 * within the host, so the nicaddr field in the RCB isn't used.
13716c8d8eccSSepherosa Ziehau 	 */
13726c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1373beedf5beSSepherosa Ziehau 	BGE_HOSTADDR(taddr, ret->bnx_rx_ret_ring_paddr);
13746c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
13756c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
13766c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
13776c8d8eccSSepherosa Ziehau 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1378e0f74fc8SSepherosa Ziehau 	    BGE_RCB_MAXLEN_FLAGS(BNX_RETURN_RING_CNT, 0));
13796c8d8eccSSepherosa Ziehau 
13806c8d8eccSSepherosa Ziehau 	/* Set random backoff seed for TX */
13816c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13826c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
13836c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
13846c8d8eccSSepherosa Ziehau 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
13856c8d8eccSSepherosa Ziehau 	    BGE_TX_BACKOFF_SEED_MASK);
13866c8d8eccSSepherosa Ziehau 
13876c8d8eccSSepherosa Ziehau 	/* Set inter-packet gap */
13886c8d8eccSSepherosa Ziehau 	val = 0x2620;
1389b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1390b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
13916c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
13926c8d8eccSSepherosa Ziehau 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
13936c8d8eccSSepherosa Ziehau 	}
13946c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
13956c8d8eccSSepherosa Ziehau 
13966c8d8eccSSepherosa Ziehau 	/*
13976c8d8eccSSepherosa Ziehau 	 * Specify which ring to use for packets that don't match
13986c8d8eccSSepherosa Ziehau 	 * any RX rules.
13996c8d8eccSSepherosa Ziehau 	 */
14006c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
14016c8d8eccSSepherosa Ziehau 
14026c8d8eccSSepherosa Ziehau 	/*
14036c8d8eccSSepherosa Ziehau 	 * Configure number of RX lists. One interrupt distribution
14046c8d8eccSSepherosa Ziehau 	 * list, sixteen active lists, one bad frames class.
14056c8d8eccSSepherosa Ziehau 	 */
14066c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
14076c8d8eccSSepherosa Ziehau 
14086c8d8eccSSepherosa Ziehau 	/* Inialize RX list placement stats mask. */
14096c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
14106c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
14116c8d8eccSSepherosa Ziehau 
14126c8d8eccSSepherosa Ziehau 	/* Disable host coalescing until we get it set up */
14136c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
14146c8d8eccSSepherosa Ziehau 
14156c8d8eccSSepherosa Ziehau 	/* Poll to make sure it's shut down. */
14166c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
14176c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
14186c8d8eccSSepherosa Ziehau 			break;
14196c8d8eccSSepherosa Ziehau 		DELAY(10);
14206c8d8eccSSepherosa Ziehau 	}
14216c8d8eccSSepherosa Ziehau 
14226c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
14236c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
14246c8d8eccSSepherosa Ziehau 			  "host coalescing engine failed to idle\n");
14256c8d8eccSSepherosa Ziehau 		return(ENXIO);
14266c8d8eccSSepherosa Ziehau 	}
14276c8d8eccSSepherosa Ziehau 
14286c8d8eccSSepherosa Ziehau 	/* Set up host coalescing defaults */
14296c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bnx_rx_coal_ticks);
14306c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bnx_tx_coal_ticks);
14316c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bnx_rx_coal_bds);
14326c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bnx_tx_coal_bds);
14336c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, sc->bnx_rx_coal_bds_int);
14346c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, sc->bnx_tx_coal_bds_int);
14356c8d8eccSSepherosa Ziehau 
14366c8d8eccSSepherosa Ziehau 	/* Set up address of status block */
14376c8d8eccSSepherosa Ziehau 	bzero(sc->bnx_ldata.bnx_status_block, BGE_STATUS_BLK_SZ);
14386c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
14396c8d8eccSSepherosa Ziehau 	    BGE_ADDR_HI(sc->bnx_ldata.bnx_status_block_paddr));
14406c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
14416c8d8eccSSepherosa Ziehau 	    BGE_ADDR_LO(sc->bnx_ldata.bnx_status_block_paddr));
14426c8d8eccSSepherosa Ziehau 
14436c8d8eccSSepherosa Ziehau 	/* Set up status block partail update size. */
14446c8d8eccSSepherosa Ziehau 	val = BGE_STATBLKSZ_32BYTE;
14456c8d8eccSSepherosa Ziehau #if 0
14466c8d8eccSSepherosa Ziehau 	/*
14476c8d8eccSSepherosa Ziehau 	 * Does not seem to have visible effect in both
14486c8d8eccSSepherosa Ziehau 	 * bulk data (1472B UDP datagram) and tiny data
14496c8d8eccSSepherosa Ziehau 	 * (18B UDP datagram) TX tests.
14506c8d8eccSSepherosa Ziehau 	 */
14516c8d8eccSSepherosa Ziehau 	val |= BGE_HCCMODE_CLRTICK_TX;
14526c8d8eccSSepherosa Ziehau #endif
14536c8d8eccSSepherosa Ziehau 	/* Turn on host coalescing state machine */
14546c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
14556c8d8eccSSepherosa Ziehau 
14566c8d8eccSSepherosa Ziehau 	/* Turn on RX BD completion state machine and enable attentions */
14576c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
14586c8d8eccSSepherosa Ziehau 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
14596c8d8eccSSepherosa Ziehau 
14606c8d8eccSSepherosa Ziehau 	/* Turn on RX list placement state machine */
14616c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
14626c8d8eccSSepherosa Ziehau 
14636c8d8eccSSepherosa Ziehau 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
14646c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
14656c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
14666c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_FRMHDR_DMA_ENB;
14676c8d8eccSSepherosa Ziehau 
14686c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
14696c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_TBI;
14706c8d8eccSSepherosa Ziehau 	else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
14716c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_GMII;
14726c8d8eccSSepherosa Ziehau 	else
14736c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_MII;
14746c8d8eccSSepherosa Ziehau 
14756c8d8eccSSepherosa Ziehau 	/* Turn on DMA, clear stats */
14766c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
14776c8d8eccSSepherosa Ziehau 
14786c8d8eccSSepherosa Ziehau 	/* Set misc. local control, enable interrupts on attentions */
14796c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
14806c8d8eccSSepherosa Ziehau 
14816c8d8eccSSepherosa Ziehau #ifdef notdef
14826c8d8eccSSepherosa Ziehau 	/* Assert GPIO pins for PHY reset */
14836c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
14846c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
14856c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
14866c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
14876c8d8eccSSepherosa Ziehau #endif
14886c8d8eccSSepherosa Ziehau 
14896c8d8eccSSepherosa Ziehau 	/* Turn on write DMA state machine */
14906c8d8eccSSepherosa Ziehau 	val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
14916c8d8eccSSepherosa Ziehau 	/* Enable host coalescing bug fix. */
14926c8d8eccSSepherosa Ziehau 	val |= BGE_WDMAMODE_STATUS_TAG_FIX;
14936c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
14946c8d8eccSSepherosa Ziehau 		/* Request larger DMA burst size to get better performance. */
14956c8d8eccSSepherosa Ziehau 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
14966c8d8eccSSepherosa Ziehau 	}
14976c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
14986c8d8eccSSepherosa Ziehau 	DELAY(40);
14996c8d8eccSSepherosa Ziehau 
15003730a14dSSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
1501b96cbbb6SSepherosa Ziehau 		uint32_t dmactl, dmactl_reg;
15026c8d8eccSSepherosa Ziehau 
1503b96cbbb6SSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1504b96cbbb6SSepherosa Ziehau 			dmactl_reg = BGE_RDMA_RSRVCTRL2;
1505b96cbbb6SSepherosa Ziehau 		else
1506b96cbbb6SSepherosa Ziehau 			dmactl_reg = BGE_RDMA_RSRVCTRL;
1507b96cbbb6SSepherosa Ziehau 
1508b96cbbb6SSepherosa Ziehau 		dmactl = CSR_READ_4(sc, dmactl_reg);
15096c8d8eccSSepherosa Ziehau 		/*
15106c8d8eccSSepherosa Ziehau 		 * Adjust tx margin to prevent TX data corruption and
15116c8d8eccSSepherosa Ziehau 		 * fix internal FIFO overflow.
15126c8d8eccSSepherosa Ziehau 		 */
15136c8d8eccSSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1514b96cbbb6SSepherosa Ziehau 		    sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1515b96cbbb6SSepherosa Ziehau 		    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
15166c8d8eccSSepherosa Ziehau 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
15176c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
15186c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
15196c8d8eccSSepherosa Ziehau 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
15206c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
15216c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
15226c8d8eccSSepherosa Ziehau 		}
15236c8d8eccSSepherosa Ziehau 		/*
15246c8d8eccSSepherosa Ziehau 		 * Enable fix for read DMA FIFO overruns.
15256c8d8eccSSepherosa Ziehau 		 * The fix is to limit the number of RX BDs
15266c8d8eccSSepherosa Ziehau 		 * the hardware would fetch at a fime.
15276c8d8eccSSepherosa Ziehau 		 */
1528b96cbbb6SSepherosa Ziehau 		CSR_WRITE_4(sc, dmactl_reg,
15296c8d8eccSSepherosa Ziehau 		    dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
15306c8d8eccSSepherosa Ziehau 	}
15316c8d8eccSSepherosa Ziehau 
15326c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
15336c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
15346c8d8eccSSepherosa Ziehau 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
15356c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
15366c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1537b96cbbb6SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1538b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1539b96cbbb6SSepherosa Ziehau 		uint32_t ctrl_reg;
1540b96cbbb6SSepherosa Ziehau 
1541b96cbbb6SSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1542b96cbbb6SSepherosa Ziehau 			ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL2;
1543b96cbbb6SSepherosa Ziehau 		else
1544b96cbbb6SSepherosa Ziehau 			ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL;
1545b96cbbb6SSepherosa Ziehau 
15466c8d8eccSSepherosa Ziehau 		/*
15476c8d8eccSSepherosa Ziehau 		 * Allow 4KB burst length reads for non-LSO frames.
15486c8d8eccSSepherosa Ziehau 		 * Enable 512B burst length reads for buffer descriptors.
15496c8d8eccSSepherosa Ziehau 		 */
1550b96cbbb6SSepherosa Ziehau 		CSR_WRITE_4(sc, ctrl_reg,
1551b96cbbb6SSepherosa Ziehau 		    CSR_READ_4(sc, ctrl_reg) |
15526c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
15536c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
15546c8d8eccSSepherosa Ziehau 	}
15556c8d8eccSSepherosa Ziehau 
15566c8d8eccSSepherosa Ziehau 	/* Turn on read DMA state machine */
15576c8d8eccSSepherosa Ziehau 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
15586c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
15596c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
15606c8d8eccSSepherosa Ziehau         if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
15616c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
15626c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
15636c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
15646c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
15656c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
15666c8d8eccSSepherosa Ziehau 	}
1567b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1568b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
15696c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
15706c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_H2BNC_VLAN_DET;
15716c8d8eccSSepherosa Ziehau 		/*
15726c8d8eccSSepherosa Ziehau 		 * Allow multiple outstanding read requests from
15736c8d8eccSSepherosa Ziehau 		 * non-LSO read DMA engine.
15746c8d8eccSSepherosa Ziehau 		 */
15756c8d8eccSSepherosa Ziehau 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
15766c8d8eccSSepherosa Ziehau 	}
157760e67e3fSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM57766)
157860e67e3fSSepherosa Ziehau 		val |= BGE_RDMAMODE_JMB_2K_MMRR;
157966deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO)
158066deb1c1SSepherosa Ziehau 		val |= BGE_RDMAMODE_TSO4_ENABLE;
15816c8d8eccSSepherosa Ziehau 	val |= BGE_RDMAMODE_FIFO_LONG_BURST;
15826c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
15836c8d8eccSSepherosa Ziehau 	DELAY(40);
15846c8d8eccSSepherosa Ziehau 
15856c8d8eccSSepherosa Ziehau 	/* Turn on RX data completion state machine */
15866c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
15876c8d8eccSSepherosa Ziehau 
15886c8d8eccSSepherosa Ziehau 	/* Turn on RX BD initiator state machine */
15896c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
15906c8d8eccSSepherosa Ziehau 
15916c8d8eccSSepherosa Ziehau 	/* Turn on RX data and RX BD initiator state machine */
15926c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
15936c8d8eccSSepherosa Ziehau 
15946c8d8eccSSepherosa Ziehau 	/* Turn on send BD completion state machine */
15956c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
15966c8d8eccSSepherosa Ziehau 
15976c8d8eccSSepherosa Ziehau 	/* Turn on send data completion state machine */
15986c8d8eccSSepherosa Ziehau 	val = BGE_SDCMODE_ENABLE;
15996c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
16006c8d8eccSSepherosa Ziehau 		val |= BGE_SDCMODE_CDELAY;
16016c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
16026c8d8eccSSepherosa Ziehau 
16036c8d8eccSSepherosa Ziehau 	/* Turn on send data initiator state machine */
160466deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
160566deb1c1SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
160666deb1c1SSepherosa Ziehau 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
160766deb1c1SSepherosa Ziehau 	} else {
16086c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
160966deb1c1SSepherosa Ziehau 	}
16106c8d8eccSSepherosa Ziehau 
16116c8d8eccSSepherosa Ziehau 	/* Turn on send BD initiator state machine */
16126c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
16136c8d8eccSSepherosa Ziehau 
16146c8d8eccSSepherosa Ziehau 	/* Turn on send BD selector state machine */
16156c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
16166c8d8eccSSepherosa Ziehau 
16176c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
16186c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
16196c8d8eccSSepherosa Ziehau 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
16206c8d8eccSSepherosa Ziehau 
16216c8d8eccSSepherosa Ziehau 	/* ack/clear link change events */
16226c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16236c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16246c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
16256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
16266c8d8eccSSepherosa Ziehau 
16276c8d8eccSSepherosa Ziehau 	/*
16286c8d8eccSSepherosa Ziehau 	 * Enable attention when the link has changed state for
16296c8d8eccSSepherosa Ziehau 	 * devices that use auto polling.
16306c8d8eccSSepherosa Ziehau 	 */
16316c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
16326c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
16336c8d8eccSSepherosa Ziehau  	} else {
16346c8d8eccSSepherosa Ziehau 		if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
16356c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
16366c8d8eccSSepherosa Ziehau 			DELAY(80);
16376c8d8eccSSepherosa Ziehau 		}
16386c8d8eccSSepherosa Ziehau 	}
16396c8d8eccSSepherosa Ziehau 
16406c8d8eccSSepherosa Ziehau 	/*
16416c8d8eccSSepherosa Ziehau 	 * Clear any pending link state attention.
16426c8d8eccSSepherosa Ziehau 	 * Otherwise some link state change events may be lost until attention
16436c8d8eccSSepherosa Ziehau 	 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
16446c8d8eccSSepherosa Ziehau 	 * It's not necessary on newer BCM chips - perhaps enabling link
16456c8d8eccSSepherosa Ziehau 	 * state change attentions implies clearing pending attention.
16466c8d8eccSSepherosa Ziehau 	 */
16476c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16486c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16496c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
16506c8d8eccSSepherosa Ziehau 
16516c8d8eccSSepherosa Ziehau 	/* Enable link state change attentions. */
16526c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
16536c8d8eccSSepherosa Ziehau 
16546c8d8eccSSepherosa Ziehau 	return(0);
16556c8d8eccSSepherosa Ziehau }
16566c8d8eccSSepherosa Ziehau 
16576c8d8eccSSepherosa Ziehau /*
16586c8d8eccSSepherosa Ziehau  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
16596c8d8eccSSepherosa Ziehau  * against our list and return its name if we find a match. Note
16606c8d8eccSSepherosa Ziehau  * that since the Broadcom controller contains VPD support, we
16616c8d8eccSSepherosa Ziehau  * can get the device name string from the controller itself instead
16626c8d8eccSSepherosa Ziehau  * of the compiled-in string. This is a little slow, but it guarantees
16636c8d8eccSSepherosa Ziehau  * we'll always announce the right product name.
16646c8d8eccSSepherosa Ziehau  */
16656c8d8eccSSepherosa Ziehau static int
16666c8d8eccSSepherosa Ziehau bnx_probe(device_t dev)
16676c8d8eccSSepherosa Ziehau {
16686c8d8eccSSepherosa Ziehau 	const struct bnx_type *t;
16696c8d8eccSSepherosa Ziehau 	uint16_t product, vendor;
16706c8d8eccSSepherosa Ziehau 
16716c8d8eccSSepherosa Ziehau 	if (!pci_is_pcie(dev))
16726c8d8eccSSepherosa Ziehau 		return ENXIO;
16736c8d8eccSSepherosa Ziehau 
16746c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
16756c8d8eccSSepherosa Ziehau 	vendor = pci_get_vendor(dev);
16766c8d8eccSSepherosa Ziehau 
16776c8d8eccSSepherosa Ziehau 	for (t = bnx_devs; t->bnx_name != NULL; t++) {
16786c8d8eccSSepherosa Ziehau 		if (vendor == t->bnx_vid && product == t->bnx_did)
16796c8d8eccSSepherosa Ziehau 			break;
16806c8d8eccSSepherosa Ziehau 	}
16816c8d8eccSSepherosa Ziehau 	if (t->bnx_name == NULL)
16826c8d8eccSSepherosa Ziehau 		return ENXIO;
16836c8d8eccSSepherosa Ziehau 
16846c8d8eccSSepherosa Ziehau 	device_set_desc(dev, t->bnx_name);
16856c8d8eccSSepherosa Ziehau 	return 0;
16866c8d8eccSSepherosa Ziehau }
16876c8d8eccSSepherosa Ziehau 
16886c8d8eccSSepherosa Ziehau static int
16896c8d8eccSSepherosa Ziehau bnx_attach(device_t dev)
16906c8d8eccSSepherosa Ziehau {
16916c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
16926c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
1693e594b5c4SSepherosa Ziehau 	uint32_t hwcfg = 0;
1694329f9016SSepherosa Ziehau 	int error = 0, rid, capmask, i;
16956c8d8eccSSepherosa Ziehau 	uint8_t ether_addr[ETHER_ADDR_LEN];
169607e9f7c0SSascha Wildner 	uint16_t product;
16976c8d8eccSSepherosa Ziehau 	uintptr_t mii_priv = 0;
169866deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
169966deb1c1SSepherosa Ziehau 	char desc[32];
170066deb1c1SSepherosa Ziehau #endif
17014fa38985SSepherosa Ziehau #ifdef IFPOLL_ENABLE
17024fa38985SSepherosa Ziehau 	int offset, offset_def;
17034fa38985SSepherosa Ziehau #endif
17046c8d8eccSSepherosa Ziehau 
17056c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
17066c8d8eccSSepherosa Ziehau 	sc->bnx_dev = dev;
1707*7dbaa833SSepherosa Ziehau 	callout_init_mp(&sc->bnx_tick_timer);
17086c8d8eccSSepherosa Ziehau 	lwkt_serialize_init(&sc->bnx_jslot_serializer);
1709f33ac8a4SSepherosa Ziehau 	lwkt_serialize_init(&sc->bnx_main_serialize);
17106c8d8eccSSepherosa Ziehau 
17116c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
17126c8d8eccSSepherosa Ziehau 
17136c8d8eccSSepherosa Ziehau #ifndef BURN_BRIDGES
17146c8d8eccSSepherosa Ziehau 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
17156c8d8eccSSepherosa Ziehau 		uint32_t irq, mem;
17166c8d8eccSSepherosa Ziehau 
17176c8d8eccSSepherosa Ziehau 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
17186c8d8eccSSepherosa Ziehau 		mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
17196c8d8eccSSepherosa Ziehau 
17206c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip is in D%d power mode "
17216c8d8eccSSepherosa Ziehau 		    "-- setting to D0\n", pci_get_powerstate(dev));
17226c8d8eccSSepherosa Ziehau 
17236c8d8eccSSepherosa Ziehau 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
17246c8d8eccSSepherosa Ziehau 
17256c8d8eccSSepherosa Ziehau 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
17266c8d8eccSSepherosa Ziehau 		pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
17276c8d8eccSSepherosa Ziehau 	}
17286c8d8eccSSepherosa Ziehau #endif	/* !BURN_BRIDGE */
17296c8d8eccSSepherosa Ziehau 
17306c8d8eccSSepherosa Ziehau 	/*
17316c8d8eccSSepherosa Ziehau 	 * Map control/status registers.
17326c8d8eccSSepherosa Ziehau 	 */
17336c8d8eccSSepherosa Ziehau 	pci_enable_busmaster(dev);
17346c8d8eccSSepherosa Ziehau 
17356c8d8eccSSepherosa Ziehau 	rid = BGE_PCI_BAR0;
17366c8d8eccSSepherosa Ziehau 	sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
17376c8d8eccSSepherosa Ziehau 	    RF_ACTIVE);
17386c8d8eccSSepherosa Ziehau 
17396c8d8eccSSepherosa Ziehau 	if (sc->bnx_res == NULL) {
17406c8d8eccSSepherosa Ziehau 		device_printf(dev, "couldn't map memory\n");
17416c8d8eccSSepherosa Ziehau 		return ENXIO;
17426c8d8eccSSepherosa Ziehau 	}
17436c8d8eccSSepherosa Ziehau 
17446c8d8eccSSepherosa Ziehau 	sc->bnx_btag = rman_get_bustag(sc->bnx_res);
17456c8d8eccSSepherosa Ziehau 	sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
17466c8d8eccSSepherosa Ziehau 
17476c8d8eccSSepherosa Ziehau 	/* Save various chip information */
17486c8d8eccSSepherosa Ziehau 	sc->bnx_chipid =
17496c8d8eccSSepherosa Ziehau 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
17506c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
17516c8d8eccSSepherosa Ziehau 	if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
17526c8d8eccSSepherosa Ziehau 		/* All chips having dedicated ASICREV register have CPMU */
17536c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_CPMU;
17546c8d8eccSSepherosa Ziehau 
17556c8d8eccSSepherosa Ziehau 		switch (product) {
17566c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5717:
1757d79f5d8fSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5717C:
17586c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5718:
17596c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5719:
17606c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1761b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5725:
1762b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5727:
1763b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5762:
17646c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
17656c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
17666c8d8eccSSepherosa Ziehau 			break;
17676c8d8eccSSepherosa Ziehau 
17686c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57761:
176932ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57762:
17706c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57765:
177132ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57766:
17726c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57781:
177332ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57782:
17746c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57785:
177532ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57786:
17766c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57791:
17776c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57795:
17786c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
17796c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
17806c8d8eccSSepherosa Ziehau 			break;
17816c8d8eccSSepherosa Ziehau 
17826c8d8eccSSepherosa Ziehau 		default:
17836c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
17846c8d8eccSSepherosa Ziehau 			    BGE_PCI_PRODID_ASICREV, 4);
17856c8d8eccSSepherosa Ziehau 			break;
17866c8d8eccSSepherosa Ziehau 		}
17876c8d8eccSSepherosa Ziehau 	}
1788d79f5d8fSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1789d79f5d8fSSepherosa Ziehau 		sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1790d79f5d8fSSepherosa Ziehau 
17916c8d8eccSSepherosa Ziehau 	sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
17926c8d8eccSSepherosa Ziehau 	sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
17936c8d8eccSSepherosa Ziehau 
17946c8d8eccSSepherosa Ziehau 	switch (sc->bnx_asicrev) {
17956c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5717:
17966c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5719:
17976c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5720:
1798f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1799f368d0d9SSepherosa Ziehau 		break;
1800f368d0d9SSepherosa Ziehau 
1801b96cbbb6SSepherosa Ziehau 	case BGE_ASICREV_BCM5762:
1802b96cbbb6SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_57765_PLUS;
1803b96cbbb6SSepherosa Ziehau 		break;
1804b96cbbb6SSepherosa Ziehau 
18056c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM57765:
180632ff3c80SSepherosa Ziehau 	case BGE_ASICREV_BCM57766:
1807f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
18086c8d8eccSSepherosa Ziehau 		break;
18096c8d8eccSSepherosa Ziehau 	}
18106c8d8eccSSepherosa Ziehau 
181166deb1c1SSepherosa Ziehau 	sc->bnx_flags |= BNX_FLAG_TSO;
181266deb1c1SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
181366deb1c1SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
181466deb1c1SSepherosa Ziehau 		sc->bnx_flags &= ~BNX_FLAG_TSO;
181566deb1c1SSepherosa Ziehau 
1816df9ccc98SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1817df9ccc98SSepherosa Ziehau 	    BNX_IS_57765_FAMILY(sc)) {
1818df9ccc98SSepherosa Ziehau 		/*
1819df9ccc98SSepherosa Ziehau 		 * All BCM57785 and BCM5718 families chips have a bug that
1820df9ccc98SSepherosa Ziehau 		 * under certain situation interrupt will not be enabled
1821df9ccc98SSepherosa Ziehau 		 * even if status tag is written to BGE_MBX_IRQ0_LO mailbox.
1822df9ccc98SSepherosa Ziehau 		 *
1823df9ccc98SSepherosa Ziehau 		 * While BCM5719 and BCM5720 have a hardware workaround
1824df9ccc98SSepherosa Ziehau 		 * which could fix the above bug.
1825df9ccc98SSepherosa Ziehau 		 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
1826df9ccc98SSepherosa Ziehau 		 * bnx_chipinit().
1827df9ccc98SSepherosa Ziehau 		 *
1828df9ccc98SSepherosa Ziehau 		 * For the rest of the chips in these two families, we will
1829df9ccc98SSepherosa Ziehau 		 * have to poll the status block at high rate (10ms currently)
1830df9ccc98SSepherosa Ziehau 		 * to check whether the interrupt is hosed or not.
1831f33ac8a4SSepherosa Ziehau 		 * See bnx_check_intr() for details.
1832df9ccc98SSepherosa Ziehau 		 */
1833df9ccc98SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
1834df9ccc98SSepherosa Ziehau 	}
1835df9ccc98SSepherosa Ziehau 
18366c8d8eccSSepherosa Ziehau 	sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
18376c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
18386c8d8eccSSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720)
18396c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
18406c8d8eccSSepherosa Ziehau 	else
18416c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
18426c8d8eccSSepherosa Ziehau 	device_printf(dev, "CHIP ID 0x%08x; "
18436c8d8eccSSepherosa Ziehau 		      "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
18446c8d8eccSSepherosa Ziehau 		      sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
18456c8d8eccSSepherosa Ziehau 
18466c8d8eccSSepherosa Ziehau 	/*
18476c8d8eccSSepherosa Ziehau 	 * Set various PHY quirk flags.
18486c8d8eccSSepherosa Ziehau 	 */
18496c8d8eccSSepherosa Ziehau 
18506c8d8eccSSepherosa Ziehau 	capmask = MII_CAPMASK_DEFAULT;
185146283a40SSepherosa Ziehau 	if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
185246283a40SSepherosa Ziehau 	    product == PCI_PRODUCT_BROADCOM_BCM57795) {
18536c8d8eccSSepherosa Ziehau 		/* 10/100 only */
18546c8d8eccSSepherosa Ziehau 		capmask &= ~BMSR_EXTSTAT;
18556c8d8eccSSepherosa Ziehau 	}
18566c8d8eccSSepherosa Ziehau 
18576c8d8eccSSepherosa Ziehau 	mii_priv |= BRGPHY_FLAG_WIRESPEED;
1858b96cbbb6SSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5762_A0)
1859b96cbbb6SSepherosa Ziehau 		mii_priv |= BRGPHY_FLAG_5762_A0;
18606c8d8eccSSepherosa Ziehau 
18616c8d8eccSSepherosa Ziehau 	/* Initialize if_name earlier, so if_printf could be used */
18626c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
18636c8d8eccSSepherosa Ziehau 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
18646c8d8eccSSepherosa Ziehau 
18656c8d8eccSSepherosa Ziehau 	/* Try to reset the chip. */
18666c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
18676c8d8eccSSepherosa Ziehau 
18686c8d8eccSSepherosa Ziehau 	if (bnx_chipinit(sc)) {
18696c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip initialization failed\n");
18706c8d8eccSSepherosa Ziehau 		error = ENXIO;
18716c8d8eccSSepherosa Ziehau 		goto fail;
18726c8d8eccSSepherosa Ziehau 	}
18736c8d8eccSSepherosa Ziehau 
18746c8d8eccSSepherosa Ziehau 	/*
18756c8d8eccSSepherosa Ziehau 	 * Get station address
18766c8d8eccSSepherosa Ziehau 	 */
18776c8d8eccSSepherosa Ziehau 	error = bnx_get_eaddr(sc, ether_addr);
18786c8d8eccSSepherosa Ziehau 	if (error) {
18796c8d8eccSSepherosa Ziehau 		device_printf(dev, "failed to read station address\n");
18806c8d8eccSSepherosa Ziehau 		goto fail;
18816c8d8eccSSepherosa Ziehau 	}
18826c8d8eccSSepherosa Ziehau 
188333a04907SSepherosa Ziehau 	/* XXX */
188433a04907SSepherosa Ziehau 	sc->bnx_tx_ringcnt = 1;
1885beedf5beSSepherosa Ziehau 	sc->bnx_rx_retcnt = 1;
188633a04907SSepherosa Ziehau 
18874fa38985SSepherosa Ziehau 	if ((sc->bnx_rx_retcnt == 1 && sc->bnx_tx_ringcnt == 1) ||
18884fa38985SSepherosa Ziehau 	    (sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt > 1)) {
18894fa38985SSepherosa Ziehau 	    	/*
18904fa38985SSepherosa Ziehau 		 * The RX ring and the corresponding TX ring processing
18914fa38985SSepherosa Ziehau 		 * should be on the same CPU, since they share the same
18924fa38985SSepherosa Ziehau 		 * status block.
18934fa38985SSepherosa Ziehau 		 */
18944fa38985SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_RXTX_BUNDLE;
18954fa38985SSepherosa Ziehau 		if (bootverbose)
18964fa38985SSepherosa Ziehau 			device_printf(dev, "RX/TX bundle\n");
18974fa38985SSepherosa Ziehau 	} else {
18984fa38985SSepherosa Ziehau 		KKASSERT(sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt == 1);
18994fa38985SSepherosa Ziehau 	}
19004fa38985SSepherosa Ziehau 
1901beedf5beSSepherosa Ziehau 	error = bnx_dma_alloc(dev);
19026c8d8eccSSepherosa Ziehau 	if (error)
19036c8d8eccSSepherosa Ziehau 		goto fail;
19046c8d8eccSSepherosa Ziehau 
19054fa38985SSepherosa Ziehau #ifdef IFPOLL_ENABLE
19064fa38985SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
19074fa38985SSepherosa Ziehau 		/*
19084fa38985SSepherosa Ziehau 		 * NPOLLING RX/TX CPU offset
19094fa38985SSepherosa Ziehau 		 */
19104fa38985SSepherosa Ziehau 		if (sc->bnx_rx_retcnt == ncpus2) {
19114fa38985SSepherosa Ziehau 			offset = 0;
19124fa38985SSepherosa Ziehau 		} else {
19134fa38985SSepherosa Ziehau 			offset_def =
19144fa38985SSepherosa Ziehau 			(sc->bnx_rx_retcnt * device_get_unit(dev)) % ncpus2;
19154fa38985SSepherosa Ziehau 			offset = device_getenv_int(dev, "npoll.offset",
19164fa38985SSepherosa Ziehau 			    offset_def);
19174fa38985SSepherosa Ziehau 			if (offset >= ncpus2 ||
19184fa38985SSepherosa Ziehau 			    offset % sc->bnx_rx_retcnt != 0) {
19194fa38985SSepherosa Ziehau 				device_printf(dev, "invalid npoll.offset %d, "
19204fa38985SSepherosa Ziehau 				    "use %d\n", offset, offset_def);
19214fa38985SSepherosa Ziehau 				offset = offset_def;
19224fa38985SSepherosa Ziehau 			}
19234fa38985SSepherosa Ziehau 		}
19244fa38985SSepherosa Ziehau 		sc->bnx_npoll_rxoff = offset;
19254fa38985SSepherosa Ziehau 		sc->bnx_npoll_txoff = offset;
19264fa38985SSepherosa Ziehau 	} else {
19274fa38985SSepherosa Ziehau 		/*
19284fa38985SSepherosa Ziehau 		 * NPOLLING RX CPU offset
19294fa38985SSepherosa Ziehau 		 */
19304fa38985SSepherosa Ziehau 		if (sc->bnx_rx_retcnt == ncpus2) {
19314fa38985SSepherosa Ziehau 			offset = 0;
19324fa38985SSepherosa Ziehau 		} else {
19334fa38985SSepherosa Ziehau 			offset_def =
19344fa38985SSepherosa Ziehau 			(sc->bnx_rx_retcnt * device_get_unit(dev)) % ncpus2;
19354fa38985SSepherosa Ziehau 			offset = device_getenv_int(dev, "npoll.rxoff",
19364fa38985SSepherosa Ziehau 			    offset_def);
19374fa38985SSepherosa Ziehau 			if (offset >= ncpus2 ||
19384fa38985SSepherosa Ziehau 			    offset % sc->bnx_rx_retcnt != 0) {
19394fa38985SSepherosa Ziehau 				device_printf(dev, "invalid npoll.rxoff %d, "
19404fa38985SSepherosa Ziehau 				    "use %d\n", offset, offset_def);
19414fa38985SSepherosa Ziehau 				offset = offset_def;
19424fa38985SSepherosa Ziehau 			}
19434fa38985SSepherosa Ziehau 		}
19444fa38985SSepherosa Ziehau 		sc->bnx_npoll_rxoff = offset;
19454fa38985SSepherosa Ziehau 
19464fa38985SSepherosa Ziehau 		/*
19474fa38985SSepherosa Ziehau 		 * NPOLLING TX CPU offset
19484fa38985SSepherosa Ziehau 		 */
19494fa38985SSepherosa Ziehau 		offset_def = device_get_unit(dev) % ncpus2;
19504fa38985SSepherosa Ziehau 		offset = device_getenv_int(dev, "npoll.txoff", offset_def);
19514fa38985SSepherosa Ziehau 		if (offset >= ncpus2) {
19524fa38985SSepherosa Ziehau 			device_printf(dev, "invalid npoll.txoff %d, use %d\n",
19534fa38985SSepherosa Ziehau 			    offset, offset_def);
19544fa38985SSepherosa Ziehau 			offset = offset_def;
19554fa38985SSepherosa Ziehau 		}
19564fa38985SSepherosa Ziehau 		sc->bnx_npoll_txoff = offset;
19574fa38985SSepherosa Ziehau 	}
19584fa38985SSepherosa Ziehau #endif	/* IFPOLL_ENABLE */
19594fa38985SSepherosa Ziehau 
196016b32c4cSSepherosa Ziehau 	/*
196116b32c4cSSepherosa Ziehau 	 * Allocate interrupt
196216b32c4cSSepherosa Ziehau 	 */
19630c7da01dSSepherosa Ziehau 	error = bnx_alloc_intr(sc);
19640c7da01dSSepherosa Ziehau 	if (error)
196516b32c4cSSepherosa Ziehau 		goto fail;
196616b32c4cSSepherosa Ziehau 
1967329f9016SSepherosa Ziehau 	/* Setup serializers */
1968329f9016SSepherosa Ziehau 	bnx_setup_serialize(sc);
1969329f9016SSepherosa Ziehau 
19706c8d8eccSSepherosa Ziehau 	/* Set default tuneable values. */
19716c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
19726c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
19736c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
19746c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
1975306e5498SSepherosa Ziehau 	sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
1976306e5498SSepherosa Ziehau 	sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
19776c8d8eccSSepherosa Ziehau 
19786c8d8eccSSepherosa Ziehau 	/* Set up ifnet structure */
19796c8d8eccSSepherosa Ziehau 	ifp->if_softc = sc;
19806c8d8eccSSepherosa Ziehau 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
19816c8d8eccSSepherosa Ziehau 	ifp->if_ioctl = bnx_ioctl;
19826c8d8eccSSepherosa Ziehau 	ifp->if_start = bnx_start;
198339a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
198439a8d43aSSepherosa Ziehau 	ifp->if_npoll = bnx_npoll;
19856c8d8eccSSepherosa Ziehau #endif
19866c8d8eccSSepherosa Ziehau 	ifp->if_init = bnx_init;
1987329f9016SSepherosa Ziehau 	ifp->if_serialize = bnx_serialize;
1988329f9016SSepherosa Ziehau 	ifp->if_deserialize = bnx_deserialize;
1989329f9016SSepherosa Ziehau 	ifp->if_tryserialize = bnx_tryserialize;
1990329f9016SSepherosa Ziehau #ifdef INVARIANTS
1991329f9016SSepherosa Ziehau 	ifp->if_serialize_assert = bnx_serialize_assert;
1992329f9016SSepherosa Ziehau #endif
19936c8d8eccSSepherosa Ziehau 	ifp->if_mtu = ETHERMTU;
19946c8d8eccSSepherosa Ziehau 	ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
19956c8d8eccSSepherosa Ziehau 
19966c8d8eccSSepherosa Ziehau 	ifp->if_capabilities |= IFCAP_HWCSUM;
19976c8d8eccSSepherosa Ziehau 	ifp->if_hwassist = BNX_CSUM_FEATURES;
199866deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
199966deb1c1SSepherosa Ziehau 		ifp->if_capabilities |= IFCAP_TSO;
200066deb1c1SSepherosa Ziehau 		ifp->if_hwassist |= CSUM_TSO;
200166deb1c1SSepherosa Ziehau 	}
20026c8d8eccSSepherosa Ziehau 	ifp->if_capenable = ifp->if_capabilities;
20036c8d8eccSSepherosa Ziehau 
2004329f9016SSepherosa Ziehau 	ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
2005329f9016SSepherosa Ziehau 	ifq_set_ready(&ifp->if_snd);
2006329f9016SSepherosa Ziehau 	ifq_set_subq_cnt(&ifp->if_snd, sc->bnx_tx_ringcnt);
2007329f9016SSepherosa Ziehau 
20086c8d8eccSSepherosa Ziehau 	/*
20096c8d8eccSSepherosa Ziehau 	 * Figure out what sort of media we have by checking the
20106c8d8eccSSepherosa Ziehau 	 * hardware config word in the first 32k of NIC internal memory,
20116c8d8eccSSepherosa Ziehau 	 * or fall back to examining the EEPROM if necessary.
20126c8d8eccSSepherosa Ziehau 	 * Note: on some BCM5700 cards, this value appears to be unset.
20136c8d8eccSSepherosa Ziehau 	 * If that's the case, we have to rely on identifying the NIC
20146c8d8eccSSepherosa Ziehau 	 * by its PCI subsystem ID, as we do below for the SysKonnect
20156c8d8eccSSepherosa Ziehau 	 * SK-9D41.
20166c8d8eccSSepherosa Ziehau 	 */
20176c8d8eccSSepherosa Ziehau 	if (bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
20186c8d8eccSSepherosa Ziehau 		hwcfg = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
20196c8d8eccSSepherosa Ziehau 	} else {
20206c8d8eccSSepherosa Ziehau 		if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
20216c8d8eccSSepherosa Ziehau 				    sizeof(hwcfg))) {
20226c8d8eccSSepherosa Ziehau 			device_printf(dev, "failed to read EEPROM\n");
20236c8d8eccSSepherosa Ziehau 			error = ENXIO;
20246c8d8eccSSepherosa Ziehau 			goto fail;
20256c8d8eccSSepherosa Ziehau 		}
20266c8d8eccSSepherosa Ziehau 		hwcfg = ntohl(hwcfg);
20276c8d8eccSSepherosa Ziehau 	}
20286c8d8eccSSepherosa Ziehau 
20296c8d8eccSSepherosa Ziehau 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
20306c8d8eccSSepherosa Ziehau 	if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
20316c8d8eccSSepherosa Ziehau 	    (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
20326c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_TBI;
20336c8d8eccSSepherosa Ziehau 
20346c8d8eccSSepherosa Ziehau 	/* Setup MI MODE */
20356c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_CPMU)
20366c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
20376c8d8eccSSepherosa Ziehau 	else
20386c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_BASE;
20396c8d8eccSSepherosa Ziehau 
20406c8d8eccSSepherosa Ziehau 	/* Setup link status update stuffs */
20416c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
20426c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_tbi_link_upd;
20436c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20446c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
20456c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_autopoll_link_upd;
20466c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20476c8d8eccSSepherosa Ziehau 	} else {
20486c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_copper_link_upd;
20496c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
20506c8d8eccSSepherosa Ziehau 	}
20516c8d8eccSSepherosa Ziehau 
20526c8d8eccSSepherosa Ziehau 	/* Set default PHY address */
20536c8d8eccSSepherosa Ziehau 	sc->bnx_phyno = 1;
20546c8d8eccSSepherosa Ziehau 
20556c8d8eccSSepherosa Ziehau 	/*
20566c8d8eccSSepherosa Ziehau 	 * PHY address mapping for various devices.
20576c8d8eccSSepherosa Ziehau 	 *
20586c8d8eccSSepherosa Ziehau 	 *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
20596c8d8eccSSepherosa Ziehau 	 * ---------+-------+-------+-------+-------+
20606c8d8eccSSepherosa Ziehau 	 * BCM57XX  |   1   |   X   |   X   |   X   |
20616c8d8eccSSepherosa Ziehau 	 * BCM5704  |   1   |   X   |   1   |   X   |
20626c8d8eccSSepherosa Ziehau 	 * BCM5717  |   1   |   8   |   2   |   9   |
20636c8d8eccSSepherosa Ziehau 	 * BCM5719  |   1   |   8   |   2   |   9   |
20646c8d8eccSSepherosa Ziehau 	 * BCM5720  |   1   |   8   |   2   |   9   |
20656c8d8eccSSepherosa Ziehau 	 *
20666c8d8eccSSepherosa Ziehau 	 * Other addresses may respond but they are not
20676c8d8eccSSepherosa Ziehau 	 * IEEE compliant PHYs and should be ignored.
20686c8d8eccSSepherosa Ziehau 	 */
206980969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
20706c8d8eccSSepherosa Ziehau 		int f;
20716c8d8eccSSepherosa Ziehau 
20726c8d8eccSSepherosa Ziehau 		f = pci_get_function(dev);
20736c8d8eccSSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
20746c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
20756c8d8eccSSepherosa Ziehau 			    BGE_SGDIGSTS_IS_SERDES)
20766c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 8;
20776c8d8eccSSepherosa Ziehau 			else
20786c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 1;
20796c8d8eccSSepherosa Ziehau 		} else {
20806c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
20816c8d8eccSSepherosa Ziehau 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
20826c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 8;
20836c8d8eccSSepherosa Ziehau 			else
20846c8d8eccSSepherosa Ziehau 				sc->bnx_phyno = f + 1;
20856c8d8eccSSepherosa Ziehau 		}
20866c8d8eccSSepherosa Ziehau 	}
20876c8d8eccSSepherosa Ziehau 
20886c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
20896c8d8eccSSepherosa Ziehau 		ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
20906c8d8eccSSepherosa Ziehau 		    bnx_ifmedia_upd, bnx_ifmedia_sts);
20916c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
20926c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia,
20936c8d8eccSSepherosa Ziehau 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
20946c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
20956c8d8eccSSepherosa Ziehau 		ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
20966c8d8eccSSepherosa Ziehau 		sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
20976c8d8eccSSepherosa Ziehau 	} else {
20986c8d8eccSSepherosa Ziehau 		struct mii_probe_args mii_args;
20996c8d8eccSSepherosa Ziehau 
21006c8d8eccSSepherosa Ziehau 		mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
21016c8d8eccSSepherosa Ziehau 		mii_args.mii_probemask = 1 << sc->bnx_phyno;
21026c8d8eccSSepherosa Ziehau 		mii_args.mii_capmask = capmask;
21036c8d8eccSSepherosa Ziehau 		mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
21046c8d8eccSSepherosa Ziehau 		mii_args.mii_priv = mii_priv;
21056c8d8eccSSepherosa Ziehau 
21066c8d8eccSSepherosa Ziehau 		error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
21076c8d8eccSSepherosa Ziehau 		if (error) {
21086c8d8eccSSepherosa Ziehau 			device_printf(dev, "MII without any PHY!\n");
21096c8d8eccSSepherosa Ziehau 			goto fail;
21106c8d8eccSSepherosa Ziehau 		}
21116c8d8eccSSepherosa Ziehau 	}
21126c8d8eccSSepherosa Ziehau 
21136c8d8eccSSepherosa Ziehau 	/*
21146c8d8eccSSepherosa Ziehau 	 * Create sysctl nodes.
21156c8d8eccSSepherosa Ziehau 	 */
21166c8d8eccSSepherosa Ziehau 	sysctl_ctx_init(&sc->bnx_sysctl_ctx);
21176c8d8eccSSepherosa Ziehau 	sc->bnx_sysctl_tree = SYSCTL_ADD_NODE(&sc->bnx_sysctl_ctx,
21186c8d8eccSSepherosa Ziehau 					      SYSCTL_STATIC_CHILDREN(_hw),
21196c8d8eccSSepherosa Ziehau 					      OID_AUTO,
21206c8d8eccSSepherosa Ziehau 					      device_get_nameunit(dev),
21216c8d8eccSSepherosa Ziehau 					      CTLFLAG_RD, 0, "");
21226c8d8eccSSepherosa Ziehau 	if (sc->bnx_sysctl_tree == NULL) {
21236c8d8eccSSepherosa Ziehau 		device_printf(dev, "can't add sysctl node\n");
21246c8d8eccSSepherosa Ziehau 		error = ENXIO;
21256c8d8eccSSepherosa Ziehau 		goto fail;
21266c8d8eccSSepherosa Ziehau 	}
21276c8d8eccSSepherosa Ziehau 
212893146551SSepherosa Ziehau 	SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
212993146551SSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
213093146551SSepherosa Ziehau 	    "rx_rings", CTLFLAG_RD, &sc->bnx_rx_retcnt, 0, "# of RX rings");
213193146551SSepherosa Ziehau 	SYSCTL_ADD_INT(&sc->bnx_sysctl_ctx,
213293146551SSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
213393146551SSepherosa Ziehau 	    "tx_rings", CTLFLAG_RD, &sc->bnx_tx_ringcnt, 0, "# of TX rings");
213493146551SSepherosa Ziehau 
21356c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21366c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21376c8d8eccSSepherosa Ziehau 			OID_AUTO, "rx_coal_ticks",
21386c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21396c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_ticks, "I",
21406c8d8eccSSepherosa Ziehau 			"Receive coalescing ticks (usec).");
21416c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21426c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21436c8d8eccSSepherosa Ziehau 			OID_AUTO, "tx_coal_ticks",
21446c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21456c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_ticks, "I",
21466c8d8eccSSepherosa Ziehau 			"Transmit coalescing ticks (usec).");
21476c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21486c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21496c8d8eccSSepherosa Ziehau 			OID_AUTO, "rx_coal_bds",
21506c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21516c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_bds, "I",
21526c8d8eccSSepherosa Ziehau 			"Receive max coalesced BD count.");
21536c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21546c8d8eccSSepherosa Ziehau 			SYSCTL_CHILDREN(sc->bnx_sysctl_tree),
21556c8d8eccSSepherosa Ziehau 			OID_AUTO, "tx_coal_bds",
21566c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
21576c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_bds, "I",
21586c8d8eccSSepherosa Ziehau 			"Transmit max coalesced BD count.");
21596c8d8eccSSepherosa Ziehau 	/*
21606c8d8eccSSepherosa Ziehau 	 * A common design characteristic for many Broadcom
21616c8d8eccSSepherosa Ziehau 	 * client controllers is that they only support a
21626c8d8eccSSepherosa Ziehau 	 * single outstanding DMA read operation on the PCIe
21636c8d8eccSSepherosa Ziehau 	 * bus. This means that it will take twice as long to
21646c8d8eccSSepherosa Ziehau 	 * fetch a TX frame that is split into header and
21656c8d8eccSSepherosa Ziehau 	 * payload buffers as it does to fetch a single,
21666c8d8eccSSepherosa Ziehau 	 * contiguous TX frame (2 reads vs. 1 read). For these
21676c8d8eccSSepherosa Ziehau 	 * controllers, coalescing buffers to reduce the number
21686c8d8eccSSepherosa Ziehau 	 * of memory reads is effective way to get maximum
21696c8d8eccSSepherosa Ziehau 	 * performance(about 940Mbps).  Without collapsing TX
21706c8d8eccSSepherosa Ziehau 	 * buffers the maximum TCP bulk transfer performance
21716c8d8eccSSepherosa Ziehau 	 * is about 850Mbps. However forcing coalescing mbufs
21726c8d8eccSSepherosa Ziehau 	 * consumes a lot of CPU cycles, so leave it off by
21736c8d8eccSSepherosa Ziehau 	 * default.
21746c8d8eccSSepherosa Ziehau 	 */
2175aad4de2bSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21766c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2177aad4de2bSSepherosa Ziehau 	    "force_defrag", CTLTYPE_INT | CTLFLAG_RW,
2178aad4de2bSSepherosa Ziehau 	    sc, 0, bnx_sysctl_force_defrag, "I",
21796c8d8eccSSepherosa Ziehau 	    "Force defragment on TX path");
21806c8d8eccSSepherosa Ziehau 
2181472c99c8SSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
2182c9b7f592SSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
2183472c99c8SSepherosa Ziehau 	    "tx_wreg", CTLTYPE_INT | CTLFLAG_RW,
2184472c99c8SSepherosa Ziehau 	    sc, 0, bnx_sysctl_tx_wreg, "I",
2185c9b7f592SSepherosa Ziehau 	    "# of segments before writing to hardware register");
2186c9b7f592SSepherosa Ziehau 
21876c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21886c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
21896c8d8eccSSepherosa Ziehau 	    "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
21906c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
21916c8d8eccSSepherosa Ziehau 	    "Receive max coalesced BD count during interrupt.");
21926c8d8eccSSepherosa Ziehau 	SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
21936c8d8eccSSepherosa Ziehau 	    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
21946c8d8eccSSepherosa Ziehau 	    "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
21956c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
21966c8d8eccSSepherosa Ziehau 	    "Transmit max coalesced BD count during interrupt.");
21976c8d8eccSSepherosa Ziehau 
21984fa38985SSepherosa Ziehau #ifdef IFPOLL_ENABLE
21994fa38985SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
22004fa38985SSepherosa Ziehau 		SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
22014fa38985SSepherosa Ziehau 		    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
22024fa38985SSepherosa Ziehau 		    "npoll_offset", CTLTYPE_INT | CTLFLAG_RW,
22034fa38985SSepherosa Ziehau 		    sc, 0, bnx_sysctl_npoll_offset, "I",
22044fa38985SSepherosa Ziehau 		    "NPOLLING cpu offset");
22054fa38985SSepherosa Ziehau 	} else {
22064fa38985SSepherosa Ziehau 		SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
22074fa38985SSepherosa Ziehau 		    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
22084fa38985SSepherosa Ziehau 		    "npoll_rxoff", CTLTYPE_INT | CTLFLAG_RW,
22094fa38985SSepherosa Ziehau 		    sc, 0, bnx_sysctl_npoll_rxoff, "I",
22104fa38985SSepherosa Ziehau 		    "NPOLLING RX cpu offset");
22114fa38985SSepherosa Ziehau 		SYSCTL_ADD_PROC(&sc->bnx_sysctl_ctx,
22124fa38985SSepherosa Ziehau 		    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
22134fa38985SSepherosa Ziehau 		    "npoll_txoff", CTLTYPE_INT | CTLFLAG_RW,
22144fa38985SSepherosa Ziehau 		    sc, 0, bnx_sysctl_npoll_txoff, "I",
22154fa38985SSepherosa Ziehau 		    "NPOLLING TX cpu offset");
22164fa38985SSepherosa Ziehau 	}
22174fa38985SSepherosa Ziehau #endif
22184fa38985SSepherosa Ziehau 
221966deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
222066deb1c1SSepherosa Ziehau 	for (i = 0; i < BNX_TSO_NSTATS; ++i) {
222166deb1c1SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
222266deb1c1SSepherosa Ziehau 		SYSCTL_ADD_ULONG(&sc->bnx_sysctl_ctx,
222366deb1c1SSepherosa Ziehau 		    SYSCTL_CHILDREN(sc->bnx_sysctl_tree), OID_AUTO,
222466deb1c1SSepherosa Ziehau 		    desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
222566deb1c1SSepherosa Ziehau 	}
222666deb1c1SSepherosa Ziehau #endif
222766deb1c1SSepherosa Ziehau 
22286c8d8eccSSepherosa Ziehau 	/*
22296c8d8eccSSepherosa Ziehau 	 * Call MI attach routine.
22306c8d8eccSSepherosa Ziehau 	 */
2231329f9016SSepherosa Ziehau 	ether_ifattach(ifp, ether_addr, NULL);
22326c8d8eccSSepherosa Ziehau 
2233329f9016SSepherosa Ziehau 	/* Setup TX rings and subqueues */
2234329f9016SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
2235329f9016SSepherosa Ziehau 		struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
2236329f9016SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
2237329f9016SSepherosa Ziehau 
2238329f9016SSepherosa Ziehau 		ifsq_set_cpuid(ifsq, txr->bnx_tx_cpuid);
2239329f9016SSepherosa Ziehau 		ifsq_set_hw_serialize(ifsq, &txr->bnx_tx_serialize);
2240329f9016SSepherosa Ziehau 		ifsq_set_priv(ifsq, txr);
22413397dea6SSepherosa Ziehau 		txr->bnx_ifsq = ifsq;
2242329f9016SSepherosa Ziehau 
22433397dea6SSepherosa Ziehau 		ifsq_watchdog_init(&txr->bnx_tx_watchdog, ifsq, bnx_watchdog);
2244329f9016SSepherosa Ziehau 	}
22454c77af2dSSepherosa Ziehau 
22460c7da01dSSepherosa Ziehau 	error = bnx_setup_intr(sc);
22476c8d8eccSSepherosa Ziehau 	if (error) {
22486c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
22496c8d8eccSSepherosa Ziehau 		goto fail;
22506c8d8eccSSepherosa Ziehau 	}
2251*7dbaa833SSepherosa Ziehau 	bnx_set_tick_cpuid(sc, FALSE);
22528ca0f604SSepherosa Ziehau 
22536c8d8eccSSepherosa Ziehau 	return(0);
22546c8d8eccSSepherosa Ziehau fail:
22556c8d8eccSSepherosa Ziehau 	bnx_detach(dev);
22566c8d8eccSSepherosa Ziehau 	return(error);
22576c8d8eccSSepherosa Ziehau }
22586c8d8eccSSepherosa Ziehau 
22596c8d8eccSSepherosa Ziehau static int
22606c8d8eccSSepherosa Ziehau bnx_detach(device_t dev)
22616c8d8eccSSepherosa Ziehau {
22626c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
22636c8d8eccSSepherosa Ziehau 
22646c8d8eccSSepherosa Ziehau 	if (device_is_attached(dev)) {
22656c8d8eccSSepherosa Ziehau 		struct ifnet *ifp = &sc->arpcom.ac_if;
22666c8d8eccSSepherosa Ziehau 
2267329f9016SSepherosa Ziehau 		ifnet_serialize_all(ifp);
22686c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
22696c8d8eccSSepherosa Ziehau 		bnx_reset(sc);
2270f33ac8a4SSepherosa Ziehau 		bnx_teardown_intr(sc, sc->bnx_intr_cnt);
2271329f9016SSepherosa Ziehau 		ifnet_deserialize_all(ifp);
22726c8d8eccSSepherosa Ziehau 
22736c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
22746c8d8eccSSepherosa Ziehau 	}
22756c8d8eccSSepherosa Ziehau 
22766c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
22776c8d8eccSSepherosa Ziehau 		ifmedia_removeall(&sc->bnx_ifmedia);
22786c8d8eccSSepherosa Ziehau 	if (sc->bnx_miibus)
22796c8d8eccSSepherosa Ziehau 		device_delete_child(dev, sc->bnx_miibus);
22806c8d8eccSSepherosa Ziehau 	bus_generic_detach(dev);
22816c8d8eccSSepherosa Ziehau 
22820c7da01dSSepherosa Ziehau 	bnx_free_intr(sc);
22836c8d8eccSSepherosa Ziehau 
22846c8d8eccSSepherosa Ziehau 	if (sc->bnx_res != NULL) {
22856c8d8eccSSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_MEMORY,
22866c8d8eccSSepherosa Ziehau 		    BGE_PCI_BAR0, sc->bnx_res);
22876c8d8eccSSepherosa Ziehau 	}
22886c8d8eccSSepherosa Ziehau 
22896c8d8eccSSepherosa Ziehau 	if (sc->bnx_sysctl_tree != NULL)
22906c8d8eccSSepherosa Ziehau 		sysctl_ctx_free(&sc->bnx_sysctl_ctx);
22916c8d8eccSSepherosa Ziehau 
22926c8d8eccSSepherosa Ziehau 	bnx_dma_free(sc);
22936c8d8eccSSepherosa Ziehau 
2294329f9016SSepherosa Ziehau 	if (sc->bnx_serialize != NULL)
2295329f9016SSepherosa Ziehau 		kfree(sc->bnx_serialize, M_DEVBUF);
2296329f9016SSepherosa Ziehau 
22976c8d8eccSSepherosa Ziehau 	return 0;
22986c8d8eccSSepherosa Ziehau }
22996c8d8eccSSepherosa Ziehau 
23006c8d8eccSSepherosa Ziehau static void
23016c8d8eccSSepherosa Ziehau bnx_reset(struct bnx_softc *sc)
23026c8d8eccSSepherosa Ziehau {
23036c8d8eccSSepherosa Ziehau 	device_t dev;
23046c8d8eccSSepherosa Ziehau 	uint32_t cachesize, command, pcistate, reset;
23056c8d8eccSSepherosa Ziehau 	void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
23066c8d8eccSSepherosa Ziehau 	int i, val = 0;
23076c8d8eccSSepherosa Ziehau 	uint16_t devctl;
23086c8d8eccSSepherosa Ziehau 
23096c8d8eccSSepherosa Ziehau 	dev = sc->bnx_dev;
23106c8d8eccSSepherosa Ziehau 
23116c8d8eccSSepherosa Ziehau 	write_op = bnx_writemem_direct;
23126c8d8eccSSepherosa Ziehau 
23136c8d8eccSSepherosa Ziehau 	/* Save some important PCI state. */
23146c8d8eccSSepherosa Ziehau 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
23156c8d8eccSSepherosa Ziehau 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
23166c8d8eccSSepherosa Ziehau 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
23176c8d8eccSSepherosa Ziehau 
23186c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
23196c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
23206c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
23216c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
23226c8d8eccSSepherosa Ziehau 
23236c8d8eccSSepherosa Ziehau 	/* Disable fastboot on controllers that support it. */
23246c8d8eccSSepherosa Ziehau 	if (bootverbose)
23256c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
23266c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
23276c8d8eccSSepherosa Ziehau 
23286c8d8eccSSepherosa Ziehau 	/*
23296c8d8eccSSepherosa Ziehau 	 * Write the magic number to SRAM at offset 0xB50.
23306c8d8eccSSepherosa Ziehau 	 * When firmware finishes its initialization it will
23316c8d8eccSSepherosa Ziehau 	 * write ~BGE_MAGIC_NUMBER to the same location.
23326c8d8eccSSepherosa Ziehau 	 */
23336c8d8eccSSepherosa Ziehau 	bnx_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
23346c8d8eccSSepherosa Ziehau 
23356c8d8eccSSepherosa Ziehau 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
23366c8d8eccSSepherosa Ziehau 
23376c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
23386c8d8eccSSepherosa Ziehau 	/* Force PCI-E 1.0a mode */
23393730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc) &&
23406c8d8eccSSepherosa Ziehau 	    CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
23416c8d8eccSSepherosa Ziehau 	    (BGE_PCIE_PHY_TSTCTL_PSCRAM |
23426c8d8eccSSepherosa Ziehau 	     BGE_PCIE_PHY_TSTCTL_PCIE10)) {
23436c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
23446c8d8eccSSepherosa Ziehau 		    BGE_PCIE_PHY_TSTCTL_PSCRAM);
23456c8d8eccSSepherosa Ziehau 	}
23466c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
23476c8d8eccSSepherosa Ziehau 		/* Prevent PCIE link training during global reset */
23486c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
23496c8d8eccSSepherosa Ziehau 		reset |= (1<<29);
23506c8d8eccSSepherosa Ziehau 	}
23516c8d8eccSSepherosa Ziehau 
23526c8d8eccSSepherosa Ziehau 	/*
23536c8d8eccSSepherosa Ziehau 	 * Set GPHY Power Down Override to leave GPHY
23546c8d8eccSSepherosa Ziehau 	 * powered up in D0 uninitialized.
23556c8d8eccSSepherosa Ziehau 	 */
23566c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
23576c8d8eccSSepherosa Ziehau 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
23586c8d8eccSSepherosa Ziehau 
23596c8d8eccSSepherosa Ziehau 	/* Issue global reset */
23606c8d8eccSSepherosa Ziehau 	write_op(sc, BGE_MISC_CFG, reset);
23616c8d8eccSSepherosa Ziehau 
23626c8d8eccSSepherosa Ziehau 	DELAY(1000);
23636c8d8eccSSepherosa Ziehau 
23646c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
23656c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
23666c8d8eccSSepherosa Ziehau 		uint32_t v;
23676c8d8eccSSepherosa Ziehau 
23686c8d8eccSSepherosa Ziehau 		DELAY(500000); /* wait for link training to complete */
23696c8d8eccSSepherosa Ziehau 		v = pci_read_config(dev, 0xc4, 4);
23706c8d8eccSSepherosa Ziehau 		pci_write_config(dev, 0xc4, v | (1<<15), 4);
23716c8d8eccSSepherosa Ziehau 	}
23726c8d8eccSSepherosa Ziehau 
23736c8d8eccSSepherosa Ziehau 	devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
23746c8d8eccSSepherosa Ziehau 
23756c8d8eccSSepherosa Ziehau 	/* Disable no snoop and disable relaxed ordering. */
23766c8d8eccSSepherosa Ziehau 	devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
23776c8d8eccSSepherosa Ziehau 
23786c8d8eccSSepherosa Ziehau 	/* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
23796c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
23806c8d8eccSSepherosa Ziehau 		devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
23816c8d8eccSSepherosa Ziehau 		devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
23826c8d8eccSSepherosa Ziehau 	}
23836c8d8eccSSepherosa Ziehau 
23846c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
23856c8d8eccSSepherosa Ziehau 	    devctl, 2);
23866c8d8eccSSepherosa Ziehau 
23876c8d8eccSSepherosa Ziehau 	/* Clear error status. */
23886c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
23896c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_CORR_ERR |
23906c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_NFATAL_ERR |
23916c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_FATAL_ERR |
23926c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_UNSUPP_REQ, 2);
23936c8d8eccSSepherosa Ziehau 
23946c8d8eccSSepherosa Ziehau 	/* Reset some of the PCI state that got zapped by reset */
23956c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
23966c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
23976c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
23986c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
23996c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
24006c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
24016c8d8eccSSepherosa Ziehau 	write_op(sc, BGE_MISC_CFG, (65 << 1));
24026c8d8eccSSepherosa Ziehau 
24036c8d8eccSSepherosa Ziehau 	/* Enable memory arbiter */
24046c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
24056c8d8eccSSepherosa Ziehau 
24066c8d8eccSSepherosa Ziehau 	/*
24076c8d8eccSSepherosa Ziehau 	 * Poll until we see the 1's complement of the magic number.
2408ddd93a5cSSepherosa Ziehau 	 * This indicates that the firmware initialization is complete.
24096c8d8eccSSepherosa Ziehau 	 */
24106c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
24116c8d8eccSSepherosa Ziehau 		val = bnx_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
24126c8d8eccSSepherosa Ziehau 		if (val == ~BGE_MAGIC_NUMBER)
24136c8d8eccSSepherosa Ziehau 			break;
24146c8d8eccSSepherosa Ziehau 		DELAY(10);
24156c8d8eccSSepherosa Ziehau 	}
24166c8d8eccSSepherosa Ziehau 	if (i == BNX_FIRMWARE_TIMEOUT) {
24176c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "firmware handshake "
24186c8d8eccSSepherosa Ziehau 			  "timed out, found 0x%08x\n", val);
24196c8d8eccSSepherosa Ziehau 	}
24206c8d8eccSSepherosa Ziehau 
24216c8d8eccSSepherosa Ziehau 	/* BCM57765 A0 needs additional time before accessing. */
24226c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
24236c8d8eccSSepherosa Ziehau 		DELAY(10 * 1000);
24246c8d8eccSSepherosa Ziehau 
24256c8d8eccSSepherosa Ziehau 	/*
24266c8d8eccSSepherosa Ziehau 	 * XXX Wait for the value of the PCISTATE register to
24276c8d8eccSSepherosa Ziehau 	 * return to its original pre-reset state. This is a
24286c8d8eccSSepherosa Ziehau 	 * fairly good indicator of reset completion. If we don't
24296c8d8eccSSepherosa Ziehau 	 * wait for the reset to fully complete, trying to read
24306c8d8eccSSepherosa Ziehau 	 * from the device's non-PCI registers may yield garbage
24316c8d8eccSSepherosa Ziehau 	 * results.
24326c8d8eccSSepherosa Ziehau 	 */
24336c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
24346c8d8eccSSepherosa Ziehau 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
24356c8d8eccSSepherosa Ziehau 			break;
24366c8d8eccSSepherosa Ziehau 		DELAY(10);
24376c8d8eccSSepherosa Ziehau 	}
24386c8d8eccSSepherosa Ziehau 
24396c8d8eccSSepherosa Ziehau 	/* Fix up byte swapping */
24406c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
24416c8d8eccSSepherosa Ziehau 
24426c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
24436c8d8eccSSepherosa Ziehau 
24446c8d8eccSSepherosa Ziehau 	/*
24456c8d8eccSSepherosa Ziehau 	 * The 5704 in TBI mode apparently needs some special
24466c8d8eccSSepherosa Ziehau 	 * adjustment to insure the SERDES drive level is set
24476c8d8eccSSepherosa Ziehau 	 * to 1.2V.
24486c8d8eccSSepherosa Ziehau 	 */
24496c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
24506c8d8eccSSepherosa Ziehau 	    (sc->bnx_flags & BNX_FLAG_TBI)) {
24516c8d8eccSSepherosa Ziehau 		uint32_t serdescfg;
24526c8d8eccSSepherosa Ziehau 
24536c8d8eccSSepherosa Ziehau 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
24546c8d8eccSSepherosa Ziehau 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
24556c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
24566c8d8eccSSepherosa Ziehau 	}
24576c8d8eccSSepherosa Ziehau 
24587892075dSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_MODE,
24597892075dSSepherosa Ziehau 	    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
24607892075dSSepherosa Ziehau 	DELAY(80);
24617892075dSSepherosa Ziehau 
24626c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
24633730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc)) {
24646c8d8eccSSepherosa Ziehau 		uint32_t v;
24656c8d8eccSSepherosa Ziehau 
24666c8d8eccSSepherosa Ziehau 		/* Enable Data FIFO protection. */
2467f1f34fc4SSepherosa Ziehau 		v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2468f1f34fc4SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
24696c8d8eccSSepherosa Ziehau 	}
24706c8d8eccSSepherosa Ziehau 
24716c8d8eccSSepherosa Ziehau 	DELAY(10000);
24726c8d8eccSSepherosa Ziehau 
24736c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
24746c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
24756c8d8eccSSepherosa Ziehau 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
24766c8d8eccSSepherosa Ziehau 	}
24776c8d8eccSSepherosa Ziehau }
24786c8d8eccSSepherosa Ziehau 
24796c8d8eccSSepherosa Ziehau /*
24806c8d8eccSSepherosa Ziehau  * Frame reception handling. This is called if there's a frame
24816c8d8eccSSepherosa Ziehau  * on the receive return list.
24826c8d8eccSSepherosa Ziehau  *
24836c8d8eccSSepherosa Ziehau  * Note: we have to be able to handle two possibilities here:
24846c8d8eccSSepherosa Ziehau  * 1) the frame is from the jumbo recieve ring
24856c8d8eccSSepherosa Ziehau  * 2) the frame is from the standard receive ring
24866c8d8eccSSepherosa Ziehau  */
24876c8d8eccSSepherosa Ziehau 
24886c8d8eccSSepherosa Ziehau static void
2489beedf5beSSepherosa Ziehau bnx_rxeof(struct bnx_rx_ret_ring *ret, uint16_t rx_prod, int count)
24906c8d8eccSSepherosa Ziehau {
2491beedf5beSSepherosa Ziehau 	struct bnx_softc *sc = ret->bnx_sc;
2492beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = ret->bnx_std;
2493beedf5beSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
24946c8d8eccSSepherosa Ziehau 	int stdcnt = 0, jumbocnt = 0;
24956c8d8eccSSepherosa Ziehau 
2496beedf5beSSepherosa Ziehau 	while (ret->bnx_rx_saved_considx != rx_prod && count != 0) {
24976c8d8eccSSepherosa Ziehau 		struct bge_rx_bd	*cur_rx;
24986c8d8eccSSepherosa Ziehau 		uint32_t		rxidx;
24996c8d8eccSSepherosa Ziehau 		struct mbuf		*m = NULL;
25006c8d8eccSSepherosa Ziehau 		uint16_t		vlan_tag = 0;
25016c8d8eccSSepherosa Ziehau 		int			have_tag = 0;
25026c8d8eccSSepherosa Ziehau 
250397381780SSepherosa Ziehau 		--count;
250497381780SSepherosa Ziehau 
2505beedf5beSSepherosa Ziehau 		cur_rx = &ret->bnx_rx_ret_ring[ret->bnx_rx_saved_considx];
25066c8d8eccSSepherosa Ziehau 
25076c8d8eccSSepherosa Ziehau 		rxidx = cur_rx->bge_idx;
2508beedf5beSSepherosa Ziehau 		BNX_INC(ret->bnx_rx_saved_considx, BNX_RETURN_RING_CNT);
25096c8d8eccSSepherosa Ziehau 
25106c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
25116c8d8eccSSepherosa Ziehau 			have_tag = 1;
25126c8d8eccSSepherosa Ziehau 			vlan_tag = cur_rx->bge_vlan_tag;
25136c8d8eccSSepherosa Ziehau 		}
25146c8d8eccSSepherosa Ziehau 
25156c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
25166c8d8eccSSepherosa Ziehau 			BNX_INC(sc->bnx_jumbo, BGE_JUMBO_RX_RING_CNT);
25176c8d8eccSSepherosa Ziehau 			jumbocnt++;
25186c8d8eccSSepherosa Ziehau 
25196c8d8eccSSepherosa Ziehau 			if (rxidx != sc->bnx_jumbo) {
2520d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
25216c8d8eccSSepherosa Ziehau 				if_printf(ifp, "sw jumbo index(%d) "
25226c8d8eccSSepherosa Ziehau 				    "and hw jumbo index(%d) mismatch, drop!\n",
25236c8d8eccSSepherosa Ziehau 				    sc->bnx_jumbo, rxidx);
25246c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, rxidx);
25256c8d8eccSSepherosa Ziehau 				continue;
25266c8d8eccSSepherosa Ziehau 			}
25276c8d8eccSSepherosa Ziehau 
2528beedf5beSSepherosa Ziehau 			m = sc->bnx_cdata.bnx_rx_jumbo_chain[rxidx].bnx_rx_mbuf;
25296c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2530d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
25316c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
25326c8d8eccSSepherosa Ziehau 				continue;
25336c8d8eccSSepherosa Ziehau 			}
25346c8d8eccSSepherosa Ziehau 			if (bnx_newbuf_jumbo(sc, sc->bnx_jumbo, 0)) {
2535d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
25366c8d8eccSSepherosa Ziehau 				bnx_setup_rxdesc_jumbo(sc, sc->bnx_jumbo);
25376c8d8eccSSepherosa Ziehau 				continue;
25386c8d8eccSSepherosa Ziehau 			}
25396c8d8eccSSepherosa Ziehau 		} else {
2540beedf5beSSepherosa Ziehau 			BNX_INC(std->bnx_rx_std, BGE_STD_RX_RING_CNT);
25416c8d8eccSSepherosa Ziehau 			stdcnt++;
25426c8d8eccSSepherosa Ziehau 
2543beedf5beSSepherosa Ziehau 			if (rxidx != std->bnx_rx_std) {
2544d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
25456c8d8eccSSepherosa Ziehau 				if_printf(ifp, "sw std index(%d) "
25466c8d8eccSSepherosa Ziehau 				    "and hw std index(%d) mismatch, drop!\n",
2547beedf5beSSepherosa Ziehau 				    std->bnx_rx_std, rxidx);
2548beedf5beSSepherosa Ziehau 				bnx_setup_rxdesc_std(std, rxidx);
25496c8d8eccSSepherosa Ziehau 				continue;
25506c8d8eccSSepherosa Ziehau 			}
25516c8d8eccSSepherosa Ziehau 
2552beedf5beSSepherosa Ziehau 			m = std->bnx_rx_std_buf[rxidx].bnx_rx_mbuf;
25536c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2554d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
2555beedf5beSSepherosa Ziehau 				bnx_setup_rxdesc_std(std, std->bnx_rx_std);
25566c8d8eccSSepherosa Ziehau 				continue;
25576c8d8eccSSepherosa Ziehau 			}
2558beedf5beSSepherosa Ziehau 			if (bnx_newbuf_std(ret, std->bnx_rx_std, 0)) {
2559d40991efSSepherosa Ziehau 				IFNET_STAT_INC(ifp, ierrors, 1);
2560beedf5beSSepherosa Ziehau 				bnx_setup_rxdesc_std(std, std->bnx_rx_std);
25616c8d8eccSSepherosa Ziehau 				continue;
25626c8d8eccSSepherosa Ziehau 			}
25636c8d8eccSSepherosa Ziehau 		}
25646c8d8eccSSepherosa Ziehau 
2565d40991efSSepherosa Ziehau 		IFNET_STAT_INC(ifp, ipackets, 1);
25666c8d8eccSSepherosa Ziehau 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
25676c8d8eccSSepherosa Ziehau 		m->m_pkthdr.rcvif = ifp;
25686c8d8eccSSepherosa Ziehau 
25696c8d8eccSSepherosa Ziehau 		if ((ifp->if_capenable & IFCAP_RXCSUM) &&
25706c8d8eccSSepherosa Ziehau 		    (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
25716c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
25726c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
25736c8d8eccSSepherosa Ziehau 				if ((cur_rx->bge_error_flag &
25746c8d8eccSSepherosa Ziehau 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
25756c8d8eccSSepherosa Ziehau 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
25766c8d8eccSSepherosa Ziehau 			}
25776c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
25786c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_data =
25796c8d8eccSSepherosa Ziehau 				    cur_rx->bge_tcp_udp_csum;
25806c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
25816c8d8eccSSepherosa Ziehau 				    CSUM_PSEUDO_HDR;
25826c8d8eccSSepherosa Ziehau 			}
25836c8d8eccSSepherosa Ziehau 		}
25846c8d8eccSSepherosa Ziehau 
25856c8d8eccSSepherosa Ziehau 		/*
25866c8d8eccSSepherosa Ziehau 		 * If we received a packet with a vlan tag, pass it
25876c8d8eccSSepherosa Ziehau 		 * to vlan_input() instead of ether_input().
25886c8d8eccSSepherosa Ziehau 		 */
25896c8d8eccSSepherosa Ziehau 		if (have_tag) {
25906c8d8eccSSepherosa Ziehau 			m->m_flags |= M_VLANTAG;
25916c8d8eccSSepherosa Ziehau 			m->m_pkthdr.ether_vlantag = vlan_tag;
25926c8d8eccSSepherosa Ziehau 		}
25936c8d8eccSSepherosa Ziehau 		ifp->if_input(ifp, m);
25946c8d8eccSSepherosa Ziehau 	}
25956c8d8eccSSepherosa Ziehau 
2596beedf5beSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_CONS0_LO, ret->bnx_rx_saved_considx);
25976c8d8eccSSepherosa Ziehau 	if (stdcnt)
2598beedf5beSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, std->bnx_rx_std);
25996c8d8eccSSepherosa Ziehau 	if (jumbocnt)
26006c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
26016c8d8eccSSepherosa Ziehau }
26026c8d8eccSSepherosa Ziehau 
26036c8d8eccSSepherosa Ziehau static void
260433a04907SSepherosa Ziehau bnx_txeof(struct bnx_tx_ring *txr, uint16_t tx_cons)
26056c8d8eccSSepherosa Ziehau {
260633a04907SSepherosa Ziehau 	struct ifnet *ifp = &txr->bnx_sc->arpcom.ac_if;
26076c8d8eccSSepherosa Ziehau 
26086c8d8eccSSepherosa Ziehau 	/*
26096c8d8eccSSepherosa Ziehau 	 * Go through our tx ring and free mbufs for those
26106c8d8eccSSepherosa Ziehau 	 * frames that have been sent.
26116c8d8eccSSepherosa Ziehau 	 */
261233a04907SSepherosa Ziehau 	while (txr->bnx_tx_saved_considx != tx_cons) {
2613fa4b1067SSepherosa Ziehau 		struct bnx_tx_buf *buf;
26146c8d8eccSSepherosa Ziehau 		uint32_t idx = 0;
26156c8d8eccSSepherosa Ziehau 
261633a04907SSepherosa Ziehau 		idx = txr->bnx_tx_saved_considx;
2617fa4b1067SSepherosa Ziehau 		buf = &txr->bnx_tx_buf[idx];
2618fa4b1067SSepherosa Ziehau 		if (buf->bnx_tx_mbuf != NULL) {
2619d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, opackets, 1);
262033a04907SSepherosa Ziehau 			bus_dmamap_unload(txr->bnx_tx_mtag,
2621fa4b1067SSepherosa Ziehau 			    buf->bnx_tx_dmamap);
2622fa4b1067SSepherosa Ziehau 			m_freem(buf->bnx_tx_mbuf);
2623fa4b1067SSepherosa Ziehau 			buf->bnx_tx_mbuf = NULL;
26246c8d8eccSSepherosa Ziehau 		}
2625fa639b88SSepherosa Ziehau 		txr->bnx_tx_cnt--;
262633a04907SSepherosa Ziehau 		BNX_INC(txr->bnx_tx_saved_considx, BGE_TX_RING_CNT);
26276c8d8eccSSepherosa Ziehau 	}
26286c8d8eccSSepherosa Ziehau 
2629fa639b88SSepherosa Ziehau 	if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) >=
26306c8d8eccSSepherosa Ziehau 	    (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
26313397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
26326c8d8eccSSepherosa Ziehau 
2633fa639b88SSepherosa Ziehau 	if (txr->bnx_tx_cnt == 0)
26343397dea6SSepherosa Ziehau 		txr->bnx_tx_watchdog.wd_timer = 0;
26356c8d8eccSSepherosa Ziehau 
26363397dea6SSepherosa Ziehau 	if (!ifsq_is_empty(txr->bnx_ifsq))
26373397dea6SSepherosa Ziehau 		ifsq_devstart(txr->bnx_ifsq);
26386c8d8eccSSepherosa Ziehau }
26396c8d8eccSSepherosa Ziehau 
264039a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
26416c8d8eccSSepherosa Ziehau 
26426c8d8eccSSepherosa Ziehau static void
26434fa38985SSepherosa Ziehau bnx_npoll_rx(struct ifnet *ifp __unused, void *xret, int cycle)
26444fa38985SSepherosa Ziehau {
26454fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = xret;
26464fa38985SSepherosa Ziehau 	uint16_t rx_prod;
26474fa38985SSepherosa Ziehau 
26484fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
26494fa38985SSepherosa Ziehau 
26504fa38985SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
26514fa38985SSepherosa Ziehau 	cpu_lfence();
26524fa38985SSepherosa Ziehau 
26534fa38985SSepherosa Ziehau 	rx_prod = *ret->bnx_rx_considx;
26544fa38985SSepherosa Ziehau 	if (ret->bnx_rx_saved_considx != rx_prod)
26554fa38985SSepherosa Ziehau 		bnx_rxeof(ret, rx_prod, cycle);
26564fa38985SSepherosa Ziehau }
26574fa38985SSepherosa Ziehau 
26584fa38985SSepherosa Ziehau static void
26594fa38985SSepherosa Ziehau bnx_npoll_tx(struct ifnet *ifp __unused, void *xtxr, int cycle __unused)
26604fa38985SSepherosa Ziehau {
26614fa38985SSepherosa Ziehau 	struct bnx_tx_ring *txr = xtxr;
26624fa38985SSepherosa Ziehau 	uint16_t tx_cons;
26634fa38985SSepherosa Ziehau 
26644fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
26654fa38985SSepherosa Ziehau 
26664fa38985SSepherosa Ziehau 	tx_cons = *txr->bnx_tx_considx;
26674fa38985SSepherosa Ziehau 	if (txr->bnx_tx_saved_considx != tx_cons)
26684fa38985SSepherosa Ziehau 		bnx_txeof(txr, tx_cons);
26694fa38985SSepherosa Ziehau }
26704fa38985SSepherosa Ziehau 
26714fa38985SSepherosa Ziehau static void
26724fa38985SSepherosa Ziehau bnx_npoll_status(struct ifnet *ifp)
26734fa38985SSepherosa Ziehau {
26744fa38985SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
26754fa38985SSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
26764fa38985SSepherosa Ziehau 
26774fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
26784fa38985SSepherosa Ziehau 
26794fa38985SSepherosa Ziehau 	if ((sblk->bge_status & BGE_STATFLAG_LINKSTATE_CHANGED) ||
26804fa38985SSepherosa Ziehau 	    sc->bnx_link_evt)
26814fa38985SSepherosa Ziehau 		bnx_link_poll(sc);
26824fa38985SSepherosa Ziehau }
26834fa38985SSepherosa Ziehau 
26844fa38985SSepherosa Ziehau static void
268539a8d43aSSepherosa Ziehau bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
268639a8d43aSSepherosa Ziehau {
268739a8d43aSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
26884fa38985SSepherosa Ziehau 	int i;
268939a8d43aSSepherosa Ziehau 
2690329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
269139a8d43aSSepherosa Ziehau 
269239a8d43aSSepherosa Ziehau 	if (info != NULL) {
26934fa38985SSepherosa Ziehau 		/*
26944fa38985SSepherosa Ziehau 		 * TODO handle RXTX bundle and non-bundle
26954fa38985SSepherosa Ziehau 		 */
26964fa38985SSepherosa Ziehau 		info->ifpi_status.status_func = bnx_npoll_status;
26974fa38985SSepherosa Ziehau 		info->ifpi_status.serializer = &sc->bnx_main_serialize;
269839a8d43aSSepherosa Ziehau 
26994fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
27004fa38985SSepherosa Ziehau 			struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
27014fa38985SSepherosa Ziehau 			int idx = i + sc->bnx_npoll_txoff;
27024fa38985SSepherosa Ziehau 
27034fa38985SSepherosa Ziehau 			KKASSERT(idx < ncpus2);
27044fa38985SSepherosa Ziehau 			info->ifpi_tx[idx].poll_func = bnx_npoll_tx;
27054fa38985SSepherosa Ziehau 			info->ifpi_tx[idx].arg = txr;
27064fa38985SSepherosa Ziehau 			info->ifpi_tx[idx].serializer = &txr->bnx_tx_serialize;
27074fa38985SSepherosa Ziehau 			ifsq_set_cpuid(txr->bnx_ifsq, idx);
27084fa38985SSepherosa Ziehau 		}
27094fa38985SSepherosa Ziehau 
27104fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
27114fa38985SSepherosa Ziehau 			struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
27124fa38985SSepherosa Ziehau 			int idx = i + sc->bnx_npoll_rxoff;
27134fa38985SSepherosa Ziehau 
27144fa38985SSepherosa Ziehau 			KKASSERT(idx < ncpus2);
27154fa38985SSepherosa Ziehau 			info->ifpi_rx[idx].poll_func = bnx_npoll_rx;
27164fa38985SSepherosa Ziehau 			info->ifpi_rx[idx].arg = ret;
27174fa38985SSepherosa Ziehau 			info->ifpi_rx[idx].serializer =
27184fa38985SSepherosa Ziehau 			    &ret->bnx_rx_ret_serialize;
27194fa38985SSepherosa Ziehau 		}
272039a8d43aSSepherosa Ziehau 
2721*7dbaa833SSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING) {
272239a8d43aSSepherosa Ziehau 			bnx_disable_intr(sc);
2723*7dbaa833SSepherosa Ziehau 			bnx_set_tick_cpuid(sc, TRUE);
2724*7dbaa833SSepherosa Ziehau 		}
272539a8d43aSSepherosa Ziehau 	} else {
27264fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
27274fa38985SSepherosa Ziehau 			ifsq_set_cpuid(sc->bnx_tx_ring[i].bnx_ifsq,
27284fa38985SSepherosa Ziehau 			    sc->bnx_tx_ring[i].bnx_tx_cpuid);
27294fa38985SSepherosa Ziehau 		}
2730*7dbaa833SSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING) {
273139a8d43aSSepherosa Ziehau 			bnx_enable_intr(sc);
2732*7dbaa833SSepherosa Ziehau 			bnx_set_tick_cpuid(sc, FALSE);
2733*7dbaa833SSepherosa Ziehau 		}
273439a8d43aSSepherosa Ziehau 	}
273539a8d43aSSepherosa Ziehau }
273639a8d43aSSepherosa Ziehau 
273739a8d43aSSepherosa Ziehau #endif	/* IFPOLL_ENABLE */
27386c8d8eccSSepherosa Ziehau 
27396c8d8eccSSepherosa Ziehau static void
27406c8d8eccSSepherosa Ziehau bnx_intr_legacy(void *xsc)
27416c8d8eccSSepherosa Ziehau {
27426c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
27434fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
27446c8d8eccSSepherosa Ziehau 
27454fa38985SSepherosa Ziehau 	if (ret->bnx_saved_status_tag == *ret->bnx_hw_status_tag) {
27466c8d8eccSSepherosa Ziehau 		uint32_t val;
27476c8d8eccSSepherosa Ziehau 
27486c8d8eccSSepherosa Ziehau 		val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
27496c8d8eccSSepherosa Ziehau 		if (val & BGE_PCISTAT_INTR_NOTACT)
27506c8d8eccSSepherosa Ziehau 			return;
27516c8d8eccSSepherosa Ziehau 	}
27526c8d8eccSSepherosa Ziehau 
27536c8d8eccSSepherosa Ziehau 	/*
27546c8d8eccSSepherosa Ziehau 	 * NOTE:
27556c8d8eccSSepherosa Ziehau 	 * Interrupt will have to be disabled if tagged status
27566c8d8eccSSepherosa Ziehau 	 * is used, else interrupt will always be asserted on
27576c8d8eccSSepherosa Ziehau 	 * certain chips (at least on BCM5750 AX/BX).
27586c8d8eccSSepherosa Ziehau 	 */
27596c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
27606c8d8eccSSepherosa Ziehau 
27616c8d8eccSSepherosa Ziehau 	bnx_intr(sc);
27626c8d8eccSSepherosa Ziehau }
27636c8d8eccSSepherosa Ziehau 
27646c8d8eccSSepherosa Ziehau static void
27656c8d8eccSSepherosa Ziehau bnx_msi(void *xsc)
27666c8d8eccSSepherosa Ziehau {
27676c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
27686c8d8eccSSepherosa Ziehau 
27696c8d8eccSSepherosa Ziehau 	/* Disable interrupt first */
27706c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
27716c8d8eccSSepherosa Ziehau 	bnx_intr(sc);
27726c8d8eccSSepherosa Ziehau }
27736c8d8eccSSepherosa Ziehau 
27746c8d8eccSSepherosa Ziehau static void
27756c8d8eccSSepherosa Ziehau bnx_msi_oneshot(void *xsc)
27766c8d8eccSSepherosa Ziehau {
27776c8d8eccSSepherosa Ziehau 	bnx_intr(xsc);
27786c8d8eccSSepherosa Ziehau }
27796c8d8eccSSepherosa Ziehau 
27806c8d8eccSSepherosa Ziehau static void
27816c8d8eccSSepherosa Ziehau bnx_intr(struct bnx_softc *sc)
27826c8d8eccSSepherosa Ziehau {
27836c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
27844fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
27856c8d8eccSSepherosa Ziehau 	struct bge_status_block *sblk = sc->bnx_ldata.bnx_status_block;
27866c8d8eccSSepherosa Ziehau 	uint32_t status;
27876c8d8eccSSepherosa Ziehau 
2788329f9016SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
2789329f9016SSepherosa Ziehau 
27904fa38985SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
27916c8d8eccSSepherosa Ziehau 	/*
27926c8d8eccSSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
27936c8d8eccSSepherosa Ziehau 	 * before rx_prod, tx_cons and status.
27946c8d8eccSSepherosa Ziehau 	 */
27956c8d8eccSSepherosa Ziehau 	cpu_lfence();
27966c8d8eccSSepherosa Ziehau 
27976c8d8eccSSepherosa Ziehau 	status = sblk->bge_status;
27986c8d8eccSSepherosa Ziehau 
27996c8d8eccSSepherosa Ziehau 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt)
28006c8d8eccSSepherosa Ziehau 		bnx_link_poll(sc);
28016c8d8eccSSepherosa Ziehau 
28026c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_RUNNING) {
28034fa38985SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
28043a16b7b8SSepherosa Ziehau 		uint16_t rx_prod, tx_cons;
28053a16b7b8SSepherosa Ziehau 
2806329f9016SSepherosa Ziehau 		lwkt_serialize_enter(&ret->bnx_rx_ret_serialize);
28073a16b7b8SSepherosa Ziehau 		rx_prod = *ret->bnx_rx_considx;
2808beedf5beSSepherosa Ziehau 		if (ret->bnx_rx_saved_considx != rx_prod)
2809beedf5beSSepherosa Ziehau 			bnx_rxeof(ret, rx_prod, -1);
2810329f9016SSepherosa Ziehau 		lwkt_serialize_exit(&ret->bnx_rx_ret_serialize);
28116c8d8eccSSepherosa Ziehau 
2812329f9016SSepherosa Ziehau 		lwkt_serialize_enter(&txr->bnx_tx_serialize);
2813329f9016SSepherosa Ziehau 		tx_cons = *txr->bnx_tx_considx;
281433a04907SSepherosa Ziehau 		if (txr->bnx_tx_saved_considx != tx_cons)
281533a04907SSepherosa Ziehau 			bnx_txeof(txr, tx_cons);
2816329f9016SSepherosa Ziehau 		lwkt_serialize_exit(&txr->bnx_tx_serialize);
28176c8d8eccSSepherosa Ziehau 	}
28186c8d8eccSSepherosa Ziehau 
28194fa38985SSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, ret->bnx_saved_status_tag << 24);
28206c8d8eccSSepherosa Ziehau }
28216c8d8eccSSepherosa Ziehau 
28226c8d8eccSSepherosa Ziehau static void
28236c8d8eccSSepherosa Ziehau bnx_tick(void *xsc)
28246c8d8eccSSepherosa Ziehau {
28256c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
28266c8d8eccSSepherosa Ziehau 
2827329f9016SSepherosa Ziehau 	lwkt_serialize_enter(&sc->bnx_main_serialize);
28286c8d8eccSSepherosa Ziehau 
28296c8d8eccSSepherosa Ziehau 	bnx_stats_update_regs(sc);
28306c8d8eccSSepherosa Ziehau 
28316c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
28326c8d8eccSSepherosa Ziehau 		/*
28336c8d8eccSSepherosa Ziehau 		 * Since in TBI mode auto-polling can't be used we should poll
28346c8d8eccSSepherosa Ziehau 		 * link status manually. Here we register pending link event
28356c8d8eccSSepherosa Ziehau 		 * and trigger interrupt.
28366c8d8eccSSepherosa Ziehau 		 */
28376c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
28386c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
28396c8d8eccSSepherosa Ziehau 	} else if (!sc->bnx_link) {
28406c8d8eccSSepherosa Ziehau 		mii_tick(device_get_softc(sc->bnx_miibus));
28416c8d8eccSSepherosa Ziehau 	}
28426c8d8eccSSepherosa Ziehau 
2843*7dbaa833SSepherosa Ziehau 	callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
2844*7dbaa833SSepherosa Ziehau 	    sc->bnx_tick_cpuid);
28456c8d8eccSSepherosa Ziehau 
2846329f9016SSepherosa Ziehau 	lwkt_serialize_exit(&sc->bnx_main_serialize);
28476c8d8eccSSepherosa Ziehau }
28486c8d8eccSSepherosa Ziehau 
28496c8d8eccSSepherosa Ziehau static void
28506c8d8eccSSepherosa Ziehau bnx_stats_update_regs(struct bnx_softc *sc)
28516c8d8eccSSepherosa Ziehau {
28526c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
28536c8d8eccSSepherosa Ziehau 	struct bge_mac_stats_regs stats;
28546c8d8eccSSepherosa Ziehau 	uint32_t *s;
28556c8d8eccSSepherosa Ziehau 	int i;
28566c8d8eccSSepherosa Ziehau 
28576c8d8eccSSepherosa Ziehau 	s = (uint32_t *)&stats;
28586c8d8eccSSepherosa Ziehau 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28596c8d8eccSSepherosa Ziehau 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28606c8d8eccSSepherosa Ziehau 		s++;
28616c8d8eccSSepherosa Ziehau 	}
28626c8d8eccSSepherosa Ziehau 
2863d40991efSSepherosa Ziehau 	IFNET_STAT_SET(ifp, collisions,
28646c8d8eccSSepherosa Ziehau 	   (stats.dot3StatsSingleCollisionFrames +
28656c8d8eccSSepherosa Ziehau 	   stats.dot3StatsMultipleCollisionFrames +
28666c8d8eccSSepherosa Ziehau 	   stats.dot3StatsExcessiveCollisions +
2867d40991efSSepherosa Ziehau 	   stats.dot3StatsLateCollisions));
28686c8d8eccSSepherosa Ziehau }
28696c8d8eccSSepherosa Ziehau 
28706c8d8eccSSepherosa Ziehau /*
28716c8d8eccSSepherosa Ziehau  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
28726c8d8eccSSepherosa Ziehau  * pointers to descriptors.
28736c8d8eccSSepherosa Ziehau  */
28746c8d8eccSSepherosa Ziehau static int
287533a04907SSepherosa Ziehau bnx_encap(struct bnx_tx_ring *txr, struct mbuf **m_head0, uint32_t *txidx,
2876c9b7f592SSepherosa Ziehau     int *segs_used)
28776c8d8eccSSepherosa Ziehau {
28786c8d8eccSSepherosa Ziehau 	struct bge_tx_bd *d = NULL;
287966deb1c1SSepherosa Ziehau 	uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
28806c8d8eccSSepherosa Ziehau 	bus_dma_segment_t segs[BNX_NSEG_NEW];
28816c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
28826c8d8eccSSepherosa Ziehau 	int error, maxsegs, nsegs, idx, i;
28836c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = *m_head0, *m_new;
28846c8d8eccSSepherosa Ziehau 
288566deb1c1SSepherosa Ziehau 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
288666deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
288766deb1c1SSepherosa Ziehau 		int tso_nsegs;
288866deb1c1SSepherosa Ziehau #endif
288966deb1c1SSepherosa Ziehau 
289033a04907SSepherosa Ziehau 		error = bnx_setup_tso(txr, m_head0, &mss, &csum_flags);
289166deb1c1SSepherosa Ziehau 		if (error)
289266deb1c1SSepherosa Ziehau 			return error;
289366deb1c1SSepherosa Ziehau 		m_head = *m_head0;
289466deb1c1SSepherosa Ziehau 
289566deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
2896f0336d39SSepherosa Ziehau 		tso_nsegs = (m_head->m_pkthdr.len /
2897f0336d39SSepherosa Ziehau 		    m_head->m_pkthdr.tso_segsz) - 1;
289866deb1c1SSepherosa Ziehau 		if (tso_nsegs > (BNX_TSO_NSTATS - 1))
289966deb1c1SSepherosa Ziehau 			tso_nsegs = BNX_TSO_NSTATS - 1;
290066deb1c1SSepherosa Ziehau 		else if (tso_nsegs < 0)
290166deb1c1SSepherosa Ziehau 			tso_nsegs = 0;
29025a0c3c3aSSascha Wildner 		txr->bnx_sc->bnx_tsosegs[tso_nsegs]++;
290366deb1c1SSepherosa Ziehau #endif
290466deb1c1SSepherosa Ziehau 	} else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
29056c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
29066c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
29076c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
29086c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
29096c8d8eccSSepherosa Ziehau 		if (m_head->m_flags & M_LASTFRAG)
29106c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
29116c8d8eccSSepherosa Ziehau 		else if (m_head->m_flags & M_FRAG)
29126c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
29136c8d8eccSSepherosa Ziehau 	}
291466deb1c1SSepherosa Ziehau 	if (m_head->m_flags & M_VLANTAG) {
291566deb1c1SSepherosa Ziehau 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
291666deb1c1SSepherosa Ziehau 		vlan_tag = m_head->m_pkthdr.ether_vlantag;
291766deb1c1SSepherosa Ziehau 	}
29186c8d8eccSSepherosa Ziehau 
29196c8d8eccSSepherosa Ziehau 	idx = *txidx;
2920fa4b1067SSepherosa Ziehau 	map = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
29216c8d8eccSSepherosa Ziehau 
2922fa639b88SSepherosa Ziehau 	maxsegs = (BGE_TX_RING_CNT - txr->bnx_tx_cnt) - BNX_NSEG_RSVD;
29236c8d8eccSSepherosa Ziehau 	KASSERT(maxsegs >= BNX_NSEG_SPARE,
29246c8d8eccSSepherosa Ziehau 		("not enough segments %d", maxsegs));
29256c8d8eccSSepherosa Ziehau 
29266c8d8eccSSepherosa Ziehau 	if (maxsegs > BNX_NSEG_NEW)
29276c8d8eccSSepherosa Ziehau 		maxsegs = BNX_NSEG_NEW;
29286c8d8eccSSepherosa Ziehau 
29296c8d8eccSSepherosa Ziehau 	/*
29306c8d8eccSSepherosa Ziehau 	 * Pad outbound frame to BGE_MIN_FRAMELEN for an unusual reason.
29316c8d8eccSSepherosa Ziehau 	 * The bge hardware will pad out Tx runts to BGE_MIN_FRAMELEN,
29326c8d8eccSSepherosa Ziehau 	 * but when such padded frames employ the bge IP/TCP checksum
29336c8d8eccSSepherosa Ziehau 	 * offload, the hardware checksum assist gives incorrect results
29346c8d8eccSSepherosa Ziehau 	 * (possibly from incorporating its own padding into the UDP/TCP
29356c8d8eccSSepherosa Ziehau 	 * checksum; who knows).  If we pad such runts with zeros, the
29366c8d8eccSSepherosa Ziehau 	 * onboard checksum comes out correct.
29376c8d8eccSSepherosa Ziehau 	 */
29386c8d8eccSSepherosa Ziehau 	if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
29396c8d8eccSSepherosa Ziehau 	    m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
29406c8d8eccSSepherosa Ziehau 		error = m_devpad(m_head, BNX_MIN_FRAMELEN);
29416c8d8eccSSepherosa Ziehau 		if (error)
29426c8d8eccSSepherosa Ziehau 			goto back;
29436c8d8eccSSepherosa Ziehau 	}
29446c8d8eccSSepherosa Ziehau 
294579a64343SSepherosa Ziehau 	if ((txr->bnx_tx_flags & BNX_TX_FLAG_SHORTDMA) &&
294633a04907SSepherosa Ziehau 	    m_head->m_next != NULL) {
29476c8d8eccSSepherosa Ziehau 		m_new = bnx_defrag_shortdma(m_head);
29486c8d8eccSSepherosa Ziehau 		if (m_new == NULL) {
29496c8d8eccSSepherosa Ziehau 			error = ENOBUFS;
29506c8d8eccSSepherosa Ziehau 			goto back;
29516c8d8eccSSepherosa Ziehau 		}
29526c8d8eccSSepherosa Ziehau 		*m_head0 = m_head = m_new;
29536c8d8eccSSepherosa Ziehau 	}
295466deb1c1SSepherosa Ziehau 	if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
2955aad4de2bSSepherosa Ziehau 	    (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG) &&
2956aad4de2bSSepherosa Ziehau 	    m_head->m_next != NULL) {
29576c8d8eccSSepherosa Ziehau 		/*
29586c8d8eccSSepherosa Ziehau 		 * Forcefully defragment mbuf chain to overcome hardware
29596c8d8eccSSepherosa Ziehau 		 * limitation which only support a single outstanding
29606c8d8eccSSepherosa Ziehau 		 * DMA read operation.  If it fails, keep moving on using
29616c8d8eccSSepherosa Ziehau 		 * the original mbuf chain.
29626c8d8eccSSepherosa Ziehau 		 */
29636c8d8eccSSepherosa Ziehau 		m_new = m_defrag(m_head, MB_DONTWAIT);
29646c8d8eccSSepherosa Ziehau 		if (m_new != NULL)
29656c8d8eccSSepherosa Ziehau 			*m_head0 = m_head = m_new;
29666c8d8eccSSepherosa Ziehau 	}
29676c8d8eccSSepherosa Ziehau 
296833a04907SSepherosa Ziehau 	error = bus_dmamap_load_mbuf_defrag(txr->bnx_tx_mtag, map,
29696c8d8eccSSepherosa Ziehau 	    m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
29706c8d8eccSSepherosa Ziehau 	if (error)
29716c8d8eccSSepherosa Ziehau 		goto back;
2972c9b7f592SSepherosa Ziehau 	*segs_used += nsegs;
29736c8d8eccSSepherosa Ziehau 
29746c8d8eccSSepherosa Ziehau 	m_head = *m_head0;
297533a04907SSepherosa Ziehau 	bus_dmamap_sync(txr->bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
29766c8d8eccSSepherosa Ziehau 
29776c8d8eccSSepherosa Ziehau 	for (i = 0; ; i++) {
297833a04907SSepherosa Ziehau 		d = &txr->bnx_tx_ring[idx];
29796c8d8eccSSepherosa Ziehau 
29806c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
29816c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
29826c8d8eccSSepherosa Ziehau 		d->bge_len = segs[i].ds_len;
29836c8d8eccSSepherosa Ziehau 		d->bge_flags = csum_flags;
298466deb1c1SSepherosa Ziehau 		d->bge_vlan_tag = vlan_tag;
298566deb1c1SSepherosa Ziehau 		d->bge_mss = mss;
29866c8d8eccSSepherosa Ziehau 
29876c8d8eccSSepherosa Ziehau 		if (i == nsegs - 1)
29886c8d8eccSSepherosa Ziehau 			break;
29896c8d8eccSSepherosa Ziehau 		BNX_INC(idx, BGE_TX_RING_CNT);
29906c8d8eccSSepherosa Ziehau 	}
29916c8d8eccSSepherosa Ziehau 	/* Mark the last segment as end of packet... */
29926c8d8eccSSepherosa Ziehau 	d->bge_flags |= BGE_TXBDFLAG_END;
29936c8d8eccSSepherosa Ziehau 
29946c8d8eccSSepherosa Ziehau 	/*
29956c8d8eccSSepherosa Ziehau 	 * Insure that the map for this transmission is placed at
29966c8d8eccSSepherosa Ziehau 	 * the array index of the last descriptor in this chain.
29976c8d8eccSSepherosa Ziehau 	 */
2998fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[*txidx].bnx_tx_dmamap = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
2999fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[idx].bnx_tx_dmamap = map;
3000fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[idx].bnx_tx_mbuf = m_head;
3001fa639b88SSepherosa Ziehau 	txr->bnx_tx_cnt += nsegs;
30026c8d8eccSSepherosa Ziehau 
30036c8d8eccSSepherosa Ziehau 	BNX_INC(idx, BGE_TX_RING_CNT);
30046c8d8eccSSepherosa Ziehau 	*txidx = idx;
30056c8d8eccSSepherosa Ziehau back:
30066c8d8eccSSepherosa Ziehau 	if (error) {
30076c8d8eccSSepherosa Ziehau 		m_freem(*m_head0);
30086c8d8eccSSepherosa Ziehau 		*m_head0 = NULL;
30096c8d8eccSSepherosa Ziehau 	}
30106c8d8eccSSepherosa Ziehau 	return error;
30116c8d8eccSSepherosa Ziehau }
30126c8d8eccSSepherosa Ziehau 
30136c8d8eccSSepherosa Ziehau /*
30146c8d8eccSSepherosa Ziehau  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
30156c8d8eccSSepherosa Ziehau  * to the mbuf data regions directly in the transmit descriptors.
30166c8d8eccSSepherosa Ziehau  */
30176c8d8eccSSepherosa Ziehau static void
3018f0a26983SSepherosa Ziehau bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
30196c8d8eccSSepherosa Ziehau {
30203397dea6SSepherosa Ziehau 	struct bnx_tx_ring *txr = ifsq_get_priv(ifsq);
30216c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = NULL;
30226c8d8eccSSepherosa Ziehau 	uint32_t prodidx;
3023c9b7f592SSepherosa Ziehau 	int nsegs = 0;
30246c8d8eccSSepherosa Ziehau 
30253397dea6SSepherosa Ziehau 	KKASSERT(txr->bnx_ifsq == ifsq);
3026329f9016SSepherosa Ziehau 	ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
3027f0a26983SSepherosa Ziehau 
30283397dea6SSepherosa Ziehau 	if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
30296c8d8eccSSepherosa Ziehau 		return;
30306c8d8eccSSepherosa Ziehau 
303133a04907SSepherosa Ziehau 	prodidx = txr->bnx_tx_prodidx;
30326c8d8eccSSepherosa Ziehau 
3033fa4b1067SSepherosa Ziehau 	while (txr->bnx_tx_buf[prodidx].bnx_tx_mbuf == NULL) {
30346c8d8eccSSepherosa Ziehau 		/*
30356c8d8eccSSepherosa Ziehau 		 * Sanity check: avoid coming within BGE_NSEG_RSVD
30366c8d8eccSSepherosa Ziehau 		 * descriptors of the end of the ring.  Also make
30376c8d8eccSSepherosa Ziehau 		 * sure there are BGE_NSEG_SPARE descriptors for
3038a1bd58c9SSepherosa Ziehau 		 * jumbo buffers' or TSO segments' defragmentation.
30396c8d8eccSSepherosa Ziehau 		 */
3040fa639b88SSepherosa Ziehau 		if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) <
30416c8d8eccSSepherosa Ziehau 		    (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
30423397dea6SSepherosa Ziehau 			ifsq_set_oactive(ifsq);
30436c8d8eccSSepherosa Ziehau 			break;
30446c8d8eccSSepherosa Ziehau 		}
30456c8d8eccSSepherosa Ziehau 
30463397dea6SSepherosa Ziehau 		m_head = ifsq_dequeue(ifsq, NULL);
3047a1bd58c9SSepherosa Ziehau 		if (m_head == NULL)
3048a1bd58c9SSepherosa Ziehau 			break;
3049a1bd58c9SSepherosa Ziehau 
30506c8d8eccSSepherosa Ziehau 		/*
30516c8d8eccSSepherosa Ziehau 		 * Pack the data into the transmit ring. If we
30526c8d8eccSSepherosa Ziehau 		 * don't have room, set the OACTIVE flag and wait
30536c8d8eccSSepherosa Ziehau 		 * for the NIC to drain the ring.
30546c8d8eccSSepherosa Ziehau 		 */
305533a04907SSepherosa Ziehau 		if (bnx_encap(txr, &m_head, &prodidx, &nsegs)) {
30563397dea6SSepherosa Ziehau 			ifsq_set_oactive(ifsq);
3057d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, oerrors, 1);
30586c8d8eccSSepherosa Ziehau 			break;
30596c8d8eccSSepherosa Ziehau 		}
30606c8d8eccSSepherosa Ziehau 
306133a04907SSepherosa Ziehau 		if (nsegs >= txr->bnx_tx_wreg) {
30626c8d8eccSSepherosa Ziehau 			/* Transmit */
30638bd43d5dSSepherosa Ziehau 			bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3064c9b7f592SSepherosa Ziehau 			nsegs = 0;
3065c9b7f592SSepherosa Ziehau 		}
30666c8d8eccSSepherosa Ziehau 
3067c9b7f592SSepherosa Ziehau 		ETHER_BPF_MTAP(ifp, m_head);
30686c8d8eccSSepherosa Ziehau 
30696c8d8eccSSepherosa Ziehau 		/*
30706c8d8eccSSepherosa Ziehau 		 * Set a timeout in case the chip goes out to lunch.
30716c8d8eccSSepherosa Ziehau 		 */
30723397dea6SSepherosa Ziehau 		txr->bnx_tx_watchdog.wd_timer = 5;
30736c8d8eccSSepherosa Ziehau 	}
30746c8d8eccSSepherosa Ziehau 
3075c9b7f592SSepherosa Ziehau 	if (nsegs > 0) {
3076c9b7f592SSepherosa Ziehau 		/* Transmit */
30778bd43d5dSSepherosa Ziehau 		bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3078c9b7f592SSepherosa Ziehau 	}
307933a04907SSepherosa Ziehau 	txr->bnx_tx_prodidx = prodidx;
3080c9b7f592SSepherosa Ziehau }
3081c9b7f592SSepherosa Ziehau 
30826c8d8eccSSepherosa Ziehau static void
30836c8d8eccSSepherosa Ziehau bnx_init(void *xsc)
30846c8d8eccSSepherosa Ziehau {
30856c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
30866c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
30876c8d8eccSSepherosa Ziehau 	uint16_t *m;
30886c8d8eccSSepherosa Ziehau 	uint32_t mode;
308933a04907SSepherosa Ziehau 	int i;
3090*7dbaa833SSepherosa Ziehau 	boolean_t polling;
30916c8d8eccSSepherosa Ziehau 
3092329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
30936c8d8eccSSepherosa Ziehau 
30946c8d8eccSSepherosa Ziehau 	/* Cancel pending I/O and flush buffers. */
30956c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
30966c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
30976c8d8eccSSepherosa Ziehau 	bnx_chipinit(sc);
30986c8d8eccSSepherosa Ziehau 
30996c8d8eccSSepherosa Ziehau 	/*
31006c8d8eccSSepherosa Ziehau 	 * Init the various state machines, ring
31016c8d8eccSSepherosa Ziehau 	 * control blocks and firmware.
31026c8d8eccSSepherosa Ziehau 	 */
31036c8d8eccSSepherosa Ziehau 	if (bnx_blockinit(sc)) {
31046c8d8eccSSepherosa Ziehau 		if_printf(ifp, "initialization failure\n");
31056c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
31066c8d8eccSSepherosa Ziehau 		return;
31076c8d8eccSSepherosa Ziehau 	}
31086c8d8eccSSepherosa Ziehau 
31096c8d8eccSSepherosa Ziehau 	/* Specify MTU. */
31106c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
31116c8d8eccSSepherosa Ziehau 	    ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
31126c8d8eccSSepherosa Ziehau 
31136c8d8eccSSepherosa Ziehau 	/* Load our MAC address. */
31146c8d8eccSSepherosa Ziehau 	m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
31156c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
31166c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
31176c8d8eccSSepherosa Ziehau 
31186c8d8eccSSepherosa Ziehau 	/* Enable or disable promiscuous mode as needed. */
31196c8d8eccSSepherosa Ziehau 	bnx_setpromisc(sc);
31206c8d8eccSSepherosa Ziehau 
31216c8d8eccSSepherosa Ziehau 	/* Program multicast filter. */
31226c8d8eccSSepherosa Ziehau 	bnx_setmulti(sc);
31236c8d8eccSSepherosa Ziehau 
31246c8d8eccSSepherosa Ziehau 	/* Init RX ring. */
3125beedf5beSSepherosa Ziehau 	if (bnx_init_rx_ring_std(&sc->bnx_rx_std_ring)) {
31266c8d8eccSSepherosa Ziehau 		if_printf(ifp, "RX ring initialization failed\n");
31276c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
31286c8d8eccSSepherosa Ziehau 		return;
31296c8d8eccSSepherosa Ziehau 	}
31306c8d8eccSSepherosa Ziehau 
31316c8d8eccSSepherosa Ziehau 	/* Init jumbo RX ring. */
31326c8d8eccSSepherosa Ziehau 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
31336c8d8eccSSepherosa Ziehau 		if (bnx_init_rx_ring_jumbo(sc)) {
31346c8d8eccSSepherosa Ziehau 			if_printf(ifp, "Jumbo RX ring initialization failed\n");
31356c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
31366c8d8eccSSepherosa Ziehau 			return;
31376c8d8eccSSepherosa Ziehau 		}
31386c8d8eccSSepherosa Ziehau 	}
31396c8d8eccSSepherosa Ziehau 
31406c8d8eccSSepherosa Ziehau 	/* Init our RX return ring index */
3141beedf5beSSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i)
3142beedf5beSSepherosa Ziehau 		sc->bnx_rx_ret_ring[i].bnx_rx_saved_considx = 0;
31436c8d8eccSSepherosa Ziehau 
31446c8d8eccSSepherosa Ziehau 	/* Init TX ring. */
314533a04907SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
314633a04907SSepherosa Ziehau 		bnx_init_tx_ring(&sc->bnx_tx_ring[i]);
31476c8d8eccSSepherosa Ziehau 
31486c8d8eccSSepherosa Ziehau 	/* Enable TX MAC state machine lockup fix. */
31496c8d8eccSSepherosa Ziehau 	mode = CSR_READ_4(sc, BGE_TX_MODE);
31506c8d8eccSSepherosa Ziehau 	mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
3151b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
3152b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
31536c8d8eccSSepherosa Ziehau 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
31546c8d8eccSSepherosa Ziehau 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
31556c8d8eccSSepherosa Ziehau 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
31566c8d8eccSSepherosa Ziehau 	}
31576c8d8eccSSepherosa Ziehau 	/* Turn on transmitter */
31586c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
31596c8d8eccSSepherosa Ziehau 
31606c8d8eccSSepherosa Ziehau 	/* Turn on receiver */
31616c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
31626c8d8eccSSepherosa Ziehau 
31636c8d8eccSSepherosa Ziehau 	/*
31646c8d8eccSSepherosa Ziehau 	 * Set the number of good frames to receive after RX MBUF
31656c8d8eccSSepherosa Ziehau 	 * Low Watermark has been reached.  After the RX MAC receives
31666c8d8eccSSepherosa Ziehau 	 * this number of frames, it will drop subsequent incoming
31676c8d8eccSSepherosa Ziehau 	 * frames until the MBUF High Watermark is reached.
31686c8d8eccSSepherosa Ziehau 	 */
3169bcb29629SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc))
31706c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
31716c8d8eccSSepherosa Ziehau 	else
31726c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
31736c8d8eccSSepherosa Ziehau 
3174f33ac8a4SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI) {
31756c8d8eccSSepherosa Ziehau 		if (bootverbose) {
31766c8d8eccSSepherosa Ziehau 			if_printf(ifp, "MSI_MODE: %#x\n",
31776c8d8eccSSepherosa Ziehau 			    CSR_READ_4(sc, BGE_MSI_MODE));
31786c8d8eccSSepherosa Ziehau 		}
31796c8d8eccSSepherosa Ziehau 	}
31806c8d8eccSSepherosa Ziehau 
31816c8d8eccSSepherosa Ziehau 	/* Tell firmware we're alive. */
31826c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
31836c8d8eccSSepherosa Ziehau 
31846c8d8eccSSepherosa Ziehau 	/* Enable host interrupts if polling(4) is not enabled. */
31856c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
3186*7dbaa833SSepherosa Ziehau 
3187*7dbaa833SSepherosa Ziehau 	polling = FALSE;
318839a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
318939a8d43aSSepherosa Ziehau 	if (ifp->if_flags & IFF_NPOLLING)
3190*7dbaa833SSepherosa Ziehau 		polling = TRUE;
3191*7dbaa833SSepherosa Ziehau #endif
3192*7dbaa833SSepherosa Ziehau 	if (polling)
31936c8d8eccSSepherosa Ziehau 		bnx_disable_intr(sc);
31946c8d8eccSSepherosa Ziehau 	else
31956c8d8eccSSepherosa Ziehau 		bnx_enable_intr(sc);
3196*7dbaa833SSepherosa Ziehau 	bnx_set_tick_cpuid(sc, polling);
31976c8d8eccSSepherosa Ziehau 
31986c8d8eccSSepherosa Ziehau 	bnx_ifmedia_upd(ifp);
31996c8d8eccSSepherosa Ziehau 
32006c8d8eccSSepherosa Ziehau 	ifp->if_flags |= IFF_RUNNING;
32013397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
32023397dea6SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
32033397dea6SSepherosa Ziehau 
32043397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
32053397dea6SSepherosa Ziehau 		ifsq_watchdog_start(&txr->bnx_tx_watchdog);
32063397dea6SSepherosa Ziehau 	}
32076c8d8eccSSepherosa Ziehau 
3208*7dbaa833SSepherosa Ziehau 	callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
3209*7dbaa833SSepherosa Ziehau 	    sc->bnx_tick_cpuid);
32106c8d8eccSSepherosa Ziehau }
32116c8d8eccSSepherosa Ziehau 
32126c8d8eccSSepherosa Ziehau /*
32136c8d8eccSSepherosa Ziehau  * Set media options.
32146c8d8eccSSepherosa Ziehau  */
32156c8d8eccSSepherosa Ziehau static int
32166c8d8eccSSepherosa Ziehau bnx_ifmedia_upd(struct ifnet *ifp)
32176c8d8eccSSepherosa Ziehau {
32186c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
32196c8d8eccSSepherosa Ziehau 
32206c8d8eccSSepherosa Ziehau 	/* If this is a 1000baseX NIC, enable the TBI port. */
32216c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
32226c8d8eccSSepherosa Ziehau 		struct ifmedia *ifm = &sc->bnx_ifmedia;
32236c8d8eccSSepherosa Ziehau 
32246c8d8eccSSepherosa Ziehau 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
32256c8d8eccSSepherosa Ziehau 			return(EINVAL);
32266c8d8eccSSepherosa Ziehau 
32276c8d8eccSSepherosa Ziehau 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
32286c8d8eccSSepherosa Ziehau 		case IFM_AUTO:
32296c8d8eccSSepherosa Ziehau 			break;
32306c8d8eccSSepherosa Ziehau 
32316c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
32326c8d8eccSSepherosa Ziehau 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
32336c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
32346c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
32356c8d8eccSSepherosa Ziehau 			} else {
32366c8d8eccSSepherosa Ziehau 				BNX_SETBIT(sc, BGE_MAC_MODE,
32376c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
32386c8d8eccSSepherosa Ziehau 			}
32396c8d8eccSSepherosa Ziehau 			break;
32406c8d8eccSSepherosa Ziehau 		default:
32416c8d8eccSSepherosa Ziehau 			return(EINVAL);
32426c8d8eccSSepherosa Ziehau 		}
32436c8d8eccSSepherosa Ziehau 	} else {
32446c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
32456c8d8eccSSepherosa Ziehau 
32466c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
32476c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
32486c8d8eccSSepherosa Ziehau 		if (mii->mii_instance) {
32496c8d8eccSSepherosa Ziehau 			struct mii_softc *miisc;
32506c8d8eccSSepherosa Ziehau 
32516c8d8eccSSepherosa Ziehau 			LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
32526c8d8eccSSepherosa Ziehau 				mii_phy_reset(miisc);
32536c8d8eccSSepherosa Ziehau 		}
32546c8d8eccSSepherosa Ziehau 		mii_mediachg(mii);
32556c8d8eccSSepherosa Ziehau 
32566c8d8eccSSepherosa Ziehau 		/*
32576c8d8eccSSepherosa Ziehau 		 * Force an interrupt so that we will call bnx_link_upd
32586c8d8eccSSepherosa Ziehau 		 * if needed and clear any pending link state attention.
32596c8d8eccSSepherosa Ziehau 		 * Without this we are not getting any further interrupts
32606c8d8eccSSepherosa Ziehau 		 * for link state changes and thus will not UP the link and
32616c8d8eccSSepherosa Ziehau 		 * not be able to send in bnx_start.  The only way to get
32626c8d8eccSSepherosa Ziehau 		 * things working was to receive a packet and get an RX
32636c8d8eccSSepherosa Ziehau 		 * intr.
32646c8d8eccSSepherosa Ziehau 		 *
32656c8d8eccSSepherosa Ziehau 		 * bnx_tick should help for fiber cards and we might not
32666c8d8eccSSepherosa Ziehau 		 * need to do this here if BNX_FLAG_TBI is set but as
32676c8d8eccSSepherosa Ziehau 		 * we poll for fiber anyway it should not harm.
32686c8d8eccSSepherosa Ziehau 		 */
32696c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
32706c8d8eccSSepherosa Ziehau 	}
32716c8d8eccSSepherosa Ziehau 	return(0);
32726c8d8eccSSepherosa Ziehau }
32736c8d8eccSSepherosa Ziehau 
32746c8d8eccSSepherosa Ziehau /*
32756c8d8eccSSepherosa Ziehau  * Report current media status.
32766c8d8eccSSepherosa Ziehau  */
32776c8d8eccSSepherosa Ziehau static void
32786c8d8eccSSepherosa Ziehau bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
32796c8d8eccSSepherosa Ziehau {
32806c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
32816c8d8eccSSepherosa Ziehau 
32826c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
32836c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = IFM_AVALID;
32846c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = IFM_ETHER;
32856c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_STS) &
32866c8d8eccSSepherosa Ziehau 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
32876c8d8eccSSepherosa Ziehau 			ifmr->ifm_status |= IFM_ACTIVE;
32886c8d8eccSSepherosa Ziehau 		} else {
32896c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_NONE;
32906c8d8eccSSepherosa Ziehau 			return;
32916c8d8eccSSepherosa Ziehau 		}
32926c8d8eccSSepherosa Ziehau 
32936c8d8eccSSepherosa Ziehau 		ifmr->ifm_active |= IFM_1000_SX;
32946c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
32956c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_HDX;
32966c8d8eccSSepherosa Ziehau 		else
32976c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_FDX;
32986c8d8eccSSepherosa Ziehau 	} else {
32996c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
33006c8d8eccSSepherosa Ziehau 
33016c8d8eccSSepherosa Ziehau 		mii_pollstat(mii);
33026c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = mii->mii_media_active;
33036c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = mii->mii_media_status;
33046c8d8eccSSepherosa Ziehau 	}
33056c8d8eccSSepherosa Ziehau }
33066c8d8eccSSepherosa Ziehau 
33076c8d8eccSSepherosa Ziehau static int
33086c8d8eccSSepherosa Ziehau bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
33096c8d8eccSSepherosa Ziehau {
33106c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
33116c8d8eccSSepherosa Ziehau 	struct ifreq *ifr = (struct ifreq *)data;
33126c8d8eccSSepherosa Ziehau 	int mask, error = 0;
33136c8d8eccSSepherosa Ziehau 
3314329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
33156c8d8eccSSepherosa Ziehau 
33166c8d8eccSSepherosa Ziehau 	switch (command) {
33176c8d8eccSSepherosa Ziehau 	case SIOCSIFMTU:
33186c8d8eccSSepherosa Ziehau 		if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
33196c8d8eccSSepherosa Ziehau 		    (BNX_IS_JUMBO_CAPABLE(sc) &&
33206c8d8eccSSepherosa Ziehau 		     ifr->ifr_mtu > BNX_JUMBO_MTU)) {
33216c8d8eccSSepherosa Ziehau 			error = EINVAL;
33226c8d8eccSSepherosa Ziehau 		} else if (ifp->if_mtu != ifr->ifr_mtu) {
33236c8d8eccSSepherosa Ziehau 			ifp->if_mtu = ifr->ifr_mtu;
33246c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING)
33256c8d8eccSSepherosa Ziehau 				bnx_init(sc);
33266c8d8eccSSepherosa Ziehau 		}
33276c8d8eccSSepherosa Ziehau 		break;
33286c8d8eccSSepherosa Ziehau 	case SIOCSIFFLAGS:
33296c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_UP) {
33306c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING) {
33316c8d8eccSSepherosa Ziehau 				mask = ifp->if_flags ^ sc->bnx_if_flags;
33326c8d8eccSSepherosa Ziehau 
33336c8d8eccSSepherosa Ziehau 				/*
33346c8d8eccSSepherosa Ziehau 				 * If only the state of the PROMISC flag
33356c8d8eccSSepherosa Ziehau 				 * changed, then just use the 'set promisc
33366c8d8eccSSepherosa Ziehau 				 * mode' command instead of reinitializing
33376c8d8eccSSepherosa Ziehau 				 * the entire NIC. Doing a full re-init
33386c8d8eccSSepherosa Ziehau 				 * means reloading the firmware and waiting
33396c8d8eccSSepherosa Ziehau 				 * for it to start up, which may take a
33406c8d8eccSSepherosa Ziehau 				 * second or two.  Similarly for ALLMULTI.
33416c8d8eccSSepherosa Ziehau 				 */
33426c8d8eccSSepherosa Ziehau 				if (mask & IFF_PROMISC)
33436c8d8eccSSepherosa Ziehau 					bnx_setpromisc(sc);
33446c8d8eccSSepherosa Ziehau 				if (mask & IFF_ALLMULTI)
33456c8d8eccSSepherosa Ziehau 					bnx_setmulti(sc);
33466c8d8eccSSepherosa Ziehau 			} else {
33476c8d8eccSSepherosa Ziehau 				bnx_init(sc);
33486c8d8eccSSepherosa Ziehau 			}
33496c8d8eccSSepherosa Ziehau 		} else if (ifp->if_flags & IFF_RUNNING) {
33506c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
33516c8d8eccSSepherosa Ziehau 		}
33526c8d8eccSSepherosa Ziehau 		sc->bnx_if_flags = ifp->if_flags;
33536c8d8eccSSepherosa Ziehau 		break;
33546c8d8eccSSepherosa Ziehau 	case SIOCADDMULTI:
33556c8d8eccSSepherosa Ziehau 	case SIOCDELMULTI:
33566c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING)
33576c8d8eccSSepherosa Ziehau 			bnx_setmulti(sc);
33586c8d8eccSSepherosa Ziehau 		break;
33596c8d8eccSSepherosa Ziehau 	case SIOCSIFMEDIA:
33606c8d8eccSSepherosa Ziehau 	case SIOCGIFMEDIA:
33616c8d8eccSSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_TBI) {
33626c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
33636c8d8eccSSepherosa Ziehau 			    &sc->bnx_ifmedia, command);
33646c8d8eccSSepherosa Ziehau 		} else {
33656c8d8eccSSepherosa Ziehau 			struct mii_data *mii;
33666c8d8eccSSepherosa Ziehau 
33676c8d8eccSSepherosa Ziehau 			mii = device_get_softc(sc->bnx_miibus);
33686c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
33696c8d8eccSSepherosa Ziehau 					      &mii->mii_media, command);
33706c8d8eccSSepherosa Ziehau 		}
33716c8d8eccSSepherosa Ziehau 		break;
33726c8d8eccSSepherosa Ziehau         case SIOCSIFCAP:
33736c8d8eccSSepherosa Ziehau 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
33746c8d8eccSSepherosa Ziehau 		if (mask & IFCAP_HWCSUM) {
33756c8d8eccSSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
337666deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TXCSUM)
337766deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= BNX_CSUM_FEATURES;
33786c8d8eccSSepherosa Ziehau 			else
337966deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
338066deb1c1SSepherosa Ziehau 		}
338166deb1c1SSepherosa Ziehau 		if (mask & IFCAP_TSO) {
338266deb1c1SSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_TSO);
338366deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TSO)
338466deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= CSUM_TSO;
338566deb1c1SSepherosa Ziehau 			else
338666deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~CSUM_TSO;
33876c8d8eccSSepherosa Ziehau 		}
33886c8d8eccSSepherosa Ziehau 		break;
33896c8d8eccSSepherosa Ziehau 	default:
33906c8d8eccSSepherosa Ziehau 		error = ether_ioctl(ifp, command, data);
33916c8d8eccSSepherosa Ziehau 		break;
33926c8d8eccSSepherosa Ziehau 	}
33936c8d8eccSSepherosa Ziehau 	return error;
33946c8d8eccSSepherosa Ziehau }
33956c8d8eccSSepherosa Ziehau 
33966c8d8eccSSepherosa Ziehau static void
33973397dea6SSepherosa Ziehau bnx_watchdog(struct ifaltq_subque *ifsq)
33986c8d8eccSSepherosa Ziehau {
33993397dea6SSepherosa Ziehau 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
34006c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
34013397dea6SSepherosa Ziehau 	int i;
34023397dea6SSepherosa Ziehau 
34033397dea6SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
34046c8d8eccSSepherosa Ziehau 
34056c8d8eccSSepherosa Ziehau 	if_printf(ifp, "watchdog timeout -- resetting\n");
34066c8d8eccSSepherosa Ziehau 
34076c8d8eccSSepherosa Ziehau 	bnx_init(sc);
34086c8d8eccSSepherosa Ziehau 
3409d40991efSSepherosa Ziehau 	IFNET_STAT_INC(ifp, oerrors, 1);
34106c8d8eccSSepherosa Ziehau 
34113397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
34123397dea6SSepherosa Ziehau 		ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
34136c8d8eccSSepherosa Ziehau }
34146c8d8eccSSepherosa Ziehau 
34156c8d8eccSSepherosa Ziehau /*
34166c8d8eccSSepherosa Ziehau  * Stop the adapter and free any mbufs allocated to the
34176c8d8eccSSepherosa Ziehau  * RX and TX lists.
34186c8d8eccSSepherosa Ziehau  */
34196c8d8eccSSepherosa Ziehau static void
34206c8d8eccSSepherosa Ziehau bnx_stop(struct bnx_softc *sc)
34216c8d8eccSSepherosa Ziehau {
34226c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
342333a04907SSepherosa Ziehau 	int i;
34246c8d8eccSSepherosa Ziehau 
3425329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
34266c8d8eccSSepherosa Ziehau 
3427*7dbaa833SSepherosa Ziehau 	callout_stop(&sc->bnx_tick_timer);
34286c8d8eccSSepherosa Ziehau 
34296c8d8eccSSepherosa Ziehau 	/*
34306c8d8eccSSepherosa Ziehau 	 * Disable all of the receiver blocks
34316c8d8eccSSepherosa Ziehau 	 */
34326c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
34336c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
34346c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
34356c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
34366c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
34376c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
34386c8d8eccSSepherosa Ziehau 
34396c8d8eccSSepherosa Ziehau 	/*
34406c8d8eccSSepherosa Ziehau 	 * Disable all of the transmit blocks
34416c8d8eccSSepherosa Ziehau 	 */
34426c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
34436c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
34446c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
34456c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
34466c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
34476c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
34486c8d8eccSSepherosa Ziehau 
34496c8d8eccSSepherosa Ziehau 	/*
34506c8d8eccSSepherosa Ziehau 	 * Shut down all of the memory managers and related
34516c8d8eccSSepherosa Ziehau 	 * state machines.
34526c8d8eccSSepherosa Ziehau 	 */
34536c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
34546c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
34556c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
34566c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
34576c8d8eccSSepherosa Ziehau 
34586c8d8eccSSepherosa Ziehau 	/* Disable host interrupts. */
34596c8d8eccSSepherosa Ziehau 	bnx_disable_intr(sc);
34606c8d8eccSSepherosa Ziehau 
34616c8d8eccSSepherosa Ziehau 	/*
34626c8d8eccSSepherosa Ziehau 	 * Tell firmware we're shutting down.
34636c8d8eccSSepherosa Ziehau 	 */
34646c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
34656c8d8eccSSepherosa Ziehau 
34666c8d8eccSSepherosa Ziehau 	/* Free the RX lists. */
3467beedf5beSSepherosa Ziehau 	bnx_free_rx_ring_std(&sc->bnx_rx_std_ring);
34686c8d8eccSSepherosa Ziehau 
34696c8d8eccSSepherosa Ziehau 	/* Free jumbo RX list. */
34706c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
34716c8d8eccSSepherosa Ziehau 		bnx_free_rx_ring_jumbo(sc);
34726c8d8eccSSepherosa Ziehau 
34736c8d8eccSSepherosa Ziehau 	/* Free TX buffers. */
34744fa38985SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
34754fa38985SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
34766c8d8eccSSepherosa Ziehau 
34774fa38985SSepherosa Ziehau 		txr->bnx_saved_status_tag = 0;
34784fa38985SSepherosa Ziehau 		bnx_free_tx_ring(txr);
34794fa38985SSepherosa Ziehau 	}
34804fa38985SSepherosa Ziehau 
34814fa38985SSepherosa Ziehau 	/* Clear saved status tag */
34824fa38985SSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i)
34834fa38985SSepherosa Ziehau 		sc->bnx_rx_ret_ring[i].bnx_saved_status_tag = 0;
34844fa38985SSepherosa Ziehau 
34856c8d8eccSSepherosa Ziehau 	sc->bnx_link = 0;
34866c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
34876c8d8eccSSepherosa Ziehau 
34889ed293e0SSepherosa Ziehau 	ifp->if_flags &= ~IFF_RUNNING;
34893397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
34903397dea6SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
34913397dea6SSepherosa Ziehau 
34923397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
34933397dea6SSepherosa Ziehau 		ifsq_watchdog_stop(&txr->bnx_tx_watchdog);
34943397dea6SSepherosa Ziehau 	}
34956c8d8eccSSepherosa Ziehau }
34966c8d8eccSSepherosa Ziehau 
34976c8d8eccSSepherosa Ziehau /*
34986c8d8eccSSepherosa Ziehau  * Stop all chip I/O so that the kernel's probe routines don't
34996c8d8eccSSepherosa Ziehau  * get confused by errant DMAs when rebooting.
35006c8d8eccSSepherosa Ziehau  */
35016c8d8eccSSepherosa Ziehau static void
35026c8d8eccSSepherosa Ziehau bnx_shutdown(device_t dev)
35036c8d8eccSSepherosa Ziehau {
35046c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
35056c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
35066c8d8eccSSepherosa Ziehau 
3507329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
35086c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
35096c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
3510329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
35116c8d8eccSSepherosa Ziehau }
35126c8d8eccSSepherosa Ziehau 
35136c8d8eccSSepherosa Ziehau static int
35146c8d8eccSSepherosa Ziehau bnx_suspend(device_t dev)
35156c8d8eccSSepherosa Ziehau {
35166c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
35176c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
35186c8d8eccSSepherosa Ziehau 
3519329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
35206c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
3521329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
35226c8d8eccSSepherosa Ziehau 
35236c8d8eccSSepherosa Ziehau 	return 0;
35246c8d8eccSSepherosa Ziehau }
35256c8d8eccSSepherosa Ziehau 
35266c8d8eccSSepherosa Ziehau static int
35276c8d8eccSSepherosa Ziehau bnx_resume(device_t dev)
35286c8d8eccSSepherosa Ziehau {
35296c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
35306c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
35316c8d8eccSSepherosa Ziehau 
3532329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
35336c8d8eccSSepherosa Ziehau 
35346c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_UP) {
35353397dea6SSepherosa Ziehau 		int i;
35366c8d8eccSSepherosa Ziehau 
35373397dea6SSepherosa Ziehau 		bnx_init(sc);
35383397dea6SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
35393397dea6SSepherosa Ziehau 			ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
35406c8d8eccSSepherosa Ziehau 	}
35416c8d8eccSSepherosa Ziehau 
3542329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
35436c8d8eccSSepherosa Ziehau 
35446c8d8eccSSepherosa Ziehau 	return 0;
35456c8d8eccSSepherosa Ziehau }
35466c8d8eccSSepherosa Ziehau 
35476c8d8eccSSepherosa Ziehau static void
35486c8d8eccSSepherosa Ziehau bnx_setpromisc(struct bnx_softc *sc)
35496c8d8eccSSepherosa Ziehau {
35506c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
35516c8d8eccSSepherosa Ziehau 
35526c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_PROMISC)
35536c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
35546c8d8eccSSepherosa Ziehau 	else
35556c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
35566c8d8eccSSepherosa Ziehau }
35576c8d8eccSSepherosa Ziehau 
35586c8d8eccSSepherosa Ziehau static void
35596c8d8eccSSepherosa Ziehau bnx_dma_free(struct bnx_softc *sc)
35606c8d8eccSSepherosa Ziehau {
3561beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
35626c8d8eccSSepherosa Ziehau 	int i;
35636c8d8eccSSepherosa Ziehau 
3564beedf5beSSepherosa Ziehau 	/* Destroy RX return rings */
3565beedf5beSSepherosa Ziehau 	if (sc->bnx_rx_ret_ring != NULL) {
3566beedf5beSSepherosa Ziehau 		for (i = 0; i < sc->bnx_rx_retcnt; ++i)
3567beedf5beSSepherosa Ziehau 			bnx_destroy_rx_ret_ring(&sc->bnx_rx_ret_ring[i]);
3568beedf5beSSepherosa Ziehau 		kfree(sc->bnx_rx_ret_ring, M_DEVBUF);
3569beedf5beSSepherosa Ziehau 	}
3570beedf5beSSepherosa Ziehau 
35716c8d8eccSSepherosa Ziehau 	/* Destroy RX mbuf DMA stuffs. */
3572beedf5beSSepherosa Ziehau 	if (std->bnx_rx_mtag != NULL) {
35736c8d8eccSSepherosa Ziehau 		for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3574beedf5beSSepherosa Ziehau 			KKASSERT(std->bnx_rx_std_buf[i].bnx_rx_mbuf == NULL);
3575beedf5beSSepherosa Ziehau 			bus_dmamap_destroy(std->bnx_rx_mtag,
3576beedf5beSSepherosa Ziehau 			    std->bnx_rx_std_buf[i].bnx_rx_dmamap);
35776c8d8eccSSepherosa Ziehau 		}
3578beedf5beSSepherosa Ziehau 		bus_dma_tag_destroy(std->bnx_rx_mtag);
35796c8d8eccSSepherosa Ziehau 	}
35806c8d8eccSSepherosa Ziehau 
3581beedf5beSSepherosa Ziehau 	/* Destroy standard RX ring */
3582beedf5beSSepherosa Ziehau 	bnx_dma_block_free(std->bnx_rx_std_ring_tag,
3583beedf5beSSepherosa Ziehau 	    std->bnx_rx_std_ring_map, std->bnx_rx_std_ring);
3584beedf5beSSepherosa Ziehau 
358533a04907SSepherosa Ziehau 	/* Destroy TX rings */
358633a04907SSepherosa Ziehau 	if (sc->bnx_tx_ring != NULL) {
358733a04907SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
358833a04907SSepherosa Ziehau 			bnx_destroy_tx_ring(&sc->bnx_tx_ring[i]);
358933a04907SSepherosa Ziehau 		kfree(sc->bnx_tx_ring, M_DEVBUF);
35906c8d8eccSSepherosa Ziehau 	}
35916c8d8eccSSepherosa Ziehau 
35926c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
35936c8d8eccSSepherosa Ziehau 		bnx_free_jumbo_mem(sc);
35946c8d8eccSSepherosa Ziehau 
35956c8d8eccSSepherosa Ziehau 	/* Destroy status block */
35966c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_status_tag,
35976c8d8eccSSepherosa Ziehau 	    sc->bnx_cdata.bnx_status_map,
35986c8d8eccSSepherosa Ziehau 	    sc->bnx_ldata.bnx_status_block);
35996c8d8eccSSepherosa Ziehau 
36006c8d8eccSSepherosa Ziehau 	/* Destroy the parent tag */
36016c8d8eccSSepherosa Ziehau 	if (sc->bnx_cdata.bnx_parent_tag != NULL)
36026c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
36036c8d8eccSSepherosa Ziehau }
36046c8d8eccSSepherosa Ziehau 
36056c8d8eccSSepherosa Ziehau static int
3606beedf5beSSepherosa Ziehau bnx_dma_alloc(device_t dev)
36076c8d8eccSSepherosa Ziehau {
3608beedf5beSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
3609beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
36108bd43d5dSSepherosa Ziehau 	int i, error, mbx;
36116c8d8eccSSepherosa Ziehau 
36126c8d8eccSSepherosa Ziehau 	/*
36136c8d8eccSSepherosa Ziehau 	 * Allocate the parent bus DMA tag appropriate for PCI.
36146c8d8eccSSepherosa Ziehau 	 *
36156c8d8eccSSepherosa Ziehau 	 * All of the NetExtreme/NetLink controllers have 4GB boundary
36166c8d8eccSSepherosa Ziehau 	 * DMA bug.
36176c8d8eccSSepherosa Ziehau 	 * Whenever an address crosses a multiple of the 4GB boundary
36186c8d8eccSSepherosa Ziehau 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
36196c8d8eccSSepherosa Ziehau 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
36206c8d8eccSSepherosa Ziehau 	 * state machine will lockup and cause the device to hang.
36216c8d8eccSSepherosa Ziehau 	 */
36226c8d8eccSSepherosa Ziehau 	error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
3623beedf5beSSepherosa Ziehau 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3624beedf5beSSepherosa Ziehau 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
36256c8d8eccSSepherosa Ziehau 	    0, &sc->bnx_cdata.bnx_parent_tag);
36266c8d8eccSSepherosa Ziehau 	if (error) {
3627beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create parent DMA tag\n");
36286c8d8eccSSepherosa Ziehau 		return error;
36296c8d8eccSSepherosa Ziehau 	}
36306c8d8eccSSepherosa Ziehau 
36316c8d8eccSSepherosa Ziehau 	/*
36326c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for status block.
36336c8d8eccSSepherosa Ziehau 	 */
36346c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_STATUS_BLK_SZ,
36356c8d8eccSSepherosa Ziehau 	    &sc->bnx_cdata.bnx_status_tag,
36366c8d8eccSSepherosa Ziehau 	    &sc->bnx_cdata.bnx_status_map,
36376c8d8eccSSepherosa Ziehau 	    (void *)&sc->bnx_ldata.bnx_status_block,
36386c8d8eccSSepherosa Ziehau 	    &sc->bnx_ldata.bnx_status_block_paddr);
36396c8d8eccSSepherosa Ziehau 	if (error) {
3640beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create status block\n");
36416c8d8eccSSepherosa Ziehau 		return error;
36426c8d8eccSSepherosa Ziehau 	}
36436c8d8eccSSepherosa Ziehau 
3644beedf5beSSepherosa Ziehau 	/*
3645beedf5beSSepherosa Ziehau 	 * Create DMA tag and maps for RX mbufs.
3646beedf5beSSepherosa Ziehau 	 */
3647beedf5beSSepherosa Ziehau 	std->bnx_sc = sc;
3648329f9016SSepherosa Ziehau 	lwkt_serialize_init(&std->bnx_rx_std_serialize);
3649beedf5beSSepherosa Ziehau 	error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
3650beedf5beSSepherosa Ziehau 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3651beedf5beSSepherosa Ziehau 	    NULL, NULL, MCLBYTES, 1, MCLBYTES,
3652beedf5beSSepherosa Ziehau 	    BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK, &std->bnx_rx_mtag);
3653beedf5beSSepherosa Ziehau 	if (error) {
3654beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create RX mbuf DMA tag\n");
3655beedf5beSSepherosa Ziehau 		return error;
3656beedf5beSSepherosa Ziehau 	}
3657beedf5beSSepherosa Ziehau 
3658beedf5beSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; ++i) {
3659beedf5beSSepherosa Ziehau 		error = bus_dmamap_create(std->bnx_rx_mtag, BUS_DMA_WAITOK,
3660beedf5beSSepherosa Ziehau 		    &std->bnx_rx_std_buf[i].bnx_rx_dmamap);
3661beedf5beSSepherosa Ziehau 		if (error) {
3662beedf5beSSepherosa Ziehau 			int j;
3663beedf5beSSepherosa Ziehau 
3664beedf5beSSepherosa Ziehau 			for (j = 0; j < i; ++j) {
3665beedf5beSSepherosa Ziehau 				bus_dmamap_destroy(std->bnx_rx_mtag,
3666beedf5beSSepherosa Ziehau 				    std->bnx_rx_std_buf[j].bnx_rx_dmamap);
3667beedf5beSSepherosa Ziehau 			}
3668beedf5beSSepherosa Ziehau 			bus_dma_tag_destroy(std->bnx_rx_mtag);
3669beedf5beSSepherosa Ziehau 			std->bnx_rx_mtag = NULL;
3670beedf5beSSepherosa Ziehau 
3671beedf5beSSepherosa Ziehau 			device_printf(dev,
3672beedf5beSSepherosa Ziehau 			    "could not create %dth RX mbuf DMA map\n", i);
3673beedf5beSSepherosa Ziehau 			return error;
3674beedf5beSSepherosa Ziehau 		}
3675beedf5beSSepherosa Ziehau 	}
3676beedf5beSSepherosa Ziehau 
3677beedf5beSSepherosa Ziehau 	/*
3678beedf5beSSepherosa Ziehau 	 * Create DMA stuffs for standard RX ring.
3679beedf5beSSepherosa Ziehau 	 */
3680beedf5beSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
3681beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_tag,
3682beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_map,
3683beedf5beSSepherosa Ziehau 	    (void *)&std->bnx_rx_std_ring,
3684beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_paddr);
3685beedf5beSSepherosa Ziehau 	if (error) {
3686beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create std RX ring\n");
3687beedf5beSSepherosa Ziehau 		return error;
3688beedf5beSSepherosa Ziehau 	}
3689beedf5beSSepherosa Ziehau 
3690beedf5beSSepherosa Ziehau 	/*
3691beedf5beSSepherosa Ziehau 	 * Create RX return rings
3692beedf5beSSepherosa Ziehau 	 */
3693beedf5beSSepherosa Ziehau 	sc->bnx_rx_ret_ring = kmalloc_cachealign(
3694beedf5beSSepherosa Ziehau 	    sizeof(struct bnx_rx_ret_ring) * sc->bnx_rx_retcnt, M_DEVBUF,
3695beedf5beSSepherosa Ziehau 	    M_WAITOK | M_ZERO);
3696beedf5beSSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
3697beedf5beSSepherosa Ziehau 		struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
3698beedf5beSSepherosa Ziehau 
3699beedf5beSSepherosa Ziehau 		ret->bnx_sc = sc;
3700beedf5beSSepherosa Ziehau 		ret->bnx_std = std;
37013a16b7b8SSepherosa Ziehau 
37023a16b7b8SSepherosa Ziehau 		/* XXX */
37033a16b7b8SSepherosa Ziehau 		ret->bnx_rx_considx =
37043a16b7b8SSepherosa Ziehau 		&sc->bnx_ldata.bnx_status_block->bge_idx[0].bge_rx_prod_idx;
37054fa38985SSepherosa Ziehau 		ret->bnx_hw_status_tag =
37064fa38985SSepherosa Ziehau 		&sc->bnx_ldata.bnx_status_block->bge_status_tag;
37073a16b7b8SSepherosa Ziehau 
3708beedf5beSSepherosa Ziehau 		error = bnx_create_rx_ret_ring(ret);
3709beedf5beSSepherosa Ziehau 		if (error) {
3710beedf5beSSepherosa Ziehau 			device_printf(dev,
3711beedf5beSSepherosa Ziehau 			    "could not create %dth RX ret ring\n", i);
3712beedf5beSSepherosa Ziehau 			return error;
3713beedf5beSSepherosa Ziehau 		}
3714beedf5beSSepherosa Ziehau 	}
3715beedf5beSSepherosa Ziehau 
3716beedf5beSSepherosa Ziehau 	/*
3717beedf5beSSepherosa Ziehau 	 * Create TX rings
3718beedf5beSSepherosa Ziehau 	 */
37198bd43d5dSSepherosa Ziehau 	mbx = BGE_MBX_TX_HOST_PROD0_LO;
372033a04907SSepherosa Ziehau 	sc->bnx_tx_ring = kmalloc_cachealign(
372133a04907SSepherosa Ziehau 	    sizeof(struct bnx_tx_ring) * sc->bnx_tx_ringcnt, M_DEVBUF,
372233a04907SSepherosa Ziehau 	    M_WAITOK | M_ZERO);
372333a04907SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
372433a04907SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
372533a04907SSepherosa Ziehau 
372633a04907SSepherosa Ziehau 		txr->bnx_sc = sc;
37278bd43d5dSSepherosa Ziehau 		txr->bnx_tx_mbx = mbx;
37288bd43d5dSSepherosa Ziehau 
37298bd43d5dSSepherosa Ziehau 		if (mbx & 0x4)
37308bd43d5dSSepherosa Ziehau 			mbx -= 0x4;
37318bd43d5dSSepherosa Ziehau 		else
37328bd43d5dSSepherosa Ziehau 			mbx += 0xc;
37338bd43d5dSSepherosa Ziehau 
37343a16b7b8SSepherosa Ziehau 		/* XXX */
37353a16b7b8SSepherosa Ziehau 		txr->bnx_tx_considx =
37363a16b7b8SSepherosa Ziehau 		&sc->bnx_ldata.bnx_status_block->bge_idx[0].bge_tx_cons_idx;
37373a16b7b8SSepherosa Ziehau 
373833a04907SSepherosa Ziehau 		error = bnx_create_tx_ring(txr);
373933a04907SSepherosa Ziehau 		if (error) {
3740beedf5beSSepherosa Ziehau 			device_printf(dev,
3741beedf5beSSepherosa Ziehau 			    "could not create %dth TX ring\n", i);
3742beedf5beSSepherosa Ziehau 			return error;
3743beedf5beSSepherosa Ziehau 		}
3744beedf5beSSepherosa Ziehau 	}
3745beedf5beSSepherosa Ziehau 
3746beedf5beSSepherosa Ziehau 	/*
3747beedf5beSSepherosa Ziehau 	 * Create jumbo buffer pool.
3748beedf5beSSepherosa Ziehau 	 */
3749beedf5beSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
3750beedf5beSSepherosa Ziehau 		error = bnx_alloc_jumbo_mem(sc);
3751beedf5beSSepherosa Ziehau 		if (error) {
3752beedf5beSSepherosa Ziehau 			device_printf(dev,
3753beedf5beSSepherosa Ziehau 			    "could not create jumbo buffer pool\n");
375433a04907SSepherosa Ziehau 			return error;
375533a04907SSepherosa Ziehau 		}
375633a04907SSepherosa Ziehau 	}
375733a04907SSepherosa Ziehau 
37586c8d8eccSSepherosa Ziehau 	return 0;
37596c8d8eccSSepherosa Ziehau }
37606c8d8eccSSepherosa Ziehau 
37616c8d8eccSSepherosa Ziehau static int
37626c8d8eccSSepherosa Ziehau bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
37636c8d8eccSSepherosa Ziehau 		    bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
37646c8d8eccSSepherosa Ziehau {
37656c8d8eccSSepherosa Ziehau 	bus_dmamem_t dmem;
37666c8d8eccSSepherosa Ziehau 	int error;
37676c8d8eccSSepherosa Ziehau 
37686c8d8eccSSepherosa Ziehau 	error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
37696c8d8eccSSepherosa Ziehau 				    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
37706c8d8eccSSepherosa Ziehau 				    size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
37716c8d8eccSSepherosa Ziehau 	if (error)
37726c8d8eccSSepherosa Ziehau 		return error;
37736c8d8eccSSepherosa Ziehau 
37746c8d8eccSSepherosa Ziehau 	*tag = dmem.dmem_tag;
37756c8d8eccSSepherosa Ziehau 	*map = dmem.dmem_map;
37766c8d8eccSSepherosa Ziehau 	*addr = dmem.dmem_addr;
37776c8d8eccSSepherosa Ziehau 	*paddr = dmem.dmem_busaddr;
37786c8d8eccSSepherosa Ziehau 
37796c8d8eccSSepherosa Ziehau 	return 0;
37806c8d8eccSSepherosa Ziehau }
37816c8d8eccSSepherosa Ziehau 
37826c8d8eccSSepherosa Ziehau static void
37836c8d8eccSSepherosa Ziehau bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
37846c8d8eccSSepherosa Ziehau {
37856c8d8eccSSepherosa Ziehau 	if (tag != NULL) {
37866c8d8eccSSepherosa Ziehau 		bus_dmamap_unload(tag, map);
37876c8d8eccSSepherosa Ziehau 		bus_dmamem_free(tag, addr, map);
37886c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(tag);
37896c8d8eccSSepherosa Ziehau 	}
37906c8d8eccSSepherosa Ziehau }
37916c8d8eccSSepherosa Ziehau 
37926c8d8eccSSepherosa Ziehau static void
37936c8d8eccSSepherosa Ziehau bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
37946c8d8eccSSepherosa Ziehau {
37956c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
37966c8d8eccSSepherosa Ziehau 
37976c8d8eccSSepherosa Ziehau #define PCS_ENCODE_ERR	(BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
37986c8d8eccSSepherosa Ziehau 
37996c8d8eccSSepherosa Ziehau 	/*
38006c8d8eccSSepherosa Ziehau 	 * Sometimes PCS encoding errors are detected in
38016c8d8eccSSepherosa Ziehau 	 * TBI mode (on fiber NICs), and for some reason
38026c8d8eccSSepherosa Ziehau 	 * the chip will signal them as link changes.
38036c8d8eccSSepherosa Ziehau 	 * If we get a link change event, but the 'PCS
38046c8d8eccSSepherosa Ziehau 	 * encoding error' bit in the MAC status register
38056c8d8eccSSepherosa Ziehau 	 * is set, don't bother doing a link check.
38066c8d8eccSSepherosa Ziehau 	 * This avoids spurious "gigabit link up" messages
38076c8d8eccSSepherosa Ziehau 	 * that sometimes appear on fiber NICs during
38086c8d8eccSSepherosa Ziehau 	 * periods of heavy traffic.
38096c8d8eccSSepherosa Ziehau 	 */
38106c8d8eccSSepherosa Ziehau 	if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
38116c8d8eccSSepherosa Ziehau 		if (!sc->bnx_link) {
38126c8d8eccSSepherosa Ziehau 			sc->bnx_link++;
38136c8d8eccSSepherosa Ziehau 			if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
38146c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
38156c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_TBI_SEND_CFGS);
38166c8d8eccSSepherosa Ziehau 			}
38176c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
38186c8d8eccSSepherosa Ziehau 
38196c8d8eccSSepherosa Ziehau 			if (bootverbose)
38206c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link UP\n");
38216c8d8eccSSepherosa Ziehau 
38226c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_UP;
38236c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
38246c8d8eccSSepherosa Ziehau 		}
38256c8d8eccSSepherosa Ziehau 	} else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
38266c8d8eccSSepherosa Ziehau 		if (sc->bnx_link) {
38276c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
38286c8d8eccSSepherosa Ziehau 
38296c8d8eccSSepherosa Ziehau 			if (bootverbose)
38306c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link DOWN\n");
38316c8d8eccSSepherosa Ziehau 
38326c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_DOWN;
38336c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
38346c8d8eccSSepherosa Ziehau 		}
38356c8d8eccSSepherosa Ziehau 	}
38366c8d8eccSSepherosa Ziehau 
38376c8d8eccSSepherosa Ziehau #undef PCS_ENCODE_ERR
38386c8d8eccSSepherosa Ziehau 
38396c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
38406c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
38416c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
38426c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
38436c8d8eccSSepherosa Ziehau }
38446c8d8eccSSepherosa Ziehau 
38456c8d8eccSSepherosa Ziehau static void
38466c8d8eccSSepherosa Ziehau bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
38476c8d8eccSSepherosa Ziehau {
38486c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
38496c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
38506c8d8eccSSepherosa Ziehau 
38516c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
38526c8d8eccSSepherosa Ziehau 	bnx_miibus_statchg(sc->bnx_dev);
38536c8d8eccSSepherosa Ziehau 
38546c8d8eccSSepherosa Ziehau 	if (bootverbose) {
38556c8d8eccSSepherosa Ziehau 		if (sc->bnx_link)
38566c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
38576c8d8eccSSepherosa Ziehau 		else
38586c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
38596c8d8eccSSepherosa Ziehau 	}
38606c8d8eccSSepherosa Ziehau 
38616c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
38626c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
38636c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
38646c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
38656c8d8eccSSepherosa Ziehau }
38666c8d8eccSSepherosa Ziehau 
38676c8d8eccSSepherosa Ziehau static void
38686c8d8eccSSepherosa Ziehau bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
38696c8d8eccSSepherosa Ziehau {
38706c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
38716c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
38726c8d8eccSSepherosa Ziehau 
38736c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
38746c8d8eccSSepherosa Ziehau 
38756c8d8eccSSepherosa Ziehau 	if (!sc->bnx_link &&
38766c8d8eccSSepherosa Ziehau 	    (mii->mii_media_status & IFM_ACTIVE) &&
38776c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38786c8d8eccSSepherosa Ziehau 		sc->bnx_link++;
38796c8d8eccSSepherosa Ziehau 		if (bootverbose)
38806c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
38816c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_link &&
38826c8d8eccSSepherosa Ziehau 	    (!(mii->mii_media_status & IFM_ACTIVE) ||
38836c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38846c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
38856c8d8eccSSepherosa Ziehau 		if (bootverbose)
38866c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
38876c8d8eccSSepherosa Ziehau 	}
38886c8d8eccSSepherosa Ziehau 
38896c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
38906c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
38916c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
38926c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
38936c8d8eccSSepherosa Ziehau }
38946c8d8eccSSepherosa Ziehau 
38956c8d8eccSSepherosa Ziehau static int
38966c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
38976c8d8eccSSepherosa Ziehau {
38986c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
38996c8d8eccSSepherosa Ziehau 
39006c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39016c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_ticks,
39026c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
39036c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_CHG);
39046c8d8eccSSepherosa Ziehau }
39056c8d8eccSSepherosa Ziehau 
39066c8d8eccSSepherosa Ziehau static int
39076c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
39086c8d8eccSSepherosa Ziehau {
39096c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39106c8d8eccSSepherosa Ziehau 
39116c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39126c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_ticks,
39136c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
39146c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_CHG);
39156c8d8eccSSepherosa Ziehau }
39166c8d8eccSSepherosa Ziehau 
39176c8d8eccSSepherosa Ziehau static int
39186c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
39196c8d8eccSSepherosa Ziehau {
39206c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39216c8d8eccSSepherosa Ziehau 
39226c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39236c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds,
39246c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
39256c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_CHG);
39266c8d8eccSSepherosa Ziehau }
39276c8d8eccSSepherosa Ziehau 
39286c8d8eccSSepherosa Ziehau static int
39296c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
39306c8d8eccSSepherosa Ziehau {
39316c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39326c8d8eccSSepherosa Ziehau 
39336c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39346c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds,
39356c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
39366c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_CHG);
39376c8d8eccSSepherosa Ziehau }
39386c8d8eccSSepherosa Ziehau 
39396c8d8eccSSepherosa Ziehau static int
39406c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
39416c8d8eccSSepherosa Ziehau {
39426c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39436c8d8eccSSepherosa Ziehau 
39446c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39456c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds_int,
39466c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
39476c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_INT_CHG);
39486c8d8eccSSepherosa Ziehau }
39496c8d8eccSSepherosa Ziehau 
39506c8d8eccSSepherosa Ziehau static int
39516c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
39526c8d8eccSSepherosa Ziehau {
39536c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39546c8d8eccSSepherosa Ziehau 
39556c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
39566c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds_int,
39576c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
39586c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_INT_CHG);
39596c8d8eccSSepherosa Ziehau }
39606c8d8eccSSepherosa Ziehau 
39616c8d8eccSSepherosa Ziehau static int
39626c8d8eccSSepherosa Ziehau bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
39636c8d8eccSSepherosa Ziehau     int coal_min, int coal_max, uint32_t coal_chg_mask)
39646c8d8eccSSepherosa Ziehau {
39656c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
39666c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
39676c8d8eccSSepherosa Ziehau 	int error = 0, v;
39686c8d8eccSSepherosa Ziehau 
3969329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
39706c8d8eccSSepherosa Ziehau 
39716c8d8eccSSepherosa Ziehau 	v = *coal;
39726c8d8eccSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &v, 0, req);
39736c8d8eccSSepherosa Ziehau 	if (!error && req->newptr != NULL) {
39746c8d8eccSSepherosa Ziehau 		if (v < coal_min || v > coal_max) {
39756c8d8eccSSepherosa Ziehau 			error = EINVAL;
39766c8d8eccSSepherosa Ziehau 		} else {
39776c8d8eccSSepherosa Ziehau 			*coal = v;
39786c8d8eccSSepherosa Ziehau 			sc->bnx_coal_chg |= coal_chg_mask;
3979f5014362SSepherosa Ziehau 
3980f5014362SSepherosa Ziehau 			/* Commit changes */
3981f5014362SSepherosa Ziehau 			bnx_coal_change(sc);
39826c8d8eccSSepherosa Ziehau 		}
39836c8d8eccSSepherosa Ziehau 	}
39846c8d8eccSSepherosa Ziehau 
3985329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
39866c8d8eccSSepherosa Ziehau 	return error;
39876c8d8eccSSepherosa Ziehau }
39886c8d8eccSSepherosa Ziehau 
39896c8d8eccSSepherosa Ziehau static void
39906c8d8eccSSepherosa Ziehau bnx_coal_change(struct bnx_softc *sc)
39916c8d8eccSSepherosa Ziehau {
39926c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
39936c8d8eccSSepherosa Ziehau 
3994329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
39956c8d8eccSSepherosa Ziehau 
39966c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
39976c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
39986c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_ticks);
39996c8d8eccSSepherosa Ziehau 		DELAY(10);
4000e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
40016c8d8eccSSepherosa Ziehau 
40026c8d8eccSSepherosa Ziehau 		if (bootverbose) {
40036c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_ticks -> %u\n",
40046c8d8eccSSepherosa Ziehau 				  sc->bnx_rx_coal_ticks);
40056c8d8eccSSepherosa Ziehau 		}
40066c8d8eccSSepherosa Ziehau 	}
40076c8d8eccSSepherosa Ziehau 
40086c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
40096c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
40106c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_ticks);
40116c8d8eccSSepherosa Ziehau 		DELAY(10);
4012e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_TX_COAL_TICKS);
40136c8d8eccSSepherosa Ziehau 
40146c8d8eccSSepherosa Ziehau 		if (bootverbose) {
40156c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_ticks -> %u\n",
40166c8d8eccSSepherosa Ziehau 				  sc->bnx_tx_coal_ticks);
40176c8d8eccSSepherosa Ziehau 		}
40186c8d8eccSSepherosa Ziehau 	}
40196c8d8eccSSepherosa Ziehau 
40206c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
40216c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS,
40226c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds);
40236c8d8eccSSepherosa Ziehau 		DELAY(10);
4024e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
40256c8d8eccSSepherosa Ziehau 
40266c8d8eccSSepherosa Ziehau 		if (bootverbose) {
40276c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_bds -> %u\n",
40286c8d8eccSSepherosa Ziehau 				  sc->bnx_rx_coal_bds);
40296c8d8eccSSepherosa Ziehau 		}
40306c8d8eccSSepherosa Ziehau 	}
40316c8d8eccSSepherosa Ziehau 
40326c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
40336c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS,
40346c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds);
40356c8d8eccSSepherosa Ziehau 		DELAY(10);
4036e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS);
40376c8d8eccSSepherosa Ziehau 
40386c8d8eccSSepherosa Ziehau 		if (bootverbose) {
4039cc98a7c2SSepherosa Ziehau 			if_printf(ifp, "tx_coal_bds -> %u\n",
40406c8d8eccSSepherosa Ziehau 				  sc->bnx_tx_coal_bds);
40416c8d8eccSSepherosa Ziehau 		}
40426c8d8eccSSepherosa Ziehau 	}
40436c8d8eccSSepherosa Ziehau 
40446c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
40456c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
40466c8d8eccSSepherosa Ziehau 		    sc->bnx_rx_coal_bds_int);
40476c8d8eccSSepherosa Ziehau 		DELAY(10);
4048e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT);
40496c8d8eccSSepherosa Ziehau 
40506c8d8eccSSepherosa Ziehau 		if (bootverbose) {
40516c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_bds_int -> %u\n",
40526c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds_int);
40536c8d8eccSSepherosa Ziehau 		}
40546c8d8eccSSepherosa Ziehau 	}
40556c8d8eccSSepherosa Ziehau 
40566c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
40576c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
40586c8d8eccSSepherosa Ziehau 		    sc->bnx_tx_coal_bds_int);
40596c8d8eccSSepherosa Ziehau 		DELAY(10);
4060e594b5c4SSepherosa Ziehau 		CSR_READ_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT);
40616c8d8eccSSepherosa Ziehau 
40626c8d8eccSSepherosa Ziehau 		if (bootverbose) {
40636c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_bds_int -> %u\n",
40646c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds_int);
40656c8d8eccSSepherosa Ziehau 		}
40666c8d8eccSSepherosa Ziehau 	}
40676c8d8eccSSepherosa Ziehau 
40686c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
40696c8d8eccSSepherosa Ziehau }
40706c8d8eccSSepherosa Ziehau 
40716c8d8eccSSepherosa Ziehau static void
4072f33ac8a4SSepherosa Ziehau bnx_check_intr(void *xintr)
4073df9ccc98SSepherosa Ziehau {
4074f33ac8a4SSepherosa Ziehau 	struct bnx_intr_data *intr = xintr;
4075f33ac8a4SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret;
4076f33ac8a4SSepherosa Ziehau 	struct bnx_tx_ring *txr;
4077f33ac8a4SSepherosa Ziehau 	struct ifnet *ifp;
4078df9ccc98SSepherosa Ziehau 
4079f33ac8a4SSepherosa Ziehau 	lwkt_serialize_enter(intr->bnx_intr_serialize);
4080df9ccc98SSepherosa Ziehau 
4081f33ac8a4SSepherosa Ziehau 	KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4082df9ccc98SSepherosa Ziehau 
4083f33ac8a4SSepherosa Ziehau 	ifp = &intr->bnx_sc->arpcom.ac_if;
408439a8d43aSSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4085f33ac8a4SSepherosa Ziehau 		lwkt_serialize_exit(intr->bnx_intr_serialize);
4086df9ccc98SSepherosa Ziehau 		return;
4087df9ccc98SSepherosa Ziehau 	}
4088df9ccc98SSepherosa Ziehau 
4089f33ac8a4SSepherosa Ziehau 	txr = intr->bnx_txr;
4090f33ac8a4SSepherosa Ziehau 	ret = intr->bnx_ret;
4091f33ac8a4SSepherosa Ziehau 
40923a16b7b8SSepherosa Ziehau 	if (*ret->bnx_rx_considx != ret->bnx_rx_saved_considx ||
40933a16b7b8SSepherosa Ziehau 	    *txr->bnx_tx_considx != txr->bnx_tx_saved_considx) {
4094f33ac8a4SSepherosa Ziehau 		if (intr->bnx_rx_check_considx == ret->bnx_rx_saved_considx &&
4095f33ac8a4SSepherosa Ziehau 		    intr->bnx_tx_check_considx == txr->bnx_tx_saved_considx) {
4096f33ac8a4SSepherosa Ziehau 			if (!intr->bnx_intr_maylose) {
4097f33ac8a4SSepherosa Ziehau 				intr->bnx_intr_maylose = TRUE;
4098df9ccc98SSepherosa Ziehau 				goto done;
4099df9ccc98SSepherosa Ziehau 			}
4100df9ccc98SSepherosa Ziehau 			if (bootverbose)
4101df9ccc98SSepherosa Ziehau 				if_printf(ifp, "lost interrupt\n");
4102f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_func(intr->bnx_intr_arg);
4103df9ccc98SSepherosa Ziehau 		}
4104df9ccc98SSepherosa Ziehau 	}
4105f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_maylose = FALSE;
4106f33ac8a4SSepherosa Ziehau 	intr->bnx_rx_check_considx = ret->bnx_rx_saved_considx;
4107f33ac8a4SSepherosa Ziehau 	intr->bnx_tx_check_considx = txr->bnx_tx_saved_considx;
4108df9ccc98SSepherosa Ziehau 
4109df9ccc98SSepherosa Ziehau done:
4110f33ac8a4SSepherosa Ziehau 	callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4111f33ac8a4SSepherosa Ziehau 	    intr->bnx_intr_check, intr);
4112f33ac8a4SSepherosa Ziehau 	lwkt_serialize_exit(intr->bnx_intr_serialize);
4113df9ccc98SSepherosa Ziehau }
4114df9ccc98SSepherosa Ziehau 
4115df9ccc98SSepherosa Ziehau static void
41166c8d8eccSSepherosa Ziehau bnx_enable_intr(struct bnx_softc *sc)
41176c8d8eccSSepherosa Ziehau {
41186c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
41194fa38985SSepherosa Ziehau 	struct bnx_intr_data *intr;
4120f33ac8a4SSepherosa Ziehau 	int i;
41216c8d8eccSSepherosa Ziehau 
4122f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4123f33ac8a4SSepherosa Ziehau 		lwkt_serialize_handler_enable(
4124f33ac8a4SSepherosa Ziehau 		    sc->bnx_intr_data[i].bnx_intr_serialize);
4125f33ac8a4SSepherosa Ziehau 	}
41266c8d8eccSSepherosa Ziehau 
41276c8d8eccSSepherosa Ziehau 	/*
41286c8d8eccSSepherosa Ziehau 	 * Enable interrupt.
41296c8d8eccSSepherosa Ziehau 	 */
41304fa38985SSepherosa Ziehau 	intr = &sc->bnx_intr_data[0]; /* XXX */
41314fa38985SSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, (*intr->bnx_saved_status_tag) << 24);
41326c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
41336c8d8eccSSepherosa Ziehau 		/* XXX Linux driver */
41344fa38985SSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_IRQ0_LO,
41354fa38985SSepherosa Ziehau 		    (*intr->bnx_saved_status_tag) << 24);
41366c8d8eccSSepherosa Ziehau 	}
41376c8d8eccSSepherosa Ziehau 
41386c8d8eccSSepherosa Ziehau 	/*
41396c8d8eccSSepherosa Ziehau 	 * Unmask the interrupt when we stop polling.
41406c8d8eccSSepherosa Ziehau 	 */
41416c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
41426c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
41436c8d8eccSSepherosa Ziehau 
41446c8d8eccSSepherosa Ziehau 	/*
41456c8d8eccSSepherosa Ziehau 	 * Trigger another interrupt, since above writing
41466c8d8eccSSepherosa Ziehau 	 * to interrupt mailbox0 may acknowledge pending
41476c8d8eccSSepherosa Ziehau 	 * interrupt.
41486c8d8eccSSepherosa Ziehau 	 */
41496c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4150df9ccc98SSepherosa Ziehau 
4151df9ccc98SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4152df9ccc98SSepherosa Ziehau 		if (bootverbose)
4153df9ccc98SSepherosa Ziehau 			if_printf(ifp, "status tag bug workaround\n");
4154df9ccc98SSepherosa Ziehau 
4155f33ac8a4SSepherosa Ziehau 		for (i = 0; i < sc->bnx_intr_cnt; ++i) {
41564fa38985SSepherosa Ziehau 			intr = &sc->bnx_intr_data[i];
4157f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_maylose = FALSE;
4158f33ac8a4SSepherosa Ziehau 			intr->bnx_rx_check_considx = 0;
4159f33ac8a4SSepherosa Ziehau 			intr->bnx_tx_check_considx = 0;
4160f33ac8a4SSepherosa Ziehau 			callout_reset_bycpu(&intr->bnx_intr_timer,
4161f33ac8a4SSepherosa Ziehau 			    BNX_INTR_CKINTVL, intr->bnx_intr_check, intr,
4162f33ac8a4SSepherosa Ziehau 			    intr->bnx_intr_cpuid);
4163f33ac8a4SSepherosa Ziehau 		}
4164df9ccc98SSepherosa Ziehau 	}
41656c8d8eccSSepherosa Ziehau }
41666c8d8eccSSepherosa Ziehau 
41676c8d8eccSSepherosa Ziehau static void
41686c8d8eccSSepherosa Ziehau bnx_disable_intr(struct bnx_softc *sc)
41696c8d8eccSSepherosa Ziehau {
4170f33ac8a4SSepherosa Ziehau 	int i;
4171f33ac8a4SSepherosa Ziehau 
4172f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4173f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4174f33ac8a4SSepherosa Ziehau 
4175f33ac8a4SSepherosa Ziehau 		callout_stop(&intr->bnx_intr_timer);
4176f33ac8a4SSepherosa Ziehau 		intr->bnx_intr_maylose = FALSE;
4177f33ac8a4SSepherosa Ziehau 		intr->bnx_rx_check_considx = 0;
4178f33ac8a4SSepherosa Ziehau 		intr->bnx_tx_check_considx = 0;
4179f33ac8a4SSepherosa Ziehau 	}
41806c8d8eccSSepherosa Ziehau 
41816c8d8eccSSepherosa Ziehau 	/*
41826c8d8eccSSepherosa Ziehau 	 * Mask the interrupt when we start polling.
41836c8d8eccSSepherosa Ziehau 	 */
41846c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
41856c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
41866c8d8eccSSepherosa Ziehau 
41876c8d8eccSSepherosa Ziehau 	/*
41886c8d8eccSSepherosa Ziehau 	 * Acknowledge possible asserted interrupt.
41896c8d8eccSSepherosa Ziehau 	 */
41906c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
41916c8d8eccSSepherosa Ziehau 
4192f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4193f33ac8a4SSepherosa Ziehau 		lwkt_serialize_handler_disable(
4194f33ac8a4SSepherosa Ziehau 		    sc->bnx_intr_data[i].bnx_intr_serialize);
4195f33ac8a4SSepherosa Ziehau 	}
41966c8d8eccSSepherosa Ziehau }
41976c8d8eccSSepherosa Ziehau 
41986c8d8eccSSepherosa Ziehau static int
41996c8d8eccSSepherosa Ziehau bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
42006c8d8eccSSepherosa Ziehau {
42016c8d8eccSSepherosa Ziehau 	uint32_t mac_addr;
42026c8d8eccSSepherosa Ziehau 	int ret = 1;
42036c8d8eccSSepherosa Ziehau 
42046c8d8eccSSepherosa Ziehau 	mac_addr = bnx_readmem_ind(sc, 0x0c14);
42056c8d8eccSSepherosa Ziehau 	if ((mac_addr >> 16) == 0x484b) {
42066c8d8eccSSepherosa Ziehau 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
42076c8d8eccSSepherosa Ziehau 		ether_addr[1] = (uint8_t)mac_addr;
42086c8d8eccSSepherosa Ziehau 		mac_addr = bnx_readmem_ind(sc, 0x0c18);
42096c8d8eccSSepherosa Ziehau 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
42106c8d8eccSSepherosa Ziehau 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
42116c8d8eccSSepherosa Ziehau 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
42126c8d8eccSSepherosa Ziehau 		ether_addr[5] = (uint8_t)mac_addr;
42136c8d8eccSSepherosa Ziehau 		ret = 0;
42146c8d8eccSSepherosa Ziehau 	}
42156c8d8eccSSepherosa Ziehau 	return ret;
42166c8d8eccSSepherosa Ziehau }
42176c8d8eccSSepherosa Ziehau 
42186c8d8eccSSepherosa Ziehau static int
42196c8d8eccSSepherosa Ziehau bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
42206c8d8eccSSepherosa Ziehau {
42216c8d8eccSSepherosa Ziehau 	int mac_offset = BGE_EE_MAC_OFFSET;
42226c8d8eccSSepherosa Ziehau 
422380969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
422480969639SSepherosa Ziehau 		int f;
422580969639SSepherosa Ziehau 
422680969639SSepherosa Ziehau 		f = pci_get_function(sc->bnx_dev);
422780969639SSepherosa Ziehau 		if (f & 1)
422880969639SSepherosa Ziehau 			mac_offset = BGE_EE_MAC_OFFSET_5717;
422980969639SSepherosa Ziehau 		if (f > 1)
423080969639SSepherosa Ziehau 			mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
423180969639SSepherosa Ziehau 	}
42326c8d8eccSSepherosa Ziehau 
42336c8d8eccSSepherosa Ziehau 	return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
42346c8d8eccSSepherosa Ziehau }
42356c8d8eccSSepherosa Ziehau 
42366c8d8eccSSepherosa Ziehau static int
42376c8d8eccSSepherosa Ziehau bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
42386c8d8eccSSepherosa Ziehau {
42396c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
42406c8d8eccSSepherosa Ziehau 		return 1;
42416c8d8eccSSepherosa Ziehau 
42426c8d8eccSSepherosa Ziehau 	return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
42436c8d8eccSSepherosa Ziehau 			       ETHER_ADDR_LEN);
42446c8d8eccSSepherosa Ziehau }
42456c8d8eccSSepherosa Ziehau 
42466c8d8eccSSepherosa Ziehau static int
42476c8d8eccSSepherosa Ziehau bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
42486c8d8eccSSepherosa Ziehau {
42496c8d8eccSSepherosa Ziehau 	static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
42506c8d8eccSSepherosa Ziehau 		/* NOTE: Order is critical */
42516c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_mem,
42526c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_nvram,
42536c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_eeprom,
42546c8d8eccSSepherosa Ziehau 		NULL
42556c8d8eccSSepherosa Ziehau 	};
42566c8d8eccSSepherosa Ziehau 	const bnx_eaddr_fcn_t *func;
42576c8d8eccSSepherosa Ziehau 
42586c8d8eccSSepherosa Ziehau 	for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
42596c8d8eccSSepherosa Ziehau 		if ((*func)(sc, eaddr) == 0)
42606c8d8eccSSepherosa Ziehau 			break;
42616c8d8eccSSepherosa Ziehau 	}
42626c8d8eccSSepherosa Ziehau 	return (*func == NULL ? ENXIO : 0);
42636c8d8eccSSepherosa Ziehau }
42646c8d8eccSSepherosa Ziehau 
42656c8d8eccSSepherosa Ziehau /*
42666c8d8eccSSepherosa Ziehau  * NOTE: 'm' is not freed upon failure
42676c8d8eccSSepherosa Ziehau  */
42686c8d8eccSSepherosa Ziehau struct mbuf *
42696c8d8eccSSepherosa Ziehau bnx_defrag_shortdma(struct mbuf *m)
42706c8d8eccSSepherosa Ziehau {
42716c8d8eccSSepherosa Ziehau 	struct mbuf *n;
42726c8d8eccSSepherosa Ziehau 	int found;
42736c8d8eccSSepherosa Ziehau 
42746c8d8eccSSepherosa Ziehau 	/*
42756c8d8eccSSepherosa Ziehau 	 * If device receive two back-to-back send BDs with less than
42766c8d8eccSSepherosa Ziehau 	 * or equal to 8 total bytes then the device may hang.  The two
42776c8d8eccSSepherosa Ziehau 	 * back-to-back send BDs must in the same frame for this failure
42786c8d8eccSSepherosa Ziehau 	 * to occur.  Scan mbuf chains and see whether two back-to-back
42796c8d8eccSSepherosa Ziehau 	 * send BDs are there.  If this is the case, allocate new mbuf
42806c8d8eccSSepherosa Ziehau 	 * and copy the frame to workaround the silicon bug.
42816c8d8eccSSepherosa Ziehau 	 */
42826c8d8eccSSepherosa Ziehau 	for (n = m, found = 0; n != NULL; n = n->m_next) {
42836c8d8eccSSepherosa Ziehau 		if (n->m_len < 8) {
42846c8d8eccSSepherosa Ziehau 			found++;
42856c8d8eccSSepherosa Ziehau 			if (found > 1)
42866c8d8eccSSepherosa Ziehau 				break;
42876c8d8eccSSepherosa Ziehau 			continue;
42886c8d8eccSSepherosa Ziehau 		}
42896c8d8eccSSepherosa Ziehau 		found = 0;
42906c8d8eccSSepherosa Ziehau 	}
42916c8d8eccSSepherosa Ziehau 
42926c8d8eccSSepherosa Ziehau 	if (found > 1)
42936c8d8eccSSepherosa Ziehau 		n = m_defrag(m, MB_DONTWAIT);
42946c8d8eccSSepherosa Ziehau 	else
42956c8d8eccSSepherosa Ziehau 		n = m;
42966c8d8eccSSepherosa Ziehau 	return n;
42976c8d8eccSSepherosa Ziehau }
42986c8d8eccSSepherosa Ziehau 
42996c8d8eccSSepherosa Ziehau static void
43006c8d8eccSSepherosa Ziehau bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
43016c8d8eccSSepherosa Ziehau {
43026c8d8eccSSepherosa Ziehau 	int i;
43036c8d8eccSSepherosa Ziehau 
43046c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, reg, bit);
43056c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
43066c8d8eccSSepherosa Ziehau 		if ((CSR_READ_4(sc, reg) & bit) == 0)
43076c8d8eccSSepherosa Ziehau 			return;
43086c8d8eccSSepherosa Ziehau 		DELAY(100);
43096c8d8eccSSepherosa Ziehau 	}
43106c8d8eccSSepherosa Ziehau }
43116c8d8eccSSepherosa Ziehau 
43126c8d8eccSSepherosa Ziehau static void
43136c8d8eccSSepherosa Ziehau bnx_link_poll(struct bnx_softc *sc)
43146c8d8eccSSepherosa Ziehau {
43156c8d8eccSSepherosa Ziehau 	uint32_t status;
43166c8d8eccSSepherosa Ziehau 
43176c8d8eccSSepherosa Ziehau 	status = CSR_READ_4(sc, BGE_MAC_STS);
43186c8d8eccSSepherosa Ziehau 	if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
43196c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt = 0;
43206c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd(sc, status);
43216c8d8eccSSepherosa Ziehau 	}
43226c8d8eccSSepherosa Ziehau }
43236c8d8eccSSepherosa Ziehau 
43246c8d8eccSSepherosa Ziehau static void
43256c8d8eccSSepherosa Ziehau bnx_enable_msi(struct bnx_softc *sc)
43266c8d8eccSSepherosa Ziehau {
43276c8d8eccSSepherosa Ziehau 	uint32_t msi_mode;
43286c8d8eccSSepherosa Ziehau 
43296c8d8eccSSepherosa Ziehau 	msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
43306c8d8eccSSepherosa Ziehau 	msi_mode |= BGE_MSIMODE_ENABLE;
43316c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
43326c8d8eccSSepherosa Ziehau 		/*
43336c8d8eccSSepherosa Ziehau 		 * NOTE:
43346c8d8eccSSepherosa Ziehau 		 * 5718-PG105-R says that "one shot" mode
43356c8d8eccSSepherosa Ziehau 		 * does not work if MSI is used, however,
43366c8d8eccSSepherosa Ziehau 		 * it obviously works.
43376c8d8eccSSepherosa Ziehau 		 */
43386c8d8eccSSepherosa Ziehau 		msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
43396c8d8eccSSepherosa Ziehau 	}
43406c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
43416c8d8eccSSepherosa Ziehau }
43426c8d8eccSSepherosa Ziehau 
43436c8d8eccSSepherosa Ziehau static uint32_t
43446c8d8eccSSepherosa Ziehau bnx_dma_swap_options(struct bnx_softc *sc)
43456c8d8eccSSepherosa Ziehau {
43466c8d8eccSSepherosa Ziehau 	uint32_t dma_options;
43476c8d8eccSSepherosa Ziehau 
43486c8d8eccSSepherosa Ziehau 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
43496c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
43506c8d8eccSSepherosa Ziehau #if BYTE_ORDER == BIG_ENDIAN
43516c8d8eccSSepherosa Ziehau 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
43526c8d8eccSSepherosa Ziehau #endif
4353b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
4354b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
43556c8d8eccSSepherosa Ziehau 		dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
43566c8d8eccSSepherosa Ziehau 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
43576c8d8eccSSepherosa Ziehau 		    BGE_MODECTL_HTX2B_ENABLE;
43586c8d8eccSSepherosa Ziehau 	}
43596c8d8eccSSepherosa Ziehau 	return dma_options;
43606c8d8eccSSepherosa Ziehau }
436166deb1c1SSepherosa Ziehau 
436266deb1c1SSepherosa Ziehau static int
436333a04907SSepherosa Ziehau bnx_setup_tso(struct bnx_tx_ring *txr, struct mbuf **mp,
436466deb1c1SSepherosa Ziehau     uint16_t *mss0, uint16_t *flags0)
436566deb1c1SSepherosa Ziehau {
436666deb1c1SSepherosa Ziehau 	struct mbuf *m;
436766deb1c1SSepherosa Ziehau 	struct ip *ip;
436866deb1c1SSepherosa Ziehau 	struct tcphdr *th;
436966deb1c1SSepherosa Ziehau 	int thoff, iphlen, hoff, hlen;
437066deb1c1SSepherosa Ziehau 	uint16_t flags, mss;
437166deb1c1SSepherosa Ziehau 
4372f7a2269aSSepherosa Ziehau 	m = *mp;
4373f7a2269aSSepherosa Ziehau 	KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
4374f7a2269aSSepherosa Ziehau 
4375f7a2269aSSepherosa Ziehau 	hoff = m->m_pkthdr.csum_lhlen;
4376f7a2269aSSepherosa Ziehau 	iphlen = m->m_pkthdr.csum_iphlen;
4377f7a2269aSSepherosa Ziehau 	thoff = m->m_pkthdr.csum_thlen;
4378f7a2269aSSepherosa Ziehau 
4379f7a2269aSSepherosa Ziehau 	KASSERT(hoff > 0, ("invalid ether header len"));
4380f7a2269aSSepherosa Ziehau 	KASSERT(iphlen > 0, ("invalid ip header len"));
4381f7a2269aSSepherosa Ziehau 	KASSERT(thoff > 0, ("invalid tcp header len"));
4382f7a2269aSSepherosa Ziehau 
4383f7a2269aSSepherosa Ziehau 	if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
4384f7a2269aSSepherosa Ziehau 		m = m_pullup(m, hoff + iphlen + thoff);
4385f7a2269aSSepherosa Ziehau 		if (m == NULL) {
4386f7a2269aSSepherosa Ziehau 			*mp = NULL;
4387f7a2269aSSepherosa Ziehau 			return ENOBUFS;
4388f7a2269aSSepherosa Ziehau 		}
4389f7a2269aSSepherosa Ziehau 		*mp = m;
4390f7a2269aSSepherosa Ziehau 	}
4391f7a2269aSSepherosa Ziehau 	ip = mtodoff(m, struct ip *, hoff);
4392f7a2269aSSepherosa Ziehau 	th = mtodoff(m, struct tcphdr *, hoff + iphlen);
4393f7a2269aSSepherosa Ziehau 
4394f0336d39SSepherosa Ziehau 	mss = m->m_pkthdr.tso_segsz;
439566deb1c1SSepherosa Ziehau 	flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
439666deb1c1SSepherosa Ziehau 
439766deb1c1SSepherosa Ziehau 	ip->ip_len = htons(mss + iphlen + thoff);
439866deb1c1SSepherosa Ziehau 	th->th_sum = 0;
439966deb1c1SSepherosa Ziehau 
440066deb1c1SSepherosa Ziehau 	hlen = (iphlen + thoff) >> 2;
440166deb1c1SSepherosa Ziehau 	mss |= ((hlen & 0x3) << 14);
440266deb1c1SSepherosa Ziehau 	flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);
440366deb1c1SSepherosa Ziehau 
440466deb1c1SSepherosa Ziehau 	*mss0 = mss;
440566deb1c1SSepherosa Ziehau 	*flags0 = flags;
440666deb1c1SSepherosa Ziehau 
440766deb1c1SSepherosa Ziehau 	return 0;
440866deb1c1SSepherosa Ziehau }
440933a04907SSepherosa Ziehau 
441033a04907SSepherosa Ziehau static int
441133a04907SSepherosa Ziehau bnx_create_tx_ring(struct bnx_tx_ring *txr)
441233a04907SSepherosa Ziehau {
441333a04907SSepherosa Ziehau 	bus_size_t txmaxsz, txmaxsegsz;
441433a04907SSepherosa Ziehau 	int i, error;
441533a04907SSepherosa Ziehau 
4416329f9016SSepherosa Ziehau 	lwkt_serialize_init(&txr->bnx_tx_serialize);
4417329f9016SSepherosa Ziehau 
441833a04907SSepherosa Ziehau 	/*
441933a04907SSepherosa Ziehau 	 * Create DMA tag and maps for TX mbufs.
442033a04907SSepherosa Ziehau 	 */
442133a04907SSepherosa Ziehau 	if (txr->bnx_sc->bnx_flags & BNX_FLAG_TSO)
442233a04907SSepherosa Ziehau 		txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
442333a04907SSepherosa Ziehau 	else
442433a04907SSepherosa Ziehau 		txmaxsz = BNX_JUMBO_FRAMELEN;
442533a04907SSepherosa Ziehau 	if (txr->bnx_sc->bnx_asicrev == BGE_ASICREV_BCM57766)
442633a04907SSepherosa Ziehau 		txmaxsegsz = MCLBYTES;
442733a04907SSepherosa Ziehau 	else
442833a04907SSepherosa Ziehau 		txmaxsegsz = PAGE_SIZE;
442933a04907SSepherosa Ziehau 	error = bus_dma_tag_create(txr->bnx_sc->bnx_cdata.bnx_parent_tag,
443033a04907SSepherosa Ziehau 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
443133a04907SSepherosa Ziehau 	    txmaxsz, BNX_NSEG_NEW, txmaxsegsz,
443233a04907SSepherosa Ziehau 	    BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
443333a04907SSepherosa Ziehau 	    &txr->bnx_tx_mtag);
443433a04907SSepherosa Ziehau 	if (error) {
443533a04907SSepherosa Ziehau 		device_printf(txr->bnx_sc->bnx_dev,
4436beedf5beSSepherosa Ziehau 		    "could not create TX mbuf DMA tag\n");
443733a04907SSepherosa Ziehau 		return error;
443833a04907SSepherosa Ziehau 	}
443933a04907SSepherosa Ziehau 
444033a04907SSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
444133a04907SSepherosa Ziehau 		error = bus_dmamap_create(txr->bnx_tx_mtag,
444233a04907SSepherosa Ziehau 		    BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
4443fa4b1067SSepherosa Ziehau 		    &txr->bnx_tx_buf[i].bnx_tx_dmamap);
444433a04907SSepherosa Ziehau 		if (error) {
444533a04907SSepherosa Ziehau 			int j;
444633a04907SSepherosa Ziehau 
444733a04907SSepherosa Ziehau 			for (j = 0; j < i; ++j) {
444833a04907SSepherosa Ziehau 				bus_dmamap_destroy(txr->bnx_tx_mtag,
4449fa4b1067SSepherosa Ziehau 				    txr->bnx_tx_buf[j].bnx_tx_dmamap);
445033a04907SSepherosa Ziehau 			}
445133a04907SSepherosa Ziehau 			bus_dma_tag_destroy(txr->bnx_tx_mtag);
445233a04907SSepherosa Ziehau 			txr->bnx_tx_mtag = NULL;
445333a04907SSepherosa Ziehau 
445433a04907SSepherosa Ziehau 			device_printf(txr->bnx_sc->bnx_dev,
4455beedf5beSSepherosa Ziehau 			    "could not create TX mbuf DMA map\n");
445633a04907SSepherosa Ziehau 			return error;
445733a04907SSepherosa Ziehau 		}
445833a04907SSepherosa Ziehau 	}
445933a04907SSepherosa Ziehau 
446033a04907SSepherosa Ziehau 	/*
446133a04907SSepherosa Ziehau 	 * Create DMA stuffs for TX ring.
446233a04907SSepherosa Ziehau 	 */
446333a04907SSepherosa Ziehau 	error = bnx_dma_block_alloc(txr->bnx_sc, BGE_TX_RING_SZ,
4464beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_tag,
4465beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_map,
4466beedf5beSSepherosa Ziehau 	    (void *)&txr->bnx_tx_ring,
4467beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_paddr);
446833a04907SSepherosa Ziehau 	if (error) {
446933a04907SSepherosa Ziehau 		device_printf(txr->bnx_sc->bnx_dev,
447033a04907SSepherosa Ziehau 		    "could not create TX ring\n");
447133a04907SSepherosa Ziehau 		return error;
447233a04907SSepherosa Ziehau 	}
447333a04907SSepherosa Ziehau 
447479a64343SSepherosa Ziehau 	txr->bnx_tx_flags |= BNX_TX_FLAG_SHORTDMA;
447533a04907SSepherosa Ziehau 	txr->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
447633a04907SSepherosa Ziehau 
447733a04907SSepherosa Ziehau 	return 0;
447833a04907SSepherosa Ziehau }
447933a04907SSepherosa Ziehau 
448033a04907SSepherosa Ziehau static void
448133a04907SSepherosa Ziehau bnx_destroy_tx_ring(struct bnx_tx_ring *txr)
448233a04907SSepherosa Ziehau {
448333a04907SSepherosa Ziehau 	/* Destroy TX mbuf DMA stuffs. */
448433a04907SSepherosa Ziehau 	if (txr->bnx_tx_mtag != NULL) {
448533a04907SSepherosa Ziehau 		int i;
448633a04907SSepherosa Ziehau 
448733a04907SSepherosa Ziehau 		for (i = 0; i < BGE_TX_RING_CNT; i++) {
4488fa4b1067SSepherosa Ziehau 			KKASSERT(txr->bnx_tx_buf[i].bnx_tx_mbuf == NULL);
448933a04907SSepherosa Ziehau 			bus_dmamap_destroy(txr->bnx_tx_mtag,
4490fa4b1067SSepherosa Ziehau 			    txr->bnx_tx_buf[i].bnx_tx_dmamap);
449133a04907SSepherosa Ziehau 		}
449233a04907SSepherosa Ziehau 		bus_dma_tag_destroy(txr->bnx_tx_mtag);
449333a04907SSepherosa Ziehau 	}
449433a04907SSepherosa Ziehau 
449533a04907SSepherosa Ziehau 	/* Destroy TX ring */
449633a04907SSepherosa Ziehau 	bnx_dma_block_free(txr->bnx_tx_ring_tag,
449733a04907SSepherosa Ziehau 	    txr->bnx_tx_ring_map, txr->bnx_tx_ring);
449833a04907SSepherosa Ziehau }
4499aad4de2bSSepherosa Ziehau 
4500aad4de2bSSepherosa Ziehau static int
4501aad4de2bSSepherosa Ziehau bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS)
4502aad4de2bSSepherosa Ziehau {
4503aad4de2bSSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
4504aad4de2bSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
4505aad4de2bSSepherosa Ziehau 	struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
4506aad4de2bSSepherosa Ziehau 	int error, defrag, i;
4507aad4de2bSSepherosa Ziehau 
4508aad4de2bSSepherosa Ziehau 	if (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG)
4509aad4de2bSSepherosa Ziehau 		defrag = 1;
4510aad4de2bSSepherosa Ziehau 	else
4511aad4de2bSSepherosa Ziehau 		defrag = 0;
4512aad4de2bSSepherosa Ziehau 
4513aad4de2bSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &defrag, 0, req);
4514aad4de2bSSepherosa Ziehau 	if (error || req->newptr == NULL)
4515aad4de2bSSepherosa Ziehau 		return error;
4516aad4de2bSSepherosa Ziehau 
4517329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
4518aad4de2bSSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4519aad4de2bSSepherosa Ziehau 		txr = &sc->bnx_tx_ring[i];
4520aad4de2bSSepherosa Ziehau 		if (defrag)
4521aad4de2bSSepherosa Ziehau 			txr->bnx_tx_flags |= BNX_TX_FLAG_FORCE_DEFRAG;
4522aad4de2bSSepherosa Ziehau 		else
4523aad4de2bSSepherosa Ziehau 			txr->bnx_tx_flags &= ~BNX_TX_FLAG_FORCE_DEFRAG;
4524aad4de2bSSepherosa Ziehau 	}
4525329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
4526aad4de2bSSepherosa Ziehau 
4527aad4de2bSSepherosa Ziehau 	return 0;
4528aad4de2bSSepherosa Ziehau }
4529472c99c8SSepherosa Ziehau 
4530472c99c8SSepherosa Ziehau static int
4531472c99c8SSepherosa Ziehau bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS)
4532472c99c8SSepherosa Ziehau {
4533472c99c8SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
4534472c99c8SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
4535472c99c8SSepherosa Ziehau 	struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
4536472c99c8SSepherosa Ziehau 	int error, tx_wreg, i;
4537472c99c8SSepherosa Ziehau 
4538472c99c8SSepherosa Ziehau 	tx_wreg = txr->bnx_tx_wreg;
4539472c99c8SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &tx_wreg, 0, req);
4540472c99c8SSepherosa Ziehau 	if (error || req->newptr == NULL)
4541472c99c8SSepherosa Ziehau 		return error;
4542472c99c8SSepherosa Ziehau 
4543329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
4544472c99c8SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
4545472c99c8SSepherosa Ziehau 		sc->bnx_tx_ring[i].bnx_tx_wreg = tx_wreg;
4546329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
4547472c99c8SSepherosa Ziehau 
4548472c99c8SSepherosa Ziehau 	return 0;
4549472c99c8SSepherosa Ziehau }
4550beedf5beSSepherosa Ziehau 
4551beedf5beSSepherosa Ziehau static int
4552beedf5beSSepherosa Ziehau bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *ret)
4553beedf5beSSepherosa Ziehau {
4554beedf5beSSepherosa Ziehau 	int error;
4555beedf5beSSepherosa Ziehau 
4556329f9016SSepherosa Ziehau 	lwkt_serialize_init(&ret->bnx_rx_ret_serialize);
4557329f9016SSepherosa Ziehau 
4558beedf5beSSepherosa Ziehau 	/*
4559beedf5beSSepherosa Ziehau 	 * Create DMA stuffs for RX return ring.
4560beedf5beSSepherosa Ziehau 	 */
4561beedf5beSSepherosa Ziehau 	error = bnx_dma_block_alloc(ret->bnx_sc,
4562beedf5beSSepherosa Ziehau 	    BGE_RX_RTN_RING_SZ(BNX_RETURN_RING_CNT),
4563beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_tag,
4564beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_map,
4565beedf5beSSepherosa Ziehau 	    (void *)&ret->bnx_rx_ret_ring,
4566beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_paddr);
4567beedf5beSSepherosa Ziehau 	if (error) {
4568beedf5beSSepherosa Ziehau 		device_printf(ret->bnx_sc->bnx_dev,
4569beedf5beSSepherosa Ziehau 		    "could not create RX ret ring\n");
4570beedf5beSSepherosa Ziehau 		return error;
4571beedf5beSSepherosa Ziehau 	}
4572beedf5beSSepherosa Ziehau 
4573beedf5beSSepherosa Ziehau 	/* Shadow standard ring's RX mbuf DMA tag */
4574beedf5beSSepherosa Ziehau 	ret->bnx_rx_mtag = ret->bnx_std->bnx_rx_mtag;
4575beedf5beSSepherosa Ziehau 
4576beedf5beSSepherosa Ziehau 	/*
4577beedf5beSSepherosa Ziehau 	 * Create tmp DMA map for RX mbufs.
4578beedf5beSSepherosa Ziehau 	 */
4579beedf5beSSepherosa Ziehau 	error = bus_dmamap_create(ret->bnx_rx_mtag, BUS_DMA_WAITOK,
4580beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_tmpmap);
4581beedf5beSSepherosa Ziehau 	if (error) {
4582beedf5beSSepherosa Ziehau 		device_printf(ret->bnx_sc->bnx_dev,
4583beedf5beSSepherosa Ziehau 		    "could not create tmp RX mbuf DMA map\n");
4584beedf5beSSepherosa Ziehau 		ret->bnx_rx_mtag = NULL;
4585beedf5beSSepherosa Ziehau 		return error;
4586beedf5beSSepherosa Ziehau 	}
4587beedf5beSSepherosa Ziehau 	return 0;
4588beedf5beSSepherosa Ziehau }
4589beedf5beSSepherosa Ziehau 
4590beedf5beSSepherosa Ziehau static void
4591beedf5beSSepherosa Ziehau bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *ret)
4592beedf5beSSepherosa Ziehau {
4593beedf5beSSepherosa Ziehau 	/* Destroy tmp RX mbuf DMA map */
4594beedf5beSSepherosa Ziehau 	if (ret->bnx_rx_mtag != NULL)
4595beedf5beSSepherosa Ziehau 		bus_dmamap_destroy(ret->bnx_rx_mtag, ret->bnx_rx_tmpmap);
4596beedf5beSSepherosa Ziehau 
4597beedf5beSSepherosa Ziehau 	/* Destroy RX return ring */
4598beedf5beSSepherosa Ziehau 	bnx_dma_block_free(ret->bnx_rx_ret_ring_tag,
4599beedf5beSSepherosa Ziehau 	    ret->bnx_rx_ret_ring_map, ret->bnx_rx_ret_ring);
4600beedf5beSSepherosa Ziehau }
46010c7da01dSSepherosa Ziehau 
46020c7da01dSSepherosa Ziehau static int
46030c7da01dSSepherosa Ziehau bnx_alloc_intr(struct bnx_softc *sc)
46040c7da01dSSepherosa Ziehau {
4605f33ac8a4SSepherosa Ziehau 	struct bnx_intr_data *intr;
46060c7da01dSSepherosa Ziehau 	u_int intr_flags;
46070c7da01dSSepherosa Ziehau 
4608f33ac8a4SSepherosa Ziehau 	sc->bnx_intr_cnt = 1;
46090c7da01dSSepherosa Ziehau 
4610f33ac8a4SSepherosa Ziehau 	intr = &sc->bnx_intr_data[0];
4611f33ac8a4SSepherosa Ziehau 	intr->bnx_sc = sc;
4612f33ac8a4SSepherosa Ziehau 	intr->bnx_ret = &sc->bnx_rx_ret_ring[0];
4613f33ac8a4SSepherosa Ziehau 	intr->bnx_txr = &sc->bnx_tx_ring[0];
4614f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_serialize = &sc->bnx_main_serialize;
4615f33ac8a4SSepherosa Ziehau 	callout_init_mp(&intr->bnx_intr_timer);
4616f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_check = bnx_check_intr;
46174fa38985SSepherosa Ziehau 	intr->bnx_saved_status_tag = &intr->bnx_ret->bnx_saved_status_tag;
4618f33ac8a4SSepherosa Ziehau 
4619f33ac8a4SSepherosa Ziehau 	sc->bnx_intr_type = pci_alloc_1intr(sc->bnx_dev, bnx_msi_enable,
4620f33ac8a4SSepherosa Ziehau 	    &intr->bnx_intr_rid, &intr_flags);
4621f33ac8a4SSepherosa Ziehau 
4622f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_res = bus_alloc_resource_any(sc->bnx_dev, SYS_RES_IRQ,
4623f33ac8a4SSepherosa Ziehau 	    &intr->bnx_intr_rid, intr_flags);
4624f33ac8a4SSepherosa Ziehau 	if (intr->bnx_intr_res == NULL) {
46250c7da01dSSepherosa Ziehau 		device_printf(sc->bnx_dev, "could not alloc interrupt\n");
46260c7da01dSSepherosa Ziehau 		return ENXIO;
46270c7da01dSSepherosa Ziehau 	}
46280c7da01dSSepherosa Ziehau 
4629f33ac8a4SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI) {
46300c7da01dSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_ONESHOT_MSI;
46310c7da01dSSepherosa Ziehau 		bnx_enable_msi(sc);
4632f33ac8a4SSepherosa Ziehau 
4633f33ac8a4SSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_ONESHOT_MSI) {
4634f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_func = bnx_msi_oneshot;
4635f33ac8a4SSepherosa Ziehau 			if (bootverbose)
4636f33ac8a4SSepherosa Ziehau 				device_printf(sc->bnx_dev, "oneshot MSI\n");
4637f33ac8a4SSepherosa Ziehau 		} else {
4638f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_func = bnx_msi;
46390c7da01dSSepherosa Ziehau 		}
4640f33ac8a4SSepherosa Ziehau 	} else {
4641f33ac8a4SSepherosa Ziehau 		intr->bnx_intr_func = bnx_intr_legacy;
4642f33ac8a4SSepherosa Ziehau 	}
4643f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_arg = sc;
4644f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_cpuid = rman_get_cpuid(intr->bnx_intr_res);
4645f33ac8a4SSepherosa Ziehau 
4646f33ac8a4SSepherosa Ziehau 	intr->bnx_txr->bnx_tx_cpuid = intr->bnx_intr_cpuid;
4647f33ac8a4SSepherosa Ziehau 
46480c7da01dSSepherosa Ziehau 	return 0;
46490c7da01dSSepherosa Ziehau }
46500c7da01dSSepherosa Ziehau 
46510c7da01dSSepherosa Ziehau static int
46520c7da01dSSepherosa Ziehau bnx_setup_intr(struct bnx_softc *sc)
46530c7da01dSSepherosa Ziehau {
4654f33ac8a4SSepherosa Ziehau 	int error, i;
46550c7da01dSSepherosa Ziehau 
4656f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4657f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4658f33ac8a4SSepherosa Ziehau 
4659f33ac8a4SSepherosa Ziehau 		error = bus_setup_intr_descr(sc->bnx_dev, intr->bnx_intr_res,
4660f33ac8a4SSepherosa Ziehau 		    INTR_MPSAFE, intr->bnx_intr_func, intr->bnx_intr_arg,
4661f33ac8a4SSepherosa Ziehau 		    &intr->bnx_intr_hand, intr->bnx_intr_serialize,
4662f33ac8a4SSepherosa Ziehau 		    intr->bnx_intr_desc);
46630c7da01dSSepherosa Ziehau 		if (error) {
4664f33ac8a4SSepherosa Ziehau 			device_printf(sc->bnx_dev,
4665f33ac8a4SSepherosa Ziehau 			    "could not set up %dth intr\n", i);
4666f33ac8a4SSepherosa Ziehau 			bnx_teardown_intr(sc, i);
46670c7da01dSSepherosa Ziehau 			return error;
46680c7da01dSSepherosa Ziehau 		}
4669f33ac8a4SSepherosa Ziehau 	}
46700c7da01dSSepherosa Ziehau 	return 0;
46710c7da01dSSepherosa Ziehau }
46720c7da01dSSepherosa Ziehau 
46730c7da01dSSepherosa Ziehau static void
4674f33ac8a4SSepherosa Ziehau bnx_teardown_intr(struct bnx_softc *sc, int cnt)
4675f33ac8a4SSepherosa Ziehau {
4676f33ac8a4SSepherosa Ziehau 	int i;
4677f33ac8a4SSepherosa Ziehau 
4678f33ac8a4SSepherosa Ziehau 	for (i = 0; i < cnt; ++i) {
4679f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4680f33ac8a4SSepherosa Ziehau 
4681f33ac8a4SSepherosa Ziehau 		bus_teardown_intr(sc->bnx_dev, intr->bnx_intr_res,
4682f33ac8a4SSepherosa Ziehau 		    intr->bnx_intr_hand);
4683f33ac8a4SSepherosa Ziehau 	}
4684f33ac8a4SSepherosa Ziehau }
4685f33ac8a4SSepherosa Ziehau 
4686f33ac8a4SSepherosa Ziehau static void
46870c7da01dSSepherosa Ziehau bnx_free_intr(struct bnx_softc *sc)
46880c7da01dSSepherosa Ziehau {
4689f33ac8a4SSepherosa Ziehau 	struct bnx_intr_data *intr;
4690f33ac8a4SSepherosa Ziehau 
4691f33ac8a4SSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt <= 1);
4692f33ac8a4SSepherosa Ziehau 	intr = &sc->bnx_intr_data[0];
4693f33ac8a4SSepherosa Ziehau 
4694f33ac8a4SSepherosa Ziehau 	if (intr->bnx_intr_res != NULL) {
46950c7da01dSSepherosa Ziehau 		bus_release_resource(sc->bnx_dev, SYS_RES_IRQ,
4696f33ac8a4SSepherosa Ziehau 		    intr->bnx_intr_rid, intr->bnx_intr_res);
46970c7da01dSSepherosa Ziehau 	}
4698f33ac8a4SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI)
46990c7da01dSSepherosa Ziehau 		pci_release_msi(sc->bnx_dev);
47000c7da01dSSepherosa Ziehau }
4701329f9016SSepherosa Ziehau 
4702329f9016SSepherosa Ziehau static void
4703329f9016SSepherosa Ziehau bnx_setup_serialize(struct bnx_softc *sc)
4704329f9016SSepherosa Ziehau {
4705329f9016SSepherosa Ziehau 	int i, j;
4706329f9016SSepherosa Ziehau 
4707329f9016SSepherosa Ziehau 	/*
4708329f9016SSepherosa Ziehau 	 * Allocate serializer array
4709329f9016SSepherosa Ziehau 	 */
4710329f9016SSepherosa Ziehau 
4711329f9016SSepherosa Ziehau 	/* Main + RX STD + TX + RX RET */
4712329f9016SSepherosa Ziehau 	sc->bnx_serialize_cnt = 1 + 1 + sc->bnx_tx_ringcnt + sc->bnx_rx_retcnt;
4713329f9016SSepherosa Ziehau 
4714329f9016SSepherosa Ziehau 	sc->bnx_serialize =
4715329f9016SSepherosa Ziehau 	    kmalloc(sc->bnx_serialize_cnt * sizeof(struct lwkt_serialize *),
4716329f9016SSepherosa Ziehau 	        M_DEVBUF, M_WAITOK | M_ZERO);
4717329f9016SSepherosa Ziehau 
4718329f9016SSepherosa Ziehau 	/*
4719329f9016SSepherosa Ziehau 	 * Setup serializers
4720329f9016SSepherosa Ziehau 	 *
4721329f9016SSepherosa Ziehau 	 * NOTE: Order is critical
4722329f9016SSepherosa Ziehau 	 */
4723329f9016SSepherosa Ziehau 
4724329f9016SSepherosa Ziehau 	i = 0;
4725329f9016SSepherosa Ziehau 
4726329f9016SSepherosa Ziehau 	KKASSERT(i < sc->bnx_serialize_cnt);
4727329f9016SSepherosa Ziehau 	sc->bnx_serialize[i++] = &sc->bnx_main_serialize;
4728329f9016SSepherosa Ziehau 
4729329f9016SSepherosa Ziehau 	KKASSERT(i < sc->bnx_serialize_cnt);
4730329f9016SSepherosa Ziehau 	sc->bnx_serialize[i++] = &sc->bnx_rx_std_ring.bnx_rx_std_serialize;
4731329f9016SSepherosa Ziehau 
4732329f9016SSepherosa Ziehau 	for (j = 0; j < sc->bnx_rx_retcnt; ++j) {
4733329f9016SSepherosa Ziehau 		KKASSERT(i < sc->bnx_serialize_cnt);
4734329f9016SSepherosa Ziehau 		sc->bnx_serialize[i++] =
4735329f9016SSepherosa Ziehau 		    &sc->bnx_rx_ret_ring[j].bnx_rx_ret_serialize;
4736329f9016SSepherosa Ziehau 	}
4737329f9016SSepherosa Ziehau 
4738329f9016SSepherosa Ziehau 	for (j = 0; j < sc->bnx_tx_ringcnt; ++j) {
4739329f9016SSepherosa Ziehau 		KKASSERT(i < sc->bnx_serialize_cnt);
4740329f9016SSepherosa Ziehau 		sc->bnx_serialize[i++] =
4741329f9016SSepherosa Ziehau 		    &sc->bnx_tx_ring[j].bnx_tx_serialize;
4742329f9016SSepherosa Ziehau 	}
4743329f9016SSepherosa Ziehau 
4744329f9016SSepherosa Ziehau 	KKASSERT(i == sc->bnx_serialize_cnt);
4745329f9016SSepherosa Ziehau }
4746329f9016SSepherosa Ziehau 
4747329f9016SSepherosa Ziehau static void
4748329f9016SSepherosa Ziehau bnx_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
4749329f9016SSepherosa Ziehau {
4750329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
4751329f9016SSepherosa Ziehau 
4752329f9016SSepherosa Ziehau 	ifnet_serialize_array_enter(sc->bnx_serialize,
4753329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
4754329f9016SSepherosa Ziehau }
4755329f9016SSepherosa Ziehau 
4756329f9016SSepherosa Ziehau static void
4757329f9016SSepherosa Ziehau bnx_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
4758329f9016SSepherosa Ziehau {
4759329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
4760329f9016SSepherosa Ziehau 
4761329f9016SSepherosa Ziehau 	ifnet_serialize_array_exit(sc->bnx_serialize,
4762329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
4763329f9016SSepherosa Ziehau }
4764329f9016SSepherosa Ziehau 
4765329f9016SSepherosa Ziehau static int
4766329f9016SSepherosa Ziehau bnx_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
4767329f9016SSepherosa Ziehau {
4768329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
4769329f9016SSepherosa Ziehau 
4770329f9016SSepherosa Ziehau 	return ifnet_serialize_array_try(sc->bnx_serialize,
4771329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
4772329f9016SSepherosa Ziehau }
4773329f9016SSepherosa Ziehau 
4774329f9016SSepherosa Ziehau #ifdef INVARIANTS
4775329f9016SSepherosa Ziehau 
4776329f9016SSepherosa Ziehau static void
4777329f9016SSepherosa Ziehau bnx_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
4778329f9016SSepherosa Ziehau     boolean_t serialized)
4779329f9016SSepherosa Ziehau {
4780329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
4781329f9016SSepherosa Ziehau 
4782329f9016SSepherosa Ziehau 	ifnet_serialize_array_assert(sc->bnx_serialize, sc->bnx_serialize_cnt,
4783329f9016SSepherosa Ziehau 	    slz, serialized);
4784329f9016SSepherosa Ziehau }
4785329f9016SSepherosa Ziehau 
4786329f9016SSepherosa Ziehau #endif	/* INVARIANTS */
47874fa38985SSepherosa Ziehau 
47884fa38985SSepherosa Ziehau #ifdef IFPOLL_ENABLE
47894fa38985SSepherosa Ziehau 
47904fa38985SSepherosa Ziehau static int
47914fa38985SSepherosa Ziehau bnx_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS)
47924fa38985SSepherosa Ziehau {
47934fa38985SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
47944fa38985SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
47954fa38985SSepherosa Ziehau 	int error, off;
47964fa38985SSepherosa Ziehau 
47974fa38985SSepherosa Ziehau 	off = sc->bnx_npoll_rxoff;
47984fa38985SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &off, 0, req);
47994fa38985SSepherosa Ziehau 	if (error || req->newptr == NULL)
48004fa38985SSepherosa Ziehau 		return error;
48014fa38985SSepherosa Ziehau 	if (off < 0)
48024fa38985SSepherosa Ziehau 		return EINVAL;
48034fa38985SSepherosa Ziehau 
48044fa38985SSepherosa Ziehau 	ifnet_serialize_all(ifp);
48054fa38985SSepherosa Ziehau 	if (off >= ncpus2 || off % sc->bnx_rx_retcnt != 0) {
48064fa38985SSepherosa Ziehau 		error = EINVAL;
48074fa38985SSepherosa Ziehau 	} else {
48084fa38985SSepherosa Ziehau 		error = 0;
48094fa38985SSepherosa Ziehau 		sc->bnx_npoll_txoff = off;
48104fa38985SSepherosa Ziehau 		sc->bnx_npoll_rxoff = off;
48114fa38985SSepherosa Ziehau 	}
48124fa38985SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
48134fa38985SSepherosa Ziehau 
48144fa38985SSepherosa Ziehau 	return error;
48154fa38985SSepherosa Ziehau }
48164fa38985SSepherosa Ziehau 
48174fa38985SSepherosa Ziehau static int
48184fa38985SSepherosa Ziehau bnx_sysctl_npoll_rxoff(SYSCTL_HANDLER_ARGS)
48194fa38985SSepherosa Ziehau {
48204fa38985SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
48214fa38985SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
48224fa38985SSepherosa Ziehau 	int error, off;
48234fa38985SSepherosa Ziehau 
48244fa38985SSepherosa Ziehau 	off = sc->bnx_npoll_rxoff;
48254fa38985SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &off, 0, req);
48264fa38985SSepherosa Ziehau 	if (error || req->newptr == NULL)
48274fa38985SSepherosa Ziehau 		return error;
48284fa38985SSepherosa Ziehau 	if (off < 0)
48294fa38985SSepherosa Ziehau 		return EINVAL;
48304fa38985SSepherosa Ziehau 
48314fa38985SSepherosa Ziehau 	ifnet_serialize_all(ifp);
48324fa38985SSepherosa Ziehau 	if (off >= ncpus2 || off % sc->bnx_rx_retcnt != 0) {
48334fa38985SSepherosa Ziehau 		error = EINVAL;
48344fa38985SSepherosa Ziehau 	} else {
48354fa38985SSepherosa Ziehau 		error = 0;
48364fa38985SSepherosa Ziehau 		sc->bnx_npoll_rxoff = off;
48374fa38985SSepherosa Ziehau 	}
48384fa38985SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
48394fa38985SSepherosa Ziehau 
48404fa38985SSepherosa Ziehau 	return error;
48414fa38985SSepherosa Ziehau }
48424fa38985SSepherosa Ziehau 
48434fa38985SSepherosa Ziehau static int
48444fa38985SSepherosa Ziehau bnx_sysctl_npoll_txoff(SYSCTL_HANDLER_ARGS)
48454fa38985SSepherosa Ziehau {
48464fa38985SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
48474fa38985SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
48484fa38985SSepherosa Ziehau 	int error, off;
48494fa38985SSepherosa Ziehau 
48504fa38985SSepherosa Ziehau 	off = sc->bnx_npoll_txoff;
48514fa38985SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &off, 0, req);
48524fa38985SSepherosa Ziehau 	if (error || req->newptr == NULL)
48534fa38985SSepherosa Ziehau 		return error;
48544fa38985SSepherosa Ziehau 	if (off < 0)
48554fa38985SSepherosa Ziehau 		return EINVAL;
48564fa38985SSepherosa Ziehau 
48574fa38985SSepherosa Ziehau 	ifnet_serialize_all(ifp);
48584fa38985SSepherosa Ziehau 	if (off >= ncpus2) {
48594fa38985SSepherosa Ziehau 		error = EINVAL;
48604fa38985SSepherosa Ziehau 	} else {
48614fa38985SSepherosa Ziehau 		error = 0;
48624fa38985SSepherosa Ziehau 		sc->bnx_npoll_txoff = off;
48634fa38985SSepherosa Ziehau 	}
48644fa38985SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
48654fa38985SSepherosa Ziehau 
48664fa38985SSepherosa Ziehau 	return error;
48674fa38985SSepherosa Ziehau }
48684fa38985SSepherosa Ziehau 
48694fa38985SSepherosa Ziehau #endif	/* IFPOLL_ENABLE */
4870*7dbaa833SSepherosa Ziehau 
4871*7dbaa833SSepherosa Ziehau static void
4872*7dbaa833SSepherosa Ziehau bnx_set_tick_cpuid(struct bnx_softc *sc, boolean_t polling)
4873*7dbaa833SSepherosa Ziehau {
4874*7dbaa833SSepherosa Ziehau 	if (polling)
4875*7dbaa833SSepherosa Ziehau 		sc->bnx_tick_cpuid = 0; /* XXX */
4876*7dbaa833SSepherosa Ziehau 	else
4877*7dbaa833SSepherosa Ziehau 		sc->bnx_tick_cpuid = sc->bnx_intr_data[0].bnx_intr_cpuid;
4878*7dbaa833SSepherosa Ziehau }
4879