xref: /onnv-gate/usr/src/uts/common/io/bge/bge_main2.c (revision 1611:2ffa29275ba7)
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 /*
231369Sdduvall  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
241369Sdduvall  * Use is subject to license terms.
251369Sdduvall  */
261369Sdduvall 
271369Sdduvall #pragma ident	"%Z%%M%	%I%	%E% SMI"
281369Sdduvall 
291369Sdduvall #include "sys/bge_impl2.h"
301369Sdduvall #include <sys/sdt.h>
311369Sdduvall 
321369Sdduvall /*
331369Sdduvall  * This is the string displayed by modinfo, etc.
341369Sdduvall  * Make sure you keep the version ID up to date!
351369Sdduvall  */
36*1611Szh199473 static char bge_ident[] = "BCM579x driver v0.51";
371369Sdduvall 
381369Sdduvall /*
391369Sdduvall  * Property names
401369Sdduvall  */
411369Sdduvall static char debug_propname[] = "bge-debug-flags";
421369Sdduvall static char clsize_propname[] = "cache-line-size";
431369Sdduvall static char latency_propname[] = "latency-timer";
441369Sdduvall static char localmac_boolname[] = "local-mac-address?";
451369Sdduvall static char localmac_propname[] = "local-mac-address";
461369Sdduvall static char macaddr_propname[] = "mac-address";
471369Sdduvall static char subdev_propname[] = "subsystem-id";
481369Sdduvall static char subven_propname[] = "subsystem-vendor-id";
491369Sdduvall static char rxrings_propname[] = "bge-rx-rings";
501369Sdduvall static char txrings_propname[] = "bge-tx-rings";
511369Sdduvall static char default_mtu[] = "default-mtu";
521369Sdduvall 
531369Sdduvall static int bge_add_intrs(bge_t *, int);
541369Sdduvall static void bge_rem_intrs(bge_t *);
551369Sdduvall 
561369Sdduvall /*
571369Sdduvall  * Describes the chip's DMA engine
581369Sdduvall  */
591369Sdduvall static ddi_dma_attr_t dma_attr = {
601369Sdduvall 	DMA_ATTR_V0,			/* dma_attr version	*/
611369Sdduvall 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
621369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_addr_hi	*/
631369Sdduvall 	0x00000000FFFFFFFFull,		/* dma_attr_count_max	*/
641369Sdduvall 	0x0000000000000001ull,		/* dma_attr_align	*/
651369Sdduvall 	0x00000FFF,			/* dma_attr_burstsizes	*/
661369Sdduvall 	0x00000001,			/* dma_attr_minxfer	*/
671369Sdduvall 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
681369Sdduvall 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_seg		*/
691369Sdduvall 	1,				/* dma_attr_sgllen 	*/
701369Sdduvall 	0x00000001,			/* dma_attr_granular 	*/
711369Sdduvall 	0				/* dma_attr_flags 	*/
721369Sdduvall };
731369Sdduvall 
741369Sdduvall /*
751369Sdduvall  * PIO access attributes for registers
761369Sdduvall  */
771369Sdduvall static ddi_device_acc_attr_t bge_reg_accattr = {
781369Sdduvall 	DDI_DEVICE_ATTR_V0,
791369Sdduvall 	DDI_NEVERSWAP_ACC,
801369Sdduvall 	DDI_STRICTORDER_ACC
811369Sdduvall };
821369Sdduvall 
831369Sdduvall /*
841369Sdduvall  * DMA access attributes for descriptors: NOT to be byte swapped.
851369Sdduvall  */
861369Sdduvall static ddi_device_acc_attr_t bge_desc_accattr = {
871369Sdduvall 	DDI_DEVICE_ATTR_V0,
881369Sdduvall 	DDI_NEVERSWAP_ACC,
891369Sdduvall 	DDI_STRICTORDER_ACC
901369Sdduvall };
911369Sdduvall 
921369Sdduvall /*
931369Sdduvall  * DMA access attributes for data: NOT to be byte swapped.
941369Sdduvall  */
951369Sdduvall static ddi_device_acc_attr_t bge_data_accattr = {
961369Sdduvall 	DDI_DEVICE_ATTR_V0,
971369Sdduvall 	DDI_NEVERSWAP_ACC,
981369Sdduvall 	DDI_STRICTORDER_ACC
991369Sdduvall };
1001369Sdduvall 
1011369Sdduvall static ether_addr_t bge_broadcast_addr = {
1021369Sdduvall 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1031369Sdduvall };
1041369Sdduvall 
1051369Sdduvall /*
1061369Sdduvall  * Versions of the O/S up to Solaris 8 didn't support network booting
1071369Sdduvall  * from any network interface except the first (NET0).  Patching this
1081369Sdduvall  * flag to a non-zero value will tell the driver to work around this
1091369Sdduvall  * limitation by creating an extra (internal) pathname node.  To do
1101369Sdduvall  * this, just add a line like the following to the CLIENT'S etc/system
1111369Sdduvall  * file ON THE ROOT FILESYSTEM SERVER before booting the client:
1121369Sdduvall  *
1131369Sdduvall  *	set bge:bge_net1_boot_support = 1;
1141369Sdduvall  */
1151369Sdduvall static uint32_t bge_net1_boot_support = 1;
1161369Sdduvall 
1171369Sdduvall /*
1181369Sdduvall  * ========== Transmit and receive ring reinitialisation ==========
1191369Sdduvall  */
1201369Sdduvall 
1211369Sdduvall /*
1221369Sdduvall  * These <reinit> routines each reset the specified ring to an initial
1231369Sdduvall  * state, assuming that the corresponding <init> routine has already
1241369Sdduvall  * been called exactly once.
1251369Sdduvall  */
1261369Sdduvall 
1271369Sdduvall static void
1281369Sdduvall bge_reinit_send_ring(send_ring_t *srp)
1291369Sdduvall {
1301369Sdduvall 	/*
1311369Sdduvall 	 * Reinitialise control variables ...
1321369Sdduvall 	 */
1331369Sdduvall 	ASSERT(srp->tx_flow == 0);
1341369Sdduvall 	srp->tx_next = 0;
1351369Sdduvall 	srp->tx_free = srp->desc.nslots;
1361369Sdduvall 
1371369Sdduvall 	ASSERT(mutex_owned(srp->tc_lock));
1381369Sdduvall 	srp->tc_next = 0;
1391369Sdduvall 
1401369Sdduvall 	/*
1411369Sdduvall 	 * Zero and sync all the h/w Send Buffer Descriptors
1421369Sdduvall 	 */
1431369Sdduvall 	DMA_ZERO(srp->desc);
1441369Sdduvall 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
1451369Sdduvall }
1461369Sdduvall 
1471369Sdduvall static void
1481369Sdduvall bge_reinit_recv_ring(recv_ring_t *rrp)
1491369Sdduvall {
1501369Sdduvall 	/*
1511369Sdduvall 	 * Reinitialise control variables ...
1521369Sdduvall 	 */
1531369Sdduvall 	rrp->rx_next = 0;
1541369Sdduvall }
1551369Sdduvall 
1561369Sdduvall static void
1571369Sdduvall bge_reinit_buff_ring(buff_ring_t *brp, uint64_t ring)
1581369Sdduvall {
1591369Sdduvall 	bge_rbd_t *hw_rbd_p;
1601369Sdduvall 	sw_rbd_t *srbdp;
1611369Sdduvall 	uint32_t bufsize;
1621369Sdduvall 	uint32_t nslots;
1631369Sdduvall 	uint32_t slot;
1641369Sdduvall 
1651369Sdduvall 	static uint16_t ring_type_flag[BGE_BUFF_RINGS_MAX] = {
1661369Sdduvall 		RBD_FLAG_STD_RING,
1671369Sdduvall 		RBD_FLAG_JUMBO_RING,
1681369Sdduvall 		RBD_FLAG_MINI_RING
1691369Sdduvall 	};
1701369Sdduvall 
1711369Sdduvall 	/*
1721369Sdduvall 	 * Zero, initialise and sync all the h/w Receive Buffer Descriptors
1731369Sdduvall 	 * Note: all the remaining fields (<type>, <flags>, <ip_cksum>,
1741369Sdduvall 	 * <tcp_udp_cksum>, <error_flag>, <vlan_tag>, and <reserved>)
1751369Sdduvall 	 * should be zeroed, and so don't need to be set up specifically
1761369Sdduvall 	 * once the whole area has been cleared.
1771369Sdduvall 	 */
1781369Sdduvall 	DMA_ZERO(brp->desc);
1791369Sdduvall 
1801369Sdduvall 	hw_rbd_p = DMA_VPTR(brp->desc);
1811369Sdduvall 	nslots = brp->desc.nslots;
1821369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
1831369Sdduvall 	bufsize = brp->buf[0].size;
1841369Sdduvall 	srbdp = brp->sw_rbds;
1851369Sdduvall 	for (slot = 0; slot < nslots; ++hw_rbd_p, ++srbdp, ++slot) {
1861369Sdduvall 		hw_rbd_p->host_buf_addr = srbdp->pbuf.cookie.dmac_laddress;
1871369Sdduvall 		hw_rbd_p->index = slot;
1881369Sdduvall 		hw_rbd_p->len = bufsize;
1891369Sdduvall 		hw_rbd_p->opaque = srbdp->pbuf.token;
1901369Sdduvall 		hw_rbd_p->flags |= ring_type_flag[ring];
1911369Sdduvall 	}
1921369Sdduvall 
1931369Sdduvall 	DMA_SYNC(brp->desc, DDI_DMA_SYNC_FORDEV);
1941369Sdduvall 
1951369Sdduvall 	/*
1961369Sdduvall 	 * Finally, reinitialise the ring control variables ...
1971369Sdduvall 	 */
1981369Sdduvall 	brp->rf_next = (nslots != 0) ? (nslots-1) : 0;
1991369Sdduvall }
2001369Sdduvall 
2011369Sdduvall /*
2021369Sdduvall  * Reinitialize all rings
2031369Sdduvall  */
2041369Sdduvall static void
2051369Sdduvall bge_reinit_rings(bge_t *bgep)
2061369Sdduvall {
2071369Sdduvall 	uint64_t ring;
2081369Sdduvall 
2091369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
2101369Sdduvall 
2111369Sdduvall 	/*
2121369Sdduvall 	 * Send Rings ...
2131369Sdduvall 	 */
2141369Sdduvall 	for (ring = 0; ring < bgep->chipid.tx_rings; ++ring)
2151369Sdduvall 		bge_reinit_send_ring(&bgep->send[ring]);
2161369Sdduvall 
2171369Sdduvall 	/*
2181369Sdduvall 	 * Receive Return Rings ...
2191369Sdduvall 	 */
2201369Sdduvall 	for (ring = 0; ring < bgep->chipid.rx_rings; ++ring)
2211369Sdduvall 		bge_reinit_recv_ring(&bgep->recv[ring]);
2221369Sdduvall 
2231369Sdduvall 	/*
2241369Sdduvall 	 * Receive Producer Rings ...
2251369Sdduvall 	 */
2261369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
2271369Sdduvall 		bge_reinit_buff_ring(&bgep->buff[ring], ring);
2281369Sdduvall }
2291369Sdduvall 
2301369Sdduvall /*
2311369Sdduvall  * ========== Internal state management entry points ==========
2321369Sdduvall  */
2331369Sdduvall 
2341369Sdduvall #undef	BGE_DBG
2351369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
2361369Sdduvall 
2371369Sdduvall /*
2381369Sdduvall  * These routines provide all the functionality required by the
2391369Sdduvall  * corresponding GLD entry points, but don't update the GLD state
2401369Sdduvall  * so they can be called internally without disturbing our record
2411369Sdduvall  * of what GLD thinks we should be doing ...
2421369Sdduvall  */
2431369Sdduvall 
2441369Sdduvall /*
2451369Sdduvall  *	bge_reset() -- reset h/w & rings to initial state
2461369Sdduvall  */
2471369Sdduvall static void
2481408Srandyf #ifdef BGE_IPMI_ASF
2491408Srandyf bge_reset(bge_t *bgep, uint_t asf_mode)
2501408Srandyf #else
2511369Sdduvall bge_reset(bge_t *bgep)
2521408Srandyf #endif
2531369Sdduvall {
2541369Sdduvall 	uint64_t	ring;
2551369Sdduvall 
2561369Sdduvall 	BGE_TRACE(("bge_reset($%p)", (void *)bgep));
2571369Sdduvall 
2581369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
2591369Sdduvall 
2601369Sdduvall 	/*
2611369Sdduvall 	 * Grab all the other mutexes in the world (this should
2621369Sdduvall 	 * ensure no other threads are manipulating driver state)
2631369Sdduvall 	 */
2641369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
2651369Sdduvall 		mutex_enter(bgep->recv[ring].rx_lock);
2661369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
2671369Sdduvall 		mutex_enter(bgep->buff[ring].rf_lock);
2681369Sdduvall 	rw_enter(bgep->errlock, RW_WRITER);
2691369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
2701369Sdduvall 		mutex_enter(bgep->send[ring].tc_lock);
2711369Sdduvall 
2721408Srandyf #ifdef BGE_IPMI_ASF
2731408Srandyf 	bge_chip_reset(bgep, B_TRUE, asf_mode);
2741408Srandyf #else
2751369Sdduvall 	bge_chip_reset(bgep, B_TRUE);
2761408Srandyf #endif
2771369Sdduvall 	bge_reinit_rings(bgep);
2781369Sdduvall 
2791369Sdduvall 	/*
2801369Sdduvall 	 * Free the world ...
2811369Sdduvall 	 */
2821369Sdduvall 	for (ring = BGE_SEND_RINGS_MAX; ring-- > 0; )
2831369Sdduvall 		mutex_exit(bgep->send[ring].tc_lock);
2841369Sdduvall 	rw_exit(bgep->errlock);
2851369Sdduvall 	for (ring = BGE_BUFF_RINGS_MAX; ring-- > 0; )
2861369Sdduvall 		mutex_exit(bgep->buff[ring].rf_lock);
2871369Sdduvall 	for (ring = BGE_RECV_RINGS_MAX; ring-- > 0; )
2881369Sdduvall 		mutex_exit(bgep->recv[ring].rx_lock);
2891369Sdduvall 
2901369Sdduvall 	BGE_DEBUG(("bge_reset($%p) done", (void *)bgep));
2911369Sdduvall }
2921369Sdduvall 
2931369Sdduvall /*
2941369Sdduvall  *	bge_stop() -- stop processing, don't reset h/w or rings
2951369Sdduvall  */
2961369Sdduvall static void
2971369Sdduvall bge_stop(bge_t *bgep)
2981369Sdduvall {
2991369Sdduvall 	BGE_TRACE(("bge_stop($%p)", (void *)bgep));
3001369Sdduvall 
3011369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3021369Sdduvall 
3031408Srandyf #ifdef BGE_IPMI_ASF
3041408Srandyf 	if (bgep->asf_enabled) {
3051408Srandyf 		bgep->asf_pseudostop = B_TRUE;
3061408Srandyf 	} else {
3071408Srandyf #endif
3081408Srandyf 		bge_chip_stop(bgep, B_FALSE);
3091408Srandyf #ifdef BGE_IPMI_ASF
3101408Srandyf 	}
3111408Srandyf #endif
3121369Sdduvall 
3131369Sdduvall 	BGE_DEBUG(("bge_stop($%p) done", (void *)bgep));
3141369Sdduvall }
3151369Sdduvall 
3161369Sdduvall /*
3171369Sdduvall  *	bge_start() -- start transmitting/receiving
3181369Sdduvall  */
3191369Sdduvall static void
3201369Sdduvall bge_start(bge_t *bgep, boolean_t reset_phys)
3211369Sdduvall {
3221369Sdduvall 	BGE_TRACE(("bge_start($%p, %d)", (void *)bgep, reset_phys));
3231369Sdduvall 
3241369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3251369Sdduvall 
3261369Sdduvall 	/*
3271369Sdduvall 	 * Start chip processing, including enabling interrupts
3281369Sdduvall 	 */
3291369Sdduvall 	bge_chip_start(bgep, reset_phys);
3301369Sdduvall 
3311369Sdduvall 	BGE_DEBUG(("bge_start($%p, %d) done", (void *)bgep, reset_phys));
3321369Sdduvall }
3331369Sdduvall 
3341369Sdduvall /*
3351369Sdduvall  * bge_restart - restart transmitting/receiving after error or suspend
3361369Sdduvall  */
3371369Sdduvall void
3381369Sdduvall bge_restart(bge_t *bgep, boolean_t reset_phys)
3391369Sdduvall {
3401369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3411369Sdduvall 
3421408Srandyf #ifdef BGE_IPMI_ASF
3431408Srandyf 	if (bgep->asf_enabled) {
3441408Srandyf 		bge_reset(bgep, ASF_MODE_POST_INIT);
3451408Srandyf 	} else
3461408Srandyf 		bge_reset(bgep, ASF_MODE_NONE);
3471408Srandyf #else
3481369Sdduvall 	bge_reset(bgep);
3491408Srandyf #endif
3501369Sdduvall 	if (bgep->bge_mac_state == BGE_MAC_STARTED) {
3511369Sdduvall 		bge_start(bgep, reset_phys);
3521369Sdduvall 		bgep->watchdog = 0;
3531369Sdduvall 		ddi_trigger_softintr(bgep->resched_id);
3541369Sdduvall 	}
3551369Sdduvall 
3561369Sdduvall 	BGE_DEBUG(("bge_restart($%p, %d) done", (void *)bgep, reset_phys));
3571369Sdduvall }
3581369Sdduvall 
3591369Sdduvall 
3601369Sdduvall /*
3611369Sdduvall  * ========== Nemo-required management entry points ==========
3621369Sdduvall  */
3631369Sdduvall 
3641369Sdduvall #undef	BGE_DBG
3651369Sdduvall #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
3661369Sdduvall 
3671369Sdduvall /*
3681369Sdduvall  *	bge_m_stop() -- stop transmitting/receiving
3691369Sdduvall  */
3701369Sdduvall static void
3711369Sdduvall bge_m_stop(void *arg)
3721369Sdduvall {
3731369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
3741369Sdduvall 
3751369Sdduvall 	BGE_TRACE(("bge_m_stop($%p)", arg));
3761369Sdduvall 
3771369Sdduvall 	/*
3781369Sdduvall 	 * Just stop processing, then record new GLD state
3791369Sdduvall 	 */
3801369Sdduvall 	mutex_enter(bgep->genlock);
3811369Sdduvall 	bgep->link_up_msg = bgep->link_down_msg = " (stopped)";
3821369Sdduvall 	bge_stop(bgep);
3831369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STOPPED;
3841369Sdduvall 	BGE_DEBUG(("bge_m_stop($%p) done", arg));
3851369Sdduvall 	mutex_exit(bgep->genlock);
3861369Sdduvall }
3871369Sdduvall 
3881369Sdduvall /*
3891369Sdduvall  *	bge_m_start() -- start transmitting/receiving
3901369Sdduvall  */
3911369Sdduvall static int
3921369Sdduvall bge_m_start(void *arg)
3931369Sdduvall {
3941369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
3951369Sdduvall 
3961369Sdduvall 	BGE_TRACE(("bge_m_start($%p)", arg));
3971369Sdduvall 
3981369Sdduvall 	/*
3991369Sdduvall 	 * Start processing and record new GLD state
4001369Sdduvall 	 */
4011369Sdduvall 	mutex_enter(bgep->genlock);
4021408Srandyf #ifdef BGE_IPMI_ASF
4031408Srandyf 	if (bgep->asf_enabled) {
4041408Srandyf 		if ((bgep->asf_status == ASF_STAT_RUN) &&
4051408Srandyf 			(bgep->asf_pseudostop)) {
4061408Srandyf 
4071408Srandyf 			bgep->link_up_msg = bgep->link_down_msg
4081408Srandyf 				= " (initialized)";
4091408Srandyf 			bgep->bge_mac_state = BGE_MAC_STARTED;
4101408Srandyf 			mutex_exit(bgep->genlock);
4111408Srandyf 			return (0);
4121408Srandyf 		}
4131408Srandyf 	}
4141408Srandyf 	bge_reset(bgep, ASF_MODE_INIT);
4151408Srandyf #else
4161369Sdduvall 	bge_reset(bgep);
4171408Srandyf #endif
4181369Sdduvall 	bgep->link_up_msg = bgep->link_down_msg = " (initialized)";
4191369Sdduvall 	bge_start(bgep, B_TRUE);
4201369Sdduvall 	bgep->bge_mac_state = BGE_MAC_STARTED;
4211369Sdduvall 	BGE_DEBUG(("bge_m_start($%p) done", arg));
4221408Srandyf 
4231408Srandyf #ifdef BGE_IPMI_ASF
4241408Srandyf 	if (bgep->asf_enabled) {
4251408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
4261408Srandyf 			/* start ASF heart beat */
4271408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
4281408Srandyf 				(void *)bgep,
4291408Srandyf 				drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
4301408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
4311408Srandyf 		}
4321408Srandyf 	}
4331408Srandyf #endif
4341369Sdduvall 	mutex_exit(bgep->genlock);
4351369Sdduvall 
4361369Sdduvall 	return (0);
4371369Sdduvall }
4381369Sdduvall 
4391369Sdduvall /*
4401369Sdduvall  *	bge_m_unicst_set() -- set the physical network address
4411369Sdduvall  */
4421369Sdduvall static int
4431369Sdduvall bge_m_unicst(void *arg, const uint8_t *macaddr)
4441369Sdduvall {
4451369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
4461369Sdduvall 
4471369Sdduvall 	BGE_TRACE(("bge_m_unicst_set($%p, %s)", arg,
4481369Sdduvall 		ether_sprintf((void *)macaddr)));
4491369Sdduvall 
4501369Sdduvall 	/*
4511369Sdduvall 	 * Remember the new current address in the driver state
4521369Sdduvall 	 * Sync the chip's idea of the address too ...
4531369Sdduvall 	 */
4541369Sdduvall 	mutex_enter(bgep->genlock);
4551369Sdduvall 	ethaddr_copy(macaddr, bgep->curr_addr.addr);
4561408Srandyf #ifdef BGE_IPMI_ASF
4571408Srandyf 	bge_chip_sync(bgep, B_FALSE);
4581408Srandyf 	if (bgep->asf_enabled) {
4591408Srandyf 		/*
4601408Srandyf 		 * The above bge_chip_sync() function wrote the ethernet MAC
4611408Srandyf 		 * addresses registers which destroyed the IPMI/ASF sideband.
4621408Srandyf 		 * Here, we have to reset chip to make IPMI/ASF sideband work.
4631408Srandyf 		 */
4641408Srandyf 		if (bgep->asf_status == ASF_STAT_RUN) {
4651408Srandyf 			/*
4661408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
4671408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
4681408Srandyf 			 * may crash.
4691408Srandyf 			 */
4701408Srandyf 			bge_asf_update_status(bgep);
4711408Srandyf 			bge_asf_stop_timer(bgep);
4721408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
4731408Srandyf 
4741408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
4751408Srandyf 		}
4761408Srandyf 		bge_chip_stop(bgep, B_TRUE);
4771408Srandyf 
4781408Srandyf 		bge_restart(bgep, B_FALSE);
4791408Srandyf 		/*
4801408Srandyf 		 * Start our ASF heartbeat counter as soon as possible.
4811408Srandyf 		 */
4821408Srandyf 		if (bgep->asf_status != ASF_STAT_RUN) {
4831408Srandyf 			/* start ASF heart beat */
4841408Srandyf 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
4851408Srandyf 				(void *)bgep,
4861408Srandyf 				drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
4871408Srandyf 			bgep->asf_status = ASF_STAT_RUN;
4881408Srandyf 		}
4891408Srandyf 	}
4901408Srandyf #else
4911369Sdduvall 	bge_chip_sync(bgep);
4921408Srandyf #endif
4931369Sdduvall 	BGE_DEBUG(("bge_m_unicst_set($%p) done", arg));
4941369Sdduvall 	mutex_exit(bgep->genlock);
4951369Sdduvall 
4961369Sdduvall 	return (0);
4971369Sdduvall }
4981369Sdduvall 
4991369Sdduvall /*
5001369Sdduvall  * Compute the index of the required bit in the multicast hash map.
5011369Sdduvall  * This must mirror the way the hardware actually does it!
5021369Sdduvall  * See Broadcom document 570X-PG102-R page 125.
5031369Sdduvall  */
5041369Sdduvall static uint32_t
5051369Sdduvall bge_hash_index(const uint8_t *mca)
5061369Sdduvall {
5071369Sdduvall 	uint32_t hash;
5081369Sdduvall 
5091369Sdduvall 	CRC32(hash, mca, ETHERADDRL, -1U, crc32_table);
5101369Sdduvall 
5111369Sdduvall 	return (hash);
5121369Sdduvall }
5131369Sdduvall 
5141369Sdduvall /*
5151369Sdduvall  *	bge_m_multicst_add() -- enable/disable a multicast address
5161369Sdduvall  */
5171369Sdduvall static int
5181369Sdduvall bge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
5191369Sdduvall {
5201369Sdduvall 	bge_t *bgep = arg;		/* private device info	*/
5211369Sdduvall 	uint32_t hash;
5221369Sdduvall 	uint32_t index;
5231369Sdduvall 	uint32_t word;
5241369Sdduvall 	uint32_t bit;
5251369Sdduvall 	uint8_t *refp;
5261369Sdduvall 
5271369Sdduvall 	BGE_TRACE(("bge_m_multicst($%p, %s, %s)", arg,
5281369Sdduvall 		(add) ? "add" : "remove", ether_sprintf((void *)mca)));
5291369Sdduvall 
5301369Sdduvall 	/*
5311369Sdduvall 	 * Precalculate all required masks, pointers etc ...
5321369Sdduvall 	 */
5331369Sdduvall 	hash = bge_hash_index(mca);
5341369Sdduvall 	index = hash % BGE_HASH_TABLE_SIZE;
5351369Sdduvall 	word = index/32u;
5361369Sdduvall 	bit = 1 << (index % 32u);
5371369Sdduvall 	refp = &bgep->mcast_refs[index];
5381369Sdduvall 
5391369Sdduvall 	BGE_DEBUG(("bge_m_multicst: hash 0x%x index %d (%d:0x%x) = %d",
5401369Sdduvall 		hash, index, word, bit, *refp));
5411369Sdduvall 
5421369Sdduvall 	/*
5431369Sdduvall 	 * We must set the appropriate bit in the hash map (and the
5441369Sdduvall 	 * corresponding h/w register) when the refcount goes from 0
5451369Sdduvall 	 * to >0, and clear it when the last ref goes away (refcount
5461369Sdduvall 	 * goes from >0 back to 0).  If we change the hash map, we
5471369Sdduvall 	 * must also update the chip's hardware map registers.
5481369Sdduvall 	 */
5491369Sdduvall 	mutex_enter(bgep->genlock);
5501369Sdduvall 	if (add) {
5511369Sdduvall 		if ((*refp)++ == 0) {
5521369Sdduvall 			bgep->mcast_hash[word] |= bit;
5531408Srandyf #ifdef BGE_IPMI_ASF
5541408Srandyf 			bge_chip_sync(bgep, B_TRUE);
5551408Srandyf #else
5561369Sdduvall 			bge_chip_sync(bgep);
5571408Srandyf #endif
5581369Sdduvall 		}
5591369Sdduvall 	} else {
5601369Sdduvall 		if (--(*refp) == 0) {
5611369Sdduvall 			bgep->mcast_hash[word] &= ~bit;
5621408Srandyf #ifdef BGE_IPMI_ASF
5631408Srandyf 			bge_chip_sync(bgep, B_TRUE);
5641408Srandyf #else
5651369Sdduvall 			bge_chip_sync(bgep);
5661408Srandyf #endif
5671369Sdduvall 		}
5681369Sdduvall 	}
5691369Sdduvall 	BGE_DEBUG(("bge_m_multicst($%p) done", arg));
5701369Sdduvall 	mutex_exit(bgep->genlock);
5711369Sdduvall 
5721369Sdduvall 	return (0);
5731369Sdduvall }
5741369Sdduvall 
5751369Sdduvall /*
5761369Sdduvall  * bge_m_promisc() -- set or reset promiscuous mode on the board
5771369Sdduvall  *
5781369Sdduvall  *	Program the hardware to enable/disable promiscuous and/or
5791369Sdduvall  *	receive-all-multicast modes.
5801369Sdduvall  */
5811369Sdduvall static int
5821369Sdduvall bge_m_promisc(void *arg, boolean_t on)
5831369Sdduvall {
5841369Sdduvall 	bge_t *bgep = arg;
5851369Sdduvall 
5861369Sdduvall 	BGE_TRACE(("bge_m_promisc_set($%p, %d)", arg, on));
5871369Sdduvall 
5881369Sdduvall 	/*
5891369Sdduvall 	 * Store MAC layer specified mode and pass to chip layer to update h/w
5901369Sdduvall 	 */
5911369Sdduvall 	mutex_enter(bgep->genlock);
5921369Sdduvall 	bgep->promisc = on;
5931408Srandyf #ifdef BGE_IPMI_ASF
5941408Srandyf 	bge_chip_sync(bgep, B_TRUE);
5951408Srandyf #else
5961369Sdduvall 	bge_chip_sync(bgep);
5971408Srandyf #endif
5981369Sdduvall 	BGE_DEBUG(("bge_m_promisc_set($%p) done", arg));
5991369Sdduvall 	mutex_exit(bgep->genlock);
6001369Sdduvall 	return (0);
6011369Sdduvall }
6021369Sdduvall 
6031369Sdduvall /*
6041369Sdduvall  * Loopback ioctl code
6051369Sdduvall  */
6061369Sdduvall 
6071369Sdduvall static lb_property_t loopmodes[] = {
6081369Sdduvall 	{ normal,	"normal",	BGE_LOOP_NONE		},
6091369Sdduvall 	{ external,	"1000Mbps",	BGE_LOOP_EXTERNAL_1000	},
6101369Sdduvall 	{ external,	"100Mbps",	BGE_LOOP_EXTERNAL_100	},
6111369Sdduvall 	{ external,	"10Mbps",	BGE_LOOP_EXTERNAL_10	},
6121369Sdduvall 	{ internal,	"PHY",		BGE_LOOP_INTERNAL_PHY	},
6131369Sdduvall 	{ internal,	"MAC",		BGE_LOOP_INTERNAL_MAC	}
6141369Sdduvall };
6151369Sdduvall 
6161369Sdduvall static enum ioc_reply
6171369Sdduvall bge_set_loop_mode(bge_t *bgep, uint32_t mode)
6181369Sdduvall {
6191369Sdduvall 	const char *msg;
6201369Sdduvall 
6211369Sdduvall 	/*
6221369Sdduvall 	 * If the mode isn't being changed, there's nothing to do ...
6231369Sdduvall 	 */
6241369Sdduvall 	if (mode == bgep->param_loop_mode)
6251369Sdduvall 		return (IOC_ACK);
6261369Sdduvall 
6271369Sdduvall 	/*
6281369Sdduvall 	 * Validate the requested mode and prepare a suitable message
6291369Sdduvall 	 * to explain the link down/up cycle that the change will
6301369Sdduvall 	 * probably induce ...
6311369Sdduvall 	 */
6321369Sdduvall 	switch (mode) {
6331369Sdduvall 	default:
6341369Sdduvall 		return (IOC_INVAL);
6351369Sdduvall 
6361369Sdduvall 	case BGE_LOOP_NONE:
6371369Sdduvall 		msg = " (loopback disabled)";
6381369Sdduvall 		break;
6391369Sdduvall 
6401369Sdduvall 	case BGE_LOOP_EXTERNAL_1000:
6411369Sdduvall 	case BGE_LOOP_EXTERNAL_100:
6421369Sdduvall 	case BGE_LOOP_EXTERNAL_10:
6431369Sdduvall 		msg = " (external loopback selected)";
6441369Sdduvall 		break;
6451369Sdduvall 
6461369Sdduvall 	case BGE_LOOP_INTERNAL_PHY:
6471369Sdduvall 		msg = " (PHY internal loopback selected)";
6481369Sdduvall 		break;
6491369Sdduvall 
6501369Sdduvall 	case BGE_LOOP_INTERNAL_MAC:
6511369Sdduvall 		msg = " (MAC internal loopback selected)";
6521369Sdduvall 		break;
6531369Sdduvall 	}
6541369Sdduvall 
6551369Sdduvall 	/*
6561369Sdduvall 	 * All OK; tell the caller to reprogram
6571369Sdduvall 	 * the PHY and/or MAC for the new mode ...
6581369Sdduvall 	 */
6591369Sdduvall 	bgep->link_down_msg = bgep->link_up_msg = msg;
6601369Sdduvall 	bgep->param_loop_mode = mode;
6611369Sdduvall 	return (IOC_RESTART_ACK);
6621369Sdduvall }
6631369Sdduvall 
6641369Sdduvall static enum ioc_reply
6651369Sdduvall bge_loop_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
6661369Sdduvall {
6671369Sdduvall 	lb_info_sz_t *lbsp;
6681369Sdduvall 	lb_property_t *lbpp;
6691369Sdduvall 	uint32_t *lbmp;
6701369Sdduvall 	int cmd;
6711369Sdduvall 
6721369Sdduvall 	_NOTE(ARGUNUSED(wq))
6731369Sdduvall 
6741369Sdduvall 	/*
6751369Sdduvall 	 * Validate format of ioctl
6761369Sdduvall 	 */
6771369Sdduvall 	if (mp->b_cont == NULL)
6781369Sdduvall 		return (IOC_INVAL);
6791369Sdduvall 
6801369Sdduvall 	cmd = iocp->ioc_cmd;
6811369Sdduvall 	switch (cmd) {
6821369Sdduvall 	default:
6831369Sdduvall 		/* NOTREACHED */
6841369Sdduvall 		bge_error(bgep, "bge_loop_ioctl: invalid cmd 0x%x", cmd);
6851369Sdduvall 		return (IOC_INVAL);
6861369Sdduvall 
6871369Sdduvall 	case LB_GET_INFO_SIZE:
6881369Sdduvall 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
6891369Sdduvall 			return (IOC_INVAL);
6901369Sdduvall 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
6911369Sdduvall 		*lbsp = sizeof (loopmodes);
6921369Sdduvall 		return (IOC_REPLY);
6931369Sdduvall 
6941369Sdduvall 	case LB_GET_INFO:
6951369Sdduvall 		if (iocp->ioc_count != sizeof (loopmodes))
6961369Sdduvall 			return (IOC_INVAL);
6971369Sdduvall 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
6981369Sdduvall 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
6991369Sdduvall 		return (IOC_REPLY);
7001369Sdduvall 
7011369Sdduvall 	case LB_GET_MODE:
7021369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
7031369Sdduvall 			return (IOC_INVAL);
7041369Sdduvall 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
7051369Sdduvall 		*lbmp = bgep->param_loop_mode;
7061369Sdduvall 		return (IOC_REPLY);
7071369Sdduvall 
7081369Sdduvall 	case LB_SET_MODE:
7091369Sdduvall 		if (iocp->ioc_count != sizeof (uint32_t))
7101369Sdduvall 			return (IOC_INVAL);
7111369Sdduvall 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
7121369Sdduvall 		return (bge_set_loop_mode(bgep, *lbmp));
7131369Sdduvall 	}
7141369Sdduvall }
7151369Sdduvall 
7161369Sdduvall /*
7171369Sdduvall  * Specific bge IOCTLs, the gld module handles the generic ones.
7181369Sdduvall  */
7191369Sdduvall static void
7201369Sdduvall bge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
7211369Sdduvall {
7221369Sdduvall 	bge_t *bgep = arg;
7231369Sdduvall 	struct iocblk *iocp;
7241369Sdduvall 	enum ioc_reply status;
7251369Sdduvall 	boolean_t need_privilege;
7261369Sdduvall 	int err;
7271369Sdduvall 	int cmd;
7281369Sdduvall 
7291369Sdduvall 	/*
7301369Sdduvall 	 * Validate the command before bothering with the mutex ...
7311369Sdduvall 	 */
7321369Sdduvall 	iocp = (struct iocblk *)mp->b_rptr;
7331369Sdduvall 	iocp->ioc_error = 0;
7341369Sdduvall 	need_privilege = B_TRUE;
7351369Sdduvall 	cmd = iocp->ioc_cmd;
7361369Sdduvall 	switch (cmd) {
7371369Sdduvall 	default:
7381369Sdduvall 		miocnak(wq, mp, 0, EINVAL);
7391369Sdduvall 		return;
7401369Sdduvall 
7411369Sdduvall 	case BGE_MII_READ:
7421369Sdduvall 	case BGE_MII_WRITE:
7431369Sdduvall 	case BGE_SEE_READ:
7441369Sdduvall 	case BGE_SEE_WRITE:
7451369Sdduvall 	case BGE_DIAG:
7461369Sdduvall 	case BGE_PEEK:
7471369Sdduvall 	case BGE_POKE:
7481369Sdduvall 	case BGE_PHY_RESET:
7491369Sdduvall 	case BGE_SOFT_RESET:
7501369Sdduvall 	case BGE_HARD_RESET:
7511369Sdduvall 		break;
7521369Sdduvall 
7531369Sdduvall 	case LB_GET_INFO_SIZE:
7541369Sdduvall 	case LB_GET_INFO:
7551369Sdduvall 	case LB_GET_MODE:
7561369Sdduvall 		need_privilege = B_FALSE;
7571369Sdduvall 		/* FALLTHRU */
7581369Sdduvall 	case LB_SET_MODE:
7591369Sdduvall 		break;
7601369Sdduvall 
7611369Sdduvall 	case ND_GET:
7621369Sdduvall 		need_privilege = B_FALSE;
7631369Sdduvall 		/* FALLTHRU */
7641369Sdduvall 	case ND_SET:
7651369Sdduvall 		break;
7661369Sdduvall 	}
7671369Sdduvall 
7681369Sdduvall 	if (need_privilege) {
7691369Sdduvall 		/*
7701369Sdduvall 		 * Check for specific net_config privilege on Solaris 10+.
7711369Sdduvall 		 * Otherwise just check for root access ...
7721369Sdduvall 		 */
7731369Sdduvall 		if (secpolicy_net_config != NULL)
7741369Sdduvall 			err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
7751369Sdduvall 		else
7761369Sdduvall 			err = drv_priv(iocp->ioc_cr);
7771369Sdduvall 		if (err != 0) {
7781369Sdduvall 			miocnak(wq, mp, 0, err);
7791369Sdduvall 			return;
7801369Sdduvall 		}
7811369Sdduvall 	}
7821369Sdduvall 
7831369Sdduvall 	mutex_enter(bgep->genlock);
7841369Sdduvall 
7851369Sdduvall 	switch (cmd) {
7861369Sdduvall 	default:
7871369Sdduvall 		_NOTE(NOTREACHED)
7881369Sdduvall 		status = IOC_INVAL;
7891369Sdduvall 		break;
7901369Sdduvall 
7911369Sdduvall 	case BGE_MII_READ:
7921369Sdduvall 	case BGE_MII_WRITE:
7931369Sdduvall 	case BGE_SEE_READ:
7941369Sdduvall 	case BGE_SEE_WRITE:
7951369Sdduvall 	case BGE_DIAG:
7961369Sdduvall 	case BGE_PEEK:
7971369Sdduvall 	case BGE_POKE:
7981369Sdduvall 	case BGE_PHY_RESET:
7991369Sdduvall 	case BGE_SOFT_RESET:
8001369Sdduvall 	case BGE_HARD_RESET:
8011369Sdduvall 		status = bge_chip_ioctl(bgep, wq, mp, iocp);
8021369Sdduvall 		break;
8031369Sdduvall 
8041369Sdduvall 	case LB_GET_INFO_SIZE:
8051369Sdduvall 	case LB_GET_INFO:
8061369Sdduvall 	case LB_GET_MODE:
8071369Sdduvall 	case LB_SET_MODE:
8081369Sdduvall 		status = bge_loop_ioctl(bgep, wq, mp, iocp);
8091369Sdduvall 		break;
8101369Sdduvall 
8111369Sdduvall 	case ND_GET:
8121369Sdduvall 	case ND_SET:
8131369Sdduvall 		status = bge_nd_ioctl(bgep, wq, mp, iocp);
8141369Sdduvall 		break;
8151369Sdduvall 	}
8161369Sdduvall 
8171369Sdduvall 	/*
8181369Sdduvall 	 * Do we need to reprogram the PHY and/or the MAC?
8191369Sdduvall 	 * Do it now, while we still have the mutex.
8201369Sdduvall 	 *
8211369Sdduvall 	 * Note: update the PHY first, 'cos it controls the
8221369Sdduvall 	 * speed/duplex parameters that the MAC code uses.
8231369Sdduvall 	 */
8241369Sdduvall 	switch (status) {
8251369Sdduvall 	case IOC_RESTART_REPLY:
8261369Sdduvall 	case IOC_RESTART_ACK:
8271369Sdduvall 		bge_phys_update(bgep);
8281408Srandyf #ifdef BGE_IPMI_ASF
8291408Srandyf 		bge_chip_sync(bgep, B_FALSE);
8301408Srandyf #else
8311369Sdduvall 		bge_chip_sync(bgep);
8321408Srandyf #endif
8331369Sdduvall 		if (bgep->intr_type == DDI_INTR_TYPE_MSI)
8341369Sdduvall 			bge_chip_msi_trig(bgep);
8351369Sdduvall 		break;
8361369Sdduvall 	}
8371369Sdduvall 
8381369Sdduvall 	mutex_exit(bgep->genlock);
8391369Sdduvall 
8401369Sdduvall 	/*
8411369Sdduvall 	 * Finally, decide how to reply
8421369Sdduvall 	 */
8431369Sdduvall 	switch (status) {
8441369Sdduvall 	default:
8451369Sdduvall 	case IOC_INVAL:
8461369Sdduvall 		/*
8471369Sdduvall 		 * Error, reply with a NAK and EINVAL or the specified error
8481369Sdduvall 		 */
8491369Sdduvall 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
8501369Sdduvall 			EINVAL : iocp->ioc_error);
8511369Sdduvall 		break;
8521369Sdduvall 
8531369Sdduvall 	case IOC_DONE:
8541369Sdduvall 		/*
8551369Sdduvall 		 * OK, reply already sent
8561369Sdduvall 		 */
8571369Sdduvall 		break;
8581369Sdduvall 
8591369Sdduvall 	case IOC_RESTART_ACK:
8601369Sdduvall 	case IOC_ACK:
8611369Sdduvall 		/*
8621369Sdduvall 		 * OK, reply with an ACK
8631369Sdduvall 		 */
8641369Sdduvall 		miocack(wq, mp, 0, 0);
8651369Sdduvall 		break;
8661369Sdduvall 
8671369Sdduvall 	case IOC_RESTART_REPLY:
8681369Sdduvall 	case IOC_REPLY:
8691369Sdduvall 		/*
8701369Sdduvall 		 * OK, send prepared reply as ACK or NAK
8711369Sdduvall 		 */
8721369Sdduvall 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
8731369Sdduvall 			M_IOCACK : M_IOCNAK;
8741369Sdduvall 		qreply(wq, mp);
8751369Sdduvall 		break;
8761369Sdduvall 	}
8771369Sdduvall }
8781369Sdduvall 
8791369Sdduvall static void
8801369Sdduvall bge_m_resources(void *arg)
8811369Sdduvall {
8821369Sdduvall 	bge_t *bgep = arg;
8831369Sdduvall 	recv_ring_t *rrp;
8841369Sdduvall 	mac_rx_fifo_t mrf;
8851369Sdduvall 	int ring;
8861369Sdduvall 
8871369Sdduvall 	mutex_enter(bgep->genlock);
8881369Sdduvall 
8891369Sdduvall 	/*
8901369Sdduvall 	 * Register Rx rings as resources and save mac
8911369Sdduvall 	 * resource id for future reference
8921369Sdduvall 	 */
8931369Sdduvall 	mrf.mrf_type = MAC_RX_FIFO;
8941369Sdduvall 	mrf.mrf_blank = bge_chip_blank;
8951369Sdduvall 	mrf.mrf_arg = (void *)bgep;
8961369Sdduvall 	mrf.mrf_normal_blank_time = bge_rx_ticks_norm;
8971369Sdduvall 	mrf.mrf_normal_pkt_count = bge_rx_count_norm;
8981369Sdduvall 
8991369Sdduvall 	for (ring = 0; ring < bgep->chipid.rx_rings; ring++) {
9001369Sdduvall 		rrp = &bgep->recv[ring];
9011369Sdduvall 		rrp->handle = mac_resource_add(bgep->macp,
9021369Sdduvall 		    (mac_resource_t *)&mrf);
9031369Sdduvall 	}
9041369Sdduvall 
9051369Sdduvall 	mutex_exit(bgep->genlock);
9061369Sdduvall }
9071369Sdduvall 
9081369Sdduvall /*
9091369Sdduvall  * ========== Per-instance setup/teardown code ==========
9101369Sdduvall  */
9111369Sdduvall 
9121369Sdduvall #undef	BGE_DBG
9131369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
9141369Sdduvall 
9151369Sdduvall /*
9161369Sdduvall  * Utility routine to carve a slice off a chunk of allocated memory,
9171369Sdduvall  * updating the chunk descriptor accordingly.  The size of the slice
9181369Sdduvall  * is given by the product of the <qty> and <size> parameters.
9191369Sdduvall  */
9201369Sdduvall static void
9211369Sdduvall bge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
9221369Sdduvall 	uint32_t qty, uint32_t size)
9231369Sdduvall {
9241369Sdduvall 	static uint32_t sequence = 0xbcd5704a;
9251369Sdduvall 	size_t totsize;
9261369Sdduvall 
9271369Sdduvall 	totsize = qty*size;
9281369Sdduvall 	ASSERT(size >= 0);
9291369Sdduvall 	ASSERT(totsize <= chunk->alength);
9301369Sdduvall 
9311369Sdduvall 	*slice = *chunk;
9321369Sdduvall 	slice->nslots = qty;
9331369Sdduvall 	slice->size = size;
9341369Sdduvall 	slice->alength = totsize;
9351369Sdduvall 	slice->token = ++sequence;
9361369Sdduvall 
9371369Sdduvall 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
9381369Sdduvall 	chunk->alength -= totsize;
9391369Sdduvall 	chunk->offset += totsize;
9401369Sdduvall 	chunk->cookie.dmac_laddress += totsize;
9411369Sdduvall 	chunk->cookie.dmac_size -= totsize;
9421369Sdduvall }
9431369Sdduvall 
9441369Sdduvall /*
9451369Sdduvall  * Initialise the specified Receive Producer (Buffer) Ring, using
9461369Sdduvall  * the information in the <dma_area> descriptors that it contains
9471369Sdduvall  * to set up all the other fields. This routine should be called
9481369Sdduvall  * only once for each ring.
9491369Sdduvall  */
9501369Sdduvall static void
9511369Sdduvall bge_init_buff_ring(bge_t *bgep, uint64_t ring)
9521369Sdduvall {
9531369Sdduvall 	buff_ring_t *brp;
9541369Sdduvall 	bge_status_t *bsp;
9551369Sdduvall 	sw_rbd_t *srbdp;
9561369Sdduvall 	dma_area_t pbuf;
9571369Sdduvall 	uint32_t bufsize;
9581369Sdduvall 	uint32_t nslots;
9591369Sdduvall 	uint32_t slot;
9601369Sdduvall 	uint32_t split;
9611369Sdduvall 
9621369Sdduvall 	static bge_regno_t nic_ring_addrs[BGE_BUFF_RINGS_MAX] = {
9631369Sdduvall 		NIC_MEM_SHADOW_BUFF_STD,
9641369Sdduvall 		NIC_MEM_SHADOW_BUFF_JUMBO,
9651369Sdduvall 		NIC_MEM_SHADOW_BUFF_MINI
9661369Sdduvall 	};
9671369Sdduvall 	static bge_regno_t mailbox_regs[BGE_BUFF_RINGS_MAX] = {
9681369Sdduvall 		RECV_STD_PROD_INDEX_REG,
9691369Sdduvall 		RECV_JUMBO_PROD_INDEX_REG,
9701369Sdduvall 		RECV_MINI_PROD_INDEX_REG
9711369Sdduvall 	};
9721369Sdduvall 	static bge_regno_t buff_cons_xref[BGE_BUFF_RINGS_MAX] = {
9731369Sdduvall 		STATUS_STD_BUFF_CONS_INDEX,
9741369Sdduvall 		STATUS_JUMBO_BUFF_CONS_INDEX,
9751369Sdduvall 		STATUS_MINI_BUFF_CONS_INDEX
9761369Sdduvall 	};
9771369Sdduvall 
9781369Sdduvall 	BGE_TRACE(("bge_init_buff_ring($%p, %d)",
9791369Sdduvall 		(void *)bgep, ring));
9801369Sdduvall 
9811369Sdduvall 	brp = &bgep->buff[ring];
9821369Sdduvall 	nslots = brp->desc.nslots;
9831369Sdduvall 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
9841369Sdduvall 	bufsize = brp->buf[0].size;
9851369Sdduvall 
9861369Sdduvall 	/*
9871369Sdduvall 	 * Set up the copy of the h/w RCB
9881369Sdduvall 	 *
9891369Sdduvall 	 * Note: unlike Send & Receive Return Rings, (where the max_len
9901369Sdduvall 	 * field holds the number of slots), in a Receive Buffer Ring
9911369Sdduvall 	 * this field indicates the size of each buffer in the ring.
9921369Sdduvall 	 */
9931369Sdduvall 	brp->hw_rcb.host_ring_addr = brp->desc.cookie.dmac_laddress;
9941369Sdduvall 	brp->hw_rcb.max_len = bufsize;
9951369Sdduvall 	brp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
9961369Sdduvall 	brp->hw_rcb.nic_ring_addr = nic_ring_addrs[ring];
9971369Sdduvall 
9981369Sdduvall 	/*
9991369Sdduvall 	 * Other one-off initialisation of per-ring data
10001369Sdduvall 	 */
10011369Sdduvall 	brp->bgep = bgep;
10021369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
10031369Sdduvall 	brp->cons_index_p = &bsp->buff_cons_index[buff_cons_xref[ring]];
10041369Sdduvall 	brp->chip_mbx_reg = mailbox_regs[ring];
10051369Sdduvall 	mutex_init(brp->rf_lock, NULL, MUTEX_DRIVER,
10061369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
10071369Sdduvall 
10081369Sdduvall 	/*
10091369Sdduvall 	 * Allocate the array of s/w Receive Buffer Descriptors
10101369Sdduvall 	 */
10111369Sdduvall 	srbdp = kmem_zalloc(nslots*sizeof (*srbdp), KM_SLEEP);
10121369Sdduvall 	brp->sw_rbds = srbdp;
10131369Sdduvall 
10141369Sdduvall 	/*
10151369Sdduvall 	 * Now initialise each array element once and for all
10161369Sdduvall 	 */
10171369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
10181369Sdduvall 		pbuf = brp->buf[split];
10191369Sdduvall 		for (slot = 0; slot < nslots/BGE_SPLIT; ++srbdp, ++slot)
10201369Sdduvall 			bge_slice_chunk(&srbdp->pbuf, &pbuf, 1, bufsize);
10211369Sdduvall 		ASSERT(pbuf.alength == 0);
10221369Sdduvall 	}
10231369Sdduvall }
10241369Sdduvall 
10251369Sdduvall /*
10261369Sdduvall  * Clean up initialisation done above before the memory is freed
10271369Sdduvall  */
10281369Sdduvall static void
10291369Sdduvall bge_fini_buff_ring(bge_t *bgep, uint64_t ring)
10301369Sdduvall {
10311369Sdduvall 	buff_ring_t *brp;
10321369Sdduvall 	sw_rbd_t *srbdp;
10331369Sdduvall 
10341369Sdduvall 	BGE_TRACE(("bge_fini_buff_ring($%p, %d)",
10351369Sdduvall 		(void *)bgep, ring));
10361369Sdduvall 
10371369Sdduvall 	brp = &bgep->buff[ring];
10381369Sdduvall 	srbdp = brp->sw_rbds;
10391369Sdduvall 	kmem_free(srbdp, brp->desc.nslots*sizeof (*srbdp));
10401369Sdduvall 
10411369Sdduvall 	mutex_destroy(brp->rf_lock);
10421369Sdduvall }
10431369Sdduvall 
10441369Sdduvall /*
10451369Sdduvall  * Initialise the specified Receive (Return) Ring, using the
10461369Sdduvall  * information in the <dma_area> descriptors that it contains
10471369Sdduvall  * to set up all the other fields. This routine should be called
10481369Sdduvall  * only once for each ring.
10491369Sdduvall  */
10501369Sdduvall static void
10511369Sdduvall bge_init_recv_ring(bge_t *bgep, uint64_t ring)
10521369Sdduvall {
10531369Sdduvall 	recv_ring_t *rrp;
10541369Sdduvall 	bge_status_t *bsp;
10551369Sdduvall 	uint32_t nslots;
10561369Sdduvall 
10571369Sdduvall 	BGE_TRACE(("bge_init_recv_ring($%p, %d)",
10581369Sdduvall 		(void *)bgep, ring));
10591369Sdduvall 
10601369Sdduvall 	/*
10611369Sdduvall 	 * The chip architecture requires that receive return rings have
10621369Sdduvall 	 * 512 or 1024 or 2048 elements per ring.  See 570X-PG108-R page 103.
10631369Sdduvall 	 */
10641369Sdduvall 	rrp = &bgep->recv[ring];
10651369Sdduvall 	nslots = rrp->desc.nslots;
10661369Sdduvall 	ASSERT(nslots == 0 || nslots == 512 ||
10671369Sdduvall 		nslots == 1024 || nslots == 2048);
10681369Sdduvall 
10691369Sdduvall 	/*
10701369Sdduvall 	 * Set up the copy of the h/w RCB
10711369Sdduvall 	 */
10721369Sdduvall 	rrp->hw_rcb.host_ring_addr = rrp->desc.cookie.dmac_laddress;
10731369Sdduvall 	rrp->hw_rcb.max_len = nslots;
10741369Sdduvall 	rrp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
10751369Sdduvall 	rrp->hw_rcb.nic_ring_addr = 0;
10761369Sdduvall 
10771369Sdduvall 	/*
10781369Sdduvall 	 * Other one-off initialisation of per-ring data
10791369Sdduvall 	 */
10801369Sdduvall 	rrp->bgep = bgep;
10811369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
10821369Sdduvall 	rrp->prod_index_p = RECV_INDEX_P(bsp, ring);
10831369Sdduvall 	rrp->chip_mbx_reg = RECV_RING_CONS_INDEX_REG(ring);
10841369Sdduvall 	mutex_init(rrp->rx_lock, NULL, MUTEX_DRIVER,
10851369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
10861369Sdduvall }
10871369Sdduvall 
10881369Sdduvall 
10891369Sdduvall /*
10901369Sdduvall  * Clean up initialisation done above before the memory is freed
10911369Sdduvall  */
10921369Sdduvall static void
10931369Sdduvall bge_fini_recv_ring(bge_t *bgep, uint64_t ring)
10941369Sdduvall {
10951369Sdduvall 	recv_ring_t *rrp;
10961369Sdduvall 
10971369Sdduvall 	BGE_TRACE(("bge_fini_recv_ring($%p, %d)",
10981369Sdduvall 		(void *)bgep, ring));
10991369Sdduvall 
11001369Sdduvall 	rrp = &bgep->recv[ring];
11011369Sdduvall 	if (rrp->rx_softint)
11021369Sdduvall 		ddi_remove_softintr(rrp->rx_softint);
11031369Sdduvall 	mutex_destroy(rrp->rx_lock);
11041369Sdduvall }
11051369Sdduvall 
11061369Sdduvall /*
11071369Sdduvall  * Initialise the specified Send Ring, using the information in the
11081369Sdduvall  * <dma_area> descriptors that it contains to set up all the other
11091369Sdduvall  * fields. This routine should be called only once for each ring.
11101369Sdduvall  */
11111369Sdduvall static void
11121369Sdduvall bge_init_send_ring(bge_t *bgep, uint64_t ring)
11131369Sdduvall {
11141369Sdduvall 	send_ring_t *srp;
11151369Sdduvall 	bge_status_t *bsp;
11161369Sdduvall 	sw_sbd_t *ssbdp;
11171369Sdduvall 	dma_area_t desc;
11181369Sdduvall 	dma_area_t pbuf;
11191369Sdduvall 	uint32_t nslots;
11201369Sdduvall 	uint32_t slot;
11211369Sdduvall 	uint32_t split;
11221369Sdduvall 
11231369Sdduvall 	BGE_TRACE(("bge_init_send_ring($%p, %d)",
11241369Sdduvall 		(void *)bgep, ring));
11251369Sdduvall 
11261369Sdduvall 	/*
11271369Sdduvall 	 * The chip architecture requires that host-based send rings
11281369Sdduvall 	 * have 512 elements per ring.  See 570X-PG102-R page 56.
11291369Sdduvall 	 */
11301369Sdduvall 	srp = &bgep->send[ring];
11311369Sdduvall 	nslots = srp->desc.nslots;
11321369Sdduvall 	ASSERT(nslots == 0 || nslots == 512);
11331369Sdduvall 
11341369Sdduvall 	/*
11351369Sdduvall 	 * Set up the copy of the h/w RCB
11361369Sdduvall 	 */
11371369Sdduvall 	srp->hw_rcb.host_ring_addr = srp->desc.cookie.dmac_laddress;
11381369Sdduvall 	srp->hw_rcb.max_len = nslots;
11391369Sdduvall 	srp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
11401369Sdduvall 	srp->hw_rcb.nic_ring_addr = NIC_MEM_SHADOW_SEND_RING(ring, nslots);
11411369Sdduvall 
11421369Sdduvall 	/*
11431369Sdduvall 	 * Other one-off initialisation of per-ring data
11441369Sdduvall 	 */
11451369Sdduvall 	srp->bgep = bgep;
11461369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
11471369Sdduvall 	srp->cons_index_p = SEND_INDEX_P(bsp, ring);
11481369Sdduvall 	srp->chip_mbx_reg = SEND_RING_HOST_INDEX_REG(ring);
11491369Sdduvall 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
11501369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
11511369Sdduvall 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
11521369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
11531369Sdduvall 
11541369Sdduvall 	/*
11551369Sdduvall 	 * Allocate the array of s/w Send Buffer Descriptors
11561369Sdduvall 	 */
11571369Sdduvall 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
11581369Sdduvall 	srp->sw_sbds = ssbdp;
11591369Sdduvall 
11601369Sdduvall 	/*
11611369Sdduvall 	 * Now initialise each array element once and for all
11621369Sdduvall 	 */
11631369Sdduvall 	desc = srp->desc;
11641369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
11651369Sdduvall 		pbuf = srp->buf[split];
11661369Sdduvall 		for (slot = 0; slot < nslots/BGE_SPLIT; ++ssbdp, ++slot) {
11671369Sdduvall 			bge_slice_chunk(&ssbdp->desc, &desc, 1,
11681369Sdduvall 				sizeof (bge_sbd_t));
11691369Sdduvall 			bge_slice_chunk(&ssbdp->pbuf, &pbuf, 1,
11701369Sdduvall 				bgep->chipid.snd_buff_size);
11711369Sdduvall 		}
11721369Sdduvall 		ASSERT(pbuf.alength == 0);
11731369Sdduvall 	}
11741369Sdduvall 	ASSERT(desc.alength == 0);
11751369Sdduvall }
11761369Sdduvall 
11771369Sdduvall /*
11781369Sdduvall  * Clean up initialisation done above before the memory is freed
11791369Sdduvall  */
11801369Sdduvall static void
11811369Sdduvall bge_fini_send_ring(bge_t *bgep, uint64_t ring)
11821369Sdduvall {
11831369Sdduvall 	send_ring_t *srp;
11841369Sdduvall 	sw_sbd_t *ssbdp;
11851369Sdduvall 
11861369Sdduvall 	BGE_TRACE(("bge_fini_send_ring($%p, %d)",
11871369Sdduvall 		(void *)bgep, ring));
11881369Sdduvall 
11891369Sdduvall 	srp = &bgep->send[ring];
11901369Sdduvall 	ssbdp = srp->sw_sbds;
11911369Sdduvall 	kmem_free(ssbdp, srp->desc.nslots*sizeof (*ssbdp));
11921369Sdduvall 
11931369Sdduvall 	mutex_destroy(srp->tx_lock);
11941369Sdduvall 	mutex_destroy(srp->tc_lock);
11951369Sdduvall }
11961369Sdduvall 
11971369Sdduvall /*
11981369Sdduvall  * Initialise all transmit, receive, and buffer rings.
11991369Sdduvall  * (also a few top-level mutexen that can't be done until
12001369Sdduvall  * the h/w interrupt handler has been registered 'cos we
12011369Sdduvall  * need the cookie).
12021369Sdduvall  */
12031369Sdduvall static void
12041369Sdduvall bge_init_rings(bge_t *bgep)
12051369Sdduvall {
12061369Sdduvall 	uint64_t ring;
12071369Sdduvall 
12081369Sdduvall 	BGE_TRACE(("bge_init_rings($%p)", (void *)bgep));
12091369Sdduvall 
12101369Sdduvall 	mutex_init(bgep->genlock, NULL, MUTEX_DRIVER,
12111369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
12121369Sdduvall 	mutex_init(bgep->softintrlock, NULL, MUTEX_DRIVER,
12131369Sdduvall 		DDI_INTR_PRI(bgep->intr_pri));
12141369Sdduvall 	rw_init(bgep->errlock, NULL, RW_DRIVER,
12151369Sdduvall 	    DDI_INTR_PRI(bgep->intr_pri));
12161369Sdduvall 
12171369Sdduvall 	/*
12181369Sdduvall 	 * Perform one-off initialisation of each ring ...
12191369Sdduvall 	 */
12201369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
12211369Sdduvall 		bge_init_send_ring(bgep, ring);
12221369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
12231369Sdduvall 		bge_init_recv_ring(bgep, ring);
12241369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
12251369Sdduvall 		bge_init_buff_ring(bgep, ring);
12261369Sdduvall }
12271369Sdduvall 
12281369Sdduvall /*
12291369Sdduvall  * Undo the work of bge_init_rings() above before the memory is freed
12301369Sdduvall  */
12311369Sdduvall static void
12321369Sdduvall bge_fini_rings(bge_t *bgep)
12331369Sdduvall {
12341369Sdduvall 	uint64_t ring;
12351369Sdduvall 
12361369Sdduvall 	BGE_TRACE(("bge_fini_rings($%p)", (void *)bgep));
12371369Sdduvall 
12381369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
12391369Sdduvall 		bge_fini_buff_ring(bgep, ring);
12401369Sdduvall 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
12411369Sdduvall 		bge_fini_recv_ring(bgep, ring);
12421369Sdduvall 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
12431369Sdduvall 		bge_fini_send_ring(bgep, ring);
12441369Sdduvall 
12451369Sdduvall 	rw_destroy(bgep->errlock);
12461369Sdduvall 	mutex_destroy(bgep->softintrlock);
12471369Sdduvall 	mutex_destroy(bgep->genlock);
12481369Sdduvall }
12491369Sdduvall 
12501369Sdduvall /*
12511369Sdduvall  * Allocate an area of memory and a DMA handle for accessing it
12521369Sdduvall  */
12531369Sdduvall static int
12541369Sdduvall bge_alloc_dma_mem(bge_t *bgep, size_t memsize, ddi_device_acc_attr_t *attr_p,
12551369Sdduvall 	uint_t dma_flags, dma_area_t *dma_p)
12561369Sdduvall {
12571369Sdduvall 	caddr_t va;
12581369Sdduvall 	int err;
12591369Sdduvall 
12601369Sdduvall 	BGE_TRACE(("bge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
12611369Sdduvall 		(void *)bgep, memsize, attr_p, dma_flags, dma_p));
12621369Sdduvall 
12631369Sdduvall 	/*
12641369Sdduvall 	 * Allocate handle
12651369Sdduvall 	 */
12661369Sdduvall 	err = ddi_dma_alloc_handle(bgep->devinfo, &dma_attr,
12671369Sdduvall 		DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
12681369Sdduvall 	if (err != DDI_SUCCESS)
12691369Sdduvall 		return (DDI_FAILURE);
12701369Sdduvall 
12711369Sdduvall 	/*
12721369Sdduvall 	 * Allocate memory
12731369Sdduvall 	 */
12741369Sdduvall 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
12751369Sdduvall 		dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
12761369Sdduvall 		DDI_DMA_SLEEP, NULL, &va, &dma_p->alength, &dma_p->acc_hdl);
12771369Sdduvall 	if (err != DDI_SUCCESS)
12781369Sdduvall 		return (DDI_FAILURE);
12791369Sdduvall 
12801369Sdduvall 	/*
12811369Sdduvall 	 * Bind the two together
12821369Sdduvall 	 */
12831369Sdduvall 	dma_p->mem_va = va;
12841369Sdduvall 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
12851369Sdduvall 		va, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL,
12861369Sdduvall 		&dma_p->cookie, &dma_p->ncookies);
12871369Sdduvall 
12881369Sdduvall 	BGE_DEBUG(("bge_alloc_dma_mem(): bind %d bytes; err %d, %d cookies",
12891369Sdduvall 		dma_p->alength, err, dma_p->ncookies));
12901369Sdduvall 
12911369Sdduvall 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
12921369Sdduvall 		return (DDI_FAILURE);
12931369Sdduvall 
12941369Sdduvall 	dma_p->nslots = ~0U;
12951369Sdduvall 	dma_p->size = ~0U;
12961369Sdduvall 	dma_p->token = ~0U;
12971369Sdduvall 	dma_p->offset = 0;
12981369Sdduvall 	return (DDI_SUCCESS);
12991369Sdduvall }
13001369Sdduvall 
13011369Sdduvall /*
13021369Sdduvall  * Free one allocated area of DMAable memory
13031369Sdduvall  */
13041369Sdduvall static void
13051369Sdduvall bge_free_dma_mem(dma_area_t *dma_p)
13061369Sdduvall {
13071369Sdduvall 	if (dma_p->dma_hdl != NULL) {
13081369Sdduvall 		if (dma_p->ncookies) {
13091369Sdduvall 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
13101369Sdduvall 			dma_p->ncookies = 0;
13111369Sdduvall 		}
13121369Sdduvall 		ddi_dma_free_handle(&dma_p->dma_hdl);
13131369Sdduvall 		dma_p->dma_hdl = NULL;
13141369Sdduvall 	}
13151369Sdduvall 
13161369Sdduvall 	if (dma_p->acc_hdl != NULL) {
13171369Sdduvall 		ddi_dma_mem_free(&dma_p->acc_hdl);
13181369Sdduvall 		dma_p->acc_hdl = NULL;
13191369Sdduvall 	}
13201369Sdduvall }
13211369Sdduvall 
13221369Sdduvall /*
13231369Sdduvall  * This function allocates all the transmit and receive buffers
13241369Sdduvall  * and descriptors, in four chunks (or one, if MONOLITHIC).
13251369Sdduvall  */
13261369Sdduvall static int
13271369Sdduvall bge_alloc_bufs(bge_t *bgep)
13281369Sdduvall {
13291369Sdduvall 	dma_area_t area;
13301369Sdduvall 	size_t rxbuffsize;
13311369Sdduvall 	size_t txbuffsize;
13321369Sdduvall 	size_t rxbuffdescsize;
13331369Sdduvall 	size_t rxdescsize;
13341369Sdduvall 	size_t txdescsize;
13351369Sdduvall 	uint64_t ring;
13361369Sdduvall 	uint64_t rx_rings = bgep->chipid.rx_rings;
13371369Sdduvall 	uint64_t tx_rings = bgep->chipid.tx_rings;
13381369Sdduvall 	int split;
13391369Sdduvall 	int err;
13401369Sdduvall 
13411369Sdduvall 	BGE_TRACE(("bge_alloc_bufs($%p)",
13421369Sdduvall 		(void *)bgep));
13431369Sdduvall 
13441369Sdduvall 	rxbuffsize = BGE_STD_SLOTS_USED*BGE_STD_BUFF_SIZE;
13451369Sdduvall 	rxbuffsize += bgep->chipid.jumbo_slots*bgep->chipid.recv_jumbo_size;
13461369Sdduvall 	rxbuffsize += BGE_MINI_SLOTS_USED*BGE_MINI_BUFF_SIZE;
13471369Sdduvall 
13481369Sdduvall 	txbuffsize = BGE_SEND_SLOTS_USED*bgep->chipid.snd_buff_size;
13491369Sdduvall 	txbuffsize *= tx_rings;
13501369Sdduvall 
13511369Sdduvall 	rxdescsize = rx_rings*bgep->chipid.recv_slots;
13521369Sdduvall 	rxdescsize *= sizeof (bge_rbd_t);
13531369Sdduvall 
13541369Sdduvall 	rxbuffdescsize = BGE_STD_SLOTS_USED;
13551369Sdduvall 	rxbuffdescsize += bgep->chipid.jumbo_slots;
13561369Sdduvall 	rxbuffdescsize += BGE_MINI_SLOTS_USED;
13571369Sdduvall 	rxbuffdescsize *= sizeof (bge_rbd_t);
13581369Sdduvall 
13591369Sdduvall 	txdescsize = tx_rings*BGE_SEND_SLOTS_USED;
13601369Sdduvall 	txdescsize *= sizeof (bge_sbd_t);
13611369Sdduvall 	txdescsize += sizeof (bge_statistics_t);
13621369Sdduvall 	txdescsize += sizeof (bge_status_t);
13631369Sdduvall 	txdescsize += BGE_STATUS_PADDING;
13641369Sdduvall 
13651369Sdduvall #if	BGE_MONOLITHIC
13661369Sdduvall 
13671369Sdduvall 	err = bge_alloc_dma_mem(bgep,
13681369Sdduvall 		rxbuffsize+txbuffsize+rxbuffdescsize+rxdescsize+txdescsize,
13691369Sdduvall 		&bge_data_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &area);
13701369Sdduvall 	if (err != DDI_SUCCESS)
13711369Sdduvall 		return (DDI_FAILURE);
13721369Sdduvall 
13731369Sdduvall 	BGE_DEBUG(("allocated range $%p-$%p (0x%lx-0x%lx)",
13741369Sdduvall 		DMA_VPTR(area),
13751369Sdduvall 		(caddr_t)DMA_VPTR(area)+area.alength,
13761369Sdduvall 		area.cookie.dmac_laddress,
13771369Sdduvall 		area.cookie.dmac_laddress+area.alength));
13781369Sdduvall 
13791369Sdduvall 	bge_slice_chunk(&bgep->rx_buff[0], &area, 1, rxbuffsize);
13801369Sdduvall 	bge_slice_chunk(&bgep->tx_buff[0], &area, 1, txbuffsize);
13811369Sdduvall 	bge_slice_chunk(&bgep->rx_desc[0], &area, 1, rxdescsize);
13821369Sdduvall 	bge_slice_chunk(&bgep->tx_desc, &area, 1, txdescsize);
13831369Sdduvall 
13841369Sdduvall #else
13851369Sdduvall 	/*
13861369Sdduvall 	 * Allocate memory & handles for RX buffers
13871369Sdduvall 	 */
13881369Sdduvall 	ASSERT((rxbuffsize % BGE_SPLIT) == 0);
13891369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
13901369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxbuffsize/BGE_SPLIT,
13911369Sdduvall 			&bge_data_accattr, DDI_DMA_READ | BGE_DMA_MODE,
13921369Sdduvall 			&bgep->rx_buff[split]);
13931369Sdduvall 		if (err != DDI_SUCCESS)
13941369Sdduvall 			return (DDI_FAILURE);
13951369Sdduvall 	}
13961369Sdduvall 
13971369Sdduvall 	/*
13981369Sdduvall 	 * Allocate memory & handles for TX buffers
13991369Sdduvall 	 */
14001369Sdduvall 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
14011369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
14021369Sdduvall 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
14031369Sdduvall 			&bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
14041369Sdduvall 			&bgep->tx_buff[split]);
14051369Sdduvall 		if (err != DDI_SUCCESS)
14061369Sdduvall 			return (DDI_FAILURE);
14071369Sdduvall 	}
14081369Sdduvall 
14091369Sdduvall 	/*
14101369Sdduvall 	 * Allocate memory & handles for receive return rings
14111369Sdduvall 	 */
14121369Sdduvall 	ASSERT((rxdescsize % rx_rings) == 0);
14131369Sdduvall 	for (split = 0; split < rx_rings; ++split) {
14141369Sdduvall 		err = bge_alloc_dma_mem(bgep, rxdescsize/rx_rings,
14151369Sdduvall 			&bge_desc_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
14161369Sdduvall 			&bgep->rx_desc[split]);
14171369Sdduvall 		if (err != DDI_SUCCESS)
14181369Sdduvall 			return (DDI_FAILURE);
14191369Sdduvall 	}
14201369Sdduvall 
14211369Sdduvall 	/*
14221369Sdduvall 	 * Allocate memory & handles for buffer (producer) descriptor rings
14231369Sdduvall 	 */
14241369Sdduvall 	err = bge_alloc_dma_mem(bgep, rxbuffdescsize, &bge_desc_accattr,
14251369Sdduvall 		DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->rx_desc[split]);
14261369Sdduvall 	if (err != DDI_SUCCESS)
14271369Sdduvall 		return (DDI_FAILURE);
14281369Sdduvall 
14291369Sdduvall 	/*
14301369Sdduvall 	 * Allocate memory & handles for TX descriptor rings,
14311369Sdduvall 	 * status block, and statistics area
14321369Sdduvall 	 */
14331369Sdduvall 	err = bge_alloc_dma_mem(bgep, txdescsize, &bge_desc_accattr,
14341369Sdduvall 		DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->tx_desc);
14351369Sdduvall 	if (err != DDI_SUCCESS)
14361369Sdduvall 		return (DDI_FAILURE);
14371369Sdduvall 
14381369Sdduvall #endif	/* BGE_MONOLITHIC */
14391369Sdduvall 
14401369Sdduvall 	/*
14411369Sdduvall 	 * Now carve up each of the allocated areas ...
14421369Sdduvall 	 */
14431369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
14441369Sdduvall 		area = bgep->rx_buff[split];
14451369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].buf[split],
14461369Sdduvall 			&area, BGE_STD_SLOTS_USED/BGE_SPLIT,
14471369Sdduvall 			BGE_STD_BUFF_SIZE);
14481369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].buf[split],
14491369Sdduvall 			&area, bgep->chipid.jumbo_slots/BGE_SPLIT,
14501369Sdduvall 			bgep->chipid.recv_jumbo_size);
14511369Sdduvall 		bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].buf[split],
14521369Sdduvall 			&area, BGE_MINI_SLOTS_USED/BGE_SPLIT,
14531369Sdduvall 			BGE_MINI_BUFF_SIZE);
14541369Sdduvall 		ASSERT(area.alength >= 0);
14551369Sdduvall 	}
14561369Sdduvall 
14571369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split) {
14581369Sdduvall 		area = bgep->tx_buff[split];
14591369Sdduvall 		for (ring = 0; ring < tx_rings; ++ring)
14601369Sdduvall 			bge_slice_chunk(&bgep->send[ring].buf[split],
14611369Sdduvall 				&area, BGE_SEND_SLOTS_USED/BGE_SPLIT,
14621369Sdduvall 				bgep->chipid.snd_buff_size);
14631369Sdduvall 		for (; ring < BGE_SEND_RINGS_MAX; ++ring)
14641369Sdduvall 			bge_slice_chunk(&bgep->send[ring].buf[split],
14651369Sdduvall 				&area, 0/BGE_SPLIT,
14661369Sdduvall 				bgep->chipid.snd_buff_size);
14671369Sdduvall 		ASSERT(area.alength >= 0);
14681369Sdduvall 	}
14691369Sdduvall 
14701369Sdduvall 	for (ring = 0; ring < rx_rings; ++ring)
14711369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &bgep->rx_desc[ring],
14721369Sdduvall 			bgep->chipid.recv_slots, sizeof (bge_rbd_t));
14731369Sdduvall 
14741369Sdduvall 	area = bgep->rx_desc[rx_rings];
14751369Sdduvall 	for (; ring < BGE_RECV_RINGS_MAX; ++ring)
14761369Sdduvall 		bge_slice_chunk(&bgep->recv[ring].desc, &area,
14771369Sdduvall 			0, sizeof (bge_rbd_t));
14781369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].desc, &area,
14791369Sdduvall 		BGE_STD_SLOTS_USED, sizeof (bge_rbd_t));
14801369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].desc, &area,
14811369Sdduvall 		bgep->chipid.jumbo_slots, sizeof (bge_rbd_t));
14821369Sdduvall 	bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].desc, &area,
14831369Sdduvall 		BGE_MINI_SLOTS_USED, sizeof (bge_rbd_t));
14841369Sdduvall 	ASSERT(area.alength == 0);
14851369Sdduvall 
14861369Sdduvall 	area = bgep->tx_desc;
14871369Sdduvall 	for (ring = 0; ring < tx_rings; ++ring)
14881369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
14891369Sdduvall 			BGE_SEND_SLOTS_USED, sizeof (bge_sbd_t));
14901369Sdduvall 	for (; ring < BGE_SEND_RINGS_MAX; ++ring)
14911369Sdduvall 		bge_slice_chunk(&bgep->send[ring].desc, &area,
14921369Sdduvall 			0, sizeof (bge_sbd_t));
14931369Sdduvall 	bge_slice_chunk(&bgep->statistics, &area, 1, sizeof (bge_statistics_t));
14941369Sdduvall 	bge_slice_chunk(&bgep->status_block, &area, 1, sizeof (bge_status_t));
14951369Sdduvall 	ASSERT(area.alength == BGE_STATUS_PADDING);
14961369Sdduvall 	DMA_ZERO(bgep->status_block);
14971369Sdduvall 
14981369Sdduvall 	return (DDI_SUCCESS);
14991369Sdduvall }
15001369Sdduvall 
15011369Sdduvall /*
15021369Sdduvall  * This routine frees the transmit and receive buffers and descriptors.
15031369Sdduvall  * Make sure the chip is stopped before calling it!
15041369Sdduvall  */
15051369Sdduvall static void
15061369Sdduvall bge_free_bufs(bge_t *bgep)
15071369Sdduvall {
15081369Sdduvall 	int split;
15091369Sdduvall 
15101369Sdduvall 	BGE_TRACE(("bge_free_bufs($%p)",
15111369Sdduvall 		(void *)bgep));
15121369Sdduvall 
15131369Sdduvall #if	BGE_MONOLITHIC
15141369Sdduvall 	bge_free_dma_mem(&bgep->rx_buff[0]);
15151369Sdduvall #else
15161369Sdduvall 	bge_free_dma_mem(&bgep->tx_desc);
15171369Sdduvall 	for (split = 0; split < BGE_RECV_RINGS_SPLIT; ++split)
15181369Sdduvall 		bge_free_dma_mem(&bgep->rx_desc[split]);
15191369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
15201369Sdduvall 		bge_free_dma_mem(&bgep->tx_buff[split]);
15211369Sdduvall 	for (split = 0; split < BGE_SPLIT; ++split)
15221369Sdduvall 		bge_free_dma_mem(&bgep->rx_buff[split]);
15231369Sdduvall #endif	/* BGE_MONOLITHIC */
15241369Sdduvall }
15251369Sdduvall 
15261369Sdduvall /*
15271369Sdduvall  * Determine (initial) MAC address ("BIA") to use for this interface
15281369Sdduvall  */
15291369Sdduvall 
15301369Sdduvall static void
15311369Sdduvall bge_find_mac_address(bge_t *bgep, chip_id_t *cidp)
15321369Sdduvall {
15331369Sdduvall 	struct ether_addr sysaddr;
15341369Sdduvall 	char propbuf[8];		/* "true" or "false", plus NUL	*/
15351369Sdduvall 	uchar_t *bytes;
15361369Sdduvall 	int *ints;
15371369Sdduvall 	uint_t nelts;
15381369Sdduvall 	int err;
15391369Sdduvall 
15401369Sdduvall 	BGE_TRACE(("bge_find_mac_address($%p)",
15411369Sdduvall 		(void *)bgep));
15421369Sdduvall 
15431369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: hw_mac_addr %012llx, => %s (%sset)",
15441369Sdduvall 		cidp->hw_mac_addr,
15451369Sdduvall 		ether_sprintf((void *)cidp->vendor_addr.addr),
15461369Sdduvall 		cidp->vendor_addr.set ? "" : "not "));
15471369Sdduvall 
15481369Sdduvall 	/*
15491369Sdduvall 	 * The "vendor's factory-set address" may already have
15501369Sdduvall 	 * been extracted from the chip, but if the property
15511369Sdduvall 	 * "local-mac-address" is set we use that instead.  It
15521369Sdduvall 	 * will normally be set by OBP, but it could also be
15531369Sdduvall 	 * specified in a .conf file(!)
15541369Sdduvall 	 *
15551369Sdduvall 	 * There doesn't seem to be a way to define byte-array
15561369Sdduvall 	 * properties in a .conf, so we check whether it looks
15571369Sdduvall 	 * like an array of 6 ints instead.
15581369Sdduvall 	 *
15591369Sdduvall 	 * Then, we check whether it looks like an array of 6
15601369Sdduvall 	 * bytes (which it should, if OBP set it).  If we can't
15611369Sdduvall 	 * make sense of it either way, we'll ignore it.
15621369Sdduvall 	 */
15631369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
15641369Sdduvall 		DDI_PROP_DONTPASS, localmac_propname, &ints, &nelts);
15651369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
15661369Sdduvall 		if (nelts == ETHERADDRL) {
15671369Sdduvall 			while (nelts--)
15681369Sdduvall 				cidp->vendor_addr.addr[nelts] = ints[nelts];
15691369Sdduvall 			cidp->vendor_addr.set = 1;
15701369Sdduvall 		}
15711369Sdduvall 		ddi_prop_free(ints);
15721369Sdduvall 	}
15731369Sdduvall 
15741369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
15751369Sdduvall 		DDI_PROP_DONTPASS, localmac_propname, &bytes, &nelts);
15761369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
15771369Sdduvall 		if (nelts == ETHERADDRL) {
15781369Sdduvall 			while (nelts--)
15791369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
15801369Sdduvall 			cidp->vendor_addr.set = 1;
15811369Sdduvall 		}
15821369Sdduvall 		ddi_prop_free(bytes);
15831369Sdduvall 	}
15841369Sdduvall 
15851369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +local %s (%sset)",
15861369Sdduvall 		ether_sprintf((void *)cidp->vendor_addr.addr),
15871369Sdduvall 		cidp->vendor_addr.set ? "" : "not "));
15881369Sdduvall 
15891369Sdduvall 	/*
15901369Sdduvall 	 * Look up the OBP property "local-mac-address?".  Note that even
15911369Sdduvall 	 * though its value is a string (which should be "true" or "false"),
15921369Sdduvall 	 * it can't be decoded by ddi_prop_lookup_string(9F).  So, we zero
15931369Sdduvall 	 * the buffer first and then fetch the property as an untyped array;
15941369Sdduvall 	 * this may or may not include a final NUL, but since there will
15951369Sdduvall 	 * always be one left at the end of the buffer we can now treat it
15961369Sdduvall 	 * as a string anyway.
15971369Sdduvall 	 */
15981369Sdduvall 	nelts = sizeof (propbuf);
15991369Sdduvall 	bzero(propbuf, nelts--);
16001369Sdduvall 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, bgep->devinfo,
16011369Sdduvall 		DDI_PROP_CANSLEEP, localmac_boolname, propbuf, (int *)&nelts);
16021369Sdduvall 
16031369Sdduvall 	/*
16041369Sdduvall 	 * Now, if the address still isn't set from the hardware (SEEPROM)
16051369Sdduvall 	 * or the OBP or .conf property, OR if the user has foolishly set
16061369Sdduvall 	 * 'local-mac-address? = false', use "the system address" instead
16071369Sdduvall 	 * (but only if it's non-null i.e. has been set from the IDPROM).
16081369Sdduvall 	 */
16091369Sdduvall 	if (cidp->vendor_addr.set == 0 || strcmp(propbuf, "false") == 0)
16101369Sdduvall 		if (localetheraddr(NULL, &sysaddr) != 0) {
16111369Sdduvall 			ethaddr_copy(&sysaddr, cidp->vendor_addr.addr);
16121369Sdduvall 			cidp->vendor_addr.set = 1;
16131369Sdduvall 		}
16141369Sdduvall 
16151369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: +system %s (%sset)",
16161369Sdduvall 		ether_sprintf((void *)cidp->vendor_addr.addr),
16171369Sdduvall 		cidp->vendor_addr.set ? "" : "not "));
16181369Sdduvall 
16191369Sdduvall 	/*
16201369Sdduvall 	 * Finally(!), if there's a valid "mac-address" property (created
16211369Sdduvall 	 * if we netbooted from this interface), we must use this instead
16221369Sdduvall 	 * of any of the above to ensure that the NFS/install server doesn't
16231369Sdduvall 	 * get confused by the address changing as Solaris takes over!
16241369Sdduvall 	 */
16251369Sdduvall 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
16261369Sdduvall 		DDI_PROP_DONTPASS, macaddr_propname, &bytes, &nelts);
16271369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
16281369Sdduvall 		if (nelts == ETHERADDRL) {
16291369Sdduvall 			while (nelts--)
16301369Sdduvall 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
16311369Sdduvall 			cidp->vendor_addr.set = 1;
16321369Sdduvall 		}
16331369Sdduvall 		ddi_prop_free(bytes);
16341369Sdduvall 	}
16351369Sdduvall 
16361369Sdduvall 	BGE_DEBUG(("bge_find_mac_address: =final %s (%sset)",
16371369Sdduvall 		ether_sprintf((void *)cidp->vendor_addr.addr),
16381369Sdduvall 		cidp->vendor_addr.set ? "" : "not "));
16391369Sdduvall }
16401369Sdduvall 
16411369Sdduvall static void
16421408Srandyf #ifdef BGE_IPMI_ASF
16431408Srandyf bge_unattach(bge_t *bgep, uint_t asf_mode)
16441408Srandyf #else
16451369Sdduvall bge_unattach(bge_t *bgep)
16461408Srandyf #endif
16471369Sdduvall {
16481369Sdduvall 	mac_t	*macp;
16491369Sdduvall 
16501369Sdduvall 	BGE_TRACE(("bge_unattach($%p)",
16511369Sdduvall 		(void *)bgep));
16521369Sdduvall 
16531369Sdduvall 	/*
16541369Sdduvall 	 * Flag that no more activity may be initiated
16551369Sdduvall 	 */
16561369Sdduvall 	bgep->progress &= ~PROGRESS_READY;
16571369Sdduvall 
16581369Sdduvall 	/*
16591369Sdduvall 	 * Quiesce the PHY and MAC (leave it reset but still powered).
16601369Sdduvall 	 * Clean up and free all BGE data structures
16611369Sdduvall 	 */
16621369Sdduvall 	if (bgep->cyclic_id) {
16631369Sdduvall 		mutex_enter(&cpu_lock);
16641369Sdduvall 		cyclic_remove(bgep->cyclic_id);
16651369Sdduvall 		mutex_exit(&cpu_lock);
16661369Sdduvall 	}
16671369Sdduvall 	if (bgep->progress & PROGRESS_KSTATS)
16681369Sdduvall 		bge_fini_kstats(bgep);
16691369Sdduvall 	if (bgep->progress & PROGRESS_NDD)
16701369Sdduvall 		bge_nd_cleanup(bgep);
16711369Sdduvall 	if (bgep->progress & PROGRESS_PHY)
16721369Sdduvall 		bge_phys_reset(bgep);
16731369Sdduvall 	if (bgep->progress & PROGRESS_HWINT) {
16741369Sdduvall 		mutex_enter(bgep->genlock);
16751408Srandyf #ifdef BGE_IPMI_ASF
16761408Srandyf 		bge_chip_reset(bgep, B_FALSE, asf_mode);
16771408Srandyf 		if (bgep->asf_enabled) {
16781408Srandyf 			/*
16791408Srandyf 			 * This register has been overlaid. We restore its
16801408Srandyf 			 * initial value here.
16811408Srandyf 			 */
16821408Srandyf 			bge_nic_put32(bgep, BGE_NIC_DATA_SIG_ADDR,
16831408Srandyf 			    BGE_NIC_DATA_SIG);
16841408Srandyf 		}
16851408Srandyf #else
16861369Sdduvall 		bge_chip_reset(bgep, B_FALSE);
16871408Srandyf #endif
16881369Sdduvall 		mutex_exit(bgep->genlock);
16891369Sdduvall 	}
16901369Sdduvall 
16911369Sdduvall 	if (bgep->progress & PROGRESS_INTR) {
16921369Sdduvall 		bge_rem_intrs(bgep);
16931369Sdduvall 		bge_fini_rings(bgep);
16941369Sdduvall 	}
16951369Sdduvall 
16961369Sdduvall 	if (bgep->progress & PROGRESS_FACTOTUM)
16971369Sdduvall 		ddi_remove_softintr(bgep->factotum_id);
16981369Sdduvall 	if (bgep->progress & PROGRESS_RESCHED)
16991369Sdduvall 		ddi_remove_softintr(bgep->resched_id);
17001369Sdduvall 	bge_free_bufs(bgep);
17011369Sdduvall 	if (bgep->progress & PROGRESS_REGS)
17021369Sdduvall 		ddi_regs_map_free(&bgep->io_handle);
17031369Sdduvall 	if (bgep->progress & PROGRESS_CFG)
17041369Sdduvall 		pci_config_teardown(&bgep->cfg_handle);
17051369Sdduvall 
17061369Sdduvall 	ddi_remove_minor_node(bgep->devinfo, NULL);
17071369Sdduvall 	macp = bgep->macp;
17081369Sdduvall 	kmem_free(macp, sizeof (*macp));
17091369Sdduvall 	kmem_free(bgep, sizeof (*bgep));
17101369Sdduvall }
17111369Sdduvall 
17121369Sdduvall static int
17131369Sdduvall bge_resume(dev_info_t *devinfo)
17141369Sdduvall {
17151369Sdduvall 	bge_t *bgep;				/* Our private data	*/
17161369Sdduvall 	chip_id_t *cidp;
17171369Sdduvall 	chip_id_t chipid;
17181369Sdduvall 
17191369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
17201369Sdduvall 	if (bgep == NULL)
17211369Sdduvall 		return (DDI_FAILURE);
17221369Sdduvall 
17231369Sdduvall 	/*
17241369Sdduvall 	 * Refuse to resume if the data structures aren't consistent
17251369Sdduvall 	 */
17261369Sdduvall 	if (bgep->devinfo != devinfo)
17271369Sdduvall 		return (DDI_FAILURE);
17281369Sdduvall 
17291408Srandyf #ifdef BGE_IPMI_ASF
17301408Srandyf 	/*
17311408Srandyf 	 * Power management hasn't been supported in BGE now. If you
17321408Srandyf 	 * want to implement it, please add the ASF/IPMI related
17331408Srandyf 	 * code here.
17341408Srandyf 	 */
17351408Srandyf 
17361408Srandyf #endif
17371408Srandyf 
17381369Sdduvall 	/*
17391369Sdduvall 	 * Read chip ID & set up config space command register(s)
17401369Sdduvall 	 * Refuse to resume if the chip has changed its identity!
17411369Sdduvall 	 */
17421369Sdduvall 	cidp = &bgep->chipid;
17431369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, B_FALSE);
17441369Sdduvall 	if (chipid.vendor != cidp->vendor)
17451369Sdduvall 		return (DDI_FAILURE);
17461369Sdduvall 	if (chipid.device != cidp->device)
17471369Sdduvall 		return (DDI_FAILURE);
17481369Sdduvall 	if (chipid.revision != cidp->revision)
17491369Sdduvall 		return (DDI_FAILURE);
17501369Sdduvall 	if (chipid.asic_rev != cidp->asic_rev)
17511369Sdduvall 		return (DDI_FAILURE);
17521369Sdduvall 
17531369Sdduvall 	/*
17541369Sdduvall 	 * All OK, reinitialise h/w & kick off GLD scheduling
17551369Sdduvall 	 */
17561369Sdduvall 	mutex_enter(bgep->genlock);
17571369Sdduvall 	bge_restart(bgep, B_TRUE);
17581369Sdduvall 	mutex_exit(bgep->genlock);
17591369Sdduvall 	return (DDI_SUCCESS);
17601369Sdduvall }
17611369Sdduvall 
17621369Sdduvall static uint8_t ether_brdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
17631369Sdduvall 
17641369Sdduvall /*
17651369Sdduvall  * attach(9E) -- Attach a device to the system
17661369Sdduvall  *
17671369Sdduvall  * Called once for each board successfully probed.
17681369Sdduvall  */
17691369Sdduvall static int
17701369Sdduvall bge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
17711369Sdduvall {
17721369Sdduvall 	bge_t *bgep;				/* Our private data	*/
17731369Sdduvall 	mac_t *macp;
17741369Sdduvall 	chip_id_t *cidp;
17751369Sdduvall 	cyc_handler_t cychand;
17761369Sdduvall 	cyc_time_t cyctime;
17771369Sdduvall 	caddr_t regs;
17781369Sdduvall 	int instance;
17791369Sdduvall 	int err;
17801369Sdduvall 	mac_info_t *mip;
17811369Sdduvall 	int intr_types;
17821369Sdduvall 	int i;
17831408Srandyf #ifdef BGE_IPMI_ASF
17841408Srandyf 	uint32_t mhcrValue;
17851408Srandyf #endif
17861369Sdduvall 
17871369Sdduvall 	instance = ddi_get_instance(devinfo);
17881369Sdduvall 
17891369Sdduvall 	BGE_GTRACE(("bge_attach($%p, %d) instance %d",
17901369Sdduvall 		(void *)devinfo, cmd, instance));
17911369Sdduvall 	BGE_BRKPT(NULL, "bge_attach");
17921369Sdduvall 
17931369Sdduvall 	switch (cmd) {
17941369Sdduvall 	default:
17951369Sdduvall 		return (DDI_FAILURE);
17961369Sdduvall 
17971369Sdduvall 	case DDI_RESUME:
17981369Sdduvall 		return (bge_resume(devinfo));
17991369Sdduvall 
18001369Sdduvall 	case DDI_ATTACH:
18011369Sdduvall 		break;
18021369Sdduvall 	}
18031369Sdduvall 
18041369Sdduvall 	/*
18051369Sdduvall 	 * Allocate mac_t and BGE private structures, and
18061369Sdduvall 	 * cross-link them so that given either one of these or
18071369Sdduvall 	 * the devinfo the others can be derived.
18081369Sdduvall 	 */
18091369Sdduvall 	macp = kmem_zalloc(sizeof (*macp), KM_SLEEP);
18101369Sdduvall 	bgep = kmem_zalloc(sizeof (*bgep), KM_SLEEP);
18111369Sdduvall 	ddi_set_driver_private(devinfo, bgep);
18121369Sdduvall 	bgep->bge_guard = BGE_GUARD;
18131369Sdduvall 	bgep->devinfo = devinfo;
18141369Sdduvall 	bgep->macp = macp;
18151369Sdduvall 	macp->m_driver = bgep;
18161369Sdduvall 
18171369Sdduvall 	/*
18181369Sdduvall 	 * Initialize more fields in BGE private data
18191369Sdduvall 	 */
18201369Sdduvall 	bgep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18211369Sdduvall 		DDI_PROP_DONTPASS, debug_propname, bge_debug);
18221369Sdduvall 	(void) snprintf(bgep->ifname, sizeof (bgep->ifname), "%s%d",
18231369Sdduvall 		BGE_DRIVER_NAME, instance);
18241369Sdduvall 
18251369Sdduvall 	/*
18261369Sdduvall 	 * Look up the IOMMU's page size for DVMA mappings (must be
18271369Sdduvall 	 * a power of 2) and convert to a mask.  This can be used to
18281369Sdduvall 	 * determine whether a message buffer crosses a page boundary.
18291369Sdduvall 	 * Note: in 2s complement binary notation, if X is a power of
18301369Sdduvall 	 * 2, then -X has the representation "11...1100...00".
18311369Sdduvall 	 */
18321369Sdduvall 	bgep->pagemask = dvma_pagesize(devinfo);
18331369Sdduvall 	ASSERT(ddi_ffs(bgep->pagemask) == ddi_fls(bgep->pagemask));
18341369Sdduvall 	bgep->pagemask = -bgep->pagemask;
18351369Sdduvall 
18361369Sdduvall 	/*
18371369Sdduvall 	 * Map config space registers
18381369Sdduvall 	 * Read chip ID & set up config space command register(s)
18391369Sdduvall 	 *
18401369Sdduvall 	 * Note: this leaves the chip accessible by Memory Space
18411369Sdduvall 	 * accesses, but with interrupts and Bus Mastering off.
18421369Sdduvall 	 * This should ensure that nothing untoward will happen
18431369Sdduvall 	 * if it has been left active by the (net-)bootloader.
18441369Sdduvall 	 * We'll re-enable Bus Mastering once we've reset the chip,
18451369Sdduvall 	 * and allow interrupts only when everything else is set up.
18461369Sdduvall 	 */
18471369Sdduvall 	err = pci_config_setup(devinfo, &bgep->cfg_handle);
18481408Srandyf #ifdef BGE_IPMI_ASF
18491408Srandyf 	mhcrValue = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MHCR);
18501408Srandyf 	if (mhcrValue & MHCR_ENABLE_ENDIAN_WORD_SWAP) {
18511408Srandyf 		bgep->asf_wordswapped = B_TRUE;
18521408Srandyf 	} else {
18531408Srandyf 		bgep->asf_wordswapped = B_FALSE;
18541408Srandyf 	}
18551408Srandyf 	bge_asf_get_config(bgep);
18561408Srandyf #endif
18571369Sdduvall 	if (err != DDI_SUCCESS) {
18581369Sdduvall 		bge_problem(bgep, "pci_config_setup() failed");
18591369Sdduvall 		goto attach_fail;
18601369Sdduvall 	}
18611369Sdduvall 	bgep->progress |= PROGRESS_CFG;
18621369Sdduvall 	cidp = &bgep->chipid;
18631369Sdduvall 	bzero(cidp, sizeof (*cidp));
18641369Sdduvall 	bge_chip_cfg_init(bgep, cidp, B_FALSE);
18651369Sdduvall 
18661408Srandyf #ifdef BGE_IPMI_ASF
18671408Srandyf 	if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
18681408Srandyf 	    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
18691408Srandyf 		bgep->asf_newhandshake = B_TRUE;
18701408Srandyf 	} else {
18711408Srandyf 		bgep->asf_newhandshake = B_FALSE;
18721408Srandyf 	}
18731408Srandyf #endif
18741408Srandyf 
18751369Sdduvall 	/*
18761369Sdduvall 	 * Update those parts of the chip ID derived from volatile
18771369Sdduvall 	 * registers with the values seen by OBP (in case the chip
18781369Sdduvall 	 * has been reset externally and therefore lost them).
18791369Sdduvall 	 */
18801369Sdduvall 	cidp->subven = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18811369Sdduvall 		DDI_PROP_DONTPASS, subven_propname, cidp->subven);
18821369Sdduvall 	cidp->subdev = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18831369Sdduvall 		DDI_PROP_DONTPASS, subdev_propname, cidp->subdev);
18841369Sdduvall 	cidp->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18851369Sdduvall 		DDI_PROP_DONTPASS, clsize_propname, cidp->clsize);
18861369Sdduvall 	cidp->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18871369Sdduvall 		DDI_PROP_DONTPASS, latency_propname, cidp->latency);
18881369Sdduvall 	cidp->rx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18891369Sdduvall 		DDI_PROP_DONTPASS, rxrings_propname, cidp->rx_rings);
18901369Sdduvall 	cidp->tx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18911369Sdduvall 		DDI_PROP_DONTPASS, txrings_propname, cidp->tx_rings);
18921369Sdduvall 
18931369Sdduvall 	if (bge_jumbo_enable == B_TRUE) {
18941369Sdduvall 		cidp->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
18951369Sdduvall 			DDI_PROP_DONTPASS, default_mtu, BGE_DEFAULT_MTU);
18961369Sdduvall 		if ((cidp->default_mtu < BGE_DEFAULT_MTU)||
18971369Sdduvall 			(cidp->default_mtu > BGE_MAXIMUM_MTU)) {
18981369Sdduvall 			cidp->default_mtu = BGE_DEFAULT_MTU;
18991369Sdduvall 		}
19001369Sdduvall 	}
19011369Sdduvall 	/*
19021369Sdduvall 	 * Map operating registers
19031369Sdduvall 	 */
19041369Sdduvall 	err = ddi_regs_map_setup(devinfo, BGE_PCI_OPREGS_RNUMBER,
19051369Sdduvall 		&regs, 0, 0, &bge_reg_accattr, &bgep->io_handle);
19061369Sdduvall 	if (err != DDI_SUCCESS) {
19071369Sdduvall 		bge_problem(bgep, "ddi_regs_map_setup() failed");
19081369Sdduvall 		goto attach_fail;
19091369Sdduvall 	}
19101369Sdduvall 	bgep->io_regs = regs;
19111369Sdduvall 	bgep->progress |= PROGRESS_REGS;
19121369Sdduvall 
19131369Sdduvall 	/*
19141369Sdduvall 	 * Characterise the device, so we know its requirements.
19151369Sdduvall 	 * Then allocate the appropriate TX and RX descriptors & buffers.
19161369Sdduvall 	 */
19171369Sdduvall 	bge_chip_id_init(bgep);
19181369Sdduvall 	err = bge_alloc_bufs(bgep);
19191369Sdduvall 	if (err != DDI_SUCCESS) {
19201369Sdduvall 		bge_problem(bgep, "DMA buffer allocation failed");
19211369Sdduvall 		goto attach_fail;
19221369Sdduvall 	}
19231369Sdduvall 
19241369Sdduvall 	/*
19251369Sdduvall 	 * Add the softint handlers:
19261369Sdduvall 	 *
19271369Sdduvall 	 * Both of these handlers are used to avoid restrictions on the
19281369Sdduvall 	 * context and/or mutexes required for some operations.  In
19291369Sdduvall 	 * particular, the hardware interrupt handler and its subfunctions
19301369Sdduvall 	 * can detect a number of conditions that we don't want to handle
19311369Sdduvall 	 * in that context or with that set of mutexes held.  So, these
19321369Sdduvall 	 * softints are triggered instead:
19331369Sdduvall 	 *
19341369Sdduvall 	 * the <resched> softint is triggered if if we have previously
19351369Sdduvall 	 * had to refuse to send a packet because of resource shortage
19361369Sdduvall 	 * (we've run out of transmit buffers), but the send completion
19371369Sdduvall 	 * interrupt handler has now detected that more buffers have
19381369Sdduvall 	 * become available.
19391369Sdduvall 	 *
19401369Sdduvall 	 * the <factotum> is triggered if the h/w interrupt handler
19411369Sdduvall 	 * sees the <link state changed> or <error> bits in the status
19421369Sdduvall 	 * block.  It's also triggered periodically to poll the link
19431369Sdduvall 	 * state, just in case we aren't getting link status change
19441369Sdduvall 	 * interrupts ...
19451369Sdduvall 	 */
19461369Sdduvall 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->resched_id,
19471369Sdduvall 		NULL, NULL, bge_reschedule, (caddr_t)bgep);
19481369Sdduvall 	if (err != DDI_SUCCESS) {
19491369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
19501369Sdduvall 		goto attach_fail;
19511369Sdduvall 	}
19521369Sdduvall 	bgep->progress |= PROGRESS_RESCHED;
19531369Sdduvall 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->factotum_id,
19541369Sdduvall 		NULL, NULL, bge_chip_factotum, (caddr_t)bgep);
19551369Sdduvall 	if (err != DDI_SUCCESS) {
19561369Sdduvall 		bge_problem(bgep, "ddi_add_softintr() failed");
19571369Sdduvall 		goto attach_fail;
19581369Sdduvall 	}
19591369Sdduvall 	bgep->progress |= PROGRESS_FACTOTUM;
19601369Sdduvall 
19611369Sdduvall 	/* Get supported interrupt types */
19621369Sdduvall 	if (ddi_intr_get_supported_types(devinfo, &intr_types) != DDI_SUCCESS) {
19631369Sdduvall 		bge_error(bgep, "ddi_intr_get_supported_types failed\n");
19641369Sdduvall 
19651369Sdduvall 		goto attach_fail;
19661369Sdduvall 	}
19671369Sdduvall 
19681369Sdduvall 	bge_log(bgep, "ddi_intr_get_supported_types() returned: %x",
19691369Sdduvall 	    intr_types);
19701369Sdduvall 
19711369Sdduvall 	if ((intr_types & DDI_INTR_TYPE_MSI) && bgep->chipid.msi_enabled) {
19721369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
19731369Sdduvall 			bge_error(bgep, "MSI registration failed, "
19741369Sdduvall 			    "trying FIXED interrupt type\n");
19751369Sdduvall 		} else {
19761369Sdduvall 			bge_log(bgep, "Using MSI interrupt type\n");
19771369Sdduvall 
19781369Sdduvall 			bgep->intr_type = DDI_INTR_TYPE_MSI;
19791369Sdduvall 			bgep->progress |= PROGRESS_INTR;
19801369Sdduvall 		}
19811369Sdduvall 	}
19821369Sdduvall 
19831369Sdduvall 	if (!(bgep->progress & PROGRESS_INTR) &&
19841369Sdduvall 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
19851369Sdduvall 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
19861369Sdduvall 			bge_error(bgep, "FIXED interrupt "
19871369Sdduvall 			    "registration failed\n");
19881369Sdduvall 			goto attach_fail;
19891369Sdduvall 		}
19901369Sdduvall 
19911369Sdduvall 		bge_log(bgep, "Using FIXED interrupt type\n");
19921369Sdduvall 
19931369Sdduvall 		bgep->intr_type = DDI_INTR_TYPE_FIXED;
19941369Sdduvall 		bgep->progress |= PROGRESS_INTR;
19951369Sdduvall 	}
19961369Sdduvall 
19971369Sdduvall 	if (!(bgep->progress & PROGRESS_INTR)) {
19981369Sdduvall 		bge_error(bgep, "No interrupts registered\n");
19991369Sdduvall 		goto attach_fail;
20001369Sdduvall 	}
20011369Sdduvall 
20021369Sdduvall 	/*
20031369Sdduvall 	 * Note that interrupts are not enabled yet as
20041369Sdduvall 	 * mutex locks are not initialized.
20051369Sdduvall 	 * Initialize rings and mutex locks.
20061369Sdduvall 	 */
20071369Sdduvall 	bge_init_rings(bgep);
20081369Sdduvall 	bgep->progress |= PROGRESS_HWINT;
20091369Sdduvall 
20101369Sdduvall 	/*
20111369Sdduvall 	 * Now that mutex locks are initialized, enable interrupts.
20121369Sdduvall 	 */
20131369Sdduvall 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
20141369Sdduvall 		/* Call ddi_intr_block_enable() for MSI interrupts */
20151369Sdduvall 		(void) ddi_intr_block_enable(bgep->htable, bgep->intr_cnt);
20161369Sdduvall 	} else {
20171369Sdduvall 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
20181369Sdduvall 		for (i = 0; i < bgep->intr_cnt; i++) {
20191369Sdduvall 			(void) ddi_intr_enable(bgep->htable[i]);
20201369Sdduvall 		}
20211369Sdduvall 	}
20221369Sdduvall 
20231369Sdduvall 	/*
20241369Sdduvall 	 * Initialise link state variables
20251369Sdduvall 	 * Stop, reset & reinitialise the chip.
20261369Sdduvall 	 * Initialise the (internal) PHY.
20271369Sdduvall 	 */
20281369Sdduvall 	bgep->link_state = LINK_STATE_UNKNOWN;
20291369Sdduvall 	bgep->link_up_msg = bgep->link_down_msg = " (initialized)";
20301369Sdduvall 
20311369Sdduvall 	mutex_enter(bgep->genlock);
20321369Sdduvall 
20331369Sdduvall 	/*
20341369Sdduvall 	 * Reset chip & rings to initial state; also reset address
20351369Sdduvall 	 * filtering, promiscuity, loopback mode.
20361369Sdduvall 	 */
20371408Srandyf #ifdef BGE_IPMI_ASF
20381408Srandyf 	bge_reset(bgep, ASF_MODE_SHUTDOWN);
20391408Srandyf #else
20401369Sdduvall 	bge_reset(bgep);
20411408Srandyf #endif
20421369Sdduvall 
20431369Sdduvall 	bzero(bgep->mcast_hash, sizeof (bgep->mcast_hash));
20441369Sdduvall 	bzero(bgep->mcast_refs, sizeof (bgep->mcast_refs));
20451369Sdduvall 	bgep->promisc = B_FALSE;
20461369Sdduvall 	bgep->param_loop_mode = BGE_LOOP_NONE;
20471369Sdduvall 
20481369Sdduvall 	mutex_exit(bgep->genlock);
20491369Sdduvall 
20501369Sdduvall 	bge_phys_init(bgep);
20511369Sdduvall 	bgep->progress |= PROGRESS_PHY;
20521369Sdduvall 
20531369Sdduvall 	/*
20541369Sdduvall 	 * Register NDD-tweakable parameters
20551369Sdduvall 	 */
20561369Sdduvall 	if (bge_nd_init(bgep)) {
20571369Sdduvall 		bge_problem(bgep, "bge_nd_init() failed");
20581369Sdduvall 		goto attach_fail;
20591369Sdduvall 	}
20601369Sdduvall 	bgep->progress |= PROGRESS_NDD;
20611369Sdduvall 
20621369Sdduvall 	/*
20631369Sdduvall 	 * Create & initialise named kstats
20641369Sdduvall 	 */
20651369Sdduvall 	bge_init_kstats(bgep, instance);
20661369Sdduvall 	bgep->progress |= PROGRESS_KSTATS;
20671369Sdduvall 
20681369Sdduvall 	/*
20691369Sdduvall 	 * Determine whether to override the chip's own MAC address
20701369Sdduvall 	 */
20711369Sdduvall 	bge_find_mac_address(bgep, cidp);
20721369Sdduvall 	ethaddr_copy(cidp->vendor_addr.addr, bgep->curr_addr.addr);
20731369Sdduvall 	bgep->curr_addr.set = 1;
20741369Sdduvall 
20751369Sdduvall 	/*
20761369Sdduvall 	 * Initialize pointers to device specific functions which
20771369Sdduvall 	 * will be used by the generic layer.
20781369Sdduvall 	 */
20791369Sdduvall 	mip = &(macp->m_info);
20801369Sdduvall 	mip->mi_media = DL_ETHER;
20811369Sdduvall 	mip->mi_sdu_min = 0;
20821369Sdduvall 	mip->mi_sdu_max = cidp->ethmax_size - sizeof (struct ether_header);
20831369Sdduvall 	mip->mi_poll = DL_CAPAB_POLL;
20841369Sdduvall 
2085*1611Szh199473 	/*
2086*1611Szh199473 	 * Workaround for Incorrect pseudo-header checksum calculation.
2087*1611Szh199473 	 * 	Use partial checksum offload for all affected chips.
2088*1611Szh199473 	 */
2089*1611Szh199473 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
2090*1611Szh199473 		mip->mi_cksum = HCKSUM_INET_PARTIAL | HCKSUM_IPHDRCKSUM;
2091*1611Szh199473 	else
2092*1611Szh199473 		mip->mi_cksum = HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM;
2093*1611Szh199473 
20941369Sdduvall 	mip->mi_addr_length = ETHERADDRL;
20951369Sdduvall 	bcopy(ether_brdcst, mip->mi_brdcst_addr, ETHERADDRL);
20961369Sdduvall 	bcopy(bgep->curr_addr.addr, mip->mi_unicst_addr, ETHERADDRL);
20971369Sdduvall 
20981369Sdduvall 	MAC_STAT_MIB(mip->mi_stat);
20991369Sdduvall 	mip->mi_stat[MAC_STAT_UNKNOWNS] = B_FALSE;
21001369Sdduvall 	MAC_STAT_ETHER(mip->mi_stat);
21011369Sdduvall 	mip->mi_stat[MAC_STAT_SQE_ERRORS] = B_FALSE;
21021369Sdduvall 	mip->mi_stat[MAC_STAT_MACRCV_ERRORS] = B_FALSE;
21031369Sdduvall 	if (!(bgep->chipid.flags & CHIP_FLAG_SERDES))
21041369Sdduvall 		MAC_STAT_MII(mip->mi_stat);
21051369Sdduvall 
21061369Sdduvall 	macp->m_stat = bge_m_stat;
21071369Sdduvall 	macp->m_stop = bge_m_stop;
21081369Sdduvall 	macp->m_start = bge_m_start;
21091369Sdduvall 	macp->m_unicst = bge_m_unicst;
21101369Sdduvall 	macp->m_multicst = bge_m_multicst;
21111369Sdduvall 	macp->m_promisc = bge_m_promisc;
21121369Sdduvall 	macp->m_tx = bge_m_tx;
21131369Sdduvall 	macp->m_resources = bge_m_resources;
21141369Sdduvall 	macp->m_ioctl = bge_m_ioctl;
21151369Sdduvall 
21161369Sdduvall 	macp->m_dip = devinfo;
21171369Sdduvall 	macp->m_ident = MAC_IDENT;
21181369Sdduvall 
21191369Sdduvall 	/*
21201369Sdduvall 	 * Finally, we're ready to register ourselves with the MAC layer
21211369Sdduvall 	 * interface; if this succeeds, we're all ready to start()
21221369Sdduvall 	 */
21231369Sdduvall 	if (mac_register(macp) != 0)
21241369Sdduvall 		goto attach_fail;
21251369Sdduvall 
21261369Sdduvall 	cychand.cyh_func = bge_chip_cyclic;
21271369Sdduvall 	cychand.cyh_arg = bgep;
21281369Sdduvall 	cychand.cyh_level = CY_LOCK_LEVEL;
21291369Sdduvall 	cyctime.cyt_when = 0;
21301369Sdduvall 	cyctime.cyt_interval = BGE_CYCLIC_PERIOD;
21311369Sdduvall 	mutex_enter(&cpu_lock);
21321369Sdduvall 	bgep->cyclic_id = cyclic_add(&cychand, &cyctime);
21331369Sdduvall 	mutex_exit(&cpu_lock);
21341369Sdduvall 
21351369Sdduvall 	bgep->progress |= PROGRESS_READY;
21361369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
21371369Sdduvall 	return (DDI_SUCCESS);
21381369Sdduvall 
21391369Sdduvall attach_fail:
21401408Srandyf #ifdef BGE_IPMI_ASF
21411408Srandyf 	bge_unattach(bgep, ASF_MODE_NONE);
21421408Srandyf #else
21431369Sdduvall 	bge_unattach(bgep);
21441408Srandyf #endif
21451369Sdduvall 	return (DDI_FAILURE);
21461369Sdduvall }
21471369Sdduvall 
21481369Sdduvall /*
21491369Sdduvall  *	bge_suspend() -- suspend transmit/receive for powerdown
21501369Sdduvall  */
21511369Sdduvall static int
21521369Sdduvall bge_suspend(bge_t *bgep)
21531369Sdduvall {
21541369Sdduvall 	/*
21551369Sdduvall 	 * Stop processing and idle (powerdown) the PHY ...
21561369Sdduvall 	 */
21571369Sdduvall 	mutex_enter(bgep->genlock);
21581408Srandyf #ifdef BGE_IPMI_ASF
21591408Srandyf 	/*
21601408Srandyf 	 * Power management hasn't been supported in BGE now. If you
21611408Srandyf 	 * want to implement it, please add the ASF/IPMI related
21621408Srandyf 	 * code here.
21631408Srandyf 	 */
21641408Srandyf #endif
21651369Sdduvall 	bge_stop(bgep);
21661369Sdduvall 	bge_phys_idle(bgep);
21671369Sdduvall 	mutex_exit(bgep->genlock);
21681369Sdduvall 
21691369Sdduvall 	return (DDI_SUCCESS);
21701369Sdduvall }
21711369Sdduvall 
21721369Sdduvall /*
21731369Sdduvall  * detach(9E) -- Detach a device from the system
21741369Sdduvall  */
21751369Sdduvall static int
21761369Sdduvall bge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
21771369Sdduvall {
21781369Sdduvall 	bge_t *bgep;
21791408Srandyf #ifdef BGE_IPMI_ASF
21801408Srandyf 	uint_t asf_mode;
21811408Srandyf 	asf_mode = ASF_MODE_NONE;
21821408Srandyf #endif
21831369Sdduvall 
21841369Sdduvall 	BGE_GTRACE(("bge_detach($%p, %d)", (void *)devinfo, cmd));
21851369Sdduvall 
21861369Sdduvall 	bgep = ddi_get_driver_private(devinfo);
21871369Sdduvall 
21881369Sdduvall 	switch (cmd) {
21891369Sdduvall 	default:
21901369Sdduvall 		return (DDI_FAILURE);
21911369Sdduvall 
21921369Sdduvall 	case DDI_SUSPEND:
21931369Sdduvall 		return (bge_suspend(bgep));
21941369Sdduvall 
21951369Sdduvall 	case DDI_DETACH:
21961369Sdduvall 		break;
21971369Sdduvall 	}
21981369Sdduvall 
21991408Srandyf #ifdef BGE_IPMI_ASF
22001408Srandyf 	mutex_enter(bgep->genlock);
22011408Srandyf 	if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
22021408Srandyf 
22031408Srandyf 		bge_asf_update_status(bgep);
22041408Srandyf 		bge_asf_stop_timer(bgep);
22051408Srandyf 		bgep->asf_status = ASF_STAT_STOP;
22061408Srandyf 
22071408Srandyf 		bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
22081408Srandyf 
22091408Srandyf 		if (bgep->asf_pseudostop) {
22101408Srandyf 			bgep->link_up_msg = bgep->link_down_msg = " (stopped)";
22111408Srandyf 			bge_chip_stop(bgep, B_FALSE);
22121408Srandyf 			bgep->bge_mac_state = BGE_MAC_STOPPED;
22131408Srandyf 			bgep->asf_pseudostop = B_FALSE;
22141408Srandyf 		}
22151408Srandyf 
22161408Srandyf 		asf_mode = ASF_MODE_POST_SHUTDOWN;
22171408Srandyf 	}
22181408Srandyf 	mutex_exit(bgep->genlock);
22191408Srandyf #endif
22201408Srandyf 
22211369Sdduvall 	/*
22221369Sdduvall 	 * Unregister from the GLD subsystem.  This can fail, in
22231369Sdduvall 	 * particular if there are DLPI style-2 streams still open -
22241369Sdduvall 	 * in which case we just return failure without shutting
22251369Sdduvall 	 * down chip operations.
22261369Sdduvall 	 */
22271369Sdduvall 	if (mac_unregister(bgep->macp) != 0)
22281369Sdduvall 		return (DDI_FAILURE);
22291369Sdduvall 
22301369Sdduvall 	/*
22311369Sdduvall 	 * All activity stopped, so we can clean up & exit
22321369Sdduvall 	 */
22331408Srandyf #ifdef BGE_IPMI_ASF
22341408Srandyf 	bge_unattach(bgep, asf_mode);
22351408Srandyf #else
22361369Sdduvall 	bge_unattach(bgep);
22371408Srandyf #endif
22381369Sdduvall 	return (DDI_SUCCESS);
22391369Sdduvall }
22401369Sdduvall 
22411369Sdduvall 
22421369Sdduvall /*
22431369Sdduvall  * ========== Module Loading Data & Entry Points ==========
22441369Sdduvall  */
22451369Sdduvall 
22461369Sdduvall #undef	BGE_DBG
22471369Sdduvall #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
22481369Sdduvall 
22491369Sdduvall DDI_DEFINE_STREAM_OPS(bge_dev_ops, nulldev, nulldev, bge_attach, bge_detach,
22501369Sdduvall     nodev, NULL, D_MP, NULL);
22511369Sdduvall 
22521369Sdduvall static struct modldrv bge_modldrv = {
22531369Sdduvall 	&mod_driverops,		/* Type of module.  This one is a driver */
22541369Sdduvall 	bge_ident,		/* short description */
22551369Sdduvall 	&bge_dev_ops		/* driver specific ops */
22561369Sdduvall };
22571369Sdduvall 
22581369Sdduvall static struct modlinkage modlinkage = {
22591369Sdduvall 	MODREV_1, (void *)&bge_modldrv, NULL
22601369Sdduvall };
22611369Sdduvall 
22621369Sdduvall 
22631369Sdduvall int
22641369Sdduvall _info(struct modinfo *modinfop)
22651369Sdduvall {
22661369Sdduvall 	return (mod_info(&modlinkage, modinfop));
22671369Sdduvall }
22681369Sdduvall 
22691369Sdduvall int
22701369Sdduvall _init(void)
22711369Sdduvall {
22721369Sdduvall 	int status;
22731369Sdduvall 
22741369Sdduvall 	mac_init_ops(&bge_dev_ops, "bge");
22751369Sdduvall 	status = mod_install(&modlinkage);
22761369Sdduvall 	if (status == DDI_SUCCESS)
22771369Sdduvall 		mutex_init(bge_log_mutex, NULL, MUTEX_DRIVER, NULL);
22781369Sdduvall 	else
22791369Sdduvall 		mac_fini_ops(&bge_dev_ops);
22801369Sdduvall 	return (status);
22811369Sdduvall }
22821369Sdduvall 
22831369Sdduvall int
22841369Sdduvall _fini(void)
22851369Sdduvall {
22861369Sdduvall 	int status;
22871369Sdduvall 
22881369Sdduvall 	status = mod_remove(&modlinkage);
22891369Sdduvall 	if (status == DDI_SUCCESS) {
22901369Sdduvall 		mac_fini_ops(&bge_dev_ops);
22911369Sdduvall 		mutex_destroy(bge_log_mutex);
22921369Sdduvall 	}
22931369Sdduvall 	return (status);
22941369Sdduvall }
22951369Sdduvall 
22961369Sdduvall 
22971369Sdduvall /*
22981369Sdduvall  * bge_add_intrs:
22991369Sdduvall  *
23001369Sdduvall  * Register FIXED or MSI interrupts.
23011369Sdduvall  */
23021369Sdduvall static int
23031369Sdduvall bge_add_intrs(bge_t *bgep, int	intr_type)
23041369Sdduvall {
23051369Sdduvall 	dev_info_t	*dip = bgep->devinfo;
23061369Sdduvall 	int		avail, actual, intr_size, count = 0;
23071369Sdduvall 	int		i, flag, ret;
23081369Sdduvall 
23091369Sdduvall 	bge_log(bgep, "bge_add_intrs: interrupt type 0x%x\n", intr_type);
23101369Sdduvall 
23111369Sdduvall 	/* Get number of interrupts */
23121369Sdduvall 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
23131369Sdduvall 	if ((ret != DDI_SUCCESS) || (count == 0)) {
23141369Sdduvall 		bge_error(bgep, "ddi_intr_get_nintrs() failure, ret: %d, "
23151369Sdduvall 		    "count: %d", ret, count);
23161369Sdduvall 
23171369Sdduvall 		return (DDI_FAILURE);
23181369Sdduvall 	}
23191369Sdduvall 
23201369Sdduvall 	/* Get number of available interrupts */
23211369Sdduvall 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
23221369Sdduvall 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
23231369Sdduvall 		bge_error(bgep, "ddi_intr_get_navail() failure, "
23241369Sdduvall 		    "ret: %d, avail: %d\n", ret, avail);
23251369Sdduvall 
23261369Sdduvall 		return (DDI_FAILURE);
23271369Sdduvall 	}
23281369Sdduvall 
23291369Sdduvall 	if (avail < count) {
23301369Sdduvall 		bge_log(bgep, "nitrs() returned %d, navail returned %d\n",
23311369Sdduvall 		    count, avail);
23321369Sdduvall 	}
23331369Sdduvall 
23341369Sdduvall 	/*
23351369Sdduvall 	 * BGE hardware generates only single MSI even though it claims
23361369Sdduvall 	 * to support multiple MSIs. So, hard code MSI count value to 1.
23371369Sdduvall 	 */
23381369Sdduvall 	if (intr_type == DDI_INTR_TYPE_MSI) {
23391369Sdduvall 		count = 1;
23401369Sdduvall 		flag = DDI_INTR_ALLOC_STRICT;
23411369Sdduvall 	} else {
23421369Sdduvall 		flag = DDI_INTR_ALLOC_NORMAL;
23431369Sdduvall 	}
23441369Sdduvall 
23451369Sdduvall 	/* Allocate an array of interrupt handles */
23461369Sdduvall 	intr_size = count * sizeof (ddi_intr_handle_t);
23471369Sdduvall 	bgep->htable = kmem_alloc(intr_size, KM_SLEEP);
23481369Sdduvall 
23491369Sdduvall 	/* Call ddi_intr_alloc() */
23501369Sdduvall 	ret = ddi_intr_alloc(dip, bgep->htable, intr_type, 0,
23511369Sdduvall 	    count, &actual, flag);
23521369Sdduvall 
23531369Sdduvall 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
23541369Sdduvall 		bge_error(bgep, "ddi_intr_alloc() failed %d\n", ret);
23551369Sdduvall 
23561369Sdduvall 		kmem_free(bgep->htable, intr_size);
23571369Sdduvall 		return (DDI_FAILURE);
23581369Sdduvall 	}
23591369Sdduvall 
23601369Sdduvall 	if (actual < count) {
23611369Sdduvall 		bge_log(bgep, "Requested: %d, Received: %d\n", count, actual);
23621369Sdduvall 	}
23631369Sdduvall 
23641369Sdduvall 	bgep->intr_cnt = actual;
23651369Sdduvall 
23661369Sdduvall 	/*
23671369Sdduvall 	 * Get priority for first msi, assume remaining are all the same
23681369Sdduvall 	 */
23691369Sdduvall 	if ((ret = ddi_intr_get_pri(bgep->htable[0], &bgep->intr_pri)) !=
23701369Sdduvall 	    DDI_SUCCESS) {
23711369Sdduvall 		bge_error(bgep, "ddi_intr_get_pri() failed %d\n", ret);
23721369Sdduvall 
23731369Sdduvall 		/* Free already allocated intr */
23741369Sdduvall 		for (i = 0; i < actual; i++) {
23751369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
23761369Sdduvall 		}
23771369Sdduvall 
23781369Sdduvall 		kmem_free(bgep->htable, intr_size);
23791369Sdduvall 		return (DDI_FAILURE);
23801369Sdduvall 	}
23811369Sdduvall 
23821369Sdduvall 	/* Call ddi_intr_add_handler() */
23831369Sdduvall 	for (i = 0; i < actual; i++) {
23841369Sdduvall 		if ((ret = ddi_intr_add_handler(bgep->htable[i], bge_intr,
23851369Sdduvall 		    (caddr_t)bgep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
23861369Sdduvall 			bge_error(bgep, "ddi_intr_add_handler() "
23871369Sdduvall 			    "failed %d\n", ret);
23881369Sdduvall 
23891369Sdduvall 			/* Free already allocated intr */
23901369Sdduvall 			for (i = 0; i < actual; i++) {
23911369Sdduvall 				(void) ddi_intr_free(bgep->htable[i]);
23921369Sdduvall 			}
23931369Sdduvall 
23941369Sdduvall 			kmem_free(bgep->htable, intr_size);
23951369Sdduvall 			return (DDI_FAILURE);
23961369Sdduvall 		}
23971369Sdduvall 	}
23981369Sdduvall 
23991369Sdduvall 	if ((ret = ddi_intr_get_cap(bgep->htable[0], &bgep->intr_cap))
24001369Sdduvall 		!= DDI_SUCCESS) {
24011369Sdduvall 		bge_error(bgep, "ddi_intr_get_cap() failed %d\n", ret);
24021369Sdduvall 
24031369Sdduvall 		for (i = 0; i < actual; i++) {
24041369Sdduvall 			(void) ddi_intr_remove_handler(bgep->htable[i]);
24051369Sdduvall 			(void) ddi_intr_free(bgep->htable[i]);
24061369Sdduvall 		}
24071369Sdduvall 
24081369Sdduvall 		kmem_free(bgep->htable, intr_size);
24091369Sdduvall 		return (DDI_FAILURE);
24101369Sdduvall 	}
24111369Sdduvall 
24121369Sdduvall 	return (DDI_SUCCESS);
24131369Sdduvall }
24141369Sdduvall 
24151369Sdduvall /*
24161369Sdduvall  * bge_rem_intrs:
24171369Sdduvall  *
24181369Sdduvall  * Unregister FIXED or MSI interrupts
24191369Sdduvall  */
24201369Sdduvall static void
24211369Sdduvall bge_rem_intrs(bge_t *bgep)
24221369Sdduvall {
24231369Sdduvall 	int	i;
24241369Sdduvall 
24251369Sdduvall 	bge_log(bgep, "bge_rem_intrs\n");
24261369Sdduvall 
24271369Sdduvall 	/* Disable all interrupts */
24281369Sdduvall 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
24291369Sdduvall 		/* Call ddi_intr_block_disable() */
24301369Sdduvall 		(void) ddi_intr_block_disable(bgep->htable, bgep->intr_cnt);
24311369Sdduvall 	} else {
24321369Sdduvall 		for (i = 0; i < bgep->intr_cnt; i++) {
24331369Sdduvall 			(void) ddi_intr_disable(bgep->htable[i]);
24341369Sdduvall 		}
24351369Sdduvall 	}
24361369Sdduvall 
24371369Sdduvall 	/* Call ddi_intr_remove_handler() */
24381369Sdduvall 	for (i = 0; i < bgep->intr_cnt; i++) {
24391369Sdduvall 		(void) ddi_intr_remove_handler(bgep->htable[i]);
24401369Sdduvall 		(void) ddi_intr_free(bgep->htable[i]);
24411369Sdduvall 	}
24421369Sdduvall 
24431369Sdduvall 	kmem_free(bgep->htable, bgep->intr_cnt * sizeof (ddi_intr_handle_t));
24441369Sdduvall }
2445