xref: /onnv-gate/usr/src/uts/common/io/bge/bge_main2.c (revision 5107:bb9efa2ee1e8)
11369Sdduvall /*
21369Sdduvall  * CDDL HEADER START
31369Sdduvall  *
41369Sdduvall  * The contents of this file are subject to the terms of the
51369Sdduvall  * Common Development and Distribution License (the "License").
61369Sdduvall  * You may not use this file except in compliance with the License.
71369Sdduvall  *
81369Sdduvall  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91369Sdduvall  * or http://www.opensolaris.org/os/licensing.
101369Sdduvall  * See the License for the specific language governing permissions
111369Sdduvall  * and limitations under the License.
121369Sdduvall  *
131369Sdduvall  * When distributing Covered Code, include this CDDL HEADER in each
141369Sdduvall  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151369Sdduvall  * If applicable, add the following below this CDDL HEADER, with the
161369Sdduvall  * fields enclosed by brackets "[]" replaced with your own identifying
171369Sdduvall  * information: Portions Copyright [yyyy] [name of copyright owner]
181369Sdduvall  *
191369Sdduvall  * CDDL HEADER END
201369Sdduvall  */
211369Sdduvall 
221369Sdduvall /*
233390Szh199473  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241369Sdduvall  * Use is subject to license terms.
251369Sdduvall  */
261369Sdduvall 
271369Sdduvall #pragma ident	"%Z%%M%	%I%	%E% SMI"
281369Sdduvall 
292675Szh199473 #include "bge_impl.h"
301369Sdduvall #include <sys/sdt.h>
311369Sdduvall 
321369Sdduvall /*
331369Sdduvall  * This is the string displayed by modinfo, etc.
341369Sdduvall  * Make sure you keep the version ID up to date!
351369Sdduvall  */
36*5107Seota static char bge_ident[] = "Broadcom Gb Ethernet v0.60";
371369Sdduvall 
381369Sdduvall /*
391369Sdduvall  * Property names
401369Sdduvall  */
411369Sdduvall static char debug_propname[] = "bge-debug-flags";
421369Sdduvall static char clsize_propname[] = "cache-line-size";
431369Sdduvall static char latency_propname[] = "latency-timer";
441369Sdduvall static char localmac_boolname[] = "local-mac-address?";
451369Sdduvall static char localmac_propname[] = "local-mac-address";
461369Sdduvall static char macaddr_propname[] = "mac-address";
471369Sdduvall static char subdev_propname[] = "subsystem-id";
481369Sdduvall static char subven_propname[] = "subsystem-vendor-id";
491369Sdduvall static char rxrings_propname[] = "bge-rx-rings";
501369Sdduvall static char txrings_propname[] = "bge-tx-rings";
511865Sdilpreet static char fm_cap[] = "fm-capable";
521908Sly149593 static char default_mtu[] = "default_mtu";
531369Sdduvall 
541369Sdduvall static int bge_add_intrs(bge_t *, int);
551369Sdduvall static void bge_rem_intrs(bge_t *);
561369Sdduvall 
571369Sdduvall /*
581369Sdduvall  * Describes the chip's DMA engine
591369Sdduvall  */
601369Sdduvall static ddi_dma_attr_t dma_attr = {
611369Sdduvall 	DMA_ATTR_V0,			/* dma_attr version	*/
621369Sdduvall 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
631369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_addr_hi	*/
641369Sdduvall 	0x00000000FFFFFFFFull,		/* dma_attr_count_max	*/
651369Sdduvall 	0x0000000000000001ull,		/* dma_attr_align	*/
661369Sdduvall 	0x00000FFF,			/* dma_attr_burstsizes	*/
671369Sdduvall 	0x00000001,			/* dma_attr_minxfer	*/
681369Sdduvall 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
691369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_seg		*/
701369Sdduvall 	1,				/* dma_attr_sgllen 	*/
711369Sdduvall 	0x00000001,			/* dma_attr_granular 	*/
721865Sdilpreet 	DDI_DMA_FLAGERR			/* dma_attr_flags */
731369Sdduvall };
741369Sdduvall 
751369Sdduvall /*
761369Sdduvall  * PIO access attributes for registers
771369Sdduvall  */
781369Sdduvall static ddi_device_acc_attr_t bge_reg_accattr = {
791369Sdduvall 	DDI_DEVICE_ATTR_V0,
801369Sdduvall 	DDI_NEVERSWAP_ACC,
811865Sdilpreet 	DDI_STRICTORDER_ACC,
821865Sdilpreet 	DDI_FLAGERR_ACC
831369Sdduvall };
841369Sdduvall 
851369Sdduvall /*
861369Sdduvall  * DMA access attributes for descriptors: NOT to be byte swapped.
871369Sdduvall  */
881369Sdduvall static ddi_device_acc_attr_t bge_desc_accattr = {
891369Sdduvall 	DDI_DEVICE_ATTR_V0,
901369Sdduvall 	DDI_NEVERSWAP_ACC,
911865Sdilpreet 	DDI_STRICTORDER_ACC,
921865Sdilpreet 	DDI_FLAGERR_ACC
931369Sdduvall };
941369Sdduvall 
951369Sdduvall /*
961369Sdduvall  * DMA access attributes for data: NOT to be byte swapped.
971369Sdduvall  */
981369Sdduvall static ddi_device_acc_attr_t bge_data_accattr = {
991369Sdduvall 	DDI_DEVICE_ATTR_V0,
1001369Sdduvall 	DDI_NEVERSWAP_ACC,
1011369Sdduvall 	DDI_STRICTORDER_ACC
1021369Sdduvall };
1031369Sdduvall 
1041369Sdduvall /*
1051369Sdduvall  * Versions of the O/S up to Solaris 8 didn't support network booting
1061369Sdduvall  * from any network interface except the first (NET0).  Patching this
1071369Sdduvall  * flag to a non-zero value will tell the driver to work around this
1081369Sdduvall  * limitation by creating an extra (internal) pathname node.  To do
1091369Sdduvall  * this, just add a line like the following to the CLIENT'S etc/system
1101369Sdduvall  * file ON THE ROOT FILESYSTEM SERVER before booting the client:
1111369Sdduvall  *
1121369Sdduvall  *	set bge:bge_net1_boot_support = 1;
1131369Sdduvall  */
1141369Sdduvall static uint32_t bge_net1_boot_support = 1;
1151369Sdduvall 
1162311Sseb static int		bge_m_start(void *);
1172311Sseb static void		bge_m_stop(void *);
1182311Sseb static int		bge_m_promisc(void *, boolean_t);
1192311Sseb static int		bge_m_multicst(void *, boolean_t, const uint8_t *);
1202311Sseb static int		bge_m_unicst(void *, const uint8_t *);
1212311Sseb static void		bge_m_resources(void *);
1222311Sseb static void		bge_m_ioctl(void *, queue_t *, mblk_t *);
1232311Sseb static boolean_t	bge_m_getcapab(void *, mac_capab_t, void *);
1242331Skrgopi static int		bge_unicst_set(void *, const uint8_t *,
1252331Skrgopi     mac_addr_slot_t);
1262331Skrgopi static int		bge_m_unicst_add(void *, mac_multi_addr_t *);
1272331Skrgopi static int		bge_m_unicst_remove(void *, mac_addr_slot_t);
1282331Skrgopi static int		bge_m_unicst_modify(void *, mac_multi_addr_t *);
1292331Skrgopi static int		bge_m_unicst_get(void *, mac_multi_addr_t *);
1302311Sseb 
1312311Sseb #define	BGE_M_CALLBACK_FLAGS	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB)
1322311Sseb 
1332311Sseb static mac_callbacks_t bge_m_callbacks = {
1342311Sseb 	BGE_M_CALLBACK_FLAGS,
1352311Sseb 	bge_m_stat,
1362311Sseb 	bge_m_start,
1372311Sseb 	bge_m_stop,
1382311Sseb 	bge_m_promisc,
1392311Sseb 	bge_m_multicst,
1402311Sseb 	bge_m_unicst,
1412311Sseb 	bge_m_tx,
1422311Sseb 	bge_m_resources,
1432311Sseb 	bge_m_ioctl,
1442311Sseb 	bge_m_getcapab
1452311Sseb };
1462311Sseb 
1471369Sdduvall /*
1481369Sdduvall  * ========== Transmit and receive ring reinitialisation ==========
1491369Sdduvall  */
1501369Sdduvall 
1511369Sdduvall /*
1521369Sdduvall  * These <reinit> routines each reset the specified ring to an initial
1531369Sdduvall  * state, assuming that the corresponding <init> routine has already
1541369Sdduvall  * been called exactly once.
1551369Sdduvall  */
1561369Sdduvall 
1571369Sdduvall static void
1581369Sdduvall bge_reinit_send_ring(send_ring_t *srp)
1591369Sdduvall {
1603334Sgs150176 	bge_queue_t *txbuf_queue;
1613334Sgs150176 	bge_queue_item_t *txbuf_head;
1623334Sgs150176 	sw_txbuf_t *txbuf;
1633334Sgs150176 	sw_sbd_t *ssbdp;
1643334Sgs150176 	uint32_t slot;
1653334Sgs150176 
1661369Sdduvall 	/*
1671369Sdduvall 	 * Reinitialise control variables ...
1681369Sdduvall 	 */
1693334Sgs150176 	srp->tx_flow = 0;
1701369Sdduvall 	srp->tx_next = 0;
1713334Sgs150176 	srp->txfill_next = 0;
1721369Sdduvall 	srp->tx_free = srp->desc.nslots;
1731369Sdduvall 	ASSERT(mutex_owned(srp->tc_lock));
1741369Sdduvall 	srp->tc_next = 0;
1753334Sgs150176 	srp->txpkt_next = 0;
1763334Sgs150176 	srp->tx_block = 0;
1773334Sgs150176 	srp->tx_nobd = 0;
1783334Sgs150176 	srp->tx_nobuf = 0;
1793334Sgs150176 
1803334Sgs150176 	/*
1813334Sgs150176 	 * Initialize the tx buffer push queue
1823334Sgs150176 	 */
1833334Sgs150176 	mutex_enter(srp->freetxbuf_lock);
1843334Sgs150176 	mutex_enter(srp->txbuf_lock);
1853334Sgs150176 	txbuf_queue = &srp->freetxbuf_queue;
1863334Sgs150176 	txbuf_queue->head = NULL;
1873334Sgs150176 	txbuf_queue->count = 0;
1883334Sgs150176 	txbuf_queue->lock = srp->freetxbuf_lock;
1893334Sgs150176 	srp->txbuf_push_queue = txbuf_queue;
1903334Sgs150176 
1913334Sgs150176 	/*
1923334Sgs150176 	 * Initialize the tx buffer pop queue
1933334Sgs150176 	 */
1943334Sgs150176 	txbuf_queue = &srp->txbuf_queue;
1953334Sgs150176 	txbuf_queue->head = NULL;
1963334Sgs150176 	txbuf_queue->count = 0;
1973334Sgs150176 	txbuf_queue->lock = srp->txbuf_lock;
1983334Sgs150176 	srp->txbuf_pop_queue = txbuf_queue;
1993334Sgs150176 	txbuf_head = srp->txbuf_head;
2003334Sgs150176 	txbuf = srp->txbuf;
2013334Sgs150176 	for (slot = 0; slot < srp->tx_buffers; ++slot) {
2023334Sgs150176 		txbuf_head->item = txbuf;
2033334Sgs150176 		txbuf_head->next = txbuf_queue->head;
2043334Sgs150176 		txbuf_queue->head = txbuf_head;
2053334Sgs150176 		txbuf_queue->count++;
2063334Sgs150176 		txbuf++;
2073334Sgs150176 		txbuf_head++;
2083334Sgs150176 	}
2093334Sgs150176 	mutex_exit(srp->txbuf_lock);
2103334Sgs150176 	mutex_exit(srp->freetxbuf_lock);
2111369Sdduvall 
2121369Sdduvall 	/*
2131369Sdduvall 	 * Zero and sync all the h/w Send Buffer Descriptors
2141369Sdduvall 	 */
2151369Sdduvall 	DMA_ZERO(srp->desc);
2161369Sdduvall 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
2173334Sgs150176 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
2183334Sgs150176 	ssbdp = srp->sw_sbds;
2193334Sgs150176 	for (slot = 0; slot < srp->desc.nslots; ++ssbdp, ++slot)
2203334Sgs150176 		ssbdp->pbuf = NULL;
2211369Sdduvall }
2221369Sdduvall 
2231369Sdduvall static void
2241369Sdduvall bge_reinit_recv_ring(recv_ring_t *rrp)
2251369Sdduvall {
2261369Sdduvall 	/*
2271369Sdduvall 	 * Reinitialise control variables ...
2281369Sdduvall 	 */
2291369Sdduvall 	rrp->rx_next = 0;
2301369Sdduvall }
2311369Sdduvall 
2321369Sdduvall static void
2333334Sgs150176 bge_reinit_buff_ring(buff_ring_t *brp, uint32_t ring)
2341369Sdduvall {
2351369Sdduvall 	bge_rbd_t *hw_rbd_p;
2361369Sdduvall 	sw_rbd_t *srbdp;
2371369Sdduvall 	uint32_t bufsize;
2381369Sdduvall 	uint32_t nslots;
2391369Sdduvall 	uint32_t slot;
2401369Sdduvall 
2411369Sdduvall 	static uint16_t ring_type_flag[BGE_BUFF_RINGS_MAX] = {
2421369Sdduvall 		RBD_FLAG_STD_RING,
2431369Sdduvall 		RBD_FLAG_JUMBO_RING,
2441369Sdduvall 		RBD_FLAG_MINI_RING
2451369Sdduvall 	};
2461369Sdduvall 
2471369Sdduvall 	/*
2481369Sdduvall 	 * Zero, initialise and sync all the h/w Receive Buffer Descriptors
2491369Sdduvall 	 * Note: all the remaining fields (<type>, <flags>, <ip_cksum>,
2501369Sdduvall 	 * <tcp_udp_cksum>, <error_flag>, <vlan_tag>, and <reserved>)
2511369Sdduvall 	 * should be zeroed, and so don't need to be set up specifically
2521369Sdduvall 	 * once the whole area has been cleared.
2531369Sdduvall 	 */
2541369Sdduvall 	DMA_ZERO(brp->desc);
2551369Sdduvall 
2561369Sdduvall 	hw_rbd_p = DMA_VPTR(brp->desc);
2571369Sdduvall 	nslots = brp->desc.nslots;
2581369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
2591369Sdduvall 	bufsize = brp->buf[0].size;
2601369Sdduvall 	srbdp = brp->sw_rbds;
2611369Sdduvall 	for (slot = 0; slot < nslots; ++hw_rbd_p, ++srbdp, ++slot) {
2621369Sdduvall 		hw_rbd_p->host_buf_addr = srbdp->pbuf.cookie.dmac_laddress;
2631369Sdduvall 		hw_rbd_p->index = slot;
2641369Sdduvall 		hw_rbd_p->len = bufsize;
2651369Sdduvall 		hw_rbd_p->opaque = srbdp->pbuf.token;
2661369Sdduvall 		hw_rbd_p->flags |= ring_type_flag[ring];
2671369Sdduvall 	}
2681369Sdduvall 
2691369Sdduvall 	DMA_SYNC(brp->desc, DDI_DMA_SYNC_FORDEV);
2701369Sdduvall 
2711369Sdduvall 	/*
2721369Sdduvall 	 * Finally, reinitialise the ring control variables ...
2731369Sdduvall 	 */
2741369Sdduvall 	brp->rf_next = (nslots != 0) ? (nslots-1) : 0;
2751369Sdduvall }
2761369Sdduvall 
2771369Sdduvall /*
2781369Sdduvall  * Reinitialize all rings
2791369Sdduvall  */
2801369Sdduvall static void
2811369Sdduvall bge_reinit_rings(bge_t *bgep)
2821369Sdduvall {
2833334Sgs150176 	uint32_t ring;
2841369Sdduvall 
2851369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
2861369Sdduvall 
2871369Sdduvall 	/*
2881369Sdduvall 	 * Send Rings ...
2891369Sdduvall 	 */
2901369Sdduvall 	for (ring = 0; ring < bgep->chipid.tx_rings; ++ring)
2911369Sdduvall 		bge_reinit_send_ring(&bgep->send[ring]);
2921369Sdduvall 
2931369Sdduvall 	/*
2941369Sdduvall 	 * Receive Return Rings ...
2951369Sdduvall 	 */
2961369Sdduvall 	for (ring = 0; ring < bgep->chipid.rx_rings; ++ring)
2971369Sdduvall 		bge_reinit_recv_ring(&bgep->recv[ring]);
2981369Sdduvall 
2991369Sdduvall 	/*
3001369Sdduvall 	 * Receive Producer Rings ...
3011369Sdduvall 	 */
3021369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
3031369Sdduvall 		bge_reinit_buff_ring(&bgep->buff[ring], ring);
3041369Sdduvall }
3051369Sdduvall 
3061369Sdduvall /*
3071369Sdduvall  * ========== Internal state management entry points ==========
3081369Sdduvall  */
3091369Sdduvall 
3101369Sdduvall #undef	BGE_DBG
3111369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
3121369Sdduvall 
3131369Sdduvall /*
3141369Sdduvall  * These routines provide all the functionality required by the
3151369Sdduvall  * corresponding GLD entry points, but don't update the GLD state
3161369Sdduvall  * so they can be called internally without disturbing our record
3171369Sdduvall  * of what GLD thinks we should be doing ...
3181369Sdduvall  */
3191369Sdduvall 
3201369Sdduvall /*
3211369Sdduvall  *	bge_reset() -- reset h/w & rings to initial state
3221369Sdduvall  */
3231865Sdilpreet static int
3241408Srandyf #ifdef BGE_IPMI_ASF
3251408Srandyf bge_reset(bge_t *bgep, uint_t asf_mode)
3261408Srandyf #else
3271369Sdduvall bge_reset(bge_t *bgep)
3281408Srandyf #endif
3291369Sdduvall {
3303334Sgs150176 	uint32_t	ring;
3311865Sdilpreet 	int retval;
3321369Sdduvall 
3331369Sdduvall 	BGE_TRACE(("bge_reset($%p)", (void *)bgep));
3341369Sdduvall 
3351369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3361369Sdduvall 
3371369Sdduvall 	/*
3381369Sdduvall 	 * Grab all the other mutexes in the world (this should
3391369Sdduvall 	 * ensure no other threads are manipulating driver state)
3401369Sdduvall 	 */
3411369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
3421369Sdduvall 		mutex_enter(bgep->recv[ring].rx_lock);
3431369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
3441369Sdduvall 		mutex_enter(bgep->buff[ring].rf_lock);
3451369Sdduvall 	rw_enter(bgep->errlock, RW_WRITER);
3461369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3473334Sgs150176 		mutex_enter(bgep->send[ring].tx_lock);
3483334Sgs150176 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3491369Sdduvall 		mutex_enter(bgep->send[ring].tc_lock);
3501369Sdduvall 
3511408Srandyf #ifdef BGE_IPMI_ASF
3521865Sdilpreet 	retval = bge_chip_reset(bgep, B_TRUE, asf_mode);
3531408Srandyf #else
3541865Sdilpreet 	retval = bge_chip_reset(bgep, B_TRUE);
3551408Srandyf #endif
3561369Sdduvall 	bge_reinit_rings(bgep);
3571369Sdduvall 
3581369Sdduvall 	/*
3591369Sdduvall 	 * Free the world ...
3601369Sdduvall 	 */
3611369Sdduvall 	for (ring = BGE_SEND_RINGS_MAX; ring-- > 0; )
3621369Sdduvall 		mutex_exit(bgep->send[ring].tc_lock);
3633334Sgs150176 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3643334Sgs150176 		mutex_exit(bgep->send[ring].tx_lock);
3651369Sdduvall 	rw_exit(bgep->errlock);
3661369Sdduvall 	for (ring = BGE_BUFF_RINGS_MAX; ring-- > 0; )
3671369Sdduvall 		mutex_exit(bgep->buff[ring].rf_lock);
3681369Sdduvall 	for (ring = BGE_RECV_RINGS_MAX; ring-- > 0; )
3691369Sdduvall 		mutex_exit(bgep->recv[ring].rx_lock);
3701369Sdduvall 
3711369Sdduvall 	BGE_DEBUG(("bge_reset($%p) done", (void *)bgep));
3721865Sdilpreet 	return (retval);
3731369Sdduvall }
3741369Sdduvall 
3751369Sdduvall /*
3761369Sdduvall  *	bge_stop() -- stop processing, don't reset h/w or rings
3771369Sdduvall  */
3781369Sdduvall static void
3791369Sdduvall bge_stop(bge_t *bgep)
3801369Sdduvall {
3811369Sdduvall 	BGE_TRACE(("bge_stop($%p)", (void *)bgep));
3821369Sdduvall 
3831369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3841369Sdduvall 
3851408Srandyf #ifdef BGE_IPMI_ASF
3861408Srandyf 	if (bgep->asf_enabled) {
3871408Srandyf 		bgep->asf_pseudostop = B_TRUE;
3881408Srandyf 	} else {
3891408Srandyf #endif
3901408Srandyf 		bge_chip_stop(bgep, B_FALSE);
3911408Srandyf #ifdef BGE_IPMI_ASF
3921408Srandyf 	}
3931408Srandyf #endif
3941369Sdduvall 
3951369Sdduvall 	BGE_DEBUG(("bge_stop($%p) done", (void *)bgep));
3961369Sdduvall }
3971369Sdduvall 
3981369Sdduvall /*
3991369Sdduvall  *	bge_start() -- start transmitting/receiving
4001369Sdduvall  */
4011865Sdilpreet static int
4021369Sdduvall bge_start(bge_t *bgep, boolean_t reset_phys)
4031369Sdduvall {
4041865Sdilpreet 	int retval;
4051865Sdilpreet 
4061369Sdduvall 	BGE_TRACE(("bge_start($%p, %d)", (void *)bgep, reset_phys));
4071369Sdduvall 
4081369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
4091369Sdduvall 
4101369Sdduvall 	/*
4111369Sdduvall 	 * Start chip processing, including enabling interrupts
4121369Sdduvall 	 */
4131865Sdilpreet 	retval = bge_chip_start(bgep, reset_phys);
4141369Sdduvall 
4151369Sdduvall 	BGE_DEBUG(("bge_start($%p, %d) done", (void *)bgep, reset_phys));
4161865Sdilpreet 	return (retval);
4171369Sdduvall }
4181369Sdduvall 
4191369Sdduvall /*
4201369Sdduvall  * bge_restart - restart transmitting/receiving after error or suspend
4211369Sdduvall  */
4221865Sdilpreet int
4231369Sdduvall bge_restart(bge_t *bgep, boolean_t reset_phys)
4241369Sdduvall {
4251865Sdilpreet 	int retval = DDI_SUCCESS;
4261369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
4271369Sdduvall 
4281408Srandyf #ifdef BGE_IPMI_ASF
4291408Srandyf 	if (bgep->asf_enabled) {
4301865Sdilpreet 		if (bge_reset(bgep, ASF_MODE_POST_INIT) != DDI_SUCCESS)
4311865Sdilpreet 			retval = DDI_FAILURE;
4321408Srandyf 	} else
4331865Sdilpreet 		if (bge_reset(bgep, ASF_MODE_NONE) != DDI_SUCCESS)
4341865Sdilpreet 			retval = DDI_FAILURE;
4351408Srandyf #else
4361865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS)
4371865Sdilpreet 		retval = DDI_FAILURE;
4381408Srandyf #endif
4393440Szh199473 	if (bgep->bge_mac_state == BGE_MAC_STARTED) {
4401865Sdilpreet 		if (bge_start(bgep, reset_phys) != DDI_SUCCESS)
4411865Sdilpreet 			retval = DDI_FAILURE;
4421369Sdduvall 		bgep->watchdog = 0;
4433334Sgs150176 		ddi_trigger_softintr(bgep->drain_id);
4441369Sdduvall 	}
4451369Sdduvall 
4461369Sdduvall 	BGE_DEBUG(("bge_restart($%p, %d) done", (void *)bgep, reset_phys));
4471865Sdilpreet 	return (retval);
4481369Sdduvall }
4491369Sdduvall 
4501369Sdduvall 
4511369Sdduvall /*
4521369Sdduvall  * ========== Nemo-required management entry points ==========
4531369Sdduvall  */
4541369Sdduvall 
4551369Sdduvall #undef	BGE_DBG
4561369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
4571369Sdduvall 
4581369Sdduvall /*
4591369Sdduvall  *	bge_m_stop() -- stop transmitting/receiving
4601369Sdduvall  */
4611369Sdduvall static void
4621369Sdduvall bge_m_stop(void *arg)
4631369Sdduvall {
4641369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
4653334Sgs150176 	send_ring_t *srp;
4663334Sgs150176 	uint32_t ring;
4671369Sdduvall 
4681369Sdduvall 	BGE_TRACE(("bge_m_stop($%p)", arg));
4691369Sdduvall 
4701369Sdduvall 	/*
4711369Sdduvall 	 * Just stop processing, then record new GLD state
4721369Sdduvall 	 */
4731369Sdduvall 	mutex_enter(bgep->genlock);
4741865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
4751865Sdilpreet 		/* can happen during autorecovery */
4761865Sdilpreet 		mutex_exit(bgep->genlock);
4771865Sdilpreet 		return;
4781865Sdilpreet 	}
4791369Sdduvall 	bge_stop(bgep);
4803334Sgs150176 	/*
4813334Sgs150176 	 * Free the possible tx buffers allocated in tx process.
4823334Sgs150176 	 */
4833334Sgs150176 #ifdef BGE_IPMI_ASF
4843334Sgs150176 	if (!bgep->asf_pseudostop)
4853334Sgs150176 #endif
4863334Sgs150176 	{
4873334Sgs150176 		rw_enter(bgep->errlock, RW_WRITER);
4883334Sgs150176 		for (ring = 0; ring < bgep->chipid.tx_rings; ++ring) {
4893334Sgs150176 			srp = &bgep->send[ring];
4903334Sgs150176 			mutex_enter(srp->tx_lock);
4913334Sgs150176 			if (srp->tx_array > 1)
4923334Sgs150176 				bge_free_txbuf_arrays(srp);
4933334Sgs150176 			mutex_exit(srp->tx_lock);
4943334Sgs150176 		}
4953334Sgs150176 		rw_exit(bgep->errlock);
4963334Sgs150176 	}
4971369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STOPPED;
4981369Sdduvall 	BGE_DEBUG(("bge_m_stop($%p) done", arg));
4991865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
5001865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
5011369Sdduvall 	mutex_exit(bgep->genlock);
5021369Sdduvall }
5031369Sdduvall 
5041369Sdduvall /*
5051369Sdduvall  *	bge_m_start() -- start transmitting/receiving
5061369Sdduvall  */
5071369Sdduvall static int
5081369Sdduvall bge_m_start(void *arg)
5091369Sdduvall {
5101369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
5111369Sdduvall 
5121369Sdduvall 	BGE_TRACE(("bge_m_start($%p)", arg));
5131369Sdduvall 
5141369Sdduvall 	/*
5151369Sdduvall 	 * Start processing and record new GLD state
5161369Sdduvall 	 */
5171369Sdduvall 	mutex_enter(bgep->genlock);
5181865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
5191865Sdilpreet 		/* can happen during autorecovery */
5201865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5211865Sdilpreet 		mutex_exit(bgep->genlock);
5221865Sdilpreet 		return (EIO);
5231865Sdilpreet 	}
5241408Srandyf #ifdef BGE_IPMI_ASF
5251408Srandyf 	if (bgep->asf_enabled) {
5261408Srandyf 		if ((bgep->asf_status == ASF_STAT_RUN) &&
5274588Sml149210 		    (bgep->asf_pseudostop)) {
5281408Srandyf 			bgep->bge_mac_state = BGE_MAC_STARTED;
5291408Srandyf 			mutex_exit(bgep->genlock);
5301408Srandyf 			return (0);
5311408Srandyf 		}
5321408Srandyf 	}
5331865Sdilpreet 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
5341408Srandyf #else
5351865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS) {
5361408Srandyf #endif
5371865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
5381865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
5391865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5401865Sdilpreet 		mutex_exit(bgep->genlock);
5411865Sdilpreet 		return (EIO);
5421865Sdilpreet 	}
5431865Sdilpreet 	if (bge_start(bgep, B_TRUE) != DDI_SUCCESS) {
5441865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
5451865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
5461865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5471865Sdilpreet 		mutex_exit(bgep->genlock);
5481865Sdilpreet 		return (EIO);
5491865Sdilpreet 	}
5501369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STARTED;
5511369Sdduvall 	BGE_DEBUG(("bge_m_start($%p) done", arg));
5521408Srandyf 
5531865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
5541865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5551865Sdilpreet 		mutex_exit(bgep->genlock);
5561865Sdilpreet 		return (EIO);
5571865Sdilpreet 	}
5581865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
5591865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5601865Sdilpreet 		mutex_exit(bgep->genlock);
5611865Sdilpreet 		return (EIO);
5621865Sdilpreet 	}
5631408Srandyf #ifdef BGE_IPMI_ASF
5641408Srandyf 	if (bgep->asf_enabled) {
5651408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
5661408Srandyf 			/* start ASF heart beat */
5671408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
5684588Sml149210 			    (void *)bgep,
5694588Sml149210 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
5701408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
5711408Srandyf 		}
5721408Srandyf 	}
5731408Srandyf #endif
5741369Sdduvall 	mutex_exit(bgep->genlock);
5751369Sdduvall 
5761369Sdduvall 	return (0);
5771369Sdduvall }
5781369Sdduvall 
5791369Sdduvall /*
5802331Skrgopi  *	bge_m_unicst() -- set the physical network address
5811369Sdduvall  */
5821369Sdduvall static int
5831369Sdduvall bge_m_unicst(void *arg, const uint8_t *macaddr)
5841369Sdduvall {
5852331Skrgopi 	/*
5862331Skrgopi 	 * Request to set address in
5872331Skrgopi 	 * address slot 0, i.e., default address
5882331Skrgopi 	 */
5892331Skrgopi 	return (bge_unicst_set(arg, macaddr, 0));
5902331Skrgopi }
5912331Skrgopi 
5922331Skrgopi /*
5932331Skrgopi  *	bge_unicst_set() -- set the physical network address
5942331Skrgopi  */
5952331Skrgopi static int
5962331Skrgopi bge_unicst_set(void *arg, const uint8_t *macaddr, mac_addr_slot_t slot)
5972331Skrgopi {
5981369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
5991369Sdduvall 
6001369Sdduvall 	BGE_TRACE(("bge_m_unicst_set($%p, %s)", arg,
6014588Sml149210 	    ether_sprintf((void *)macaddr)));
6021369Sdduvall 	/*
6031369Sdduvall 	 * Remember the new current address in the driver state
6041369Sdduvall 	 * Sync the chip's idea of the address too ...
6051369Sdduvall 	 */
6061369Sdduvall 	mutex_enter(bgep->genlock);
6071865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
6081865Sdilpreet 		/* can happen during autorecovery */
6091865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6101865Sdilpreet 		mutex_exit(bgep->genlock);
6111865Sdilpreet 		return (EIO);
6121865Sdilpreet 	}
6132331Skrgopi 	ethaddr_copy(macaddr, bgep->curr_addr[slot].addr);
6141408Srandyf #ifdef BGE_IPMI_ASF
6151865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE) {
6161865Sdilpreet #else
6171865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
6181865Sdilpreet #endif
6191865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
6201865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
6211865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6221865Sdilpreet 		mutex_exit(bgep->genlock);
6231865Sdilpreet 		return (EIO);
6241865Sdilpreet 	}
6251865Sdilpreet #ifdef BGE_IPMI_ASF
6261408Srandyf 	if (bgep->asf_enabled) {
6271408Srandyf 		/*
6281408Srandyf 		 * The above bge_chip_sync() function wrote the ethernet MAC
6291408Srandyf 		 * addresses registers which destroyed the IPMI/ASF sideband.
6301408Srandyf 		 * Here, we have to reset chip to make IPMI/ASF sideband work.
6311408Srandyf 		 */
6321408Srandyf 		if (bgep->asf_status == ASF_STAT_RUN) {
6331408Srandyf 			/*
6341408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
6351408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
6361408Srandyf 			 * may crash.
6371408Srandyf 			 */
6381408Srandyf 			bge_asf_update_status(bgep);
6391408Srandyf 			bge_asf_stop_timer(bgep);
6401408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
6411408Srandyf 
6421408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
6431408Srandyf 		}
6441865Sdilpreet 		bge_chip_stop(bgep, B_FALSE);
6451408Srandyf 
6461865Sdilpreet 		if (bge_restart(bgep, B_FALSE) == DDI_FAILURE) {
6471865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
6481865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->io_handle);
6491865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
6501865Sdilpreet 			    DDI_SERVICE_DEGRADED);
6511865Sdilpreet 			mutex_exit(bgep->genlock);
6521865Sdilpreet 			return (EIO);
6531865Sdilpreet 		}
6541865Sdilpreet 
6551408Srandyf 		/*
6561408Srandyf 		 * Start our ASF heartbeat counter as soon as possible.
6571408Srandyf 		 */
6581408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
6591408Srandyf 			/* start ASF heart beat */
6601408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
6614588Sml149210 			    (void *)bgep,
6624588Sml149210 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
6631408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
6641408Srandyf 		}
6651408Srandyf 	}
6661408Srandyf #endif
6671369Sdduvall 	BGE_DEBUG(("bge_m_unicst_set($%p) done", arg));
6681865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
6691865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6701865Sdilpreet 		mutex_exit(bgep->genlock);
6711865Sdilpreet 		return (EIO);
6721865Sdilpreet 	}
6731865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
6741865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6751865Sdilpreet 		mutex_exit(bgep->genlock);
6761865Sdilpreet 		return (EIO);
6771865Sdilpreet 	}
6781369Sdduvall 	mutex_exit(bgep->genlock);
6791369Sdduvall 
6801369Sdduvall 	return (0);
6811369Sdduvall }
6821369Sdduvall 
6831369Sdduvall /*
6842331Skrgopi  * The following four routines are used as callbacks for multiple MAC
6852331Skrgopi  * address support:
6862331Skrgopi  *    -  bge_m_unicst_add(void *, mac_multi_addr_t *);
6872331Skrgopi  *    -  bge_m_unicst_remove(void *, mac_addr_slot_t);
6882331Skrgopi  *    -  bge_m_unicst_modify(void *, mac_multi_addr_t *);
6892331Skrgopi  *    -  bge_m_unicst_get(void *, mac_multi_addr_t *);
6902331Skrgopi  */
6912331Skrgopi 
6922331Skrgopi /*
6932331Skrgopi  * bge_m_unicst_add() - will find an unused address slot, set the
6942331Skrgopi  * address value to the one specified, reserve that slot and enable
6952331Skrgopi  * the NIC to start filtering on the new MAC address.
6962331Skrgopi  * address slot. Returns 0 on success.
6972331Skrgopi  */
6982331Skrgopi static int
6992331Skrgopi bge_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
7002331Skrgopi {
7012331Skrgopi 	bge_t *bgep = arg;		/* private device info	*/
7022331Skrgopi 	mac_addr_slot_t slot;
7032406Skrgopi 	int err;
7042331Skrgopi 
7052331Skrgopi 	if (mac_unicst_verify(bgep->mh,
7062331Skrgopi 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
7072331Skrgopi 		return (EINVAL);
7082331Skrgopi 
7092331Skrgopi 	mutex_enter(bgep->genlock);
7102331Skrgopi 	if (bgep->unicst_addr_avail == 0) {
7112331Skrgopi 		/* no slots available */
7122331Skrgopi 		mutex_exit(bgep->genlock);
7132331Skrgopi 		return (ENOSPC);
7142331Skrgopi 	}
7152331Skrgopi 
7162331Skrgopi 	/*
7172331Skrgopi 	 * Primary/default address is in slot 0. The next three
7182331Skrgopi 	 * addresses are the multiple MAC addresses. So multiple
7192331Skrgopi 	 * MAC address 0 is in slot 1, 1 in slot 2, and so on.
7202406Skrgopi 	 * So the first multiple MAC address resides in slot 1.
7212331Skrgopi 	 */
7222406Skrgopi 	for (slot = 1; slot < bgep->unicst_addr_total; slot++) {
7232406Skrgopi 		if (bgep->curr_addr[slot].set == B_FALSE) {
7242406Skrgopi 			bgep->curr_addr[slot].set = B_TRUE;
7252331Skrgopi 			break;
7262331Skrgopi 		}
7272331Skrgopi 	}
7282331Skrgopi 
7292406Skrgopi 	ASSERT(slot < bgep->unicst_addr_total);
7302331Skrgopi 	bgep->unicst_addr_avail--;
7312331Skrgopi 	mutex_exit(bgep->genlock);
7322331Skrgopi 	maddr->mma_slot = slot;
7332331Skrgopi 
7342331Skrgopi 	if ((err = bge_unicst_set(bgep, maddr->mma_addr, slot)) != 0) {
7352331Skrgopi 		mutex_enter(bgep->genlock);
7362406Skrgopi 		bgep->curr_addr[slot].set = B_FALSE;
7372331Skrgopi 		bgep->unicst_addr_avail++;
7382331Skrgopi 		mutex_exit(bgep->genlock);
7392331Skrgopi 	}
7402331Skrgopi 	return (err);
7412331Skrgopi }
7422331Skrgopi 
7432331Skrgopi /*
7442331Skrgopi  * bge_m_unicst_remove() - removes a MAC address that was added by a
7452331Skrgopi  * call to bge_m_unicst_add(). The slot number that was returned in
7462331Skrgopi  * add() is passed in the call to remove the address.
7472331Skrgopi  * Returns 0 on success.
7482331Skrgopi  */
7492331Skrgopi static int
7502331Skrgopi bge_m_unicst_remove(void *arg, mac_addr_slot_t slot)
7512331Skrgopi {
7522331Skrgopi 	bge_t *bgep = arg;		/* private device info	*/
7532331Skrgopi 
7542406Skrgopi 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
7552406Skrgopi 		return (EINVAL);
7562406Skrgopi 
7572331Skrgopi 	mutex_enter(bgep->genlock);
7582406Skrgopi 	if (bgep->curr_addr[slot].set == B_TRUE) {
7592406Skrgopi 		bgep->curr_addr[slot].set = B_FALSE;
7602331Skrgopi 		bgep->unicst_addr_avail++;
7612331Skrgopi 		mutex_exit(bgep->genlock);
7622331Skrgopi 		/*
7632331Skrgopi 		 * Copy the default address to the passed slot
7642331Skrgopi 		 */
7652406Skrgopi 		return (bge_unicst_set(bgep, bgep->curr_addr[0].addr, slot));
7662331Skrgopi 	}
7672331Skrgopi 	mutex_exit(bgep->genlock);
7682331Skrgopi 	return (EINVAL);
7692331Skrgopi }
7702331Skrgopi 
7712331Skrgopi /*
7722331Skrgopi  * bge_m_unicst_modify() - modifies the value of an address that
7732331Skrgopi  * has been added by bge_m_unicst_add(). The new address, address
7742331Skrgopi  * length and the slot number that was returned in the call to add
7752331Skrgopi  * should be passed to bge_m_unicst_modify(). mma_flags should be
7762331Skrgopi  * set to 0. Returns 0 on success.
7772331Skrgopi  */
7782331Skrgopi static int
7792331Skrgopi bge_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
7802331Skrgopi {
7812331Skrgopi 	bge_t *bgep = arg;		/* private device info	*/
7822331Skrgopi 	mac_addr_slot_t slot;
7832331Skrgopi 
7842331Skrgopi 	if (mac_unicst_verify(bgep->mh,
7852331Skrgopi 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
7862331Skrgopi 		return (EINVAL);
7872331Skrgopi 
7882331Skrgopi 	slot = maddr->mma_slot;
7892331Skrgopi 
7902406Skrgopi 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
7912406Skrgopi 		return (EINVAL);
7922406Skrgopi 
7932331Skrgopi 	mutex_enter(bgep->genlock);
7942406Skrgopi 	if (bgep->curr_addr[slot].set == B_TRUE) {
7952331Skrgopi 		mutex_exit(bgep->genlock);
7962331Skrgopi 		return (bge_unicst_set(bgep, maddr->mma_addr, slot));
7972331Skrgopi 	}
7982331Skrgopi 	mutex_exit(bgep->genlock);
7992331Skrgopi 
8002331Skrgopi 	return (EINVAL);
8012331Skrgopi }
8022331Skrgopi 
8032331Skrgopi /*
8042331Skrgopi  * bge_m_unicst_get() - will get the MAC address and all other
8052331Skrgopi  * information related to the address slot passed in mac_multi_addr_t.
8062331Skrgopi  * mma_flags should be set to 0 in the call.
8072331Skrgopi  * On return, mma_flags can take the following values:
8082331Skrgopi  * 1) MMAC_SLOT_UNUSED
8092331Skrgopi  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
8102331Skrgopi  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
8112331Skrgopi  * 4) MMAC_SLOT_USED
8122331Skrgopi  */
8132331Skrgopi static int
8142331Skrgopi bge_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
8152331Skrgopi {
8162331Skrgopi 	bge_t *bgep = arg;		/* private device info	*/
8172331Skrgopi 	mac_addr_slot_t slot;
8182331Skrgopi 
8192331Skrgopi 	slot = maddr->mma_slot;
8202331Skrgopi 
8212406Skrgopi 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
8222331Skrgopi 		return (EINVAL);
8232331Skrgopi 
8242331Skrgopi 	mutex_enter(bgep->genlock);
8252406Skrgopi 	if (bgep->curr_addr[slot].set == B_TRUE) {
8262406Skrgopi 		ethaddr_copy(bgep->curr_addr[slot].addr,
8272331Skrgopi 		    maddr->mma_addr);
8282331Skrgopi 		maddr->mma_flags = MMAC_SLOT_USED;
8292331Skrgopi 	} else {
8302331Skrgopi 		maddr->mma_flags = MMAC_SLOT_UNUSED;
8312331Skrgopi 	}
8322331Skrgopi 	mutex_exit(bgep->genlock);
8332331Skrgopi 
8342331Skrgopi 	return (0);
8352331Skrgopi }
8362331Skrgopi 
8372331Skrgopi /*
8381369Sdduvall  * Compute the index of the required bit in the multicast hash map.
8391369Sdduvall  * This must mirror the way the hardware actually does it!
8401369Sdduvall  * See Broadcom document 570X-PG102-R page 125.
8411369Sdduvall  */
8421369Sdduvall static uint32_t
8431369Sdduvall bge_hash_index(const uint8_t *mca)
8441369Sdduvall {
8451369Sdduvall 	uint32_t hash;
8461369Sdduvall 
8471369Sdduvall 	CRC32(hash, mca, ETHERADDRL, -1U, crc32_table);
8481369Sdduvall 
8491369Sdduvall 	return (hash);
8501369Sdduvall }
8511369Sdduvall 
8521369Sdduvall /*
8531369Sdduvall  *	bge_m_multicst_add() -- enable/disable a multicast address
8541369Sdduvall  */
8551369Sdduvall static int
8561369Sdduvall bge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
8571369Sdduvall {
8581369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
8591369Sdduvall 	uint32_t hash;
8601369Sdduvall 	uint32_t index;
8611369Sdduvall 	uint32_t word;
8621369Sdduvall 	uint32_t bit;
8631369Sdduvall 	uint8_t *refp;
8641369Sdduvall 
8651369Sdduvall 	BGE_TRACE(("bge_m_multicst($%p, %s, %s)", arg,
8664588Sml149210 	    (add) ? "add" : "remove", ether_sprintf((void *)mca)));
8671369Sdduvall 
8681369Sdduvall 	/*
8691369Sdduvall 	 * Precalculate all required masks, pointers etc ...
8701369Sdduvall 	 */
8711369Sdduvall 	hash = bge_hash_index(mca);
8721369Sdduvall 	index = hash % BGE_HASH_TABLE_SIZE;
8731369Sdduvall 	word = index/32u;
8741369Sdduvall 	bit = 1 << (index % 32u);
8751369Sdduvall 	refp = &bgep->mcast_refs[index];
8761369Sdduvall 
8771369Sdduvall 	BGE_DEBUG(("bge_m_multicst: hash 0x%x index %d (%d:0x%x) = %d",
8784588Sml149210 	    hash, index, word, bit, *refp));
8791369Sdduvall 
8801369Sdduvall 	/*
8811369Sdduvall 	 * We must set the appropriate bit in the hash map (and the
8821369Sdduvall 	 * corresponding h/w register) when the refcount goes from 0
8831369Sdduvall 	 * to >0, and clear it when the last ref goes away (refcount
8841369Sdduvall 	 * goes from >0 back to 0).  If we change the hash map, we
8851369Sdduvall 	 * must also update the chip's hardware map registers.
8861369Sdduvall 	 */
8871369Sdduvall 	mutex_enter(bgep->genlock);
8881865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
8891865Sdilpreet 		/* can happen during autorecovery */
8901865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
8911865Sdilpreet 		mutex_exit(bgep->genlock);
8921865Sdilpreet 		return (EIO);
8931865Sdilpreet 	}
8941369Sdduvall 	if (add) {
8951369Sdduvall 		if ((*refp)++ == 0) {
8961369Sdduvall 			bgep->mcast_hash[word] |= bit;
8971408Srandyf #ifdef BGE_IPMI_ASF
8981865Sdilpreet 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
8991408Srandyf #else
9001865Sdilpreet 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
9011408Srandyf #endif
9021865Sdilpreet 				(void) bge_check_acc_handle(bgep,
9031865Sdilpreet 				    bgep->cfg_handle);
9041865Sdilpreet 				(void) bge_check_acc_handle(bgep,
9051865Sdilpreet 				    bgep->io_handle);
9061865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
9071865Sdilpreet 				    DDI_SERVICE_DEGRADED);
9081865Sdilpreet 				mutex_exit(bgep->genlock);
9091865Sdilpreet 				return (EIO);
9101865Sdilpreet 			}
9111369Sdduvall 		}
9121369Sdduvall 	} else {
9131369Sdduvall 		if (--(*refp) == 0) {
9141369Sdduvall 			bgep->mcast_hash[word] &= ~bit;
9151408Srandyf #ifdef BGE_IPMI_ASF
9161865Sdilpreet 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
9171408Srandyf #else
9181865Sdilpreet 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
9191408Srandyf #endif
9201865Sdilpreet 				(void) bge_check_acc_handle(bgep,
9211865Sdilpreet 				    bgep->cfg_handle);
9221865Sdilpreet 				(void) bge_check_acc_handle(bgep,
9231865Sdilpreet 				    bgep->io_handle);
9241865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
9251865Sdilpreet 				    DDI_SERVICE_DEGRADED);
9261865Sdilpreet 				mutex_exit(bgep->genlock);
9271865Sdilpreet 				return (EIO);
9281865Sdilpreet 			}
9291369Sdduvall 		}
9301369Sdduvall 	}
9311369Sdduvall 	BGE_DEBUG(("bge_m_multicst($%p) done", arg));
9321865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
9331865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9341865Sdilpreet 		mutex_exit(bgep->genlock);
9351865Sdilpreet 		return (EIO);
9361865Sdilpreet 	}
9371865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
9381865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9391865Sdilpreet 		mutex_exit(bgep->genlock);
9401865Sdilpreet 		return (EIO);
9411865Sdilpreet 	}
9421369Sdduvall 	mutex_exit(bgep->genlock);
9431369Sdduvall 
9441369Sdduvall 	return (0);
9451369Sdduvall }
9461369Sdduvall 
9471369Sdduvall /*
9481369Sdduvall  * bge_m_promisc() -- set or reset promiscuous mode on the board
9491369Sdduvall  *
9501369Sdduvall  *	Program the hardware to enable/disable promiscuous and/or
9511369Sdduvall  *	receive-all-multicast modes.
9521369Sdduvall  */
9531369Sdduvall static int
9541369Sdduvall bge_m_promisc(void *arg, boolean_t on)
9551369Sdduvall {
9561369Sdduvall 	bge_t *bgep = arg;
9571369Sdduvall 
9581369Sdduvall 	BGE_TRACE(("bge_m_promisc_set($%p, %d)", arg, on));
9591369Sdduvall 
9601369Sdduvall 	/*
9611369Sdduvall 	 * Store MAC layer specified mode and pass to chip layer to update h/w
9621369Sdduvall 	 */
9631369Sdduvall 	mutex_enter(bgep->genlock);
9641865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
9651865Sdilpreet 		/* can happen during autorecovery */
9661865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9671865Sdilpreet 		mutex_exit(bgep->genlock);
9681865Sdilpreet 		return (EIO);
9691865Sdilpreet 	}
9701369Sdduvall 	bgep->promisc = on;
9711408Srandyf #ifdef BGE_IPMI_ASF
9721865Sdilpreet 	if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
9731408Srandyf #else
9741865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
9751408Srandyf #endif
9761865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
9771865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
9781865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9791865Sdilpreet 		mutex_exit(bgep->genlock);
9801865Sdilpreet 		return (EIO);
9811865Sdilpreet 	}
9821369Sdduvall 	BGE_DEBUG(("bge_m_promisc_set($%p) done", arg));
9831865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
9841865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9851865Sdilpreet 		mutex_exit(bgep->genlock);
9861865Sdilpreet 		return (EIO);
9871865Sdilpreet 	}
9881865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
9891865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
9901865Sdilpreet 		mutex_exit(bgep->genlock);
9911865Sdilpreet 		return (EIO);
9921865Sdilpreet 	}
9931369Sdduvall 	mutex_exit(bgep->genlock);
9941369Sdduvall 	return (0);
9951369Sdduvall }
9961369Sdduvall 
9972311Sseb /*ARGSUSED*/
9982311Sseb static boolean_t
9992311Sseb bge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
10002311Sseb {
10012331Skrgopi 	bge_t *bgep = arg;
10022331Skrgopi 
10032311Sseb 	switch (cap) {
10042311Sseb 	case MAC_CAPAB_HCKSUM: {
10052311Sseb 		uint32_t *txflags = cap_data;
10062311Sseb 
10072311Sseb 		*txflags = HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM;
10082311Sseb 		break;
10092311Sseb 	}
10102331Skrgopi 
10112311Sseb 	case MAC_CAPAB_POLL:
10122311Sseb 		/*
10132311Sseb 		 * There's nothing for us to fill in, simply returning
10142311Sseb 		 * B_TRUE stating that we support polling is sufficient.
10152311Sseb 		 */
10162311Sseb 		break;
10172331Skrgopi 
10182331Skrgopi 	case MAC_CAPAB_MULTIADDRESS: {
10192331Skrgopi 		multiaddress_capab_t	*mmacp = cap_data;
10202331Skrgopi 
10212331Skrgopi 		mutex_enter(bgep->genlock);
10222406Skrgopi 		/*
10232406Skrgopi 		 * The number of MAC addresses made available by
10242406Skrgopi 		 * this capability is one less than the total as
10252406Skrgopi 		 * the primary address in slot 0 is counted in
10262406Skrgopi 		 * the total.
10272406Skrgopi 		 */
10282406Skrgopi 		mmacp->maddr_naddr = bgep->unicst_addr_total - 1;
10292331Skrgopi 		mmacp->maddr_naddrfree = bgep->unicst_addr_avail;
10302331Skrgopi 		/* No multiple factory addresses, set mma_flag to 0 */
10312331Skrgopi 		mmacp->maddr_flag = 0;
10322331Skrgopi 		mmacp->maddr_handle = bgep;
10332331Skrgopi 		mmacp->maddr_add = bge_m_unicst_add;
10342331Skrgopi 		mmacp->maddr_remove = bge_m_unicst_remove;
10352331Skrgopi 		mmacp->maddr_modify = bge_m_unicst_modify;
10362331Skrgopi 		mmacp->maddr_get = bge_m_unicst_get;
10372331Skrgopi 		mmacp->maddr_reserve = NULL;
10382331Skrgopi 		mutex_exit(bgep->genlock);
10392331Skrgopi 		break;
10402331Skrgopi 	}
10412331Skrgopi 
10422311Sseb 	default:
10432311Sseb 		return (B_FALSE);
10442311Sseb 	}
10452311Sseb 	return (B_TRUE);
10462311Sseb }
10472311Sseb 
10481369Sdduvall /*
10491369Sdduvall  * Loopback ioctl code
10501369Sdduvall  */
10511369Sdduvall 
10521369Sdduvall static lb_property_t loopmodes[] = {
10531369Sdduvall 	{ normal,	"normal",	BGE_LOOP_NONE		},
10541369Sdduvall 	{ external,	"1000Mbps",	BGE_LOOP_EXTERNAL_1000	},
10551369Sdduvall 	{ external,	"100Mbps",	BGE_LOOP_EXTERNAL_100	},
10561369Sdduvall 	{ external,	"10Mbps",	BGE_LOOP_EXTERNAL_10	},
10571369Sdduvall 	{ internal,	"PHY",		BGE_LOOP_INTERNAL_PHY	},
10581369Sdduvall 	{ internal,	"MAC",		BGE_LOOP_INTERNAL_MAC	}
10591369Sdduvall };
10601369Sdduvall 
10611369Sdduvall static enum ioc_reply
10621369Sdduvall bge_set_loop_mode(bge_t *bgep, uint32_t mode)
10631369Sdduvall {
10641369Sdduvall 	/*
10651369Sdduvall 	 * If the mode isn't being changed, there's nothing to do ...
10661369Sdduvall 	 */
10671369Sdduvall 	if (mode == bgep->param_loop_mode)
10681369Sdduvall 		return (IOC_ACK);
10691369Sdduvall 
10701369Sdduvall 	/*
10711369Sdduvall 	 * Validate the requested mode and prepare a suitable message
10721369Sdduvall 	 * to explain the link down/up cycle that the change will
10731369Sdduvall 	 * probably induce ...
10741369Sdduvall 	 */
10751369Sdduvall 	switch (mode) {
10761369Sdduvall 	default:
10771369Sdduvall 		return (IOC_INVAL);
10781369Sdduvall 
10791369Sdduvall 	case BGE_LOOP_NONE:
10801369Sdduvall 	case BGE_LOOP_EXTERNAL_1000:
10811369Sdduvall 	case BGE_LOOP_EXTERNAL_100:
10821369Sdduvall 	case BGE_LOOP_EXTERNAL_10:
10831369Sdduvall 	case BGE_LOOP_INTERNAL_PHY:
10841369Sdduvall 	case BGE_LOOP_INTERNAL_MAC:
10851369Sdduvall 		break;
10861369Sdduvall 	}
10871369Sdduvall 
10881369Sdduvall 	/*
10891369Sdduvall 	 * All OK; tell the caller to reprogram
10901369Sdduvall 	 * the PHY and/or MAC for the new mode ...
10911369Sdduvall 	 */
10921369Sdduvall 	bgep->param_loop_mode = mode;
10931369Sdduvall 	return (IOC_RESTART_ACK);
10941369Sdduvall }
10951369Sdduvall 
10961369Sdduvall static enum ioc_reply
10971369Sdduvall bge_loop_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
10981369Sdduvall {
10991369Sdduvall 	lb_info_sz_t *lbsp;
11001369Sdduvall 	lb_property_t *lbpp;
11011369Sdduvall 	uint32_t *lbmp;
11021369Sdduvall 	int cmd;
11031369Sdduvall 
11041369Sdduvall 	_NOTE(ARGUNUSED(wq))
11051369Sdduvall 
11061369Sdduvall 	/*
11071369Sdduvall 	 * Validate format of ioctl
11081369Sdduvall 	 */
11091369Sdduvall 	if (mp->b_cont == NULL)
11101369Sdduvall 		return (IOC_INVAL);
11111369Sdduvall 
11121369Sdduvall 	cmd = iocp->ioc_cmd;
11131369Sdduvall 	switch (cmd) {
11141369Sdduvall 	default:
11151369Sdduvall 		/* NOTREACHED */
11161369Sdduvall 		bge_error(bgep, "bge_loop_ioctl: invalid cmd 0x%x", cmd);
11171369Sdduvall 		return (IOC_INVAL);
11181369Sdduvall 
11191369Sdduvall 	case LB_GET_INFO_SIZE:
11201369Sdduvall 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
11211369Sdduvall 			return (IOC_INVAL);
11221369Sdduvall 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
11231369Sdduvall 		*lbsp = sizeof (loopmodes);
11241369Sdduvall 		return (IOC_REPLY);
11251369Sdduvall 
11261369Sdduvall 	case LB_GET_INFO:
11271369Sdduvall 		if (iocp->ioc_count != sizeof (loopmodes))
11281369Sdduvall 			return (IOC_INVAL);
11291369Sdduvall 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
11301369Sdduvall 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
11311369Sdduvall 		return (IOC_REPLY);
11321369Sdduvall 
11331369Sdduvall 	case LB_GET_MODE:
11341369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
11351369Sdduvall 			return (IOC_INVAL);
11361369Sdduvall 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
11371369Sdduvall 		*lbmp = bgep->param_loop_mode;
11381369Sdduvall 		return (IOC_REPLY);
11391369Sdduvall 
11401369Sdduvall 	case LB_SET_MODE:
11411369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
11421369Sdduvall 			return (IOC_INVAL);
11431369Sdduvall 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
11441369Sdduvall 		return (bge_set_loop_mode(bgep, *lbmp));
11451369Sdduvall 	}
11461369Sdduvall }
11471369Sdduvall 
11481369Sdduvall /*
11491369Sdduvall  * Specific bge IOCTLs, the gld module handles the generic ones.
11501369Sdduvall  */
11511369Sdduvall static void
11521369Sdduvall bge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
11531369Sdduvall {
11541369Sdduvall 	bge_t *bgep = arg;
11551369Sdduvall 	struct iocblk *iocp;
11561369Sdduvall 	enum ioc_reply status;
11571369Sdduvall 	boolean_t need_privilege;
11581369Sdduvall 	int err;
11591369Sdduvall 	int cmd;
11601369Sdduvall 
11611369Sdduvall 	/*
11621369Sdduvall 	 * Validate the command before bothering with the mutex ...
11631369Sdduvall 	 */
11641369Sdduvall 	iocp = (struct iocblk *)mp->b_rptr;
11651369Sdduvall 	iocp->ioc_error = 0;
11661369Sdduvall 	need_privilege = B_TRUE;
11671369Sdduvall 	cmd = iocp->ioc_cmd;
11681369Sdduvall 	switch (cmd) {
11691369Sdduvall 	default:
11701369Sdduvall 		miocnak(wq, mp, 0, EINVAL);
11711369Sdduvall 		return;
11721369Sdduvall 
11731369Sdduvall 	case BGE_MII_READ:
11741369Sdduvall 	case BGE_MII_WRITE:
11751369Sdduvall 	case BGE_SEE_READ:
11761369Sdduvall 	case BGE_SEE_WRITE:
11772675Szh199473 	case BGE_FLASH_READ:
11782675Szh199473 	case BGE_FLASH_WRITE:
11791369Sdduvall 	case BGE_DIAG:
11801369Sdduvall 	case BGE_PEEK:
11811369Sdduvall 	case BGE_POKE:
11821369Sdduvall 	case BGE_PHY_RESET:
11831369Sdduvall 	case BGE_SOFT_RESET:
11841369Sdduvall 	case BGE_HARD_RESET:
11851369Sdduvall 		break;
11861369Sdduvall 
11871369Sdduvall 	case LB_GET_INFO_SIZE:
11881369Sdduvall 	case LB_GET_INFO:
11891369Sdduvall 	case LB_GET_MODE:
11901369Sdduvall 		need_privilege = B_FALSE;
11911369Sdduvall 		/* FALLTHRU */
11921369Sdduvall 	case LB_SET_MODE:
11931369Sdduvall 		break;
11941369Sdduvall 
11951369Sdduvall 	case ND_GET:
11961369Sdduvall 		need_privilege = B_FALSE;
11971369Sdduvall 		/* FALLTHRU */
11981369Sdduvall 	case ND_SET:
11991369Sdduvall 		break;
12001369Sdduvall 	}
12011369Sdduvall 
12021369Sdduvall 	if (need_privilege) {
12031369Sdduvall 		/*
12041369Sdduvall 		 * Check for specific net_config privilege on Solaris 10+.
12051369Sdduvall 		 */
12062681Sgs150176 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
12071369Sdduvall 		if (err != 0) {
12081369Sdduvall 			miocnak(wq, mp, 0, err);
12091369Sdduvall 			return;
12101369Sdduvall 		}
12111369Sdduvall 	}
12121369Sdduvall 
12131369Sdduvall 	mutex_enter(bgep->genlock);
12141865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
12151865Sdilpreet 		/* can happen during autorecovery */
12161865Sdilpreet 		mutex_exit(bgep->genlock);
12171865Sdilpreet 		miocnak(wq, mp, 0, EIO);
12181865Sdilpreet 		return;
12191865Sdilpreet 	}
12201369Sdduvall 
12211369Sdduvall 	switch (cmd) {
12221369Sdduvall 	default:
12231369Sdduvall 		_NOTE(NOTREACHED)
12241369Sdduvall 		status = IOC_INVAL;
12251369Sdduvall 		break;
12261369Sdduvall 
12271369Sdduvall 	case BGE_MII_READ:
12281369Sdduvall 	case BGE_MII_WRITE:
12291369Sdduvall 	case BGE_SEE_READ:
12301369Sdduvall 	case BGE_SEE_WRITE:
12312675Szh199473 	case BGE_FLASH_READ:
12322675Szh199473 	case BGE_FLASH_WRITE:
12331369Sdduvall 	case BGE_DIAG:
12341369Sdduvall 	case BGE_PEEK:
12351369Sdduvall 	case BGE_POKE:
12361369Sdduvall 	case BGE_PHY_RESET:
12371369Sdduvall 	case BGE_SOFT_RESET:
12381369Sdduvall 	case BGE_HARD_RESET:
12391369Sdduvall 		status = bge_chip_ioctl(bgep, wq, mp, iocp);
12401369Sdduvall 		break;
12411369Sdduvall 
12421369Sdduvall 	case LB_GET_INFO_SIZE:
12431369Sdduvall 	case LB_GET_INFO:
12441369Sdduvall 	case LB_GET_MODE:
12451369Sdduvall 	case LB_SET_MODE:
12461369Sdduvall 		status = bge_loop_ioctl(bgep, wq, mp, iocp);
12471369Sdduvall 		break;
12481369Sdduvall 
12491369Sdduvall 	case ND_GET:
12501369Sdduvall 	case ND_SET:
12511369Sdduvall 		status = bge_nd_ioctl(bgep, wq, mp, iocp);
12521369Sdduvall 		break;
12531369Sdduvall 	}
12541369Sdduvall 
12551369Sdduvall 	/*
12561369Sdduvall 	 * Do we need to reprogram the PHY and/or the MAC?
12571369Sdduvall 	 * Do it now, while we still have the mutex.
12581369Sdduvall 	 *
12591369Sdduvall 	 * Note: update the PHY first, 'cos it controls the
12601369Sdduvall 	 * speed/duplex parameters that the MAC code uses.
12611369Sdduvall 	 */
12621369Sdduvall 	switch (status) {
12631369Sdduvall 	case IOC_RESTART_REPLY:
12641369Sdduvall 	case IOC_RESTART_ACK:
12651865Sdilpreet 		if (bge_phys_update(bgep) != DDI_SUCCESS) {
12661865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
12671865Sdilpreet 			    DDI_SERVICE_DEGRADED);
12681865Sdilpreet 			status = IOC_INVAL;
12691865Sdilpreet 		}
12701408Srandyf #ifdef BGE_IPMI_ASF
12712675Szh199473 		if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
12721408Srandyf #else
12731865Sdilpreet 		if (bge_chip_sync(bgep) == DDI_FAILURE) {
12741408Srandyf #endif
12751865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
12761865Sdilpreet 			    DDI_SERVICE_DEGRADED);
12771865Sdilpreet 			status = IOC_INVAL;
12781865Sdilpreet 		}
12791369Sdduvall 		if (bgep->intr_type == DDI_INTR_TYPE_MSI)
12801369Sdduvall 			bge_chip_msi_trig(bgep);
12811369Sdduvall 		break;
12821369Sdduvall 	}
12831369Sdduvall 
12841865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
12851865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
12861865Sdilpreet 		status = IOC_INVAL;
12871865Sdilpreet 	}
12881865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
12891865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
12901865Sdilpreet 		status = IOC_INVAL;
12911865Sdilpreet 	}
12921369Sdduvall 	mutex_exit(bgep->genlock);
12931369Sdduvall 
12941369Sdduvall 	/*
12951369Sdduvall 	 * Finally, decide how to reply
12961369Sdduvall 	 */
12971369Sdduvall 	switch (status) {
12981369Sdduvall 	default:
12991369Sdduvall 	case IOC_INVAL:
13001369Sdduvall 		/*
13011369Sdduvall 		 * Error, reply with a NAK and EINVAL or the specified error
13021369Sdduvall 		 */
13031369Sdduvall 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
13044588Sml149210 		    EINVAL : iocp->ioc_error);
13051369Sdduvall 		break;
13061369Sdduvall 
13071369Sdduvall 	case IOC_DONE:
13081369Sdduvall 		/*
13091369Sdduvall 		 * OK, reply already sent
13101369Sdduvall 		 */
13111369Sdduvall 		break;
13121369Sdduvall 
13131369Sdduvall 	case IOC_RESTART_ACK:
13141369Sdduvall 	case IOC_ACK:
13151369Sdduvall 		/*
13161369Sdduvall 		 * OK, reply with an ACK
13171369Sdduvall 		 */
13181369Sdduvall 		miocack(wq, mp, 0, 0);
13191369Sdduvall 		break;
13201369Sdduvall 
13211369Sdduvall 	case IOC_RESTART_REPLY:
13221369Sdduvall 	case IOC_REPLY:
13231369Sdduvall 		/*
13241369Sdduvall 		 * OK, send prepared reply as ACK or NAK
13251369Sdduvall 		 */
13261369Sdduvall 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
13274588Sml149210 		    M_IOCACK : M_IOCNAK;
13281369Sdduvall 		qreply(wq, mp);
13291369Sdduvall 		break;
13301369Sdduvall 	}
13311369Sdduvall }
13321369Sdduvall 
13331369Sdduvall static void
13341369Sdduvall bge_m_resources(void *arg)
13351369Sdduvall {
13361369Sdduvall 	bge_t *bgep = arg;
13371369Sdduvall 	recv_ring_t *rrp;
13381369Sdduvall 	mac_rx_fifo_t mrf;
13391369Sdduvall 	int ring;
13401369Sdduvall 
13411369Sdduvall 	mutex_enter(bgep->genlock);
13421369Sdduvall 
13431369Sdduvall 	/*
13441369Sdduvall 	 * Register Rx rings as resources and save mac
13451369Sdduvall 	 * resource id for future reference
13461369Sdduvall 	 */
13471369Sdduvall 	mrf.mrf_type = MAC_RX_FIFO;
13481369Sdduvall 	mrf.mrf_blank = bge_chip_blank;
13491369Sdduvall 	mrf.mrf_arg = (void *)bgep;
13501369Sdduvall 	mrf.mrf_normal_blank_time = bge_rx_ticks_norm;
13511369Sdduvall 	mrf.mrf_normal_pkt_count = bge_rx_count_norm;
13521369Sdduvall 
13531369Sdduvall 	for (ring = 0; ring < bgep->chipid.rx_rings; ring++) {
13541369Sdduvall 		rrp = &bgep->recv[ring];
13552311Sseb 		rrp->handle = mac_resource_add(bgep->mh,
13561369Sdduvall 		    (mac_resource_t *)&mrf);
13571369Sdduvall 	}
13581369Sdduvall 
13591369Sdduvall 	mutex_exit(bgep->genlock);
13601369Sdduvall }
13611369Sdduvall 
13621369Sdduvall /*
13631369Sdduvall  * ========== Per-instance setup/teardown code ==========
13641369Sdduvall  */
13651369Sdduvall 
13661369Sdduvall #undef	BGE_DBG
13671369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
13683334Sgs150176 /*
13693334Sgs150176  * Allocate an area of memory and a DMA handle for accessing it
13703334Sgs150176  */
13713334Sgs150176 static int
13723334Sgs150176 bge_alloc_dma_mem(bge_t *bgep, size_t memsize, ddi_device_acc_attr_t *attr_p,
13733334Sgs150176 	uint_t dma_flags, dma_area_t *dma_p)
13743334Sgs150176 {
13753334Sgs150176 	caddr_t va;
13763334Sgs150176 	int err;
13773334Sgs150176 
13783334Sgs150176 	BGE_TRACE(("bge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
13794588Sml149210 	    (void *)bgep, memsize, attr_p, dma_flags, dma_p));
13803334Sgs150176 
13813334Sgs150176 	/*
13823334Sgs150176 	 * Allocate handle
13833334Sgs150176 	 */
13843334Sgs150176 	err = ddi_dma_alloc_handle(bgep->devinfo, &dma_attr,
13854588Sml149210 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_hdl);
13863334Sgs150176 	if (err != DDI_SUCCESS)
13873334Sgs150176 		return (DDI_FAILURE);
13883334Sgs150176 
13893334Sgs150176 	/*
13903334Sgs150176 	 * Allocate memory
13913334Sgs150176 	 */
13923334Sgs150176 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
13934588Sml149210 	    dma_flags, DDI_DMA_DONTWAIT, NULL, &va, &dma_p->alength,
13944588Sml149210 	    &dma_p->acc_hdl);
13953334Sgs150176 	if (err != DDI_SUCCESS)
13963334Sgs150176 		return (DDI_FAILURE);
13973334Sgs150176 
13983334Sgs150176 	/*
13993334Sgs150176 	 * Bind the two together
14003334Sgs150176 	 */
14013334Sgs150176 	dma_p->mem_va = va;
14023334Sgs150176 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
14034588Sml149210 	    va, dma_p->alength, dma_flags, DDI_DMA_DONTWAIT, NULL,
14044588Sml149210 	    &dma_p->cookie, &dma_p->ncookies);
14053334Sgs150176 
14063334Sgs150176 	BGE_DEBUG(("bge_alloc_dma_mem(): bind %d bytes; err %d, %d cookies",
14074588Sml149210 	    dma_p->alength, err, dma_p->ncookies));
14083334Sgs150176 
14093334Sgs150176 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
14103334Sgs150176 		return (DDI_FAILURE);
14113334Sgs150176 
14123334Sgs150176 	dma_p->nslots = ~0U;
14133334Sgs150176 	dma_p->size = ~0U;
14143334Sgs150176 	dma_p->token = ~0U;
14153334Sgs150176 	dma_p->offset = 0;
14163334Sgs150176 	return (DDI_SUCCESS);
14173334Sgs150176 }
14183334Sgs150176 
14193334Sgs150176 /*
14203334Sgs150176  * Free one allocated area of DMAable memory
14213334Sgs150176  */
14223334Sgs150176 static void
14233334Sgs150176 bge_free_dma_mem(dma_area_t *dma_p)
14243334Sgs150176 {
14253334Sgs150176 	if (dma_p->dma_hdl != NULL) {
14263334Sgs150176 		if (dma_p->ncookies) {
14273334Sgs150176 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
14283334Sgs150176 			dma_p->ncookies = 0;
14293334Sgs150176 		}
14303334Sgs150176 		ddi_dma_free_handle(&dma_p->dma_hdl);
14313334Sgs150176 		dma_p->dma_hdl = NULL;
14323334Sgs150176 	}
14333334Sgs150176 
14343334Sgs150176 	if (dma_p->acc_hdl != NULL) {
14353334Sgs150176 		ddi_dma_mem_free(&dma_p->acc_hdl);
14363334Sgs150176 		dma_p->acc_hdl = NULL;
14373334Sgs150176 	}
14383334Sgs150176 }
14391369Sdduvall /*
14401369Sdduvall  * Utility routine to carve a slice off a chunk of allocated memory,
14411369Sdduvall  * updating the chunk descriptor accordingly.  The size of the slice
14421369Sdduvall  * is given by the product of the <qty> and <size> parameters.
14431369Sdduvall  */
14441369Sdduvall static void
14451369Sdduvall bge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
14461369Sdduvall 	uint32_t qty, uint32_t size)
14471369Sdduvall {
14481369Sdduvall 	static uint32_t sequence = 0xbcd5704a;
14491369Sdduvall 	size_t totsize;
14501369Sdduvall 
14511369Sdduvall 	totsize = qty*size;
14521369Sdduvall 	ASSERT(size >= 0);
14531369Sdduvall 	ASSERT(totsize <= chunk->alength);
14541369Sdduvall 
14551369Sdduvall 	*slice = *chunk;
14561369Sdduvall 	slice->nslots = qty;
14571369Sdduvall 	slice->size = size;
14581369Sdduvall 	slice->alength = totsize;
14591369Sdduvall 	slice->token = ++sequence;
14601369Sdduvall 
14611369Sdduvall 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
14621369Sdduvall 	chunk->alength -= totsize;
14631369Sdduvall 	chunk->offset += totsize;
14641369Sdduvall 	chunk->cookie.dmac_laddress += totsize;
14651369Sdduvall 	chunk->cookie.dmac_size -= totsize;
14661369Sdduvall }
14671369Sdduvall 
14681369Sdduvall /*
14691369Sdduvall  * Initialise the specified Receive Producer (Buffer) Ring, using
14701369Sdduvall  * the information in the <dma_area> descriptors that it contains
14711369Sdduvall  * to set up all the other fields. This routine should be called
14721369Sdduvall  * only once for each ring.
14731369Sdduvall  */
14741369Sdduvall static void
14751369Sdduvall bge_init_buff_ring(bge_t *bgep, uint64_t ring)
14761369Sdduvall {
14771369Sdduvall 	buff_ring_t *brp;
14781369Sdduvall 	bge_status_t *bsp;
14791369Sdduvall 	sw_rbd_t *srbdp;
14801369Sdduvall 	dma_area_t pbuf;
14811369Sdduvall 	uint32_t bufsize;
14821369Sdduvall 	uint32_t nslots;
14831369Sdduvall 	uint32_t slot;
14841369Sdduvall 	uint32_t split;
14851369Sdduvall 
14861369Sdduvall 	static bge_regno_t nic_ring_addrs[BGE_BUFF_RINGS_MAX] = {
14871369Sdduvall 		NIC_MEM_SHADOW_BUFF_STD,
14881369Sdduvall 		NIC_MEM_SHADOW_BUFF_JUMBO,
14891369Sdduvall 		NIC_MEM_SHADOW_BUFF_MINI
14901369Sdduvall 	};
14911369Sdduvall 	static bge_regno_t mailbox_regs[BGE_BUFF_RINGS_MAX] = {
14921369Sdduvall 		RECV_STD_PROD_INDEX_REG,
14931369Sdduvall 		RECV_JUMBO_PROD_INDEX_REG,
14941369Sdduvall 		RECV_MINI_PROD_INDEX_REG
14951369Sdduvall 	};
14961369Sdduvall 	static bge_regno_t buff_cons_xref[BGE_BUFF_RINGS_MAX] = {
14971369Sdduvall 		STATUS_STD_BUFF_CONS_INDEX,
14981369Sdduvall 		STATUS_JUMBO_BUFF_CONS_INDEX,
14991369Sdduvall 		STATUS_MINI_BUFF_CONS_INDEX
15001369Sdduvall 	};
15011369Sdduvall 
15021369Sdduvall 	BGE_TRACE(("bge_init_buff_ring($%p, %d)",
15034588Sml149210 	    (void *)bgep, ring));
15041369Sdduvall 
15051369Sdduvall 	brp = &bgep->buff[ring];
15061369Sdduvall 	nslots = brp->desc.nslots;
15071369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
15081369Sdduvall 	bufsize = brp->buf[0].size;
15091369Sdduvall 
15101369Sdduvall 	/*
15111369Sdduvall 	 * Set up the copy of the h/w RCB
15121369Sdduvall 	 *
15131369Sdduvall 	 * Note: unlike Send & Receive Return Rings, (where the max_len
15141369Sdduvall 	 * field holds the number of slots), in a Receive Buffer Ring
15151369Sdduvall 	 * this field indicates the size of each buffer in the ring.
15161369Sdduvall 	 */
15171369Sdduvall 	brp->hw_rcb.host_ring_addr = brp->desc.cookie.dmac_laddress;
15181369Sdduvall 	brp->hw_rcb.max_len = bufsize;
15191369Sdduvall 	brp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
15201369Sdduvall 	brp->hw_rcb.nic_ring_addr = nic_ring_addrs[ring];
15211369Sdduvall 
15221369Sdduvall 	/*
15231369Sdduvall 	 * Other one-off initialisation of per-ring data
15241369Sdduvall 	 */
15251369Sdduvall 	brp->bgep = bgep;
15261369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
15271369Sdduvall 	brp->cons_index_p = &bsp->buff_cons_index[buff_cons_xref[ring]];
15281369Sdduvall 	brp->chip_mbx_reg = mailbox_regs[ring];
15291369Sdduvall 	mutex_init(brp->rf_lock, NULL, MUTEX_DRIVER,
15301369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
15311369Sdduvall 
15321369Sdduvall 	/*
15331369Sdduvall 	 * Allocate the array of s/w Receive Buffer Descriptors
15341369Sdduvall 	 */
15351369Sdduvall 	srbdp = kmem_zalloc(nslots*sizeof (*srbdp), KM_SLEEP);
15361369Sdduvall 	brp->sw_rbds = srbdp;
15371369Sdduvall 
15381369Sdduvall 	/*
15391369Sdduvall 	 * Now initialise each array element once and for all
15401369Sdduvall 	 */
15411369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
15421369Sdduvall 		pbuf = brp->buf[split];
15431369Sdduvall 		for (slot = 0; slot < nslots/BGE_SPLIT; ++srbdp, ++slot)
15441369Sdduvall 			bge_slice_chunk(&srbdp->pbuf, &pbuf, 1, bufsize);
15451369Sdduvall 		ASSERT(pbuf.alength == 0);
15461369Sdduvall 	}
15471369Sdduvall }
15481369Sdduvall 
15491369Sdduvall /*
15501369Sdduvall  * Clean up initialisation done above before the memory is freed
15511369Sdduvall  */
15521369Sdduvall static void
15531369Sdduvall bge_fini_buff_ring(bge_t *bgep, uint64_t ring)
15541369Sdduvall {
15551369Sdduvall 	buff_ring_t *brp;
15561369Sdduvall 	sw_rbd_t *srbdp;
15571369Sdduvall 
15581369Sdduvall 	BGE_TRACE(("bge_fini_buff_ring($%p, %d)",
15594588Sml149210 	    (void *)bgep, ring));
15601369Sdduvall 
15611369Sdduvall 	brp = &bgep->buff[ring];
15621369Sdduvall 	srbdp = brp->sw_rbds;
15631369Sdduvall 	kmem_free(srbdp, brp->desc.nslots*sizeof (*srbdp));
15641369Sdduvall 
15651369Sdduvall 	mutex_destroy(brp->rf_lock);
15661369Sdduvall }
15671369Sdduvall 
15681369Sdduvall /*
15691369Sdduvall  * Initialise the specified Receive (Return) Ring, using the
15701369Sdduvall  * information in the <dma_area> descriptors that it contains
15711369Sdduvall  * to set up all the other fields. This routine should be called
15721369Sdduvall  * only once for each ring.
15731369Sdduvall  */
15741369Sdduvall static void
15751369Sdduvall bge_init_recv_ring(bge_t *bgep, uint64_t ring)
15761369Sdduvall {
15771369Sdduvall 	recv_ring_t *rrp;
15781369Sdduvall 	bge_status_t *bsp;
15791369Sdduvall 	uint32_t nslots;
15801369Sdduvall 
15811369Sdduvall 	BGE_TRACE(("bge_init_recv_ring($%p, %d)",
15824588Sml149210 	    (void *)bgep, ring));
15831369Sdduvall 
15841369Sdduvall 	/*
15851369Sdduvall 	 * The chip architecture requires that receive return rings have
15861369Sdduvall 	 * 512 or 1024 or 2048 elements per ring.  See 570X-PG108-R page 103.
15871369Sdduvall 	 */
15881369Sdduvall 	rrp = &bgep->recv[ring];
15891369Sdduvall 	nslots = rrp->desc.nslots;
15901369Sdduvall 	ASSERT(nslots == 0 || nslots == 512 ||
15914588Sml149210 	    nslots == 1024 || nslots == 2048);
15921369Sdduvall 
15931369Sdduvall 	/*
15941369Sdduvall 	 * Set up the copy of the h/w RCB
15951369Sdduvall 	 */
15961369Sdduvall 	rrp->hw_rcb.host_ring_addr = rrp->desc.cookie.dmac_laddress;
15971369Sdduvall 	rrp->hw_rcb.max_len = nslots;
15981369Sdduvall 	rrp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
15991369Sdduvall 	rrp->hw_rcb.nic_ring_addr = 0;
16001369Sdduvall 
16011369Sdduvall 	/*
16021369Sdduvall 	 * Other one-off initialisation of per-ring data
16031369Sdduvall 	 */
16041369Sdduvall 	rrp->bgep = bgep;
16051369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
16061369Sdduvall 	rrp->prod_index_p = RECV_INDEX_P(bsp, ring);
16071369Sdduvall 	rrp->chip_mbx_reg = RECV_RING_CONS_INDEX_REG(ring);
16081369Sdduvall 	mutex_init(rrp->rx_lock, NULL, MUTEX_DRIVER,
16091369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
16101369Sdduvall }
16111369Sdduvall 
16121369Sdduvall 
16131369Sdduvall /*
16141369Sdduvall  * Clean up initialisation done above before the memory is freed
16151369Sdduvall  */
16161369Sdduvall static void
16171369Sdduvall bge_fini_recv_ring(bge_t *bgep, uint64_t ring)
16181369Sdduvall {
16191369Sdduvall 	recv_ring_t *rrp;
16201369Sdduvall 
16211369Sdduvall 	BGE_TRACE(("bge_fini_recv_ring($%p, %d)",
16224588Sml149210 	    (void *)bgep, ring));
16231369Sdduvall 
16241369Sdduvall 	rrp = &bgep->recv[ring];
16251369Sdduvall 	if (rrp->rx_softint)
16261369Sdduvall 		ddi_remove_softintr(rrp->rx_softint);
16271369Sdduvall 	mutex_destroy(rrp->rx_lock);
16281369Sdduvall }
16291369Sdduvall 
16301369Sdduvall /*
16311369Sdduvall  * Initialise the specified Send Ring, using the information in the
16321369Sdduvall  * <dma_area> descriptors that it contains to set up all the other
16331369Sdduvall  * fields. This routine should be called only once for each ring.
16341369Sdduvall  */
16351369Sdduvall static void
16361369Sdduvall bge_init_send_ring(bge_t *bgep, uint64_t ring)
16371369Sdduvall {
16381369Sdduvall 	send_ring_t *srp;
16391369Sdduvall 	bge_status_t *bsp;
16401369Sdduvall 	sw_sbd_t *ssbdp;
16411369Sdduvall 	dma_area_t desc;
16421369Sdduvall 	dma_area_t pbuf;
16431369Sdduvall 	uint32_t nslots;
16441369Sdduvall 	uint32_t slot;
16451369Sdduvall 	uint32_t split;
16463334Sgs150176 	sw_txbuf_t *txbuf;
16471369Sdduvall 
16481369Sdduvall 	BGE_TRACE(("bge_init_send_ring($%p, %d)",
16494588Sml149210 	    (void *)bgep, ring));
16501369Sdduvall 
16511369Sdduvall 	/*
16521369Sdduvall 	 * The chip architecture requires that host-based send rings
16531369Sdduvall 	 * have 512 elements per ring.  See 570X-PG102-R page 56.
16541369Sdduvall 	 */
16551369Sdduvall 	srp = &bgep->send[ring];
16561369Sdduvall 	nslots = srp->desc.nslots;
16571369Sdduvall 	ASSERT(nslots == 0 || nslots == 512);
16581369Sdduvall 
16591369Sdduvall 	/*
16601369Sdduvall 	 * Set up the copy of the h/w RCB
16611369Sdduvall 	 */
16621369Sdduvall 	srp->hw_rcb.host_ring_addr = srp->desc.cookie.dmac_laddress;
16631369Sdduvall 	srp->hw_rcb.max_len = nslots;
16641369Sdduvall 	srp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
16651369Sdduvall 	srp->hw_rcb.nic_ring_addr = NIC_MEM_SHADOW_SEND_RING(ring, nslots);
16661369Sdduvall 
16671369Sdduvall 	/*
16681369Sdduvall 	 * Other one-off initialisation of per-ring data
16691369Sdduvall 	 */
16701369Sdduvall 	srp->bgep = bgep;
16711369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
16721369Sdduvall 	srp->cons_index_p = SEND_INDEX_P(bsp, ring);
16731369Sdduvall 	srp->chip_mbx_reg = SEND_RING_HOST_INDEX_REG(ring);
16741369Sdduvall 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
16751369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
16763334Sgs150176 	mutex_init(srp->txbuf_lock, NULL, MUTEX_DRIVER,
16773334Sgs150176 	    DDI_INTR_PRI(bgep->intr_pri));
16783334Sgs150176 	mutex_init(srp->freetxbuf_lock, NULL, MUTEX_DRIVER,
16793334Sgs150176 	    DDI_INTR_PRI(bgep->intr_pri));
16801369Sdduvall 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
16811369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
16823334Sgs150176 	if (nslots == 0)
16833334Sgs150176 		return;
16841369Sdduvall 
16851369Sdduvall 	/*
16861369Sdduvall 	 * Allocate the array of s/w Send Buffer Descriptors
16871369Sdduvall 	 */
16881369Sdduvall 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
16893334Sgs150176 	txbuf = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (*txbuf), KM_SLEEP);
16903334Sgs150176 	srp->txbuf_head =
16913334Sgs150176 	    kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (bge_queue_item_t), KM_SLEEP);
16923334Sgs150176 	srp->pktp = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (send_pkt_t), KM_SLEEP);
16931369Sdduvall 	srp->sw_sbds = ssbdp;
16943334Sgs150176 	srp->txbuf = txbuf;
16953334Sgs150176 	srp->tx_buffers = BGE_SEND_BUF_NUM;
16963334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
16973334Sgs150176 	if (bgep->chipid.snd_buff_size > BGE_SEND_BUFF_SIZE_DEFAULT)
16983334Sgs150176 		srp->tx_array_max = BGE_SEND_BUF_ARRAY_JUMBO;
16993334Sgs150176 	else
17003334Sgs150176 		srp->tx_array_max = BGE_SEND_BUF_ARRAY;
17013334Sgs150176 	srp->tx_array = 1;
17021369Sdduvall 
17031369Sdduvall 	/*
17043334Sgs150176 	 * Chunk tx desc area
17051369Sdduvall 	 */
17061369Sdduvall 	desc = srp->desc;
17073334Sgs150176 	for (slot = 0; slot < nslots; ++ssbdp, ++slot) {
17083334Sgs150176 		bge_slice_chunk(&ssbdp->desc, &desc, 1,
17093334Sgs150176 		    sizeof (bge_sbd_t));
17103334Sgs150176 	}
17113334Sgs150176 	ASSERT(desc.alength == 0);
17123334Sgs150176 
17133334Sgs150176 	/*
17143334Sgs150176 	 * Chunk tx buffer area
17153334Sgs150176 	 */
17161369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
17173334Sgs150176 		pbuf = srp->buf[0][split];
17183334Sgs150176 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
17193334Sgs150176 			bge_slice_chunk(&txbuf->buf, &pbuf, 1,
17203334Sgs150176 			    bgep->chipid.snd_buff_size);
17213334Sgs150176 			txbuf++;
17221369Sdduvall 		}
17231369Sdduvall 		ASSERT(pbuf.alength == 0);
17241369Sdduvall 	}
17251369Sdduvall }
17261369Sdduvall 
17271369Sdduvall /*
17281369Sdduvall  * Clean up initialisation done above before the memory is freed
17291369Sdduvall  */
17301369Sdduvall static void
17311369Sdduvall bge_fini_send_ring(bge_t *bgep, uint64_t ring)
17321369Sdduvall {
17331369Sdduvall 	send_ring_t *srp;
17343334Sgs150176 	uint32_t array;
17353334Sgs150176 	uint32_t split;
17363334Sgs150176 	uint32_t nslots;
17371369Sdduvall 
17381369Sdduvall 	BGE_TRACE(("bge_fini_send_ring($%p, %d)",
17394588Sml149210 	    (void *)bgep, ring));
17401369Sdduvall 
17411369Sdduvall 	srp = &bgep->send[ring];
17423334Sgs150176 	mutex_destroy(srp->tc_lock);
17433334Sgs150176 	mutex_destroy(srp->freetxbuf_lock);
17443334Sgs150176 	mutex_destroy(srp->txbuf_lock);
17451369Sdduvall 	mutex_destroy(srp->tx_lock);
17463334Sgs150176 	nslots = srp->desc.nslots;
17473334Sgs150176 	if (nslots == 0)
17483334Sgs150176 		return;
17493334Sgs150176 
17503334Sgs150176 	for (array = 1; array < srp->tx_array; ++array)
17513334Sgs150176 		for (split = 0; split < BGE_SPLIT; ++split)
17523334Sgs150176 			bge_free_dma_mem(&srp->buf[array][split]);
17533334Sgs150176 	kmem_free(srp->sw_sbds, nslots*sizeof (*srp->sw_sbds));
17543334Sgs150176 	kmem_free(srp->txbuf_head, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf_head));
17553334Sgs150176 	kmem_free(srp->txbuf, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf));
17563334Sgs150176 	kmem_free(srp->pktp, BGE_SEND_BUF_MAX*sizeof (*srp->pktp));
17573334Sgs150176 	srp->sw_sbds = NULL;
17583334Sgs150176 	srp->txbuf_head = NULL;
17593334Sgs150176 	srp->txbuf = NULL;
17603334Sgs150176 	srp->pktp = NULL;
17611369Sdduvall }
17621369Sdduvall 
17631369Sdduvall /*
17641369Sdduvall  * Initialise all transmit, receive, and buffer rings.
17651369Sdduvall  */
17661865Sdilpreet void
17671369Sdduvall bge_init_rings(bge_t *bgep)
17681369Sdduvall {
17693334Sgs150176 	uint32_t ring;
17701369Sdduvall 
17711369Sdduvall 	BGE_TRACE(("bge_init_rings($%p)", (void *)bgep));
17721369Sdduvall 
17731369Sdduvall 	/*
17741369Sdduvall 	 * Perform one-off initialisation of each ring ...
17751369Sdduvall 	 */
17761369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
17771369Sdduvall 		bge_init_send_ring(bgep, ring);
17781369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
17791369Sdduvall 		bge_init_recv_ring(bgep, ring);
17801369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
17811369Sdduvall 		bge_init_buff_ring(bgep, ring);
17821369Sdduvall }
17831369Sdduvall 
17841369Sdduvall /*
17851369Sdduvall  * Undo the work of bge_init_rings() above before the memory is freed
17861369Sdduvall  */
17871865Sdilpreet void
17881369Sdduvall bge_fini_rings(bge_t *bgep)
17891369Sdduvall {
17903334Sgs150176 	uint32_t ring;
17911369Sdduvall 
17921369Sdduvall 	BGE_TRACE(("bge_fini_rings($%p)", (void *)bgep));
17931369Sdduvall 
17941369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
17951369Sdduvall 		bge_fini_buff_ring(bgep, ring);
17961369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
17971369Sdduvall 		bge_fini_recv_ring(bgep, ring);
17981369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
17991369Sdduvall 		bge_fini_send_ring(bgep, ring);
18001369Sdduvall }
18011369Sdduvall 
18021369Sdduvall /*
18033334Sgs150176  * Called from the bge_m_stop() to free the tx buffers which are
18043334Sgs150176  * allocated from the tx process.
18051369Sdduvall  */
18063334Sgs150176 void
18073334Sgs150176 bge_free_txbuf_arrays(send_ring_t *srp)
18081369Sdduvall {
18093334Sgs150176 	uint32_t array;
18103334Sgs150176 	uint32_t split;
18113334Sgs150176 
18123334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
18131369Sdduvall 
18141369Sdduvall 	/*
18153334Sgs150176 	 * Free the extra tx buffer DMA area
18161369Sdduvall 	 */
18173334Sgs150176 	for (array = 1; array < srp->tx_array; ++array)
18183334Sgs150176 		for (split = 0; split < BGE_SPLIT; ++split)
18193334Sgs150176 			bge_free_dma_mem(&srp->buf[array][split]);
18201369Sdduvall 
18211369Sdduvall 	/*
18223334Sgs150176 	 * Restore initial tx buffer numbers
18231369Sdduvall 	 */
18243334Sgs150176 	srp->tx_array = 1;
18253334Sgs150176 	srp->tx_buffers = BGE_SEND_BUF_NUM;
18263334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
18273334Sgs150176 	srp->tx_flow = 0;
18283334Sgs150176 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
18291369Sdduvall }
18301369Sdduvall 
18311369Sdduvall /*
18323334Sgs150176  * Called from tx process to allocate more tx buffers
18331369Sdduvall  */
18343334Sgs150176 bge_queue_item_t *
18353334Sgs150176 bge_alloc_txbuf_array(bge_t *bgep, send_ring_t *srp)
18361369Sdduvall {
18373334Sgs150176 	bge_queue_t *txbuf_queue;
18383334Sgs150176 	bge_queue_item_t *txbuf_item_last;
18393334Sgs150176 	bge_queue_item_t *txbuf_item;
18403334Sgs150176 	bge_queue_item_t *txbuf_item_rtn;
18413334Sgs150176 	sw_txbuf_t *txbuf;
18423334Sgs150176 	dma_area_t area;
18433334Sgs150176 	size_t txbuffsize;
18443334Sgs150176 	uint32_t slot;
18453334Sgs150176 	uint32_t array;
18463334Sgs150176 	uint32_t split;
18473334Sgs150176 	uint32_t err;
18483334Sgs150176 
18493334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
18503334Sgs150176 
18513334Sgs150176 	array = srp->tx_array;
18523334Sgs150176 	if (array >= srp->tx_array_max)
18533334Sgs150176 		return (NULL);
18543334Sgs150176 
18553334Sgs150176 	/*
18563334Sgs150176 	 * Allocate memory & handles for TX buffers
18573334Sgs150176 	 */
18583334Sgs150176 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
18593334Sgs150176 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
18603334Sgs150176 	for (split = 0; split < BGE_SPLIT; ++split) {
18613334Sgs150176 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
18624588Sml149210 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
18634588Sml149210 		    &srp->buf[array][split]);
18643334Sgs150176 		if (err != DDI_SUCCESS) {
18653334Sgs150176 			/* Free the last already allocated OK chunks */
18663334Sgs150176 			for (slot = 0; slot <= split; ++slot)
18673334Sgs150176 				bge_free_dma_mem(&srp->buf[array][slot]);
18683334Sgs150176 			srp->tx_alloc_fail++;
18693334Sgs150176 			return (NULL);
18701369Sdduvall 		}
18713334Sgs150176 	}
18723334Sgs150176 
18733334Sgs150176 	/*
18743334Sgs150176 	 * Chunk tx buffer area
18753334Sgs150176 	 */
18763334Sgs150176 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
18773334Sgs150176 	for (split = 0; split < BGE_SPLIT; ++split) {
18783334Sgs150176 		area = srp->buf[array][split];
18793334Sgs150176 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
18803334Sgs150176 			bge_slice_chunk(&txbuf->buf, &area, 1,
18813334Sgs150176 			    bgep->chipid.snd_buff_size);
18823334Sgs150176 			txbuf++;
18833334Sgs150176 		}
18841369Sdduvall 	}
18851369Sdduvall 
18863334Sgs150176 	/*
18873334Sgs150176 	 * Add above buffers to the tx buffer pop queue
18883334Sgs150176 	 */
18893334Sgs150176 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
18903334Sgs150176 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
18913334Sgs150176 	txbuf_item_last = NULL;
18923334Sgs150176 	for (slot = 0; slot < BGE_SEND_BUF_NUM; ++slot) {
18933334Sgs150176 		txbuf_item->item = txbuf;
18943334Sgs150176 		txbuf_item->next = txbuf_item_last;
18953334Sgs150176 		txbuf_item_last = txbuf_item;
18963334Sgs150176 		txbuf++;
18973334Sgs150176 		txbuf_item++;
18981369Sdduvall 	}
18993334Sgs150176 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
19003334Sgs150176 	txbuf_item_rtn = txbuf_item;
19013334Sgs150176 	txbuf_item++;
19023334Sgs150176 	txbuf_queue = srp->txbuf_pop_queue;
19033334Sgs150176 	mutex_enter(txbuf_queue->lock);
19043334Sgs150176 	txbuf_item->next = txbuf_queue->head;
19053334Sgs150176 	txbuf_queue->head = txbuf_item_last;
19063334Sgs150176 	txbuf_queue->count += BGE_SEND_BUF_NUM - 1;
19073334Sgs150176 	mutex_exit(txbuf_queue->lock);
19083334Sgs150176 
19093334Sgs150176 	srp->tx_array++;
19103334Sgs150176 	srp->tx_buffers += BGE_SEND_BUF_NUM;
19113334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
19123334Sgs150176 
19133334Sgs150176 	return (txbuf_item_rtn);
19141369Sdduvall }
19151369Sdduvall 
19161369Sdduvall /*
19171369Sdduvall  * This function allocates all the transmit and receive buffers
19183334Sgs150176  * and descriptors, in four chunks.
19191369Sdduvall  */
19201865Sdilpreet int
19211369Sdduvall bge_alloc_bufs(bge_t *bgep)
19221369Sdduvall {
19231369Sdduvall 	dma_area_t area;
19241369Sdduvall 	size_t rxbuffsize;
19251369Sdduvall 	size_t txbuffsize;
19261369Sdduvall 	size_t rxbuffdescsize;
19271369Sdduvall 	size_t rxdescsize;
19281369Sdduvall 	size_t txdescsize;
19293334Sgs150176 	uint32_t ring;
19303334Sgs150176 	uint32_t rx_rings = bgep->chipid.rx_rings;
19313334Sgs150176 	uint32_t tx_rings = bgep->chipid.tx_rings;
19321369Sdduvall 	int split;
19331369Sdduvall 	int err;
19341369Sdduvall 
19351369Sdduvall 	BGE_TRACE(("bge_alloc_bufs($%p)",
19364588Sml149210 	    (void *)bgep));
19371369Sdduvall 
19381908Sly149593 	rxbuffsize = BGE_STD_SLOTS_USED*bgep->chipid.std_buf_size;
19391369Sdduvall 	rxbuffsize += bgep->chipid.jumbo_slots*bgep->chipid.recv_jumbo_size;
19401369Sdduvall 	rxbuffsize += BGE_MINI_SLOTS_USED*BGE_MINI_BUFF_SIZE;
19411369Sdduvall 
19423334Sgs150176 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
19431369Sdduvall 	txbuffsize *= tx_rings;
19441369Sdduvall 
19451369Sdduvall 	rxdescsize = rx_rings*bgep->chipid.recv_slots;
19461369Sdduvall 	rxdescsize *= sizeof (bge_rbd_t);
19471369Sdduvall 
19481369Sdduvall 	rxbuffdescsize = BGE_STD_SLOTS_USED;
19491369Sdduvall 	rxbuffdescsize += bgep->chipid.jumbo_slots;
19501369Sdduvall 	rxbuffdescsize += BGE_MINI_SLOTS_USED;
19511369Sdduvall 	rxbuffdescsize *= sizeof (bge_rbd_t);
19521369Sdduvall 
19531369Sdduvall 	txdescsize = tx_rings*BGE_SEND_SLOTS_USED;
19541369Sdduvall 	txdescsize *= sizeof (bge_sbd_t);
19551369Sdduvall 	txdescsize += sizeof (bge_statistics_t);
19561369Sdduvall 	txdescsize += sizeof (bge_status_t);
19571369Sdduvall 	txdescsize += BGE_STATUS_PADDING;
19581369Sdduvall 
19591369Sdduvall 	/*
19603907Szh199473 	 * Enable PCI relaxed ordering only for RX/TX data buffers
19613907Szh199473 	 */
19623907Szh199473 	if (bge_relaxed_ordering)
19633907Szh199473 		dma_attr.dma_attr_flags |= DDI_DMA_RELAXED_ORDERING;
19643907Szh199473 
19653907Szh199473 	/*
19661369Sdduvall 	 * Allocate memory & handles for RX buffers
19671369Sdduvall 	 */
19681369Sdduvall 	ASSERT((rxbuffsize % BGE_SPLIT) == 0);
19691369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
19701369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxbuffsize/BGE_SPLIT,
19714588Sml149210 		    &bge_data_accattr, DDI_DMA_READ | BGE_DMA_MODE,
19724588Sml149210 		    &bgep->rx_buff[split]);
19731369Sdduvall 		if (err != DDI_SUCCESS)
19741369Sdduvall 			return (DDI_FAILURE);
19751369Sdduvall 	}
19761369Sdduvall 
19771369Sdduvall 	/*
19781369Sdduvall 	 * Allocate memory & handles for TX buffers
19791369Sdduvall 	 */
19801369Sdduvall 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
19811369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
19821369Sdduvall 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
19834588Sml149210 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
19844588Sml149210 		    &bgep->tx_buff[split]);
19851369Sdduvall 		if (err != DDI_SUCCESS)
19861369Sdduvall 			return (DDI_FAILURE);
19871369Sdduvall 	}
19881369Sdduvall 
19893907Szh199473 	dma_attr.dma_attr_flags &= ~DDI_DMA_RELAXED_ORDERING;
19903907Szh199473 
19911369Sdduvall 	/*
19921369Sdduvall 	 * Allocate memory & handles for receive return rings
19931369Sdduvall 	 */
19941369Sdduvall 	ASSERT((rxdescsize % rx_rings) == 0);
19951369Sdduvall 	for (split = 0; split < rx_rings; ++split) {
19961369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxdescsize/rx_rings,
19974588Sml149210 		    &bge_desc_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
19984588Sml149210 		    &bgep->rx_desc[split]);
19991369Sdduvall 		if (err != DDI_SUCCESS)
20001369Sdduvall 			return (DDI_FAILURE);
20011369Sdduvall 	}
20021369Sdduvall 
20031369Sdduvall 	/*
20041369Sdduvall 	 * Allocate memory & handles for buffer (producer) descriptor rings
20051369Sdduvall 	 */
20061369Sdduvall 	err = bge_alloc_dma_mem(bgep, rxbuffdescsize, &bge_desc_accattr,
20074588Sml149210 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->rx_desc[split]);
20081369Sdduvall 	if (err != DDI_SUCCESS)
20091369Sdduvall 		return (DDI_FAILURE);
20101369Sdduvall 
20111369Sdduvall 	/*
20121369Sdduvall 	 * Allocate memory & handles for TX descriptor rings,
20131369Sdduvall 	 * status block, and statistics area
20141369Sdduvall 	 */
20151369Sdduvall 	err = bge_alloc_dma_mem(bgep, txdescsize, &bge_desc_accattr,
20164588Sml149210 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->tx_desc);
20171369Sdduvall 	if (err != DDI_SUCCESS)
20181369Sdduvall 		return (DDI_FAILURE);
20191369Sdduvall 
20201369Sdduvall 	/*
20211369Sdduvall 	 * Now carve up each of the allocated areas ...
20221369Sdduvall 	 */
20231369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
20241369Sdduvall 		area = bgep->rx_buff[split];
20251369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].buf[split],
20264588Sml149210 		    &area, BGE_STD_SLOTS_USED/BGE_SPLIT,
20274588Sml149210 		    bgep->chipid.std_buf_size);
20281369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].buf[split],
20294588Sml149210 		    &area, bgep->chipid.jumbo_slots/BGE_SPLIT,
20304588Sml149210 		    bgep->chipid.recv_jumbo_size);
20311369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].buf[split],
20324588Sml149210 		    &area, BGE_MINI_SLOTS_USED/BGE_SPLIT,
20334588Sml149210 		    BGE_MINI_BUFF_SIZE);
20341369Sdduvall 		ASSERT(area.alength >= 0);
20351369Sdduvall 	}
20361369Sdduvall 
20371369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
20381369Sdduvall 		area = bgep->tx_buff[split];
20391369Sdduvall 		for (ring = 0; ring < tx_rings; ++ring)
20403334Sgs150176 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
20414588Sml149210 			    &area, BGE_SEND_BUF_NUM/BGE_SPLIT,
20424588Sml149210 			    bgep->chipid.snd_buff_size);
20431369Sdduvall 		for (; ring < BGE_SEND_RINGS_MAX; ++ring)
20443334Sgs150176 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
20454588Sml149210 			    &area, 0, bgep->chipid.snd_buff_size);
20461369Sdduvall 		ASSERT(area.alength >= 0);
20471369Sdduvall 	}
20481369Sdduvall 
20491369Sdduvall 	for (ring = 0; ring < rx_rings; ++ring)
20501369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &bgep->rx_desc[ring],
20514588Sml149210 		    bgep->chipid.recv_slots, sizeof (bge_rbd_t));
20521369Sdduvall 
20531369Sdduvall 	area = bgep->rx_desc[rx_rings];
20541369Sdduvall 	for (; ring < BGE_RECV_RINGS_MAX; ++ring)
20551369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &area,
20564588Sml149210 		    0, sizeof (bge_rbd_t));
20571369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].desc, &area,
20584588Sml149210 	    BGE_STD_SLOTS_USED, sizeof (bge_rbd_t));
20591369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].desc, &area,
20604588Sml149210 	    bgep->chipid.jumbo_slots, sizeof (bge_rbd_t));
20611369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].desc, &area,
20624588Sml149210 	    BGE_MINI_SLOTS_USED, sizeof (bge_rbd_t));
20631369Sdduvall 	ASSERT(area.alength == 0);
20641369Sdduvall 
20651369Sdduvall 	area = bgep->tx_desc;
20661369Sdduvall 	for (ring = 0; ring < tx_rings; ++ring)
20671369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
20684588Sml149210 		    BGE_SEND_SLOTS_USED, sizeof (bge_sbd_t));
20691369Sdduvall 	for (; ring < BGE_SEND_RINGS_MAX; ++ring)
20701369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
20714588Sml149210 		    0, sizeof (bge_sbd_t));
20721369Sdduvall 	bge_slice_chunk(&bgep->statistics, &area, 1, sizeof (bge_statistics_t));
20731369Sdduvall 	bge_slice_chunk(&bgep->status_block, &area, 1, sizeof (bge_status_t));
20741369Sdduvall 	ASSERT(area.alength == BGE_STATUS_PADDING);
20751369Sdduvall 	DMA_ZERO(bgep->status_block);
20761369Sdduvall 
20771369Sdduvall 	return (DDI_SUCCESS);
20781369Sdduvall }
20791369Sdduvall 
20801369Sdduvall /*
20811369Sdduvall  * This routine frees the transmit and receive buffers and descriptors.
20821369Sdduvall  * Make sure the chip is stopped before calling it!
20831369Sdduvall  */
20841865Sdilpreet void
20851369Sdduvall bge_free_bufs(bge_t *bgep)
20861369Sdduvall {
20871369Sdduvall 	int split;
20881369Sdduvall 
20891369Sdduvall 	BGE_TRACE(("bge_free_bufs($%p)",
20904588Sml149210 	    (void *)bgep));
20911369Sdduvall 
20921369Sdduvall 	bge_free_dma_mem(&bgep->tx_desc);
20931369Sdduvall 	for (split = 0; split < BGE_RECV_RINGS_SPLIT; ++split)
20941369Sdduvall 		bge_free_dma_mem(&bgep->rx_desc[split]);
20951369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
20961369Sdduvall 		bge_free_dma_mem(&bgep->tx_buff[split]);
20971369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
20981369Sdduvall 		bge_free_dma_mem(&bgep->rx_buff[split]);
20991369Sdduvall }
21001369Sdduvall 
21011369Sdduvall /*
21021369Sdduvall  * Determine (initial) MAC address ("BIA") to use for this interface
21031369Sdduvall  */
21041369Sdduvall 
21051369Sdduvall static void
21061369Sdduvall bge_find_mac_address(bge_t *bgep, chip_id_t *cidp)
21071369Sdduvall {
21081369Sdduvall 	struct ether_addr sysaddr;
21091369Sdduvall 	char propbuf[8];		/* "true" or "false", plus NUL	*/
21101369Sdduvall 	uchar_t *bytes;
21111369Sdduvall 	int *ints;
21121369Sdduvall 	uint_t nelts;
21131369Sdduvall 	int err;
21141369Sdduvall 
21151369Sdduvall 	BGE_TRACE(("bge_find_mac_address($%p)",
21164588Sml149210 	    (void *)bgep));
21171369Sdduvall 
21181369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: hw_mac_addr %012llx, => %s (%sset)",
21194588Sml149210 	    cidp->hw_mac_addr,
21204588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
21214588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
21221369Sdduvall 
21231369Sdduvall 	/*
21241369Sdduvall 	 * The "vendor's factory-set address" may already have
21251369Sdduvall 	 * been extracted from the chip, but if the property
21261369Sdduvall 	 * "local-mac-address" is set we use that instead.  It
21271369Sdduvall 	 * will normally be set by OBP, but it could also be
21281369Sdduvall 	 * specified in a .conf file(!)
21291369Sdduvall 	 *
21301369Sdduvall 	 * There doesn't seem to be a way to define byte-array
21311369Sdduvall 	 * properties in a .conf, so we check whether it looks
21321369Sdduvall 	 * like an array of 6 ints instead.
21331369Sdduvall 	 *
21341369Sdduvall 	 * Then, we check whether it looks like an array of 6
21351369Sdduvall 	 * bytes (which it should, if OBP set it).  If we can't
21361369Sdduvall 	 * make sense of it either way, we'll ignore it.
21371369Sdduvall 	 */
21381369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
21394588Sml149210 	    DDI_PROP_DONTPASS, localmac_propname, &ints, &nelts);
21401369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
21411369Sdduvall 		if (nelts == ETHERADDRL) {
21421369Sdduvall 			while (nelts--)
21431369Sdduvall 				cidp->vendor_addr.addr[nelts] = ints[nelts];
21442331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
21451369Sdduvall 		}
21461369Sdduvall 		ddi_prop_free(ints);
21471369Sdduvall 	}
21481369Sdduvall 
21491369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
21504588Sml149210 	    DDI_PROP_DONTPASS, localmac_propname, &bytes, &nelts);
21511369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
21521369Sdduvall 		if (nelts == ETHERADDRL) {
21531369Sdduvall 			while (nelts--)
21541369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
21552331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
21561369Sdduvall 		}
21571369Sdduvall 		ddi_prop_free(bytes);
21581369Sdduvall 	}
21591369Sdduvall 
21601369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +local %s (%sset)",
21614588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
21624588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
21631369Sdduvall 
21641369Sdduvall 	/*
21651369Sdduvall 	 * Look up the OBP property "local-mac-address?".  Note that even
21661369Sdduvall 	 * though its value is a string (which should be "true" or "false"),
21671369Sdduvall 	 * it can't be decoded by ddi_prop_lookup_string(9F).  So, we zero
21681369Sdduvall 	 * the buffer first and then fetch the property as an untyped array;
21691369Sdduvall 	 * this may or may not include a final NUL, but since there will
21701369Sdduvall 	 * always be one left at the end of the buffer we can now treat it
21711369Sdduvall 	 * as a string anyway.
21721369Sdduvall 	 */
21731369Sdduvall 	nelts = sizeof (propbuf);
21741369Sdduvall 	bzero(propbuf, nelts--);
21751369Sdduvall 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, bgep->devinfo,
21764588Sml149210 	    DDI_PROP_CANSLEEP, localmac_boolname, propbuf, (int *)&nelts);
21771369Sdduvall 
21781369Sdduvall 	/*
21791369Sdduvall 	 * Now, if the address still isn't set from the hardware (SEEPROM)
21801369Sdduvall 	 * or the OBP or .conf property, OR if the user has foolishly set
21811369Sdduvall 	 * 'local-mac-address? = false', use "the system address" instead
21821369Sdduvall 	 * (but only if it's non-null i.e. has been set from the IDPROM).
21831369Sdduvall 	 */
21842331Skrgopi 	if (cidp->vendor_addr.set == B_FALSE || strcmp(propbuf, "false") == 0)
21851369Sdduvall 		if (localetheraddr(NULL, &sysaddr) != 0) {
21861369Sdduvall 			ethaddr_copy(&sysaddr, cidp->vendor_addr.addr);
21872331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
21881369Sdduvall 		}
21891369Sdduvall 
21901369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +system %s (%sset)",
21914588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
21924588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
21931369Sdduvall 
21941369Sdduvall 	/*
21951369Sdduvall 	 * Finally(!), if there's a valid "mac-address" property (created
21961369Sdduvall 	 * if we netbooted from this interface), we must use this instead
21971369Sdduvall 	 * of any of the above to ensure that the NFS/install server doesn't
21981369Sdduvall 	 * get confused by the address changing as Solaris takes over!
21991369Sdduvall 	 */
22001369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
22014588Sml149210 	    DDI_PROP_DONTPASS, macaddr_propname, &bytes, &nelts);
22021369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
22031369Sdduvall 		if (nelts == ETHERADDRL) {
22041369Sdduvall 			while (nelts--)
22051369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
22062331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
22071369Sdduvall 		}
22081369Sdduvall 		ddi_prop_free(bytes);
22091369Sdduvall 	}
22101369Sdduvall 
22111369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: =final %s (%sset)",
22124588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
22134588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
22141369Sdduvall }
22151369Sdduvall 
22161865Sdilpreet 
22171865Sdilpreet /*ARGSUSED*/
22181865Sdilpreet int
22191865Sdilpreet bge_check_acc_handle(bge_t *bgep, ddi_acc_handle_t handle)
22201865Sdilpreet {
22211865Sdilpreet 	ddi_fm_error_t de;
22221865Sdilpreet 
22231865Sdilpreet 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
22241865Sdilpreet 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
22251865Sdilpreet 	return (de.fme_status);
22261865Sdilpreet }
22271865Sdilpreet 
22281865Sdilpreet /*ARGSUSED*/
22291865Sdilpreet int
22301865Sdilpreet bge_check_dma_handle(bge_t *bgep, ddi_dma_handle_t handle)
22311865Sdilpreet {
22321865Sdilpreet 	ddi_fm_error_t de;
22331865Sdilpreet 
22341865Sdilpreet 	ASSERT(bgep->progress & PROGRESS_BUFS);
22351865Sdilpreet 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
22361865Sdilpreet 	return (de.fme_status);
22371865Sdilpreet }
22381865Sdilpreet 
22391865Sdilpreet /*
22401865Sdilpreet  * The IO fault service error handling callback function
22411865Sdilpreet  */
22421865Sdilpreet /*ARGSUSED*/
22431865Sdilpreet static int
22441865Sdilpreet bge_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
22451865Sdilpreet {
22461865Sdilpreet 	/*
22471865Sdilpreet 	 * as the driver can always deal with an error in any dma or
22481865Sdilpreet 	 * access handle, we can just return the fme_status value.
22491865Sdilpreet 	 */
22501865Sdilpreet 	pci_ereport_post(dip, err, NULL);
22511865Sdilpreet 	return (err->fme_status);
22521865Sdilpreet }
22531865Sdilpreet 
22541865Sdilpreet static void
22551865Sdilpreet bge_fm_init(bge_t *bgep)
22561865Sdilpreet {
22571865Sdilpreet 	ddi_iblock_cookie_t iblk;
22581865Sdilpreet 
22591865Sdilpreet 	/* Only register with IO Fault Services if we have some capability */
22601865Sdilpreet 	if (bgep->fm_capabilities) {
22611865Sdilpreet 		bge_reg_accattr.devacc_attr_access = DDI_FLAGERR_ACC;
22621865Sdilpreet 		bge_desc_accattr.devacc_attr_access = DDI_FLAGERR_ACC;
22631865Sdilpreet 		dma_attr.dma_attr_flags = DDI_DMA_FLAGERR;
22641865Sdilpreet 
22651865Sdilpreet 		/* Register capabilities with IO Fault Services */
22661865Sdilpreet 		ddi_fm_init(bgep->devinfo, &bgep->fm_capabilities, &iblk);
22671865Sdilpreet 
22681865Sdilpreet 		/*
22691865Sdilpreet 		 * Initialize pci ereport capabilities if ereport capable
22701865Sdilpreet 		 */
22711865Sdilpreet 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
22721865Sdilpreet 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
22731865Sdilpreet 			pci_ereport_setup(bgep->devinfo);
22741865Sdilpreet 
22751865Sdilpreet 		/*
22761865Sdilpreet 		 * Register error callback if error callback capable
22771865Sdilpreet 		 */
22781865Sdilpreet 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
22791865Sdilpreet 			ddi_fm_handler_register(bgep->devinfo,
22804588Sml149210 			    bge_fm_error_cb, (void*) bgep);
22811865Sdilpreet 	} else {
22821865Sdilpreet 		/*
22831865Sdilpreet 		 * These fields have to be cleared of FMA if there are no
22841865Sdilpreet 		 * FMA capabilities at runtime.
22851865Sdilpreet 		 */
22861865Sdilpreet 		bge_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
22871865Sdilpreet 		bge_desc_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
22881865Sdilpreet 		dma_attr.dma_attr_flags = 0;
22891865Sdilpreet 	}
22901865Sdilpreet }
22911865Sdilpreet 
22921865Sdilpreet static void
22931865Sdilpreet bge_fm_fini(bge_t *bgep)
22941865Sdilpreet {
22951865Sdilpreet 	/* Only unregister FMA capabilities if we registered some */
22961865Sdilpreet 	if (bgep->fm_capabilities) {
22971865Sdilpreet 
22981865Sdilpreet 		/*
22991865Sdilpreet 		 * Release any resources allocated by pci_ereport_setup()
23001865Sdilpreet 		 */
23011865Sdilpreet 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
23021865Sdilpreet 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
23031865Sdilpreet 			pci_ereport_teardown(bgep->devinfo);
23041865Sdilpreet 
23051865Sdilpreet 		/*
23061865Sdilpreet 		 * Un-register error callback if error callback capable
23071865Sdilpreet 		 */
23081865Sdilpreet 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
23091865Sdilpreet 			ddi_fm_handler_unregister(bgep->devinfo);
23101865Sdilpreet 
23111865Sdilpreet 		/* Unregister from IO Fault Services */
23121865Sdilpreet 		ddi_fm_fini(bgep->devinfo);
23131865Sdilpreet 	}
23141865Sdilpreet }
23151865Sdilpreet 
23161369Sdduvall static void
23171408Srandyf #ifdef BGE_IPMI_ASF
23181408Srandyf bge_unattach(bge_t *bgep, uint_t asf_mode)
23191408Srandyf #else
23201369Sdduvall bge_unattach(bge_t *bgep)
23211408Srandyf #endif
23221369Sdduvall {
23231369Sdduvall 	BGE_TRACE(("bge_unattach($%p)",
23241369Sdduvall 		(void *)bgep));
23251369Sdduvall 
23261369Sdduvall 	/*
23271369Sdduvall 	 * Flag that no more activity may be initiated
23281369Sdduvall 	 */
23291369Sdduvall 	bgep->progress &= ~PROGRESS_READY;
23301369Sdduvall 
23311369Sdduvall 	/*
23321369Sdduvall 	 * Quiesce the PHY and MAC (leave it reset but still powered).
23331369Sdduvall 	 * Clean up and free all BGE data structures
23341369Sdduvall 	 */
2335*5107Seota 	if (bgep->periodic_id != NULL) {
2336*5107Seota 		ddi_periodic_delete(bgep->periodic_id);
2337*5107Seota 		bgep->periodic_id = NULL;
23381369Sdduvall 	}
23391369Sdduvall 	if (bgep->progress & PROGRESS_KSTATS)
23401369Sdduvall 		bge_fini_kstats(bgep);
23411369Sdduvall 	if (bgep->progress & PROGRESS_NDD)
23421369Sdduvall 		bge_nd_cleanup(bgep);
23431369Sdduvall 	if (bgep->progress & PROGRESS_PHY)
23441369Sdduvall 		bge_phys_reset(bgep);
23451369Sdduvall 	if (bgep->progress & PROGRESS_HWINT) {
23461369Sdduvall 		mutex_enter(bgep->genlock);
23471408Srandyf #ifdef BGE_IPMI_ASF
23481865Sdilpreet 		if (bge_chip_reset(bgep, B_FALSE, asf_mode) != DDI_SUCCESS)
23491865Sdilpreet #else
23501865Sdilpreet 		if (bge_chip_reset(bgep, B_FALSE) != DDI_SUCCESS)
23511865Sdilpreet #endif
23521865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
23531865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
23541865Sdilpreet #ifdef BGE_IPMI_ASF
23551408Srandyf 		if (bgep->asf_enabled) {
23561408Srandyf 			/*
23571408Srandyf 			 * This register has been overlaid. We restore its
23581408Srandyf 			 * initial value here.
23591408Srandyf 			 */
23601408Srandyf 			bge_nic_put32(bgep, BGE_NIC_DATA_SIG_ADDR,
23611408Srandyf 			    BGE_NIC_DATA_SIG);
23621408Srandyf 		}
23631408Srandyf #endif
23641865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
23651865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
23661865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
23671865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
23681865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
23691865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
23701369Sdduvall 		mutex_exit(bgep->genlock);
23711369Sdduvall 	}
23721369Sdduvall 	if (bgep->progress & PROGRESS_INTR) {
23731865Sdilpreet 		bge_intr_disable(bgep);
23741369Sdduvall 		bge_fini_rings(bgep);
23751369Sdduvall 	}
23761865Sdilpreet 	if (bgep->progress & PROGRESS_HWINT) {
23771865Sdilpreet 		bge_rem_intrs(bgep);
23781865Sdilpreet 		rw_destroy(bgep->errlock);
23791865Sdilpreet 		mutex_destroy(bgep->softintrlock);
23801865Sdilpreet 		mutex_destroy(bgep->genlock);
23811865Sdilpreet 	}
23821369Sdduvall 	if (bgep->progress & PROGRESS_FACTOTUM)
23831369Sdduvall 		ddi_remove_softintr(bgep->factotum_id);
23841369Sdduvall 	if (bgep->progress & PROGRESS_RESCHED)
23853334Sgs150176 		ddi_remove_softintr(bgep->drain_id);
23861865Sdilpreet 	if (bgep->progress & PROGRESS_BUFS)
23871865Sdilpreet 		bge_free_bufs(bgep);
23881369Sdduvall 	if (bgep->progress & PROGRESS_REGS)
23891369Sdduvall 		ddi_regs_map_free(&bgep->io_handle);
23901369Sdduvall 	if (bgep->progress & PROGRESS_CFG)
23911369Sdduvall 		pci_config_teardown(&bgep->cfg_handle);
23921369Sdduvall 
23931865Sdilpreet 	bge_fm_fini(bgep);
23941865Sdilpreet 
23951369Sdduvall 	ddi_remove_minor_node(bgep->devinfo, NULL);
23963334Sgs150176 	kmem_free(bgep->pstats, sizeof (bge_statistics_reg_t));
23973334Sgs150176 	kmem_free(bgep->nd_params, PARAM_COUNT * sizeof (nd_param_t));
23981369Sdduvall 	kmem_free(bgep, sizeof (*bgep));
23991369Sdduvall }
24001369Sdduvall 
24011369Sdduvall static int
24021369Sdduvall bge_resume(dev_info_t *devinfo)
24031369Sdduvall {
24041369Sdduvall 	bge_t *bgep;				/* Our private data	*/
24051369Sdduvall 	chip_id_t *cidp;
24061369Sdduvall 	chip_id_t chipid;
24071369Sdduvall 
24081369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
24091369Sdduvall 	if (bgep == NULL)
24101369Sdduvall 		return (DDI_FAILURE);
24111369Sdduvall 
24121369Sdduvall 	/*
24131369Sdduvall 	 * Refuse to resume if the data structures aren't consistent
24141369Sdduvall 	 */
24151369Sdduvall 	if (bgep->devinfo != devinfo)
24161369Sdduvall 		return (DDI_FAILURE);
24171369Sdduvall 
24181408Srandyf #ifdef BGE_IPMI_ASF
24191408Srandyf 	/*
24201408Srandyf 	 * Power management hasn't been supported in BGE now. If you
24211408Srandyf 	 * want to implement it, please add the ASF/IPMI related
24221408Srandyf 	 * code here.
24231408Srandyf 	 */
24241408Srandyf 
24251408Srandyf #endif
24261408Srandyf 
24271369Sdduvall 	/*
24281369Sdduvall 	 * Read chip ID & set up config space command register(s)
24291369Sdduvall 	 * Refuse to resume if the chip has changed its identity!
24301369Sdduvall 	 */
24311369Sdduvall 	cidp = &bgep->chipid;
24321865Sdilpreet 	mutex_enter(bgep->genlock);
24331369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, B_FALSE);
24341865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
24351865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
24361865Sdilpreet 		mutex_exit(bgep->genlock);
24371865Sdilpreet 		return (DDI_FAILURE);
24381865Sdilpreet 	}
24391865Sdilpreet 	mutex_exit(bgep->genlock);
24401369Sdduvall 	if (chipid.vendor != cidp->vendor)
24411369Sdduvall 		return (DDI_FAILURE);
24421369Sdduvall 	if (chipid.device != cidp->device)
24431369Sdduvall 		return (DDI_FAILURE);
24441369Sdduvall 	if (chipid.revision != cidp->revision)
24451369Sdduvall 		return (DDI_FAILURE);
24461369Sdduvall 	if (chipid.asic_rev != cidp->asic_rev)
24471369Sdduvall 		return (DDI_FAILURE);
24481369Sdduvall 
24491369Sdduvall 	/*
24501369Sdduvall 	 * All OK, reinitialise h/w & kick off GLD scheduling
24511369Sdduvall 	 */
24521369Sdduvall 	mutex_enter(bgep->genlock);
24531865Sdilpreet 	if (bge_restart(bgep, B_TRUE) != DDI_SUCCESS) {
24541865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
24551865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
24561865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
24571865Sdilpreet 		mutex_exit(bgep->genlock);
24581865Sdilpreet 		return (DDI_FAILURE);
24591865Sdilpreet 	}
24601865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
24611865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
24621865Sdilpreet 		mutex_exit(bgep->genlock);
24631865Sdilpreet 		return (DDI_FAILURE);
24641865Sdilpreet 	}
24651865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
24661865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
24671865Sdilpreet 		mutex_exit(bgep->genlock);
24681865Sdilpreet 		return (DDI_FAILURE);
24691865Sdilpreet 	}
24701369Sdduvall 	mutex_exit(bgep->genlock);
24711369Sdduvall 	return (DDI_SUCCESS);
24721369Sdduvall }
24731369Sdduvall 
24741369Sdduvall /*
24751369Sdduvall  * attach(9E) -- Attach a device to the system
24761369Sdduvall  *
24771369Sdduvall  * Called once for each board successfully probed.
24781369Sdduvall  */
24791369Sdduvall static int
24801369Sdduvall bge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
24811369Sdduvall {
24821369Sdduvall 	bge_t *bgep;				/* Our private data	*/
24832311Sseb 	mac_register_t *macp;
24841369Sdduvall 	chip_id_t *cidp;
24851369Sdduvall 	caddr_t regs;
24861369Sdduvall 	int instance;
24871369Sdduvall 	int err;
24881369Sdduvall 	int intr_types;
24891408Srandyf #ifdef BGE_IPMI_ASF
24901408Srandyf 	uint32_t mhcrValue;
24913918Sml149210 #ifdef __sparc
24923918Sml149210 	uint16_t value16;
24933918Sml149210 #endif
24943918Sml149210 #ifdef BGE_NETCONSOLE
24953918Sml149210 	int retval;
24963918Sml149210 #endif
24971408Srandyf #endif
24981369Sdduvall 
24991369Sdduvall 	instance = ddi_get_instance(devinfo);
25001369Sdduvall 
25011369Sdduvall 	BGE_GTRACE(("bge_attach($%p, %d) instance %d",
25024588Sml149210 	    (void *)devinfo, cmd, instance));
25031369Sdduvall 	BGE_BRKPT(NULL, "bge_attach");
25041369Sdduvall 
25051369Sdduvall 	switch (cmd) {
25061369Sdduvall 	default:
25071369Sdduvall 		return (DDI_FAILURE);
25081369Sdduvall 
25091369Sdduvall 	case DDI_RESUME:
25101369Sdduvall 		return (bge_resume(devinfo));
25111369Sdduvall 
25121369Sdduvall 	case DDI_ATTACH:
25131369Sdduvall 		break;
25141369Sdduvall 	}
25151369Sdduvall 
25161369Sdduvall 	bgep = kmem_zalloc(sizeof (*bgep), KM_SLEEP);
25173334Sgs150176 	bgep->pstats = kmem_zalloc(sizeof (bge_statistics_reg_t), KM_SLEEP);
25183334Sgs150176 	bgep->nd_params =
25193334Sgs150176 	    kmem_zalloc(PARAM_COUNT * sizeof (nd_param_t), KM_SLEEP);
25201369Sdduvall 	ddi_set_driver_private(devinfo, bgep);
25211369Sdduvall 	bgep->bge_guard = BGE_GUARD;
25221369Sdduvall 	bgep->devinfo = devinfo;
25231369Sdduvall 
25241369Sdduvall 	/*
25251369Sdduvall 	 * Initialize more fields in BGE private data
25261369Sdduvall 	 */
25271369Sdduvall 	bgep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
25284588Sml149210 	    DDI_PROP_DONTPASS, debug_propname, bge_debug);
25291369Sdduvall 	(void) snprintf(bgep->ifname, sizeof (bgep->ifname), "%s%d",
25304588Sml149210 	    BGE_DRIVER_NAME, instance);
25311369Sdduvall 
25321369Sdduvall 	/*
25331865Sdilpreet 	 * Initialize for fma support
25341865Sdilpreet 	 */
25351865Sdilpreet 	bgep->fm_capabilities = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
25361865Sdilpreet 	    DDI_PROP_DONTPASS, fm_cap,
25371865Sdilpreet 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
25381865Sdilpreet 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
25391865Sdilpreet 	BGE_DEBUG(("bgep->fm_capabilities = %d", bgep->fm_capabilities));
25401865Sdilpreet 	bge_fm_init(bgep);
25411865Sdilpreet 
25421865Sdilpreet 	/*
25431369Sdduvall 	 * Look up the IOMMU's page size for DVMA mappings (must be
25441369Sdduvall 	 * a power of 2) and convert to a mask.  This can be used to
25451369Sdduvall 	 * determine whether a message buffer crosses a page boundary.
25461369Sdduvall 	 * Note: in 2s complement binary notation, if X is a power of
25471369Sdduvall 	 * 2, then -X has the representation "11...1100...00".
25481369Sdduvall 	 */
25491369Sdduvall 	bgep->pagemask = dvma_pagesize(devinfo);
25501369Sdduvall 	ASSERT(ddi_ffs(bgep->pagemask) == ddi_fls(bgep->pagemask));
25511369Sdduvall 	bgep->pagemask = -bgep->pagemask;
25521369Sdduvall 
25531369Sdduvall 	/*
25541369Sdduvall 	 * Map config space registers
25551369Sdduvall 	 * Read chip ID & set up config space command register(s)
25561369Sdduvall 	 *
25571369Sdduvall 	 * Note: this leaves the chip accessible by Memory Space
25581369Sdduvall 	 * accesses, but with interrupts and Bus Mastering off.
25591369Sdduvall 	 * This should ensure that nothing untoward will happen
25601369Sdduvall 	 * if it has been left active by the (net-)bootloader.
25611369Sdduvall 	 * We'll re-enable Bus Mastering once we've reset the chip,
25621369Sdduvall 	 * and allow interrupts only when everything else is set up.
25631369Sdduvall 	 */
25641369Sdduvall 	err = pci_config_setup(devinfo, &bgep->cfg_handle);
25651408Srandyf #ifdef BGE_IPMI_ASF
25663918Sml149210 #ifdef __sparc
25673918Sml149210 	value16 = pci_config_get16(bgep->cfg_handle, PCI_CONF_COMM);
25683918Sml149210 	value16 = value16 | (PCI_COMM_MAE | PCI_COMM_ME);
25693918Sml149210 	pci_config_put16(bgep->cfg_handle, PCI_CONF_COMM, value16);
25703918Sml149210 	mhcrValue = MHCR_ENABLE_INDIRECT_ACCESS |
25714588Sml149210 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
25724588Sml149210 	    MHCR_MASK_INTERRUPT_MODE |
25734588Sml149210 	    MHCR_MASK_PCI_INT_OUTPUT |
25744588Sml149210 	    MHCR_CLEAR_INTERRUPT_INTA |
25754588Sml149210 	    MHCR_ENABLE_ENDIAN_WORD_SWAP |
25764588Sml149210 	    MHCR_ENABLE_ENDIAN_BYTE_SWAP;
25773918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcrValue);
25783918Sml149210 	bge_ind_put32(bgep, MEMORY_ARBITER_MODE_REG,
25794588Sml149210 	    bge_ind_get32(bgep, MEMORY_ARBITER_MODE_REG) |
25804588Sml149210 	    MEMORY_ARBITER_ENABLE);
25813918Sml149210 #else
25821408Srandyf 	mhcrValue = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MHCR);
25833918Sml149210 #endif
25841408Srandyf 	if (mhcrValue & MHCR_ENABLE_ENDIAN_WORD_SWAP) {
25851408Srandyf 		bgep->asf_wordswapped = B_TRUE;
25861408Srandyf 	} else {
25871408Srandyf 		bgep->asf_wordswapped = B_FALSE;
25881408Srandyf 	}
25891408Srandyf 	bge_asf_get_config(bgep);
25901408Srandyf #endif
25911369Sdduvall 	if (err != DDI_SUCCESS) {
25921369Sdduvall 		bge_problem(bgep, "pci_config_setup() failed");
25931369Sdduvall 		goto attach_fail;
25941369Sdduvall 	}
25951369Sdduvall 	bgep->progress |= PROGRESS_CFG;
25961369Sdduvall 	cidp = &bgep->chipid;
25971369Sdduvall 	bzero(cidp, sizeof (*cidp));
25981369Sdduvall 	bge_chip_cfg_init(bgep, cidp, B_FALSE);
25991865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
26001865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
26011865Sdilpreet 		goto attach_fail;
26021865Sdilpreet 	}
26031369Sdduvall 
26041408Srandyf #ifdef BGE_IPMI_ASF
26051408Srandyf 	if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
26061408Srandyf 	    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
26071408Srandyf 		bgep->asf_newhandshake = B_TRUE;
26081408Srandyf 	} else {
26091408Srandyf 		bgep->asf_newhandshake = B_FALSE;
26101408Srandyf 	}
26111408Srandyf #endif
26121408Srandyf 
26131369Sdduvall 	/*
26141369Sdduvall 	 * Update those parts of the chip ID derived from volatile
26151369Sdduvall 	 * registers with the values seen by OBP (in case the chip
26161369Sdduvall 	 * has been reset externally and therefore lost them).
26171369Sdduvall 	 */
26181369Sdduvall 	cidp->subven = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26194588Sml149210 	    DDI_PROP_DONTPASS, subven_propname, cidp->subven);
26201369Sdduvall 	cidp->subdev = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26214588Sml149210 	    DDI_PROP_DONTPASS, subdev_propname, cidp->subdev);
26221369Sdduvall 	cidp->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26234588Sml149210 	    DDI_PROP_DONTPASS, clsize_propname, cidp->clsize);
26241369Sdduvall 	cidp->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26254588Sml149210 	    DDI_PROP_DONTPASS, latency_propname, cidp->latency);
26261369Sdduvall 	cidp->rx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26274588Sml149210 	    DDI_PROP_DONTPASS, rxrings_propname, cidp->rx_rings);
26281369Sdduvall 	cidp->tx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26294588Sml149210 	    DDI_PROP_DONTPASS, txrings_propname, cidp->tx_rings);
26301369Sdduvall 
26311369Sdduvall 	if (bge_jumbo_enable == B_TRUE) {
26321369Sdduvall 		cidp->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
26334588Sml149210 		    DDI_PROP_DONTPASS, default_mtu, BGE_DEFAULT_MTU);
26341369Sdduvall 		if ((cidp->default_mtu < BGE_DEFAULT_MTU)||
26354588Sml149210 		    (cidp->default_mtu > BGE_MAXIMUM_MTU)) {
26361369Sdduvall 			cidp->default_mtu = BGE_DEFAULT_MTU;
26371369Sdduvall 		}
26381369Sdduvall 	}
26391369Sdduvall 	/*
26401369Sdduvall 	 * Map operating registers
26411369Sdduvall 	 */
26421369Sdduvall 	err = ddi_regs_map_setup(devinfo, BGE_PCI_OPREGS_RNUMBER,
26434588Sml149210 	    &regs, 0, 0, &bge_reg_accattr, &bgep->io_handle);
26441369Sdduvall 	if (err != DDI_SUCCESS) {
26451369Sdduvall 		bge_problem(bgep, "ddi_regs_map_setup() failed");
26461369Sdduvall 		goto attach_fail;
26471369Sdduvall 	}
26481369Sdduvall 	bgep->io_regs = regs;
26491369Sdduvall 	bgep->progress |= PROGRESS_REGS;
26501369Sdduvall 
26511369Sdduvall 	/*
26521369Sdduvall 	 * Characterise the device, so we know its requirements.
26531369Sdduvall 	 * Then allocate the appropriate TX and RX descriptors & buffers.
26541369Sdduvall 	 */
26551865Sdilpreet 	if (bge_chip_id_init(bgep) == EIO) {
26561865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
26571865Sdilpreet 		goto attach_fail;
26581865Sdilpreet 	}
26591369Sdduvall 	err = bge_alloc_bufs(bgep);
26601369Sdduvall 	if (err != DDI_SUCCESS) {
26611369Sdduvall 		bge_problem(bgep, "DMA buffer allocation failed");
26621369Sdduvall 		goto attach_fail;
26631369Sdduvall 	}
26641865Sdilpreet 	bgep->progress |= PROGRESS_BUFS;
26651369Sdduvall 
26661369Sdduvall 	/*
26671369Sdduvall 	 * Add the softint handlers:
26681369Sdduvall 	 *
26691369Sdduvall 	 * Both of these handlers are used to avoid restrictions on the
26701369Sdduvall 	 * context and/or mutexes required for some operations.  In
26711369Sdduvall 	 * particular, the hardware interrupt handler and its subfunctions
26721369Sdduvall 	 * can detect a number of conditions that we don't want to handle
26731369Sdduvall 	 * in that context or with that set of mutexes held.  So, these
26741369Sdduvall 	 * softints are triggered instead:
26751369Sdduvall 	 *
26762135Szh199473 	 * the <resched> softint is triggered if we have previously
26771369Sdduvall 	 * had to refuse to send a packet because of resource shortage
26781369Sdduvall 	 * (we've run out of transmit buffers), but the send completion
26791369Sdduvall 	 * interrupt handler has now detected that more buffers have
26801369Sdduvall 	 * become available.
26811369Sdduvall 	 *
26821369Sdduvall 	 * the <factotum> is triggered if the h/w interrupt handler
26831369Sdduvall 	 * sees the <link state changed> or <error> bits in the status
26841369Sdduvall 	 * block.  It's also triggered periodically to poll the link
26851369Sdduvall 	 * state, just in case we aren't getting link status change
26861369Sdduvall 	 * interrupts ...
26871369Sdduvall 	 */
26883334Sgs150176 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->drain_id,
26894588Sml149210 	    NULL, NULL, bge_send_drain, (caddr_t)bgep);
26901369Sdduvall 	if (err != DDI_SUCCESS) {
26911369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
26921369Sdduvall 		goto attach_fail;
26931369Sdduvall 	}
26941369Sdduvall 	bgep->progress |= PROGRESS_RESCHED;
26951369Sdduvall 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->factotum_id,
26964588Sml149210 	    NULL, NULL, bge_chip_factotum, (caddr_t)bgep);
26971369Sdduvall 	if (err != DDI_SUCCESS) {
26981369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
26991369Sdduvall 		goto attach_fail;
27001369Sdduvall 	}
27011369Sdduvall 	bgep->progress |= PROGRESS_FACTOTUM;
27021369Sdduvall 
27031369Sdduvall 	/* Get supported interrupt types */
27041369Sdduvall 	if (ddi_intr_get_supported_types(devinfo, &intr_types) != DDI_SUCCESS) {
27051369Sdduvall 		bge_error(bgep, "ddi_intr_get_supported_types failed\n");
27061369Sdduvall 
27071369Sdduvall 		goto attach_fail;
27081369Sdduvall 	}
27091369Sdduvall 
27102675Szh199473 	BGE_DEBUG(("%s: ddi_intr_get_supported_types() returned: %x",
27114588Sml149210 	    bgep->ifname, intr_types));
27121369Sdduvall 
27131369Sdduvall 	if ((intr_types & DDI_INTR_TYPE_MSI) && bgep->chipid.msi_enabled) {
27141369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
27151369Sdduvall 			bge_error(bgep, "MSI registration failed, "
27161369Sdduvall 			    "trying FIXED interrupt type\n");
27171369Sdduvall 		} else {
27182675Szh199473 			BGE_DEBUG(("%s: Using MSI interrupt type",
27194588Sml149210 			    bgep->ifname));
27201369Sdduvall 			bgep->intr_type = DDI_INTR_TYPE_MSI;
27211865Sdilpreet 			bgep->progress |= PROGRESS_HWINT;
27221369Sdduvall 		}
27231369Sdduvall 	}
27241369Sdduvall 
27251865Sdilpreet 	if (!(bgep->progress & PROGRESS_HWINT) &&
27261369Sdduvall 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
27271369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
27281369Sdduvall 			bge_error(bgep, "FIXED interrupt "
27291369Sdduvall 			    "registration failed\n");
27301369Sdduvall 			goto attach_fail;
27311369Sdduvall 		}
27321369Sdduvall 
27332675Szh199473 		BGE_DEBUG(("%s: Using FIXED interrupt type", bgep->ifname));
27341369Sdduvall 
27351369Sdduvall 		bgep->intr_type = DDI_INTR_TYPE_FIXED;
27361865Sdilpreet 		bgep->progress |= PROGRESS_HWINT;
27371369Sdduvall 	}
27381369Sdduvall 
27391865Sdilpreet 	if (!(bgep->progress & PROGRESS_HWINT)) {
27401369Sdduvall 		bge_error(bgep, "No interrupts registered\n");
27411369Sdduvall 		goto attach_fail;
27421369Sdduvall 	}
27431369Sdduvall 
27441369Sdduvall 	/*
27451369Sdduvall 	 * Note that interrupts are not enabled yet as
27461865Sdilpreet 	 * mutex locks are not initialized. Initialize mutex locks.
27471865Sdilpreet 	 */
27481865Sdilpreet 	mutex_init(bgep->genlock, NULL, MUTEX_DRIVER,
27491865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
27501865Sdilpreet 	mutex_init(bgep->softintrlock, NULL, MUTEX_DRIVER,
27511865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
27521865Sdilpreet 	rw_init(bgep->errlock, NULL, RW_DRIVER,
27531865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
27541865Sdilpreet 
27551865Sdilpreet 	/*
27561865Sdilpreet 	 * Initialize rings.
27571369Sdduvall 	 */
27581369Sdduvall 	bge_init_rings(bgep);
27591369Sdduvall 
27601369Sdduvall 	/*
27611369Sdduvall 	 * Now that mutex locks are initialized, enable interrupts.
27621369Sdduvall 	 */
27631865Sdilpreet 	bge_intr_enable(bgep);
27641865Sdilpreet 	bgep->progress |= PROGRESS_INTR;
27651369Sdduvall 
27661369Sdduvall 	/*
27671369Sdduvall 	 * Initialise link state variables
27681369Sdduvall 	 * Stop, reset & reinitialise the chip.
27691369Sdduvall 	 * Initialise the (internal) PHY.
27701369Sdduvall 	 */
27711369Sdduvall 	bgep->link_state = LINK_STATE_UNKNOWN;
27721369Sdduvall 
27731369Sdduvall 	mutex_enter(bgep->genlock);
27741369Sdduvall 
27751369Sdduvall 	/*
27761369Sdduvall 	 * Reset chip & rings to initial state; also reset address
27771369Sdduvall 	 * filtering, promiscuity, loopback mode.
27781369Sdduvall 	 */
27791408Srandyf #ifdef BGE_IPMI_ASF
27803918Sml149210 #ifdef BGE_NETCONSOLE
27813918Sml149210 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
27823918Sml149210 #else
27831865Sdilpreet 	if (bge_reset(bgep, ASF_MODE_SHUTDOWN) != DDI_SUCCESS) {
27843918Sml149210 #endif
27851408Srandyf #else
27861865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS) {
27871408Srandyf #endif
27881865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
27891865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
27901865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
27911865Sdilpreet 		mutex_exit(bgep->genlock);
27921865Sdilpreet 		goto attach_fail;
27931865Sdilpreet 	}
27941369Sdduvall 
27952675Szh199473 #ifdef BGE_IPMI_ASF
27962675Szh199473 	if (bgep->asf_enabled) {
27972675Szh199473 		bgep->asf_status = ASF_STAT_RUN_INIT;
27982675Szh199473 	}
27992675Szh199473 #endif
28002675Szh199473 
28011369Sdduvall 	bzero(bgep->mcast_hash, sizeof (bgep->mcast_hash));
28021369Sdduvall 	bzero(bgep->mcast_refs, sizeof (bgep->mcast_refs));
28031369Sdduvall 	bgep->promisc = B_FALSE;
28041369Sdduvall 	bgep->param_loop_mode = BGE_LOOP_NONE;
28051865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
28061865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
28071865Sdilpreet 		mutex_exit(bgep->genlock);
28081865Sdilpreet 		goto attach_fail;
28091865Sdilpreet 	}
28101865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
28111865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
28121865Sdilpreet 		mutex_exit(bgep->genlock);
28131865Sdilpreet 		goto attach_fail;
28141865Sdilpreet 	}
28151369Sdduvall 
28161369Sdduvall 	mutex_exit(bgep->genlock);
28171369Sdduvall 
28181865Sdilpreet 	if (bge_phys_init(bgep) == EIO) {
28191865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
28201865Sdilpreet 		goto attach_fail;
28211865Sdilpreet 	}
28221369Sdduvall 	bgep->progress |= PROGRESS_PHY;
28231369Sdduvall 
28241369Sdduvall 	/*
28251369Sdduvall 	 * Register NDD-tweakable parameters
28261369Sdduvall 	 */
28271369Sdduvall 	if (bge_nd_init(bgep)) {
28281369Sdduvall 		bge_problem(bgep, "bge_nd_init() failed");
28291369Sdduvall 		goto attach_fail;
28301369Sdduvall 	}
28311369Sdduvall 	bgep->progress |= PROGRESS_NDD;
28321369Sdduvall 
28331369Sdduvall 	/*
28341369Sdduvall 	 * Create & initialise named kstats
28351369Sdduvall 	 */
28361369Sdduvall 	bge_init_kstats(bgep, instance);
28371369Sdduvall 	bgep->progress |= PROGRESS_KSTATS;
28381369Sdduvall 
28391369Sdduvall 	/*
28401369Sdduvall 	 * Determine whether to override the chip's own MAC address
28411369Sdduvall 	 */
28421369Sdduvall 	bge_find_mac_address(bgep, cidp);
28432331Skrgopi 	ethaddr_copy(cidp->vendor_addr.addr, bgep->curr_addr[0].addr);
28442331Skrgopi 	bgep->curr_addr[0].set = B_TRUE;
28452331Skrgopi 
28462406Skrgopi 	bgep->unicst_addr_total = MAC_ADDRESS_REGS_MAX;
28472406Skrgopi 	/*
28482406Skrgopi 	 * Address available is one less than MAX
28492406Skrgopi 	 * as primary address is not advertised
28502406Skrgopi 	 * as a multiple MAC address.
28512406Skrgopi 	 */
28522331Skrgopi 	bgep->unicst_addr_avail = MAC_ADDRESS_REGS_MAX - 1;
28531369Sdduvall 
28542311Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
28552311Sseb 		goto attach_fail;
28562311Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
28572311Sseb 	macp->m_driver = bgep;
28581369Sdduvall 	macp->m_dip = devinfo;
28592331Skrgopi 	macp->m_src_addr = bgep->curr_addr[0].addr;
28602311Sseb 	macp->m_callbacks = &bge_m_callbacks;
28612311Sseb 	macp->m_min_sdu = 0;
28622311Sseb 	macp->m_max_sdu = cidp->ethmax_size - sizeof (struct ether_header);
28631369Sdduvall 	/*
28641369Sdduvall 	 * Finally, we're ready to register ourselves with the MAC layer
28651369Sdduvall 	 * interface; if this succeeds, we're all ready to start()
28661369Sdduvall 	 */
28672311Sseb 	err = mac_register(macp, &bgep->mh);
28682311Sseb 	mac_free(macp);
28692311Sseb 	if (err != 0)
28701369Sdduvall 		goto attach_fail;
28711369Sdduvall 
2872*5107Seota 	/*
2873*5107Seota 	 * Register a periodical handler.
2874*5107Seota 	 * bge_chip_cyclic() is invoked in kernel context.
2875*5107Seota 	 */
2876*5107Seota 	bgep->periodic_id = ddi_periodic_add(bge_chip_cyclic, bgep,
2877*5107Seota 	    BGE_CYCLIC_PERIOD, DDI_IPL_0);
28781369Sdduvall 
28791369Sdduvall 	bgep->progress |= PROGRESS_READY;
28801369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
28813918Sml149210 #ifdef BGE_IPMI_ASF
28823918Sml149210 #ifdef BGE_NETCONSOLE
28833918Sml149210 	if (bgep->asf_enabled) {
28843918Sml149210 		mutex_enter(bgep->genlock);
28853918Sml149210 		retval = bge_chip_start(bgep, B_TRUE);
28863918Sml149210 		mutex_exit(bgep->genlock);
28873918Sml149210 		if (retval != DDI_SUCCESS)
28883918Sml149210 			goto attach_fail;
28893918Sml149210 	}
28903918Sml149210 #endif
28913918Sml149210 #endif
28921369Sdduvall 	return (DDI_SUCCESS);
28931369Sdduvall 
28941369Sdduvall attach_fail:
28951408Srandyf #ifdef BGE_IPMI_ASF
28962675Szh199473 	bge_unattach(bgep, ASF_MODE_SHUTDOWN);
28971408Srandyf #else
28981369Sdduvall 	bge_unattach(bgep);
28991408Srandyf #endif
29001369Sdduvall 	return (DDI_FAILURE);
29011369Sdduvall }
29021369Sdduvall 
29031369Sdduvall /*
29041369Sdduvall  *	bge_suspend() -- suspend transmit/receive for powerdown
29051369Sdduvall  */
29061369Sdduvall static int
29071369Sdduvall bge_suspend(bge_t *bgep)
29081369Sdduvall {
29091369Sdduvall 	/*
29101369Sdduvall 	 * Stop processing and idle (powerdown) the PHY ...
29111369Sdduvall 	 */
29121369Sdduvall 	mutex_enter(bgep->genlock);
29131408Srandyf #ifdef BGE_IPMI_ASF
29141408Srandyf 	/*
29151408Srandyf 	 * Power management hasn't been supported in BGE now. If you
29161408Srandyf 	 * want to implement it, please add the ASF/IPMI related
29171408Srandyf 	 * code here.
29181408Srandyf 	 */
29191408Srandyf #endif
29201369Sdduvall 	bge_stop(bgep);
29211865Sdilpreet 	if (bge_phys_idle(bgep) != DDI_SUCCESS) {
29221865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
29231865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
29241865Sdilpreet 		mutex_exit(bgep->genlock);
29251865Sdilpreet 		return (DDI_FAILURE);
29261865Sdilpreet 	}
29271865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
29281865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
29291865Sdilpreet 		mutex_exit(bgep->genlock);
29301865Sdilpreet 		return (DDI_FAILURE);
29311865Sdilpreet 	}
29321369Sdduvall 	mutex_exit(bgep->genlock);
29331369Sdduvall 
29341369Sdduvall 	return (DDI_SUCCESS);
29351369Sdduvall }
29361369Sdduvall 
29371369Sdduvall /*
29381369Sdduvall  * detach(9E) -- Detach a device from the system
29391369Sdduvall  */
29401369Sdduvall static int
29411369Sdduvall bge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
29421369Sdduvall {
29431369Sdduvall 	bge_t *bgep;
29441408Srandyf #ifdef BGE_IPMI_ASF
29451408Srandyf 	uint_t asf_mode;
29461408Srandyf 	asf_mode = ASF_MODE_NONE;
29471408Srandyf #endif
29481369Sdduvall 
29491369Sdduvall 	BGE_GTRACE(("bge_detach($%p, %d)", (void *)devinfo, cmd));
29501369Sdduvall 
29511369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
29521369Sdduvall 
29531369Sdduvall 	switch (cmd) {
29541369Sdduvall 	default:
29551369Sdduvall 		return (DDI_FAILURE);
29561369Sdduvall 
29571369Sdduvall 	case DDI_SUSPEND:
29581369Sdduvall 		return (bge_suspend(bgep));
29591369Sdduvall 
29601369Sdduvall 	case DDI_DETACH:
29611369Sdduvall 		break;
29621369Sdduvall 	}
29631369Sdduvall 
29641408Srandyf #ifdef BGE_IPMI_ASF
29651408Srandyf 	mutex_enter(bgep->genlock);
29662675Szh199473 	if (bgep->asf_enabled && ((bgep->asf_status == ASF_STAT_RUN) ||
29674588Sml149210 	    (bgep->asf_status == ASF_STAT_RUN_INIT))) {
29681408Srandyf 
29691408Srandyf 		bge_asf_update_status(bgep);
29702675Szh199473 		if (bgep->asf_status == ASF_STAT_RUN) {
29712675Szh199473 			bge_asf_stop_timer(bgep);
29722675Szh199473 		}
29731408Srandyf 		bgep->asf_status = ASF_STAT_STOP;
29741408Srandyf 
29751408Srandyf 		bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
29761408Srandyf 
29771408Srandyf 		if (bgep->asf_pseudostop) {
29781408Srandyf 			bge_chip_stop(bgep, B_FALSE);
29791408Srandyf 			bgep->bge_mac_state = BGE_MAC_STOPPED;
29801408Srandyf 			bgep->asf_pseudostop = B_FALSE;
29811408Srandyf 		}
29821408Srandyf 
29831408Srandyf 		asf_mode = ASF_MODE_POST_SHUTDOWN;
29841865Sdilpreet 
29851865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
29861865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
29871865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
29881865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
29891865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
29901865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
29911408Srandyf 	}
29921408Srandyf 	mutex_exit(bgep->genlock);
29931408Srandyf #endif
29941408Srandyf 
29951369Sdduvall 	/*
29961369Sdduvall 	 * Unregister from the GLD subsystem.  This can fail, in
29971369Sdduvall 	 * particular if there are DLPI style-2 streams still open -
29981369Sdduvall 	 * in which case we just return failure without shutting
29991369Sdduvall 	 * down chip operations.
30001369Sdduvall 	 */
30012311Sseb 	if (mac_unregister(bgep->mh) != 0)
30021369Sdduvall 		return (DDI_FAILURE);
30031369Sdduvall 
30041369Sdduvall 	/*
30051369Sdduvall 	 * All activity stopped, so we can clean up & exit
30061369Sdduvall 	 */
30071408Srandyf #ifdef BGE_IPMI_ASF
30081408Srandyf 	bge_unattach(bgep, asf_mode);
30091408Srandyf #else
30101369Sdduvall 	bge_unattach(bgep);
30111408Srandyf #endif
30121369Sdduvall 	return (DDI_SUCCESS);
30131369Sdduvall }
30141369Sdduvall 
30151369Sdduvall 
30161369Sdduvall /*
30171369Sdduvall  * ========== Module Loading Data & Entry Points ==========
30181369Sdduvall  */
30191369Sdduvall 
30201369Sdduvall #undef	BGE_DBG
30211369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
30221369Sdduvall 
30231369Sdduvall DDI_DEFINE_STREAM_OPS(bge_dev_ops, nulldev, nulldev, bge_attach, bge_detach,
30241369Sdduvall     nodev, NULL, D_MP, NULL);
30251369Sdduvall 
30261369Sdduvall static struct modldrv bge_modldrv = {
30271369Sdduvall 	&mod_driverops,		/* Type of module.  This one is a driver */
30281369Sdduvall 	bge_ident,		/* short description */
30291369Sdduvall 	&bge_dev_ops		/* driver specific ops */
30301369Sdduvall };
30311369Sdduvall 
30321369Sdduvall static struct modlinkage modlinkage = {
30331369Sdduvall 	MODREV_1, (void *)&bge_modldrv, NULL
30341369Sdduvall };
30351369Sdduvall 
30361369Sdduvall 
30371369Sdduvall int
30381369Sdduvall _info(struct modinfo *modinfop)
30391369Sdduvall {
30401369Sdduvall 	return (mod_info(&modlinkage, modinfop));
30411369Sdduvall }
30421369Sdduvall 
30431369Sdduvall int
30441369Sdduvall _init(void)
30451369Sdduvall {
30461369Sdduvall 	int status;
30471369Sdduvall 
30481369Sdduvall 	mac_init_ops(&bge_dev_ops, "bge");
30491369Sdduvall 	status = mod_install(&modlinkage);
30501369Sdduvall 	if (status == DDI_SUCCESS)
30511369Sdduvall 		mutex_init(bge_log_mutex, NULL, MUTEX_DRIVER, NULL);
30521369Sdduvall 	else
30531369Sdduvall 		mac_fini_ops(&bge_dev_ops);
30541369Sdduvall 	return (status);
30551369Sdduvall }
30561369Sdduvall 
30571369Sdduvall int
30581369Sdduvall _fini(void)
30591369Sdduvall {
30601369Sdduvall 	int status;
30611369Sdduvall 
30621369Sdduvall 	status = mod_remove(&modlinkage);
30631369Sdduvall 	if (status == DDI_SUCCESS) {
30641369Sdduvall 		mac_fini_ops(&bge_dev_ops);
30651369Sdduvall 		mutex_destroy(bge_log_mutex);
30661369Sdduvall 	}
30671369Sdduvall 	return (status);
30681369Sdduvall }
30691369Sdduvall 
30701369Sdduvall 
30711369Sdduvall /*
30721369Sdduvall  * bge_add_intrs:
30731369Sdduvall  *
30741369Sdduvall  * Register FIXED or MSI interrupts.
30751369Sdduvall  */
30761369Sdduvall static int
30771369Sdduvall bge_add_intrs(bge_t *bgep, int	intr_type)
30781369Sdduvall {
30791369Sdduvall 	dev_info_t	*dip = bgep->devinfo;
30801369Sdduvall 	int		avail, actual, intr_size, count = 0;
30811369Sdduvall 	int		i, flag, ret;
30821369Sdduvall 
30832675Szh199473 	BGE_DEBUG(("bge_add_intrs($%p, 0x%x)", (void *)bgep, intr_type));
30841369Sdduvall 
30851369Sdduvall 	/* Get number of interrupts */
30861369Sdduvall 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
30871369Sdduvall 	if ((ret != DDI_SUCCESS) || (count == 0)) {
30881369Sdduvall 		bge_error(bgep, "ddi_intr_get_nintrs() failure, ret: %d, "
30891369Sdduvall 		    "count: %d", ret, count);
30901369Sdduvall 
30911369Sdduvall 		return (DDI_FAILURE);
30921369Sdduvall 	}
30931369Sdduvall 
30941369Sdduvall 	/* Get number of available interrupts */
30951369Sdduvall 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
30961369Sdduvall 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
30971369Sdduvall 		bge_error(bgep, "ddi_intr_get_navail() failure, "
30981369Sdduvall 		    "ret: %d, avail: %d\n", ret, avail);
30991369Sdduvall 
31001369Sdduvall 		return (DDI_FAILURE);
31011369Sdduvall 	}
31021369Sdduvall 
31031369Sdduvall 	if (avail < count) {
31042675Szh199473 		BGE_DEBUG(("%s: nintrs() returned %d, navail returned %d",
31052675Szh199473 		    bgep->ifname, count, avail));
31061369Sdduvall 	}
31071369Sdduvall 
31081369Sdduvall 	/*
31091369Sdduvall 	 * BGE hardware generates only single MSI even though it claims
31101369Sdduvall 	 * to support multiple MSIs. So, hard code MSI count value to 1.
31111369Sdduvall 	 */
31121369Sdduvall 	if (intr_type == DDI_INTR_TYPE_MSI) {
31131369Sdduvall 		count = 1;
31141369Sdduvall 		flag = DDI_INTR_ALLOC_STRICT;
31151369Sdduvall 	} else {
31161369Sdduvall 		flag = DDI_INTR_ALLOC_NORMAL;
31171369Sdduvall 	}
31181369Sdduvall 
31191369Sdduvall 	/* Allocate an array of interrupt handles */
31201369Sdduvall 	intr_size = count * sizeof (ddi_intr_handle_t);
31211369Sdduvall 	bgep->htable = kmem_alloc(intr_size, KM_SLEEP);
31221369Sdduvall 
31231369Sdduvall 	/* Call ddi_intr_alloc() */
31241369Sdduvall 	ret = ddi_intr_alloc(dip, bgep->htable, intr_type, 0,
31251369Sdduvall 	    count, &actual, flag);
31261369Sdduvall 
31271369Sdduvall 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
31281369Sdduvall 		bge_error(bgep, "ddi_intr_alloc() failed %d\n", ret);
31291369Sdduvall 
31301369Sdduvall 		kmem_free(bgep->htable, intr_size);
31311369Sdduvall 		return (DDI_FAILURE);
31321369Sdduvall 	}
31331369Sdduvall 
31341369Sdduvall 	if (actual < count) {
31352675Szh199473 		BGE_DEBUG(("%s: Requested: %d, Received: %d",
31364588Sml149210 		    bgep->ifname, count, actual));
31371369Sdduvall 	}
31381369Sdduvall 
31391369Sdduvall 	bgep->intr_cnt = actual;
31401369Sdduvall 
31411369Sdduvall 	/*
31421369Sdduvall 	 * Get priority for first msi, assume remaining are all the same
31431369Sdduvall 	 */
31441369Sdduvall 	if ((ret = ddi_intr_get_pri(bgep->htable[0], &bgep->intr_pri)) !=
31451369Sdduvall 	    DDI_SUCCESS) {
31461369Sdduvall 		bge_error(bgep, "ddi_intr_get_pri() failed %d\n", ret);
31471369Sdduvall 
31481369Sdduvall 		/* Free already allocated intr */
31491369Sdduvall 		for (i = 0; i < actual; i++) {
31501369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
31511369Sdduvall 		}
31521369Sdduvall 
31531369Sdduvall 		kmem_free(bgep->htable, intr_size);
31541369Sdduvall 		return (DDI_FAILURE);
31551369Sdduvall 	}
31561369Sdduvall 
31571369Sdduvall 	/* Call ddi_intr_add_handler() */
31581369Sdduvall 	for (i = 0; i < actual; i++) {
31591369Sdduvall 		if ((ret = ddi_intr_add_handler(bgep->htable[i], bge_intr,
31601369Sdduvall 		    (caddr_t)bgep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
31611369Sdduvall 			bge_error(bgep, "ddi_intr_add_handler() "
31621369Sdduvall 			    "failed %d\n", ret);
31631369Sdduvall 
31641369Sdduvall 			/* Free already allocated intr */
31651369Sdduvall 			for (i = 0; i < actual; i++) {
31661369Sdduvall 				(void) ddi_intr_free(bgep->htable[i]);
31671369Sdduvall 			}
31681369Sdduvall 
31691369Sdduvall 			kmem_free(bgep->htable, intr_size);
31701369Sdduvall 			return (DDI_FAILURE);
31711369Sdduvall 		}
31721369Sdduvall 	}
31731369Sdduvall 
31741369Sdduvall 	if ((ret = ddi_intr_get_cap(bgep->htable[0], &bgep->intr_cap))
31754588Sml149210 	    != DDI_SUCCESS) {
31761369Sdduvall 		bge_error(bgep, "ddi_intr_get_cap() failed %d\n", ret);
31771369Sdduvall 
31781369Sdduvall 		for (i = 0; i < actual; i++) {
31791369Sdduvall 			(void) ddi_intr_remove_handler(bgep->htable[i]);
31801369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
31811369Sdduvall 		}
31821369Sdduvall 
31831369Sdduvall 		kmem_free(bgep->htable, intr_size);
31841369Sdduvall 		return (DDI_FAILURE);
31851369Sdduvall 	}
31861369Sdduvall 
31871369Sdduvall 	return (DDI_SUCCESS);
31881369Sdduvall }
31891369Sdduvall 
31901369Sdduvall /*
31911369Sdduvall  * bge_rem_intrs:
31921369Sdduvall  *
31931369Sdduvall  * Unregister FIXED or MSI interrupts
31941369Sdduvall  */
31951369Sdduvall static void
31961369Sdduvall bge_rem_intrs(bge_t *bgep)
31971369Sdduvall {
31981369Sdduvall 	int	i;
31991369Sdduvall 
32002675Szh199473 	BGE_DEBUG(("bge_rem_intrs($%p)", (void *)bgep));
32011369Sdduvall 
32021865Sdilpreet 	/* Call ddi_intr_remove_handler() */
32031865Sdilpreet 	for (i = 0; i < bgep->intr_cnt; i++) {
32041865Sdilpreet 		(void) ddi_intr_remove_handler(bgep->htable[i]);
32051865Sdilpreet 		(void) ddi_intr_free(bgep->htable[i]);
32061865Sdilpreet 	}
32071865Sdilpreet 
32081865Sdilpreet 	kmem_free(bgep->htable, bgep->intr_cnt * sizeof (ddi_intr_handle_t));
32091865Sdilpreet }
32101865Sdilpreet 
32111865Sdilpreet 
32121865Sdilpreet void
32131865Sdilpreet bge_intr_enable(bge_t *bgep)
32141865Sdilpreet {
32151865Sdilpreet 	int i;
32161865Sdilpreet 
32171865Sdilpreet 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
32181865Sdilpreet 		/* Call ddi_intr_block_enable() for MSI interrupts */
32191865Sdilpreet 		(void) ddi_intr_block_enable(bgep->htable, bgep->intr_cnt);
32201865Sdilpreet 	} else {
32211865Sdilpreet 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
32221865Sdilpreet 		for (i = 0; i < bgep->intr_cnt; i++) {
32231865Sdilpreet 			(void) ddi_intr_enable(bgep->htable[i]);
32241865Sdilpreet 		}
32251865Sdilpreet 	}
32261865Sdilpreet }
32271865Sdilpreet 
32281865Sdilpreet 
32291865Sdilpreet void
32301865Sdilpreet bge_intr_disable(bge_t *bgep)
32311865Sdilpreet {
32321865Sdilpreet 	int i;
32331865Sdilpreet 
32341369Sdduvall 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
32351369Sdduvall 		/* Call ddi_intr_block_disable() */
32361369Sdduvall 		(void) ddi_intr_block_disable(bgep->htable, bgep->intr_cnt);
32371369Sdduvall 	} else {
32381369Sdduvall 		for (i = 0; i < bgep->intr_cnt; i++) {
32391369Sdduvall 			(void) ddi_intr_disable(bgep->htable[i]);
32401369Sdduvall 		}
32411369Sdduvall 	}
32421369Sdduvall }
3243