xref: /onnv-gate/usr/src/uts/common/io/bge/bge_main2.c (revision 11236:1127b4f9e96b)
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 /*
238922SYong.Tan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
241369Sdduvall  * Use is subject to license terms.
251369Sdduvall  */
261369Sdduvall 
272675Szh199473 #include "bge_impl.h"
281369Sdduvall #include <sys/sdt.h>
298275SEric Cheng #include <sys/mac_provider.h>
306789Sam223141 #include <sys/mac.h>
318275SEric Cheng #include <sys/mac_flow.h>
321369Sdduvall 
331369Sdduvall /*
341369Sdduvall  * This is the string displayed by modinfo, etc.
358922SYong.Tan@Sun.COM  */
368922SYong.Tan@Sun.COM static char bge_ident[] = "Broadcom Gb Ethernet";
378922SYong.Tan@Sun.COM /*
381369Sdduvall  * Make sure you keep the version ID up to date!
391369Sdduvall  */
4011065SRiz.Nadaf@Sun.COM static char bge_version[] = "Broadcom Gb Ethernet v1.13";
411369Sdduvall 
421369Sdduvall /*
431369Sdduvall  * Property names
441369Sdduvall  */
451369Sdduvall static char debug_propname[] = "bge-debug-flags";
461369Sdduvall static char clsize_propname[] = "cache-line-size";
471369Sdduvall static char latency_propname[] = "latency-timer";
481369Sdduvall static char localmac_boolname[] = "local-mac-address?";
491369Sdduvall static char localmac_propname[] = "local-mac-address";
501369Sdduvall static char macaddr_propname[] = "mac-address";
511369Sdduvall static char subdev_propname[] = "subsystem-id";
521369Sdduvall static char subven_propname[] = "subsystem-vendor-id";
531369Sdduvall static char rxrings_propname[] = "bge-rx-rings";
541369Sdduvall static char txrings_propname[] = "bge-tx-rings";
551865Sdilpreet static char fm_cap[] = "fm-capable";
561908Sly149593 static char default_mtu[] = "default_mtu";
571369Sdduvall 
581369Sdduvall static int bge_add_intrs(bge_t *, int);
591369Sdduvall static void bge_rem_intrs(bge_t *);
608275SEric Cheng static int bge_unicst_set(void *, const uint8_t *, int);
611369Sdduvall 
621369Sdduvall /*
631369Sdduvall  * Describes the chip's DMA engine
641369Sdduvall  */
651369Sdduvall static ddi_dma_attr_t dma_attr = {
661369Sdduvall 	DMA_ATTR_V0,			/* dma_attr version	*/
671369Sdduvall 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
681369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_addr_hi	*/
691369Sdduvall 	0x00000000FFFFFFFFull,		/* dma_attr_count_max	*/
701369Sdduvall 	0x0000000000000001ull,		/* dma_attr_align	*/
711369Sdduvall 	0x00000FFF,			/* dma_attr_burstsizes	*/
721369Sdduvall 	0x00000001,			/* dma_attr_minxfer	*/
731369Sdduvall 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
741369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_seg		*/
751369Sdduvall 	1,				/* dma_attr_sgllen 	*/
761369Sdduvall 	0x00000001,			/* dma_attr_granular 	*/
771865Sdilpreet 	DDI_DMA_FLAGERR			/* dma_attr_flags */
781369Sdduvall };
791369Sdduvall 
801369Sdduvall /*
811369Sdduvall  * PIO access attributes for registers
821369Sdduvall  */
831369Sdduvall static ddi_device_acc_attr_t bge_reg_accattr = {
84*11236SStephen.Hanson@Sun.COM 	DDI_DEVICE_ATTR_V1,
851369Sdduvall 	DDI_NEVERSWAP_ACC,
861865Sdilpreet 	DDI_STRICTORDER_ACC,
871865Sdilpreet 	DDI_FLAGERR_ACC
881369Sdduvall };
891369Sdduvall 
901369Sdduvall /*
911369Sdduvall  * DMA access attributes for descriptors: NOT to be byte swapped.
921369Sdduvall  */
931369Sdduvall static ddi_device_acc_attr_t bge_desc_accattr = {
941369Sdduvall 	DDI_DEVICE_ATTR_V0,
951369Sdduvall 	DDI_NEVERSWAP_ACC,
96*11236SStephen.Hanson@Sun.COM 	DDI_STRICTORDER_ACC
971369Sdduvall };
981369Sdduvall 
991369Sdduvall /*
1001369Sdduvall  * DMA access attributes for data: NOT to be byte swapped.
1011369Sdduvall  */
1021369Sdduvall static ddi_device_acc_attr_t bge_data_accattr = {
1031369Sdduvall 	DDI_DEVICE_ATTR_V0,
1041369Sdduvall 	DDI_NEVERSWAP_ACC,
1051369Sdduvall 	DDI_STRICTORDER_ACC
1061369Sdduvall };
1071369Sdduvall 
1082311Sseb static int		bge_m_start(void *);
1092311Sseb static void		bge_m_stop(void *);
1102311Sseb static int		bge_m_promisc(void *, boolean_t);
1112311Sseb static int		bge_m_multicst(void *, boolean_t, const uint8_t *);
1122311Sseb static void		bge_m_ioctl(void *, queue_t *, mblk_t *);
1132311Sseb static boolean_t	bge_m_getcapab(void *, mac_capab_t, void *);
1142331Skrgopi static int		bge_unicst_set(void *, const uint8_t *,
1158275SEric Cheng     int);
1165903Ssowmini static int		bge_m_setprop(void *, const char *, mac_prop_id_t,
1175903Ssowmini     uint_t, const void *);
1185903Ssowmini static int		bge_m_getprop(void *, const char *, mac_prop_id_t,
1198118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
1205903Ssowmini static int		bge_set_priv_prop(bge_t *, const char *, uint_t,
1215903Ssowmini     const void *);
1225903Ssowmini static int		bge_get_priv_prop(bge_t *, const char *, uint_t,
1236512Ssowmini     uint_t, void *);
1245903Ssowmini 
1258275SEric Cheng #define	BGE_M_CALLBACK_FLAGS (MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
1262311Sseb 
1272311Sseb static mac_callbacks_t bge_m_callbacks = {
1282311Sseb 	BGE_M_CALLBACK_FLAGS,
1292311Sseb 	bge_m_stat,
1302311Sseb 	bge_m_start,
1312311Sseb 	bge_m_stop,
1322311Sseb 	bge_m_promisc,
1332311Sseb 	bge_m_multicst,
1348275SEric Cheng 	NULL,
1352311Sseb 	bge_m_tx,
1362311Sseb 	bge_m_ioctl,
1375903Ssowmini 	bge_m_getcapab,
1385903Ssowmini 	NULL,
1395903Ssowmini 	NULL,
1405903Ssowmini 	bge_m_setprop,
1415903Ssowmini 	bge_m_getprop
1422311Sseb };
1432311Sseb 
1446512Ssowmini mac_priv_prop_t bge_priv_prop[] = {
1456512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_RW},
1466512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_RW}
1476512Ssowmini };
1486512Ssowmini 
1496512Ssowmini #define	BGE_MAX_PRIV_PROPS \
1506512Ssowmini 	(sizeof (bge_priv_prop) / sizeof (mac_priv_prop_t))
1516512Ssowmini 
1528275SEric Cheng uint8_t zero_addr[6] = {0, 0, 0, 0, 0, 0};
1531369Sdduvall /*
1541369Sdduvall  * ========== Transmit and receive ring reinitialisation ==========
1551369Sdduvall  */
1561369Sdduvall 
1571369Sdduvall /*
1581369Sdduvall  * These <reinit> routines each reset the specified ring to an initial
1591369Sdduvall  * state, assuming that the corresponding <init> routine has already
1601369Sdduvall  * been called exactly once.
1611369Sdduvall  */
1621369Sdduvall 
1631369Sdduvall static void
1641369Sdduvall bge_reinit_send_ring(send_ring_t *srp)
1651369Sdduvall {
1663334Sgs150176 	bge_queue_t *txbuf_queue;
1673334Sgs150176 	bge_queue_item_t *txbuf_head;
1683334Sgs150176 	sw_txbuf_t *txbuf;
1693334Sgs150176 	sw_sbd_t *ssbdp;
1703334Sgs150176 	uint32_t slot;
1713334Sgs150176 
1721369Sdduvall 	/*
1731369Sdduvall 	 * Reinitialise control variables ...
1741369Sdduvall 	 */
1753334Sgs150176 	srp->tx_flow = 0;
1761369Sdduvall 	srp->tx_next = 0;
1773334Sgs150176 	srp->txfill_next = 0;
1781369Sdduvall 	srp->tx_free = srp->desc.nslots;
1791369Sdduvall 	ASSERT(mutex_owned(srp->tc_lock));
1801369Sdduvall 	srp->tc_next = 0;
1813334Sgs150176 	srp->txpkt_next = 0;
1823334Sgs150176 	srp->tx_block = 0;
1833334Sgs150176 	srp->tx_nobd = 0;
1843334Sgs150176 	srp->tx_nobuf = 0;
1853334Sgs150176 
1863334Sgs150176 	/*
1873334Sgs150176 	 * Initialize the tx buffer push queue
1883334Sgs150176 	 */
1893334Sgs150176 	mutex_enter(srp->freetxbuf_lock);
1903334Sgs150176 	mutex_enter(srp->txbuf_lock);
1913334Sgs150176 	txbuf_queue = &srp->freetxbuf_queue;
1923334Sgs150176 	txbuf_queue->head = NULL;
1933334Sgs150176 	txbuf_queue->count = 0;
1943334Sgs150176 	txbuf_queue->lock = srp->freetxbuf_lock;
1953334Sgs150176 	srp->txbuf_push_queue = txbuf_queue;
1963334Sgs150176 
1973334Sgs150176 	/*
1983334Sgs150176 	 * Initialize the tx buffer pop queue
1993334Sgs150176 	 */
2003334Sgs150176 	txbuf_queue = &srp->txbuf_queue;
2013334Sgs150176 	txbuf_queue->head = NULL;
2023334Sgs150176 	txbuf_queue->count = 0;
2033334Sgs150176 	txbuf_queue->lock = srp->txbuf_lock;
2043334Sgs150176 	srp->txbuf_pop_queue = txbuf_queue;
2053334Sgs150176 	txbuf_head = srp->txbuf_head;
2063334Sgs150176 	txbuf = srp->txbuf;
2073334Sgs150176 	for (slot = 0; slot < srp->tx_buffers; ++slot) {
2083334Sgs150176 		txbuf_head->item = txbuf;
2093334Sgs150176 		txbuf_head->next = txbuf_queue->head;
2103334Sgs150176 		txbuf_queue->head = txbuf_head;
2113334Sgs150176 		txbuf_queue->count++;
2123334Sgs150176 		txbuf++;
2133334Sgs150176 		txbuf_head++;
2143334Sgs150176 	}
2153334Sgs150176 	mutex_exit(srp->txbuf_lock);
2163334Sgs150176 	mutex_exit(srp->freetxbuf_lock);
2171369Sdduvall 
2181369Sdduvall 	/*
2191369Sdduvall 	 * Zero and sync all the h/w Send Buffer Descriptors
2201369Sdduvall 	 */
2211369Sdduvall 	DMA_ZERO(srp->desc);
2221369Sdduvall 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
2233334Sgs150176 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
2243334Sgs150176 	ssbdp = srp->sw_sbds;
2253334Sgs150176 	for (slot = 0; slot < srp->desc.nslots; ++ssbdp, ++slot)
2263334Sgs150176 		ssbdp->pbuf = NULL;
2271369Sdduvall }
2281369Sdduvall 
2291369Sdduvall static void
2301369Sdduvall bge_reinit_recv_ring(recv_ring_t *rrp)
2311369Sdduvall {
2321369Sdduvall 	/*
2331369Sdduvall 	 * Reinitialise control variables ...
2341369Sdduvall 	 */
2351369Sdduvall 	rrp->rx_next = 0;
2361369Sdduvall }
2371369Sdduvall 
2381369Sdduvall static void
2393334Sgs150176 bge_reinit_buff_ring(buff_ring_t *brp, uint32_t ring)
2401369Sdduvall {
2411369Sdduvall 	bge_rbd_t *hw_rbd_p;
2421369Sdduvall 	sw_rbd_t *srbdp;
2431369Sdduvall 	uint32_t bufsize;
2441369Sdduvall 	uint32_t nslots;
2451369Sdduvall 	uint32_t slot;
2461369Sdduvall 
2471369Sdduvall 	static uint16_t ring_type_flag[BGE_BUFF_RINGS_MAX] = {
2481369Sdduvall 		RBD_FLAG_STD_RING,
2491369Sdduvall 		RBD_FLAG_JUMBO_RING,
2501369Sdduvall 		RBD_FLAG_MINI_RING
2511369Sdduvall 	};
2521369Sdduvall 
2531369Sdduvall 	/*
2541369Sdduvall 	 * Zero, initialise and sync all the h/w Receive Buffer Descriptors
2551369Sdduvall 	 * Note: all the remaining fields (<type>, <flags>, <ip_cksum>,
2561369Sdduvall 	 * <tcp_udp_cksum>, <error_flag>, <vlan_tag>, and <reserved>)
2571369Sdduvall 	 * should be zeroed, and so don't need to be set up specifically
2581369Sdduvall 	 * once the whole area has been cleared.
2591369Sdduvall 	 */
2601369Sdduvall 	DMA_ZERO(brp->desc);
2611369Sdduvall 
2621369Sdduvall 	hw_rbd_p = DMA_VPTR(brp->desc);
2631369Sdduvall 	nslots = brp->desc.nslots;
2641369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
2651369Sdduvall 	bufsize = brp->buf[0].size;
2661369Sdduvall 	srbdp = brp->sw_rbds;
2671369Sdduvall 	for (slot = 0; slot < nslots; ++hw_rbd_p, ++srbdp, ++slot) {
2681369Sdduvall 		hw_rbd_p->host_buf_addr = srbdp->pbuf.cookie.dmac_laddress;
2697099Syt223700 		hw_rbd_p->index = (uint16_t)slot;
2707099Syt223700 		hw_rbd_p->len = (uint16_t)bufsize;
2711369Sdduvall 		hw_rbd_p->opaque = srbdp->pbuf.token;
2721369Sdduvall 		hw_rbd_p->flags |= ring_type_flag[ring];
2731369Sdduvall 	}
2741369Sdduvall 
2751369Sdduvall 	DMA_SYNC(brp->desc, DDI_DMA_SYNC_FORDEV);
2761369Sdduvall 
2771369Sdduvall 	/*
2781369Sdduvall 	 * Finally, reinitialise the ring control variables ...
2791369Sdduvall 	 */
2801369Sdduvall 	brp->rf_next = (nslots != 0) ? (nslots-1) : 0;
2811369Sdduvall }
2821369Sdduvall 
2831369Sdduvall /*
2841369Sdduvall  * Reinitialize all rings
2851369Sdduvall  */
2861369Sdduvall static void
2871369Sdduvall bge_reinit_rings(bge_t *bgep)
2881369Sdduvall {
2893334Sgs150176 	uint32_t ring;
2901369Sdduvall 
2911369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
2921369Sdduvall 
2931369Sdduvall 	/*
2941369Sdduvall 	 * Send Rings ...
2951369Sdduvall 	 */
2961369Sdduvall 	for (ring = 0; ring < bgep->chipid.tx_rings; ++ring)
2971369Sdduvall 		bge_reinit_send_ring(&bgep->send[ring]);
2981369Sdduvall 
2991369Sdduvall 	/*
3001369Sdduvall 	 * Receive Return Rings ...
3011369Sdduvall 	 */
3021369Sdduvall 	for (ring = 0; ring < bgep->chipid.rx_rings; ++ring)
3031369Sdduvall 		bge_reinit_recv_ring(&bgep->recv[ring]);
3041369Sdduvall 
3051369Sdduvall 	/*
3061369Sdduvall 	 * Receive Producer Rings ...
3071369Sdduvall 	 */
3081369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
3091369Sdduvall 		bge_reinit_buff_ring(&bgep->buff[ring], ring);
3101369Sdduvall }
3111369Sdduvall 
3121369Sdduvall /*
3131369Sdduvall  * ========== Internal state management entry points ==========
3141369Sdduvall  */
3151369Sdduvall 
3161369Sdduvall #undef	BGE_DBG
3171369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
3181369Sdduvall 
3191369Sdduvall /*
3201369Sdduvall  * These routines provide all the functionality required by the
3211369Sdduvall  * corresponding GLD entry points, but don't update the GLD state
3221369Sdduvall  * so they can be called internally without disturbing our record
3231369Sdduvall  * of what GLD thinks we should be doing ...
3241369Sdduvall  */
3251369Sdduvall 
3261369Sdduvall /*
3271369Sdduvall  *	bge_reset() -- reset h/w & rings to initial state
3281369Sdduvall  */
3291865Sdilpreet static int
3301408Srandyf #ifdef BGE_IPMI_ASF
3311408Srandyf bge_reset(bge_t *bgep, uint_t asf_mode)
3321408Srandyf #else
3331369Sdduvall bge_reset(bge_t *bgep)
3341408Srandyf #endif
3351369Sdduvall {
3363334Sgs150176 	uint32_t	ring;
3371865Sdilpreet 	int retval;
3381369Sdduvall 
3391369Sdduvall 	BGE_TRACE(("bge_reset($%p)", (void *)bgep));
3401369Sdduvall 
3411369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3421369Sdduvall 
3431369Sdduvall 	/*
3441369Sdduvall 	 * Grab all the other mutexes in the world (this should
3451369Sdduvall 	 * ensure no other threads are manipulating driver state)
3461369Sdduvall 	 */
3471369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
3481369Sdduvall 		mutex_enter(bgep->recv[ring].rx_lock);
3491369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
3501369Sdduvall 		mutex_enter(bgep->buff[ring].rf_lock);
3511369Sdduvall 	rw_enter(bgep->errlock, RW_WRITER);
3521369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3533334Sgs150176 		mutex_enter(bgep->send[ring].tx_lock);
3543334Sgs150176 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3551369Sdduvall 		mutex_enter(bgep->send[ring].tc_lock);
3561369Sdduvall 
3571408Srandyf #ifdef BGE_IPMI_ASF
3581865Sdilpreet 	retval = bge_chip_reset(bgep, B_TRUE, asf_mode);
3591408Srandyf #else
3601865Sdilpreet 	retval = bge_chip_reset(bgep, B_TRUE);
3611408Srandyf #endif
3621369Sdduvall 	bge_reinit_rings(bgep);
3631369Sdduvall 
3641369Sdduvall 	/*
3651369Sdduvall 	 * Free the world ...
3661369Sdduvall 	 */
3671369Sdduvall 	for (ring = BGE_SEND_RINGS_MAX; ring-- > 0; )
3681369Sdduvall 		mutex_exit(bgep->send[ring].tc_lock);
3693334Sgs150176 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
3703334Sgs150176 		mutex_exit(bgep->send[ring].tx_lock);
3711369Sdduvall 	rw_exit(bgep->errlock);
3721369Sdduvall 	for (ring = BGE_BUFF_RINGS_MAX; ring-- > 0; )
3731369Sdduvall 		mutex_exit(bgep->buff[ring].rf_lock);
3741369Sdduvall 	for (ring = BGE_RECV_RINGS_MAX; ring-- > 0; )
3751369Sdduvall 		mutex_exit(bgep->recv[ring].rx_lock);
3761369Sdduvall 
3771369Sdduvall 	BGE_DEBUG(("bge_reset($%p) done", (void *)bgep));
3781865Sdilpreet 	return (retval);
3791369Sdduvall }
3801369Sdduvall 
3811369Sdduvall /*
3821369Sdduvall  *	bge_stop() -- stop processing, don't reset h/w or rings
3831369Sdduvall  */
3841369Sdduvall static void
3851369Sdduvall bge_stop(bge_t *bgep)
3861369Sdduvall {
3871369Sdduvall 	BGE_TRACE(("bge_stop($%p)", (void *)bgep));
3881369Sdduvall 
3891369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3901369Sdduvall 
3911408Srandyf #ifdef BGE_IPMI_ASF
3921408Srandyf 	if (bgep->asf_enabled) {
3931408Srandyf 		bgep->asf_pseudostop = B_TRUE;
3941408Srandyf 	} else {
3951408Srandyf #endif
3961408Srandyf 		bge_chip_stop(bgep, B_FALSE);
3971408Srandyf #ifdef BGE_IPMI_ASF
3981408Srandyf 	}
3991408Srandyf #endif
4001369Sdduvall 
4011369Sdduvall 	BGE_DEBUG(("bge_stop($%p) done", (void *)bgep));
4021369Sdduvall }
4031369Sdduvall 
4041369Sdduvall /*
4051369Sdduvall  *	bge_start() -- start transmitting/receiving
4061369Sdduvall  */
4071865Sdilpreet static int
4081369Sdduvall bge_start(bge_t *bgep, boolean_t reset_phys)
4091369Sdduvall {
4101865Sdilpreet 	int retval;
4111865Sdilpreet 
4121369Sdduvall 	BGE_TRACE(("bge_start($%p, %d)", (void *)bgep, reset_phys));
4131369Sdduvall 
4141369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
4151369Sdduvall 
4161369Sdduvall 	/*
4171369Sdduvall 	 * Start chip processing, including enabling interrupts
4181369Sdduvall 	 */
4191865Sdilpreet 	retval = bge_chip_start(bgep, reset_phys);
4201369Sdduvall 
4211369Sdduvall 	BGE_DEBUG(("bge_start($%p, %d) done", (void *)bgep, reset_phys));
4221865Sdilpreet 	return (retval);
4231369Sdduvall }
4241369Sdduvall 
4251369Sdduvall /*
4261369Sdduvall  * bge_restart - restart transmitting/receiving after error or suspend
4271369Sdduvall  */
4281865Sdilpreet int
4291369Sdduvall bge_restart(bge_t *bgep, boolean_t reset_phys)
4301369Sdduvall {
4311865Sdilpreet 	int retval = DDI_SUCCESS;
4321369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
4331369Sdduvall 
4341408Srandyf #ifdef BGE_IPMI_ASF
4351408Srandyf 	if (bgep->asf_enabled) {
4361865Sdilpreet 		if (bge_reset(bgep, ASF_MODE_POST_INIT) != DDI_SUCCESS)
4371865Sdilpreet 			retval = DDI_FAILURE;
4381408Srandyf 	} else
4391865Sdilpreet 		if (bge_reset(bgep, ASF_MODE_NONE) != DDI_SUCCESS)
4401865Sdilpreet 			retval = DDI_FAILURE;
4411408Srandyf #else
4421865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS)
4431865Sdilpreet 		retval = DDI_FAILURE;
4441408Srandyf #endif
4453440Szh199473 	if (bgep->bge_mac_state == BGE_MAC_STARTED) {
4461865Sdilpreet 		if (bge_start(bgep, reset_phys) != DDI_SUCCESS)
4471865Sdilpreet 			retval = DDI_FAILURE;
4481369Sdduvall 		bgep->watchdog = 0;
4493334Sgs150176 		ddi_trigger_softintr(bgep->drain_id);
4501369Sdduvall 	}
4511369Sdduvall 
4521369Sdduvall 	BGE_DEBUG(("bge_restart($%p, %d) done", (void *)bgep, reset_phys));
4531865Sdilpreet 	return (retval);
4541369Sdduvall }
4551369Sdduvall 
4561369Sdduvall 
4571369Sdduvall /*
4581369Sdduvall  * ========== Nemo-required management entry points ==========
4591369Sdduvall  */
4601369Sdduvall 
4611369Sdduvall #undef	BGE_DBG
4621369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
4631369Sdduvall 
4641369Sdduvall /*
4651369Sdduvall  *	bge_m_stop() -- stop transmitting/receiving
4661369Sdduvall  */
4671369Sdduvall static void
4681369Sdduvall bge_m_stop(void *arg)
4691369Sdduvall {
4701369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
4713334Sgs150176 	send_ring_t *srp;
4723334Sgs150176 	uint32_t ring;
4731369Sdduvall 
4741369Sdduvall 	BGE_TRACE(("bge_m_stop($%p)", arg));
4751369Sdduvall 
4761369Sdduvall 	/*
4771369Sdduvall 	 * Just stop processing, then record new GLD state
4781369Sdduvall 	 */
4791369Sdduvall 	mutex_enter(bgep->genlock);
4801865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
4811865Sdilpreet 		/* can happen during autorecovery */
4828922SYong.Tan@Sun.COM 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
4838922SYong.Tan@Sun.COM 	} else
4848922SYong.Tan@Sun.COM 		bge_stop(bgep);
4856546Sgh162552 
4866546Sgh162552 	bgep->link_update_timer = 0;
4876546Sgh162552 	bgep->link_state = LINK_STATE_UNKNOWN;
4886546Sgh162552 	mac_link_update(bgep->mh, bgep->link_state);
4896546Sgh162552 
4903334Sgs150176 	/*
4913334Sgs150176 	 * Free the possible tx buffers allocated in tx process.
4923334Sgs150176 	 */
4933334Sgs150176 #ifdef BGE_IPMI_ASF
4943334Sgs150176 	if (!bgep->asf_pseudostop)
4953334Sgs150176 #endif
4963334Sgs150176 	{
4973334Sgs150176 		rw_enter(bgep->errlock, RW_WRITER);
4983334Sgs150176 		for (ring = 0; ring < bgep->chipid.tx_rings; ++ring) {
4993334Sgs150176 			srp = &bgep->send[ring];
5003334Sgs150176 			mutex_enter(srp->tx_lock);
5013334Sgs150176 			if (srp->tx_array > 1)
5023334Sgs150176 				bge_free_txbuf_arrays(srp);
5033334Sgs150176 			mutex_exit(srp->tx_lock);
5043334Sgs150176 		}
5053334Sgs150176 		rw_exit(bgep->errlock);
5063334Sgs150176 	}
5071369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STOPPED;
5081369Sdduvall 	BGE_DEBUG(("bge_m_stop($%p) done", arg));
5091865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
5101865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
5111369Sdduvall 	mutex_exit(bgep->genlock);
5121369Sdduvall }
5131369Sdduvall 
5141369Sdduvall /*
5151369Sdduvall  *	bge_m_start() -- start transmitting/receiving
5161369Sdduvall  */
5171369Sdduvall static int
5181369Sdduvall bge_m_start(void *arg)
5191369Sdduvall {
5201369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
5211369Sdduvall 
5221369Sdduvall 	BGE_TRACE(("bge_m_start($%p)", arg));
5231369Sdduvall 
5241369Sdduvall 	/*
5251369Sdduvall 	 * Start processing and record new GLD state
5261369Sdduvall 	 */
5271369Sdduvall 	mutex_enter(bgep->genlock);
5281865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
5291865Sdilpreet 		/* can happen during autorecovery */
5301865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5311865Sdilpreet 		mutex_exit(bgep->genlock);
5321865Sdilpreet 		return (EIO);
5331865Sdilpreet 	}
5341408Srandyf #ifdef BGE_IPMI_ASF
5351408Srandyf 	if (bgep->asf_enabled) {
5361408Srandyf 		if ((bgep->asf_status == ASF_STAT_RUN) &&
5374588Sml149210 		    (bgep->asf_pseudostop)) {
5381408Srandyf 			bgep->bge_mac_state = BGE_MAC_STARTED;
5391408Srandyf 			mutex_exit(bgep->genlock);
5401408Srandyf 			return (0);
5411408Srandyf 		}
5421408Srandyf 	}
5431865Sdilpreet 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
5441408Srandyf #else
5451865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS) {
5461408Srandyf #endif
5471865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
5481865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
5491865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5501865Sdilpreet 		mutex_exit(bgep->genlock);
5511865Sdilpreet 		return (EIO);
5521865Sdilpreet 	}
5531865Sdilpreet 	if (bge_start(bgep, B_TRUE) != DDI_SUCCESS) {
5541865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
5551865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
5561865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5571865Sdilpreet 		mutex_exit(bgep->genlock);
5581865Sdilpreet 		return (EIO);
5591865Sdilpreet 	}
5609918SYong.Tan@Sun.COM 	bgep->watchdog = 0;
5611369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STARTED;
5621369Sdduvall 	BGE_DEBUG(("bge_m_start($%p) done", arg));
5631408Srandyf 
5641865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
5651865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5661865Sdilpreet 		mutex_exit(bgep->genlock);
5671865Sdilpreet 		return (EIO);
5681865Sdilpreet 	}
5691865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
5701865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5711865Sdilpreet 		mutex_exit(bgep->genlock);
5721865Sdilpreet 		return (EIO);
5731865Sdilpreet 	}
5741408Srandyf #ifdef BGE_IPMI_ASF
5751408Srandyf 	if (bgep->asf_enabled) {
5761408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
5771408Srandyf 			/* start ASF heart beat */
5781408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
5794588Sml149210 			    (void *)bgep,
5804588Sml149210 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
5811408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
5821408Srandyf 		}
5831408Srandyf 	}
5841408Srandyf #endif
5851369Sdduvall 	mutex_exit(bgep->genlock);
5861369Sdduvall 
5871369Sdduvall 	return (0);
5881369Sdduvall }
5891369Sdduvall 
5901369Sdduvall /*
5912331Skrgopi  *	bge_unicst_set() -- set the physical network address
5922331Skrgopi  */
5932331Skrgopi static int
5948275SEric Cheng bge_unicst_set(void *arg, const uint8_t *macaddr, int slot)
5952331Skrgopi {
5961369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
5971369Sdduvall 
5981369Sdduvall 	BGE_TRACE(("bge_m_unicst_set($%p, %s)", arg,
5994588Sml149210 	    ether_sprintf((void *)macaddr)));
6001369Sdduvall 	/*
6011369Sdduvall 	 * Remember the new current address in the driver state
6021369Sdduvall 	 * Sync the chip's idea of the address too ...
6031369Sdduvall 	 */
6041369Sdduvall 	mutex_enter(bgep->genlock);
6051865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
6061865Sdilpreet 		/* can happen during autorecovery */
6071865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6081865Sdilpreet 		mutex_exit(bgep->genlock);
6091865Sdilpreet 		return (EIO);
6101865Sdilpreet 	}
6112331Skrgopi 	ethaddr_copy(macaddr, bgep->curr_addr[slot].addr);
6121408Srandyf #ifdef BGE_IPMI_ASF
6131865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE) {
6141865Sdilpreet #else
6151865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
6161865Sdilpreet #endif
6171865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
6181865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
6191865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6201865Sdilpreet 		mutex_exit(bgep->genlock);
6211865Sdilpreet 		return (EIO);
6221865Sdilpreet 	}
6231865Sdilpreet #ifdef BGE_IPMI_ASF
6241408Srandyf 	if (bgep->asf_enabled) {
6251408Srandyf 		/*
6261408Srandyf 		 * The above bge_chip_sync() function wrote the ethernet MAC
6271408Srandyf 		 * addresses registers which destroyed the IPMI/ASF sideband.
6281408Srandyf 		 * Here, we have to reset chip to make IPMI/ASF sideband work.
6291408Srandyf 		 */
6301408Srandyf 		if (bgep->asf_status == ASF_STAT_RUN) {
6311408Srandyf 			/*
6321408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
6331408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
6341408Srandyf 			 * may crash.
6351408Srandyf 			 */
6361408Srandyf 			bge_asf_update_status(bgep);
6371408Srandyf 			bge_asf_stop_timer(bgep);
6381408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
6391408Srandyf 
6401408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
6411408Srandyf 		}
6421865Sdilpreet 		bge_chip_stop(bgep, B_FALSE);
6431408Srandyf 
6441865Sdilpreet 		if (bge_restart(bgep, B_FALSE) == DDI_FAILURE) {
6451865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
6461865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->io_handle);
6471865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
6481865Sdilpreet 			    DDI_SERVICE_DEGRADED);
6491865Sdilpreet 			mutex_exit(bgep->genlock);
6501865Sdilpreet 			return (EIO);
6511865Sdilpreet 		}
6521865Sdilpreet 
6531408Srandyf 		/*
6541408Srandyf 		 * Start our ASF heartbeat counter as soon as possible.
6551408Srandyf 		 */
6561408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
6571408Srandyf 			/* start ASF heart beat */
6581408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
6594588Sml149210 			    (void *)bgep,
6604588Sml149210 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
6611408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
6621408Srandyf 		}
6631408Srandyf 	}
6641408Srandyf #endif
6651369Sdduvall 	BGE_DEBUG(("bge_m_unicst_set($%p) done", arg));
6661865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
6671865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6681865Sdilpreet 		mutex_exit(bgep->genlock);
6691865Sdilpreet 		return (EIO);
6701865Sdilpreet 	}
6711865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
6721865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
6731865Sdilpreet 		mutex_exit(bgep->genlock);
6741865Sdilpreet 		return (EIO);
6751865Sdilpreet 	}
6761369Sdduvall 	mutex_exit(bgep->genlock);
6771369Sdduvall 
6781369Sdduvall 	return (0);
6791369Sdduvall }
6801369Sdduvall 
6815903Ssowmini extern void bge_wake_factotum(bge_t *);
6825903Ssowmini 
6835903Ssowmini static boolean_t
6845903Ssowmini bge_param_locked(mac_prop_id_t pr_num)
6855903Ssowmini {
6865903Ssowmini 	/*
6875903Ssowmini 	 * All adv_* parameters are locked (read-only) while
6885903Ssowmini 	 * the device is in any sort of loopback mode ...
6895903Ssowmini 	 */
6905903Ssowmini 	switch (pr_num) {
6916789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
6926789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
6936789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
6946789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
6956789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
6966789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
6976789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
6986789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
6996789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
7006789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
7016789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
7026789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
7036789Sam223141 		case MAC_PROP_AUTONEG:
7046789Sam223141 		case MAC_PROP_FLOWCTRL:
7055903Ssowmini 			return (B_TRUE);
7065903Ssowmini 	}
7075903Ssowmini 	return (B_FALSE);
7085903Ssowmini }
7095903Ssowmini /*
7105903Ssowmini  * callback functions for set/get of properties
7115903Ssowmini  */
7125903Ssowmini static int
7135903Ssowmini bge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
7145903Ssowmini     uint_t pr_valsize, const void *pr_val)
7155903Ssowmini {
7165903Ssowmini 	bge_t *bgep = barg;
7175903Ssowmini 	int err = 0;
7186512Ssowmini 	uint32_t cur_mtu, new_mtu;
7195903Ssowmini 	uint_t	maxsdu;
7205903Ssowmini 	link_flowctrl_t fl;
7215903Ssowmini 
7225903Ssowmini 	mutex_enter(bgep->genlock);
7235903Ssowmini 	if (bgep->param_loop_mode != BGE_LOOP_NONE &&
7245903Ssowmini 	    bge_param_locked(pr_num)) {
7255903Ssowmini 		/*
7265903Ssowmini 		 * All adv_* parameters are locked (read-only)
7275903Ssowmini 		 * while the device is in any sort of loopback mode.
7285903Ssowmini 		 */
7295903Ssowmini 		mutex_exit(bgep->genlock);
7305903Ssowmini 		return (EBUSY);
7315903Ssowmini 	}
7326512Ssowmini 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
7336789Sam223141 	    ((pr_num == MAC_PROP_EN_100FDX_CAP) ||
7348118SVasumathi.Sundaram@Sun.COM 	    (pr_num == MAC_PROP_EN_100HDX_CAP) ||
7356789Sam223141 	    (pr_num == MAC_PROP_EN_10FDX_CAP) ||
7366789Sam223141 	    (pr_num == MAC_PROP_EN_10HDX_CAP))) {
7376512Ssowmini 		/*
7386512Ssowmini 		 * these properties are read/write on copper,
7396512Ssowmini 		 * read-only and 0 on serdes
7406512Ssowmini 		 */
7416512Ssowmini 		mutex_exit(bgep->genlock);
7426512Ssowmini 		return (ENOTSUP);
7436512Ssowmini 	}
7448922SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep) &&
7458922SYong.Tan@Sun.COM 	    ((pr_num == MAC_PROP_EN_1000FDX_CAP) ||
7467678SYong.Tan@Sun.COM 	    (pr_num == MAC_PROP_EN_1000HDX_CAP))) {
7477678SYong.Tan@Sun.COM 		mutex_exit(bgep->genlock);
7487678SYong.Tan@Sun.COM 		return (ENOTSUP);
7497678SYong.Tan@Sun.COM 	}
7506512Ssowmini 
7515903Ssowmini 	switch (pr_num) {
7526789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
7535903Ssowmini 			bgep->param_en_1000fdx = *(uint8_t *)pr_val;
7545903Ssowmini 			bgep->param_adv_1000fdx = *(uint8_t *)pr_val;
7555903Ssowmini 			goto reprogram;
7566789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
7575903Ssowmini 			bgep->param_en_1000hdx = *(uint8_t *)pr_val;
7585903Ssowmini 			bgep->param_adv_1000hdx = *(uint8_t *)pr_val;
7595903Ssowmini 			goto reprogram;
7606789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
7615903Ssowmini 			bgep->param_en_100fdx = *(uint8_t *)pr_val;
7625903Ssowmini 			bgep->param_adv_100fdx = *(uint8_t *)pr_val;
7635903Ssowmini 			goto reprogram;
7646789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
7655903Ssowmini 			bgep->param_en_100hdx = *(uint8_t *)pr_val;
7665903Ssowmini 			bgep->param_adv_100hdx = *(uint8_t *)pr_val;
7675903Ssowmini 			goto reprogram;
7686789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
7695903Ssowmini 			bgep->param_en_10fdx = *(uint8_t *)pr_val;
7705903Ssowmini 			bgep->param_adv_10fdx = *(uint8_t *)pr_val;
7715903Ssowmini 			goto reprogram;
7726789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
7735903Ssowmini 			bgep->param_en_10hdx = *(uint8_t *)pr_val;
7745903Ssowmini 			bgep->param_adv_10hdx = *(uint8_t *)pr_val;
7755903Ssowmini reprogram:
7765903Ssowmini 			if (err == 0 && bge_reprogram(bgep) == IOC_INVAL)
7775903Ssowmini 				err = EINVAL;
7785903Ssowmini 			break;
7796789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
7806789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
7816789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
7826789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
7836789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
7846789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
7856789Sam223141 		case MAC_PROP_STATUS:
7866789Sam223141 		case MAC_PROP_SPEED:
7876789Sam223141 		case MAC_PROP_DUPLEX:
7886512Ssowmini 			err = ENOTSUP; /* read-only prop. Can't set this */
7895903Ssowmini 			break;
7906789Sam223141 		case MAC_PROP_AUTONEG:
7915903Ssowmini 			bgep->param_adv_autoneg = *(uint8_t *)pr_val;
7925903Ssowmini 			if (bge_reprogram(bgep) == IOC_INVAL)
7935903Ssowmini 				err = EINVAL;
7945903Ssowmini 			break;
7956789Sam223141 		case MAC_PROP_MTU:
7965903Ssowmini 			cur_mtu = bgep->chipid.default_mtu;
7975903Ssowmini 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
7986512Ssowmini 
7995903Ssowmini 			if (new_mtu == cur_mtu) {
8005903Ssowmini 				err = 0;
8015903Ssowmini 				break;
8025903Ssowmini 			}
8035903Ssowmini 			if (new_mtu < BGE_DEFAULT_MTU ||
8045903Ssowmini 			    new_mtu > BGE_MAXIMUM_MTU) {
8055903Ssowmini 				err = EINVAL;
8065903Ssowmini 				break;
8075903Ssowmini 			}
8085903Ssowmini 			if ((new_mtu > BGE_DEFAULT_MTU) &&
8095903Ssowmini 			    (bgep->chipid.flags & CHIP_FLAG_NO_JUMBO)) {
8105903Ssowmini 				err = EINVAL;
8115903Ssowmini 				break;
8125903Ssowmini 			}
8135903Ssowmini 			if (bgep->bge_mac_state == BGE_MAC_STARTED) {
8145903Ssowmini 				err = EBUSY;
8155903Ssowmini 				break;
8165903Ssowmini 			}
8175903Ssowmini 			bgep->chipid.default_mtu = new_mtu;
8185903Ssowmini 			if (bge_chip_id_init(bgep)) {
8195903Ssowmini 				err = EINVAL;
8205903Ssowmini 				break;
8215903Ssowmini 			}
8225903Ssowmini 			maxsdu = bgep->chipid.ethmax_size -
8235903Ssowmini 			    sizeof (struct ether_header);
8245903Ssowmini 			err = mac_maxsdu_update(bgep->mh, maxsdu);
8255903Ssowmini 			if (err == 0) {
8265903Ssowmini 				bgep->bge_dma_error = B_TRUE;
8275903Ssowmini 				bgep->manual_reset = B_TRUE;
8285903Ssowmini 				bge_chip_stop(bgep, B_TRUE);
8295903Ssowmini 				bge_wake_factotum(bgep);
8305903Ssowmini 				err = 0;
8315903Ssowmini 			}
8325903Ssowmini 			break;
8336789Sam223141 		case MAC_PROP_FLOWCTRL:
8345903Ssowmini 			bcopy(pr_val, &fl, sizeof (fl));
8355903Ssowmini 			switch (fl) {
8365903Ssowmini 			default:
8376512Ssowmini 				err = ENOTSUP;
8385903Ssowmini 				break;
8395903Ssowmini 			case LINK_FLOWCTRL_NONE:
8405903Ssowmini 				bgep->param_adv_pause = 0;
8415903Ssowmini 				bgep->param_adv_asym_pause = 0;
8425903Ssowmini 
8435903Ssowmini 				bgep->param_link_rx_pause = B_FALSE;
8445903Ssowmini 				bgep->param_link_tx_pause = B_FALSE;
8455903Ssowmini 				break;
8465903Ssowmini 			case LINK_FLOWCTRL_RX:
8475903Ssowmini 				bgep->param_adv_pause = 1;
8485903Ssowmini 				bgep->param_adv_asym_pause = 1;
8495903Ssowmini 
8505903Ssowmini 				bgep->param_link_rx_pause = B_TRUE;
8515903Ssowmini 				bgep->param_link_tx_pause = B_FALSE;
8525903Ssowmini 				break;
8535903Ssowmini 			case LINK_FLOWCTRL_TX:
8545903Ssowmini 				bgep->param_adv_pause = 0;
8555903Ssowmini 				bgep->param_adv_asym_pause = 1;
8565903Ssowmini 
8575903Ssowmini 				bgep->param_link_rx_pause = B_FALSE;
8585903Ssowmini 				bgep->param_link_tx_pause = B_TRUE;
8595903Ssowmini 				break;
8605903Ssowmini 			case LINK_FLOWCTRL_BI:
8615903Ssowmini 				bgep->param_adv_pause = 1;
8628922SYong.Tan@Sun.COM 				bgep->param_adv_asym_pause = 0;
8635903Ssowmini 
8645903Ssowmini 				bgep->param_link_rx_pause = B_TRUE;
8655903Ssowmini 				bgep->param_link_tx_pause = B_TRUE;
8665903Ssowmini 				break;
8675903Ssowmini 			}
8685903Ssowmini 
8695903Ssowmini 			if (err == 0) {
8705903Ssowmini 				if (bge_reprogram(bgep) == IOC_INVAL)
8715903Ssowmini 					err = EINVAL;
8725903Ssowmini 			}
8735903Ssowmini 
8745903Ssowmini 			break;
8756789Sam223141 		case MAC_PROP_PRIVATE:
8765903Ssowmini 			err = bge_set_priv_prop(bgep, pr_name, pr_valsize,
8775903Ssowmini 			    pr_val);
8785903Ssowmini 			break;
8796512Ssowmini 		default:
8806512Ssowmini 			err = ENOTSUP;
8816512Ssowmini 			break;
8825903Ssowmini 	}
8835903Ssowmini 	mutex_exit(bgep->genlock);
8845903Ssowmini 	return (err);
8855903Ssowmini }
8866512Ssowmini 
8878118SVasumathi.Sundaram@Sun.COM /* ARGSUSED */
8885903Ssowmini static int
8895903Ssowmini bge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
8908118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
8915903Ssowmini {
8925903Ssowmini 	bge_t *bgep = barg;
8935903Ssowmini 	int err = 0;
8945903Ssowmini 	link_flowctrl_t fl;
8956512Ssowmini 	uint64_t speed;
8966512Ssowmini 	int flags = bgep->chipid.flags;
8976789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
8986512Ssowmini 
8996512Ssowmini 	if (pr_valsize == 0)
9006512Ssowmini 		return (EINVAL);
9015903Ssowmini 	bzero(pr_val, pr_valsize);
9028118SVasumathi.Sundaram@Sun.COM 
9038118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
9048118SVasumathi.Sundaram@Sun.COM 
9058118SVasumathi.Sundaram@Sun.COM 	mutex_enter(bgep->genlock);
9068118SVasumathi.Sundaram@Sun.COM 	if ((bgep->param_loop_mode != BGE_LOOP_NONE &&
9078118SVasumathi.Sundaram@Sun.COM 	    bge_param_locked(pr_num)) ||
9088118SVasumathi.Sundaram@Sun.COM 	    ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
9098118SVasumathi.Sundaram@Sun.COM 	    ((pr_num == MAC_PROP_EN_100FDX_CAP) ||
9108118SVasumathi.Sundaram@Sun.COM 	    (pr_num == MAC_PROP_EN_100HDX_CAP) ||
9118118SVasumathi.Sundaram@Sun.COM 	    (pr_num == MAC_PROP_EN_10FDX_CAP) ||
9128118SVasumathi.Sundaram@Sun.COM 	    (pr_num == MAC_PROP_EN_10HDX_CAP))) ||
9138118SVasumathi.Sundaram@Sun.COM 	    (DEVICE_5906_SERIES_CHIPSETS(bgep) &&
9148922SYong.Tan@Sun.COM 	    ((pr_num == MAC_PROP_EN_1000FDX_CAP) ||
9158922SYong.Tan@Sun.COM 	    (pr_num == MAC_PROP_EN_1000HDX_CAP))))
9168118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
9178118SVasumathi.Sundaram@Sun.COM 	mutex_exit(bgep->genlock);
9188118SVasumathi.Sundaram@Sun.COM 
9195903Ssowmini 	switch (pr_num) {
9206789Sam223141 		case MAC_PROP_DUPLEX:
9218118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
9226512Ssowmini 			if (pr_valsize < sizeof (link_duplex_t))
9235903Ssowmini 				return (EINVAL);
9246512Ssowmini 			bcopy(&bgep->param_link_duplex, pr_val,
9256512Ssowmini 			    sizeof (link_duplex_t));
9265903Ssowmini 			break;
9276789Sam223141 		case MAC_PROP_SPEED:
9288118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
9296512Ssowmini 			if (pr_valsize < sizeof (speed))
9305903Ssowmini 				return (EINVAL);
9316512Ssowmini 			speed = bgep->param_link_speed * 1000000ull;
9326512Ssowmini 			bcopy(&speed, pr_val, sizeof (speed));
9335903Ssowmini 			break;
9346789Sam223141 		case MAC_PROP_STATUS:
9358118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
9366512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
9375903Ssowmini 				return (EINVAL);
9386512Ssowmini 			bcopy(&bgep->link_state, pr_val,
9396512Ssowmini 			    sizeof (link_state_t));
9405903Ssowmini 			break;
9416789Sam223141 		case MAC_PROP_AUTONEG:
9426512Ssowmini 			if (is_default)
9436512Ssowmini 				*(uint8_t *)pr_val = 1;
9446512Ssowmini 			else
9456512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_autoneg;
9465903Ssowmini 			break;
9476789Sam223141 		case MAC_PROP_FLOWCTRL:
9486512Ssowmini 			if (pr_valsize < sizeof (fl))
9495903Ssowmini 				return (EINVAL);
9506512Ssowmini 			if (is_default) {
9516512Ssowmini 				fl = LINK_FLOWCTRL_BI;
9526512Ssowmini 				bcopy(&fl, pr_val, sizeof (fl));
9536512Ssowmini 				break;
9546512Ssowmini 			}
9556512Ssowmini 
9565903Ssowmini 			if (bgep->param_link_rx_pause &&
9575903Ssowmini 			    !bgep->param_link_tx_pause)
9585903Ssowmini 				fl = LINK_FLOWCTRL_RX;
9595903Ssowmini 
9605903Ssowmini 			if (!bgep->param_link_rx_pause &&
9615903Ssowmini 			    !bgep->param_link_tx_pause)
9625903Ssowmini 				fl = LINK_FLOWCTRL_NONE;
9635903Ssowmini 
9645903Ssowmini 			if (!bgep->param_link_rx_pause &&
9655903Ssowmini 			    bgep->param_link_tx_pause)
9665903Ssowmini 				fl = LINK_FLOWCTRL_TX;
9675903Ssowmini 
9685903Ssowmini 			if (bgep->param_link_rx_pause &&
9695903Ssowmini 			    bgep->param_link_tx_pause)
9705903Ssowmini 				fl = LINK_FLOWCTRL_BI;
9715903Ssowmini 			bcopy(&fl, pr_val, sizeof (fl));
9725903Ssowmini 			break;
9736789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
9748118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
9757678SYong.Tan@Sun.COM 			if (is_default) {
9767678SYong.Tan@Sun.COM 				if (DEVICE_5906_SERIES_CHIPSETS(bgep))
9777678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 0;
9787678SYong.Tan@Sun.COM 				else
9797678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 1;
9807678SYong.Tan@Sun.COM 			}
9816512Ssowmini 			else
9826512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_1000fdx;
9835903Ssowmini 			break;
9846789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
9857678SYong.Tan@Sun.COM 			if (is_default) {
9867678SYong.Tan@Sun.COM 				if (DEVICE_5906_SERIES_CHIPSETS(bgep))
9877678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 0;
9887678SYong.Tan@Sun.COM 				else
9897678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 1;
9907678SYong.Tan@Sun.COM 			}
9916512Ssowmini 			else
9926512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_1000fdx;
9935903Ssowmini 			break;
9946789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
9958118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
9967678SYong.Tan@Sun.COM 			if (is_default) {
9977678SYong.Tan@Sun.COM 				if (DEVICE_5906_SERIES_CHIPSETS(bgep))
9987678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 0;
9997678SYong.Tan@Sun.COM 				else
10007678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 1;
10017678SYong.Tan@Sun.COM 			}
10026512Ssowmini 			else
10036512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_1000hdx;
10045903Ssowmini 			break;
10056789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
10067678SYong.Tan@Sun.COM 			if (is_default) {
10077678SYong.Tan@Sun.COM 				if (DEVICE_5906_SERIES_CHIPSETS(bgep))
10087678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 0;
10097678SYong.Tan@Sun.COM 				else
10107678SYong.Tan@Sun.COM 					*(uint8_t *)pr_val = 1;
10117678SYong.Tan@Sun.COM 			}
10126512Ssowmini 			else
10136512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_1000hdx;
10145903Ssowmini 			break;
10156789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
10168118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
10176512Ssowmini 			if (is_default) {
10186512Ssowmini 				*(uint8_t *)pr_val =
10196512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10206512Ssowmini 			} else {
10216512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_100fdx;
10226512Ssowmini 			}
10235903Ssowmini 			break;
10246789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
10256512Ssowmini 			if (is_default) {
10266512Ssowmini 				*(uint8_t *)pr_val =
10276512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10286512Ssowmini 			} else {
10296512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_100fdx;
10306512Ssowmini 			}
10315903Ssowmini 			break;
10326789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
10338118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
10346512Ssowmini 			if (is_default) {
10356512Ssowmini 				*(uint8_t *)pr_val =
10366512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10376512Ssowmini 			} else {
10386512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_100hdx;
10396512Ssowmini 			}
10405903Ssowmini 			break;
10416789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
10426512Ssowmini 			if (is_default) {
10436512Ssowmini 				*(uint8_t *)pr_val =
10446512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10456512Ssowmini 			} else {
10466512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_100hdx;
10476512Ssowmini 			}
10485903Ssowmini 			break;
10496789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
10508118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
10516512Ssowmini 			if (is_default) {
10526512Ssowmini 				*(uint8_t *)pr_val =
10536512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10546512Ssowmini 			} else {
10556512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_10fdx;
10566512Ssowmini 			}
10575903Ssowmini 			break;
10586789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
10596512Ssowmini 			if (is_default) {
10606512Ssowmini 				*(uint8_t *)pr_val =
10616512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10626512Ssowmini 			} else {
10636512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_10fdx;
10646512Ssowmini 			}
10655903Ssowmini 			break;
10666789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
10678118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
10686512Ssowmini 			if (is_default) {
10696512Ssowmini 				*(uint8_t *)pr_val =
10706512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10716512Ssowmini 			} else {
10726512Ssowmini 				*(uint8_t *)pr_val = bgep->param_adv_10hdx;
10736512Ssowmini 			}
10745903Ssowmini 			break;
10756789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
10766512Ssowmini 			if (is_default) {
10776512Ssowmini 				*(uint8_t *)pr_val =
10786512Ssowmini 				    ((flags & CHIP_FLAG_SERDES) ? 0 : 1);
10796512Ssowmini 			} else {
10806512Ssowmini 				*(uint8_t *)pr_val = bgep->param_en_10hdx;
10816512Ssowmini 			}
10825903Ssowmini 			break;
10836789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
10846789Sam223141 		case MAC_PROP_EN_100T4_CAP:
10858118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
10866512Ssowmini 			*(uint8_t *)pr_val = 0;
10876512Ssowmini 			break;
10886789Sam223141 		case MAC_PROP_PRIVATE:
10896512Ssowmini 			err = bge_get_priv_prop(bgep, pr_name, pr_flags,
10906512Ssowmini 			    pr_valsize, pr_val);
10916512Ssowmini 			return (err);
10929514SGirish.Moodalbail@Sun.COM 		case MAC_PROP_MTU: {
10939514SGirish.Moodalbail@Sun.COM 			mac_propval_range_t range;
10949514SGirish.Moodalbail@Sun.COM 
10959514SGirish.Moodalbail@Sun.COM 			if (!(pr_flags & MAC_PROP_POSSIBLE))
10969514SGirish.Moodalbail@Sun.COM 				return (ENOTSUP);
10979514SGirish.Moodalbail@Sun.COM 			if (pr_valsize < sizeof (mac_propval_range_t))
10989514SGirish.Moodalbail@Sun.COM 				return (EINVAL);
10999514SGirish.Moodalbail@Sun.COM 			range.mpr_count = 1;
11009514SGirish.Moodalbail@Sun.COM 			range.mpr_type = MAC_PROPVAL_UINT32;
11019514SGirish.Moodalbail@Sun.COM 			range.range_uint32[0].mpur_min =
11029514SGirish.Moodalbail@Sun.COM 			    range.range_uint32[0].mpur_max = BGE_DEFAULT_MTU;
110310162SYong.Tan@Sun.COM 			if (!(flags & CHIP_FLAG_NO_JUMBO))
11049514SGirish.Moodalbail@Sun.COM 				range.range_uint32[0].mpur_max =
11059514SGirish.Moodalbail@Sun.COM 				    BGE_MAXIMUM_MTU;
11069514SGirish.Moodalbail@Sun.COM 			bcopy(&range, pr_val, sizeof (range));
11079514SGirish.Moodalbail@Sun.COM 			break;
11089514SGirish.Moodalbail@Sun.COM 		}
11095903Ssowmini 		default:
11106512Ssowmini 			return (ENOTSUP);
11115903Ssowmini 	}
11125903Ssowmini 	return (0);
11135903Ssowmini }
11145903Ssowmini 
11155903Ssowmini /* ARGSUSED */
11165903Ssowmini static int
11175903Ssowmini bge_set_priv_prop(bge_t *bgep, const char *pr_name, uint_t pr_valsize,
11185903Ssowmini     const void *pr_val)
11195903Ssowmini {
11205903Ssowmini 	int err = 0;
11215903Ssowmini 	long result;
11225903Ssowmini 
11236512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
11246512Ssowmini 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
11256512Ssowmini 		if (result > 1 || result < 0) {
11266512Ssowmini 			err = EINVAL;
11276512Ssowmini 		} else {
11287099Syt223700 			bgep->param_adv_pause = (uint32_t)result;
11296512Ssowmini 			if (bge_reprogram(bgep) == IOC_INVAL)
11306512Ssowmini 				err = EINVAL;
11316512Ssowmini 		}
11326512Ssowmini 		return (err);
11336512Ssowmini 	}
11346512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
11356512Ssowmini 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
11366512Ssowmini 		if (result > 1 || result < 0) {
11376512Ssowmini 			err = EINVAL;
11386512Ssowmini 		} else {
11397099Syt223700 			bgep->param_adv_asym_pause = (uint32_t)result;
11406512Ssowmini 			if (bge_reprogram(bgep) == IOC_INVAL)
11416512Ssowmini 				err = EINVAL;
11426512Ssowmini 		}
11436512Ssowmini 		return (err);
11446512Ssowmini 	}
11455903Ssowmini 	if (strcmp(pr_name, "_drain_max") == 0) {
11465903Ssowmini 
11475903Ssowmini 		/*
11485903Ssowmini 		 * on the Tx side, we need to update the h/w register for
11495903Ssowmini 		 * real packet transmission per packet. The drain_max parameter
11505903Ssowmini 		 * is used to reduce the register access. This parameter
11515903Ssowmini 		 * controls the max number of packets that we will hold before
11525903Ssowmini 		 * updating the bge h/w to trigger h/w transmit. The bge
11535903Ssowmini 		 * chipset usually has a max of 512 Tx descriptors, thus
11545903Ssowmini 		 * the upper bound on drain_max is 512.
11555903Ssowmini 		 */
11565903Ssowmini 		if (pr_val == NULL) {
11575903Ssowmini 			err = EINVAL;
11585903Ssowmini 			return (err);
11595903Ssowmini 		}
11605903Ssowmini 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
11615903Ssowmini 		if (result > 512 || result < 1)
11625903Ssowmini 			err = EINVAL;
11635903Ssowmini 		else {
11645903Ssowmini 			bgep->param_drain_max = (uint32_t)result;
11655903Ssowmini 			if (bge_reprogram(bgep) == IOC_INVAL)
11665903Ssowmini 				err = EINVAL;
11675903Ssowmini 		}
11685903Ssowmini 		return (err);
11695903Ssowmini 	}
11705903Ssowmini 	if (strcmp(pr_name, "_msi_cnt") == 0) {
11715903Ssowmini 
11725903Ssowmini 		if (pr_val == NULL) {
11735903Ssowmini 			err = EINVAL;
11745903Ssowmini 			return (err);
11755903Ssowmini 		}
11765903Ssowmini 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
11775903Ssowmini 		if (result > 7 || result < 0)
11785903Ssowmini 			err = EINVAL;
11795903Ssowmini 		else {
11805903Ssowmini 			bgep->param_msi_cnt = (uint32_t)result;
11815903Ssowmini 			if (bge_reprogram(bgep) == IOC_INVAL)
11825903Ssowmini 				err = EINVAL;
11835903Ssowmini 		}
11845903Ssowmini 		return (err);
11855903Ssowmini 	}
11869731SYong.Tan@Sun.COM 	if (strcmp(pr_name, "_rx_intr_coalesce_blank_time") == 0) {
11879731SYong.Tan@Sun.COM 		if (ddi_strtol(pr_val, (char **)NULL, 0, &result) != 0)
11889731SYong.Tan@Sun.COM 			return (EINVAL);
11899731SYong.Tan@Sun.COM 		if (result < 0)
11909731SYong.Tan@Sun.COM 			err = EINVAL;
11919731SYong.Tan@Sun.COM 		else {
11929731SYong.Tan@Sun.COM 			bgep->chipid.rx_ticks_norm = (uint32_t)result;
11939731SYong.Tan@Sun.COM 			bge_chip_coalesce_update(bgep);
11949731SYong.Tan@Sun.COM 		}
11959731SYong.Tan@Sun.COM 		return (err);
11969731SYong.Tan@Sun.COM 	}
11979731SYong.Tan@Sun.COM 
11989731SYong.Tan@Sun.COM 	if (strcmp(pr_name, "_rx_intr_coalesce_pkt_cnt") == 0) {
11996512Ssowmini 		if (ddi_strtol(pr_val, (char **)NULL, 0, &result) != 0)
12005903Ssowmini 			return (EINVAL);
12015903Ssowmini 
12029731SYong.Tan@Sun.COM 		if (result < 0)
12039731SYong.Tan@Sun.COM 			err = EINVAL;
12049731SYong.Tan@Sun.COM 		else {
12059731SYong.Tan@Sun.COM 			bgep->chipid.rx_count_norm = (uint32_t)result;
12069731SYong.Tan@Sun.COM 			bge_chip_coalesce_update(bgep);
12079731SYong.Tan@Sun.COM 		}
12089731SYong.Tan@Sun.COM 		return (err);
12095903Ssowmini 	}
12109731SYong.Tan@Sun.COM 	if (strcmp(pr_name, "_tx_intr_coalesce_blank_time") == 0) {
12115903Ssowmini 		if (ddi_strtol(pr_val, (char **)NULL, 0, &result) != 0)
12125903Ssowmini 			return (EINVAL);
12139731SYong.Tan@Sun.COM 		if (result < 0)
12149731SYong.Tan@Sun.COM 			err = EINVAL;
12159731SYong.Tan@Sun.COM 		else {
12169731SYong.Tan@Sun.COM 			bgep->chipid.tx_ticks_norm = (uint32_t)result;
12179731SYong.Tan@Sun.COM 			bge_chip_coalesce_update(bgep);
12189731SYong.Tan@Sun.COM 		}
12199731SYong.Tan@Sun.COM 		return (err);
12209731SYong.Tan@Sun.COM 	}
12219731SYong.Tan@Sun.COM 
12229731SYong.Tan@Sun.COM 	if (strcmp(pr_name, "_tx_intr_coalesce_pkt_cnt") == 0) {
12239731SYong.Tan@Sun.COM 		if (ddi_strtol(pr_val, (char **)NULL, 0, &result) != 0)
12249731SYong.Tan@Sun.COM 			return (EINVAL);
12259731SYong.Tan@Sun.COM 
12269731SYong.Tan@Sun.COM 		if (result < 0)
12279731SYong.Tan@Sun.COM 			err = EINVAL;
12289731SYong.Tan@Sun.COM 		else {
12299731SYong.Tan@Sun.COM 			bgep->chipid.tx_count_norm = (uint32_t)result;
12309731SYong.Tan@Sun.COM 			bge_chip_coalesce_update(bgep);
12319731SYong.Tan@Sun.COM 		}
12329731SYong.Tan@Sun.COM 		return (err);
12335903Ssowmini 	}
12346512Ssowmini 	return (ENOTSUP);
12355903Ssowmini }
12365903Ssowmini 
12375903Ssowmini static int
12386512Ssowmini bge_get_priv_prop(bge_t *bge, const char *pr_name, uint_t pr_flags,
12396512Ssowmini     uint_t pr_valsize, void *pr_val)
12405903Ssowmini {
12416512Ssowmini 	int err = ENOTSUP;
12426789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
12436512Ssowmini 	int value;
12446512Ssowmini 
12456512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
12466512Ssowmini 		value = (is_default? 1 : bge->param_adv_pause);
12476512Ssowmini 		err = 0;
12486512Ssowmini 		goto done;
12496512Ssowmini 	}
12506512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
12516512Ssowmini 		value = (is_default? 1 : bge->param_adv_asym_pause);
12526512Ssowmini 		err = 0;
12536512Ssowmini 		goto done;
12546512Ssowmini 	}
12555903Ssowmini 	if (strcmp(pr_name, "_drain_max") == 0) {
12566512Ssowmini 		value = (is_default? 64 : bge->param_drain_max);
12575903Ssowmini 		err = 0;
12585903Ssowmini 		goto done;
12595903Ssowmini 	}
12605903Ssowmini 	if (strcmp(pr_name, "_msi_cnt") == 0) {
12616512Ssowmini 		value = (is_default? 0 : bge->param_msi_cnt);
12625903Ssowmini 		err = 0;
12635903Ssowmini 		goto done;
12645903Ssowmini 	}
12655903Ssowmini 
12665903Ssowmini 	if (strcmp(pr_name, "_intr_coalesce_blank_time") == 0) {
12676512Ssowmini 		value = (is_default? bge_rx_ticks_norm :
12686512Ssowmini 		    bge->chipid.rx_ticks_norm);
12695903Ssowmini 		err = 0;
12705903Ssowmini 		goto done;
12715903Ssowmini 	}
12725903Ssowmini 
12735903Ssowmini 	if (strcmp(pr_name, "_intr_coalesce_pkt_cnt") == 0) {
12746512Ssowmini 		value = (is_default? bge_rx_count_norm :
12756512Ssowmini 		    bge->chipid.rx_count_norm);
12765903Ssowmini 		err = 0;
12775903Ssowmini 		goto done;
12785903Ssowmini 	}
12795903Ssowmini 
12805903Ssowmini done:
12816512Ssowmini 	if (err == 0) {
12826512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
12835903Ssowmini 	}
12845903Ssowmini 	return (err);
12855903Ssowmini }
12865903Ssowmini 
12872331Skrgopi /*
12881369Sdduvall  * Compute the index of the required bit in the multicast hash map.
12891369Sdduvall  * This must mirror the way the hardware actually does it!
12901369Sdduvall  * See Broadcom document 570X-PG102-R page 125.
12911369Sdduvall  */
12921369Sdduvall static uint32_t
12931369Sdduvall bge_hash_index(const uint8_t *mca)
12941369Sdduvall {
12951369Sdduvall 	uint32_t hash;
12961369Sdduvall 
12971369Sdduvall 	CRC32(hash, mca, ETHERADDRL, -1U, crc32_table);
12981369Sdduvall 
12991369Sdduvall 	return (hash);
13001369Sdduvall }
13011369Sdduvall 
13021369Sdduvall /*
13031369Sdduvall  *	bge_m_multicst_add() -- enable/disable a multicast address
13041369Sdduvall  */
13051369Sdduvall static int
13061369Sdduvall bge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
13071369Sdduvall {
13081369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
13091369Sdduvall 	uint32_t hash;
13101369Sdduvall 	uint32_t index;
13111369Sdduvall 	uint32_t word;
13121369Sdduvall 	uint32_t bit;
13131369Sdduvall 	uint8_t *refp;
13141369Sdduvall 
13151369Sdduvall 	BGE_TRACE(("bge_m_multicst($%p, %s, %s)", arg,
13164588Sml149210 	    (add) ? "add" : "remove", ether_sprintf((void *)mca)));
13171369Sdduvall 
13181369Sdduvall 	/*
13191369Sdduvall 	 * Precalculate all required masks, pointers etc ...
13201369Sdduvall 	 */
13211369Sdduvall 	hash = bge_hash_index(mca);
13221369Sdduvall 	index = hash % BGE_HASH_TABLE_SIZE;
13231369Sdduvall 	word = index/32u;
13241369Sdduvall 	bit = 1 << (index % 32u);
13251369Sdduvall 	refp = &bgep->mcast_refs[index];
13261369Sdduvall 
13271369Sdduvall 	BGE_DEBUG(("bge_m_multicst: hash 0x%x index %d (%d:0x%x) = %d",
13284588Sml149210 	    hash, index, word, bit, *refp));
13291369Sdduvall 
13301369Sdduvall 	/*
13311369Sdduvall 	 * We must set the appropriate bit in the hash map (and the
13321369Sdduvall 	 * corresponding h/w register) when the refcount goes from 0
13331369Sdduvall 	 * to >0, and clear it when the last ref goes away (refcount
13341369Sdduvall 	 * goes from >0 back to 0).  If we change the hash map, we
13351369Sdduvall 	 * must also update the chip's hardware map registers.
13361369Sdduvall 	 */
13371369Sdduvall 	mutex_enter(bgep->genlock);
13381865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
13391865Sdilpreet 		/* can happen during autorecovery */
13401865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
13411865Sdilpreet 		mutex_exit(bgep->genlock);
13421865Sdilpreet 		return (EIO);
13431865Sdilpreet 	}
13441369Sdduvall 	if (add) {
13451369Sdduvall 		if ((*refp)++ == 0) {
13461369Sdduvall 			bgep->mcast_hash[word] |= bit;
13471408Srandyf #ifdef BGE_IPMI_ASF
13481865Sdilpreet 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
13491408Srandyf #else
13501865Sdilpreet 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
13511408Srandyf #endif
13521865Sdilpreet 				(void) bge_check_acc_handle(bgep,
13531865Sdilpreet 				    bgep->cfg_handle);
13541865Sdilpreet 				(void) bge_check_acc_handle(bgep,
13551865Sdilpreet 				    bgep->io_handle);
13561865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
13571865Sdilpreet 				    DDI_SERVICE_DEGRADED);
13581865Sdilpreet 				mutex_exit(bgep->genlock);
13591865Sdilpreet 				return (EIO);
13601865Sdilpreet 			}
13611369Sdduvall 		}
13621369Sdduvall 	} else {
13631369Sdduvall 		if (--(*refp) == 0) {
13641369Sdduvall 			bgep->mcast_hash[word] &= ~bit;
13651408Srandyf #ifdef BGE_IPMI_ASF
13661865Sdilpreet 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
13671408Srandyf #else
13681865Sdilpreet 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
13691408Srandyf #endif
13701865Sdilpreet 				(void) bge_check_acc_handle(bgep,
13711865Sdilpreet 				    bgep->cfg_handle);
13721865Sdilpreet 				(void) bge_check_acc_handle(bgep,
13731865Sdilpreet 				    bgep->io_handle);
13741865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
13751865Sdilpreet 				    DDI_SERVICE_DEGRADED);
13761865Sdilpreet 				mutex_exit(bgep->genlock);
13771865Sdilpreet 				return (EIO);
13781865Sdilpreet 			}
13791369Sdduvall 		}
13801369Sdduvall 	}
13811369Sdduvall 	BGE_DEBUG(("bge_m_multicst($%p) done", arg));
13821865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
13831865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
13841865Sdilpreet 		mutex_exit(bgep->genlock);
13851865Sdilpreet 		return (EIO);
13861865Sdilpreet 	}
13871865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
13881865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
13891865Sdilpreet 		mutex_exit(bgep->genlock);
13901865Sdilpreet 		return (EIO);
13911865Sdilpreet 	}
13921369Sdduvall 	mutex_exit(bgep->genlock);
13931369Sdduvall 
13941369Sdduvall 	return (0);
13951369Sdduvall }
13961369Sdduvall 
13971369Sdduvall /*
13981369Sdduvall  * bge_m_promisc() -- set or reset promiscuous mode on the board
13991369Sdduvall  *
14001369Sdduvall  *	Program the hardware to enable/disable promiscuous and/or
14011369Sdduvall  *	receive-all-multicast modes.
14021369Sdduvall  */
14031369Sdduvall static int
14041369Sdduvall bge_m_promisc(void *arg, boolean_t on)
14051369Sdduvall {
14061369Sdduvall 	bge_t *bgep = arg;
14071369Sdduvall 
14081369Sdduvall 	BGE_TRACE(("bge_m_promisc_set($%p, %d)", arg, on));
14091369Sdduvall 
14101369Sdduvall 	/*
14111369Sdduvall 	 * Store MAC layer specified mode and pass to chip layer to update h/w
14121369Sdduvall 	 */
14131369Sdduvall 	mutex_enter(bgep->genlock);
14141865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
14151865Sdilpreet 		/* can happen during autorecovery */
14161865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
14171865Sdilpreet 		mutex_exit(bgep->genlock);
14181865Sdilpreet 		return (EIO);
14191865Sdilpreet 	}
14201369Sdduvall 	bgep->promisc = on;
14211408Srandyf #ifdef BGE_IPMI_ASF
14221865Sdilpreet 	if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
14231408Srandyf #else
14241865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
14251408Srandyf #endif
14261865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
14271865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
14281865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
14291865Sdilpreet 		mutex_exit(bgep->genlock);
14301865Sdilpreet 		return (EIO);
14311865Sdilpreet 	}
14321369Sdduvall 	BGE_DEBUG(("bge_m_promisc_set($%p) done", arg));
14331865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
14341865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
14351865Sdilpreet 		mutex_exit(bgep->genlock);
14361865Sdilpreet 		return (EIO);
14371865Sdilpreet 	}
14381865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
14391865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
14401865Sdilpreet 		mutex_exit(bgep->genlock);
14411865Sdilpreet 		return (EIO);
14421865Sdilpreet 	}
14431369Sdduvall 	mutex_exit(bgep->genlock);
14441369Sdduvall 	return (0);
14451369Sdduvall }
14461369Sdduvall 
14478275SEric Cheng /*
14488275SEric Cheng  * Find the slot for the specified unicast address
14498275SEric Cheng  */
14508275SEric Cheng int
14518275SEric Cheng bge_unicst_find(bge_t *bgep, const uint8_t *mac_addr)
14528275SEric Cheng {
14538275SEric Cheng 	int slot;
14548275SEric Cheng 
14558275SEric Cheng 	ASSERT(mutex_owned(bgep->genlock));
14568275SEric Cheng 
14578275SEric Cheng 	for (slot = 0; slot < bgep->unicst_addr_total; slot++) {
14588275SEric Cheng 		if (bcmp(bgep->curr_addr[slot].addr, mac_addr, ETHERADDRL) == 0)
14598275SEric Cheng 			return (slot);
14608275SEric Cheng 	}
14618275SEric Cheng 
14628275SEric Cheng 	return (-1);
14638275SEric Cheng }
14648275SEric Cheng 
14658275SEric Cheng /*
14668275SEric Cheng  * Programs the classifier to start steering packets matching 'mac_addr' to the
14678275SEric Cheng  * specified ring 'arg'.
14688275SEric Cheng  */
14698275SEric Cheng static int
14708275SEric Cheng bge_addmac(void *arg, const uint8_t *mac_addr)
14718275SEric Cheng {
14728275SEric Cheng 	recv_ring_t *rrp = (recv_ring_t *)arg;
14738275SEric Cheng 	bge_t		*bgep = rrp->bgep;
14748275SEric Cheng 	bge_recv_rule_t	*rulep = bgep->recv_rules;
14758275SEric Cheng 	bge_rule_info_t	*rinfop = NULL;
14768275SEric Cheng 	uint8_t		ring = (uint8_t)(rrp - bgep->recv) + 1;
14778275SEric Cheng 	int		i;
14788275SEric Cheng 	uint16_t	tmp16;
14798275SEric Cheng 	uint32_t	tmp32;
14808275SEric Cheng 	int		slot;
14818275SEric Cheng 	int		err;
14828275SEric Cheng 
14838275SEric Cheng 	mutex_enter(bgep->genlock);
14848275SEric Cheng 	if (bgep->unicst_addr_avail == 0) {
14858275SEric Cheng 		mutex_exit(bgep->genlock);
14868275SEric Cheng 		return (ENOSPC);
14878275SEric Cheng 	}
14888275SEric Cheng 
14898275SEric Cheng 	/*
14908275SEric Cheng 	 * First add the unicast address to a available slot.
14918275SEric Cheng 	 */
14928275SEric Cheng 	slot = bge_unicst_find(bgep, mac_addr);
14938275SEric Cheng 	ASSERT(slot == -1);
14948275SEric Cheng 
14958275SEric Cheng 	for (slot = 0; slot < bgep->unicst_addr_total; slot++) {
14968275SEric Cheng 		if (!bgep->curr_addr[slot].set) {
14978275SEric Cheng 			bgep->curr_addr[slot].set = B_TRUE;
14988275SEric Cheng 			break;
14998275SEric Cheng 		}
15008275SEric Cheng 	}
15018275SEric Cheng 
15028275SEric Cheng 	ASSERT(slot < bgep->unicst_addr_total);
15038275SEric Cheng 	bgep->unicst_addr_avail--;
15048275SEric Cheng 	mutex_exit(bgep->genlock);
15058275SEric Cheng 
15068275SEric Cheng 	if ((err = bge_unicst_set(bgep, mac_addr, slot)) != 0)
15078275SEric Cheng 		goto fail;
15088275SEric Cheng 
15098275SEric Cheng 	/* A rule is already here. Deny this.  */
15108275SEric Cheng 	if (rrp->mac_addr_rule != NULL) {
15118275SEric Cheng 		err = ether_cmp(mac_addr, rrp->mac_addr_val) ? EEXIST : EBUSY;
15128275SEric Cheng 		goto fail;
15138275SEric Cheng 	}
15148275SEric Cheng 
15158275SEric Cheng 	/*
15168275SEric Cheng 	 * Allocate a bge_rule_info_t to keep track of which rule slots
15178275SEric Cheng 	 * are being used.
15188275SEric Cheng 	 */
15198275SEric Cheng 	rinfop = kmem_zalloc(sizeof (bge_rule_info_t), KM_NOSLEEP);
15208275SEric Cheng 	if (rinfop == NULL) {
15218275SEric Cheng 		err = ENOMEM;
15228275SEric Cheng 		goto fail;
15238275SEric Cheng 	}
15248275SEric Cheng 
15258275SEric Cheng 	/*
15268275SEric Cheng 	 * Look for the starting slot to place the rules.
15278275SEric Cheng 	 * The two slots we reserve must be contiguous.
15288275SEric Cheng 	 */
15298275SEric Cheng 	for (i = 0; i + 1 < RECV_RULES_NUM_MAX; i++)
15308275SEric Cheng 		if ((rulep[i].control & RECV_RULE_CTL_ENABLE) == 0 &&
15318275SEric Cheng 		    (rulep[i+1].control & RECV_RULE_CTL_ENABLE) == 0)
15328275SEric Cheng 			break;
15338275SEric Cheng 
15348275SEric Cheng 	ASSERT(i + 1 < RECV_RULES_NUM_MAX);
15358275SEric Cheng 
15368275SEric Cheng 	bcopy(mac_addr, &tmp32, sizeof (tmp32));
15378275SEric Cheng 	rulep[i].mask_value = ntohl(tmp32);
15388275SEric Cheng 	rulep[i].control = RULE_DEST_MAC_1(ring) | RECV_RULE_CTL_AND;
15398275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep[i].mask_value);
15408275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep[i].control);
15418275SEric Cheng 
15428275SEric Cheng 	bcopy(mac_addr + 4, &tmp16, sizeof (tmp16));
15438275SEric Cheng 	rulep[i+1].mask_value = 0xffff0000 | ntohs(tmp16);
15448275SEric Cheng 	rulep[i+1].control = RULE_DEST_MAC_2(ring);
15458275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_MASK_REG(i+1), rulep[i+1].mask_value);
15468275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i+1), rulep[i+1].control);
15478275SEric Cheng 	rinfop->start = i;
15488275SEric Cheng 	rinfop->count = 2;
15498275SEric Cheng 
15508275SEric Cheng 	rrp->mac_addr_rule = rinfop;
15518275SEric Cheng 	bcopy(mac_addr, rrp->mac_addr_val, ETHERADDRL);
15528275SEric Cheng 
15538275SEric Cheng 	return (0);
15548275SEric Cheng 
15558275SEric Cheng fail:
15568275SEric Cheng 	/* Clear the address just set */
15578275SEric Cheng 	(void) bge_unicst_set(bgep, zero_addr, slot);
15588275SEric Cheng 	mutex_enter(bgep->genlock);
15598275SEric Cheng 	bgep->curr_addr[slot].set = B_FALSE;
15608275SEric Cheng 	bgep->unicst_addr_avail++;
15618275SEric Cheng 	mutex_exit(bgep->genlock);
15628275SEric Cheng 
15638275SEric Cheng 	return (err);
15648275SEric Cheng }
15658275SEric Cheng 
15668275SEric Cheng /*
15678275SEric Cheng  * Stop classifying packets matching the MAC address to the specified ring.
15688275SEric Cheng  */
15698275SEric Cheng static int
15708275SEric Cheng bge_remmac(void *arg, const uint8_t *mac_addr)
15718275SEric Cheng {
15728275SEric Cheng 	recv_ring_t	*rrp = (recv_ring_t *)arg;
15738275SEric Cheng 	bge_t		*bgep = rrp->bgep;
15748275SEric Cheng 	bge_recv_rule_t *rulep = bgep->recv_rules;
15758275SEric Cheng 	bge_rule_info_t *rinfop = rrp->mac_addr_rule;
15768275SEric Cheng 	int		start;
15778275SEric Cheng 	int		slot;
15788275SEric Cheng 	int		err;
15798275SEric Cheng 
15808275SEric Cheng 	/*
15818275SEric Cheng 	 * Remove the MAC address from its slot.
15828275SEric Cheng 	 */
15838275SEric Cheng 	mutex_enter(bgep->genlock);
15848275SEric Cheng 	slot = bge_unicst_find(bgep, mac_addr);
15858275SEric Cheng 	if (slot == -1) {
15868275SEric Cheng 		mutex_exit(bgep->genlock);
15878275SEric Cheng 		return (EINVAL);
15888275SEric Cheng 	}
15898275SEric Cheng 
15908275SEric Cheng 	ASSERT(bgep->curr_addr[slot].set);
15918275SEric Cheng 	mutex_exit(bgep->genlock);
15928275SEric Cheng 
15938275SEric Cheng 	if ((err = bge_unicst_set(bgep, zero_addr, slot)) != 0)
15948275SEric Cheng 		return (err);
15958275SEric Cheng 
15968275SEric Cheng 	if (rinfop == NULL || ether_cmp(mac_addr, rrp->mac_addr_val) != 0)
15978275SEric Cheng 		return (EINVAL);
15988275SEric Cheng 
15998275SEric Cheng 	start = rinfop->start;
16008275SEric Cheng 	rulep[start].mask_value = 0;
16018275SEric Cheng 	rulep[start].control = 0;
16028275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_MASK_REG(start), rulep[start].mask_value);
16038275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(start), rulep[start].control);
16048275SEric Cheng 	start++;
16058275SEric Cheng 	rulep[start].mask_value = 0;
16068275SEric Cheng 	rulep[start].control = 0;
16078275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_MASK_REG(start), rulep[start].mask_value);
16088275SEric Cheng 	bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(start), rulep[start].control);
16098275SEric Cheng 
16108275SEric Cheng 	kmem_free(rinfop, sizeof (bge_rule_info_t));
16118275SEric Cheng 	rrp->mac_addr_rule = NULL;
16128275SEric Cheng 	bzero(rrp->mac_addr_val, ETHERADDRL);
16138275SEric Cheng 
16148275SEric Cheng 	mutex_enter(bgep->genlock);
16158275SEric Cheng 	bgep->curr_addr[slot].set = B_FALSE;
16168275SEric Cheng 	bgep->unicst_addr_avail++;
16178275SEric Cheng 	mutex_exit(bgep->genlock);
16188275SEric Cheng 
16198275SEric Cheng 	return (0);
16208275SEric Cheng }
16218275SEric Cheng 
16228275SEric Cheng static int
16238275SEric Cheng bge_flag_intr_enable(mac_intr_handle_t ih)
16248275SEric Cheng {
16258275SEric Cheng 	recv_ring_t *rrp = (recv_ring_t *)ih;
16268275SEric Cheng 	bge_t *bgep = rrp->bgep;
16278275SEric Cheng 
16288275SEric Cheng 	mutex_enter(bgep->genlock);
16298275SEric Cheng 	rrp->poll_flag = 0;
16308275SEric Cheng 	mutex_exit(bgep->genlock);
16318275SEric Cheng 
16328275SEric Cheng 	return (0);
16338275SEric Cheng }
16348275SEric Cheng 
16358275SEric Cheng static int
16368275SEric Cheng bge_flag_intr_disable(mac_intr_handle_t ih)
16378275SEric Cheng {
16388275SEric Cheng 	recv_ring_t *rrp = (recv_ring_t *)ih;
16398275SEric Cheng 	bge_t *bgep = rrp->bgep;
16408275SEric Cheng 
16418275SEric Cheng 	mutex_enter(bgep->genlock);
16428275SEric Cheng 	rrp->poll_flag = 1;
16438275SEric Cheng 	mutex_exit(bgep->genlock);
16448275SEric Cheng 
16458275SEric Cheng 	return (0);
16468275SEric Cheng }
16478275SEric Cheng 
16488275SEric Cheng static int
16498275SEric Cheng bge_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
16508275SEric Cheng {
16518275SEric Cheng 	recv_ring_t *rx_ring;
16528275SEric Cheng 
16538275SEric Cheng 	rx_ring = (recv_ring_t *)rh;
16548275SEric Cheng 	mutex_enter(rx_ring->rx_lock);
16558275SEric Cheng 	rx_ring->ring_gen_num = mr_gen_num;
16568275SEric Cheng 	mutex_exit(rx_ring->rx_lock);
16578275SEric Cheng 	return (0);
16588275SEric Cheng }
16598275SEric Cheng 
16608275SEric Cheng 
16618275SEric Cheng /*
16628275SEric Cheng  * Callback funtion for MAC layer to register all rings
16638275SEric Cheng  * for given ring_group, noted by rg_index.
16648275SEric Cheng  */
16658275SEric Cheng void
16668275SEric Cheng bge_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
16678275SEric Cheng     const int index, mac_ring_info_t *infop, mac_ring_handle_t rh)
16688275SEric Cheng {
16698275SEric Cheng 	bge_t *bgep = arg;
16708275SEric Cheng 	mac_intr_t *mintr;
16718275SEric Cheng 
16728275SEric Cheng 	switch (rtype) {
16738275SEric Cheng 	case MAC_RING_TYPE_RX: {
16748275SEric Cheng 		recv_ring_t *rx_ring;
16758275SEric Cheng 		ASSERT(rg_index >= 0 && rg_index < MIN(bgep->chipid.rx_rings,
16768275SEric Cheng 		    MAC_ADDRESS_REGS_MAX) && index == 0);
16778275SEric Cheng 
16788275SEric Cheng 		rx_ring = &bgep->recv[rg_index];
16798275SEric Cheng 		rx_ring->ring_handle = rh;
16808275SEric Cheng 
16818275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rx_ring;
16828275SEric Cheng 		infop->mri_start = bge_ring_start;
16838275SEric Cheng 		infop->mri_stop = NULL;
16848275SEric Cheng 		infop->mri_poll = bge_poll_ring;
16858275SEric Cheng 
16868275SEric Cheng 		mintr = &infop->mri_intr;
16878275SEric Cheng 		mintr->mi_handle = (mac_intr_handle_t)rx_ring;
16888275SEric Cheng 		mintr->mi_enable = bge_flag_intr_enable;
16898275SEric Cheng 		mintr->mi_disable = bge_flag_intr_disable;
16908275SEric Cheng 
16918275SEric Cheng 		break;
16928275SEric Cheng 	}
16938275SEric Cheng 	case MAC_RING_TYPE_TX:
16948275SEric Cheng 	default:
16958275SEric Cheng 		ASSERT(0);
16968275SEric Cheng 		break;
16978275SEric Cheng 	}
16988275SEric Cheng }
16998275SEric Cheng 
17008275SEric Cheng /*
17018275SEric Cheng  * Fill infop passed as argument
17028275SEric Cheng  * fill in respective ring_group info
17038275SEric Cheng  * Each group has a single ring in it. We keep it simple
17048275SEric Cheng  * and use the same internal handle for rings and groups.
17058275SEric Cheng  */
17068275SEric Cheng void
17078275SEric Cheng bge_fill_group(void *arg, mac_ring_type_t rtype, const int rg_index,
17088275SEric Cheng 	mac_group_info_t *infop, mac_group_handle_t gh)
17098275SEric Cheng {
17108275SEric Cheng 	bge_t *bgep = arg;
17118275SEric Cheng 
17128275SEric Cheng 	switch (rtype) {
17138275SEric Cheng 	case MAC_RING_TYPE_RX: {
17148275SEric Cheng 		recv_ring_t *rx_ring;
17158275SEric Cheng 
17168275SEric Cheng 		ASSERT(rg_index >= 0 && rg_index < MIN(bgep->chipid.rx_rings,
17178275SEric Cheng 		    MAC_ADDRESS_REGS_MAX));
17188275SEric Cheng 		rx_ring = &bgep->recv[rg_index];
17198275SEric Cheng 		rx_ring->ring_group_handle = gh;
17208275SEric Cheng 
17218275SEric Cheng 		infop->mgi_driver = (mac_group_driver_t)rx_ring;
17228275SEric Cheng 		infop->mgi_start = NULL;
17238275SEric Cheng 		infop->mgi_stop = NULL;
17248275SEric Cheng 		infop->mgi_addmac = bge_addmac;
17258275SEric Cheng 		infop->mgi_remmac = bge_remmac;
17268275SEric Cheng 		infop->mgi_count = 1;
17278275SEric Cheng 		break;
17288275SEric Cheng 	}
17298275SEric Cheng 	case MAC_RING_TYPE_TX:
17308275SEric Cheng 	default:
17318275SEric Cheng 		ASSERT(0);
17328275SEric Cheng 		break;
17338275SEric Cheng 	}
17348275SEric Cheng }
17358275SEric Cheng 
17362311Sseb /*ARGSUSED*/
17372311Sseb static boolean_t
17382311Sseb bge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
17392311Sseb {
17402331Skrgopi 	bge_t *bgep = arg;
17412331Skrgopi 
17422311Sseb 	switch (cap) {
17432311Sseb 	case MAC_CAPAB_HCKSUM: {
17442311Sseb 		uint32_t *txflags = cap_data;
17452311Sseb 
17462311Sseb 		*txflags = HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM;
17472311Sseb 		break;
17482311Sseb 	}
17498275SEric Cheng 	case MAC_CAPAB_RINGS: {
17508275SEric Cheng 		mac_capab_rings_t *cap_rings = cap_data;
17518275SEric Cheng 
17528275SEric Cheng 		/* Temporarily disable multiple tx rings. */
17538275SEric Cheng 		if (cap_rings->mr_type != MAC_RING_TYPE_RX)
17548275SEric Cheng 			return (B_FALSE);
17558275SEric Cheng 
17568275SEric Cheng 		cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
17578275SEric Cheng 		cap_rings->mr_rnum = cap_rings->mr_gnum =
17588275SEric Cheng 		    MIN(bgep->chipid.rx_rings, MAC_ADDRESS_REGS_MAX);
17598275SEric Cheng 		cap_rings->mr_rget = bge_fill_ring;
17608275SEric Cheng 		cap_rings->mr_gget = bge_fill_group;
17612331Skrgopi 		break;
17622331Skrgopi 	}
17632311Sseb 	default:
17642311Sseb 		return (B_FALSE);
17652311Sseb 	}
17662311Sseb 	return (B_TRUE);
17672311Sseb }
17682311Sseb 
17691369Sdduvall /*
17701369Sdduvall  * Loopback ioctl code
17711369Sdduvall  */
17721369Sdduvall 
17731369Sdduvall static lb_property_t loopmodes[] = {
17741369Sdduvall 	{ normal,	"normal",	BGE_LOOP_NONE		},
17751369Sdduvall 	{ external,	"1000Mbps",	BGE_LOOP_EXTERNAL_1000	},
17761369Sdduvall 	{ external,	"100Mbps",	BGE_LOOP_EXTERNAL_100	},
17771369Sdduvall 	{ external,	"10Mbps",	BGE_LOOP_EXTERNAL_10	},
17781369Sdduvall 	{ internal,	"PHY",		BGE_LOOP_INTERNAL_PHY	},
17791369Sdduvall 	{ internal,	"MAC",		BGE_LOOP_INTERNAL_MAC	}
17801369Sdduvall };
17811369Sdduvall 
17821369Sdduvall static enum ioc_reply
17831369Sdduvall bge_set_loop_mode(bge_t *bgep, uint32_t mode)
17841369Sdduvall {
17851369Sdduvall 	/*
17861369Sdduvall 	 * If the mode isn't being changed, there's nothing to do ...
17871369Sdduvall 	 */
17881369Sdduvall 	if (mode == bgep->param_loop_mode)
17891369Sdduvall 		return (IOC_ACK);
17901369Sdduvall 
17911369Sdduvall 	/*
17921369Sdduvall 	 * Validate the requested mode and prepare a suitable message
17931369Sdduvall 	 * to explain the link down/up cycle that the change will
17941369Sdduvall 	 * probably induce ...
17951369Sdduvall 	 */
17961369Sdduvall 	switch (mode) {
17971369Sdduvall 	default:
17981369Sdduvall 		return (IOC_INVAL);
17991369Sdduvall 
18001369Sdduvall 	case BGE_LOOP_NONE:
18011369Sdduvall 	case BGE_LOOP_EXTERNAL_1000:
18021369Sdduvall 	case BGE_LOOP_EXTERNAL_100:
18031369Sdduvall 	case BGE_LOOP_EXTERNAL_10:
18041369Sdduvall 	case BGE_LOOP_INTERNAL_PHY:
18051369Sdduvall 	case BGE_LOOP_INTERNAL_MAC:
18061369Sdduvall 		break;
18071369Sdduvall 	}
18081369Sdduvall 
18091369Sdduvall 	/*
18101369Sdduvall 	 * All OK; tell the caller to reprogram
18111369Sdduvall 	 * the PHY and/or MAC for the new mode ...
18121369Sdduvall 	 */
18131369Sdduvall 	bgep->param_loop_mode = mode;
18141369Sdduvall 	return (IOC_RESTART_ACK);
18151369Sdduvall }
18161369Sdduvall 
18171369Sdduvall static enum ioc_reply
18181369Sdduvall bge_loop_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
18191369Sdduvall {
18201369Sdduvall 	lb_info_sz_t *lbsp;
18211369Sdduvall 	lb_property_t *lbpp;
18221369Sdduvall 	uint32_t *lbmp;
18231369Sdduvall 	int cmd;
18241369Sdduvall 
18251369Sdduvall 	_NOTE(ARGUNUSED(wq))
18261369Sdduvall 
18271369Sdduvall 	/*
18281369Sdduvall 	 * Validate format of ioctl
18291369Sdduvall 	 */
18301369Sdduvall 	if (mp->b_cont == NULL)
18311369Sdduvall 		return (IOC_INVAL);
18321369Sdduvall 
18331369Sdduvall 	cmd = iocp->ioc_cmd;
18341369Sdduvall 	switch (cmd) {
18351369Sdduvall 	default:
18361369Sdduvall 		/* NOTREACHED */
18371369Sdduvall 		bge_error(bgep, "bge_loop_ioctl: invalid cmd 0x%x", cmd);
18381369Sdduvall 		return (IOC_INVAL);
18391369Sdduvall 
18401369Sdduvall 	case LB_GET_INFO_SIZE:
18411369Sdduvall 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
18421369Sdduvall 			return (IOC_INVAL);
18437099Syt223700 		lbsp = (void *)mp->b_cont->b_rptr;
18441369Sdduvall 		*lbsp = sizeof (loopmodes);
18451369Sdduvall 		return (IOC_REPLY);
18461369Sdduvall 
18471369Sdduvall 	case LB_GET_INFO:
18481369Sdduvall 		if (iocp->ioc_count != sizeof (loopmodes))
18491369Sdduvall 			return (IOC_INVAL);
18507099Syt223700 		lbpp = (void *)mp->b_cont->b_rptr;
18511369Sdduvall 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
18521369Sdduvall 		return (IOC_REPLY);
18531369Sdduvall 
18541369Sdduvall 	case LB_GET_MODE:
18551369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
18561369Sdduvall 			return (IOC_INVAL);
18577099Syt223700 		lbmp = (void *)mp->b_cont->b_rptr;
18581369Sdduvall 		*lbmp = bgep->param_loop_mode;
18591369Sdduvall 		return (IOC_REPLY);
18601369Sdduvall 
18611369Sdduvall 	case LB_SET_MODE:
18621369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
18631369Sdduvall 			return (IOC_INVAL);
18647099Syt223700 		lbmp = (void *)mp->b_cont->b_rptr;
18651369Sdduvall 		return (bge_set_loop_mode(bgep, *lbmp));
18661369Sdduvall 	}
18671369Sdduvall }
18681369Sdduvall 
18691369Sdduvall /*
18701369Sdduvall  * Specific bge IOCTLs, the gld module handles the generic ones.
18711369Sdduvall  */
18721369Sdduvall static void
18731369Sdduvall bge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
18741369Sdduvall {
18751369Sdduvall 	bge_t *bgep = arg;
18761369Sdduvall 	struct iocblk *iocp;
18771369Sdduvall 	enum ioc_reply status;
18781369Sdduvall 	boolean_t need_privilege;
18791369Sdduvall 	int err;
18801369Sdduvall 	int cmd;
18811369Sdduvall 
18821369Sdduvall 	/*
18831369Sdduvall 	 * Validate the command before bothering with the mutex ...
18841369Sdduvall 	 */
18857099Syt223700 	iocp = (void *)mp->b_rptr;
18861369Sdduvall 	iocp->ioc_error = 0;
18871369Sdduvall 	need_privilege = B_TRUE;
18881369Sdduvall 	cmd = iocp->ioc_cmd;
18891369Sdduvall 	switch (cmd) {
18901369Sdduvall 	default:
18911369Sdduvall 		miocnak(wq, mp, 0, EINVAL);
18921369Sdduvall 		return;
18931369Sdduvall 
18941369Sdduvall 	case BGE_MII_READ:
18951369Sdduvall 	case BGE_MII_WRITE:
18961369Sdduvall 	case BGE_SEE_READ:
18971369Sdduvall 	case BGE_SEE_WRITE:
18982675Szh199473 	case BGE_FLASH_READ:
18992675Szh199473 	case BGE_FLASH_WRITE:
19001369Sdduvall 	case BGE_DIAG:
19011369Sdduvall 	case BGE_PEEK:
19021369Sdduvall 	case BGE_POKE:
19031369Sdduvall 	case BGE_PHY_RESET:
19041369Sdduvall 	case BGE_SOFT_RESET:
19051369Sdduvall 	case BGE_HARD_RESET:
19061369Sdduvall 		break;
19071369Sdduvall 
19081369Sdduvall 	case LB_GET_INFO_SIZE:
19091369Sdduvall 	case LB_GET_INFO:
19101369Sdduvall 	case LB_GET_MODE:
19111369Sdduvall 		need_privilege = B_FALSE;
19121369Sdduvall 		/* FALLTHRU */
19131369Sdduvall 	case LB_SET_MODE:
19141369Sdduvall 		break;
19151369Sdduvall 
19161369Sdduvall 	}
19171369Sdduvall 
19181369Sdduvall 	if (need_privilege) {
19191369Sdduvall 		/*
19201369Sdduvall 		 * Check for specific net_config privilege on Solaris 10+.
19211369Sdduvall 		 */
19222681Sgs150176 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
19231369Sdduvall 		if (err != 0) {
19241369Sdduvall 			miocnak(wq, mp, 0, err);
19251369Sdduvall 			return;
19261369Sdduvall 		}
19271369Sdduvall 	}
19281369Sdduvall 
19291369Sdduvall 	mutex_enter(bgep->genlock);
19301865Sdilpreet 	if (!(bgep->progress & PROGRESS_INTR)) {
19311865Sdilpreet 		/* can happen during autorecovery */
19321865Sdilpreet 		mutex_exit(bgep->genlock);
19331865Sdilpreet 		miocnak(wq, mp, 0, EIO);
19341865Sdilpreet 		return;
19351865Sdilpreet 	}
19361369Sdduvall 
19371369Sdduvall 	switch (cmd) {
19381369Sdduvall 	default:
19391369Sdduvall 		_NOTE(NOTREACHED)
19401369Sdduvall 		status = IOC_INVAL;
19411369Sdduvall 		break;
19421369Sdduvall 
19431369Sdduvall 	case BGE_MII_READ:
19441369Sdduvall 	case BGE_MII_WRITE:
19451369Sdduvall 	case BGE_SEE_READ:
19461369Sdduvall 	case BGE_SEE_WRITE:
19472675Szh199473 	case BGE_FLASH_READ:
19482675Szh199473 	case BGE_FLASH_WRITE:
19491369Sdduvall 	case BGE_DIAG:
19501369Sdduvall 	case BGE_PEEK:
19511369Sdduvall 	case BGE_POKE:
19521369Sdduvall 	case BGE_PHY_RESET:
19531369Sdduvall 	case BGE_SOFT_RESET:
19541369Sdduvall 	case BGE_HARD_RESET:
19551369Sdduvall 		status = bge_chip_ioctl(bgep, wq, mp, iocp);
19561369Sdduvall 		break;
19571369Sdduvall 
19581369Sdduvall 	case LB_GET_INFO_SIZE:
19591369Sdduvall 	case LB_GET_INFO:
19601369Sdduvall 	case LB_GET_MODE:
19611369Sdduvall 	case LB_SET_MODE:
19621369Sdduvall 		status = bge_loop_ioctl(bgep, wq, mp, iocp);
19631369Sdduvall 		break;
19641369Sdduvall 
19651369Sdduvall 	}
19661369Sdduvall 
19671369Sdduvall 	/*
19681369Sdduvall 	 * Do we need to reprogram the PHY and/or the MAC?
19691369Sdduvall 	 * Do it now, while we still have the mutex.
19701369Sdduvall 	 *
19711369Sdduvall 	 * Note: update the PHY first, 'cos it controls the
19721369Sdduvall 	 * speed/duplex parameters that the MAC code uses.
19731369Sdduvall 	 */
19741369Sdduvall 	switch (status) {
19751369Sdduvall 	case IOC_RESTART_REPLY:
19761369Sdduvall 	case IOC_RESTART_ACK:
19775903Ssowmini 		if (bge_reprogram(bgep) == IOC_INVAL)
19781865Sdilpreet 			status = IOC_INVAL;
19791369Sdduvall 		break;
19801369Sdduvall 	}
19811369Sdduvall 
19821865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
19831865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
19841865Sdilpreet 		status = IOC_INVAL;
19851865Sdilpreet 	}
19861865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
19871865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
19881865Sdilpreet 		status = IOC_INVAL;
19891865Sdilpreet 	}
19901369Sdduvall 	mutex_exit(bgep->genlock);
19911369Sdduvall 
19921369Sdduvall 	/*
19931369Sdduvall 	 * Finally, decide how to reply
19941369Sdduvall 	 */
19951369Sdduvall 	switch (status) {
19961369Sdduvall 	default:
19971369Sdduvall 	case IOC_INVAL:
19981369Sdduvall 		/*
19991369Sdduvall 		 * Error, reply with a NAK and EINVAL or the specified error
20001369Sdduvall 		 */
20011369Sdduvall 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
20024588Sml149210 		    EINVAL : iocp->ioc_error);
20031369Sdduvall 		break;
20041369Sdduvall 
20051369Sdduvall 	case IOC_DONE:
20061369Sdduvall 		/*
20071369Sdduvall 		 * OK, reply already sent
20081369Sdduvall 		 */
20091369Sdduvall 		break;
20101369Sdduvall 
20111369Sdduvall 	case IOC_RESTART_ACK:
20121369Sdduvall 	case IOC_ACK:
20131369Sdduvall 		/*
20141369Sdduvall 		 * OK, reply with an ACK
20151369Sdduvall 		 */
20161369Sdduvall 		miocack(wq, mp, 0, 0);
20171369Sdduvall 		break;
20181369Sdduvall 
20191369Sdduvall 	case IOC_RESTART_REPLY:
20201369Sdduvall 	case IOC_REPLY:
20211369Sdduvall 		/*
20221369Sdduvall 		 * OK, send prepared reply as ACK or NAK
20231369Sdduvall 		 */
20241369Sdduvall 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
20254588Sml149210 		    M_IOCACK : M_IOCNAK;
20261369Sdduvall 		qreply(wq, mp);
20271369Sdduvall 		break;
20281369Sdduvall 	}
20291369Sdduvall }
20301369Sdduvall 
20311369Sdduvall /*
20321369Sdduvall  * ========== Per-instance setup/teardown code ==========
20331369Sdduvall  */
20341369Sdduvall 
20351369Sdduvall #undef	BGE_DBG
20361369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
20373334Sgs150176 /*
20383334Sgs150176  * Allocate an area of memory and a DMA handle for accessing it
20393334Sgs150176  */
20403334Sgs150176 static int
20413334Sgs150176 bge_alloc_dma_mem(bge_t *bgep, size_t memsize, ddi_device_acc_attr_t *attr_p,
20423334Sgs150176 	uint_t dma_flags, dma_area_t *dma_p)
20433334Sgs150176 {
20443334Sgs150176 	caddr_t va;
20453334Sgs150176 	int err;
20463334Sgs150176 
20473334Sgs150176 	BGE_TRACE(("bge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
20484588Sml149210 	    (void *)bgep, memsize, attr_p, dma_flags, dma_p));
20493334Sgs150176 
20503334Sgs150176 	/*
20513334Sgs150176 	 * Allocate handle
20523334Sgs150176 	 */
20533334Sgs150176 	err = ddi_dma_alloc_handle(bgep->devinfo, &dma_attr,
20544588Sml149210 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_hdl);
20553334Sgs150176 	if (err != DDI_SUCCESS)
20563334Sgs150176 		return (DDI_FAILURE);
20573334Sgs150176 
20583334Sgs150176 	/*
20593334Sgs150176 	 * Allocate memory
20603334Sgs150176 	 */
20613334Sgs150176 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
20624588Sml149210 	    dma_flags, DDI_DMA_DONTWAIT, NULL, &va, &dma_p->alength,
20634588Sml149210 	    &dma_p->acc_hdl);
20643334Sgs150176 	if (err != DDI_SUCCESS)
20653334Sgs150176 		return (DDI_FAILURE);
20663334Sgs150176 
20673334Sgs150176 	/*
20683334Sgs150176 	 * Bind the two together
20693334Sgs150176 	 */
20703334Sgs150176 	dma_p->mem_va = va;
20713334Sgs150176 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
20724588Sml149210 	    va, dma_p->alength, dma_flags, DDI_DMA_DONTWAIT, NULL,
20734588Sml149210 	    &dma_p->cookie, &dma_p->ncookies);
20743334Sgs150176 
20753334Sgs150176 	BGE_DEBUG(("bge_alloc_dma_mem(): bind %d bytes; err %d, %d cookies",
20764588Sml149210 	    dma_p->alength, err, dma_p->ncookies));
20773334Sgs150176 
20783334Sgs150176 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
20793334Sgs150176 		return (DDI_FAILURE);
20803334Sgs150176 
20813334Sgs150176 	dma_p->nslots = ~0U;
20823334Sgs150176 	dma_p->size = ~0U;
20833334Sgs150176 	dma_p->token = ~0U;
20843334Sgs150176 	dma_p->offset = 0;
20853334Sgs150176 	return (DDI_SUCCESS);
20863334Sgs150176 }
20873334Sgs150176 
20883334Sgs150176 /*
20893334Sgs150176  * Free one allocated area of DMAable memory
20903334Sgs150176  */
20913334Sgs150176 static void
20923334Sgs150176 bge_free_dma_mem(dma_area_t *dma_p)
20933334Sgs150176 {
20943334Sgs150176 	if (dma_p->dma_hdl != NULL) {
20953334Sgs150176 		if (dma_p->ncookies) {
20963334Sgs150176 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
20973334Sgs150176 			dma_p->ncookies = 0;
20983334Sgs150176 		}
20993334Sgs150176 		ddi_dma_free_handle(&dma_p->dma_hdl);
21003334Sgs150176 		dma_p->dma_hdl = NULL;
21013334Sgs150176 	}
21023334Sgs150176 
21033334Sgs150176 	if (dma_p->acc_hdl != NULL) {
21043334Sgs150176 		ddi_dma_mem_free(&dma_p->acc_hdl);
21053334Sgs150176 		dma_p->acc_hdl = NULL;
21063334Sgs150176 	}
21073334Sgs150176 }
21081369Sdduvall /*
21091369Sdduvall  * Utility routine to carve a slice off a chunk of allocated memory,
21101369Sdduvall  * updating the chunk descriptor accordingly.  The size of the slice
21111369Sdduvall  * is given by the product of the <qty> and <size> parameters.
21121369Sdduvall  */
21131369Sdduvall static void
21141369Sdduvall bge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
21151369Sdduvall 	uint32_t qty, uint32_t size)
21161369Sdduvall {
21171369Sdduvall 	static uint32_t sequence = 0xbcd5704a;
21181369Sdduvall 	size_t totsize;
21191369Sdduvall 
21201369Sdduvall 	totsize = qty*size;
21211369Sdduvall 	ASSERT(totsize <= chunk->alength);
21221369Sdduvall 
21231369Sdduvall 	*slice = *chunk;
21241369Sdduvall 	slice->nslots = qty;
21251369Sdduvall 	slice->size = size;
21261369Sdduvall 	slice->alength = totsize;
21271369Sdduvall 	slice->token = ++sequence;
21281369Sdduvall 
21291369Sdduvall 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
21301369Sdduvall 	chunk->alength -= totsize;
21311369Sdduvall 	chunk->offset += totsize;
21321369Sdduvall 	chunk->cookie.dmac_laddress += totsize;
21331369Sdduvall 	chunk->cookie.dmac_size -= totsize;
21341369Sdduvall }
21351369Sdduvall 
21361369Sdduvall /*
21371369Sdduvall  * Initialise the specified Receive Producer (Buffer) Ring, using
21381369Sdduvall  * the information in the <dma_area> descriptors that it contains
21391369Sdduvall  * to set up all the other fields. This routine should be called
21401369Sdduvall  * only once for each ring.
21411369Sdduvall  */
21421369Sdduvall static void
21431369Sdduvall bge_init_buff_ring(bge_t *bgep, uint64_t ring)
21441369Sdduvall {
21451369Sdduvall 	buff_ring_t *brp;
21461369Sdduvall 	bge_status_t *bsp;
21471369Sdduvall 	sw_rbd_t *srbdp;
21481369Sdduvall 	dma_area_t pbuf;
21491369Sdduvall 	uint32_t bufsize;
21501369Sdduvall 	uint32_t nslots;
21511369Sdduvall 	uint32_t slot;
21521369Sdduvall 	uint32_t split;
21531369Sdduvall 
21541369Sdduvall 	static bge_regno_t nic_ring_addrs[BGE_BUFF_RINGS_MAX] = {
21551369Sdduvall 		NIC_MEM_SHADOW_BUFF_STD,
21561369Sdduvall 		NIC_MEM_SHADOW_BUFF_JUMBO,
21571369Sdduvall 		NIC_MEM_SHADOW_BUFF_MINI
21581369Sdduvall 	};
21591369Sdduvall 	static bge_regno_t mailbox_regs[BGE_BUFF_RINGS_MAX] = {
21601369Sdduvall 		RECV_STD_PROD_INDEX_REG,
21611369Sdduvall 		RECV_JUMBO_PROD_INDEX_REG,
21621369Sdduvall 		RECV_MINI_PROD_INDEX_REG
21631369Sdduvall 	};
21641369Sdduvall 	static bge_regno_t buff_cons_xref[BGE_BUFF_RINGS_MAX] = {
21651369Sdduvall 		STATUS_STD_BUFF_CONS_INDEX,
21661369Sdduvall 		STATUS_JUMBO_BUFF_CONS_INDEX,
21671369Sdduvall 		STATUS_MINI_BUFF_CONS_INDEX
21681369Sdduvall 	};
21691369Sdduvall 
21701369Sdduvall 	BGE_TRACE(("bge_init_buff_ring($%p, %d)",
21714588Sml149210 	    (void *)bgep, ring));
21721369Sdduvall 
21731369Sdduvall 	brp = &bgep->buff[ring];
21741369Sdduvall 	nslots = brp->desc.nslots;
21751369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
21761369Sdduvall 	bufsize = brp->buf[0].size;
21771369Sdduvall 
21781369Sdduvall 	/*
21791369Sdduvall 	 * Set up the copy of the h/w RCB
21801369Sdduvall 	 *
21811369Sdduvall 	 * Note: unlike Send & Receive Return Rings, (where the max_len
21821369Sdduvall 	 * field holds the number of slots), in a Receive Buffer Ring
21831369Sdduvall 	 * this field indicates the size of each buffer in the ring.
21841369Sdduvall 	 */
21851369Sdduvall 	brp->hw_rcb.host_ring_addr = brp->desc.cookie.dmac_laddress;
21867099Syt223700 	brp->hw_rcb.max_len = (uint16_t)bufsize;
21871369Sdduvall 	brp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
21881369Sdduvall 	brp->hw_rcb.nic_ring_addr = nic_ring_addrs[ring];
21891369Sdduvall 
21901369Sdduvall 	/*
21911369Sdduvall 	 * Other one-off initialisation of per-ring data
21921369Sdduvall 	 */
21931369Sdduvall 	brp->bgep = bgep;
21941369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
21951369Sdduvall 	brp->cons_index_p = &bsp->buff_cons_index[buff_cons_xref[ring]];
21961369Sdduvall 	brp->chip_mbx_reg = mailbox_regs[ring];
21971369Sdduvall 	mutex_init(brp->rf_lock, NULL, MUTEX_DRIVER,
21981369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
21991369Sdduvall 
22001369Sdduvall 	/*
22011369Sdduvall 	 * Allocate the array of s/w Receive Buffer Descriptors
22021369Sdduvall 	 */
22031369Sdduvall 	srbdp = kmem_zalloc(nslots*sizeof (*srbdp), KM_SLEEP);
22041369Sdduvall 	brp->sw_rbds = srbdp;
22051369Sdduvall 
22061369Sdduvall 	/*
22071369Sdduvall 	 * Now initialise each array element once and for all
22081369Sdduvall 	 */
22091369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
22101369Sdduvall 		pbuf = brp->buf[split];
22111369Sdduvall 		for (slot = 0; slot < nslots/BGE_SPLIT; ++srbdp, ++slot)
22121369Sdduvall 			bge_slice_chunk(&srbdp->pbuf, &pbuf, 1, bufsize);
22131369Sdduvall 		ASSERT(pbuf.alength == 0);
22141369Sdduvall 	}
22151369Sdduvall }
22161369Sdduvall 
22171369Sdduvall /*
22181369Sdduvall  * Clean up initialisation done above before the memory is freed
22191369Sdduvall  */
22201369Sdduvall static void
22211369Sdduvall bge_fini_buff_ring(bge_t *bgep, uint64_t ring)
22221369Sdduvall {
22231369Sdduvall 	buff_ring_t *brp;
22241369Sdduvall 	sw_rbd_t *srbdp;
22251369Sdduvall 
22261369Sdduvall 	BGE_TRACE(("bge_fini_buff_ring($%p, %d)",
22274588Sml149210 	    (void *)bgep, ring));
22281369Sdduvall 
22291369Sdduvall 	brp = &bgep->buff[ring];
22301369Sdduvall 	srbdp = brp->sw_rbds;
22311369Sdduvall 	kmem_free(srbdp, brp->desc.nslots*sizeof (*srbdp));
22321369Sdduvall 
22331369Sdduvall 	mutex_destroy(brp->rf_lock);
22341369Sdduvall }
22351369Sdduvall 
22361369Sdduvall /*
22371369Sdduvall  * Initialise the specified Receive (Return) Ring, using the
22381369Sdduvall  * information in the <dma_area> descriptors that it contains
22391369Sdduvall  * to set up all the other fields. This routine should be called
22401369Sdduvall  * only once for each ring.
22411369Sdduvall  */
22421369Sdduvall static void
22431369Sdduvall bge_init_recv_ring(bge_t *bgep, uint64_t ring)
22441369Sdduvall {
22451369Sdduvall 	recv_ring_t *rrp;
22461369Sdduvall 	bge_status_t *bsp;
22471369Sdduvall 	uint32_t nslots;
22481369Sdduvall 
22491369Sdduvall 	BGE_TRACE(("bge_init_recv_ring($%p, %d)",
22504588Sml149210 	    (void *)bgep, ring));
22511369Sdduvall 
22521369Sdduvall 	/*
22531369Sdduvall 	 * The chip architecture requires that receive return rings have
22541369Sdduvall 	 * 512 or 1024 or 2048 elements per ring.  See 570X-PG108-R page 103.
22551369Sdduvall 	 */
22561369Sdduvall 	rrp = &bgep->recv[ring];
22571369Sdduvall 	nslots = rrp->desc.nslots;
22581369Sdduvall 	ASSERT(nslots == 0 || nslots == 512 ||
22594588Sml149210 	    nslots == 1024 || nslots == 2048);
22601369Sdduvall 
22611369Sdduvall 	/*
22621369Sdduvall 	 * Set up the copy of the h/w RCB
22631369Sdduvall 	 */
22641369Sdduvall 	rrp->hw_rcb.host_ring_addr = rrp->desc.cookie.dmac_laddress;
22657099Syt223700 	rrp->hw_rcb.max_len = (uint16_t)nslots;
22661369Sdduvall 	rrp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
22671369Sdduvall 	rrp->hw_rcb.nic_ring_addr = 0;
22681369Sdduvall 
22691369Sdduvall 	/*
22701369Sdduvall 	 * Other one-off initialisation of per-ring data
22711369Sdduvall 	 */
22721369Sdduvall 	rrp->bgep = bgep;
22731369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
22741369Sdduvall 	rrp->prod_index_p = RECV_INDEX_P(bsp, ring);
22751369Sdduvall 	rrp->chip_mbx_reg = RECV_RING_CONS_INDEX_REG(ring);
22761369Sdduvall 	mutex_init(rrp->rx_lock, NULL, MUTEX_DRIVER,
22771369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
22781369Sdduvall }
22791369Sdduvall 
22801369Sdduvall 
22811369Sdduvall /*
22821369Sdduvall  * Clean up initialisation done above before the memory is freed
22831369Sdduvall  */
22841369Sdduvall static void
22851369Sdduvall bge_fini_recv_ring(bge_t *bgep, uint64_t ring)
22861369Sdduvall {
22871369Sdduvall 	recv_ring_t *rrp;
22881369Sdduvall 
22891369Sdduvall 	BGE_TRACE(("bge_fini_recv_ring($%p, %d)",
22904588Sml149210 	    (void *)bgep, ring));
22911369Sdduvall 
22921369Sdduvall 	rrp = &bgep->recv[ring];
22931369Sdduvall 	if (rrp->rx_softint)
22941369Sdduvall 		ddi_remove_softintr(rrp->rx_softint);
22951369Sdduvall 	mutex_destroy(rrp->rx_lock);
22961369Sdduvall }
22971369Sdduvall 
22981369Sdduvall /*
22991369Sdduvall  * Initialise the specified Send Ring, using the information in the
23001369Sdduvall  * <dma_area> descriptors that it contains to set up all the other
23011369Sdduvall  * fields. This routine should be called only once for each ring.
23021369Sdduvall  */
23031369Sdduvall static void
23041369Sdduvall bge_init_send_ring(bge_t *bgep, uint64_t ring)
23051369Sdduvall {
23061369Sdduvall 	send_ring_t *srp;
23071369Sdduvall 	bge_status_t *bsp;
23081369Sdduvall 	sw_sbd_t *ssbdp;
23091369Sdduvall 	dma_area_t desc;
23101369Sdduvall 	dma_area_t pbuf;
23111369Sdduvall 	uint32_t nslots;
23121369Sdduvall 	uint32_t slot;
23131369Sdduvall 	uint32_t split;
23143334Sgs150176 	sw_txbuf_t *txbuf;
23151369Sdduvall 
23161369Sdduvall 	BGE_TRACE(("bge_init_send_ring($%p, %d)",
23174588Sml149210 	    (void *)bgep, ring));
23181369Sdduvall 
23191369Sdduvall 	/*
23201369Sdduvall 	 * The chip architecture requires that host-based send rings
23211369Sdduvall 	 * have 512 elements per ring.  See 570X-PG102-R page 56.
23221369Sdduvall 	 */
23231369Sdduvall 	srp = &bgep->send[ring];
23241369Sdduvall 	nslots = srp->desc.nslots;
23251369Sdduvall 	ASSERT(nslots == 0 || nslots == 512);
23261369Sdduvall 
23271369Sdduvall 	/*
23281369Sdduvall 	 * Set up the copy of the h/w RCB
23291369Sdduvall 	 */
23301369Sdduvall 	srp->hw_rcb.host_ring_addr = srp->desc.cookie.dmac_laddress;
23317099Syt223700 	srp->hw_rcb.max_len = (uint16_t)nslots;
23321369Sdduvall 	srp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
23331369Sdduvall 	srp->hw_rcb.nic_ring_addr = NIC_MEM_SHADOW_SEND_RING(ring, nslots);
23341369Sdduvall 
23351369Sdduvall 	/*
23361369Sdduvall 	 * Other one-off initialisation of per-ring data
23371369Sdduvall 	 */
23381369Sdduvall 	srp->bgep = bgep;
23391369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
23401369Sdduvall 	srp->cons_index_p = SEND_INDEX_P(bsp, ring);
23411369Sdduvall 	srp->chip_mbx_reg = SEND_RING_HOST_INDEX_REG(ring);
23421369Sdduvall 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
23431369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
23443334Sgs150176 	mutex_init(srp->txbuf_lock, NULL, MUTEX_DRIVER,
23453334Sgs150176 	    DDI_INTR_PRI(bgep->intr_pri));
23463334Sgs150176 	mutex_init(srp->freetxbuf_lock, NULL, MUTEX_DRIVER,
23473334Sgs150176 	    DDI_INTR_PRI(bgep->intr_pri));
23481369Sdduvall 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
23491369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
23503334Sgs150176 	if (nslots == 0)
23513334Sgs150176 		return;
23521369Sdduvall 
23531369Sdduvall 	/*
23541369Sdduvall 	 * Allocate the array of s/w Send Buffer Descriptors
23551369Sdduvall 	 */
23561369Sdduvall 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
23573334Sgs150176 	txbuf = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (*txbuf), KM_SLEEP);
23583334Sgs150176 	srp->txbuf_head =
23593334Sgs150176 	    kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (bge_queue_item_t), KM_SLEEP);
23603334Sgs150176 	srp->pktp = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (send_pkt_t), KM_SLEEP);
23611369Sdduvall 	srp->sw_sbds = ssbdp;
23623334Sgs150176 	srp->txbuf = txbuf;
23633334Sgs150176 	srp->tx_buffers = BGE_SEND_BUF_NUM;
23643334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
23653334Sgs150176 	if (bgep->chipid.snd_buff_size > BGE_SEND_BUFF_SIZE_DEFAULT)
23663334Sgs150176 		srp->tx_array_max = BGE_SEND_BUF_ARRAY_JUMBO;
23673334Sgs150176 	else
23683334Sgs150176 		srp->tx_array_max = BGE_SEND_BUF_ARRAY;
23693334Sgs150176 	srp->tx_array = 1;
23701369Sdduvall 
23711369Sdduvall 	/*
23723334Sgs150176 	 * Chunk tx desc area
23731369Sdduvall 	 */
23741369Sdduvall 	desc = srp->desc;
23753334Sgs150176 	for (slot = 0; slot < nslots; ++ssbdp, ++slot) {
23763334Sgs150176 		bge_slice_chunk(&ssbdp->desc, &desc, 1,
23773334Sgs150176 		    sizeof (bge_sbd_t));
23783334Sgs150176 	}
23793334Sgs150176 	ASSERT(desc.alength == 0);
23803334Sgs150176 
23813334Sgs150176 	/*
23823334Sgs150176 	 * Chunk tx buffer area
23833334Sgs150176 	 */
23841369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
23853334Sgs150176 		pbuf = srp->buf[0][split];
23863334Sgs150176 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
23873334Sgs150176 			bge_slice_chunk(&txbuf->buf, &pbuf, 1,
23883334Sgs150176 			    bgep->chipid.snd_buff_size);
23893334Sgs150176 			txbuf++;
23901369Sdduvall 		}
23911369Sdduvall 		ASSERT(pbuf.alength == 0);
23921369Sdduvall 	}
23931369Sdduvall }
23941369Sdduvall 
23951369Sdduvall /*
23961369Sdduvall  * Clean up initialisation done above before the memory is freed
23971369Sdduvall  */
23981369Sdduvall static void
23991369Sdduvall bge_fini_send_ring(bge_t *bgep, uint64_t ring)
24001369Sdduvall {
24011369Sdduvall 	send_ring_t *srp;
24023334Sgs150176 	uint32_t array;
24033334Sgs150176 	uint32_t split;
24043334Sgs150176 	uint32_t nslots;
24051369Sdduvall 
24061369Sdduvall 	BGE_TRACE(("bge_fini_send_ring($%p, %d)",
24074588Sml149210 	    (void *)bgep, ring));
24081369Sdduvall 
24091369Sdduvall 	srp = &bgep->send[ring];
24103334Sgs150176 	mutex_destroy(srp->tc_lock);
24113334Sgs150176 	mutex_destroy(srp->freetxbuf_lock);
24123334Sgs150176 	mutex_destroy(srp->txbuf_lock);
24131369Sdduvall 	mutex_destroy(srp->tx_lock);
24143334Sgs150176 	nslots = srp->desc.nslots;
24153334Sgs150176 	if (nslots == 0)
24163334Sgs150176 		return;
24173334Sgs150176 
24183334Sgs150176 	for (array = 1; array < srp->tx_array; ++array)
24193334Sgs150176 		for (split = 0; split < BGE_SPLIT; ++split)
24203334Sgs150176 			bge_free_dma_mem(&srp->buf[array][split]);
24213334Sgs150176 	kmem_free(srp->sw_sbds, nslots*sizeof (*srp->sw_sbds));
24223334Sgs150176 	kmem_free(srp->txbuf_head, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf_head));
24233334Sgs150176 	kmem_free(srp->txbuf, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf));
24243334Sgs150176 	kmem_free(srp->pktp, BGE_SEND_BUF_MAX*sizeof (*srp->pktp));
24253334Sgs150176 	srp->sw_sbds = NULL;
24263334Sgs150176 	srp->txbuf_head = NULL;
24273334Sgs150176 	srp->txbuf = NULL;
24283334Sgs150176 	srp->pktp = NULL;
24291369Sdduvall }
24301369Sdduvall 
24311369Sdduvall /*
24321369Sdduvall  * Initialise all transmit, receive, and buffer rings.
24331369Sdduvall  */
24341865Sdilpreet void
24351369Sdduvall bge_init_rings(bge_t *bgep)
24361369Sdduvall {
24373334Sgs150176 	uint32_t ring;
24381369Sdduvall 
24391369Sdduvall 	BGE_TRACE(("bge_init_rings($%p)", (void *)bgep));
24401369Sdduvall 
24411369Sdduvall 	/*
24421369Sdduvall 	 * Perform one-off initialisation of each ring ...
24431369Sdduvall 	 */
24441369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
24451369Sdduvall 		bge_init_send_ring(bgep, ring);
24461369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
24471369Sdduvall 		bge_init_recv_ring(bgep, ring);
24481369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
24491369Sdduvall 		bge_init_buff_ring(bgep, ring);
24501369Sdduvall }
24511369Sdduvall 
24521369Sdduvall /*
24531369Sdduvall  * Undo the work of bge_init_rings() above before the memory is freed
24541369Sdduvall  */
24551865Sdilpreet void
24561369Sdduvall bge_fini_rings(bge_t *bgep)
24571369Sdduvall {
24583334Sgs150176 	uint32_t ring;
24591369Sdduvall 
24601369Sdduvall 	BGE_TRACE(("bge_fini_rings($%p)", (void *)bgep));
24611369Sdduvall 
24621369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
24631369Sdduvall 		bge_fini_buff_ring(bgep, ring);
24641369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
24651369Sdduvall 		bge_fini_recv_ring(bgep, ring);
24661369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
24671369Sdduvall 		bge_fini_send_ring(bgep, ring);
24681369Sdduvall }
24691369Sdduvall 
24701369Sdduvall /*
24713334Sgs150176  * Called from the bge_m_stop() to free the tx buffers which are
24723334Sgs150176  * allocated from the tx process.
24731369Sdduvall  */
24743334Sgs150176 void
24753334Sgs150176 bge_free_txbuf_arrays(send_ring_t *srp)
24761369Sdduvall {
24773334Sgs150176 	uint32_t array;
24783334Sgs150176 	uint32_t split;
24793334Sgs150176 
24803334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
24811369Sdduvall 
24821369Sdduvall 	/*
24833334Sgs150176 	 * Free the extra tx buffer DMA area
24841369Sdduvall 	 */
24853334Sgs150176 	for (array = 1; array < srp->tx_array; ++array)
24863334Sgs150176 		for (split = 0; split < BGE_SPLIT; ++split)
24873334Sgs150176 			bge_free_dma_mem(&srp->buf[array][split]);
24881369Sdduvall 
24891369Sdduvall 	/*
24903334Sgs150176 	 * Restore initial tx buffer numbers
24911369Sdduvall 	 */
24923334Sgs150176 	srp->tx_array = 1;
24933334Sgs150176 	srp->tx_buffers = BGE_SEND_BUF_NUM;
24943334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
24953334Sgs150176 	srp->tx_flow = 0;
24963334Sgs150176 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
24971369Sdduvall }
24981369Sdduvall 
24991369Sdduvall /*
25003334Sgs150176  * Called from tx process to allocate more tx buffers
25011369Sdduvall  */
25023334Sgs150176 bge_queue_item_t *
25033334Sgs150176 bge_alloc_txbuf_array(bge_t *bgep, send_ring_t *srp)
25041369Sdduvall {
25053334Sgs150176 	bge_queue_t *txbuf_queue;
25063334Sgs150176 	bge_queue_item_t *txbuf_item_last;
25073334Sgs150176 	bge_queue_item_t *txbuf_item;
25083334Sgs150176 	bge_queue_item_t *txbuf_item_rtn;
25093334Sgs150176 	sw_txbuf_t *txbuf;
25103334Sgs150176 	dma_area_t area;
25113334Sgs150176 	size_t txbuffsize;
25123334Sgs150176 	uint32_t slot;
25133334Sgs150176 	uint32_t array;
25143334Sgs150176 	uint32_t split;
25153334Sgs150176 	uint32_t err;
25163334Sgs150176 
25173334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
25183334Sgs150176 
25193334Sgs150176 	array = srp->tx_array;
25203334Sgs150176 	if (array >= srp->tx_array_max)
25213334Sgs150176 		return (NULL);
25223334Sgs150176 
25233334Sgs150176 	/*
25243334Sgs150176 	 * Allocate memory & handles for TX buffers
25253334Sgs150176 	 */
25263334Sgs150176 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
25273334Sgs150176 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
25283334Sgs150176 	for (split = 0; split < BGE_SPLIT; ++split) {
25293334Sgs150176 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
25304588Sml149210 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
25314588Sml149210 		    &srp->buf[array][split]);
25323334Sgs150176 		if (err != DDI_SUCCESS) {
25333334Sgs150176 			/* Free the last already allocated OK chunks */
25343334Sgs150176 			for (slot = 0; slot <= split; ++slot)
25353334Sgs150176 				bge_free_dma_mem(&srp->buf[array][slot]);
25363334Sgs150176 			srp->tx_alloc_fail++;
25373334Sgs150176 			return (NULL);
25381369Sdduvall 		}
25393334Sgs150176 	}
25403334Sgs150176 
25413334Sgs150176 	/*
25423334Sgs150176 	 * Chunk tx buffer area
25433334Sgs150176 	 */
25443334Sgs150176 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
25453334Sgs150176 	for (split = 0; split < BGE_SPLIT; ++split) {
25463334Sgs150176 		area = srp->buf[array][split];
25473334Sgs150176 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
25483334Sgs150176 			bge_slice_chunk(&txbuf->buf, &area, 1,
25493334Sgs150176 			    bgep->chipid.snd_buff_size);
25503334Sgs150176 			txbuf++;
25513334Sgs150176 		}
25521369Sdduvall 	}
25531369Sdduvall 
25543334Sgs150176 	/*
25553334Sgs150176 	 * Add above buffers to the tx buffer pop queue
25563334Sgs150176 	 */
25573334Sgs150176 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
25583334Sgs150176 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
25593334Sgs150176 	txbuf_item_last = NULL;
25603334Sgs150176 	for (slot = 0; slot < BGE_SEND_BUF_NUM; ++slot) {
25613334Sgs150176 		txbuf_item->item = txbuf;
25623334Sgs150176 		txbuf_item->next = txbuf_item_last;
25633334Sgs150176 		txbuf_item_last = txbuf_item;
25643334Sgs150176 		txbuf++;
25653334Sgs150176 		txbuf_item++;
25661369Sdduvall 	}
25673334Sgs150176 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
25683334Sgs150176 	txbuf_item_rtn = txbuf_item;
25693334Sgs150176 	txbuf_item++;
25703334Sgs150176 	txbuf_queue = srp->txbuf_pop_queue;
25713334Sgs150176 	mutex_enter(txbuf_queue->lock);
25723334Sgs150176 	txbuf_item->next = txbuf_queue->head;
25733334Sgs150176 	txbuf_queue->head = txbuf_item_last;
25743334Sgs150176 	txbuf_queue->count += BGE_SEND_BUF_NUM - 1;
25753334Sgs150176 	mutex_exit(txbuf_queue->lock);
25763334Sgs150176 
25773334Sgs150176 	srp->tx_array++;
25783334Sgs150176 	srp->tx_buffers += BGE_SEND_BUF_NUM;
25793334Sgs150176 	srp->tx_buffers_low = srp->tx_buffers / 4;
25803334Sgs150176 
25813334Sgs150176 	return (txbuf_item_rtn);
25821369Sdduvall }
25831369Sdduvall 
25841369Sdduvall /*
25851369Sdduvall  * This function allocates all the transmit and receive buffers
25863334Sgs150176  * and descriptors, in four chunks.
25871369Sdduvall  */
25881865Sdilpreet int
25891369Sdduvall bge_alloc_bufs(bge_t *bgep)
25901369Sdduvall {
25911369Sdduvall 	dma_area_t area;
25921369Sdduvall 	size_t rxbuffsize;
25931369Sdduvall 	size_t txbuffsize;
25941369Sdduvall 	size_t rxbuffdescsize;
25951369Sdduvall 	size_t rxdescsize;
25961369Sdduvall 	size_t txdescsize;
25973334Sgs150176 	uint32_t ring;
25983334Sgs150176 	uint32_t rx_rings = bgep->chipid.rx_rings;
25993334Sgs150176 	uint32_t tx_rings = bgep->chipid.tx_rings;
26001369Sdduvall 	int split;
26011369Sdduvall 	int err;
26021369Sdduvall 
26031369Sdduvall 	BGE_TRACE(("bge_alloc_bufs($%p)",
26044588Sml149210 	    (void *)bgep));
26051369Sdduvall 
26061908Sly149593 	rxbuffsize = BGE_STD_SLOTS_USED*bgep->chipid.std_buf_size;
26071369Sdduvall 	rxbuffsize += bgep->chipid.jumbo_slots*bgep->chipid.recv_jumbo_size;
26081369Sdduvall 	rxbuffsize += BGE_MINI_SLOTS_USED*BGE_MINI_BUFF_SIZE;
26091369Sdduvall 
26103334Sgs150176 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
26111369Sdduvall 	txbuffsize *= tx_rings;
26121369Sdduvall 
26131369Sdduvall 	rxdescsize = rx_rings*bgep->chipid.recv_slots;
26141369Sdduvall 	rxdescsize *= sizeof (bge_rbd_t);
26151369Sdduvall 
26161369Sdduvall 	rxbuffdescsize = BGE_STD_SLOTS_USED;
26171369Sdduvall 	rxbuffdescsize += bgep->chipid.jumbo_slots;
26181369Sdduvall 	rxbuffdescsize += BGE_MINI_SLOTS_USED;
26191369Sdduvall 	rxbuffdescsize *= sizeof (bge_rbd_t);
26201369Sdduvall 
26211369Sdduvall 	txdescsize = tx_rings*BGE_SEND_SLOTS_USED;
26221369Sdduvall 	txdescsize *= sizeof (bge_sbd_t);
26231369Sdduvall 	txdescsize += sizeof (bge_statistics_t);
26241369Sdduvall 	txdescsize += sizeof (bge_status_t);
26251369Sdduvall 	txdescsize += BGE_STATUS_PADDING;
26261369Sdduvall 
26271369Sdduvall 	/*
26283907Szh199473 	 * Enable PCI relaxed ordering only for RX/TX data buffers
26293907Szh199473 	 */
26303907Szh199473 	if (bge_relaxed_ordering)
26313907Szh199473 		dma_attr.dma_attr_flags |= DDI_DMA_RELAXED_ORDERING;
26323907Szh199473 
26333907Szh199473 	/*
26341369Sdduvall 	 * Allocate memory & handles for RX buffers
26351369Sdduvall 	 */
26361369Sdduvall 	ASSERT((rxbuffsize % BGE_SPLIT) == 0);
26371369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
26381369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxbuffsize/BGE_SPLIT,
26394588Sml149210 		    &bge_data_accattr, DDI_DMA_READ | BGE_DMA_MODE,
26404588Sml149210 		    &bgep->rx_buff[split]);
26411369Sdduvall 		if (err != DDI_SUCCESS)
26421369Sdduvall 			return (DDI_FAILURE);
26431369Sdduvall 	}
26441369Sdduvall 
26451369Sdduvall 	/*
26461369Sdduvall 	 * Allocate memory & handles for TX buffers
26471369Sdduvall 	 */
26481369Sdduvall 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
26491369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
26501369Sdduvall 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
26514588Sml149210 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
26524588Sml149210 		    &bgep->tx_buff[split]);
26531369Sdduvall 		if (err != DDI_SUCCESS)
26541369Sdduvall 			return (DDI_FAILURE);
26551369Sdduvall 	}
26561369Sdduvall 
26573907Szh199473 	dma_attr.dma_attr_flags &= ~DDI_DMA_RELAXED_ORDERING;
26583907Szh199473 
26591369Sdduvall 	/*
26601369Sdduvall 	 * Allocate memory & handles for receive return rings
26611369Sdduvall 	 */
26621369Sdduvall 	ASSERT((rxdescsize % rx_rings) == 0);
26631369Sdduvall 	for (split = 0; split < rx_rings; ++split) {
26641369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxdescsize/rx_rings,
26654588Sml149210 		    &bge_desc_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
26664588Sml149210 		    &bgep->rx_desc[split]);
26671369Sdduvall 		if (err != DDI_SUCCESS)
26681369Sdduvall 			return (DDI_FAILURE);
26691369Sdduvall 	}
26701369Sdduvall 
26711369Sdduvall 	/*
26721369Sdduvall 	 * Allocate memory & handles for buffer (producer) descriptor rings
26731369Sdduvall 	 */
26741369Sdduvall 	err = bge_alloc_dma_mem(bgep, rxbuffdescsize, &bge_desc_accattr,
26754588Sml149210 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->rx_desc[split]);
26761369Sdduvall 	if (err != DDI_SUCCESS)
26771369Sdduvall 		return (DDI_FAILURE);
26781369Sdduvall 
26791369Sdduvall 	/*
26801369Sdduvall 	 * Allocate memory & handles for TX descriptor rings,
26811369Sdduvall 	 * status block, and statistics area
26821369Sdduvall 	 */
26831369Sdduvall 	err = bge_alloc_dma_mem(bgep, txdescsize, &bge_desc_accattr,
26844588Sml149210 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->tx_desc);
26851369Sdduvall 	if (err != DDI_SUCCESS)
26861369Sdduvall 		return (DDI_FAILURE);
26871369Sdduvall 
26881369Sdduvall 	/*
26891369Sdduvall 	 * Now carve up each of the allocated areas ...
26901369Sdduvall 	 */
26911369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
26921369Sdduvall 		area = bgep->rx_buff[split];
26931369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].buf[split],
26944588Sml149210 		    &area, BGE_STD_SLOTS_USED/BGE_SPLIT,
26954588Sml149210 		    bgep->chipid.std_buf_size);
26961369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].buf[split],
26974588Sml149210 		    &area, bgep->chipid.jumbo_slots/BGE_SPLIT,
26984588Sml149210 		    bgep->chipid.recv_jumbo_size);
26991369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].buf[split],
27004588Sml149210 		    &area, BGE_MINI_SLOTS_USED/BGE_SPLIT,
27014588Sml149210 		    BGE_MINI_BUFF_SIZE);
27021369Sdduvall 	}
27031369Sdduvall 
27041369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
27051369Sdduvall 		area = bgep->tx_buff[split];
27061369Sdduvall 		for (ring = 0; ring < tx_rings; ++ring)
27073334Sgs150176 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
27084588Sml149210 			    &area, BGE_SEND_BUF_NUM/BGE_SPLIT,
27094588Sml149210 			    bgep->chipid.snd_buff_size);
27101369Sdduvall 		for (; ring < BGE_SEND_RINGS_MAX; ++ring)
27113334Sgs150176 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
27124588Sml149210 			    &area, 0, bgep->chipid.snd_buff_size);
27131369Sdduvall 	}
27141369Sdduvall 
27151369Sdduvall 	for (ring = 0; ring < rx_rings; ++ring)
27161369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &bgep->rx_desc[ring],
27174588Sml149210 		    bgep->chipid.recv_slots, sizeof (bge_rbd_t));
27181369Sdduvall 
27191369Sdduvall 	area = bgep->rx_desc[rx_rings];
27201369Sdduvall 	for (; ring < BGE_RECV_RINGS_MAX; ++ring)
27211369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &area,
27224588Sml149210 		    0, sizeof (bge_rbd_t));
27231369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].desc, &area,
27244588Sml149210 	    BGE_STD_SLOTS_USED, sizeof (bge_rbd_t));
27251369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].desc, &area,
27264588Sml149210 	    bgep->chipid.jumbo_slots, sizeof (bge_rbd_t));
27271369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].desc, &area,
27284588Sml149210 	    BGE_MINI_SLOTS_USED, sizeof (bge_rbd_t));
27291369Sdduvall 	ASSERT(area.alength == 0);
27301369Sdduvall 
27311369Sdduvall 	area = bgep->tx_desc;
27321369Sdduvall 	for (ring = 0; ring < tx_rings; ++ring)
27331369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
27344588Sml149210 		    BGE_SEND_SLOTS_USED, sizeof (bge_sbd_t));
27351369Sdduvall 	for (; ring < BGE_SEND_RINGS_MAX; ++ring)
27361369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
27374588Sml149210 		    0, sizeof (bge_sbd_t));
27381369Sdduvall 	bge_slice_chunk(&bgep->statistics, &area, 1, sizeof (bge_statistics_t));
27391369Sdduvall 	bge_slice_chunk(&bgep->status_block, &area, 1, sizeof (bge_status_t));
27401369Sdduvall 	ASSERT(area.alength == BGE_STATUS_PADDING);
27411369Sdduvall 	DMA_ZERO(bgep->status_block);
27421369Sdduvall 
27431369Sdduvall 	return (DDI_SUCCESS);
27441369Sdduvall }
27451369Sdduvall 
27461369Sdduvall /*
27471369Sdduvall  * This routine frees the transmit and receive buffers and descriptors.
27481369Sdduvall  * Make sure the chip is stopped before calling it!
27491369Sdduvall  */
27501865Sdilpreet void
27511369Sdduvall bge_free_bufs(bge_t *bgep)
27521369Sdduvall {
27531369Sdduvall 	int split;
27541369Sdduvall 
27551369Sdduvall 	BGE_TRACE(("bge_free_bufs($%p)",
27564588Sml149210 	    (void *)bgep));
27571369Sdduvall 
27581369Sdduvall 	bge_free_dma_mem(&bgep->tx_desc);
27591369Sdduvall 	for (split = 0; split < BGE_RECV_RINGS_SPLIT; ++split)
27601369Sdduvall 		bge_free_dma_mem(&bgep->rx_desc[split]);
27611369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
27621369Sdduvall 		bge_free_dma_mem(&bgep->tx_buff[split]);
27631369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
27641369Sdduvall 		bge_free_dma_mem(&bgep->rx_buff[split]);
27651369Sdduvall }
27661369Sdduvall 
27671369Sdduvall /*
27681369Sdduvall  * Determine (initial) MAC address ("BIA") to use for this interface
27691369Sdduvall  */
27701369Sdduvall 
27711369Sdduvall static void
27721369Sdduvall bge_find_mac_address(bge_t *bgep, chip_id_t *cidp)
27731369Sdduvall {
27741369Sdduvall 	struct ether_addr sysaddr;
27751369Sdduvall 	char propbuf[8];		/* "true" or "false", plus NUL	*/
27761369Sdduvall 	uchar_t *bytes;
27771369Sdduvall 	int *ints;
27781369Sdduvall 	uint_t nelts;
27791369Sdduvall 	int err;
27801369Sdduvall 
27811369Sdduvall 	BGE_TRACE(("bge_find_mac_address($%p)",
27824588Sml149210 	    (void *)bgep));
27831369Sdduvall 
27841369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: hw_mac_addr %012llx, => %s (%sset)",
27854588Sml149210 	    cidp->hw_mac_addr,
27864588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
27874588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
27881369Sdduvall 
27891369Sdduvall 	/*
27901369Sdduvall 	 * The "vendor's factory-set address" may already have
27911369Sdduvall 	 * been extracted from the chip, but if the property
27921369Sdduvall 	 * "local-mac-address" is set we use that instead.  It
27931369Sdduvall 	 * will normally be set by OBP, but it could also be
27941369Sdduvall 	 * specified in a .conf file(!)
27951369Sdduvall 	 *
27961369Sdduvall 	 * There doesn't seem to be a way to define byte-array
27971369Sdduvall 	 * properties in a .conf, so we check whether it looks
27981369Sdduvall 	 * like an array of 6 ints instead.
27991369Sdduvall 	 *
28001369Sdduvall 	 * Then, we check whether it looks like an array of 6
28011369Sdduvall 	 * bytes (which it should, if OBP set it).  If we can't
28021369Sdduvall 	 * make sense of it either way, we'll ignore it.
28031369Sdduvall 	 */
28041369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
28054588Sml149210 	    DDI_PROP_DONTPASS, localmac_propname, &ints, &nelts);
28061369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
28071369Sdduvall 		if (nelts == ETHERADDRL) {
28081369Sdduvall 			while (nelts--)
28091369Sdduvall 				cidp->vendor_addr.addr[nelts] = ints[nelts];
28102331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
28111369Sdduvall 		}
28121369Sdduvall 		ddi_prop_free(ints);
28131369Sdduvall 	}
28141369Sdduvall 
28151369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
28164588Sml149210 	    DDI_PROP_DONTPASS, localmac_propname, &bytes, &nelts);
28171369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
28181369Sdduvall 		if (nelts == ETHERADDRL) {
28191369Sdduvall 			while (nelts--)
28201369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
28212331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
28221369Sdduvall 		}
28231369Sdduvall 		ddi_prop_free(bytes);
28241369Sdduvall 	}
28251369Sdduvall 
28261369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +local %s (%sset)",
28274588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
28284588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
28291369Sdduvall 
28301369Sdduvall 	/*
28311369Sdduvall 	 * Look up the OBP property "local-mac-address?".  Note that even
28321369Sdduvall 	 * though its value is a string (which should be "true" or "false"),
28331369Sdduvall 	 * it can't be decoded by ddi_prop_lookup_string(9F).  So, we zero
28341369Sdduvall 	 * the buffer first and then fetch the property as an untyped array;
28351369Sdduvall 	 * this may or may not include a final NUL, but since there will
28361369Sdduvall 	 * always be one left at the end of the buffer we can now treat it
28371369Sdduvall 	 * as a string anyway.
28381369Sdduvall 	 */
28391369Sdduvall 	nelts = sizeof (propbuf);
28401369Sdduvall 	bzero(propbuf, nelts--);
28411369Sdduvall 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, bgep->devinfo,
28424588Sml149210 	    DDI_PROP_CANSLEEP, localmac_boolname, propbuf, (int *)&nelts);
28431369Sdduvall 
28441369Sdduvall 	/*
28451369Sdduvall 	 * Now, if the address still isn't set from the hardware (SEEPROM)
28461369Sdduvall 	 * or the OBP or .conf property, OR if the user has foolishly set
28471369Sdduvall 	 * 'local-mac-address? = false', use "the system address" instead
28481369Sdduvall 	 * (but only if it's non-null i.e. has been set from the IDPROM).
28491369Sdduvall 	 */
28502331Skrgopi 	if (cidp->vendor_addr.set == B_FALSE || strcmp(propbuf, "false") == 0)
28511369Sdduvall 		if (localetheraddr(NULL, &sysaddr) != 0) {
28521369Sdduvall 			ethaddr_copy(&sysaddr, cidp->vendor_addr.addr);
28532331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
28541369Sdduvall 		}
28551369Sdduvall 
28561369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +system %s (%sset)",
28574588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
28584588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
28591369Sdduvall 
28601369Sdduvall 	/*
28611369Sdduvall 	 * Finally(!), if there's a valid "mac-address" property (created
28621369Sdduvall 	 * if we netbooted from this interface), we must use this instead
28631369Sdduvall 	 * of any of the above to ensure that the NFS/install server doesn't
28641369Sdduvall 	 * get confused by the address changing as Solaris takes over!
28651369Sdduvall 	 */
28661369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
28674588Sml149210 	    DDI_PROP_DONTPASS, macaddr_propname, &bytes, &nelts);
28681369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
28691369Sdduvall 		if (nelts == ETHERADDRL) {
28701369Sdduvall 			while (nelts--)
28711369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
28722331Skrgopi 			cidp->vendor_addr.set = B_TRUE;
28731369Sdduvall 		}
28741369Sdduvall 		ddi_prop_free(bytes);
28751369Sdduvall 	}
28761369Sdduvall 
28771369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: =final %s (%sset)",
28784588Sml149210 	    ether_sprintf((void *)cidp->vendor_addr.addr),
28794588Sml149210 	    cidp->vendor_addr.set ? "" : "not "));
28801369Sdduvall }
28811369Sdduvall 
28821865Sdilpreet 
28831865Sdilpreet /*ARGSUSED*/
28841865Sdilpreet int
28851865Sdilpreet bge_check_acc_handle(bge_t *bgep, ddi_acc_handle_t handle)
28861865Sdilpreet {
28871865Sdilpreet 	ddi_fm_error_t de;
28881865Sdilpreet 
28891865Sdilpreet 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
28901865Sdilpreet 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
28911865Sdilpreet 	return (de.fme_status);
28921865Sdilpreet }
28931865Sdilpreet 
28941865Sdilpreet /*ARGSUSED*/
28951865Sdilpreet int
28961865Sdilpreet bge_check_dma_handle(bge_t *bgep, ddi_dma_handle_t handle)
28971865Sdilpreet {
28981865Sdilpreet 	ddi_fm_error_t de;
28991865Sdilpreet 
29001865Sdilpreet 	ASSERT(bgep->progress & PROGRESS_BUFS);
29011865Sdilpreet 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
29021865Sdilpreet 	return (de.fme_status);
29031865Sdilpreet }
29041865Sdilpreet 
29051865Sdilpreet /*
29061865Sdilpreet  * The IO fault service error handling callback function
29071865Sdilpreet  */
29081865Sdilpreet /*ARGSUSED*/
29091865Sdilpreet static int
29101865Sdilpreet bge_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
29111865Sdilpreet {
29121865Sdilpreet 	/*
29131865Sdilpreet 	 * as the driver can always deal with an error in any dma or
29141865Sdilpreet 	 * access handle, we can just return the fme_status value.
29151865Sdilpreet 	 */
29161865Sdilpreet 	pci_ereport_post(dip, err, NULL);
29171865Sdilpreet 	return (err->fme_status);
29181865Sdilpreet }
29191865Sdilpreet 
29201865Sdilpreet static void
29211865Sdilpreet bge_fm_init(bge_t *bgep)
29221865Sdilpreet {
29231865Sdilpreet 	ddi_iblock_cookie_t iblk;
29241865Sdilpreet 
29251865Sdilpreet 	/* Only register with IO Fault Services if we have some capability */
29261865Sdilpreet 	if (bgep->fm_capabilities) {
29271865Sdilpreet 		bge_reg_accattr.devacc_attr_access = DDI_FLAGERR_ACC;
29281865Sdilpreet 		dma_attr.dma_attr_flags = DDI_DMA_FLAGERR;
29291865Sdilpreet 
29301865Sdilpreet 		/* Register capabilities with IO Fault Services */
29311865Sdilpreet 		ddi_fm_init(bgep->devinfo, &bgep->fm_capabilities, &iblk);
29321865Sdilpreet 
29331865Sdilpreet 		/*
29341865Sdilpreet 		 * Initialize pci ereport capabilities if ereport capable
29351865Sdilpreet 		 */
29361865Sdilpreet 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
29371865Sdilpreet 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
29381865Sdilpreet 			pci_ereport_setup(bgep->devinfo);
29391865Sdilpreet 
29401865Sdilpreet 		/*
29411865Sdilpreet 		 * Register error callback if error callback capable
29421865Sdilpreet 		 */
29431865Sdilpreet 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
29441865Sdilpreet 			ddi_fm_handler_register(bgep->devinfo,
29454588Sml149210 			    bge_fm_error_cb, (void*) bgep);
29461865Sdilpreet 	} else {
29471865Sdilpreet 		/*
29481865Sdilpreet 		 * These fields have to be cleared of FMA if there are no
29491865Sdilpreet 		 * FMA capabilities at runtime.
29501865Sdilpreet 		 */
29511865Sdilpreet 		bge_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
29521865Sdilpreet 		dma_attr.dma_attr_flags = 0;
29531865Sdilpreet 	}
29541865Sdilpreet }
29551865Sdilpreet 
29561865Sdilpreet static void
29571865Sdilpreet bge_fm_fini(bge_t *bgep)
29581865Sdilpreet {
29591865Sdilpreet 	/* Only unregister FMA capabilities if we registered some */
29601865Sdilpreet 	if (bgep->fm_capabilities) {
29611865Sdilpreet 
29621865Sdilpreet 		/*
29631865Sdilpreet 		 * Release any resources allocated by pci_ereport_setup()
29641865Sdilpreet 		 */
29651865Sdilpreet 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
29661865Sdilpreet 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
29671865Sdilpreet 			pci_ereport_teardown(bgep->devinfo);
29681865Sdilpreet 
29691865Sdilpreet 		/*
29701865Sdilpreet 		 * Un-register error callback if error callback capable
29711865Sdilpreet 		 */
29721865Sdilpreet 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
29731865Sdilpreet 			ddi_fm_handler_unregister(bgep->devinfo);
29741865Sdilpreet 
29751865Sdilpreet 		/* Unregister from IO Fault Services */
29761865Sdilpreet 		ddi_fm_fini(bgep->devinfo);
29771865Sdilpreet 	}
29781865Sdilpreet }
29791865Sdilpreet 
29801369Sdduvall static void
29811408Srandyf #ifdef BGE_IPMI_ASF
29821408Srandyf bge_unattach(bge_t *bgep, uint_t asf_mode)
29831408Srandyf #else
29841369Sdduvall bge_unattach(bge_t *bgep)
29851408Srandyf #endif
29861369Sdduvall {
29871369Sdduvall 	BGE_TRACE(("bge_unattach($%p)",
29881369Sdduvall 		(void *)bgep));
29891369Sdduvall 
29901369Sdduvall 	/*
29911369Sdduvall 	 * Flag that no more activity may be initiated
29921369Sdduvall 	 */
29931369Sdduvall 	bgep->progress &= ~PROGRESS_READY;
29941369Sdduvall 
29951369Sdduvall 	/*
29961369Sdduvall 	 * Quiesce the PHY and MAC (leave it reset but still powered).
29971369Sdduvall 	 * Clean up and free all BGE data structures
29981369Sdduvall 	 */
29995107Seota 	if (bgep->periodic_id != NULL) {
30005107Seota 		ddi_periodic_delete(bgep->periodic_id);
30015107Seota 		bgep->periodic_id = NULL;
30021369Sdduvall 	}
30031369Sdduvall 	if (bgep->progress & PROGRESS_KSTATS)
30041369Sdduvall 		bge_fini_kstats(bgep);
30051369Sdduvall 	if (bgep->progress & PROGRESS_PHY)
30061369Sdduvall 		bge_phys_reset(bgep);
30071369Sdduvall 	if (bgep->progress & PROGRESS_HWINT) {
30081369Sdduvall 		mutex_enter(bgep->genlock);
30091408Srandyf #ifdef BGE_IPMI_ASF
30101865Sdilpreet 		if (bge_chip_reset(bgep, B_FALSE, asf_mode) != DDI_SUCCESS)
30111865Sdilpreet #else
30121865Sdilpreet 		if (bge_chip_reset(bgep, B_FALSE) != DDI_SUCCESS)
30131865Sdilpreet #endif
30141865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
30151865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
30161865Sdilpreet #ifdef BGE_IPMI_ASF
30171408Srandyf 		if (bgep->asf_enabled) {
30181408Srandyf 			/*
30191408Srandyf 			 * This register has been overlaid. We restore its
30201408Srandyf 			 * initial value here.
30211408Srandyf 			 */
30221408Srandyf 			bge_nic_put32(bgep, BGE_NIC_DATA_SIG_ADDR,
30231408Srandyf 			    BGE_NIC_DATA_SIG);
30241408Srandyf 		}
30251408Srandyf #endif
30261865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
30271865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
30281865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
30291865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
30301865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
30311865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
30321369Sdduvall 		mutex_exit(bgep->genlock);
30331369Sdduvall 	}
30341369Sdduvall 	if (bgep->progress & PROGRESS_INTR) {
30351865Sdilpreet 		bge_intr_disable(bgep);
30361369Sdduvall 		bge_fini_rings(bgep);
30371369Sdduvall 	}
30381865Sdilpreet 	if (bgep->progress & PROGRESS_HWINT) {
30391865Sdilpreet 		bge_rem_intrs(bgep);
30401865Sdilpreet 		rw_destroy(bgep->errlock);
30411865Sdilpreet 		mutex_destroy(bgep->softintrlock);
30421865Sdilpreet 		mutex_destroy(bgep->genlock);
30431865Sdilpreet 	}
30441369Sdduvall 	if (bgep->progress & PROGRESS_FACTOTUM)
30451369Sdduvall 		ddi_remove_softintr(bgep->factotum_id);
30461369Sdduvall 	if (bgep->progress & PROGRESS_RESCHED)
30473334Sgs150176 		ddi_remove_softintr(bgep->drain_id);
30481865Sdilpreet 	if (bgep->progress & PROGRESS_BUFS)
30491865Sdilpreet 		bge_free_bufs(bgep);
30501369Sdduvall 	if (bgep->progress & PROGRESS_REGS)
30511369Sdduvall 		ddi_regs_map_free(&bgep->io_handle);
30521369Sdduvall 	if (bgep->progress & PROGRESS_CFG)
30531369Sdduvall 		pci_config_teardown(&bgep->cfg_handle);
30541369Sdduvall 
30551865Sdilpreet 	bge_fm_fini(bgep);
30561865Sdilpreet 
30571369Sdduvall 	ddi_remove_minor_node(bgep->devinfo, NULL);
30583334Sgs150176 	kmem_free(bgep->pstats, sizeof (bge_statistics_reg_t));
30591369Sdduvall 	kmem_free(bgep, sizeof (*bgep));
30601369Sdduvall }
30611369Sdduvall 
30621369Sdduvall static int
30631369Sdduvall bge_resume(dev_info_t *devinfo)
30641369Sdduvall {
30651369Sdduvall 	bge_t *bgep;				/* Our private data	*/
30661369Sdduvall 	chip_id_t *cidp;
30671369Sdduvall 	chip_id_t chipid;
30681369Sdduvall 
30691369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
30701369Sdduvall 	if (bgep == NULL)
30711369Sdduvall 		return (DDI_FAILURE);
30721369Sdduvall 
30731369Sdduvall 	/*
30741369Sdduvall 	 * Refuse to resume if the data structures aren't consistent
30751369Sdduvall 	 */
30761369Sdduvall 	if (bgep->devinfo != devinfo)
30771369Sdduvall 		return (DDI_FAILURE);
30781369Sdduvall 
30791408Srandyf #ifdef BGE_IPMI_ASF
30801408Srandyf 	/*
30811408Srandyf 	 * Power management hasn't been supported in BGE now. If you
30821408Srandyf 	 * want to implement it, please add the ASF/IPMI related
30831408Srandyf 	 * code here.
30841408Srandyf 	 */
30851408Srandyf 
30861408Srandyf #endif
30871408Srandyf 
30881369Sdduvall 	/*
30891369Sdduvall 	 * Read chip ID & set up config space command register(s)
30901369Sdduvall 	 * Refuse to resume if the chip has changed its identity!
30911369Sdduvall 	 */
30921369Sdduvall 	cidp = &bgep->chipid;
30931865Sdilpreet 	mutex_enter(bgep->genlock);
30941369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, B_FALSE);
30951865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
30961865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
30971865Sdilpreet 		mutex_exit(bgep->genlock);
30981865Sdilpreet 		return (DDI_FAILURE);
30991865Sdilpreet 	}
31001865Sdilpreet 	mutex_exit(bgep->genlock);
31011369Sdduvall 	if (chipid.vendor != cidp->vendor)
31021369Sdduvall 		return (DDI_FAILURE);
31031369Sdduvall 	if (chipid.device != cidp->device)
31041369Sdduvall 		return (DDI_FAILURE);
31051369Sdduvall 	if (chipid.revision != cidp->revision)
31061369Sdduvall 		return (DDI_FAILURE);
31071369Sdduvall 	if (chipid.asic_rev != cidp->asic_rev)
31081369Sdduvall 		return (DDI_FAILURE);
31091369Sdduvall 
31101369Sdduvall 	/*
31111369Sdduvall 	 * All OK, reinitialise h/w & kick off GLD scheduling
31121369Sdduvall 	 */
31131369Sdduvall 	mutex_enter(bgep->genlock);
31141865Sdilpreet 	if (bge_restart(bgep, B_TRUE) != DDI_SUCCESS) {
31151865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
31161865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
31171865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
31181865Sdilpreet 		mutex_exit(bgep->genlock);
31191865Sdilpreet 		return (DDI_FAILURE);
31201865Sdilpreet 	}
31211865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
31221865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
31231865Sdilpreet 		mutex_exit(bgep->genlock);
31241865Sdilpreet 		return (DDI_FAILURE);
31251865Sdilpreet 	}
31261865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
31271865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
31281865Sdilpreet 		mutex_exit(bgep->genlock);
31291865Sdilpreet 		return (DDI_FAILURE);
31301865Sdilpreet 	}
31311369Sdduvall 	mutex_exit(bgep->genlock);
31321369Sdduvall 	return (DDI_SUCCESS);
31331369Sdduvall }
31341369Sdduvall 
31351369Sdduvall /*
31361369Sdduvall  * attach(9E) -- Attach a device to the system
31371369Sdduvall  *
31381369Sdduvall  * Called once for each board successfully probed.
31391369Sdduvall  */
31401369Sdduvall static int
31411369Sdduvall bge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
31421369Sdduvall {
31431369Sdduvall 	bge_t *bgep;				/* Our private data	*/
31442311Sseb 	mac_register_t *macp;
31451369Sdduvall 	chip_id_t *cidp;
31461369Sdduvall 	caddr_t regs;
31471369Sdduvall 	int instance;
31481369Sdduvall 	int err;
31491369Sdduvall 	int intr_types;
31501408Srandyf #ifdef BGE_IPMI_ASF
31511408Srandyf 	uint32_t mhcrValue;
31523918Sml149210 #ifdef __sparc
31533918Sml149210 	uint16_t value16;
31543918Sml149210 #endif
31553918Sml149210 #ifdef BGE_NETCONSOLE
31563918Sml149210 	int retval;
31573918Sml149210 #endif
31581408Srandyf #endif
31591369Sdduvall 
31601369Sdduvall 	instance = ddi_get_instance(devinfo);
31611369Sdduvall 
31621369Sdduvall 	BGE_GTRACE(("bge_attach($%p, %d) instance %d",
31634588Sml149210 	    (void *)devinfo, cmd, instance));
31641369Sdduvall 	BGE_BRKPT(NULL, "bge_attach");
31651369Sdduvall 
31661369Sdduvall 	switch (cmd) {
31671369Sdduvall 	default:
31681369Sdduvall 		return (DDI_FAILURE);
31691369Sdduvall 
31701369Sdduvall 	case DDI_RESUME:
31711369Sdduvall 		return (bge_resume(devinfo));
31721369Sdduvall 
31731369Sdduvall 	case DDI_ATTACH:
31741369Sdduvall 		break;
31751369Sdduvall 	}
31761369Sdduvall 
31771369Sdduvall 	bgep = kmem_zalloc(sizeof (*bgep), KM_SLEEP);
31783334Sgs150176 	bgep->pstats = kmem_zalloc(sizeof (bge_statistics_reg_t), KM_SLEEP);
31791369Sdduvall 	ddi_set_driver_private(devinfo, bgep);
31801369Sdduvall 	bgep->bge_guard = BGE_GUARD;
31811369Sdduvall 	bgep->devinfo = devinfo;
31825903Ssowmini 	bgep->param_drain_max = 64;
31835903Ssowmini 	bgep->param_msi_cnt = 0;
31845903Ssowmini 	bgep->param_loop_mode = 0;
31851369Sdduvall 
31861369Sdduvall 	/*
31871369Sdduvall 	 * Initialize more fields in BGE private data
31881369Sdduvall 	 */
31891369Sdduvall 	bgep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
31904588Sml149210 	    DDI_PROP_DONTPASS, debug_propname, bge_debug);
31911369Sdduvall 	(void) snprintf(bgep->ifname, sizeof (bgep->ifname), "%s%d",
31924588Sml149210 	    BGE_DRIVER_NAME, instance);
31931369Sdduvall 
31941369Sdduvall 	/*
31951865Sdilpreet 	 * Initialize for fma support
31961865Sdilpreet 	 */
31971865Sdilpreet 	bgep->fm_capabilities = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
31981865Sdilpreet 	    DDI_PROP_DONTPASS, fm_cap,
31991865Sdilpreet 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
32001865Sdilpreet 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
32011865Sdilpreet 	BGE_DEBUG(("bgep->fm_capabilities = %d", bgep->fm_capabilities));
32021865Sdilpreet 	bge_fm_init(bgep);
32031865Sdilpreet 
32041865Sdilpreet 	/*
32051369Sdduvall 	 * Look up the IOMMU's page size for DVMA mappings (must be
32061369Sdduvall 	 * a power of 2) and convert to a mask.  This can be used to
32071369Sdduvall 	 * determine whether a message buffer crosses a page boundary.
32081369Sdduvall 	 * Note: in 2s complement binary notation, if X is a power of
32091369Sdduvall 	 * 2, then -X has the representation "11...1100...00".
32101369Sdduvall 	 */
32111369Sdduvall 	bgep->pagemask = dvma_pagesize(devinfo);
32121369Sdduvall 	ASSERT(ddi_ffs(bgep->pagemask) == ddi_fls(bgep->pagemask));
32131369Sdduvall 	bgep->pagemask = -bgep->pagemask;
32141369Sdduvall 
32151369Sdduvall 	/*
32161369Sdduvall 	 * Map config space registers
32171369Sdduvall 	 * Read chip ID & set up config space command register(s)
32181369Sdduvall 	 *
32191369Sdduvall 	 * Note: this leaves the chip accessible by Memory Space
32201369Sdduvall 	 * accesses, but with interrupts and Bus Mastering off.
32211369Sdduvall 	 * This should ensure that nothing untoward will happen
32221369Sdduvall 	 * if it has been left active by the (net-)bootloader.
32231369Sdduvall 	 * We'll re-enable Bus Mastering once we've reset the chip,
32241369Sdduvall 	 * and allow interrupts only when everything else is set up.
32251369Sdduvall 	 */
32261369Sdduvall 	err = pci_config_setup(devinfo, &bgep->cfg_handle);
32271408Srandyf #ifdef BGE_IPMI_ASF
32283918Sml149210 #ifdef __sparc
32293918Sml149210 	value16 = pci_config_get16(bgep->cfg_handle, PCI_CONF_COMM);
32303918Sml149210 	value16 = value16 | (PCI_COMM_MAE | PCI_COMM_ME);
32313918Sml149210 	pci_config_put16(bgep->cfg_handle, PCI_CONF_COMM, value16);
32323918Sml149210 	mhcrValue = MHCR_ENABLE_INDIRECT_ACCESS |
32334588Sml149210 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
32344588Sml149210 	    MHCR_MASK_INTERRUPT_MODE |
32354588Sml149210 	    MHCR_MASK_PCI_INT_OUTPUT |
32364588Sml149210 	    MHCR_CLEAR_INTERRUPT_INTA |
32374588Sml149210 	    MHCR_ENABLE_ENDIAN_WORD_SWAP |
32384588Sml149210 	    MHCR_ENABLE_ENDIAN_BYTE_SWAP;
32393918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcrValue);
32403918Sml149210 	bge_ind_put32(bgep, MEMORY_ARBITER_MODE_REG,
32414588Sml149210 	    bge_ind_get32(bgep, MEMORY_ARBITER_MODE_REG) |
32424588Sml149210 	    MEMORY_ARBITER_ENABLE);
32433918Sml149210 #else
32441408Srandyf 	mhcrValue = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MHCR);
32453918Sml149210 #endif
32461408Srandyf 	if (mhcrValue & MHCR_ENABLE_ENDIAN_WORD_SWAP) {
32471408Srandyf 		bgep->asf_wordswapped = B_TRUE;
32481408Srandyf 	} else {
32491408Srandyf 		bgep->asf_wordswapped = B_FALSE;
32501408Srandyf 	}
32511408Srandyf 	bge_asf_get_config(bgep);
32521408Srandyf #endif
32531369Sdduvall 	if (err != DDI_SUCCESS) {
32541369Sdduvall 		bge_problem(bgep, "pci_config_setup() failed");
32551369Sdduvall 		goto attach_fail;
32561369Sdduvall 	}
32571369Sdduvall 	bgep->progress |= PROGRESS_CFG;
32581369Sdduvall 	cidp = &bgep->chipid;
32591369Sdduvall 	bzero(cidp, sizeof (*cidp));
32601369Sdduvall 	bge_chip_cfg_init(bgep, cidp, B_FALSE);
32611865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
32621865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
32631865Sdilpreet 		goto attach_fail;
32641865Sdilpreet 	}
32651369Sdduvall 
32661408Srandyf #ifdef BGE_IPMI_ASF
32671408Srandyf 	if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
32681408Srandyf 	    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
32691408Srandyf 		bgep->asf_newhandshake = B_TRUE;
32701408Srandyf 	} else {
32711408Srandyf 		bgep->asf_newhandshake = B_FALSE;
32721408Srandyf 	}
32731408Srandyf #endif
32741408Srandyf 
32751369Sdduvall 	/*
32761369Sdduvall 	 * Update those parts of the chip ID derived from volatile
32771369Sdduvall 	 * registers with the values seen by OBP (in case the chip
32781369Sdduvall 	 * has been reset externally and therefore lost them).
32791369Sdduvall 	 */
32801369Sdduvall 	cidp->subven = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32814588Sml149210 	    DDI_PROP_DONTPASS, subven_propname, cidp->subven);
32821369Sdduvall 	cidp->subdev = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32834588Sml149210 	    DDI_PROP_DONTPASS, subdev_propname, cidp->subdev);
32841369Sdduvall 	cidp->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32854588Sml149210 	    DDI_PROP_DONTPASS, clsize_propname, cidp->clsize);
32861369Sdduvall 	cidp->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32874588Sml149210 	    DDI_PROP_DONTPASS, latency_propname, cidp->latency);
32881369Sdduvall 	cidp->rx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32894588Sml149210 	    DDI_PROP_DONTPASS, rxrings_propname, cidp->rx_rings);
32901369Sdduvall 	cidp->tx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
32914588Sml149210 	    DDI_PROP_DONTPASS, txrings_propname, cidp->tx_rings);
32921369Sdduvall 
329310464SYong.Tan@Sun.COM 	cidp->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
329410464SYong.Tan@Sun.COM 	    DDI_PROP_DONTPASS, default_mtu, BGE_DEFAULT_MTU);
329510464SYong.Tan@Sun.COM 	if ((cidp->default_mtu < BGE_DEFAULT_MTU) ||
329610464SYong.Tan@Sun.COM 	    (cidp->default_mtu > BGE_MAXIMUM_MTU)) {
329710464SYong.Tan@Sun.COM 		cidp->default_mtu = BGE_DEFAULT_MTU;
329810464SYong.Tan@Sun.COM 	}
329910464SYong.Tan@Sun.COM 
33001369Sdduvall 	/*
33011369Sdduvall 	 * Map operating registers
33021369Sdduvall 	 */
33031369Sdduvall 	err = ddi_regs_map_setup(devinfo, BGE_PCI_OPREGS_RNUMBER,
33044588Sml149210 	    &regs, 0, 0, &bge_reg_accattr, &bgep->io_handle);
33051369Sdduvall 	if (err != DDI_SUCCESS) {
33061369Sdduvall 		bge_problem(bgep, "ddi_regs_map_setup() failed");
33071369Sdduvall 		goto attach_fail;
33081369Sdduvall 	}
33091369Sdduvall 	bgep->io_regs = regs;
33101369Sdduvall 	bgep->progress |= PROGRESS_REGS;
33111369Sdduvall 
33121369Sdduvall 	/*
33131369Sdduvall 	 * Characterise the device, so we know its requirements.
33141369Sdduvall 	 * Then allocate the appropriate TX and RX descriptors & buffers.
33151369Sdduvall 	 */
33161865Sdilpreet 	if (bge_chip_id_init(bgep) == EIO) {
33171865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
33181865Sdilpreet 		goto attach_fail;
33191865Sdilpreet 	}
33206512Ssowmini 
33211369Sdduvall 	err = bge_alloc_bufs(bgep);
33221369Sdduvall 	if (err != DDI_SUCCESS) {
33231369Sdduvall 		bge_problem(bgep, "DMA buffer allocation failed");
33241369Sdduvall 		goto attach_fail;
33251369Sdduvall 	}
33261865Sdilpreet 	bgep->progress |= PROGRESS_BUFS;
33271369Sdduvall 
33281369Sdduvall 	/*
33291369Sdduvall 	 * Add the softint handlers:
33301369Sdduvall 	 *
33311369Sdduvall 	 * Both of these handlers are used to avoid restrictions on the
33321369Sdduvall 	 * context and/or mutexes required for some operations.  In
33331369Sdduvall 	 * particular, the hardware interrupt handler and its subfunctions
33341369Sdduvall 	 * can detect a number of conditions that we don't want to handle
33351369Sdduvall 	 * in that context or with that set of mutexes held.  So, these
33361369Sdduvall 	 * softints are triggered instead:
33371369Sdduvall 	 *
33382135Szh199473 	 * the <resched> softint is triggered if we have previously
33391369Sdduvall 	 * had to refuse to send a packet because of resource shortage
33401369Sdduvall 	 * (we've run out of transmit buffers), but the send completion
33411369Sdduvall 	 * interrupt handler has now detected that more buffers have
33421369Sdduvall 	 * become available.
33431369Sdduvall 	 *
33441369Sdduvall 	 * the <factotum> is triggered if the h/w interrupt handler
33451369Sdduvall 	 * sees the <link state changed> or <error> bits in the status
33461369Sdduvall 	 * block.  It's also triggered periodically to poll the link
33471369Sdduvall 	 * state, just in case we aren't getting link status change
33481369Sdduvall 	 * interrupts ...
33491369Sdduvall 	 */
33503334Sgs150176 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->drain_id,
33514588Sml149210 	    NULL, NULL, bge_send_drain, (caddr_t)bgep);
33521369Sdduvall 	if (err != DDI_SUCCESS) {
33531369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
33541369Sdduvall 		goto attach_fail;
33551369Sdduvall 	}
33561369Sdduvall 	bgep->progress |= PROGRESS_RESCHED;
33571369Sdduvall 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->factotum_id,
33584588Sml149210 	    NULL, NULL, bge_chip_factotum, (caddr_t)bgep);
33591369Sdduvall 	if (err != DDI_SUCCESS) {
33601369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
33611369Sdduvall 		goto attach_fail;
33621369Sdduvall 	}
33631369Sdduvall 	bgep->progress |= PROGRESS_FACTOTUM;
33641369Sdduvall 
33651369Sdduvall 	/* Get supported interrupt types */
33661369Sdduvall 	if (ddi_intr_get_supported_types(devinfo, &intr_types) != DDI_SUCCESS) {
33671369Sdduvall 		bge_error(bgep, "ddi_intr_get_supported_types failed\n");
33681369Sdduvall 
33691369Sdduvall 		goto attach_fail;
33701369Sdduvall 	}
33711369Sdduvall 
33722675Szh199473 	BGE_DEBUG(("%s: ddi_intr_get_supported_types() returned: %x",
33734588Sml149210 	    bgep->ifname, intr_types));
33741369Sdduvall 
33751369Sdduvall 	if ((intr_types & DDI_INTR_TYPE_MSI) && bgep->chipid.msi_enabled) {
33761369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
33771369Sdduvall 			bge_error(bgep, "MSI registration failed, "
33781369Sdduvall 			    "trying FIXED interrupt type\n");
33791369Sdduvall 		} else {
33802675Szh199473 			BGE_DEBUG(("%s: Using MSI interrupt type",
33814588Sml149210 			    bgep->ifname));
33821369Sdduvall 			bgep->intr_type = DDI_INTR_TYPE_MSI;
33831865Sdilpreet 			bgep->progress |= PROGRESS_HWINT;
33841369Sdduvall 		}
33851369Sdduvall 	}
33861369Sdduvall 
33871865Sdilpreet 	if (!(bgep->progress & PROGRESS_HWINT) &&
33881369Sdduvall 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
33891369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
33901369Sdduvall 			bge_error(bgep, "FIXED interrupt "
33911369Sdduvall 			    "registration failed\n");
33921369Sdduvall 			goto attach_fail;
33931369Sdduvall 		}
33941369Sdduvall 
33952675Szh199473 		BGE_DEBUG(("%s: Using FIXED interrupt type", bgep->ifname));
33961369Sdduvall 
33971369Sdduvall 		bgep->intr_type = DDI_INTR_TYPE_FIXED;
33981865Sdilpreet 		bgep->progress |= PROGRESS_HWINT;
33991369Sdduvall 	}
34001369Sdduvall 
34011865Sdilpreet 	if (!(bgep->progress & PROGRESS_HWINT)) {
34021369Sdduvall 		bge_error(bgep, "No interrupts registered\n");
34031369Sdduvall 		goto attach_fail;
34041369Sdduvall 	}
34051369Sdduvall 
34061369Sdduvall 	/*
34071369Sdduvall 	 * Note that interrupts are not enabled yet as
34081865Sdilpreet 	 * mutex locks are not initialized. Initialize mutex locks.
34091865Sdilpreet 	 */
34101865Sdilpreet 	mutex_init(bgep->genlock, NULL, MUTEX_DRIVER,
34111865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
34121865Sdilpreet 	mutex_init(bgep->softintrlock, NULL, MUTEX_DRIVER,
34131865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
34141865Sdilpreet 	rw_init(bgep->errlock, NULL, RW_DRIVER,
34151865Sdilpreet 	    DDI_INTR_PRI(bgep->intr_pri));
34161865Sdilpreet 
34171865Sdilpreet 	/*
34181865Sdilpreet 	 * Initialize rings.
34191369Sdduvall 	 */
34201369Sdduvall 	bge_init_rings(bgep);
34211369Sdduvall 
34221369Sdduvall 	/*
34231369Sdduvall 	 * Now that mutex locks are initialized, enable interrupts.
34241369Sdduvall 	 */
34251865Sdilpreet 	bge_intr_enable(bgep);
34261865Sdilpreet 	bgep->progress |= PROGRESS_INTR;
34271369Sdduvall 
34281369Sdduvall 	/*
34291369Sdduvall 	 * Initialise link state variables
34301369Sdduvall 	 * Stop, reset & reinitialise the chip.
34311369Sdduvall 	 * Initialise the (internal) PHY.
34321369Sdduvall 	 */
34331369Sdduvall 	bgep->link_state = LINK_STATE_UNKNOWN;
34341369Sdduvall 
34351369Sdduvall 	mutex_enter(bgep->genlock);
34361369Sdduvall 
34371369Sdduvall 	/*
34381369Sdduvall 	 * Reset chip & rings to initial state; also reset address
34391369Sdduvall 	 * filtering, promiscuity, loopback mode.
34401369Sdduvall 	 */
34411408Srandyf #ifdef BGE_IPMI_ASF
34423918Sml149210 #ifdef BGE_NETCONSOLE
34433918Sml149210 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
34443918Sml149210 #else
34451865Sdilpreet 	if (bge_reset(bgep, ASF_MODE_SHUTDOWN) != DDI_SUCCESS) {
34463918Sml149210 #endif
34471408Srandyf #else
34481865Sdilpreet 	if (bge_reset(bgep) != DDI_SUCCESS) {
34491408Srandyf #endif
34501865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
34511865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
34521865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
34531865Sdilpreet 		mutex_exit(bgep->genlock);
34541865Sdilpreet 		goto attach_fail;
34551865Sdilpreet 	}
34561369Sdduvall 
34572675Szh199473 #ifdef BGE_IPMI_ASF
34582675Szh199473 	if (bgep->asf_enabled) {
34592675Szh199473 		bgep->asf_status = ASF_STAT_RUN_INIT;
34602675Szh199473 	}
34612675Szh199473 #endif
34622675Szh199473 
34631369Sdduvall 	bzero(bgep->mcast_hash, sizeof (bgep->mcast_hash));
34641369Sdduvall 	bzero(bgep->mcast_refs, sizeof (bgep->mcast_refs));
34651369Sdduvall 	bgep->promisc = B_FALSE;
34661369Sdduvall 	bgep->param_loop_mode = BGE_LOOP_NONE;
34671865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
34681865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
34691865Sdilpreet 		mutex_exit(bgep->genlock);
34701865Sdilpreet 		goto attach_fail;
34711865Sdilpreet 	}
34721865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
34731865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
34741865Sdilpreet 		mutex_exit(bgep->genlock);
34751865Sdilpreet 		goto attach_fail;
34761865Sdilpreet 	}
34771369Sdduvall 
34781369Sdduvall 	mutex_exit(bgep->genlock);
34791369Sdduvall 
34801865Sdilpreet 	if (bge_phys_init(bgep) == EIO) {
34811865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
34821865Sdilpreet 		goto attach_fail;
34831865Sdilpreet 	}
34841369Sdduvall 	bgep->progress |= PROGRESS_PHY;
34851369Sdduvall 
34861369Sdduvall 	/*
34876512Ssowmini 	 * initialize NDD-tweakable parameters
34881369Sdduvall 	 */
34891369Sdduvall 	if (bge_nd_init(bgep)) {
34901369Sdduvall 		bge_problem(bgep, "bge_nd_init() failed");
34911369Sdduvall 		goto attach_fail;
34921369Sdduvall 	}
34931369Sdduvall 	bgep->progress |= PROGRESS_NDD;
34941369Sdduvall 
34951369Sdduvall 	/*
34961369Sdduvall 	 * Create & initialise named kstats
34971369Sdduvall 	 */
34981369Sdduvall 	bge_init_kstats(bgep, instance);
34991369Sdduvall 	bgep->progress |= PROGRESS_KSTATS;
35001369Sdduvall 
35011369Sdduvall 	/*
35021369Sdduvall 	 * Determine whether to override the chip's own MAC address
35031369Sdduvall 	 */
35041369Sdduvall 	bge_find_mac_address(bgep, cidp);
35052331Skrgopi 
35062406Skrgopi 	bgep->unicst_addr_total = MAC_ADDRESS_REGS_MAX;
35078275SEric Cheng 	bgep->unicst_addr_avail = MAC_ADDRESS_REGS_MAX;
35081369Sdduvall 
35092311Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
35102311Sseb 		goto attach_fail;
35112311Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
35122311Sseb 	macp->m_driver = bgep;
35131369Sdduvall 	macp->m_dip = devinfo;
35148275SEric Cheng 	macp->m_src_addr = cidp->vendor_addr.addr;
35152311Sseb 	macp->m_callbacks = &bge_m_callbacks;
35162311Sseb 	macp->m_min_sdu = 0;
35172311Sseb 	macp->m_max_sdu = cidp->ethmax_size - sizeof (struct ether_header);
35185895Syz147064 	macp->m_margin = VLAN_TAGSZ;
35196512Ssowmini 	macp->m_priv_props = bge_priv_prop;
35206512Ssowmini 	macp->m_priv_prop_count = BGE_MAX_PRIV_PROPS;
35218275SEric Cheng 	macp->m_v12n = MAC_VIRT_LEVEL1;
35226512Ssowmini 
35231369Sdduvall 	/*
35241369Sdduvall 	 * Finally, we're ready to register ourselves with the MAC layer
35251369Sdduvall 	 * interface; if this succeeds, we're all ready to start()
35261369Sdduvall 	 */
35272311Sseb 	err = mac_register(macp, &bgep->mh);
35282311Sseb 	mac_free(macp);
35292311Sseb 	if (err != 0)
35301369Sdduvall 		goto attach_fail;
35311369Sdduvall 
35325107Seota 	/*
35335107Seota 	 * Register a periodical handler.
35345107Seota 	 * bge_chip_cyclic() is invoked in kernel context.
35355107Seota 	 */
35365107Seota 	bgep->periodic_id = ddi_periodic_add(bge_chip_cyclic, bgep,
35375107Seota 	    BGE_CYCLIC_PERIOD, DDI_IPL_0);
35381369Sdduvall 
35391369Sdduvall 	bgep->progress |= PROGRESS_READY;
35401369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
35413918Sml149210 #ifdef BGE_IPMI_ASF
35423918Sml149210 #ifdef BGE_NETCONSOLE
35433918Sml149210 	if (bgep->asf_enabled) {
35443918Sml149210 		mutex_enter(bgep->genlock);
35453918Sml149210 		retval = bge_chip_start(bgep, B_TRUE);
35463918Sml149210 		mutex_exit(bgep->genlock);
35473918Sml149210 		if (retval != DDI_SUCCESS)
35483918Sml149210 			goto attach_fail;
35493918Sml149210 	}
35503918Sml149210 #endif
35513918Sml149210 #endif
35527656SSherry.Moore@Sun.COM 
35537656SSherry.Moore@Sun.COM 	ddi_report_dev(devinfo);
35548993SYong.Tan@Sun.COM 	BGE_REPORT((bgep, "bge version: %s", bge_version));
35558922SYong.Tan@Sun.COM 
35561369Sdduvall 	return (DDI_SUCCESS);
35571369Sdduvall 
35581369Sdduvall attach_fail:
35591408Srandyf #ifdef BGE_IPMI_ASF
35602675Szh199473 	bge_unattach(bgep, ASF_MODE_SHUTDOWN);
35611408Srandyf #else
35621369Sdduvall 	bge_unattach(bgep);
35631408Srandyf #endif
35641369Sdduvall 	return (DDI_FAILURE);
35651369Sdduvall }
35661369Sdduvall 
35671369Sdduvall /*
35681369Sdduvall  *	bge_suspend() -- suspend transmit/receive for powerdown
35691369Sdduvall  */
35701369Sdduvall static int
35711369Sdduvall bge_suspend(bge_t *bgep)
35721369Sdduvall {
35731369Sdduvall 	/*
35741369Sdduvall 	 * Stop processing and idle (powerdown) the PHY ...
35751369Sdduvall 	 */
35761369Sdduvall 	mutex_enter(bgep->genlock);
35771408Srandyf #ifdef BGE_IPMI_ASF
35781408Srandyf 	/*
35791408Srandyf 	 * Power management hasn't been supported in BGE now. If you
35801408Srandyf 	 * want to implement it, please add the ASF/IPMI related
35811408Srandyf 	 * code here.
35821408Srandyf 	 */
35831408Srandyf #endif
35841369Sdduvall 	bge_stop(bgep);
35851865Sdilpreet 	if (bge_phys_idle(bgep) != DDI_SUCCESS) {
35861865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
35871865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
35881865Sdilpreet 		mutex_exit(bgep->genlock);
35891865Sdilpreet 		return (DDI_FAILURE);
35901865Sdilpreet 	}
35911865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
35921865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
35931865Sdilpreet 		mutex_exit(bgep->genlock);
35941865Sdilpreet 		return (DDI_FAILURE);
35951865Sdilpreet 	}
35961369Sdduvall 	mutex_exit(bgep->genlock);
35971369Sdduvall 
35981369Sdduvall 	return (DDI_SUCCESS);
35991369Sdduvall }
36001369Sdduvall 
36011369Sdduvall /*
36027656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
36037656SSherry.Moore@Sun.COM  *
36047656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
36057656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
36067656SSherry.Moore@Sun.COM  * blocked.
36077656SSherry.Moore@Sun.COM  *
36087656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
36097656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
36107656SSherry.Moore@Sun.COM  */
36117656SSherry.Moore@Sun.COM #ifdef	__sparc
36127656SSherry.Moore@Sun.COM #define	bge_quiesce	ddi_quiesce_not_supported
36137656SSherry.Moore@Sun.COM #else
36147656SSherry.Moore@Sun.COM static int
36157656SSherry.Moore@Sun.COM bge_quiesce(dev_info_t *devinfo)
36167656SSherry.Moore@Sun.COM {
36177656SSherry.Moore@Sun.COM 	bge_t *bgep = ddi_get_driver_private(devinfo);
36187656SSherry.Moore@Sun.COM 
36197656SSherry.Moore@Sun.COM 	if (bgep == NULL)
36207656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
36217656SSherry.Moore@Sun.COM 
36227656SSherry.Moore@Sun.COM 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
36237656SSherry.Moore@Sun.COM 		bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
36247656SSherry.Moore@Sun.COM 		    MHCR_MASK_PCI_INT_OUTPUT);
36257656SSherry.Moore@Sun.COM 	} else {
36267656SSherry.Moore@Sun.COM 		bge_reg_clr32(bgep, MSI_MODE_REG, MSI_MSI_ENABLE);
36277656SSherry.Moore@Sun.COM 	}
36287656SSherry.Moore@Sun.COM 
36297656SSherry.Moore@Sun.COM 	/* Stop the chip */
36307656SSherry.Moore@Sun.COM 	bge_chip_stop_nonblocking(bgep);
36317656SSherry.Moore@Sun.COM 
36327656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
36337656SSherry.Moore@Sun.COM }
36347656SSherry.Moore@Sun.COM #endif
36357656SSherry.Moore@Sun.COM 
36367656SSherry.Moore@Sun.COM /*
36371369Sdduvall  * detach(9E) -- Detach a device from the system
36381369Sdduvall  */
36391369Sdduvall static int
36401369Sdduvall bge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
36411369Sdduvall {
36421369Sdduvall 	bge_t *bgep;
36431408Srandyf #ifdef BGE_IPMI_ASF
36441408Srandyf 	uint_t asf_mode;
36451408Srandyf 	asf_mode = ASF_MODE_NONE;
36461408Srandyf #endif
36471369Sdduvall 
36481369Sdduvall 	BGE_GTRACE(("bge_detach($%p, %d)", (void *)devinfo, cmd));
36491369Sdduvall 
36501369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
36511369Sdduvall 
36521369Sdduvall 	switch (cmd) {
36531369Sdduvall 	default:
36541369Sdduvall 		return (DDI_FAILURE);
36551369Sdduvall 
36561369Sdduvall 	case DDI_SUSPEND:
36571369Sdduvall 		return (bge_suspend(bgep));
36581369Sdduvall 
36591369Sdduvall 	case DDI_DETACH:
36601369Sdduvall 		break;
36611369Sdduvall 	}
36621369Sdduvall 
36631408Srandyf #ifdef BGE_IPMI_ASF
36641408Srandyf 	mutex_enter(bgep->genlock);
36652675Szh199473 	if (bgep->asf_enabled && ((bgep->asf_status == ASF_STAT_RUN) ||
36664588Sml149210 	    (bgep->asf_status == ASF_STAT_RUN_INIT))) {
36671408Srandyf 
36681408Srandyf 		bge_asf_update_status(bgep);
36692675Szh199473 		if (bgep->asf_status == ASF_STAT_RUN) {
36702675Szh199473 			bge_asf_stop_timer(bgep);
36712675Szh199473 		}
36721408Srandyf 		bgep->asf_status = ASF_STAT_STOP;
36731408Srandyf 
36741408Srandyf 		bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
36751408Srandyf 
36761408Srandyf 		if (bgep->asf_pseudostop) {
36771408Srandyf 			bge_chip_stop(bgep, B_FALSE);
36781408Srandyf 			bgep->bge_mac_state = BGE_MAC_STOPPED;
36791408Srandyf 			bgep->asf_pseudostop = B_FALSE;
36801408Srandyf 		}
36811408Srandyf 
36821408Srandyf 		asf_mode = ASF_MODE_POST_SHUTDOWN;
36831865Sdilpreet 
36841865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
36851865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
36861865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
36871865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
36881865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
36891865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
36901408Srandyf 	}
36911408Srandyf 	mutex_exit(bgep->genlock);
36921408Srandyf #endif
36931408Srandyf 
36941369Sdduvall 	/*
36951369Sdduvall 	 * Unregister from the GLD subsystem.  This can fail, in
36961369Sdduvall 	 * particular if there are DLPI style-2 streams still open -
36971369Sdduvall 	 * in which case we just return failure without shutting
36981369Sdduvall 	 * down chip operations.
36991369Sdduvall 	 */
37002311Sseb 	if (mac_unregister(bgep->mh) != 0)
37011369Sdduvall 		return (DDI_FAILURE);
37021369Sdduvall 
37031369Sdduvall 	/*
37041369Sdduvall 	 * All activity stopped, so we can clean up & exit
37051369Sdduvall 	 */
37061408Srandyf #ifdef BGE_IPMI_ASF
37071408Srandyf 	bge_unattach(bgep, asf_mode);
37081408Srandyf #else
37091369Sdduvall 	bge_unattach(bgep);
37101408Srandyf #endif
37111369Sdduvall 	return (DDI_SUCCESS);
37121369Sdduvall }
37131369Sdduvall 
37141369Sdduvall 
37151369Sdduvall /*
37161369Sdduvall  * ========== Module Loading Data & Entry Points ==========
37171369Sdduvall  */
37181369Sdduvall 
37191369Sdduvall #undef	BGE_DBG
37201369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
37211369Sdduvall 
37227656SSherry.Moore@Sun.COM DDI_DEFINE_STREAM_OPS(bge_dev_ops,
37237656SSherry.Moore@Sun.COM 	nulldev,	/* identify */
37247656SSherry.Moore@Sun.COM 	nulldev,	/* probe */
37257656SSherry.Moore@Sun.COM 	bge_attach,	/* attach */
37267656SSherry.Moore@Sun.COM 	bge_detach,	/* detach */
37277656SSherry.Moore@Sun.COM 	nodev,		/* reset */
37287656SSherry.Moore@Sun.COM 	NULL,		/* cb_ops */
37297656SSherry.Moore@Sun.COM 	D_MP,		/* bus_ops */
37307656SSherry.Moore@Sun.COM 	NULL,		/* power */
37317656SSherry.Moore@Sun.COM 	bge_quiesce	/* quiesce */
37327656SSherry.Moore@Sun.COM );
37331369Sdduvall 
37341369Sdduvall static struct modldrv bge_modldrv = {
37351369Sdduvall 	&mod_driverops,		/* Type of module.  This one is a driver */
37361369Sdduvall 	bge_ident,		/* short description */
37371369Sdduvall 	&bge_dev_ops		/* driver specific ops */
37381369Sdduvall };
37391369Sdduvall 
37401369Sdduvall static struct modlinkage modlinkage = {
37411369Sdduvall 	MODREV_1, (void *)&bge_modldrv, NULL
37421369Sdduvall };
37431369Sdduvall 
37441369Sdduvall 
37451369Sdduvall int
37461369Sdduvall _info(struct modinfo *modinfop)
37471369Sdduvall {
37481369Sdduvall 	return (mod_info(&modlinkage, modinfop));
37491369Sdduvall }
37501369Sdduvall 
37511369Sdduvall int
37521369Sdduvall _init(void)
37531369Sdduvall {
37541369Sdduvall 	int status;
37551369Sdduvall 
37561369Sdduvall 	mac_init_ops(&bge_dev_ops, "bge");
37571369Sdduvall 	status = mod_install(&modlinkage);
37581369Sdduvall 	if (status == DDI_SUCCESS)
37591369Sdduvall 		mutex_init(bge_log_mutex, NULL, MUTEX_DRIVER, NULL);
37601369Sdduvall 	else
37611369Sdduvall 		mac_fini_ops(&bge_dev_ops);
37621369Sdduvall 	return (status);
37631369Sdduvall }
37641369Sdduvall 
37651369Sdduvall int
37661369Sdduvall _fini(void)
37671369Sdduvall {
37681369Sdduvall 	int status;
37691369Sdduvall 
37701369Sdduvall 	status = mod_remove(&modlinkage);
37711369Sdduvall 	if (status == DDI_SUCCESS) {
37721369Sdduvall 		mac_fini_ops(&bge_dev_ops);
37731369Sdduvall 		mutex_destroy(bge_log_mutex);
37741369Sdduvall 	}
37751369Sdduvall 	return (status);
37761369Sdduvall }
37771369Sdduvall 
37781369Sdduvall 
37791369Sdduvall /*
37801369Sdduvall  * bge_add_intrs:
37811369Sdduvall  *
37821369Sdduvall  * Register FIXED or MSI interrupts.
37831369Sdduvall  */
37841369Sdduvall static int
37851369Sdduvall bge_add_intrs(bge_t *bgep, int	intr_type)
37861369Sdduvall {
37871369Sdduvall 	dev_info_t	*dip = bgep->devinfo;
37881369Sdduvall 	int		avail, actual, intr_size, count = 0;
37891369Sdduvall 	int		i, flag, ret;
37901369Sdduvall 
37912675Szh199473 	BGE_DEBUG(("bge_add_intrs($%p, 0x%x)", (void *)bgep, intr_type));
37921369Sdduvall 
37931369Sdduvall 	/* Get number of interrupts */
37941369Sdduvall 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
37951369Sdduvall 	if ((ret != DDI_SUCCESS) || (count == 0)) {
37961369Sdduvall 		bge_error(bgep, "ddi_intr_get_nintrs() failure, ret: %d, "
37971369Sdduvall 		    "count: %d", ret, count);
37981369Sdduvall 
37991369Sdduvall 		return (DDI_FAILURE);
38001369Sdduvall 	}
38011369Sdduvall 
38021369Sdduvall 	/* Get number of available interrupts */
38031369Sdduvall 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
38041369Sdduvall 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
38051369Sdduvall 		bge_error(bgep, "ddi_intr_get_navail() failure, "
38061369Sdduvall 		    "ret: %d, avail: %d\n", ret, avail);
38071369Sdduvall 
38081369Sdduvall 		return (DDI_FAILURE);
38091369Sdduvall 	}
38101369Sdduvall 
38111369Sdduvall 	if (avail < count) {
38122675Szh199473 		BGE_DEBUG(("%s: nintrs() returned %d, navail returned %d",
38132675Szh199473 		    bgep->ifname, count, avail));
38141369Sdduvall 	}
38151369Sdduvall 
38161369Sdduvall 	/*
38171369Sdduvall 	 * BGE hardware generates only single MSI even though it claims
38181369Sdduvall 	 * to support multiple MSIs. So, hard code MSI count value to 1.
38191369Sdduvall 	 */
38201369Sdduvall 	if (intr_type == DDI_INTR_TYPE_MSI) {
38211369Sdduvall 		count = 1;
38221369Sdduvall 		flag = DDI_INTR_ALLOC_STRICT;
38231369Sdduvall 	} else {
38241369Sdduvall 		flag = DDI_INTR_ALLOC_NORMAL;
38251369Sdduvall 	}
38261369Sdduvall 
38271369Sdduvall 	/* Allocate an array of interrupt handles */
38281369Sdduvall 	intr_size = count * sizeof (ddi_intr_handle_t);
38291369Sdduvall 	bgep->htable = kmem_alloc(intr_size, KM_SLEEP);
38301369Sdduvall 
38311369Sdduvall 	/* Call ddi_intr_alloc() */
38321369Sdduvall 	ret = ddi_intr_alloc(dip, bgep->htable, intr_type, 0,
38331369Sdduvall 	    count, &actual, flag);
38341369Sdduvall 
38351369Sdduvall 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
38361369Sdduvall 		bge_error(bgep, "ddi_intr_alloc() failed %d\n", ret);
38371369Sdduvall 
38381369Sdduvall 		kmem_free(bgep->htable, intr_size);
38391369Sdduvall 		return (DDI_FAILURE);
38401369Sdduvall 	}
38411369Sdduvall 
38421369Sdduvall 	if (actual < count) {
38432675Szh199473 		BGE_DEBUG(("%s: Requested: %d, Received: %d",
38444588Sml149210 		    bgep->ifname, count, actual));
38451369Sdduvall 	}
38461369Sdduvall 
38471369Sdduvall 	bgep->intr_cnt = actual;
38481369Sdduvall 
38491369Sdduvall 	/*
38501369Sdduvall 	 * Get priority for first msi, assume remaining are all the same
38511369Sdduvall 	 */
38521369Sdduvall 	if ((ret = ddi_intr_get_pri(bgep->htable[0], &bgep->intr_pri)) !=
38531369Sdduvall 	    DDI_SUCCESS) {
38541369Sdduvall 		bge_error(bgep, "ddi_intr_get_pri() failed %d\n", ret);
38551369Sdduvall 
38561369Sdduvall 		/* Free already allocated intr */
38571369Sdduvall 		for (i = 0; i < actual; i++) {
38581369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
38591369Sdduvall 		}
38601369Sdduvall 
38611369Sdduvall 		kmem_free(bgep->htable, intr_size);
38621369Sdduvall 		return (DDI_FAILURE);
38631369Sdduvall 	}
38641369Sdduvall 
38651369Sdduvall 	/* Call ddi_intr_add_handler() */
38661369Sdduvall 	for (i = 0; i < actual; i++) {
38671369Sdduvall 		if ((ret = ddi_intr_add_handler(bgep->htable[i], bge_intr,
38681369Sdduvall 		    (caddr_t)bgep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
38691369Sdduvall 			bge_error(bgep, "ddi_intr_add_handler() "
38701369Sdduvall 			    "failed %d\n", ret);
38711369Sdduvall 
38721369Sdduvall 			/* Free already allocated intr */
38731369Sdduvall 			for (i = 0; i < actual; i++) {
38741369Sdduvall 				(void) ddi_intr_free(bgep->htable[i]);
38751369Sdduvall 			}
38761369Sdduvall 
38771369Sdduvall 			kmem_free(bgep->htable, intr_size);
38781369Sdduvall 			return (DDI_FAILURE);
38791369Sdduvall 		}
38801369Sdduvall 	}
38811369Sdduvall 
38821369Sdduvall 	if ((ret = ddi_intr_get_cap(bgep->htable[0], &bgep->intr_cap))
38834588Sml149210 	    != DDI_SUCCESS) {
38841369Sdduvall 		bge_error(bgep, "ddi_intr_get_cap() failed %d\n", ret);
38851369Sdduvall 
38861369Sdduvall 		for (i = 0; i < actual; i++) {
38871369Sdduvall 			(void) ddi_intr_remove_handler(bgep->htable[i]);
38881369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
38891369Sdduvall 		}
38901369Sdduvall 
38911369Sdduvall 		kmem_free(bgep->htable, intr_size);
38921369Sdduvall 		return (DDI_FAILURE);
38931369Sdduvall 	}
38941369Sdduvall 
38951369Sdduvall 	return (DDI_SUCCESS);
38961369Sdduvall }
38971369Sdduvall 
38981369Sdduvall /*
38991369Sdduvall  * bge_rem_intrs:
39001369Sdduvall  *
39011369Sdduvall  * Unregister FIXED or MSI interrupts
39021369Sdduvall  */
39031369Sdduvall static void
39041369Sdduvall bge_rem_intrs(bge_t *bgep)
39051369Sdduvall {
39061369Sdduvall 	int	i;
39071369Sdduvall 
39082675Szh199473 	BGE_DEBUG(("bge_rem_intrs($%p)", (void *)bgep));
39091369Sdduvall 
39101865Sdilpreet 	/* Call ddi_intr_remove_handler() */
39111865Sdilpreet 	for (i = 0; i < bgep->intr_cnt; i++) {
39121865Sdilpreet 		(void) ddi_intr_remove_handler(bgep->htable[i]);
39131865Sdilpreet 		(void) ddi_intr_free(bgep->htable[i]);
39141865Sdilpreet 	}
39151865Sdilpreet 
39161865Sdilpreet 	kmem_free(bgep->htable, bgep->intr_cnt * sizeof (ddi_intr_handle_t));
39171865Sdilpreet }
39181865Sdilpreet 
39191865Sdilpreet 
39201865Sdilpreet void
39211865Sdilpreet bge_intr_enable(bge_t *bgep)
39221865Sdilpreet {
39231865Sdilpreet 	int i;
39241865Sdilpreet 
39251865Sdilpreet 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
39261865Sdilpreet 		/* Call ddi_intr_block_enable() for MSI interrupts */
39271865Sdilpreet 		(void) ddi_intr_block_enable(bgep->htable, bgep->intr_cnt);
39281865Sdilpreet 	} else {
39291865Sdilpreet 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
39301865Sdilpreet 		for (i = 0; i < bgep->intr_cnt; i++) {
39311865Sdilpreet 			(void) ddi_intr_enable(bgep->htable[i]);
39321865Sdilpreet 		}
39331865Sdilpreet 	}
39341865Sdilpreet }
39351865Sdilpreet 
39361865Sdilpreet 
39371865Sdilpreet void
39381865Sdilpreet bge_intr_disable(bge_t *bgep)
39391865Sdilpreet {
39401865Sdilpreet 	int i;
39411865Sdilpreet 
39421369Sdduvall 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
39431369Sdduvall 		/* Call ddi_intr_block_disable() */
39441369Sdduvall 		(void) ddi_intr_block_disable(bgep->htable, bgep->intr_cnt);
39451369Sdduvall 	} else {
39461369Sdduvall 		for (i = 0; i < bgep->intr_cnt; i++) {
39471369Sdduvall 			(void) ddi_intr_disable(bgep->htable[i]);
39481369Sdduvall 		}
39491369Sdduvall 	}
39501369Sdduvall }
39515903Ssowmini 
39525903Ssowmini int
39535903Ssowmini bge_reprogram(bge_t *bgep)
39545903Ssowmini {
39555903Ssowmini 	int status = 0;
39565903Ssowmini 
39575903Ssowmini 	ASSERT(mutex_owned(bgep->genlock));
39585903Ssowmini 
39595903Ssowmini 	if (bge_phys_update(bgep) != DDI_SUCCESS) {
39605903Ssowmini 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
39615903Ssowmini 		status = IOC_INVAL;
39625903Ssowmini 	}
39635903Ssowmini #ifdef BGE_IPMI_ASF
39645903Ssowmini 	if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
39655903Ssowmini #else
39665903Ssowmini 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
39675903Ssowmini #endif
39685903Ssowmini 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
39695903Ssowmini 		status = IOC_INVAL;
39705903Ssowmini 	}
39715903Ssowmini 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
39725903Ssowmini 		bge_chip_msi_trig(bgep);
39735903Ssowmini 	return (status);
39745903Ssowmini }
3975