xref: /onnv-gate/usr/src/uts/sun4u/io/sbd_io.c (revision 11651:b0aa8d9f0afd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54845Svikram  * Common Development and Distribution License (the "License").
64845Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11651SVijay.Balakrishna@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/debug.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/cred.h>
300Sstevel@tonic-gate #include <sys/dditypes.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/sunndi.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
350Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/note.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/sbdpriv.h>
400Sstevel@tonic-gate #include <sys/sbd_io.h>
410Sstevel@tonic-gate #include <sys/machsystm.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate extern void sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip);
450Sstevel@tonic-gate extern sbd_state_t ostate_cvt(sbd_istate_t);
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Given a dev_info_t of a branch root, walk down the
490Sstevel@tonic-gate  * branch to attach drivers
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate /*ARGSUSED*/
520Sstevel@tonic-gate void
sbd_attach_io(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)530Sstevel@tonic-gate sbd_attach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	ASSERT(e_ddi_branch_held(dip));
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	(void) e_ddi_branch_configure(dip, NULL, 0);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	ASSERT(sbp->sb_iopath[unit] != NULL);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	(void) ddi_pathname(dip, sbp->sb_iopath[unit]);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * remove device nodes for the branch indicated by dip
680Sstevel@tonic-gate  * Hold the status lock so that status can safely do ddi_pathname().
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate /*ARGSUSED*/
710Sstevel@tonic-gate void
sbd_detach_io(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)720Sstevel@tonic-gate sbd_detach_io(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int rv;
750Sstevel@tonic-gate 	dev_info_t *fdip = NULL;
760Sstevel@tonic-gate 	sbd_board_t *sbp = SBDH2BD(hp->h_sbd);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	ASSERT(e_ddi_branch_held(dip));
790Sstevel@tonic-gate 	mutex_enter(&sbp->sb_slock);
800Sstevel@tonic-gate 	rv = e_ddi_branch_unconfigure(dip, &fdip, DEVI_BRANCH_EVENT);
810Sstevel@tonic-gate 	mutex_exit(&sbp->sb_slock);
820Sstevel@tonic-gate 	if (rv) {
830Sstevel@tonic-gate 		/*
840Sstevel@tonic-gate 		 * If non-NULL, fdip is returned held and must be released.
850Sstevel@tonic-gate 		 */
860Sstevel@tonic-gate 		if (fdip != NULL) {
870Sstevel@tonic-gate 			sbd_errno_decode(rv, ep, fdip);
880Sstevel@tonic-gate 			ddi_release_devi(fdip);
890Sstevel@tonic-gate 		} else {
900Sstevel@tonic-gate 			sbd_errno_decode(rv, ep, dip);
910Sstevel@tonic-gate 		}
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*ARGSUSED*/
960Sstevel@tonic-gate void
sbd_init_io_unit(sbd_board_t * sbp,int unit)970Sstevel@tonic-gate sbd_init_io_unit(sbd_board_t *sbp, int unit)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	sbd_istate_t	new_state;
1000Sstevel@tonic-gate 	sbd_io_unit_t	*ip;
1010Sstevel@tonic-gate 	dev_info_t	*dip;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_IO, unit)) {
1060Sstevel@tonic-gate 		new_state = SBD_STATE_CONFIGURED;
1070Sstevel@tonic-gate 	} else if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_IO, unit)) {
1080Sstevel@tonic-gate 		new_state = SBD_STATE_CONNECTED;
1090Sstevel@tonic-gate 	} else {
1100Sstevel@tonic-gate 		new_state = SBD_STATE_EMPTY;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][unit];
1130Sstevel@tonic-gate 	ip->sbi_cm.sbdev_cond = sbd_get_comp_cond(dip);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * Any changes to this io component should be performed above
1170Sstevel@tonic-gate 	 * this call to ensure the component is fully initialized
1180Sstevel@tonic-gate 	 * before transitioning to the new state.
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO, unit, new_state);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*ARGSUSED*/
1240Sstevel@tonic-gate int
sbd_disconnect_io(sbd_handle_t * hp,int unit)1250Sstevel@tonic-gate sbd_disconnect_io(sbd_handle_t *hp, int unit)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate int
sbd_pre_attach_io(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)1310Sstevel@tonic-gate sbd_pre_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
1340Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devlist))
1350Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devnum))
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*ARGSUSED*/
1410Sstevel@tonic-gate int
sbd_pre_detach_io(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)1420Sstevel@tonic-gate sbd_pre_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	fn_t	f = "sbd_pre_detach_io";
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	PR_IO("%s...\n", f);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (devnum <= 0)
1490Sstevel@tonic-gate 		return (-1);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* fail if any I/O devices are referenced */
1520Sstevel@tonic-gate 	if (sbd_check_io_refs(hp, devlist, devnum) > 0) {
1530Sstevel@tonic-gate 		PR_IO("%s: failed - I/O devices ref'd\n", f);
1540Sstevel@tonic-gate 		return (-1);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	return (0);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*ARGSUSED*/
1610Sstevel@tonic-gate int
sbd_post_attach_io(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)1620Sstevel@tonic-gate sbd_post_attach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
1650Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devlist))
1660Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devnum))
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (0);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*ARGSUSED*/
1720Sstevel@tonic-gate int
sbd_post_detach_io(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)1730Sstevel@tonic-gate sbd_post_detach_io(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	return (0);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*ARGSUSED*/
1790Sstevel@tonic-gate int
sbd_io_status(sbd_handle_t * hp,sbd_devset_t devset,sbd_dev_stat_t * dsp)1800Sstevel@tonic-gate sbd_io_status(sbd_handle_t *hp, sbd_devset_t devset, sbd_dev_stat_t *dsp)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	int		i, ix;
1830Sstevel@tonic-gate 	sbd_board_t	*sbp;
1840Sstevel@tonic-gate 	sbd_io_stat_t	*isp;
1850Sstevel@tonic-gate 	sbd_io_unit_t	*ip;
1860Sstevel@tonic-gate 	sbd_istate_t	dstate;
1870Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
1880Sstevel@tonic-gate 	sbderror_t	*ep;
1890Sstevel@tonic-gate 	sbd_error_t	*sep;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	ep = HD2MACHERR(hp);
1970Sstevel@tonic-gate 	sep = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
1980Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * Concurrent status and unconfigure, disconnect are allowed.
2020Sstevel@tonic-gate 	 * To prevent DR code from accessing stale dips, check the
2030Sstevel@tonic-gate 	 * present devset and access the dips with status lock held.
2040Sstevel@tonic-gate 	 * Disconnect and unconfigure code change dip state with
2050Sstevel@tonic-gate 	 * status lock (sb_slock) held.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	mutex_enter(&sbp->sb_slock);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
2120Sstevel@tonic-gate 		dev_info_t	*dip;
2130Sstevel@tonic-gate 		int		unit;
2140Sstevel@tonic-gate 		int		namelen;
2150Sstevel@tonic-gate 		int		refcount = 0;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
2180Sstevel@tonic-gate 			continue;
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * Check to make sure the io component is in a state
2210Sstevel@tonic-gate 		 * where its fully initialized.
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 		if (SBD_DEVICE_STATE(sbp, SBD_COMP_IO, i) == SBD_STATE_EMPTY)
2240Sstevel@tonic-gate 			continue;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
2270Sstevel@tonic-gate 		if (dip == NULL)
2280Sstevel@tonic-gate 			continue;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		isp = &dsp->d_io;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		bzero((caddr_t)isp, sizeof (*isp));
2330Sstevel@tonic-gate 		namelen = sizeof (isp->is_name);
2340Sstevel@tonic-gate 		(void) ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
2350Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, OBP_DEVICETYPE,
2360Sstevel@tonic-gate 		    (caddr_t)isp->is_name, &namelen);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		isp->is_unit = sbdp_get_unit_num(hdp, dip);
2390Sstevel@tonic-gate 		if (isp->is_unit < 0) {
2400Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
2410Sstevel@tonic-gate 				continue;
2420Sstevel@tonic-gate 			else {
2430Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
2440Sstevel@tonic-gate 				break;
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 		unit = isp->is_unit;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		dstate = SBD_DEVICE_STATE(sbp, SBD_COMP_IO, unit);
2500Sstevel@tonic-gate 		isp->is_ostate	= ostate_cvt(dstate);
2510Sstevel@tonic-gate 		isp->is_type = SBD_COMP_IO;
2520Sstevel@tonic-gate 		ip = SBD_GET_BOARD_IOUNIT(sbp, unit);
2534845Svikram 		ip->sbi_cm.sbdev_cond = sbd_get_comp_cond(dip);
2540Sstevel@tonic-gate 		isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond;
2550Sstevel@tonic-gate 		isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy;
2560Sstevel@tonic-gate 		isp->is_cm.c_time = ip->sbi_cm.sbdev_time;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		/*
2600Sstevel@tonic-gate 		 * This is safe to do as unconfigure and disconnect
2610Sstevel@tonic-gate 		 * hold the status lock while changing dip state.
2620Sstevel@tonic-gate 		 */
2630Sstevel@tonic-gate 		(void) ddi_pathname(dip, isp->is_pathname);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		/*
2660Sstevel@tonic-gate 		 * We use a dummy handle in which to collect
2670Sstevel@tonic-gate 		 * the major numbers of unsafe devices.
2680Sstevel@tonic-gate 		 */
269*11651SVijay.Balakrishna@Sun.COM 		sbdp_check_devices(dip, &refcount, sep, NULL);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		isp->is_referenced = (refcount == 0) ? 0 : 1;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		isp->is_unsafe_count = 0;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * Reset error field since we don't care about
2770Sstevel@tonic-gate 		 * errors at this level.  The unsafe devices
2780Sstevel@tonic-gate 		 * will be reported in the structure.
2790Sstevel@tonic-gate 		 */
2800Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NOERROR);
2810Sstevel@tonic-gate 		ep->e_rsc[0] = '\0';
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		ix++;
2840Sstevel@tonic-gate 		dsp++;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	mutex_exit(&sbp->sb_slock);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	kmem_free(sep, sizeof (sbd_error_t));
2900Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	return (ix);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*ARGSUSED*/
2960Sstevel@tonic-gate int
sbd_io_cnt(sbd_handle_t * hp,sbd_devset_t devset)2970Sstevel@tonic-gate sbd_io_cnt(sbd_handle_t *hp, sbd_devset_t devset)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	int		i, ix;
3000Sstevel@tonic-gate 	sbd_board_t	*sbp;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
3100Sstevel@tonic-gate 		dev_info_t	*dip;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
3130Sstevel@tonic-gate 			continue;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
3160Sstevel@tonic-gate 		if (dip == NULL)
3170Sstevel@tonic-gate 			continue;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		ix++;
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	return (ix);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate int
sbd_check_io_refs(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)3260Sstevel@tonic-gate sbd_check_io_refs(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	register int	i, reftotal = 0;
3290Sstevel@tonic-gate 	fn_t	f = "sbd_check_io_refs";
3300Sstevel@tonic-gate 	sbd_error_t *sep;
3310Sstevel@tonic-gate 	sbderror_t *ep;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	sep = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
3340Sstevel@tonic-gate 	ep = HD2MACHERR(hp);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
3370Sstevel@tonic-gate 		dev_info_t	*dip;
3380Sstevel@tonic-gate 		int		ref;
339*11651SVijay.Balakrishna@Sun.COM 		int		refcount_non_gldv3;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		dip = devlist[i].dv_dip;
3420Sstevel@tonic-gate 		ref = 0;
343*11651SVijay.Balakrishna@Sun.COM 		refcount_non_gldv3 = 0;
344*11651SVijay.Balakrishna@Sun.COM 		sbdp_check_devices(dip, &ref, sep, &refcount_non_gldv3);
345*11651SVijay.Balakrishna@Sun.COM 		ASSERT(refcount_non_gldv3 >= 0);
346*11651SVijay.Balakrishna@Sun.COM 		ASSERT(ref >= refcount_non_gldv3);
347*11651SVijay.Balakrishna@Sun.COM 		/*
348*11651SVijay.Balakrishna@Sun.COM 		 * Ignore reference counts of non-gldv3 network devices
349*11651SVijay.Balakrishna@Sun.COM 		 * as Crossbow creates reference counts for non-active
350*11651SVijay.Balakrishna@Sun.COM 		 * (unplumbed) instances.  Reference count check in
351*11651SVijay.Balakrishna@Sun.COM 		 * detach() known to prevent device from detaching
352*11651SVijay.Balakrishna@Sun.COM 		 * as necessary.
353*11651SVijay.Balakrishna@Sun.COM 		 */
354*11651SVijay.Balakrishna@Sun.COM 		ref -= refcount_non_gldv3;
3550Sstevel@tonic-gate 		if (ref) {
3560Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) == 0) {
3570Sstevel@tonic-gate 				SBD_GET_PERR(sep, ep);
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 			SBD_GET_PERR(sep, &devlist[i].dv_error);
3600Sstevel@tonic-gate 		}
361*11651SVijay.Balakrishna@Sun.COM 		PR_IO("%s: dip(%s) ref = %d\n", f, ddi_get_name(dip), ref);
3620Sstevel@tonic-gate 		reftotal += ref;
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	kmem_free(sep, sizeof (sbd_error_t));
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	return (reftotal);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate int
sbd_check_io_attached(dev_info_t * dip,void * arg)3710Sstevel@tonic-gate sbd_check_io_attached(dev_info_t *dip, void *arg)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	dev_info_t **tdip;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	tdip = (dev_info_t **)arg;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (dip == *tdip) {
3780Sstevel@tonic-gate 		int state;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		state = ddi_get_devstate(dip);
3811333Scth 		if (i_ddi_devi_attached(dip) || (state == DDI_DEVSTATE_UP)) {
3820Sstevel@tonic-gate 			*tdip = NULL;
3830Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate int
sbd_pre_release_io(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)3900Sstevel@tonic-gate sbd_pre_release_io(sbd_handle_t *hp,
3910Sstevel@tonic-gate 	sbd_devlist_t *devlist, int devnum)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	fn_t	f = "sbd_pre_release_io";
3940Sstevel@tonic-gate 	int	rv = 0;
3950Sstevel@tonic-gate 	int	i;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ASSERT(devnum > 0);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/* fail if any I/O devices are referenced */
4000Sstevel@tonic-gate 	if ((rv = sbd_check_io_refs(hp, devlist, devnum)) > 0) {
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * One of the devices may have failed check to see which
4030Sstevel@tonic-gate 		 * and set in the main handle
4040Sstevel@tonic-gate 		 */
4050Sstevel@tonic-gate 		for (i = 0; i < devnum; i++) {
4060Sstevel@tonic-gate 			if (SBD_GET_ERR(&devlist[i].dv_error) != 0) {
4070Sstevel@tonic-gate 				(void) sbd_set_err_in_hdl(hp,
4080Sstevel@tonic-gate 				    &devlist[i].dv_error);
4090Sstevel@tonic-gate 				break;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 		}
4120Sstevel@tonic-gate 		PR_IO("%s: failed - I/O devices ref'd\n", f);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	return (rv);
4160Sstevel@tonic-gate }
417