xref: /dflybsd-src/sys/dev/netif/bnx/if_bnx.c (revision 030b0c8c4cf27c560ccec70410c8e21934ae677d)
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>
65afc5d5f3SSepherosa Ziehau #include <net/if_ringmap.h>
66695a8586SSepherosa Ziehau #include <net/toeplitz.h>
67695a8586SSepherosa Ziehau #include <net/toeplitz2.h>
686c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_var.h>
696c8d8eccSSepherosa Ziehau #include <net/vlan/if_vlan_ether.h>
706c8d8eccSSepherosa Ziehau 
716c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/mii.h>
726c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/miivar.h>
736c8d8eccSSepherosa Ziehau #include <dev/netif/mii_layer/brgphyreg.h>
746c8d8eccSSepherosa Ziehau 
75dcb4b80dSSascha Wildner #include "pcidevs.h"
766c8d8eccSSepherosa Ziehau #include <bus/pci/pcireg.h>
776c8d8eccSSepherosa Ziehau #include <bus/pci/pcivar.h>
786c8d8eccSSepherosa Ziehau 
796c8d8eccSSepherosa Ziehau #include <dev/netif/bge/if_bgereg.h>
806c8d8eccSSepherosa Ziehau #include <dev/netif/bnx/if_bnxvar.h>
816c8d8eccSSepherosa Ziehau 
826c8d8eccSSepherosa Ziehau /* "device miibus" required.  See GENERIC if you get errors here. */
836c8d8eccSSepherosa Ziehau #include "miibus_if.h"
846c8d8eccSSepherosa Ziehau 
853b18363fSSepherosa Ziehau #define BNX_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
866c8d8eccSSepherosa Ziehau 
874aa71e73SSepherosa Ziehau #define	BNX_RESET_SHUTDOWN	0
884aa71e73SSepherosa Ziehau #define	BNX_RESET_START		1
894aa71e73SSepherosa Ziehau #define	BNX_RESET_SUSPEND	2
904aa71e73SSepherosa Ziehau 
91df9ccc98SSepherosa Ziehau #define BNX_INTR_CKINTVL	((10 * hz) / 1000)	/* 10ms */
92df9ccc98SSepherosa Ziehau 
93695a8586SSepherosa Ziehau #ifdef BNX_RSS_DEBUG
94695a8586SSepherosa Ziehau #define BNX_RSS_DPRINTF(sc, lvl, fmt, ...) \
95695a8586SSepherosa Ziehau do { \
96695a8586SSepherosa Ziehau 	if (sc->bnx_rss_debug >= lvl) \
97695a8586SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, fmt, __VA_ARGS__); \
98695a8586SSepherosa Ziehau } while (0)
99695a8586SSepherosa Ziehau #else	/* !BNX_RSS_DEBUG */
100695a8586SSepherosa Ziehau #define BNX_RSS_DPRINTF(sc, lvl, fmt, ...)	((void)0)
101695a8586SSepherosa Ziehau #endif	/* BNX_RSS_DEBUG */
102695a8586SSepherosa Ziehau 
1036c8d8eccSSepherosa Ziehau static const struct bnx_type {
1046c8d8eccSSepherosa Ziehau 	uint16_t		bnx_vid;
1056c8d8eccSSepherosa Ziehau 	uint16_t		bnx_did;
1066c8d8eccSSepherosa Ziehau 	char			*bnx_name;
1076c8d8eccSSepherosa Ziehau } bnx_devs[] = {
1086c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717,
1096c8d8eccSSepherosa Ziehau 		"Broadcom BCM5717 Gigabit Ethernet" },
110d79f5d8fSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5717C,
111d79f5d8fSSepherosa Ziehau 		"Broadcom BCM5717C Gigabit Ethernet" },
1126c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5718,
1136c8d8eccSSepherosa Ziehau 		"Broadcom BCM5718 Gigabit Ethernet" },
1146c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5719,
1156c8d8eccSSepherosa Ziehau 		"Broadcom BCM5719 Gigabit Ethernet" },
1166c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5720_ALT,
1176c8d8eccSSepherosa Ziehau 		"Broadcom BCM5720 Gigabit Ethernet" },
1186c8d8eccSSepherosa Ziehau 
119b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5725,
120b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5725 Gigabit Ethernet" },
121b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5727,
122b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5727 Gigabit Ethernet" },
123b96cbbb6SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM5762,
124b96cbbb6SSepherosa Ziehau 		"Broadcom BCM5762 Gigabit Ethernet" },
125b96cbbb6SSepherosa Ziehau 
1266c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57761,
1276c8d8eccSSepherosa Ziehau 		"Broadcom BCM57761 Gigabit Ethernet" },
12832ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57762,
12932ff3c80SSepherosa Ziehau 		"Broadcom BCM57762 Gigabit Ethernet" },
130c1ed6db1SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57764,
131c1ed6db1SSepherosa Ziehau 		"Broadcom BCM57764 Gigabit Ethernet" },
1326c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57765,
1336c8d8eccSSepherosa Ziehau 		"Broadcom BCM57765 Gigabit Ethernet" },
13432ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57766,
13532ff3c80SSepherosa Ziehau 		"Broadcom BCM57766 Gigabit Ethernet" },
136c1ed6db1SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57767,
137c1ed6db1SSepherosa Ziehau 		"Broadcom BCM57767 Gigabit Ethernet" },
13832ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57781,
13932ff3c80SSepherosa Ziehau 		"Broadcom BCM57781 Gigabit Ethernet" },
14032ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57782,
14132ff3c80SSepherosa Ziehau 		"Broadcom BCM57782 Gigabit Ethernet" },
1426c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57785,
1436c8d8eccSSepherosa Ziehau 		"Broadcom BCM57785 Gigabit Ethernet" },
14432ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57786,
14532ff3c80SSepherosa Ziehau 		"Broadcom BCM57786 Gigabit Ethernet" },
146c1ed6db1SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57787,
147c1ed6db1SSepherosa Ziehau 		"Broadcom BCM57787 Gigabit Ethernet" },
14832ff3c80SSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57791,
14932ff3c80SSepherosa Ziehau 		"Broadcom BCM57791 Fast Ethernet" },
1506c8d8eccSSepherosa Ziehau 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM57795,
1516c8d8eccSSepherosa Ziehau 		"Broadcom BCM57795 Fast Ethernet" },
1526c8d8eccSSepherosa Ziehau 
1536c8d8eccSSepherosa Ziehau 	{ 0, 0, NULL }
1546c8d8eccSSepherosa Ziehau };
1556c8d8eccSSepherosa Ziehau 
1561c9d03f6SSepherosa Ziehau static const int bnx_tx_mailbox[BNX_TX_RING_MAX] = {
1571c9d03f6SSepherosa Ziehau 	BGE_MBX_TX_HOST_PROD0_LO,
1581c9d03f6SSepherosa Ziehau 	BGE_MBX_TX_HOST_PROD0_HI,
1591c9d03f6SSepherosa Ziehau 	BGE_MBX_TX_HOST_PROD1_LO,
1601c9d03f6SSepherosa Ziehau 	BGE_MBX_TX_HOST_PROD1_HI
1611c9d03f6SSepherosa Ziehau };
1621c9d03f6SSepherosa Ziehau 
1636c8d8eccSSepherosa Ziehau #define BNX_IS_JUMBO_CAPABLE(sc)	((sc)->bnx_flags & BNX_FLAG_JUMBO)
1646c8d8eccSSepherosa Ziehau #define BNX_IS_5717_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_5717_PLUS)
165f368d0d9SSepherosa Ziehau #define BNX_IS_57765_PLUS(sc)		((sc)->bnx_flags & BNX_FLAG_57765_PLUS)
166f368d0d9SSepherosa Ziehau #define BNX_IS_57765_FAMILY(sc)	 \
167f368d0d9SSepherosa Ziehau 	((sc)->bnx_flags & BNX_FLAG_57765_FAMILY)
1686c8d8eccSSepherosa Ziehau 
1696c8d8eccSSepherosa Ziehau typedef int	(*bnx_eaddr_fcn_t)(struct bnx_softc *, uint8_t[]);
1706c8d8eccSSepherosa Ziehau 
1716c8d8eccSSepherosa Ziehau static int	bnx_probe(device_t);
1726c8d8eccSSepherosa Ziehau static int	bnx_attach(device_t);
1736c8d8eccSSepherosa Ziehau static int	bnx_detach(device_t);
1746c8d8eccSSepherosa Ziehau static void	bnx_shutdown(device_t);
1756c8d8eccSSepherosa Ziehau static int	bnx_suspend(device_t);
1766c8d8eccSSepherosa Ziehau static int	bnx_resume(device_t);
1776c8d8eccSSepherosa Ziehau static int	bnx_miibus_readreg(device_t, int, int);
1786c8d8eccSSepherosa Ziehau static int	bnx_miibus_writereg(device_t, int, int, int);
1796c8d8eccSSepherosa Ziehau static void	bnx_miibus_statchg(device_t);
1806c8d8eccSSepherosa Ziehau 
18124e16e4bSSepherosa Ziehau static int	bnx_handle_status(struct bnx_softc *);
18239a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
18339a8d43aSSepherosa Ziehau static void	bnx_npoll(struct ifnet *, struct ifpoll_info *);
1844fa38985SSepherosa Ziehau static void	bnx_npoll_rx(struct ifnet *, void *, int);
1854fa38985SSepherosa Ziehau static void	bnx_npoll_tx(struct ifnet *, void *, int);
186695a8586SSepherosa Ziehau static void	bnx_npoll_tx_notag(struct ifnet *, void *, int);
1874fa38985SSepherosa Ziehau static void	bnx_npoll_status(struct ifnet *);
188695a8586SSepherosa Ziehau static void	bnx_npoll_status_notag(struct ifnet *);
1896c8d8eccSSepherosa Ziehau #endif
1906c8d8eccSSepherosa Ziehau static void	bnx_intr_legacy(void *);
19103cc99fdSSepherosa Ziehau static void	bnx_msi(void *);
1926c8d8eccSSepherosa Ziehau static void	bnx_intr(struct bnx_softc *);
193695a8586SSepherosa Ziehau static void	bnx_msix_status(void *);
194695a8586SSepherosa Ziehau static void	bnx_msix_tx_status(void *);
195695a8586SSepherosa Ziehau static void	bnx_msix_rx(void *);
196695a8586SSepherosa Ziehau static void	bnx_msix_rxtx(void *);
1976c8d8eccSSepherosa Ziehau static void	bnx_enable_intr(struct bnx_softc *);
1986c8d8eccSSepherosa Ziehau static void	bnx_disable_intr(struct bnx_softc *);
19933a04907SSepherosa Ziehau static void	bnx_txeof(struct bnx_tx_ring *, uint16_t);
200beedf5beSSepherosa Ziehau static void	bnx_rxeof(struct bnx_rx_ret_ring *, uint16_t, int);
2010c7da01dSSepherosa Ziehau static int	bnx_alloc_intr(struct bnx_softc *);
2020c7da01dSSepherosa Ziehau static int	bnx_setup_intr(struct bnx_softc *);
2030c7da01dSSepherosa Ziehau static void	bnx_free_intr(struct bnx_softc *);
204f33ac8a4SSepherosa Ziehau static void	bnx_teardown_intr(struct bnx_softc *, int);
205695a8586SSepherosa Ziehau static int	bnx_alloc_msix(struct bnx_softc *);
206695a8586SSepherosa Ziehau static void	bnx_free_msix(struct bnx_softc *, boolean_t);
207695a8586SSepherosa Ziehau static void	bnx_check_intr_rxtx(void *);
208695a8586SSepherosa Ziehau static void	bnx_check_intr_rx(void *);
209695a8586SSepherosa Ziehau static void	bnx_check_intr_tx(void *);
210841cdf08SSepherosa Ziehau static void	bnx_rx_std_refill_ithread(void *);
211841cdf08SSepherosa Ziehau static void	bnx_rx_std_refill(void *, void *);
212695a8586SSepherosa Ziehau static void	bnx_rx_std_refill_sched_ipi(void *);
213695a8586SSepherosa Ziehau static void	bnx_rx_std_refill_stop(void *);
214695a8586SSepherosa Ziehau static void	bnx_rx_std_refill_sched(struct bnx_rx_ret_ring *,
215695a8586SSepherosa Ziehau 		    struct bnx_rx_std_ring *);
2166c8d8eccSSepherosa Ziehau 
217f0a26983SSepherosa Ziehau static void	bnx_start(struct ifnet *, struct ifaltq_subque *);
2186c8d8eccSSepherosa Ziehau static int	bnx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
2196c8d8eccSSepherosa Ziehau static void	bnx_init(void *);
2206c8d8eccSSepherosa Ziehau static void	bnx_stop(struct bnx_softc *);
2213397dea6SSepherosa Ziehau static void	bnx_watchdog(struct ifaltq_subque *);
2226c8d8eccSSepherosa Ziehau static int	bnx_ifmedia_upd(struct ifnet *);
2236c8d8eccSSepherosa Ziehau static void	bnx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
2246c8d8eccSSepherosa Ziehau static void	bnx_tick(void *);
225329f9016SSepherosa Ziehau static void	bnx_serialize(struct ifnet *, enum ifnet_serialize);
226329f9016SSepherosa Ziehau static void	bnx_deserialize(struct ifnet *, enum ifnet_serialize);
227329f9016SSepherosa Ziehau static int	bnx_tryserialize(struct ifnet *, enum ifnet_serialize);
228329f9016SSepherosa Ziehau #ifdef INVARIANTS
229329f9016SSepherosa Ziehau static void	bnx_serialize_assert(struct ifnet *, enum ifnet_serialize,
230329f9016SSepherosa Ziehau 		    boolean_t);
231329f9016SSepherosa Ziehau #endif
232695a8586SSepherosa Ziehau static void	bnx_serialize_skipmain(struct bnx_softc *);
233695a8586SSepherosa Ziehau static void	bnx_deserialize_skipmain(struct bnx_softc *sc);
2346c8d8eccSSepherosa Ziehau 
2356c8d8eccSSepherosa Ziehau static int	bnx_alloc_jumbo_mem(struct bnx_softc *);
2366c8d8eccSSepherosa Ziehau static void	bnx_free_jumbo_mem(struct bnx_softc *);
2376c8d8eccSSepherosa Ziehau static struct bnx_jslot
2386c8d8eccSSepherosa Ziehau 		*bnx_jalloc(struct bnx_softc *);
2396c8d8eccSSepherosa Ziehau static void	bnx_jfree(void *);
2406c8d8eccSSepherosa Ziehau static void	bnx_jref(void *);
241beedf5beSSepherosa Ziehau static int	bnx_newbuf_std(struct bnx_rx_ret_ring *, int, int);
2426c8d8eccSSepherosa Ziehau static int	bnx_newbuf_jumbo(struct bnx_softc *, int, int);
243beedf5beSSepherosa Ziehau static void	bnx_setup_rxdesc_std(struct bnx_rx_std_ring *, int);
2446c8d8eccSSepherosa Ziehau static void	bnx_setup_rxdesc_jumbo(struct bnx_softc *, int);
245beedf5beSSepherosa Ziehau static int	bnx_init_rx_ring_std(struct bnx_rx_std_ring *);
246beedf5beSSepherosa Ziehau static void	bnx_free_rx_ring_std(struct bnx_rx_std_ring *);
2476c8d8eccSSepherosa Ziehau static int	bnx_init_rx_ring_jumbo(struct bnx_softc *);
2486c8d8eccSSepherosa Ziehau static void	bnx_free_rx_ring_jumbo(struct bnx_softc *);
24933a04907SSepherosa Ziehau static void	bnx_free_tx_ring(struct bnx_tx_ring *);
25033a04907SSepherosa Ziehau static int	bnx_init_tx_ring(struct bnx_tx_ring *);
25133a04907SSepherosa Ziehau static int	bnx_create_tx_ring(struct bnx_tx_ring *);
25233a04907SSepherosa Ziehau static void	bnx_destroy_tx_ring(struct bnx_tx_ring *);
253beedf5beSSepherosa Ziehau static int	bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *);
254beedf5beSSepherosa Ziehau static void	bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *);
255beedf5beSSepherosa Ziehau static int	bnx_dma_alloc(device_t);
2566c8d8eccSSepherosa Ziehau static void	bnx_dma_free(struct bnx_softc *);
2576c8d8eccSSepherosa Ziehau static int	bnx_dma_block_alloc(struct bnx_softc *, bus_size_t,
2586c8d8eccSSepherosa Ziehau 		    bus_dma_tag_t *, bus_dmamap_t *, void **, bus_addr_t *);
2596c8d8eccSSepherosa Ziehau static void	bnx_dma_block_free(bus_dma_tag_t, bus_dmamap_t, void *);
2606c8d8eccSSepherosa Ziehau static struct mbuf *
2616c8d8eccSSepherosa Ziehau 		bnx_defrag_shortdma(struct mbuf *);
26233a04907SSepherosa Ziehau static int	bnx_encap(struct bnx_tx_ring *, struct mbuf **,
263c9b7f592SSepherosa Ziehau 		    uint32_t *, int *);
26433a04907SSepherosa Ziehau static int	bnx_setup_tso(struct bnx_tx_ring *, struct mbuf **,
26566deb1c1SSepherosa Ziehau 		    uint16_t *, uint16_t *);
266329f9016SSepherosa Ziehau static void	bnx_setup_serialize(struct bnx_softc *);
2677dbaa833SSepherosa Ziehau static void	bnx_set_tick_cpuid(struct bnx_softc *, boolean_t);
268695a8586SSepherosa Ziehau static void	bnx_setup_ring_cnt(struct bnx_softc *);
2696c8d8eccSSepherosa Ziehau 
270b19ddf7eSSepherosa Ziehau static struct pktinfo *bnx_rss_info(struct pktinfo *,
271b19ddf7eSSepherosa Ziehau 		    const struct bge_rx_bd *);
272695a8586SSepherosa Ziehau static void	bnx_init_rss(struct bnx_softc *);
2736c8d8eccSSepherosa Ziehau static void	bnx_reset(struct bnx_softc *);
2746c8d8eccSSepherosa Ziehau static int	bnx_chipinit(struct bnx_softc *);
2756c8d8eccSSepherosa Ziehau static int	bnx_blockinit(struct bnx_softc *);
2766c8d8eccSSepherosa Ziehau static void	bnx_stop_block(struct bnx_softc *, bus_size_t, uint32_t);
277695a8586SSepherosa Ziehau static void	bnx_enable_msi(struct bnx_softc *, boolean_t);
2786c8d8eccSSepherosa Ziehau static void	bnx_setmulti(struct bnx_softc *);
2796c8d8eccSSepherosa Ziehau static void	bnx_setpromisc(struct bnx_softc *);
2806c8d8eccSSepherosa Ziehau static void	bnx_stats_update_regs(struct bnx_softc *);
2816c8d8eccSSepherosa Ziehau static uint32_t	bnx_dma_swap_options(struct bnx_softc *);
2826c8d8eccSSepherosa Ziehau 
2836c8d8eccSSepherosa Ziehau static uint32_t	bnx_readmem_ind(struct bnx_softc *, uint32_t);
2846c8d8eccSSepherosa Ziehau static void	bnx_writemem_ind(struct bnx_softc *, uint32_t, uint32_t);
2856c8d8eccSSepherosa Ziehau #ifdef notdef
2866c8d8eccSSepherosa Ziehau static uint32_t	bnx_readreg_ind(struct bnx_softc *, uint32_t);
2876c8d8eccSSepherosa Ziehau #endif
2886c8d8eccSSepherosa Ziehau static void	bnx_writemem_direct(struct bnx_softc *, uint32_t, uint32_t);
2896c8d8eccSSepherosa Ziehau static void	bnx_writembx(struct bnx_softc *, int, int);
2906c8d8eccSSepherosa Ziehau static int	bnx_read_nvram(struct bnx_softc *, caddr_t, int, int);
2916c8d8eccSSepherosa Ziehau static uint8_t	bnx_eeprom_getbyte(struct bnx_softc *, uint32_t, uint8_t *);
2926c8d8eccSSepherosa Ziehau static int	bnx_read_eeprom(struct bnx_softc *, caddr_t, uint32_t, size_t);
2936c8d8eccSSepherosa Ziehau 
2946c8d8eccSSepherosa Ziehau static void	bnx_tbi_link_upd(struct bnx_softc *, uint32_t);
2956c8d8eccSSepherosa Ziehau static void	bnx_copper_link_upd(struct bnx_softc *, uint32_t);
2966c8d8eccSSepherosa Ziehau static void	bnx_autopoll_link_upd(struct bnx_softc *, uint32_t);
2976c8d8eccSSepherosa Ziehau static void	bnx_link_poll(struct bnx_softc *);
2986c8d8eccSSepherosa Ziehau 
2996c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_mem(struct bnx_softc *, uint8_t[]);
3006c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_nvram(struct bnx_softc *, uint8_t[]);
3016c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr_eeprom(struct bnx_softc *, uint8_t[]);
3026c8d8eccSSepherosa Ziehau static int	bnx_get_eaddr(struct bnx_softc *, uint8_t[]);
3036c8d8eccSSepherosa Ziehau 
3046c8d8eccSSepherosa Ziehau static void	bnx_coal_change(struct bnx_softc *);
305aad4de2bSSepherosa Ziehau static int	bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS);
306472c99c8SSepherosa Ziehau static int	bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS);
3076c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS);
3086c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS);
3096c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS);
310a86cc105SSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds_poll(SYSCTL_HANDLER_ARGS);
3116c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS);
31227357d84SSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds_poll(SYSCTL_HANDLER_ARGS);
3136c8d8eccSSepherosa Ziehau static int	bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS);
3146c8d8eccSSepherosa Ziehau static int	bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS);
3156c8d8eccSSepherosa Ziehau static int	bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *,
3166c8d8eccSSepherosa Ziehau 		    int, int, uint32_t);
317841cdf08SSepherosa Ziehau static int	bnx_sysctl_std_refill(SYSCTL_HANDLER_ARGS);
3186c8d8eccSSepherosa Ziehau 
3194aa71e73SSepherosa Ziehau static void	bnx_sig_post_reset(struct bnx_softc *, int);
3204aa71e73SSepherosa Ziehau static void	bnx_sig_pre_reset(struct bnx_softc *, int);
3219f5082d5SSepherosa Ziehau static void	bnx_ape_lock_init(struct bnx_softc *);
3229f5082d5SSepherosa Ziehau static void	bnx_ape_read_fw_ver(struct bnx_softc *);
3239f5082d5SSepherosa Ziehau static int	bnx_ape_lock(struct bnx_softc *, int);
3249f5082d5SSepherosa Ziehau static void	bnx_ape_unlock(struct bnx_softc *, int);
3259f5082d5SSepherosa Ziehau static void	bnx_ape_send_event(struct bnx_softc *, uint32_t);
3269f5082d5SSepherosa Ziehau static void	bnx_ape_driver_state_change(struct bnx_softc *, int);
3274aa71e73SSepherosa Ziehau 
3286c8d8eccSSepherosa Ziehau static int	bnx_msi_enable = 1;
329695a8586SSepherosa Ziehau static int	bnx_msix_enable = 1;
330695a8586SSepherosa Ziehau 
331695a8586SSepherosa Ziehau static int	bnx_rx_rings = 0; /* auto */
332695a8586SSepherosa Ziehau static int	bnx_tx_rings = 0; /* auto */
333695a8586SSepherosa Ziehau 
3346c8d8eccSSepherosa Ziehau TUNABLE_INT("hw.bnx.msi.enable", &bnx_msi_enable);
335695a8586SSepherosa Ziehau TUNABLE_INT("hw.bnx.msix.enable", &bnx_msix_enable);
336695a8586SSepherosa Ziehau TUNABLE_INT("hw.bnx.rx_rings", &bnx_rx_rings);
337695a8586SSepherosa Ziehau TUNABLE_INT("hw.bnx.tx_rings", &bnx_tx_rings);
3386c8d8eccSSepherosa Ziehau 
3396c8d8eccSSepherosa Ziehau static device_method_t bnx_methods[] = {
3406c8d8eccSSepherosa Ziehau 	/* Device interface */
3416c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_probe,		bnx_probe),
3426c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_attach,	bnx_attach),
3436c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_detach,	bnx_detach),
3446c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_shutdown,	bnx_shutdown),
3456c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_suspend,	bnx_suspend),
3466c8d8eccSSepherosa Ziehau 	DEVMETHOD(device_resume,	bnx_resume),
3476c8d8eccSSepherosa Ziehau 
3486c8d8eccSSepherosa Ziehau 	/* bus interface */
3496c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
3506c8d8eccSSepherosa Ziehau 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
3516c8d8eccSSepherosa Ziehau 
3526c8d8eccSSepherosa Ziehau 	/* MII interface */
3536c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_readreg,	bnx_miibus_readreg),
3546c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_writereg,	bnx_miibus_writereg),
3556c8d8eccSSepherosa Ziehau 	DEVMETHOD(miibus_statchg,	bnx_miibus_statchg),
3566c8d8eccSSepherosa Ziehau 
357d3c9c58eSSascha Wildner 	DEVMETHOD_END
3586c8d8eccSSepherosa Ziehau };
3596c8d8eccSSepherosa Ziehau 
3606c8d8eccSSepherosa Ziehau static DEFINE_CLASS_0(bnx, bnx_driver, bnx_methods, sizeof(struct bnx_softc));
3616c8d8eccSSepherosa Ziehau static devclass_t bnx_devclass;
3626c8d8eccSSepherosa Ziehau 
3636c8d8eccSSepherosa Ziehau DECLARE_DUMMY_MODULE(if_bnx);
3641996f1e5SSepherosa Ziehau MODULE_DEPEND(if_bnx, miibus, 1, 1, 1);
3656c8d8eccSSepherosa Ziehau DRIVER_MODULE(if_bnx, pci, bnx_driver, bnx_devclass, NULL, NULL);
3666c8d8eccSSepherosa Ziehau DRIVER_MODULE(miibus, bnx, miibus_driver, miibus_devclass, NULL, NULL);
3676c8d8eccSSepherosa Ziehau 
3686c8d8eccSSepherosa Ziehau static uint32_t
bnx_readmem_ind(struct bnx_softc * sc,uint32_t off)3696c8d8eccSSepherosa Ziehau bnx_readmem_ind(struct bnx_softc *sc, uint32_t off)
3706c8d8eccSSepherosa Ziehau {
3716c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
3726c8d8eccSSepherosa Ziehau 	uint32_t val;
3736c8d8eccSSepherosa Ziehau 
3746c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
3756c8d8eccSSepherosa Ziehau 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
3766c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
3776c8d8eccSSepherosa Ziehau 	return (val);
3786c8d8eccSSepherosa Ziehau }
3796c8d8eccSSepherosa Ziehau 
3806c8d8eccSSepherosa Ziehau static void
bnx_writemem_ind(struct bnx_softc * sc,uint32_t off,uint32_t val)3816c8d8eccSSepherosa Ziehau bnx_writemem_ind(struct bnx_softc *sc, uint32_t off, uint32_t val)
3826c8d8eccSSepherosa Ziehau {
3836c8d8eccSSepherosa Ziehau 	device_t dev = sc->bnx_dev;
3846c8d8eccSSepherosa Ziehau 
3856c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
3866c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
3876c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
3886c8d8eccSSepherosa Ziehau }
3896c8d8eccSSepherosa Ziehau 
3906c8d8eccSSepherosa Ziehau static void
bnx_writemem_direct(struct bnx_softc * sc,uint32_t off,uint32_t val)3916c8d8eccSSepherosa Ziehau bnx_writemem_direct(struct bnx_softc *sc, uint32_t off, uint32_t val)
3926c8d8eccSSepherosa Ziehau {
3936c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
3946c8d8eccSSepherosa Ziehau }
3956c8d8eccSSepherosa Ziehau 
3966c8d8eccSSepherosa Ziehau static void
bnx_writembx(struct bnx_softc * sc,int off,int val)3976c8d8eccSSepherosa Ziehau bnx_writembx(struct bnx_softc *sc, int off, int val)
3986c8d8eccSSepherosa Ziehau {
3996c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, off, val);
4006c8d8eccSSepherosa Ziehau }
4016c8d8eccSSepherosa Ziehau 
4026c8d8eccSSepherosa Ziehau /*
4036c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from NVRAM.
4046c8d8eccSSepherosa Ziehau  */
4056c8d8eccSSepherosa Ziehau static int
bnx_read_nvram(struct bnx_softc * sc,caddr_t dest,int off,int cnt)4066c8d8eccSSepherosa Ziehau bnx_read_nvram(struct bnx_softc *sc, caddr_t dest, int off, int cnt)
4076c8d8eccSSepherosa Ziehau {
4086c8d8eccSSepherosa Ziehau 	return (1);
4096c8d8eccSSepherosa Ziehau }
4106c8d8eccSSepherosa Ziehau 
4116c8d8eccSSepherosa Ziehau /*
4126c8d8eccSSepherosa Ziehau  * Read a byte of data stored in the EEPROM at address 'addr.' The
4136c8d8eccSSepherosa Ziehau  * BCM570x supports both the traditional bitbang interface and an
4146c8d8eccSSepherosa Ziehau  * auto access interface for reading the EEPROM. We use the auto
4156c8d8eccSSepherosa Ziehau  * access method.
4166c8d8eccSSepherosa Ziehau  */
4176c8d8eccSSepherosa Ziehau static uint8_t
bnx_eeprom_getbyte(struct bnx_softc * sc,uint32_t addr,uint8_t * dest)4186c8d8eccSSepherosa Ziehau bnx_eeprom_getbyte(struct bnx_softc *sc, uint32_t addr, uint8_t *dest)
4196c8d8eccSSepherosa Ziehau {
4206c8d8eccSSepherosa Ziehau 	int i;
4216c8d8eccSSepherosa Ziehau 	uint32_t byte = 0;
4226c8d8eccSSepherosa Ziehau 
4236c8d8eccSSepherosa Ziehau 	/*
4246c8d8eccSSepherosa Ziehau 	 * Enable use of auto EEPROM access so we can avoid
4256c8d8eccSSepherosa Ziehau 	 * having to use the bitbang method.
4266c8d8eccSSepherosa Ziehau 	 */
4276c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
4286c8d8eccSSepherosa Ziehau 
4296c8d8eccSSepherosa Ziehau 	/* Reset the EEPROM, load the clock period. */
4306c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR,
4316c8d8eccSSepherosa Ziehau 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
4326c8d8eccSSepherosa Ziehau 	DELAY(20);
4336c8d8eccSSepherosa Ziehau 
4346c8d8eccSSepherosa Ziehau 	/* Issue the read EEPROM command. */
4356c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
4366c8d8eccSSepherosa Ziehau 
4376c8d8eccSSepherosa Ziehau 	/* Wait for completion */
4386c8d8eccSSepherosa Ziehau 	for(i = 0; i < BNX_TIMEOUT * 10; i++) {
4396c8d8eccSSepherosa Ziehau 		DELAY(10);
4406c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
4416c8d8eccSSepherosa Ziehau 			break;
4426c8d8eccSSepherosa Ziehau 	}
4436c8d8eccSSepherosa Ziehau 
4446c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
4456c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "eeprom read timed out\n");
4466c8d8eccSSepherosa Ziehau 		return(1);
4476c8d8eccSSepherosa Ziehau 	}
4486c8d8eccSSepherosa Ziehau 
4496c8d8eccSSepherosa Ziehau 	/* Get result. */
4506c8d8eccSSepherosa Ziehau 	byte = CSR_READ_4(sc, BGE_EE_DATA);
4516c8d8eccSSepherosa Ziehau 
4526c8d8eccSSepherosa Ziehau         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
4536c8d8eccSSepherosa Ziehau 
4546c8d8eccSSepherosa Ziehau 	return(0);
4556c8d8eccSSepherosa Ziehau }
4566c8d8eccSSepherosa Ziehau 
4576c8d8eccSSepherosa Ziehau /*
4586c8d8eccSSepherosa Ziehau  * Read a sequence of bytes from the EEPROM.
4596c8d8eccSSepherosa Ziehau  */
4606c8d8eccSSepherosa Ziehau static int
bnx_read_eeprom(struct bnx_softc * sc,caddr_t dest,uint32_t off,size_t len)4616c8d8eccSSepherosa Ziehau bnx_read_eeprom(struct bnx_softc *sc, caddr_t dest, uint32_t off, size_t len)
4626c8d8eccSSepherosa Ziehau {
4636c8d8eccSSepherosa Ziehau 	size_t i;
4646c8d8eccSSepherosa Ziehau 	int err;
4656c8d8eccSSepherosa Ziehau 	uint8_t byte;
4666c8d8eccSSepherosa Ziehau 
4676c8d8eccSSepherosa Ziehau 	for (byte = 0, err = 0, i = 0; i < len; i++) {
4686c8d8eccSSepherosa Ziehau 		err = bnx_eeprom_getbyte(sc, off + i, &byte);
4696c8d8eccSSepherosa Ziehau 		if (err)
4706c8d8eccSSepherosa Ziehau 			break;
4716c8d8eccSSepherosa Ziehau 		*(dest + i) = byte;
4726c8d8eccSSepherosa Ziehau 	}
4736c8d8eccSSepherosa Ziehau 
4746c8d8eccSSepherosa Ziehau 	return(err ? 1 : 0);
4756c8d8eccSSepherosa Ziehau }
4766c8d8eccSSepherosa Ziehau 
4776c8d8eccSSepherosa Ziehau static int
bnx_miibus_readreg(device_t dev,int phy,int reg)4786c8d8eccSSepherosa Ziehau bnx_miibus_readreg(device_t dev, int phy, int reg)
4796c8d8eccSSepherosa Ziehau {
4806c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
4816c8d8eccSSepherosa Ziehau 	uint32_t val;
4826c8d8eccSSepherosa Ziehau 	int i;
4836c8d8eccSSepherosa Ziehau 
4846c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
4856c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
4866c8d8eccSSepherosa Ziehau 
4879f5082d5SSepherosa Ziehau 	if (bnx_ape_lock(sc, sc->bnx_phy_ape_lock) != 0)
4889f5082d5SSepherosa Ziehau 		return 0;
4899f5082d5SSepherosa Ziehau 
4906c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
4916c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
4926c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
4936c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
4946c8d8eccSSepherosa Ziehau 		DELAY(80);
4956c8d8eccSSepherosa Ziehau 	}
4966c8d8eccSSepherosa Ziehau 
4976c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
4986c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
4996c8d8eccSSepherosa Ziehau 
5006c8d8eccSSepherosa Ziehau 	/* Poll for the PHY register access to complete. */
5016c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
5026c8d8eccSSepherosa Ziehau 		DELAY(10);
5036c8d8eccSSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_MI_COMM);
5046c8d8eccSSepherosa Ziehau 		if ((val & BGE_MICOMM_BUSY) == 0) {
5056c8d8eccSSepherosa Ziehau 			DELAY(5);
5066c8d8eccSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_MI_COMM);
5076c8d8eccSSepherosa Ziehau 			break;
5086c8d8eccSSepherosa Ziehau 		}
5096c8d8eccSSepherosa Ziehau 	}
5106c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
5116c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY read timed out "
5126c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
5136c8d8eccSSepherosa Ziehau 		val = 0;
5146c8d8eccSSepherosa Ziehau 	}
5156c8d8eccSSepherosa Ziehau 
5166c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
5176c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5186c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
5196c8d8eccSSepherosa Ziehau 		DELAY(80);
5206c8d8eccSSepherosa Ziehau 	}
5216c8d8eccSSepherosa Ziehau 
5229f5082d5SSepherosa Ziehau 	bnx_ape_unlock(sc, sc->bnx_phy_ape_lock);
5239f5082d5SSepherosa Ziehau 
5246c8d8eccSSepherosa Ziehau 	if (val & BGE_MICOMM_READFAIL)
5256c8d8eccSSepherosa Ziehau 		return 0;
5266c8d8eccSSepherosa Ziehau 
5276c8d8eccSSepherosa Ziehau 	return (val & 0xFFFF);
5286c8d8eccSSepherosa Ziehau }
5296c8d8eccSSepherosa Ziehau 
5306c8d8eccSSepherosa Ziehau static int
bnx_miibus_writereg(device_t dev,int phy,int reg,int val)5316c8d8eccSSepherosa Ziehau bnx_miibus_writereg(device_t dev, int phy, int reg, int val)
5326c8d8eccSSepherosa Ziehau {
5336c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
5346c8d8eccSSepherosa Ziehau 	int i;
5356c8d8eccSSepherosa Ziehau 
5366c8d8eccSSepherosa Ziehau 	KASSERT(phy == sc->bnx_phyno,
5376c8d8eccSSepherosa Ziehau 	    ("invalid phyno %d, should be %d", phy, sc->bnx_phyno));
5386c8d8eccSSepherosa Ziehau 
5399f5082d5SSepherosa Ziehau 	if (bnx_ape_lock(sc, sc->bnx_phy_ape_lock) != 0)
5409f5082d5SSepherosa Ziehau 		return 0;
5419f5082d5SSepherosa Ziehau 
5426c8d8eccSSepherosa Ziehau 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
5436c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5446c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE,
5456c8d8eccSSepherosa Ziehau 		    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
5466c8d8eccSSepherosa Ziehau 		DELAY(80);
5476c8d8eccSSepherosa Ziehau 	}
5486c8d8eccSSepherosa Ziehau 
5496c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
5506c8d8eccSSepherosa Ziehau 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
5516c8d8eccSSepherosa Ziehau 
5526c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
5536c8d8eccSSepherosa Ziehau 		DELAY(10);
5546c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
5556c8d8eccSSepherosa Ziehau 			DELAY(5);
5566c8d8eccSSepherosa Ziehau 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
5576c8d8eccSSepherosa Ziehau 			break;
5586c8d8eccSSepherosa Ziehau 		}
5596c8d8eccSSepherosa Ziehau 	}
5606c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
5616c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "PHY write timed out "
5626c8d8eccSSepherosa Ziehau 		    "(phy %d, reg %d, val %d)\n", phy, reg, val);
5636c8d8eccSSepherosa Ziehau 	}
5646c8d8eccSSepherosa Ziehau 
5656c8d8eccSSepherosa Ziehau 	/* Restore the autopoll bit if necessary. */
5666c8d8eccSSepherosa Ziehau 	if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
5676c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
5686c8d8eccSSepherosa Ziehau 		DELAY(80);
5696c8d8eccSSepherosa Ziehau 	}
5706c8d8eccSSepherosa Ziehau 
5719f5082d5SSepherosa Ziehau 	bnx_ape_unlock(sc, sc->bnx_phy_ape_lock);
5729f5082d5SSepherosa Ziehau 
5736c8d8eccSSepherosa Ziehau 	return 0;
5746c8d8eccSSepherosa Ziehau }
5756c8d8eccSSepherosa Ziehau 
5766c8d8eccSSepherosa Ziehau static void
bnx_miibus_statchg(device_t dev)5776c8d8eccSSepherosa Ziehau bnx_miibus_statchg(device_t dev)
5786c8d8eccSSepherosa Ziehau {
5796c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
5806c8d8eccSSepherosa Ziehau 	struct mii_data *mii;
5814aa71e73SSepherosa Ziehau 	uint32_t mac_mode;
5826c8d8eccSSepherosa Ziehau 
5836c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
5844aa71e73SSepherosa Ziehau 	if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING) == 0)
5854aa71e73SSepherosa Ziehau 		return;
5864aa71e73SSepherosa Ziehau 
5876c8d8eccSSepherosa Ziehau 	mii = device_get_softc(sc->bnx_miibus);
5886c8d8eccSSepherosa Ziehau 
5896c8d8eccSSepherosa Ziehau 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
5906c8d8eccSSepherosa Ziehau 	    (IFM_ACTIVE | IFM_AVALID)) {
5916c8d8eccSSepherosa Ziehau 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5926c8d8eccSSepherosa Ziehau 		case IFM_10_T:
5936c8d8eccSSepherosa Ziehau 		case IFM_100_TX:
5946c8d8eccSSepherosa Ziehau 			sc->bnx_link = 1;
5956c8d8eccSSepherosa Ziehau 			break;
5966c8d8eccSSepherosa Ziehau 		case IFM_1000_T:
5976c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
5986c8d8eccSSepherosa Ziehau 		case IFM_2500_SX:
5996c8d8eccSSepherosa Ziehau 			sc->bnx_link = 1;
6006c8d8eccSSepherosa Ziehau 			break;
6016c8d8eccSSepherosa Ziehau 		default:
6026c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
6036c8d8eccSSepherosa Ziehau 			break;
6046c8d8eccSSepherosa Ziehau 		}
6056c8d8eccSSepherosa Ziehau 	} else {
6066c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
6076c8d8eccSSepherosa Ziehau 	}
6086c8d8eccSSepherosa Ziehau 	if (sc->bnx_link == 0)
6096c8d8eccSSepherosa Ziehau 		return;
6106c8d8eccSSepherosa Ziehau 
6114aa71e73SSepherosa Ziehau 	/*
6124aa71e73SSepherosa Ziehau 	 * APE firmware touches these registers to keep the MAC
6134aa71e73SSepherosa Ziehau 	 * connected to the outside world.  Try to keep the
6144aa71e73SSepherosa Ziehau 	 * accesses atomic.
6154aa71e73SSepherosa Ziehau 	 */
6166c8d8eccSSepherosa Ziehau 
6174aa71e73SSepherosa Ziehau 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
6184aa71e73SSepherosa Ziehau 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
6194aa71e73SSepherosa Ziehau 
6204aa71e73SSepherosa Ziehau 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
6214aa71e73SSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
6224aa71e73SSepherosa Ziehau 		mac_mode |= BGE_PORTMODE_GMII;
6234aa71e73SSepherosa Ziehau 	else
6244aa71e73SSepherosa Ziehau 		mac_mode |= BGE_PORTMODE_MII;
6254aa71e73SSepherosa Ziehau 
6264aa71e73SSepherosa Ziehau 	if ((mii->mii_media_active & IFM_GMASK) != IFM_FDX)
6274aa71e73SSepherosa Ziehau 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
6284aa71e73SSepherosa Ziehau 
6294aa71e73SSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
6304aa71e73SSepherosa Ziehau 	DELAY(40);
6316c8d8eccSSepherosa Ziehau }
6326c8d8eccSSepherosa Ziehau 
6336c8d8eccSSepherosa Ziehau /*
6346c8d8eccSSepherosa Ziehau  * Memory management for jumbo frames.
6356c8d8eccSSepherosa Ziehau  */
6366c8d8eccSSepherosa Ziehau static int
bnx_alloc_jumbo_mem(struct bnx_softc * sc)6376c8d8eccSSepherosa Ziehau bnx_alloc_jumbo_mem(struct bnx_softc *sc)
6386c8d8eccSSepherosa Ziehau {
6396c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
6406c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
6416c8d8eccSSepherosa Ziehau 	uint8_t *ptr;
6426c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
6436c8d8eccSSepherosa Ziehau 	int i, error;
6446c8d8eccSSepherosa Ziehau 
6456c8d8eccSSepherosa Ziehau 	/*
6466c8d8eccSSepherosa Ziehau 	 * Create tag for jumbo mbufs.
6476c8d8eccSSepherosa Ziehau 	 * This is really a bit of a kludge. We allocate a special
6486c8d8eccSSepherosa Ziehau 	 * jumbo buffer pool which (thanks to the way our DMA
6496c8d8eccSSepherosa Ziehau 	 * memory allocation works) will consist of contiguous
6506c8d8eccSSepherosa Ziehau 	 * pages. This means that even though a jumbo buffer might
6516c8d8eccSSepherosa Ziehau 	 * be larger than a page size, we don't really need to
6526c8d8eccSSepherosa Ziehau 	 * map it into more than one DMA segment. However, the
6536c8d8eccSSepherosa Ziehau 	 * default mbuf tag will result in multi-segment mappings,
6546c8d8eccSSepherosa Ziehau 	 * so we have to create a special jumbo mbuf tag that
6556c8d8eccSSepherosa Ziehau 	 * lets us get away with mapping the jumbo buffers as
6566c8d8eccSSepherosa Ziehau 	 * a single segment. I think eventually the driver should
6576c8d8eccSSepherosa Ziehau 	 * be changed so that it uses ordinary mbufs and cluster
6586c8d8eccSSepherosa Ziehau 	 * buffers, i.e. jumbo frames can span multiple DMA
6596c8d8eccSSepherosa Ziehau 	 * descriptors. But that's a project for another day.
6606c8d8eccSSepherosa Ziehau 	 */
6616c8d8eccSSepherosa Ziehau 
6626c8d8eccSSepherosa Ziehau 	/*
6636c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo RX ring.
6646c8d8eccSSepherosa Ziehau 	 */
6656c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_JUMBO_RX_RING_SZ,
6666c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
6676c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_rx_jumbo_ring_map,
6686c8d8eccSSepherosa Ziehau 				    (void *)&sc->bnx_ldata.bnx_rx_jumbo_ring,
6696c8d8eccSSepherosa Ziehau 				    &sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
6706c8d8eccSSepherosa Ziehau 	if (error) {
6716c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo RX ring\n");
6726c8d8eccSSepherosa Ziehau 		return error;
6736c8d8eccSSepherosa Ziehau 	}
6746c8d8eccSSepherosa Ziehau 
6756c8d8eccSSepherosa Ziehau 	/*
6766c8d8eccSSepherosa Ziehau 	 * Create DMA stuffs for jumbo buffer block.
6776c8d8eccSSepherosa Ziehau 	 */
6786c8d8eccSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BNX_JMEM,
6796c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_tag,
6806c8d8eccSSepherosa Ziehau 				    &sc->bnx_cdata.bnx_jumbo_map,
6816c8d8eccSSepherosa Ziehau 				    (void **)&sc->bnx_ldata.bnx_jumbo_buf,
6826c8d8eccSSepherosa Ziehau 				    &paddr);
6836c8d8eccSSepherosa Ziehau 	if (error) {
6846c8d8eccSSepherosa Ziehau 		if_printf(ifp, "could not create jumbo buffer\n");
6856c8d8eccSSepherosa Ziehau 		return error;
6866c8d8eccSSepherosa Ziehau 	}
6876c8d8eccSSepherosa Ziehau 
6886c8d8eccSSepherosa Ziehau 	SLIST_INIT(&sc->bnx_jfree_listhead);
6896c8d8eccSSepherosa Ziehau 
6906c8d8eccSSepherosa Ziehau 	/*
6916c8d8eccSSepherosa Ziehau 	 * Now divide it up into 9K pieces and save the addresses
6926c8d8eccSSepherosa Ziehau 	 * in an array. Note that we play an evil trick here by using
6936c8d8eccSSepherosa Ziehau 	 * the first few bytes in the buffer to hold the the address
6946c8d8eccSSepherosa Ziehau 	 * of the softc structure for this interface. This is because
6956c8d8eccSSepherosa Ziehau 	 * bnx_jfree() needs it, but it is called by the mbuf management
6966c8d8eccSSepherosa Ziehau 	 * code which will not pass it to us explicitly.
6976c8d8eccSSepherosa Ziehau 	 */
6986c8d8eccSSepherosa Ziehau 	for (i = 0, ptr = sc->bnx_ldata.bnx_jumbo_buf; i < BNX_JSLOTS; i++) {
6996c8d8eccSSepherosa Ziehau 		entry = &sc->bnx_cdata.bnx_jslots[i];
7006c8d8eccSSepherosa Ziehau 		entry->bnx_sc = sc;
7016c8d8eccSSepherosa Ziehau 		entry->bnx_buf = ptr;
7026c8d8eccSSepherosa Ziehau 		entry->bnx_paddr = paddr;
7036c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 0;
7046c8d8eccSSepherosa Ziehau 		entry->bnx_slot = i;
7056c8d8eccSSepherosa Ziehau 		SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead, entry, jslot_link);
7066c8d8eccSSepherosa Ziehau 
7076c8d8eccSSepherosa Ziehau 		ptr += BNX_JLEN;
7086c8d8eccSSepherosa Ziehau 		paddr += BNX_JLEN;
7096c8d8eccSSepherosa Ziehau 	}
7106c8d8eccSSepherosa Ziehau 	return 0;
7116c8d8eccSSepherosa Ziehau }
7126c8d8eccSSepherosa Ziehau 
7136c8d8eccSSepherosa Ziehau static void
bnx_free_jumbo_mem(struct bnx_softc * sc)7146c8d8eccSSepherosa Ziehau bnx_free_jumbo_mem(struct bnx_softc *sc)
7156c8d8eccSSepherosa Ziehau {
7166c8d8eccSSepherosa Ziehau 	/* Destroy jumbo RX ring. */
7176c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_rx_jumbo_ring_tag,
7186c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_rx_jumbo_ring_map,
7196c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_rx_jumbo_ring);
7206c8d8eccSSepherosa Ziehau 
7216c8d8eccSSepherosa Ziehau 	/* Destroy jumbo buffer block. */
7226c8d8eccSSepherosa Ziehau 	bnx_dma_block_free(sc->bnx_cdata.bnx_jumbo_tag,
7236c8d8eccSSepherosa Ziehau 			   sc->bnx_cdata.bnx_jumbo_map,
7246c8d8eccSSepherosa Ziehau 			   sc->bnx_ldata.bnx_jumbo_buf);
7256c8d8eccSSepherosa Ziehau }
7266c8d8eccSSepherosa Ziehau 
7276c8d8eccSSepherosa Ziehau /*
7286c8d8eccSSepherosa Ziehau  * Allocate a jumbo buffer.
7296c8d8eccSSepherosa Ziehau  */
7306c8d8eccSSepherosa Ziehau static struct bnx_jslot *
bnx_jalloc(struct bnx_softc * sc)7316c8d8eccSSepherosa Ziehau bnx_jalloc(struct bnx_softc *sc)
7326c8d8eccSSepherosa Ziehau {
7336c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry;
7346c8d8eccSSepherosa Ziehau 
7356c8d8eccSSepherosa Ziehau 	lwkt_serialize_enter(&sc->bnx_jslot_serializer);
7366c8d8eccSSepherosa Ziehau 	entry = SLIST_FIRST(&sc->bnx_jfree_listhead);
7376c8d8eccSSepherosa Ziehau 	if (entry) {
7386c8d8eccSSepherosa Ziehau 		SLIST_REMOVE_HEAD(&sc->bnx_jfree_listhead, jslot_link);
7396c8d8eccSSepherosa Ziehau 		entry->bnx_inuse = 1;
7406c8d8eccSSepherosa Ziehau 	} else {
7416c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "no free jumbo buffers\n");
7426c8d8eccSSepherosa Ziehau 	}
7436c8d8eccSSepherosa Ziehau 	lwkt_serialize_exit(&sc->bnx_jslot_serializer);
7446c8d8eccSSepherosa Ziehau 	return(entry);
7456c8d8eccSSepherosa Ziehau }
7466c8d8eccSSepherosa Ziehau 
7476c8d8eccSSepherosa Ziehau /*
7486c8d8eccSSepherosa Ziehau  * Adjust usage count on a jumbo buffer.
7496c8d8eccSSepherosa Ziehau  */
7506c8d8eccSSepherosa Ziehau static void
bnx_jref(void * arg)7516c8d8eccSSepherosa Ziehau bnx_jref(void *arg)
7526c8d8eccSSepherosa Ziehau {
7536c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
7546c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
7556c8d8eccSSepherosa Ziehau 
7566c8d8eccSSepherosa Ziehau 	if (sc == NULL)
7576c8d8eccSSepherosa Ziehau 		panic("bnx_jref: can't find softc pointer!");
7586c8d8eccSSepherosa Ziehau 
7596c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
7606c8d8eccSSepherosa Ziehau 		panic("bnx_jref: asked to reference buffer "
7616c8d8eccSSepherosa Ziehau 		    "that we don't manage!");
7626c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
7636c8d8eccSSepherosa Ziehau 		panic("bnx_jref: buffer already free!");
7646c8d8eccSSepherosa Ziehau 	} else {
7656c8d8eccSSepherosa Ziehau 		atomic_add_int(&entry->bnx_inuse, 1);
7666c8d8eccSSepherosa Ziehau 	}
7676c8d8eccSSepherosa Ziehau }
7686c8d8eccSSepherosa Ziehau 
7696c8d8eccSSepherosa Ziehau /*
7706c8d8eccSSepherosa Ziehau  * Release a jumbo buffer.
7716c8d8eccSSepherosa Ziehau  */
7726c8d8eccSSepherosa Ziehau static void
bnx_jfree(void * arg)7736c8d8eccSSepherosa Ziehau bnx_jfree(void *arg)
7746c8d8eccSSepherosa Ziehau {
7756c8d8eccSSepherosa Ziehau 	struct bnx_jslot *entry = (struct bnx_jslot *)arg;
7766c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = entry->bnx_sc;
7776c8d8eccSSepherosa Ziehau 
7786c8d8eccSSepherosa Ziehau 	if (sc == NULL)
7796c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: can't find softc pointer!");
7806c8d8eccSSepherosa Ziehau 
7816c8d8eccSSepherosa Ziehau 	if (&sc->bnx_cdata.bnx_jslots[entry->bnx_slot] != entry) {
7826c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: asked to free buffer that we don't manage!");
7836c8d8eccSSepherosa Ziehau 	} else if (entry->bnx_inuse == 0) {
7846c8d8eccSSepherosa Ziehau 		panic("bnx_jfree: buffer already free!");
7856c8d8eccSSepherosa Ziehau 	} else {
7866c8d8eccSSepherosa Ziehau 		/*
7876c8d8eccSSepherosa Ziehau 		 * Possible MP race to 0, use the serializer.  The atomic insn
7886c8d8eccSSepherosa Ziehau 		 * is still needed for races against bnx_jref().
7896c8d8eccSSepherosa Ziehau 		 */
7906c8d8eccSSepherosa Ziehau 		lwkt_serialize_enter(&sc->bnx_jslot_serializer);
7916c8d8eccSSepherosa Ziehau 		atomic_subtract_int(&entry->bnx_inuse, 1);
7926c8d8eccSSepherosa Ziehau 		if (entry->bnx_inuse == 0) {
7936c8d8eccSSepherosa Ziehau 			SLIST_INSERT_HEAD(&sc->bnx_jfree_listhead,
7946c8d8eccSSepherosa Ziehau 					  entry, jslot_link);
7956c8d8eccSSepherosa Ziehau 		}
7966c8d8eccSSepherosa Ziehau 		lwkt_serialize_exit(&sc->bnx_jslot_serializer);
7976c8d8eccSSepherosa Ziehau 	}
7986c8d8eccSSepherosa Ziehau }
7996c8d8eccSSepherosa Ziehau 
8006c8d8eccSSepherosa Ziehau 
8016c8d8eccSSepherosa Ziehau /*
8026c8d8eccSSepherosa Ziehau  * Intialize a standard receive ring descriptor.
8036c8d8eccSSepherosa Ziehau  */
8046c8d8eccSSepherosa Ziehau static int
bnx_newbuf_std(struct bnx_rx_ret_ring * ret,int i,int init)805beedf5beSSepherosa Ziehau bnx_newbuf_std(struct bnx_rx_ret_ring *ret, int i, int init)
8066c8d8eccSSepherosa Ziehau {
8076c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
8086c8d8eccSSepherosa Ziehau 	bus_dma_segment_t seg;
8096c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
8106c8d8eccSSepherosa Ziehau 	int error, nsegs;
811beedf5beSSepherosa Ziehau 	struct bnx_rx_buf *rb;
8126c8d8eccSSepherosa Ziehau 
813841cdf08SSepherosa Ziehau 	rb = &ret->bnx_std->bnx_rx_std_buf[i];
814841cdf08SSepherosa Ziehau 	KASSERT(!rb->bnx_rx_refilled, ("RX buf %dth has been refilled", i));
815841cdf08SSepherosa Ziehau 
816b5523eacSSascha Wildner 	m_new = m_getcl(init ? M_WAITOK : M_NOWAIT, MT_DATA, M_PKTHDR);
817841cdf08SSepherosa Ziehau 	if (m_new == NULL) {
818841cdf08SSepherosa Ziehau 		error = ENOBUFS;
819841cdf08SSepherosa Ziehau 		goto back;
820841cdf08SSepherosa Ziehau 	}
8216c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
8226c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
8236c8d8eccSSepherosa Ziehau 
824beedf5beSSepherosa Ziehau 	error = bus_dmamap_load_mbuf_segment(ret->bnx_rx_mtag,
825beedf5beSSepherosa Ziehau 	    ret->bnx_rx_tmpmap, m_new, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
8266c8d8eccSSepherosa Ziehau 	if (error) {
8276c8d8eccSSepherosa Ziehau 		m_freem(m_new);
828841cdf08SSepherosa Ziehau 		goto back;
8296c8d8eccSSepherosa Ziehau 	}
8306c8d8eccSSepherosa Ziehau 
8316c8d8eccSSepherosa Ziehau 	if (!init) {
832beedf5beSSepherosa Ziehau 		bus_dmamap_sync(ret->bnx_rx_mtag, rb->bnx_rx_dmamap,
8336c8d8eccSSepherosa Ziehau 		    BUS_DMASYNC_POSTREAD);
834beedf5beSSepherosa Ziehau 		bus_dmamap_unload(ret->bnx_rx_mtag, rb->bnx_rx_dmamap);
8356c8d8eccSSepherosa Ziehau 	}
8366c8d8eccSSepherosa Ziehau 
837beedf5beSSepherosa Ziehau 	map = ret->bnx_rx_tmpmap;
838beedf5beSSepherosa Ziehau 	ret->bnx_rx_tmpmap = rb->bnx_rx_dmamap;
8396c8d8eccSSepherosa Ziehau 
840841cdf08SSepherosa Ziehau 	rb->bnx_rx_dmamap = map;
841beedf5beSSepherosa Ziehau 	rb->bnx_rx_mbuf = m_new;
842beedf5beSSepherosa Ziehau 	rb->bnx_rx_paddr = seg.ds_addr;
843695a8586SSepherosa Ziehau 	rb->bnx_rx_len = m_new->m_len;
844841cdf08SSepherosa Ziehau back:
845841cdf08SSepherosa Ziehau 	cpu_sfence();
846841cdf08SSepherosa Ziehau 	rb->bnx_rx_refilled = 1;
847841cdf08SSepherosa Ziehau 	return error;
8486c8d8eccSSepherosa Ziehau }
8496c8d8eccSSepherosa Ziehau 
8506c8d8eccSSepherosa Ziehau static void
bnx_setup_rxdesc_std(struct bnx_rx_std_ring * std,int i)851beedf5beSSepherosa Ziehau bnx_setup_rxdesc_std(struct bnx_rx_std_ring *std, int i)
8526c8d8eccSSepherosa Ziehau {
853841cdf08SSepherosa Ziehau 	struct bnx_rx_buf *rb;
8546c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
855695a8586SSepherosa Ziehau 	bus_addr_t paddr;
856695a8586SSepherosa Ziehau 	int len;
8576c8d8eccSSepherosa Ziehau 
858beedf5beSSepherosa Ziehau 	rb = &std->bnx_rx_std_buf[i];
859841cdf08SSepherosa Ziehau 	KASSERT(rb->bnx_rx_refilled, ("RX buf %dth is not refilled", i));
860695a8586SSepherosa Ziehau 
861695a8586SSepherosa Ziehau 	paddr = rb->bnx_rx_paddr;
862695a8586SSepherosa Ziehau 	len = rb->bnx_rx_len;
863695a8586SSepherosa Ziehau 
864695a8586SSepherosa Ziehau 	cpu_mfence();
865695a8586SSepherosa Ziehau 
866841cdf08SSepherosa Ziehau 	rb->bnx_rx_refilled = 0;
8676c8d8eccSSepherosa Ziehau 
868841cdf08SSepherosa Ziehau 	r = &std->bnx_rx_std_ring[i];
869695a8586SSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(paddr);
870695a8586SSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(paddr);
871695a8586SSepherosa Ziehau 	r->bge_len = len;
8726c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
8736c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END;
8746c8d8eccSSepherosa Ziehau }
8756c8d8eccSSepherosa Ziehau 
8766c8d8eccSSepherosa Ziehau /*
8776c8d8eccSSepherosa Ziehau  * Initialize a jumbo receive ring descriptor. This allocates
8786c8d8eccSSepherosa Ziehau  * a jumbo buffer from the pool managed internally by the driver.
8796c8d8eccSSepherosa Ziehau  */
8806c8d8eccSSepherosa Ziehau static int
bnx_newbuf_jumbo(struct bnx_softc * sc,int i,int init)8816c8d8eccSSepherosa Ziehau bnx_newbuf_jumbo(struct bnx_softc *sc, int i, int init)
8826c8d8eccSSepherosa Ziehau {
8836c8d8eccSSepherosa Ziehau 	struct mbuf *m_new = NULL;
8846c8d8eccSSepherosa Ziehau 	struct bnx_jslot *buf;
8856c8d8eccSSepherosa Ziehau 	bus_addr_t paddr;
8866c8d8eccSSepherosa Ziehau 
8876c8d8eccSSepherosa Ziehau 	/* Allocate the mbuf. */
888b5523eacSSascha Wildner 	MGETHDR(m_new, init ? M_WAITOK : M_NOWAIT, MT_DATA);
8896c8d8eccSSepherosa Ziehau 	if (m_new == NULL)
8906c8d8eccSSepherosa Ziehau 		return ENOBUFS;
8916c8d8eccSSepherosa Ziehau 
8926c8d8eccSSepherosa Ziehau 	/* Allocate the jumbo buffer */
8936c8d8eccSSepherosa Ziehau 	buf = bnx_jalloc(sc);
8946c8d8eccSSepherosa Ziehau 	if (buf == NULL) {
8956c8d8eccSSepherosa Ziehau 		m_freem(m_new);
8966c8d8eccSSepherosa Ziehau 		return ENOBUFS;
8976c8d8eccSSepherosa Ziehau 	}
8986c8d8eccSSepherosa Ziehau 
8996c8d8eccSSepherosa Ziehau 	/* Attach the buffer to the mbuf. */
9006c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_arg = buf;
9016c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_buf = buf->bnx_buf;
9026c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_free = bnx_jfree;
9036c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_ref = bnx_jref;
9046c8d8eccSSepherosa Ziehau 	m_new->m_ext.ext_size = BNX_JUMBO_FRAMELEN;
9056c8d8eccSSepherosa Ziehau 
9066c8d8eccSSepherosa Ziehau 	m_new->m_flags |= M_EXT;
9076c8d8eccSSepherosa Ziehau 
9086c8d8eccSSepherosa Ziehau 	m_new->m_data = m_new->m_ext.ext_buf;
9096c8d8eccSSepherosa Ziehau 	m_new->m_len = m_new->m_pkthdr.len = m_new->m_ext.ext_size;
9106c8d8eccSSepherosa Ziehau 
9116c8d8eccSSepherosa Ziehau 	paddr = buf->bnx_paddr;
9126c8d8eccSSepherosa Ziehau 	m_adj(m_new, ETHER_ALIGN);
9136c8d8eccSSepherosa Ziehau 	paddr += ETHER_ALIGN;
9146c8d8eccSSepherosa Ziehau 
9156c8d8eccSSepherosa Ziehau 	/* Save necessary information */
916beedf5beSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_mbuf = m_new;
917beedf5beSSepherosa Ziehau 	sc->bnx_cdata.bnx_rx_jumbo_chain[i].bnx_rx_paddr = paddr;
9186c8d8eccSSepherosa Ziehau 
9196c8d8eccSSepherosa Ziehau 	/* Set up the descriptor. */
9206c8d8eccSSepherosa Ziehau 	bnx_setup_rxdesc_jumbo(sc, i);
9216c8d8eccSSepherosa Ziehau 	return 0;
9226c8d8eccSSepherosa Ziehau }
9236c8d8eccSSepherosa Ziehau 
9246c8d8eccSSepherosa Ziehau static void
bnx_setup_rxdesc_jumbo(struct bnx_softc * sc,int i)9256c8d8eccSSepherosa Ziehau bnx_setup_rxdesc_jumbo(struct bnx_softc *sc, int i)
9266c8d8eccSSepherosa Ziehau {
9276c8d8eccSSepherosa Ziehau 	struct bge_rx_bd *r;
928beedf5beSSepherosa Ziehau 	struct bnx_rx_buf *rc;
9296c8d8eccSSepherosa Ziehau 
9306c8d8eccSSepherosa Ziehau 	r = &sc->bnx_ldata.bnx_rx_jumbo_ring[i];
9316c8d8eccSSepherosa Ziehau 	rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
9326c8d8eccSSepherosa Ziehau 
933beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(rc->bnx_rx_paddr);
934beedf5beSSepherosa Ziehau 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(rc->bnx_rx_paddr);
935beedf5beSSepherosa Ziehau 	r->bge_len = rc->bnx_rx_mbuf->m_len;
9366c8d8eccSSepherosa Ziehau 	r->bge_idx = i;
9376c8d8eccSSepherosa Ziehau 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
9386c8d8eccSSepherosa Ziehau }
9396c8d8eccSSepherosa Ziehau 
9406c8d8eccSSepherosa Ziehau static int
bnx_init_rx_ring_std(struct bnx_rx_std_ring * std)941beedf5beSSepherosa Ziehau bnx_init_rx_ring_std(struct bnx_rx_std_ring *std)
9426c8d8eccSSepherosa Ziehau {
9436c8d8eccSSepherosa Ziehau 	int i, error;
9446c8d8eccSSepherosa Ziehau 
9456c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
946beedf5beSSepherosa Ziehau 		/* Use the first RX return ring's tmp RX mbuf DMA map */
947beedf5beSSepherosa Ziehau 		error = bnx_newbuf_std(&std->bnx_sc->bnx_rx_ret_ring[0], i, 1);
9486c8d8eccSSepherosa Ziehau 		if (error)
9496c8d8eccSSepherosa Ziehau 			return error;
950841cdf08SSepherosa Ziehau 		bnx_setup_rxdesc_std(std, i);
95187c7a7cfSSascha Wildner 	}
9526c8d8eccSSepherosa Ziehau 
953625c3ba3SSepherosa Ziehau 	std->bnx_rx_std_used = 0;
954841cdf08SSepherosa Ziehau 	std->bnx_rx_std_refill = 0;
955841cdf08SSepherosa Ziehau 	std->bnx_rx_std_running = 0;
956841cdf08SSepherosa Ziehau 	cpu_sfence();
957841cdf08SSepherosa Ziehau 	lwkt_serialize_handler_enable(&std->bnx_rx_std_serialize);
958841cdf08SSepherosa Ziehau 
959beedf5beSSepherosa Ziehau 	std->bnx_rx_std = BGE_STD_RX_RING_CNT - 1;
960beedf5beSSepherosa Ziehau 	bnx_writembx(std->bnx_sc, BGE_MBX_RX_STD_PROD_LO, std->bnx_rx_std);
9616c8d8eccSSepherosa Ziehau 
9626c8d8eccSSepherosa Ziehau 	return(0);
9636c8d8eccSSepherosa Ziehau }
9646c8d8eccSSepherosa Ziehau 
9656c8d8eccSSepherosa Ziehau static void
bnx_free_rx_ring_std(struct bnx_rx_std_ring * std)966beedf5beSSepherosa Ziehau bnx_free_rx_ring_std(struct bnx_rx_std_ring *std)
9676c8d8eccSSepherosa Ziehau {
9686c8d8eccSSepherosa Ziehau 	int i;
9696c8d8eccSSepherosa Ziehau 
970841cdf08SSepherosa Ziehau 	lwkt_serialize_handler_disable(&std->bnx_rx_std_serialize);
971841cdf08SSepherosa Ziehau 
9726c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
973beedf5beSSepherosa Ziehau 		struct bnx_rx_buf *rb = &std->bnx_rx_std_buf[i];
9746c8d8eccSSepherosa Ziehau 
975841cdf08SSepherosa Ziehau 		rb->bnx_rx_refilled = 0;
976beedf5beSSepherosa Ziehau 		if (rb->bnx_rx_mbuf != NULL) {
977beedf5beSSepherosa Ziehau 			bus_dmamap_unload(std->bnx_rx_mtag, rb->bnx_rx_dmamap);
978beedf5beSSepherosa Ziehau 			m_freem(rb->bnx_rx_mbuf);
979beedf5beSSepherosa Ziehau 			rb->bnx_rx_mbuf = NULL;
9806c8d8eccSSepherosa Ziehau 		}
981beedf5beSSepherosa Ziehau 		bzero(&std->bnx_rx_std_ring[i], sizeof(struct bge_rx_bd));
9826c8d8eccSSepherosa Ziehau 	}
9836c8d8eccSSepherosa Ziehau }
9846c8d8eccSSepherosa Ziehau 
9856c8d8eccSSepherosa Ziehau static int
bnx_init_rx_ring_jumbo(struct bnx_softc * sc)9866c8d8eccSSepherosa Ziehau bnx_init_rx_ring_jumbo(struct bnx_softc *sc)
9876c8d8eccSSepherosa Ziehau {
9886c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
9896c8d8eccSSepherosa Ziehau 	int i, error;
9906c8d8eccSSepherosa Ziehau 
9916c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
9926c8d8eccSSepherosa Ziehau 		error = bnx_newbuf_jumbo(sc, i, 1);
9936c8d8eccSSepherosa Ziehau 		if (error)
9946c8d8eccSSepherosa Ziehau 			return error;
99587c7a7cfSSascha Wildner 	}
9966c8d8eccSSepherosa Ziehau 
9976c8d8eccSSepherosa Ziehau 	sc->bnx_jumbo = BGE_JUMBO_RX_RING_CNT - 1;
9986c8d8eccSSepherosa Ziehau 
9996c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
10006c8d8eccSSepherosa Ziehau 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
10016c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
10026c8d8eccSSepherosa Ziehau 
10036c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bnx_jumbo);
10046c8d8eccSSepherosa Ziehau 
10056c8d8eccSSepherosa Ziehau 	return(0);
10066c8d8eccSSepherosa Ziehau }
10076c8d8eccSSepherosa Ziehau 
10086c8d8eccSSepherosa Ziehau static void
bnx_free_rx_ring_jumbo(struct bnx_softc * sc)10096c8d8eccSSepherosa Ziehau bnx_free_rx_ring_jumbo(struct bnx_softc *sc)
10106c8d8eccSSepherosa Ziehau {
10116c8d8eccSSepherosa Ziehau 	int i;
10126c8d8eccSSepherosa Ziehau 
10136c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1014beedf5beSSepherosa Ziehau 		struct bnx_rx_buf *rc = &sc->bnx_cdata.bnx_rx_jumbo_chain[i];
10156c8d8eccSSepherosa Ziehau 
1016beedf5beSSepherosa Ziehau 		if (rc->bnx_rx_mbuf != NULL) {
1017beedf5beSSepherosa Ziehau 			m_freem(rc->bnx_rx_mbuf);
1018beedf5beSSepherosa Ziehau 			rc->bnx_rx_mbuf = NULL;
10196c8d8eccSSepherosa Ziehau 		}
10206c8d8eccSSepherosa Ziehau 		bzero(&sc->bnx_ldata.bnx_rx_jumbo_ring[i],
10216c8d8eccSSepherosa Ziehau 		    sizeof(struct bge_rx_bd));
10226c8d8eccSSepherosa Ziehau 	}
10236c8d8eccSSepherosa Ziehau }
10246c8d8eccSSepherosa Ziehau 
10256c8d8eccSSepherosa Ziehau static void
bnx_free_tx_ring(struct bnx_tx_ring * txr)102633a04907SSepherosa Ziehau bnx_free_tx_ring(struct bnx_tx_ring *txr)
10276c8d8eccSSepherosa Ziehau {
10286c8d8eccSSepherosa Ziehau 	int i;
10296c8d8eccSSepherosa Ziehau 
10306c8d8eccSSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1031fa4b1067SSepherosa Ziehau 		struct bnx_tx_buf *buf = &txr->bnx_tx_buf[i];
1032fa4b1067SSepherosa Ziehau 
1033fa4b1067SSepherosa Ziehau 		if (buf->bnx_tx_mbuf != NULL) {
103433a04907SSepherosa Ziehau 			bus_dmamap_unload(txr->bnx_tx_mtag,
1035fa4b1067SSepherosa Ziehau 			    buf->bnx_tx_dmamap);
1036fa4b1067SSepherosa Ziehau 			m_freem(buf->bnx_tx_mbuf);
1037fa4b1067SSepherosa Ziehau 			buf->bnx_tx_mbuf = NULL;
10386c8d8eccSSepherosa Ziehau 		}
103933a04907SSepherosa Ziehau 		bzero(&txr->bnx_tx_ring[i], sizeof(struct bge_tx_bd));
10406c8d8eccSSepherosa Ziehau 	}
104133a04907SSepherosa Ziehau 	txr->bnx_tx_saved_considx = BNX_TXCONS_UNSET;
10426c8d8eccSSepherosa Ziehau }
10436c8d8eccSSepherosa Ziehau 
10446c8d8eccSSepherosa Ziehau static int
bnx_init_tx_ring(struct bnx_tx_ring * txr)104533a04907SSepherosa Ziehau bnx_init_tx_ring(struct bnx_tx_ring *txr)
10466c8d8eccSSepherosa Ziehau {
1047fa639b88SSepherosa Ziehau 	txr->bnx_tx_cnt = 0;
104833a04907SSepherosa Ziehau 	txr->bnx_tx_saved_considx = 0;
104933a04907SSepherosa Ziehau 	txr->bnx_tx_prodidx = 0;
10506c8d8eccSSepherosa Ziehau 
10516c8d8eccSSepherosa Ziehau 	/* Initialize transmit producer index for host-memory send ring. */
10528bd43d5dSSepherosa Ziehau 	bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, txr->bnx_tx_prodidx);
10536c8d8eccSSepherosa Ziehau 
10546c8d8eccSSepherosa Ziehau 	return(0);
10556c8d8eccSSepherosa Ziehau }
10566c8d8eccSSepherosa Ziehau 
10576c8d8eccSSepherosa Ziehau static void
bnx_setmulti(struct bnx_softc * sc)10586c8d8eccSSepherosa Ziehau bnx_setmulti(struct bnx_softc *sc)
10596c8d8eccSSepherosa Ziehau {
10606c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
10616c8d8eccSSepherosa Ziehau 	struct ifmultiaddr *ifma;
10626c8d8eccSSepherosa Ziehau 	uint32_t hashes[4] = { 0, 0, 0, 0 };
10636c8d8eccSSepherosa Ziehau 	int h, i;
10646c8d8eccSSepherosa Ziehau 
10656c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
10666c8d8eccSSepherosa Ziehau 
10676c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
10686c8d8eccSSepherosa Ziehau 		for (i = 0; i < 4; i++)
10696c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
10706c8d8eccSSepherosa Ziehau 		return;
10716c8d8eccSSepherosa Ziehau 	}
10726c8d8eccSSepherosa Ziehau 
10736c8d8eccSSepherosa Ziehau 	/* First, zot all the existing filters. */
10746c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
10756c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
10766c8d8eccSSepherosa Ziehau 
10776c8d8eccSSepherosa Ziehau 	/* Now program new ones. */
10786c8d8eccSSepherosa Ziehau 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
10796c8d8eccSSepherosa Ziehau 		if (ifma->ifma_addr->sa_family != AF_LINK)
10806c8d8eccSSepherosa Ziehau 			continue;
10816c8d8eccSSepherosa Ziehau 		h = ether_crc32_le(
10826c8d8eccSSepherosa Ziehau 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
10836c8d8eccSSepherosa Ziehau 		    ETHER_ADDR_LEN) & 0x7f;
10846c8d8eccSSepherosa Ziehau 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
10856c8d8eccSSepherosa Ziehau 	}
10866c8d8eccSSepherosa Ziehau 
10876c8d8eccSSepherosa Ziehau 	for (i = 0; i < 4; i++)
10886c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
10896c8d8eccSSepherosa Ziehau }
10906c8d8eccSSepherosa Ziehau 
10916c8d8eccSSepherosa Ziehau /*
10926c8d8eccSSepherosa Ziehau  * Do endian, PCI and DMA initialization. Also check the on-board ROM
10936c8d8eccSSepherosa Ziehau  * self-test results.
10946c8d8eccSSepherosa Ziehau  */
10956c8d8eccSSepherosa Ziehau static int
bnx_chipinit(struct bnx_softc * sc)10966c8d8eccSSepherosa Ziehau bnx_chipinit(struct bnx_softc *sc)
10976c8d8eccSSepherosa Ziehau {
10986c8d8eccSSepherosa Ziehau 	uint32_t dma_rw_ctl, mode_ctl;
10996c8d8eccSSepherosa Ziehau 	int i;
11006c8d8eccSSepherosa Ziehau 
11016c8d8eccSSepherosa Ziehau 	/* Set endian type before we access any non-PCI registers. */
11026c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_MISC_CTL,
11036c8d8eccSSepherosa Ziehau 	    BGE_INIT | BGE_PCIMISCCTL_TAGGED_STATUS, 4);
11046c8d8eccSSepherosa Ziehau 
11056c8d8eccSSepherosa Ziehau 	/*
11066c8d8eccSSepherosa Ziehau 	 * Clear the MAC statistics block in the NIC's
11076c8d8eccSSepherosa Ziehau 	 * internal memory.
11086c8d8eccSSepherosa Ziehau 	 */
11096c8d8eccSSepherosa Ziehau 	for (i = BGE_STATS_BLOCK;
11106c8d8eccSSepherosa Ziehau 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
11116c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
11126c8d8eccSSepherosa Ziehau 
11136c8d8eccSSepherosa Ziehau 	for (i = BGE_STATUS_BLOCK;
11146c8d8eccSSepherosa Ziehau 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
11156c8d8eccSSepherosa Ziehau 		BNX_MEMWIN_WRITE(sc, i, 0);
11166c8d8eccSSepherosa Ziehau 
1117d7872545SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc)) {
1118d7872545SSepherosa Ziehau 		uint32_t val;
1119d7872545SSepherosa Ziehau 
1120d7872545SSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0) {
1121d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1122d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1123d7872545SSepherosa Ziehau 
1124d7872545SSepherosa Ziehau 			/* Access the lower 1K of PL PCI-E block registers. */
1125d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1126d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_PL_SEL);
1127d7872545SSepherosa Ziehau 
1128d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_PL_LO_PHYCTL5);
1129d7872545SSepherosa Ziehau 			val |= BGE_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ;
1130d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_PL_LO_PHYCTL5, val);
1131d7872545SSepherosa Ziehau 
1132d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1133d7872545SSepherosa Ziehau 		}
1134d7872545SSepherosa Ziehau 		if (sc->bnx_chiprev != BGE_CHIPREV_57765_AX) {
11351749651bSSepherosa Ziehau 			/* Fix transmit hangs */
11361749651bSSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL);
11371749651bSSepherosa Ziehau 			val |= BGE_CPMU_PADRNG_CTL_RDIV2;
11381749651bSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL, val);
11391749651bSSepherosa Ziehau 
1140d7872545SSepherosa Ziehau 			mode_ctl = CSR_READ_4(sc, BGE_MODE_CTL);
1141d7872545SSepherosa Ziehau 			val = mode_ctl & ~BGE_MODECTL_PCIE_PORTS;
1142d7872545SSepherosa Ziehau 
1143d7872545SSepherosa Ziehau 			/* Access the lower 1K of DL PCI-E block registers. */
1144d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL,
1145d7872545SSepherosa Ziehau 			    val | BGE_MODECTL_PCIE_DL_SEL);
1146d7872545SSepherosa Ziehau 
1147d7872545SSepherosa Ziehau 			val = CSR_READ_4(sc, BGE_PCIE_DL_LO_FTSMAX);
1148d7872545SSepherosa Ziehau 			val &= ~BGE_PCIE_DL_LO_FTSMAX_MASK;
1149d7872545SSepherosa Ziehau 			val |= BGE_PCIE_DL_LO_FTSMAX_VAL;
1150d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_PCIE_DL_LO_FTSMAX, val);
1151d7872545SSepherosa Ziehau 
1152d7872545SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1153d7872545SSepherosa Ziehau 		}
1154d7872545SSepherosa Ziehau 
1155d7872545SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_CPMU_LSPD_10MB_CLK);
1156d7872545SSepherosa Ziehau 		val &= ~BGE_CPMU_LSPD_10MB_MACCLK_MASK;
1157d7872545SSepherosa Ziehau 		val |= BGE_CPMU_LSPD_10MB_MACCLK_6_25;
1158d7872545SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_CPMU_LSPD_10MB_CLK, val);
1159d7872545SSepherosa Ziehau 	}
1160d7872545SSepherosa Ziehau 
11612890cca3SSepherosa Ziehau 	/*
11622890cca3SSepherosa Ziehau 	 * Set up the PCI DMA control register.
11632890cca3SSepherosa Ziehau 	 */
11642890cca3SSepherosa Ziehau 	dma_rw_ctl = pci_read_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, 4);
11652890cca3SSepherosa Ziehau 	/*
11662890cca3SSepherosa Ziehau 	 * Disable 32bytes cache alignment for DMA write to host memory
11672890cca3SSepherosa Ziehau 	 *
11682890cca3SSepherosa Ziehau 	 * NOTE:
11692890cca3SSepherosa Ziehau 	 * 64bytes cache alignment for DMA write to host memory is still
11702890cca3SSepherosa Ziehau 	 * enabled.
11712890cca3SSepherosa Ziehau 	 */
11722890cca3SSepherosa Ziehau 	dma_rw_ctl |= BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
11736c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
11746c8d8eccSSepherosa Ziehau 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
11756c8d8eccSSepherosa Ziehau 	/*
11766c8d8eccSSepherosa Ziehau 	 * Enable HW workaround for controllers that misinterpret
11776c8d8eccSSepherosa Ziehau 	 * a status tag update and leave interrupts permanently
11786c8d8eccSSepherosa Ziehau 	 * disabled.
11796c8d8eccSSepherosa Ziehau 	 */
11806c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev != BGE_ASICREV_BCM5717 &&
1181b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev != BGE_ASICREV_BCM5762 &&
11822890cca3SSepherosa Ziehau 	    !BNX_IS_57765_FAMILY(sc))
11836c8d8eccSSepherosa Ziehau 		dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
11842890cca3SSepherosa Ziehau 	if (bootverbose) {
11852890cca3SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "DMA read/write %#x\n",
11862890cca3SSepherosa Ziehau 		    dma_rw_ctl);
11876c8d8eccSSepherosa Ziehau 	}
11886c8d8eccSSepherosa Ziehau 	pci_write_config(sc->bnx_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
11896c8d8eccSSepherosa Ziehau 
11906c8d8eccSSepherosa Ziehau 	/*
11916c8d8eccSSepherosa Ziehau 	 * Set up general mode register.
11926c8d8eccSSepherosa Ziehau 	 */
11939f5082d5SSepherosa Ziehau 	mode_ctl = bnx_dma_swap_options(sc);
11949f5082d5SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
11959f5082d5SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
11969f5082d5SSepherosa Ziehau 		/* Retain Host-2-BMC settings written by APE firmware. */
11979f5082d5SSepherosa Ziehau 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
11989f5082d5SSepherosa Ziehau 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
11999f5082d5SSepherosa Ziehau 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
12009f5082d5SSepherosa Ziehau 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
12019f5082d5SSepherosa Ziehau 	}
12029f5082d5SSepherosa Ziehau 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR |
12036c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
12046c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
12056c8d8eccSSepherosa Ziehau 
12066c8d8eccSSepherosa Ziehau 	/*
12076c8d8eccSSepherosa Ziehau 	 * Disable memory write invalidate.  Apparently it is not supported
12086c8d8eccSSepherosa Ziehau 	 * properly by these devices.  Also ensure that INTx isn't disabled,
12096c8d8eccSSepherosa Ziehau 	 * as these chips need it even when using MSI.
12106c8d8eccSSepherosa Ziehau 	 */
12116c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_CMD,
12126c8d8eccSSepherosa Ziehau 	    (PCIM_CMD_MWRICEN | PCIM_CMD_INTxDIS), 4);
12136c8d8eccSSepherosa Ziehau 
12146c8d8eccSSepherosa Ziehau 	/* Set the timer prescaler (always 66Mhz) */
12156c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
12166c8d8eccSSepherosa Ziehau 
12176c8d8eccSSepherosa Ziehau 	return(0);
12186c8d8eccSSepherosa Ziehau }
12196c8d8eccSSepherosa Ziehau 
12206c8d8eccSSepherosa Ziehau static int
bnx_blockinit(struct bnx_softc * sc)12216c8d8eccSSepherosa Ziehau bnx_blockinit(struct bnx_softc *sc)
12226c8d8eccSSepherosa Ziehau {
1223695a8586SSepherosa Ziehau 	struct bnx_intr_data *intr;
12246c8d8eccSSepherosa Ziehau 	struct bge_rcb *rcb;
12256c8d8eccSSepherosa Ziehau 	bus_size_t vrcb;
12266c8d8eccSSepherosa Ziehau 	bge_hostaddr taddr;
12276c8d8eccSSepherosa Ziehau 	uint32_t val;
12286c8d8eccSSepherosa Ziehau 	int i, limit;
12296c8d8eccSSepherosa Ziehau 
12306c8d8eccSSepherosa Ziehau 	/*
12316c8d8eccSSepherosa Ziehau 	 * Initialize the memory window pointer register so that
12326c8d8eccSSepherosa Ziehau 	 * we can access the first 32K of internal NIC RAM. This will
12336c8d8eccSSepherosa Ziehau 	 * allow us to set up the TX send ring RCBs and the RX return
12346c8d8eccSSepherosa Ziehau 	 * ring RCBs, plus other things which live in NIC memory.
12356c8d8eccSSepherosa Ziehau 	 */
12366c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
12376c8d8eccSSepherosa Ziehau 
12386c8d8eccSSepherosa Ziehau 	/* Configure mbuf pool watermarks */
1239f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
12406c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12416c8d8eccSSepherosa Ziehau 		if (sc->arpcom.ac_if.if_mtu > ETHERMTU) {
12426c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
12436c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
12446c8d8eccSSepherosa Ziehau 		} else {
12456c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
12466c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
12476c8d8eccSSepherosa Ziehau 		}
12486c8d8eccSSepherosa Ziehau 	} else {
12496c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12506c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12516c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
12526c8d8eccSSepherosa Ziehau 	}
12536c8d8eccSSepherosa Ziehau 
12546c8d8eccSSepherosa Ziehau 	/* Configure DMA resource watermarks */
12556c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
12566c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
12576c8d8eccSSepherosa Ziehau 
12586c8d8eccSSepherosa Ziehau 	/* Enable buffer manager */
12596c8d8eccSSepherosa Ziehau 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
12606c8d8eccSSepherosa Ziehau 	/*
12616c8d8eccSSepherosa Ziehau 	 * Change the arbitration algorithm of TXMBUF read request to
12626c8d8eccSSepherosa Ziehau 	 * round-robin instead of priority based for BCM5719.  When
12636c8d8eccSSepherosa Ziehau 	 * TXFIFO is almost empty, RDMA will hold its request until
12646c8d8eccSSepherosa Ziehau 	 * TXFIFO is not almost empty.
12656c8d8eccSSepherosa Ziehau 	 */
12666c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
12676c8d8eccSSepherosa Ziehau 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1268e5eebe34SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
1269e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0 ||
1270e5eebe34SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5720_A0)
1271e5eebe34SSepherosa Ziehau 		val |= BGE_BMANMODE_LOMBUF_ATTN;
12726c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
12736c8d8eccSSepherosa Ziehau 
12746c8d8eccSSepherosa Ziehau 	/* Poll for buffer manager start indication */
12756c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
12766c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
12776c8d8eccSSepherosa Ziehau 			break;
12786c8d8eccSSepherosa Ziehau 		DELAY(10);
12796c8d8eccSSepherosa Ziehau 	}
12806c8d8eccSSepherosa Ziehau 
12816c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
12826c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
12836c8d8eccSSepherosa Ziehau 			  "buffer manager failed to start\n");
12846c8d8eccSSepherosa Ziehau 		return(ENXIO);
12856c8d8eccSSepherosa Ziehau 	}
12866c8d8eccSSepherosa Ziehau 
12876c8d8eccSSepherosa Ziehau 	/* Enable flow-through queues */
12886c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
12896c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
12906c8d8eccSSepherosa Ziehau 
12916c8d8eccSSepherosa Ziehau 	/* Wait until queue initialization is complete */
12926c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
12936c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
12946c8d8eccSSepherosa Ziehau 			break;
12956c8d8eccSSepherosa Ziehau 		DELAY(10);
12966c8d8eccSSepherosa Ziehau 	}
12976c8d8eccSSepherosa Ziehau 
12986c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
12996c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
13006c8d8eccSSepherosa Ziehau 			  "flow-through queue init failed\n");
13016c8d8eccSSepherosa Ziehau 		return(ENXIO);
13026c8d8eccSSepherosa Ziehau 	}
13036c8d8eccSSepherosa Ziehau 
13046c8d8eccSSepherosa Ziehau 	/*
13056c8d8eccSSepherosa Ziehau 	 * Summary of rings supported by the controller:
13066c8d8eccSSepherosa Ziehau 	 *
13076c8d8eccSSepherosa Ziehau 	 * Standard Receive Producer Ring
13086c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "standard"
13096c8d8eccSSepherosa Ziehau 	 *   sized frames (typically 1536 bytes) to the controller.
13106c8d8eccSSepherosa Ziehau 	 *
13116c8d8eccSSepherosa Ziehau 	 * Jumbo Receive Producer Ring
13126c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for jumbo sized
13136c8d8eccSSepherosa Ziehau 	 *   frames (i.e. anything bigger than the "standard" frames)
13146c8d8eccSSepherosa Ziehau 	 *   to the controller.
13156c8d8eccSSepherosa Ziehau 	 *
13166c8d8eccSSepherosa Ziehau 	 * Mini Receive Producer Ring
13176c8d8eccSSepherosa Ziehau 	 * - This ring is used to feed receive buffers for "mini"
13186c8d8eccSSepherosa Ziehau 	 *   sized frames to the controller.
13196c8d8eccSSepherosa Ziehau 	 * - This feature required external memory for the controller
13206c8d8eccSSepherosa Ziehau 	 *   but was never used in a production system.  Should always
13216c8d8eccSSepherosa Ziehau 	 *   be disabled.
13226c8d8eccSSepherosa Ziehau 	 *
13236c8d8eccSSepherosa Ziehau 	 * Receive Return Ring
13246c8d8eccSSepherosa Ziehau 	 * - After the controller has placed an incoming frame into a
13256c8d8eccSSepherosa Ziehau 	 *   receive buffer that buffer is moved into a receive return
13266c8d8eccSSepherosa Ziehau 	 *   ring.  The driver is then responsible to passing the
13272a581495SSepherosa Ziehau 	 *   buffer up to the stack.  BCM5718/BCM57785 families support
13282a581495SSepherosa Ziehau 	 *   multiple receive return rings.
13296c8d8eccSSepherosa Ziehau 	 *
13306c8d8eccSSepherosa Ziehau 	 * Send Ring
13312a581495SSepherosa Ziehau 	 * - This ring is used for outgoing frames.  BCM5719/BCM5720
13322a581495SSepherosa Ziehau 	 *   support multiple send rings.
13336c8d8eccSSepherosa Ziehau 	 */
13346c8d8eccSSepherosa Ziehau 
13356c8d8eccSSepherosa Ziehau 	/* Initialize the standard receive producer ring control block. */
13366c8d8eccSSepherosa Ziehau 	rcb = &sc->bnx_ldata.bnx_info.bnx_std_rx_rcb;
13376c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_lo =
1338beedf5beSSepherosa Ziehau 	    BGE_ADDR_LO(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
13396c8d8eccSSepherosa Ziehau 	rcb->bge_hostaddr.bge_addr_hi =
1340beedf5beSSepherosa Ziehau 	    BGE_ADDR_HI(sc->bnx_rx_std_ring.bnx_rx_std_ring_paddr);
1341f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
13426c8d8eccSSepherosa Ziehau 		/*
13436c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
13446c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Maximum RX frame size
13456c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
13466c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
13476c8d8eccSSepherosa Ziehau 		 */
13486c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
13496c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(512, BNX_MAX_FRAMELEN << 2);
13506c8d8eccSSepherosa Ziehau 	} else {
13516c8d8eccSSepherosa Ziehau 		/*
13526c8d8eccSSepherosa Ziehau 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
13536c8d8eccSSepherosa Ziehau 		 * Bits 15-2 : Reserved (should be 0)
13546c8d8eccSSepherosa Ziehau 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
13556c8d8eccSSepherosa Ziehau 		 * Bit 0     : Reserved
13566c8d8eccSSepherosa Ziehau 		 */
13576c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13586c8d8eccSSepherosa Ziehau 	}
1359303fdc72SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
13606c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
13616c8d8eccSSepherosa Ziehau 	else
13626c8d8eccSSepherosa Ziehau 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
13636c8d8eccSSepherosa Ziehau 	/* Write the standard receive producer ring control block. */
13646c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
13656c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
13666c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1367695a8586SSepherosa Ziehau 	if (!BNX_IS_5717_PLUS(sc))
13686c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
13696c8d8eccSSepherosa Ziehau 	/* Reset the standard receive producer ring producer index. */
13706c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
13716c8d8eccSSepherosa Ziehau 
13726c8d8eccSSepherosa Ziehau 	/*
13736c8d8eccSSepherosa Ziehau 	 * Initialize the jumbo RX producer ring control
13746c8d8eccSSepherosa Ziehau 	 * block.  We set the 'ring disabled' bit in the
13756c8d8eccSSepherosa Ziehau 	 * flags field until we're actually ready to start
13766c8d8eccSSepherosa Ziehau 	 * using this ring (i.e. once we set the MTU
13776c8d8eccSSepherosa Ziehau 	 * high enough to require it).
13786c8d8eccSSepherosa Ziehau 	 */
13796c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
13806c8d8eccSSepherosa Ziehau 		rcb = &sc->bnx_ldata.bnx_info.bnx_jumbo_rx_rcb;
13816c8d8eccSSepherosa Ziehau 		/* Get the jumbo receive producer ring RCB parameters. */
13826c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_lo =
13836c8d8eccSSepherosa Ziehau 		    BGE_ADDR_LO(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
13846c8d8eccSSepherosa Ziehau 		rcb->bge_hostaddr.bge_addr_hi =
13856c8d8eccSSepherosa Ziehau 		    BGE_ADDR_HI(sc->bnx_ldata.bnx_rx_jumbo_ring_paddr);
13866c8d8eccSSepherosa Ziehau 		rcb->bge_maxlen_flags =
13876c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(BNX_MAX_FRAMELEN,
13886c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
1389303fdc72SSepherosa Ziehau 		if (BNX_IS_5717_PLUS(sc))
13906c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
13916c8d8eccSSepherosa Ziehau 		else
13926c8d8eccSSepherosa Ziehau 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
13936c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
13946c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_hi);
13956c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
13966c8d8eccSSepherosa Ziehau 		    rcb->bge_hostaddr.bge_addr_lo);
13976c8d8eccSSepherosa Ziehau 		/* Program the jumbo receive producer ring RCB parameters. */
13986c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13996c8d8eccSSepherosa Ziehau 		    rcb->bge_maxlen_flags);
14006c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
14016c8d8eccSSepherosa Ziehau 		/* Reset the jumbo receive producer ring producer index. */
14026c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
14036c8d8eccSSepherosa Ziehau 	}
14046c8d8eccSSepherosa Ziehau 
14056c8d8eccSSepherosa Ziehau 	/*
14066c8d8eccSSepherosa Ziehau 	 * The BD ring replenish thresholds control how often the
14076c8d8eccSSepherosa Ziehau 	 * hardware fetches new BD's from the producer rings in host
14086c8d8eccSSepherosa Ziehau 	 * memory.  Setting the value too low on a busy system can
14096c8d8eccSSepherosa Ziehau 	 * starve the hardware and recue the throughpout.
14106c8d8eccSSepherosa Ziehau 	 *
14116c8d8eccSSepherosa Ziehau 	 * Set the BD ring replentish thresholds. The recommended
14126c8d8eccSSepherosa Ziehau 	 * values are 1/8th the number of descriptors allocated to
14136c8d8eccSSepherosa Ziehau 	 * each ring.
14146c8d8eccSSepherosa Ziehau 	 */
14156c8d8eccSSepherosa Ziehau 	val = 8;
14166c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
14176c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
14186c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
14196c8d8eccSSepherosa Ziehau 		    BGE_JUMBO_RX_RING_CNT/8);
14206c8d8eccSSepherosa Ziehau 	}
1421f368d0d9SSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
14226c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
14236c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
14246c8d8eccSSepherosa Ziehau 	}
14256c8d8eccSSepherosa Ziehau 
14266c8d8eccSSepherosa Ziehau 	/*
14276c8d8eccSSepherosa Ziehau 	 * Disable all send rings by setting the 'ring disabled' bit
14286c8d8eccSSepherosa Ziehau 	 * in the flags field of all the TX send ring control blocks,
14296c8d8eccSSepherosa Ziehau 	 * located in NIC memory.
14306c8d8eccSSepherosa Ziehau 	 */
143180969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc))
143280969639SSepherosa Ziehau 		limit = 4;
1433b96cbbb6SSepherosa Ziehau 	else if (BNX_IS_57765_FAMILY(sc) ||
1434b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762)
14354f23029eSSepherosa Ziehau 		limit = 2;
143680969639SSepherosa Ziehau 	else
14376c8d8eccSSepherosa Ziehau 		limit = 1;
14386c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
14396c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
14406c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14416c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
14426c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
14436c8d8eccSSepherosa Ziehau 	}
14446c8d8eccSSepherosa Ziehau 
1445695a8586SSepherosa Ziehau 	/*
1446695a8586SSepherosa Ziehau 	 * Configure send ring RCBs
1447695a8586SSepherosa Ziehau 	 */
14486c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1449695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
1450695a8586SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
1451695a8586SSepherosa Ziehau 
145233a04907SSepherosa Ziehau 		BGE_HOSTADDR(taddr, txr->bnx_tx_ring_paddr);
1453695a8586SSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi,
1454695a8586SSepherosa Ziehau 		    taddr.bge_addr_hi);
1455695a8586SSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo,
1456695a8586SSepherosa Ziehau 		    taddr.bge_addr_lo);
14576c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14586c8d8eccSSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1459695a8586SSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
1460695a8586SSepherosa Ziehau 	}
14616c8d8eccSSepherosa Ziehau 
14626c8d8eccSSepherosa Ziehau 	/*
14636c8d8eccSSepherosa Ziehau 	 * Disable all receive return rings by setting the
14646c8d8eccSSepherosa Ziehau 	 * 'ring disabled' bit in the flags field of all the receive
14656c8d8eccSSepherosa Ziehau 	 * return ring control blocks, located in NIC memory.
14666c8d8eccSSepherosa Ziehau 	 */
146780969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
14686c8d8eccSSepherosa Ziehau 		/* Should be 17, use 16 until we get an SRAM map. */
14696c8d8eccSSepherosa Ziehau 		limit = 16;
1470b96cbbb6SSepherosa Ziehau 	} else if (BNX_IS_57765_FAMILY(sc) ||
1471b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
14726c8d8eccSSepherosa Ziehau 		limit = 4;
14736c8d8eccSSepherosa Ziehau 	} else {
14746c8d8eccSSepherosa Ziehau 		limit = 1;
14756c8d8eccSSepherosa Ziehau 	}
14766c8d8eccSSepherosa Ziehau 	/* Disable all receive return rings. */
14776c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
14786c8d8eccSSepherosa Ziehau 	for (i = 0; i < limit; i++) {
14796c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
14806c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
14816c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14826c8d8eccSSepherosa Ziehau 		    BGE_RCB_FLAG_RING_DISABLED);
14836c8d8eccSSepherosa Ziehau 		bnx_writembx(sc, BGE_MBX_RX_CONS0_LO +
14846c8d8eccSSepherosa Ziehau 		    (i * (sizeof(uint64_t))), 0);
14856c8d8eccSSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
14866c8d8eccSSepherosa Ziehau 	}
14876c8d8eccSSepherosa Ziehau 
14886c8d8eccSSepherosa Ziehau 	/*
14892a581495SSepherosa Ziehau 	 * Set up receive return rings.
14906c8d8eccSSepherosa Ziehau 	 */
14916c8d8eccSSepherosa Ziehau 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1492695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
1493695a8586SSepherosa Ziehau 		struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
1494695a8586SSepherosa Ziehau 
1495beedf5beSSepherosa Ziehau 		BGE_HOSTADDR(taddr, ret->bnx_rx_ret_ring_paddr);
1496695a8586SSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi,
1497695a8586SSepherosa Ziehau 		    taddr.bge_addr_hi);
1498695a8586SSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo,
1499695a8586SSepherosa Ziehau 		    taddr.bge_addr_lo);
15006c8d8eccSSepherosa Ziehau 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1501e0f74fc8SSepherosa Ziehau 		    BGE_RCB_MAXLEN_FLAGS(BNX_RETURN_RING_CNT, 0));
1502695a8586SSepherosa Ziehau 		vrcb += sizeof(struct bge_rcb);
1503695a8586SSepherosa Ziehau 	}
15046c8d8eccSSepherosa Ziehau 
15056c8d8eccSSepherosa Ziehau 	/* Set random backoff seed for TX */
15066c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
15074aa71e73SSepherosa Ziehau 	    (sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
15086c8d8eccSSepherosa Ziehau 	     sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
15094aa71e73SSepherosa Ziehau 	     sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5]) &
15106c8d8eccSSepherosa Ziehau 	    BGE_TX_BACKOFF_SEED_MASK);
15116c8d8eccSSepherosa Ziehau 
15126c8d8eccSSepherosa Ziehau 	/* Set inter-packet gap */
15136c8d8eccSSepherosa Ziehau 	val = 0x2620;
1514b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1515b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
15166c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
15176c8d8eccSSepherosa Ziehau 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
15186c8d8eccSSepherosa Ziehau 	}
15196c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
15206c8d8eccSSepherosa Ziehau 
15216c8d8eccSSepherosa Ziehau 	/*
15226c8d8eccSSepherosa Ziehau 	 * Specify which ring to use for packets that don't match
15236c8d8eccSSepherosa Ziehau 	 * any RX rules.
15246c8d8eccSSepherosa Ziehau 	 */
15256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
15266c8d8eccSSepherosa Ziehau 
15276c8d8eccSSepherosa Ziehau 	/*
15286c8d8eccSSepherosa Ziehau 	 * Configure number of RX lists. One interrupt distribution
15296c8d8eccSSepherosa Ziehau 	 * list, sixteen active lists, one bad frames class.
15306c8d8eccSSepherosa Ziehau 	 */
15316c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
15326c8d8eccSSepherosa Ziehau 
15336c8d8eccSSepherosa Ziehau 	/* Inialize RX list placement stats mask. */
15346c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
15356c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
15366c8d8eccSSepherosa Ziehau 
15376c8d8eccSSepherosa Ziehau 	/* Disable host coalescing until we get it set up */
15386c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
15396c8d8eccSSepherosa Ziehau 
15406c8d8eccSSepherosa Ziehau 	/* Poll to make sure it's shut down. */
15416c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
15426c8d8eccSSepherosa Ziehau 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
15436c8d8eccSSepherosa Ziehau 			break;
15446c8d8eccSSepherosa Ziehau 		DELAY(10);
15456c8d8eccSSepherosa Ziehau 	}
15466c8d8eccSSepherosa Ziehau 
15476c8d8eccSSepherosa Ziehau 	if (i == BNX_TIMEOUT) {
15486c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
15496c8d8eccSSepherosa Ziehau 			  "host coalescing engine failed to idle\n");
15506c8d8eccSSepherosa Ziehau 		return(ENXIO);
15516c8d8eccSSepherosa Ziehau 	}
15526c8d8eccSSepherosa Ziehau 
15536c8d8eccSSepherosa Ziehau 	/* Set up host coalescing defaults */
1554695a8586SSepherosa Ziehau 	sc->bnx_coal_chg = BNX_RX_COAL_TICKS_CHG |
1555695a8586SSepherosa Ziehau 	    BNX_TX_COAL_TICKS_CHG |
1556695a8586SSepherosa Ziehau 	    BNX_RX_COAL_BDS_CHG |
1557695a8586SSepherosa Ziehau 	    BNX_TX_COAL_BDS_CHG |
1558695a8586SSepherosa Ziehau 	    BNX_RX_COAL_BDS_INT_CHG |
1559695a8586SSepherosa Ziehau 	    BNX_TX_COAL_BDS_INT_CHG;
1560695a8586SSepherosa Ziehau 	bnx_coal_change(sc);
15616c8d8eccSSepherosa Ziehau 
1562695a8586SSepherosa Ziehau 	/*
1563695a8586SSepherosa Ziehau 	 * Set up addresses of status blocks
1564695a8586SSepherosa Ziehau 	 */
1565695a8586SSepherosa Ziehau 	intr = &sc->bnx_intr_data[0];
15660a806e3aSSepherosa Ziehau 	bzero(intr->bnx_status_block, BGE_STATUS_BLK_SZ);
15676c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
15680a806e3aSSepherosa Ziehau 	    BGE_ADDR_HI(intr->bnx_status_block_paddr));
15696c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
15700a806e3aSSepherosa Ziehau 	    BGE_ADDR_LO(intr->bnx_status_block_paddr));
1571695a8586SSepherosa Ziehau 	for (i = 1; i < sc->bnx_intr_cnt; ++i) {
1572695a8586SSepherosa Ziehau 		intr = &sc->bnx_intr_data[i];
1573695a8586SSepherosa Ziehau 		bzero(intr->bnx_status_block, BGE_STATUS_BLK_SZ);
1574695a8586SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_VEC1_STATUSBLK_ADDR_HI + ((i - 1) * 8),
1575695a8586SSepherosa Ziehau 		    BGE_ADDR_HI(intr->bnx_status_block_paddr));
1576695a8586SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_VEC1_STATUSBLK_ADDR_LO + ((i - 1) * 8),
1577695a8586SSepherosa Ziehau 		    BGE_ADDR_LO(intr->bnx_status_block_paddr));
1578695a8586SSepherosa Ziehau 	}
15796c8d8eccSSepherosa Ziehau 
15806c8d8eccSSepherosa Ziehau 	/* Set up status block partail update size. */
15816c8d8eccSSepherosa Ziehau 	val = BGE_STATBLKSZ_32BYTE;
15826c8d8eccSSepherosa Ziehau #if 0
15836c8d8eccSSepherosa Ziehau 	/*
15846c8d8eccSSepherosa Ziehau 	 * Does not seem to have visible effect in both
15856c8d8eccSSepherosa Ziehau 	 * bulk data (1472B UDP datagram) and tiny data
15866c8d8eccSSepherosa Ziehau 	 * (18B UDP datagram) TX tests.
15876c8d8eccSSepherosa Ziehau 	 */
15886c8d8eccSSepherosa Ziehau 	val |= BGE_HCCMODE_CLRTICK_TX;
15896c8d8eccSSepherosa Ziehau #endif
15906c8d8eccSSepherosa Ziehau 	/* Turn on host coalescing state machine */
15916c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
15926c8d8eccSSepherosa Ziehau 
15936c8d8eccSSepherosa Ziehau 	/* Turn on RX BD completion state machine and enable attentions */
15946c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
15956c8d8eccSSepherosa Ziehau 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
15966c8d8eccSSepherosa Ziehau 
15976c8d8eccSSepherosa Ziehau 	/* Turn on RX list placement state machine */
15986c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
15996c8d8eccSSepherosa Ziehau 
16006c8d8eccSSepherosa Ziehau 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
16016c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
16026c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
16036c8d8eccSSepherosa Ziehau 	    BGE_MACMODE_FRMHDR_DMA_ENB;
16046c8d8eccSSepherosa Ziehau 
16056c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
16066c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_TBI;
16076c8d8eccSSepherosa Ziehau 	else if (sc->bnx_flags & BNX_FLAG_MII_SERDES)
16086c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_GMII;
16096c8d8eccSSepherosa Ziehau 	else
16106c8d8eccSSepherosa Ziehau 		val |= BGE_PORTMODE_MII;
16116c8d8eccSSepherosa Ziehau 
16129f5082d5SSepherosa Ziehau 	/* Allow APE to send/receive frames. */
16139f5082d5SSepherosa Ziehau 	if (sc->bnx_mfw_flags & BNX_MFW_ON_APE)
16149f5082d5SSepherosa Ziehau 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
16159f5082d5SSepherosa Ziehau 
16166c8d8eccSSepherosa Ziehau 	/* Turn on DMA, clear stats */
16176c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
16184aa71e73SSepherosa Ziehau 	DELAY(40);
16196c8d8eccSSepherosa Ziehau 
16206c8d8eccSSepherosa Ziehau 	/* Set misc. local control, enable interrupts on attentions */
16214aa71e73SSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
16226c8d8eccSSepherosa Ziehau 
16236c8d8eccSSepherosa Ziehau #ifdef notdef
16246c8d8eccSSepherosa Ziehau 	/* Assert GPIO pins for PHY reset */
16256c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
16266c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
16276c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
16286c8d8eccSSepherosa Ziehau 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
16296c8d8eccSSepherosa Ziehau #endif
16306c8d8eccSSepherosa Ziehau 
1631695a8586SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSIX)
1632695a8586SSepherosa Ziehau 		bnx_enable_msi(sc, TRUE);
1633695a8586SSepherosa Ziehau 
16346c8d8eccSSepherosa Ziehau 	/* Turn on write DMA state machine */
16356c8d8eccSSepherosa Ziehau 	val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
16366c8d8eccSSepherosa Ziehau 	/* Enable host coalescing bug fix. */
16376c8d8eccSSepherosa Ziehau 	val |= BGE_WDMAMODE_STATUS_TAG_FIX;
16386c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5785) {
16396c8d8eccSSepherosa Ziehau 		/* Request larger DMA burst size to get better performance. */
16406c8d8eccSSepherosa Ziehau 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
16416c8d8eccSSepherosa Ziehau 	}
16426c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
16436c8d8eccSSepherosa Ziehau 	DELAY(40);
16446c8d8eccSSepherosa Ziehau 
16453730a14dSSepherosa Ziehau 	if (BNX_IS_57765_PLUS(sc)) {
1646b96cbbb6SSepherosa Ziehau 		uint32_t dmactl, dmactl_reg;
16476c8d8eccSSepherosa Ziehau 
1648b96cbbb6SSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1649b96cbbb6SSepherosa Ziehau 			dmactl_reg = BGE_RDMA_RSRVCTRL2;
1650b96cbbb6SSepherosa Ziehau 		else
1651b96cbbb6SSepherosa Ziehau 			dmactl_reg = BGE_RDMA_RSRVCTRL;
1652b96cbbb6SSepherosa Ziehau 
1653b96cbbb6SSepherosa Ziehau 		dmactl = CSR_READ_4(sc, dmactl_reg);
16546c8d8eccSSepherosa Ziehau 		/*
16556c8d8eccSSepherosa Ziehau 		 * Adjust tx margin to prevent TX data corruption and
16566c8d8eccSSepherosa Ziehau 		 * fix internal FIFO overflow.
16576c8d8eccSSepherosa Ziehau 		 */
16586c8d8eccSSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
1659b96cbbb6SSepherosa Ziehau 		    sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1660b96cbbb6SSepherosa Ziehau 		    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
16616c8d8eccSSepherosa Ziehau 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
16626c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
16636c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
16646c8d8eccSSepherosa Ziehau 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
16656c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
16666c8d8eccSSepherosa Ziehau 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
16676c8d8eccSSepherosa Ziehau 		}
16686c8d8eccSSepherosa Ziehau 		/*
16696c8d8eccSSepherosa Ziehau 		 * Enable fix for read DMA FIFO overruns.
16706c8d8eccSSepherosa Ziehau 		 * The fix is to limit the number of RX BDs
16716c8d8eccSSepherosa Ziehau 		 * the hardware would fetch at a fime.
16726c8d8eccSSepherosa Ziehau 		 */
1673b96cbbb6SSepherosa Ziehau 		CSR_WRITE_4(sc, dmactl_reg,
16746c8d8eccSSepherosa Ziehau 		    dmactl | BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
16756c8d8eccSSepherosa Ziehau 	}
16766c8d8eccSSepherosa Ziehau 
16776c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719) {
16786c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
16796c8d8eccSSepherosa Ziehau 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
16806c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
16816c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
1682b96cbbb6SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1683b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
1684b96cbbb6SSepherosa Ziehau 		uint32_t ctrl_reg;
1685b96cbbb6SSepherosa Ziehau 
1686b96cbbb6SSepherosa Ziehau 		if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
1687b96cbbb6SSepherosa Ziehau 			ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL2;
1688b96cbbb6SSepherosa Ziehau 		else
1689b96cbbb6SSepherosa Ziehau 			ctrl_reg = BGE_RDMA_LSO_CRPTEN_CTRL;
1690b96cbbb6SSepherosa Ziehau 
16916c8d8eccSSepherosa Ziehau 		/*
16926c8d8eccSSepherosa Ziehau 		 * Allow 4KB burst length reads for non-LSO frames.
16936c8d8eccSSepherosa Ziehau 		 * Enable 512B burst length reads for buffer descriptors.
16946c8d8eccSSepherosa Ziehau 		 */
1695b96cbbb6SSepherosa Ziehau 		CSR_WRITE_4(sc, ctrl_reg,
1696b96cbbb6SSepherosa Ziehau 		    CSR_READ_4(sc, ctrl_reg) |
16976c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
16986c8d8eccSSepherosa Ziehau 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
16996c8d8eccSSepherosa Ziehau 	}
17006c8d8eccSSepherosa Ziehau 
17016c8d8eccSSepherosa Ziehau 	/* Turn on read DMA state machine */
17026c8d8eccSSepherosa Ziehau 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
17036c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717)
17046c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
17056c8d8eccSSepherosa Ziehau         if (sc->bnx_asicrev == BGE_ASICREV_BCM5784 ||
17066c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM5785 ||
17076c8d8eccSSepherosa Ziehau             sc->bnx_asicrev == BGE_ASICREV_BCM57780) {
17086c8d8eccSSepherosa Ziehau 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
17096c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
17106c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
17116c8d8eccSSepherosa Ziehau 	}
1712b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
1713b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
17146c8d8eccSSepherosa Ziehau 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
17156c8d8eccSSepherosa Ziehau 		    BGE_RDMAMODE_H2BNC_VLAN_DET;
17166c8d8eccSSepherosa Ziehau 		/*
17176c8d8eccSSepherosa Ziehau 		 * Allow multiple outstanding read requests from
17186c8d8eccSSepherosa Ziehau 		 * non-LSO read DMA engine.
17196c8d8eccSSepherosa Ziehau 		 */
17206c8d8eccSSepherosa Ziehau 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
17216c8d8eccSSepherosa Ziehau 	}
172260e67e3fSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM57766)
172360e67e3fSSepherosa Ziehau 		val |= BGE_RDMAMODE_JMB_2K_MMRR;
172466deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO)
172566deb1c1SSepherosa Ziehau 		val |= BGE_RDMAMODE_TSO4_ENABLE;
17266c8d8eccSSepherosa Ziehau 	val |= BGE_RDMAMODE_FIFO_LONG_BURST;
17276c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
17286c8d8eccSSepherosa Ziehau 	DELAY(40);
17296c8d8eccSSepherosa Ziehau 
17302eaa7169SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
17312eaa7169SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
17322eaa7169SSepherosa Ziehau 	    	uint32_t thresh;
17332eaa7169SSepherosa Ziehau 
17342eaa7169SSepherosa Ziehau 		thresh = ETHERMTU_JUMBO;
17352eaa7169SSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
17362eaa7169SSepherosa Ziehau 			thresh = ETHERMTU;
17372eaa7169SSepherosa Ziehau 
17382eaa7169SSepherosa Ziehau 		for (i = 0; i < BGE_RDMA_NCHAN; ++i) {
17392eaa7169SSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_RDMA_LENGTH + (i << 2)) > thresh)
17402eaa7169SSepherosa Ziehau 				break;
17412eaa7169SSepherosa Ziehau 		}
17422eaa7169SSepherosa Ziehau 		if (i < BGE_RDMA_NCHAN) {
17432eaa7169SSepherosa Ziehau 			if (bootverbose) {
17442eaa7169SSepherosa Ziehau 				if_printf(&sc->arpcom.ac_if,
17452eaa7169SSepherosa Ziehau 				    "enable RDMA WA\n");
17462eaa7169SSepherosa Ziehau 			}
17472eaa7169SSepherosa Ziehau 			if (sc->bnx_asicrev == BGE_ASICREV_BCM5719)
17482eaa7169SSepherosa Ziehau 				sc->bnx_rdma_wa = BGE_RDMA_TX_LENGTH_WA_5719;
17492eaa7169SSepherosa Ziehau 			else
17502eaa7169SSepherosa Ziehau 				sc->bnx_rdma_wa = BGE_RDMA_TX_LENGTH_WA_5720;
17512eaa7169SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
17522eaa7169SSepherosa Ziehau 			    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
17532eaa7169SSepherosa Ziehau 			    sc->bnx_rdma_wa);
17542eaa7169SSepherosa Ziehau 		} else {
17552eaa7169SSepherosa Ziehau 			sc->bnx_rdma_wa = 0;
17562eaa7169SSepherosa Ziehau 		}
17572eaa7169SSepherosa Ziehau 	}
17582eaa7169SSepherosa Ziehau 
17596c8d8eccSSepherosa Ziehau 	/* Turn on RX data completion state machine */
17606c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
17616c8d8eccSSepherosa Ziehau 
17626c8d8eccSSepherosa Ziehau 	/* Turn on RX BD initiator state machine */
17636c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
17646c8d8eccSSepherosa Ziehau 
17656c8d8eccSSepherosa Ziehau 	/* Turn on RX data and RX BD initiator state machine */
17666c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
17676c8d8eccSSepherosa Ziehau 
17686c8d8eccSSepherosa Ziehau 	/* Turn on send BD completion state machine */
17696c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
17706c8d8eccSSepherosa Ziehau 
17716c8d8eccSSepherosa Ziehau 	/* Turn on send data completion state machine */
17726c8d8eccSSepherosa Ziehau 	val = BGE_SDCMODE_ENABLE;
17736c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5761)
17746c8d8eccSSepherosa Ziehau 		val |= BGE_SDCMODE_CDELAY;
17756c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
17766c8d8eccSSepherosa Ziehau 
17776c8d8eccSSepherosa Ziehau 	/* Turn on send data initiator state machine */
177866deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
177966deb1c1SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
178066deb1c1SSepherosa Ziehau 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
178166deb1c1SSepherosa Ziehau 	} else {
17826c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
178366deb1c1SSepherosa Ziehau 	}
17846c8d8eccSSepherosa Ziehau 
17856c8d8eccSSepherosa Ziehau 	/* Turn on send BD initiator state machine */
1786695a8586SSepherosa Ziehau 	val = BGE_SBDIMODE_ENABLE;
1787695a8586SSepherosa Ziehau 	if (sc->bnx_tx_ringcnt > 1)
1788695a8586SSepherosa Ziehau 		val |= BGE_SBDIMODE_MULTI_TXR;
1789695a8586SSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SBDI_MODE, val);
17906c8d8eccSSepherosa Ziehau 
17916c8d8eccSSepherosa Ziehau 	/* Turn on send BD selector state machine */
17926c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
17936c8d8eccSSepherosa Ziehau 
17946c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
17956c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
17966c8d8eccSSepherosa Ziehau 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
17976c8d8eccSSepherosa Ziehau 
17986c8d8eccSSepherosa Ziehau 	/* ack/clear link change events */
17996c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
18006c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
18016c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
18026c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
18036c8d8eccSSepherosa Ziehau 
18046c8d8eccSSepherosa Ziehau 	/*
18056c8d8eccSSepherosa Ziehau 	 * Enable attention when the link has changed state for
18066c8d8eccSSepherosa Ziehau 	 * devices that use auto polling.
18076c8d8eccSSepherosa Ziehau 	 */
18086c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
18096c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
18106c8d8eccSSepherosa Ziehau  	} else {
18116c8d8eccSSepherosa Ziehau 		if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
18126c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bnx_mi_mode);
18136c8d8eccSSepherosa Ziehau 			DELAY(80);
18146c8d8eccSSepherosa Ziehau 		}
18156c8d8eccSSepherosa Ziehau 	}
18166c8d8eccSSepherosa Ziehau 
18176c8d8eccSSepherosa Ziehau 	/*
18186c8d8eccSSepherosa Ziehau 	 * Clear any pending link state attention.
18196c8d8eccSSepherosa Ziehau 	 * Otherwise some link state change events may be lost until attention
18206c8d8eccSSepherosa Ziehau 	 * is cleared by bnx_intr() -> bnx_softc.bnx_link_upd() sequence.
18216c8d8eccSSepherosa Ziehau 	 * It's not necessary on newer BCM chips - perhaps enabling link
18226c8d8eccSSepherosa Ziehau 	 * state change attentions implies clearing pending attention.
18236c8d8eccSSepherosa Ziehau 	 */
18246c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
18256c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
18266c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
18276c8d8eccSSepherosa Ziehau 
18286c8d8eccSSepherosa Ziehau 	/* Enable link state change attentions. */
18296c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
18306c8d8eccSSepherosa Ziehau 
18316c8d8eccSSepherosa Ziehau 	return(0);
18326c8d8eccSSepherosa Ziehau }
18336c8d8eccSSepherosa Ziehau 
18346c8d8eccSSepherosa Ziehau /*
18356c8d8eccSSepherosa Ziehau  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
18366c8d8eccSSepherosa Ziehau  * against our list and return its name if we find a match. Note
18376c8d8eccSSepherosa Ziehau  * that since the Broadcom controller contains VPD support, we
18386c8d8eccSSepherosa Ziehau  * can get the device name string from the controller itself instead
18396c8d8eccSSepherosa Ziehau  * of the compiled-in string. This is a little slow, but it guarantees
18406c8d8eccSSepherosa Ziehau  * we'll always announce the right product name.
18416c8d8eccSSepherosa Ziehau  */
18426c8d8eccSSepherosa Ziehau static int
bnx_probe(device_t dev)18436c8d8eccSSepherosa Ziehau bnx_probe(device_t dev)
18446c8d8eccSSepherosa Ziehau {
18456c8d8eccSSepherosa Ziehau 	const struct bnx_type *t;
18466c8d8eccSSepherosa Ziehau 	uint16_t product, vendor;
18476c8d8eccSSepherosa Ziehau 
18486c8d8eccSSepherosa Ziehau 	if (!pci_is_pcie(dev))
18496c8d8eccSSepherosa Ziehau 		return ENXIO;
18506c8d8eccSSepherosa Ziehau 
18516c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
18526c8d8eccSSepherosa Ziehau 	vendor = pci_get_vendor(dev);
18536c8d8eccSSepherosa Ziehau 
18546c8d8eccSSepherosa Ziehau 	for (t = bnx_devs; t->bnx_name != NULL; t++) {
18556c8d8eccSSepherosa Ziehau 		if (vendor == t->bnx_vid && product == t->bnx_did)
18566c8d8eccSSepherosa Ziehau 			break;
18576c8d8eccSSepherosa Ziehau 	}
18586c8d8eccSSepherosa Ziehau 	if (t->bnx_name == NULL)
18596c8d8eccSSepherosa Ziehau 		return ENXIO;
18606c8d8eccSSepherosa Ziehau 
18616c8d8eccSSepherosa Ziehau 	device_set_desc(dev, t->bnx_name);
18626c8d8eccSSepherosa Ziehau 	return 0;
18636c8d8eccSSepherosa Ziehau }
18646c8d8eccSSepherosa Ziehau 
18656c8d8eccSSepherosa Ziehau static int
bnx_attach(device_t dev)18666c8d8eccSSepherosa Ziehau bnx_attach(device_t dev)
18676c8d8eccSSepherosa Ziehau {
18686c8d8eccSSepherosa Ziehau 	struct ifnet *ifp;
18696c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc;
1870841cdf08SSepherosa Ziehau 	struct bnx_rx_std_ring *std;
187126595b18SSascha Wildner 	struct sysctl_ctx_list *ctx;
187226595b18SSascha Wildner 	struct sysctl_oid_list *tree;
1873e594b5c4SSepherosa Ziehau 	uint32_t hwcfg = 0;
1874841cdf08SSepherosa Ziehau 	int error = 0, rid, capmask, i, std_cpuid, std_cpuid_def;
18756c8d8eccSSepherosa Ziehau 	uint8_t ether_addr[ETHER_ADDR_LEN];
187607e9f7c0SSascha Wildner 	uint16_t product;
18776c8d8eccSSepherosa Ziehau 	uintptr_t mii_priv = 0;
1878695a8586SSepherosa Ziehau #if defined(BNX_TSO_DEBUG) || defined(BNX_RSS_DEBUG) || defined(BNX_TSS_DEBUG)
187966deb1c1SSepherosa Ziehau 	char desc[32];
188066deb1c1SSepherosa Ziehau #endif
18816c8d8eccSSepherosa Ziehau 
18826c8d8eccSSepherosa Ziehau 	sc = device_get_softc(dev);
18836c8d8eccSSepherosa Ziehau 	sc->bnx_dev = dev;
18847dbaa833SSepherosa Ziehau 	callout_init_mp(&sc->bnx_tick_timer);
18856c8d8eccSSepherosa Ziehau 	lwkt_serialize_init(&sc->bnx_jslot_serializer);
1886f33ac8a4SSepherosa Ziehau 	lwkt_serialize_init(&sc->bnx_main_serialize);
18876c8d8eccSSepherosa Ziehau 
1888695a8586SSepherosa Ziehau 	/* Always setup interrupt mailboxes */
1889695a8586SSepherosa Ziehau 	for (i = 0; i < BNX_INTR_MAX; ++i) {
1890695a8586SSepherosa Ziehau 		callout_init_mp(&sc->bnx_intr_data[i].bnx_intr_timer);
1891695a8586SSepherosa Ziehau 		sc->bnx_intr_data[i].bnx_sc = sc;
1892695a8586SSepherosa Ziehau 		sc->bnx_intr_data[i].bnx_intr_mbx = BGE_MBX_IRQ0_LO + (i * 8);
1893695a8586SSepherosa Ziehau 		sc->bnx_intr_data[i].bnx_intr_rid = -1;
1894695a8586SSepherosa Ziehau 		sc->bnx_intr_data[i].bnx_intr_cpuid = -1;
1895695a8586SSepherosa Ziehau 	}
1896695a8586SSepherosa Ziehau 
18979f5082d5SSepherosa Ziehau 	sc->bnx_func_addr = pci_get_function(dev);
18986c8d8eccSSepherosa Ziehau 	product = pci_get_device(dev);
18996c8d8eccSSepherosa Ziehau 
19006c8d8eccSSepherosa Ziehau #ifndef BURN_BRIDGES
19016c8d8eccSSepherosa Ziehau 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
19026c8d8eccSSepherosa Ziehau 		uint32_t irq, mem;
19036c8d8eccSSepherosa Ziehau 
19046c8d8eccSSepherosa Ziehau 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
19056c8d8eccSSepherosa Ziehau 		mem = pci_read_config(dev, BGE_PCI_BAR0, 4);
19066c8d8eccSSepherosa Ziehau 
19076c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip is in D%d power mode "
19086c8d8eccSSepherosa Ziehau 		    "-- setting to D0\n", pci_get_powerstate(dev));
19096c8d8eccSSepherosa Ziehau 
19106c8d8eccSSepherosa Ziehau 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
19116c8d8eccSSepherosa Ziehau 
19126c8d8eccSSepherosa Ziehau 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
19136c8d8eccSSepherosa Ziehau 		pci_write_config(dev, BGE_PCI_BAR0, mem, 4);
19146c8d8eccSSepherosa Ziehau 	}
19156c8d8eccSSepherosa Ziehau #endif	/* !BURN_BRIDGE */
19166c8d8eccSSepherosa Ziehau 
19176c8d8eccSSepherosa Ziehau 	/*
19186c8d8eccSSepherosa Ziehau 	 * Map control/status registers.
19196c8d8eccSSepherosa Ziehau 	 */
19206c8d8eccSSepherosa Ziehau 	pci_enable_busmaster(dev);
19216c8d8eccSSepherosa Ziehau 
19226c8d8eccSSepherosa Ziehau 	rid = BGE_PCI_BAR0;
19236c8d8eccSSepherosa Ziehau 	sc->bnx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
19246c8d8eccSSepherosa Ziehau 	    RF_ACTIVE);
19256c8d8eccSSepherosa Ziehau 
19266c8d8eccSSepherosa Ziehau 	if (sc->bnx_res == NULL) {
19276c8d8eccSSepherosa Ziehau 		device_printf(dev, "couldn't map memory\n");
19286c8d8eccSSepherosa Ziehau 		return ENXIO;
19296c8d8eccSSepherosa Ziehau 	}
19306c8d8eccSSepherosa Ziehau 
19316c8d8eccSSepherosa Ziehau 	sc->bnx_btag = rman_get_bustag(sc->bnx_res);
19326c8d8eccSSepherosa Ziehau 	sc->bnx_bhandle = rman_get_bushandle(sc->bnx_res);
19336c8d8eccSSepherosa Ziehau 
19346c8d8eccSSepherosa Ziehau 	/* Save various chip information */
19356c8d8eccSSepherosa Ziehau 	sc->bnx_chipid =
19366c8d8eccSSepherosa Ziehau 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
19376c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
19386c8d8eccSSepherosa Ziehau 	if (BGE_ASICREV(sc->bnx_chipid) == BGE_ASICREV_USE_PRODID_REG) {
19396c8d8eccSSepherosa Ziehau 		/* All chips having dedicated ASICREV register have CPMU */
19406c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_CPMU;
19416c8d8eccSSepherosa Ziehau 
19426c8d8eccSSepherosa Ziehau 		switch (product) {
19436c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5717:
1944d79f5d8fSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5717C:
19456c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5718:
19466c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5719:
19476c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5720_ALT:
1948b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5725:
1949b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5727:
1950b96cbbb6SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM5762:
1951c1ed6db1SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57764:
1952c1ed6db1SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57767:
1953c1ed6db1SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57787:
19546c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
19556c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
19566c8d8eccSSepherosa Ziehau 			break;
19576c8d8eccSSepherosa Ziehau 
19586c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57761:
195932ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57762:
19606c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57765:
196132ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57766:
19626c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57781:
196332ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57782:
19646c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57785:
196532ff3c80SSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57786:
19666c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57791:
19676c8d8eccSSepherosa Ziehau 		case PCI_PRODUCT_BROADCOM_BCM57795:
19686c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
19696c8d8eccSSepherosa Ziehau 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
19706c8d8eccSSepherosa Ziehau 			break;
19716c8d8eccSSepherosa Ziehau 
19726c8d8eccSSepherosa Ziehau 		default:
19736c8d8eccSSepherosa Ziehau 			sc->bnx_chipid = pci_read_config(dev,
19746c8d8eccSSepherosa Ziehau 			    BGE_PCI_PRODID_ASICREV, 4);
19756c8d8eccSSepherosa Ziehau 			break;
19766c8d8eccSSepherosa Ziehau 		}
19776c8d8eccSSepherosa Ziehau 	}
1978d79f5d8fSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5717_C0)
1979d79f5d8fSSepherosa Ziehau 		sc->bnx_chipid = BGE_CHIPID_BCM5720_A0;
1980d79f5d8fSSepherosa Ziehau 
19816c8d8eccSSepherosa Ziehau 	sc->bnx_asicrev = BGE_ASICREV(sc->bnx_chipid);
19826c8d8eccSSepherosa Ziehau 	sc->bnx_chiprev = BGE_CHIPREV(sc->bnx_chipid);
19836c8d8eccSSepherosa Ziehau 
19846c8d8eccSSepherosa Ziehau 	switch (sc->bnx_asicrev) {
19856c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5717:
19866c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5719:
19876c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM5720:
1988f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_5717_PLUS | BNX_FLAG_57765_PLUS;
1989f368d0d9SSepherosa Ziehau 		break;
1990f368d0d9SSepherosa Ziehau 
1991b96cbbb6SSepherosa Ziehau 	case BGE_ASICREV_BCM5762:
1992b96cbbb6SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_57765_PLUS;
1993b96cbbb6SSepherosa Ziehau 		break;
1994b96cbbb6SSepherosa Ziehau 
19956c8d8eccSSepherosa Ziehau 	case BGE_ASICREV_BCM57765:
199632ff3c80SSepherosa Ziehau 	case BGE_ASICREV_BCM57766:
1997f368d0d9SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_57765_FAMILY | BNX_FLAG_57765_PLUS;
19986c8d8eccSSepherosa Ziehau 		break;
19996c8d8eccSSepherosa Ziehau 	}
20006c8d8eccSSepherosa Ziehau 
20014aa71e73SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
20024aa71e73SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
20034aa71e73SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
20044aa71e73SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762)
20054aa71e73SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_APE;
20064aa71e73SSepherosa Ziehau 
200766deb1c1SSepherosa Ziehau 	sc->bnx_flags |= BNX_FLAG_TSO;
200866deb1c1SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 &&
200966deb1c1SSepherosa Ziehau 	    sc->bnx_chipid == BGE_CHIPID_BCM5719_A0)
201066deb1c1SSepherosa Ziehau 		sc->bnx_flags &= ~BNX_FLAG_TSO;
201166deb1c1SSepherosa Ziehau 
2012df9ccc98SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717 ||
2013df9ccc98SSepherosa Ziehau 	    BNX_IS_57765_FAMILY(sc)) {
2014df9ccc98SSepherosa Ziehau 		/*
2015df9ccc98SSepherosa Ziehau 		 * All BCM57785 and BCM5718 families chips have a bug that
2016df9ccc98SSepherosa Ziehau 		 * under certain situation interrupt will not be enabled
201797ba8fc5SSepherosa Ziehau 		 * even if status tag is written to interrupt mailbox.
2018df9ccc98SSepherosa Ziehau 		 *
2019df9ccc98SSepherosa Ziehau 		 * While BCM5719 and BCM5720 have a hardware workaround
2020df9ccc98SSepherosa Ziehau 		 * which could fix the above bug.
2021df9ccc98SSepherosa Ziehau 		 * See the comment near BGE_PCIDMARWCTL_TAGGED_STATUS_WA in
2022df9ccc98SSepherosa Ziehau 		 * bnx_chipinit().
2023df9ccc98SSepherosa Ziehau 		 *
2024df9ccc98SSepherosa Ziehau 		 * For the rest of the chips in these two families, we will
2025df9ccc98SSepherosa Ziehau 		 * have to poll the status block at high rate (10ms currently)
2026df9ccc98SSepherosa Ziehau 		 * to check whether the interrupt is hosed or not.
2027695a8586SSepherosa Ziehau 		 * See bnx_check_intr_*() for details.
2028df9ccc98SSepherosa Ziehau 		 */
2029df9ccc98SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_STATUSTAG_BUG;
2030df9ccc98SSepherosa Ziehau 	}
2031df9ccc98SSepherosa Ziehau 
20326c8d8eccSSepherosa Ziehau 	sc->bnx_pciecap = pci_get_pciecap_ptr(sc->bnx_dev);
20336c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
20346c8d8eccSSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720)
20356c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_2048);
20366c8d8eccSSepherosa Ziehau 	else
20376c8d8eccSSepherosa Ziehau 		pcie_set_max_readrq(dev, PCIEM_DEVCTL_MAX_READRQ_4096);
20386c8d8eccSSepherosa Ziehau 	device_printf(dev, "CHIP ID 0x%08x; "
20396c8d8eccSSepherosa Ziehau 		      "ASIC REV 0x%02x; CHIP REV 0x%02x\n",
20406c8d8eccSSepherosa Ziehau 		      sc->bnx_chipid, sc->bnx_asicrev, sc->bnx_chiprev);
20416c8d8eccSSepherosa Ziehau 
20426c8d8eccSSepherosa Ziehau 	/*
20436c8d8eccSSepherosa Ziehau 	 * Set various PHY quirk flags.
20446c8d8eccSSepherosa Ziehau 	 */
20456c8d8eccSSepherosa Ziehau 
20466c8d8eccSSepherosa Ziehau 	capmask = MII_CAPMASK_DEFAULT;
204746283a40SSepherosa Ziehau 	if (product == PCI_PRODUCT_BROADCOM_BCM57791 ||
204846283a40SSepherosa Ziehau 	    product == PCI_PRODUCT_BROADCOM_BCM57795) {
20496c8d8eccSSepherosa Ziehau 		/* 10/100 only */
20506c8d8eccSSepherosa Ziehau 		capmask &= ~BMSR_EXTSTAT;
20516c8d8eccSSepherosa Ziehau 	}
20526c8d8eccSSepherosa Ziehau 
20536c8d8eccSSepherosa Ziehau 	mii_priv |= BRGPHY_FLAG_WIRESPEED;
2054b96cbbb6SSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5762_A0)
2055b96cbbb6SSepherosa Ziehau 		mii_priv |= BRGPHY_FLAG_5762_A0;
20566c8d8eccSSepherosa Ziehau 
20579f5082d5SSepherosa Ziehau 	/*
20589f5082d5SSepherosa Ziehau 	 * Chips with APE need BAR2 access for APE registers/memory.
20599f5082d5SSepherosa Ziehau 	 */
20609f5082d5SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_APE) {
20619f5082d5SSepherosa Ziehau 		uint32_t pcistate;
20629f5082d5SSepherosa Ziehau 
20639f5082d5SSepherosa Ziehau 		rid = PCIR_BAR(2);
20649f5082d5SSepherosa Ziehau 		sc->bnx_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20659f5082d5SSepherosa Ziehau 		    RF_ACTIVE);
20669f5082d5SSepherosa Ziehau 		if (sc->bnx_res2 == NULL) {
20679f5082d5SSepherosa Ziehau 			device_printf(dev, "couldn't map BAR2 memory\n");
20689f5082d5SSepherosa Ziehau 			error = ENXIO;
20699f5082d5SSepherosa Ziehau 			goto fail;
20709f5082d5SSepherosa Ziehau 		}
20719f5082d5SSepherosa Ziehau 
20729f5082d5SSepherosa Ziehau 		/* Enable APE register/memory access by host driver. */
20739f5082d5SSepherosa Ziehau 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
20749f5082d5SSepherosa Ziehau 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
20759f5082d5SSepherosa Ziehau 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
20769f5082d5SSepherosa Ziehau 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
20779f5082d5SSepherosa Ziehau 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
20789f5082d5SSepherosa Ziehau 
20799f5082d5SSepherosa Ziehau 		bnx_ape_lock_init(sc);
20809f5082d5SSepherosa Ziehau 		bnx_ape_read_fw_ver(sc);
20819f5082d5SSepherosa Ziehau 	}
20829f5082d5SSepherosa Ziehau 
20836c8d8eccSSepherosa Ziehau 	/* Initialize if_name earlier, so if_printf could be used */
20846c8d8eccSSepherosa Ziehau 	ifp = &sc->arpcom.ac_if;
20856c8d8eccSSepherosa Ziehau 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
20866c8d8eccSSepherosa Ziehau 
20874aa71e73SSepherosa Ziehau 	/*
20884aa71e73SSepherosa Ziehau 	 * Try to reset the chip.
20894aa71e73SSepherosa Ziehau 	 */
20904aa71e73SSepherosa Ziehau 	bnx_sig_pre_reset(sc, BNX_RESET_SHUTDOWN);
20916c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
20924aa71e73SSepherosa Ziehau 	bnx_sig_post_reset(sc, BNX_RESET_SHUTDOWN);
20936c8d8eccSSepherosa Ziehau 
20946c8d8eccSSepherosa Ziehau 	if (bnx_chipinit(sc)) {
20956c8d8eccSSepherosa Ziehau 		device_printf(dev, "chip initialization failed\n");
20966c8d8eccSSepherosa Ziehau 		error = ENXIO;
20976c8d8eccSSepherosa Ziehau 		goto fail;
20986c8d8eccSSepherosa Ziehau 	}
20996c8d8eccSSepherosa Ziehau 
21006c8d8eccSSepherosa Ziehau 	/*
21016c8d8eccSSepherosa Ziehau 	 * Get station address
21026c8d8eccSSepherosa Ziehau 	 */
21036c8d8eccSSepherosa Ziehau 	error = bnx_get_eaddr(sc, ether_addr);
21046c8d8eccSSepherosa Ziehau 	if (error) {
21056c8d8eccSSepherosa Ziehau 		device_printf(dev, "failed to read station address\n");
21066c8d8eccSSepherosa Ziehau 		goto fail;
21076c8d8eccSSepherosa Ziehau 	}
21086c8d8eccSSepherosa Ziehau 
2109695a8586SSepherosa Ziehau 	/* Setup RX/TX and interrupt count */
2110695a8586SSepherosa Ziehau 	bnx_setup_ring_cnt(sc);
211133a04907SSepherosa Ziehau 
21124fa38985SSepherosa Ziehau 	if ((sc->bnx_rx_retcnt == 1 && sc->bnx_tx_ringcnt == 1) ||
21134fa38985SSepherosa Ziehau 	    (sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt > 1)) {
21144fa38985SSepherosa Ziehau 	    	/*
21154fa38985SSepherosa Ziehau 		 * The RX ring and the corresponding TX ring processing
21164fa38985SSepherosa Ziehau 		 * should be on the same CPU, since they share the same
21174fa38985SSepherosa Ziehau 		 * status block.
21184fa38985SSepherosa Ziehau 		 */
21194fa38985SSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_RXTX_BUNDLE;
21204fa38985SSepherosa Ziehau 		if (bootverbose)
21214fa38985SSepherosa Ziehau 			device_printf(dev, "RX/TX bundle\n");
2122695a8586SSepherosa Ziehau 		if (sc->bnx_tx_ringcnt > 1) {
2123695a8586SSepherosa Ziehau 			/*
2124695a8586SSepherosa Ziehau 			 * Multiple TX rings do not share status block
2125695a8586SSepherosa Ziehau 			 * with link status, so link status will have
2126695a8586SSepherosa Ziehau 			 * to save its own status_tag.
2127695a8586SSepherosa Ziehau 			 */
2128695a8586SSepherosa Ziehau 			sc->bnx_flags |= BNX_FLAG_STATUS_HASTAG;
2129695a8586SSepherosa Ziehau 			if (bootverbose)
2130695a8586SSepherosa Ziehau 				device_printf(dev, "status needs tag\n");
2131695a8586SSepherosa Ziehau 		}
21324fa38985SSepherosa Ziehau 	} else {
21334fa38985SSepherosa Ziehau 		KKASSERT(sc->bnx_rx_retcnt > 1 && sc->bnx_tx_ringcnt == 1);
2134695a8586SSepherosa Ziehau 		if (bootverbose)
2135695a8586SSepherosa Ziehau 			device_printf(dev, "RX/TX not bundled\n");
21364fa38985SSepherosa Ziehau 	}
21374fa38985SSepherosa Ziehau 
2138beedf5beSSepherosa Ziehau 	error = bnx_dma_alloc(dev);
21396c8d8eccSSepherosa Ziehau 	if (error)
21406c8d8eccSSepherosa Ziehau 		goto fail;
21416c8d8eccSSepherosa Ziehau 
214216b32c4cSSepherosa Ziehau 	/*
214316b32c4cSSepherosa Ziehau 	 * Allocate interrupt
214416b32c4cSSepherosa Ziehau 	 */
21450c7da01dSSepherosa Ziehau 	error = bnx_alloc_intr(sc);
21460c7da01dSSepherosa Ziehau 	if (error)
214716b32c4cSSepherosa Ziehau 		goto fail;
214816b32c4cSSepherosa Ziehau 
2149329f9016SSepherosa Ziehau 	/* Setup serializers */
2150329f9016SSepherosa Ziehau 	bnx_setup_serialize(sc);
2151329f9016SSepherosa Ziehau 
21526c8d8eccSSepherosa Ziehau 	/* Set default tuneable values. */
21536c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_ticks = BNX_RX_COAL_TICKS_DEF;
21546c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_ticks = BNX_TX_COAL_TICKS_DEF;
21556c8d8eccSSepherosa Ziehau 	sc->bnx_rx_coal_bds = BNX_RX_COAL_BDS_DEF;
2156a86cc105SSepherosa Ziehau 	sc->bnx_rx_coal_bds_poll = sc->bnx_rx_ret_ring[0].bnx_rx_cntmax;
21576c8d8eccSSepherosa Ziehau 	sc->bnx_tx_coal_bds = BNX_TX_COAL_BDS_DEF;
215827357d84SSepherosa Ziehau 	sc->bnx_tx_coal_bds_poll = BNX_TX_COAL_BDS_POLL_DEF;
2159306e5498SSepherosa Ziehau 	sc->bnx_rx_coal_bds_int = BNX_RX_COAL_BDS_INT_DEF;
2160306e5498SSepherosa Ziehau 	sc->bnx_tx_coal_bds_int = BNX_TX_COAL_BDS_INT_DEF;
21616c8d8eccSSepherosa Ziehau 
21626c8d8eccSSepherosa Ziehau 	/* Set up ifnet structure */
21636c8d8eccSSepherosa Ziehau 	ifp->if_softc = sc;
21646c8d8eccSSepherosa Ziehau 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
21656c8d8eccSSepherosa Ziehau 	ifp->if_ioctl = bnx_ioctl;
21666c8d8eccSSepherosa Ziehau 	ifp->if_start = bnx_start;
216739a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
216839a8d43aSSepherosa Ziehau 	ifp->if_npoll = bnx_npoll;
21696c8d8eccSSepherosa Ziehau #endif
21706c8d8eccSSepherosa Ziehau 	ifp->if_init = bnx_init;
2171329f9016SSepherosa Ziehau 	ifp->if_serialize = bnx_serialize;
2172329f9016SSepherosa Ziehau 	ifp->if_deserialize = bnx_deserialize;
2173329f9016SSepherosa Ziehau 	ifp->if_tryserialize = bnx_tryserialize;
2174329f9016SSepherosa Ziehau #ifdef INVARIANTS
2175329f9016SSepherosa Ziehau 	ifp->if_serialize_assert = bnx_serialize_assert;
2176329f9016SSepherosa Ziehau #endif
21776c8d8eccSSepherosa Ziehau 	ifp->if_mtu = ETHERMTU;
21786c8d8eccSSepherosa Ziehau 	ifp->if_capabilities = IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
21796c8d8eccSSepherosa Ziehau 
21806c8d8eccSSepherosa Ziehau 	ifp->if_capabilities |= IFCAP_HWCSUM;
21816c8d8eccSSepherosa Ziehau 	ifp->if_hwassist = BNX_CSUM_FEATURES;
218266deb1c1SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TSO) {
218366deb1c1SSepherosa Ziehau 		ifp->if_capabilities |= IFCAP_TSO;
218466deb1c1SSepherosa Ziehau 		ifp->if_hwassist |= CSUM_TSO;
218566deb1c1SSepherosa Ziehau 	}
2186b19ddf7eSSepherosa Ziehau 	if (BNX_RSS_ENABLED(sc))
2187b19ddf7eSSepherosa Ziehau 		ifp->if_capabilities |= IFCAP_RSS;
21886c8d8eccSSepherosa Ziehau 	ifp->if_capenable = ifp->if_capabilities;
21896c8d8eccSSepherosa Ziehau 
219014929979SSepherosa Ziehau 	ifp->if_nmbclusters = BGE_STD_RX_RING_CNT;
219114929979SSepherosa Ziehau 
2192329f9016SSepherosa Ziehau 	ifq_set_maxlen(&ifp->if_snd, BGE_TX_RING_CNT - 1);
2193329f9016SSepherosa Ziehau 	ifq_set_ready(&ifp->if_snd);
2194329f9016SSepherosa Ziehau 	ifq_set_subq_cnt(&ifp->if_snd, sc->bnx_tx_ringcnt);
2195329f9016SSepherosa Ziehau 
2196695a8586SSepherosa Ziehau 	if (sc->bnx_tx_ringcnt > 1) {
21976af7a1dcSSepherosa Ziehau 		ifp->if_mapsubq = ifq_mapsubq_modulo;
21986af7a1dcSSepherosa Ziehau 		ifq_set_subq_divisor(&ifp->if_snd, sc->bnx_tx_ringcnt);
2199695a8586SSepherosa Ziehau 	}
2200695a8586SSepherosa Ziehau 
22016c8d8eccSSepherosa Ziehau 	/*
22026c8d8eccSSepherosa Ziehau 	 * Figure out what sort of media we have by checking the
22036c8d8eccSSepherosa Ziehau 	 * hardware config word in the first 32k of NIC internal memory,
22046c8d8eccSSepherosa Ziehau 	 * or fall back to examining the EEPROM if necessary.
22056c8d8eccSSepherosa Ziehau 	 * Note: on some BCM5700 cards, this value appears to be unset.
22066c8d8eccSSepherosa Ziehau 	 * If that's the case, we have to rely on identifying the NIC
22076c8d8eccSSepherosa Ziehau 	 * by its PCI subsystem ID, as we do below for the SysKonnect
22086c8d8eccSSepherosa Ziehau 	 * SK-9D41.
22096c8d8eccSSepherosa Ziehau 	 */
22100551ac06SSepherosa Ziehau 	if (bnx_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC) {
22110551ac06SSepherosa Ziehau 		hwcfg = bnx_readmem_ind(sc, BGE_SRAM_DATA_CFG);
22126c8d8eccSSepherosa Ziehau 	} else {
22136c8d8eccSSepherosa Ziehau 		if (bnx_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
22146c8d8eccSSepherosa Ziehau 				    sizeof(hwcfg))) {
22156c8d8eccSSepherosa Ziehau 			device_printf(dev, "failed to read EEPROM\n");
22166c8d8eccSSepherosa Ziehau 			error = ENXIO;
22176c8d8eccSSepherosa Ziehau 			goto fail;
22186c8d8eccSSepherosa Ziehau 		}
22196c8d8eccSSepherosa Ziehau 		hwcfg = ntohl(hwcfg);
22206c8d8eccSSepherosa Ziehau 	}
22216c8d8eccSSepherosa Ziehau 
22226c8d8eccSSepherosa Ziehau 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
22236c8d8eccSSepherosa Ziehau 	if (pci_get_subvendor(dev) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9D41 ||
22246c8d8eccSSepherosa Ziehau 	    (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
22256c8d8eccSSepherosa Ziehau 		sc->bnx_flags |= BNX_FLAG_TBI;
22266c8d8eccSSepherosa Ziehau 
22276c8d8eccSSepherosa Ziehau 	/* Setup MI MODE */
22286c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_CPMU)
22296c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_500KHZ_CONST;
22306c8d8eccSSepherosa Ziehau 	else
22316c8d8eccSSepherosa Ziehau 		sc->bnx_mi_mode = BGE_MIMODE_BASE;
22326c8d8eccSSepherosa Ziehau 
22336c8d8eccSSepherosa Ziehau 	/* Setup link status update stuffs */
22346c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
22356c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_tbi_link_upd;
22366c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
22376c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_mi_mode & BGE_MIMODE_AUTOPOLL) {
22386c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_autopoll_link_upd;
22396c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
22406c8d8eccSSepherosa Ziehau 	} else {
22416c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd = bnx_copper_link_upd;
22426c8d8eccSSepherosa Ziehau 		sc->bnx_link_chg = BGE_MACSTAT_LINK_CHANGED;
22436c8d8eccSSepherosa Ziehau 	}
22446c8d8eccSSepherosa Ziehau 
22456c8d8eccSSepherosa Ziehau 	/* Set default PHY address */
22466c8d8eccSSepherosa Ziehau 	sc->bnx_phyno = 1;
22476c8d8eccSSepherosa Ziehau 
22486c8d8eccSSepherosa Ziehau 	/*
22496c8d8eccSSepherosa Ziehau 	 * PHY address mapping for various devices.
22506c8d8eccSSepherosa Ziehau 	 *
22516c8d8eccSSepherosa Ziehau 	 *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
22526c8d8eccSSepherosa Ziehau 	 * ---------+-------+-------+-------+-------+
22536c8d8eccSSepherosa Ziehau 	 * BCM57XX  |   1   |   X   |   X   |   X   |
22546c8d8eccSSepherosa Ziehau 	 * BCM5717  |   1   |   8   |   2   |   9   |
22556c8d8eccSSepherosa Ziehau 	 * BCM5719  |   1   |   8   |   2   |   9   |
22566c8d8eccSSepherosa Ziehau 	 * BCM5720  |   1   |   8   |   2   |   9   |
22576c8d8eccSSepherosa Ziehau 	 *
22589f5082d5SSepherosa Ziehau 	 *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
22599f5082d5SSepherosa Ziehau 	 * ---------+-------+-------+-------+-------+
22609f5082d5SSepherosa Ziehau 	 * BCM57XX  |   X   |   X   |   X   |   X   |
22619f5082d5SSepherosa Ziehau 	 * BCM5717  |   X   |   X   |   X   |   X   |
22629f5082d5SSepherosa Ziehau 	 * BCM5719  |   3   |   10  |   4   |   11  |
22639f5082d5SSepherosa Ziehau 	 * BCM5720  |   X   |   X   |   X   |   X   |
22649f5082d5SSepherosa Ziehau 	 *
22656c8d8eccSSepherosa Ziehau 	 * Other addresses may respond but they are not
22666c8d8eccSSepherosa Ziehau 	 * IEEE compliant PHYs and should be ignored.
22676c8d8eccSSepherosa Ziehau 	 */
226880969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
22696c8d8eccSSepherosa Ziehau 		if (sc->bnx_chipid == BGE_CHIPID_BCM5717_A0) {
22706c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
22716c8d8eccSSepherosa Ziehau 			    BGE_SGDIGSTS_IS_SERDES)
22729f5082d5SSepherosa Ziehau 				sc->bnx_phyno = sc->bnx_func_addr + 8;
22736c8d8eccSSepherosa Ziehau 			else
22749f5082d5SSepherosa Ziehau 				sc->bnx_phyno = sc->bnx_func_addr + 1;
22756c8d8eccSSepherosa Ziehau 		} else {
22766c8d8eccSSepherosa Ziehau 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
22776c8d8eccSSepherosa Ziehau 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
22789f5082d5SSepherosa Ziehau 				sc->bnx_phyno = sc->bnx_func_addr + 8;
22796c8d8eccSSepherosa Ziehau 			else
22809f5082d5SSepherosa Ziehau 				sc->bnx_phyno = sc->bnx_func_addr + 1;
22816c8d8eccSSepherosa Ziehau 		}
22826c8d8eccSSepherosa Ziehau 	}
22836c8d8eccSSepherosa Ziehau 
22846c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
22856c8d8eccSSepherosa Ziehau 		ifmedia_init(&sc->bnx_ifmedia, IFM_IMASK,
22866c8d8eccSSepherosa Ziehau 		    bnx_ifmedia_upd, bnx_ifmedia_sts);
22876c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
22886c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia,
22896c8d8eccSSepherosa Ziehau 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
22906c8d8eccSSepherosa Ziehau 		ifmedia_add(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
22916c8d8eccSSepherosa Ziehau 		ifmedia_set(&sc->bnx_ifmedia, IFM_ETHER|IFM_AUTO);
22926c8d8eccSSepherosa Ziehau 		sc->bnx_ifmedia.ifm_media = sc->bnx_ifmedia.ifm_cur->ifm_media;
22936c8d8eccSSepherosa Ziehau 	} else {
22946c8d8eccSSepherosa Ziehau 		struct mii_probe_args mii_args;
22956c8d8eccSSepherosa Ziehau 
22966c8d8eccSSepherosa Ziehau 		mii_probe_args_init(&mii_args, bnx_ifmedia_upd, bnx_ifmedia_sts);
22976c8d8eccSSepherosa Ziehau 		mii_args.mii_probemask = 1 << sc->bnx_phyno;
22986c8d8eccSSepherosa Ziehau 		mii_args.mii_capmask = capmask;
22996c8d8eccSSepherosa Ziehau 		mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
23006c8d8eccSSepherosa Ziehau 		mii_args.mii_priv = mii_priv;
23016c8d8eccSSepherosa Ziehau 
23026c8d8eccSSepherosa Ziehau 		error = mii_probe(dev, &sc->bnx_miibus, &mii_args);
23036c8d8eccSSepherosa Ziehau 		if (error) {
23046c8d8eccSSepherosa Ziehau 			device_printf(dev, "MII without any PHY!\n");
23056c8d8eccSSepherosa Ziehau 			goto fail;
23066c8d8eccSSepherosa Ziehau 		}
23076c8d8eccSSepherosa Ziehau 	}
23086c8d8eccSSepherosa Ziehau 
230926595b18SSascha Wildner 	ctx = device_get_sysctl_ctx(sc->bnx_dev);
231026595b18SSascha Wildner 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bnx_dev));
23116c8d8eccSSepherosa Ziehau 
231226595b18SSascha Wildner 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
231393146551SSepherosa Ziehau 	    "rx_rings", CTLFLAG_RD, &sc->bnx_rx_retcnt, 0, "# of RX rings");
231426595b18SSascha Wildner 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
231593146551SSepherosa Ziehau 	    "tx_rings", CTLFLAG_RD, &sc->bnx_tx_ringcnt, 0, "# of TX rings");
231693146551SSepherosa Ziehau 
231726595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rx_coal_ticks",
23186c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
23196c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_ticks, "I",
23206c8d8eccSSepherosa Ziehau 			"Receive coalescing ticks (usec).");
232126595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tx_coal_ticks",
23226c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
23236c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_ticks, "I",
23246c8d8eccSSepherosa Ziehau 			"Transmit coalescing ticks (usec).");
232526595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rx_coal_bds",
23266c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
23276c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_bds, "I",
23286c8d8eccSSepherosa Ziehau 			"Receive max coalesced BD count.");
232926595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rx_coal_bds_poll",
2330a86cc105SSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
2331a86cc105SSepherosa Ziehau 			sc, 0, bnx_sysctl_rx_coal_bds_poll, "I",
2332a86cc105SSepherosa Ziehau 			"Receive max coalesced BD count in polling.");
233326595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tx_coal_bds",
23346c8d8eccSSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
23356c8d8eccSSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_bds, "I",
23366c8d8eccSSepherosa Ziehau 			"Transmit max coalesced BD count.");
233726595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tx_coal_bds_poll",
233827357d84SSepherosa Ziehau 			CTLTYPE_INT | CTLFLAG_RW,
233927357d84SSepherosa Ziehau 			sc, 0, bnx_sysctl_tx_coal_bds_poll, "I",
234027357d84SSepherosa Ziehau 			"Transmit max coalesced BD count in polling.");
23416c8d8eccSSepherosa Ziehau 	/*
23426c8d8eccSSepherosa Ziehau 	 * A common design characteristic for many Broadcom
23436c8d8eccSSepherosa Ziehau 	 * client controllers is that they only support a
23446c8d8eccSSepherosa Ziehau 	 * single outstanding DMA read operation on the PCIe
23456c8d8eccSSepherosa Ziehau 	 * bus. This means that it will take twice as long to
23466c8d8eccSSepherosa Ziehau 	 * fetch a TX frame that is split into header and
23476c8d8eccSSepherosa Ziehau 	 * payload buffers as it does to fetch a single,
23486c8d8eccSSepherosa Ziehau 	 * contiguous TX frame (2 reads vs. 1 read). For these
23496c8d8eccSSepherosa Ziehau 	 * controllers, coalescing buffers to reduce the number
23506c8d8eccSSepherosa Ziehau 	 * of memory reads is effective way to get maximum
23516c8d8eccSSepherosa Ziehau 	 * performance(about 940Mbps).  Without collapsing TX
23526c8d8eccSSepherosa Ziehau 	 * buffers the maximum TCP bulk transfer performance
23536c8d8eccSSepherosa Ziehau 	 * is about 850Mbps. However forcing coalescing mbufs
23546c8d8eccSSepherosa Ziehau 	 * consumes a lot of CPU cycles, so leave it off by
23556c8d8eccSSepherosa Ziehau 	 * default.
23566c8d8eccSSepherosa Ziehau 	 */
235726595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
2358aad4de2bSSepherosa Ziehau 	    "force_defrag", CTLTYPE_INT | CTLFLAG_RW,
2359aad4de2bSSepherosa Ziehau 	    sc, 0, bnx_sysctl_force_defrag, "I",
23606c8d8eccSSepherosa Ziehau 	    "Force defragment on TX path");
23616c8d8eccSSepherosa Ziehau 
236226595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
2363472c99c8SSepherosa Ziehau 	    "tx_wreg", CTLTYPE_INT | CTLFLAG_RW,
2364472c99c8SSepherosa Ziehau 	    sc, 0, bnx_sysctl_tx_wreg, "I",
2365c9b7f592SSepherosa Ziehau 	    "# of segments before writing to hardware register");
2366c9b7f592SSepherosa Ziehau 
236726595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
2368841cdf08SSepherosa Ziehau 	    "std_refill", CTLTYPE_INT | CTLFLAG_RW,
2369841cdf08SSepherosa Ziehau 	    sc, 0, bnx_sysctl_std_refill, "I",
2370841cdf08SSepherosa Ziehau 	    "# of packets received before scheduling standard refilling");
2371841cdf08SSepherosa Ziehau 
237226595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
23736c8d8eccSSepherosa Ziehau 	    "rx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
23746c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_rx_coal_bds_int, "I",
23756c8d8eccSSepherosa Ziehau 	    "Receive max coalesced BD count during interrupt.");
237626595b18SSascha Wildner 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
23776c8d8eccSSepherosa Ziehau 	    "tx_coal_bds_int", CTLTYPE_INT | CTLFLAG_RW,
23786c8d8eccSSepherosa Ziehau 	    sc, 0, bnx_sysctl_tx_coal_bds_int, "I",
23796c8d8eccSSepherosa Ziehau 	    "Transmit max coalesced BD count during interrupt.");
23806c8d8eccSSepherosa Ziehau 
238102596bedSSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSIX) {
238202596bedSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tx_cpumap",
238302596bedSSepherosa Ziehau 		    CTLTYPE_OPAQUE | CTLFLAG_RD,
238402596bedSSepherosa Ziehau 		    sc->bnx_tx_rmap, 0, if_ringmap_cpumap_sysctl, "I",
238502596bedSSepherosa Ziehau 		    "TX ring CPU map");
238602596bedSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rx_cpumap",
238702596bedSSepherosa Ziehau 		    CTLTYPE_OPAQUE | CTLFLAG_RD,
238802596bedSSepherosa Ziehau 		    sc->bnx_rx_rmap, 0, if_ringmap_cpumap_sysctl, "I",
238902596bedSSepherosa Ziehau 		    "RX ring CPU map");
23904fa38985SSepherosa Ziehau 	} else {
239102596bedSSepherosa Ziehau #ifdef IFPOLL_ENABLE
239202596bedSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tx_poll_cpumap",
239302596bedSSepherosa Ziehau 		    CTLTYPE_OPAQUE | CTLFLAG_RD,
239402596bedSSepherosa Ziehau 		    sc->bnx_tx_rmap, 0, if_ringmap_cpumap_sysctl, "I",
239502596bedSSepherosa Ziehau 		    "TX poll CPU map");
239602596bedSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rx_poll_cpumap",
239702596bedSSepherosa Ziehau 		    CTLTYPE_OPAQUE | CTLFLAG_RD,
239802596bedSSepherosa Ziehau 		    sc->bnx_rx_rmap, 0, if_ringmap_cpumap_sysctl, "I",
239902596bedSSepherosa Ziehau 		    "RX poll CPU map");
24004fa38985SSepherosa Ziehau #endif
240102596bedSSepherosa Ziehau 	}
24024fa38985SSepherosa Ziehau 
2403695a8586SSepherosa Ziehau #ifdef BNX_RSS_DEBUG
240426595b18SSascha Wildner 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
2405695a8586SSepherosa Ziehau 	    "std_refill_mask", CTLFLAG_RD,
2406695a8586SSepherosa Ziehau 	    &sc->bnx_rx_std_ring.bnx_rx_std_refill, 0, "");
240726595b18SSascha Wildner 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
2408625c3ba3SSepherosa Ziehau 	    "std_used", CTLFLAG_RD,
2409625c3ba3SSepherosa Ziehau 	    &sc->bnx_rx_std_ring.bnx_rx_std_used, 0, "");
241026595b18SSascha Wildner 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
2411695a8586SSepherosa Ziehau 	    "rss_debug", CTLFLAG_RW, &sc->bnx_rss_debug, 0, "");
2412695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
2413695a8586SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "rx_pkt%d", i);
241426595b18SSascha Wildner 		SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
2415695a8586SSepherosa Ziehau 		    desc, CTLFLAG_RW, &sc->bnx_rx_ret_ring[i].bnx_rx_pkt, "");
2416625c3ba3SSepherosa Ziehau 
2417625c3ba3SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "rx_force_sched%d", i);
241826595b18SSascha Wildner 		SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
2419625c3ba3SSepherosa Ziehau 		    desc, CTLFLAG_RW,
2420625c3ba3SSepherosa Ziehau 		    &sc->bnx_rx_ret_ring[i].bnx_rx_force_sched, "");
2421695a8586SSepherosa Ziehau 	}
2422695a8586SSepherosa Ziehau #endif
2423695a8586SSepherosa Ziehau #ifdef BNX_TSS_DEBUG
2424695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
2425695a8586SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "tx_pkt%d", i);
242626595b18SSascha Wildner 		SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
2427695a8586SSepherosa Ziehau 		    desc, CTLFLAG_RW, &sc->bnx_tx_ring[i].bnx_tx_pkt, "");
2428695a8586SSepherosa Ziehau 	}
2429695a8586SSepherosa Ziehau #endif
2430695a8586SSepherosa Ziehau 
243126595b18SSascha Wildner 	SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
2432695a8586SSepherosa Ziehau 	    "norxbds", CTLFLAG_RW, &sc->bnx_norxbds, "");
2433695a8586SSepherosa Ziehau 
243426595b18SSascha Wildner 	SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
2435695a8586SSepherosa Ziehau 	    "errors", CTLFLAG_RW, &sc->bnx_errors, "");
2436695a8586SSepherosa Ziehau 
243766deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
243866deb1c1SSepherosa Ziehau 	for (i = 0; i < BNX_TSO_NSTATS; ++i) {
243966deb1c1SSepherosa Ziehau 		ksnprintf(desc, sizeof(desc), "tso%d", i + 1);
244026595b18SSascha Wildner 		SYSCTL_ADD_ULONG(ctx, tree, OID_AUTO,
244166deb1c1SSepherosa Ziehau 		    desc, CTLFLAG_RW, &sc->bnx_tsosegs[i], "");
244266deb1c1SSepherosa Ziehau 	}
244366deb1c1SSepherosa Ziehau #endif
244466deb1c1SSepherosa Ziehau 
24456c8d8eccSSepherosa Ziehau 	/*
24466c8d8eccSSepherosa Ziehau 	 * Call MI attach routine.
24476c8d8eccSSepherosa Ziehau 	 */
2448329f9016SSepherosa Ziehau 	ether_ifattach(ifp, ether_addr, NULL);
24496c8d8eccSSepherosa Ziehau 
2450329f9016SSepherosa Ziehau 	/* Setup TX rings and subqueues */
2451329f9016SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
2452329f9016SSepherosa Ziehau 		struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
2453329f9016SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
2454329f9016SSepherosa Ziehau 
2455329f9016SSepherosa Ziehau 		ifsq_set_cpuid(ifsq, txr->bnx_tx_cpuid);
2456329f9016SSepherosa Ziehau 		ifsq_set_hw_serialize(ifsq, &txr->bnx_tx_serialize);
2457329f9016SSepherosa Ziehau 		ifsq_set_priv(ifsq, txr);
24583397dea6SSepherosa Ziehau 		txr->bnx_ifsq = ifsq;
2459329f9016SSepherosa Ziehau 
2460e2292763SMatthew Dillon 		ifsq_watchdog_init(&txr->bnx_tx_watchdog, ifsq,
2461e2292763SMatthew Dillon 				   bnx_watchdog, 0);
2462695a8586SSepherosa Ziehau 
2463695a8586SSepherosa Ziehau 		if (bootverbose) {
2464695a8586SSepherosa Ziehau 			device_printf(dev, "txr %d -> cpu%d\n", i,
2465695a8586SSepherosa Ziehau 			    txr->bnx_tx_cpuid);
2466695a8586SSepherosa Ziehau 		}
2467329f9016SSepherosa Ziehau 	}
24684c77af2dSSepherosa Ziehau 
24690c7da01dSSepherosa Ziehau 	error = bnx_setup_intr(sc);
24706c8d8eccSSepherosa Ziehau 	if (error) {
24716c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
24726c8d8eccSSepherosa Ziehau 		goto fail;
24736c8d8eccSSepherosa Ziehau 	}
24747dbaa833SSepherosa Ziehau 	bnx_set_tick_cpuid(sc, FALSE);
24758ca0f604SSepherosa Ziehau 
2476841cdf08SSepherosa Ziehau 	/*
2477841cdf08SSepherosa Ziehau 	 * Create RX standard ring refilling thread
2478841cdf08SSepherosa Ziehau 	 */
247902596bedSSepherosa Ziehau 	std_cpuid_def = if_ringmap_cpumap(sc->bnx_rx_rmap, 0);
2480841cdf08SSepherosa Ziehau 	std_cpuid = device_getenv_int(dev, "std.cpuid", std_cpuid_def);
2481841cdf08SSepherosa Ziehau 	if (std_cpuid < 0 || std_cpuid >= ncpus) {
2482841cdf08SSepherosa Ziehau 		device_printf(dev, "invalid std.cpuid %d, use %d\n",
2483841cdf08SSepherosa Ziehau 		    std_cpuid, std_cpuid_def);
2484841cdf08SSepherosa Ziehau 		std_cpuid = std_cpuid_def;
2485841cdf08SSepherosa Ziehau 	}
2486841cdf08SSepherosa Ziehau 
2487841cdf08SSepherosa Ziehau 	std = &sc->bnx_rx_std_ring;
2488c450d4d8SSepherosa Ziehau 	lwkt_create(bnx_rx_std_refill_ithread, std, &std->bnx_rx_std_ithread,
2489c450d4d8SSepherosa Ziehau 	    NULL, TDF_NOSTART | TDF_INTTHREAD, std_cpuid,
2490841cdf08SSepherosa Ziehau 	    "%s std", device_get_nameunit(dev));
2491c450d4d8SSepherosa Ziehau 	lwkt_setpri(std->bnx_rx_std_ithread, TDPRI_INT_MED);
2492c450d4d8SSepherosa Ziehau 	std->bnx_rx_std_ithread->td_preemptable = lwkt_preempt;
2493841cdf08SSepherosa Ziehau 
24946c8d8eccSSepherosa Ziehau 	return(0);
24956c8d8eccSSepherosa Ziehau fail:
24966c8d8eccSSepherosa Ziehau 	bnx_detach(dev);
24976c8d8eccSSepherosa Ziehau 	return(error);
24986c8d8eccSSepherosa Ziehau }
24996c8d8eccSSepherosa Ziehau 
25006c8d8eccSSepherosa Ziehau static int
bnx_detach(device_t dev)25016c8d8eccSSepherosa Ziehau bnx_detach(device_t dev)
25026c8d8eccSSepherosa Ziehau {
25036c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
2504c450d4d8SSepherosa Ziehau 	struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
25056c8d8eccSSepherosa Ziehau 
25066c8d8eccSSepherosa Ziehau 	if (device_is_attached(dev)) {
25076c8d8eccSSepherosa Ziehau 		struct ifnet *ifp = &sc->arpcom.ac_if;
25086c8d8eccSSepherosa Ziehau 
2509329f9016SSepherosa Ziehau 		ifnet_serialize_all(ifp);
25106c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
2511f33ac8a4SSepherosa Ziehau 		bnx_teardown_intr(sc, sc->bnx_intr_cnt);
2512329f9016SSepherosa Ziehau 		ifnet_deserialize_all(ifp);
25136c8d8eccSSepherosa Ziehau 
25146c8d8eccSSepherosa Ziehau 		ether_ifdetach(ifp);
25156c8d8eccSSepherosa Ziehau 	}
25166c8d8eccSSepherosa Ziehau 
2517c450d4d8SSepherosa Ziehau 	if (std->bnx_rx_std_ithread != NULL) {
2518841cdf08SSepherosa Ziehau 		tsleep_interlock(std, 0);
2519695a8586SSepherosa Ziehau 
2520c450d4d8SSepherosa Ziehau 		if (std->bnx_rx_std_ithread->td_gd == mycpu) {
2521695a8586SSepherosa Ziehau 			bnx_rx_std_refill_stop(std);
2522695a8586SSepherosa Ziehau 		} else {
2523c450d4d8SSepherosa Ziehau 			lwkt_send_ipiq(std->bnx_rx_std_ithread->td_gd,
2524695a8586SSepherosa Ziehau 			    bnx_rx_std_refill_stop, std);
2525695a8586SSepherosa Ziehau 		}
2526695a8586SSepherosa Ziehau 
2527841cdf08SSepherosa Ziehau 		tsleep(std, PINTERLOCKED, "bnx_detach", 0);
2528841cdf08SSepherosa Ziehau 		if (bootverbose)
2529841cdf08SSepherosa Ziehau 			device_printf(dev, "RX std ithread exited\n");
2530695a8586SSepherosa Ziehau 
2531695a8586SSepherosa Ziehau 		lwkt_synchronize_ipiqs("bnx_detach_ipiq");
2532841cdf08SSepherosa Ziehau 	}
2533841cdf08SSepherosa Ziehau 
25346c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI)
25356c8d8eccSSepherosa Ziehau 		ifmedia_removeall(&sc->bnx_ifmedia);
25366c8d8eccSSepherosa Ziehau 	if (sc->bnx_miibus)
25376c8d8eccSSepherosa Ziehau 		device_delete_child(dev, sc->bnx_miibus);
25386c8d8eccSSepherosa Ziehau 	bus_generic_detach(dev);
25396c8d8eccSSepherosa Ziehau 
25400c7da01dSSepherosa Ziehau 	bnx_free_intr(sc);
25416c8d8eccSSepherosa Ziehau 
2542695a8586SSepherosa Ziehau 	if (sc->bnx_msix_mem_res != NULL) {
2543695a8586SSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_MEMORY, sc->bnx_msix_mem_rid,
2544695a8586SSepherosa Ziehau 		    sc->bnx_msix_mem_res);
2545695a8586SSepherosa Ziehau 	}
25466c8d8eccSSepherosa Ziehau 	if (sc->bnx_res != NULL) {
25476c8d8eccSSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_MEMORY,
25486c8d8eccSSepherosa Ziehau 		    BGE_PCI_BAR0, sc->bnx_res);
25496c8d8eccSSepherosa Ziehau 	}
25509f5082d5SSepherosa Ziehau 	if (sc->bnx_res2 != NULL) {
25519f5082d5SSepherosa Ziehau 		bus_release_resource(dev, SYS_RES_MEMORY,
25529f5082d5SSepherosa Ziehau 		    PCIR_BAR(2), sc->bnx_res2);
25539f5082d5SSepherosa Ziehau 	}
25546c8d8eccSSepherosa Ziehau 
25556c8d8eccSSepherosa Ziehau 	bnx_dma_free(sc);
25566c8d8eccSSepherosa Ziehau 
2557329f9016SSepherosa Ziehau 	if (sc->bnx_serialize != NULL)
2558329f9016SSepherosa Ziehau 		kfree(sc->bnx_serialize, M_DEVBUF);
2559329f9016SSepherosa Ziehau 
25602929cd44SSepherosa Ziehau 	if (sc->bnx_rx_rmap != NULL)
25612929cd44SSepherosa Ziehau 		if_ringmap_free(sc->bnx_rx_rmap);
25622929cd44SSepherosa Ziehau 	if (sc->bnx_tx_rmap != NULL)
25632929cd44SSepherosa Ziehau 		if_ringmap_free(sc->bnx_tx_rmap);
25642929cd44SSepherosa Ziehau 
25656c8d8eccSSepherosa Ziehau 	return 0;
25666c8d8eccSSepherosa Ziehau }
25676c8d8eccSSepherosa Ziehau 
25686c8d8eccSSepherosa Ziehau static void
bnx_reset(struct bnx_softc * sc)25696c8d8eccSSepherosa Ziehau bnx_reset(struct bnx_softc *sc)
25706c8d8eccSSepherosa Ziehau {
25714aa71e73SSepherosa Ziehau 	device_t dev = sc->bnx_dev;
25724aa71e73SSepherosa Ziehau 	uint32_t cachesize, command, reset, mac_mode, mac_mode_mask;
25736c8d8eccSSepherosa Ziehau 	void (*write_op)(struct bnx_softc *, uint32_t, uint32_t);
25746c8d8eccSSepherosa Ziehau 	int i, val = 0;
25756c8d8eccSSepherosa Ziehau 	uint16_t devctl;
25766c8d8eccSSepherosa Ziehau 
25774aa71e73SSepherosa Ziehau 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
25789f5082d5SSepherosa Ziehau 	if (sc->bnx_mfw_flags & BNX_MFW_ON_APE)
25799f5082d5SSepherosa Ziehau 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
25804aa71e73SSepherosa Ziehau 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
25816c8d8eccSSepherosa Ziehau 
25826c8d8eccSSepherosa Ziehau 	write_op = bnx_writemem_direct;
25836c8d8eccSSepherosa Ziehau 
25849f5082d5SSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
25859f5082d5SSepherosa Ziehau 	for (i = 0; i < 8000; i++) {
25869f5082d5SSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
25879f5082d5SSepherosa Ziehau 			break;
25889f5082d5SSepherosa Ziehau 		DELAY(20);
25899f5082d5SSepherosa Ziehau 	}
25909f5082d5SSepherosa Ziehau 	if (i == 8000)
25919f5082d5SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "NVRAM lock timedout!\n");
25929f5082d5SSepherosa Ziehau 
25939f5082d5SSepherosa Ziehau 	/* Take APE lock when performing reset. */
25949f5082d5SSepherosa Ziehau 	bnx_ape_lock(sc, BGE_APE_LOCK_GRC);
25959f5082d5SSepherosa Ziehau 
25966c8d8eccSSepherosa Ziehau 	/* Save some important PCI state. */
25976c8d8eccSSepherosa Ziehau 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
25986c8d8eccSSepherosa Ziehau 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
25996c8d8eccSSepherosa Ziehau 
26006c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
26016c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
26026c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
26036c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
26046c8d8eccSSepherosa Ziehau 
26056c8d8eccSSepherosa Ziehau 	/* Disable fastboot on controllers that support it. */
26066c8d8eccSSepherosa Ziehau 	if (bootverbose)
26076c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "Disabling fastboot\n");
26086c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
26096c8d8eccSSepherosa Ziehau 
26106c8d8eccSSepherosa Ziehau 	/*
26116c8d8eccSSepherosa Ziehau 	 * Write the magic number to SRAM at offset 0xB50.
26126c8d8eccSSepherosa Ziehau 	 * When firmware finishes its initialization it will
26130551ac06SSepherosa Ziehau 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
26146c8d8eccSSepherosa Ziehau 	 */
26150551ac06SSepherosa Ziehau 	bnx_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
26166c8d8eccSSepherosa Ziehau 
26176c8d8eccSSepherosa Ziehau 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
26186c8d8eccSSepherosa Ziehau 
26196c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
26206c8d8eccSSepherosa Ziehau 	/* Force PCI-E 1.0a mode */
26213730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc) &&
26226c8d8eccSSepherosa Ziehau 	    CSR_READ_4(sc, BGE_PCIE_PHY_TSTCTL) ==
26236c8d8eccSSepherosa Ziehau 	    (BGE_PCIE_PHY_TSTCTL_PSCRAM |
26246c8d8eccSSepherosa Ziehau 	     BGE_PCIE_PHY_TSTCTL_PCIE10)) {
26256c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_PHY_TSTCTL,
26266c8d8eccSSepherosa Ziehau 		    BGE_PCIE_PHY_TSTCTL_PSCRAM);
26276c8d8eccSSepherosa Ziehau 	}
26286c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid != BGE_CHIPID_BCM5750_A0) {
26296c8d8eccSSepherosa Ziehau 		/* Prevent PCIE link training during global reset */
26306c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
26316c8d8eccSSepherosa Ziehau 		reset |= (1<<29);
26326c8d8eccSSepherosa Ziehau 	}
26336c8d8eccSSepherosa Ziehau 
26346c8d8eccSSepherosa Ziehau 	/*
26357acfcee2SSepherosa Ziehau 	 * Set the clock to the highest frequency to avoid timeout.
26367acfcee2SSepherosa Ziehau 	 */
26377acfcee2SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717) {
26387acfcee2SSepherosa Ziehau 		BNX_SETBIT(sc, BGE_CPMU_CLCK_ORIDE_ENABLE,
26397acfcee2SSepherosa Ziehau 		    BGE_CPMU_MAC_ORIDE_ENABLE);
26407acfcee2SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
26417acfcee2SSepherosa Ziehau 	    sc->bnx_asicrev ==  BGE_ASICREV_BCM5720) {
26427acfcee2SSepherosa Ziehau 		BNX_SETBIT(sc, BGE_CPMU_CLCK_ORIDE,
26437acfcee2SSepherosa Ziehau 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
26447acfcee2SSepherosa Ziehau 	}
26457acfcee2SSepherosa Ziehau 
26467acfcee2SSepherosa Ziehau 	/*
26476c8d8eccSSepherosa Ziehau 	 * Set GPHY Power Down Override to leave GPHY
26486c8d8eccSSepherosa Ziehau 	 * powered up in D0 uninitialized.
26496c8d8eccSSepherosa Ziehau 	 */
26506c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0)
26516c8d8eccSSepherosa Ziehau 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
26526c8d8eccSSepherosa Ziehau 
26536c8d8eccSSepherosa Ziehau 	/* Issue global reset */
26546c8d8eccSSepherosa Ziehau 	write_op(sc, BGE_MISC_CFG, reset);
26556c8d8eccSSepherosa Ziehau 
26564aa71e73SSepherosa Ziehau 	DELAY(100 * 1000);
26576c8d8eccSSepherosa Ziehau 
26586c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
26596c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM5750_A0) {
26606c8d8eccSSepherosa Ziehau 		uint32_t v;
26616c8d8eccSSepherosa Ziehau 
26626c8d8eccSSepherosa Ziehau 		DELAY(500000); /* wait for link training to complete */
26636c8d8eccSSepherosa Ziehau 		v = pci_read_config(dev, 0xc4, 4);
26646c8d8eccSSepherosa Ziehau 		pci_write_config(dev, 0xc4, v | (1<<15), 4);
26656c8d8eccSSepherosa Ziehau 	}
26666c8d8eccSSepherosa Ziehau 
26676c8d8eccSSepherosa Ziehau 	devctl = pci_read_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL, 2);
26686c8d8eccSSepherosa Ziehau 
26696c8d8eccSSepherosa Ziehau 	/* Disable no snoop and disable relaxed ordering. */
26706c8d8eccSSepherosa Ziehau 	devctl &= ~(PCIEM_DEVCTL_RELAX_ORDER | PCIEM_DEVCTL_NOSNOOP);
26716c8d8eccSSepherosa Ziehau 
26726c8d8eccSSepherosa Ziehau 	/* Old PCI-E chips only support 128 bytes Max PayLoad Size. */
26736c8d8eccSSepherosa Ziehau 	if ((sc->bnx_flags & BNX_FLAG_CPMU) == 0) {
26746c8d8eccSSepherosa Ziehau 		devctl &= ~PCIEM_DEVCTL_MAX_PAYLOAD_MASK;
26756c8d8eccSSepherosa Ziehau 		devctl |= PCIEM_DEVCTL_MAX_PAYLOAD_128;
26766c8d8eccSSepherosa Ziehau 	}
26776c8d8eccSSepherosa Ziehau 
26786c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVCTRL,
26796c8d8eccSSepherosa Ziehau 	    devctl, 2);
26806c8d8eccSSepherosa Ziehau 
26816c8d8eccSSepherosa Ziehau 	/* Clear error status. */
26826c8d8eccSSepherosa Ziehau 	pci_write_config(dev, sc->bnx_pciecap + PCIER_DEVSTS,
26836c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_CORR_ERR |
26846c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_NFATAL_ERR |
26856c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_FATAL_ERR |
26866c8d8eccSSepherosa Ziehau 	    PCIEM_DEVSTS_UNSUPP_REQ, 2);
26876c8d8eccSSepherosa Ziehau 
26886c8d8eccSSepherosa Ziehau 	/* Reset some of the PCI state that got zapped by reset */
26896c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_MISC_CTL,
26906c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
26916c8d8eccSSepherosa Ziehau 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW|
26926c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_TAGGED_STATUS, 4);
26934aa71e73SSepherosa Ziehau 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
26949f5082d5SSepherosa Ziehau 	if (sc->bnx_mfw_flags & BNX_MFW_ON_APE) {
26959f5082d5SSepherosa Ziehau 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
26969f5082d5SSepherosa Ziehau 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
26979f5082d5SSepherosa Ziehau 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
26989f5082d5SSepherosa Ziehau 	}
26994aa71e73SSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
27006c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
27016c8d8eccSSepherosa Ziehau 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
27026c8d8eccSSepherosa Ziehau 
27036c8d8eccSSepherosa Ziehau 	/* Enable memory arbiter */
27046c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
27056c8d8eccSSepherosa Ziehau 
27064aa71e73SSepherosa Ziehau 	/* Fix up byte swapping */
27074aa71e73SSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MODE_CTL, bnx_dma_swap_options(sc));
27084aa71e73SSepherosa Ziehau 
27094aa71e73SSepherosa Ziehau 	val = CSR_READ_4(sc, BGE_MAC_MODE);
27104aa71e73SSepherosa Ziehau 	val = (val & ~mac_mode_mask) | mac_mode;
27114aa71e73SSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
27124aa71e73SSepherosa Ziehau 	DELAY(40);
27134aa71e73SSepherosa Ziehau 
27149f5082d5SSepherosa Ziehau 	bnx_ape_unlock(sc, BGE_APE_LOCK_GRC);
27159f5082d5SSepherosa Ziehau 
27166c8d8eccSSepherosa Ziehau 	/*
27176c8d8eccSSepherosa Ziehau 	 * Poll until we see the 1's complement of the magic number.
2718ddd93a5cSSepherosa Ziehau 	 * This indicates that the firmware initialization is complete.
27196c8d8eccSSepherosa Ziehau 	 */
27206c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_FIRMWARE_TIMEOUT; i++) {
27210551ac06SSepherosa Ziehau 		val = bnx_readmem_ind(sc, BGE_SRAM_FW_MB);
27220551ac06SSepherosa Ziehau 		if (val == ~BGE_SRAM_FW_MB_MAGIC)
27236c8d8eccSSepherosa Ziehau 			break;
27246c8d8eccSSepherosa Ziehau 		DELAY(10);
27256c8d8eccSSepherosa Ziehau 	}
27266c8d8eccSSepherosa Ziehau 	if (i == BNX_FIRMWARE_TIMEOUT) {
27276c8d8eccSSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "firmware handshake "
27286c8d8eccSSepherosa Ziehau 			  "timed out, found 0x%08x\n", val);
27296c8d8eccSSepherosa Ziehau 	}
27306c8d8eccSSepherosa Ziehau 
27316c8d8eccSSepherosa Ziehau 	/* BCM57765 A0 needs additional time before accessing. */
27326c8d8eccSSepherosa Ziehau 	if (sc->bnx_chipid == BGE_CHIPID_BCM57765_A0)
27336c8d8eccSSepherosa Ziehau 		DELAY(10 * 1000);
27346c8d8eccSSepherosa Ziehau 
27356c8d8eccSSepherosa Ziehau 	/*
27366c8d8eccSSepherosa Ziehau 	 * The 5704 in TBI mode apparently needs some special
27376c8d8eccSSepherosa Ziehau 	 * adjustment to insure the SERDES drive level is set
27386c8d8eccSSepherosa Ziehau 	 * to 1.2V.
27396c8d8eccSSepherosa Ziehau 	 */
27406c8d8eccSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5704 &&
27416c8d8eccSSepherosa Ziehau 	    (sc->bnx_flags & BNX_FLAG_TBI)) {
27426c8d8eccSSepherosa Ziehau 		uint32_t serdescfg;
27436c8d8eccSSepherosa Ziehau 
27446c8d8eccSSepherosa Ziehau 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
27456c8d8eccSSepherosa Ziehau 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
27466c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
27476c8d8eccSSepherosa Ziehau 	}
27486c8d8eccSSepherosa Ziehau 
27497892075dSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MI_MODE,
27507892075dSSepherosa Ziehau 	    sc->bnx_mi_mode & ~BGE_MIMODE_AUTOPOLL);
27517892075dSSepherosa Ziehau 	DELAY(80);
27527892075dSSepherosa Ziehau 
27536c8d8eccSSepherosa Ziehau 	/* XXX: Broadcom Linux driver. */
27543730a14dSSepherosa Ziehau 	if (!BNX_IS_57765_PLUS(sc)) {
27556c8d8eccSSepherosa Ziehau 		uint32_t v;
27566c8d8eccSSepherosa Ziehau 
27576c8d8eccSSepherosa Ziehau 		/* Enable Data FIFO protection. */
2758f1f34fc4SSepherosa Ziehau 		v = CSR_READ_4(sc, BGE_PCIE_TLDLPL_PORT);
2759f1f34fc4SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_PCIE_TLDLPL_PORT, v | (1 << 25));
27606c8d8eccSSepherosa Ziehau 	}
27616c8d8eccSSepherosa Ziehau 
27626c8d8eccSSepherosa Ziehau 	DELAY(10000);
27636c8d8eccSSepherosa Ziehau 
27647acfcee2SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5717) {
27657acfcee2SSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE_ENABLE,
27667acfcee2SSepherosa Ziehau 		    BGE_CPMU_MAC_ORIDE_ENABLE);
27677acfcee2SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
27687acfcee2SSepherosa Ziehau 	    sc->bnx_asicrev ==  BGE_ASICREV_BCM5720) {
27696c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
27706c8d8eccSSepherosa Ziehau 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
27717acfcee2SSepherosa Ziehau 	} else if (sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
27727acfcee2SSepherosa Ziehau 		/*
27737acfcee2SSepherosa Ziehau 		 * Increase the core clock speed to fix TX timeout for
27747acfcee2SSepherosa Ziehau 		 * 5762 on 100Mbps link.
27757acfcee2SSepherosa Ziehau 		 */
27767acfcee2SSepherosa Ziehau 		BNX_SETBIT(sc, BGE_CPMU_CLCK_ORIDE_ENABLE,
27777acfcee2SSepherosa Ziehau 		    BGE_CPMU_MAC_ORIDE_ENABLE);
27786c8d8eccSSepherosa Ziehau 	}
27796c8d8eccSSepherosa Ziehau }
27806c8d8eccSSepherosa Ziehau 
27816c8d8eccSSepherosa Ziehau /*
27826c8d8eccSSepherosa Ziehau  * Frame reception handling. This is called if there's a frame
27836c8d8eccSSepherosa Ziehau  * on the receive return list.
27846c8d8eccSSepherosa Ziehau  *
27856c8d8eccSSepherosa Ziehau  * Note: we have to be able to handle two possibilities here:
27866c8d8eccSSepherosa Ziehau  * 1) the frame is from the jumbo recieve ring
27876c8d8eccSSepherosa Ziehau  * 2) the frame is from the standard receive ring
27886c8d8eccSSepherosa Ziehau  */
27896c8d8eccSSepherosa Ziehau 
27906c8d8eccSSepherosa Ziehau static void
bnx_rxeof(struct bnx_rx_ret_ring * ret,uint16_t rx_prod,int count)2791beedf5beSSepherosa Ziehau bnx_rxeof(struct bnx_rx_ret_ring *ret, uint16_t rx_prod, int count)
27926c8d8eccSSepherosa Ziehau {
2793beedf5beSSepherosa Ziehau 	struct bnx_softc *sc = ret->bnx_sc;
2794beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = ret->bnx_std;
2795beedf5beSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
2796ff37a356SSepherosa Ziehau 	int std_used = 0, cpuid = mycpuid;
27976c8d8eccSSepherosa Ziehau 
2798beedf5beSSepherosa Ziehau 	while (ret->bnx_rx_saved_considx != rx_prod && count != 0) {
2799b19ddf7eSSepherosa Ziehau 		struct pktinfo pi0, *pi = NULL;
28006c8d8eccSSepherosa Ziehau 		struct bge_rx_bd *cur_rx;
2801841cdf08SSepherosa Ziehau 		struct bnx_rx_buf *rb;
28026c8d8eccSSepherosa Ziehau 		uint32_t rxidx;
28036c8d8eccSSepherosa Ziehau 		struct mbuf *m = NULL;
28046c8d8eccSSepherosa Ziehau 		uint16_t vlan_tag = 0;
28056c8d8eccSSepherosa Ziehau 		int have_tag = 0;
28066c8d8eccSSepherosa Ziehau 
280797381780SSepherosa Ziehau 		--count;
280897381780SSepherosa Ziehau 
2809beedf5beSSepherosa Ziehau 		cur_rx = &ret->bnx_rx_ret_ring[ret->bnx_rx_saved_considx];
28106c8d8eccSSepherosa Ziehau 
28116c8d8eccSSepherosa Ziehau 		rxidx = cur_rx->bge_idx;
2812695a8586SSepherosa Ziehau 		KKASSERT(rxidx < BGE_STD_RX_RING_CNT);
2813695a8586SSepherosa Ziehau 
2814beedf5beSSepherosa Ziehau 		BNX_INC(ret->bnx_rx_saved_considx, BNX_RETURN_RING_CNT);
2815695a8586SSepherosa Ziehau #ifdef BNX_RSS_DEBUG
2816695a8586SSepherosa Ziehau 		ret->bnx_rx_pkt++;
2817695a8586SSepherosa Ziehau #endif
28186c8d8eccSSepherosa Ziehau 
28196c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
28206c8d8eccSSepherosa Ziehau 			have_tag = 1;
28216c8d8eccSSepherosa Ziehau 			vlan_tag = cur_rx->bge_vlan_tag;
28226c8d8eccSSepherosa Ziehau 		}
28236c8d8eccSSepherosa Ziehau 
2824625c3ba3SSepherosa Ziehau 		if (ret->bnx_rx_cnt >= ret->bnx_rx_cntmax) {
2825625c3ba3SSepherosa Ziehau 			atomic_add_int(&std->bnx_rx_std_used, std_used);
2826625c3ba3SSepherosa Ziehau 			std_used = 0;
2827625c3ba3SSepherosa Ziehau 
2828695a8586SSepherosa Ziehau 			bnx_rx_std_refill_sched(ret, std);
2829625c3ba3SSepherosa Ziehau 		}
2830841cdf08SSepherosa Ziehau 		ret->bnx_rx_cnt++;
2831625c3ba3SSepherosa Ziehau 		++std_used;
28326c8d8eccSSepherosa Ziehau 
2833841cdf08SSepherosa Ziehau 		rb = &std->bnx_rx_std_buf[rxidx];
2834841cdf08SSepherosa Ziehau 		m = rb->bnx_rx_mbuf;
28356c8d8eccSSepherosa Ziehau 		if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2836d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, ierrors, 1);
2837841cdf08SSepherosa Ziehau 			cpu_sfence();
2838841cdf08SSepherosa Ziehau 			rb->bnx_rx_refilled = 1;
28396c8d8eccSSepherosa Ziehau 			continue;
28406c8d8eccSSepherosa Ziehau 		}
2841841cdf08SSepherosa Ziehau 		if (bnx_newbuf_std(ret, rxidx, 0)) {
2842d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, ierrors, 1);
28436c8d8eccSSepherosa Ziehau 			continue;
28446c8d8eccSSepherosa Ziehau 		}
28456c8d8eccSSepherosa Ziehau 
2846d40991efSSepherosa Ziehau 		IFNET_STAT_INC(ifp, ipackets, 1);
28476c8d8eccSSepherosa Ziehau 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
28486c8d8eccSSepherosa Ziehau 		m->m_pkthdr.rcvif = ifp;
28496c8d8eccSSepherosa Ziehau 
28506c8d8eccSSepherosa Ziehau 		if ((ifp->if_capenable & IFCAP_RXCSUM) &&
28516c8d8eccSSepherosa Ziehau 		    (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
28526c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
28536c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
28546c8d8eccSSepherosa Ziehau 				if ((cur_rx->bge_error_flag &
28556c8d8eccSSepherosa Ziehau 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
28566c8d8eccSSepherosa Ziehau 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
28576c8d8eccSSepherosa Ziehau 			}
28586c8d8eccSSepherosa Ziehau 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
28596c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_data =
28606c8d8eccSSepherosa Ziehau 				    cur_rx->bge_tcp_udp_csum;
28616c8d8eccSSepherosa Ziehau 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
28626c8d8eccSSepherosa Ziehau 				    CSUM_PSEUDO_HDR;
28636c8d8eccSSepherosa Ziehau 			}
28646c8d8eccSSepherosa Ziehau 		}
2865b19ddf7eSSepherosa Ziehau 		if (ifp->if_capenable & IFCAP_RSS) {
2866b19ddf7eSSepherosa Ziehau 			pi = bnx_rss_info(&pi0, cur_rx);
2867b19ddf7eSSepherosa Ziehau 			if (pi != NULL &&
28687558541bSSepherosa Ziehau 			    (cur_rx->bge_flags & BGE_RXBDFLAG_RSS_HASH))
28697558541bSSepherosa Ziehau 				m_sethash(m, toeplitz_hash(cur_rx->bge_hash));
2870b19ddf7eSSepherosa Ziehau 		}
28716c8d8eccSSepherosa Ziehau 
28726c8d8eccSSepherosa Ziehau 		/*
28736c8d8eccSSepherosa Ziehau 		 * If we received a packet with a vlan tag, pass it
28746c8d8eccSSepherosa Ziehau 		 * to vlan_input() instead of ether_input().
28756c8d8eccSSepherosa Ziehau 		 */
28766c8d8eccSSepherosa Ziehau 		if (have_tag) {
28776c8d8eccSSepherosa Ziehau 			m->m_flags |= M_VLANTAG;
28786c8d8eccSSepherosa Ziehau 			m->m_pkthdr.ether_vlantag = vlan_tag;
28796c8d8eccSSepherosa Ziehau 		}
2880be4134c6SFranco Fichtner 		ifp->if_input(ifp, m, pi, cpuid);
28816c8d8eccSSepherosa Ziehau 	}
2882ac2936fdSSepherosa Ziehau 	bnx_writembx(sc, ret->bnx_rx_mbx, ret->bnx_rx_saved_considx);
2883695a8586SSepherosa Ziehau 
2884625c3ba3SSepherosa Ziehau 	if (std_used > 0) {
2885625c3ba3SSepherosa Ziehau 		int cur_std_used;
2886625c3ba3SSepherosa Ziehau 
2887625c3ba3SSepherosa Ziehau 		cur_std_used = atomic_fetchadd_int(&std->bnx_rx_std_used,
2888625c3ba3SSepherosa Ziehau 		    std_used);
2889625c3ba3SSepherosa Ziehau 		if (cur_std_used + std_used >= (BGE_STD_RX_RING_CNT / 2)) {
2890625c3ba3SSepherosa Ziehau #ifdef BNX_RSS_DEBUG
2891625c3ba3SSepherosa Ziehau 			ret->bnx_rx_force_sched++;
2892625c3ba3SSepherosa Ziehau #endif
2893695a8586SSepherosa Ziehau 			bnx_rx_std_refill_sched(ret, std);
28946c8d8eccSSepherosa Ziehau 		}
2895625c3ba3SSepherosa Ziehau 	}
2896625c3ba3SSepherosa Ziehau }
28976c8d8eccSSepherosa Ziehau 
28986c8d8eccSSepherosa Ziehau static void
bnx_txeof(struct bnx_tx_ring * txr,uint16_t tx_cons)289933a04907SSepherosa Ziehau bnx_txeof(struct bnx_tx_ring *txr, uint16_t tx_cons)
29006c8d8eccSSepherosa Ziehau {
290133a04907SSepherosa Ziehau 	struct ifnet *ifp = &txr->bnx_sc->arpcom.ac_if;
29026c8d8eccSSepherosa Ziehau 
29036c8d8eccSSepherosa Ziehau 	/*
29046c8d8eccSSepherosa Ziehau 	 * Go through our tx ring and free mbufs for those
29056c8d8eccSSepherosa Ziehau 	 * frames that have been sent.
29066c8d8eccSSepherosa Ziehau 	 */
290733a04907SSepherosa Ziehau 	while (txr->bnx_tx_saved_considx != tx_cons) {
2908fa4b1067SSepherosa Ziehau 		struct bnx_tx_buf *buf;
29096c8d8eccSSepherosa Ziehau 		uint32_t idx = 0;
29106c8d8eccSSepherosa Ziehau 
291133a04907SSepherosa Ziehau 		idx = txr->bnx_tx_saved_considx;
2912fa4b1067SSepherosa Ziehau 		buf = &txr->bnx_tx_buf[idx];
2913fa4b1067SSepherosa Ziehau 		if (buf->bnx_tx_mbuf != NULL) {
2914d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, opackets, 1);
2915695a8586SSepherosa Ziehau #ifdef BNX_TSS_DEBUG
2916695a8586SSepherosa Ziehau 			txr->bnx_tx_pkt++;
2917695a8586SSepherosa Ziehau #endif
291833a04907SSepherosa Ziehau 			bus_dmamap_unload(txr->bnx_tx_mtag,
2919fa4b1067SSepherosa Ziehau 			    buf->bnx_tx_dmamap);
2920fa4b1067SSepherosa Ziehau 			m_freem(buf->bnx_tx_mbuf);
2921fa4b1067SSepherosa Ziehau 			buf->bnx_tx_mbuf = NULL;
29226c8d8eccSSepherosa Ziehau 		}
2923fa639b88SSepherosa Ziehau 		txr->bnx_tx_cnt--;
292433a04907SSepherosa Ziehau 		BNX_INC(txr->bnx_tx_saved_considx, BGE_TX_RING_CNT);
29256c8d8eccSSepherosa Ziehau 	}
29266c8d8eccSSepherosa Ziehau 
2927fa639b88SSepherosa Ziehau 	if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) >=
29286c8d8eccSSepherosa Ziehau 	    (BNX_NSEG_RSVD + BNX_NSEG_SPARE))
29293397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
29306c8d8eccSSepherosa Ziehau 
2931fa639b88SSepherosa Ziehau 	if (txr->bnx_tx_cnt == 0)
2932e2292763SMatthew Dillon 		ifsq_watchdog_set_count(&txr->bnx_tx_watchdog, 0);
29336c8d8eccSSepherosa Ziehau 
29343397dea6SSepherosa Ziehau 	if (!ifsq_is_empty(txr->bnx_ifsq))
29353397dea6SSepherosa Ziehau 		ifsq_devstart(txr->bnx_ifsq);
29366c8d8eccSSepherosa Ziehau }
29376c8d8eccSSepherosa Ziehau 
293824e16e4bSSepherosa Ziehau static int
bnx_handle_status(struct bnx_softc * sc)2939695a8586SSepherosa Ziehau bnx_handle_status(struct bnx_softc *sc)
2940695a8586SSepherosa Ziehau {
2941695a8586SSepherosa Ziehau 	uint32_t status;
294224e16e4bSSepherosa Ziehau 	int handle = 0;
2943695a8586SSepherosa Ziehau 
2944695a8586SSepherosa Ziehau 	status = *sc->bnx_hw_status;
2945695a8586SSepherosa Ziehau 
2946695a8586SSepherosa Ziehau 	if (status & BGE_STATFLAG_ERROR) {
2947695a8586SSepherosa Ziehau 		uint32_t val;
2948695a8586SSepherosa Ziehau 		int reset = 0;
2949695a8586SSepherosa Ziehau 
2950695a8586SSepherosa Ziehau 		sc->bnx_errors++;
2951695a8586SSepherosa Ziehau 
2952695a8586SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_FLOW_ATTN);
2953695a8586SSepherosa Ziehau 		if (val & ~BGE_FLOWATTN_MB_LOWAT) {
2954695a8586SSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if,
2955695a8586SSepherosa Ziehau 			    "flow attn 0x%08x\n", val);
2956695a8586SSepherosa Ziehau 			reset = 1;
2957695a8586SSepherosa Ziehau 		}
2958695a8586SSepherosa Ziehau 
2959695a8586SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_MSI_STATUS);
2960695a8586SSepherosa Ziehau 		if (val & ~BGE_MSISTAT_MSI_PCI_REQ) {
2961695a8586SSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if,
2962695a8586SSepherosa Ziehau 			    "msi status 0x%08x\n", val);
2963695a8586SSepherosa Ziehau 			reset = 1;
2964695a8586SSepherosa Ziehau 		}
2965695a8586SSepherosa Ziehau 
2966695a8586SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_RDMA_STATUS);
2967695a8586SSepherosa Ziehau 		if (val) {
2968695a8586SSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if,
2969695a8586SSepherosa Ziehau 			    "rmda status 0x%08x\n", val);
2970695a8586SSepherosa Ziehau 			reset = 1;
2971695a8586SSepherosa Ziehau 		}
2972695a8586SSepherosa Ziehau 
2973695a8586SSepherosa Ziehau 		val = CSR_READ_4(sc, BGE_WDMA_STATUS);
2974695a8586SSepherosa Ziehau 		if (val) {
2975695a8586SSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if,
2976695a8586SSepherosa Ziehau 			    "wdma status 0x%08x\n", val);
2977695a8586SSepherosa Ziehau 			reset = 1;
2978695a8586SSepherosa Ziehau 		}
2979695a8586SSepherosa Ziehau 
2980695a8586SSepherosa Ziehau 		if (reset) {
2981695a8586SSepherosa Ziehau 			bnx_serialize_skipmain(sc);
2982695a8586SSepherosa Ziehau 			bnx_init(sc);
2983695a8586SSepherosa Ziehau 			bnx_deserialize_skipmain(sc);
2984695a8586SSepherosa Ziehau 		}
298524e16e4bSSepherosa Ziehau 		handle = 1;
2986695a8586SSepherosa Ziehau 	}
2987695a8586SSepherosa Ziehau 
298824e16e4bSSepherosa Ziehau 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) || sc->bnx_link_evt) {
298924e16e4bSSepherosa Ziehau 		if (bootverbose) {
299024e16e4bSSepherosa Ziehau 			if_printf(&sc->arpcom.ac_if, "link change, "
299124e16e4bSSepherosa Ziehau 			    "link_evt %d\n", sc->bnx_link_evt);
299224e16e4bSSepherosa Ziehau 		}
2993695a8586SSepherosa Ziehau 		bnx_link_poll(sc);
299424e16e4bSSepherosa Ziehau 		handle = 1;
299524e16e4bSSepherosa Ziehau 	}
299624e16e4bSSepherosa Ziehau 
299724e16e4bSSepherosa Ziehau 	return handle;
2998695a8586SSepherosa Ziehau }
2999695a8586SSepherosa Ziehau 
300039a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
30016c8d8eccSSepherosa Ziehau 
30026c8d8eccSSepherosa Ziehau static void
bnx_npoll_rx(struct ifnet * ifp __unused,void * xret,int cycle)30034fa38985SSepherosa Ziehau bnx_npoll_rx(struct ifnet *ifp __unused, void *xret, int cycle)
30044fa38985SSepherosa Ziehau {
30054fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = xret;
30064fa38985SSepherosa Ziehau 	uint16_t rx_prod;
30074fa38985SSepherosa Ziehau 
30084fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
30094fa38985SSepherosa Ziehau 
30104fa38985SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
30114fa38985SSepherosa Ziehau 	cpu_lfence();
30124fa38985SSepherosa Ziehau 
30134fa38985SSepherosa Ziehau 	rx_prod = *ret->bnx_rx_considx;
30144fa38985SSepherosa Ziehau 	if (ret->bnx_rx_saved_considx != rx_prod)
30154fa38985SSepherosa Ziehau 		bnx_rxeof(ret, rx_prod, cycle);
30164fa38985SSepherosa Ziehau }
30174fa38985SSepherosa Ziehau 
30184fa38985SSepherosa Ziehau static void
bnx_npoll_tx_notag(struct ifnet * ifp __unused,void * xtxr,int cycle __unused)3019695a8586SSepherosa Ziehau bnx_npoll_tx_notag(struct ifnet *ifp __unused, void *xtxr, int cycle __unused)
30204fa38985SSepherosa Ziehau {
30214fa38985SSepherosa Ziehau 	struct bnx_tx_ring *txr = xtxr;
30224fa38985SSepherosa Ziehau 	uint16_t tx_cons;
30234fa38985SSepherosa Ziehau 
30244fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
30254fa38985SSepherosa Ziehau 
30264fa38985SSepherosa Ziehau 	tx_cons = *txr->bnx_tx_considx;
30274fa38985SSepherosa Ziehau 	if (txr->bnx_tx_saved_considx != tx_cons)
30284fa38985SSepherosa Ziehau 		bnx_txeof(txr, tx_cons);
30294fa38985SSepherosa Ziehau }
30304fa38985SSepherosa Ziehau 
30314fa38985SSepherosa Ziehau static void
bnx_npoll_tx(struct ifnet * ifp,void * xtxr,int cycle)3032695a8586SSepherosa Ziehau bnx_npoll_tx(struct ifnet *ifp, void *xtxr, int cycle)
3033695a8586SSepherosa Ziehau {
3034695a8586SSepherosa Ziehau 	struct bnx_tx_ring *txr = xtxr;
3035695a8586SSepherosa Ziehau 
3036695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
3037695a8586SSepherosa Ziehau 
3038695a8586SSepherosa Ziehau 	txr->bnx_saved_status_tag = *txr->bnx_hw_status_tag;
3039695a8586SSepherosa Ziehau 	cpu_lfence();
3040695a8586SSepherosa Ziehau 	bnx_npoll_tx_notag(ifp, txr, cycle);
3041695a8586SSepherosa Ziehau }
3042695a8586SSepherosa Ziehau 
3043695a8586SSepherosa Ziehau static void
bnx_npoll_status_notag(struct ifnet * ifp)3044695a8586SSepherosa Ziehau bnx_npoll_status_notag(struct ifnet *ifp)
30454fa38985SSepherosa Ziehau {
30464fa38985SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
30474fa38985SSepherosa Ziehau 
30484fa38985SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
30494fa38985SSepherosa Ziehau 
305024e16e4bSSepherosa Ziehau 	if (bnx_handle_status(sc)) {
305124e16e4bSSepherosa Ziehau 		/*
305224e16e4bSSepherosa Ziehau 		 * Status changes are handled; force the chip to
305324e16e4bSSepherosa Ziehau 		 * update the status block to reflect whether there
305424e16e4bSSepherosa Ziehau 		 * are more status changes or not, else staled status
305524e16e4bSSepherosa Ziehau 		 * changes are always seen.
305624e16e4bSSepherosa Ziehau 		 */
305724e16e4bSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
305824e16e4bSSepherosa Ziehau 	}
3059695a8586SSepherosa Ziehau }
3060695a8586SSepherosa Ziehau 
3061695a8586SSepherosa Ziehau static void
bnx_npoll_status(struct ifnet * ifp)3062695a8586SSepherosa Ziehau bnx_npoll_status(struct ifnet *ifp)
3063695a8586SSepherosa Ziehau {
3064695a8586SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
3065695a8586SSepherosa Ziehau 
3066695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3067695a8586SSepherosa Ziehau 
3068695a8586SSepherosa Ziehau 	sc->bnx_saved_status_tag = *sc->bnx_hw_status_tag;
3069695a8586SSepherosa Ziehau 	cpu_lfence();
3070695a8586SSepherosa Ziehau 	bnx_npoll_status_notag(ifp);
30714fa38985SSepherosa Ziehau }
30724fa38985SSepherosa Ziehau 
30734fa38985SSepherosa Ziehau static void
bnx_npoll(struct ifnet * ifp,struct ifpoll_info * info)307439a8d43aSSepherosa Ziehau bnx_npoll(struct ifnet *ifp, struct ifpoll_info *info)
307539a8d43aSSepherosa Ziehau {
307639a8d43aSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
30774fa38985SSepherosa Ziehau 	int i;
307839a8d43aSSepherosa Ziehau 
3079329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
308039a8d43aSSepherosa Ziehau 
308139a8d43aSSepherosa Ziehau 	if (info != NULL) {
3082695a8586SSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_STATUS_HASTAG)
30834fa38985SSepherosa Ziehau 			info->ifpi_status.status_func = bnx_npoll_status;
3084695a8586SSepherosa Ziehau 		else
3085695a8586SSepherosa Ziehau 			info->ifpi_status.status_func = bnx_npoll_status_notag;
30864fa38985SSepherosa Ziehau 		info->ifpi_status.serializer = &sc->bnx_main_serialize;
308739a8d43aSSepherosa Ziehau 
30884fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
30894fa38985SSepherosa Ziehau 			struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
309002596bedSSepherosa Ziehau 			int cpu = if_ringmap_cpumap(sc->bnx_tx_rmap, i);
30914fa38985SSepherosa Ziehau 
309202596bedSSepherosa Ziehau 			KKASSERT(cpu < netisr_ncpus);
3093695a8586SSepherosa Ziehau 			if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
309402596bedSSepherosa Ziehau 				info->ifpi_tx[cpu].poll_func =
3095695a8586SSepherosa Ziehau 				    bnx_npoll_tx_notag;
3096695a8586SSepherosa Ziehau 			} else {
309702596bedSSepherosa Ziehau 				info->ifpi_tx[cpu].poll_func = bnx_npoll_tx;
3098695a8586SSepherosa Ziehau 			}
309902596bedSSepherosa Ziehau 			info->ifpi_tx[cpu].arg = txr;
310002596bedSSepherosa Ziehau 			info->ifpi_tx[cpu].serializer = &txr->bnx_tx_serialize;
310102596bedSSepherosa Ziehau 			ifsq_set_cpuid(txr->bnx_ifsq, cpu);
31024fa38985SSepherosa Ziehau 		}
31034fa38985SSepherosa Ziehau 
31044fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
31054fa38985SSepherosa Ziehau 			struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
310602596bedSSepherosa Ziehau 			int cpu = if_ringmap_cpumap(sc->bnx_rx_rmap, i);
31074fa38985SSepherosa Ziehau 
310802596bedSSepherosa Ziehau 			KKASSERT(cpu < netisr_ncpus);
310902596bedSSepherosa Ziehau 			info->ifpi_rx[cpu].poll_func = bnx_npoll_rx;
311002596bedSSepherosa Ziehau 			info->ifpi_rx[cpu].arg = ret;
311102596bedSSepherosa Ziehau 			info->ifpi_rx[cpu].serializer =
31124fa38985SSepherosa Ziehau 			    &ret->bnx_rx_ret_serialize;
31134fa38985SSepherosa Ziehau 		}
311439a8d43aSSepherosa Ziehau 
31157dbaa833SSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING) {
311639a8d43aSSepherosa Ziehau 			bnx_disable_intr(sc);
31177dbaa833SSepherosa Ziehau 			bnx_set_tick_cpuid(sc, TRUE);
311827357d84SSepherosa Ziehau 
3119a86cc105SSepherosa Ziehau 			sc->bnx_coal_chg = BNX_TX_COAL_BDS_CHG |
3120a86cc105SSepherosa Ziehau 			    BNX_RX_COAL_BDS_CHG;
312127357d84SSepherosa Ziehau 			bnx_coal_change(sc);
31227dbaa833SSepherosa Ziehau 		}
312339a8d43aSSepherosa Ziehau 	} else {
31244fa38985SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
31254fa38985SSepherosa Ziehau 			ifsq_set_cpuid(sc->bnx_tx_ring[i].bnx_ifsq,
31264fa38985SSepherosa Ziehau 			    sc->bnx_tx_ring[i].bnx_tx_cpuid);
31274fa38985SSepherosa Ziehau 		}
31287dbaa833SSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING) {
3129a86cc105SSepherosa Ziehau 			sc->bnx_coal_chg = BNX_TX_COAL_BDS_CHG |
3130a86cc105SSepherosa Ziehau 			    BNX_RX_COAL_BDS_CHG;
313127357d84SSepherosa Ziehau 			bnx_coal_change(sc);
313227357d84SSepherosa Ziehau 
313339a8d43aSSepherosa Ziehau 			bnx_enable_intr(sc);
31347dbaa833SSepherosa Ziehau 			bnx_set_tick_cpuid(sc, FALSE);
31357dbaa833SSepherosa Ziehau 		}
313639a8d43aSSepherosa Ziehau 	}
313739a8d43aSSepherosa Ziehau }
313839a8d43aSSepherosa Ziehau 
313939a8d43aSSepherosa Ziehau #endif	/* IFPOLL_ENABLE */
31406c8d8eccSSepherosa Ziehau 
31416c8d8eccSSepherosa Ziehau static void
bnx_intr_legacy(void * xsc)31426c8d8eccSSepherosa Ziehau bnx_intr_legacy(void *xsc)
31436c8d8eccSSepherosa Ziehau {
31446c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
31454fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
31466c8d8eccSSepherosa Ziehau 
31474fa38985SSepherosa Ziehau 	if (ret->bnx_saved_status_tag == *ret->bnx_hw_status_tag) {
31486c8d8eccSSepherosa Ziehau 		uint32_t val;
31496c8d8eccSSepherosa Ziehau 
31506c8d8eccSSepherosa Ziehau 		val = pci_read_config(sc->bnx_dev, BGE_PCI_PCISTATE, 4);
31516c8d8eccSSepherosa Ziehau 		if (val & BGE_PCISTAT_INTR_NOTACT)
31526c8d8eccSSepherosa Ziehau 			return;
31536c8d8eccSSepherosa Ziehau 	}
31546c8d8eccSSepherosa Ziehau 
31556c8d8eccSSepherosa Ziehau 	/*
31566c8d8eccSSepherosa Ziehau 	 * NOTE:
31576c8d8eccSSepherosa Ziehau 	 * Interrupt will have to be disabled if tagged status
31586c8d8eccSSepherosa Ziehau 	 * is used, else interrupt will always be asserted on
31596c8d8eccSSepherosa Ziehau 	 * certain chips (at least on BCM5750 AX/BX).
31606c8d8eccSSepherosa Ziehau 	 */
31616c8d8eccSSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, 1);
31626c8d8eccSSepherosa Ziehau 
31636c8d8eccSSepherosa Ziehau 	bnx_intr(sc);
31646c8d8eccSSepherosa Ziehau }
31656c8d8eccSSepherosa Ziehau 
31666c8d8eccSSepherosa Ziehau static void
bnx_msi(void * xsc)316703cc99fdSSepherosa Ziehau bnx_msi(void *xsc)
31686c8d8eccSSepherosa Ziehau {
31696c8d8eccSSepherosa Ziehau 	bnx_intr(xsc);
31706c8d8eccSSepherosa Ziehau }
31716c8d8eccSSepherosa Ziehau 
31726c8d8eccSSepherosa Ziehau static void
bnx_intr(struct bnx_softc * sc)31736c8d8eccSSepherosa Ziehau bnx_intr(struct bnx_softc *sc)
31746c8d8eccSSepherosa Ziehau {
31756c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
31764fa38985SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
31776c8d8eccSSepherosa Ziehau 
3178329f9016SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3179329f9016SSepherosa Ziehau 
31804fa38985SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
31816c8d8eccSSepherosa Ziehau 	/*
31826c8d8eccSSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
31836c8d8eccSSepherosa Ziehau 	 * before rx_prod, tx_cons and status.
31846c8d8eccSSepherosa Ziehau 	 */
31856c8d8eccSSepherosa Ziehau 	cpu_lfence();
31866c8d8eccSSepherosa Ziehau 
3187695a8586SSepherosa Ziehau 	bnx_handle_status(sc);
31886c8d8eccSSepherosa Ziehau 
31896c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_RUNNING) {
31904fa38985SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
31913a16b7b8SSepherosa Ziehau 		uint16_t rx_prod, tx_cons;
31923a16b7b8SSepherosa Ziehau 
3193329f9016SSepherosa Ziehau 		lwkt_serialize_enter(&ret->bnx_rx_ret_serialize);
31943a16b7b8SSepherosa Ziehau 		rx_prod = *ret->bnx_rx_considx;
3195beedf5beSSepherosa Ziehau 		if (ret->bnx_rx_saved_considx != rx_prod)
3196beedf5beSSepherosa Ziehau 			bnx_rxeof(ret, rx_prod, -1);
3197329f9016SSepherosa Ziehau 		lwkt_serialize_exit(&ret->bnx_rx_ret_serialize);
31986c8d8eccSSepherosa Ziehau 
3199329f9016SSepherosa Ziehau 		lwkt_serialize_enter(&txr->bnx_tx_serialize);
3200329f9016SSepherosa Ziehau 		tx_cons = *txr->bnx_tx_considx;
320133a04907SSepherosa Ziehau 		if (txr->bnx_tx_saved_considx != tx_cons)
320233a04907SSepherosa Ziehau 			bnx_txeof(txr, tx_cons);
3203329f9016SSepherosa Ziehau 		lwkt_serialize_exit(&txr->bnx_tx_serialize);
32046c8d8eccSSepherosa Ziehau 	}
32056c8d8eccSSepherosa Ziehau 
32064fa38985SSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, ret->bnx_saved_status_tag << 24);
32076c8d8eccSSepherosa Ziehau }
32086c8d8eccSSepherosa Ziehau 
32096c8d8eccSSepherosa Ziehau static void
bnx_msix_tx_status(void * xtxr)3210695a8586SSepherosa Ziehau bnx_msix_tx_status(void *xtxr)
3211695a8586SSepherosa Ziehau {
3212695a8586SSepherosa Ziehau 	struct bnx_tx_ring *txr = xtxr;
3213695a8586SSepherosa Ziehau 	struct bnx_softc *sc = txr->bnx_sc;
3214695a8586SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
3215695a8586SSepherosa Ziehau 
3216695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3217695a8586SSepherosa Ziehau 
3218695a8586SSepherosa Ziehau 	txr->bnx_saved_status_tag = *txr->bnx_hw_status_tag;
3219695a8586SSepherosa Ziehau 	/*
3220695a8586SSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
3221695a8586SSepherosa Ziehau 	 * before tx_cons and status.
3222695a8586SSepherosa Ziehau 	 */
3223695a8586SSepherosa Ziehau 	cpu_lfence();
3224695a8586SSepherosa Ziehau 
3225695a8586SSepherosa Ziehau 	bnx_handle_status(sc);
3226695a8586SSepherosa Ziehau 
3227695a8586SSepherosa Ziehau 	if (ifp->if_flags & IFF_RUNNING) {
3228695a8586SSepherosa Ziehau 		uint16_t tx_cons;
3229695a8586SSepherosa Ziehau 
3230695a8586SSepherosa Ziehau 		lwkt_serialize_enter(&txr->bnx_tx_serialize);
3231695a8586SSepherosa Ziehau 		tx_cons = *txr->bnx_tx_considx;
3232695a8586SSepherosa Ziehau 		if (txr->bnx_tx_saved_considx != tx_cons)
3233695a8586SSepherosa Ziehau 			bnx_txeof(txr, tx_cons);
3234695a8586SSepherosa Ziehau 		lwkt_serialize_exit(&txr->bnx_tx_serialize);
3235695a8586SSepherosa Ziehau 	}
3236695a8586SSepherosa Ziehau 
3237695a8586SSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, txr->bnx_saved_status_tag << 24);
3238695a8586SSepherosa Ziehau }
3239695a8586SSepherosa Ziehau 
3240695a8586SSepherosa Ziehau static void
bnx_msix_rx(void * xret)3241695a8586SSepherosa Ziehau bnx_msix_rx(void *xret)
3242695a8586SSepherosa Ziehau {
3243695a8586SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = xret;
3244695a8586SSepherosa Ziehau 	uint16_t rx_prod;
3245695a8586SSepherosa Ziehau 
3246695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
3247695a8586SSepherosa Ziehau 
3248695a8586SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
3249695a8586SSepherosa Ziehau 	/*
3250695a8586SSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
3251695a8586SSepherosa Ziehau 	 * before rx_prod.
3252695a8586SSepherosa Ziehau 	 */
3253695a8586SSepherosa Ziehau 	cpu_lfence();
3254695a8586SSepherosa Ziehau 
3255695a8586SSepherosa Ziehau 	rx_prod = *ret->bnx_rx_considx;
3256695a8586SSepherosa Ziehau 	if (ret->bnx_rx_saved_considx != rx_prod)
3257695a8586SSepherosa Ziehau 		bnx_rxeof(ret, rx_prod, -1);
3258695a8586SSepherosa Ziehau 
3259695a8586SSepherosa Ziehau 	bnx_writembx(ret->bnx_sc, ret->bnx_msix_mbx,
3260695a8586SSepherosa Ziehau 	    ret->bnx_saved_status_tag << 24);
3261695a8586SSepherosa Ziehau }
3262695a8586SSepherosa Ziehau 
3263695a8586SSepherosa Ziehau static void
bnx_msix_rxtx(void * xret)3264695a8586SSepherosa Ziehau bnx_msix_rxtx(void *xret)
3265695a8586SSepherosa Ziehau {
3266695a8586SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = xret;
3267695a8586SSepherosa Ziehau 	struct bnx_tx_ring *txr = ret->bnx_txr;
3268695a8586SSepherosa Ziehau 	uint16_t rx_prod, tx_cons;
3269695a8586SSepherosa Ziehau 
3270695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&ret->bnx_rx_ret_serialize);
3271695a8586SSepherosa Ziehau 
3272695a8586SSepherosa Ziehau 	ret->bnx_saved_status_tag = *ret->bnx_hw_status_tag;
3273695a8586SSepherosa Ziehau 	/*
3274695a8586SSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
3275695a8586SSepherosa Ziehau 	 * before rx_prod and tx_cons.
3276695a8586SSepherosa Ziehau 	 */
3277695a8586SSepherosa Ziehau 	cpu_lfence();
3278695a8586SSepherosa Ziehau 
3279695a8586SSepherosa Ziehau 	rx_prod = *ret->bnx_rx_considx;
3280695a8586SSepherosa Ziehau 	if (ret->bnx_rx_saved_considx != rx_prod)
3281695a8586SSepherosa Ziehau 		bnx_rxeof(ret, rx_prod, -1);
3282695a8586SSepherosa Ziehau 
3283695a8586SSepherosa Ziehau 	lwkt_serialize_enter(&txr->bnx_tx_serialize);
3284695a8586SSepherosa Ziehau 	tx_cons = *txr->bnx_tx_considx;
3285695a8586SSepherosa Ziehau 	if (txr->bnx_tx_saved_considx != tx_cons)
3286695a8586SSepherosa Ziehau 		bnx_txeof(txr, tx_cons);
3287695a8586SSepherosa Ziehau 	lwkt_serialize_exit(&txr->bnx_tx_serialize);
3288695a8586SSepherosa Ziehau 
3289695a8586SSepherosa Ziehau 	bnx_writembx(ret->bnx_sc, ret->bnx_msix_mbx,
3290695a8586SSepherosa Ziehau 	    ret->bnx_saved_status_tag << 24);
3291695a8586SSepherosa Ziehau }
3292695a8586SSepherosa Ziehau 
3293695a8586SSepherosa Ziehau static void
bnx_msix_status(void * xsc)3294695a8586SSepherosa Ziehau bnx_msix_status(void *xsc)
3295695a8586SSepherosa Ziehau {
3296695a8586SSepherosa Ziehau 	struct bnx_softc *sc = xsc;
3297695a8586SSepherosa Ziehau 
3298695a8586SSepherosa Ziehau 	ASSERT_SERIALIZED(&sc->bnx_main_serialize);
3299695a8586SSepherosa Ziehau 
3300695a8586SSepherosa Ziehau 	sc->bnx_saved_status_tag = *sc->bnx_hw_status_tag;
3301695a8586SSepherosa Ziehau 	/*
3302695a8586SSepherosa Ziehau 	 * Use a load fence to ensure that status_tag is saved
3303695a8586SSepherosa Ziehau 	 * before status.
3304695a8586SSepherosa Ziehau 	 */
3305695a8586SSepherosa Ziehau 	cpu_lfence();
3306695a8586SSepherosa Ziehau 
3307695a8586SSepherosa Ziehau 	bnx_handle_status(sc);
3308695a8586SSepherosa Ziehau 
3309695a8586SSepherosa Ziehau 	bnx_writembx(sc, BGE_MBX_IRQ0_LO, sc->bnx_saved_status_tag << 24);
3310695a8586SSepherosa Ziehau }
3311695a8586SSepherosa Ziehau 
3312695a8586SSepherosa Ziehau static void
bnx_tick(void * xsc)33136c8d8eccSSepherosa Ziehau bnx_tick(void *xsc)
33146c8d8eccSSepherosa Ziehau {
33156c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
33166c8d8eccSSepherosa Ziehau 
3317329f9016SSepherosa Ziehau 	lwkt_serialize_enter(&sc->bnx_main_serialize);
33186c8d8eccSSepherosa Ziehau 
33196c8d8eccSSepherosa Ziehau 	bnx_stats_update_regs(sc);
33206c8d8eccSSepherosa Ziehau 
33216c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
33226c8d8eccSSepherosa Ziehau 		/*
33236c8d8eccSSepherosa Ziehau 		 * Since in TBI mode auto-polling can't be used we should poll
33246c8d8eccSSepherosa Ziehau 		 * link status manually. Here we register pending link event
33256c8d8eccSSepherosa Ziehau 		 * and trigger interrupt.
33266c8d8eccSSepherosa Ziehau 		 */
33276c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
33286c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
33296c8d8eccSSepherosa Ziehau 	} else if (!sc->bnx_link) {
33306c8d8eccSSepherosa Ziehau 		mii_tick(device_get_softc(sc->bnx_miibus));
33316c8d8eccSSepherosa Ziehau 	}
33326c8d8eccSSepherosa Ziehau 
33337dbaa833SSepherosa Ziehau 	callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
33347dbaa833SSepherosa Ziehau 	    sc->bnx_tick_cpuid);
33356c8d8eccSSepherosa Ziehau 
3336329f9016SSepherosa Ziehau 	lwkt_serialize_exit(&sc->bnx_main_serialize);
33376c8d8eccSSepherosa Ziehau }
33386c8d8eccSSepherosa Ziehau 
33396c8d8eccSSepherosa Ziehau static void
bnx_stats_update_regs(struct bnx_softc * sc)33406c8d8eccSSepherosa Ziehau bnx_stats_update_regs(struct bnx_softc *sc)
33416c8d8eccSSepherosa Ziehau {
33426c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
33436c8d8eccSSepherosa Ziehau 	struct bge_mac_stats_regs stats;
3344695a8586SSepherosa Ziehau 	uint32_t *s, val;
33456c8d8eccSSepherosa Ziehau 	int i;
33466c8d8eccSSepherosa Ziehau 
33476c8d8eccSSepherosa Ziehau 	s = (uint32_t *)&stats;
33486c8d8eccSSepherosa Ziehau 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
33496c8d8eccSSepherosa Ziehau 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
33506c8d8eccSSepherosa Ziehau 		s++;
33516c8d8eccSSepherosa Ziehau 	}
33526c8d8eccSSepherosa Ziehau 
3353d40991efSSepherosa Ziehau 	IFNET_STAT_SET(ifp, collisions,
33546c8d8eccSSepherosa Ziehau 	   (stats.dot3StatsSingleCollisionFrames +
33556c8d8eccSSepherosa Ziehau 	   stats.dot3StatsMultipleCollisionFrames +
33566c8d8eccSSepherosa Ziehau 	   stats.dot3StatsExcessiveCollisions +
3357d40991efSSepherosa Ziehau 	   stats.dot3StatsLateCollisions));
3358695a8586SSepherosa Ziehau 
3359695a8586SSepherosa Ziehau 	val = CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
3360695a8586SSepherosa Ziehau 	sc->bnx_norxbds += val;
33612eaa7169SSepherosa Ziehau 
33622eaa7169SSepherosa Ziehau 	if (sc->bnx_rdma_wa != 0) {
33632eaa7169SSepherosa Ziehau 		if (stats.ifHCOutUcastPkts + stats.ifHCOutMulticastPkts +
33642eaa7169SSepherosa Ziehau 		    stats.ifHCOutBroadcastPkts > BGE_RDMA_NCHAN) {
33652eaa7169SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
33662eaa7169SSepherosa Ziehau 			    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) &
33672eaa7169SSepherosa Ziehau 			    ~sc->bnx_rdma_wa);
33682eaa7169SSepherosa Ziehau 			sc->bnx_rdma_wa = 0;
33692eaa7169SSepherosa Ziehau 			if (bootverbose)
33702eaa7169SSepherosa Ziehau 				if_printf(ifp, "disable RDMA WA\n");
33712eaa7169SSepherosa Ziehau 		}
33722eaa7169SSepherosa Ziehau 	}
33736c8d8eccSSepherosa Ziehau }
33746c8d8eccSSepherosa Ziehau 
33756c8d8eccSSepherosa Ziehau /*
33766c8d8eccSSepherosa Ziehau  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
33776c8d8eccSSepherosa Ziehau  * pointers to descriptors.
33786c8d8eccSSepherosa Ziehau  */
33796c8d8eccSSepherosa Ziehau static int
bnx_encap(struct bnx_tx_ring * txr,struct mbuf ** m_head0,uint32_t * txidx,int * segs_used)338033a04907SSepherosa Ziehau bnx_encap(struct bnx_tx_ring *txr, struct mbuf **m_head0, uint32_t *txidx,
3381c9b7f592SSepherosa Ziehau     int *segs_used)
33826c8d8eccSSepherosa Ziehau {
33836c8d8eccSSepherosa Ziehau 	struct bge_tx_bd *d = NULL;
338466deb1c1SSepherosa Ziehau 	uint16_t csum_flags = 0, vlan_tag = 0, mss = 0;
33856c8d8eccSSepherosa Ziehau 	bus_dma_segment_t segs[BNX_NSEG_NEW];
33866c8d8eccSSepherosa Ziehau 	bus_dmamap_t map;
33876c8d8eccSSepherosa Ziehau 	int error, maxsegs, nsegs, idx, i;
33886c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = *m_head0, *m_new;
33896c8d8eccSSepherosa Ziehau 
339066deb1c1SSepherosa Ziehau 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
339166deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
339266deb1c1SSepherosa Ziehau 		int tso_nsegs;
339366deb1c1SSepherosa Ziehau #endif
339466deb1c1SSepherosa Ziehau 
339533a04907SSepherosa Ziehau 		error = bnx_setup_tso(txr, m_head0, &mss, &csum_flags);
339666deb1c1SSepherosa Ziehau 		if (error)
339766deb1c1SSepherosa Ziehau 			return error;
339866deb1c1SSepherosa Ziehau 		m_head = *m_head0;
339966deb1c1SSepherosa Ziehau 
340066deb1c1SSepherosa Ziehau #ifdef BNX_TSO_DEBUG
3401f0336d39SSepherosa Ziehau 		tso_nsegs = (m_head->m_pkthdr.len /
3402f0336d39SSepherosa Ziehau 		    m_head->m_pkthdr.tso_segsz) - 1;
340366deb1c1SSepherosa Ziehau 		if (tso_nsegs > (BNX_TSO_NSTATS - 1))
340466deb1c1SSepherosa Ziehau 			tso_nsegs = BNX_TSO_NSTATS - 1;
340566deb1c1SSepherosa Ziehau 		else if (tso_nsegs < 0)
340666deb1c1SSepherosa Ziehau 			tso_nsegs = 0;
34075a0c3c3aSSascha Wildner 		txr->bnx_sc->bnx_tsosegs[tso_nsegs]++;
340866deb1c1SSepherosa Ziehau #endif
340966deb1c1SSepherosa Ziehau 	} else if (m_head->m_pkthdr.csum_flags & BNX_CSUM_FEATURES) {
34106c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
34116c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
34126c8d8eccSSepherosa Ziehau 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
34136c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
34146c8d8eccSSepherosa Ziehau 		if (m_head->m_flags & M_LASTFRAG)
34156c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
34166c8d8eccSSepherosa Ziehau 		else if (m_head->m_flags & M_FRAG)
34176c8d8eccSSepherosa Ziehau 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
34186c8d8eccSSepherosa Ziehau 	}
341966deb1c1SSepherosa Ziehau 	if (m_head->m_flags & M_VLANTAG) {
342066deb1c1SSepherosa Ziehau 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
342166deb1c1SSepherosa Ziehau 		vlan_tag = m_head->m_pkthdr.ether_vlantag;
342266deb1c1SSepherosa Ziehau 	}
34236c8d8eccSSepherosa Ziehau 
34246c8d8eccSSepherosa Ziehau 	idx = *txidx;
3425fa4b1067SSepherosa Ziehau 	map = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
34266c8d8eccSSepherosa Ziehau 
3427fa639b88SSepherosa Ziehau 	maxsegs = (BGE_TX_RING_CNT - txr->bnx_tx_cnt) - BNX_NSEG_RSVD;
34286c8d8eccSSepherosa Ziehau 	KASSERT(maxsegs >= BNX_NSEG_SPARE,
34296c8d8eccSSepherosa Ziehau 		("not enough segments %d", maxsegs));
34306c8d8eccSSepherosa Ziehau 
34316c8d8eccSSepherosa Ziehau 	if (maxsegs > BNX_NSEG_NEW)
34326c8d8eccSSepherosa Ziehau 		maxsegs = BNX_NSEG_NEW;
34336c8d8eccSSepherosa Ziehau 
34346c8d8eccSSepherosa Ziehau 	/*
3435edf33009SSepherosa Ziehau 	 * Pad outbound frame to BNX_MIN_FRAMELEN for an unusual reason.
3436edf33009SSepherosa Ziehau 	 * The bge hardware will pad out Tx runts to BNX_MIN_FRAMELEN,
34376c8d8eccSSepherosa Ziehau 	 * but when such padded frames employ the bge IP/TCP checksum
34386c8d8eccSSepherosa Ziehau 	 * offload, the hardware checksum assist gives incorrect results
34396c8d8eccSSepherosa Ziehau 	 * (possibly from incorporating its own padding into the UDP/TCP
34406c8d8eccSSepherosa Ziehau 	 * checksum; who knows).  If we pad such runts with zeros, the
34416c8d8eccSSepherosa Ziehau 	 * onboard checksum comes out correct.
34426c8d8eccSSepherosa Ziehau 	 */
34436c8d8eccSSepherosa Ziehau 	if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) &&
34446c8d8eccSSepherosa Ziehau 	    m_head->m_pkthdr.len < BNX_MIN_FRAMELEN) {
34456c8d8eccSSepherosa Ziehau 		error = m_devpad(m_head, BNX_MIN_FRAMELEN);
34466c8d8eccSSepherosa Ziehau 		if (error)
34476c8d8eccSSepherosa Ziehau 			goto back;
34486c8d8eccSSepherosa Ziehau 	}
34496c8d8eccSSepherosa Ziehau 
345079a64343SSepherosa Ziehau 	if ((txr->bnx_tx_flags & BNX_TX_FLAG_SHORTDMA) &&
345133a04907SSepherosa Ziehau 	    m_head->m_next != NULL) {
34526c8d8eccSSepherosa Ziehau 		m_new = bnx_defrag_shortdma(m_head);
34536c8d8eccSSepherosa Ziehau 		if (m_new == NULL) {
34546c8d8eccSSepherosa Ziehau 			error = ENOBUFS;
34556c8d8eccSSepherosa Ziehau 			goto back;
34566c8d8eccSSepherosa Ziehau 		}
34576c8d8eccSSepherosa Ziehau 		*m_head0 = m_head = m_new;
34586c8d8eccSSepherosa Ziehau 	}
345966deb1c1SSepherosa Ziehau 	if ((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
3460aad4de2bSSepherosa Ziehau 	    (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG) &&
3461aad4de2bSSepherosa Ziehau 	    m_head->m_next != NULL) {
34626c8d8eccSSepherosa Ziehau 		/*
34636c8d8eccSSepherosa Ziehau 		 * Forcefully defragment mbuf chain to overcome hardware
34646c8d8eccSSepherosa Ziehau 		 * limitation which only support a single outstanding
34656c8d8eccSSepherosa Ziehau 		 * DMA read operation.  If it fails, keep moving on using
34666c8d8eccSSepherosa Ziehau 		 * the original mbuf chain.
34676c8d8eccSSepherosa Ziehau 		 */
3468b5523eacSSascha Wildner 		m_new = m_defrag(m_head, M_NOWAIT);
34696c8d8eccSSepherosa Ziehau 		if (m_new != NULL)
34706c8d8eccSSepherosa Ziehau 			*m_head0 = m_head = m_new;
34716c8d8eccSSepherosa Ziehau 	}
34726c8d8eccSSepherosa Ziehau 
347333a04907SSepherosa Ziehau 	error = bus_dmamap_load_mbuf_defrag(txr->bnx_tx_mtag, map,
34746c8d8eccSSepherosa Ziehau 	    m_head0, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
34756c8d8eccSSepherosa Ziehau 	if (error)
34766c8d8eccSSepherosa Ziehau 		goto back;
3477c9b7f592SSepherosa Ziehau 	*segs_used += nsegs;
34786c8d8eccSSepherosa Ziehau 
34796c8d8eccSSepherosa Ziehau 	m_head = *m_head0;
348033a04907SSepherosa Ziehau 	bus_dmamap_sync(txr->bnx_tx_mtag, map, BUS_DMASYNC_PREWRITE);
34816c8d8eccSSepherosa Ziehau 
34826c8d8eccSSepherosa Ziehau 	for (i = 0; ; i++) {
348333a04907SSepherosa Ziehau 		d = &txr->bnx_tx_ring[idx];
34846c8d8eccSSepherosa Ziehau 
34856c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
34866c8d8eccSSepherosa Ziehau 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
34876c8d8eccSSepherosa Ziehau 		d->bge_len = segs[i].ds_len;
34886c8d8eccSSepherosa Ziehau 		d->bge_flags = csum_flags;
348966deb1c1SSepherosa Ziehau 		d->bge_vlan_tag = vlan_tag;
349066deb1c1SSepherosa Ziehau 		d->bge_mss = mss;
34916c8d8eccSSepherosa Ziehau 
34926c8d8eccSSepherosa Ziehau 		if (i == nsegs - 1)
34936c8d8eccSSepherosa Ziehau 			break;
34946c8d8eccSSepherosa Ziehau 		BNX_INC(idx, BGE_TX_RING_CNT);
34956c8d8eccSSepherosa Ziehau 	}
34966c8d8eccSSepherosa Ziehau 	/* Mark the last segment as end of packet... */
34976c8d8eccSSepherosa Ziehau 	d->bge_flags |= BGE_TXBDFLAG_END;
34986c8d8eccSSepherosa Ziehau 
34996c8d8eccSSepherosa Ziehau 	/*
35006c8d8eccSSepherosa Ziehau 	 * Insure that the map for this transmission is placed at
35016c8d8eccSSepherosa Ziehau 	 * the array index of the last descriptor in this chain.
35026c8d8eccSSepherosa Ziehau 	 */
3503fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[*txidx].bnx_tx_dmamap = txr->bnx_tx_buf[idx].bnx_tx_dmamap;
3504fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[idx].bnx_tx_dmamap = map;
3505fa4b1067SSepherosa Ziehau 	txr->bnx_tx_buf[idx].bnx_tx_mbuf = m_head;
3506fa639b88SSepherosa Ziehau 	txr->bnx_tx_cnt += nsegs;
35076c8d8eccSSepherosa Ziehau 
35086c8d8eccSSepherosa Ziehau 	BNX_INC(idx, BGE_TX_RING_CNT);
35096c8d8eccSSepherosa Ziehau 	*txidx = idx;
35106c8d8eccSSepherosa Ziehau back:
35116c8d8eccSSepherosa Ziehau 	if (error) {
35126c8d8eccSSepherosa Ziehau 		m_freem(*m_head0);
35136c8d8eccSSepherosa Ziehau 		*m_head0 = NULL;
35146c8d8eccSSepherosa Ziehau 	}
35156c8d8eccSSepherosa Ziehau 	return error;
35166c8d8eccSSepherosa Ziehau }
35176c8d8eccSSepherosa Ziehau 
35186c8d8eccSSepherosa Ziehau /*
35196c8d8eccSSepherosa Ziehau  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
35206c8d8eccSSepherosa Ziehau  * to the mbuf data regions directly in the transmit descriptors.
35216c8d8eccSSepherosa Ziehau  */
35226c8d8eccSSepherosa Ziehau static void
bnx_start(struct ifnet * ifp,struct ifaltq_subque * ifsq)3523f0a26983SSepherosa Ziehau bnx_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
35246c8d8eccSSepherosa Ziehau {
35253397dea6SSepherosa Ziehau 	struct bnx_tx_ring *txr = ifsq_get_priv(ifsq);
35266c8d8eccSSepherosa Ziehau 	struct mbuf *m_head = NULL;
35276c8d8eccSSepherosa Ziehau 	uint32_t prodidx;
3528c9b7f592SSepherosa Ziehau 	int nsegs = 0;
35296c8d8eccSSepherosa Ziehau 
35303397dea6SSepherosa Ziehau 	KKASSERT(txr->bnx_ifsq == ifsq);
3531329f9016SSepherosa Ziehau 	ASSERT_SERIALIZED(&txr->bnx_tx_serialize);
3532f0a26983SSepherosa Ziehau 
35333397dea6SSepherosa Ziehau 	if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
35346c8d8eccSSepherosa Ziehau 		return;
35356c8d8eccSSepherosa Ziehau 
353633a04907SSepherosa Ziehau 	prodidx = txr->bnx_tx_prodidx;
35376c8d8eccSSepherosa Ziehau 
3538fa4b1067SSepherosa Ziehau 	while (txr->bnx_tx_buf[prodidx].bnx_tx_mbuf == NULL) {
35396c8d8eccSSepherosa Ziehau 		/*
35406c8d8eccSSepherosa Ziehau 		 * Sanity check: avoid coming within BGE_NSEG_RSVD
35416c8d8eccSSepherosa Ziehau 		 * descriptors of the end of the ring.  Also make
35426c8d8eccSSepherosa Ziehau 		 * sure there are BGE_NSEG_SPARE descriptors for
3543a1bd58c9SSepherosa Ziehau 		 * jumbo buffers' or TSO segments' defragmentation.
35446c8d8eccSSepherosa Ziehau 		 */
3545fa639b88SSepherosa Ziehau 		if ((BGE_TX_RING_CNT - txr->bnx_tx_cnt) <
35466c8d8eccSSepherosa Ziehau 		    (BNX_NSEG_RSVD + BNX_NSEG_SPARE)) {
35473397dea6SSepherosa Ziehau 			ifsq_set_oactive(ifsq);
35486c8d8eccSSepherosa Ziehau 			break;
35496c8d8eccSSepherosa Ziehau 		}
35506c8d8eccSSepherosa Ziehau 
3551ac9843a1SSepherosa Ziehau 		m_head = ifsq_dequeue(ifsq);
3552a1bd58c9SSepherosa Ziehau 		if (m_head == NULL)
3553a1bd58c9SSepherosa Ziehau 			break;
3554a1bd58c9SSepherosa Ziehau 
35556c8d8eccSSepherosa Ziehau 		/*
35566c8d8eccSSepherosa Ziehau 		 * Pack the data into the transmit ring. If we
35576c8d8eccSSepherosa Ziehau 		 * don't have room, set the OACTIVE flag and wait
35586c8d8eccSSepherosa Ziehau 		 * for the NIC to drain the ring.
35596c8d8eccSSepherosa Ziehau 		 */
356033a04907SSepherosa Ziehau 		if (bnx_encap(txr, &m_head, &prodidx, &nsegs)) {
35613397dea6SSepherosa Ziehau 			ifsq_set_oactive(ifsq);
3562d40991efSSepherosa Ziehau 			IFNET_STAT_INC(ifp, oerrors, 1);
35636c8d8eccSSepherosa Ziehau 			break;
35646c8d8eccSSepherosa Ziehau 		}
35656c8d8eccSSepherosa Ziehau 
356633a04907SSepherosa Ziehau 		if (nsegs >= txr->bnx_tx_wreg) {
35676c8d8eccSSepherosa Ziehau 			/* Transmit */
35688bd43d5dSSepherosa Ziehau 			bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3569c9b7f592SSepherosa Ziehau 			nsegs = 0;
3570c9b7f592SSepherosa Ziehau 		}
35716c8d8eccSSepherosa Ziehau 
3572c9b7f592SSepherosa Ziehau 		ETHER_BPF_MTAP(ifp, m_head);
35736c8d8eccSSepherosa Ziehau 
35746c8d8eccSSepherosa Ziehau 		/*
35756c8d8eccSSepherosa Ziehau 		 * Set a timeout in case the chip goes out to lunch.
35766c8d8eccSSepherosa Ziehau 		 */
3577e2292763SMatthew Dillon 		ifsq_watchdog_set_count(&txr->bnx_tx_watchdog, 5);
35786c8d8eccSSepherosa Ziehau 	}
35796c8d8eccSSepherosa Ziehau 
3580c9b7f592SSepherosa Ziehau 	if (nsegs > 0) {
3581c9b7f592SSepherosa Ziehau 		/* Transmit */
35828bd43d5dSSepherosa Ziehau 		bnx_writembx(txr->bnx_sc, txr->bnx_tx_mbx, prodidx);
3583c9b7f592SSepherosa Ziehau 	}
358433a04907SSepherosa Ziehau 	txr->bnx_tx_prodidx = prodidx;
3585c9b7f592SSepherosa Ziehau }
3586c9b7f592SSepherosa Ziehau 
35876c8d8eccSSepherosa Ziehau static void
bnx_init(void * xsc)35886c8d8eccSSepherosa Ziehau bnx_init(void *xsc)
35896c8d8eccSSepherosa Ziehau {
35906c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = xsc;
35916c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
35926c8d8eccSSepherosa Ziehau 	uint16_t *m;
35936c8d8eccSSepherosa Ziehau 	uint32_t mode;
359433a04907SSepherosa Ziehau 	int i;
35957dbaa833SSepherosa Ziehau 	boolean_t polling;
35966c8d8eccSSepherosa Ziehau 
3597329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
35986c8d8eccSSepherosa Ziehau 
35996c8d8eccSSepherosa Ziehau 	/* Cancel pending I/O and flush buffers. */
36006c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
36014aa71e73SSepherosa Ziehau 
36024aa71e73SSepherosa Ziehau 	bnx_sig_pre_reset(sc, BNX_RESET_START);
36036c8d8eccSSepherosa Ziehau 	bnx_reset(sc);
36044aa71e73SSepherosa Ziehau 	bnx_sig_post_reset(sc, BNX_RESET_START);
36054aa71e73SSepherosa Ziehau 
36066c8d8eccSSepherosa Ziehau 	bnx_chipinit(sc);
36076c8d8eccSSepherosa Ziehau 
36086c8d8eccSSepherosa Ziehau 	/*
36096c8d8eccSSepherosa Ziehau 	 * Init the various state machines, ring
36106c8d8eccSSepherosa Ziehau 	 * control blocks and firmware.
36116c8d8eccSSepherosa Ziehau 	 */
36126c8d8eccSSepherosa Ziehau 	if (bnx_blockinit(sc)) {
36136c8d8eccSSepherosa Ziehau 		if_printf(ifp, "initialization failure\n");
36146c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
36156c8d8eccSSepherosa Ziehau 		return;
36166c8d8eccSSepherosa Ziehau 	}
36176c8d8eccSSepherosa Ziehau 
36186c8d8eccSSepherosa Ziehau 	/* Specify MTU. */
36196c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
36206c8d8eccSSepherosa Ziehau 	    ETHER_HDR_LEN + ETHER_CRC_LEN + EVL_ENCAPLEN);
36216c8d8eccSSepherosa Ziehau 
36226c8d8eccSSepherosa Ziehau 	/* Load our MAC address. */
36236c8d8eccSSepherosa Ziehau 	m = (uint16_t *)&sc->arpcom.ac_enaddr[0];
36246c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
36256c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
36266c8d8eccSSepherosa Ziehau 
36276c8d8eccSSepherosa Ziehau 	/* Enable or disable promiscuous mode as needed. */
36286c8d8eccSSepherosa Ziehau 	bnx_setpromisc(sc);
36296c8d8eccSSepherosa Ziehau 
36306c8d8eccSSepherosa Ziehau 	/* Program multicast filter. */
36316c8d8eccSSepherosa Ziehau 	bnx_setmulti(sc);
36326c8d8eccSSepherosa Ziehau 
36336c8d8eccSSepherosa Ziehau 	/* Init RX ring. */
3634beedf5beSSepherosa Ziehau 	if (bnx_init_rx_ring_std(&sc->bnx_rx_std_ring)) {
36356c8d8eccSSepherosa Ziehau 		if_printf(ifp, "RX ring initialization failed\n");
36366c8d8eccSSepherosa Ziehau 		bnx_stop(sc);
36376c8d8eccSSepherosa Ziehau 		return;
36386c8d8eccSSepherosa Ziehau 	}
36396c8d8eccSSepherosa Ziehau 
36406c8d8eccSSepherosa Ziehau 	/* Init jumbo RX ring. */
36416c8d8eccSSepherosa Ziehau 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) {
36426c8d8eccSSepherosa Ziehau 		if (bnx_init_rx_ring_jumbo(sc)) {
36436c8d8eccSSepherosa Ziehau 			if_printf(ifp, "Jumbo RX ring initialization failed\n");
36446c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
36456c8d8eccSSepherosa Ziehau 			return;
36466c8d8eccSSepherosa Ziehau 		}
36476c8d8eccSSepherosa Ziehau 	}
36486c8d8eccSSepherosa Ziehau 
36496c8d8eccSSepherosa Ziehau 	/* Init our RX return ring index */
3650841cdf08SSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
3651841cdf08SSepherosa Ziehau 		struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
3652841cdf08SSepherosa Ziehau 
3653841cdf08SSepherosa Ziehau 		ret->bnx_rx_saved_considx = 0;
3654841cdf08SSepherosa Ziehau 		ret->bnx_rx_cnt = 0;
3655841cdf08SSepherosa Ziehau 	}
36566c8d8eccSSepherosa Ziehau 
36576c8d8eccSSepherosa Ziehau 	/* Init TX ring. */
365833a04907SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
365933a04907SSepherosa Ziehau 		bnx_init_tx_ring(&sc->bnx_tx_ring[i]);
36606c8d8eccSSepherosa Ziehau 
36616c8d8eccSSepherosa Ziehau 	/* Enable TX MAC state machine lockup fix. */
36626c8d8eccSSepherosa Ziehau 	mode = CSR_READ_4(sc, BGE_TX_MODE);
36636c8d8eccSSepherosa Ziehau 	mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
3664b96cbbb6SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5720 ||
3665b96cbbb6SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5762) {
36666c8d8eccSSepherosa Ziehau 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
36676c8d8eccSSepherosa Ziehau 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
36686c8d8eccSSepherosa Ziehau 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
36696c8d8eccSSepherosa Ziehau 	}
36706c8d8eccSSepherosa Ziehau 	/* Turn on transmitter */
36716c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
36724aa71e73SSepherosa Ziehau 	DELAY(100);
36736c8d8eccSSepherosa Ziehau 
3674695a8586SSepherosa Ziehau 	/* Initialize RSS */
36759f5082d5SSepherosa Ziehau 	mode = BGE_RXMODE_ENABLE | BGE_RXMODE_IPV6_ENABLE;
36765850b34bSSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5762)
36775850b34bSSepherosa Ziehau 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
3678695a8586SSepherosa Ziehau 	if (BNX_RSS_ENABLED(sc)) {
3679695a8586SSepherosa Ziehau 		bnx_init_rss(sc);
3680695a8586SSepherosa Ziehau 		mode |= BGE_RXMODE_RSS_ENABLE |
3681695a8586SSepherosa Ziehau 		    BGE_RXMODE_RSS_HASH_MASK_BITS |
3682695a8586SSepherosa Ziehau 		    BGE_RXMODE_RSS_IPV4_HASH |
3683695a8586SSepherosa Ziehau 		    BGE_RXMODE_RSS_TCP_IPV4_HASH;
3684695a8586SSepherosa Ziehau 	}
36856c8d8eccSSepherosa Ziehau 	/* Turn on receiver */
3686695a8586SSepherosa Ziehau 	BNX_SETBIT(sc, BGE_RX_MODE, mode);
36874aa71e73SSepherosa Ziehau 	DELAY(10);
36886c8d8eccSSepherosa Ziehau 
36896c8d8eccSSepherosa Ziehau 	/*
36906c8d8eccSSepherosa Ziehau 	 * Set the number of good frames to receive after RX MBUF
36916c8d8eccSSepherosa Ziehau 	 * Low Watermark has been reached.  After the RX MAC receives
36926c8d8eccSSepherosa Ziehau 	 * this number of frames, it will drop subsequent incoming
36936c8d8eccSSepherosa Ziehau 	 * frames until the MBUF High Watermark is reached.
36946c8d8eccSSepherosa Ziehau 	 */
3695bcb29629SSepherosa Ziehau 	if (BNX_IS_57765_FAMILY(sc))
36966c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
36976c8d8eccSSepherosa Ziehau 	else
36986c8d8eccSSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
36996c8d8eccSSepherosa Ziehau 
3700695a8586SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI ||
3701695a8586SSepherosa Ziehau 	    sc->bnx_intr_type == PCI_INTR_TYPE_MSIX) {
37026c8d8eccSSepherosa Ziehau 		if (bootverbose) {
37036c8d8eccSSepherosa Ziehau 			if_printf(ifp, "MSI_MODE: %#x\n",
37046c8d8eccSSepherosa Ziehau 			    CSR_READ_4(sc, BGE_MSI_MODE));
37056c8d8eccSSepherosa Ziehau 		}
37066c8d8eccSSepherosa Ziehau 	}
37076c8d8eccSSepherosa Ziehau 
37086c8d8eccSSepherosa Ziehau 	/* Tell firmware we're alive. */
37096c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37106c8d8eccSSepherosa Ziehau 
37116c8d8eccSSepherosa Ziehau 	/* Enable host interrupts if polling(4) is not enabled. */
37126c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA, 4);
37137dbaa833SSepherosa Ziehau 
37147dbaa833SSepherosa Ziehau 	polling = FALSE;
371539a8d43aSSepherosa Ziehau #ifdef IFPOLL_ENABLE
371639a8d43aSSepherosa Ziehau 	if (ifp->if_flags & IFF_NPOLLING)
37177dbaa833SSepherosa Ziehau 		polling = TRUE;
37187dbaa833SSepherosa Ziehau #endif
37197dbaa833SSepherosa Ziehau 	if (polling)
37206c8d8eccSSepherosa Ziehau 		bnx_disable_intr(sc);
37216c8d8eccSSepherosa Ziehau 	else
37226c8d8eccSSepherosa Ziehau 		bnx_enable_intr(sc);
37237dbaa833SSepherosa Ziehau 	bnx_set_tick_cpuid(sc, polling);
37246c8d8eccSSepherosa Ziehau 
37256c8d8eccSSepherosa Ziehau 	ifp->if_flags |= IFF_RUNNING;
37263397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
37273397dea6SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
37283397dea6SSepherosa Ziehau 
37293397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
37303397dea6SSepherosa Ziehau 		ifsq_watchdog_start(&txr->bnx_tx_watchdog);
37313397dea6SSepherosa Ziehau 	}
37326c8d8eccSSepherosa Ziehau 
37334aa71e73SSepherosa Ziehau 	bnx_ifmedia_upd(ifp);
37344aa71e73SSepherosa Ziehau 
37357dbaa833SSepherosa Ziehau 	callout_reset_bycpu(&sc->bnx_tick_timer, hz, bnx_tick, sc,
37367dbaa833SSepherosa Ziehau 	    sc->bnx_tick_cpuid);
37376c8d8eccSSepherosa Ziehau }
37386c8d8eccSSepherosa Ziehau 
37396c8d8eccSSepherosa Ziehau /*
37406c8d8eccSSepherosa Ziehau  * Set media options.
37416c8d8eccSSepherosa Ziehau  */
37426c8d8eccSSepherosa Ziehau static int
bnx_ifmedia_upd(struct ifnet * ifp)37436c8d8eccSSepherosa Ziehau bnx_ifmedia_upd(struct ifnet *ifp)
37446c8d8eccSSepherosa Ziehau {
37456c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
37466c8d8eccSSepherosa Ziehau 
37476c8d8eccSSepherosa Ziehau 	/* If this is a 1000baseX NIC, enable the TBI port. */
37486c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
37496c8d8eccSSepherosa Ziehau 		struct ifmedia *ifm = &sc->bnx_ifmedia;
37506c8d8eccSSepherosa Ziehau 
37516c8d8eccSSepherosa Ziehau 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
37526c8d8eccSSepherosa Ziehau 			return(EINVAL);
37536c8d8eccSSepherosa Ziehau 
37546c8d8eccSSepherosa Ziehau 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
37556c8d8eccSSepherosa Ziehau 		case IFM_AUTO:
37566c8d8eccSSepherosa Ziehau 			break;
37576c8d8eccSSepherosa Ziehau 
37586c8d8eccSSepherosa Ziehau 		case IFM_1000_SX:
37596c8d8eccSSepherosa Ziehau 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
37606c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
37616c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
37626c8d8eccSSepherosa Ziehau 			} else {
37636c8d8eccSSepherosa Ziehau 				BNX_SETBIT(sc, BGE_MAC_MODE,
37646c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_HALF_DUPLEX);
37656c8d8eccSSepherosa Ziehau 			}
37664aa71e73SSepherosa Ziehau 			DELAY(40);
37676c8d8eccSSepherosa Ziehau 			break;
37686c8d8eccSSepherosa Ziehau 		default:
37696c8d8eccSSepherosa Ziehau 			return(EINVAL);
37706c8d8eccSSepherosa Ziehau 		}
37716c8d8eccSSepherosa Ziehau 	} else {
37726c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
37736c8d8eccSSepherosa Ziehau 
37746c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt++;
37756c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
37766c8d8eccSSepherosa Ziehau 		if (mii->mii_instance) {
37776c8d8eccSSepherosa Ziehau 			struct mii_softc *miisc;
37786c8d8eccSSepherosa Ziehau 
37796c8d8eccSSepherosa Ziehau 			LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
37806c8d8eccSSepherosa Ziehau 				mii_phy_reset(miisc);
37816c8d8eccSSepherosa Ziehau 		}
37826c8d8eccSSepherosa Ziehau 		mii_mediachg(mii);
37836c8d8eccSSepherosa Ziehau 
37846c8d8eccSSepherosa Ziehau 		/*
37856c8d8eccSSepherosa Ziehau 		 * Force an interrupt so that we will call bnx_link_upd
37866c8d8eccSSepherosa Ziehau 		 * if needed and clear any pending link state attention.
37876c8d8eccSSepherosa Ziehau 		 * Without this we are not getting any further interrupts
37886c8d8eccSSepherosa Ziehau 		 * for link state changes and thus will not UP the link and
37896c8d8eccSSepherosa Ziehau 		 * not be able to send in bnx_start.  The only way to get
37906c8d8eccSSepherosa Ziehau 		 * things working was to receive a packet and get an RX
37916c8d8eccSSepherosa Ziehau 		 * intr.
37926c8d8eccSSepherosa Ziehau 		 *
37936c8d8eccSSepherosa Ziehau 		 * bnx_tick should help for fiber cards and we might not
37946c8d8eccSSepherosa Ziehau 		 * need to do this here if BNX_FLAG_TBI is set but as
37956c8d8eccSSepherosa Ziehau 		 * we poll for fiber anyway it should not harm.
37966c8d8eccSSepherosa Ziehau 		 */
37976c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
37986c8d8eccSSepherosa Ziehau 	}
37996c8d8eccSSepherosa Ziehau 	return(0);
38006c8d8eccSSepherosa Ziehau }
38016c8d8eccSSepherosa Ziehau 
38026c8d8eccSSepherosa Ziehau /*
38036c8d8eccSSepherosa Ziehau  * Report current media status.
38046c8d8eccSSepherosa Ziehau  */
38056c8d8eccSSepherosa Ziehau static void
bnx_ifmedia_sts(struct ifnet * ifp,struct ifmediareq * ifmr)38066c8d8eccSSepherosa Ziehau bnx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
38076c8d8eccSSepherosa Ziehau {
38086c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
38096c8d8eccSSepherosa Ziehau 
38104aa71e73SSepherosa Ziehau 	if ((ifp->if_flags & IFF_RUNNING) == 0)
38114aa71e73SSepherosa Ziehau 		return;
38124aa71e73SSepherosa Ziehau 
38136c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_TBI) {
38146c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = IFM_AVALID;
38156c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = IFM_ETHER;
38166c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_STS) &
38176c8d8eccSSepherosa Ziehau 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
38186c8d8eccSSepherosa Ziehau 			ifmr->ifm_status |= IFM_ACTIVE;
38196c8d8eccSSepherosa Ziehau 		} else {
38206c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_NONE;
38216c8d8eccSSepherosa Ziehau 			return;
38226c8d8eccSSepherosa Ziehau 		}
38236c8d8eccSSepherosa Ziehau 
38246c8d8eccSSepherosa Ziehau 		ifmr->ifm_active |= IFM_1000_SX;
38256c8d8eccSSepherosa Ziehau 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
38266c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_HDX;
38276c8d8eccSSepherosa Ziehau 		else
38286c8d8eccSSepherosa Ziehau 			ifmr->ifm_active |= IFM_FDX;
38296c8d8eccSSepherosa Ziehau 	} else {
38306c8d8eccSSepherosa Ziehau 		struct mii_data *mii = device_get_softc(sc->bnx_miibus);
38316c8d8eccSSepherosa Ziehau 
38326c8d8eccSSepherosa Ziehau 		mii_pollstat(mii);
38336c8d8eccSSepherosa Ziehau 		ifmr->ifm_active = mii->mii_media_active;
38346c8d8eccSSepherosa Ziehau 		ifmr->ifm_status = mii->mii_media_status;
38356c8d8eccSSepherosa Ziehau 	}
38366c8d8eccSSepherosa Ziehau }
38376c8d8eccSSepherosa Ziehau 
38386c8d8eccSSepherosa Ziehau static int
bnx_ioctl(struct ifnet * ifp,u_long command,caddr_t data,struct ucred * cr)38396c8d8eccSSepherosa Ziehau bnx_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
38406c8d8eccSSepherosa Ziehau {
38416c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
38426c8d8eccSSepherosa Ziehau 	struct ifreq *ifr = (struct ifreq *)data;
38436c8d8eccSSepherosa Ziehau 	int mask, error = 0;
38446c8d8eccSSepherosa Ziehau 
3845329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
38466c8d8eccSSepherosa Ziehau 
38476c8d8eccSSepherosa Ziehau 	switch (command) {
38486c8d8eccSSepherosa Ziehau 	case SIOCSIFMTU:
38496c8d8eccSSepherosa Ziehau 		if ((!BNX_IS_JUMBO_CAPABLE(sc) && ifr->ifr_mtu > ETHERMTU) ||
38506c8d8eccSSepherosa Ziehau 		    (BNX_IS_JUMBO_CAPABLE(sc) &&
38516c8d8eccSSepherosa Ziehau 		     ifr->ifr_mtu > BNX_JUMBO_MTU)) {
38526c8d8eccSSepherosa Ziehau 			error = EINVAL;
38536c8d8eccSSepherosa Ziehau 		} else if (ifp->if_mtu != ifr->ifr_mtu) {
38546c8d8eccSSepherosa Ziehau 			ifp->if_mtu = ifr->ifr_mtu;
38556c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING)
38566c8d8eccSSepherosa Ziehau 				bnx_init(sc);
38576c8d8eccSSepherosa Ziehau 		}
38586c8d8eccSSepherosa Ziehau 		break;
38596c8d8eccSSepherosa Ziehau 	case SIOCSIFFLAGS:
38606c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_UP) {
38616c8d8eccSSepherosa Ziehau 			if (ifp->if_flags & IFF_RUNNING) {
38626c8d8eccSSepherosa Ziehau 				mask = ifp->if_flags ^ sc->bnx_if_flags;
38636c8d8eccSSepherosa Ziehau 
38646c8d8eccSSepherosa Ziehau 				/*
38656c8d8eccSSepherosa Ziehau 				 * If only the state of the PROMISC flag
38666c8d8eccSSepherosa Ziehau 				 * changed, then just use the 'set promisc
38676c8d8eccSSepherosa Ziehau 				 * mode' command instead of reinitializing
38686c8d8eccSSepherosa Ziehau 				 * the entire NIC. Doing a full re-init
38696c8d8eccSSepherosa Ziehau 				 * means reloading the firmware and waiting
38706c8d8eccSSepherosa Ziehau 				 * for it to start up, which may take a
38716c8d8eccSSepherosa Ziehau 				 * second or two.  Similarly for ALLMULTI.
38726c8d8eccSSepherosa Ziehau 				 */
38736c8d8eccSSepherosa Ziehau 				if (mask & IFF_PROMISC)
38746c8d8eccSSepherosa Ziehau 					bnx_setpromisc(sc);
38756c8d8eccSSepherosa Ziehau 				if (mask & IFF_ALLMULTI)
38766c8d8eccSSepherosa Ziehau 					bnx_setmulti(sc);
38776c8d8eccSSepherosa Ziehau 			} else {
38786c8d8eccSSepherosa Ziehau 				bnx_init(sc);
38796c8d8eccSSepherosa Ziehau 			}
38806c8d8eccSSepherosa Ziehau 		} else if (ifp->if_flags & IFF_RUNNING) {
38816c8d8eccSSepherosa Ziehau 			bnx_stop(sc);
38826c8d8eccSSepherosa Ziehau 		}
38836c8d8eccSSepherosa Ziehau 		sc->bnx_if_flags = ifp->if_flags;
38846c8d8eccSSepherosa Ziehau 		break;
38856c8d8eccSSepherosa Ziehau 	case SIOCADDMULTI:
38866c8d8eccSSepherosa Ziehau 	case SIOCDELMULTI:
38876c8d8eccSSepherosa Ziehau 		if (ifp->if_flags & IFF_RUNNING)
38886c8d8eccSSepherosa Ziehau 			bnx_setmulti(sc);
38896c8d8eccSSepherosa Ziehau 		break;
38906c8d8eccSSepherosa Ziehau 	case SIOCSIFMEDIA:
38916c8d8eccSSepherosa Ziehau 	case SIOCGIFMEDIA:
38926c8d8eccSSepherosa Ziehau 		if (sc->bnx_flags & BNX_FLAG_TBI) {
38936c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
38946c8d8eccSSepherosa Ziehau 			    &sc->bnx_ifmedia, command);
38956c8d8eccSSepherosa Ziehau 		} else {
38966c8d8eccSSepherosa Ziehau 			struct mii_data *mii;
38976c8d8eccSSepherosa Ziehau 
38986c8d8eccSSepherosa Ziehau 			mii = device_get_softc(sc->bnx_miibus);
38996c8d8eccSSepherosa Ziehau 			error = ifmedia_ioctl(ifp, ifr,
39006c8d8eccSSepherosa Ziehau 					      &mii->mii_media, command);
39016c8d8eccSSepherosa Ziehau 		}
39026c8d8eccSSepherosa Ziehau 		break;
39036c8d8eccSSepherosa Ziehau         case SIOCSIFCAP:
39046c8d8eccSSepherosa Ziehau 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
39056c8d8eccSSepherosa Ziehau 		if (mask & IFCAP_HWCSUM) {
39066c8d8eccSSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
390766deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TXCSUM)
390866deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= BNX_CSUM_FEATURES;
39096c8d8eccSSepherosa Ziehau 			else
391066deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~BNX_CSUM_FEATURES;
391166deb1c1SSepherosa Ziehau 		}
391266deb1c1SSepherosa Ziehau 		if (mask & IFCAP_TSO) {
391366deb1c1SSepherosa Ziehau 			ifp->if_capenable ^= (mask & IFCAP_TSO);
391466deb1c1SSepherosa Ziehau 			if (ifp->if_capenable & IFCAP_TSO)
391566deb1c1SSepherosa Ziehau 				ifp->if_hwassist |= CSUM_TSO;
391666deb1c1SSepherosa Ziehau 			else
391766deb1c1SSepherosa Ziehau 				ifp->if_hwassist &= ~CSUM_TSO;
39186c8d8eccSSepherosa Ziehau 		}
3919b19ddf7eSSepherosa Ziehau 		if (mask & IFCAP_RSS)
3920b19ddf7eSSepherosa Ziehau 			ifp->if_capenable ^= IFCAP_RSS;
39216c8d8eccSSepherosa Ziehau 		break;
39226c8d8eccSSepherosa Ziehau 	default:
39236c8d8eccSSepherosa Ziehau 		error = ether_ioctl(ifp, command, data);
39246c8d8eccSSepherosa Ziehau 		break;
39256c8d8eccSSepherosa Ziehau 	}
39266c8d8eccSSepherosa Ziehau 	return error;
39276c8d8eccSSepherosa Ziehau }
39286c8d8eccSSepherosa Ziehau 
39296c8d8eccSSepherosa Ziehau static void
bnx_watchdog(struct ifaltq_subque * ifsq)39303397dea6SSepherosa Ziehau bnx_watchdog(struct ifaltq_subque *ifsq)
39316c8d8eccSSepherosa Ziehau {
39323397dea6SSepherosa Ziehau 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
39336c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
39343397dea6SSepherosa Ziehau 	int i;
39353397dea6SSepherosa Ziehau 
39363397dea6SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
39376c8d8eccSSepherosa Ziehau 
39386c8d8eccSSepherosa Ziehau 	if_printf(ifp, "watchdog timeout -- resetting\n");
39396c8d8eccSSepherosa Ziehau 
39406c8d8eccSSepherosa Ziehau 	bnx_init(sc);
39416c8d8eccSSepherosa Ziehau 
3942d40991efSSepherosa Ziehau 	IFNET_STAT_INC(ifp, oerrors, 1);
39436c8d8eccSSepherosa Ziehau 
39443397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
39453397dea6SSepherosa Ziehau 		ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
39466c8d8eccSSepherosa Ziehau }
39476c8d8eccSSepherosa Ziehau 
39486c8d8eccSSepherosa Ziehau /*
39496c8d8eccSSepherosa Ziehau  * Stop the adapter and free any mbufs allocated to the
39506c8d8eccSSepherosa Ziehau  * RX and TX lists.
39516c8d8eccSSepherosa Ziehau  */
39526c8d8eccSSepherosa Ziehau static void
bnx_stop(struct bnx_softc * sc)39536c8d8eccSSepherosa Ziehau bnx_stop(struct bnx_softc *sc)
39546c8d8eccSSepherosa Ziehau {
39556c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
395633a04907SSepherosa Ziehau 	int i;
39576c8d8eccSSepherosa Ziehau 
3958329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
39596c8d8eccSSepherosa Ziehau 
39607dbaa833SSepherosa Ziehau 	callout_stop(&sc->bnx_tick_timer);
39616c8d8eccSSepherosa Ziehau 
39624aa71e73SSepherosa Ziehau 	/* Disable host interrupts. */
39634aa71e73SSepherosa Ziehau 	bnx_disable_intr(sc);
39644aa71e73SSepherosa Ziehau 
39654aa71e73SSepherosa Ziehau 	/*
39664aa71e73SSepherosa Ziehau 	 * Tell firmware we're shutting down.
39674aa71e73SSepherosa Ziehau 	 */
39684aa71e73SSepherosa Ziehau 	bnx_sig_pre_reset(sc, BNX_RESET_SHUTDOWN);
39694aa71e73SSepherosa Ziehau 
39706c8d8eccSSepherosa Ziehau 	/*
39716c8d8eccSSepherosa Ziehau 	 * Disable all of the receiver blocks
39726c8d8eccSSepherosa Ziehau 	 */
39736c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
39746c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
39756c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
39766c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
39776c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
39786c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
39796c8d8eccSSepherosa Ziehau 
39806c8d8eccSSepherosa Ziehau 	/*
39816c8d8eccSSepherosa Ziehau 	 * Disable all of the transmit blocks
39826c8d8eccSSepherosa Ziehau 	 */
39836c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
39846c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
39856c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
39866c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
39876c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
39886c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
39896c8d8eccSSepherosa Ziehau 
39906c8d8eccSSepherosa Ziehau 	/*
39916c8d8eccSSepherosa Ziehau 	 * Shut down all of the memory managers and related
39926c8d8eccSSepherosa Ziehau 	 * state machines.
39936c8d8eccSSepherosa Ziehau 	 */
39946c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
39956c8d8eccSSepherosa Ziehau 	bnx_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
39966c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
39976c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
39986c8d8eccSSepherosa Ziehau 
39994aa71e73SSepherosa Ziehau 	bnx_reset(sc);
40004aa71e73SSepherosa Ziehau 	bnx_sig_post_reset(sc, BNX_RESET_SHUTDOWN);
40016c8d8eccSSepherosa Ziehau 
40026c8d8eccSSepherosa Ziehau 	/*
40036c8d8eccSSepherosa Ziehau 	 * Tell firmware we're shutting down.
40046c8d8eccSSepherosa Ziehau 	 */
40056c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
40066c8d8eccSSepherosa Ziehau 
40076c8d8eccSSepherosa Ziehau 	/* Free the RX lists. */
4008beedf5beSSepherosa Ziehau 	bnx_free_rx_ring_std(&sc->bnx_rx_std_ring);
40096c8d8eccSSepherosa Ziehau 
40106c8d8eccSSepherosa Ziehau 	/* Free jumbo RX list. */
40116c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
40126c8d8eccSSepherosa Ziehau 		bnx_free_rx_ring_jumbo(sc);
40136c8d8eccSSepherosa Ziehau 
40146c8d8eccSSepherosa Ziehau 	/* Free TX buffers. */
40154fa38985SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
40164fa38985SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
40176c8d8eccSSepherosa Ziehau 
40184fa38985SSepherosa Ziehau 		txr->bnx_saved_status_tag = 0;
40194fa38985SSepherosa Ziehau 		bnx_free_tx_ring(txr);
40204fa38985SSepherosa Ziehau 	}
40214fa38985SSepherosa Ziehau 
40224fa38985SSepherosa Ziehau 	/* Clear saved status tag */
40234fa38985SSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i)
40244fa38985SSepherosa Ziehau 		sc->bnx_rx_ret_ring[i].bnx_saved_status_tag = 0;
40254fa38985SSepherosa Ziehau 
40266c8d8eccSSepherosa Ziehau 	sc->bnx_link = 0;
40276c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
40286c8d8eccSSepherosa Ziehau 
40299ed293e0SSepherosa Ziehau 	ifp->if_flags &= ~IFF_RUNNING;
40303397dea6SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
40313397dea6SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
40323397dea6SSepherosa Ziehau 
40333397dea6SSepherosa Ziehau 		ifsq_clr_oactive(txr->bnx_ifsq);
40343397dea6SSepherosa Ziehau 		ifsq_watchdog_stop(&txr->bnx_tx_watchdog);
40353397dea6SSepherosa Ziehau 	}
40366c8d8eccSSepherosa Ziehau }
40376c8d8eccSSepherosa Ziehau 
40386c8d8eccSSepherosa Ziehau /*
40396c8d8eccSSepherosa Ziehau  * Stop all chip I/O so that the kernel's probe routines don't
40406c8d8eccSSepherosa Ziehau  * get confused by errant DMAs when rebooting.
40416c8d8eccSSepherosa Ziehau  */
40426c8d8eccSSepherosa Ziehau static void
bnx_shutdown(device_t dev)40436c8d8eccSSepherosa Ziehau bnx_shutdown(device_t dev)
40446c8d8eccSSepherosa Ziehau {
40456c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
40466c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40476c8d8eccSSepherosa Ziehau 
4048329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
40496c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
4050329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
40516c8d8eccSSepherosa Ziehau }
40526c8d8eccSSepherosa Ziehau 
40536c8d8eccSSepherosa Ziehau static int
bnx_suspend(device_t dev)40546c8d8eccSSepherosa Ziehau bnx_suspend(device_t dev)
40556c8d8eccSSepherosa Ziehau {
40566c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
40576c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40586c8d8eccSSepherosa Ziehau 
4059329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
40606c8d8eccSSepherosa Ziehau 	bnx_stop(sc);
4061329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
40626c8d8eccSSepherosa Ziehau 
40636c8d8eccSSepherosa Ziehau 	return 0;
40646c8d8eccSSepherosa Ziehau }
40656c8d8eccSSepherosa Ziehau 
40666c8d8eccSSepherosa Ziehau static int
bnx_resume(device_t dev)40676c8d8eccSSepherosa Ziehau bnx_resume(device_t dev)
40686c8d8eccSSepherosa Ziehau {
40696c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
40706c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40716c8d8eccSSepherosa Ziehau 
4072329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
40736c8d8eccSSepherosa Ziehau 
40746c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_UP) {
40753397dea6SSepherosa Ziehau 		int i;
40766c8d8eccSSepherosa Ziehau 
40773397dea6SSepherosa Ziehau 		bnx_init(sc);
40783397dea6SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
40793397dea6SSepherosa Ziehau 			ifsq_devstart_sched(sc->bnx_tx_ring[i].bnx_ifsq);
40806c8d8eccSSepherosa Ziehau 	}
40816c8d8eccSSepherosa Ziehau 
4082329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
40836c8d8eccSSepherosa Ziehau 
40846c8d8eccSSepherosa Ziehau 	return 0;
40856c8d8eccSSepherosa Ziehau }
40866c8d8eccSSepherosa Ziehau 
40876c8d8eccSSepherosa Ziehau static void
bnx_setpromisc(struct bnx_softc * sc)40886c8d8eccSSepherosa Ziehau bnx_setpromisc(struct bnx_softc *sc)
40896c8d8eccSSepherosa Ziehau {
40906c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
40916c8d8eccSSepherosa Ziehau 
40926c8d8eccSSepherosa Ziehau 	if (ifp->if_flags & IFF_PROMISC)
40936c8d8eccSSepherosa Ziehau 		BNX_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
40946c8d8eccSSepherosa Ziehau 	else
40956c8d8eccSSepherosa Ziehau 		BNX_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
40966c8d8eccSSepherosa Ziehau }
40976c8d8eccSSepherosa Ziehau 
40986c8d8eccSSepherosa Ziehau static void
bnx_dma_free(struct bnx_softc * sc)40996c8d8eccSSepherosa Ziehau bnx_dma_free(struct bnx_softc *sc)
41006c8d8eccSSepherosa Ziehau {
4101beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
41026c8d8eccSSepherosa Ziehau 	int i;
41036c8d8eccSSepherosa Ziehau 
4104beedf5beSSepherosa Ziehau 	/* Destroy RX return rings */
4105beedf5beSSepherosa Ziehau 	if (sc->bnx_rx_ret_ring != NULL) {
4106beedf5beSSepherosa Ziehau 		for (i = 0; i < sc->bnx_rx_retcnt; ++i)
4107beedf5beSSepherosa Ziehau 			bnx_destroy_rx_ret_ring(&sc->bnx_rx_ret_ring[i]);
4108beedf5beSSepherosa Ziehau 		kfree(sc->bnx_rx_ret_ring, M_DEVBUF);
4109beedf5beSSepherosa Ziehau 	}
4110beedf5beSSepherosa Ziehau 
41116c8d8eccSSepherosa Ziehau 	/* Destroy RX mbuf DMA stuffs. */
4112beedf5beSSepherosa Ziehau 	if (std->bnx_rx_mtag != NULL) {
41136c8d8eccSSepherosa Ziehau 		for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
4114beedf5beSSepherosa Ziehau 			KKASSERT(std->bnx_rx_std_buf[i].bnx_rx_mbuf == NULL);
4115beedf5beSSepherosa Ziehau 			bus_dmamap_destroy(std->bnx_rx_mtag,
4116beedf5beSSepherosa Ziehau 			    std->bnx_rx_std_buf[i].bnx_rx_dmamap);
41176c8d8eccSSepherosa Ziehau 		}
4118beedf5beSSepherosa Ziehau 		bus_dma_tag_destroy(std->bnx_rx_mtag);
41196c8d8eccSSepherosa Ziehau 	}
41206c8d8eccSSepherosa Ziehau 
4121beedf5beSSepherosa Ziehau 	/* Destroy standard RX ring */
4122beedf5beSSepherosa Ziehau 	bnx_dma_block_free(std->bnx_rx_std_ring_tag,
4123beedf5beSSepherosa Ziehau 	    std->bnx_rx_std_ring_map, std->bnx_rx_std_ring);
4124beedf5beSSepherosa Ziehau 
412533a04907SSepherosa Ziehau 	/* Destroy TX rings */
412633a04907SSepherosa Ziehau 	if (sc->bnx_tx_ring != NULL) {
412733a04907SSepherosa Ziehau 		for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
412833a04907SSepherosa Ziehau 			bnx_destroy_tx_ring(&sc->bnx_tx_ring[i]);
412933a04907SSepherosa Ziehau 		kfree(sc->bnx_tx_ring, M_DEVBUF);
41306c8d8eccSSepherosa Ziehau 	}
41316c8d8eccSSepherosa Ziehau 
41326c8d8eccSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc))
41336c8d8eccSSepherosa Ziehau 		bnx_free_jumbo_mem(sc);
41346c8d8eccSSepherosa Ziehau 
41350a806e3aSSepherosa Ziehau 	/* Destroy status blocks */
41360a806e3aSSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
41370a806e3aSSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
41380a806e3aSSepherosa Ziehau 
41390a806e3aSSepherosa Ziehau 		bnx_dma_block_free(intr->bnx_status_tag,
41400a806e3aSSepherosa Ziehau 		    intr->bnx_status_map, intr->bnx_status_block);
41410a806e3aSSepherosa Ziehau 	}
41426c8d8eccSSepherosa Ziehau 
41436c8d8eccSSepherosa Ziehau 	/* Destroy the parent tag */
41446c8d8eccSSepherosa Ziehau 	if (sc->bnx_cdata.bnx_parent_tag != NULL)
41456c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(sc->bnx_cdata.bnx_parent_tag);
41466c8d8eccSSepherosa Ziehau }
41476c8d8eccSSepherosa Ziehau 
41486c8d8eccSSepherosa Ziehau static int
bnx_dma_alloc(device_t dev)4149beedf5beSSepherosa Ziehau bnx_dma_alloc(device_t dev)
41506c8d8eccSSepherosa Ziehau {
4151beedf5beSSepherosa Ziehau 	struct bnx_softc *sc = device_get_softc(dev);
4152beedf5beSSepherosa Ziehau 	struct bnx_rx_std_ring *std = &sc->bnx_rx_std_ring;
4153ac2936fdSSepherosa Ziehau 	int i, error, mbx;
41546c8d8eccSSepherosa Ziehau 
41556c8d8eccSSepherosa Ziehau 	/*
41566c8d8eccSSepherosa Ziehau 	 * Allocate the parent bus DMA tag appropriate for PCI.
41576c8d8eccSSepherosa Ziehau 	 *
41586c8d8eccSSepherosa Ziehau 	 * All of the NetExtreme/NetLink controllers have 4GB boundary
41596c8d8eccSSepherosa Ziehau 	 * DMA bug.
41606c8d8eccSSepherosa Ziehau 	 * Whenever an address crosses a multiple of the 4GB boundary
41616c8d8eccSSepherosa Ziehau 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
41626c8d8eccSSepherosa Ziehau 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
41636c8d8eccSSepherosa Ziehau 	 * state machine will lockup and cause the device to hang.
41646c8d8eccSSepherosa Ziehau 	 */
41656c8d8eccSSepherosa Ziehau 	error = bus_dma_tag_create(NULL, 1, BGE_DMA_BOUNDARY_4G,
4166*030b0c8cSMichael Neumann 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
4167beedf5beSSepherosa Ziehau 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
41686c8d8eccSSepherosa Ziehau 	    0, &sc->bnx_cdata.bnx_parent_tag);
41696c8d8eccSSepherosa Ziehau 	if (error) {
4170beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create parent DMA tag\n");
41716c8d8eccSSepherosa Ziehau 		return error;
41726c8d8eccSSepherosa Ziehau 	}
41736c8d8eccSSepherosa Ziehau 
41746c8d8eccSSepherosa Ziehau 	/*
41750a806e3aSSepherosa Ziehau 	 * Create DMA stuffs for status blocks.
41766c8d8eccSSepherosa Ziehau 	 */
41770a806e3aSSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
41780a806e3aSSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
41790a806e3aSSepherosa Ziehau 
4180695a8586SSepherosa Ziehau 		error = bnx_dma_block_alloc(sc,
4181695a8586SSepherosa Ziehau 		    __VM_CACHELINE_ALIGN(BGE_STATUS_BLK_SZ),
41820a806e3aSSepherosa Ziehau 		    &intr->bnx_status_tag, &intr->bnx_status_map,
41830a806e3aSSepherosa Ziehau 		    (void *)&intr->bnx_status_block,
41840a806e3aSSepherosa Ziehau 		    &intr->bnx_status_block_paddr);
41856c8d8eccSSepherosa Ziehau 		if (error) {
41860a806e3aSSepherosa Ziehau 			device_printf(dev,
41870a806e3aSSepherosa Ziehau 			    "could not create %dth status block\n", i);
41886c8d8eccSSepherosa Ziehau 			return error;
41896c8d8eccSSepherosa Ziehau 		}
41900a806e3aSSepherosa Ziehau 	}
4191695a8586SSepherosa Ziehau 	sc->bnx_hw_status = &sc->bnx_intr_data[0].bnx_status_block->bge_status;
4192695a8586SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_STATUS_HASTAG) {
4193695a8586SSepherosa Ziehau 		sc->bnx_hw_status_tag =
4194695a8586SSepherosa Ziehau 		    &sc->bnx_intr_data[0].bnx_status_block->bge_status_tag;
4195695a8586SSepherosa Ziehau 	}
41966c8d8eccSSepherosa Ziehau 
4197beedf5beSSepherosa Ziehau 	/*
4198beedf5beSSepherosa Ziehau 	 * Create DMA tag and maps for RX mbufs.
4199beedf5beSSepherosa Ziehau 	 */
4200beedf5beSSepherosa Ziehau 	std->bnx_sc = sc;
4201329f9016SSepherosa Ziehau 	lwkt_serialize_init(&std->bnx_rx_std_serialize);
4202beedf5beSSepherosa Ziehau 	error = bus_dma_tag_create(sc->bnx_cdata.bnx_parent_tag, 1, 0,
4203beedf5beSSepherosa Ziehau 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
4204*030b0c8cSMichael Neumann 	    MCLBYTES, 1, MCLBYTES,
4205beedf5beSSepherosa Ziehau 	    BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK, &std->bnx_rx_mtag);
4206beedf5beSSepherosa Ziehau 	if (error) {
4207beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create RX mbuf DMA tag\n");
4208beedf5beSSepherosa Ziehau 		return error;
4209beedf5beSSepherosa Ziehau 	}
4210beedf5beSSepherosa Ziehau 
4211beedf5beSSepherosa Ziehau 	for (i = 0; i < BGE_STD_RX_RING_CNT; ++i) {
4212beedf5beSSepherosa Ziehau 		error = bus_dmamap_create(std->bnx_rx_mtag, BUS_DMA_WAITOK,
4213beedf5beSSepherosa Ziehau 		    &std->bnx_rx_std_buf[i].bnx_rx_dmamap);
4214beedf5beSSepherosa Ziehau 		if (error) {
4215beedf5beSSepherosa Ziehau 			int j;
4216beedf5beSSepherosa Ziehau 
4217beedf5beSSepherosa Ziehau 			for (j = 0; j < i; ++j) {
4218beedf5beSSepherosa Ziehau 				bus_dmamap_destroy(std->bnx_rx_mtag,
4219beedf5beSSepherosa Ziehau 				    std->bnx_rx_std_buf[j].bnx_rx_dmamap);
4220beedf5beSSepherosa Ziehau 			}
4221beedf5beSSepherosa Ziehau 			bus_dma_tag_destroy(std->bnx_rx_mtag);
4222beedf5beSSepherosa Ziehau 			std->bnx_rx_mtag = NULL;
4223beedf5beSSepherosa Ziehau 
4224beedf5beSSepherosa Ziehau 			device_printf(dev,
4225beedf5beSSepherosa Ziehau 			    "could not create %dth RX mbuf DMA map\n", i);
4226beedf5beSSepherosa Ziehau 			return error;
4227beedf5beSSepherosa Ziehau 		}
4228beedf5beSSepherosa Ziehau 	}
4229beedf5beSSepherosa Ziehau 
4230beedf5beSSepherosa Ziehau 	/*
4231beedf5beSSepherosa Ziehau 	 * Create DMA stuffs for standard RX ring.
4232beedf5beSSepherosa Ziehau 	 */
4233beedf5beSSepherosa Ziehau 	error = bnx_dma_block_alloc(sc, BGE_STD_RX_RING_SZ,
4234beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_tag,
4235beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_map,
4236beedf5beSSepherosa Ziehau 	    (void *)&std->bnx_rx_std_ring,
4237beedf5beSSepherosa Ziehau 	    &std->bnx_rx_std_ring_paddr);
4238beedf5beSSepherosa Ziehau 	if (error) {
4239beedf5beSSepherosa Ziehau 		device_printf(dev, "could not create std RX ring\n");
4240beedf5beSSepherosa Ziehau 		return error;
4241beedf5beSSepherosa Ziehau 	}
4242beedf5beSSepherosa Ziehau 
4243beedf5beSSepherosa Ziehau 	/*
4244beedf5beSSepherosa Ziehau 	 * Create RX return rings
4245beedf5beSSepherosa Ziehau 	 */
4246ac2936fdSSepherosa Ziehau 	mbx = BGE_MBX_RX_CONS0_LO;
424762938642SMatthew Dillon 	sc->bnx_rx_ret_ring =
424862938642SMatthew Dillon 		kmalloc(sizeof(struct bnx_rx_ret_ring) * sc->bnx_rx_retcnt,
424962938642SMatthew Dillon 			M_DEVBUF,
425062938642SMatthew Dillon 			M_WAITOK | M_ZERO | M_CACHEALIGN);
4251beedf5beSSepherosa Ziehau 	for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4252beedf5beSSepherosa Ziehau 		struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[i];
42530a806e3aSSepherosa Ziehau 		struct bnx_intr_data *intr;
4254beedf5beSSepherosa Ziehau 
4255beedf5beSSepherosa Ziehau 		ret->bnx_sc = sc;
4256beedf5beSSepherosa Ziehau 		ret->bnx_std = std;
4257ac2936fdSSepherosa Ziehau 		ret->bnx_rx_mbx = mbx;
4258841cdf08SSepherosa Ziehau 		ret->bnx_rx_cntmax = (BGE_STD_RX_RING_CNT / 4) /
4259841cdf08SSepherosa Ziehau 		    sc->bnx_rx_retcnt;
4260841cdf08SSepherosa Ziehau 		ret->bnx_rx_mask = 1 << i;
42613a16b7b8SSepherosa Ziehau 
4262695a8586SSepherosa Ziehau 		if (!BNX_RSS_ENABLED(sc)) {
42630a806e3aSSepherosa Ziehau 			intr = &sc->bnx_intr_data[0];
42640a806e3aSSepherosa Ziehau 		} else {
42650a806e3aSSepherosa Ziehau 			KKASSERT(i + 1 < sc->bnx_intr_cnt);
42660a806e3aSSepherosa Ziehau 			intr = &sc->bnx_intr_data[i + 1];
42670a806e3aSSepherosa Ziehau 		}
42680a806e3aSSepherosa Ziehau 
4269695a8586SSepherosa Ziehau 		if (i == 0) {
42703a16b7b8SSepherosa Ziehau 			ret->bnx_rx_considx =
42710a806e3aSSepherosa Ziehau 			    &intr->bnx_status_block->bge_idx[0].bge_rx_prod_idx;
4272695a8586SSepherosa Ziehau 		} else if (i == 1) {
4273695a8586SSepherosa Ziehau 			ret->bnx_rx_considx =
4274695a8586SSepherosa Ziehau 			    &intr->bnx_status_block->bge_rx_jumbo_cons_idx;
4275695a8586SSepherosa Ziehau 		} else if (i == 2) {
4276695a8586SSepherosa Ziehau 			ret->bnx_rx_considx =
4277695a8586SSepherosa Ziehau 			    &intr->bnx_status_block->bge_rsvd1;
4278695a8586SSepherosa Ziehau 		} else if (i == 3) {
4279695a8586SSepherosa Ziehau 			ret->bnx_rx_considx =
4280695a8586SSepherosa Ziehau 			    &intr->bnx_status_block->bge_rx_mini_cons_idx;
4281695a8586SSepherosa Ziehau 		} else {
4282695a8586SSepherosa Ziehau 			panic("unknown RX return ring %d\n", i);
4283695a8586SSepherosa Ziehau 		}
42844fa38985SSepherosa Ziehau 		ret->bnx_hw_status_tag =
42850a806e3aSSepherosa Ziehau 		    &intr->bnx_status_block->bge_status_tag;
42863a16b7b8SSepherosa Ziehau 
4287beedf5beSSepherosa Ziehau 		error = bnx_create_rx_ret_ring(ret);
4288beedf5beSSepherosa Ziehau 		if (error) {
4289beedf5beSSepherosa Ziehau 			device_printf(dev,
4290beedf5beSSepherosa Ziehau 			    "could not create %dth RX ret ring\n", i);
4291beedf5beSSepherosa Ziehau 			return error;
4292beedf5beSSepherosa Ziehau 		}
4293ac2936fdSSepherosa Ziehau 		mbx += 8;
4294beedf5beSSepherosa Ziehau 	}
4295beedf5beSSepherosa Ziehau 
4296beedf5beSSepherosa Ziehau 	/*
4297beedf5beSSepherosa Ziehau 	 * Create TX rings
4298beedf5beSSepherosa Ziehau 	 */
429962938642SMatthew Dillon 	sc->bnx_tx_ring =
430062938642SMatthew Dillon 		kmalloc(sizeof(struct bnx_tx_ring) * sc->bnx_tx_ringcnt,
430162938642SMatthew Dillon 			M_DEVBUF,
430262938642SMatthew Dillon 			M_WAITOK | M_ZERO | M_CACHEALIGN);
430333a04907SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
430433a04907SSepherosa Ziehau 		struct bnx_tx_ring *txr = &sc->bnx_tx_ring[i];
43050a806e3aSSepherosa Ziehau 		struct bnx_intr_data *intr;
430633a04907SSepherosa Ziehau 
430733a04907SSepherosa Ziehau 		txr->bnx_sc = sc;
43081c9d03f6SSepherosa Ziehau 		txr->bnx_tx_mbx = bnx_tx_mailbox[i];
43098bd43d5dSSepherosa Ziehau 
43100a806e3aSSepherosa Ziehau 		if (sc->bnx_tx_ringcnt == 1) {
43110a806e3aSSepherosa Ziehau 			intr = &sc->bnx_intr_data[0];
43120a806e3aSSepherosa Ziehau 		} else {
43130a806e3aSSepherosa Ziehau 			KKASSERT(i + 1 < sc->bnx_intr_cnt);
43140a806e3aSSepherosa Ziehau 			intr = &sc->bnx_intr_data[i + 1];
43150a806e3aSSepherosa Ziehau 		}
43160a806e3aSSepherosa Ziehau 
4317695a8586SSepherosa Ziehau 		if ((sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) == 0) {
4318695a8586SSepherosa Ziehau 			txr->bnx_hw_status_tag =
4319695a8586SSepherosa Ziehau 			    &intr->bnx_status_block->bge_status_tag;
4320695a8586SSepherosa Ziehau 		}
43213a16b7b8SSepherosa Ziehau 		txr->bnx_tx_considx =
43220a806e3aSSepherosa Ziehau 		    &intr->bnx_status_block->bge_idx[0].bge_tx_cons_idx;
43233a16b7b8SSepherosa Ziehau 
432433a04907SSepherosa Ziehau 		error = bnx_create_tx_ring(txr);
432533a04907SSepherosa Ziehau 		if (error) {
4326beedf5beSSepherosa Ziehau 			device_printf(dev,
4327beedf5beSSepherosa Ziehau 			    "could not create %dth TX ring\n", i);
4328beedf5beSSepherosa Ziehau 			return error;
4329beedf5beSSepherosa Ziehau 		}
4330beedf5beSSepherosa Ziehau 	}
4331beedf5beSSepherosa Ziehau 
4332beedf5beSSepherosa Ziehau 	/*
4333beedf5beSSepherosa Ziehau 	 * Create jumbo buffer pool.
4334beedf5beSSepherosa Ziehau 	 */
4335beedf5beSSepherosa Ziehau 	if (BNX_IS_JUMBO_CAPABLE(sc)) {
4336beedf5beSSepherosa Ziehau 		error = bnx_alloc_jumbo_mem(sc);
4337beedf5beSSepherosa Ziehau 		if (error) {
4338beedf5beSSepherosa Ziehau 			device_printf(dev,
4339beedf5beSSepherosa Ziehau 			    "could not create jumbo buffer pool\n");
434033a04907SSepherosa Ziehau 			return error;
434133a04907SSepherosa Ziehau 		}
434233a04907SSepherosa Ziehau 	}
434333a04907SSepherosa Ziehau 
43446c8d8eccSSepherosa Ziehau 	return 0;
43456c8d8eccSSepherosa Ziehau }
43466c8d8eccSSepherosa Ziehau 
43476c8d8eccSSepherosa Ziehau static int
bnx_dma_block_alloc(struct bnx_softc * sc,bus_size_t size,bus_dma_tag_t * tag,bus_dmamap_t * map,void ** addr,bus_addr_t * paddr)43486c8d8eccSSepherosa Ziehau bnx_dma_block_alloc(struct bnx_softc *sc, bus_size_t size, bus_dma_tag_t *tag,
43496c8d8eccSSepherosa Ziehau 		    bus_dmamap_t *map, void **addr, bus_addr_t *paddr)
43506c8d8eccSSepherosa Ziehau {
43516c8d8eccSSepherosa Ziehau 	bus_dmamem_t dmem;
43526c8d8eccSSepherosa Ziehau 	int error;
43536c8d8eccSSepherosa Ziehau 
43546c8d8eccSSepherosa Ziehau 	error = bus_dmamem_coherent(sc->bnx_cdata.bnx_parent_tag, PAGE_SIZE, 0,
43556c8d8eccSSepherosa Ziehau 				    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
43566c8d8eccSSepherosa Ziehau 				    size, BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmem);
43576c8d8eccSSepherosa Ziehau 	if (error)
43586c8d8eccSSepherosa Ziehau 		return error;
43596c8d8eccSSepherosa Ziehau 
43606c8d8eccSSepherosa Ziehau 	*tag = dmem.dmem_tag;
43616c8d8eccSSepherosa Ziehau 	*map = dmem.dmem_map;
43626c8d8eccSSepherosa Ziehau 	*addr = dmem.dmem_addr;
43636c8d8eccSSepherosa Ziehau 	*paddr = dmem.dmem_busaddr;
43646c8d8eccSSepherosa Ziehau 
43656c8d8eccSSepherosa Ziehau 	return 0;
43666c8d8eccSSepherosa Ziehau }
43676c8d8eccSSepherosa Ziehau 
43686c8d8eccSSepherosa Ziehau static void
bnx_dma_block_free(bus_dma_tag_t tag,bus_dmamap_t map,void * addr)43696c8d8eccSSepherosa Ziehau bnx_dma_block_free(bus_dma_tag_t tag, bus_dmamap_t map, void *addr)
43706c8d8eccSSepherosa Ziehau {
43716c8d8eccSSepherosa Ziehau 	if (tag != NULL) {
43726c8d8eccSSepherosa Ziehau 		bus_dmamap_unload(tag, map);
43736c8d8eccSSepherosa Ziehau 		bus_dmamem_free(tag, addr, map);
43746c8d8eccSSepherosa Ziehau 		bus_dma_tag_destroy(tag);
43756c8d8eccSSepherosa Ziehau 	}
43766c8d8eccSSepherosa Ziehau }
43776c8d8eccSSepherosa Ziehau 
43786c8d8eccSSepherosa Ziehau static void
bnx_tbi_link_upd(struct bnx_softc * sc,uint32_t status)43796c8d8eccSSepherosa Ziehau bnx_tbi_link_upd(struct bnx_softc *sc, uint32_t status)
43806c8d8eccSSepherosa Ziehau {
43816c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
43826c8d8eccSSepherosa Ziehau 
43836c8d8eccSSepherosa Ziehau #define PCS_ENCODE_ERR	(BGE_MACSTAT_PORT_DECODE_ERROR|BGE_MACSTAT_MI_COMPLETE)
43846c8d8eccSSepherosa Ziehau 
43856c8d8eccSSepherosa Ziehau 	/*
43866c8d8eccSSepherosa Ziehau 	 * Sometimes PCS encoding errors are detected in
43876c8d8eccSSepherosa Ziehau 	 * TBI mode (on fiber NICs), and for some reason
43886c8d8eccSSepherosa Ziehau 	 * the chip will signal them as link changes.
43896c8d8eccSSepherosa Ziehau 	 * If we get a link change event, but the 'PCS
43906c8d8eccSSepherosa Ziehau 	 * encoding error' bit in the MAC status register
43916c8d8eccSSepherosa Ziehau 	 * is set, don't bother doing a link check.
43926c8d8eccSSepherosa Ziehau 	 * This avoids spurious "gigabit link up" messages
43936c8d8eccSSepherosa Ziehau 	 * that sometimes appear on fiber NICs during
43946c8d8eccSSepherosa Ziehau 	 * periods of heavy traffic.
43956c8d8eccSSepherosa Ziehau 	 */
43966c8d8eccSSepherosa Ziehau 	if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
43976c8d8eccSSepherosa Ziehau 		if (!sc->bnx_link) {
43986c8d8eccSSepherosa Ziehau 			sc->bnx_link++;
43996c8d8eccSSepherosa Ziehau 			if (sc->bnx_asicrev == BGE_ASICREV_BCM5704) {
44006c8d8eccSSepherosa Ziehau 				BNX_CLRBIT(sc, BGE_MAC_MODE,
44016c8d8eccSSepherosa Ziehau 				    BGE_MACMODE_TBI_SEND_CFGS);
44024aa71e73SSepherosa Ziehau 				DELAY(40);
44036c8d8eccSSepherosa Ziehau 			}
44046c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
44056c8d8eccSSepherosa Ziehau 
44066c8d8eccSSepherosa Ziehau 			if (bootverbose)
44076c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link UP\n");
44086c8d8eccSSepherosa Ziehau 
44096c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_UP;
44106c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
44116c8d8eccSSepherosa Ziehau 		}
44126c8d8eccSSepherosa Ziehau 	} else if ((status & PCS_ENCODE_ERR) != PCS_ENCODE_ERR) {
44136c8d8eccSSepherosa Ziehau 		if (sc->bnx_link) {
44146c8d8eccSSepherosa Ziehau 			sc->bnx_link = 0;
44156c8d8eccSSepherosa Ziehau 
44166c8d8eccSSepherosa Ziehau 			if (bootverbose)
44176c8d8eccSSepherosa Ziehau 				if_printf(ifp, "link DOWN\n");
44186c8d8eccSSepherosa Ziehau 
44196c8d8eccSSepherosa Ziehau 			ifp->if_link_state = LINK_STATE_DOWN;
44206c8d8eccSSepherosa Ziehau 			if_link_state_change(ifp);
44216c8d8eccSSepherosa Ziehau 		}
44226c8d8eccSSepherosa Ziehau 	}
44236c8d8eccSSepherosa Ziehau 
44246c8d8eccSSepherosa Ziehau #undef PCS_ENCODE_ERR
44256c8d8eccSSepherosa Ziehau 
44266c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
44276c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
44286c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
44296c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
44306c8d8eccSSepherosa Ziehau }
44316c8d8eccSSepherosa Ziehau 
44326c8d8eccSSepherosa Ziehau static void
bnx_copper_link_upd(struct bnx_softc * sc,uint32_t status __unused)44336c8d8eccSSepherosa Ziehau bnx_copper_link_upd(struct bnx_softc *sc, uint32_t status __unused)
44346c8d8eccSSepherosa Ziehau {
44356c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
44366c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
44376c8d8eccSSepherosa Ziehau 
44386c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
44396c8d8eccSSepherosa Ziehau 	bnx_miibus_statchg(sc->bnx_dev);
44406c8d8eccSSepherosa Ziehau 
44416c8d8eccSSepherosa Ziehau 	if (bootverbose) {
44426c8d8eccSSepherosa Ziehau 		if (sc->bnx_link)
44436c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
44446c8d8eccSSepherosa Ziehau 		else
44456c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
44466c8d8eccSSepherosa Ziehau 	}
44476c8d8eccSSepherosa Ziehau 
44486c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
44496c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
44506c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
44516c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
44526c8d8eccSSepherosa Ziehau }
44536c8d8eccSSepherosa Ziehau 
44546c8d8eccSSepherosa Ziehau static void
bnx_autopoll_link_upd(struct bnx_softc * sc,uint32_t status __unused)44556c8d8eccSSepherosa Ziehau bnx_autopoll_link_upd(struct bnx_softc *sc, uint32_t status __unused)
44566c8d8eccSSepherosa Ziehau {
44576c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
44586c8d8eccSSepherosa Ziehau 	struct mii_data *mii = device_get_softc(sc->bnx_miibus);
44596c8d8eccSSepherosa Ziehau 
44606c8d8eccSSepherosa Ziehau 	mii_pollstat(mii);
44616c8d8eccSSepherosa Ziehau 
44626c8d8eccSSepherosa Ziehau 	if (!sc->bnx_link &&
44636c8d8eccSSepherosa Ziehau 	    (mii->mii_media_status & IFM_ACTIVE) &&
44646c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
44656c8d8eccSSepherosa Ziehau 		sc->bnx_link++;
44666c8d8eccSSepherosa Ziehau 		if (bootverbose)
44676c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link UP\n");
44686c8d8eccSSepherosa Ziehau 	} else if (sc->bnx_link &&
44696c8d8eccSSepherosa Ziehau 	    (!(mii->mii_media_status & IFM_ACTIVE) ||
44706c8d8eccSSepherosa Ziehau 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
44716c8d8eccSSepherosa Ziehau 		sc->bnx_link = 0;
44726c8d8eccSSepherosa Ziehau 		if (bootverbose)
44736c8d8eccSSepherosa Ziehau 			if_printf(ifp, "link DOWN\n");
44746c8d8eccSSepherosa Ziehau 	}
44756c8d8eccSSepherosa Ziehau 
44766c8d8eccSSepherosa Ziehau 	/* Clear the attention. */
44776c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
44786c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
44796c8d8eccSSepherosa Ziehau 	    BGE_MACSTAT_LINK_CHANGED);
44806c8d8eccSSepherosa Ziehau }
44816c8d8eccSSepherosa Ziehau 
44826c8d8eccSSepherosa Ziehau static int
bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)44836c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_ticks(SYSCTL_HANDLER_ARGS)
44846c8d8eccSSepherosa Ziehau {
44856c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
44866c8d8eccSSepherosa Ziehau 
44876c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
44886c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_ticks,
44896c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_MIN, BNX_RX_COAL_TICKS_MAX,
44906c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_TICKS_CHG);
44916c8d8eccSSepherosa Ziehau }
44926c8d8eccSSepherosa Ziehau 
44936c8d8eccSSepherosa Ziehau static int
bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)44946c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_ticks(SYSCTL_HANDLER_ARGS)
44956c8d8eccSSepherosa Ziehau {
44966c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
44976c8d8eccSSepherosa Ziehau 
44986c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
44996c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_ticks,
45006c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_MIN, BNX_TX_COAL_TICKS_MAX,
45016c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_TICKS_CHG);
45026c8d8eccSSepherosa Ziehau }
45036c8d8eccSSepherosa Ziehau 
45046c8d8eccSSepherosa Ziehau static int
bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)45056c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds(SYSCTL_HANDLER_ARGS)
45066c8d8eccSSepherosa Ziehau {
45076c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
45086c8d8eccSSepherosa Ziehau 
45096c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
45106c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds,
45116c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
45126c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_CHG);
45136c8d8eccSSepherosa Ziehau }
45146c8d8eccSSepherosa Ziehau 
45156c8d8eccSSepherosa Ziehau static int
bnx_sysctl_rx_coal_bds_poll(SYSCTL_HANDLER_ARGS)4516a86cc105SSepherosa Ziehau bnx_sysctl_rx_coal_bds_poll(SYSCTL_HANDLER_ARGS)
4517a86cc105SSepherosa Ziehau {
4518a86cc105SSepherosa Ziehau 	struct bnx_softc *sc = arg1;
4519a86cc105SSepherosa Ziehau 
4520a86cc105SSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
4521a86cc105SSepherosa Ziehau 	    &sc->bnx_rx_coal_bds_poll,
4522a86cc105SSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
4523a86cc105SSepherosa Ziehau 	    BNX_RX_COAL_BDS_CHG);
4524a86cc105SSepherosa Ziehau }
4525a86cc105SSepherosa Ziehau 
4526a86cc105SSepherosa Ziehau static int
bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)45276c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds(SYSCTL_HANDLER_ARGS)
45286c8d8eccSSepherosa Ziehau {
45296c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
45306c8d8eccSSepherosa Ziehau 
45316c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
45326c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds,
45336c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
45346c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_CHG);
45356c8d8eccSSepherosa Ziehau }
45366c8d8eccSSepherosa Ziehau 
45376c8d8eccSSepherosa Ziehau static int
bnx_sysctl_tx_coal_bds_poll(SYSCTL_HANDLER_ARGS)453827357d84SSepherosa Ziehau bnx_sysctl_tx_coal_bds_poll(SYSCTL_HANDLER_ARGS)
453927357d84SSepherosa Ziehau {
454027357d84SSepherosa Ziehau 	struct bnx_softc *sc = arg1;
454127357d84SSepherosa Ziehau 
454227357d84SSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
454327357d84SSepherosa Ziehau 	    &sc->bnx_tx_coal_bds_poll,
454427357d84SSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
454527357d84SSepherosa Ziehau 	    BNX_TX_COAL_BDS_CHG);
454627357d84SSepherosa Ziehau }
454727357d84SSepherosa Ziehau 
454827357d84SSepherosa Ziehau static int
bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)45496c8d8eccSSepherosa Ziehau bnx_sysctl_rx_coal_bds_int(SYSCTL_HANDLER_ARGS)
45506c8d8eccSSepherosa Ziehau {
45516c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
45526c8d8eccSSepherosa Ziehau 
45536c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
45546c8d8eccSSepherosa Ziehau 	    &sc->bnx_rx_coal_bds_int,
45556c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_MIN, BNX_RX_COAL_BDS_MAX,
45566c8d8eccSSepherosa Ziehau 	    BNX_RX_COAL_BDS_INT_CHG);
45576c8d8eccSSepherosa Ziehau }
45586c8d8eccSSepherosa Ziehau 
45596c8d8eccSSepherosa Ziehau static int
bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)45606c8d8eccSSepherosa Ziehau bnx_sysctl_tx_coal_bds_int(SYSCTL_HANDLER_ARGS)
45616c8d8eccSSepherosa Ziehau {
45626c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
45636c8d8eccSSepherosa Ziehau 
45646c8d8eccSSepherosa Ziehau 	return bnx_sysctl_coal_chg(oidp, arg1, arg2, req,
45656c8d8eccSSepherosa Ziehau 	    &sc->bnx_tx_coal_bds_int,
45666c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_MIN, BNX_TX_COAL_BDS_MAX,
45676c8d8eccSSepherosa Ziehau 	    BNX_TX_COAL_BDS_INT_CHG);
45686c8d8eccSSepherosa Ziehau }
45696c8d8eccSSepherosa Ziehau 
45706c8d8eccSSepherosa Ziehau static int
bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS,uint32_t * coal,int coal_min,int coal_max,uint32_t coal_chg_mask)45716c8d8eccSSepherosa Ziehau bnx_sysctl_coal_chg(SYSCTL_HANDLER_ARGS, uint32_t *coal,
45726c8d8eccSSepherosa Ziehau     int coal_min, int coal_max, uint32_t coal_chg_mask)
45736c8d8eccSSepherosa Ziehau {
45746c8d8eccSSepherosa Ziehau 	struct bnx_softc *sc = arg1;
45756c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
45766c8d8eccSSepherosa Ziehau 	int error = 0, v;
45776c8d8eccSSepherosa Ziehau 
4578329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
45796c8d8eccSSepherosa Ziehau 
45806c8d8eccSSepherosa Ziehau 	v = *coal;
45816c8d8eccSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &v, 0, req);
45826c8d8eccSSepherosa Ziehau 	if (!error && req->newptr != NULL) {
45836c8d8eccSSepherosa Ziehau 		if (v < coal_min || v > coal_max) {
45846c8d8eccSSepherosa Ziehau 			error = EINVAL;
45856c8d8eccSSepherosa Ziehau 		} else {
45866c8d8eccSSepherosa Ziehau 			*coal = v;
45876c8d8eccSSepherosa Ziehau 			sc->bnx_coal_chg |= coal_chg_mask;
4588f5014362SSepherosa Ziehau 
4589f5014362SSepherosa Ziehau 			/* Commit changes */
4590f5014362SSepherosa Ziehau 			bnx_coal_change(sc);
45916c8d8eccSSepherosa Ziehau 		}
45926c8d8eccSSepherosa Ziehau 	}
45936c8d8eccSSepherosa Ziehau 
4594329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
45956c8d8eccSSepherosa Ziehau 	return error;
45966c8d8eccSSepherosa Ziehau }
45976c8d8eccSSepherosa Ziehau 
45986c8d8eccSSepherosa Ziehau static void
bnx_coal_change(struct bnx_softc * sc)45996c8d8eccSSepherosa Ziehau bnx_coal_change(struct bnx_softc *sc)
46006c8d8eccSSepherosa Ziehau {
46016c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
4602695a8586SSepherosa Ziehau 	int i;
46036c8d8eccSSepherosa Ziehau 
4604329f9016SSepherosa Ziehau 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
46056c8d8eccSSepherosa Ziehau 
46066c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_TICKS_CHG) {
4607695a8586SSepherosa Ziehau 		if (sc->bnx_rx_retcnt == 1) {
46086c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS,
46096c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_ticks);
4610695a8586SSepherosa Ziehau 			i = 0;
4611695a8586SSepherosa Ziehau 		} else {
4612695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, 0);
4613695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4614695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_RX_COAL_TICKS +
4615695a8586SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE),
4616695a8586SSepherosa Ziehau 				    sc->bnx_rx_coal_ticks);
4617695a8586SSepherosa Ziehau 			}
4618695a8586SSepherosa Ziehau 		}
4619695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4620695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_RX_COAL_TICKS +
4621695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4622695a8586SSepherosa Ziehau 		}
46236c8d8eccSSepherosa Ziehau 		if (bootverbose) {
46246c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_ticks -> %u\n",
46256c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_ticks);
46266c8d8eccSSepherosa Ziehau 		}
46276c8d8eccSSepherosa Ziehau 	}
46286c8d8eccSSepherosa Ziehau 
46296c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_TICKS_CHG) {
4630695a8586SSepherosa Ziehau 		if (sc->bnx_tx_ringcnt == 1) {
46316c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS,
46326c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_ticks);
4633695a8586SSepherosa Ziehau 			i = 0;
4634695a8586SSepherosa Ziehau 		} else {
4635695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, 0);
4636695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4637695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_TX_COAL_TICKS +
4638695a8586SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE),
4639695a8586SSepherosa Ziehau 				    sc->bnx_tx_coal_ticks);
4640695a8586SSepherosa Ziehau 			}
4641695a8586SSepherosa Ziehau 		}
4642695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4643695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_TX_COAL_TICKS +
4644695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4645695a8586SSepherosa Ziehau 		}
46466c8d8eccSSepherosa Ziehau 		if (bootverbose) {
46476c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_ticks -> %u\n",
46486c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_ticks);
46496c8d8eccSSepherosa Ziehau 		}
46506c8d8eccSSepherosa Ziehau 	}
46516c8d8eccSSepherosa Ziehau 
46526c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_CHG) {
4653a86cc105SSepherosa Ziehau 		uint32_t rx_coal_bds;
4654a86cc105SSepherosa Ziehau 
4655a86cc105SSepherosa Ziehau 		if (ifp->if_flags & IFF_NPOLLING)
4656a86cc105SSepherosa Ziehau 			rx_coal_bds = sc->bnx_rx_coal_bds_poll;
4657a86cc105SSepherosa Ziehau 		else
4658a86cc105SSepherosa Ziehau 			rx_coal_bds = sc->bnx_rx_coal_bds;
4659a86cc105SSepherosa Ziehau 
4660695a8586SSepherosa Ziehau 		if (sc->bnx_rx_retcnt == 1) {
4661a86cc105SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, rx_coal_bds);
4662695a8586SSepherosa Ziehau 			i = 0;
4663695a8586SSepherosa Ziehau 		} else {
4664695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, 0);
4665695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4666695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS +
4667a86cc105SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE), rx_coal_bds);
4668695a8586SSepherosa Ziehau 			}
4669695a8586SSepherosa Ziehau 		}
4670695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4671695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS +
4672695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4673695a8586SSepherosa Ziehau 		}
46746c8d8eccSSepherosa Ziehau 		if (bootverbose) {
4675a86cc105SSepherosa Ziehau 			if_printf(ifp, "%srx_coal_bds -> %u\n",
4676a86cc105SSepherosa Ziehau 			    (ifp->if_flags & IFF_NPOLLING) ? "polling " : "",
4677a86cc105SSepherosa Ziehau 			    rx_coal_bds);
46786c8d8eccSSepherosa Ziehau 		}
46796c8d8eccSSepherosa Ziehau 	}
46806c8d8eccSSepherosa Ziehau 
46816c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_CHG) {
468227357d84SSepherosa Ziehau 		uint32_t tx_coal_bds;
468327357d84SSepherosa Ziehau 
468427357d84SSepherosa Ziehau 		if (ifp->if_flags & IFF_NPOLLING)
468527357d84SSepherosa Ziehau 			tx_coal_bds = sc->bnx_tx_coal_bds_poll;
468627357d84SSepherosa Ziehau 		else
468727357d84SSepherosa Ziehau 			tx_coal_bds = sc->bnx_tx_coal_bds;
468827357d84SSepherosa Ziehau 
4689695a8586SSepherosa Ziehau 		if (sc->bnx_tx_ringcnt == 1) {
469027357d84SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, tx_coal_bds);
4691695a8586SSepherosa Ziehau 			i = 0;
4692695a8586SSepherosa Ziehau 		} else {
4693695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, 0);
4694695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4695695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS +
469627357d84SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE), tx_coal_bds);
4697695a8586SSepherosa Ziehau 			}
4698695a8586SSepherosa Ziehau 		}
4699695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4700695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS +
4701695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4702695a8586SSepherosa Ziehau 		}
47036c8d8eccSSepherosa Ziehau 		if (bootverbose) {
470427357d84SSepherosa Ziehau 			if_printf(ifp, "%stx_coal_bds -> %u\n",
470527357d84SSepherosa Ziehau 			    (ifp->if_flags & IFF_NPOLLING) ? "polling " : "",
470627357d84SSepherosa Ziehau 			    tx_coal_bds);
47076c8d8eccSSepherosa Ziehau 		}
47086c8d8eccSSepherosa Ziehau 	}
47096c8d8eccSSepherosa Ziehau 
47106c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_RX_COAL_BDS_INT_CHG) {
4711695a8586SSepherosa Ziehau 		if (sc->bnx_rx_retcnt == 1) {
47126c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT,
47136c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds_int);
4714695a8586SSepherosa Ziehau 			i = 0;
4715695a8586SSepherosa Ziehau 		} else {
4716695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
4717695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_rx_retcnt; ++i) {
4718695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS_INT +
4719695a8586SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE),
4720695a8586SSepherosa Ziehau 				    sc->bnx_rx_coal_bds_int);
4721695a8586SSepherosa Ziehau 			}
4722695a8586SSepherosa Ziehau 		}
4723695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4724695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_RX_MAX_COAL_BDS_INT +
4725695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4726695a8586SSepherosa Ziehau 		}
47276c8d8eccSSepherosa Ziehau 		if (bootverbose) {
47286c8d8eccSSepherosa Ziehau 			if_printf(ifp, "rx_coal_bds_int -> %u\n",
47296c8d8eccSSepherosa Ziehau 			    sc->bnx_rx_coal_bds_int);
47306c8d8eccSSepherosa Ziehau 		}
47316c8d8eccSSepherosa Ziehau 	}
47326c8d8eccSSepherosa Ziehau 
47336c8d8eccSSepherosa Ziehau 	if (sc->bnx_coal_chg & BNX_TX_COAL_BDS_INT_CHG) {
4734695a8586SSepherosa Ziehau 		if (sc->bnx_tx_ringcnt == 1) {
47356c8d8eccSSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT,
47366c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds_int);
4737695a8586SSepherosa Ziehau 			i = 0;
4738695a8586SSepherosa Ziehau 		} else {
4739695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
4740695a8586SSepherosa Ziehau 			for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
4741695a8586SSepherosa Ziehau 				CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS_INT +
4742695a8586SSepherosa Ziehau 				    (i * BGE_VEC_COALSET_SIZE),
4743695a8586SSepherosa Ziehau 				    sc->bnx_tx_coal_bds_int);
4744695a8586SSepherosa Ziehau 			}
4745695a8586SSepherosa Ziehau 		}
4746695a8586SSepherosa Ziehau 		for (; i < BNX_INTR_MAX - 1; ++i) {
4747695a8586SSepherosa Ziehau 			CSR_WRITE_4(sc, BGE_VEC1_TX_MAX_COAL_BDS_INT +
4748695a8586SSepherosa Ziehau 			    (i * BGE_VEC_COALSET_SIZE), 0);
4749695a8586SSepherosa Ziehau 		}
47506c8d8eccSSepherosa Ziehau 		if (bootverbose) {
47516c8d8eccSSepherosa Ziehau 			if_printf(ifp, "tx_coal_bds_int -> %u\n",
47526c8d8eccSSepherosa Ziehau 			    sc->bnx_tx_coal_bds_int);
47536c8d8eccSSepherosa Ziehau 		}
47546c8d8eccSSepherosa Ziehau 	}
47556c8d8eccSSepherosa Ziehau 
47566c8d8eccSSepherosa Ziehau 	sc->bnx_coal_chg = 0;
47576c8d8eccSSepherosa Ziehau }
47586c8d8eccSSepherosa Ziehau 
47596c8d8eccSSepherosa Ziehau static void
bnx_check_intr_rxtx(void * xintr)4760695a8586SSepherosa Ziehau bnx_check_intr_rxtx(void *xintr)
4761df9ccc98SSepherosa Ziehau {
4762f33ac8a4SSepherosa Ziehau 	struct bnx_intr_data *intr = xintr;
4763f33ac8a4SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret;
4764f33ac8a4SSepherosa Ziehau 	struct bnx_tx_ring *txr;
4765f33ac8a4SSepherosa Ziehau 	struct ifnet *ifp;
4766df9ccc98SSepherosa Ziehau 
4767f33ac8a4SSepherosa Ziehau 	lwkt_serialize_enter(intr->bnx_intr_serialize);
4768df9ccc98SSepherosa Ziehau 
4769f33ac8a4SSepherosa Ziehau 	KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4770df9ccc98SSepherosa Ziehau 
4771f33ac8a4SSepherosa Ziehau 	ifp = &intr->bnx_sc->arpcom.ac_if;
477239a8d43aSSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4773f33ac8a4SSepherosa Ziehau 		lwkt_serialize_exit(intr->bnx_intr_serialize);
4774df9ccc98SSepherosa Ziehau 		return;
4775df9ccc98SSepherosa Ziehau 	}
4776df9ccc98SSepherosa Ziehau 
4777f33ac8a4SSepherosa Ziehau 	txr = intr->bnx_txr;
4778f33ac8a4SSepherosa Ziehau 	ret = intr->bnx_ret;
4779f33ac8a4SSepherosa Ziehau 
47803a16b7b8SSepherosa Ziehau 	if (*ret->bnx_rx_considx != ret->bnx_rx_saved_considx ||
47813a16b7b8SSepherosa Ziehau 	    *txr->bnx_tx_considx != txr->bnx_tx_saved_considx) {
4782f33ac8a4SSepherosa Ziehau 		if (intr->bnx_rx_check_considx == ret->bnx_rx_saved_considx &&
4783f33ac8a4SSepherosa Ziehau 		    intr->bnx_tx_check_considx == txr->bnx_tx_saved_considx) {
4784f33ac8a4SSepherosa Ziehau 			if (!intr->bnx_intr_maylose) {
4785f33ac8a4SSepherosa Ziehau 				intr->bnx_intr_maylose = TRUE;
4786df9ccc98SSepherosa Ziehau 				goto done;
4787df9ccc98SSepherosa Ziehau 			}
4788df9ccc98SSepherosa Ziehau 			if (bootverbose)
4789df9ccc98SSepherosa Ziehau 				if_printf(ifp, "lost interrupt\n");
4790f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_func(intr->bnx_intr_arg);
4791df9ccc98SSepherosa Ziehau 		}
4792df9ccc98SSepherosa Ziehau 	}
4793f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_maylose = FALSE;
4794f33ac8a4SSepherosa Ziehau 	intr->bnx_rx_check_considx = ret->bnx_rx_saved_considx;
4795f33ac8a4SSepherosa Ziehau 	intr->bnx_tx_check_considx = txr->bnx_tx_saved_considx;
4796df9ccc98SSepherosa Ziehau 
4797df9ccc98SSepherosa Ziehau done:
4798f33ac8a4SSepherosa Ziehau 	callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4799f33ac8a4SSepherosa Ziehau 	    intr->bnx_intr_check, intr);
4800f33ac8a4SSepherosa Ziehau 	lwkt_serialize_exit(intr->bnx_intr_serialize);
4801df9ccc98SSepherosa Ziehau }
4802df9ccc98SSepherosa Ziehau 
4803df9ccc98SSepherosa Ziehau static void
bnx_check_intr_tx(void * xintr)4804695a8586SSepherosa Ziehau bnx_check_intr_tx(void *xintr)
4805695a8586SSepherosa Ziehau {
4806695a8586SSepherosa Ziehau 	struct bnx_intr_data *intr = xintr;
4807695a8586SSepherosa Ziehau 	struct bnx_tx_ring *txr;
4808695a8586SSepherosa Ziehau 	struct ifnet *ifp;
4809695a8586SSepherosa Ziehau 
4810695a8586SSepherosa Ziehau 	lwkt_serialize_enter(intr->bnx_intr_serialize);
4811695a8586SSepherosa Ziehau 
4812695a8586SSepherosa Ziehau 	KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4813695a8586SSepherosa Ziehau 
4814695a8586SSepherosa Ziehau 	ifp = &intr->bnx_sc->arpcom.ac_if;
4815695a8586SSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4816695a8586SSepherosa Ziehau 		lwkt_serialize_exit(intr->bnx_intr_serialize);
4817695a8586SSepherosa Ziehau 		return;
4818695a8586SSepherosa Ziehau 	}
4819695a8586SSepherosa Ziehau 
4820695a8586SSepherosa Ziehau 	txr = intr->bnx_txr;
4821695a8586SSepherosa Ziehau 
4822695a8586SSepherosa Ziehau 	if (*txr->bnx_tx_considx != txr->bnx_tx_saved_considx) {
4823695a8586SSepherosa Ziehau 		if (intr->bnx_tx_check_considx == txr->bnx_tx_saved_considx) {
4824695a8586SSepherosa Ziehau 			if (!intr->bnx_intr_maylose) {
4825695a8586SSepherosa Ziehau 				intr->bnx_intr_maylose = TRUE;
4826695a8586SSepherosa Ziehau 				goto done;
4827695a8586SSepherosa Ziehau 			}
4828695a8586SSepherosa Ziehau 			if (bootverbose)
4829695a8586SSepherosa Ziehau 				if_printf(ifp, "lost interrupt\n");
4830695a8586SSepherosa Ziehau 			intr->bnx_intr_func(intr->bnx_intr_arg);
4831695a8586SSepherosa Ziehau 		}
4832695a8586SSepherosa Ziehau 	}
4833695a8586SSepherosa Ziehau 	intr->bnx_intr_maylose = FALSE;
4834695a8586SSepherosa Ziehau 	intr->bnx_tx_check_considx = txr->bnx_tx_saved_considx;
4835695a8586SSepherosa Ziehau 
4836695a8586SSepherosa Ziehau done:
4837695a8586SSepherosa Ziehau 	callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4838695a8586SSepherosa Ziehau 	    intr->bnx_intr_check, intr);
4839695a8586SSepherosa Ziehau 	lwkt_serialize_exit(intr->bnx_intr_serialize);
4840695a8586SSepherosa Ziehau }
4841695a8586SSepherosa Ziehau 
4842695a8586SSepherosa Ziehau static void
bnx_check_intr_rx(void * xintr)4843695a8586SSepherosa Ziehau bnx_check_intr_rx(void *xintr)
4844695a8586SSepherosa Ziehau {
4845695a8586SSepherosa Ziehau 	struct bnx_intr_data *intr = xintr;
4846695a8586SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret;
4847695a8586SSepherosa Ziehau 	struct ifnet *ifp;
4848695a8586SSepherosa Ziehau 
4849695a8586SSepherosa Ziehau 	lwkt_serialize_enter(intr->bnx_intr_serialize);
4850695a8586SSepherosa Ziehau 
4851695a8586SSepherosa Ziehau 	KKASSERT(mycpuid == intr->bnx_intr_cpuid);
4852695a8586SSepherosa Ziehau 
4853695a8586SSepherosa Ziehau 	ifp = &intr->bnx_sc->arpcom.ac_if;
4854695a8586SSepherosa Ziehau 	if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
4855695a8586SSepherosa Ziehau 		lwkt_serialize_exit(intr->bnx_intr_serialize);
4856695a8586SSepherosa Ziehau 		return;
4857695a8586SSepherosa Ziehau 	}
4858695a8586SSepherosa Ziehau 
4859695a8586SSepherosa Ziehau 	ret = intr->bnx_ret;
4860695a8586SSepherosa Ziehau 
4861695a8586SSepherosa Ziehau 	if (*ret->bnx_rx_considx != ret->bnx_rx_saved_considx) {
4862695a8586SSepherosa Ziehau 		if (intr->bnx_rx_check_considx == ret->bnx_rx_saved_considx) {
4863695a8586SSepherosa Ziehau 			if (!intr->bnx_intr_maylose) {
4864695a8586SSepherosa Ziehau 				intr->bnx_intr_maylose = TRUE;
4865695a8586SSepherosa Ziehau 				goto done;
4866695a8586SSepherosa Ziehau 			}
4867695a8586SSepherosa Ziehau 			if (bootverbose)
4868695a8586SSepherosa Ziehau 				if_printf(ifp, "lost interrupt\n");
4869695a8586SSepherosa Ziehau 			intr->bnx_intr_func(intr->bnx_intr_arg);
4870695a8586SSepherosa Ziehau 		}
4871695a8586SSepherosa Ziehau 	}
4872695a8586SSepherosa Ziehau 	intr->bnx_intr_maylose = FALSE;
4873695a8586SSepherosa Ziehau 	intr->bnx_rx_check_considx = ret->bnx_rx_saved_considx;
4874695a8586SSepherosa Ziehau 
4875695a8586SSepherosa Ziehau done:
4876695a8586SSepherosa Ziehau 	callout_reset(&intr->bnx_intr_timer, BNX_INTR_CKINTVL,
4877695a8586SSepherosa Ziehau 	    intr->bnx_intr_check, intr);
4878695a8586SSepherosa Ziehau 	lwkt_serialize_exit(intr->bnx_intr_serialize);
4879695a8586SSepherosa Ziehau }
4880695a8586SSepherosa Ziehau 
4881695a8586SSepherosa Ziehau static void
bnx_enable_intr(struct bnx_softc * sc)48826c8d8eccSSepherosa Ziehau bnx_enable_intr(struct bnx_softc *sc)
48836c8d8eccSSepherosa Ziehau {
48846c8d8eccSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
4885f33ac8a4SSepherosa Ziehau 	int i;
48866c8d8eccSSepherosa Ziehau 
4887f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4888f33ac8a4SSepherosa Ziehau 		lwkt_serialize_handler_enable(
4889f33ac8a4SSepherosa Ziehau 		    sc->bnx_intr_data[i].bnx_intr_serialize);
4890f33ac8a4SSepherosa Ziehau 	}
48916c8d8eccSSepherosa Ziehau 
48926c8d8eccSSepherosa Ziehau 	/*
48936c8d8eccSSepherosa Ziehau 	 * Enable interrupt.
48946c8d8eccSSepherosa Ziehau 	 */
489597ba8fc5SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
489697ba8fc5SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
489797ba8fc5SSepherosa Ziehau 
489897ba8fc5SSepherosa Ziehau 		bnx_writembx(sc, intr->bnx_intr_mbx,
489997ba8fc5SSepherosa Ziehau 		    (*intr->bnx_saved_status_tag) << 24);
49006c8d8eccSSepherosa Ziehau 		/* XXX Linux driver */
490197ba8fc5SSepherosa Ziehau 		bnx_writembx(sc, intr->bnx_intr_mbx,
490297ba8fc5SSepherosa Ziehau 		    (*intr->bnx_saved_status_tag) << 24);
490397ba8fc5SSepherosa Ziehau 	}
49046c8d8eccSSepherosa Ziehau 
49056c8d8eccSSepherosa Ziehau 	/*
49066c8d8eccSSepherosa Ziehau 	 * Unmask the interrupt when we stop polling.
49076c8d8eccSSepherosa Ziehau 	 */
49086c8d8eccSSepherosa Ziehau 	PCI_CLRBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
49096c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
49106c8d8eccSSepherosa Ziehau 
49116c8d8eccSSepherosa Ziehau 	/*
49126c8d8eccSSepherosa Ziehau 	 * Trigger another interrupt, since above writing
49136c8d8eccSSepherosa Ziehau 	 * to interrupt mailbox0 may acknowledge pending
49146c8d8eccSSepherosa Ziehau 	 * interrupt.
49156c8d8eccSSepherosa Ziehau 	 */
49166c8d8eccSSepherosa Ziehau 	BNX_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4917df9ccc98SSepherosa Ziehau 
4918df9ccc98SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_STATUSTAG_BUG) {
4919df9ccc98SSepherosa Ziehau 		if (bootverbose)
4920df9ccc98SSepherosa Ziehau 			if_printf(ifp, "status tag bug workaround\n");
4921df9ccc98SSepherosa Ziehau 
4922f33ac8a4SSepherosa Ziehau 		for (i = 0; i < sc->bnx_intr_cnt; ++i) {
492397ba8fc5SSepherosa Ziehau 			struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
492497ba8fc5SSepherosa Ziehau 
4925695a8586SSepherosa Ziehau 			if (intr->bnx_intr_check == NULL)
4926695a8586SSepherosa Ziehau 				continue;
4927f33ac8a4SSepherosa Ziehau 			intr->bnx_intr_maylose = FALSE;
4928f33ac8a4SSepherosa Ziehau 			intr->bnx_rx_check_considx = 0;
4929f33ac8a4SSepherosa Ziehau 			intr->bnx_tx_check_considx = 0;
4930f33ac8a4SSepherosa Ziehau 			callout_reset_bycpu(&intr->bnx_intr_timer,
4931f33ac8a4SSepherosa Ziehau 			    BNX_INTR_CKINTVL, intr->bnx_intr_check, intr,
4932f33ac8a4SSepherosa Ziehau 			    intr->bnx_intr_cpuid);
4933f33ac8a4SSepherosa Ziehau 		}
4934df9ccc98SSepherosa Ziehau 	}
49356c8d8eccSSepherosa Ziehau }
49366c8d8eccSSepherosa Ziehau 
49376c8d8eccSSepherosa Ziehau static void
bnx_disable_intr(struct bnx_softc * sc)49386c8d8eccSSepherosa Ziehau bnx_disable_intr(struct bnx_softc *sc)
49396c8d8eccSSepherosa Ziehau {
4940f33ac8a4SSepherosa Ziehau 	int i;
4941f33ac8a4SSepherosa Ziehau 
4942f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4943f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
4944f33ac8a4SSepherosa Ziehau 
4945f33ac8a4SSepherosa Ziehau 		callout_stop(&intr->bnx_intr_timer);
4946f33ac8a4SSepherosa Ziehau 		intr->bnx_intr_maylose = FALSE;
4947f33ac8a4SSepherosa Ziehau 		intr->bnx_rx_check_considx = 0;
4948f33ac8a4SSepherosa Ziehau 		intr->bnx_tx_check_considx = 0;
4949f33ac8a4SSepherosa Ziehau 	}
49506c8d8eccSSepherosa Ziehau 
49516c8d8eccSSepherosa Ziehau 	/*
49526c8d8eccSSepherosa Ziehau 	 * Mask the interrupt when we start polling.
49536c8d8eccSSepherosa Ziehau 	 */
49546c8d8eccSSepherosa Ziehau 	PCI_SETBIT(sc->bnx_dev, BGE_PCI_MISC_CTL,
49556c8d8eccSSepherosa Ziehau 	    BGE_PCIMISCCTL_MASK_PCI_INTR, 4);
49566c8d8eccSSepherosa Ziehau 
49576c8d8eccSSepherosa Ziehau 	/*
49586c8d8eccSSepherosa Ziehau 	 * Acknowledge possible asserted interrupt.
49596c8d8eccSSepherosa Ziehau 	 */
4960695a8586SSepherosa Ziehau 	for (i = 0; i < BNX_INTR_MAX; ++i)
496197ba8fc5SSepherosa Ziehau 		bnx_writembx(sc, sc->bnx_intr_data[i].bnx_intr_mbx, 1);
49626c8d8eccSSepherosa Ziehau 
4963f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
4964f33ac8a4SSepherosa Ziehau 		lwkt_serialize_handler_disable(
4965f33ac8a4SSepherosa Ziehau 		    sc->bnx_intr_data[i].bnx_intr_serialize);
4966f33ac8a4SSepherosa Ziehau 	}
49676c8d8eccSSepherosa Ziehau }
49686c8d8eccSSepherosa Ziehau 
49696c8d8eccSSepherosa Ziehau static int
bnx_get_eaddr_mem(struct bnx_softc * sc,uint8_t ether_addr[])49706c8d8eccSSepherosa Ziehau bnx_get_eaddr_mem(struct bnx_softc *sc, uint8_t ether_addr[])
49716c8d8eccSSepherosa Ziehau {
49726c8d8eccSSepherosa Ziehau 	uint32_t mac_addr;
49736c8d8eccSSepherosa Ziehau 	int ret = 1;
49746c8d8eccSSepherosa Ziehau 
49756c8d8eccSSepherosa Ziehau 	mac_addr = bnx_readmem_ind(sc, 0x0c14);
49766c8d8eccSSepherosa Ziehau 	if ((mac_addr >> 16) == 0x484b) {
49776c8d8eccSSepherosa Ziehau 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
49786c8d8eccSSepherosa Ziehau 		ether_addr[1] = (uint8_t)mac_addr;
49796c8d8eccSSepherosa Ziehau 		mac_addr = bnx_readmem_ind(sc, 0x0c18);
49806c8d8eccSSepherosa Ziehau 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
49816c8d8eccSSepherosa Ziehau 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
49826c8d8eccSSepherosa Ziehau 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
49836c8d8eccSSepherosa Ziehau 		ether_addr[5] = (uint8_t)mac_addr;
49846c8d8eccSSepherosa Ziehau 		ret = 0;
49856c8d8eccSSepherosa Ziehau 	}
49866c8d8eccSSepherosa Ziehau 	return ret;
49876c8d8eccSSepherosa Ziehau }
49886c8d8eccSSepherosa Ziehau 
49896c8d8eccSSepherosa Ziehau static int
bnx_get_eaddr_nvram(struct bnx_softc * sc,uint8_t ether_addr[])49906c8d8eccSSepherosa Ziehau bnx_get_eaddr_nvram(struct bnx_softc *sc, uint8_t ether_addr[])
49916c8d8eccSSepherosa Ziehau {
49926c8d8eccSSepherosa Ziehau 	int mac_offset = BGE_EE_MAC_OFFSET;
49936c8d8eccSSepherosa Ziehau 
499480969639SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
499580969639SSepherosa Ziehau 		int f;
499680969639SSepherosa Ziehau 
499780969639SSepherosa Ziehau 		f = pci_get_function(sc->bnx_dev);
499880969639SSepherosa Ziehau 		if (f & 1)
499980969639SSepherosa Ziehau 			mac_offset = BGE_EE_MAC_OFFSET_5717;
500080969639SSepherosa Ziehau 		if (f > 1)
500180969639SSepherosa Ziehau 			mac_offset += BGE_EE_MAC_OFFSET_5717_OFF;
500280969639SSepherosa Ziehau 	}
50036c8d8eccSSepherosa Ziehau 
50046c8d8eccSSepherosa Ziehau 	return bnx_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
50056c8d8eccSSepherosa Ziehau }
50066c8d8eccSSepherosa Ziehau 
50076c8d8eccSSepherosa Ziehau static int
bnx_get_eaddr_eeprom(struct bnx_softc * sc,uint8_t ether_addr[])50086c8d8eccSSepherosa Ziehau bnx_get_eaddr_eeprom(struct bnx_softc *sc, uint8_t ether_addr[])
50096c8d8eccSSepherosa Ziehau {
50106c8d8eccSSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_NO_EEPROM)
50116c8d8eccSSepherosa Ziehau 		return 1;
50126c8d8eccSSepherosa Ziehau 
50136c8d8eccSSepherosa Ziehau 	return bnx_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
50146c8d8eccSSepherosa Ziehau 			       ETHER_ADDR_LEN);
50156c8d8eccSSepherosa Ziehau }
50166c8d8eccSSepherosa Ziehau 
50176c8d8eccSSepherosa Ziehau static int
bnx_get_eaddr(struct bnx_softc * sc,uint8_t eaddr[])50186c8d8eccSSepherosa Ziehau bnx_get_eaddr(struct bnx_softc *sc, uint8_t eaddr[])
50196c8d8eccSSepherosa Ziehau {
50206c8d8eccSSepherosa Ziehau 	static const bnx_eaddr_fcn_t bnx_eaddr_funcs[] = {
50216c8d8eccSSepherosa Ziehau 		/* NOTE: Order is critical */
50226c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_mem,
50236c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_nvram,
50246c8d8eccSSepherosa Ziehau 		bnx_get_eaddr_eeprom,
50256c8d8eccSSepherosa Ziehau 		NULL
50266c8d8eccSSepherosa Ziehau 	};
50276c8d8eccSSepherosa Ziehau 	const bnx_eaddr_fcn_t *func;
50286c8d8eccSSepherosa Ziehau 
50296c8d8eccSSepherosa Ziehau 	for (func = bnx_eaddr_funcs; *func != NULL; ++func) {
50306c8d8eccSSepherosa Ziehau 		if ((*func)(sc, eaddr) == 0)
50316c8d8eccSSepherosa Ziehau 			break;
50326c8d8eccSSepherosa Ziehau 	}
50336c8d8eccSSepherosa Ziehau 	return (*func == NULL ? ENXIO : 0);
50346c8d8eccSSepherosa Ziehau }
50356c8d8eccSSepherosa Ziehau 
50366c8d8eccSSepherosa Ziehau /*
50376c8d8eccSSepherosa Ziehau  * NOTE: 'm' is not freed upon failure
50386c8d8eccSSepherosa Ziehau  */
50398406cf70SSascha Wildner static struct mbuf *
bnx_defrag_shortdma(struct mbuf * m)50406c8d8eccSSepherosa Ziehau bnx_defrag_shortdma(struct mbuf *m)
50416c8d8eccSSepherosa Ziehau {
50426c8d8eccSSepherosa Ziehau 	struct mbuf *n;
50436c8d8eccSSepherosa Ziehau 	int found;
50446c8d8eccSSepherosa Ziehau 
50456c8d8eccSSepherosa Ziehau 	/*
50466c8d8eccSSepherosa Ziehau 	 * If device receive two back-to-back send BDs with less than
50476c8d8eccSSepherosa Ziehau 	 * or equal to 8 total bytes then the device may hang.  The two
50486c8d8eccSSepherosa Ziehau 	 * back-to-back send BDs must in the same frame for this failure
50496c8d8eccSSepherosa Ziehau 	 * to occur.  Scan mbuf chains and see whether two back-to-back
50506c8d8eccSSepherosa Ziehau 	 * send BDs are there.  If this is the case, allocate new mbuf
50516c8d8eccSSepherosa Ziehau 	 * and copy the frame to workaround the silicon bug.
50526c8d8eccSSepherosa Ziehau 	 */
50536c8d8eccSSepherosa Ziehau 	for (n = m, found = 0; n != NULL; n = n->m_next) {
50546c8d8eccSSepherosa Ziehau 		if (n->m_len < 8) {
50556c8d8eccSSepherosa Ziehau 			found++;
50566c8d8eccSSepherosa Ziehau 			if (found > 1)
50576c8d8eccSSepherosa Ziehau 				break;
50586c8d8eccSSepherosa Ziehau 			continue;
50596c8d8eccSSepherosa Ziehau 		}
50606c8d8eccSSepherosa Ziehau 		found = 0;
50616c8d8eccSSepherosa Ziehau 	}
50626c8d8eccSSepherosa Ziehau 
50636c8d8eccSSepherosa Ziehau 	if (found > 1)
5064b5523eacSSascha Wildner 		n = m_defrag(m, M_NOWAIT);
50656c8d8eccSSepherosa Ziehau 	else
50666c8d8eccSSepherosa Ziehau 		n = m;
50676c8d8eccSSepherosa Ziehau 	return n;
50686c8d8eccSSepherosa Ziehau }
50696c8d8eccSSepherosa Ziehau 
50706c8d8eccSSepherosa Ziehau static void
bnx_stop_block(struct bnx_softc * sc,bus_size_t reg,uint32_t bit)50716c8d8eccSSepherosa Ziehau bnx_stop_block(struct bnx_softc *sc, bus_size_t reg, uint32_t bit)
50726c8d8eccSSepherosa Ziehau {
50736c8d8eccSSepherosa Ziehau 	int i;
50746c8d8eccSSepherosa Ziehau 
50756c8d8eccSSepherosa Ziehau 	BNX_CLRBIT(sc, reg, bit);
50766c8d8eccSSepherosa Ziehau 	for (i = 0; i < BNX_TIMEOUT; i++) {
50776c8d8eccSSepherosa Ziehau 		if ((CSR_READ_4(sc, reg) & bit) == 0)
50786c8d8eccSSepherosa Ziehau 			return;
50796c8d8eccSSepherosa Ziehau 		DELAY(100);
50806c8d8eccSSepherosa Ziehau 	}
50816c8d8eccSSepherosa Ziehau }
50826c8d8eccSSepherosa Ziehau 
50836c8d8eccSSepherosa Ziehau static void
bnx_link_poll(struct bnx_softc * sc)50846c8d8eccSSepherosa Ziehau bnx_link_poll(struct bnx_softc *sc)
50856c8d8eccSSepherosa Ziehau {
50866c8d8eccSSepherosa Ziehau 	uint32_t status;
50876c8d8eccSSepherosa Ziehau 
50886c8d8eccSSepherosa Ziehau 	status = CSR_READ_4(sc, BGE_MAC_STS);
50896c8d8eccSSepherosa Ziehau 	if ((status & sc->bnx_link_chg) || sc->bnx_link_evt) {
50906c8d8eccSSepherosa Ziehau 		sc->bnx_link_evt = 0;
50916c8d8eccSSepherosa Ziehau 		sc->bnx_link_upd(sc, status);
50926c8d8eccSSepherosa Ziehau 	}
50936c8d8eccSSepherosa Ziehau }
50946c8d8eccSSepherosa Ziehau 
50956c8d8eccSSepherosa Ziehau static void
bnx_enable_msi(struct bnx_softc * sc,boolean_t is_msix)5096695a8586SSepherosa Ziehau bnx_enable_msi(struct bnx_softc *sc, boolean_t is_msix)
50976c8d8eccSSepherosa Ziehau {
50986c8d8eccSSepherosa Ziehau 	uint32_t msi_mode;
50996c8d8eccSSepherosa Ziehau 
51006c8d8eccSSepherosa Ziehau 	msi_mode = CSR_READ_4(sc, BGE_MSI_MODE);
51016c8d8eccSSepherosa Ziehau 	msi_mode |= BGE_MSIMODE_ENABLE;
51026c8d8eccSSepherosa Ziehau 	/*
51036c8d8eccSSepherosa Ziehau 	 * NOTE:
51040b4feeacSSepherosa Ziehau 	 * 5718-PG105-R says that "one shot" mode does not work
51050b4feeacSSepherosa Ziehau 	 * if MSI is used, however, it obviously works.
51066c8d8eccSSepherosa Ziehau 	 */
51076c8d8eccSSepherosa Ziehau 	msi_mode &= ~BGE_MSIMODE_ONESHOT_DISABLE;
5108695a8586SSepherosa Ziehau 	if (is_msix)
5109695a8586SSepherosa Ziehau 		msi_mode |= BGE_MSIMODE_MSIX_MULTIMODE;
5110695a8586SSepherosa Ziehau 	else
5111695a8586SSepherosa Ziehau 		msi_mode &= ~BGE_MSIMODE_MSIX_MULTIMODE;
51126c8d8eccSSepherosa Ziehau 	CSR_WRITE_4(sc, BGE_MSI_MODE, msi_mode);
51136c8d8eccSSepherosa Ziehau }
51146c8d8eccSSepherosa Ziehau 
51156c8d8eccSSepherosa Ziehau static uint32_t
bnx_dma_swap_options(struct bnx_softc * sc)51166c8d8eccSSepherosa Ziehau bnx_dma_swap_options(struct bnx_softc *sc)
51176c8d8eccSSepherosa Ziehau {
51186c8d8eccSSepherosa Ziehau 	uint32_t dma_options;
51196c8d8eccSSepherosa Ziehau 
51206c8d8eccSSepherosa Ziehau 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
51216c8d8eccSSepherosa Ziehau 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
51226c8d8eccSSepherosa Ziehau #if BYTE_ORDER == BIG_ENDIAN
51236c8d8eccSSepherosa Ziehau 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
51246c8d8eccSSepherosa Ziehau #endif
51256c8d8eccSSepherosa Ziehau 	return dma_options;
51266c8d8eccSSepherosa Ziehau }
512766deb1c1SSepherosa Ziehau 
512866deb1c1SSepherosa Ziehau static int
bnx_setup_tso(struct bnx_tx_ring * txr,struct mbuf ** mp,uint16_t * mss0,uint16_t * flags0)512933a04907SSepherosa Ziehau bnx_setup_tso(struct bnx_tx_ring *txr, struct mbuf **mp,
513066deb1c1SSepherosa Ziehau     uint16_t *mss0, uint16_t *flags0)
513166deb1c1SSepherosa Ziehau {
513266deb1c1SSepherosa Ziehau 	struct mbuf *m;
513366deb1c1SSepherosa Ziehau 	struct ip *ip;
513466deb1c1SSepherosa Ziehau 	struct tcphdr *th;
513566deb1c1SSepherosa Ziehau 	int thoff, iphlen, hoff, hlen;
513666deb1c1SSepherosa Ziehau 	uint16_t flags, mss;
513766deb1c1SSepherosa Ziehau 
5138f7a2269aSSepherosa Ziehau 	m = *mp;
5139f7a2269aSSepherosa Ziehau 	KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
5140f7a2269aSSepherosa Ziehau 
5141f7a2269aSSepherosa Ziehau 	hoff = m->m_pkthdr.csum_lhlen;
5142f7a2269aSSepherosa Ziehau 	iphlen = m->m_pkthdr.csum_iphlen;
5143f7a2269aSSepherosa Ziehau 	thoff = m->m_pkthdr.csum_thlen;
5144f7a2269aSSepherosa Ziehau 
5145f7a2269aSSepherosa Ziehau 	KASSERT(hoff > 0, ("invalid ether header len"));
5146f7a2269aSSepherosa Ziehau 	KASSERT(iphlen > 0, ("invalid ip header len"));
5147f7a2269aSSepherosa Ziehau 	KASSERT(thoff > 0, ("invalid tcp header len"));
5148f7a2269aSSepherosa Ziehau 
5149f7a2269aSSepherosa Ziehau 	if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
5150f7a2269aSSepherosa Ziehau 		m = m_pullup(m, hoff + iphlen + thoff);
5151f7a2269aSSepherosa Ziehau 		if (m == NULL) {
5152f7a2269aSSepherosa Ziehau 			*mp = NULL;
5153f7a2269aSSepherosa Ziehau 			return ENOBUFS;
5154f7a2269aSSepherosa Ziehau 		}
5155f7a2269aSSepherosa Ziehau 		*mp = m;
5156f7a2269aSSepherosa Ziehau 	}
5157f7a2269aSSepherosa Ziehau 	ip = mtodoff(m, struct ip *, hoff);
5158f7a2269aSSepherosa Ziehau 	th = mtodoff(m, struct tcphdr *, hoff + iphlen);
5159f7a2269aSSepherosa Ziehau 
5160f0336d39SSepherosa Ziehau 	mss = m->m_pkthdr.tso_segsz;
516166deb1c1SSepherosa Ziehau 	flags = BGE_TXBDFLAG_CPU_PRE_DMA | BGE_TXBDFLAG_CPU_POST_DMA;
516266deb1c1SSepherosa Ziehau 
516366deb1c1SSepherosa Ziehau 	ip->ip_len = htons(mss + iphlen + thoff);
516466deb1c1SSepherosa Ziehau 	th->th_sum = 0;
516566deb1c1SSepherosa Ziehau 
516666deb1c1SSepherosa Ziehau 	hlen = (iphlen + thoff) >> 2;
516766deb1c1SSepherosa Ziehau 	mss |= ((hlen & 0x3) << 14);
516866deb1c1SSepherosa Ziehau 	flags |= ((hlen & 0xf8) << 7) | ((hlen & 0x4) << 2);
516966deb1c1SSepherosa Ziehau 
517066deb1c1SSepherosa Ziehau 	*mss0 = mss;
517166deb1c1SSepherosa Ziehau 	*flags0 = flags;
517266deb1c1SSepherosa Ziehau 
517366deb1c1SSepherosa Ziehau 	return 0;
517466deb1c1SSepherosa Ziehau }
517533a04907SSepherosa Ziehau 
517633a04907SSepherosa Ziehau static int
bnx_create_tx_ring(struct bnx_tx_ring * txr)517733a04907SSepherosa Ziehau bnx_create_tx_ring(struct bnx_tx_ring *txr)
517833a04907SSepherosa Ziehau {
517933a04907SSepherosa Ziehau 	bus_size_t txmaxsz, txmaxsegsz;
518033a04907SSepherosa Ziehau 	int i, error;
518133a04907SSepherosa Ziehau 
5182329f9016SSepherosa Ziehau 	lwkt_serialize_init(&txr->bnx_tx_serialize);
5183329f9016SSepherosa Ziehau 
518433a04907SSepherosa Ziehau 	/*
518533a04907SSepherosa Ziehau 	 * Create DMA tag and maps for TX mbufs.
518633a04907SSepherosa Ziehau 	 */
518733a04907SSepherosa Ziehau 	if (txr->bnx_sc->bnx_flags & BNX_FLAG_TSO)
518833a04907SSepherosa Ziehau 		txmaxsz = IP_MAXPACKET + sizeof(struct ether_vlan_header);
518933a04907SSepherosa Ziehau 	else
519033a04907SSepherosa Ziehau 		txmaxsz = BNX_JUMBO_FRAMELEN;
519133a04907SSepherosa Ziehau 	if (txr->bnx_sc->bnx_asicrev == BGE_ASICREV_BCM57766)
519233a04907SSepherosa Ziehau 		txmaxsegsz = MCLBYTES;
519333a04907SSepherosa Ziehau 	else
519433a04907SSepherosa Ziehau 		txmaxsegsz = PAGE_SIZE;
519533a04907SSepherosa Ziehau 	error = bus_dma_tag_create(txr->bnx_sc->bnx_cdata.bnx_parent_tag,
5196*030b0c8cSMichael Neumann 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
519733a04907SSepherosa Ziehau 	    txmaxsz, BNX_NSEG_NEW, txmaxsegsz,
519833a04907SSepherosa Ziehau 	    BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
519933a04907SSepherosa Ziehau 	    &txr->bnx_tx_mtag);
520033a04907SSepherosa Ziehau 	if (error) {
520133a04907SSepherosa Ziehau 		device_printf(txr->bnx_sc->bnx_dev,
5202beedf5beSSepherosa Ziehau 		    "could not create TX mbuf DMA tag\n");
520333a04907SSepherosa Ziehau 		return error;
520433a04907SSepherosa Ziehau 	}
520533a04907SSepherosa Ziehau 
520633a04907SSepherosa Ziehau 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
520733a04907SSepherosa Ziehau 		error = bus_dmamap_create(txr->bnx_tx_mtag,
520833a04907SSepherosa Ziehau 		    BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
5209fa4b1067SSepherosa Ziehau 		    &txr->bnx_tx_buf[i].bnx_tx_dmamap);
521033a04907SSepherosa Ziehau 		if (error) {
521133a04907SSepherosa Ziehau 			int j;
521233a04907SSepherosa Ziehau 
521333a04907SSepherosa Ziehau 			for (j = 0; j < i; ++j) {
521433a04907SSepherosa Ziehau 				bus_dmamap_destroy(txr->bnx_tx_mtag,
5215fa4b1067SSepherosa Ziehau 				    txr->bnx_tx_buf[j].bnx_tx_dmamap);
521633a04907SSepherosa Ziehau 			}
521733a04907SSepherosa Ziehau 			bus_dma_tag_destroy(txr->bnx_tx_mtag);
521833a04907SSepherosa Ziehau 			txr->bnx_tx_mtag = NULL;
521933a04907SSepherosa Ziehau 
522033a04907SSepherosa Ziehau 			device_printf(txr->bnx_sc->bnx_dev,
5221beedf5beSSepherosa Ziehau 			    "could not create TX mbuf DMA map\n");
522233a04907SSepherosa Ziehau 			return error;
522333a04907SSepherosa Ziehau 		}
522433a04907SSepherosa Ziehau 	}
522533a04907SSepherosa Ziehau 
522633a04907SSepherosa Ziehau 	/*
522733a04907SSepherosa Ziehau 	 * Create DMA stuffs for TX ring.
522833a04907SSepherosa Ziehau 	 */
522933a04907SSepherosa Ziehau 	error = bnx_dma_block_alloc(txr->bnx_sc, BGE_TX_RING_SZ,
5230beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_tag,
5231beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_map,
5232beedf5beSSepherosa Ziehau 	    (void *)&txr->bnx_tx_ring,
5233beedf5beSSepherosa Ziehau 	    &txr->bnx_tx_ring_paddr);
523433a04907SSepherosa Ziehau 	if (error) {
523533a04907SSepherosa Ziehau 		device_printf(txr->bnx_sc->bnx_dev,
523633a04907SSepherosa Ziehau 		    "could not create TX ring\n");
523733a04907SSepherosa Ziehau 		return error;
523833a04907SSepherosa Ziehau 	}
523933a04907SSepherosa Ziehau 
524079a64343SSepherosa Ziehau 	txr->bnx_tx_flags |= BNX_TX_FLAG_SHORTDMA;
524133a04907SSepherosa Ziehau 	txr->bnx_tx_wreg = BNX_TX_WREG_NSEGS;
524233a04907SSepherosa Ziehau 
524333a04907SSepherosa Ziehau 	return 0;
524433a04907SSepherosa Ziehau }
524533a04907SSepherosa Ziehau 
524633a04907SSepherosa Ziehau static void
bnx_destroy_tx_ring(struct bnx_tx_ring * txr)524733a04907SSepherosa Ziehau bnx_destroy_tx_ring(struct bnx_tx_ring *txr)
524833a04907SSepherosa Ziehau {
524933a04907SSepherosa Ziehau 	/* Destroy TX mbuf DMA stuffs. */
525033a04907SSepherosa Ziehau 	if (txr->bnx_tx_mtag != NULL) {
525133a04907SSepherosa Ziehau 		int i;
525233a04907SSepherosa Ziehau 
525333a04907SSepherosa Ziehau 		for (i = 0; i < BGE_TX_RING_CNT; i++) {
5254fa4b1067SSepherosa Ziehau 			KKASSERT(txr->bnx_tx_buf[i].bnx_tx_mbuf == NULL);
525533a04907SSepherosa Ziehau 			bus_dmamap_destroy(txr->bnx_tx_mtag,
5256fa4b1067SSepherosa Ziehau 			    txr->bnx_tx_buf[i].bnx_tx_dmamap);
525733a04907SSepherosa Ziehau 		}
525833a04907SSepherosa Ziehau 		bus_dma_tag_destroy(txr->bnx_tx_mtag);
525933a04907SSepherosa Ziehau 	}
526033a04907SSepherosa Ziehau 
526133a04907SSepherosa Ziehau 	/* Destroy TX ring */
526233a04907SSepherosa Ziehau 	bnx_dma_block_free(txr->bnx_tx_ring_tag,
526333a04907SSepherosa Ziehau 	    txr->bnx_tx_ring_map, txr->bnx_tx_ring);
526433a04907SSepherosa Ziehau }
5265aad4de2bSSepherosa Ziehau 
5266aad4de2bSSepherosa Ziehau static int
bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS)5267aad4de2bSSepherosa Ziehau bnx_sysctl_force_defrag(SYSCTL_HANDLER_ARGS)
5268aad4de2bSSepherosa Ziehau {
5269aad4de2bSSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
5270aad4de2bSSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
5271aad4de2bSSepherosa Ziehau 	struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
5272aad4de2bSSepherosa Ziehau 	int error, defrag, i;
5273aad4de2bSSepherosa Ziehau 
5274aad4de2bSSepherosa Ziehau 	if (txr->bnx_tx_flags & BNX_TX_FLAG_FORCE_DEFRAG)
5275aad4de2bSSepherosa Ziehau 		defrag = 1;
5276aad4de2bSSepherosa Ziehau 	else
5277aad4de2bSSepherosa Ziehau 		defrag = 0;
5278aad4de2bSSepherosa Ziehau 
5279aad4de2bSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &defrag, 0, req);
5280aad4de2bSSepherosa Ziehau 	if (error || req->newptr == NULL)
5281aad4de2bSSepherosa Ziehau 		return error;
5282aad4de2bSSepherosa Ziehau 
5283329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
5284aad4de2bSSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i) {
5285aad4de2bSSepherosa Ziehau 		txr = &sc->bnx_tx_ring[i];
5286aad4de2bSSepherosa Ziehau 		if (defrag)
5287aad4de2bSSepherosa Ziehau 			txr->bnx_tx_flags |= BNX_TX_FLAG_FORCE_DEFRAG;
5288aad4de2bSSepherosa Ziehau 		else
5289aad4de2bSSepherosa Ziehau 			txr->bnx_tx_flags &= ~BNX_TX_FLAG_FORCE_DEFRAG;
5290aad4de2bSSepherosa Ziehau 	}
5291329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
5292aad4de2bSSepherosa Ziehau 
5293aad4de2bSSepherosa Ziehau 	return 0;
5294aad4de2bSSepherosa Ziehau }
5295472c99c8SSepherosa Ziehau 
5296472c99c8SSepherosa Ziehau static int
bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS)5297472c99c8SSepherosa Ziehau bnx_sysctl_tx_wreg(SYSCTL_HANDLER_ARGS)
5298472c99c8SSepherosa Ziehau {
5299472c99c8SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
5300472c99c8SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
5301472c99c8SSepherosa Ziehau 	struct bnx_tx_ring *txr = &sc->bnx_tx_ring[0];
5302472c99c8SSepherosa Ziehau 	int error, tx_wreg, i;
5303472c99c8SSepherosa Ziehau 
5304472c99c8SSepherosa Ziehau 	tx_wreg = txr->bnx_tx_wreg;
5305472c99c8SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &tx_wreg, 0, req);
5306472c99c8SSepherosa Ziehau 	if (error || req->newptr == NULL)
5307472c99c8SSepherosa Ziehau 		return error;
5308472c99c8SSepherosa Ziehau 
5309329f9016SSepherosa Ziehau 	ifnet_serialize_all(ifp);
5310472c99c8SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
5311472c99c8SSepherosa Ziehau 		sc->bnx_tx_ring[i].bnx_tx_wreg = tx_wreg;
5312329f9016SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
5313472c99c8SSepherosa Ziehau 
5314472c99c8SSepherosa Ziehau 	return 0;
5315472c99c8SSepherosa Ziehau }
5316beedf5beSSepherosa Ziehau 
5317beedf5beSSepherosa Ziehau static int
bnx_create_rx_ret_ring(struct bnx_rx_ret_ring * ret)5318beedf5beSSepherosa Ziehau bnx_create_rx_ret_ring(struct bnx_rx_ret_ring *ret)
5319beedf5beSSepherosa Ziehau {
5320beedf5beSSepherosa Ziehau 	int error;
5321beedf5beSSepherosa Ziehau 
5322329f9016SSepherosa Ziehau 	lwkt_serialize_init(&ret->bnx_rx_ret_serialize);
5323329f9016SSepherosa Ziehau 
5324beedf5beSSepherosa Ziehau 	/*
5325beedf5beSSepherosa Ziehau 	 * Create DMA stuffs for RX return ring.
5326beedf5beSSepherosa Ziehau 	 */
5327beedf5beSSepherosa Ziehau 	error = bnx_dma_block_alloc(ret->bnx_sc,
5328beedf5beSSepherosa Ziehau 	    BGE_RX_RTN_RING_SZ(BNX_RETURN_RING_CNT),
5329beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_tag,
5330beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_map,
5331beedf5beSSepherosa Ziehau 	    (void *)&ret->bnx_rx_ret_ring,
5332beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_ret_ring_paddr);
5333beedf5beSSepherosa Ziehau 	if (error) {
5334beedf5beSSepherosa Ziehau 		device_printf(ret->bnx_sc->bnx_dev,
5335beedf5beSSepherosa Ziehau 		    "could not create RX ret ring\n");
5336beedf5beSSepherosa Ziehau 		return error;
5337beedf5beSSepherosa Ziehau 	}
5338beedf5beSSepherosa Ziehau 
5339beedf5beSSepherosa Ziehau 	/* Shadow standard ring's RX mbuf DMA tag */
5340beedf5beSSepherosa Ziehau 	ret->bnx_rx_mtag = ret->bnx_std->bnx_rx_mtag;
5341beedf5beSSepherosa Ziehau 
5342beedf5beSSepherosa Ziehau 	/*
5343beedf5beSSepherosa Ziehau 	 * Create tmp DMA map for RX mbufs.
5344beedf5beSSepherosa Ziehau 	 */
5345beedf5beSSepherosa Ziehau 	error = bus_dmamap_create(ret->bnx_rx_mtag, BUS_DMA_WAITOK,
5346beedf5beSSepherosa Ziehau 	    &ret->bnx_rx_tmpmap);
5347beedf5beSSepherosa Ziehau 	if (error) {
5348beedf5beSSepherosa Ziehau 		device_printf(ret->bnx_sc->bnx_dev,
5349beedf5beSSepherosa Ziehau 		    "could not create tmp RX mbuf DMA map\n");
5350beedf5beSSepherosa Ziehau 		ret->bnx_rx_mtag = NULL;
5351beedf5beSSepherosa Ziehau 		return error;
5352beedf5beSSepherosa Ziehau 	}
5353beedf5beSSepherosa Ziehau 	return 0;
5354beedf5beSSepherosa Ziehau }
5355beedf5beSSepherosa Ziehau 
5356beedf5beSSepherosa Ziehau static void
bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring * ret)5357beedf5beSSepherosa Ziehau bnx_destroy_rx_ret_ring(struct bnx_rx_ret_ring *ret)
5358beedf5beSSepherosa Ziehau {
5359beedf5beSSepherosa Ziehau 	/* Destroy tmp RX mbuf DMA map */
5360beedf5beSSepherosa Ziehau 	if (ret->bnx_rx_mtag != NULL)
5361beedf5beSSepherosa Ziehau 		bus_dmamap_destroy(ret->bnx_rx_mtag, ret->bnx_rx_tmpmap);
5362beedf5beSSepherosa Ziehau 
5363beedf5beSSepherosa Ziehau 	/* Destroy RX return ring */
5364beedf5beSSepherosa Ziehau 	bnx_dma_block_free(ret->bnx_rx_ret_ring_tag,
5365beedf5beSSepherosa Ziehau 	    ret->bnx_rx_ret_ring_map, ret->bnx_rx_ret_ring);
5366beedf5beSSepherosa Ziehau }
53670c7da01dSSepherosa Ziehau 
53680c7da01dSSepherosa Ziehau static int
bnx_alloc_intr(struct bnx_softc * sc)53690c7da01dSSepherosa Ziehau bnx_alloc_intr(struct bnx_softc *sc)
53700c7da01dSSepherosa Ziehau {
5371f33ac8a4SSepherosa Ziehau 	struct bnx_intr_data *intr;
53720c7da01dSSepherosa Ziehau 	u_int intr_flags;
5373695a8586SSepherosa Ziehau 	int error;
5374695a8586SSepherosa Ziehau 
5375695a8586SSepherosa Ziehau 	if (sc->bnx_intr_cnt > 1) {
5376695a8586SSepherosa Ziehau 		error = bnx_alloc_msix(sc);
5377695a8586SSepherosa Ziehau 		if (error)
5378695a8586SSepherosa Ziehau 			return error;
5379695a8586SSepherosa Ziehau 		KKASSERT(sc->bnx_intr_type == PCI_INTR_TYPE_MSIX);
5380695a8586SSepherosa Ziehau 		return 0;
5381695a8586SSepherosa Ziehau 	}
53820c7da01dSSepherosa Ziehau 
53830a806e3aSSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt == 1);
53840c7da01dSSepherosa Ziehau 
5385f33ac8a4SSepherosa Ziehau 	intr = &sc->bnx_intr_data[0];
5386f33ac8a4SSepherosa Ziehau 	intr->bnx_ret = &sc->bnx_rx_ret_ring[0];
5387f33ac8a4SSepherosa Ziehau 	intr->bnx_txr = &sc->bnx_tx_ring[0];
5388f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5389695a8586SSepherosa Ziehau 	intr->bnx_intr_check = bnx_check_intr_rxtx;
53904fa38985SSepherosa Ziehau 	intr->bnx_saved_status_tag = &intr->bnx_ret->bnx_saved_status_tag;
5391f33ac8a4SSepherosa Ziehau 
5392f33ac8a4SSepherosa Ziehau 	sc->bnx_intr_type = pci_alloc_1intr(sc->bnx_dev, bnx_msi_enable,
5393f33ac8a4SSepherosa Ziehau 	    &intr->bnx_intr_rid, &intr_flags);
5394f33ac8a4SSepherosa Ziehau 
5395f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_res = bus_alloc_resource_any(sc->bnx_dev, SYS_RES_IRQ,
5396f33ac8a4SSepherosa Ziehau 	    &intr->bnx_intr_rid, intr_flags);
5397f33ac8a4SSepherosa Ziehau 	if (intr->bnx_intr_res == NULL) {
53980c7da01dSSepherosa Ziehau 		device_printf(sc->bnx_dev, "could not alloc interrupt\n");
53990c7da01dSSepherosa Ziehau 		return ENXIO;
54000c7da01dSSepherosa Ziehau 	}
54010c7da01dSSepherosa Ziehau 
5402f33ac8a4SSepherosa Ziehau 	if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI) {
5403695a8586SSepherosa Ziehau 		bnx_enable_msi(sc, FALSE);
540403cc99fdSSepherosa Ziehau 		intr->bnx_intr_func = bnx_msi;
5405f33ac8a4SSepherosa Ziehau 		if (bootverbose)
5406f33ac8a4SSepherosa Ziehau 			device_printf(sc->bnx_dev, "oneshot MSI\n");
5407f33ac8a4SSepherosa Ziehau 	} else {
5408f33ac8a4SSepherosa Ziehau 		intr->bnx_intr_func = bnx_intr_legacy;
5409f33ac8a4SSepherosa Ziehau 	}
5410f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_arg = sc;
5411f33ac8a4SSepherosa Ziehau 	intr->bnx_intr_cpuid = rman_get_cpuid(intr->bnx_intr_res);
5412f33ac8a4SSepherosa Ziehau 
5413f33ac8a4SSepherosa Ziehau 	intr->bnx_txr->bnx_tx_cpuid = intr->bnx_intr_cpuid;
5414f33ac8a4SSepherosa Ziehau 
54150c7da01dSSepherosa Ziehau 	return 0;
54160c7da01dSSepherosa Ziehau }
54170c7da01dSSepherosa Ziehau 
54180c7da01dSSepherosa Ziehau static int
bnx_setup_intr(struct bnx_softc * sc)54190c7da01dSSepherosa Ziehau bnx_setup_intr(struct bnx_softc *sc)
54200c7da01dSSepherosa Ziehau {
5421f33ac8a4SSepherosa Ziehau 	int error, i;
54220c7da01dSSepherosa Ziehau 
5423f33ac8a4SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
5424f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
5425f33ac8a4SSepherosa Ziehau 
5426f33ac8a4SSepherosa Ziehau 		error = bus_setup_intr_descr(sc->bnx_dev, intr->bnx_intr_res,
5427f33ac8a4SSepherosa Ziehau 		    INTR_MPSAFE, intr->bnx_intr_func, intr->bnx_intr_arg,
5428f33ac8a4SSepherosa Ziehau 		    &intr->bnx_intr_hand, intr->bnx_intr_serialize,
5429f33ac8a4SSepherosa Ziehau 		    intr->bnx_intr_desc);
54300c7da01dSSepherosa Ziehau 		if (error) {
5431f33ac8a4SSepherosa Ziehau 			device_printf(sc->bnx_dev,
5432f33ac8a4SSepherosa Ziehau 			    "could not set up %dth intr\n", i);
5433f33ac8a4SSepherosa Ziehau 			bnx_teardown_intr(sc, i);
54340c7da01dSSepherosa Ziehau 			return error;
54350c7da01dSSepherosa Ziehau 		}
5436f33ac8a4SSepherosa Ziehau 	}
54370c7da01dSSepherosa Ziehau 	return 0;
54380c7da01dSSepherosa Ziehau }
54390c7da01dSSepherosa Ziehau 
54400c7da01dSSepherosa Ziehau static void
bnx_teardown_intr(struct bnx_softc * sc,int cnt)5441f33ac8a4SSepherosa Ziehau bnx_teardown_intr(struct bnx_softc *sc, int cnt)
5442f33ac8a4SSepherosa Ziehau {
5443f33ac8a4SSepherosa Ziehau 	int i;
5444f33ac8a4SSepherosa Ziehau 
5445f33ac8a4SSepherosa Ziehau 	for (i = 0; i < cnt; ++i) {
5446f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
5447f33ac8a4SSepherosa Ziehau 
5448f33ac8a4SSepherosa Ziehau 		bus_teardown_intr(sc->bnx_dev, intr->bnx_intr_res,
5449f33ac8a4SSepherosa Ziehau 		    intr->bnx_intr_hand);
5450f33ac8a4SSepherosa Ziehau 	}
5451f33ac8a4SSepherosa Ziehau }
5452f33ac8a4SSepherosa Ziehau 
5453f33ac8a4SSepherosa Ziehau static void
bnx_free_intr(struct bnx_softc * sc)54540c7da01dSSepherosa Ziehau bnx_free_intr(struct bnx_softc *sc)
54550c7da01dSSepherosa Ziehau {
5456695a8586SSepherosa Ziehau 	if (sc->bnx_intr_type != PCI_INTR_TYPE_MSIX) {
5457f33ac8a4SSepherosa Ziehau 		struct bnx_intr_data *intr;
5458f33ac8a4SSepherosa Ziehau 
5459f33ac8a4SSepherosa Ziehau 		KKASSERT(sc->bnx_intr_cnt <= 1);
5460f33ac8a4SSepherosa Ziehau 		intr = &sc->bnx_intr_data[0];
5461f33ac8a4SSepherosa Ziehau 
5462f33ac8a4SSepherosa Ziehau 		if (intr->bnx_intr_res != NULL) {
54630c7da01dSSepherosa Ziehau 			bus_release_resource(sc->bnx_dev, SYS_RES_IRQ,
5464f33ac8a4SSepherosa Ziehau 			    intr->bnx_intr_rid, intr->bnx_intr_res);
54650c7da01dSSepherosa Ziehau 		}
5466f33ac8a4SSepherosa Ziehau 		if (sc->bnx_intr_type == PCI_INTR_TYPE_MSI)
54670c7da01dSSepherosa Ziehau 			pci_release_msi(sc->bnx_dev);
5468695a8586SSepherosa Ziehau 	} else {
5469695a8586SSepherosa Ziehau 		bnx_free_msix(sc, TRUE);
5470695a8586SSepherosa Ziehau 	}
54710c7da01dSSepherosa Ziehau }
5472329f9016SSepherosa Ziehau 
5473329f9016SSepherosa Ziehau static void
bnx_setup_serialize(struct bnx_softc * sc)5474329f9016SSepherosa Ziehau bnx_setup_serialize(struct bnx_softc *sc)
5475329f9016SSepherosa Ziehau {
5476329f9016SSepherosa Ziehau 	int i, j;
5477329f9016SSepherosa Ziehau 
5478329f9016SSepherosa Ziehau 	/*
5479329f9016SSepherosa Ziehau 	 * Allocate serializer array
5480329f9016SSepherosa Ziehau 	 */
5481329f9016SSepherosa Ziehau 
5482329f9016SSepherosa Ziehau 	/* Main + RX STD + TX + RX RET */
5483329f9016SSepherosa Ziehau 	sc->bnx_serialize_cnt = 1 + 1 + sc->bnx_tx_ringcnt + sc->bnx_rx_retcnt;
5484329f9016SSepherosa Ziehau 
5485329f9016SSepherosa Ziehau 	sc->bnx_serialize =
5486329f9016SSepherosa Ziehau 	    kmalloc(sc->bnx_serialize_cnt * sizeof(struct lwkt_serialize *),
5487329f9016SSepherosa Ziehau 	        M_DEVBUF, M_WAITOK | M_ZERO);
5488329f9016SSepherosa Ziehau 
5489329f9016SSepherosa Ziehau 	/*
5490329f9016SSepherosa Ziehau 	 * Setup serializers
5491329f9016SSepherosa Ziehau 	 *
5492329f9016SSepherosa Ziehau 	 * NOTE: Order is critical
5493329f9016SSepherosa Ziehau 	 */
5494329f9016SSepherosa Ziehau 
5495329f9016SSepherosa Ziehau 	i = 0;
5496329f9016SSepherosa Ziehau 
5497329f9016SSepherosa Ziehau 	KKASSERT(i < sc->bnx_serialize_cnt);
5498329f9016SSepherosa Ziehau 	sc->bnx_serialize[i++] = &sc->bnx_main_serialize;
5499329f9016SSepherosa Ziehau 
5500329f9016SSepherosa Ziehau 	KKASSERT(i < sc->bnx_serialize_cnt);
5501329f9016SSepherosa Ziehau 	sc->bnx_serialize[i++] = &sc->bnx_rx_std_ring.bnx_rx_std_serialize;
5502329f9016SSepherosa Ziehau 
5503329f9016SSepherosa Ziehau 	for (j = 0; j < sc->bnx_rx_retcnt; ++j) {
5504329f9016SSepherosa Ziehau 		KKASSERT(i < sc->bnx_serialize_cnt);
5505329f9016SSepherosa Ziehau 		sc->bnx_serialize[i++] =
5506329f9016SSepherosa Ziehau 		    &sc->bnx_rx_ret_ring[j].bnx_rx_ret_serialize;
5507329f9016SSepherosa Ziehau 	}
5508329f9016SSepherosa Ziehau 
5509329f9016SSepherosa Ziehau 	for (j = 0; j < sc->bnx_tx_ringcnt; ++j) {
5510329f9016SSepherosa Ziehau 		KKASSERT(i < sc->bnx_serialize_cnt);
5511329f9016SSepherosa Ziehau 		sc->bnx_serialize[i++] =
5512329f9016SSepherosa Ziehau 		    &sc->bnx_tx_ring[j].bnx_tx_serialize;
5513329f9016SSepherosa Ziehau 	}
5514329f9016SSepherosa Ziehau 
5515329f9016SSepherosa Ziehau 	KKASSERT(i == sc->bnx_serialize_cnt);
5516329f9016SSepherosa Ziehau }
5517329f9016SSepherosa Ziehau 
5518329f9016SSepherosa Ziehau static void
bnx_serialize(struct ifnet * ifp,enum ifnet_serialize slz)5519329f9016SSepherosa Ziehau bnx_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
5520329f9016SSepherosa Ziehau {
5521329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
5522329f9016SSepherosa Ziehau 
5523329f9016SSepherosa Ziehau 	ifnet_serialize_array_enter(sc->bnx_serialize,
5524329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
5525329f9016SSepherosa Ziehau }
5526329f9016SSepherosa Ziehau 
5527329f9016SSepherosa Ziehau static void
bnx_deserialize(struct ifnet * ifp,enum ifnet_serialize slz)5528329f9016SSepherosa Ziehau bnx_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
5529329f9016SSepherosa Ziehau {
5530329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
5531329f9016SSepherosa Ziehau 
5532329f9016SSepherosa Ziehau 	ifnet_serialize_array_exit(sc->bnx_serialize,
5533329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
5534329f9016SSepherosa Ziehau }
5535329f9016SSepherosa Ziehau 
5536329f9016SSepherosa Ziehau static int
bnx_tryserialize(struct ifnet * ifp,enum ifnet_serialize slz)5537329f9016SSepherosa Ziehau bnx_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
5538329f9016SSepherosa Ziehau {
5539329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
5540329f9016SSepherosa Ziehau 
5541329f9016SSepherosa Ziehau 	return ifnet_serialize_array_try(sc->bnx_serialize,
5542329f9016SSepherosa Ziehau 	    sc->bnx_serialize_cnt, slz);
5543329f9016SSepherosa Ziehau }
5544329f9016SSepherosa Ziehau 
5545329f9016SSepherosa Ziehau #ifdef INVARIANTS
5546329f9016SSepherosa Ziehau 
5547329f9016SSepherosa Ziehau static void
bnx_serialize_assert(struct ifnet * ifp,enum ifnet_serialize slz,boolean_t serialized)5548329f9016SSepherosa Ziehau bnx_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
5549329f9016SSepherosa Ziehau     boolean_t serialized)
5550329f9016SSepherosa Ziehau {
5551329f9016SSepherosa Ziehau 	struct bnx_softc *sc = ifp->if_softc;
5552329f9016SSepherosa Ziehau 
5553329f9016SSepherosa Ziehau 	ifnet_serialize_array_assert(sc->bnx_serialize, sc->bnx_serialize_cnt,
5554329f9016SSepherosa Ziehau 	    slz, serialized);
5555329f9016SSepherosa Ziehau }
5556329f9016SSepherosa Ziehau 
5557329f9016SSepherosa Ziehau #endif	/* INVARIANTS */
55584fa38985SSepherosa Ziehau 
55597dbaa833SSepherosa Ziehau static void
bnx_set_tick_cpuid(struct bnx_softc * sc,boolean_t polling)55607dbaa833SSepherosa Ziehau bnx_set_tick_cpuid(struct bnx_softc *sc, boolean_t polling)
55617dbaa833SSepherosa Ziehau {
55627dbaa833SSepherosa Ziehau 	if (polling)
55637dbaa833SSepherosa Ziehau 		sc->bnx_tick_cpuid = 0; /* XXX */
55647dbaa833SSepherosa Ziehau 	else
55657dbaa833SSepherosa Ziehau 		sc->bnx_tick_cpuid = sc->bnx_intr_data[0].bnx_intr_cpuid;
55667dbaa833SSepherosa Ziehau }
5567841cdf08SSepherosa Ziehau 
5568841cdf08SSepherosa Ziehau static void
bnx_rx_std_refill_ithread(void * xstd)5569841cdf08SSepherosa Ziehau bnx_rx_std_refill_ithread(void *xstd)
5570841cdf08SSepherosa Ziehau {
5571841cdf08SSepherosa Ziehau 	struct bnx_rx_std_ring *std = xstd;
5572841cdf08SSepherosa Ziehau 	struct globaldata *gd = mycpu;
5573841cdf08SSepherosa Ziehau 
5574841cdf08SSepherosa Ziehau 	crit_enter_gd(gd);
5575841cdf08SSepherosa Ziehau 
5576841cdf08SSepherosa Ziehau 	while (!std->bnx_rx_std_stop) {
5577841cdf08SSepherosa Ziehau 		if (std->bnx_rx_std_refill) {
5578841cdf08SSepherosa Ziehau 			lwkt_serialize_handler_call(
5579841cdf08SSepherosa Ziehau 			    &std->bnx_rx_std_serialize,
5580841cdf08SSepherosa Ziehau 			    bnx_rx_std_refill, std, NULL);
5581841cdf08SSepherosa Ziehau 		}
5582841cdf08SSepherosa Ziehau 
5583841cdf08SSepherosa Ziehau 		crit_exit_gd(gd);
5584841cdf08SSepherosa Ziehau 		crit_enter_gd(gd);
5585841cdf08SSepherosa Ziehau 
5586695a8586SSepherosa Ziehau 		atomic_poll_release_int(&std->bnx_rx_std_running);
5587695a8586SSepherosa Ziehau 		cpu_mfence();
5588695a8586SSepherosa Ziehau 
5589841cdf08SSepherosa Ziehau 		if (!std->bnx_rx_std_refill && !std->bnx_rx_std_stop) {
5590841cdf08SSepherosa Ziehau 			lwkt_deschedule_self(gd->gd_curthread);
5591841cdf08SSepherosa Ziehau 			lwkt_switch();
5592841cdf08SSepherosa Ziehau 		}
5593841cdf08SSepherosa Ziehau 	}
5594841cdf08SSepherosa Ziehau 
5595841cdf08SSepherosa Ziehau 	crit_exit_gd(gd);
5596841cdf08SSepherosa Ziehau 
5597841cdf08SSepherosa Ziehau 	wakeup(std);
5598841cdf08SSepherosa Ziehau 
5599841cdf08SSepherosa Ziehau 	lwkt_exit();
5600841cdf08SSepherosa Ziehau }
5601841cdf08SSepherosa Ziehau 
5602841cdf08SSepherosa Ziehau static void
bnx_rx_std_refill(void * xstd,void * frame __unused)5603841cdf08SSepherosa Ziehau bnx_rx_std_refill(void *xstd, void *frame __unused)
5604841cdf08SSepherosa Ziehau {
5605841cdf08SSepherosa Ziehau 	struct bnx_rx_std_ring *std = xstd;
5606695a8586SSepherosa Ziehau 	int cnt, refill_mask;
5607841cdf08SSepherosa Ziehau 
5608841cdf08SSepherosa Ziehau again:
5609841cdf08SSepherosa Ziehau 	cnt = 0;
5610841cdf08SSepherosa Ziehau 
5611841cdf08SSepherosa Ziehau 	cpu_lfence();
5612695a8586SSepherosa Ziehau 	refill_mask = std->bnx_rx_std_refill;
5613695a8586SSepherosa Ziehau 	atomic_clear_int(&std->bnx_rx_std_refill, refill_mask);
5614841cdf08SSepherosa Ziehau 
5615695a8586SSepherosa Ziehau 	while (refill_mask) {
5616695a8586SSepherosa Ziehau 		uint16_t check_idx = std->bnx_rx_std;
5617695a8586SSepherosa Ziehau 		int ret_idx;
5618695a8586SSepherosa Ziehau 
5619695a8586SSepherosa Ziehau 		ret_idx = bsfl(refill_mask);
5620841cdf08SSepherosa Ziehau 		for (;;) {
5621841cdf08SSepherosa Ziehau 			struct bnx_rx_buf *rb;
5622695a8586SSepherosa Ziehau 			int refilled;
5623841cdf08SSepherosa Ziehau 
5624841cdf08SSepherosa Ziehau 			BNX_INC(check_idx, BGE_STD_RX_RING_CNT);
5625841cdf08SSepherosa Ziehau 			rb = &std->bnx_rx_std_buf[check_idx];
5626695a8586SSepherosa Ziehau 			refilled = rb->bnx_rx_refilled;
5627841cdf08SSepherosa Ziehau 			cpu_lfence();
5628695a8586SSepherosa Ziehau 			if (refilled) {
5629841cdf08SSepherosa Ziehau 				bnx_setup_rxdesc_std(std, check_idx);
5630841cdf08SSepherosa Ziehau 				std->bnx_rx_std = check_idx;
5631841cdf08SSepherosa Ziehau 				++cnt;
5632695a8586SSepherosa Ziehau 				if (cnt >= 8) {
5633625c3ba3SSepherosa Ziehau 					atomic_subtract_int(
5634625c3ba3SSepherosa Ziehau 					    &std->bnx_rx_std_used, cnt);
5635695a8586SSepherosa Ziehau 					bnx_writembx(std->bnx_sc,
5636695a8586SSepherosa Ziehau 					    BGE_MBX_RX_STD_PROD_LO,
5637695a8586SSepherosa Ziehau 					    std->bnx_rx_std);
5638695a8586SSepherosa Ziehau 					cnt = 0;
5639695a8586SSepherosa Ziehau 				}
5640841cdf08SSepherosa Ziehau 			} else {
5641841cdf08SSepherosa Ziehau 				break;
5642841cdf08SSepherosa Ziehau 			}
5643841cdf08SSepherosa Ziehau 		}
5644695a8586SSepherosa Ziehau 		refill_mask &= ~(1 << ret_idx);
5645695a8586SSepherosa Ziehau 	}
5646841cdf08SSepherosa Ziehau 
5647841cdf08SSepherosa Ziehau 	if (cnt) {
5648625c3ba3SSepherosa Ziehau 		atomic_subtract_int(&std->bnx_rx_std_used, cnt);
5649841cdf08SSepherosa Ziehau 		bnx_writembx(std->bnx_sc, BGE_MBX_RX_STD_PROD_LO,
5650841cdf08SSepherosa Ziehau 		    std->bnx_rx_std);
5651841cdf08SSepherosa Ziehau 	}
5652841cdf08SSepherosa Ziehau 
5653841cdf08SSepherosa Ziehau 	if (std->bnx_rx_std_refill)
5654841cdf08SSepherosa Ziehau 		goto again;
5655841cdf08SSepherosa Ziehau 
5656841cdf08SSepherosa Ziehau 	atomic_poll_release_int(&std->bnx_rx_std_running);
5657841cdf08SSepherosa Ziehau 	cpu_mfence();
5658841cdf08SSepherosa Ziehau 
5659841cdf08SSepherosa Ziehau 	if (std->bnx_rx_std_refill)
5660841cdf08SSepherosa Ziehau 		goto again;
5661841cdf08SSepherosa Ziehau }
5662841cdf08SSepherosa Ziehau 
5663841cdf08SSepherosa Ziehau static int
bnx_sysctl_std_refill(SYSCTL_HANDLER_ARGS)5664841cdf08SSepherosa Ziehau bnx_sysctl_std_refill(SYSCTL_HANDLER_ARGS)
5665841cdf08SSepherosa Ziehau {
5666841cdf08SSepherosa Ziehau 	struct bnx_softc *sc = (void *)arg1;
5667841cdf08SSepherosa Ziehau 	struct ifnet *ifp = &sc->arpcom.ac_if;
5668841cdf08SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = &sc->bnx_rx_ret_ring[0];
5669841cdf08SSepherosa Ziehau 	int error, cntmax, i;
5670841cdf08SSepherosa Ziehau 
5671841cdf08SSepherosa Ziehau 	cntmax = ret->bnx_rx_cntmax;
5672841cdf08SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &cntmax, 0, req);
5673841cdf08SSepherosa Ziehau 	if (error || req->newptr == NULL)
5674841cdf08SSepherosa Ziehau 		return error;
5675841cdf08SSepherosa Ziehau 
5676841cdf08SSepherosa Ziehau 	ifnet_serialize_all(ifp);
5677841cdf08SSepherosa Ziehau 
5678625c3ba3SSepherosa Ziehau 	if ((cntmax * sc->bnx_rx_retcnt) >= BGE_STD_RX_RING_CNT / 2) {
5679841cdf08SSepherosa Ziehau 		error = EINVAL;
5680841cdf08SSepherosa Ziehau 		goto back;
5681841cdf08SSepherosa Ziehau 	}
5682841cdf08SSepherosa Ziehau 
5683841cdf08SSepherosa Ziehau 	for (i = 0; i < sc->bnx_tx_ringcnt; ++i)
5684841cdf08SSepherosa Ziehau 		sc->bnx_rx_ret_ring[i].bnx_rx_cntmax = cntmax;
5685841cdf08SSepherosa Ziehau 	error = 0;
5686841cdf08SSepherosa Ziehau 
5687841cdf08SSepherosa Ziehau back:
5688841cdf08SSepherosa Ziehau 	ifnet_deserialize_all(ifp);
5689841cdf08SSepherosa Ziehau 
5690841cdf08SSepherosa Ziehau 	return error;
5691841cdf08SSepherosa Ziehau }
5692695a8586SSepherosa Ziehau 
5693695a8586SSepherosa Ziehau static void
bnx_init_rss(struct bnx_softc * sc)5694695a8586SSepherosa Ziehau bnx_init_rss(struct bnx_softc *sc)
5695695a8586SSepherosa Ziehau {
5696695a8586SSepherosa Ziehau 	uint8_t key[BGE_RSS_KEYREG_CNT * BGE_RSS_KEYREG_SIZE];
5697695a8586SSepherosa Ziehau 	int i, j, r;
5698695a8586SSepherosa Ziehau 
5699695a8586SSepherosa Ziehau 	KKASSERT(BNX_RSS_ENABLED(sc));
5700695a8586SSepherosa Ziehau 
57010f02bb87SSepherosa Ziehau 	/*
570202596bedSSepherosa Ziehau 	 * Configure RSS redirect table.
57030f02bb87SSepherosa Ziehau 	 */
570402596bedSSepherosa Ziehau 	if_ringmap_rdrtable(sc->bnx_rx_rmap, sc->bnx_rdr_table,
570502596bedSSepherosa Ziehau 	    BNX_RDRTABLE_SIZE);
5706695a8586SSepherosa Ziehau 	r = 0;
5707695a8586SSepherosa Ziehau 	for (j = 0; j < BGE_RSS_INDIR_TBL_CNT; ++j) {
5708695a8586SSepherosa Ziehau 		uint32_t tbl = 0;
5709695a8586SSepherosa Ziehau 
5710695a8586SSepherosa Ziehau 		for (i = 0; i < BGE_RSS_INDIR_TBLENT_CNT; ++i) {
5711695a8586SSepherosa Ziehau 			uint32_t q;
5712695a8586SSepherosa Ziehau 
571302596bedSSepherosa Ziehau 			q = sc->bnx_rdr_table[r];
5714695a8586SSepherosa Ziehau 			tbl |= q << (BGE_RSS_INDIR_TBLENT_SHIFT *
5715695a8586SSepherosa Ziehau 			    (BGE_RSS_INDIR_TBLENT_CNT - i - 1));
5716695a8586SSepherosa Ziehau 			++r;
5717695a8586SSepherosa Ziehau 		}
5718695a8586SSepherosa Ziehau 
5719695a8586SSepherosa Ziehau 		BNX_RSS_DPRINTF(sc, 1, "tbl%d %08x\n", j, tbl);
5720695a8586SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RSS_INDIR_TBL(j), tbl);
5721695a8586SSepherosa Ziehau 	}
5722695a8586SSepherosa Ziehau 
5723695a8586SSepherosa Ziehau 	toeplitz_get_key(key, sizeof(key));
5724695a8586SSepherosa Ziehau 	for (i = 0; i < BGE_RSS_KEYREG_CNT; ++i) {
5725695a8586SSepherosa Ziehau 		uint32_t keyreg;
5726695a8586SSepherosa Ziehau 
5727695a8586SSepherosa Ziehau 		keyreg = BGE_RSS_KEYREG_VAL(key, i);
5728695a8586SSepherosa Ziehau 
5729695a8586SSepherosa Ziehau 		BNX_RSS_DPRINTF(sc, 1, "key%d %08x\n", i, keyreg);
5730695a8586SSepherosa Ziehau 		CSR_WRITE_4(sc, BGE_RSS_KEYREG(i), keyreg);
5731695a8586SSepherosa Ziehau 	}
5732695a8586SSepherosa Ziehau }
5733695a8586SSepherosa Ziehau 
5734695a8586SSepherosa Ziehau static void
bnx_setup_ring_cnt(struct bnx_softc * sc)5735695a8586SSepherosa Ziehau bnx_setup_ring_cnt(struct bnx_softc *sc)
5736695a8586SSepherosa Ziehau {
573702596bedSSepherosa Ziehau 	int msix_enable, msix_cnt, msix_ring, ring_max, ring_cnt;
5738695a8586SSepherosa Ziehau 
573902596bedSSepherosa Ziehau 	/* One RX ring. */
574002596bedSSepherosa Ziehau 	sc->bnx_rx_rmap = if_ringmap_alloc(sc->bnx_dev, 1, 1);
574102596bedSSepherosa Ziehau 
574202596bedSSepherosa Ziehau 	if (netisr_ncpus == 1)
574302596bedSSepherosa Ziehau 		goto skip_rx;
5744695a8586SSepherosa Ziehau 
5745695a8586SSepherosa Ziehau 	msix_enable = device_getenv_int(sc->bnx_dev, "msix.enable",
5746695a8586SSepherosa Ziehau 	    bnx_msix_enable);
5747695a8586SSepherosa Ziehau 	if (!msix_enable)
574802596bedSSepherosa Ziehau 		goto skip_rx;
5749695a8586SSepherosa Ziehau 
5750695a8586SSepherosa Ziehau 	/*
5751695a8586SSepherosa Ziehau 	 * One MSI-X vector is dedicated to status or single TX queue,
5752695a8586SSepherosa Ziehau 	 * so make sure that there are enough MSI-X vectors.
5753695a8586SSepherosa Ziehau 	 */
575402596bedSSepherosa Ziehau 	msix_cnt = pci_msix_count(sc->bnx_dev);
575502596bedSSepherosa Ziehau 	if (msix_cnt <= 1)
575602596bedSSepherosa Ziehau 		goto skip_rx;
575702596bedSSepherosa Ziehau 	if (bootverbose)
575802596bedSSepherosa Ziehau 		device_printf(sc->bnx_dev, "MSI-X count %d\n", msix_cnt);
575902596bedSSepherosa Ziehau 	msix_ring = msix_cnt - 1;
5760695a8586SSepherosa Ziehau 
5761695a8586SSepherosa Ziehau 	/*
5762695a8586SSepherosa Ziehau 	 * Setup RX ring count
5763695a8586SSepherosa Ziehau 	 */
5764695a8586SSepherosa Ziehau 	ring_max = BNX_RX_RING_MAX;
576502596bedSSepherosa Ziehau 	if (ring_max > msix_ring)
576602596bedSSepherosa Ziehau 		ring_max = msix_ring;
576702596bedSSepherosa Ziehau 	ring_cnt = device_getenv_int(sc->bnx_dev, "rx_rings", bnx_rx_rings);
5768695a8586SSepherosa Ziehau 
576902596bedSSepherosa Ziehau 	if_ringmap_free(sc->bnx_rx_rmap);
577002596bedSSepherosa Ziehau 	sc->bnx_rx_rmap = if_ringmap_alloc(sc->bnx_dev, ring_cnt, ring_max);
5771695a8586SSepherosa Ziehau 
577202596bedSSepherosa Ziehau skip_rx:
577302596bedSSepherosa Ziehau 	sc->bnx_rx_retcnt = if_ringmap_count(sc->bnx_rx_rmap);
5774695a8586SSepherosa Ziehau 
5775695a8586SSepherosa Ziehau 	/*
5776695a8586SSepherosa Ziehau 	 * Setup TX ring count
5777695a8586SSepherosa Ziehau 	 *
5778695a8586SSepherosa Ziehau 	 * Currently only BCM5719 and BCM5720 support multiple TX rings
5779695a8586SSepherosa Ziehau 	 * and the TX ring count must be less than the RX ring count.
5780695a8586SSepherosa Ziehau 	 */
5781695a8586SSepherosa Ziehau 	if (sc->bnx_asicrev == BGE_ASICREV_BCM5719 ||
5782695a8586SSepherosa Ziehau 	    sc->bnx_asicrev == BGE_ASICREV_BCM5720) {
5783695a8586SSepherosa Ziehau 		ring_max = BNX_TX_RING_MAX;
5784695a8586SSepherosa Ziehau 		if (ring_max > sc->bnx_rx_retcnt)
5785695a8586SSepherosa Ziehau 			ring_max = sc->bnx_rx_retcnt;
578602596bedSSepherosa Ziehau 		ring_cnt = device_getenv_int(sc->bnx_dev, "tx_rings",
5787695a8586SSepherosa Ziehau 		    bnx_tx_rings);
578802596bedSSepherosa Ziehau 	} else {
578902596bedSSepherosa Ziehau 		ring_max = 1;
579002596bedSSepherosa Ziehau 		ring_cnt = 1;
579102596bedSSepherosa Ziehau 	}
579202596bedSSepherosa Ziehau 	sc->bnx_tx_rmap = if_ringmap_alloc(sc->bnx_dev, ring_cnt, ring_max);
579302596bedSSepherosa Ziehau 	if_ringmap_align(sc->bnx_dev, sc->bnx_rx_rmap, sc->bnx_tx_rmap);
579402596bedSSepherosa Ziehau 
579502596bedSSepherosa Ziehau 	sc->bnx_tx_ringcnt = if_ringmap_count(sc->bnx_tx_rmap);
579602596bedSSepherosa Ziehau 	KASSERT(sc->bnx_tx_ringcnt <= sc->bnx_rx_retcnt,
579702596bedSSepherosa Ziehau 	    ("invalid TX ring count %d and RX ring count %d",
579802596bedSSepherosa Ziehau 	     sc->bnx_tx_ringcnt, sc->bnx_rx_retcnt));
579902596bedSSepherosa Ziehau 
580002596bedSSepherosa Ziehau 	/*
580102596bedSSepherosa Ziehau 	 * Setup interrupt count.
580202596bedSSepherosa Ziehau 	 */
580302596bedSSepherosa Ziehau 	if (sc->bnx_rx_retcnt == 1) {
580402596bedSSepherosa Ziehau 		sc->bnx_intr_cnt = 1;
580502596bedSSepherosa Ziehau 	} else {
580602596bedSSepherosa Ziehau 		/*
580702596bedSSepherosa Ziehau 		 * We need one extra MSI-X vector for link status or
580802596bedSSepherosa Ziehau 		 * TX ring (if only one TX ring is enabled).
580902596bedSSepherosa Ziehau 		 */
581002596bedSSepherosa Ziehau 		sc->bnx_intr_cnt = sc->bnx_rx_retcnt + 1;
581102596bedSSepherosa Ziehau 	}
581202596bedSSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt <= BNX_INTR_MAX);
581302596bedSSepherosa Ziehau 
581402596bedSSepherosa Ziehau 	if (bootverbose) {
581502596bedSSepherosa Ziehau 		device_printf(sc->bnx_dev, "intr count %d, "
581602596bedSSepherosa Ziehau 		    "RX ring %d, TX ring %d\n", sc->bnx_intr_cnt,
581702596bedSSepherosa Ziehau 		    sc->bnx_rx_retcnt, sc->bnx_tx_ringcnt);
5818695a8586SSepherosa Ziehau 	}
5819695a8586SSepherosa Ziehau }
5820695a8586SSepherosa Ziehau 
5821695a8586SSepherosa Ziehau static int
bnx_alloc_msix(struct bnx_softc * sc)5822695a8586SSepherosa Ziehau bnx_alloc_msix(struct bnx_softc *sc)
5823695a8586SSepherosa Ziehau {
5824695a8586SSepherosa Ziehau 	struct bnx_intr_data *intr;
5825695a8586SSepherosa Ziehau 	boolean_t setup = FALSE;
582602596bedSSepherosa Ziehau 	int error, i;
5827695a8586SSepherosa Ziehau 
5828695a8586SSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt > 1);
5829695a8586SSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt == sc->bnx_rx_retcnt + 1);
5830695a8586SSepherosa Ziehau 
5831695a8586SSepherosa Ziehau 	if (sc->bnx_flags & BNX_FLAG_RXTX_BUNDLE) {
5832695a8586SSepherosa Ziehau 		/*
5833695a8586SSepherosa Ziehau 		 * Link status
5834695a8586SSepherosa Ziehau 		 */
5835695a8586SSepherosa Ziehau 		intr = &sc->bnx_intr_data[0];
5836695a8586SSepherosa Ziehau 
5837695a8586SSepherosa Ziehau 		intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5838695a8586SSepherosa Ziehau 		intr->bnx_saved_status_tag = &sc->bnx_saved_status_tag;
5839695a8586SSepherosa Ziehau 
5840695a8586SSepherosa Ziehau 		intr->bnx_intr_func = bnx_msix_status;
5841695a8586SSepherosa Ziehau 		intr->bnx_intr_arg = sc;
5842695a8586SSepherosa Ziehau 		intr->bnx_intr_cpuid = 0; /* XXX */
5843695a8586SSepherosa Ziehau 
5844695a8586SSepherosa Ziehau 		ksnprintf(intr->bnx_intr_desc0, sizeof(intr->bnx_intr_desc0),
5845695a8586SSepherosa Ziehau 		    "%s sts", device_get_nameunit(sc->bnx_dev));
5846695a8586SSepherosa Ziehau 		intr->bnx_intr_desc = intr->bnx_intr_desc0;
5847695a8586SSepherosa Ziehau 
5848695a8586SSepherosa Ziehau 		/*
5849695a8586SSepherosa Ziehau 		 * RX/TX rings
5850695a8586SSepherosa Ziehau 		 */
5851695a8586SSepherosa Ziehau 		for (i = 1; i < sc->bnx_intr_cnt; ++i) {
5852695a8586SSepherosa Ziehau 			int idx = i - 1;
5853695a8586SSepherosa Ziehau 
5854695a8586SSepherosa Ziehau 			intr = &sc->bnx_intr_data[i];
5855695a8586SSepherosa Ziehau 
5856695a8586SSepherosa Ziehau 			KKASSERT(idx < sc->bnx_rx_retcnt);
5857695a8586SSepherosa Ziehau 			intr->bnx_ret = &sc->bnx_rx_ret_ring[idx];
5858695a8586SSepherosa Ziehau 			if (idx < sc->bnx_tx_ringcnt) {
5859695a8586SSepherosa Ziehau 				intr->bnx_txr = &sc->bnx_tx_ring[idx];
5860695a8586SSepherosa Ziehau 				intr->bnx_ret->bnx_txr = intr->bnx_txr;
5861695a8586SSepherosa Ziehau 			}
5862695a8586SSepherosa Ziehau 
5863695a8586SSepherosa Ziehau 			intr->bnx_intr_serialize =
5864695a8586SSepherosa Ziehau 			    &intr->bnx_ret->bnx_rx_ret_serialize;
5865695a8586SSepherosa Ziehau 			intr->bnx_saved_status_tag =
5866695a8586SSepherosa Ziehau 			    &intr->bnx_ret->bnx_saved_status_tag;
5867695a8586SSepherosa Ziehau 
5868695a8586SSepherosa Ziehau 			intr->bnx_intr_arg = intr->bnx_ret;
586902596bedSSepherosa Ziehau 			intr->bnx_intr_cpuid =
587002596bedSSepherosa Ziehau 			    if_ringmap_cpumap(sc->bnx_rx_rmap, idx);
587102596bedSSepherosa Ziehau 			KKASSERT(intr->bnx_intr_cpuid < netisr_ncpus);
5872695a8586SSepherosa Ziehau 
5873695a8586SSepherosa Ziehau 			if (intr->bnx_txr == NULL) {
5874695a8586SSepherosa Ziehau 				intr->bnx_intr_check = bnx_check_intr_rx;
5875695a8586SSepherosa Ziehau 				intr->bnx_intr_func = bnx_msix_rx;
5876695a8586SSepherosa Ziehau 				ksnprintf(intr->bnx_intr_desc0,
5877695a8586SSepherosa Ziehau 				    sizeof(intr->bnx_intr_desc0), "%s rx%d",
5878695a8586SSepherosa Ziehau 				    device_get_nameunit(sc->bnx_dev), idx);
5879695a8586SSepherosa Ziehau 			} else {
588002596bedSSepherosa Ziehau #ifdef INVARIANTS
588102596bedSSepherosa Ziehau 				int tx_cpuid;
588202596bedSSepherosa Ziehau #endif
588302596bedSSepherosa Ziehau 
5884695a8586SSepherosa Ziehau 				intr->bnx_intr_check = bnx_check_intr_rxtx;
5885695a8586SSepherosa Ziehau 				intr->bnx_intr_func = bnx_msix_rxtx;
5886695a8586SSepherosa Ziehau 				ksnprintf(intr->bnx_intr_desc0,
5887695a8586SSepherosa Ziehau 				    sizeof(intr->bnx_intr_desc0), "%s rxtx%d",
5888695a8586SSepherosa Ziehau 				    device_get_nameunit(sc->bnx_dev), idx);
5889695a8586SSepherosa Ziehau 
589002596bedSSepherosa Ziehau #ifdef INVARIANTS
589102596bedSSepherosa Ziehau 				tx_cpuid = if_ringmap_cpumap(sc->bnx_tx_rmap,
589202596bedSSepherosa Ziehau 				    idx);
589302596bedSSepherosa Ziehau 				KASSERT(intr->bnx_intr_cpuid == tx_cpuid,
589402596bedSSepherosa Ziehau 				    ("RX intr cpu%d, TX intr cpu%d, mismatch",
589502596bedSSepherosa Ziehau 				     intr->bnx_intr_cpuid, tx_cpuid));
589602596bedSSepherosa Ziehau #endif
5897695a8586SSepherosa Ziehau 				intr->bnx_txr->bnx_tx_cpuid =
5898695a8586SSepherosa Ziehau 				    intr->bnx_intr_cpuid;
5899695a8586SSepherosa Ziehau 			}
5900695a8586SSepherosa Ziehau 			intr->bnx_intr_desc = intr->bnx_intr_desc0;
5901695a8586SSepherosa Ziehau 
5902695a8586SSepherosa Ziehau 			intr->bnx_ret->bnx_msix_mbx = intr->bnx_intr_mbx;
5903695a8586SSepherosa Ziehau 		}
5904695a8586SSepherosa Ziehau 	} else {
5905695a8586SSepherosa Ziehau 		/*
590602596bedSSepherosa Ziehau 		 * TX ring0 and link status
5907695a8586SSepherosa Ziehau 		 */
5908695a8586SSepherosa Ziehau 		intr = &sc->bnx_intr_data[0];
5909695a8586SSepherosa Ziehau 
5910695a8586SSepherosa Ziehau 		intr->bnx_txr = &sc->bnx_tx_ring[0];
5911695a8586SSepherosa Ziehau 		intr->bnx_intr_serialize = &sc->bnx_main_serialize;
5912695a8586SSepherosa Ziehau 		intr->bnx_intr_check = bnx_check_intr_tx;
5913695a8586SSepherosa Ziehau 		intr->bnx_saved_status_tag =
5914695a8586SSepherosa Ziehau 		    &intr->bnx_txr->bnx_saved_status_tag;
5915695a8586SSepherosa Ziehau 
5916695a8586SSepherosa Ziehau 		intr->bnx_intr_func = bnx_msix_tx_status;
5917695a8586SSepherosa Ziehau 		intr->bnx_intr_arg = intr->bnx_txr;
591802596bedSSepherosa Ziehau 		intr->bnx_intr_cpuid = if_ringmap_cpumap(sc->bnx_tx_rmap, 0);
591902596bedSSepherosa Ziehau 		KKASSERT(intr->bnx_intr_cpuid < netisr_ncpus);
5920695a8586SSepherosa Ziehau 
5921695a8586SSepherosa Ziehau 		ksnprintf(intr->bnx_intr_desc0, sizeof(intr->bnx_intr_desc0),
5922695a8586SSepherosa Ziehau 		    "%s ststx", device_get_nameunit(sc->bnx_dev));
5923695a8586SSepherosa Ziehau 		intr->bnx_intr_desc = intr->bnx_intr_desc0;
5924695a8586SSepherosa Ziehau 
5925695a8586SSepherosa Ziehau 		intr->bnx_txr->bnx_tx_cpuid = intr->bnx_intr_cpuid;
5926695a8586SSepherosa Ziehau 
5927695a8586SSepherosa Ziehau 		/*
5928695a8586SSepherosa Ziehau 		 * RX rings
5929695a8586SSepherosa Ziehau 		 */
5930695a8586SSepherosa Ziehau 		for (i = 1; i < sc->bnx_intr_cnt; ++i) {
5931695a8586SSepherosa Ziehau 			int idx = i - 1;
5932695a8586SSepherosa Ziehau 
5933695a8586SSepherosa Ziehau 			intr = &sc->bnx_intr_data[i];
5934695a8586SSepherosa Ziehau 
5935695a8586SSepherosa Ziehau 			KKASSERT(idx < sc->bnx_rx_retcnt);
5936695a8586SSepherosa Ziehau 			intr->bnx_ret = &sc->bnx_rx_ret_ring[idx];
5937695a8586SSepherosa Ziehau 			intr->bnx_intr_serialize =
5938695a8586SSepherosa Ziehau 			    &intr->bnx_ret->bnx_rx_ret_serialize;
5939695a8586SSepherosa Ziehau 			intr->bnx_intr_check = bnx_check_intr_rx;
5940695a8586SSepherosa Ziehau 			intr->bnx_saved_status_tag =
5941695a8586SSepherosa Ziehau 			    &intr->bnx_ret->bnx_saved_status_tag;
5942695a8586SSepherosa Ziehau 
5943695a8586SSepherosa Ziehau 			intr->bnx_intr_func = bnx_msix_rx;
5944695a8586SSepherosa Ziehau 			intr->bnx_intr_arg = intr->bnx_ret;
594502596bedSSepherosa Ziehau 			intr->bnx_intr_cpuid =
594602596bedSSepherosa Ziehau 			    if_ringmap_cpumap(sc->bnx_rx_rmap, idx);
594702596bedSSepherosa Ziehau 			KKASSERT(intr->bnx_intr_cpuid < netisr_ncpus);
5948695a8586SSepherosa Ziehau 
5949695a8586SSepherosa Ziehau 			ksnprintf(intr->bnx_intr_desc0,
5950695a8586SSepherosa Ziehau 			    sizeof(intr->bnx_intr_desc0), "%s rx%d",
5951695a8586SSepherosa Ziehau 			    device_get_nameunit(sc->bnx_dev), idx);
5952695a8586SSepherosa Ziehau 			intr->bnx_intr_desc = intr->bnx_intr_desc0;
5953695a8586SSepherosa Ziehau 
5954695a8586SSepherosa Ziehau 			intr->bnx_ret->bnx_msix_mbx = intr->bnx_intr_mbx;
5955695a8586SSepherosa Ziehau 		}
5956695a8586SSepherosa Ziehau 	}
5957695a8586SSepherosa Ziehau 
59589f5082d5SSepherosa Ziehau 	if (BNX_IS_5717_PLUS(sc)) {
5959695a8586SSepherosa Ziehau 		sc->bnx_msix_mem_rid = PCIR_BAR(4);
59609f5082d5SSepherosa Ziehau 	} else {
59619f5082d5SSepherosa Ziehau 		if (sc->bnx_res2 == NULL)
5962715109c2SSepherosa Ziehau 			sc->bnx_msix_mem_rid = PCIR_BAR(2);
59639f5082d5SSepherosa Ziehau 	}
59649f5082d5SSepherosa Ziehau 	if (sc->bnx_msix_mem_rid != 0) {
5965695a8586SSepherosa Ziehau 		sc->bnx_msix_mem_res = bus_alloc_resource_any(sc->bnx_dev,
5966695a8586SSepherosa Ziehau 		    SYS_RES_MEMORY, &sc->bnx_msix_mem_rid, RF_ACTIVE);
5967695a8586SSepherosa Ziehau 		if (sc->bnx_msix_mem_res == NULL) {
59689f5082d5SSepherosa Ziehau 			device_printf(sc->bnx_dev,
59699f5082d5SSepherosa Ziehau 			    "could not alloc MSI-X table\n");
5970695a8586SSepherosa Ziehau 			return ENXIO;
5971695a8586SSepherosa Ziehau 		}
59729f5082d5SSepherosa Ziehau 	}
5973695a8586SSepherosa Ziehau 
5974695a8586SSepherosa Ziehau 	bnx_enable_msi(sc, TRUE);
5975695a8586SSepherosa Ziehau 
5976695a8586SSepherosa Ziehau 	error = pci_setup_msix(sc->bnx_dev);
5977695a8586SSepherosa Ziehau 	if (error) {
5978695a8586SSepherosa Ziehau 		device_printf(sc->bnx_dev, "could not setup MSI-X\n");
5979695a8586SSepherosa Ziehau 		goto back;
5980695a8586SSepherosa Ziehau 	}
5981695a8586SSepherosa Ziehau 	setup = TRUE;
5982695a8586SSepherosa Ziehau 
5983695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
5984695a8586SSepherosa Ziehau 		intr = &sc->bnx_intr_data[i];
5985695a8586SSepherosa Ziehau 
5986695a8586SSepherosa Ziehau 		error = pci_alloc_msix_vector(sc->bnx_dev, i,
5987695a8586SSepherosa Ziehau 		    &intr->bnx_intr_rid, intr->bnx_intr_cpuid);
5988695a8586SSepherosa Ziehau 		if (error) {
5989695a8586SSepherosa Ziehau 			device_printf(sc->bnx_dev,
5990695a8586SSepherosa Ziehau 			    "could not alloc MSI-X %d on cpu%d\n",
5991695a8586SSepherosa Ziehau 			    i, intr->bnx_intr_cpuid);
5992695a8586SSepherosa Ziehau 			goto back;
5993695a8586SSepherosa Ziehau 		}
5994695a8586SSepherosa Ziehau 
5995695a8586SSepherosa Ziehau 		intr->bnx_intr_res = bus_alloc_resource_any(sc->bnx_dev,
5996695a8586SSepherosa Ziehau 		    SYS_RES_IRQ, &intr->bnx_intr_rid, RF_ACTIVE);
5997695a8586SSepherosa Ziehau 		if (intr->bnx_intr_res == NULL) {
5998695a8586SSepherosa Ziehau 			device_printf(sc->bnx_dev,
5999695a8586SSepherosa Ziehau 			    "could not alloc MSI-X %d resource\n", i);
6000695a8586SSepherosa Ziehau 			error = ENXIO;
6001695a8586SSepherosa Ziehau 			goto back;
6002695a8586SSepherosa Ziehau 		}
6003695a8586SSepherosa Ziehau 	}
6004695a8586SSepherosa Ziehau 
6005695a8586SSepherosa Ziehau 	pci_enable_msix(sc->bnx_dev);
6006695a8586SSepherosa Ziehau 	sc->bnx_intr_type = PCI_INTR_TYPE_MSIX;
6007695a8586SSepherosa Ziehau back:
6008695a8586SSepherosa Ziehau 	if (error)
6009695a8586SSepherosa Ziehau 		bnx_free_msix(sc, setup);
6010695a8586SSepherosa Ziehau 	return error;
6011695a8586SSepherosa Ziehau }
6012695a8586SSepherosa Ziehau 
6013695a8586SSepherosa Ziehau static void
bnx_free_msix(struct bnx_softc * sc,boolean_t setup)6014695a8586SSepherosa Ziehau bnx_free_msix(struct bnx_softc *sc, boolean_t setup)
6015695a8586SSepherosa Ziehau {
6016695a8586SSepherosa Ziehau 	int i;
6017695a8586SSepherosa Ziehau 
6018695a8586SSepherosa Ziehau 	KKASSERT(sc->bnx_intr_cnt > 1);
6019695a8586SSepherosa Ziehau 
6020695a8586SSepherosa Ziehau 	for (i = 0; i < sc->bnx_intr_cnt; ++i) {
6021695a8586SSepherosa Ziehau 		struct bnx_intr_data *intr = &sc->bnx_intr_data[i];
6022695a8586SSepherosa Ziehau 
6023695a8586SSepherosa Ziehau 		if (intr->bnx_intr_res != NULL) {
6024695a8586SSepherosa Ziehau 			bus_release_resource(sc->bnx_dev, SYS_RES_IRQ,
6025695a8586SSepherosa Ziehau 			    intr->bnx_intr_rid, intr->bnx_intr_res);
6026695a8586SSepherosa Ziehau 		}
6027695a8586SSepherosa Ziehau 		if (intr->bnx_intr_rid >= 0) {
6028695a8586SSepherosa Ziehau 			pci_release_msix_vector(sc->bnx_dev,
6029695a8586SSepherosa Ziehau 			    intr->bnx_intr_rid);
6030695a8586SSepherosa Ziehau 		}
6031695a8586SSepherosa Ziehau 	}
6032695a8586SSepherosa Ziehau 	if (setup)
6033695a8586SSepherosa Ziehau 		pci_teardown_msix(sc->bnx_dev);
6034695a8586SSepherosa Ziehau }
6035695a8586SSepherosa Ziehau 
6036695a8586SSepherosa Ziehau static void
bnx_rx_std_refill_sched_ipi(void * xret)6037695a8586SSepherosa Ziehau bnx_rx_std_refill_sched_ipi(void *xret)
6038695a8586SSepherosa Ziehau {
6039695a8586SSepherosa Ziehau 	struct bnx_rx_ret_ring *ret = xret;
6040695a8586SSepherosa Ziehau 	struct bnx_rx_std_ring *std = ret->bnx_std;
6041695a8586SSepherosa Ziehau 	struct globaldata *gd = mycpu;
6042695a8586SSepherosa Ziehau 
6043695a8586SSepherosa Ziehau 	crit_enter_gd(gd);
6044695a8586SSepherosa Ziehau 
6045695a8586SSepherosa Ziehau 	atomic_set_int(&std->bnx_rx_std_refill, ret->bnx_rx_mask);
6046695a8586SSepherosa Ziehau 	cpu_sfence();
6047695a8586SSepherosa Ziehau 
6048c450d4d8SSepherosa Ziehau 	KKASSERT(std->bnx_rx_std_ithread->td_gd == gd);
6049c450d4d8SSepherosa Ziehau 	lwkt_schedule(std->bnx_rx_std_ithread);
6050695a8586SSepherosa Ziehau 
6051695a8586SSepherosa Ziehau 	crit_exit_gd(gd);
6052695a8586SSepherosa Ziehau }
6053695a8586SSepherosa Ziehau 
6054695a8586SSepherosa Ziehau static void
bnx_rx_std_refill_stop(void * xstd)6055695a8586SSepherosa Ziehau bnx_rx_std_refill_stop(void *xstd)
6056695a8586SSepherosa Ziehau {
6057695a8586SSepherosa Ziehau 	struct bnx_rx_std_ring *std = xstd;
6058695a8586SSepherosa Ziehau 	struct globaldata *gd = mycpu;
6059695a8586SSepherosa Ziehau 
6060695a8586SSepherosa Ziehau 	crit_enter_gd(gd);
6061695a8586SSepherosa Ziehau 
6062695a8586SSepherosa Ziehau 	std->bnx_rx_std_stop = 1;
6063695a8586SSepherosa Ziehau 	cpu_sfence();
6064695a8586SSepherosa Ziehau 
6065c450d4d8SSepherosa Ziehau 	KKASSERT(std->bnx_rx_std_ithread->td_gd == gd);
6066c450d4d8SSepherosa Ziehau 	lwkt_schedule(std->bnx_rx_std_ithread);
6067695a8586SSepherosa Ziehau 
6068695a8586SSepherosa Ziehau 	crit_exit_gd(gd);
6069695a8586SSepherosa Ziehau }
6070695a8586SSepherosa Ziehau 
6071695a8586SSepherosa Ziehau static void
bnx_serialize_skipmain(struct bnx_softc * sc)6072695a8586SSepherosa Ziehau bnx_serialize_skipmain(struct bnx_softc *sc)
6073695a8586SSepherosa Ziehau {
6074695a8586SSepherosa Ziehau 	lwkt_serialize_array_enter(sc->bnx_serialize,
6075695a8586SSepherosa Ziehau 	    sc->bnx_serialize_cnt, 1);
6076695a8586SSepherosa Ziehau }
6077695a8586SSepherosa Ziehau 
6078695a8586SSepherosa Ziehau static void
bnx_deserialize_skipmain(struct bnx_softc * sc)6079695a8586SSepherosa Ziehau bnx_deserialize_skipmain(struct bnx_softc *sc)
6080695a8586SSepherosa Ziehau {
6081695a8586SSepherosa Ziehau 	lwkt_serialize_array_exit(sc->bnx_serialize,
6082695a8586SSepherosa Ziehau 	    sc->bnx_serialize_cnt, 1);
6083695a8586SSepherosa Ziehau }
6084695a8586SSepherosa Ziehau 
6085695a8586SSepherosa Ziehau static void
bnx_rx_std_refill_sched(struct bnx_rx_ret_ring * ret,struct bnx_rx_std_ring * std)6086695a8586SSepherosa Ziehau bnx_rx_std_refill_sched(struct bnx_rx_ret_ring *ret,
6087695a8586SSepherosa Ziehau     struct bnx_rx_std_ring *std)
6088695a8586SSepherosa Ziehau {
6089695a8586SSepherosa Ziehau 	struct globaldata *gd = mycpu;
6090695a8586SSepherosa Ziehau 
6091695a8586SSepherosa Ziehau 	ret->bnx_rx_cnt = 0;
6092695a8586SSepherosa Ziehau 	cpu_sfence();
6093695a8586SSepherosa Ziehau 
6094695a8586SSepherosa Ziehau 	crit_enter_gd(gd);
6095695a8586SSepherosa Ziehau 
6096695a8586SSepherosa Ziehau 	atomic_set_int(&std->bnx_rx_std_refill, ret->bnx_rx_mask);
6097695a8586SSepherosa Ziehau 	cpu_sfence();
6098695a8586SSepherosa Ziehau 	if (atomic_poll_acquire_int(&std->bnx_rx_std_running)) {
6099c450d4d8SSepherosa Ziehau 		if (std->bnx_rx_std_ithread->td_gd == gd) {
6100c450d4d8SSepherosa Ziehau 			lwkt_schedule(std->bnx_rx_std_ithread);
6101695a8586SSepherosa Ziehau 		} else {
6102c450d4d8SSepherosa Ziehau 			lwkt_send_ipiq(std->bnx_rx_std_ithread->td_gd,
6103695a8586SSepherosa Ziehau 			    bnx_rx_std_refill_sched_ipi, ret);
6104695a8586SSepherosa Ziehau 		}
6105695a8586SSepherosa Ziehau 	}
6106695a8586SSepherosa Ziehau 
6107695a8586SSepherosa Ziehau 	crit_exit_gd(gd);
6108695a8586SSepherosa Ziehau }
6109b19ddf7eSSepherosa Ziehau 
6110b19ddf7eSSepherosa Ziehau static struct pktinfo *
bnx_rss_info(struct pktinfo * pi,const struct bge_rx_bd * cur_rx)6111b19ddf7eSSepherosa Ziehau bnx_rss_info(struct pktinfo *pi, const struct bge_rx_bd *cur_rx)
6112b19ddf7eSSepherosa Ziehau {
6113b19ddf7eSSepherosa Ziehau 	/* Don't pick up IPv6 packet */
6114b19ddf7eSSepherosa Ziehau 	if (cur_rx->bge_flags & BGE_RXBDFLAG_IPV6)
6115b19ddf7eSSepherosa Ziehau 		return NULL;
6116b19ddf7eSSepherosa Ziehau 
6117b19ddf7eSSepherosa Ziehau 	/* Don't pick up IP packet w/o IP checksum */
6118b19ddf7eSSepherosa Ziehau 	if ((cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) == 0 ||
6119b19ddf7eSSepherosa Ziehau 	    (cur_rx->bge_error_flag & BGE_RXERRFLAG_IP_CSUM_NOK))
6120b19ddf7eSSepherosa Ziehau 		return NULL;
6121b19ddf7eSSepherosa Ziehau 
6122b19ddf7eSSepherosa Ziehau 	/* Don't pick up IP packet w/o TCP/UDP checksum */
6123b19ddf7eSSepherosa Ziehau 	if ((cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) == 0)
6124b19ddf7eSSepherosa Ziehau 		return NULL;
6125b19ddf7eSSepherosa Ziehau 
6126b19ddf7eSSepherosa Ziehau 	/* May be IP fragment */
6127b19ddf7eSSepherosa Ziehau 	if (cur_rx->bge_tcp_udp_csum != 0xffff)
6128b19ddf7eSSepherosa Ziehau 		return NULL;
6129b19ddf7eSSepherosa Ziehau 
6130b19ddf7eSSepherosa Ziehau 	if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_IS_TCP)
6131b19ddf7eSSepherosa Ziehau 		pi->pi_l3proto = IPPROTO_TCP;
6132b19ddf7eSSepherosa Ziehau 	else
6133b19ddf7eSSepherosa Ziehau 		pi->pi_l3proto = IPPROTO_UDP;
6134b19ddf7eSSepherosa Ziehau 	pi->pi_netisr = NETISR_IP;
6135b19ddf7eSSepherosa Ziehau 	pi->pi_flags = 0;
6136b19ddf7eSSepherosa Ziehau 
6137b19ddf7eSSepherosa Ziehau 	return pi;
6138b19ddf7eSSepherosa Ziehau }
61394aa71e73SSepherosa Ziehau 
61404aa71e73SSepherosa Ziehau static void
bnx_sig_pre_reset(struct bnx_softc * sc,int type)61414aa71e73SSepherosa Ziehau bnx_sig_pre_reset(struct bnx_softc *sc, int type)
61424aa71e73SSepherosa Ziehau {
61434aa71e73SSepherosa Ziehau 	if (type == BNX_RESET_START || type == BNX_RESET_SUSPEND)
61444aa71e73SSepherosa Ziehau 		bnx_ape_driver_state_change(sc, type);
61454aa71e73SSepherosa Ziehau }
61464aa71e73SSepherosa Ziehau 
61474aa71e73SSepherosa Ziehau static void
bnx_sig_post_reset(struct bnx_softc * sc,int type)61484aa71e73SSepherosa Ziehau bnx_sig_post_reset(struct bnx_softc *sc, int type)
61494aa71e73SSepherosa Ziehau {
61504aa71e73SSepherosa Ziehau 	if (type == BNX_RESET_SHUTDOWN)
61514aa71e73SSepherosa Ziehau 		bnx_ape_driver_state_change(sc, type);
61529f5082d5SSepherosa Ziehau }
61539f5082d5SSepherosa Ziehau 
61549f5082d5SSepherosa Ziehau /*
61559f5082d5SSepherosa Ziehau  * Clear all stale locks and select the lock for this driver instance.
61569f5082d5SSepherosa Ziehau  */
61579f5082d5SSepherosa Ziehau static void
bnx_ape_lock_init(struct bnx_softc * sc)61589f5082d5SSepherosa Ziehau bnx_ape_lock_init(struct bnx_softc *sc)
61599f5082d5SSepherosa Ziehau {
61609f5082d5SSepherosa Ziehau 	uint32_t bit, regbase;
61619f5082d5SSepherosa Ziehau 	int i;
61629f5082d5SSepherosa Ziehau 
61639f5082d5SSepherosa Ziehau 	regbase = BGE_APE_PER_LOCK_GRANT;
61649f5082d5SSepherosa Ziehau 
61659f5082d5SSepherosa Ziehau 	/* Clear any stale locks. */
61669f5082d5SSepherosa Ziehau 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
61679f5082d5SSepherosa Ziehau 		switch (i) {
61689f5082d5SSepherosa Ziehau 		case BGE_APE_LOCK_PHY0:
61699f5082d5SSepherosa Ziehau 		case BGE_APE_LOCK_PHY1:
61709f5082d5SSepherosa Ziehau 		case BGE_APE_LOCK_PHY2:
61719f5082d5SSepherosa Ziehau 		case BGE_APE_LOCK_PHY3:
61729f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
61739f5082d5SSepherosa Ziehau 			break;
61749f5082d5SSepherosa Ziehau 
61759f5082d5SSepherosa Ziehau 		default:
61769f5082d5SSepherosa Ziehau 			if (sc->bnx_func_addr == 0)
61779f5082d5SSepherosa Ziehau 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
61789f5082d5SSepherosa Ziehau 			else
61799f5082d5SSepherosa Ziehau 				bit = 1 << sc->bnx_func_addr;
61809f5082d5SSepherosa Ziehau 			break;
61819f5082d5SSepherosa Ziehau 		}
61829f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, regbase + 4 * i, bit);
61839f5082d5SSepherosa Ziehau 	}
61849f5082d5SSepherosa Ziehau 
61859f5082d5SSepherosa Ziehau 	/* Select the PHY lock based on the device's function number. */
61869f5082d5SSepherosa Ziehau 	switch (sc->bnx_func_addr) {
61879f5082d5SSepherosa Ziehau 	case 0:
61889f5082d5SSepherosa Ziehau 		sc->bnx_phy_ape_lock = BGE_APE_LOCK_PHY0;
61899f5082d5SSepherosa Ziehau 		break;
61909f5082d5SSepherosa Ziehau 
61919f5082d5SSepherosa Ziehau 	case 1:
61929f5082d5SSepherosa Ziehau 		sc->bnx_phy_ape_lock = BGE_APE_LOCK_PHY1;
61939f5082d5SSepherosa Ziehau 		break;
61949f5082d5SSepherosa Ziehau 
61959f5082d5SSepherosa Ziehau 	case 2:
61969f5082d5SSepherosa Ziehau 		sc->bnx_phy_ape_lock = BGE_APE_LOCK_PHY2;
61979f5082d5SSepherosa Ziehau 		break;
61989f5082d5SSepherosa Ziehau 
61999f5082d5SSepherosa Ziehau 	case 3:
62009f5082d5SSepherosa Ziehau 		sc->bnx_phy_ape_lock = BGE_APE_LOCK_PHY3;
62019f5082d5SSepherosa Ziehau 		break;
62029f5082d5SSepherosa Ziehau 
62039f5082d5SSepherosa Ziehau 	default:
62049f5082d5SSepherosa Ziehau 		device_printf(sc->bnx_dev,
62059f5082d5SSepherosa Ziehau 		    "PHY lock not supported on this function\n");
62069f5082d5SSepherosa Ziehau 		break;
62079f5082d5SSepherosa Ziehau 	}
62089f5082d5SSepherosa Ziehau }
62099f5082d5SSepherosa Ziehau 
62109f5082d5SSepherosa Ziehau /*
62119f5082d5SSepherosa Ziehau  * Check for APE firmware, set flags, and print version info.
62129f5082d5SSepherosa Ziehau  */
62139f5082d5SSepherosa Ziehau static void
bnx_ape_read_fw_ver(struct bnx_softc * sc)62149f5082d5SSepherosa Ziehau bnx_ape_read_fw_ver(struct bnx_softc *sc)
62159f5082d5SSepherosa Ziehau {
62169f5082d5SSepherosa Ziehau 	const char *fwtype;
62179f5082d5SSepherosa Ziehau 	uint32_t apedata, features;
62189f5082d5SSepherosa Ziehau 
62199f5082d5SSepherosa Ziehau 	/* Check for a valid APE signature in shared memory. */
62209f5082d5SSepherosa Ziehau 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
62219f5082d5SSepherosa Ziehau 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
62229f5082d5SSepherosa Ziehau 		device_printf(sc->bnx_dev, "no APE signature\n");
62239f5082d5SSepherosa Ziehau 		sc->bnx_mfw_flags &= ~BNX_MFW_ON_APE;
62249f5082d5SSepherosa Ziehau 		return;
62259f5082d5SSepherosa Ziehau 	}
62269f5082d5SSepherosa Ziehau 
62279f5082d5SSepherosa Ziehau 	/* Check if APE firmware is running. */
62289f5082d5SSepherosa Ziehau 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
62299f5082d5SSepherosa Ziehau 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
62309f5082d5SSepherosa Ziehau 		device_printf(sc->bnx_dev, "APE signature found "
62319f5082d5SSepherosa Ziehau 		    "but FW status not ready! 0x%08x\n", apedata);
62329f5082d5SSepherosa Ziehau 		return;
62339f5082d5SSepherosa Ziehau 	}
62349f5082d5SSepherosa Ziehau 
62359f5082d5SSepherosa Ziehau 	sc->bnx_mfw_flags |= BNX_MFW_ON_APE;
62369f5082d5SSepherosa Ziehau 
62379f5082d5SSepherosa Ziehau 	/* Fetch the APE firwmare type and version. */
62389f5082d5SSepherosa Ziehau 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
62399f5082d5SSepherosa Ziehau 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
62409f5082d5SSepherosa Ziehau 	if (features & BGE_APE_FW_FEATURE_NCSI) {
62419f5082d5SSepherosa Ziehau 		sc->bnx_mfw_flags |= BNX_MFW_TYPE_NCSI;
62429f5082d5SSepherosa Ziehau 		fwtype = "NCSI";
62439f5082d5SSepherosa Ziehau 	} else if (features & BGE_APE_FW_FEATURE_DASH) {
62449f5082d5SSepherosa Ziehau 		sc->bnx_mfw_flags |= BNX_MFW_TYPE_DASH;
62459f5082d5SSepherosa Ziehau 		fwtype = "DASH";
62469f5082d5SSepherosa Ziehau 	} else {
62479f5082d5SSepherosa Ziehau 		fwtype = "UNKN";
62489f5082d5SSepherosa Ziehau 	}
62499f5082d5SSepherosa Ziehau 
62509f5082d5SSepherosa Ziehau 	/* Print the APE firmware version. */
62519f5082d5SSepherosa Ziehau 	device_printf(sc->bnx_dev, "APE FW version: %s v%d.%d.%d.%d\n",
62529f5082d5SSepherosa Ziehau 	    fwtype,
62539f5082d5SSepherosa Ziehau 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
62549f5082d5SSepherosa Ziehau 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
62559f5082d5SSepherosa Ziehau 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
62569f5082d5SSepherosa Ziehau 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
62579f5082d5SSepherosa Ziehau }
62589f5082d5SSepherosa Ziehau 
62599f5082d5SSepherosa Ziehau static int
bnx_ape_lock(struct bnx_softc * sc,int locknum)62609f5082d5SSepherosa Ziehau bnx_ape_lock(struct bnx_softc *sc, int locknum)
62619f5082d5SSepherosa Ziehau {
62629f5082d5SSepherosa Ziehau 	uint32_t bit, gnt, req, status;
62639f5082d5SSepherosa Ziehau 	int i, off;
62649f5082d5SSepherosa Ziehau 
62659f5082d5SSepherosa Ziehau 	if ((sc->bnx_mfw_flags & BNX_MFW_ON_APE) == 0)
62669f5082d5SSepherosa Ziehau 		return 0;
62679f5082d5SSepherosa Ziehau 
62689f5082d5SSepherosa Ziehau 	/* Lock request/grant registers have different bases. */
62699f5082d5SSepherosa Ziehau 	req = BGE_APE_PER_LOCK_REQ;
62709f5082d5SSepherosa Ziehau 	gnt = BGE_APE_PER_LOCK_GRANT;
62719f5082d5SSepherosa Ziehau 
62729f5082d5SSepherosa Ziehau 	off = 4 * locknum;
62739f5082d5SSepherosa Ziehau 
62749f5082d5SSepherosa Ziehau 	switch (locknum) {
62759f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_GPIO:
62769f5082d5SSepherosa Ziehau 		/* Lock required when using GPIO. */
62779f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
62789f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_REQ_DRIVER0;
62799f5082d5SSepherosa Ziehau 		else
62809f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
62819f5082d5SSepherosa Ziehau 		break;
62829f5082d5SSepherosa Ziehau 
62839f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_GRC:
62849f5082d5SSepherosa Ziehau 		/* Lock required to reset the device. */
62859f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
62869f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_REQ_DRIVER0;
62879f5082d5SSepherosa Ziehau 		else
62889f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
62899f5082d5SSepherosa Ziehau 		break;
62909f5082d5SSepherosa Ziehau 
62919f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_MEM:
62929f5082d5SSepherosa Ziehau 		/* Lock required when accessing certain APE memory. */
62939f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
62949f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_REQ_DRIVER0;
62959f5082d5SSepherosa Ziehau 		else
62969f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
62979f5082d5SSepherosa Ziehau 		break;
62989f5082d5SSepherosa Ziehau 
62999f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY0:
63009f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY1:
63019f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY2:
63029f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY3:
63039f5082d5SSepherosa Ziehau 		/* Lock required when accessing PHYs. */
63049f5082d5SSepherosa Ziehau 		bit = BGE_APE_LOCK_REQ_DRIVER0;
63059f5082d5SSepherosa Ziehau 		break;
63069f5082d5SSepherosa Ziehau 
63079f5082d5SSepherosa Ziehau 	default:
63089f5082d5SSepherosa Ziehau 		return EINVAL;
63099f5082d5SSepherosa Ziehau 	}
63109f5082d5SSepherosa Ziehau 
63119f5082d5SSepherosa Ziehau 	/* Request a lock. */
63129f5082d5SSepherosa Ziehau 	APE_WRITE_4(sc, req + off, bit);
63139f5082d5SSepherosa Ziehau 
63149f5082d5SSepherosa Ziehau 	/* Wait up to 1 second to acquire lock. */
63159f5082d5SSepherosa Ziehau 	for (i = 0; i < 20000; i++) {
63169f5082d5SSepherosa Ziehau 		status = APE_READ_4(sc, gnt + off);
63179f5082d5SSepherosa Ziehau 		if (status == bit)
63189f5082d5SSepherosa Ziehau 			break;
63199f5082d5SSepherosa Ziehau 		DELAY(50);
63209f5082d5SSepherosa Ziehau 	}
63219f5082d5SSepherosa Ziehau 
63229f5082d5SSepherosa Ziehau 	/* Handle any errors. */
63239f5082d5SSepherosa Ziehau 	if (status != bit) {
63249f5082d5SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if, "APE lock %d request failed! "
63259f5082d5SSepherosa Ziehau 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
63269f5082d5SSepherosa Ziehau 		    locknum, req + off, bit & 0xFFFF, gnt + off,
63279f5082d5SSepherosa Ziehau 		    status & 0xFFFF);
63289f5082d5SSepherosa Ziehau 		/* Revoke the lock request. */
63299f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, gnt + off, bit);
63309f5082d5SSepherosa Ziehau 		return EBUSY;
63319f5082d5SSepherosa Ziehau 	}
63329f5082d5SSepherosa Ziehau 
63339f5082d5SSepherosa Ziehau 	return 0;
63349f5082d5SSepherosa Ziehau }
63359f5082d5SSepherosa Ziehau 
63369f5082d5SSepherosa Ziehau static void
bnx_ape_unlock(struct bnx_softc * sc,int locknum)63379f5082d5SSepherosa Ziehau bnx_ape_unlock(struct bnx_softc *sc, int locknum)
63389f5082d5SSepherosa Ziehau {
63399f5082d5SSepherosa Ziehau 	uint32_t bit, gnt;
63409f5082d5SSepherosa Ziehau 	int off;
63419f5082d5SSepherosa Ziehau 
63429f5082d5SSepherosa Ziehau 	if ((sc->bnx_mfw_flags & BNX_MFW_ON_APE) == 0)
63439f5082d5SSepherosa Ziehau 		return;
63449f5082d5SSepherosa Ziehau 
63459f5082d5SSepherosa Ziehau 	gnt = BGE_APE_PER_LOCK_GRANT;
63469f5082d5SSepherosa Ziehau 
63479f5082d5SSepherosa Ziehau 	off = 4 * locknum;
63489f5082d5SSepherosa Ziehau 
63499f5082d5SSepherosa Ziehau 	switch (locknum) {
63509f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_GPIO:
63519f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
63529f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
63539f5082d5SSepherosa Ziehau 		else
63549f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
63559f5082d5SSepherosa Ziehau 		break;
63569f5082d5SSepherosa Ziehau 
63579f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_GRC:
63589f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
63599f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
63609f5082d5SSepherosa Ziehau 		else
63619f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
63629f5082d5SSepherosa Ziehau 		break;
63639f5082d5SSepherosa Ziehau 
63649f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_MEM:
63659f5082d5SSepherosa Ziehau 		if (sc->bnx_func_addr == 0)
63669f5082d5SSepherosa Ziehau 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
63679f5082d5SSepherosa Ziehau 		else
63689f5082d5SSepherosa Ziehau 			bit = 1 << sc->bnx_func_addr;
63699f5082d5SSepherosa Ziehau 		break;
63709f5082d5SSepherosa Ziehau 
63719f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY0:
63729f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY1:
63739f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY2:
63749f5082d5SSepherosa Ziehau 	case BGE_APE_LOCK_PHY3:
63759f5082d5SSepherosa Ziehau 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
63769f5082d5SSepherosa Ziehau 		break;
63779f5082d5SSepherosa Ziehau 
63789f5082d5SSepherosa Ziehau 	default:
63799f5082d5SSepherosa Ziehau 		return;
63809f5082d5SSepherosa Ziehau 	}
63819f5082d5SSepherosa Ziehau 
63829f5082d5SSepherosa Ziehau 	APE_WRITE_4(sc, gnt + off, bit);
63839f5082d5SSepherosa Ziehau }
63849f5082d5SSepherosa Ziehau 
63859f5082d5SSepherosa Ziehau /*
63869f5082d5SSepherosa Ziehau  * Send an event to the APE firmware.
63879f5082d5SSepherosa Ziehau  */
63889f5082d5SSepherosa Ziehau static void
bnx_ape_send_event(struct bnx_softc * sc,uint32_t event)63899f5082d5SSepherosa Ziehau bnx_ape_send_event(struct bnx_softc *sc, uint32_t event)
63909f5082d5SSepherosa Ziehau {
63919f5082d5SSepherosa Ziehau 	uint32_t apedata;
63929f5082d5SSepherosa Ziehau 	int i;
63939f5082d5SSepherosa Ziehau 
63949f5082d5SSepherosa Ziehau 	/* NCSI does not support APE events. */
63959f5082d5SSepherosa Ziehau 	if ((sc->bnx_mfw_flags & BNX_MFW_ON_APE) == 0)
63969f5082d5SSepherosa Ziehau 		return;
63979f5082d5SSepherosa Ziehau 
63989f5082d5SSepherosa Ziehau 	/* Wait up to 1ms for APE to service previous event. */
63999f5082d5SSepherosa Ziehau 	for (i = 10; i > 0; i--) {
64009f5082d5SSepherosa Ziehau 		if (bnx_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
64019f5082d5SSepherosa Ziehau 			break;
64029f5082d5SSepherosa Ziehau 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
64039f5082d5SSepherosa Ziehau 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
64049f5082d5SSepherosa Ziehau 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
64059f5082d5SSepherosa Ziehau 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
64069f5082d5SSepherosa Ziehau 			bnx_ape_unlock(sc, BGE_APE_LOCK_MEM);
64079f5082d5SSepherosa Ziehau 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
64089f5082d5SSepherosa Ziehau 			break;
64099f5082d5SSepherosa Ziehau 		}
64109f5082d5SSepherosa Ziehau 		bnx_ape_unlock(sc, BGE_APE_LOCK_MEM);
64119f5082d5SSepherosa Ziehau 		DELAY(100);
64129f5082d5SSepherosa Ziehau 	}
64139f5082d5SSepherosa Ziehau 	if (i == 0) {
64149f5082d5SSepherosa Ziehau 		if_printf(&sc->arpcom.ac_if,
64159f5082d5SSepherosa Ziehau 		    "APE event 0x%08x send timed out\n", event);
64169f5082d5SSepherosa Ziehau 	}
64179f5082d5SSepherosa Ziehau }
64189f5082d5SSepherosa Ziehau 
64199f5082d5SSepherosa Ziehau static void
bnx_ape_driver_state_change(struct bnx_softc * sc,int kind)64209f5082d5SSepherosa Ziehau bnx_ape_driver_state_change(struct bnx_softc *sc, int kind)
64219f5082d5SSepherosa Ziehau {
64229f5082d5SSepherosa Ziehau 	uint32_t apedata, event;
64239f5082d5SSepherosa Ziehau 
64249f5082d5SSepherosa Ziehau 	if ((sc->bnx_mfw_flags & BNX_MFW_ON_APE) == 0)
64259f5082d5SSepherosa Ziehau 		return;
64269f5082d5SSepherosa Ziehau 
64279f5082d5SSepherosa Ziehau 	switch (kind) {
64289f5082d5SSepherosa Ziehau 	case BNX_RESET_START:
64299f5082d5SSepherosa Ziehau 		/* If this is the first load, clear the load counter. */
64309f5082d5SSepherosa Ziehau 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
64319f5082d5SSepherosa Ziehau 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC) {
64329f5082d5SSepherosa Ziehau 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
64339f5082d5SSepherosa Ziehau 		} else {
64349f5082d5SSepherosa Ziehau 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
64359f5082d5SSepherosa Ziehau 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
64369f5082d5SSepherosa Ziehau 		}
64379f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
64389f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_SEG_SIG_MAGIC);
64399f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
64409f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_SEG_LEN_MAGIC);
64419f5082d5SSepherosa Ziehau 
64429f5082d5SSepherosa Ziehau 		/* Add some version info if bnx(4) supports it. */
64439f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
64449f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
64459f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
64469f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
64479f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
64489f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
64499f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
64509f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_DRVR_STATE_START);
64519f5082d5SSepherosa Ziehau 		event = BGE_APE_EVENT_STATUS_STATE_START;
64529f5082d5SSepherosa Ziehau 		break;
64539f5082d5SSepherosa Ziehau 
64549f5082d5SSepherosa Ziehau 	case BNX_RESET_SHUTDOWN:
64559f5082d5SSepherosa Ziehau 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
64569f5082d5SSepherosa Ziehau 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
64579f5082d5SSepherosa Ziehau 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
64589f5082d5SSepherosa Ziehau 		break;
64599f5082d5SSepherosa Ziehau 
64609f5082d5SSepherosa Ziehau 	case BNX_RESET_SUSPEND:
64619f5082d5SSepherosa Ziehau 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
64629f5082d5SSepherosa Ziehau 		break;
64639f5082d5SSepherosa Ziehau 
64649f5082d5SSepherosa Ziehau 	default:
64659f5082d5SSepherosa Ziehau 		return;
64669f5082d5SSepherosa Ziehau 	}
64679f5082d5SSepherosa Ziehau 
64689f5082d5SSepherosa Ziehau 	bnx_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
64699f5082d5SSepherosa Ziehau 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
64704aa71e73SSepherosa Ziehau }
6471