xref: /illumos-gate/usr/src/uts/sun4u/ngdr/io/dr_io.c (revision df3cd224)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
525cf1a30Sjl  * Common Development and Distribution License (the "License").
625cf1a30Sjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2107d06da5SSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
23*df3cd224SVijay S Balakrishna  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * I/O support routines for DR
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
367c478bd9Sstevel@tonic-gate #include <sys/devops.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/poll.h>
397c478bd9Sstevel@tonic-gate #include <sys/conf.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
437c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate #include <sys/processor.h>
477c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
487c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
497c478bd9Sstevel@tonic-gate #include <sys/promif.h>
507c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
527c478bd9Sstevel@tonic-gate #include <sys/membar.h>
537c478bd9Sstevel@tonic-gate #include <sys/stack.h>
547c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
557c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
567c478bd9Sstevel@tonic-gate #include <sys/spitregs.h>
577c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
607c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
617c478bd9Sstevel@tonic-gate #include <sys/pte.h>
627c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
637c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
647c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/dr.h>
697c478bd9Sstevel@tonic-gate #include <sys/dr_util.h>
707c478bd9Sstevel@tonic-gate #include <sys/drmach.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate void
dr_init_io_unit(dr_io_unit_t * ip)737c478bd9Sstevel@tonic-gate dr_init_io_unit(dr_io_unit_t *ip)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	dr_state_t	new_state;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) {
787c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_CONFIGURED;
797c478bd9Sstevel@tonic-gate 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
807c478bd9Sstevel@tonic-gate 	} else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) {
817c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_CONNECTED;
827c478bd9Sstevel@tonic-gate 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
837c478bd9Sstevel@tonic-gate 	} else {
847c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_EMPTY;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	dr_device_transition(&ip->sbi_cm, new_state);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
907c478bd9Sstevel@tonic-gate void
dr_attach_io(dr_handle_t * hp,dr_common_unit_t * cp)917c478bd9Sstevel@tonic-gate dr_attach_io(dr_handle_t *hp, dr_common_unit_t *cp)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	sbd_error_t *err;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
967c478bd9Sstevel@tonic-gate 	err = drmach_configure(cp->sbdev_id, 0);
977c478bd9Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (!err)
1007c478bd9Sstevel@tonic-gate 		err = drmach_io_post_attach(cp->sbdev_id);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (err)
1037c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * remove device nodes for the branch indicated by cp
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1107c478bd9Sstevel@tonic-gate void
dr_detach_io(dr_handle_t * hp,dr_common_unit_t * cp)1117c478bd9Sstevel@tonic-gate dr_detach_io(dr_handle_t *hp, dr_common_unit_t *cp)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	sbd_error_t *err;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	err = drmach_unconfigure(cp->sbdev_id, 0);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (!err)
11825cf1a30Sjl 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (!err)
1217c478bd9Sstevel@tonic-gate 		err = drmach_io_post_release(cp->sbdev_id);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (err) {
1247c478bd9Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_CONFIGURED);
1257c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1307c478bd9Sstevel@tonic-gate int
dr_disconnect_io(dr_io_unit_t * ip)1317c478bd9Sstevel@tonic-gate dr_disconnect_io(dr_io_unit_t *ip)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1377c478bd9Sstevel@tonic-gate int
dr_pre_attach_io(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1387c478bd9Sstevel@tonic-gate dr_pre_attach_io(dr_handle_t *hp,
1397c478bd9Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	int		d;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
1447c478bd9Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[d];
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS configure %s", cp->sbdev_path);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	return (0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1537c478bd9Sstevel@tonic-gate int
dr_post_attach_io(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1547c478bd9Sstevel@tonic-gate dr_post_attach_io(dr_handle_t *hp,
1557c478bd9Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static int
dr_check_io_refs(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1617c478bd9Sstevel@tonic-gate dr_check_io_refs(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	register int	i, reftotal = 0;
1647c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_check_io_refs";
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
1677c478bd9Sstevel@tonic-gate 		dr_io_unit_t	*ip = (dr_io_unit_t *)devlist[i];
1687c478bd9Sstevel@tonic-gate 		dev_info_t	*dip;
1697c478bd9Sstevel@tonic-gate 		int		ref;
170*df3cd224SVijay S Balakrishna 		int		refcount_non_gldv3;
1717c478bd9Sstevel@tonic-gate 		sbd_error_t	*err;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		err = drmach_get_dip(ip->sbi_cm.sbdev_id, &dip);
1747c478bd9Sstevel@tonic-gate 		if (err)
1757c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
1767c478bd9Sstevel@tonic-gate 		else if (dip != NULL) {
1777c478bd9Sstevel@tonic-gate 			ref = 0;
178*df3cd224SVijay S Balakrishna 			refcount_non_gldv3 = 0;
1797c478bd9Sstevel@tonic-gate 			ASSERT(e_ddi_branch_held(dip));
180*df3cd224SVijay S Balakrishna 			dr_check_devices(dip, &ref, hp, NULL, NULL,
181*df3cd224SVijay S Balakrishna 			    0, &refcount_non_gldv3);
182*df3cd224SVijay S Balakrishna 			ASSERT(refcount_non_gldv3 >= 0);
183*df3cd224SVijay S Balakrishna 			ASSERT(ref >= refcount_non_gldv3);
184*df3cd224SVijay S Balakrishna 			/*
185*df3cd224SVijay S Balakrishna 			 * Ignore reference counts of non-gldv3 network devices
186*df3cd224SVijay S Balakrishna 			 * as Crossbow creates reference counts for non-active
187*df3cd224SVijay S Balakrishna 			 * (unplumbed) instances.  Reference count check in
188*df3cd224SVijay S Balakrishna 			 * detach() known to prevent device from detaching
189*df3cd224SVijay S Balakrishna 			 * as necessary.
190*df3cd224SVijay S Balakrishna 			 */
191*df3cd224SVijay S Balakrishna 			ref -= refcount_non_gldv3;
1927c478bd9Sstevel@tonic-gate 			hp->h_err = NULL;
1937c478bd9Sstevel@tonic-gate 			if (ref) {
1947c478bd9Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &ip->sbi_cm, ESBD_BUSY);
1957c478bd9Sstevel@tonic-gate 			}
1967c478bd9Sstevel@tonic-gate 			PR_IO("%s: dip(%s) ref = %d\n",
19707d06da5SSurya Prakki 			    f, ddi_get_name(dip), ref);
1987c478bd9Sstevel@tonic-gate 			reftotal += ref;
1997c478bd9Sstevel@tonic-gate 		} else {
2007c478bd9Sstevel@tonic-gate 			PR_IO("%s: NO dip for id (0x%x)\n",
20107d06da5SSurya Prakki 			    f, (uint_t)(uintptr_t)ip->sbi_cm.sbdev_id);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	return (reftotal);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate int
dr_pre_release_io(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)2097c478bd9Sstevel@tonic-gate dr_pre_release_io(dr_handle_t *hp,
2107c478bd9Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_pre_release_io";
2137c478bd9Sstevel@tonic-gate 	int	d;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	ASSERT(devnum > 0);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* fail if any I/O device pre-release fails */
2187c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2197c478bd9Sstevel@tonic-gate 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if ((hp->h_err = drmach_io_pre_release(
22207d06da5SSurya Prakki 		    ip->sbi_cm.sbdev_id)) != 0) {
2237c478bd9Sstevel@tonic-gate 			return (-1);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2287c478bd9Sstevel@tonic-gate 		dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2297c478bd9Sstevel@tonic-gate 		sbd_error_t *err;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		err = drmach_release(ip->sbi_cm.sbdev_id);
2327c478bd9Sstevel@tonic-gate 		if (err) {
2337c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error,
23407d06da5SSurya Prakki 			    &err);
2357c478bd9Sstevel@tonic-gate 			return (-1);
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* fail if any I/O devices are still referenced */
2407c478bd9Sstevel@tonic-gate 	if (dr_check_io_refs(hp, devlist, devnum) > 0) {
2417c478bd9Sstevel@tonic-gate 		PR_IO("%s: failed - I/O devices ref'd\n", f);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		/* recover before return error */
2447c478bd9Sstevel@tonic-gate 		for (d = 0; d < devnum; d++) {
2457c478bd9Sstevel@tonic-gate 			dr_io_unit_t *ip = (dr_io_unit_t *)devlist[d];
2467c478bd9Sstevel@tonic-gate 			sbd_error_t *err;
2477c478bd9Sstevel@tonic-gate 			err = drmach_io_unrelease(ip->sbi_cm.sbdev_id);
2487c478bd9Sstevel@tonic-gate 			if (err) {
2497c478bd9Sstevel@tonic-gate 				DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
2507c478bd9Sstevel@tonic-gate 				return (-1);
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		return (-1);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2597c478bd9Sstevel@tonic-gate int
dr_pre_detach_io(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)2607c478bd9Sstevel@tonic-gate dr_pre_detach_io(dr_handle_t *hp,
2617c478bd9Sstevel@tonic-gate 	dr_common_unit_t **devlist, int devnum)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	int		d;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	ASSERT(devnum > 0);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2687c478bd9Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[d];
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS unconfigure %s", cp->sbdev_path);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	return (0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2777c478bd9Sstevel@tonic-gate int
dr_post_detach_io(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)2787c478bd9Sstevel@tonic-gate dr_post_detach_io(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	register int	i;
2817c478bd9Sstevel@tonic-gate 	int		rv = 0;
2827c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_io";
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	ASSERT(devnum > 0);
2857c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
2867c478bd9Sstevel@tonic-gate 		dr_common_unit_t	*cp = devlist[i];
2877c478bd9Sstevel@tonic-gate 		if (cp->sbdev_error != NULL) {
2887c478bd9Sstevel@tonic-gate 			PR_IO("%s: Failed\n", f);
2897c478bd9Sstevel@tonic-gate 			rv = -1;
2907c478bd9Sstevel@tonic-gate 			break;
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	return (rv);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
29625e8c5aaSvikram static void
dr_get_comp_cond(dr_io_unit_t * ip,dev_info_t * dip)29725e8c5aaSvikram dr_get_comp_cond(dr_io_unit_t *ip, dev_info_t *dip)
29825e8c5aaSvikram {
29925e8c5aaSvikram 	if (dip == NULL) {
30025e8c5aaSvikram 		ip->sbi_cm.sbdev_cond = SBD_COND_UNKNOWN;
30125e8c5aaSvikram 		return;
30225e8c5aaSvikram 	}
30325e8c5aaSvikram 
30425e8c5aaSvikram 	if (DEVI(dip)->devi_flags & DEVI_RETIRED) {
30525e8c5aaSvikram 		ip->sbi_cm.sbdev_cond = SBD_COND_FAILED;
30625e8c5aaSvikram 		return;
30725e8c5aaSvikram 	}
30825e8c5aaSvikram 
30925e8c5aaSvikram 	if (DR_DEV_IS_ATTACHED(&ip->sbi_cm)) {
31025e8c5aaSvikram 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
31125e8c5aaSvikram 	} else if (DR_DEV_IS_PRESENT(&ip->sbi_cm)) {
31225e8c5aaSvikram 		ip->sbi_cm.sbdev_cond = SBD_COND_OK;
31325e8c5aaSvikram 	}
31425e8c5aaSvikram }
31525e8c5aaSvikram 
3167c478bd9Sstevel@tonic-gate int
dr_io_status(dr_handle_t * hp,dr_devset_t devset,sbd_dev_stat_t * dsp)3177c478bd9Sstevel@tonic-gate dr_io_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int		i, ix;
3207c478bd9Sstevel@tonic-gate 	dr_board_t	*bp;
3217c478bd9Sstevel@tonic-gate 	sbd_io_stat_t	*isp;
3227c478bd9Sstevel@tonic-gate 	dr_io_unit_t	*ip;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	bp = hp->h_bd;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	for (i = ix = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
3327c478bd9Sstevel@tonic-gate 		drmachid_t	 id;
3337c478bd9Sstevel@tonic-gate 		dev_info_t	*dip;
3347c478bd9Sstevel@tonic-gate 		sbd_error_t	*err;
3357c478bd9Sstevel@tonic-gate 		drmach_status_t	 pstat;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i) == 0)
3387c478bd9Sstevel@tonic-gate 			continue;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		if (ip->sbi_cm.sbdev_state == DR_STATE_EMPTY) {
3437c478bd9Sstevel@tonic-gate 			/* present, but not fully initialized */
3447c478bd9Sstevel@tonic-gate 			continue;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		id = ip->sbi_cm.sbdev_id;
3487c478bd9Sstevel@tonic-gate 		if (id == (drmachid_t)0)
3497c478bd9Sstevel@tonic-gate 			continue;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		err = drmach_status(ip->sbi_cm.sbdev_id, &pstat);
3527c478bd9Sstevel@tonic-gate 		if (err) {
3537c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&ip->sbi_cm.sbdev_error, &err);
3547c478bd9Sstevel@tonic-gate 			return (-1);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 
35725e8c5aaSvikram 		dip = NULL;
35825e8c5aaSvikram 		err = drmach_get_dip(id, &dip);
35925e8c5aaSvikram 		if (err) {
36025e8c5aaSvikram 			/* catch this in debug kernels */
36125e8c5aaSvikram 			ASSERT(0);
36225e8c5aaSvikram 
36325e8c5aaSvikram 			sbd_err_clear(&err);
36425e8c5aaSvikram 			continue;
36525e8c5aaSvikram 		}
36625e8c5aaSvikram 
3677c478bd9Sstevel@tonic-gate 		isp = &dsp->d_io;
3687c478bd9Sstevel@tonic-gate 		bzero((caddr_t)isp, sizeof (*isp));
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		isp->is_cm.c_id.c_type = ip->sbi_cm.sbdev_type;
3717c478bd9Sstevel@tonic-gate 		isp->is_cm.c_id.c_unit = ip->sbi_cm.sbdev_unum;
37207d06da5SSurya Prakki 		(void) strncpy(isp->is_cm.c_id.c_name, pstat.type,
37307d06da5SSurya Prakki 		    sizeof (isp->is_cm.c_id.c_name));
37425e8c5aaSvikram 
37525e8c5aaSvikram 		dr_get_comp_cond(ip, dip);
3767c478bd9Sstevel@tonic-gate 		isp->is_cm.c_cond = ip->sbi_cm.sbdev_cond;
3777c478bd9Sstevel@tonic-gate 		isp->is_cm.c_busy = ip->sbi_cm.sbdev_busy | pstat.busy;
3787c478bd9Sstevel@tonic-gate 		isp->is_cm.c_time = ip->sbi_cm.sbdev_time;
3797c478bd9Sstevel@tonic-gate 		isp->is_cm.c_ostate = ip->sbi_cm.sbdev_ostate;
3807c478bd9Sstevel@tonic-gate 		isp->is_cm.c_sflags = 0;
3817c478bd9Sstevel@tonic-gate 
38225e8c5aaSvikram 		if (dip == NULL) {
3837c478bd9Sstevel@tonic-gate 			isp->is_pathname[0] = '\0';
3847c478bd9Sstevel@tonic-gate 			isp->is_referenced = 0;
3857c478bd9Sstevel@tonic-gate 			isp->is_unsafe_count = 0;
3867c478bd9Sstevel@tonic-gate 		} else {
3877c478bd9Sstevel@tonic-gate 			int		refcount = 0, idx = 0;
3887c478bd9Sstevel@tonic-gate 			uint64_t	unsafe_devs[SBD_MAX_UNSAFE];
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			ASSERT(e_ddi_branch_held(dip));
3917c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(dip, isp->is_pathname);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 			/* check reference and unsafe counts on devices */
3947c478bd9Sstevel@tonic-gate 			isp->is_unsafe_count = 0;
3957c478bd9Sstevel@tonic-gate 			dr_check_devices(dip, &refcount, hp, unsafe_devs,
396*df3cd224SVijay S Balakrishna 			    &idx, SBD_MAX_UNSAFE, NULL);
3977c478bd9Sstevel@tonic-gate 			while (idx > 0) {
3987c478bd9Sstevel@tonic-gate 				isp->is_unsafe_list[idx-1] = unsafe_devs[idx-1];
3997c478bd9Sstevel@tonic-gate 				--idx;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 			isp->is_referenced = (refcount == 0) ? 0 : 1;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 			hp->h_err = NULL;
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 		ix++;
4077c478bd9Sstevel@tonic-gate 		dsp++;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (ix);
4117c478bd9Sstevel@tonic-gate }
412