xref: /illumos-gate/usr/src/uts/sun4u/ngdr/io/dr.c (revision c3937c08)
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 /*
2356f33205SJonathan Adams  * 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  * PIM-DR layer of DR driver.  Provides interface between user
297c478bd9Sstevel@tonic-gate  * level applications and the PSM-DR layer.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/note.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
387c478bd9Sstevel@tonic-gate #include <sys/devops.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/poll.h>
417c478bd9Sstevel@tonic-gate #include <sys/conf.h>
427c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
477c478bd9Sstevel@tonic-gate #include <sys/processor.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
557c478bd9Sstevel@tonic-gate #include <sys/promif.h>
567c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/dr.h>
597c478bd9Sstevel@tonic-gate #include <sys/drmach.h>
607c478bd9Sstevel@tonic-gate #include <sys/dr_util.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern int		 nulldev();
637c478bd9Sstevel@tonic-gate extern int		 nodev();
647c478bd9Sstevel@tonic-gate extern struct memlist	*phys_install;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifdef DEBUG
677c478bd9Sstevel@tonic-gate uint_t	dr_debug = 0;			/* dr.h for bit values */
687c478bd9Sstevel@tonic-gate #endif /* DEBUG */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * NOTE: state_str, nt_str and SBD_CMD_STR are only used in a debug
727c478bd9Sstevel@tonic-gate  * kernel.  They are, however, referenced during both debug and non-debug
737c478bd9Sstevel@tonic-gate  * compiles.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static char *state_str[] = {
777c478bd9Sstevel@tonic-gate 	"EMPTY", "OCCUPIED", "CONNECTED", "UNCONFIGURED",
787c478bd9Sstevel@tonic-gate 	"PARTIAL", "CONFIGURED", "RELEASE", "UNREFERENCED",
797c478bd9Sstevel@tonic-gate 	"FATAL"
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SBD_CMD_STR(c) \
837c478bd9Sstevel@tonic-gate 	(((c) == SBD_CMD_ASSIGN)	? "ASSIGN"	: \
847c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_UNASSIGN)	? "UNASSIGN"	: \
857c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_POWERON)	? "POWERON"	: \
867c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_POWEROFF)	? "POWEROFF"	: \
877c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_TEST)		? "TEST"	: \
887c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_CONNECT)	? "CONNECT"	: \
897c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_DISCONNECT)	? "DISCONNECT"	: \
907c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_CONFIGURE)	? "CONFIGURE"	: \
917c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_UNCONFIGURE)	? "UNCONFIGURE"	: \
927c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_GETNCM)		? "GETNCM"	: \
937c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_PASSTHRU)	? "PASSTHRU"	: \
947c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_STATUS)		? "STATUS"	: "unknown")
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	DR_GET_BOARD_DEVUNIT(sb, ut, un) (&((sb)->b_dev[NIX(ut)][un]))
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	DR_MAKE_MINOR(i, b)	(((i) << 16) | (b))
997c478bd9Sstevel@tonic-gate #define	DR_MINOR2INST(m)	(((m) >> 16) & 0xffff)
1007c478bd9Sstevel@tonic-gate #define	DR_MINOR2BNUM(m)	((m) & 0xffff)
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
1037c478bd9Sstevel@tonic-gate static char *dr_ie_fmt = "dr.c %d";
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* struct for drmach device name to sbd_comp_type_t mapping */
1067c478bd9Sstevel@tonic-gate typedef	struct {
1077c478bd9Sstevel@tonic-gate 	char		*s_devtype;
1087c478bd9Sstevel@tonic-gate 	sbd_comp_type_t	s_nodetype;
1097c478bd9Sstevel@tonic-gate } dr_devname_t;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* struct to map starfire device attributes - name:sbd_comp_type_t */
1127c478bd9Sstevel@tonic-gate static	dr_devname_t	dr_devattr[] = {
1137c478bd9Sstevel@tonic-gate 	{ DRMACH_DEVTYPE_MEM,	SBD_COMP_MEM },
1147c478bd9Sstevel@tonic-gate 	{ DRMACH_DEVTYPE_CPU,	SBD_COMP_CPU },
1157c478bd9Sstevel@tonic-gate 	{ DRMACH_DEVTYPE_PCI,	SBD_COMP_IO },
11625cf1a30Sjl #if defined(DRMACH_DEVTYPE_SBUS)
1177c478bd9Sstevel@tonic-gate 	{ DRMACH_DEVTYPE_SBUS,	SBD_COMP_IO },
11825cf1a30Sjl #endif
1197c478bd9Sstevel@tonic-gate #if defined(DRMACH_DEVTYPE_WCI)
1207c478bd9Sstevel@tonic-gate 	{ DRMACH_DEVTYPE_WCI,	SBD_COMP_IO },
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 	/* last s_devtype must be NULL, s_nodetype must be SBD_COMP_UNKNOWN */
1237c478bd9Sstevel@tonic-gate 	{ NULL,			SBD_COMP_UNKNOWN }
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Per instance soft-state structure.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate typedef struct dr_softstate {
1307c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
1317c478bd9Sstevel@tonic-gate 	dr_board_t	*boards;
1327c478bd9Sstevel@tonic-gate 	kmutex_t	i_lock;
1337c478bd9Sstevel@tonic-gate 	int		 dr_initialized;
1347c478bd9Sstevel@tonic-gate } dr_softstate_t;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * dr Global data elements
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate struct dr_global {
1407c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;	/* pointer to initialize soft state */
1417c478bd9Sstevel@tonic-gate 	kmutex_t	lock;
1427c478bd9Sstevel@tonic-gate } dr_g;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate dr_unsafe_devs_t	dr_unsafe_devs;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Table of known passthru commands.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate struct {
1517c478bd9Sstevel@tonic-gate 	char	*pt_name;
1527c478bd9Sstevel@tonic-gate 	int	(*pt_func)(dr_handle_t *);
1537c478bd9Sstevel@tonic-gate } pt_arr[] = {
1547c478bd9Sstevel@tonic-gate 	"quiesce",		dr_pt_test_suspend,
1557c478bd9Sstevel@tonic-gate };
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int dr_modunload_okay = 0;		/* set to non-zero to allow unload */
1587c478bd9Sstevel@tonic-gate 
1598682d1efSRichard Lowe static int	dr_dev_type_to_nt(char *);
1608682d1efSRichard Lowe 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * State transition table.  States valid transitions for "board" state.
1637c478bd9Sstevel@tonic-gate  * Recall that non-zero return value terminates operation, however
1647c478bd9Sstevel@tonic-gate  * the herrno value is what really indicates an error , if any.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate static int
_cmd2index(int c)1677c478bd9Sstevel@tonic-gate _cmd2index(int c)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Translate DR CMD to index into dr_state_transition.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	switch (c) {
1737c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:		return (0);
1747c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:	return (1);
1757c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:		return (2);
1767c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:	return (3);
1777c478bd9Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:		return (4);
1787c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:		return (5);
1797c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWERON:		return (6);
1807c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:		return (7);
1817c478bd9Sstevel@tonic-gate 	case SBD_CMD_TEST:		return (8);
1827c478bd9Sstevel@tonic-gate 	default:			return (-1);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #define	CMD2INDEX(c)	_cmd2index(c)
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static struct dr_state_trans {
1897c478bd9Sstevel@tonic-gate 	int	x_cmd;
1907c478bd9Sstevel@tonic-gate 	struct {
1917c478bd9Sstevel@tonic-gate 		int	x_rv;		/* return value of pre_op */
1927c478bd9Sstevel@tonic-gate 		int	x_err;		/* error, if any */
1937c478bd9Sstevel@tonic-gate 	} x_op[DR_STATE_MAX];
1947c478bd9Sstevel@tonic-gate } dr_state_transition[] = {
1957c478bd9Sstevel@tonic-gate 	{ SBD_CMD_CONNECT,
1967c478bd9Sstevel@tonic-gate 		{
1977c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
1987c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
1997c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2007c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2017c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2027c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2037c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2047c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2057c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	},
2087c478bd9Sstevel@tonic-gate 	{ SBD_CMD_DISCONNECT,
2097c478bd9Sstevel@tonic-gate 		{
2107c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* empty */
2117c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
2127c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* connected */
2137c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* unconfigured */
2147c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2157c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2167c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2177c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2187c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	},
2217c478bd9Sstevel@tonic-gate 	{ SBD_CMD_CONFIGURE,
2227c478bd9Sstevel@tonic-gate 		{
2237c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* empty */
2247c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* occupied */
2257c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* connected */
2267c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* unconfigured */
2277c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* partial */
2287c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* configured */
2297c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2307c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2317c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	},
2347c478bd9Sstevel@tonic-gate 	{ SBD_CMD_UNCONFIGURE,
2357c478bd9Sstevel@tonic-gate 		{
2367c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* empty */
2377c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* occupied */
2387c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2397c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2407c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* partial */
2417c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* configured */
2427c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* release */
2437c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* unreferenced */
2447c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	},
2477c478bd9Sstevel@tonic-gate 	{ SBD_CMD_ASSIGN,
2487c478bd9Sstevel@tonic-gate 		{
2497c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
2507c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
2517c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2527c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2537c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2547c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2557c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2567c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2577c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	},
2607c478bd9Sstevel@tonic-gate 	{ SBD_CMD_UNASSIGN,
2617c478bd9Sstevel@tonic-gate 		{
2627c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
2637c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
2647c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2657c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2667c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2677c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2687c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2697c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2707c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	},
2737c478bd9Sstevel@tonic-gate 	{ SBD_CMD_POWERON,
2747c478bd9Sstevel@tonic-gate 		{
2757c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
2767c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
2777c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2787c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2797c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2807c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2817c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2827c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2837c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	},
2867c478bd9Sstevel@tonic-gate 	{ SBD_CMD_POWEROFF,
2877c478bd9Sstevel@tonic-gate 		{
2887c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
2897c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
2907c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
2917c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
2927c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
2937c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
2947c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
2957c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
2967c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	},
2997c478bd9Sstevel@tonic-gate 	{ SBD_CMD_TEST,
3007c478bd9Sstevel@tonic-gate 		{
3017c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* empty */
3027c478bd9Sstevel@tonic-gate 			{ 0, 0 },			/* occupied */
3037c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* connected */
3047c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unconfigured */
3057c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* partial */
3067c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* configured */
3077c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* release */
3087c478bd9Sstevel@tonic-gate 			{ -1, ESBD_STATE },		/* unreferenced */
3097c478bd9Sstevel@tonic-gate 			{ -1, ESBD_FATAL_STATE },	/* fatal */
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	},
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Global R/W lock to synchronize access across
3167c478bd9Sstevel@tonic-gate  * multiple boards.  Users wanting multi-board access
3177c478bd9Sstevel@tonic-gate  * must grab WRITE lock, others must grab READ lock.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate krwlock_t	dr_grwlock;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Head of the boardlist used as a reference point for
3237c478bd9Sstevel@tonic-gate  * locating board structs.
3247c478bd9Sstevel@tonic-gate  * TODO: eliminate dr_boardlist
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate dr_board_t	*dr_boardlist;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * DR support functions.
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate static dr_devset_t	dr_dev2devset(sbd_comp_id_t *cid);
3327c478bd9Sstevel@tonic-gate static int		dr_check_transition(dr_board_t *bp,
3337c478bd9Sstevel@tonic-gate 					dr_devset_t *devsetp,
3347c478bd9Sstevel@tonic-gate 					struct dr_state_trans *transp,
3357c478bd9Sstevel@tonic-gate 					int cmd);
3367c478bd9Sstevel@tonic-gate static int		dr_check_unit_attached(dr_common_unit_t *dp);
3377c478bd9Sstevel@tonic-gate static sbd_error_t	*dr_init_devlists(dr_board_t *bp);
3387c478bd9Sstevel@tonic-gate static void		dr_board_discovery(dr_board_t *bp);
3397c478bd9Sstevel@tonic-gate static int		dr_board_init(dr_board_t *bp, dev_info_t *dip,
3407c478bd9Sstevel@tonic-gate 					int bd);
3417c478bd9Sstevel@tonic-gate static void		dr_board_destroy(dr_board_t *bp);
3427c478bd9Sstevel@tonic-gate static void		dr_board_transition(dr_board_t *bp, dr_state_t st);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * DR driver (DDI) entry points.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate static int	dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
3487c478bd9Sstevel@tonic-gate 				void *arg, void **result);
3497c478bd9Sstevel@tonic-gate static int	dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
3507c478bd9Sstevel@tonic-gate static int	dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
3517c478bd9Sstevel@tonic-gate static int	dr_probe(dev_info_t *dip);
3527c478bd9Sstevel@tonic-gate static int	dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
3537c478bd9Sstevel@tonic-gate 				cred_t *cred_p, int *rval_p);
3547c478bd9Sstevel@tonic-gate static int	dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
3557c478bd9Sstevel@tonic-gate static int	dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * DR command processing operations.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static int	dr_copyin_iocmd(dr_handle_t *hp);
3627c478bd9Sstevel@tonic-gate static int	dr_copyout_iocmd(dr_handle_t *hp);
3637c478bd9Sstevel@tonic-gate static int	dr_copyout_errs(dr_handle_t *hp);
3647c478bd9Sstevel@tonic-gate static int	dr_pre_op(dr_handle_t *hp);
3657c478bd9Sstevel@tonic-gate static int	dr_post_op(dr_handle_t *hp);
3667c478bd9Sstevel@tonic-gate static int	dr_exec_op(dr_handle_t *hp);
3677c478bd9Sstevel@tonic-gate static void	dr_assign_board(dr_handle_t *hp);
3687c478bd9Sstevel@tonic-gate static void	dr_unassign_board(dr_handle_t *hp);
3697c478bd9Sstevel@tonic-gate static void	dr_connect(dr_handle_t *hp);
3707c478bd9Sstevel@tonic-gate static int	dr_disconnect(dr_handle_t *hp);
3717c478bd9Sstevel@tonic-gate static void	dr_dev_configure(dr_handle_t *hp);
3727c478bd9Sstevel@tonic-gate static void	dr_dev_release(dr_handle_t *hp);
3737c478bd9Sstevel@tonic-gate static int	dr_dev_unconfigure(dr_handle_t *hp);
3747c478bd9Sstevel@tonic-gate static void	dr_dev_cancel(dr_handle_t *hp);
3757c478bd9Sstevel@tonic-gate static int	dr_dev_status(dr_handle_t *hp);
3767c478bd9Sstevel@tonic-gate static int	dr_get_ncm(dr_handle_t *hp);
3777c478bd9Sstevel@tonic-gate static int	dr_pt_ioctl(dr_handle_t *hp);
3787c478bd9Sstevel@tonic-gate static void	dr_poweron_board(dr_handle_t *hp);
3797c478bd9Sstevel@tonic-gate static void	dr_poweroff_board(dr_handle_t *hp);
3807c478bd9Sstevel@tonic-gate static void	dr_test_board(dr_handle_t *hp);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * Autoconfiguration data structures
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate struct cb_ops dr_cb_ops = {
3897c478bd9Sstevel@tonic-gate 	dr_open,	/* open */
3907c478bd9Sstevel@tonic-gate 	dr_close,	/* close */
3917c478bd9Sstevel@tonic-gate 	nodev,		/* strategy */
3927c478bd9Sstevel@tonic-gate 	nodev,		/* print */
3937c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
3947c478bd9Sstevel@tonic-gate 	nodev,		/* read */
3957c478bd9Sstevel@tonic-gate 	nodev,		/* write */
3967c478bd9Sstevel@tonic-gate 	dr_ioctl,	/* ioctl */
3977c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
3987c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
3997c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
4007c478bd9Sstevel@tonic-gate 	nochpoll,	/* chpoll */
4017c478bd9Sstevel@tonic-gate 	ddi_prop_op,	/* cb_prop_op */
4027c478bd9Sstevel@tonic-gate 	NULL,		/* struct streamtab */
4037c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_MTSAFE,	/* compatibility flags */
4047c478bd9Sstevel@tonic-gate 	CB_REV,		/* Rev */
4057c478bd9Sstevel@tonic-gate 	nodev,		/* cb_aread */
4067c478bd9Sstevel@tonic-gate 	nodev		/* cb_awrite */
4077c478bd9Sstevel@tonic-gate };
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate struct dev_ops dr_dev_ops = {
4107c478bd9Sstevel@tonic-gate 	DEVO_REV,	/* build version */
4117c478bd9Sstevel@tonic-gate 	0,		/* dev ref count */
4127c478bd9Sstevel@tonic-gate 	dr_getinfo,	/* getinfo */
4137c478bd9Sstevel@tonic-gate 	nulldev,	/* identify */
4147c478bd9Sstevel@tonic-gate 	dr_probe,	/* probe */
4157c478bd9Sstevel@tonic-gate 	dr_attach,	/* attach */
4167c478bd9Sstevel@tonic-gate 	dr_detach,	/* detach */
4177c478bd9Sstevel@tonic-gate 	nodev,		/* reset */
4187c478bd9Sstevel@tonic-gate 	&dr_cb_ops,	/* cb_ops */
4197c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL, /* bus ops */
42019397407SSherry Moore 	NULL,		/* power */
42119397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
4227c478bd9Sstevel@tonic-gate };
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
4277c478bd9Sstevel@tonic-gate 	&mod_driverops,
42819397407SSherry Moore 	"Dynamic Reconfiguration",
4297c478bd9Sstevel@tonic-gate 	&dr_dev_ops
4307c478bd9Sstevel@tonic-gate };
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
4337c478bd9Sstevel@tonic-gate 	MODREV_1,
4347c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
4357c478bd9Sstevel@tonic-gate 	NULL
4367c478bd9Sstevel@tonic-gate };
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Driver entry points.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate int
_init(void)4427c478bd9Sstevel@tonic-gate _init(void)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int	err;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * If you need to support multiple nodes (instances), then
4487c478bd9Sstevel@tonic-gate 	 * whatever the maximum number of supported nodes is would
4497c478bd9Sstevel@tonic-gate 	 * need to passed as the third parameter to ddi_soft_state_init().
4507c478bd9Sstevel@tonic-gate 	 * Alternative would be to dynamically fini and re-init the
4517c478bd9Sstevel@tonic-gate 	 * soft state structure each time a node is attached.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	err = ddi_soft_state_init((void **)&dr_g.softsp,
45419397407SSherry Moore 	    sizeof (dr_softstate_t), 1);
4557c478bd9Sstevel@tonic-gate 	if (err)
4567c478bd9Sstevel@tonic-gate 		return (err);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	mutex_init(&dr_g.lock, NULL, MUTEX_DRIVER, NULL);
4597c478bd9Sstevel@tonic-gate 	rw_init(&dr_grwlock, NULL, RW_DEFAULT, NULL);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate int
_fini(void)4657c478bd9Sstevel@tonic-gate _fini(void)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	int	err;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
4707c478bd9Sstevel@tonic-gate 		return (err);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	mutex_destroy(&dr_g.lock);
4737c478bd9Sstevel@tonic-gate 	rw_destroy(&dr_grwlock);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini((void **)&dr_g.softsp);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4817c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4877c478bd9Sstevel@tonic-gate static int
dr_open(dev_t * dev,int flag,int otyp,cred_t * cred_p)4887c478bd9Sstevel@tonic-gate dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	int		 instance;
4917c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;
4927c478bd9Sstevel@tonic-gate 	dr_board_t	*bp;
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * Don't open unless we've attached.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	instance = DR_MINOR2INST(getminor(*dev));
4977c478bd9Sstevel@tonic-gate 	softsp = ddi_get_soft_state(dr_g.softsp, instance);
4987c478bd9Sstevel@tonic-gate 	if (softsp == NULL)
4997c478bd9Sstevel@tonic-gate 		return (ENXIO);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	mutex_enter(&softsp->i_lock);
5027c478bd9Sstevel@tonic-gate 	if (!softsp->dr_initialized) {
5037c478bd9Sstevel@tonic-gate 		int		 bd;
5047c478bd9Sstevel@tonic-gate 		int		 rv = 0;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		bp = softsp->boards;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/* initialize each array element */
5097c478bd9Sstevel@tonic-gate 		for (bd = 0; bd < MAX_BOARDS; bd++, bp++) {
5107c478bd9Sstevel@tonic-gate 			rv = dr_board_init(bp, softsp->dip, bd);
5117c478bd9Sstevel@tonic-gate 			if (rv)
5127c478bd9Sstevel@tonic-gate 				break;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		if (rv == 0) {
5167c478bd9Sstevel@tonic-gate 			softsp->dr_initialized = 1;
5177c478bd9Sstevel@tonic-gate 		} else {
5187c478bd9Sstevel@tonic-gate 			/* destroy elements initialized thus far */
5197c478bd9Sstevel@tonic-gate 			while (--bp >= softsp->boards)
5207c478bd9Sstevel@tonic-gate 				dr_board_destroy(bp);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 			/* TODO: should this be another errno val ? */
5247c478bd9Sstevel@tonic-gate 			mutex_exit(&softsp->i_lock);
5257c478bd9Sstevel@tonic-gate 			return (ENXIO);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	mutex_exit(&softsp->i_lock);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	bp = &softsp->boards[DR_MINOR2BNUM(getminor(*dev))];
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * prevent opening of a dyn-ap for a board
5347c478bd9Sstevel@tonic-gate 	 * that does not exist
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if (!bp->b_assigned) {
5377c478bd9Sstevel@tonic-gate 		if (drmach_board_lookup(bp->b_num, &bp->b_id) != 0)
5387c478bd9Sstevel@tonic-gate 			return (ENODEV);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	return (0);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5457c478bd9Sstevel@tonic-gate static int
dr_close(dev_t dev,int flag,int otyp,cred_t * cred_p)5467c478bd9Sstevel@tonic-gate dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	return (0);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate /*
55225cf1a30Sjl  * Enable/disable DR features.
5537c478bd9Sstevel@tonic-gate  */
5547c478bd9Sstevel@tonic-gate int dr_enable = 1;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
5577c478bd9Sstevel@tonic-gate static int
dr_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)5587c478bd9Sstevel@tonic-gate dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
559*c3937c08SToomas Soome     cred_t *cred_p, int *rval_p)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int		rv = 0;
5627c478bd9Sstevel@tonic-gate 	int		instance;
5637c478bd9Sstevel@tonic-gate 	int		bd;
5647c478bd9Sstevel@tonic-gate 	dr_handle_t	*hp;
5657c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;
5667c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_ioctl";
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	instance = DR_MINOR2INST(getminor(dev));
5717c478bd9Sstevel@tonic-gate 	softsp = ddi_get_soft_state(dr_g.softsp, instance);
5727c478bd9Sstevel@tonic-gate 	if (softsp == NULL) {
5737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "dr%d: module not yet attached", instance);
5747c478bd9Sstevel@tonic-gate 		return (ENXIO);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (!dr_enable) {
5787c478bd9Sstevel@tonic-gate 		switch (cmd) {
5797c478bd9Sstevel@tonic-gate 			case SBD_CMD_STATUS:
5807c478bd9Sstevel@tonic-gate 			case SBD_CMD_GETNCM:
5817c478bd9Sstevel@tonic-gate 			case SBD_CMD_PASSTHRU:
5827c478bd9Sstevel@tonic-gate 				break;
5837c478bd9Sstevel@tonic-gate 			default:
5847c478bd9Sstevel@tonic-gate 				return (ENOTSUP);
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	bd = DR_MINOR2BNUM(getminor(dev));
5897c478bd9Sstevel@tonic-gate 	if (bd >= MAX_BOARDS)
5907c478bd9Sstevel@tonic-gate 		return (ENXIO);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/* get and initialize storage for new handle */
5937c478bd9Sstevel@tonic-gate 	hp = GETSTRUCT(dr_handle_t, 1);
5947c478bd9Sstevel@tonic-gate 	hp->h_bd = &softsp->boards[bd];
5957c478bd9Sstevel@tonic-gate 	hp->h_err = NULL;
5967c478bd9Sstevel@tonic-gate 	hp->h_dev = getminor(dev);
5977c478bd9Sstevel@tonic-gate 	hp->h_cmd = cmd;
5987c478bd9Sstevel@tonic-gate 	hp->h_mode = mode;
5997c478bd9Sstevel@tonic-gate 	hp->h_iap = (sbd_ioctl_arg_t *)arg;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/* copy sbd command into handle */
6027c478bd9Sstevel@tonic-gate 	rv = dr_copyin_iocmd(hp);
6037c478bd9Sstevel@tonic-gate 	if (rv) {
6047c478bd9Sstevel@tonic-gate 		FREESTRUCT(hp, dr_handle_t, 1);
6057c478bd9Sstevel@tonic-gate 		return (EINVAL);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* translate canonical name to component type */
6097c478bd9Sstevel@tonic-gate 	if (hp->h_sbdcmd.cmd_cm.c_id.c_name[0] != '\0') {
6107c478bd9Sstevel@tonic-gate 		hp->h_sbdcmd.cmd_cm.c_id.c_type =
61119397407SSherry Moore 		    dr_dev_type_to_nt(hp->h_sbdcmd.cmd_cm.c_id.c_name);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		PR_ALL("%s: c_name = %s, c_type = %d\n",
61419397407SSherry Moore 		    f,
61519397407SSherry Moore 		    hp->h_sbdcmd.cmd_cm.c_id.c_name,
61619397407SSherry Moore 		    hp->h_sbdcmd.cmd_cm.c_id.c_type);
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		/*EMPTY*/
6197c478bd9Sstevel@tonic-gate 		PR_ALL("%s: c_name is NULL\n", f);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* determine scope of operation */
6237c478bd9Sstevel@tonic-gate 	hp->h_devset = dr_dev2devset(&hp->h_sbdcmd.cmd_cm.c_id);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
6267c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
6277c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
6287c478bd9Sstevel@tonic-gate 		/* no locks needed for these commands */
6297c478bd9Sstevel@tonic-gate 		break;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	default:
6327c478bd9Sstevel@tonic-gate 		rw_enter(&dr_grwlock, RW_WRITER);
6337c478bd9Sstevel@tonic-gate 		mutex_enter(&hp->h_bd->b_lock);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		/*
6367c478bd9Sstevel@tonic-gate 		 * If we're dealing with memory at all, then we have
6377c478bd9Sstevel@tonic-gate 		 * to keep the "exclusive" global lock held.  This is
6387c478bd9Sstevel@tonic-gate 		 * necessary since we will probably need to look at
6397c478bd9Sstevel@tonic-gate 		 * multiple board structs.  Otherwise, we only have
6407c478bd9Sstevel@tonic-gate 		 * to deal with the board in question and so can drop
6417c478bd9Sstevel@tonic-gate 		 * the global lock to "shared".
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		rv = DEVSET_IN_SET(hp->h_devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
6447c478bd9Sstevel@tonic-gate 		if (rv == 0)
6457c478bd9Sstevel@tonic-gate 			rw_downgrade(&dr_grwlock);
6467c478bd9Sstevel@tonic-gate 		break;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 	rv = 0;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (rv == 0)
6517c478bd9Sstevel@tonic-gate 		rv = dr_pre_op(hp);
6527c478bd9Sstevel@tonic-gate 	if (rv == 0)
6537c478bd9Sstevel@tonic-gate 		rv = dr_exec_op(hp);
6547c478bd9Sstevel@tonic-gate 	if (rv == 0)
6557c478bd9Sstevel@tonic-gate 		rv = dr_post_op(hp);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (rv == -1)
6587c478bd9Sstevel@tonic-gate 		rv = EIO;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (hp->h_err != NULL)
6617c478bd9Sstevel@tonic-gate 		if (!(rv = dr_copyout_errs(hp)))
6627c478bd9Sstevel@tonic-gate 			rv = EIO;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* undo locking, if any, done before dr_pre_op */
6657c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
6667c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
6677c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
6687c478bd9Sstevel@tonic-gate 		break;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:
6717c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:
6727c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWERON:
6737c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:
6747c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:
6757c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:
6767c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:
6777c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:
6787c478bd9Sstevel@tonic-gate 		/* Board changed state. Log a sysevent. */
6797c478bd9Sstevel@tonic-gate 		if (rv == 0)
6807c478bd9Sstevel@tonic-gate 			(void) drmach_log_sysevent(hp->h_bd->b_num, "",
68119397407SSherry Moore 			    SE_SLEEP, 1);
6827c478bd9Sstevel@tonic-gate 		/* Fall through */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	default:
6857c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->h_bd->b_lock);
6867c478bd9Sstevel@tonic-gate 		rw_exit(&dr_grwlock);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (hp->h_opts.size != 0)
6907c478bd9Sstevel@tonic-gate 		FREESTRUCT(hp->h_opts.copts, char, hp->h_opts.size);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	FREESTRUCT(hp, dr_handle_t, 1);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	return (rv);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6987c478bd9Sstevel@tonic-gate static int
dr_probe(dev_info_t * dip)6997c478bd9Sstevel@tonic-gate dr_probe(dev_info_t *dip)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate static int
dr_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)7057c478bd9Sstevel@tonic-gate dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	int		rv, rv2;
7087c478bd9Sstevel@tonic-gate 	int		bd;
709*c3937c08SToomas Soome 	int		instance;
7107c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
7117c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	switch (cmd) {
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		rw_enter(&dr_grwlock, RW_WRITER);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		rv = ddi_soft_state_zalloc(dr_g.softsp, instance);
7227c478bd9Sstevel@tonic-gate 		if (rv != DDI_SUCCESS) {
7237c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "dr%d: failed to alloc soft-state",
72419397407SSherry Moore 			    instance);
7257c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		/* initialize softstate structure */
7297c478bd9Sstevel@tonic-gate 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
7307c478bd9Sstevel@tonic-gate 		softsp->dip = dip;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		mutex_init(&softsp->i_lock, NULL, MUTEX_DRIVER, NULL);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/* allocate board array (aka boardlist) */
7357c478bd9Sstevel@tonic-gate 		softsp->boards = GETSTRUCT(dr_board_t, MAX_BOARDS);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		/* TODO: eliminate dr_boardlist */
7387c478bd9Sstevel@tonic-gate 		dr_boardlist = softsp->boards;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		/* initialize each array element */
7417c478bd9Sstevel@tonic-gate 		rv = DDI_SUCCESS;
7427c478bd9Sstevel@tonic-gate 		for (bd = 0; bd < MAX_BOARDS; bd++) {
7437c478bd9Sstevel@tonic-gate 			dr_board_t	*bp = &softsp->boards[bd];
7447c478bd9Sstevel@tonic-gate 			char		*p, *name;
7457c478bd9Sstevel@tonic-gate 			int		 l, minor_num;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 			/*
7487c478bd9Sstevel@tonic-gate 			 * initialized board attachment point path
7497c478bd9Sstevel@tonic-gate 			 * (relative to pseudo) in a form immediately
7507c478bd9Sstevel@tonic-gate 			 * reusable as an cfgadm command argument.
7517c478bd9Sstevel@tonic-gate 			 * TODO: clean this up
7527c478bd9Sstevel@tonic-gate 			 */
7537c478bd9Sstevel@tonic-gate 			p = bp->b_path;
7547c478bd9Sstevel@tonic-gate 			l = sizeof (bp->b_path);
7557c478bd9Sstevel@tonic-gate 			(void) snprintf(p, l, "dr@%d:", instance);
7567c478bd9Sstevel@tonic-gate 			while (*p != '\0') {
7577c478bd9Sstevel@tonic-gate 				l--;
7587c478bd9Sstevel@tonic-gate 				p++;
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 			name = p;
7627c478bd9Sstevel@tonic-gate 			err = drmach_board_name(bd, p, l);
7637c478bd9Sstevel@tonic-gate 			if (err) {
7647c478bd9Sstevel@tonic-gate 				sbd_err_clear(&err);
7657c478bd9Sstevel@tonic-gate 				rv = DDI_FAILURE;
7667c478bd9Sstevel@tonic-gate 				break;
7677c478bd9Sstevel@tonic-gate 			}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 			minor_num = DR_MAKE_MINOR(instance, bd);
7707c478bd9Sstevel@tonic-gate 			rv = ddi_create_minor_node(dip, name, S_IFCHR,
771*c3937c08SToomas Soome 			    minor_num, DDI_NT_SBD_ATTACHMENT_POINT, 0);
7727c478bd9Sstevel@tonic-gate 			if (rv != DDI_SUCCESS)
7737c478bd9Sstevel@tonic-gate 				rv = DDI_FAILURE;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if (rv == DDI_SUCCESS) {
7777c478bd9Sstevel@tonic-gate 			/*
7787c478bd9Sstevel@tonic-gate 			 * Announce the node's presence.
7797c478bd9Sstevel@tonic-gate 			 */
7807c478bd9Sstevel@tonic-gate 			ddi_report_dev(dip);
7817c478bd9Sstevel@tonic-gate 		} else {
7827c478bd9Sstevel@tonic-gate 			ddi_remove_minor_node(dip, NULL);
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		/*
7857c478bd9Sstevel@tonic-gate 		 * Init registered unsafe devs.
7867c478bd9Sstevel@tonic-gate 		 */
7877c478bd9Sstevel@tonic-gate 		dr_unsafe_devs.devnames = NULL;
7887c478bd9Sstevel@tonic-gate 		rv2 = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
78919397407SSherry Moore 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
79019397407SSherry Moore 		    "unsupported-io-drivers", &dr_unsafe_devs.devnames,
79119397407SSherry Moore 		    &dr_unsafe_devs.ndevs);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		if (rv2 != DDI_PROP_SUCCESS)
7947c478bd9Sstevel@tonic-gate 			dr_unsafe_devs.ndevs = 0;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		rw_exit(&dr_grwlock);
7977c478bd9Sstevel@tonic-gate 		return (rv);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	default:
8007c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static int
dr_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)8077c478bd9Sstevel@tonic-gate dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8087c478bd9Sstevel@tonic-gate {
809*c3937c08SToomas Soome 	int		instance;
8107c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	switch (cmd) {
8137c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
8147c478bd9Sstevel@tonic-gate 		if (!dr_modunload_okay)
8157c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		rw_enter(&dr_grwlock, RW_WRITER);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
8207c478bd9Sstevel@tonic-gate 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		/* TODO: eliminate dr_boardlist */
8237c478bd9Sstevel@tonic-gate 		ASSERT(softsp->boards == dr_boardlist);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		/* remove all minor nodes */
8267c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		if (softsp->dr_initialized) {
8297c478bd9Sstevel@tonic-gate 			int bd;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			for (bd = 0; bd < MAX_BOARDS; bd++)
8327c478bd9Sstevel@tonic-gate 				dr_board_destroy(&softsp->boards[bd]);
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		FREESTRUCT(softsp->boards, dr_board_t, MAX_BOARDS);
8367c478bd9Sstevel@tonic-gate 		mutex_destroy(&softsp->i_lock);
8377c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(dr_g.softsp, instance);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		rw_exit(&dr_grwlock);
8407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	default:
8437c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static int
dr_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)8497c478bd9Sstevel@tonic-gate dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	dev_t		dev = (dev_t)arg;
8547c478bd9Sstevel@tonic-gate 	int		instance, error;
8557c478bd9Sstevel@tonic-gate 	dr_softstate_t	*softsp;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	*result = NULL;
8587c478bd9Sstevel@tonic-gate 	error = DDI_SUCCESS;
8597c478bd9Sstevel@tonic-gate 	instance = DR_MINOR2INST(getminor(dev));
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	switch (cmd) {
8627c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
8637c478bd9Sstevel@tonic-gate 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
8647c478bd9Sstevel@tonic-gate 		if (softsp == NULL)
8657c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8667c478bd9Sstevel@tonic-gate 		*result = (void *)softsp->dip;
8677c478bd9Sstevel@tonic-gate 		break;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
87004580fdfSmathue 		*result = (void *)(uintptr_t)instance;
8717c478bd9Sstevel@tonic-gate 		break;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	default:
8747c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8757c478bd9Sstevel@tonic-gate 		break;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	return (error);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /*
8827c478bd9Sstevel@tonic-gate  * DR operations.
8837c478bd9Sstevel@tonic-gate  */
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate static int
dr_copyin_iocmd(dr_handle_t * hp)8867c478bd9Sstevel@tonic-gate dr_copyin_iocmd(dr_handle_t *hp)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_copyin_iocmd";
8897c478bd9Sstevel@tonic-gate 	sbd_cmd_t	*scp = &hp->h_sbdcmd;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (hp->h_iap == NULL)
8927c478bd9Sstevel@tonic-gate 		return (EINVAL);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	bzero((caddr_t)scp, sizeof (sbd_cmd_t));
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
8977c478bd9Sstevel@tonic-gate 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
8987c478bd9Sstevel@tonic-gate 		sbd_cmd32_t	scmd32;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&scmd32, sizeof (sbd_cmd32_t));
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)hp->h_iap, (void *)&scmd32,
90319397407SSherry Moore 		    sizeof (sbd_cmd32_t), hp->h_mode)) {
9047c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
90519397407SSherry Moore 			    "%s: (32bit) failed to copyin "
90619397407SSherry Moore 			    "sbdcmd-struct", f);
9077c478bd9Sstevel@tonic-gate 			return (EFAULT);
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 		scp->cmd_cm.c_id.c_type = scmd32.cmd_cm.c_id.c_type;
9107c478bd9Sstevel@tonic-gate 		scp->cmd_cm.c_id.c_unit = scmd32.cmd_cm.c_id.c_unit;
9117c478bd9Sstevel@tonic-gate 		bcopy(&scmd32.cmd_cm.c_id.c_name[0],
91219397407SSherry Moore 		    &scp->cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
9137c478bd9Sstevel@tonic-gate 		scp->cmd_cm.c_flags = scmd32.cmd_cm.c_flags;
9147c478bd9Sstevel@tonic-gate 		scp->cmd_cm.c_len = scmd32.cmd_cm.c_len;
91504580fdfSmathue 		scp->cmd_cm.c_opts = (caddr_t)(uintptr_t)scmd32.cmd_cm.c_opts;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		switch (hp->h_cmd) {
9187c478bd9Sstevel@tonic-gate 		case SBD_CMD_STATUS:
9197c478bd9Sstevel@tonic-gate 			scp->cmd_stat.s_nbytes = scmd32.cmd_stat.s_nbytes;
9207c478bd9Sstevel@tonic-gate 			scp->cmd_stat.s_statp =
92119397407SSherry Moore 			    (caddr_t)(uintptr_t)scmd32.cmd_stat.s_statp;
9227c478bd9Sstevel@tonic-gate 			break;
9237c478bd9Sstevel@tonic-gate 		default:
9247c478bd9Sstevel@tonic-gate 			break;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 	} else
9287c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
9297c478bd9Sstevel@tonic-gate 	if (ddi_copyin((void *)hp->h_iap, (void *)scp,
93019397407SSherry Moore 	    sizeof (sbd_cmd_t), hp->h_mode) != 0) {
9317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
93219397407SSherry Moore 		    "%s: failed to copyin sbdcmd-struct", f);
9337c478bd9Sstevel@tonic-gate 		return (EFAULT);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if ((hp->h_opts.size = scp->cmd_cm.c_len) != 0) {
9377c478bd9Sstevel@tonic-gate 		hp->h_opts.copts = GETSTRUCT(char, scp->cmd_cm.c_len + 1);
9387c478bd9Sstevel@tonic-gate 		++hp->h_opts.size;
9397c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)scp->cmd_cm.c_opts,
94019397407SSherry Moore 		    (void *)hp->h_opts.copts,
94119397407SSherry Moore 		    scp->cmd_cm.c_len, hp->h_mode) != 0) {
9427c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: failed to copyin options", f);
9437c478bd9Sstevel@tonic-gate 			return (EFAULT);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	return (0);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate static int
dr_copyout_iocmd(dr_handle_t * hp)9507c478bd9Sstevel@tonic-gate dr_copyout_iocmd(dr_handle_t *hp)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_copyout_iocmd";
9537c478bd9Sstevel@tonic-gate 	sbd_cmd_t	*scp = &hp->h_sbdcmd;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if (hp->h_iap == NULL)
9567c478bd9Sstevel@tonic-gate 		return (EINVAL);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
9597c478bd9Sstevel@tonic-gate 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
9607c478bd9Sstevel@tonic-gate 		sbd_cmd32_t	scmd32;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		scmd32.cmd_cm.c_id.c_type = scp->cmd_cm.c_id.c_type;
9637c478bd9Sstevel@tonic-gate 		scmd32.cmd_cm.c_id.c_unit = scp->cmd_cm.c_id.c_unit;
9647c478bd9Sstevel@tonic-gate 		bcopy(&scp->cmd_cm.c_id.c_name[0],
96519397407SSherry Moore 		    &scmd32.cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		scmd32.cmd_cm.c_flags = scp->cmd_cm.c_flags;
9687c478bd9Sstevel@tonic-gate 		scmd32.cmd_cm.c_len = scp->cmd_cm.c_len;
96904580fdfSmathue 		scmd32.cmd_cm.c_opts = (caddr32_t)(uintptr_t)scp->cmd_cm.c_opts;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		switch (hp->h_cmd) {
9727c478bd9Sstevel@tonic-gate 		case SBD_CMD_GETNCM:
9737c478bd9Sstevel@tonic-gate 			scmd32.cmd_getncm.g_ncm = scp->cmd_getncm.g_ncm;
9747c478bd9Sstevel@tonic-gate 			break;
9757c478bd9Sstevel@tonic-gate 		default:
9767c478bd9Sstevel@tonic-gate 			break;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		if (ddi_copyout((void *)&scmd32, (void *)hp->h_iap,
98019397407SSherry Moore 		    sizeof (sbd_cmd32_t), hp->h_mode)) {
9817c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
98219397407SSherry Moore 			    "%s: (32bit) failed to copyout "
98319397407SSherry Moore 			    "sbdcmd-struct", f);
9847c478bd9Sstevel@tonic-gate 			return (EFAULT);
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 	} else
9877c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
9887c478bd9Sstevel@tonic-gate 	if (ddi_copyout((void *)scp, (void *)hp->h_iap,
98919397407SSherry Moore 	    sizeof (sbd_cmd_t), hp->h_mode) != 0) {
9907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
99119397407SSherry Moore 		    "%s: failed to copyout sbdcmd-struct", f);
9927c478bd9Sstevel@tonic-gate 		return (EFAULT);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	return (0);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate static int
dr_copyout_errs(dr_handle_t * hp)9997c478bd9Sstevel@tonic-gate dr_copyout_errs(dr_handle_t *hp)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_copyout_errs";
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL)
10047c478bd9Sstevel@tonic-gate 		return (0);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	if (hp->h_err->e_code) {
10077c478bd9Sstevel@tonic-gate 		PR_ALL("%s: error %d %s",
100819397407SSherry Moore 		    f, hp->h_err->e_code, hp->h_err->e_rsc);
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
10127c478bd9Sstevel@tonic-gate 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
10137c478bd9Sstevel@tonic-gate 		sbd_error32_t	*serr32p;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		serr32p = GETSTRUCT(sbd_error32_t, 1);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		serr32p->e_code = hp->h_err->e_code;
10187c478bd9Sstevel@tonic-gate 		bcopy(&hp->h_err->e_rsc[0], &serr32p->e_rsc[0],
101919397407SSherry Moore 		    MAXPATHLEN);
10207c478bd9Sstevel@tonic-gate 		if (ddi_copyout((void *)serr32p,
102119397407SSherry Moore 		    (void *)&((sbd_ioctl_arg32_t *)hp->h_iap)->i_err,
102219397407SSherry Moore 		    sizeof (sbd_error32_t), hp->h_mode)) {
10237c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
102419397407SSherry Moore 			    "%s: (32bit) failed to copyout", f);
10257c478bd9Sstevel@tonic-gate 			return (EFAULT);
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 		FREESTRUCT(serr32p, sbd_error32_t, 1);
10287c478bd9Sstevel@tonic-gate 	} else
10297c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
10307c478bd9Sstevel@tonic-gate 	if (ddi_copyout((void *)hp->h_err,
103119397407SSherry Moore 	    (void *)&hp->h_iap->i_err,
103219397407SSherry Moore 	    sizeof (sbd_error_t), hp->h_mode)) {
10337c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
103419397407SSherry Moore 		    "%s: failed to copyout", f);
10357c478bd9Sstevel@tonic-gate 		return (EFAULT);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	sbd_err_clear(&hp->h_err);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	return (0);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * pre-op entry point must sbd_err_set_c(), if needed.
10467c478bd9Sstevel@tonic-gate  * Return value of non-zero indicates failure.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate static int
dr_pre_op(dr_handle_t * hp)10497c478bd9Sstevel@tonic-gate dr_pre_op(dr_handle_t *hp)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	int		rv = 0, t;
10527c478bd9Sstevel@tonic-gate 	int		cmd, serr = 0;
10537c478bd9Sstevel@tonic-gate 	dr_devset_t	devset;
10547c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
10557c478bd9Sstevel@tonic-gate 	dr_handle_t	*shp = hp;
10567c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_pre_op";
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	cmd = hp->h_cmd;
10597c478bd9Sstevel@tonic-gate 	devset = shp->h_devset;
10607c478bd9Sstevel@tonic-gate 
106104580fdfSmathue 	PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_pre_op(cmd, bp->b_id, &hp->h_opts);
10647c478bd9Sstevel@tonic-gate 	if (hp->h_err != NULL) {
10657c478bd9Sstevel@tonic-gate 		PR_ALL("drmach_pre_op failed for cmd %s(%d)\n",
106619397407SSherry Moore 		    SBD_CMD_STR(cmd), cmd);
10677c478bd9Sstevel@tonic-gate 		return (-1);
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/*
10717c478bd9Sstevel@tonic-gate 	 * Check for valid state transitions.
10727c478bd9Sstevel@tonic-gate 	 */
10737c478bd9Sstevel@tonic-gate 	if ((t = CMD2INDEX(cmd)) != -1) {
10747c478bd9Sstevel@tonic-gate 		struct dr_state_trans	*transp;
10757c478bd9Sstevel@tonic-gate 		int			state_err;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		transp = &dr_state_transition[t];
10787c478bd9Sstevel@tonic-gate 		ASSERT(transp->x_cmd == cmd);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 		state_err = dr_check_transition(bp, &devset, transp, cmd);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		if (state_err < 0) {
10837c478bd9Sstevel@tonic-gate 			/*
10847c478bd9Sstevel@tonic-gate 			 * Invalidate device.
10857c478bd9Sstevel@tonic-gate 			 */
10867c478bd9Sstevel@tonic-gate 			dr_op_err(CE_IGNORE, hp, ESBD_INVAL, NULL);
10877c478bd9Sstevel@tonic-gate 			serr = -1;
10887c478bd9Sstevel@tonic-gate 			PR_ALL("%s: invalid devset (0x%x)\n",
108919397407SSherry Moore 			    f, (uint_t)devset);
10907c478bd9Sstevel@tonic-gate 		} else if (state_err != 0) {
10917c478bd9Sstevel@tonic-gate 			/*
10927c478bd9Sstevel@tonic-gate 			 * State transition is not a valid one.
10937c478bd9Sstevel@tonic-gate 			 */
10947c478bd9Sstevel@tonic-gate 			dr_op_err(CE_IGNORE, hp,
109519397407SSherry Moore 			    transp->x_op[state_err].x_err, NULL);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			serr = transp->x_op[state_err].x_rv;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 			PR_ALL("%s: invalid state %s(%d) for cmd %s(%d)\n",
110019397407SSherry Moore 			    f, state_str[state_err], state_err,
110119397407SSherry Moore 			    SBD_CMD_STR(cmd), cmd);
11027c478bd9Sstevel@tonic-gate 		} else {
11037c478bd9Sstevel@tonic-gate 			shp->h_devset = devset;
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if (serr) {
11087c478bd9Sstevel@tonic-gate 		rv = -1;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	return (rv);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate static int
dr_post_op(dr_handle_t * hp)11157c478bd9Sstevel@tonic-gate dr_post_op(dr_handle_t *hp)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	int		rv = 0;
11187c478bd9Sstevel@tonic-gate 	int		cmd;
11197c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
11207c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_op";
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	cmd = hp->h_cmd;
11237c478bd9Sstevel@tonic-gate 
112404580fdfSmathue 	PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/* errors should have been caught by now */
11277c478bd9Sstevel@tonic-gate 	ASSERT(hp->h_err == NULL);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_post_op(cmd, bp->b_id, &hp->h_opts);
11307c478bd9Sstevel@tonic-gate 	if (hp->h_err != NULL) {
11317c478bd9Sstevel@tonic-gate 		PR_ALL("drmach_post_op failed for cmd %s(%d)\n",
113219397407SSherry Moore 		    SBD_CMD_STR(cmd), cmd);
11337c478bd9Sstevel@tonic-gate 		return (-1);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	switch (cmd) {
11377c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:
11387c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:
11397c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:
11407c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:
11417c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
11427c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
11437c478bd9Sstevel@tonic-gate 		break;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	default:
11467c478bd9Sstevel@tonic-gate 		break;
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	return (rv);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate static int
dr_exec_op(dr_handle_t * hp)11537c478bd9Sstevel@tonic-gate dr_exec_op(dr_handle_t *hp)
11547c478bd9Sstevel@tonic-gate {
11557c478bd9Sstevel@tonic-gate 	int		rv = 0;
11567c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_exec_op";
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/* errors should have been caught by now */
11597c478bd9Sstevel@tonic-gate 	ASSERT(hp->h_err == NULL);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
11627c478bd9Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:
11637c478bd9Sstevel@tonic-gate 		dr_assign_board(hp);
11647c478bd9Sstevel@tonic-gate 		break;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:
11677c478bd9Sstevel@tonic-gate 		dr_unassign_board(hp);
11687c478bd9Sstevel@tonic-gate 		break;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:
11717c478bd9Sstevel@tonic-gate 		dr_poweroff_board(hp);
11727c478bd9Sstevel@tonic-gate 		break;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWERON:
11757c478bd9Sstevel@tonic-gate 		dr_poweron_board(hp);
11767c478bd9Sstevel@tonic-gate 		break;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	case SBD_CMD_TEST:
11797c478bd9Sstevel@tonic-gate 		dr_test_board(hp);
11807c478bd9Sstevel@tonic-gate 		break;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:
11837c478bd9Sstevel@tonic-gate 		dr_connect(hp);
11847c478bd9Sstevel@tonic-gate 		break;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:
11877c478bd9Sstevel@tonic-gate 		dr_dev_configure(hp);
11887c478bd9Sstevel@tonic-gate 		break;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:
11917c478bd9Sstevel@tonic-gate 		dr_dev_release(hp);
11927c478bd9Sstevel@tonic-gate 		if (hp->h_err == NULL)
11937c478bd9Sstevel@tonic-gate 			rv = dr_dev_unconfigure(hp);
11947c478bd9Sstevel@tonic-gate 		else
11957c478bd9Sstevel@tonic-gate 			dr_dev_cancel(hp);
11967c478bd9Sstevel@tonic-gate 		break;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:
11997c478bd9Sstevel@tonic-gate 		rv = dr_disconnect(hp);
12007c478bd9Sstevel@tonic-gate 		break;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
12037c478bd9Sstevel@tonic-gate 		rv = dr_dev_status(hp);
12047c478bd9Sstevel@tonic-gate 		break;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
12077c478bd9Sstevel@tonic-gate 		hp->h_sbdcmd.cmd_getncm.g_ncm = dr_get_ncm(hp);
12087c478bd9Sstevel@tonic-gate 		rv = dr_copyout_iocmd(hp);
12097c478bd9Sstevel@tonic-gate 		break;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
12127c478bd9Sstevel@tonic-gate 		rv = dr_pt_ioctl(hp);
12137c478bd9Sstevel@tonic-gate 		break;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	default:
12167c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
121719397407SSherry Moore 		    "%s: unknown command (%d)",
121819397407SSherry Moore 		    f, hp->h_cmd);
12197c478bd9Sstevel@tonic-gate 		break;
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	if (hp->h_err != NULL) {
12237c478bd9Sstevel@tonic-gate 		rv = -1;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	return (rv);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate static void
dr_assign_board(dr_handle_t * hp)12307c478bd9Sstevel@tonic-gate dr_assign_board(dr_handle_t *hp)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_assign(bp->b_num, &bp->b_id);
12357c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL) {
12367c478bd9Sstevel@tonic-gate 		bp->b_assigned = 1;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate static void
dr_unassign_board(dr_handle_t * hp)12417c478bd9Sstevel@tonic-gate dr_unassign_board(dr_handle_t *hp)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * Block out status during unassign.
12477c478bd9Sstevel@tonic-gate 	 * Not doing cv_wait_sig here as starfire SSP software
12487c478bd9Sstevel@tonic-gate 	 * ignores unassign failure and removes board from
12497c478bd9Sstevel@tonic-gate 	 * domain mask causing system panic.
12507c478bd9Sstevel@tonic-gate 	 * TODO: Change cv_wait to cv_wait_sig when SSP software
12517c478bd9Sstevel@tonic-gate 	 * handles unassign failure.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	dr_lock_status(bp);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_unassign(bp->b_id);
12567c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL) {
12577c478bd9Sstevel@tonic-gate 		/*
12587c478bd9Sstevel@tonic-gate 		 * clear drmachid_t handle; not valid after board unassign
12597c478bd9Sstevel@tonic-gate 		 */
12607c478bd9Sstevel@tonic-gate 		bp->b_id = 0;
12617c478bd9Sstevel@tonic-gate 		bp->b_assigned = 0;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	dr_unlock_status(bp);
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate static void
dr_poweron_board(dr_handle_t * hp)12687c478bd9Sstevel@tonic-gate dr_poweron_board(dr_handle_t *hp)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_poweron(bp->b_id);
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate static void
dr_poweroff_board(dr_handle_t * hp)12767c478bd9Sstevel@tonic-gate dr_poweroff_board(dr_handle_t *hp)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_poweroff(bp->b_id);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate static void
dr_test_board(dr_handle_t * hp)12847c478bd9Sstevel@tonic-gate dr_test_board(dr_handle_t *hp)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
12877c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_test(bp->b_id, &hp->h_opts,
12887c478bd9Sstevel@tonic-gate 	    dr_cmd_flags(hp) & SBD_FLAG_FORCE);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Create and populate the component nodes for a board.  Assumes that the
12937c478bd9Sstevel@tonic-gate  * devlists for the board have been initialized.
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate static void
dr_make_comp_nodes(dr_board_t * bp)1296*c3937c08SToomas Soome dr_make_comp_nodes(dr_board_t *bp)
1297*c3937c08SToomas Soome {
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	int	i;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * Make nodes for the individual components on the board.
13037c478bd9Sstevel@tonic-gate 	 * First we need to initialize memory unit data structures of board
13047c478bd9Sstevel@tonic-gate 	 * structure.
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
13077c478bd9Sstevel@tonic-gate 		dr_mem_unit_t *mp;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, i);
13107c478bd9Sstevel@tonic-gate 		dr_init_mem_unit(mp);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Initialize cpu unit data structures.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
13177c478bd9Sstevel@tonic-gate 		dr_cpu_unit_t *cp;
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		cp = dr_get_cpu_unit(bp, i);
13207c478bd9Sstevel@tonic-gate 		dr_init_cpu_unit(cp);
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	/*
13247c478bd9Sstevel@tonic-gate 	 * Initialize io unit data structures.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
13277c478bd9Sstevel@tonic-gate 		dr_io_unit_t *ip;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
13307c478bd9Sstevel@tonic-gate 		dr_init_io_unit(ip);
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	dr_board_transition(bp, DR_STATE_CONNECTED);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	bp->b_rstate = SBD_STAT_CONNECTED;
13367c478bd9Sstevel@tonic-gate 	bp->b_ostate = SBD_STAT_UNCONFIGURED;
13377c478bd9Sstevel@tonic-gate 	bp->b_cond = SBD_COND_OK;
13387c478bd9Sstevel@tonic-gate 	(void) drv_getparm(TIME, (void *)&bp->b_time);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Only do work if called to operate on an entire board
13447c478bd9Sstevel@tonic-gate  * which doesn't already have components present.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate static void
dr_connect(dr_handle_t * hp)13477c478bd9Sstevel@tonic-gate dr_connect(dr_handle_t *hp)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
13507c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_connect";
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	if (DR_DEVS_PRESENT(bp)) {
13557c478bd9Sstevel@tonic-gate 		/*
13567c478bd9Sstevel@tonic-gate 		 * Board already has devices present.
13577c478bd9Sstevel@tonic-gate 		 */
135825cf1a30Sjl 		PR_ALL("%s: devices already present (0x%lx)\n",
135919397407SSherry Moore 		    f, DR_DEVS_PRESENT(bp));
13607c478bd9Sstevel@tonic-gate 		return;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_connect(bp->b_id, &hp->h_opts);
13647c478bd9Sstevel@tonic-gate 	if (hp->h_err)
13657c478bd9Sstevel@tonic-gate 		return;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	hp->h_err = dr_init_devlists(bp);
13687c478bd9Sstevel@tonic-gate 	if (hp->h_err)
13697c478bd9Sstevel@tonic-gate 		return;
13707c478bd9Sstevel@tonic-gate 	else if (bp->b_ndev == 0) {
13717c478bd9Sstevel@tonic-gate 		dr_op_err(CE_WARN, hp, ESBD_EMPTY_BD, bp->b_path);
13727c478bd9Sstevel@tonic-gate 		return;
13737c478bd9Sstevel@tonic-gate 	} else {
13747c478bd9Sstevel@tonic-gate 		dr_make_comp_nodes(bp);
13757c478bd9Sstevel@tonic-gate 		return;
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate static int
dr_disconnect(dr_handle_t * hp)13817c478bd9Sstevel@tonic-gate dr_disconnect(dr_handle_t *hp)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	int		i;
13847c478bd9Sstevel@tonic-gate 	dr_devset_t	devset;
13857c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
13867c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_disconnect";
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	/*
13917c478bd9Sstevel@tonic-gate 	 * Only devices which are present, but
13927c478bd9Sstevel@tonic-gate 	 * unattached can be disconnected.
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	devset = hp->h_devset & DR_DEVS_PRESENT(bp) &
139519397407SSherry Moore 	    DR_DEVS_UNATTACHED(bp);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if ((devset == 0) && DR_DEVS_PRESENT(bp)) {
13987c478bd9Sstevel@tonic-gate 		dr_op_err(CE_IGNORE, hp, ESBD_EMPTY_BD, bp->b_path);
13997c478bd9Sstevel@tonic-gate 		return (0);
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/*
14037c478bd9Sstevel@tonic-gate 	 * Block out status during disconnect.
14047c478bd9Sstevel@tonic-gate 	 */
14057c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->b_slock);
14067c478bd9Sstevel@tonic-gate 	while (bp->b_sflags & DR_BSLOCK) {
14077c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
14087c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->b_slock);
14097c478bd9Sstevel@tonic-gate 			return (EINTR);
14107c478bd9Sstevel@tonic-gate 		}
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 	bp->b_sflags |= DR_BSLOCK;
14137c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->b_slock);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	hp->h_err = drmach_board_disconnect(bp->b_id, &hp->h_opts);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	DR_DEVS_DISCONNECT(bp, devset);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	ASSERT((DR_DEVS_ATTACHED(bp) & devset) == 0);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/*
14227c478bd9Sstevel@tonic-gate 	 * Update per-device state transitions.
14237c478bd9Sstevel@tonic-gate 	 */
14247c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
14257c478bd9Sstevel@tonic-gate 		dr_cpu_unit_t *cp;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
14287c478bd9Sstevel@tonic-gate 			continue;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		cp = dr_get_cpu_unit(bp, i);
14317c478bd9Sstevel@tonic-gate 		if (dr_disconnect_cpu(cp) == 0)
14327c478bd9Sstevel@tonic-gate 			dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
14337c478bd9Sstevel@tonic-gate 		else if (cp->sbc_cm.sbdev_error != NULL)
14347c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&hp->h_err, &cp->sbc_cm.sbdev_error);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 		ASSERT(cp->sbc_cm.sbdev_error == NULL);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
14407c478bd9Sstevel@tonic-gate 		dr_mem_unit_t *mp;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
14437c478bd9Sstevel@tonic-gate 			continue;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, i);
14467c478bd9Sstevel@tonic-gate 		if (dr_disconnect_mem(mp) == 0)
14477c478bd9Sstevel@tonic-gate 			dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
14487c478bd9Sstevel@tonic-gate 		else if (mp->sbm_cm.sbdev_error != NULL)
14497c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&hp->h_err, &mp->sbm_cm.sbdev_error);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_cm.sbdev_error == NULL);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
14557c478bd9Sstevel@tonic-gate 		dr_io_unit_t *ip;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
14587c478bd9Sstevel@tonic-gate 			continue;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
14617c478bd9Sstevel@tonic-gate 		if (dr_disconnect_io(ip) == 0)
14627c478bd9Sstevel@tonic-gate 			dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
14637c478bd9Sstevel@tonic-gate 		else if (ip->sbi_cm.sbdev_error != NULL)
14647c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&hp->h_err, &ip->sbi_cm.sbdev_error);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		ASSERT(ip->sbi_cm.sbdev_error == NULL);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 	if (hp->h_err) {
14697c478bd9Sstevel@tonic-gate 		/*
14707c478bd9Sstevel@tonic-gate 		 * For certain errors, drmach_board_disconnect will mark
14717c478bd9Sstevel@tonic-gate 		 * the board as unusable; in these cases the devtree must
14727c478bd9Sstevel@tonic-gate 		 * be purged so that status calls will succeed.
14737c478bd9Sstevel@tonic-gate 		 * XXX
14747c478bd9Sstevel@tonic-gate 		 * This implementation checks for discrete error codes -
14757c478bd9Sstevel@tonic-gate 		 * someday, the i/f to drmach_board_disconnect should be
14767c478bd9Sstevel@tonic-gate 		 * changed to avoid the e_code testing.
14777c478bd9Sstevel@tonic-gate 		 */
14787c478bd9Sstevel@tonic-gate 		if ((hp->h_err->e_code == ESTC_MBXRPLY) ||
147919397407SSherry Moore 		    (hp->h_err->e_code == ESTC_MBXRQST) ||
148019397407SSherry Moore 		    (hp->h_err->e_code == ESTC_SMS_ERR_UNRECOVERABLE) ||
148119397407SSherry Moore 		    (hp->h_err->e_code == ESTC_SMS_ERR_RECOVERABLE) ||
148219397407SSherry Moore 		    (hp->h_err->e_code == ESTC_DEPROBE) ||
148319397407SSherry Moore 		    (hp->h_err->e_code == EOPL_DEPROBE)) {
14847c478bd9Sstevel@tonic-gate 			bp->b_ostate = SBD_STAT_UNCONFIGURED;
14857c478bd9Sstevel@tonic-gate 			bp->b_busy = 0;
14867c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&bp->b_time);
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 			if (drmach_board_deprobe(bp->b_id))
14897c478bd9Sstevel@tonic-gate 				goto disconnect_done;
14907c478bd9Sstevel@tonic-gate 			else
14917c478bd9Sstevel@tonic-gate 				bp->b_ndev = 0;
14927c478bd9Sstevel@tonic-gate 		}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 		/*
14957c478bd9Sstevel@tonic-gate 		 * If the disconnect failed in a recoverable way,
14967c478bd9Sstevel@tonic-gate 		 * more work is required.
14977c478bd9Sstevel@tonic-gate 		 * XXX
14987c478bd9Sstevel@tonic-gate 		 * This implementation checks for discrete error codes -
14997c478bd9Sstevel@tonic-gate 		 * someday, the i/f to drmach_board_disconnect should be
15007c478bd9Sstevel@tonic-gate 		 * changed to avoid the e_code testing.
15017c478bd9Sstevel@tonic-gate 		 */
15027c478bd9Sstevel@tonic-gate 		if ((hp->h_err->e_code == ESTC_MBXRQST) ||
15037c478bd9Sstevel@tonic-gate 		    (hp->h_err->e_code == ESTC_SMS_ERR_RECOVERABLE) ||
150468ac2337Sjl 		    (hp->h_err->e_code == ESTC_DEPROBE) ||
150568ac2337Sjl 		    (hp->h_err->e_code == EOPL_DEPROBE)) {
15067c478bd9Sstevel@tonic-gate 			/*
15077c478bd9Sstevel@tonic-gate 			 * With this failure, the board has been deprobed
15087c478bd9Sstevel@tonic-gate 			 * by IKP, and reprobed.  We've already gotten rid
15097c478bd9Sstevel@tonic-gate 			 * of the old devtree, now we need to reconstruct it
15107c478bd9Sstevel@tonic-gate 			 * based on the new IKP probe
15117c478bd9Sstevel@tonic-gate 			 */
15127c478bd9Sstevel@tonic-gate 			if (dr_init_devlists(bp) || (bp->b_ndev == 0))
15137c478bd9Sstevel@tonic-gate 				goto disconnect_done;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 			dr_make_comp_nodes(bp);
15167c478bd9Sstevel@tonic-gate 		}
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * Once all the components on a board have been disconnect
15207c478bd9Sstevel@tonic-gate 	 * the board's state can transition to disconnected and
15217c478bd9Sstevel@tonic-gate 	 * we can allow the deprobe to take place.
15227c478bd9Sstevel@tonic-gate 	 */
15237c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL && DR_DEVS_PRESENT(bp) == 0) {
15247c478bd9Sstevel@tonic-gate 		dr_board_transition(bp, DR_STATE_OCCUPIED);
15257c478bd9Sstevel@tonic-gate 		bp->b_rstate = SBD_STAT_DISCONNECTED;
15267c478bd9Sstevel@tonic-gate 		bp->b_ostate = SBD_STAT_UNCONFIGURED;
15277c478bd9Sstevel@tonic-gate 		bp->b_busy = 0;
15287c478bd9Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&bp->b_time);
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 		hp->h_err = drmach_board_deprobe(bp->b_id);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		if (hp->h_err == NULL) {
15337c478bd9Sstevel@tonic-gate 			bp->b_ndev = 0;
15347c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_EMPTY);
15357c478bd9Sstevel@tonic-gate 			bp->b_rstate = SBD_STAT_EMPTY;
15367c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&bp->b_time);
15377c478bd9Sstevel@tonic-gate 		}
15387c478bd9Sstevel@tonic-gate 	}
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate disconnect_done:
15417c478bd9Sstevel@tonic-gate 	dr_unlock_status(bp);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	return (0);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /*
15477c478bd9Sstevel@tonic-gate  * Check if a particular device is a valid target of the current
15487c478bd9Sstevel@tonic-gate  * operation. Return 1 if it is a valid target, and 0 otherwise.
15497c478bd9Sstevel@tonic-gate  */
15507c478bd9Sstevel@tonic-gate static int
dr_dev_is_target(dr_dev_unit_t * dp,int present_only,uint_t uset)15517c478bd9Sstevel@tonic-gate dr_dev_is_target(dr_dev_unit_t *dp, int present_only, uint_t uset)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	dr_common_unit_t *cp;
15547c478bd9Sstevel@tonic-gate 	int		 is_present;
15557c478bd9Sstevel@tonic-gate 	int		 is_attached;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	cp = &dp->du_common;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/* check if the user requested this device */
15607c478bd9Sstevel@tonic-gate 	if ((uset & (1 << cp->sbdev_unum)) == 0) {
15617c478bd9Sstevel@tonic-gate 		return (0);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	is_present = DR_DEV_IS_PRESENT(cp) ? 1 : 0;
15657c478bd9Sstevel@tonic-gate 	is_attached = DR_DEV_IS_ATTACHED(cp) ? 1 : 0;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * If the present_only flag is set, a valid target
15697c478bd9Sstevel@tonic-gate 	 * must be present but not attached. Otherwise, it
15707c478bd9Sstevel@tonic-gate 	 * must be both present and attached.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	if (is_present && (present_only ^ is_attached)) {
15737c478bd9Sstevel@tonic-gate 		/* sanity check */
15747c478bd9Sstevel@tonic-gate 		ASSERT(cp->sbdev_id != (drmachid_t)0);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		return (1);
15777c478bd9Sstevel@tonic-gate 	}
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	return (0);
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate static void
dr_dev_make_list(dr_handle_t * hp,sbd_comp_type_t type,int present_only,dr_common_unit_t *** devlist,int * devnum)15837c478bd9Sstevel@tonic-gate dr_dev_make_list(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
1584*c3937c08SToomas Soome     dr_common_unit_t ***devlist, int *devnum)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
15877c478bd9Sstevel@tonic-gate 	int		 unum;
15887c478bd9Sstevel@tonic-gate 	int		 nunits;
15897c478bd9Sstevel@tonic-gate 	uint_t		 uset;
15907c478bd9Sstevel@tonic-gate 	int		 len;
15917c478bd9Sstevel@tonic-gate 	dr_common_unit_t **list, **wp;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	switch (type) {
15947c478bd9Sstevel@tonic-gate 	case SBD_COMP_CPU:
15957c478bd9Sstevel@tonic-gate 		nunits = MAX_CPU_UNITS_PER_BOARD;
15967c478bd9Sstevel@tonic-gate 		break;
15977c478bd9Sstevel@tonic-gate 	case SBD_COMP_MEM:
15987c478bd9Sstevel@tonic-gate 		nunits = MAX_MEM_UNITS_PER_BOARD;
15997c478bd9Sstevel@tonic-gate 		break;
16007c478bd9Sstevel@tonic-gate 	case SBD_COMP_IO:
16017c478bd9Sstevel@tonic-gate 		nunits = MAX_IO_UNITS_PER_BOARD;
16027c478bd9Sstevel@tonic-gate 		break;
16037c478bd9Sstevel@tonic-gate 	default:
16047c478bd9Sstevel@tonic-gate 		/* catch this in debug kernels */
16057c478bd9Sstevel@tonic-gate 		ASSERT(0);
16067c478bd9Sstevel@tonic-gate 		break;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/* allocate list storage. */
16107c478bd9Sstevel@tonic-gate 	len = sizeof (dr_common_unit_t *) * (nunits + 1);
16117c478bd9Sstevel@tonic-gate 	list = kmem_zalloc(len, KM_SLEEP);
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/* record length of storage in first element */
161404580fdfSmathue 	*list++ = (dr_common_unit_t *)(uintptr_t)len;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/* get bit array signifying which units are to be involved */
16177c478bd9Sstevel@tonic-gate 	uset = DEVSET_GET_UNITSET(hp->h_devset, type);
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/*
16207c478bd9Sstevel@tonic-gate 	 * Adjust the loop count for CPU devices since all cores
16217c478bd9Sstevel@tonic-gate 	 * in a CMP will be examined in a single iteration.
16227c478bd9Sstevel@tonic-gate 	 */
16237c478bd9Sstevel@tonic-gate 	if (type == SBD_COMP_CPU) {
16247c478bd9Sstevel@tonic-gate 		nunits = MAX_CMP_UNITS_PER_BOARD;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/* populate list */
16287c478bd9Sstevel@tonic-gate 	for (wp = list, unum = 0; unum < nunits; unum++) {
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 		dr_dev_unit_t	*dp;
16317c478bd9Sstevel@tonic-gate 		int		core;
16327c478bd9Sstevel@tonic-gate 		int		cunum;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 		dp = DR_GET_BOARD_DEVUNIT(bp, type, unum);
16357c478bd9Sstevel@tonic-gate 		if (dr_dev_is_target(dp, present_only, uset)) {
16367c478bd9Sstevel@tonic-gate 			*wp++ = &dp->du_common;
16377c478bd9Sstevel@tonic-gate 		}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		/* further processing is only required for CPUs */
16407c478bd9Sstevel@tonic-gate 		if (type != SBD_COMP_CPU) {
16417c478bd9Sstevel@tonic-gate 			continue;
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * Add any additional cores from the current CPU
16467c478bd9Sstevel@tonic-gate 		 * device. This is to ensure that all the cores
16477c478bd9Sstevel@tonic-gate 		 * are grouped together in the device list, and
16487c478bd9Sstevel@tonic-gate 		 * consequently sequenced together during the actual
16497c478bd9Sstevel@tonic-gate 		 * operation.
16507c478bd9Sstevel@tonic-gate 		 */
16517c478bd9Sstevel@tonic-gate 		for (core = 1; core < MAX_CORES_PER_CMP; core++) {
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 			cunum = DR_CMP_CORE_UNUM(unum, core);
16547c478bd9Sstevel@tonic-gate 			dp = DR_GET_BOARD_DEVUNIT(bp, type, cunum);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 			if (dr_dev_is_target(dp, present_only, uset)) {
16577c478bd9Sstevel@tonic-gate 				*wp++ = &dp->du_common;
16587c478bd9Sstevel@tonic-gate 			}
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/* calculate number of units in list, return result and list pointer */
16637c478bd9Sstevel@tonic-gate 	*devnum = wp - list;
16647c478bd9Sstevel@tonic-gate 	*devlist = list;
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate static void
dr_dev_clean_up(dr_handle_t * hp,dr_common_unit_t ** list,int devnum)16687c478bd9Sstevel@tonic-gate dr_dev_clean_up(dr_handle_t *hp, dr_common_unit_t **list, int devnum)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	int len;
16717c478bd9Sstevel@tonic-gate 	int n = 0;
16727c478bd9Sstevel@tonic-gate 	dr_common_unit_t *cp, **rp = list;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * move first encountered unit error to handle if handle
16767c478bd9Sstevel@tonic-gate 	 * does not yet have a recorded error.
16777c478bd9Sstevel@tonic-gate 	 */
16787c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL) {
16797c478bd9Sstevel@tonic-gate 		while (n++ < devnum) {
16807c478bd9Sstevel@tonic-gate 			cp = *rp++;
16817c478bd9Sstevel@tonic-gate 			if (cp->sbdev_error != NULL) {
16827c478bd9Sstevel@tonic-gate 				hp->h_err = cp->sbdev_error;
16837c478bd9Sstevel@tonic-gate 				cp->sbdev_error = NULL;
16847c478bd9Sstevel@tonic-gate 				break;
16857c478bd9Sstevel@tonic-gate 			}
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/* free remaining unit errors */
16907c478bd9Sstevel@tonic-gate 	while (n++ < devnum) {
16917c478bd9Sstevel@tonic-gate 		cp = *rp++;
16927c478bd9Sstevel@tonic-gate 		if (cp->sbdev_error != NULL) {
16937c478bd9Sstevel@tonic-gate 			sbd_err_clear(&cp->sbdev_error);
16947c478bd9Sstevel@tonic-gate 			cp->sbdev_error = NULL;
16957c478bd9Sstevel@tonic-gate 		}
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/* free list */
16997c478bd9Sstevel@tonic-gate 	list -= 1;
170004580fdfSmathue 	len = (int)(uintptr_t)list[0];
17017c478bd9Sstevel@tonic-gate 	kmem_free(list, len);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate static int
dr_dev_walk(dr_handle_t * hp,sbd_comp_type_t type,int present_only,int (* pre_op)(dr_handle_t *,dr_common_unit_t **,int),void (* op)(dr_handle_t *,dr_common_unit_t *),int (* post_op)(dr_handle_t *,dr_common_unit_t **,int),void (* board_op)(dr_handle_t *,dr_common_unit_t **,int))17057c478bd9Sstevel@tonic-gate dr_dev_walk(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
1706*c3937c08SToomas Soome     int (*pre_op)(dr_handle_t *, dr_common_unit_t **, int),
1707*c3937c08SToomas Soome     void (*op)(dr_handle_t *, dr_common_unit_t *),
1708*c3937c08SToomas Soome     int (*post_op)(dr_handle_t *, dr_common_unit_t **, int),
1709*c3937c08SToomas Soome     void (*board_op)(dr_handle_t *, dr_common_unit_t **, int))
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	int			  devnum, rv;
17127c478bd9Sstevel@tonic-gate 	dr_common_unit_t	**devlist;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	dr_dev_make_list(hp, type, present_only, &devlist, &devnum);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	rv = 0;
17177c478bd9Sstevel@tonic-gate 	if (devnum > 0) {
17187c478bd9Sstevel@tonic-gate 		rv = (*pre_op)(hp, devlist, devnum);
17197c478bd9Sstevel@tonic-gate 		if (rv == 0) {
17207c478bd9Sstevel@tonic-gate 			int n;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 			for (n = 0; n < devnum; n++)
17237c478bd9Sstevel@tonic-gate 				(*op)(hp, devlist[n]);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 			rv = (*post_op)(hp, devlist, devnum);
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 			(*board_op)(hp, devlist, devnum);
17287c478bd9Sstevel@tonic-gate 		}
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	dr_dev_clean_up(hp, devlist, devnum);
17327c478bd9Sstevel@tonic-gate 	return (rv);
17337c478bd9Sstevel@tonic-gate }
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17367c478bd9Sstevel@tonic-gate static int
dr_dev_noop(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)17377c478bd9Sstevel@tonic-gate dr_dev_noop(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	return (0);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate static void
dr_attach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)17437c478bd9Sstevel@tonic-gate dr_attach_update_state(dr_handle_t *hp,
1744*c3937c08SToomas Soome     dr_common_unit_t **devlist, int devnum)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
17477c478bd9Sstevel@tonic-gate 	int		i;
17487c478bd9Sstevel@tonic-gate 	dr_devset_t	devs_unattached, devs_present;
17497c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_attach_devlist";
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
17527c478bd9Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[i];
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		if (dr_check_unit_attached(cp) == -1) {
17557c478bd9Sstevel@tonic-gate 			PR_ALL("%s: ERROR %s not attached\n",
175619397407SSherry Moore 			    f, cp->sbdev_path);
17577c478bd9Sstevel@tonic-gate 			continue;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 		DR_DEV_SET_ATTACHED(cp);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_CONFIGURED);
17637c478bd9Sstevel@tonic-gate 		cp->sbdev_cond = SBD_COND_OK;
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	devs_present = DR_DEVS_PRESENT(bp);
17677c478bd9Sstevel@tonic-gate 	devs_unattached = DR_DEVS_UNATTACHED(bp);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	switch (bp->b_state) {
17707c478bd9Sstevel@tonic-gate 	case DR_STATE_CONNECTED:
17717c478bd9Sstevel@tonic-gate 	case DR_STATE_UNCONFIGURED:
17727c478bd9Sstevel@tonic-gate 		ASSERT(devs_present);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 		if (devs_unattached == 0) {
17757c478bd9Sstevel@tonic-gate 			/*
17767c478bd9Sstevel@tonic-gate 			 * All devices finally attached.
17777c478bd9Sstevel@tonic-gate 			 */
17787c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_CONFIGURED);
17797c478bd9Sstevel@tonic-gate 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
17807c478bd9Sstevel@tonic-gate 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
17817c478bd9Sstevel@tonic-gate 			hp->h_bd->b_cond = SBD_COND_OK;
17827c478bd9Sstevel@tonic-gate 			hp->h_bd->b_busy = 0;
17837c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
17847c478bd9Sstevel@tonic-gate 		} else if (devs_present != devs_unattached) {
17857c478bd9Sstevel@tonic-gate 			/*
17867c478bd9Sstevel@tonic-gate 			 * Only some devices are fully attached.
17877c478bd9Sstevel@tonic-gate 			 */
17887c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_PARTIAL);
17897c478bd9Sstevel@tonic-gate 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
17907c478bd9Sstevel@tonic-gate 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
17917c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 		break;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	case DR_STATE_PARTIAL:
17967c478bd9Sstevel@tonic-gate 		ASSERT(devs_present);
17977c478bd9Sstevel@tonic-gate 		/*
17987c478bd9Sstevel@tonic-gate 		 * All devices finally attached.
17997c478bd9Sstevel@tonic-gate 		 */
18007c478bd9Sstevel@tonic-gate 		if (devs_unattached == 0) {
18017c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_CONFIGURED);
18027c478bd9Sstevel@tonic-gate 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
18037c478bd9Sstevel@tonic-gate 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
18047c478bd9Sstevel@tonic-gate 			hp->h_bd->b_cond = SBD_COND_OK;
18057c478bd9Sstevel@tonic-gate 			hp->h_bd->b_busy = 0;
18067c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 		break;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	default:
18117c478bd9Sstevel@tonic-gate 		break;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate static void
dr_dev_configure(dr_handle_t * hp)18167c478bd9Sstevel@tonic-gate dr_dev_configure(dr_handle_t *hp)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	int rv;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	rv = dr_dev_walk(hp, SBD_COMP_CPU, 1,
182119397407SSherry Moore 	    dr_pre_attach_cpu,
182219397407SSherry Moore 	    dr_attach_cpu,
182319397407SSherry Moore 	    dr_post_attach_cpu,
182419397407SSherry Moore 	    dr_attach_update_state);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (rv >= 0) {
18277c478bd9Sstevel@tonic-gate 		rv = dr_dev_walk(hp, SBD_COMP_MEM, 1,
182819397407SSherry Moore 		    dr_pre_attach_mem,
182919397407SSherry Moore 		    dr_attach_mem,
183019397407SSherry Moore 		    dr_post_attach_mem,
183119397407SSherry Moore 		    dr_attach_update_state);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (rv >= 0) {
18357c478bd9Sstevel@tonic-gate 		(void) dr_dev_walk(hp, SBD_COMP_IO, 1,
183619397407SSherry Moore 		    dr_pre_attach_io,
183719397407SSherry Moore 		    dr_attach_io,
183819397407SSherry Moore 		    dr_post_attach_io,
183919397407SSherry Moore 		    dr_attach_update_state);
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate static void
dr_release_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)18447c478bd9Sstevel@tonic-gate dr_release_update_state(dr_handle_t *hp,
1845*c3937c08SToomas Soome     dr_common_unit_t **devlist, int devnum)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devlist))
18487c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(devnum))
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	dr_board_t *bp = hp->h_bd;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	/*
18537c478bd9Sstevel@tonic-gate 	 * If the entire board was released and all components
18547c478bd9Sstevel@tonic-gate 	 * unreferenced then transfer it to the UNREFERENCED state.
18557c478bd9Sstevel@tonic-gate 	 */
18567c478bd9Sstevel@tonic-gate 	if ((bp->b_state != DR_STATE_RELEASE) &&
185719397407SSherry Moore 	    (DR_DEVS_RELEASED(bp) == DR_DEVS_ATTACHED(bp))) {
18587c478bd9Sstevel@tonic-gate 		dr_board_transition(bp, DR_STATE_RELEASE);
18597c478bd9Sstevel@tonic-gate 		hp->h_bd->b_busy = 1;
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate /* called by dr_release_done [below] and dr_release_mem_done [dr_mem.c] */
18647c478bd9Sstevel@tonic-gate int
dr_release_dev_done(dr_common_unit_t * cp)18657c478bd9Sstevel@tonic-gate dr_release_dev_done(dr_common_unit_t *cp)
18667c478bd9Sstevel@tonic-gate {
18677c478bd9Sstevel@tonic-gate 	if (cp->sbdev_state == DR_STATE_RELEASE) {
18687c478bd9Sstevel@tonic-gate 		ASSERT(DR_DEV_IS_RELEASED(cp));
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 		DR_DEV_SET_UNREFERENCED(cp);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_UNREFERENCED);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		return (0);
18757c478bd9Sstevel@tonic-gate 	} else {
18767c478bd9Sstevel@tonic-gate 		return (-1);
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate static void
dr_release_done(dr_handle_t * hp,dr_common_unit_t * cp)18817c478bd9Sstevel@tonic-gate dr_release_done(dr_handle_t *hp, dr_common_unit_t *cp)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	dr_board_t		*bp;
18867c478bd9Sstevel@tonic-gate 	static fn_t		f = "dr_release_done";
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	/* get board pointer & sanity check */
18917c478bd9Sstevel@tonic-gate 	bp = cp->sbdev_bp;
18927c478bd9Sstevel@tonic-gate 	ASSERT(bp == hp->h_bd);
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	/*
18957c478bd9Sstevel@tonic-gate 	 * Transfer the device which just completed its release
18967c478bd9Sstevel@tonic-gate 	 * to the UNREFERENCED state.
18977c478bd9Sstevel@tonic-gate 	 */
18987c478bd9Sstevel@tonic-gate 	switch (cp->sbdev_type) {
18997c478bd9Sstevel@tonic-gate 	case SBD_COMP_MEM:
19007c478bd9Sstevel@tonic-gate 		dr_release_mem_done(cp);
19017c478bd9Sstevel@tonic-gate 		break;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	default:
19047c478bd9Sstevel@tonic-gate 		DR_DEV_SET_RELEASED(cp);
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_RELEASE);
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		(void) dr_release_dev_done(cp);
19097c478bd9Sstevel@tonic-gate 		break;
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	/*
19137c478bd9Sstevel@tonic-gate 	 * If we're not already in the RELEASE state for this
19147c478bd9Sstevel@tonic-gate 	 * board and we now have released all that were previously
19157c478bd9Sstevel@tonic-gate 	 * attached, then transfer the board to the RELEASE state.
19167c478bd9Sstevel@tonic-gate 	 */
19177c478bd9Sstevel@tonic-gate 	if ((bp->b_state == DR_STATE_RELEASE) &&
191819397407SSherry Moore 	    (DR_DEVS_RELEASED(bp) == DR_DEVS_UNREFERENCED(bp))) {
19197c478bd9Sstevel@tonic-gate 		dr_board_transition(bp, DR_STATE_UNREFERENCED);
19207c478bd9Sstevel@tonic-gate 		bp->b_busy = 1;
19217c478bd9Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&bp->b_time);
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate static void
dr_dev_release_mem(dr_handle_t * hp,dr_common_unit_t * dv)19267c478bd9Sstevel@tonic-gate dr_dev_release_mem(dr_handle_t *hp, dr_common_unit_t *dv)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	dr_release_mem(dv);
19297c478bd9Sstevel@tonic-gate 	dr_release_done(hp, dv);
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate static void
dr_dev_release(dr_handle_t * hp)19337c478bd9Sstevel@tonic-gate dr_dev_release(dr_handle_t *hp)
19347c478bd9Sstevel@tonic-gate {
19357c478bd9Sstevel@tonic-gate 	int rv;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	hp->h_bd->b_busy = 1;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	rv = dr_dev_walk(hp, SBD_COMP_CPU, 0,
194019397407SSherry Moore 	    dr_pre_release_cpu,
194119397407SSherry Moore 	    dr_release_done,
194219397407SSherry Moore 	    dr_dev_noop,
194319397407SSherry Moore 	    dr_release_update_state);
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	if (rv >= 0) {
19467c478bd9Sstevel@tonic-gate 		rv = dr_dev_walk(hp, SBD_COMP_MEM, 0,
194719397407SSherry Moore 		    dr_pre_release_mem,
194819397407SSherry Moore 		    dr_dev_release_mem,
194919397407SSherry Moore 		    dr_dev_noop,
195019397407SSherry Moore 		    dr_release_update_state);
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	if (rv >= 0) {
19547c478bd9Sstevel@tonic-gate 		rv = dr_dev_walk(hp, SBD_COMP_IO, 0,
195519397407SSherry Moore 		    dr_pre_release_io,
195619397407SSherry Moore 		    dr_release_done,
195719397407SSherry Moore 		    dr_dev_noop,
195819397407SSherry Moore 		    dr_release_update_state);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (rv < 0)
19637c478bd9Sstevel@tonic-gate 		hp->h_bd->b_busy = 0;
19647c478bd9Sstevel@tonic-gate 	/* else, b_busy will be cleared in dr_detach_update_state() */
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate static void
dr_detach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)19687c478bd9Sstevel@tonic-gate dr_detach_update_state(dr_handle_t *hp,
1969*c3937c08SToomas Soome     dr_common_unit_t **devlist, int devnum)
19707c478bd9Sstevel@tonic-gate {
19717c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
19727c478bd9Sstevel@tonic-gate 	int		i;
19737c478bd9Sstevel@tonic-gate 	dr_state_t	bstate;
19747c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_detach_update_state";
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
19777c478bd9Sstevel@tonic-gate 		dr_common_unit_t *cp = devlist[i];
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		if (dr_check_unit_attached(cp) >= 0) {
19807c478bd9Sstevel@tonic-gate 			/*
19817c478bd9Sstevel@tonic-gate 			 * Device is still attached probably due
19827c478bd9Sstevel@tonic-gate 			 * to an error.  Need to keep track of it.
19837c478bd9Sstevel@tonic-gate 			 */
19847c478bd9Sstevel@tonic-gate 			PR_ALL("%s: ERROR %s not detached\n",
198519397407SSherry Moore 			    f, cp->sbdev_path);
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 			continue;
19887c478bd9Sstevel@tonic-gate 		}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_ATTACHED(cp);
19917c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(cp);
19927c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(cp);
19937c478bd9Sstevel@tonic-gate 		dr_device_transition(cp, DR_STATE_UNCONFIGURED);
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	bstate = bp->b_state;
19977c478bd9Sstevel@tonic-gate 	if (bstate != DR_STATE_UNCONFIGURED) {
19987c478bd9Sstevel@tonic-gate 		if (DR_DEVS_PRESENT(bp) == DR_DEVS_UNATTACHED(bp)) {
19997c478bd9Sstevel@tonic-gate 			/*
20007c478bd9Sstevel@tonic-gate 			 * All devices are finally detached.
20017c478bd9Sstevel@tonic-gate 			 */
20027c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_UNCONFIGURED);
20037c478bd9Sstevel@tonic-gate 			hp->h_bd->b_ostate = SBD_STAT_UNCONFIGURED;
20047c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
20057c478bd9Sstevel@tonic-gate 		} else if ((bp->b_state != DR_STATE_PARTIAL) &&
200619397407SSherry Moore 		    (DR_DEVS_ATTACHED(bp) !=
200719397407SSherry Moore 		    DR_DEVS_PRESENT(bp))) {
20087c478bd9Sstevel@tonic-gate 			/*
20097c478bd9Sstevel@tonic-gate 			 * Some devices remain attached.
20107c478bd9Sstevel@tonic-gate 			 */
20117c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_PARTIAL);
20127c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		if ((hp->h_devset & DR_DEVS_UNATTACHED(bp)) == hp->h_devset)
20167c478bd9Sstevel@tonic-gate 			hp->h_bd->b_busy = 0;
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate static int
dr_dev_unconfigure(dr_handle_t * hp)20217c478bd9Sstevel@tonic-gate dr_dev_unconfigure(dr_handle_t *hp)
20227c478bd9Sstevel@tonic-gate {
20237c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	/*
20267c478bd9Sstevel@tonic-gate 	 * Block out status during IO unconfig.
20277c478bd9Sstevel@tonic-gate 	 */
20287c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->b_slock);
20297c478bd9Sstevel@tonic-gate 	while (bp->b_sflags & DR_BSLOCK) {
20307c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
20317c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->b_slock);
20327c478bd9Sstevel@tonic-gate 			return (EINTR);
20337c478bd9Sstevel@tonic-gate 		}
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 	bp->b_sflags |= DR_BSLOCK;
20367c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->b_slock);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	(void) dr_dev_walk(hp, SBD_COMP_IO, 0,
203919397407SSherry Moore 	    dr_pre_detach_io,
204019397407SSherry Moore 	    dr_detach_io,
204119397407SSherry Moore 	    dr_post_detach_io,
204219397407SSherry Moore 	    dr_detach_update_state);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	dr_unlock_status(bp);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	(void) dr_dev_walk(hp, SBD_COMP_CPU, 0,
204719397407SSherry Moore 	    dr_pre_detach_cpu,
204819397407SSherry Moore 	    dr_detach_cpu,
204919397407SSherry Moore 	    dr_post_detach_cpu,
205019397407SSherry Moore 	    dr_detach_update_state);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	(void) dr_dev_walk(hp, SBD_COMP_MEM, 0,
205319397407SSherry Moore 	    dr_pre_detach_mem,
205419397407SSherry Moore 	    dr_detach_mem,
205519397407SSherry Moore 	    dr_post_detach_mem,
205619397407SSherry Moore 	    dr_detach_update_state);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	return (0);
20597c478bd9Sstevel@tonic-gate }
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate static void
dr_dev_cancel(dr_handle_t * hp)20627c478bd9Sstevel@tonic-gate dr_dev_cancel(dr_handle_t *hp)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	int		i;
20657c478bd9Sstevel@tonic-gate 	dr_devset_t	devset;
20667c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
20677c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_dev_cancel";
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	/*
20727c478bd9Sstevel@tonic-gate 	 * Only devices which have been "released" are
20737c478bd9Sstevel@tonic-gate 	 * subject to cancellation.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	devset = hp->h_devset & DR_DEVS_RELEASED(bp);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Nothing to do for CPUs or IO other than change back
20797c478bd9Sstevel@tonic-gate 	 * their state.
20807c478bd9Sstevel@tonic-gate 	 */
20817c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
20827c478bd9Sstevel@tonic-gate 		dr_cpu_unit_t	*cp;
20837c478bd9Sstevel@tonic-gate 		dr_state_t	nstate;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
20867c478bd9Sstevel@tonic-gate 			continue;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 		cp = dr_get_cpu_unit(bp, i);
20897c478bd9Sstevel@tonic-gate 		if (dr_cancel_cpu(cp) == 0)
20907c478bd9Sstevel@tonic-gate 			nstate = DR_STATE_CONFIGURED;
20917c478bd9Sstevel@tonic-gate 		else
20927c478bd9Sstevel@tonic-gate 			nstate = DR_STATE_FATAL;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 		dr_device_transition(&cp->sbc_cm, nstate);
20957c478bd9Sstevel@tonic-gate 	}
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
20987c478bd9Sstevel@tonic-gate 		dr_io_unit_t *ip;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
21017c478bd9Sstevel@tonic-gate 			continue;
21027c478bd9Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
21037c478bd9Sstevel@tonic-gate 		dr_device_transition(&ip->sbi_cm, DR_STATE_CONFIGURED);
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
21067c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*mp;
21077c478bd9Sstevel@tonic-gate 		dr_state_t	nstate;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
21107c478bd9Sstevel@tonic-gate 			continue;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, i);
21137c478bd9Sstevel@tonic-gate 		if (dr_cancel_mem(mp) == 0)
21147c478bd9Sstevel@tonic-gate 			nstate = DR_STATE_CONFIGURED;
21157c478bd9Sstevel@tonic-gate 		else
21167c478bd9Sstevel@tonic-gate 			nstate = DR_STATE_FATAL;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 		dr_device_transition(&mp->sbm_cm, nstate);
21197c478bd9Sstevel@tonic-gate 	}
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	PR_ALL("%s: unreleasing devset (0x%x)\n", f, (uint_t)devset);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	DR_DEVS_CANCEL(bp, devset);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	if (DR_DEVS_RELEASED(bp) == 0) {
21267c478bd9Sstevel@tonic-gate 		dr_state_t	new_state;
21277c478bd9Sstevel@tonic-gate 		/*
21287c478bd9Sstevel@tonic-gate 		 * If the board no longer has any released devices
21297c478bd9Sstevel@tonic-gate 		 * than transfer it back to the CONFIG/PARTIAL state.
21307c478bd9Sstevel@tonic-gate 		 */
21317c478bd9Sstevel@tonic-gate 		if (DR_DEVS_ATTACHED(bp) == DR_DEVS_PRESENT(bp))
21327c478bd9Sstevel@tonic-gate 			new_state = DR_STATE_CONFIGURED;
21337c478bd9Sstevel@tonic-gate 		else
21347c478bd9Sstevel@tonic-gate 			new_state = DR_STATE_PARTIAL;
21357c478bd9Sstevel@tonic-gate 		if (bp->b_state != new_state) {
21367c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, new_state);
21377c478bd9Sstevel@tonic-gate 		}
21387c478bd9Sstevel@tonic-gate 		hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
21397c478bd9Sstevel@tonic-gate 		hp->h_bd->b_busy = 0;
21407c478bd9Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
21417c478bd9Sstevel@tonic-gate 	}
21427c478bd9Sstevel@tonic-gate }
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate static int
dr_dev_status(dr_handle_t * hp)21457c478bd9Sstevel@tonic-gate dr_dev_status(dr_handle_t *hp)
21467c478bd9Sstevel@tonic-gate {
21477c478bd9Sstevel@tonic-gate 	int		nstat, mode, ncm, sz, pbsz, pnstat;
21487c478bd9Sstevel@tonic-gate 	dr_handle_t	*shp;
21497c478bd9Sstevel@tonic-gate 	dr_devset_t	devset = 0;
21507c478bd9Sstevel@tonic-gate 	sbd_stat_t	*dstatp = NULL;
21517c478bd9Sstevel@tonic-gate 	sbd_dev_stat_t	*devstatp;
21527c478bd9Sstevel@tonic-gate 	dr_board_t	*bp;
21537c478bd9Sstevel@tonic-gate 	drmach_status_t	 pstat;
21547c478bd9Sstevel@tonic-gate 	int		rv = 0;
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
21577c478bd9Sstevel@tonic-gate 	int sz32 = 0;
21587c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_status";
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	mode = hp->h_mode;
21657c478bd9Sstevel@tonic-gate 	shp = hp;
21667c478bd9Sstevel@tonic-gate 	devset = shp->h_devset;
21677c478bd9Sstevel@tonic-gate 	bp = hp->h_bd;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * Block out disconnect, unassign, IO unconfigure and
21717c478bd9Sstevel@tonic-gate 	 * devinfo branch creation during status.
21727c478bd9Sstevel@tonic-gate 	 */
21737c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->b_slock);
21747c478bd9Sstevel@tonic-gate 	while (bp->b_sflags & DR_BSLOCK) {
21757c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
21767c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->b_slock);
21777c478bd9Sstevel@tonic-gate 			return (EINTR);
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 	bp->b_sflags |= DR_BSLOCK;
21817c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->b_slock);
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	ncm = 1;
21847c478bd9Sstevel@tonic-gate 	if (hp->h_sbdcmd.cmd_cm.c_id.c_type == SBD_COMP_NONE) {
21857c478bd9Sstevel@tonic-gate 		if (dr_cmd_flags(hp) & SBD_FLAG_ALLCMP) {
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * Calculate the maximum number of components possible
21887c478bd9Sstevel@tonic-gate 		 * for a board.  This number will be used to size the
21897c478bd9Sstevel@tonic-gate 		 * status scratch buffer used by board and component
21907c478bd9Sstevel@tonic-gate 		 * status functions.
21917c478bd9Sstevel@tonic-gate 		 * This buffer may differ in size from what is provided
21927c478bd9Sstevel@tonic-gate 		 * by the plugin, since the known component set on the
21937c478bd9Sstevel@tonic-gate 		 * board may change between the plugin's GETNCM call, and
21947c478bd9Sstevel@tonic-gate 		 * the status call.  Sizing will be adjusted to the plugin's
21957c478bd9Sstevel@tonic-gate 		 * receptacle buffer at copyout time.
21967c478bd9Sstevel@tonic-gate 		 */
21977c478bd9Sstevel@tonic-gate 			ncm = MAX_CPU_UNITS_PER_BOARD +
219819397407SSherry Moore 			    MAX_MEM_UNITS_PER_BOARD +
219919397407SSherry Moore 			    MAX_IO_UNITS_PER_BOARD;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 		} else {
22027c478bd9Sstevel@tonic-gate 			/*
22037c478bd9Sstevel@tonic-gate 			 * In the case of c_type == SBD_COMP_NONE, and
22047c478bd9Sstevel@tonic-gate 			 * SBD_FLAG_ALLCMP not specified, only the board
22057c478bd9Sstevel@tonic-gate 			 * info is to be returned, no components.
22067c478bd9Sstevel@tonic-gate 			 */
22077c478bd9Sstevel@tonic-gate 			ncm = 0;
22087c478bd9Sstevel@tonic-gate 			devset = 0;
22097c478bd9Sstevel@tonic-gate 		}
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	sz = sizeof (sbd_stat_t);
22137c478bd9Sstevel@tonic-gate 	if (ncm > 1)
22147c478bd9Sstevel@tonic-gate 		sz += sizeof (sbd_dev_stat_t) * (ncm - 1);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	pbsz = (int)hp->h_sbdcmd.cmd_stat.s_nbytes;
22187c478bd9Sstevel@tonic-gate 	pnstat = (pbsz - sizeof (sbd_stat_t))/sizeof (sbd_dev_stat_t);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/*
22217c478bd9Sstevel@tonic-gate 	 * s_nbytes describes the size of the preallocated user
22227c478bd9Sstevel@tonic-gate 	 * buffer into which the application is execting to
22237c478bd9Sstevel@tonic-gate 	 * receive the sbd_stat_t and sbd_dev_stat_t structures.
22247c478bd9Sstevel@tonic-gate 	 */
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	/*
22297c478bd9Sstevel@tonic-gate 	 * More buffer space is required for the 64bit to 32bit
22307c478bd9Sstevel@tonic-gate 	 * conversion of data structures.
22317c478bd9Sstevel@tonic-gate 	 */
22327c478bd9Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
22337c478bd9Sstevel@tonic-gate 		sz32 = sizeof (sbd_stat32_t);
22347c478bd9Sstevel@tonic-gate 		if (ncm > 1)
22357c478bd9Sstevel@tonic-gate 			sz32  += sizeof (sbd_dev_stat32_t) * (ncm - 1);
22367c478bd9Sstevel@tonic-gate 		pnstat = (pbsz - sizeof (sbd_stat32_t))/
223719397407SSherry Moore 		    sizeof (sbd_dev_stat32_t);
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	sz += sz32;
22417c478bd9Sstevel@tonic-gate #endif
22427c478bd9Sstevel@tonic-gate 	/*
22437c478bd9Sstevel@tonic-gate 	 * Since one sbd_dev_stat_t is included in the sbd_stat_t,
22447c478bd9Sstevel@tonic-gate 	 * increment the plugin's nstat count.
22457c478bd9Sstevel@tonic-gate 	 */
22467c478bd9Sstevel@tonic-gate 	++pnstat;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	if (bp->b_id == 0) {
22497c478bd9Sstevel@tonic-gate 		bzero(&pstat, sizeof (pstat));
22507c478bd9Sstevel@tonic-gate 	} else {
22517c478bd9Sstevel@tonic-gate 		sbd_error_t *err;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 		err = drmach_status(bp->b_id, &pstat);
22547c478bd9Sstevel@tonic-gate 		if (err) {
22557c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&hp->h_err, &err);
22567c478bd9Sstevel@tonic-gate 			rv = EIO;
22577c478bd9Sstevel@tonic-gate 			goto status_done;
22587c478bd9Sstevel@tonic-gate 		}
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	dstatp = (sbd_stat_t *)GETSTRUCT(char, sz);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	devstatp = &dstatp->s_stat[0];
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	dstatp->s_board = bp->b_num;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * Detect transitions between empty and disconnected.
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	if (!pstat.empty && (bp->b_rstate == SBD_STAT_EMPTY))
22717c478bd9Sstevel@tonic-gate 		bp->b_rstate = SBD_STAT_DISCONNECTED;
22727c478bd9Sstevel@tonic-gate 	else if (pstat.empty && (bp->b_rstate == SBD_STAT_DISCONNECTED))
22737c478bd9Sstevel@tonic-gate 		bp->b_rstate = SBD_STAT_EMPTY;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	dstatp->s_rstate = bp->b_rstate;
22767c478bd9Sstevel@tonic-gate 	dstatp->s_ostate = bp->b_ostate;
22777c478bd9Sstevel@tonic-gate 	dstatp->s_cond = bp->b_cond = pstat.cond;
22787c478bd9Sstevel@tonic-gate 	dstatp->s_busy = bp->b_busy | pstat.busy;
22797c478bd9Sstevel@tonic-gate 	dstatp->s_time = bp->b_time;
22807c478bd9Sstevel@tonic-gate 	dstatp->s_power = pstat.powered;
22817c478bd9Sstevel@tonic-gate 	dstatp->s_assigned = bp->b_assigned = pstat.assigned;
22827c478bd9Sstevel@tonic-gate 	dstatp->s_nstat = nstat = 0;
22837c478bd9Sstevel@tonic-gate 	bcopy(&pstat.type[0], &dstatp->s_type[0], SBD_TYPE_LEN);
22847c478bd9Sstevel@tonic-gate 	bcopy(&pstat.info[0], &dstatp->s_info[0], SBD_MAX_INFO);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
22877c478bd9Sstevel@tonic-gate 	if (devset == 0) {
22887c478bd9Sstevel@tonic-gate 		/*
22897c478bd9Sstevel@tonic-gate 		 * No device chosen.
22907c478bd9Sstevel@tonic-gate 		 */
22917c478bd9Sstevel@tonic-gate 		PR_ALL("%s: no device present\n", f);
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT))
22957c478bd9Sstevel@tonic-gate 		if ((nstat = dr_cpu_status(hp, devset, devstatp)) > 0) {
22967c478bd9Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
22977c478bd9Sstevel@tonic-gate 			devstatp += nstat;
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT))
23017c478bd9Sstevel@tonic-gate 		if ((nstat = dr_mem_status(hp, devset, devstatp)) > 0) {
23027c478bd9Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
23037c478bd9Sstevel@tonic-gate 			devstatp += nstat;
23047c478bd9Sstevel@tonic-gate 		}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT))
23077c478bd9Sstevel@tonic-gate 		if ((nstat = dr_io_status(hp, devset, devstatp)) > 0) {
23087c478bd9Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
23097c478bd9Sstevel@tonic-gate 			devstatp += nstat;
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	/*
23137c478bd9Sstevel@tonic-gate 	 * Due to a possible change in number of components between
23147c478bd9Sstevel@tonic-gate 	 * the time of plugin's GETNCM call and now, there may be
23157c478bd9Sstevel@tonic-gate 	 * more or less components than the plugin's buffer can
23167c478bd9Sstevel@tonic-gate 	 * hold.  Adjust s_nstat accordingly.
23177c478bd9Sstevel@tonic-gate 	 */
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	dstatp->s_nstat = dstatp->s_nstat > pnstat ? pnstat : dstatp->s_nstat;
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
23237c478bd9Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
23247c478bd9Sstevel@tonic-gate 		int		i, j;
23257c478bd9Sstevel@tonic-gate 		sbd_stat32_t	*dstat32p;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		dstat32p = (sbd_stat32_t *)devstatp;
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 		/* Alignment Paranoia */
23307c478bd9Sstevel@tonic-gate 		if ((ulong_t)dstat32p & 0x1) {
233104580fdfSmathue 			PR_ALL("%s: alignment: sz=0x%lx dstat32p=0x%p\n",
233207d06da5SSurya Prakki 			    f, sizeof (sbd_stat32_t), (void *)dstat32p);
23337c478bd9Sstevel@tonic-gate 			DR_OP_INTERNAL_ERROR(hp);
23347c478bd9Sstevel@tonic-gate 			rv = EINVAL;
23357c478bd9Sstevel@tonic-gate 			goto status_done;
23367c478bd9Sstevel@tonic-gate 		}
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 		/* paranoia: detect buffer overrun */
23397c478bd9Sstevel@tonic-gate 		if ((caddr_t)&dstat32p->s_stat[dstatp->s_nstat] >
234019397407SSherry Moore 		    ((caddr_t)dstatp) + sz) {
23417c478bd9Sstevel@tonic-gate 			DR_OP_INTERNAL_ERROR(hp);
23427c478bd9Sstevel@tonic-gate 			rv = EINVAL;
23437c478bd9Sstevel@tonic-gate 			goto status_done;
23447c478bd9Sstevel@tonic-gate 		}
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		/* copy sbd_stat_t structure members */
23477c478bd9Sstevel@tonic-gate #define	_SBD_STAT(t, m) dstat32p->m = (t)dstatp->m
23487c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_board);
23497c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_rstate);
23507c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_ostate);
23517c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_cond);
23527c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_busy);
23537c478bd9Sstevel@tonic-gate 		_SBD_STAT(time32_t, s_time);
23547c478bd9Sstevel@tonic-gate 		_SBD_STAT(uint32_t, s_power);
23557c478bd9Sstevel@tonic-gate 		_SBD_STAT(uint32_t, s_assigned);
23567c478bd9Sstevel@tonic-gate 		_SBD_STAT(int32_t, s_nstat);
23577c478bd9Sstevel@tonic-gate 		bcopy(&dstatp->s_type[0], &dstat32p->s_type[0],
235819397407SSherry Moore 		    SBD_TYPE_LEN);
23597c478bd9Sstevel@tonic-gate 		bcopy(&dstatp->s_info[0], &dstat32p->s_info[0],
236019397407SSherry Moore 		    SBD_MAX_INFO);
23617c478bd9Sstevel@tonic-gate #undef _SBD_STAT
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		for (i = 0; i < dstatp->s_nstat; i++) {
23647c478bd9Sstevel@tonic-gate 			sbd_dev_stat_t		*dsp = &dstatp->s_stat[i];
23657c478bd9Sstevel@tonic-gate 			sbd_dev_stat32_t	*ds32p = &dstat32p->s_stat[i];
23667c478bd9Sstevel@tonic-gate #define	_SBD_DEV_STAT(t, m) ds32p->m = (t)dsp->m
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 			/* copy sbd_cm_stat_t structure members */
23697c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_type);
23707c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_unit);
23717c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_ostate);
23727c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_cond);
23737c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_busy);
23747c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(int32_t, ds_suspend);
23757c478bd9Sstevel@tonic-gate 			_SBD_DEV_STAT(time32_t, ds_time);
23767c478bd9Sstevel@tonic-gate 			bcopy(&dsp->ds_name[0], &ds32p->ds_name[0],
23777c478bd9Sstevel@tonic-gate 			    OBP_MAXPROPNAME);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 			switch (dsp->ds_type) {
23807c478bd9Sstevel@tonic-gate 			case SBD_COMP_CPU:
23817c478bd9Sstevel@tonic-gate 				/* copy sbd_cpu_stat_t structure members */
23827c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cpu.cs_isbootproc);
23837c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cpu.cs_cpuid);
23847c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cpu.cs_speed);
23857c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cpu.cs_ecache);
23867c478bd9Sstevel@tonic-gate 				break;
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 			case SBD_COMP_MEM:
23897c478bd9Sstevel@tonic-gate 				/* copy sbd_mem_stat_t structure members */
23907c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_mem.ms_interleave);
23917c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_basepfn);
23927c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_totpages);
23937c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_detpages);
23947c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_mem.ms_pageslost);
23957c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_managed_pages);
23967c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_pages);
23977c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_first);
23987c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_last);
23997c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_mem.ms_cage_enabled);
24007c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_mem.ms_peer_is_target);
24017c478bd9Sstevel@tonic-gate 				bcopy(&dsp->d_mem.ms_peer_ap_id[0],
240219397407SSherry Moore 				    &ds32p->d_mem.ms_peer_ap_id[0],
240319397407SSherry Moore 				    sizeof (ds32p->d_mem.ms_peer_ap_id));
24047c478bd9Sstevel@tonic-gate 				break;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 			case SBD_COMP_IO:
24077c478bd9Sstevel@tonic-gate 				/* copy sbd_io_stat_t structure members */
24087c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_io.is_referenced);
24097c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_io.is_unsafe_count);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 				for (j = 0; j < SBD_MAX_UNSAFE; j++)
24127c478bd9Sstevel@tonic-gate 					_SBD_DEV_STAT(int32_t,
241319397407SSherry Moore 					    d_io.is_unsafe_list[j]);
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 				bcopy(&dsp->d_io.is_pathname[0],
24167c478bd9Sstevel@tonic-gate 				    &ds32p->d_io.is_pathname[0], MAXPATHLEN);
24177c478bd9Sstevel@tonic-gate 				break;
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 			case SBD_COMP_CMP:
24207c478bd9Sstevel@tonic-gate 				/* copy sbd_cmp_stat_t structure members */
24217c478bd9Sstevel@tonic-gate 				bcopy(&dsp->d_cmp.ps_cpuid[0],
242219397407SSherry Moore 				    &ds32p->d_cmp.ps_cpuid[0],
242319397407SSherry Moore 				    sizeof (ds32p->d_cmp.ps_cpuid));
24247c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cmp.ps_ncores);
24257c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cmp.ps_speed);
24267c478bd9Sstevel@tonic-gate 				_SBD_DEV_STAT(int32_t, d_cmp.ps_ecache);
24277c478bd9Sstevel@tonic-gate 				break;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 			default:
24307c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s: unknown dev type (%d)",
24317c478bd9Sstevel@tonic-gate 				    f, (int)dsp->ds_type);
24327c478bd9Sstevel@tonic-gate 				rv = EFAULT;
24337c478bd9Sstevel@tonic-gate 				goto status_done;
24347c478bd9Sstevel@tonic-gate 			}
24357c478bd9Sstevel@tonic-gate #undef _SBD_DEV_STAT
24367c478bd9Sstevel@tonic-gate 		}
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 		if (ddi_copyout((void *)dstat32p,
244019397407SSherry Moore 		    hp->h_sbdcmd.cmd_stat.s_statp, pbsz, mode) != 0) {
24417c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
244219397407SSherry Moore 			    "%s: failed to copyout status "
244319397407SSherry Moore 			    "for board %d", f, bp->b_num);
24447c478bd9Sstevel@tonic-gate 			rv = EFAULT;
24457c478bd9Sstevel@tonic-gate 			goto status_done;
24467c478bd9Sstevel@tonic-gate 		}
24477c478bd9Sstevel@tonic-gate 	} else
24487c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	if (ddi_copyout((void *)dstatp, hp->h_sbdcmd.cmd_stat.s_statp,
245119397407SSherry Moore 	    pbsz, mode) != 0) {
24527c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
245319397407SSherry Moore 		    "%s: failed to copyout status for board %d",
245419397407SSherry Moore 		    f, bp->b_num);
24557c478bd9Sstevel@tonic-gate 		rv = EFAULT;
24567c478bd9Sstevel@tonic-gate 		goto status_done;
24577c478bd9Sstevel@tonic-gate 	}
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate status_done:
24607c478bd9Sstevel@tonic-gate 	if (dstatp != NULL)
24617c478bd9Sstevel@tonic-gate 		FREESTRUCT(dstatp, char, sz);
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	dr_unlock_status(bp);
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	return (rv);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate static int
dr_get_ncm(dr_handle_t * hp)24697c478bd9Sstevel@tonic-gate dr_get_ncm(dr_handle_t *hp)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	int		i;
24727c478bd9Sstevel@tonic-gate 	int		ncm = 0;
24737c478bd9Sstevel@tonic-gate 	dr_devset_t	devset;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	devset = DR_DEVS_PRESENT(hp->h_bd);
24767c478bd9Sstevel@tonic-gate 	if (hp->h_sbdcmd.cmd_cm.c_id.c_type != SBD_COMP_NONE)
24777c478bd9Sstevel@tonic-gate 		devset &= DEVSET(hp->h_sbdcmd.cmd_cm.c_id.c_type,
247819397407SSherry Moore 		    DEVSET_ANYUNIT);
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	/*
24817c478bd9Sstevel@tonic-gate 	 * Handle CPUs first to deal with possible CMP
24827c478bd9Sstevel@tonic-gate 	 * devices. If the CPU is a CMP, we need to only
24837c478bd9Sstevel@tonic-gate 	 * increment ncm once even if there are multiple
24847c478bd9Sstevel@tonic-gate 	 * cores for that CMP present in the devset.
24857c478bd9Sstevel@tonic-gate 	 */
24867c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CMP_UNITS_PER_BOARD; i++) {
24877c478bd9Sstevel@tonic-gate 		if (devset & DEVSET(SBD_COMP_CMP, i)) {
24887c478bd9Sstevel@tonic-gate 			ncm++;
24897c478bd9Sstevel@tonic-gate 		}
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	/* eliminate the CPU information from the devset */
24937c478bd9Sstevel@tonic-gate 	devset &= ~(DEVSET(SBD_COMP_CMP, DEVSET_ANYUNIT));
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	for (i = 0; i < (sizeof (dr_devset_t) * 8); i++) {
24967c478bd9Sstevel@tonic-gate 		ncm += devset & 0x1;
24977c478bd9Sstevel@tonic-gate 		devset >>= 1;
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	return (ncm);
25017c478bd9Sstevel@tonic-gate }
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate /* used by dr_mem.c */
25047c478bd9Sstevel@tonic-gate /* TODO: eliminate dr_boardlist */
25057c478bd9Sstevel@tonic-gate dr_board_t *
dr_lookup_board(int board_num)25067c478bd9Sstevel@tonic-gate dr_lookup_board(int board_num)
25077c478bd9Sstevel@tonic-gate {
25087c478bd9Sstevel@tonic-gate 	dr_board_t *bp;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	ASSERT(board_num >= 0 && board_num < MAX_BOARDS);
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	bp = &dr_boardlist[board_num];
25137c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_num == board_num);
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	return (bp);
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate static dr_dev_unit_t *
dr_get_dev_unit(dr_board_t * bp,sbd_comp_type_t nt,int unit_num)25197c478bd9Sstevel@tonic-gate dr_get_dev_unit(dr_board_t *bp, sbd_comp_type_t nt, int unit_num)
25207c478bd9Sstevel@tonic-gate {
25217c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	dp = DR_GET_BOARD_DEVUNIT(bp, nt, unit_num);
25247c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_bp == bp);
25257c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_unum == unit_num);
25267c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_type == nt);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	return (dp);
25297c478bd9Sstevel@tonic-gate }
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate dr_cpu_unit_t *
dr_get_cpu_unit(dr_board_t * bp,int unit_num)25327c478bd9Sstevel@tonic-gate dr_get_cpu_unit(dr_board_t *bp, int unit_num)
25337c478bd9Sstevel@tonic-gate {
25347c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	ASSERT(unit_num >= 0 && unit_num < MAX_CPU_UNITS_PER_BOARD);
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	dp = dr_get_dev_unit(bp, SBD_COMP_CPU, unit_num);
25397c478bd9Sstevel@tonic-gate 	return (&dp->du_cpu);
25407c478bd9Sstevel@tonic-gate }
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate dr_mem_unit_t *
dr_get_mem_unit(dr_board_t * bp,int unit_num)25437c478bd9Sstevel@tonic-gate dr_get_mem_unit(dr_board_t *bp, int unit_num)
25447c478bd9Sstevel@tonic-gate {
25457c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	ASSERT(unit_num >= 0 && unit_num < MAX_MEM_UNITS_PER_BOARD);
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	dp = dr_get_dev_unit(bp, SBD_COMP_MEM, unit_num);
25507c478bd9Sstevel@tonic-gate 	return (&dp->du_mem);
25517c478bd9Sstevel@tonic-gate }
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate dr_io_unit_t *
dr_get_io_unit(dr_board_t * bp,int unit_num)25547c478bd9Sstevel@tonic-gate dr_get_io_unit(dr_board_t *bp, int unit_num)
25557c478bd9Sstevel@tonic-gate {
25567c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	ASSERT(unit_num >= 0 && unit_num < MAX_IO_UNITS_PER_BOARD);
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	dp = dr_get_dev_unit(bp, SBD_COMP_IO, unit_num);
25617c478bd9Sstevel@tonic-gate 	return (&dp->du_io);
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate dr_common_unit_t *
dr_get_common_unit(dr_board_t * bp,sbd_comp_type_t nt,int unum)25657c478bd9Sstevel@tonic-gate dr_get_common_unit(dr_board_t *bp, sbd_comp_type_t nt, int unum)
25667c478bd9Sstevel@tonic-gate {
25677c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	dp = dr_get_dev_unit(bp, nt, unum);
25707c478bd9Sstevel@tonic-gate 	return (&dp->du_common);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate static dr_devset_t
dr_dev2devset(sbd_comp_id_t * cid)25747c478bd9Sstevel@tonic-gate dr_dev2devset(sbd_comp_id_t *cid)
25757c478bd9Sstevel@tonic-gate {
25767c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_dev2devset";
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	dr_devset_t	devset;
25797c478bd9Sstevel@tonic-gate 	int		unit = cid->c_unit;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	switch (cid->c_type) {
25827c478bd9Sstevel@tonic-gate 		case SBD_COMP_NONE:
25837c478bd9Sstevel@tonic-gate 			devset =  DEVSET(SBD_COMP_CPU, DEVSET_ANYUNIT);
25847c478bd9Sstevel@tonic-gate 			devset |= DEVSET(SBD_COMP_MEM, DEVSET_ANYUNIT);
25857c478bd9Sstevel@tonic-gate 			devset |= DEVSET(SBD_COMP_IO,  DEVSET_ANYUNIT);
258625cf1a30Sjl 			PR_ALL("%s: COMP_NONE devset = 0x%lx\n", f, devset);
25877c478bd9Sstevel@tonic-gate 			break;
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 		case SBD_COMP_CPU:
25907c478bd9Sstevel@tonic-gate 			if ((unit > MAX_CPU_UNITS_PER_BOARD) || (unit < 0)) {
25917c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
259219397407SSherry Moore 				    "%s: invalid cpu unit# = %d",
259319397407SSherry Moore 				    f, unit);
25947c478bd9Sstevel@tonic-gate 				devset = 0;
25957c478bd9Sstevel@tonic-gate 			} else {
25967c478bd9Sstevel@tonic-gate 				/*
25977c478bd9Sstevel@tonic-gate 				 * Generate a devset that includes all the
25987c478bd9Sstevel@tonic-gate 				 * cores of a CMP device. If this is not a
25997c478bd9Sstevel@tonic-gate 				 * CMP, the extra cores will be eliminated
26007c478bd9Sstevel@tonic-gate 				 * later since they are not present. This is
26017c478bd9Sstevel@tonic-gate 				 * also true for CMP devices that do not have
26027c478bd9Sstevel@tonic-gate 				 * all cores active.
26037c478bd9Sstevel@tonic-gate 				 */
26047c478bd9Sstevel@tonic-gate 				devset = DEVSET(SBD_COMP_CMP, unit);
26057c478bd9Sstevel@tonic-gate 			}
26067c478bd9Sstevel@tonic-gate 
260725cf1a30Sjl 			PR_ALL("%s: CPU devset = 0x%lx\n", f, devset);
26087c478bd9Sstevel@tonic-gate 			break;
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 		case SBD_COMP_MEM:
26117c478bd9Sstevel@tonic-gate 			if (unit == SBD_NULL_UNIT) {
26127c478bd9Sstevel@tonic-gate 				unit = 0;
26137c478bd9Sstevel@tonic-gate 				cid->c_unit = 0;
26147c478bd9Sstevel@tonic-gate 			}
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 			if ((unit > MAX_MEM_UNITS_PER_BOARD) || (unit < 0)) {
26177c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
261819397407SSherry Moore 				    "%s: invalid mem unit# = %d",
261919397407SSherry Moore 				    f, unit);
26207c478bd9Sstevel@tonic-gate 				devset = 0;
26217c478bd9Sstevel@tonic-gate 			} else
26227c478bd9Sstevel@tonic-gate 				devset = DEVSET(cid->c_type, unit);
26237c478bd9Sstevel@tonic-gate 
262425cf1a30Sjl 			PR_ALL("%s: MEM devset = 0x%lx\n", f, devset);
26257c478bd9Sstevel@tonic-gate 			break;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 		case SBD_COMP_IO:
26287c478bd9Sstevel@tonic-gate 			if ((unit > MAX_IO_UNITS_PER_BOARD) || (unit < 0)) {
26297c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
263019397407SSherry Moore 				    "%s: invalid io unit# = %d",
263119397407SSherry Moore 				    f, unit);
26327c478bd9Sstevel@tonic-gate 				devset = 0;
26337c478bd9Sstevel@tonic-gate 			} else
26347c478bd9Sstevel@tonic-gate 				devset = DEVSET(cid->c_type, unit);
26357c478bd9Sstevel@tonic-gate 
263625cf1a30Sjl 			PR_ALL("%s: IO devset = 0x%lx\n", f, devset);
26377c478bd9Sstevel@tonic-gate 			break;
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 		default:
26407c478bd9Sstevel@tonic-gate 		case SBD_COMP_UNKNOWN:
26417c478bd9Sstevel@tonic-gate 			devset = 0;
26427c478bd9Sstevel@tonic-gate 			break;
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	return (devset);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  * Converts a dynamic attachment point name to a SBD_COMP_* type.
26507c478bd9Sstevel@tonic-gate  * Returns SDB_COMP_UNKNOWN if name is not recognized.
26517c478bd9Sstevel@tonic-gate  */
26527c478bd9Sstevel@tonic-gate static int
dr_dev_type_to_nt(char * type)26537c478bd9Sstevel@tonic-gate dr_dev_type_to_nt(char *type)
26547c478bd9Sstevel@tonic-gate {
26557c478bd9Sstevel@tonic-gate 	int i;
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
26587c478bd9Sstevel@tonic-gate 		if (strcmp(dr_devattr[i].s_devtype, type) == 0)
26597c478bd9Sstevel@tonic-gate 			break;
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	return (dr_devattr[i].s_nodetype);
26627c478bd9Sstevel@tonic-gate }
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate /*
26657c478bd9Sstevel@tonic-gate  * Converts a SBD_COMP_* type to a dynamic attachment point name.
26667c478bd9Sstevel@tonic-gate  * Return NULL if SBD_COMP_ type is not recognized.
26677c478bd9Sstevel@tonic-gate  */
26687c478bd9Sstevel@tonic-gate char *
dr_nt_to_dev_type(int nt)26697c478bd9Sstevel@tonic-gate dr_nt_to_dev_type(int nt)
26707c478bd9Sstevel@tonic-gate {
26717c478bd9Sstevel@tonic-gate 	int i;
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
26747c478bd9Sstevel@tonic-gate 		if (dr_devattr[i].s_nodetype == nt)
26757c478bd9Sstevel@tonic-gate 			break;
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	return (dr_devattr[i].s_devtype);
26787c478bd9Sstevel@tonic-gate }
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate /*
26827c478bd9Sstevel@tonic-gate  * State transition policy is that if there is some component for which
26837c478bd9Sstevel@tonic-gate  * the state transition is valid, then let it through. The exception is
26847c478bd9Sstevel@tonic-gate  * SBD_CMD_DISCONNECT. On disconnect, the state transition must be valid
26857c478bd9Sstevel@tonic-gate  * for ALL components.
26867c478bd9Sstevel@tonic-gate  * Returns the state that is in error, if any.
26877c478bd9Sstevel@tonic-gate  */
26887c478bd9Sstevel@tonic-gate static int
dr_check_transition(dr_board_t * bp,dr_devset_t * devsetp,struct dr_state_trans * transp,int cmd)26897c478bd9Sstevel@tonic-gate dr_check_transition(dr_board_t *bp, dr_devset_t *devsetp,
2690*c3937c08SToomas Soome     struct dr_state_trans *transp, int cmd)
26917c478bd9Sstevel@tonic-gate {
26927c478bd9Sstevel@tonic-gate 	int			s, ut;
26937c478bd9Sstevel@tonic-gate 	int			state_err = 0;
26947c478bd9Sstevel@tonic-gate 	dr_devset_t		devset;
26957c478bd9Sstevel@tonic-gate 	dr_common_unit_t	*cp;
26967c478bd9Sstevel@tonic-gate 	static fn_t		f = "dr_check_transition";
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	devset = *devsetp;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
27017c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
27027c478bd9Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_CPU, ut) == 0)
27037c478bd9Sstevel@tonic-gate 				continue;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 			cp = dr_get_common_unit(bp, SBD_COMP_CPU, ut);
27067c478bd9Sstevel@tonic-gate 			s = (int)cp->sbdev_state;
27077c478bd9Sstevel@tonic-gate 			if (!DR_DEV_IS_PRESENT(cp)) {
27087c478bd9Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_CPU, ut);
27097c478bd9Sstevel@tonic-gate 			} else {
27107c478bd9Sstevel@tonic-gate 				if (transp->x_op[s].x_rv) {
27117c478bd9Sstevel@tonic-gate 					if (!state_err)
27127c478bd9Sstevel@tonic-gate 						state_err = s;
27137c478bd9Sstevel@tonic-gate 					DEVSET_DEL(devset, SBD_COMP_CPU, ut);
27147c478bd9Sstevel@tonic-gate 				}
27157c478bd9Sstevel@tonic-gate 			}
27167c478bd9Sstevel@tonic-gate 		}
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
27197c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
27207c478bd9Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_MEM, ut) == 0)
27217c478bd9Sstevel@tonic-gate 				continue;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 			cp = dr_get_common_unit(bp, SBD_COMP_MEM, ut);
27247c478bd9Sstevel@tonic-gate 			s = (int)cp->sbdev_state;
27257c478bd9Sstevel@tonic-gate 			if (!DR_DEV_IS_PRESENT(cp)) {
27267c478bd9Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_MEM, ut);
27277c478bd9Sstevel@tonic-gate 			} else {
27287c478bd9Sstevel@tonic-gate 				if (transp->x_op[s].x_rv) {
27297c478bd9Sstevel@tonic-gate 					if (!state_err)
27307c478bd9Sstevel@tonic-gate 						state_err = s;
27317c478bd9Sstevel@tonic-gate 					DEVSET_DEL(devset, SBD_COMP_MEM, ut);
27327c478bd9Sstevel@tonic-gate 				}
27337c478bd9Sstevel@tonic-gate 			}
27347c478bd9Sstevel@tonic-gate 		}
27357c478bd9Sstevel@tonic-gate 	}
27367c478bd9Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
27377c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
27387c478bd9Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_IO, ut) == 0)
27397c478bd9Sstevel@tonic-gate 				continue;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 			cp = dr_get_common_unit(bp, SBD_COMP_IO, ut);
27427c478bd9Sstevel@tonic-gate 			s = (int)cp->sbdev_state;
27437c478bd9Sstevel@tonic-gate 			if (!DR_DEV_IS_PRESENT(cp)) {
27447c478bd9Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_IO, ut);
27457c478bd9Sstevel@tonic-gate 			} else {
27467c478bd9Sstevel@tonic-gate 				if (transp->x_op[s].x_rv) {
27477c478bd9Sstevel@tonic-gate 					if (!state_err)
27487c478bd9Sstevel@tonic-gate 						state_err = s;
27497c478bd9Sstevel@tonic-gate 					DEVSET_DEL(devset, SBD_COMP_IO, ut);
27507c478bd9Sstevel@tonic-gate 				}
27517c478bd9Sstevel@tonic-gate 			}
27527c478bd9Sstevel@tonic-gate 		}
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	PR_ALL("%s: requested devset = 0x%x, final devset = 0x%x\n",
275619397407SSherry Moore 	    f, (uint_t)*devsetp, (uint_t)devset);
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	*devsetp = devset;
27597c478bd9Sstevel@tonic-gate 	/*
27607c478bd9Sstevel@tonic-gate 	 * If there are some remaining components for which
27617c478bd9Sstevel@tonic-gate 	 * this state transition is valid, then allow them
27627c478bd9Sstevel@tonic-gate 	 * through, otherwise if none are left then return
27637c478bd9Sstevel@tonic-gate 	 * the state error. The exception is SBD_CMD_DISCONNECT.
27647c478bd9Sstevel@tonic-gate 	 * On disconnect, the state transition must be valid for ALL
27657c478bd9Sstevel@tonic-gate 	 * components.
27667c478bd9Sstevel@tonic-gate 	 */
27677c478bd9Sstevel@tonic-gate 	if (cmd == SBD_CMD_DISCONNECT)
27687c478bd9Sstevel@tonic-gate 		return (state_err);
27697c478bd9Sstevel@tonic-gate 	return (devset ? 0 : state_err);
27707c478bd9Sstevel@tonic-gate }
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate void
dr_device_transition(dr_common_unit_t * cp,dr_state_t st)27737c478bd9Sstevel@tonic-gate dr_device_transition(dr_common_unit_t *cp, dr_state_t st)
27747c478bd9Sstevel@tonic-gate {
27757c478bd9Sstevel@tonic-gate 	PR_STATE("%s STATE %s(%d) -> %s(%d)\n",
277619397407SSherry Moore 	    cp->sbdev_path,
277719397407SSherry Moore 	    state_str[cp->sbdev_state], cp->sbdev_state,
277819397407SSherry Moore 	    state_str[st], st);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	cp->sbdev_state = st;
27817c478bd9Sstevel@tonic-gate 	if (st == DR_STATE_CONFIGURED) {
27827c478bd9Sstevel@tonic-gate 		cp->sbdev_ostate = SBD_STAT_CONFIGURED;
27837c478bd9Sstevel@tonic-gate 		if (cp->sbdev_bp->b_ostate != SBD_STAT_CONFIGURED) {
27847c478bd9Sstevel@tonic-gate 			cp->sbdev_bp->b_ostate = SBD_STAT_CONFIGURED;
27857c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME,
278619397407SSherry Moore 			    (void *) &cp->sbdev_bp->b_time);
27877c478bd9Sstevel@tonic-gate 		}
27887c478bd9Sstevel@tonic-gate 	} else
27897c478bd9Sstevel@tonic-gate 		cp->sbdev_ostate = SBD_STAT_UNCONFIGURED;
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	(void) drv_getparm(TIME, (void *) &cp->sbdev_time);
27927c478bd9Sstevel@tonic-gate }
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate static void
dr_board_transition(dr_board_t * bp,dr_state_t st)27957c478bd9Sstevel@tonic-gate dr_board_transition(dr_board_t *bp, dr_state_t st)
27967c478bd9Sstevel@tonic-gate {
27977c478bd9Sstevel@tonic-gate 	PR_STATE("BOARD %d STATE: %s(%d) -> %s(%d)\n",
279819397407SSherry Moore 	    bp->b_num,
279919397407SSherry Moore 	    state_str[bp->b_state], bp->b_state,
280019397407SSherry Moore 	    state_str[st], st);
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	bp->b_state = st;
28037c478bd9Sstevel@tonic-gate }
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate void
dr_op_err(int ce,dr_handle_t * hp,int code,char * fmt,...)28067c478bd9Sstevel@tonic-gate dr_op_err(int ce, dr_handle_t *hp, int code, char *fmt, ...)
28077c478bd9Sstevel@tonic-gate {
28087c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
28097c478bd9Sstevel@tonic-gate 	va_list		args;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
28127c478bd9Sstevel@tonic-gate 	err = drerr_new_v(code, fmt, args);
28137c478bd9Sstevel@tonic-gate 	va_end(args);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	if (ce != CE_IGNORE)
28167c478bd9Sstevel@tonic-gate 		sbd_err_log(err, ce);
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	DRERR_SET_C(&hp->h_err, &err);
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate void
dr_dev_err(int ce,dr_common_unit_t * cp,int code)28227c478bd9Sstevel@tonic-gate dr_dev_err(int ce, dr_common_unit_t *cp, int code)
28237c478bd9Sstevel@tonic-gate {
28247c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	err = drerr_new(0, code, cp->sbdev_path, NULL);
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	if (ce != CE_IGNORE)
28297c478bd9Sstevel@tonic-gate 		sbd_err_log(err, ce);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	DRERR_SET_C(&cp->sbdev_error, &err);
28327c478bd9Sstevel@tonic-gate }
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate /*
28357c478bd9Sstevel@tonic-gate  * A callback routine.  Called from the drmach layer as a result of
28367c478bd9Sstevel@tonic-gate  * call to drmach_board_find_devices from dr_init_devlists.
28377c478bd9Sstevel@tonic-gate  */
28387c478bd9Sstevel@tonic-gate static sbd_error_t *
dr_dev_found(void * data,const char * name,int unum,drmachid_t id)28397c478bd9Sstevel@tonic-gate dr_dev_found(void *data, const char *name, int unum, drmachid_t id)
28407c478bd9Sstevel@tonic-gate {
28417c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = data;
28427c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
28437c478bd9Sstevel@tonic-gate 	int		 nt;
28447c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_dev_found";
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	PR_ALL("%s (board = %d, name = %s, unum = %d, id = %p)...\n",
284719397407SSherry Moore 	    f, bp->b_num, name, unum, id);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	nt = dr_dev_type_to_nt((char *)name);
28507c478bd9Sstevel@tonic-gate 	if (nt == SBD_COMP_UNKNOWN) {
28517c478bd9Sstevel@tonic-gate 		/*
28527c478bd9Sstevel@tonic-gate 		 * this should not happen.  When it does, it indicates
28537c478bd9Sstevel@tonic-gate 		 * a missmatch in devices supported by the drmach layer
28547c478bd9Sstevel@tonic-gate 		 * vs devices supported by this layer.
28557c478bd9Sstevel@tonic-gate 		 */
28567c478bd9Sstevel@tonic-gate 		return (DR_INTERNAL_ERROR());
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	dp = DR_GET_BOARD_DEVUNIT(bp, nt, unum);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	/* sanity check */
28627c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_bp == bp);
28637c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_unum == unum);
28647c478bd9Sstevel@tonic-gate 	ASSERT(dp->du_common.sbdev_type == nt);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	/* render dynamic attachment point path of this unit */
28677c478bd9Sstevel@tonic-gate 	(void) snprintf(dp->du_common.sbdev_path,
286819397407SSherry Moore 	    sizeof (dp->du_common.sbdev_path),
286919397407SSherry Moore 	    (nt == SBD_COMP_MEM ? "%s::%s" : "%s::%s%d"),
287019397407SSherry Moore 	    bp->b_path, name, DR_UNUM2SBD_UNUM(unum, nt));
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	dp->du_common.sbdev_id = id;
28737c478bd9Sstevel@tonic-gate 	DR_DEV_SET_PRESENT(&dp->du_common);
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 	bp->b_ndev++;
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	return (NULL);
28787c478bd9Sstevel@tonic-gate }
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate static sbd_error_t *
dr_init_devlists(dr_board_t * bp)28817c478bd9Sstevel@tonic-gate dr_init_devlists(dr_board_t *bp)
28827c478bd9Sstevel@tonic-gate {
28837c478bd9Sstevel@tonic-gate 	int		i;
28847c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
28857c478bd9Sstevel@tonic-gate 	dr_dev_unit_t	*dp;
28867c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_init_devlists";
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	PR_ALL("%s (%s)...\n", f, bp->b_path);
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	/* sanity check */
28917c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_ndev == 0);
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	DR_DEVS_DISCONNECT(bp, (uint_t)-1);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	/*
28967c478bd9Sstevel@tonic-gate 	 * This routine builds the board's devlist and initializes
28977c478bd9Sstevel@tonic-gate 	 * the common portion of the unit data structures.
28987c478bd9Sstevel@tonic-gate 	 * Note: because the common portion is considered
28997c478bd9Sstevel@tonic-gate 	 * uninitialized, the dr_get_*_unit() routines can not
29007c478bd9Sstevel@tonic-gate 	 * be used.
29017c478bd9Sstevel@tonic-gate 	 */
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	/*
29047c478bd9Sstevel@tonic-gate 	 * Clear out old entries, if any.
29057c478bd9Sstevel@tonic-gate 	 */
29067c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
29077c478bd9Sstevel@tonic-gate 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_CPU, i);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 		bzero(dp, sizeof (*dp));
29107c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_bp = bp;
29117c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_unum = i;
29127c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_type = SBD_COMP_CPU;
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
29167c478bd9Sstevel@tonic-gate 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_MEM, i);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 		bzero(dp, sizeof (*dp));
29197c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_bp = bp;
29207c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_unum = i;
29217c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_type = SBD_COMP_MEM;
29227c478bd9Sstevel@tonic-gate 	}
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
29257c478bd9Sstevel@tonic-gate 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_IO, i);
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 		bzero(dp, sizeof (*dp));
29287c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_bp = bp;
29297c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_unum = i;
29307c478bd9Sstevel@tonic-gate 		dp->du_common.sbdev_type = SBD_COMP_IO;
29317c478bd9Sstevel@tonic-gate 	}
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	err = NULL;
29347c478bd9Sstevel@tonic-gate 	if (bp->b_id) {
29357c478bd9Sstevel@tonic-gate 		/* find devices on this board */
29367c478bd9Sstevel@tonic-gate 		err = drmach_board_find_devices(
293719397407SSherry Moore 		    bp->b_id, bp, dr_dev_found);
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	return (err);
29417c478bd9Sstevel@tonic-gate }
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate /*
29447c478bd9Sstevel@tonic-gate  * Return the unit number of the respective drmachid if
29457c478bd9Sstevel@tonic-gate  * it's found to be attached.
29467c478bd9Sstevel@tonic-gate  */
29477c478bd9Sstevel@tonic-gate static int
dr_check_unit_attached(dr_common_unit_t * cp)29487c478bd9Sstevel@tonic-gate dr_check_unit_attached(dr_common_unit_t *cp)
29497c478bd9Sstevel@tonic-gate {
29507c478bd9Sstevel@tonic-gate 	int		rv = 0;
29517c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
29527c478bd9Sstevel@tonic-gate 	uint64_t	basepa, endpa;
29537c478bd9Sstevel@tonic-gate 	struct memlist	*ml;
29547c478bd9Sstevel@tonic-gate 	extern struct memlist	*phys_install;
29557c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
29567c478bd9Sstevel@tonic-gate 	int		yes;
29577c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_check_unit_attached";
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	switch (cp->sbdev_type) {
29607c478bd9Sstevel@tonic-gate 	case SBD_COMP_CPU:
29617c478bd9Sstevel@tonic-gate 		err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
29627c478bd9Sstevel@tonic-gate 		if (err) {
29637c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbdev_error, &err);
29647c478bd9Sstevel@tonic-gate 			rv = -1;
29657c478bd9Sstevel@tonic-gate 			break;
29667c478bd9Sstevel@tonic-gate 		}
29677c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
29687c478bd9Sstevel@tonic-gate 		if (cpu_get(cpuid) == NULL)
29697c478bd9Sstevel@tonic-gate 			rv = -1;
29707c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
29717c478bd9Sstevel@tonic-gate 		break;
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	case SBD_COMP_MEM:
29747c478bd9Sstevel@tonic-gate 		err = drmach_mem_get_base_physaddr(cp->sbdev_id, &basepa);
29757c478bd9Sstevel@tonic-gate 		if (err) {
29767c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbdev_error, &err);
29777c478bd9Sstevel@tonic-gate 			rv = -1;
29787c478bd9Sstevel@tonic-gate 			break;
29797c478bd9Sstevel@tonic-gate 		}
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 		/*
29827c478bd9Sstevel@tonic-gate 		 * basepa may not be on a alignment boundary, make it so.
29837c478bd9Sstevel@tonic-gate 		 */
29847c478bd9Sstevel@tonic-gate 		err = drmach_mem_get_slice_size(cp->sbdev_id, &endpa);
29857c478bd9Sstevel@tonic-gate 		if (err) {
29867c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbdev_error, &err);
29877c478bd9Sstevel@tonic-gate 			rv = -1;
29887c478bd9Sstevel@tonic-gate 			break;
29897c478bd9Sstevel@tonic-gate 		}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 		basepa &= ~(endpa - 1);
29927c478bd9Sstevel@tonic-gate 		endpa += basepa;
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 		/*
29957c478bd9Sstevel@tonic-gate 		 * Check if base address is in phys_install.
29967c478bd9Sstevel@tonic-gate 		 */
29977c478bd9Sstevel@tonic-gate 		memlist_read_lock();
299856f33205SJonathan Adams 		for (ml = phys_install; ml; ml = ml->ml_next)
299956f33205SJonathan Adams 			if ((endpa <= ml->ml_address) ||
300056f33205SJonathan Adams 			    (basepa >= (ml->ml_address + ml->ml_size)))
30017c478bd9Sstevel@tonic-gate 				continue;
30027c478bd9Sstevel@tonic-gate 			else
30037c478bd9Sstevel@tonic-gate 				break;
30047c478bd9Sstevel@tonic-gate 		memlist_read_unlock();
30057c478bd9Sstevel@tonic-gate 		if (ml == NULL)
30067c478bd9Sstevel@tonic-gate 			rv = -1;
30077c478bd9Sstevel@tonic-gate 		break;
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	case SBD_COMP_IO:
30107c478bd9Sstevel@tonic-gate 		err = drmach_io_is_attached(cp->sbdev_id, &yes);
30117c478bd9Sstevel@tonic-gate 		if (err) {
30127c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbdev_error, &err);
30137c478bd9Sstevel@tonic-gate 			rv = -1;
30147c478bd9Sstevel@tonic-gate 			break;
30157c478bd9Sstevel@tonic-gate 		} else if (!yes)
30167c478bd9Sstevel@tonic-gate 			rv = -1;
30177c478bd9Sstevel@tonic-gate 		break;
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	default:
302004580fdfSmathue 		PR_ALL("%s: unexpected nodetype(%d) for id 0x%p\n",
302119397407SSherry Moore 		    f, cp->sbdev_type, cp->sbdev_id);
30227c478bd9Sstevel@tonic-gate 		rv = -1;
30237c478bd9Sstevel@tonic-gate 		break;
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	return (rv);
30277c478bd9Sstevel@tonic-gate }
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate /*
30307c478bd9Sstevel@tonic-gate  * See if drmach recognizes the passthru command.  DRMACH expects the
30317c478bd9Sstevel@tonic-gate  * id to identify the thing to which the command is being applied.  Using
30327c478bd9Sstevel@tonic-gate  * nonsense SBD terms, that information has been perversely encoded in the
30337c478bd9Sstevel@tonic-gate  * c_id member of the sbd_cmd_t structure.  This logic reads those tea
30347c478bd9Sstevel@tonic-gate  * leaves, finds the associated drmach id, then calls drmach to process
30357c478bd9Sstevel@tonic-gate  * the passthru command.
30367c478bd9Sstevel@tonic-gate  */
30377c478bd9Sstevel@tonic-gate static int
dr_pt_try_drmach(dr_handle_t * hp)30387c478bd9Sstevel@tonic-gate dr_pt_try_drmach(dr_handle_t *hp)
30397c478bd9Sstevel@tonic-gate {
30407c478bd9Sstevel@tonic-gate 	dr_board_t	*bp = hp->h_bd;
30417c478bd9Sstevel@tonic-gate 	sbd_comp_id_t	*comp_id = &hp->h_sbdcmd.cmd_cm.c_id;
30427c478bd9Sstevel@tonic-gate 	drmachid_t	 id;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	if (comp_id->c_type == SBD_COMP_NONE) {
30457c478bd9Sstevel@tonic-gate 		id = bp->b_id;
30467c478bd9Sstevel@tonic-gate 	} else {
30477c478bd9Sstevel@tonic-gate 		sbd_comp_type_t	 nt;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 		nt = dr_dev_type_to_nt(comp_id->c_name);
30507c478bd9Sstevel@tonic-gate 		if (nt == SBD_COMP_UNKNOWN) {
30517c478bd9Sstevel@tonic-gate 			dr_op_err(CE_IGNORE, hp, ESBD_INVAL, comp_id->c_name);
30527c478bd9Sstevel@tonic-gate 			id = 0;
30537c478bd9Sstevel@tonic-gate 		} else {
30547c478bd9Sstevel@tonic-gate 			/* pt command applied to dynamic attachment point */
30557c478bd9Sstevel@tonic-gate 			dr_common_unit_t *cp;
30567c478bd9Sstevel@tonic-gate 			cp = dr_get_common_unit(bp, nt, comp_id->c_unit);
30577c478bd9Sstevel@tonic-gate 			id = cp->sbdev_id;
30587c478bd9Sstevel@tonic-gate 		}
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	if (hp->h_err == NULL)
30627c478bd9Sstevel@tonic-gate 		hp->h_err = drmach_passthru(id, &hp->h_opts);
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	return (hp->h_err == NULL ? 0 : -1);
30657c478bd9Sstevel@tonic-gate }
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate static int
dr_pt_ioctl(dr_handle_t * hp)30687c478bd9Sstevel@tonic-gate dr_pt_ioctl(dr_handle_t *hp)
30697c478bd9Sstevel@tonic-gate {
30707c478bd9Sstevel@tonic-gate 	int		cmd, rv, len;
30717c478bd9Sstevel@tonic-gate 	int32_t		sz;
30727c478bd9Sstevel@tonic-gate 	int		found;
30737c478bd9Sstevel@tonic-gate 	char		*copts;
30747c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_pt_ioctl";
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	sz = hp->h_opts.size;
30797c478bd9Sstevel@tonic-gate 	copts = hp->h_opts.copts;
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if (sz == 0 || copts == (char *)NULL) {
30827c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: invalid passthru args", f);
30837c478bd9Sstevel@tonic-gate 		return (EINVAL);
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	found = 0;
30877c478bd9Sstevel@tonic-gate 	for (cmd = 0; cmd < (sizeof (pt_arr) / sizeof (pt_arr[0])); cmd++) {
30887c478bd9Sstevel@tonic-gate 		len = strlen(pt_arr[cmd].pt_name);
30897c478bd9Sstevel@tonic-gate 		found = (strncmp(pt_arr[cmd].pt_name, copts, len) == 0);
30907c478bd9Sstevel@tonic-gate 		if (found)
30917c478bd9Sstevel@tonic-gate 			break;
30927c478bd9Sstevel@tonic-gate 	}
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	if (found)
30957c478bd9Sstevel@tonic-gate 		rv = (*pt_arr[cmd].pt_func)(hp);
30967c478bd9Sstevel@tonic-gate 	else
30977c478bd9Sstevel@tonic-gate 		rv = dr_pt_try_drmach(hp);
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	return (rv);
31007c478bd9Sstevel@tonic-gate }
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate /*
31037c478bd9Sstevel@tonic-gate  * Called at driver load time to determine the state and condition
31047c478bd9Sstevel@tonic-gate  * of an existing board in the system.
31057c478bd9Sstevel@tonic-gate  */
31067c478bd9Sstevel@tonic-gate static void
dr_board_discovery(dr_board_t * bp)31077c478bd9Sstevel@tonic-gate dr_board_discovery(dr_board_t *bp)
31087c478bd9Sstevel@tonic-gate {
31097c478bd9Sstevel@tonic-gate 	int			i;
31107c478bd9Sstevel@tonic-gate 	dr_devset_t		devs_lost, devs_attached = 0;
31117c478bd9Sstevel@tonic-gate 	dr_cpu_unit_t		*cp;
31127c478bd9Sstevel@tonic-gate 	dr_mem_unit_t		*mp;
31137c478bd9Sstevel@tonic-gate 	dr_io_unit_t		*ip;
31147c478bd9Sstevel@tonic-gate 	static fn_t		f = "dr_board_discovery";
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	if (DR_DEVS_PRESENT(bp) == 0) {
31177c478bd9Sstevel@tonic-gate 		PR_ALL("%s: board %d has no devices present\n",
311819397407SSherry Moore 		    f, bp->b_num);
31197c478bd9Sstevel@tonic-gate 		return;
31207c478bd9Sstevel@tonic-gate 	}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	/*
31237c478bd9Sstevel@tonic-gate 	 * Check for existence of cpus.
31247c478bd9Sstevel@tonic-gate 	 */
31257c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
31267c478bd9Sstevel@tonic-gate 		cp = dr_get_cpu_unit(bp, i);
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 		if (!DR_DEV_IS_PRESENT(&cp->sbc_cm))
31297c478bd9Sstevel@tonic-gate 			continue;
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 		if (dr_check_unit_attached(&cp->sbc_cm) >= 0) {
31327c478bd9Sstevel@tonic-gate 			DR_DEV_SET_ATTACHED(&cp->sbc_cm);
31337c478bd9Sstevel@tonic-gate 			DEVSET_ADD(devs_attached, SBD_COMP_CPU, i);
31347c478bd9Sstevel@tonic-gate 			PR_ALL("%s: board %d, cpu-unit %d - attached\n",
313519397407SSherry Moore 			    f, bp->b_num, i);
31367c478bd9Sstevel@tonic-gate 		}
31377c478bd9Sstevel@tonic-gate 		dr_init_cpu_unit(cp);
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	/*
31417c478bd9Sstevel@tonic-gate 	 * Check for existence of memory.
31427c478bd9Sstevel@tonic-gate 	 */
31437c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
31447c478bd9Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, i);
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 		if (!DR_DEV_IS_PRESENT(&mp->sbm_cm))
31477c478bd9Sstevel@tonic-gate 			continue;
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 		if (dr_check_unit_attached(&mp->sbm_cm) >= 0) {
31507c478bd9Sstevel@tonic-gate 			DR_DEV_SET_ATTACHED(&mp->sbm_cm);
31517c478bd9Sstevel@tonic-gate 			DEVSET_ADD(devs_attached, SBD_COMP_MEM, i);
31527c478bd9Sstevel@tonic-gate 			PR_ALL("%s: board %d, mem-unit %d - attached\n",
315319397407SSherry Moore 			    f, bp->b_num, i);
31547c478bd9Sstevel@tonic-gate 		}
31557c478bd9Sstevel@tonic-gate 		dr_init_mem_unit(mp);
31567c478bd9Sstevel@tonic-gate 	}
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate 	/*
31597c478bd9Sstevel@tonic-gate 	 * Check for i/o state.
31607c478bd9Sstevel@tonic-gate 	 */
31617c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
31627c478bd9Sstevel@tonic-gate 		ip = dr_get_io_unit(bp, i);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 		if (!DR_DEV_IS_PRESENT(&ip->sbi_cm))
31657c478bd9Sstevel@tonic-gate 			continue;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 		if (dr_check_unit_attached(&ip->sbi_cm) >= 0) {
31687c478bd9Sstevel@tonic-gate 			/*
31697c478bd9Sstevel@tonic-gate 			 * Found it!
31707c478bd9Sstevel@tonic-gate 			 */
31717c478bd9Sstevel@tonic-gate 			DR_DEV_SET_ATTACHED(&ip->sbi_cm);
31727c478bd9Sstevel@tonic-gate 			DEVSET_ADD(devs_attached, SBD_COMP_IO, i);
31737c478bd9Sstevel@tonic-gate 			PR_ALL("%s: board %d, io-unit %d - attached\n",
317419397407SSherry Moore 			    f, bp->b_num, i);
31757c478bd9Sstevel@tonic-gate 		}
31767c478bd9Sstevel@tonic-gate 		dr_init_io_unit(ip);
31777c478bd9Sstevel@tonic-gate 	}
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	DR_DEVS_CONFIGURE(bp, devs_attached);
31807c478bd9Sstevel@tonic-gate 	if (devs_attached && ((devs_lost = DR_DEVS_UNATTACHED(bp)) != 0)) {
31817c478bd9Sstevel@tonic-gate 		int		ut;
31827c478bd9Sstevel@tonic-gate 		/*
31837c478bd9Sstevel@tonic-gate 		 * It is not legal on board discovery to have a
31847c478bd9Sstevel@tonic-gate 		 * board that is only partially attached.  A board
31857c478bd9Sstevel@tonic-gate 		 * is either all attached or all connected.  If a
31867c478bd9Sstevel@tonic-gate 		 * board has at least one attached device, then
31877c478bd9Sstevel@tonic-gate 		 * the the remaining devices, if any, must have
31887c478bd9Sstevel@tonic-gate 		 * been lost or disconnected.  These devices can
31897c478bd9Sstevel@tonic-gate 		 * only be recovered by a full attach from scratch.
31907c478bd9Sstevel@tonic-gate 		 * Note that devices previously in the unreferenced
31917c478bd9Sstevel@tonic-gate 		 * state are subsequently lost until the next full
31927c478bd9Sstevel@tonic-gate 		 * attach.  This is necessary since the driver unload
31937c478bd9Sstevel@tonic-gate 		 * that must have occurred would have wiped out the
31947c478bd9Sstevel@tonic-gate 		 * information necessary to re-configure the device
31957c478bd9Sstevel@tonic-gate 		 * back online, e.g. memlist.
31967c478bd9Sstevel@tonic-gate 		 */
319725cf1a30Sjl 		PR_ALL("%s: some devices LOST (0x%lx)...\n", f, devs_lost);
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
32007c478bd9Sstevel@tonic-gate 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_CPU, ut))
32017c478bd9Sstevel@tonic-gate 				continue;
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 			cp = dr_get_cpu_unit(bp, ut);
32047c478bd9Sstevel@tonic-gate 			dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
32057c478bd9Sstevel@tonic-gate 		}
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
32087c478bd9Sstevel@tonic-gate 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_MEM, ut))
32097c478bd9Sstevel@tonic-gate 				continue;
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 			mp = dr_get_mem_unit(bp, ut);
32127c478bd9Sstevel@tonic-gate 			dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
32137c478bd9Sstevel@tonic-gate 		}
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
32167c478bd9Sstevel@tonic-gate 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_IO, ut))
32177c478bd9Sstevel@tonic-gate 				continue;
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 			ip = dr_get_io_unit(bp, ut);
32207c478bd9Sstevel@tonic-gate 			dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
32217c478bd9Sstevel@tonic-gate 		}
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 		DR_DEVS_DISCONNECT(bp, devs_lost);
32247c478bd9Sstevel@tonic-gate 	}
32257c478bd9Sstevel@tonic-gate }
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate static int
dr_board_init(dr_board_t * bp,dev_info_t * dip,int bd)32287c478bd9Sstevel@tonic-gate dr_board_init(dr_board_t *bp, dev_info_t *dip, int bd)
32297c478bd9Sstevel@tonic-gate {
32307c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 	mutex_init(&bp->b_lock, NULL, MUTEX_DRIVER, NULL);
32337c478bd9Sstevel@tonic-gate 	mutex_init(&bp->b_slock, NULL, MUTEX_DRIVER, NULL);
32347c478bd9Sstevel@tonic-gate 	cv_init(&bp->b_scv, NULL, CV_DRIVER, NULL);
32357c478bd9Sstevel@tonic-gate 	bp->b_rstate = SBD_STAT_EMPTY;
32367c478bd9Sstevel@tonic-gate 	bp->b_ostate = SBD_STAT_UNCONFIGURED;
32377c478bd9Sstevel@tonic-gate 	bp->b_cond = SBD_COND_UNKNOWN;
32387c478bd9Sstevel@tonic-gate 	(void) drv_getparm(TIME, (void *)&bp->b_time);
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	(void) drmach_board_lookup(bd, &bp->b_id);
32417c478bd9Sstevel@tonic-gate 	bp->b_num = bd;
32427c478bd9Sstevel@tonic-gate 	bp->b_dip = dip;
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_CPU)] = GETSTRUCT(dr_dev_unit_t,
324519397407SSherry Moore 	    MAX_CPU_UNITS_PER_BOARD);
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_MEM)] = GETSTRUCT(dr_dev_unit_t,
324819397407SSherry Moore 	    MAX_MEM_UNITS_PER_BOARD);
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_IO)] = GETSTRUCT(dr_dev_unit_t,
325119397407SSherry Moore 	    MAX_IO_UNITS_PER_BOARD);
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	/*
32547c478bd9Sstevel@tonic-gate 	 * Initialize the devlists
32557c478bd9Sstevel@tonic-gate 	 */
32567c478bd9Sstevel@tonic-gate 	err = dr_init_devlists(bp);
32577c478bd9Sstevel@tonic-gate 	if (err) {
32587c478bd9Sstevel@tonic-gate 		sbd_err_clear(&err);
32597c478bd9Sstevel@tonic-gate 		dr_board_destroy(bp);
32607c478bd9Sstevel@tonic-gate 		return (-1);
32617c478bd9Sstevel@tonic-gate 	} else if (bp->b_ndev == 0) {
32627c478bd9Sstevel@tonic-gate 		dr_board_transition(bp, DR_STATE_EMPTY);
32637c478bd9Sstevel@tonic-gate 	} else {
32647c478bd9Sstevel@tonic-gate 		/*
32657c478bd9Sstevel@tonic-gate 		 * Couldn't have made it down here without
32667c478bd9Sstevel@tonic-gate 		 * having found at least one device.
32677c478bd9Sstevel@tonic-gate 		 */
32687c478bd9Sstevel@tonic-gate 		ASSERT(DR_DEVS_PRESENT(bp) != 0);
32697c478bd9Sstevel@tonic-gate 		/*
32707c478bd9Sstevel@tonic-gate 		 * Check the state of any possible devices on the
32717c478bd9Sstevel@tonic-gate 		 * board.
32727c478bd9Sstevel@tonic-gate 		 */
32737c478bd9Sstevel@tonic-gate 		dr_board_discovery(bp);
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 		bp->b_assigned = 1;
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 		if (DR_DEVS_UNATTACHED(bp) == 0) {
32787c478bd9Sstevel@tonic-gate 			/*
32797c478bd9Sstevel@tonic-gate 			 * The board has no unattached devices, therefore
32807c478bd9Sstevel@tonic-gate 			 * by reason of insanity it must be configured!
32817c478bd9Sstevel@tonic-gate 			 */
32827c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_CONFIGURED);
32837c478bd9Sstevel@tonic-gate 			bp->b_ostate = SBD_STAT_CONFIGURED;
32847c478bd9Sstevel@tonic-gate 			bp->b_rstate = SBD_STAT_CONNECTED;
32857c478bd9Sstevel@tonic-gate 			bp->b_cond = SBD_COND_OK;
32867c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&bp->b_time);
32877c478bd9Sstevel@tonic-gate 		} else if (DR_DEVS_ATTACHED(bp)) {
32887c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_PARTIAL);
32897c478bd9Sstevel@tonic-gate 			bp->b_ostate = SBD_STAT_CONFIGURED;
32907c478bd9Sstevel@tonic-gate 			bp->b_rstate = SBD_STAT_CONNECTED;
32917c478bd9Sstevel@tonic-gate 			bp->b_cond = SBD_COND_OK;
32927c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&bp->b_time);
32937c478bd9Sstevel@tonic-gate 		} else {
32947c478bd9Sstevel@tonic-gate 			dr_board_transition(bp, DR_STATE_CONNECTED);
32957c478bd9Sstevel@tonic-gate 			bp->b_rstate = SBD_STAT_CONNECTED;
32967c478bd9Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&bp->b_time);
32977c478bd9Sstevel@tonic-gate 		}
32987c478bd9Sstevel@tonic-gate 	}
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	return (0);
33017c478bd9Sstevel@tonic-gate }
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate static void
dr_board_destroy(dr_board_t * bp)33047c478bd9Sstevel@tonic-gate dr_board_destroy(dr_board_t *bp)
33057c478bd9Sstevel@tonic-gate {
33067c478bd9Sstevel@tonic-gate 	PR_ALL("dr_board_destroy: num %d, path %s\n",
330719397407SSherry Moore 	    bp->b_num, bp->b_path);
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 	dr_board_transition(bp, DR_STATE_EMPTY);
33107c478bd9Sstevel@tonic-gate 	bp->b_rstate = SBD_STAT_EMPTY;
33117c478bd9Sstevel@tonic-gate 	(void) drv_getparm(TIME, (void *)&bp->b_time);
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	/*
33147c478bd9Sstevel@tonic-gate 	 * Free up MEM unit structs.
33157c478bd9Sstevel@tonic-gate 	 */
33167c478bd9Sstevel@tonic-gate 	FREESTRUCT(bp->b_dev[NIX(SBD_COMP_MEM)],
331719397407SSherry Moore 	    dr_dev_unit_t, MAX_MEM_UNITS_PER_BOARD);
33187c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_MEM)] = NULL;
33197c478bd9Sstevel@tonic-gate 	/*
33207c478bd9Sstevel@tonic-gate 	 * Free up CPU unit structs.
33217c478bd9Sstevel@tonic-gate 	 */
33227c478bd9Sstevel@tonic-gate 	FREESTRUCT(bp->b_dev[NIX(SBD_COMP_CPU)],
332319397407SSherry Moore 	    dr_dev_unit_t, MAX_CPU_UNITS_PER_BOARD);
33247c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_CPU)] = NULL;
33257c478bd9Sstevel@tonic-gate 	/*
33267c478bd9Sstevel@tonic-gate 	 * Free up IO unit structs.
33277c478bd9Sstevel@tonic-gate 	 */
33287c478bd9Sstevel@tonic-gate 	FREESTRUCT(bp->b_dev[NIX(SBD_COMP_IO)],
332919397407SSherry Moore 	    dr_dev_unit_t, MAX_IO_UNITS_PER_BOARD);
33307c478bd9Sstevel@tonic-gate 	bp->b_dev[NIX(SBD_COMP_IO)] = NULL;
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	mutex_destroy(&bp->b_lock);
33337c478bd9Sstevel@tonic-gate 	mutex_destroy(&bp->b_slock);
33347c478bd9Sstevel@tonic-gate 	cv_destroy(&bp->b_scv);
33357c478bd9Sstevel@tonic-gate }
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate void
dr_lock_status(dr_board_t * bp)33387c478bd9Sstevel@tonic-gate dr_lock_status(dr_board_t *bp)
33397c478bd9Sstevel@tonic-gate {
33407c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->b_slock);
33417c478bd9Sstevel@tonic-gate 	while (bp->b_sflags & DR_BSLOCK)
33427c478bd9Sstevel@tonic-gate 		cv_wait(&bp->b_scv, &bp->b_slock);
33437c478bd9Sstevel@tonic-gate 	bp->b_sflags |= DR_BSLOCK;
33447c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->b_slock);
33457c478bd9Sstevel@tonic-gate }
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate void
dr_unlock_status(dr_board_t * bp)33487c478bd9Sstevel@tonic-gate dr_unlock_status(dr_board_t *bp)
33497c478bd9Sstevel@tonic-gate {
33507c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->b_slock);
33517c478bd9Sstevel@tonic-gate 	bp->b_sflags &= ~DR_BSLOCK;
33527c478bd9Sstevel@tonic-gate 	cv_signal(&bp->b_scv);
33537c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->b_slock);
33547c478bd9Sstevel@tonic-gate }
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate /*
33577c478bd9Sstevel@tonic-gate  * Extract flags passed via ioctl.
33587c478bd9Sstevel@tonic-gate  */
33597c478bd9Sstevel@tonic-gate int
dr_cmd_flags(dr_handle_t * hp)33607c478bd9Sstevel@tonic-gate dr_cmd_flags(dr_handle_t *hp)
33617c478bd9Sstevel@tonic-gate {
33627c478bd9Sstevel@tonic-gate 	return (hp->h_sbdcmd.cmd_cm.c_flags);
33637c478bd9Sstevel@tonic-gate }
3364