xref: /onnv-gate/usr/src/uts/sun4u/io/sbd.c (revision 11474:857f9db4ef05)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54845Svikram  * Common Development and Distribution License (the "License").
64845Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
2111311SSurya.Prakki@Sun.COM 
220Sstevel@tonic-gate /*
23*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * safari system board DR module.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/debug.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/dditypes.h>
360Sstevel@tonic-gate #include <sys/devops.h>
370Sstevel@tonic-gate #include <sys/modctl.h>
380Sstevel@tonic-gate #include <sys/poll.h>
390Sstevel@tonic-gate #include <sys/conf.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/sunndi.h>
430Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/kmem.h>
460Sstevel@tonic-gate #include <sys/cpuvar.h>
470Sstevel@tonic-gate #include <sys/mem_config.h>
480Sstevel@tonic-gate #include <sys/mem_cage.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/autoconf.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
540Sstevel@tonic-gate #include <sys/machsystm.h>
550Sstevel@tonic-gate #include <sys/param.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/sbdpriv.h>
580Sstevel@tonic-gate #include <sys/sbd_io.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* start sbd includes */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <sys/systm.h>
630Sstevel@tonic-gate #include <sys/sysmacros.h>
640Sstevel@tonic-gate #include <sys/x_call.h>
650Sstevel@tonic-gate #include <sys/membar.h>
660Sstevel@tonic-gate #include <vm/seg_kmem.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern int nulldev();
690Sstevel@tonic-gate extern int nodev();
700Sstevel@tonic-gate 
710Sstevel@tonic-gate typedef struct {		/* arg to sbd_get_handle */
720Sstevel@tonic-gate 	dev_t	dev;
730Sstevel@tonic-gate 	int	cmd;
740Sstevel@tonic-gate 	int	mode;
750Sstevel@tonic-gate 	sbd_ioctl_arg_t *ioargp;
760Sstevel@tonic-gate } sbd_init_arg_t;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * sbd support operations.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static void	sbd_exec_op(sbd_handle_t *hp);
830Sstevel@tonic-gate static void	sbd_dev_configure(sbd_handle_t *hp);
840Sstevel@tonic-gate static int	sbd_dev_release(sbd_handle_t *hp);
850Sstevel@tonic-gate static int	sbd_dev_unconfigure(sbd_handle_t *hp);
860Sstevel@tonic-gate static void	sbd_attach_cpu(sbd_handle_t *hp, sbderror_t *ep,
870Sstevel@tonic-gate 				dev_info_t *dip, int unit);
880Sstevel@tonic-gate static void	sbd_detach_cpu(sbd_handle_t *hp, sbderror_t *ep,
890Sstevel@tonic-gate 				dev_info_t *dip, int unit);
900Sstevel@tonic-gate static int	sbd_detach_mem(sbd_handle_t *hp, sbderror_t *ep, int unit);
910Sstevel@tonic-gate static void	sbd_cancel(sbd_handle_t *hp);
920Sstevel@tonic-gate void 	sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip);
930Sstevel@tonic-gate int		sbd_dealloc_instance(sbd_board_t *sbp, int max_boards);
940Sstevel@tonic-gate int		sbd_errno2ecode(int error);
950Sstevel@tonic-gate #pragma weak sbdp_cpu_get_impl
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #ifdef DEBUG
980Sstevel@tonic-gate uint_t	sbd_debug	=	(uint_t)0x0;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #ifdef SBD_DEBUG_ERRS
1010Sstevel@tonic-gate /* controls which errors are injected */
1020Sstevel@tonic-gate uint_t	sbd_err_debug	=	(uint_t)0x0;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* controls printing about error injection */
1050Sstevel@tonic-gate uint_t	sbd_print_errs	=	(uint_t)0x0;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #endif /* SBD_DEBUG_ERRS */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #endif /* DEBUG */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate char	*sbd_state_str[] = {
1120Sstevel@tonic-gate 	"EMPTY", "OCCUPIED", "CONNECTED", "UNCONFIGURED",
1130Sstevel@tonic-gate 	"PARTIAL", "CONFIGURED", "RELEASE", "UNREFERENCED",
1140Sstevel@tonic-gate 	"FATAL"
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*	Note: this must be changed in tandem with sbd_ioctl.h	*/
1180Sstevel@tonic-gate char	*sbd_ct_str[] = {
1190Sstevel@tonic-gate 	"NONE", "CPU", "MEM", "IO", "UNKNOWN"
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*	Note: this must also be changed in tandem with sbd_ioctl.h */
1230Sstevel@tonic-gate #define	SBD_CMD_STR(c) \
1240Sstevel@tonic-gate 	(((c) == SBD_CMD_ASSIGN)	? "ASSIGN"	: \
1250Sstevel@tonic-gate 	((c) == SBD_CMD_UNASSIGN)	? "UNASSIGN"	: \
1260Sstevel@tonic-gate 	((c) == SBD_CMD_POWERON)	? "POWERON"	: \
1270Sstevel@tonic-gate 	((c) == SBD_CMD_POWEROFF)	? "POWEROFF"	: \
1280Sstevel@tonic-gate 	((c) == SBD_CMD_TEST)		? "TEST"	: \
1290Sstevel@tonic-gate 	((c) == SBD_CMD_CONNECT)	? "CONNECT"	: \
1300Sstevel@tonic-gate 	((c) == SBD_CMD_CONFIGURE)	? "CONFIGURE"	: \
1310Sstevel@tonic-gate 	((c) == SBD_CMD_UNCONFIGURE)	? "UNCONFIGURE"	: \
1320Sstevel@tonic-gate 	((c) == SBD_CMD_DISCONNECT)	? "DISCONNECT"	: \
1330Sstevel@tonic-gate 	((c) == SBD_CMD_STATUS)		? "STATUS"	: \
1340Sstevel@tonic-gate 	((c) == SBD_CMD_GETNCM)		? "GETNCM"	: \
1350Sstevel@tonic-gate 	((c) == SBD_CMD_PASSTHRU)	? "PASSTHRU"	: "unknown")
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Defines and structures for device tree naming and mapping
1390Sstevel@tonic-gate  * to node types
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate sbd_devattr_t *sbd_devattr;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /* defines to access the attribute struct */
1450Sstevel@tonic-gate #define	SBD_DEVNAME(i)		sbd_devattr[i].s_devname
1460Sstevel@tonic-gate #define	SBD_OTYPE(i)		sbd_devattr[(i)].s_obp_type
1470Sstevel@tonic-gate #define	SBD_COMP(i)		sbd_devattr[i].s_dnodetype
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * State transition table.  States valid transitions for "board" state.
1510Sstevel@tonic-gate  * Recall that non-zero return value terminates operation, however
1520Sstevel@tonic-gate  * the herrno value is what really indicates an error , if any.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate static int
_cmd2index(int c)1550Sstevel@tonic-gate _cmd2index(int c)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * Translate DR CMD to index into sbd_state_transition.
1590Sstevel@tonic-gate 	 */
1600Sstevel@tonic-gate 	switch (c) {
1610Sstevel@tonic-gate 	case SBD_CMD_CONNECT:		return (0);
1620Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:	return (1);
1630Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:		return (2);
1640Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:	return (3);
1650Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:		return (4);
1660Sstevel@tonic-gate 	case SBD_CMD_POWERON:		return (5);
1670Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:		return (6);
1680Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:		return (7);
1690Sstevel@tonic-gate 	case SBD_CMD_TEST:		return (8);
1700Sstevel@tonic-gate 	default:			return (-1);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #define	CMD2INDEX(c)	_cmd2index(c)
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static struct sbd_state_trans {
1770Sstevel@tonic-gate 	int	x_cmd;
1780Sstevel@tonic-gate 	struct {
1790Sstevel@tonic-gate 		int	x_rv;		/* return value of pre_op */
1800Sstevel@tonic-gate 		int	x_err;		/* errno, if any */
1810Sstevel@tonic-gate 	} x_op[SBD_NUM_STATES];
1820Sstevel@tonic-gate } sbd_state_transition[] = {
1830Sstevel@tonic-gate 	{ SBD_CMD_CONNECT,
1840Sstevel@tonic-gate 		{
1850Sstevel@tonic-gate 			{ 0, 0 },	/* empty */
1860Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
1870Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
1880Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
1890Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
1900Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
1910Sstevel@tonic-gate 			{ 1, EIO },	/* release */
1920Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
1930Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 	},
1960Sstevel@tonic-gate 	{ SBD_CMD_DISCONNECT,
1970Sstevel@tonic-gate 		{
1980Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
1990Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2000Sstevel@tonic-gate 			{ 0, 0 },	/* connected */
2010Sstevel@tonic-gate 			{ 0, 0 },	/* unconfigured */
2020Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2030Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2040Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2050Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2060Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	},
2090Sstevel@tonic-gate 	{ SBD_CMD_CONFIGURE,
2100Sstevel@tonic-gate 		{
2110Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2120Sstevel@tonic-gate 			{ 1, EIO },	/* occupied */
2130Sstevel@tonic-gate 			{ 0, 0 },	/* connected */
2140Sstevel@tonic-gate 			{ 0, 0 },	/* unconfigured */
2150Sstevel@tonic-gate 			{ 0, 0 },	/* partial */
2160Sstevel@tonic-gate 			{ 1, 0 },	/* configured */
2170Sstevel@tonic-gate 			{ 0, 0 },	/* release */
2180Sstevel@tonic-gate 			{ 0, 0 },	/* unreferenced */
2190Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 	},
2220Sstevel@tonic-gate 	{ SBD_CMD_UNCONFIGURE,
2230Sstevel@tonic-gate 		{
2240Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2250Sstevel@tonic-gate 			{ 1, EIO },	/* occupied */
2260Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2270Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2280Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2290Sstevel@tonic-gate 			{ 0, 0 },	/* configured */
2300Sstevel@tonic-gate 			{ 0, 0 },	/* release */
2310Sstevel@tonic-gate 			{ 0, 0 },	/* unreferenced */
2320Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	},
2350Sstevel@tonic-gate 	{ SBD_CMD_POWEROFF,
2360Sstevel@tonic-gate 		{
2370Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2380Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2390Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2400Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2410Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2420Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2430Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2440Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2450Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	},
2480Sstevel@tonic-gate 	{ SBD_CMD_POWERON,
2490Sstevel@tonic-gate 		{
2500Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2510Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2520Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2530Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2540Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2550Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2560Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2570Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2580Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	},
2610Sstevel@tonic-gate 	{ SBD_CMD_UNASSIGN,
2620Sstevel@tonic-gate 		{
2630Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2640Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2650Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2660Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2670Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2680Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2690Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2700Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2710Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	},
2740Sstevel@tonic-gate 	{ SBD_CMD_ASSIGN,
2750Sstevel@tonic-gate 		{
2760Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2770Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2780Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2790Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2800Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2810Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2820Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2830Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2840Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 	},
2870Sstevel@tonic-gate 	{ SBD_CMD_TEST,
2880Sstevel@tonic-gate 		{
2890Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2900Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2910Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2920Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2930Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2940Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2950Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2960Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2970Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	},
3000Sstevel@tonic-gate };
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * Global R/W lock to synchronize access across
3040Sstevel@tonic-gate  * multiple boards.  Users wanting multi-board access
3050Sstevel@tonic-gate  * must grab WRITE lock, others must grab READ lock.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate krwlock_t	sbd_grwlock;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Global to determine if an event needs to be sent
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate char send_event = 0;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Required/Expected functions.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static sbd_handle_t	*sbd_get_handle(dev_t dev, sbd_softstate_t *softsp,
3190Sstevel@tonic-gate 				intptr_t arg, sbd_init_arg_t *iap);
3200Sstevel@tonic-gate static void		sbd_release_handle(sbd_handle_t *hp);
3210Sstevel@tonic-gate static int		sbd_pre_op(sbd_handle_t *hp);
3220Sstevel@tonic-gate static void		sbd_post_op(sbd_handle_t *hp);
3230Sstevel@tonic-gate static int		sbd_probe_board(sbd_handle_t *hp);
3240Sstevel@tonic-gate static int		sbd_deprobe_board(sbd_handle_t *hp);
3250Sstevel@tonic-gate static void		sbd_connect(sbd_handle_t *hp);
3260Sstevel@tonic-gate static void		sbd_assign_board(sbd_handle_t *hp);
3270Sstevel@tonic-gate static void		sbd_unassign_board(sbd_handle_t *hp);
3280Sstevel@tonic-gate static void		sbd_poweron_board(sbd_handle_t *hp);
3290Sstevel@tonic-gate static void		sbd_poweroff_board(sbd_handle_t *hp);
3300Sstevel@tonic-gate static void		sbd_test_board(sbd_handle_t *hp);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static int		sbd_disconnect(sbd_handle_t *hp);
3330Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_attach_devlist(sbd_handle_t *hp,
3340Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3350Sstevel@tonic-gate static int		sbd_pre_attach_devlist(sbd_handle_t *hp,
3360Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3370Sstevel@tonic-gate static int		sbd_post_attach_devlist(sbd_handle_t *hp,
3380Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3390Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_release_devlist(sbd_handle_t *hp,
3400Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3410Sstevel@tonic-gate static int		sbd_pre_release_devlist(sbd_handle_t *hp,
3420Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3430Sstevel@tonic-gate static int		sbd_post_release_devlist(sbd_handle_t *hp,
3440Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3450Sstevel@tonic-gate static void		sbd_release_done(sbd_handle_t *hp,
3460Sstevel@tonic-gate 					sbd_comp_type_t nodetype,
3470Sstevel@tonic-gate 					dev_info_t *dip);
3480Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_detach_devlist(sbd_handle_t *hp,
3490Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3500Sstevel@tonic-gate static int		sbd_pre_detach_devlist(sbd_handle_t *hp,
3510Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3520Sstevel@tonic-gate static int		sbd_post_detach_devlist(sbd_handle_t *hp,
3530Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3540Sstevel@tonic-gate static void		sbd_status(sbd_handle_t *hp);
3550Sstevel@tonic-gate static void		sbd_get_ncm(sbd_handle_t *hp);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Support functions.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate static sbd_devset_t	sbd_dev2devset(sbd_comp_id_t *cid);
3620Sstevel@tonic-gate static int		sbd_copyin_ioarg(sbd_handle_t *hp, int mode, int cmd,
3630Sstevel@tonic-gate 				sbd_cmd_t *cmdp, sbd_ioctl_arg_t *iap);
3640Sstevel@tonic-gate static int		sbd_copyout_errs(int mode, sbd_ioctl_arg_t *iap,
3650Sstevel@tonic-gate 					void *arg);
3660Sstevel@tonic-gate static int		sbd_copyout_ioarg(int mode, int cmd, sbd_cmd_t *scp,
3670Sstevel@tonic-gate 				sbd_ioctl_arg_t *iap);
3680Sstevel@tonic-gate static int		sbd_check_transition(sbd_board_t *sbp,
3690Sstevel@tonic-gate 					sbd_devset_t *devsetp,
3700Sstevel@tonic-gate 					struct sbd_state_trans *transp);
3710Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_devlist(sbd_handle_t *hp,
3720Sstevel@tonic-gate 					sbd_board_t *sbp,
3730Sstevel@tonic-gate 					sbd_comp_type_t nodetype,
3740Sstevel@tonic-gate 					int max_units, uint_t uset,
3750Sstevel@tonic-gate 					int *count, int present_only);
3760Sstevel@tonic-gate static int		sbd_mem_status(sbd_handle_t *hp, sbd_devset_t devset,
3770Sstevel@tonic-gate 					sbd_dev_stat_t *dsp);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate static int		sbd_init_devlists(sbd_board_t *sbp);
3800Sstevel@tonic-gate static int		sbd_name_to_idx(char *name);
3810Sstevel@tonic-gate static int		sbd_otype_to_idx(char *otpye);
3820Sstevel@tonic-gate static int		sbd_setup_devlists(dev_info_t *dip, void *arg);
3830Sstevel@tonic-gate static void		sbd_init_mem_devlists(sbd_board_t *sbp);
3840Sstevel@tonic-gate static void		sbd_init_cpu_unit(sbd_board_t *sbp, int unit);
3850Sstevel@tonic-gate static void		sbd_board_discovery(sbd_board_t *sbp);
3860Sstevel@tonic-gate static void		sbd_board_init(sbd_board_t *sbp,
3870Sstevel@tonic-gate 				sbd_softstate_t *softsp,
3880Sstevel@tonic-gate 				int bd, dev_info_t *dip, int wnode);
3890Sstevel@tonic-gate static void		sbd_board_destroy(sbd_board_t *sbp);
3900Sstevel@tonic-gate static int		sbd_check_unit_attached(sbd_board_t *sbp,
3910Sstevel@tonic-gate 				dev_info_t *dip, int unit,
3920Sstevel@tonic-gate 				sbd_comp_type_t nodetype, sbderror_t *ep);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static sbd_state_t 	rstate_cvt(sbd_istate_t state);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * Autoconfiguration data structures
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate extern struct mod_ops mod_miscops;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate static struct modlmisc modlmisc = {
4030Sstevel@tonic-gate 	&mod_miscops,
4047240Srh87107 	"System Board DR"
4050Sstevel@tonic-gate };
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static struct modlinkage modlinkage = {
4080Sstevel@tonic-gate 	MODREV_1,
4090Sstevel@tonic-gate 	(void *)&modlmisc,
4100Sstevel@tonic-gate 	NULL
4110Sstevel@tonic-gate };
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate static int sbd_instances = 0;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * dr Global data elements
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate sbd_global sbd_g;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * We want to be able to unload the module when we wish to do so, but we don't
4220Sstevel@tonic-gate  * want anything else to unload it.  Unloading cannot occur until
4230Sstevel@tonic-gate  * sbd_teardown_instance is called by an explicit IOCTL into the parent node.
4240Sstevel@tonic-gate  * This support is for debugging purposes and should it be expected to work
4250Sstevel@tonic-gate  * on the field, it should be enhanced:
4260Sstevel@tonic-gate  * Currently, there is still a window where sbd_teardow_instance gets called,
4270Sstevel@tonic-gate  * sbd_prevent_unloading now = 0, the driver doesn't get unloaded, and
4280Sstevel@tonic-gate  * sbd_setup_instance gets called.  This may cause a panic.
4290Sstevel@tonic-gate  */
4300Sstevel@tonic-gate int sbd_prevent_unloading = 1;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * Driver entry points.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate int
_init(void)4360Sstevel@tonic-gate _init(void)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	int	err;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * If you need to support multiple nodes (instances), then
4420Sstevel@tonic-gate 	 * whatever the maximum number of supported nodes is would
4430Sstevel@tonic-gate 	 * need to passed as the third parameter to ddi_soft_state_init().
4440Sstevel@tonic-gate 	 * Alternative would be to dynamically fini and re-init the
4450Sstevel@tonic-gate 	 * soft state structure each time a node is attached.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	err = ddi_soft_state_init((void **)&sbd_g.softsp,
4480Sstevel@tonic-gate 		sizeof (sbd_softstate_t), SBD_MAX_INSTANCES);
4490Sstevel@tonic-gate 	if (err)
4500Sstevel@tonic-gate 		return (err);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0) {
4530Sstevel@tonic-gate 		ddi_soft_state_fini((void **)&sbd_g.softsp);
4540Sstevel@tonic-gate 		return (err);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/* Get the array of names from platform helper routine */
4580Sstevel@tonic-gate 	sbd_devattr = sbdp_get_devattr();
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	return (err);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate int
_fini(void)4640Sstevel@tonic-gate _fini(void)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	int	err;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (sbd_prevent_unloading)
4690Sstevel@tonic-gate 		return (DDI_FAILURE);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	ASSERT(sbd_instances == 0);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
4740Sstevel@tonic-gate 		return (err);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	ddi_soft_state_fini((void **)&sbd_g.softsp);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	return (0);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4820Sstevel@tonic-gate _info(struct modinfo *modinfop)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate int
sbd_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,char * event)4880Sstevel@tonic-gate sbd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, char *event)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	int		rv = 0, instance;
4910Sstevel@tonic-gate 	sbd_handle_t	*hp;
4920Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
4930Sstevel@tonic-gate 	sbd_init_arg_t	init_arg;
4940Sstevel@tonic-gate 	static fn_t	f = "sbd_ioctl";
4950Sstevel@tonic-gate 	int		dr_avail;
4960Sstevel@tonic-gate 
497930Smathue 	PR_BYP("sbd_ioctl cmd=%x, arg=%lx\n", cmd, arg);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/* Note: this must also be changed in tandem with sbd_ioctl.h */
5000Sstevel@tonic-gate 	switch (cmd) {
5010Sstevel@tonic-gate 		case SBD_CMD_ASSIGN:
5020Sstevel@tonic-gate 		case SBD_CMD_UNASSIGN:
5030Sstevel@tonic-gate 		case SBD_CMD_POWERON:
5040Sstevel@tonic-gate 		case SBD_CMD_POWEROFF:
5050Sstevel@tonic-gate 		case SBD_CMD_TEST:
5060Sstevel@tonic-gate 		case SBD_CMD_CONNECT:
5070Sstevel@tonic-gate 		case SBD_CMD_CONFIGURE:
5080Sstevel@tonic-gate 		case SBD_CMD_UNCONFIGURE:
5090Sstevel@tonic-gate 		case SBD_CMD_DISCONNECT:
5100Sstevel@tonic-gate 		case SBD_CMD_STATUS:
5110Sstevel@tonic-gate 		case SBD_CMD_GETNCM:
5120Sstevel@tonic-gate 		case SBD_CMD_PASSTHRU:
5130Sstevel@tonic-gate 			break;
5140Sstevel@tonic-gate 		default:
5150Sstevel@tonic-gate 			return (ENOTTY);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	instance = SBD_GET_MINOR2INST(getminor(dev));
5190Sstevel@tonic-gate 	if ((softsp = (sbd_softstate_t *)GET_SOFTC(instance)) == NULL) {
5200Sstevel@tonic-gate 		cmn_err(CE_WARN,
5210Sstevel@tonic-gate 			"sbd:%s:%d: module not yet attached",
5220Sstevel@tonic-gate 			f, instance);
5230Sstevel@tonic-gate 		return (ENXIO);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	init_arg.dev = dev;
5270Sstevel@tonic-gate 	init_arg.cmd = cmd;
5280Sstevel@tonic-gate 	init_arg.mode = mode;
5290Sstevel@tonic-gate 	init_arg.ioargp = (sbd_ioctl_arg_t *)arg;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	hp = sbd_get_handle(dev, softsp, arg, &init_arg);
5320Sstevel@tonic-gate 	/* Check to see if we support dr */
5330Sstevel@tonic-gate 	dr_avail = sbdp_dr_avail();
5340Sstevel@tonic-gate 	if (dr_avail != 1) {
5350Sstevel@tonic-gate 		switch (hp->h_cmd) {
5360Sstevel@tonic-gate 			case SBD_CMD_STATUS:
5370Sstevel@tonic-gate 			case SBD_CMD_GETNCM:
5380Sstevel@tonic-gate 			case SBD_CMD_PASSTHRU:
5390Sstevel@tonic-gate 				break;
5400Sstevel@tonic-gate 			default:
5410Sstevel@tonic-gate 				sbd_release_handle(hp);
5420Sstevel@tonic-gate 				return (ENOTSUP);
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	switch (hp->h_cmd) {
5470Sstevel@tonic-gate 	case SBD_CMD_STATUS:
5480Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
5490Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
5500Sstevel@tonic-gate 		/* no locks needed for these commands */
5510Sstevel@tonic-gate 		break;
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	default:
5540Sstevel@tonic-gate 		rw_enter(&sbd_grwlock, RW_WRITER);
5550Sstevel@tonic-gate 		mutex_enter(&SBDH2BD(hp->h_sbd)->sb_mutex);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		/*
5580Sstevel@tonic-gate 		 * If we're dealing with memory at all, then we have
5590Sstevel@tonic-gate 		 * to keep the "exclusive" global lock held.  This is
5600Sstevel@tonic-gate 		 * necessary since we will probably need to look at
5610Sstevel@tonic-gate 		 * multiple board structs.  Otherwise, we only have
5620Sstevel@tonic-gate 		 * to deal with the board in question and so can drop
5630Sstevel@tonic-gate 		 * the global lock to "shared".
5640Sstevel@tonic-gate 		 */
5650Sstevel@tonic-gate 		/*
5660Sstevel@tonic-gate 		 * XXX This is incorrect. The sh_devset has not
5670Sstevel@tonic-gate 		 * been set at this point - it is 0.
5680Sstevel@tonic-gate 		 */
5690Sstevel@tonic-gate 		rv = DEVSET_IN_SET(HD2MACHHD(hp)->sh_devset,
5700Sstevel@tonic-gate 		    SBD_COMP_MEM, DEVSET_ANYUNIT);
5710Sstevel@tonic-gate 		if (rv == 0)
5720Sstevel@tonic-gate 			rw_downgrade(&sbd_grwlock);
5730Sstevel@tonic-gate 		break;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Before any operations happen, reset the event flag
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	send_event = 0;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (sbd_pre_op(hp) == 0) {
5820Sstevel@tonic-gate 		sbd_exec_op(hp);
5830Sstevel@tonic-gate 		sbd_post_op(hp);
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	rv = SBD_GET_ERRNO(SBD_HD2ERR(hp));
5870Sstevel@tonic-gate 	*event = send_event;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/* undo locking, if any, done before sbd_pre_op */
5900Sstevel@tonic-gate 	switch (hp->h_cmd) {
5910Sstevel@tonic-gate 	case SBD_CMD_STATUS:
5920Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
5930Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
5940Sstevel@tonic-gate 		break;
5950Sstevel@tonic-gate 	default:
5960Sstevel@tonic-gate 		mutex_exit(&SBDH2BD(hp->h_sbd)->sb_mutex);
5970Sstevel@tonic-gate 		rw_exit(&sbd_grwlock);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	sbd_release_handle(hp);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	return (rv);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate int
sbd_setup_instance(int instance,dev_info_t * root,int max_boards,int wnode,caddr_t sbdp_arg)6060Sstevel@tonic-gate sbd_setup_instance(int instance, dev_info_t *root, int max_boards, int wnode,
6070Sstevel@tonic-gate 		caddr_t sbdp_arg)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	int 		b;
6100Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
6110Sstevel@tonic-gate 	sbd_board_t	*sbd_boardlist;
6120Sstevel@tonic-gate 	static fn_t	f = "sbd_setup_instance";
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	sbd_instances++;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (sbdp_setup_instance(sbdp_arg) != DDI_SUCCESS) {
6170Sstevel@tonic-gate 		sbd_instances--;
6180Sstevel@tonic-gate 		return (DDI_FAILURE);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (ALLOC_SOFTC(instance) != DDI_SUCCESS) {
6220Sstevel@tonic-gate 		cmn_err(CE_WARN,
6230Sstevel@tonic-gate 			"sbd:%s:%d: failed to alloc soft-state",
6240Sstevel@tonic-gate 			f, instance);
62511311SSurya.Prakki@Sun.COM 		(void) sbdp_teardown_instance(sbdp_arg);
6260Sstevel@tonic-gate 		sbd_instances--;
6270Sstevel@tonic-gate 		return (DDI_FAILURE);
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	softsp = (sbd_softstate_t *)GET_SOFTC(instance);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (softsp == NULL) {
6330Sstevel@tonic-gate 		cmn_err(CE_WARN,
6340Sstevel@tonic-gate 			"sbd:%s:%d: failed to get soft-state instance",
6350Sstevel@tonic-gate 			f, instance);
6360Sstevel@tonic-gate 		goto exit;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	sbd_boardlist = GETSTRUCT(sbd_board_t, max_boards);
6400Sstevel@tonic-gate 	if (sbd_boardlist == NULL) {
6410Sstevel@tonic-gate 		cmn_err(CE_WARN,
6420Sstevel@tonic-gate 			"sbd:%s: failed to alloc board list %d",
6430Sstevel@tonic-gate 			f, instance);
6440Sstevel@tonic-gate 		goto exit;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	softsp->sbd_boardlist  = (void *)sbd_boardlist;
6490Sstevel@tonic-gate 	softsp->max_boards  = max_boards;
6500Sstevel@tonic-gate 	softsp->wnode  = wnode;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	for (b = 0; b < max_boards; b++) {
6540Sstevel@tonic-gate 		sbd_board_init(sbd_boardlist++, softsp, b, root, wnode);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (DDI_SUCCESS);
6590Sstevel@tonic-gate exit:
6600Sstevel@tonic-gate 	(void) sbdp_teardown_instance(sbdp_arg);
6610Sstevel@tonic-gate 	FREE_SOFTC(instance);
6620Sstevel@tonic-gate 	sbd_instances--;
6630Sstevel@tonic-gate 	return (DDI_FAILURE);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate int
sbd_teardown_instance(int instance,caddr_t sbdp_arg)6670Sstevel@tonic-gate sbd_teardown_instance(int instance, caddr_t sbdp_arg)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (sbdp_teardown_instance(sbdp_arg) != DDI_SUCCESS)
6720Sstevel@tonic-gate 		return (DDI_FAILURE);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	softsp = (sbd_softstate_t *)GET_SOFTC(instance);
6750Sstevel@tonic-gate 	if (softsp == NULL) {
6760Sstevel@tonic-gate 		return (DDI_FAILURE);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	(void) sbd_dealloc_instance((sbd_board_t *)softsp->sbd_boardlist,
6800Sstevel@tonic-gate 		softsp->max_boards);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	FREE_SOFTC(instance);
6830Sstevel@tonic-gate 	sbd_instances--;
6840Sstevel@tonic-gate 	sbd_prevent_unloading = 0;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	return (DDI_SUCCESS);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate static void
sbd_exec_op(sbd_handle_t * hp)6900Sstevel@tonic-gate sbd_exec_op(sbd_handle_t *hp)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
6930Sstevel@tonic-gate 	static fn_t	f = "sbd_exec_op";
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	switch (hp->h_cmd) {
6960Sstevel@tonic-gate 		int	dev_canceled;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	case SBD_CMD_CONNECT:
6990Sstevel@tonic-gate 		if (sbd_probe_board(hp))
7000Sstevel@tonic-gate 			break;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		sbd_connect(hp);
7030Sstevel@tonic-gate 		break;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:
7060Sstevel@tonic-gate 		sbd_dev_configure(hp);
7070Sstevel@tonic-gate 		break;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:
7100Sstevel@tonic-gate 		if (((dev_canceled = sbd_dev_release(hp)) == 0) &&
7110Sstevel@tonic-gate 		    (SBD_GET_ERRNO(SBD_HD2ERR(hp)) == 0 &&
7120Sstevel@tonic-gate 		    SBD_GET_ERR(SBD_HD2ERR(hp)) == 0))
7130Sstevel@tonic-gate 			dev_canceled = sbd_dev_unconfigure(hp);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		if (dev_canceled)
7160Sstevel@tonic-gate 			sbd_cancel(hp);
7170Sstevel@tonic-gate 		break;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:
7200Sstevel@tonic-gate 		mutex_enter(&sbp->sb_slock);
7210Sstevel@tonic-gate 		if (sbd_disconnect(hp) == 0)
7220Sstevel@tonic-gate 			(void) sbd_deprobe_board(hp);
7230Sstevel@tonic-gate 		mutex_exit(&sbp->sb_slock);
7240Sstevel@tonic-gate 		break;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	case SBD_CMD_STATUS:
7270Sstevel@tonic-gate 		sbd_status(hp);
7280Sstevel@tonic-gate 		break;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
7310Sstevel@tonic-gate 		sbd_get_ncm(hp);
7320Sstevel@tonic-gate 		break;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:
7350Sstevel@tonic-gate 		sbd_assign_board(hp);
7360Sstevel@tonic-gate 		break;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:
7390Sstevel@tonic-gate 		sbd_unassign_board(hp);
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:
7430Sstevel@tonic-gate 		sbd_poweroff_board(hp);
7440Sstevel@tonic-gate 		break;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	case SBD_CMD_POWERON:
7470Sstevel@tonic-gate 		sbd_poweron_board(hp);
7480Sstevel@tonic-gate 		break;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	case SBD_CMD_TEST:
7510Sstevel@tonic-gate 		sbd_test_board(hp);
7520Sstevel@tonic-gate 		break;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
7550Sstevel@tonic-gate 	{
7560Sstevel@tonic-gate 		int			rv;
7570Sstevel@tonic-gate 		sbdp_handle_t		*hdp;
7580Sstevel@tonic-gate 		sbderror_t		*ep = SBD_HD2ERR(hp);
7590Sstevel@tonic-gate 		sbdp_ioctl_arg_t	ia, *iap;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		iap = &ia;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		iap->h_dev = hp->h_dev;
7640Sstevel@tonic-gate 		iap->h_cmd = hp->h_cmd;
7650Sstevel@tonic-gate 		iap->h_iap = (intptr_t)hp->h_iap;
7660Sstevel@tonic-gate 		iap->h_mode = hp->h_mode;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		hdp = sbd_get_sbdp_handle(sbp, hp);
7690Sstevel@tonic-gate 		rv = sbdp_ioctl(hdp, iap);
7700Sstevel@tonic-gate 		if (rv != 0) {
7710Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
7720Sstevel@tonic-gate 			ep->e_errno = rv;
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
7750Sstevel@tonic-gate 		break;
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	default:
7790Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), ENOTTY);
7800Sstevel@tonic-gate 		cmn_err(CE_WARN,
7810Sstevel@tonic-gate 			"sbd:%s: unknown command (%d)",
7820Sstevel@tonic-gate 			f, hp->h_cmd);
7830Sstevel@tonic-gate 		break;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (SBD_GET_ERR(SBD_HD2ERR(hp)))
7880Sstevel@tonic-gate 		PR_BYP("XXX e_code=%d", SBD_GET_ERR(SBD_HD2ERR(hp)));
7890Sstevel@tonic-gate 	if (SBD_GET_ERRNO(SBD_HD2ERR(hp)))
7900Sstevel@tonic-gate 		PR_BYP("XXX errno=%d", SBD_GET_ERRNO(SBD_HD2ERR(hp)));
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate sbd_comp_type_t
sbd_get_devtype(sbd_handle_t * hp,dev_info_t * dip)7940Sstevel@tonic-gate sbd_get_devtype(sbd_handle_t *hp, dev_info_t *dip)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	sbd_board_t	*sbp = hp ? SBDH2BD(hp->h_sbd) : NULL;
7970Sstevel@tonic-gate 	sbd_istate_t	bstate;
7980Sstevel@tonic-gate 	dev_info_t	**devlist;
7990Sstevel@tonic-gate 	int		i;
8000Sstevel@tonic-gate 	char		device[OBP_MAXDRVNAME];
8010Sstevel@tonic-gate 	int		devicelen;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	devicelen = sizeof (device);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	bstate = sbp ? SBD_BOARD_STATE(sbp) : SBD_STATE_EMPTY;
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * if the board's connected or configured, search the
8080Sstevel@tonic-gate 	 * devlists.  Otherwise check the device tree
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	switch (bstate) {
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	case SBD_STATE_CONNECTED:
8130Sstevel@tonic-gate 	case SBD_STATE_CONFIGURED:
8140Sstevel@tonic-gate 	case SBD_STATE_UNREFERENCED:
8150Sstevel@tonic-gate 	case SBD_STATE_UNCONFIGURED:
8160Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_MEM)];
8170Sstevel@tonic-gate 		for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++)
8180Sstevel@tonic-gate 			if (devlist[i] == dip)
8190Sstevel@tonic-gate 				return (SBD_COMP_MEM);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_CPU)];
8220Sstevel@tonic-gate 		for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++)
8230Sstevel@tonic-gate 			if (devlist[i] == dip)
8240Sstevel@tonic-gate 				return (SBD_COMP_CPU);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_IO)];
8270Sstevel@tonic-gate 		for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++)
8280Sstevel@tonic-gate 			if (devlist[i] == dip)
8290Sstevel@tonic-gate 				return (SBD_COMP_IO);
8300Sstevel@tonic-gate 		/*FALLTHROUGH*/
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	default:
8330Sstevel@tonic-gate 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
8340Sstevel@tonic-gate 		    OBP_DEVICETYPE,  (caddr_t)device, &devicelen))
8350Sstevel@tonic-gate 			break;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 		for (i = 0; SBD_COMP(i) != SBD_COMP_UNKNOWN; i++) {
8380Sstevel@tonic-gate 			if (strcmp(device, SBD_OTYPE(i)) != 0)
8390Sstevel@tonic-gate 				continue;
8400Sstevel@tonic-gate 			return (SBD_COMP(i));
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		break;
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	return (SBD_COMP_UNKNOWN);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate static void
sbd_dev_configure(sbd_handle_t * hp)8490Sstevel@tonic-gate sbd_dev_configure(sbd_handle_t *hp)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	int		n, unit;
8520Sstevel@tonic-gate 	int32_t		pass, devnum;
8530Sstevel@tonic-gate 	dev_info_t	*dip;
8540Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
8550Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
8560Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
8570Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	pass = 1;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
8620Sstevel@tonic-gate 	while ((devlist = sbd_get_attach_devlist(hp, &devnum, pass)) != NULL) {
8630Sstevel@tonic-gate 		int	err;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 		err = sbd_pre_attach_devlist(hp, devlist, devnum);
8660Sstevel@tonic-gate 		if (err < 0) {
8670Sstevel@tonic-gate 			break;
8680Sstevel@tonic-gate 		} else if (err > 0) {
8690Sstevel@tonic-gate 			pass++;
8700Sstevel@tonic-gate 			continue;
8710Sstevel@tonic-gate 		}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
8740Sstevel@tonic-gate 			sbderror_t	*ep;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 			ep = &devlist[n].dv_error;
8770Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, 0);
8780Sstevel@tonic-gate 			SBD_SET_ERR(ep, 0);
8790Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
8800Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
8830Sstevel@tonic-gate 			if (unit < 0) {
8840Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
8850Sstevel@tonic-gate 				break;
8860Sstevel@tonic-gate 			}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			switch (nodetype) {
8890Sstevel@tonic-gate 			case SBD_COMP_MEM:
8900Sstevel@tonic-gate 				sbd_attach_mem(hp, ep);
8910Sstevel@tonic-gate 				if (SBD_GET_ERR(ep) == ESBD_CPUONLINE) {
8920Sstevel@tonic-gate 					FREESTRUCT(devlist, sbd_devlist_t,
8930Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD);
8940Sstevel@tonic-gate 					sbd_release_sbdp_handle(hdp);
8950Sstevel@tonic-gate 					return;
8960Sstevel@tonic-gate 				}
8970Sstevel@tonic-gate 				break;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 			case SBD_COMP_CPU:
9000Sstevel@tonic-gate 				sbd_attach_cpu(hp, ep, dip, unit);
9010Sstevel@tonic-gate 				break;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 			case SBD_COMP_IO:
9040Sstevel@tonic-gate 				sbd_attach_io(hp, ep, dip, unit);
9050Sstevel@tonic-gate 				break;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 			default:
9080Sstevel@tonic-gate 				SBD_SET_ERRNO(ep, ENOTTY);
9090Sstevel@tonic-gate 				break;
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 			if (sbd_set_err_in_hdl(hp, ep) == 0)
9130Sstevel@tonic-gate 				continue;
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		err = sbd_post_attach_devlist(hp, devlist, devnum);
9170Sstevel@tonic-gate 		if (err < 0)
9180Sstevel@tonic-gate 			break;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		pass++;
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate static int
sbd_dev_release(sbd_handle_t * hp)9260Sstevel@tonic-gate sbd_dev_release(sbd_handle_t *hp)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	int		n, unit;
9290Sstevel@tonic-gate 	int32_t		pass, devnum;
9300Sstevel@tonic-gate 	dev_info_t	*dip;
9310Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
9320Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
9330Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
9340Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
9350Sstevel@tonic-gate 	int		err = 0;
9360Sstevel@tonic-gate 	int		dev_canceled;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	pass = 1;
9390Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	sbp->sb_busy = 1;
9420Sstevel@tonic-gate 	while ((devlist =
9430Sstevel@tonic-gate 		sbd_get_release_devlist(hp, &devnum, pass)) != NULL) {
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		err = sbd_pre_release_devlist(hp, devlist, devnum);
9460Sstevel@tonic-gate 		if (err < 0) {
9470Sstevel@tonic-gate 			dev_canceled = 1;
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 		} else if (err > 0) {
9500Sstevel@tonic-gate 			pass++;
9510Sstevel@tonic-gate 			continue;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		dev_canceled = 0;
9550Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
9560Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
9570Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
9600Sstevel@tonic-gate 			if (unit < 0) {
9610Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
9620Sstevel@tonic-gate 				break;
9630Sstevel@tonic-gate 			}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 			if ((nodetype == SBD_COMP_MEM) &&
9660Sstevel@tonic-gate 			    sbd_release_mem(hp, dip, unit)) {
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 				dev_canceled++;
9690Sstevel@tonic-gate 			}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 			sbd_release_done(hp, nodetype, dip);
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		err = sbd_post_release_devlist(hp, devlist, devnum);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		if (err < 0)
9770Sstevel@tonic-gate 			break;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 		if (dev_canceled)
9800Sstevel@tonic-gate 			break;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 		pass++;
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 	sbp->sb_busy = 0;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	if (dev_canceled)
9890Sstevel@tonic-gate 		return (dev_canceled);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	return (err);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate static int
sbd_dev_unconfigure(sbd_handle_t * hp)9950Sstevel@tonic-gate sbd_dev_unconfigure(sbd_handle_t *hp)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	int		n, unit;
9980Sstevel@tonic-gate 	int32_t		pass, devnum;
9990Sstevel@tonic-gate 	dev_info_t	*dip;
10000Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
10010Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
10020Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
10030Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
10040Sstevel@tonic-gate 	int		dev_canceled = 0;
10050Sstevel@tonic-gate 	static fn_t	f = "sbd_dev_unconfigure";
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	pass = 1;
10100Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	while ((devlist = sbd_get_detach_devlist(hp, &devnum, pass)) != NULL) {
10130Sstevel@tonic-gate 		int	err, detach_err = 0;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 		err = sbd_pre_detach_devlist(hp, devlist, devnum);
10160Sstevel@tonic-gate 		if (err) {
10170Sstevel@tonic-gate 			/*
10180Sstevel@tonic-gate 			 * Only cancel the operation for memory in
10190Sstevel@tonic-gate 			 * case of failure.
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, devlist->dv_dip);
10220Sstevel@tonic-gate 			if (nodetype == SBD_COMP_MEM)
10230Sstevel@tonic-gate 				dev_canceled = 1;
10240Sstevel@tonic-gate 			(void) sbd_post_detach_devlist(hp, devlist, devnum);
10250Sstevel@tonic-gate 			break;
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
10290Sstevel@tonic-gate 			sbderror_t	*ep;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 			ep = &devlist[n].dv_error;
10320Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, 0);
10330Sstevel@tonic-gate 			SBD_SET_ERR(ep, 0);
10340Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
10350Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
10380Sstevel@tonic-gate 			if (unit < 0) {
10390Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
10400Sstevel@tonic-gate 				break;
10410Sstevel@tonic-gate 			}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			switch (nodetype) {
10440Sstevel@tonic-gate 			case SBD_COMP_MEM:
10450Sstevel@tonic-gate 				dev_canceled = sbd_detach_mem(hp, ep, unit);
10460Sstevel@tonic-gate 				break;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 			case SBD_COMP_CPU:
10490Sstevel@tonic-gate 				sbd_detach_cpu(hp, ep, dip, unit);
10500Sstevel@tonic-gate 				break;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 			case SBD_COMP_IO:
10530Sstevel@tonic-gate 				sbd_detach_io(hp, ep, dip, unit);
10540Sstevel@tonic-gate 				break;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 			default:
10570Sstevel@tonic-gate 				SBD_SET_ERRNO(ep, ENOTTY);
10580Sstevel@tonic-gate 				break;
10590Sstevel@tonic-gate 			}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 			if (sbd_set_err_in_hdl(hp, ep) == 0) {
10620Sstevel@tonic-gate 				detach_err = -1;
10630Sstevel@tonic-gate 				break;
10640Sstevel@tonic-gate 			}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 		err = sbd_post_detach_devlist(hp, devlist, devnum);
10680Sstevel@tonic-gate 		if ((err < 0) || (detach_err < 0))
10690Sstevel@tonic-gate 			break;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 		pass++;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
10750Sstevel@tonic-gate 	return (dev_canceled);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate int
sbd_errno2ecode(int error)10790Sstevel@tonic-gate sbd_errno2ecode(int error)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	int	rv;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	switch (error) {
10840Sstevel@tonic-gate 	case EBUSY:
10850Sstevel@tonic-gate 		rv = ESBD_BUSY;
10860Sstevel@tonic-gate 		break;
10870Sstevel@tonic-gate 	case EINVAL:
10880Sstevel@tonic-gate 		rv = ESBD_INVAL;
10890Sstevel@tonic-gate 		break;
10900Sstevel@tonic-gate 	case EALREADY:
10910Sstevel@tonic-gate 		rv = ESBD_ALREADY;
10920Sstevel@tonic-gate 		break;
10930Sstevel@tonic-gate 	case ENODEV:
10940Sstevel@tonic-gate 		rv = ESBD_NODEV;
10950Sstevel@tonic-gate 		break;
10960Sstevel@tonic-gate 	case ENOMEM:
10970Sstevel@tonic-gate 		rv = ESBD_NOMEM;
10980Sstevel@tonic-gate 		break;
10990Sstevel@tonic-gate 	default:
11000Sstevel@tonic-gate 		rv = ESBD_INVAL;
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	return (rv);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate static void
sbd_attach_cpu(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)11070Sstevel@tonic-gate sbd_attach_cpu(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 	int rv = 0;
11100Sstevel@tonic-gate 	processorid_t	cpuid;
11110Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
11120Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
11130Sstevel@tonic-gate 	static fn_t	f = "sbd_attach_cpu";
11140Sstevel@tonic-gate 	char		*pathname;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	ASSERT(dip);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * With the introduction of CMP devices, the CPU nodes
11220Sstevel@tonic-gate 	 * are no longer directly under the top node. Since
11230Sstevel@tonic-gate 	 * there is no plan to support CPU attach in the near
11240Sstevel@tonic-gate 	 * future, a branch configure operation is not required.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
11280Sstevel@tonic-gate 	cpuid = sbdp_get_cpuid(hdp, dip);
11290Sstevel@tonic-gate 	if (cpuid < 0) {
11300Sstevel@tonic-gate 		rv = -1;
11310Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
11320Sstevel@tonic-gate 	} else if ((rv = cpu_configure(cpuid)) != 0) {
11330Sstevel@tonic-gate 		cmn_err(CE_WARN,
11340Sstevel@tonic-gate 			"sbd:%s: cpu_configure for cpuid %d failed",
11350Sstevel@tonic-gate 			f, cpuid);
11360Sstevel@tonic-gate 		SBD_SET_ERR(ep, sbd_errno2ecode(rv));
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	if (rv == 0) {
11410Sstevel@tonic-gate 		ASSERT(sbp->sb_cpupath[unit] != NULL);
11420Sstevel@tonic-gate 		pathname = sbp->sb_cpupath[unit];
11430Sstevel@tonic-gate 		(void) ddi_pathname(dip, pathname);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  *	translate errno
11490Sstevel@tonic-gate  */
11500Sstevel@tonic-gate void
sbd_errno_decode(int err,sbderror_t * ep,dev_info_t * dip)11510Sstevel@tonic-gate sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	ASSERT(err != 0);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	switch (err) {
11560Sstevel@tonic-gate 	case ENOMEM:
11570Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NOMEM);
11580Sstevel@tonic-gate 		break;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	case EBUSY:
11610Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_BUSY);
11620Sstevel@tonic-gate 		break;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	case EIO:
11650Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_IO);
11660Sstevel@tonic-gate 		break;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	case ENXIO:
11690Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NODEV);
11700Sstevel@tonic-gate 		break;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	case EINVAL:
11730Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_INVAL);
11740Sstevel@tonic-gate 		break;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	case EFAULT:
11770Sstevel@tonic-gate 	default:
11780Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_FAULT);
11790Sstevel@tonic-gate 		break;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	(void) ddi_pathname(dip, SBD_GET_ERRSTR(ep));
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate static void
sbd_detach_cpu(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)11860Sstevel@tonic-gate sbd_detach_cpu(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	processorid_t	cpuid;
11890Sstevel@tonic-gate 	int		rv;
11900Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
11910Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
11920Sstevel@tonic-gate 	sbd_error_t	*spe;
11930Sstevel@tonic-gate 	static fn_t	f = "sbd_detach_cpu";
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	ASSERT(dip);
11980Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
11990Sstevel@tonic-gate 	spe = hdp->h_err;
12000Sstevel@tonic-gate 	cpuid = sbdp_get_cpuid(hdp, dip);
12010Sstevel@tonic-gate 	if (cpuid < 0) {
12020Sstevel@tonic-gate 		SBD_GET_PERR(spe, ep);
12030Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
12040Sstevel@tonic-gate 		return;
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	if ((rv = cpu_unconfigure(cpuid)) != 0) {
12080Sstevel@tonic-gate 		SBD_SET_ERR(ep, sbd_errno2ecode(rv));
12090Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_cpupath[unit]);
12100Sstevel@tonic-gate 		cmn_err(CE_WARN,
12110Sstevel@tonic-gate 			"sbd:%s: cpu_unconfigure for cpu %d failed",
12120Sstevel@tonic-gate 			f, cpuid);
12130Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
12140Sstevel@tonic-gate 		return;
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	/*
12190Sstevel@tonic-gate 	 * Since CPU nodes are no longer configured in CPU
12200Sstevel@tonic-gate 	 * attach, the corresponding branch unconfigure
12210Sstevel@tonic-gate 	 * operation that would be performed here is also
12220Sstevel@tonic-gate 	 * no longer required.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate int
sbd_detach_mem(sbd_handle_t * hp,sbderror_t * ep,int unit)12280Sstevel@tonic-gate sbd_detach_mem(sbd_handle_t *hp, sbderror_t *ep, int unit)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
12310Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
12320Sstevel@tonic-gate 	int		i, rv;
12330Sstevel@tonic-gate 	static fn_t	f = "sbd_detach_mem";
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if (sbd_detach_memory(hp, ep, mp, unit)) {
12380Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: detach fail", f);
12390Sstevel@tonic-gate 		return (-1);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	/*
12430Sstevel@tonic-gate 	 * Now detach mem devinfo nodes with status lock held.
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
12460Sstevel@tonic-gate 		dev_info_t	*fdip = NULL;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		if (mp->sbm_dip[i] == NULL)
12490Sstevel@tonic-gate 			continue;
12500Sstevel@tonic-gate 		ASSERT(e_ddi_branch_held(mp->sbm_dip[i]));
12510Sstevel@tonic-gate 		mutex_enter(&sbp->sb_slock);
12520Sstevel@tonic-gate 		rv = e_ddi_branch_unconfigure(mp->sbm_dip[i], &fdip,
12530Sstevel@tonic-gate 		    DEVI_BRANCH_EVENT);
12540Sstevel@tonic-gate 		mutex_exit(&sbp->sb_slock);
12550Sstevel@tonic-gate 		if (rv) {
12560Sstevel@tonic-gate 			/*
12570Sstevel@tonic-gate 			 * If non-NULL, fdip is returned held and must be
12580Sstevel@tonic-gate 			 * released.
12590Sstevel@tonic-gate 			 */
12600Sstevel@tonic-gate 			if (fdip != NULL) {
12610Sstevel@tonic-gate 				sbd_errno_decode(rv, ep, fdip);
12620Sstevel@tonic-gate 				ddi_release_devi(fdip);
12630Sstevel@tonic-gate 			} else {
12640Sstevel@tonic-gate 				sbd_errno_decode(rv, ep, mp->sbm_dip[i]);
12650Sstevel@tonic-gate 			}
12660Sstevel@tonic-gate 		}
12670Sstevel@tonic-gate 	}
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	return (0);
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate /* start beginning of sbd.c */
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate  * MDR          memory support - somewhat disabled for now.
12760Sstevel@tonic-gate  * UNSAFE       unsafe driver code - I don't think we want this.
12770Sstevel@tonic-gate  *              need to check.
12780Sstevel@tonic-gate  * DEVNODE      This driver creates attachment points for individual
12790Sstevel@tonic-gate  *              components as well as boards.  We only need board
12800Sstevel@tonic-gate  *              support.
12810Sstevel@tonic-gate  * DEV2DEVSET   Put only present devices in devset.
12820Sstevel@tonic-gate  */
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate static sbd_state_t
rstate_cvt(sbd_istate_t state)12860Sstevel@tonic-gate rstate_cvt(sbd_istate_t state)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	sbd_state_t cs;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	switch (state) {
12910Sstevel@tonic-gate 	case SBD_STATE_EMPTY:
12920Sstevel@tonic-gate 		cs = SBD_STAT_EMPTY;
12930Sstevel@tonic-gate 		break;
12940Sstevel@tonic-gate 	case SBD_STATE_OCCUPIED:
12950Sstevel@tonic-gate 	case SBD_STATE_FATAL:
12960Sstevel@tonic-gate 		cs = SBD_STAT_DISCONNECTED;
12970Sstevel@tonic-gate 		break;
12980Sstevel@tonic-gate 	case SBD_STATE_CONFIGURED:
12990Sstevel@tonic-gate 	case SBD_STATE_CONNECTED:
13000Sstevel@tonic-gate 	case SBD_STATE_UNCONFIGURED:
13010Sstevel@tonic-gate 	case SBD_STATE_PARTIAL:
13020Sstevel@tonic-gate 	case SBD_STATE_RELEASE:
13030Sstevel@tonic-gate 	case SBD_STATE_UNREFERENCED:
13040Sstevel@tonic-gate 		cs = SBD_STAT_CONNECTED;
13050Sstevel@tonic-gate 		break;
13060Sstevel@tonic-gate 	default:
13070Sstevel@tonic-gate 		cs = SBD_STAT_NONE;
13080Sstevel@tonic-gate 		break;
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	return (cs);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate sbd_state_t
ostate_cvt(sbd_istate_t state)13160Sstevel@tonic-gate ostate_cvt(sbd_istate_t state)
13170Sstevel@tonic-gate {
13180Sstevel@tonic-gate 	sbd_state_t cs;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	switch (state) {
13210Sstevel@tonic-gate 	case SBD_STATE_EMPTY:
13220Sstevel@tonic-gate 	case SBD_STATE_OCCUPIED:
13230Sstevel@tonic-gate 	case SBD_STATE_UNCONFIGURED:
13240Sstevel@tonic-gate 	case SBD_STATE_CONNECTED:
13250Sstevel@tonic-gate 	case SBD_STATE_FATAL:
13260Sstevel@tonic-gate 		cs = SBD_STAT_UNCONFIGURED;
13270Sstevel@tonic-gate 		break;
13280Sstevel@tonic-gate 	case SBD_STATE_PARTIAL:
13290Sstevel@tonic-gate 	case SBD_STATE_CONFIGURED:
13300Sstevel@tonic-gate 	case SBD_STATE_RELEASE:
13310Sstevel@tonic-gate 	case SBD_STATE_UNREFERENCED:
13320Sstevel@tonic-gate 		cs = SBD_STAT_CONFIGURED;
13330Sstevel@tonic-gate 		break;
13340Sstevel@tonic-gate 	default:
13350Sstevel@tonic-gate 		cs = SBD_STAT_NONE;
13360Sstevel@tonic-gate 		break;
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	return (cs);
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate int
sbd_dealloc_instance(sbd_board_t * sbp,int max_boards)13430Sstevel@tonic-gate sbd_dealloc_instance(sbd_board_t *sbp, int max_boards)
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	int		b;
13460Sstevel@tonic-gate 	sbd_board_t    *list = sbp;
13470Sstevel@tonic-gate 	static fn_t	f = "sbd_dealloc_instance";
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (sbp == NULL) {
13520Sstevel@tonic-gate 		return (-1);
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	for (b = 0; b < max_boards; b++) {
13560Sstevel@tonic-gate 		sbd_board_destroy(sbp++);
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	FREESTRUCT(list, sbd_board_t, max_boards);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	return (0);
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate static sbd_devset_t
sbd_dev2devset(sbd_comp_id_t * cid)13650Sstevel@tonic-gate sbd_dev2devset(sbd_comp_id_t *cid)
13660Sstevel@tonic-gate {
13670Sstevel@tonic-gate 	static fn_t	f = "sbd_dev2devset";
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	sbd_devset_t	devset;
13700Sstevel@tonic-gate 	int		unit = cid->c_unit;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	switch (cid->c_type) {
13730Sstevel@tonic-gate 		case SBD_COMP_NONE:
13740Sstevel@tonic-gate 			devset =  DEVSET(SBD_COMP_CPU, DEVSET_ANYUNIT);
13750Sstevel@tonic-gate 			devset |= DEVSET(SBD_COMP_MEM, DEVSET_ANYUNIT);
13760Sstevel@tonic-gate 			devset |= DEVSET(SBD_COMP_IO,  DEVSET_ANYUNIT);
13770Sstevel@tonic-gate 			break;
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 		case SBD_COMP_CPU:
13800Sstevel@tonic-gate 			if ((unit > MAX_CPU_UNITS_PER_BOARD) || (unit < 0)) {
13810Sstevel@tonic-gate 				PR_ALL("%s: invalid cpu unit# = %d",
13820Sstevel@tonic-gate 					f, unit);
13830Sstevel@tonic-gate 				devset = 0;
13840Sstevel@tonic-gate 			} else
13850Sstevel@tonic-gate 				/*
13860Sstevel@tonic-gate 				 * Generate a devset that includes all the
13870Sstevel@tonic-gate 				 * cores of a CMP device. If this is not a
13880Sstevel@tonic-gate 				 * CMP, the extra cores will be eliminated
13890Sstevel@tonic-gate 				 * later since they are not present. This is
13900Sstevel@tonic-gate 				 * also true for CMP devices that do not have
13910Sstevel@tonic-gate 				 * all cores active.
13920Sstevel@tonic-gate 				 */
13930Sstevel@tonic-gate 				devset = DEVSET(SBD_COMP_CMP, unit);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 			break;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 		case SBD_COMP_MEM:
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 			if ((unit > MAX_MEM_UNITS_PER_BOARD) || (unit < 0)) {
14000Sstevel@tonic-gate #ifdef XXX_jeffco
14010Sstevel@tonic-gate 				PR_ALL("%s: invalid mem unit# = %d",
14020Sstevel@tonic-gate 					f, unit);
14030Sstevel@tonic-gate 				devset = 0;
14040Sstevel@tonic-gate #endif
14050Sstevel@tonic-gate 				devset = DEVSET(cid->c_type, 0);
14060Sstevel@tonic-gate 				PR_ALL("%s: adjusted MEM devset = 0x%x\n",
14070Sstevel@tonic-gate 					f, devset);
14080Sstevel@tonic-gate 			} else
14090Sstevel@tonic-gate 				devset = DEVSET(cid->c_type, unit);
14100Sstevel@tonic-gate 			break;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		case SBD_COMP_IO:
14130Sstevel@tonic-gate 			if ((unit > MAX_IO_UNITS_PER_BOARD) || (unit < 0)) {
14140Sstevel@tonic-gate 				PR_ALL("%s: invalid io unit# = %d",
14150Sstevel@tonic-gate 					f, unit);
14160Sstevel@tonic-gate 				devset = 0;
14170Sstevel@tonic-gate 			} else
14180Sstevel@tonic-gate 				devset = DEVSET(cid->c_type, unit);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 			break;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 		default:
14230Sstevel@tonic-gate 		case SBD_COMP_UNKNOWN:
14240Sstevel@tonic-gate 			devset = 0;
14250Sstevel@tonic-gate 			break;
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	return (devset);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate  * Simple mutex for covering handle list ops as it is only
14330Sstevel@tonic-gate  * used "infrequently". No need to add another mutex to the sbd_board_t.
14340Sstevel@tonic-gate  */
14350Sstevel@tonic-gate static kmutex_t sbd_handle_list_mutex;
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate static sbd_handle_t *
sbd_get_handle(dev_t dev,sbd_softstate_t * softsp,intptr_t arg,sbd_init_arg_t * iap)14380Sstevel@tonic-gate sbd_get_handle(dev_t dev, sbd_softstate_t *softsp, intptr_t arg,
14390Sstevel@tonic-gate 	sbd_init_arg_t *iap)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate 	sbd_handle_t		*hp;
14420Sstevel@tonic-gate 	sbderror_t		*ep;
14430Sstevel@tonic-gate 	sbd_priv_handle_t	*shp;
14440Sstevel@tonic-gate 	sbd_board_t		*sbp = softsp->sbd_boardlist;
14450Sstevel@tonic-gate 	int			board;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	board = SBDGETSLOT(dev);
14480Sstevel@tonic-gate 	ASSERT(board < softsp->max_boards);
14490Sstevel@tonic-gate 	sbp += board;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/*
14520Sstevel@tonic-gate 	 * Brand-new handle.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	shp = kmem_zalloc(sizeof (sbd_priv_handle_t), KM_SLEEP);
14550Sstevel@tonic-gate 	shp->sh_arg = (void *)arg;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	hp = MACHHD2HD(shp);
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	ep = &shp->sh_err;
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	hp->h_err = ep;
14620Sstevel@tonic-gate 	hp->h_sbd = (void *) sbp;
14630Sstevel@tonic-gate 	hp->h_dev = iap->dev;
14640Sstevel@tonic-gate 	hp->h_cmd = iap->cmd;
14650Sstevel@tonic-gate 	hp->h_mode = iap->mode;
14660Sstevel@tonic-gate 	sbd_init_err(ep);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	mutex_enter(&sbd_handle_list_mutex);
14690Sstevel@tonic-gate 	shp->sh_next = sbp->sb_handle;
14700Sstevel@tonic-gate 	sbp->sb_handle = shp;
14710Sstevel@tonic-gate 	mutex_exit(&sbd_handle_list_mutex);
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	return (hp);
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate void
sbd_init_err(sbderror_t * ep)14770Sstevel@tonic-gate sbd_init_err(sbderror_t *ep)
14780Sstevel@tonic-gate {
14790Sstevel@tonic-gate 	ep->e_errno = 0;
14800Sstevel@tonic-gate 	ep->e_code = 0;
14810Sstevel@tonic-gate 	ep->e_rsc[0] = '\0';
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate int
sbd_set_err_in_hdl(sbd_handle_t * hp,sbderror_t * ep)14850Sstevel@tonic-gate sbd_set_err_in_hdl(sbd_handle_t *hp, sbderror_t *ep)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate 	sbderror_t	*hep = SBD_HD2ERR(hp);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	/*
14900Sstevel@tonic-gate 	 * If there is an error logged already, don't rewrite it
14910Sstevel@tonic-gate 	 */
14920Sstevel@tonic-gate 	if (SBD_GET_ERR(hep) || SBD_GET_ERRNO(hep)) {
14930Sstevel@tonic-gate 		return (0);
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	if (SBD_GET_ERR(ep) || SBD_GET_ERRNO(ep)) {
14970Sstevel@tonic-gate 		SBD_SET_ERR(hep, SBD_GET_ERR(ep));
14980Sstevel@tonic-gate 		SBD_SET_ERRNO(hep, SBD_GET_ERRNO(ep));
14990Sstevel@tonic-gate 		SBD_SET_ERRSTR(hep, SBD_GET_ERRSTR(ep));
15000Sstevel@tonic-gate 		return (0);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	return (-1);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate static void
sbd_release_handle(sbd_handle_t * hp)15070Sstevel@tonic-gate sbd_release_handle(sbd_handle_t *hp)
15080Sstevel@tonic-gate {
15090Sstevel@tonic-gate 	sbd_priv_handle_t	*shp, **shpp;
15100Sstevel@tonic-gate 	sbd_board_t		*sbp;
15110Sstevel@tonic-gate 	static fn_t		f = "sbd_release_handle";
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if (hp == NULL)
15140Sstevel@tonic-gate 		return;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	shp = HD2MACHHD(hp);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	mutex_enter(&sbd_handle_list_mutex);
15210Sstevel@tonic-gate 	/*
15220Sstevel@tonic-gate 	 * Locate the handle in the board's reference list.
15230Sstevel@tonic-gate 	 */
15240Sstevel@tonic-gate 	for (shpp = &sbp->sb_handle; (*shpp) && ((*shpp) != shp);
15250Sstevel@tonic-gate 	    shpp = &((*shpp)->sh_next))
15260Sstevel@tonic-gate 		/* empty */;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	if (*shpp == NULL) {
15290Sstevel@tonic-gate 		cmn_err(CE_PANIC,
15300Sstevel@tonic-gate 			"sbd:%s: handle not found in board %d",
15310Sstevel@tonic-gate 			f, sbp->sb_num);
15320Sstevel@tonic-gate 		/*NOTREACHED*/
15330Sstevel@tonic-gate 	} else {
15340Sstevel@tonic-gate 		*shpp = shp->sh_next;
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 	mutex_exit(&sbd_handle_list_mutex);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if (hp->h_opts.copts != NULL) {
15390Sstevel@tonic-gate 		FREESTRUCT(hp->h_opts.copts, char, hp->h_opts.size);
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	FREESTRUCT(shp, sbd_priv_handle_t, 1);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate sbdp_handle_t *
sbd_get_sbdp_handle(sbd_board_t * sbp,sbd_handle_t * hp)15460Sstevel@tonic-gate sbd_get_sbdp_handle(sbd_board_t *sbp, sbd_handle_t *hp)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	sbdp_handle_t		*hdp;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	hdp = kmem_zalloc(sizeof (sbdp_handle_t), KM_SLEEP);
15510Sstevel@tonic-gate 	hdp->h_err = kmem_zalloc(sizeof (sbd_error_t), KM_SLEEP);
15520Sstevel@tonic-gate 	if (sbp == NULL) {
15530Sstevel@tonic-gate 		hdp->h_board = -1;
15540Sstevel@tonic-gate 		hdp->h_wnode = -1;
15550Sstevel@tonic-gate 	} else {
15560Sstevel@tonic-gate 		hdp->h_board = sbp->sb_num;
15570Sstevel@tonic-gate 		hdp->h_wnode = sbp->sb_wnode;
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (hp == NULL) {
15610Sstevel@tonic-gate 		hdp->h_flags = 0;
15620Sstevel@tonic-gate 		hdp->h_opts = NULL;
15630Sstevel@tonic-gate 	} else {
15640Sstevel@tonic-gate 		hdp->h_flags = SBD_2_SBDP_FLAGS(hp->h_flags);
15650Sstevel@tonic-gate 		hdp->h_opts = &hp->h_opts;
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	return (hdp);
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate void
sbd_release_sbdp_handle(sbdp_handle_t * hdp)15720Sstevel@tonic-gate sbd_release_sbdp_handle(sbdp_handle_t *hdp)
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate 	if (hdp == NULL)
15750Sstevel@tonic-gate 		return;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	kmem_free(hdp->h_err, sizeof (sbd_error_t));
15780Sstevel@tonic-gate 	kmem_free(hdp, sizeof (sbdp_handle_t));
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate void
sbd_reset_error_sbdph(sbdp_handle_t * hdp)15820Sstevel@tonic-gate sbd_reset_error_sbdph(sbdp_handle_t *hdp)
15830Sstevel@tonic-gate {
15840Sstevel@tonic-gate 	if ((hdp != NULL) && (hdp->h_err != NULL)) {
15850Sstevel@tonic-gate 		bzero(hdp->h_err, sizeof (sbd_error_t));
15860Sstevel@tonic-gate 	}
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate static int
sbd_copyin_ioarg(sbd_handle_t * hp,int mode,int cmd,sbd_cmd_t * cmdp,sbd_ioctl_arg_t * iap)15900Sstevel@tonic-gate sbd_copyin_ioarg(sbd_handle_t *hp, int mode, int cmd, sbd_cmd_t *cmdp,
15910Sstevel@tonic-gate 	sbd_ioctl_arg_t *iap)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	static fn_t	f = "sbd_copyin_ioarg";
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	if (iap == NULL)
15960Sstevel@tonic-gate 		return (EINVAL);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	bzero((caddr_t)cmdp, sizeof (sbd_cmd_t));
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
16010Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
16020Sstevel@tonic-gate 		sbd_cmd32_t	scmd32;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		bzero((caddr_t)&scmd32, sizeof (sbd_cmd32_t));
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 		if (ddi_copyin((void *)iap, (void *)&scmd32,
16070Sstevel@tonic-gate 				sizeof (sbd_cmd32_t), mode)) {
16080Sstevel@tonic-gate 			cmn_err(CE_WARN,
16090Sstevel@tonic-gate 				"sbd:%s: (32bit) failed to copyin "
16100Sstevel@tonic-gate 					"sbdcmd-struct", f);
16110Sstevel@tonic-gate 			return (EFAULT);
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 		cmdp->cmd_cm.c_id.c_type = scmd32.cmd_cm.c_id.c_type;
16140Sstevel@tonic-gate 		cmdp->cmd_cm.c_id.c_unit = scmd32.cmd_cm.c_id.c_unit;
16150Sstevel@tonic-gate 		bcopy(&scmd32.cmd_cm.c_id.c_name[0],
16160Sstevel@tonic-gate 			&cmdp->cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
16170Sstevel@tonic-gate 		cmdp->cmd_cm.c_flags = scmd32.cmd_cm.c_flags;
16180Sstevel@tonic-gate 		cmdp->cmd_cm.c_len = scmd32.cmd_cm.c_len;
1619930Smathue 		cmdp->cmd_cm.c_opts = (caddr_t)(uintptr_t)scmd32.cmd_cm.c_opts;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		if (cmd == SBD_CMD_PASSTHRU) {
16227240Srh87107 			PR_BYP("passthru copyin: iap=%p, sz=%ld", (void *)iap,
16230Sstevel@tonic-gate 				sizeof (sbd_cmd32_t));
1624930Smathue 			PR_BYP("passthru copyin: c_opts=%x, c_len=%d",
16250Sstevel@tonic-gate 				scmd32.cmd_cm.c_opts,
16260Sstevel@tonic-gate 				scmd32.cmd_cm.c_len);
16270Sstevel@tonic-gate 		}
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 		switch (cmd) {
16300Sstevel@tonic-gate 		case SBD_CMD_STATUS:
16310Sstevel@tonic-gate 			cmdp->cmd_stat.s_nbytes = scmd32.cmd_stat.s_nbytes;
16320Sstevel@tonic-gate 			cmdp->cmd_stat.s_statp =
1633930Smathue 				(caddr_t)(uintptr_t)scmd32.cmd_stat.s_statp;
16340Sstevel@tonic-gate 			break;
16350Sstevel@tonic-gate 		default:
16360Sstevel@tonic-gate 			break;
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 	} else
16400Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
16410Sstevel@tonic-gate 	if (ddi_copyin((void *)iap, (void *)cmdp,
16420Sstevel@tonic-gate 			sizeof (sbd_cmd_t), mode) != 0) {
16430Sstevel@tonic-gate 		cmn_err(CE_WARN,
16440Sstevel@tonic-gate 			"sbd:%s: failed to copyin sbd cmd_t struct", f);
16450Sstevel@tonic-gate 		return (EFAULT);
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * A user may set platform specific options so we need to
16490Sstevel@tonic-gate 	 * copy them in
16500Sstevel@tonic-gate 	 */
16510Sstevel@tonic-gate 	if ((cmd != SBD_CMD_STATUS) && ((hp->h_opts.size = cmdp->cmd_cm.c_len)
16520Sstevel@tonic-gate 	    > 0)) {
16530Sstevel@tonic-gate 		hp->h_opts.size += 1;	/* For null termination of string. */
16540Sstevel@tonic-gate 		hp->h_opts.copts = GETSTRUCT(char, hp->h_opts.size);
16550Sstevel@tonic-gate 		if (ddi_copyin((void *)cmdp->cmd_cm.c_opts,
16560Sstevel@tonic-gate 		    (void *)hp->h_opts.copts,
16570Sstevel@tonic-gate 		    cmdp->cmd_cm.c_len, hp->h_mode) != 0) {
16580Sstevel@tonic-gate 			/* copts is freed in sbd_release_handle(). */
16590Sstevel@tonic-gate 			cmn_err(CE_WARN,
16600Sstevel@tonic-gate 			    "sbd:%s: failed to copyin options", f);
16610Sstevel@tonic-gate 			return (EFAULT);
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	return (0);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate static int
sbd_copyout_ioarg(int mode,int cmd,sbd_cmd_t * scp,sbd_ioctl_arg_t * iap)16690Sstevel@tonic-gate sbd_copyout_ioarg(int mode, int cmd, sbd_cmd_t *scp, sbd_ioctl_arg_t *iap)
16700Sstevel@tonic-gate {
16710Sstevel@tonic-gate 	static fn_t	f = "sbd_copyout_ioarg";
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	if ((iap == NULL) || (scp == NULL))
16740Sstevel@tonic-gate 		return (EINVAL);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
16770Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
16780Sstevel@tonic-gate 		sbd_cmd32_t	scmd32;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		scmd32.cmd_cm.c_id.c_type = scp->cmd_cm.c_id.c_type;
16810Sstevel@tonic-gate 		scmd32.cmd_cm.c_id.c_unit = scp->cmd_cm.c_id.c_unit;
16820Sstevel@tonic-gate 		bcopy(scp->cmd_cm.c_id.c_name,
16830Sstevel@tonic-gate 			scmd32.cmd_cm.c_id.c_name, OBP_MAXPROPNAME);
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 		scmd32.cmd_cm.c_flags = scp->cmd_cm.c_flags;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		switch (cmd) {
16880Sstevel@tonic-gate 		case SBD_CMD_GETNCM:
16890Sstevel@tonic-gate 			scmd32.cmd_getncm.g_ncm = scp->cmd_getncm.g_ncm;
16900Sstevel@tonic-gate 			break;
16910Sstevel@tonic-gate 		default:
16920Sstevel@tonic-gate 			break;
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		if (ddi_copyout((void *)&scmd32, (void *)iap,
16960Sstevel@tonic-gate 				sizeof (sbd_cmd32_t), mode)) {
16970Sstevel@tonic-gate 			cmn_err(CE_WARN,
16980Sstevel@tonic-gate 				"sbd:%s: (32bit) failed to copyout "
16990Sstevel@tonic-gate 					"sbdcmd struct", f);
17000Sstevel@tonic-gate 			return (EFAULT);
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 	} else
17030Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
17040Sstevel@tonic-gate 	if (ddi_copyout((void *)scp, (void *)iap,
17050Sstevel@tonic-gate 			sizeof (sbd_cmd_t), mode) != 0) {
17060Sstevel@tonic-gate 		cmn_err(CE_WARN,
17070Sstevel@tonic-gate 			"sbd:%s: failed to copyout sbdcmd struct", f);
17080Sstevel@tonic-gate 		return (EFAULT);
17090Sstevel@tonic-gate 	}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	return (0);
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate static int
sbd_copyout_errs(int mode,sbd_ioctl_arg_t * iap,void * arg)17150Sstevel@tonic-gate sbd_copyout_errs(int mode, sbd_ioctl_arg_t *iap, void *arg)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	static fn_t	f = "sbd_copyout_errs";
17180Sstevel@tonic-gate 	sbd_ioctl_arg_t	*uap;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	uap = (sbd_ioctl_arg_t *)arg;
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
17230Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
17240Sstevel@tonic-gate 		sbd_error32_t err32;
17250Sstevel@tonic-gate 		sbd_ioctl_arg32_t *uap32;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 		uap32 = (sbd_ioctl_arg32_t *)arg;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 		err32.e_code = iap->ie_code;
17300Sstevel@tonic-gate 		(void) strcpy(err32.e_rsc, iap->ie_rsc);
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 		if (ddi_copyout((void *)&err32, (void *)&uap32->i_err,
17330Sstevel@tonic-gate 				sizeof (sbd_error32_t), mode)) {
17340Sstevel@tonic-gate 			cmn_err(CE_WARN,
17350Sstevel@tonic-gate 				"sbd:%s: failed to copyout ioctl32 errs",
17360Sstevel@tonic-gate 				f);
17370Sstevel@tonic-gate 			return (EFAULT);
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 	} else
17400Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
17410Sstevel@tonic-gate 	if (ddi_copyout((void *)&iap->i_err, (void *)&uap->i_err,
17420Sstevel@tonic-gate 			sizeof (sbd_error_t), mode) != 0) {
17430Sstevel@tonic-gate 		cmn_err(CE_WARN,
17440Sstevel@tonic-gate 			"sbd:%s: failed to copyout ioctl errs", f);
17450Sstevel@tonic-gate 		return (EFAULT);
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	return (0);
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*
17520Sstevel@tonic-gate  * State transition policy is that if at least one
17530Sstevel@tonic-gate  * device cannot make the transition, then none of
17540Sstevel@tonic-gate  * the requested devices are allowed to transition.
17550Sstevel@tonic-gate  *
17560Sstevel@tonic-gate  * Returns the state that is in error, if any.
17570Sstevel@tonic-gate  */
17580Sstevel@tonic-gate static int
sbd_check_transition(sbd_board_t * sbp,sbd_devset_t * devsetp,struct sbd_state_trans * transp)17590Sstevel@tonic-gate sbd_check_transition(sbd_board_t *sbp, sbd_devset_t *devsetp,
17600Sstevel@tonic-gate 			struct sbd_state_trans *transp)
17610Sstevel@tonic-gate {
17620Sstevel@tonic-gate 	int	s, ut;
17630Sstevel@tonic-gate 	int	state_err = 0;
17640Sstevel@tonic-gate 	sbd_devset_t	devset;
17650Sstevel@tonic-gate 	static fn_t	f = "sbd_check_transition";
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	devset = *devsetp;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	if (!devset) {
17700Sstevel@tonic-gate 		/*
17710Sstevel@tonic-gate 		 * Transition does not deal with any components.
17720Sstevel@tonic-gate 		 * This is the case for addboard/deleteboard.
17730Sstevel@tonic-gate 		 */
17740Sstevel@tonic-gate 		PR_ALL("%s: no devs: requested devset = 0x%x,"
17750Sstevel@tonic-gate 			" final devset = 0x%x\n",
17760Sstevel@tonic-gate 			f, (uint_t)*devsetp, (uint_t)devset);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 		return (0);
17790Sstevel@tonic-gate 	}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
17820Sstevel@tonic-gate 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
17830Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_MEM, ut) == 0)
17840Sstevel@tonic-gate 				continue;
17850Sstevel@tonic-gate 			s = (int)SBD_DEVICE_STATE(sbp, SBD_COMP_MEM, ut);
17860Sstevel@tonic-gate 			if (transp->x_op[s].x_rv) {
17870Sstevel@tonic-gate 				if (!state_err)
17880Sstevel@tonic-gate 					state_err = s;
17890Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_MEM, ut);
17900Sstevel@tonic-gate 			}
17910Sstevel@tonic-gate 		}
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
17950Sstevel@tonic-gate 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
17960Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_CPU, ut) == 0)
17970Sstevel@tonic-gate 				continue;
17980Sstevel@tonic-gate 			s = (int)SBD_DEVICE_STATE(sbp, SBD_COMP_CPU, ut);
17990Sstevel@tonic-gate 			if (transp->x_op[s].x_rv) {
18000Sstevel@tonic-gate 				if (!state_err)
18010Sstevel@tonic-gate 					state_err = s;
18020Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_CPU, ut);
18030Sstevel@tonic-gate 			}
18040Sstevel@tonic-gate 		}
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
18080Sstevel@tonic-gate 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
18090Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_IO, ut) == 0)
18100Sstevel@tonic-gate 				continue;
18110Sstevel@tonic-gate 			s = (int)SBD_DEVICE_STATE(sbp, SBD_COMP_IO, ut);
18120Sstevel@tonic-gate 			if (transp->x_op[s].x_rv) {
18130Sstevel@tonic-gate 				if (!state_err)
18140Sstevel@tonic-gate 					state_err = s;
18150Sstevel@tonic-gate 				DEVSET_DEL(devset, SBD_COMP_IO, ut);
18160Sstevel@tonic-gate 			}
18170Sstevel@tonic-gate 		}
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	PR_ALL("%s: requested devset = 0x%x, final devset = 0x%x\n",
18210Sstevel@tonic-gate 		f, (uint_t)*devsetp, (uint_t)devset);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	*devsetp = devset;
18240Sstevel@tonic-gate 	/*
18250Sstevel@tonic-gate 	 * If there are some remaining components for which
18260Sstevel@tonic-gate 	 * this state transition is valid, then allow them
18270Sstevel@tonic-gate 	 * through, otherwise if none are left then return
18280Sstevel@tonic-gate 	 * the state error.
18290Sstevel@tonic-gate 	 */
18300Sstevel@tonic-gate 	return (devset ? 0 : state_err);
18310Sstevel@tonic-gate }
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate /*
18340Sstevel@tonic-gate  * pre-op entry point must SET_ERRNO(), if needed.
18350Sstevel@tonic-gate  * Return value of non-zero indicates failure.
18360Sstevel@tonic-gate  */
18370Sstevel@tonic-gate static int
sbd_pre_op(sbd_handle_t * hp)18380Sstevel@tonic-gate sbd_pre_op(sbd_handle_t *hp)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	int		rv = 0, t;
18410Sstevel@tonic-gate 	int		cmd, serr = 0;
18420Sstevel@tonic-gate 	sbd_devset_t	devset;
18430Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
18440Sstevel@tonic-gate 	sbd_priv_handle_t	*shp = HD2MACHHD(hp);
18450Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
18460Sstevel@tonic-gate 	sbd_cmd_t	*cmdp;
18470Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_op";
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	cmd = hp->h_cmd;
18500Sstevel@tonic-gate 	devset = shp->sh_devset;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	switch (cmd) {
18530Sstevel@tonic-gate 		case SBD_CMD_CONNECT:
18540Sstevel@tonic-gate 		case SBD_CMD_DISCONNECT:
18550Sstevel@tonic-gate 		case SBD_CMD_UNCONFIGURE:
18560Sstevel@tonic-gate 		case SBD_CMD_CONFIGURE:
18570Sstevel@tonic-gate 		case SBD_CMD_ASSIGN:
18580Sstevel@tonic-gate 		case SBD_CMD_UNASSIGN:
18590Sstevel@tonic-gate 		case SBD_CMD_POWERON:
18600Sstevel@tonic-gate 		case SBD_CMD_POWEROFF:
18610Sstevel@tonic-gate 		case SBD_CMD_TEST:
18620Sstevel@tonic-gate 		/* ioctls allowed if caller has write permission */
18630Sstevel@tonic-gate 		if (!(hp->h_mode & FWRITE)) {
18640Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, EPERM);
18650Sstevel@tonic-gate 			return (-1);
18660Sstevel@tonic-gate 		}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		default:
18690Sstevel@tonic-gate 		break;
18700Sstevel@tonic-gate 	}
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	hp->h_iap = GETSTRUCT(sbd_ioctl_arg_t, 1);
18730Sstevel@tonic-gate 	rv = sbd_copyin_ioarg(hp, hp->h_mode, cmd,
18740Sstevel@tonic-gate 		(sbd_cmd_t *)hp->h_iap, shp->sh_arg);
18750Sstevel@tonic-gate 	if (rv) {
18760Sstevel@tonic-gate 		SBD_SET_ERRNO(ep, rv);
18770Sstevel@tonic-gate 		FREESTRUCT(hp->h_iap, sbd_ioctl_arg_t, 1);
18780Sstevel@tonic-gate 		hp->h_iap = NULL;
18790Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: copyin fail", f);
18800Sstevel@tonic-gate 		return (-1);
18810Sstevel@tonic-gate 	} else {
18820Sstevel@tonic-gate 		cmdp =  (sbd_cmd_t *)hp->h_iap;
18830Sstevel@tonic-gate 		if (cmdp->cmd_cm.c_id.c_name[0] != '\0') {
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 			cmdp->cmd_cm.c_id.c_type = SBD_COMP(sbd_name_to_idx(
18860Sstevel@tonic-gate 				cmdp->cmd_cm.c_id.c_name));
18870Sstevel@tonic-gate 			if (cmdp->cmd_cm.c_id.c_type == SBD_COMP_MEM) {
18880Sstevel@tonic-gate 				if (cmdp->cmd_cm.c_id.c_unit == -1)
18890Sstevel@tonic-gate 					cmdp->cmd_cm.c_id.c_unit = 0;
18900Sstevel@tonic-gate 			}
18910Sstevel@tonic-gate 		}
18920Sstevel@tonic-gate 		devset = shp->sh_orig_devset = shp->sh_devset =
18930Sstevel@tonic-gate 		    sbd_dev2devset(&cmdp->cmd_cm.c_id);
18940Sstevel@tonic-gate 		if (devset == 0) {
18950Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, EINVAL);
18960Sstevel@tonic-gate 			FREESTRUCT(hp->h_iap, sbd_ioctl_arg_t, 1);
18970Sstevel@tonic-gate 			hp->h_iap = NULL;
18980Sstevel@tonic-gate 			return (-1);
18990Sstevel@tonic-gate 		}
19000Sstevel@tonic-gate 	}
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/*
19030Sstevel@tonic-gate 	 * Always turn on these bits ala Sunfire DR.
19040Sstevel@tonic-gate 	 */
19050Sstevel@tonic-gate 	hp->h_flags |= SBD_FLAG_DEVI_FORCE;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	if (cmdp->cmd_cm.c_flags & SBD_FLAG_FORCE)
19080Sstevel@tonic-gate 		hp->h_flags |= SBD_IOCTL_FLAG_FORCE;
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	/*
19110Sstevel@tonic-gate 	 * Check for valid state transitions.
19120Sstevel@tonic-gate 	 */
19130Sstevel@tonic-gate 	if (!serr && ((t = CMD2INDEX(cmd)) != -1)) {
19140Sstevel@tonic-gate 		struct sbd_state_trans	*transp;
19150Sstevel@tonic-gate 		int			state_err;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		transp = &sbd_state_transition[t];
19180Sstevel@tonic-gate 		ASSERT(transp->x_cmd == cmd);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 		state_err = sbd_check_transition(sbp, &devset, transp);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 		if (state_err < 0) {
19230Sstevel@tonic-gate 			/*
19240Sstevel@tonic-gate 			 * Invalidate device.
19250Sstevel@tonic-gate 			 */
19260Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, ENOTTY);
19270Sstevel@tonic-gate 			serr = -1;
19280Sstevel@tonic-gate 			PR_ALL("%s: invalid devset (0x%x)\n",
19290Sstevel@tonic-gate 				f, (uint_t)devset);
19300Sstevel@tonic-gate 		} else if (state_err != 0) {
19310Sstevel@tonic-gate 			/*
19320Sstevel@tonic-gate 			 * State transition is not a valid one.
19330Sstevel@tonic-gate 			 */
19340Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, transp->x_op[state_err].x_err);
19350Sstevel@tonic-gate 			serr = transp->x_op[state_err].x_rv;
19360Sstevel@tonic-gate 			PR_ALL("%s: invalid state %s(%d) for cmd %s(%d)\n",
19370Sstevel@tonic-gate 				f, sbd_state_str[state_err], state_err,
19380Sstevel@tonic-gate 				SBD_CMD_STR(cmd), cmd);
19390Sstevel@tonic-gate 		}
19400Sstevel@tonic-gate 		if (serr && SBD_GET_ERRNO(ep) != 0) {
19410Sstevel@tonic-gate 			/*
19420Sstevel@tonic-gate 			 * A state transition error occurred.
19430Sstevel@tonic-gate 			 */
19440Sstevel@tonic-gate 			if (serr < 0) {
19450Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_INVAL);
19460Sstevel@tonic-gate 			} else {
19470Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_STATE);
19480Sstevel@tonic-gate 			}
19490Sstevel@tonic-gate 			PR_ALL("%s: invalid state transition\n", f);
19500Sstevel@tonic-gate 		} else {
19510Sstevel@tonic-gate 			shp->sh_devset = devset;
19520Sstevel@tonic-gate 		}
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	if (serr && !rv && hp->h_iap) {
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 		/*
19580Sstevel@tonic-gate 		 * There was a state error.  We successfully copied
19590Sstevel@tonic-gate 		 * in the ioctl argument, so let's fill in the
19600Sstevel@tonic-gate 		 * error and copy it back out.
19610Sstevel@tonic-gate 		 */
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 		if (SBD_GET_ERR(ep) && SBD_GET_ERRNO(ep) == 0)
19640Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, EIO);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		SBD_SET_IOCTL_ERR(&hp->h_iap->i_err,
19670Sstevel@tonic-gate 			ep->e_code,
19680Sstevel@tonic-gate 			ep->e_rsc);
19690Sstevel@tonic-gate 		(void) sbd_copyout_errs(hp->h_mode, hp->h_iap, shp->sh_arg);
19700Sstevel@tonic-gate 		FREESTRUCT(hp->h_iap, sbd_ioctl_arg_t, 1);
19710Sstevel@tonic-gate 		hp->h_iap = NULL;
19720Sstevel@tonic-gate 		rv = -1;
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	return (rv);
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate static void
sbd_post_op(sbd_handle_t * hp)19790Sstevel@tonic-gate sbd_post_op(sbd_handle_t *hp)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate 	int		cmd;
19820Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
19830Sstevel@tonic-gate 	sbd_priv_handle_t	*shp = HD2MACHHD(hp);
19840Sstevel@tonic-gate 	sbd_board_t    *sbp = SBDH2BD(hp->h_sbd);
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	cmd = hp->h_cmd;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	switch (cmd) {
19890Sstevel@tonic-gate 		case SBD_CMD_CONFIGURE:
19900Sstevel@tonic-gate 		case SBD_CMD_UNCONFIGURE:
19910Sstevel@tonic-gate 		case SBD_CMD_CONNECT:
19920Sstevel@tonic-gate 		case SBD_CMD_DISCONNECT:
19930Sstevel@tonic-gate 			sbp->sb_time = gethrestime_sec();
19940Sstevel@tonic-gate 			break;
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 		default:
19970Sstevel@tonic-gate 			break;
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	if (SBD_GET_ERR(ep) && SBD_GET_ERRNO(ep) == 0) {
20010Sstevel@tonic-gate 		SBD_SET_ERRNO(ep, EIO);
20020Sstevel@tonic-gate 	}
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	if (shp->sh_arg != NULL) {
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		if (SBD_GET_ERR(ep) != ESBD_NOERROR) {
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 			SBD_SET_IOCTL_ERR(&hp->h_iap->i_err,
20090Sstevel@tonic-gate 				ep->e_code,
20100Sstevel@tonic-gate 				ep->e_rsc);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 			(void) sbd_copyout_errs(hp->h_mode, hp->h_iap,
20130Sstevel@tonic-gate 					shp->sh_arg);
20140Sstevel@tonic-gate 		}
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 		if (hp->h_iap != NULL) {
20170Sstevel@tonic-gate 			FREESTRUCT(hp->h_iap, sbd_ioctl_arg_t, 1);
20180Sstevel@tonic-gate 			hp->h_iap = NULL;
20190Sstevel@tonic-gate 		}
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate }
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate static int
sbd_probe_board(sbd_handle_t * hp)20240Sstevel@tonic-gate sbd_probe_board(sbd_handle_t *hp)
20250Sstevel@tonic-gate {
20260Sstevel@tonic-gate 	int		rv;
20270Sstevel@tonic-gate 	sbd_board_t    *sbp;
20280Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
20290Sstevel@tonic-gate 	static fn_t	f = "sbd_probe_board";
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	ASSERT(sbp != NULL);
20340Sstevel@tonic-gate 	PR_ALL("%s for board %d", f, sbp->sb_num);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	if ((rv = sbdp_connect_board(hdp)) != 0) {
20400Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
20430Sstevel@tonic-gate 	}
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	/*
20460Sstevel@tonic-gate 	 * We need to force a recache after the connect.  The cached
20470Sstevel@tonic-gate 	 * info may be incorrect
20480Sstevel@tonic-gate 	 */
20490Sstevel@tonic-gate 	mutex_enter(&sbp->sb_flags_mutex);
20500Sstevel@tonic-gate 	sbp->sb_flags &= ~SBD_BOARD_STATUS_CACHED;
20510Sstevel@tonic-gate 	mutex_exit(&sbp->sb_flags_mutex);
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_PROBE_BOARD_PSEUDO_ERR, hp->h_err, EIO,
20540Sstevel@tonic-gate 		ESGT_PROBE, NULL);
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	return (rv);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate static int
sbd_deprobe_board(sbd_handle_t * hp)20620Sstevel@tonic-gate sbd_deprobe_board(sbd_handle_t *hp)
20630Sstevel@tonic-gate {
20640Sstevel@tonic-gate 	int		rv;
20650Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
20660Sstevel@tonic-gate 	sbd_board_t	*sbp;
20670Sstevel@tonic-gate 	static fn_t	f = "sbd_deprobe_board";
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	if ((rv = sbdp_disconnect_board(hdp)) != 0) {
20760Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	mutex_enter(&sbp->sb_flags_mutex);
20820Sstevel@tonic-gate 	sbp->sb_flags &= ~SBD_BOARD_STATUS_CACHED;
20830Sstevel@tonic-gate 	mutex_exit(&sbp->sb_flags_mutex);
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_DEPROBE_BOARD_PSEUDO_ERR, hp->h_err, EIO,
20860Sstevel@tonic-gate 		ESGT_DEPROBE, NULL);
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
20890Sstevel@tonic-gate 	return (rv);
20900Sstevel@tonic-gate }
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate /*
20930Sstevel@tonic-gate  * Check if a CPU node is part of a CMP.
20940Sstevel@tonic-gate  */
20950Sstevel@tonic-gate int
sbd_is_cmp_child(dev_info_t * dip)20960Sstevel@tonic-gate sbd_is_cmp_child(dev_info_t *dip)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate 	dev_info_t *pdip;
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if (strcmp(ddi_node_name(dip), "cpu") != 0) {
21010Sstevel@tonic-gate 		return (0);
21020Sstevel@tonic-gate 	}
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	ASSERT(pdip);
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if (strcmp(ddi_node_name(pdip), "cmp") == 0) {
21090Sstevel@tonic-gate 		return (1);
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	return (0);
21130Sstevel@tonic-gate }
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate /*
21160Sstevel@tonic-gate  * Returns the nodetype if dip is a top dip on the board of
21170Sstevel@tonic-gate  * interest or SBD_COMP_UNKNOWN otherwise
21180Sstevel@tonic-gate  */
21190Sstevel@tonic-gate static sbd_comp_type_t
get_node_type(sbd_board_t * sbp,dev_info_t * dip,int * unitp)21200Sstevel@tonic-gate get_node_type(sbd_board_t *sbp, dev_info_t *dip, int *unitp)
21210Sstevel@tonic-gate {
21220Sstevel@tonic-gate 	int		idx, unit;
21230Sstevel@tonic-gate 	sbd_handle_t	*hp;
21240Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
21250Sstevel@tonic-gate 	char		otype[OBP_MAXDRVNAME];
21260Sstevel@tonic-gate 	int		otypelen;
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	ASSERT(sbp);
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	if (unitp)
21310Sstevel@tonic-gate 		*unitp = -1;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	hp = MACHBD2HD(sbp);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
21360Sstevel@tonic-gate 	if (sbdp_get_board_num(hdp, dip) != sbp->sb_num) {
21370Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
21380Sstevel@tonic-gate 		return (SBD_COMP_UNKNOWN);
21390Sstevel@tonic-gate 	}
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	/*
21420Sstevel@tonic-gate 	 * sbdp_get_unit_num will return (-1) for cmp as there
21430Sstevel@tonic-gate 	 * is no "device_type" property associated with cmp.
21440Sstevel@tonic-gate 	 * Therefore we will just skip getting unit number for
21450Sstevel@tonic-gate 	 * cmp.  Callers of this function need to check the
21460Sstevel@tonic-gate 	 * value set in unitp before using it to dereference
21470Sstevel@tonic-gate 	 * an array.
21480Sstevel@tonic-gate 	 */
21490Sstevel@tonic-gate 	if (strcmp(ddi_node_name(dip), "cmp") == 0) {
21500Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
21510Sstevel@tonic-gate 		return (SBD_COMP_CMP);
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	otypelen = sizeof (otype);
21550Sstevel@tonic-gate 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
21560Sstevel@tonic-gate 	    OBP_DEVICETYPE,  (caddr_t)otype, &otypelen)) {
21570Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
21580Sstevel@tonic-gate 		return (SBD_COMP_UNKNOWN);
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	idx = sbd_otype_to_idx(otype);
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if (SBD_COMP(idx) == SBD_COMP_UNKNOWN) {
21640Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
21650Sstevel@tonic-gate 		return (SBD_COMP_UNKNOWN);
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	unit = sbdp_get_unit_num(hdp, dip);
21690Sstevel@tonic-gate 	if (unit == -1) {
21700Sstevel@tonic-gate 		cmn_err(CE_WARN,
21710Sstevel@tonic-gate 			"get_node_type: %s unit fail %p", otype, (void *)dip);
21720Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
21730Sstevel@tonic-gate 		return (SBD_COMP_UNKNOWN);
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	if (unitp)
21790Sstevel@tonic-gate 		*unitp = unit;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	return (SBD_COMP(idx));
21820Sstevel@tonic-gate }
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate typedef struct {
21850Sstevel@tonic-gate 	sbd_board_t	*sbp;
21860Sstevel@tonic-gate 	int		nmc;
21870Sstevel@tonic-gate 	int		hold;
21880Sstevel@tonic-gate } walk_tree_t;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate static int
sbd_setup_devlists(dev_info_t * dip,void * arg)21910Sstevel@tonic-gate sbd_setup_devlists(dev_info_t *dip, void *arg)
21920Sstevel@tonic-gate {
21930Sstevel@tonic-gate 	walk_tree_t	*wp;
21940Sstevel@tonic-gate 	dev_info_t	**devlist = NULL;
21950Sstevel@tonic-gate 	char		*pathname = NULL;
21960Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
21970Sstevel@tonic-gate 	static fn_t	f = "sbd_setup_devlists";
21980Sstevel@tonic-gate 	sbd_board_t	*sbp;
21990Sstevel@tonic-gate 	int		unit;
22000Sstevel@tonic-gate 	sbd_comp_type_t nodetype;
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	ASSERT(dip);
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	wp = (walk_tree_t *)arg;
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 	if (wp == NULL) {
22070Sstevel@tonic-gate 		PR_ALL("%s:bad arg\n", f);
22080Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	sbp = wp->sbp;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	nodetype = get_node_type(sbp, dip, &unit);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	switch (nodetype) {
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	case SBD_COMP_CPU:
22180Sstevel@tonic-gate 		pathname = sbp->sb_cpupath[unit];
22190Sstevel@tonic-gate 		break;
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	case SBD_COMP_MEM:
22220Sstevel@tonic-gate 		pathname = sbp->sb_mempath[unit];
22230Sstevel@tonic-gate 		break;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	case SBD_COMP_IO:
22260Sstevel@tonic-gate 		pathname = sbp->sb_iopath[unit];
22270Sstevel@tonic-gate 		break;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	case SBD_COMP_CMP:
22300Sstevel@tonic-gate 	case SBD_COMP_UNKNOWN:
22310Sstevel@tonic-gate 		/*
22320Sstevel@tonic-gate 		 * This dip is not of interest to us
22330Sstevel@tonic-gate 		 */
22340Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	default:
22370Sstevel@tonic-gate 		ASSERT(0);
22380Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
22390Sstevel@tonic-gate 	}
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	/*
22420Sstevel@tonic-gate 	 * dip's parent is being held busy by ddi_walk_devs(),
22430Sstevel@tonic-gate 	 * so dip doesn't have to be held while calling ddi_pathname()
22440Sstevel@tonic-gate 	 */
22450Sstevel@tonic-gate 	if (pathname) {
22460Sstevel@tonic-gate 		(void) ddi_pathname(dip, pathname);
22470Sstevel@tonic-gate 	}
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	devlist = sbp->sb_devlist[NIX(nodetype)];
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * The branch rooted at dip should already be held,
22530Sstevel@tonic-gate 	 * unless we are dealing with a core of a CMP.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	ASSERT(sbd_is_cmp_child(dip) || e_ddi_branch_held(dip));
22560Sstevel@tonic-gate 	devlist[unit] = dip;
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	/*
22590Sstevel@tonic-gate 	 * This test is required if multiple devices are considered
22600Sstevel@tonic-gate 	 * as one. This is the case for memory-controller nodes.
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	if (!SBD_DEV_IS_PRESENT(sbp, nodetype, unit)) {
22630Sstevel@tonic-gate 		sbp->sb_ndev++;
22640Sstevel@tonic-gate 		SBD_DEV_SET_PRESENT(sbp, nodetype, unit);
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	if (nodetype == SBD_COMP_MEM) {
22680Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
22690Sstevel@tonic-gate 		ASSERT(wp->nmc < SBD_NUM_MC_PER_BOARD);
22700Sstevel@tonic-gate 		mp->sbm_dip[wp->nmc++] = dip;
22710Sstevel@tonic-gate 	}
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate /*
22770Sstevel@tonic-gate  * This routine is used to construct the memory devlist.
22780Sstevel@tonic-gate  * In Starcat and Serengeti platforms, a system board can contain up to
22790Sstevel@tonic-gate  * four memory controllers (MC).  The MCs have been programmed by POST for
22800Sstevel@tonic-gate  * optimum memory interleaving amongst their peers on the same board.
22810Sstevel@tonic-gate  * This DR driver does not support deinterleaving.  Therefore, the smallest
22820Sstevel@tonic-gate  * unit of memory that can be manipulated by this driver is all of the
22830Sstevel@tonic-gate  * memory on a board.  Because of this restriction, a board's memory devlist
22840Sstevel@tonic-gate  * is populated with only one of the four (possible) MC dnodes on that board.
22850Sstevel@tonic-gate  * Care must be taken to ensure that the selected MC dnode represents the
22860Sstevel@tonic-gate  * lowest physical address to which memory on the board will respond to.
22870Sstevel@tonic-gate  * This is required in order to preserve the semantics of
22880Sstevel@tonic-gate  * sbdp_get_base_physaddr() when applied to a MC dnode stored in the
22890Sstevel@tonic-gate  * memory devlist.
22900Sstevel@tonic-gate  */
22910Sstevel@tonic-gate static void
sbd_init_mem_devlists(sbd_board_t * sbp)22920Sstevel@tonic-gate sbd_init_mem_devlists(sbd_board_t *sbp)
22930Sstevel@tonic-gate {
22940Sstevel@tonic-gate 	dev_info_t	**devlist;
22950Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
22960Sstevel@tonic-gate 	dev_info_t	*mc_dip;
22970Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
22980Sstevel@tonic-gate 	uint64_t	mc_pa, lowest_pa;
22990Sstevel@tonic-gate 	int		i;
23000Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	devlist = sbp->sb_devlist[NIX(SBD_COMP_MEM)];
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, 0);
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	mc_dip = mp->sbm_dip[0];
23070Sstevel@tonic-gate 	if (mc_dip == NULL)
23080Sstevel@tonic-gate 		return;		/* No MC dips found for this board */
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	if (sbdphw_get_base_physaddr(hdp, mc_dip, &mc_pa)) {
23130Sstevel@tonic-gate 		/* TODO: log complaint about dnode */
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate pretend_no_mem:
23160Sstevel@tonic-gate 		/*
23170Sstevel@tonic-gate 		 * We are here because sbdphw_get_base_physaddr() failed.
23180Sstevel@tonic-gate 		 * Although it is very unlikely to happen, it did.  Lucky us.
23190Sstevel@tonic-gate 		 * Since we can no longer examine _all_ of the MCs on this
23200Sstevel@tonic-gate 		 * board to determine which one is programmed to the lowest
23210Sstevel@tonic-gate 		 * physical address, we cannot involve any of the MCs on
23220Sstevel@tonic-gate 		 * this board in DR operations.  To ensure this, we pretend
23230Sstevel@tonic-gate 		 * that this board does not contain any memory.
23240Sstevel@tonic-gate 		 *
23250Sstevel@tonic-gate 		 * Paranoia: clear the dev_present mask.
23260Sstevel@tonic-gate 		 */
23270Sstevel@tonic-gate 		if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_MEM, 0)) {
23280Sstevel@tonic-gate 			ASSERT(sbp->sb_ndev != 0);
23290Sstevel@tonic-gate 			SBD_DEV_CLR_PRESENT(sbp, SBD_COMP_MEM, 0);
23300Sstevel@tonic-gate 			sbp->sb_ndev--;
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 		for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
23340Sstevel@tonic-gate 			mp->sbm_dip[i] = NULL;
23350Sstevel@tonic-gate 		}
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
23380Sstevel@tonic-gate 		return;
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	/* assume this one will win. */
23420Sstevel@tonic-gate 	devlist[0] = mc_dip;
23430Sstevel@tonic-gate 	mp->sbm_cm.sbdev_dip = mc_dip;
23440Sstevel@tonic-gate 	lowest_pa = mc_pa;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	/*
23470Sstevel@tonic-gate 	 * We know the base physical address of one of the MC devices.  Now
23480Sstevel@tonic-gate 	 * we will enumerate through all of the remaining MC devices on
23490Sstevel@tonic-gate 	 * the board to find which of them is programmed to the lowest
23500Sstevel@tonic-gate 	 * physical address.
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	for (i = 1; i < SBD_NUM_MC_PER_BOARD; i++) {
23530Sstevel@tonic-gate 		mc_dip = mp->sbm_dip[i];
23540Sstevel@tonic-gate 		if (mc_dip == NULL) {
23550Sstevel@tonic-gate 			break;
23560Sstevel@tonic-gate 		}
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 		if (sbdphw_get_base_physaddr(hdp, mc_dip, &mc_pa)) {
23590Sstevel@tonic-gate 			cmn_err(CE_NOTE, "No mem on board %d unit %d",
23600Sstevel@tonic-gate 				sbp->sb_num, i);
23610Sstevel@tonic-gate 			break;
23620Sstevel@tonic-gate 		}
23630Sstevel@tonic-gate 		if (mc_pa < lowest_pa) {
23640Sstevel@tonic-gate 			mp->sbm_cm.sbdev_dip = mc_dip;
23650Sstevel@tonic-gate 			devlist[0] = mc_dip;
23660Sstevel@tonic-gate 			lowest_pa = mc_pa;
23670Sstevel@tonic-gate 		}
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
23710Sstevel@tonic-gate }
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate static int
sbd_name_to_idx(char * name)23740Sstevel@tonic-gate sbd_name_to_idx(char *name)
23750Sstevel@tonic-gate {
23760Sstevel@tonic-gate 	int idx;
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	for (idx = 0; SBD_COMP(idx) != SBD_COMP_UNKNOWN; idx++) {
23790Sstevel@tonic-gate 		if (strcmp(name, SBD_DEVNAME(idx)) == 0) {
23800Sstevel@tonic-gate 			break;
23810Sstevel@tonic-gate 		}
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	return (idx);
23850Sstevel@tonic-gate }
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate static int
sbd_otype_to_idx(char * otype)23880Sstevel@tonic-gate sbd_otype_to_idx(char *otype)
23890Sstevel@tonic-gate {
23900Sstevel@tonic-gate 	int idx;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	for (idx = 0; SBD_COMP(idx) != SBD_COMP_UNKNOWN; idx++) {
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 		if (strcmp(otype, SBD_OTYPE(idx)) == 0) {
23950Sstevel@tonic-gate 			break;
23960Sstevel@tonic-gate 		}
23970Sstevel@tonic-gate 	}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	return (idx);
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate static int
sbd_init_devlists(sbd_board_t * sbp)24030Sstevel@tonic-gate sbd_init_devlists(sbd_board_t *sbp)
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate 	int		i;
24060Sstevel@tonic-gate 	sbd_dev_unit_t	*dp;
24070Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
24080Sstevel@tonic-gate 	walk_tree_t	*wp, walk = {0};
24090Sstevel@tonic-gate 	dev_info_t	*pdip;
24100Sstevel@tonic-gate 	static fn_t	f = "sbd_init_devlists";
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	PR_ALL("%s (board = %d)...\n", f, sbp->sb_num);
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	wp = &walk;
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	SBD_DEVS_DISCONNECT(sbp, (uint_t)-1);
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	/*
24190Sstevel@tonic-gate 	 * Clear out old entries, if any.
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
24230Sstevel@tonic-gate 		sbp->sb_devlist[NIX(SBD_COMP_MEM)][i] = NULL;
24240Sstevel@tonic-gate 		dp = (sbd_dev_unit_t *)SBD_GET_BOARD_MEMUNIT(sbp, i);
24250Sstevel@tonic-gate 		dp->u_common.sbdev_sbp = sbp;
24260Sstevel@tonic-gate 		dp->u_common.sbdev_unum = i;
24270Sstevel@tonic-gate 		dp->u_common.sbdev_type = SBD_COMP_MEM;
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, 0);
24310Sstevel@tonic-gate 	ASSERT(mp != NULL);
24320Sstevel@tonic-gate 	for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
24330Sstevel@tonic-gate 		mp->sbm_dip[i] = NULL;
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
24370Sstevel@tonic-gate 		sbp->sb_devlist[NIX(SBD_COMP_CPU)][i] = NULL;
24380Sstevel@tonic-gate 		dp = (sbd_dev_unit_t *)SBD_GET_BOARD_CPUUNIT(sbp, i);
24390Sstevel@tonic-gate 		dp->u_common.sbdev_sbp = sbp;
24400Sstevel@tonic-gate 		dp->u_common.sbdev_unum = i;
24410Sstevel@tonic-gate 		dp->u_common.sbdev_type = SBD_COMP_CPU;
24420Sstevel@tonic-gate 	}
24430Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
24440Sstevel@tonic-gate 		sbp->sb_devlist[NIX(SBD_COMP_IO)][i] = NULL;
24450Sstevel@tonic-gate 		dp = (sbd_dev_unit_t *)SBD_GET_BOARD_IOUNIT(sbp, i);
24460Sstevel@tonic-gate 		dp->u_common.sbdev_sbp = sbp;
24470Sstevel@tonic-gate 		dp->u_common.sbdev_unum = i;
24480Sstevel@tonic-gate 		dp->u_common.sbdev_type = SBD_COMP_IO;
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	wp->sbp = sbp;
24520Sstevel@tonic-gate 	wp->nmc = 0;
24530Sstevel@tonic-gate 	sbp->sb_ndev = 0;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	/*
24560Sstevel@tonic-gate 	 * ddi_walk_devs() requires that topdip's parent be held.
24570Sstevel@tonic-gate 	 */
24580Sstevel@tonic-gate 	pdip = ddi_get_parent(sbp->sb_topdip);
24590Sstevel@tonic-gate 	if (pdip) {
24600Sstevel@tonic-gate 		ndi_hold_devi(pdip);
24610Sstevel@tonic-gate 		ndi_devi_enter(pdip, &i);
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 	ddi_walk_devs(sbp->sb_topdip, sbd_setup_devlists, (void *) wp);
24640Sstevel@tonic-gate 	if (pdip) {
24650Sstevel@tonic-gate 		ndi_devi_exit(pdip, i);
24660Sstevel@tonic-gate 		ndi_rele_devi(pdip);
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * There is no point checking all the components if there
24710Sstevel@tonic-gate 	 * are no devices.
24720Sstevel@tonic-gate 	 */
24730Sstevel@tonic-gate 	if (sbp->sb_ndev == 0) {
24740Sstevel@tonic-gate 		sbp->sb_memaccess_ok = 0;
24750Sstevel@tonic-gate 		return (sbp->sb_ndev);
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	/*
24790Sstevel@tonic-gate 	 * Initialize cpu sections before calling sbd_init_mem_devlists
24800Sstevel@tonic-gate 	 * which will access the mmus.
24810Sstevel@tonic-gate 	 */
24820Sstevel@tonic-gate 	sbp->sb_memaccess_ok = 1;
24830Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
24840Sstevel@tonic-gate 		if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_CPU, i)) {
24850Sstevel@tonic-gate 			sbd_init_cpu_unit(sbp, i);
24860Sstevel@tonic-gate 			if (sbd_connect_cpu(sbp, i)) {
24870Sstevel@tonic-gate 				SBD_SET_ERR(HD2MACHERR(MACHBD2HD(sbp)),
24880Sstevel@tonic-gate 					ESBD_CPUSTART);
24890Sstevel@tonic-gate 			}
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 		}
24920Sstevel@tonic-gate 	}
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	if (sbp->sb_memaccess_ok) {
24950Sstevel@tonic-gate 		sbd_init_mem_devlists(sbp);
24960Sstevel@tonic-gate 	} else {
24970Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to access memory on board %d",
24980Sstevel@tonic-gate 		    sbp->sb_num);
24990Sstevel@tonic-gate 	}
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	return (sbp->sb_ndev);
25020Sstevel@tonic-gate }
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate static void
sbd_init_cpu_unit(sbd_board_t * sbp,int unit)25050Sstevel@tonic-gate sbd_init_cpu_unit(sbd_board_t *sbp, int unit)
25060Sstevel@tonic-gate {
25070Sstevel@tonic-gate 	sbd_istate_t	new_state;
25080Sstevel@tonic-gate 	sbd_cpu_unit_t	*cp;
25090Sstevel@tonic-gate 	int		cpuid;
25100Sstevel@tonic-gate 	dev_info_t	*dip;
25110Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
25120Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
25130Sstevel@tonic-gate 	extern kmutex_t	cpu_lock;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_CPU, unit)) {
25160Sstevel@tonic-gate 		new_state = SBD_STATE_CONFIGURED;
25170Sstevel@tonic-gate 	} else if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_CPU, unit)) {
25180Sstevel@tonic-gate 		new_state = SBD_STATE_CONNECTED;
25190Sstevel@tonic-gate 	} else {
25200Sstevel@tonic-gate 		new_state = SBD_STATE_EMPTY;
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	dip = sbp->sb_devlist[NIX(SBD_COMP_CPU)][unit];
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	cp = SBD_GET_BOARD_CPUUNIT(sbp, unit);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	cpuid = sbdp_get_cpuid(hdp, dip);
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	cp->sbc_cpu_id = cpuid;
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	if (&sbdp_cpu_get_impl)
25340Sstevel@tonic-gate 		cp->sbc_cpu_impl = sbdp_cpu_get_impl(hdp, dip);
25350Sstevel@tonic-gate 	else
25360Sstevel@tonic-gate 		cp->sbc_cpu_impl = -1;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
25390Sstevel@tonic-gate 	if ((cpuid >= 0) && cpu[cpuid])
25400Sstevel@tonic-gate 		cp->sbc_cpu_flags = cpu[cpuid]->cpu_flags;
25410Sstevel@tonic-gate 	else
25420Sstevel@tonic-gate 		cp->sbc_cpu_flags = CPU_OFFLINE | CPU_POWEROFF;
25430Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	sbd_cpu_set_prop(cp, dip);
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	cp->sbc_cm.sbdev_cond = sbd_get_comp_cond(dip);
25480Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 	/*
25510Sstevel@tonic-gate 	 * Any changes to the cpu should be performed above
25520Sstevel@tonic-gate 	 * this call to ensure the cpu is fully initialized
25530Sstevel@tonic-gate 	 * before transitioning to the new state.
25540Sstevel@tonic-gate 	 */
25550Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_CPU, unit, new_state);
25560Sstevel@tonic-gate }
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate /*
25590Sstevel@tonic-gate  * Only do work if called to operate on an entire board
25600Sstevel@tonic-gate  * which doesn't already have components present.
25610Sstevel@tonic-gate  */
25620Sstevel@tonic-gate static void
sbd_connect(sbd_handle_t * hp)25630Sstevel@tonic-gate sbd_connect(sbd_handle_t *hp)
25640Sstevel@tonic-gate {
25650Sstevel@tonic-gate 	sbd_board_t	*sbp;
25660Sstevel@tonic-gate 	sbderror_t	*ep;
25670Sstevel@tonic-gate 	static fn_t	f = "sbd_connect";
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	PR_ALL("%s board %d\n", f, sbp->sb_num);
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	ep = HD2MACHERR(hp);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	if (SBD_DEVS_PRESENT(sbp)) {
25760Sstevel@tonic-gate 		/*
25770Sstevel@tonic-gate 		 * Board already has devices present.
25780Sstevel@tonic-gate 		 */
25790Sstevel@tonic-gate 		PR_ALL("%s: devices already present (0x%x)\n",
25800Sstevel@tonic-gate 			f, SBD_DEVS_PRESENT(sbp));
25810Sstevel@tonic-gate 		SBD_SET_ERRNO(ep, EINVAL);
25820Sstevel@tonic-gate 		return;
25830Sstevel@tonic-gate 	}
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	if (sbd_init_devlists(sbp) == 0) {
25860Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: no devices present on board %d",
25870Sstevel@tonic-gate 			f, sbp->sb_num);
25880Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NODEV);
25890Sstevel@tonic-gate 		return;
25900Sstevel@tonic-gate 	} else {
25910Sstevel@tonic-gate 		int	i;
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 		/*
25940Sstevel@tonic-gate 		 * Initialize mem-unit section of board structure.
25950Sstevel@tonic-gate 		 */
25960Sstevel@tonic-gate 		for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++)
25970Sstevel@tonic-gate 			if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_MEM, i))
25980Sstevel@tonic-gate 				sbd_init_mem_unit(sbp, i, SBD_HD2ERR(hp));
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 		/*
26010Sstevel@tonic-gate 		 * Initialize sb_io sections.
26020Sstevel@tonic-gate 		 */
26030Sstevel@tonic-gate 		for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++)
26040Sstevel@tonic-gate 			if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_IO, i))
26050Sstevel@tonic-gate 				sbd_init_io_unit(sbp, i);
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 		SBD_BOARD_TRANSITION(sbp, SBD_STATE_CONNECTED);
26080Sstevel@tonic-gate 		sbp->sb_rstate = SBD_STAT_CONNECTED;
26090Sstevel@tonic-gate 		sbp->sb_ostate = SBD_STAT_UNCONFIGURED;
26100Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&sbp->sb_time);
26110Sstevel@tonic-gate 		SBD_INJECT_ERR(SBD_CONNECT_BOARD_PSEUDO_ERR, hp->h_err, EIO,
26120Sstevel@tonic-gate 			ESBD_INTERNAL, NULL);
26130Sstevel@tonic-gate 	}
26140Sstevel@tonic-gate }
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate static int
sbd_disconnect(sbd_handle_t * hp)26170Sstevel@tonic-gate sbd_disconnect(sbd_handle_t *hp)
26180Sstevel@tonic-gate {
26190Sstevel@tonic-gate 	int		i;
26200Sstevel@tonic-gate 	sbd_devset_t	devset;
26210Sstevel@tonic-gate 	sbd_board_t	*sbp;
26220Sstevel@tonic-gate 	static fn_t	f = "sbd_disconnect it";
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	PR_ALL("%s ...\n", f);
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	/*
26290Sstevel@tonic-gate 	 * Only devices which are present, but
26300Sstevel@tonic-gate 	 * unattached can be disconnected.
26310Sstevel@tonic-gate 	 */
26320Sstevel@tonic-gate 	devset = HD2MACHHD(hp)->sh_devset & SBD_DEVS_PRESENT(sbp) &
26330Sstevel@tonic-gate 			SBD_DEVS_UNATTACHED(sbp);
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	ASSERT((SBD_DEVS_ATTACHED(sbp) & devset) == 0);
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	/*
26380Sstevel@tonic-gate 	 * Update per-device state transitions.
26390Sstevel@tonic-gate 	 */
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++)
26420Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, i)) {
26430Sstevel@tonic-gate 			if (sbd_disconnect_mem(hp, i) == 0) {
26440Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, i,
26450Sstevel@tonic-gate 							SBD_STATE_EMPTY);
26460Sstevel@tonic-gate 				SBD_DEV_CLR_PRESENT(sbp, SBD_COMP_MEM, i);
26470Sstevel@tonic-gate 			}
26480Sstevel@tonic-gate 		}
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++)
26510Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CPU, i)) {
26520Sstevel@tonic-gate 			if (sbd_disconnect_cpu(hp, i) == 0) {
26530Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_CPU, i,
26540Sstevel@tonic-gate 							SBD_STATE_EMPTY);
26550Sstevel@tonic-gate 				SBD_DEV_CLR_PRESENT(sbp, SBD_COMP_CPU, i);
26560Sstevel@tonic-gate 			}
26570Sstevel@tonic-gate 		}
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++)
26600Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, i)) {
26610Sstevel@tonic-gate 			if (sbd_disconnect_io(hp, i) == 0) {
26620Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO, i,
26630Sstevel@tonic-gate 							SBD_STATE_EMPTY);
26640Sstevel@tonic-gate 				SBD_DEV_CLR_PRESENT(sbp, SBD_COMP_IO, i);
26650Sstevel@tonic-gate 			}
26660Sstevel@tonic-gate 		}
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	/*
26690Sstevel@tonic-gate 	 * Once all the components on a board have been disconnect
26700Sstevel@tonic-gate 	 * the board's state can transition to disconnected and
26710Sstevel@tonic-gate 	 * we can allow the deprobe to take place.
26720Sstevel@tonic-gate 	 */
26730Sstevel@tonic-gate 	if (SBD_DEVS_PRESENT(sbp) == 0) {
26740Sstevel@tonic-gate 		SBD_BOARD_TRANSITION(sbp, SBD_STATE_OCCUPIED);
26750Sstevel@tonic-gate 		sbp->sb_rstate = SBD_STAT_DISCONNECTED;
26760Sstevel@tonic-gate 		sbp->sb_ostate = SBD_STAT_UNCONFIGURED;
26770Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&sbp->sb_time);
26780Sstevel@tonic-gate 		SBD_INJECT_ERR(SBD_DISCONNECT_BOARD_PSEUDO_ERR, hp->h_err, EIO,
26790Sstevel@tonic-gate 			ESBD_INTERNAL, NULL);
26800Sstevel@tonic-gate 		return (0);
26810Sstevel@tonic-gate 	} else {
26820Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: could not disconnect devices on board %d",
26830Sstevel@tonic-gate 			f, sbp->sb_num);
26840Sstevel@tonic-gate 		return (-1);
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate }
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate static void
sbd_test_board(sbd_handle_t * hp)26890Sstevel@tonic-gate sbd_test_board(sbd_handle_t *hp)
26900Sstevel@tonic-gate {
26910Sstevel@tonic-gate 	sbd_board_t	*sbp;
26920Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 	PR_ALL("sbd_test_board: board %d\n", sbp->sb_num);
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	if (sbdp_test_board(hdp, &hp->h_opts) != 0) {
27020Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
27050Sstevel@tonic-gate 	}
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_TEST_BOARD_PSEUDO_ERR, hp->h_err, EIO,
27080Sstevel@tonic-gate 		ESBD_INTERNAL, NULL);
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
27110Sstevel@tonic-gate }
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate static void
sbd_assign_board(sbd_handle_t * hp)27140Sstevel@tonic-gate sbd_assign_board(sbd_handle_t *hp)
27150Sstevel@tonic-gate {
27160Sstevel@tonic-gate 	sbd_board_t	*sbp;
27170Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 	PR_ALL("sbd_assign_board: board %d\n", sbp->sb_num);
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	if (sbdp_assign_board(hdp) != 0) {
27260Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
27290Sstevel@tonic-gate 	}
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_ASSIGN_BOARD_PSEUDO_ERR, hp->h_err, EIO,
27320Sstevel@tonic-gate 		ESBD_INTERNAL, NULL);
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
27350Sstevel@tonic-gate }
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate static void
sbd_unassign_board(sbd_handle_t * hp)27380Sstevel@tonic-gate sbd_unassign_board(sbd_handle_t *hp)
27390Sstevel@tonic-gate {
27400Sstevel@tonic-gate 	sbd_board_t	*sbp;
27410Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	PR_ALL("sbd_unassign_board: board %d\n", sbp->sb_num);
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	if (sbdp_unassign_board(hdp) != 0) {
27500Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
27530Sstevel@tonic-gate 	}
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_ASSIGN_BOARD_PSEUDO_ERR, hp->h_err, EIO,
27560Sstevel@tonic-gate 		ESBD_INTERNAL, NULL);
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
27590Sstevel@tonic-gate }
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate static void
sbd_poweron_board(sbd_handle_t * hp)27620Sstevel@tonic-gate sbd_poweron_board(sbd_handle_t *hp)
27630Sstevel@tonic-gate {
27640Sstevel@tonic-gate 	sbd_board_t	*sbp;
27650Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	PR_ALL("sbd_poweron_board: %d\n", sbp->sb_num);
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	if (sbdp_poweron_board(hdp) != 0) {
27740Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
27770Sstevel@tonic-gate 	}
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_POWERON_BOARD_PSEUDO_ERR, hp->h_err, EIO,
27800Sstevel@tonic-gate 		ESBD_INTERNAL, NULL);
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
27830Sstevel@tonic-gate }
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate static void
sbd_poweroff_board(sbd_handle_t * hp)27860Sstevel@tonic-gate sbd_poweroff_board(sbd_handle_t *hp)
27870Sstevel@tonic-gate {
27880Sstevel@tonic-gate 	sbd_board_t	*sbp;
27890Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	PR_ALL("sbd_poweroff_board: %d\n", sbp->sb_num);
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	if (sbdp_poweroff_board(hdp) != 0) {
27980Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
28010Sstevel@tonic-gate 	}
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_POWEROFF_BOARD_PSEUDO_ERR, hp->h_err, EIO,
28040Sstevel@tonic-gate 		ESBD_INTERNAL, NULL);
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
28070Sstevel@tonic-gate }
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate /*
28110Sstevel@tonic-gate  * Return a list of the dip's of devices that are
28120Sstevel@tonic-gate  * either present and attached, or present only but
28130Sstevel@tonic-gate  * not yet attached for the given board.
28140Sstevel@tonic-gate  */
28150Sstevel@tonic-gate sbd_devlist_t *
sbd_get_devlist(sbd_handle_t * hp,sbd_board_t * sbp,sbd_comp_type_t nodetype,int max_units,uint_t uset,int * count,int present_only)28160Sstevel@tonic-gate sbd_get_devlist(sbd_handle_t *hp, sbd_board_t *sbp, sbd_comp_type_t nodetype,
28170Sstevel@tonic-gate 		int max_units, uint_t uset, int *count, int present_only)
28180Sstevel@tonic-gate {
28190Sstevel@tonic-gate 	int		i, ix;
28200Sstevel@tonic-gate 	sbd_devlist_t	*ret_devlist;
28210Sstevel@tonic-gate 	dev_info_t	**devlist;
28220Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	*count = 0;
28250Sstevel@tonic-gate 	ret_devlist = GETSTRUCT(sbd_devlist_t, max_units);
28260Sstevel@tonic-gate 	devlist = sbp->sb_devlist[NIX(nodetype)];
28270Sstevel@tonic-gate 	/*
28280Sstevel@tonic-gate 	 * Turn into binary value since we're going
28290Sstevel@tonic-gate 	 * to be using XOR for a comparison.
28300Sstevel@tonic-gate 	 * if (present_only) then
28310Sstevel@tonic-gate 	 *	dev must be PRESENT, but NOT ATTACHED.
28320Sstevel@tonic-gate 	 * else
28330Sstevel@tonic-gate 	 *	dev must be PRESENT AND ATTACHED.
28340Sstevel@tonic-gate 	 * endif
28350Sstevel@tonic-gate 	 */
28360Sstevel@tonic-gate 	if (present_only)
28370Sstevel@tonic-gate 		present_only = 1;
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	for (i = ix = 0; (i < max_units) && uset; i++) {
28420Sstevel@tonic-gate 		int	ut, is_present, is_attached;
28430Sstevel@tonic-gate 		dev_info_t *dip;
28440Sstevel@tonic-gate 		sbderror_t *ep = SBD_HD2ERR(hp);
28450Sstevel@tonic-gate 		int	nunits, distance, j;
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 		/*
28480Sstevel@tonic-gate 		 * For CMPs, we would like to perform DR operation on
28490Sstevel@tonic-gate 		 * all the cores before moving onto the next chip.
28500Sstevel@tonic-gate 		 * Therefore, when constructing the devlist, we process
28510Sstevel@tonic-gate 		 * all the cores together.
28520Sstevel@tonic-gate 		 */
28530Sstevel@tonic-gate 		if (nodetype == SBD_COMP_CPU) {
28540Sstevel@tonic-gate 			/*
28550Sstevel@tonic-gate 			 * Number of units to process in the inner loop
28560Sstevel@tonic-gate 			 */
28570Sstevel@tonic-gate 			nunits = MAX_CORES_PER_CMP;
28580Sstevel@tonic-gate 			/*
28590Sstevel@tonic-gate 			 * The distance between the units in the
28600Sstevel@tonic-gate 			 * board's sb_devlist structure.
28610Sstevel@tonic-gate 			 */
28620Sstevel@tonic-gate 			distance = MAX_CMP_UNITS_PER_BOARD;
28630Sstevel@tonic-gate 		} else {
28640Sstevel@tonic-gate 			nunits = 1;
28650Sstevel@tonic-gate 			distance = 0;
28660Sstevel@tonic-gate 		}
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 		for (j = 0; j < nunits; j++) {
28690Sstevel@tonic-gate 			if ((dip = devlist[i + j * distance]) == NULL)
28700Sstevel@tonic-gate 				continue;
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate 			ut = sbdp_get_unit_num(hdp, dip);
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 			if (ut == -1) {
28750Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
28760Sstevel@tonic-gate 				PR_ALL("sbd_get_devlist bad unit %d"
28770Sstevel@tonic-gate 				    " code %d errno %d",
28780Sstevel@tonic-gate 				    i, ep->e_code, ep->e_errno);
28790Sstevel@tonic-gate 			}
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 			if ((uset & (1 << ut)) == 0)
28820Sstevel@tonic-gate 				continue;
28830Sstevel@tonic-gate 			uset &= ~(1 << ut);
28840Sstevel@tonic-gate 			is_present = SBD_DEV_IS_PRESENT(sbp, nodetype, ut) ?
28850Sstevel@tonic-gate 			    1 : 0;
28860Sstevel@tonic-gate 			is_attached = SBD_DEV_IS_ATTACHED(sbp, nodetype, ut) ?
28870Sstevel@tonic-gate 			    1 : 0;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 			if (is_present && (present_only ^ is_attached)) {
28900Sstevel@tonic-gate 				ret_devlist[ix].dv_dip = dip;
28910Sstevel@tonic-gate 				sbd_init_err(&ret_devlist[ix].dv_error);
28920Sstevel@tonic-gate 				ix++;
28930Sstevel@tonic-gate 			}
28940Sstevel@tonic-gate 		}
28950Sstevel@tonic-gate 	}
28960Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate 	if ((*count = ix) == 0) {
28990Sstevel@tonic-gate 		FREESTRUCT(ret_devlist, sbd_devlist_t, max_units);
29000Sstevel@tonic-gate 		ret_devlist = NULL;
29010Sstevel@tonic-gate 	}
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 	return (ret_devlist);
29040Sstevel@tonic-gate }
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate static sbd_devlist_t *
sbd_get_attach_devlist(sbd_handle_t * hp,int32_t * devnump,int32_t pass)29070Sstevel@tonic-gate sbd_get_attach_devlist(sbd_handle_t *hp, int32_t *devnump, int32_t pass)
29080Sstevel@tonic-gate {
29090Sstevel@tonic-gate 	sbd_board_t	*sbp;
29100Sstevel@tonic-gate 	uint_t		uset;
29110Sstevel@tonic-gate 	sbd_devset_t	devset;
29120Sstevel@tonic-gate 	sbd_devlist_t	*attach_devlist;
29130Sstevel@tonic-gate 	static int	next_pass = 1;
29140Sstevel@tonic-gate 	static fn_t	f = "sbd_get_attach_devlist";
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	PR_ALL("%s (pass = %d)...\n", f, pass);
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
29190Sstevel@tonic-gate 	devset = HD2MACHHD(hp)->sh_devset;
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate 	*devnump = 0;
29220Sstevel@tonic-gate 	attach_devlist = NULL;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 	/*
29250Sstevel@tonic-gate 	 * We switch on next_pass for the cases where a board
29260Sstevel@tonic-gate 	 * does not contain a particular type of component.
29270Sstevel@tonic-gate 	 * In these situations we don't want to return NULL
29280Sstevel@tonic-gate 	 * prematurely.  We need to check other devices and
29290Sstevel@tonic-gate 	 * we don't want to check the same type multiple times.
29300Sstevel@tonic-gate 	 * For example, if there were no cpus, then on pass 1
29310Sstevel@tonic-gate 	 * we would drop through and return the memory nodes.
29320Sstevel@tonic-gate 	 * However, on pass 2 we would switch back to the memory
29330Sstevel@tonic-gate 	 * nodes thereby returning them twice!  Using next_pass
29340Sstevel@tonic-gate 	 * forces us down to the end (or next item).
29350Sstevel@tonic-gate 	 */
29360Sstevel@tonic-gate 	if (pass == 1)
29370Sstevel@tonic-gate 		next_pass = 1;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	switch (next_pass) {
29400Sstevel@tonic-gate 	case 1:
29410Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
29420Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_CPU);
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 			attach_devlist = sbd_get_devlist(hp, sbp, SBD_COMP_CPU,
29450Sstevel@tonic-gate 						MAX_CPU_UNITS_PER_BOARD,
29460Sstevel@tonic-gate 						uset, devnump, 1);
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_CPU, DEVSET_ANYUNIT);
29490Sstevel@tonic-gate 			if (!devset || attach_devlist) {
29500Sstevel@tonic-gate 				next_pass = 2;
29510Sstevel@tonic-gate 				return (attach_devlist);
29520Sstevel@tonic-gate 			}
29530Sstevel@tonic-gate 			/*
29540Sstevel@tonic-gate 			 * If the caller is interested in the entire
29550Sstevel@tonic-gate 			 * board, but there aren't any cpus, then just
29560Sstevel@tonic-gate 			 * fall through to check for the next component.
29570Sstevel@tonic-gate 			 */
29580Sstevel@tonic-gate 		}
29590Sstevel@tonic-gate 		/*FALLTHROUGH*/
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	case 2:
29620Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
29630Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_MEM);
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 			attach_devlist = sbd_get_devlist(hp, sbp, SBD_COMP_MEM,
29660Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD,
29670Sstevel@tonic-gate 						uset, devnump, 1);
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
29700Sstevel@tonic-gate 			if (!devset || attach_devlist) {
29710Sstevel@tonic-gate 				next_pass = 3;
29720Sstevel@tonic-gate 				return (attach_devlist);
29730Sstevel@tonic-gate 			}
29740Sstevel@tonic-gate 			/*
29750Sstevel@tonic-gate 			 * If the caller is interested in the entire
29760Sstevel@tonic-gate 			 * board, but there isn't any memory, then
29770Sstevel@tonic-gate 			 * just fall through to next component.
29780Sstevel@tonic-gate 			 */
29790Sstevel@tonic-gate 		}
29800Sstevel@tonic-gate 		/*FALLTHROUGH*/
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	case 3:
29840Sstevel@tonic-gate 		next_pass = -1;
29850Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
29860Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_IO);
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 			attach_devlist = sbd_get_devlist(hp, sbp, SBD_COMP_IO,
29890Sstevel@tonic-gate 						MAX_IO_UNITS_PER_BOARD,
29900Sstevel@tonic-gate 						uset, devnump, 1);
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_IO, DEVSET_ANYUNIT);
29930Sstevel@tonic-gate 			if (!devset || attach_devlist) {
29940Sstevel@tonic-gate 				next_pass = 4;
29950Sstevel@tonic-gate 				return (attach_devlist);
29960Sstevel@tonic-gate 			}
29970Sstevel@tonic-gate 		}
29980Sstevel@tonic-gate 		/*FALLTHROUGH*/
29990Sstevel@tonic-gate 
30000Sstevel@tonic-gate 	default:
30010Sstevel@tonic-gate 		*devnump = 0;
30020Sstevel@tonic-gate 		return (NULL);
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 	/*NOTREACHED*/
30050Sstevel@tonic-gate }
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate static int
sbd_pre_attach_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)30080Sstevel@tonic-gate sbd_pre_attach_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
30090Sstevel@tonic-gate 	int32_t devnum)
30100Sstevel@tonic-gate {
30110Sstevel@tonic-gate 	int		max_units = 0, rv = 0;
30120Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
30130Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_attach_devlist";
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	/*
30160Sstevel@tonic-gate 	 * In this driver, all entries in a devlist[] are
30170Sstevel@tonic-gate 	 * of the same nodetype.
30180Sstevel@tonic-gate 	 */
30190Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
30220Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	switch (nodetype) {
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate 	case SBD_COMP_MEM:
30270Sstevel@tonic-gate 		max_units = MAX_MEM_UNITS_PER_BOARD;
30280Sstevel@tonic-gate 		rv = sbd_pre_attach_mem(hp, devlist, devnum);
30290Sstevel@tonic-gate 		break;
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 	case SBD_COMP_CPU:
30320Sstevel@tonic-gate 		max_units = MAX_CPU_UNITS_PER_BOARD;
30330Sstevel@tonic-gate 		rv = sbd_pre_attach_cpu(hp, devlist, devnum);
30340Sstevel@tonic-gate 		break;
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	case SBD_COMP_IO:
30370Sstevel@tonic-gate 		max_units = MAX_IO_UNITS_PER_BOARD;
30380Sstevel@tonic-gate 		break;
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	default:
30410Sstevel@tonic-gate 		rv = -1;
30420Sstevel@tonic-gate 		break;
30430Sstevel@tonic-gate 	}
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	if (rv && max_units) {
30460Sstevel@tonic-gate 		int	i;
30470Sstevel@tonic-gate 		/*
30480Sstevel@tonic-gate 		 * Need to clean up devlist
30490Sstevel@tonic-gate 		 * if pre-op is going to fail.
30500Sstevel@tonic-gate 		 */
30510Sstevel@tonic-gate 		for (i = 0; i < max_units; i++) {
30520Sstevel@tonic-gate 			if (SBD_GET_ERRSTR(&devlist[i].dv_error)) {
30530Sstevel@tonic-gate 				SBD_FREE_ERR(&devlist[i].dv_error);
30540Sstevel@tonic-gate 			} else {
30550Sstevel@tonic-gate 				break;
30560Sstevel@tonic-gate 			}
30570Sstevel@tonic-gate 		}
30580Sstevel@tonic-gate 		FREESTRUCT(devlist, sbd_devlist_t, max_units);
30590Sstevel@tonic-gate 	}
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	/*
30620Sstevel@tonic-gate 	 * If an error occurred, return "continue"
30630Sstevel@tonic-gate 	 * indication so that we can continue attaching
30640Sstevel@tonic-gate 	 * as much as possible.
30650Sstevel@tonic-gate 	 */
30660Sstevel@tonic-gate 	return (rv ? -1 : 0);
30670Sstevel@tonic-gate }
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate static int
sbd_post_attach_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)30700Sstevel@tonic-gate sbd_post_attach_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
30710Sstevel@tonic-gate 			int32_t devnum)
30720Sstevel@tonic-gate {
30730Sstevel@tonic-gate 	int		i, max_units = 0, rv = 0;
30740Sstevel@tonic-gate 	sbd_devset_t	devs_unattached, devs_present;
30750Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
30760Sstevel@tonic-gate 	sbd_board_t 	*sbp = SBDH2BD(hp->h_sbd);
30770Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
30780Sstevel@tonic-gate 	static fn_t	f = "sbd_post_attach_devlist";
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
30810Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
30840Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
30850Sstevel@tonic-gate 
30860Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 	/*
30890Sstevel@tonic-gate 	 * Need to free up devlist[] created earlier in
30900Sstevel@tonic-gate 	 * sbd_get_attach_devlist().
30910Sstevel@tonic-gate 	 */
30920Sstevel@tonic-gate 	switch (nodetype) {
30930Sstevel@tonic-gate 	case SBD_COMP_CPU:
30940Sstevel@tonic-gate 		max_units = MAX_CPU_UNITS_PER_BOARD;
30950Sstevel@tonic-gate 		rv = sbd_post_attach_cpu(hp, devlist, devnum);
30960Sstevel@tonic-gate 		break;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	case SBD_COMP_MEM:
31000Sstevel@tonic-gate 		max_units = MAX_MEM_UNITS_PER_BOARD;
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 		rv = sbd_post_attach_mem(hp, devlist, devnum);
31030Sstevel@tonic-gate 		break;
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate 	case SBD_COMP_IO:
31060Sstevel@tonic-gate 		max_units = MAX_IO_UNITS_PER_BOARD;
31070Sstevel@tonic-gate 		break;
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	default:
31100Sstevel@tonic-gate 		rv = -1;
31110Sstevel@tonic-gate 		break;
31120Sstevel@tonic-gate 	}
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
31160Sstevel@tonic-gate 		int		unit;
31170Sstevel@tonic-gate 		dev_info_t	*dip;
31180Sstevel@tonic-gate 		sbderror_t	*ep;
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 		ep = &devlist[i].dv_error;
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 		if (sbd_set_err_in_hdl(hp, ep) == 0)
31230Sstevel@tonic-gate 			continue;
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 		dip = devlist[i].dv_dip;
31260Sstevel@tonic-gate 		nodetype = sbd_get_devtype(hp, dip);
31270Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate 		if (unit == -1) {
31300Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, ep);
31310Sstevel@tonic-gate 			continue;
31320Sstevel@tonic-gate 		}
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 		unit = sbd_check_unit_attached(sbp, dip, unit, nodetype, ep);
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 		if (unit == -1) {
31370Sstevel@tonic-gate 			PR_ALL("%s: ERROR (nt=%s, b=%d, u=%d) not attached\n",
31380Sstevel@tonic-gate 				f, sbd_ct_str[(int)nodetype], sbp->sb_num, i);
31390Sstevel@tonic-gate 			continue;
31400Sstevel@tonic-gate 		}
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 		SBD_DEV_SET_ATTACHED(sbp, nodetype, unit);
31430Sstevel@tonic-gate 		SBD_DEVICE_TRANSITION(sbp, nodetype, unit,
31440Sstevel@tonic-gate 						SBD_STATE_CONFIGURED);
31450Sstevel@tonic-gate 	}
31460Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 	if (rv) {
31490Sstevel@tonic-gate 		PR_ALL("%s: errno %d, ecode %d during attach\n",
31500Sstevel@tonic-gate 			f, SBD_GET_ERRNO(SBD_HD2ERR(hp)),
31510Sstevel@tonic-gate 			SBD_GET_ERR(HD2MACHERR(hp)));
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	devs_present = SBD_DEVS_PRESENT(sbp);
31550Sstevel@tonic-gate 	devs_unattached = SBD_DEVS_UNATTACHED(sbp);
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	switch (SBD_BOARD_STATE(sbp)) {
31580Sstevel@tonic-gate 	case SBD_STATE_CONNECTED:
31590Sstevel@tonic-gate 	case SBD_STATE_UNCONFIGURED:
31600Sstevel@tonic-gate 		ASSERT(devs_present);
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 		if (devs_unattached == 0) {
31630Sstevel@tonic-gate 			/*
31640Sstevel@tonic-gate 			 * All devices finally attached.
31650Sstevel@tonic-gate 			 */
31660Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_CONFIGURED);
31670Sstevel@tonic-gate 			sbp->sb_rstate = SBD_STAT_CONNECTED;
31680Sstevel@tonic-gate 			sbp->sb_ostate = SBD_STAT_CONFIGURED;
31690Sstevel@tonic-gate 		} else if (devs_present != devs_unattached) {
31700Sstevel@tonic-gate 			/*
31710Sstevel@tonic-gate 			 * Only some devices are fully attached.
31720Sstevel@tonic-gate 			 */
31730Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_PARTIAL);
31740Sstevel@tonic-gate 			sbp->sb_rstate = SBD_STAT_CONNECTED;
31750Sstevel@tonic-gate 			sbp->sb_ostate = SBD_STAT_UNCONFIGURED;
31760Sstevel@tonic-gate 		}
31770Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&sbp->sb_time);
31780Sstevel@tonic-gate 		break;
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	case SBD_STATE_PARTIAL:
31810Sstevel@tonic-gate 		ASSERT(devs_present);
31820Sstevel@tonic-gate 		/*
31830Sstevel@tonic-gate 		 * All devices finally attached.
31840Sstevel@tonic-gate 		 */
31850Sstevel@tonic-gate 		if (devs_unattached == 0) {
31860Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_CONFIGURED);
31870Sstevel@tonic-gate 			sbp->sb_rstate = SBD_STAT_CONNECTED;
31880Sstevel@tonic-gate 			sbp->sb_ostate = SBD_STAT_CONFIGURED;
31890Sstevel@tonic-gate 			(void) drv_getparm(TIME, (void *)&sbp->sb_time);
31900Sstevel@tonic-gate 		}
31910Sstevel@tonic-gate 		break;
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate 	default:
31940Sstevel@tonic-gate 		break;
31950Sstevel@tonic-gate 	}
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	if (max_units && devlist) {
31980Sstevel@tonic-gate 		int	i;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 		for (i = 0; i < max_units; i++) {
32010Sstevel@tonic-gate 			if (SBD_GET_ERRSTR(&devlist[i].dv_error)) {
32020Sstevel@tonic-gate 				SBD_FREE_ERR(&devlist[i].dv_error);
32030Sstevel@tonic-gate 			} else {
32040Sstevel@tonic-gate 				break;
32050Sstevel@tonic-gate 			}
32060Sstevel@tonic-gate 		}
32070Sstevel@tonic-gate 		FREESTRUCT(devlist, sbd_devlist_t, max_units);
32080Sstevel@tonic-gate 	}
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	/*
32110Sstevel@tonic-gate 	 * Our policy is to attach all components that are
32120Sstevel@tonic-gate 	 * possible, thus we always return "success" on the
32130Sstevel@tonic-gate 	 * pre and post operations.
32140Sstevel@tonic-gate 	 */
32150Sstevel@tonic-gate 	return (0);
32160Sstevel@tonic-gate }
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate /*
32190Sstevel@tonic-gate  * We only need to "release" cpu and memory devices.
32200Sstevel@tonic-gate  */
32210Sstevel@tonic-gate static sbd_devlist_t *
sbd_get_release_devlist(sbd_handle_t * hp,int32_t * devnump,int32_t pass)32220Sstevel@tonic-gate sbd_get_release_devlist(sbd_handle_t *hp, int32_t *devnump, int32_t pass)
32230Sstevel@tonic-gate {
32240Sstevel@tonic-gate 	sbd_board_t	*sbp;
32250Sstevel@tonic-gate 	uint_t		uset;
32260Sstevel@tonic-gate 	sbd_devset_t	devset;
32270Sstevel@tonic-gate 	sbd_devlist_t	*release_devlist;
32280Sstevel@tonic-gate 	static int	next_pass = 1;
32290Sstevel@tonic-gate 	static fn_t	f = "sbd_get_release_devlist";
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	PR_ALL("%s (pass = %d)...\n", f, pass);
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
32340Sstevel@tonic-gate 	devset = HD2MACHHD(hp)->sh_devset;
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate 	*devnump = 0;
32370Sstevel@tonic-gate 	release_devlist = NULL;
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 	/*
32400Sstevel@tonic-gate 	 * We switch on next_pass for the cases where a board
32410Sstevel@tonic-gate 	 * does not contain a particular type of component.
32420Sstevel@tonic-gate 	 * In these situations we don't want to return NULL
32430Sstevel@tonic-gate 	 * prematurely.  We need to check other devices and
32440Sstevel@tonic-gate 	 * we don't want to check the same type multiple times.
32450Sstevel@tonic-gate 	 * For example, if there were no cpus, then on pass 1
32460Sstevel@tonic-gate 	 * we would drop through and return the memory nodes.
32470Sstevel@tonic-gate 	 * However, on pass 2 we would switch back to the memory
32480Sstevel@tonic-gate 	 * nodes thereby returning them twice!  Using next_pass
32490Sstevel@tonic-gate 	 * forces us down to the end (or next item).
32500Sstevel@tonic-gate 	 */
32510Sstevel@tonic-gate 	if (pass == 1)
32520Sstevel@tonic-gate 		next_pass = 1;
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 	switch (next_pass) {
32550Sstevel@tonic-gate 	case 1:
32560Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
32570Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_MEM);
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 			release_devlist = sbd_get_devlist(hp, sbp,
32600Sstevel@tonic-gate 						SBD_COMP_MEM,
32610Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD,
32620Sstevel@tonic-gate 						uset, devnump, 0);
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
32650Sstevel@tonic-gate 			if (!devset || release_devlist) {
32660Sstevel@tonic-gate 				next_pass = 2;
32670Sstevel@tonic-gate 				return (release_devlist);
32680Sstevel@tonic-gate 			}
32690Sstevel@tonic-gate 			/*
32700Sstevel@tonic-gate 			 * If the caller is interested in the entire
32710Sstevel@tonic-gate 			 * board, but there isn't any memory, then
32720Sstevel@tonic-gate 			 * just fall through to next component.
32730Sstevel@tonic-gate 			 */
32740Sstevel@tonic-gate 		}
32750Sstevel@tonic-gate 		/*FALLTHROUGH*/
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	case 2:
32790Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
32800Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_CPU);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 			release_devlist = sbd_get_devlist(hp, sbp,
32830Sstevel@tonic-gate 						SBD_COMP_CPU,
32840Sstevel@tonic-gate 						MAX_CPU_UNITS_PER_BOARD,
32850Sstevel@tonic-gate 						uset, devnump, 0);
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_CPU, DEVSET_ANYUNIT);
32880Sstevel@tonic-gate 			if (!devset || release_devlist) {
32890Sstevel@tonic-gate 				next_pass = 3;
32900Sstevel@tonic-gate 				return (release_devlist);
32910Sstevel@tonic-gate 			}
32920Sstevel@tonic-gate 			/*
32930Sstevel@tonic-gate 			 * If the caller is interested in the entire
32940Sstevel@tonic-gate 			 * board, but there aren't any cpus, then just
32950Sstevel@tonic-gate 			 * fall through to check for the next component.
32960Sstevel@tonic-gate 			 */
32970Sstevel@tonic-gate 		}
32980Sstevel@tonic-gate 		/*FALLTHROUGH*/
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	case 3:
33020Sstevel@tonic-gate 		next_pass = -1;
33030Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
33040Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_IO);
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 			release_devlist = sbd_get_devlist(hp, sbp,
33070Sstevel@tonic-gate 						SBD_COMP_IO,
33080Sstevel@tonic-gate 						MAX_IO_UNITS_PER_BOARD,
33090Sstevel@tonic-gate 						uset, devnump, 0);
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_IO, DEVSET_ANYUNIT);
33120Sstevel@tonic-gate 			if (!devset || release_devlist) {
33130Sstevel@tonic-gate 				next_pass = 4;
33140Sstevel@tonic-gate 				return (release_devlist);
33150Sstevel@tonic-gate 			}
33160Sstevel@tonic-gate 		}
33170Sstevel@tonic-gate 		/*FALLTHROUGH*/
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 	default:
33200Sstevel@tonic-gate 		*devnump = 0;
33210Sstevel@tonic-gate 		return (NULL);
33220Sstevel@tonic-gate 	}
33230Sstevel@tonic-gate 	/*NOTREACHED*/
33240Sstevel@tonic-gate }
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate static int
sbd_pre_release_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)33270Sstevel@tonic-gate sbd_pre_release_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
33280Sstevel@tonic-gate 			int32_t devnum)
33290Sstevel@tonic-gate {
33300Sstevel@tonic-gate 	int		max_units = 0, rv = 0;
33310Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
33320Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_release_devlist";
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
33370Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	switch (nodetype) {
33400Sstevel@tonic-gate 	case SBD_COMP_CPU: {
33410Sstevel@tonic-gate 		int			i, mem_present = 0;
33420Sstevel@tonic-gate 		sbd_board_t		*sbp = SBDH2BD(hp->h_sbd);
33430Sstevel@tonic-gate 		sbd_devset_t		devset;
33440Sstevel@tonic-gate 		sbd_priv_handle_t	*shp = HD2MACHHD(hp);
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 		max_units = MAX_CPU_UNITS_PER_BOARD;
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 		devset = shp->sh_orig_devset;
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 		for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
33510Sstevel@tonic-gate 			/*
33520Sstevel@tonic-gate 			 * if client also requested to unconfigure memory
33530Sstevel@tonic-gate 			 * the we allow the operation. Therefore
33540Sstevel@tonic-gate 			 * we need to warranty that memory gets unconfig
33550Sstevel@tonic-gate 			 * before cpus
33560Sstevel@tonic-gate 			 */
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 			if (DEVSET_IN_SET(devset, SBD_COMP_MEM, i)) {
33590Sstevel@tonic-gate 				continue;
33600Sstevel@tonic-gate 			}
33610Sstevel@tonic-gate 			if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_MEM, i)) {
33620Sstevel@tonic-gate 				mem_present = 1;
33630Sstevel@tonic-gate 				break;
33640Sstevel@tonic-gate 			}
33650Sstevel@tonic-gate 		}
33660Sstevel@tonic-gate 		if (mem_present) {
33670Sstevel@tonic-gate 			sbderror_t	*ep = SBD_HD2ERR(hp);
33680Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_MEMONLINE);
33690Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_mempath[i]);
33700Sstevel@tonic-gate 			rv = -1;
33710Sstevel@tonic-gate 		} else {
33720Sstevel@tonic-gate 			rv = sbd_pre_release_cpu(hp, devlist, devnum);
33730Sstevel@tonic-gate 		}
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate 		break;
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 	}
33780Sstevel@tonic-gate 	case SBD_COMP_MEM:
33790Sstevel@tonic-gate 		max_units = MAX_MEM_UNITS_PER_BOARD;
33800Sstevel@tonic-gate 		rv = sbd_pre_release_mem(hp, devlist, devnum);
33810Sstevel@tonic-gate 		break;
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 
33840Sstevel@tonic-gate 	case SBD_COMP_IO:
33850Sstevel@tonic-gate 		max_units = MAX_IO_UNITS_PER_BOARD;
33860Sstevel@tonic-gate 		rv = sbd_pre_release_io(hp, devlist, devnum);
33870Sstevel@tonic-gate 		break;
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	default:
33900Sstevel@tonic-gate 		rv = -1;
33910Sstevel@tonic-gate 		break;
33920Sstevel@tonic-gate 	}
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	if (rv && max_units) {
33950Sstevel@tonic-gate 		int	i;
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 		/*
33980Sstevel@tonic-gate 		 * the individual pre_release component routines should
33990Sstevel@tonic-gate 		 * have set the error in the handle.  No need to set it
34000Sstevel@tonic-gate 		 * here
34010Sstevel@tonic-gate 		 *
34020Sstevel@tonic-gate 		 * Need to clean up dynamically allocated devlist
34030Sstevel@tonic-gate 		 * if pre-op is going to fail.
34040Sstevel@tonic-gate 		 */
34050Sstevel@tonic-gate 		for (i = 0; i < max_units; i++) {
34060Sstevel@tonic-gate 			if (SBD_GET_ERRSTR(&devlist[i].dv_error)) {
34070Sstevel@tonic-gate 				SBD_FREE_ERR(&devlist[i].dv_error);
34080Sstevel@tonic-gate 			} else {
34090Sstevel@tonic-gate 				break;
34100Sstevel@tonic-gate 			}
34110Sstevel@tonic-gate 		}
34120Sstevel@tonic-gate 		FREESTRUCT(devlist, sbd_devlist_t, max_units);
34130Sstevel@tonic-gate 	}
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 	return (rv ? -1 : 0);
34160Sstevel@tonic-gate }
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate static int
sbd_post_release_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)34190Sstevel@tonic-gate sbd_post_release_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
34200Sstevel@tonic-gate 			int32_t devnum)
34210Sstevel@tonic-gate {
34220Sstevel@tonic-gate 	int		i, max_units = 0;
34230Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
34240Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
34250Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
34260Sstevel@tonic-gate 	sbd_error_t	*spe;
34270Sstevel@tonic-gate 	static fn_t	f = "sbd_post_release_devlist";
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
34300Sstevel@tonic-gate 	ASSERT(nodetype >= SBD_COMP_CPU && nodetype <= SBD_COMP_IO);
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
34330Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	/*
34360Sstevel@tonic-gate 	 * Need to free up devlist[] created earlier in
34370Sstevel@tonic-gate 	 * sbd_get_release_devlist().
34380Sstevel@tonic-gate 	 */
34390Sstevel@tonic-gate 	switch (nodetype) {
34400Sstevel@tonic-gate 	case SBD_COMP_CPU:
34410Sstevel@tonic-gate 		max_units = MAX_CPU_UNITS_PER_BOARD;
34420Sstevel@tonic-gate 		break;
34430Sstevel@tonic-gate 
34440Sstevel@tonic-gate 	case SBD_COMP_MEM:
34450Sstevel@tonic-gate 		max_units = MAX_MEM_UNITS_PER_BOARD;
34460Sstevel@tonic-gate 		break;
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	case SBD_COMP_IO:
34490Sstevel@tonic-gate 		/*
34500Sstevel@tonic-gate 		 *  Need to check if specific I/O is referenced and
34510Sstevel@tonic-gate 		 *  fail post-op.
34520Sstevel@tonic-gate 		 */
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 		if (sbd_check_io_refs(hp, devlist, devnum) > 0) {
34550Sstevel@tonic-gate 				PR_IO("%s: error - I/O devices ref'd\n", f);
34560Sstevel@tonic-gate 		}
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 		max_units = MAX_IO_UNITS_PER_BOARD;
34590Sstevel@tonic-gate 		break;
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	default:
34620Sstevel@tonic-gate 		{
34630Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: invalid nodetype (%d)",
34640Sstevel@tonic-gate 				f, (int)nodetype);
34650Sstevel@tonic-gate 			SBD_SET_ERR(HD2MACHERR(hp), ESBD_INVAL);
34660Sstevel@tonic-gate 		}
34670Sstevel@tonic-gate 		break;
34680Sstevel@tonic-gate 	}
34690Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
34700Sstevel@tonic-gate 	spe = hdp->h_err;
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
34730Sstevel@tonic-gate 		int		unit;
34740Sstevel@tonic-gate 		sbderror_t	*ep;
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 		ep = &devlist[i].dv_error;
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 		if (sbd_set_err_in_hdl(hp, ep) == 0) {
34790Sstevel@tonic-gate 			continue;
34800Sstevel@tonic-gate 		}
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, devlist[i].dv_dip);
34830Sstevel@tonic-gate 		if (unit == -1) {
34840Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
34850Sstevel@tonic-gate 			PR_ALL("%s bad unit num: %d code %d",
34860Sstevel@tonic-gate 			    f, unit, spe->e_code);
34870Sstevel@tonic-gate 			continue;
34880Sstevel@tonic-gate 		}
34890Sstevel@tonic-gate 	}
34900Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 	if (SBD_GET_ERRNO(SBD_HD2ERR(hp))) {
34930Sstevel@tonic-gate 		PR_ALL("%s: errno %d, ecode %d during release\n",
34940Sstevel@tonic-gate 			f, SBD_GET_ERRNO(SBD_HD2ERR(hp)),
34950Sstevel@tonic-gate 			SBD_GET_ERR(SBD_HD2ERR(hp)));
34960Sstevel@tonic-gate 	}
34970Sstevel@tonic-gate 
34980Sstevel@tonic-gate 	if (max_units && devlist) {
34990Sstevel@tonic-gate 		int	i;
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 		for (i = 0; i < max_units; i++) {
35020Sstevel@tonic-gate 			if (SBD_GET_ERRSTR(&devlist[i].dv_error)) {
35030Sstevel@tonic-gate 				SBD_FREE_ERR(&devlist[i].dv_error);
35040Sstevel@tonic-gate 			} else {
35050Sstevel@tonic-gate 				break;
35060Sstevel@tonic-gate 			}
35070Sstevel@tonic-gate 		}
35080Sstevel@tonic-gate 		FREESTRUCT(devlist, sbd_devlist_t, max_units);
35090Sstevel@tonic-gate 	}
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 	return (SBD_GET_ERRNO(SBD_HD2ERR(hp)) ? -1 : 0);
35120Sstevel@tonic-gate }
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate static void
sbd_release_dev_done(sbd_board_t * sbp,sbd_comp_type_t nodetype,int unit)35150Sstevel@tonic-gate sbd_release_dev_done(sbd_board_t *sbp, sbd_comp_type_t nodetype, int unit)
35160Sstevel@tonic-gate {
35170Sstevel@tonic-gate 	SBD_DEV_SET_UNREFERENCED(sbp, nodetype, unit);
35180Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, nodetype, unit, SBD_STATE_UNREFERENCED);
35190Sstevel@tonic-gate }
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate static void
sbd_release_done(sbd_handle_t * hp,sbd_comp_type_t nodetype,dev_info_t * dip)35220Sstevel@tonic-gate sbd_release_done(sbd_handle_t *hp, sbd_comp_type_t nodetype, dev_info_t *dip)
35230Sstevel@tonic-gate {
35240Sstevel@tonic-gate 	int		unit;
35250Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
35260Sstevel@tonic-gate 	sbderror_t	*ep;
35270Sstevel@tonic-gate 	static fn_t	f = "sbd_release_done";
35280Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
35330Sstevel@tonic-gate 	ep = SBD_HD2ERR(hp);
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 	if ((unit = sbdp_get_unit_num(hdp, dip)) < 0) {
35360Sstevel@tonic-gate 		cmn_err(CE_WARN,
35370Sstevel@tonic-gate 			"sbd:%s: unable to get unit for dip (0x%p)",
35380Sstevel@tonic-gate 			f, (void *)dip);
35390Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
35400Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
35410Sstevel@tonic-gate 		return;
35420Sstevel@tonic-gate 	}
35430Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
35440Sstevel@tonic-gate 
35450Sstevel@tonic-gate 	/*
35460Sstevel@tonic-gate 	 * Transfer the device which just completed its release
35470Sstevel@tonic-gate 	 * to the UNREFERENCED state.
35480Sstevel@tonic-gate 	 */
35490Sstevel@tonic-gate 	switch (nodetype) {
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 	case SBD_COMP_MEM:
35520Sstevel@tonic-gate 		sbd_release_mem_done((void *)hp, unit);
35530Sstevel@tonic-gate 		break;
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	default:
35560Sstevel@tonic-gate 		sbd_release_dev_done(sbp, nodetype, unit);
35570Sstevel@tonic-gate 		break;
35580Sstevel@tonic-gate 	}
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	/*
35610Sstevel@tonic-gate 	 * If the entire board was released and all components
35620Sstevel@tonic-gate 	 * unreferenced then transfer it to the UNREFERENCED state.
35630Sstevel@tonic-gate 	 */
35640Sstevel@tonic-gate 	if (SBD_DEVS_RELEASED(sbp) == SBD_DEVS_UNREFERENCED(sbp)) {
35650Sstevel@tonic-gate 		SBD_BOARD_TRANSITION(sbp, SBD_STATE_UNREFERENCED);
35660Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&sbp->sb_time);
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate }
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate static sbd_devlist_t *
sbd_get_detach_devlist(sbd_handle_t * hp,int32_t * devnump,int32_t pass)35710Sstevel@tonic-gate sbd_get_detach_devlist(sbd_handle_t *hp, int32_t *devnump, int32_t pass)
35720Sstevel@tonic-gate {
35730Sstevel@tonic-gate 	sbd_board_t	*sbp;
35740Sstevel@tonic-gate 	uint_t		uset;
35750Sstevel@tonic-gate 	sbd_devset_t	devset;
35760Sstevel@tonic-gate 	sbd_devlist_t	*detach_devlist;
35770Sstevel@tonic-gate 	static int	next_pass = 1;
35780Sstevel@tonic-gate 	static fn_t	f = "sbd_get_detach_devlist";
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 	PR_ALL("%s (pass = %d)...\n", f, pass);
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
35830Sstevel@tonic-gate 	devset = HD2MACHHD(hp)->sh_devset;
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	*devnump = 0;
35860Sstevel@tonic-gate 	detach_devlist = NULL;
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	/*
35890Sstevel@tonic-gate 	 * We switch on next_pass for the cases where a board
35900Sstevel@tonic-gate 	 * does not contain a particular type of component.
35910Sstevel@tonic-gate 	 * In these situations we don't want to return NULL
35920Sstevel@tonic-gate 	 * prematurely.  We need to check other devices and
35930Sstevel@tonic-gate 	 * we don't want to check the same type multiple times.
35940Sstevel@tonic-gate 	 * For example, if there were no cpus, then on pass 1
35950Sstevel@tonic-gate 	 * we would drop through and return the memory nodes.
35960Sstevel@tonic-gate 	 * However, on pass 2 we would switch back to the memory
35970Sstevel@tonic-gate 	 * nodes thereby returning them twice!  Using next_pass
35980Sstevel@tonic-gate 	 * forces us down to the end (or next item).
35990Sstevel@tonic-gate 	 */
36000Sstevel@tonic-gate 	if (pass == 1)
36010Sstevel@tonic-gate 		next_pass = 1;
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 	switch (next_pass) {
36040Sstevel@tonic-gate 	case 1:
36050Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
36060Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_MEM);
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 			detach_devlist = sbd_get_devlist(hp, sbp,
36090Sstevel@tonic-gate 						SBD_COMP_MEM,
36100Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD,
36110Sstevel@tonic-gate 						uset, devnump, 0);
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
36140Sstevel@tonic-gate 			if (!devset || detach_devlist) {
36150Sstevel@tonic-gate 				next_pass = 2;
36160Sstevel@tonic-gate 				return (detach_devlist);
36170Sstevel@tonic-gate 			}
36180Sstevel@tonic-gate 			/*
36190Sstevel@tonic-gate 			 * If the caller is interested in the entire
36200Sstevel@tonic-gate 			 * board, but there isn't any memory, then
36210Sstevel@tonic-gate 			 * just fall through to next component.
36220Sstevel@tonic-gate 			 */
36230Sstevel@tonic-gate 		}
36240Sstevel@tonic-gate 		/*FALLTHROUGH*/
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	case 2:
36270Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
36280Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_CPU);
36290Sstevel@tonic-gate 
36300Sstevel@tonic-gate 			detach_devlist = sbd_get_devlist(hp, sbp,
36310Sstevel@tonic-gate 						SBD_COMP_CPU,
36320Sstevel@tonic-gate 						MAX_CPU_UNITS_PER_BOARD,
36330Sstevel@tonic-gate 						uset, devnump, 0);
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_CPU, DEVSET_ANYUNIT);
36360Sstevel@tonic-gate 			if (!devset || detach_devlist) {
36370Sstevel@tonic-gate 				next_pass = 2;
36380Sstevel@tonic-gate 				return (detach_devlist);
36390Sstevel@tonic-gate 			}
36400Sstevel@tonic-gate 			/*
36410Sstevel@tonic-gate 			 * If the caller is interested in the entire
36420Sstevel@tonic-gate 			 * board, but there aren't any cpus, then just
36430Sstevel@tonic-gate 			 * fall through to check for the next component.
36440Sstevel@tonic-gate 			 */
36450Sstevel@tonic-gate 		}
36460Sstevel@tonic-gate 		/*FALLTHROUGH*/
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	case 3:
36490Sstevel@tonic-gate 		next_pass = -1;
36500Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
36510Sstevel@tonic-gate 			uset = DEVSET_GET_UNITSET(devset, SBD_COMP_IO);
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate 			detach_devlist = sbd_get_devlist(hp, sbp,
36540Sstevel@tonic-gate 						SBD_COMP_IO,
36550Sstevel@tonic-gate 						MAX_IO_UNITS_PER_BOARD,
36560Sstevel@tonic-gate 						uset, devnump, 0);
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 			DEVSET_DEL(devset, SBD_COMP_IO, DEVSET_ANYUNIT);
36590Sstevel@tonic-gate 			if (!devset || detach_devlist) {
36600Sstevel@tonic-gate 				next_pass = 4;
36610Sstevel@tonic-gate 				return (detach_devlist);
36620Sstevel@tonic-gate 			}
36630Sstevel@tonic-gate 		}
36640Sstevel@tonic-gate 		/*FALLTHROUGH*/
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 	default:
36670Sstevel@tonic-gate 		*devnump = 0;
36680Sstevel@tonic-gate 		return (NULL);
36690Sstevel@tonic-gate 	}
36700Sstevel@tonic-gate 	/*NOTREACHED*/
36710Sstevel@tonic-gate }
36720Sstevel@tonic-gate 
36730Sstevel@tonic-gate static int
sbd_pre_detach_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)36740Sstevel@tonic-gate sbd_pre_detach_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
36750Sstevel@tonic-gate 	int32_t devnum)
36760Sstevel@tonic-gate {
36770Sstevel@tonic-gate 	int		rv = 0;
36780Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
36790Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_detach_devlist";
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
36840Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
36850Sstevel@tonic-gate 
36860Sstevel@tonic-gate 	switch (nodetype) {
36870Sstevel@tonic-gate 	case SBD_COMP_CPU:
36880Sstevel@tonic-gate 		rv = sbd_pre_detach_cpu(hp, devlist, devnum);
36890Sstevel@tonic-gate 		break;
36900Sstevel@tonic-gate 
36910Sstevel@tonic-gate 	case SBD_COMP_MEM:
36920Sstevel@tonic-gate 		rv = sbd_pre_detach_mem(hp, devlist, devnum);
36930Sstevel@tonic-gate 		break;
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	case SBD_COMP_IO:
36960Sstevel@tonic-gate 		rv = sbd_pre_detach_io(hp, devlist, devnum);
36970Sstevel@tonic-gate 		break;
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate 	default:
37000Sstevel@tonic-gate 		rv = -1;
37010Sstevel@tonic-gate 		break;
37020Sstevel@tonic-gate 	}
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	/*
37050Sstevel@tonic-gate 	 * We want to continue attempting to detach
37060Sstevel@tonic-gate 	 * other components.
37070Sstevel@tonic-gate 	 */
37080Sstevel@tonic-gate 	return (rv);
37090Sstevel@tonic-gate }
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate static int
sbd_post_detach_devlist(sbd_handle_t * hp,sbd_devlist_t * devlist,int32_t devnum)37120Sstevel@tonic-gate sbd_post_detach_devlist(sbd_handle_t *hp, sbd_devlist_t *devlist,
37130Sstevel@tonic-gate 			int32_t devnum)
37140Sstevel@tonic-gate {
37150Sstevel@tonic-gate 	int		i, max_units = 0, rv = 0;
37160Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
37170Sstevel@tonic-gate 	sbd_board_t	*sbp;
37180Sstevel@tonic-gate 	sbd_istate_t	bstate;
37190Sstevel@tonic-gate 	static fn_t	f = "sbd_post_detach_devlist";
37200Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
37230Sstevel@tonic-gate 	nodetype = sbd_get_devtype(hp, devlist->dv_dip);
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 	PR_ALL("%s (nt = %s(%d), num = %d)...\n",
37280Sstevel@tonic-gate 		f, sbd_ct_str[(int)nodetype], (int)nodetype, devnum);
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 	/*
37310Sstevel@tonic-gate 	 * Need to free up devlist[] created earlier in
37320Sstevel@tonic-gate 	 * sbd_get_detach_devlist().
37330Sstevel@tonic-gate 	 */
37340Sstevel@tonic-gate 	switch (nodetype) {
37350Sstevel@tonic-gate 	case SBD_COMP_CPU:
37360Sstevel@tonic-gate 		max_units = MAX_CPU_UNITS_PER_BOARD;
37370Sstevel@tonic-gate 		rv = sbd_post_detach_cpu(hp, devlist, devnum);
37380Sstevel@tonic-gate 		break;
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 	case SBD_COMP_MEM:
37410Sstevel@tonic-gate 		max_units = MAX_MEM_UNITS_PER_BOARD;
37420Sstevel@tonic-gate 		rv = sbd_post_detach_mem(hp, devlist, devnum);
37430Sstevel@tonic-gate 		break;
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate 	case SBD_COMP_IO:
37460Sstevel@tonic-gate 		max_units = MAX_IO_UNITS_PER_BOARD;
37470Sstevel@tonic-gate 		rv = sbd_post_detach_io(hp, devlist, devnum);
37480Sstevel@tonic-gate 		break;
37490Sstevel@tonic-gate 
37500Sstevel@tonic-gate 	default:
37510Sstevel@tonic-gate 		rv = -1;
37520Sstevel@tonic-gate 		break;
37530Sstevel@tonic-gate 	}
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
37570Sstevel@tonic-gate 		int		unit;
37580Sstevel@tonic-gate 		sbderror_t	*ep;
37590Sstevel@tonic-gate 		dev_info_t	*dip;
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 		ep = &devlist[i].dv_error;
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 		if (sbd_set_err_in_hdl(hp, ep) == 0)
37640Sstevel@tonic-gate 			continue;
37650Sstevel@tonic-gate 
37660Sstevel@tonic-gate 		dip = devlist[i].dv_dip;
37670Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
37680Sstevel@tonic-gate 		if (unit == -1) {
37690Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
37700Sstevel@tonic-gate 				continue;
37710Sstevel@tonic-gate 			else {
37720Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
37730Sstevel@tonic-gate 				break;
37740Sstevel@tonic-gate 			}
37750Sstevel@tonic-gate 		}
37760Sstevel@tonic-gate 		nodetype = sbd_get_devtype(hp, dip);
37770Sstevel@tonic-gate 
37780Sstevel@tonic-gate 		if (sbd_check_unit_attached(sbp, dip, unit, nodetype,
37790Sstevel@tonic-gate 		    ep) >= 0) {
37800Sstevel@tonic-gate 			/*
37810Sstevel@tonic-gate 			 * Device is still attached probably due
37820Sstevel@tonic-gate 			 * to an error.  Need to keep track of it.
37830Sstevel@tonic-gate 			 */
37840Sstevel@tonic-gate 			PR_ALL("%s: ERROR (nt=%s, b=%d, u=%d) not detached\n",
37850Sstevel@tonic-gate 				f, sbd_ct_str[(int)nodetype], sbp->sb_num,
37860Sstevel@tonic-gate 				unit);
37870Sstevel@tonic-gate 			continue;
37880Sstevel@tonic-gate 		}
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate 		SBD_DEV_CLR_ATTACHED(sbp, nodetype, unit);
37910Sstevel@tonic-gate 		SBD_DEV_CLR_RELEASED(sbp, nodetype, unit);
37920Sstevel@tonic-gate 		SBD_DEV_CLR_UNREFERENCED(sbp, nodetype, unit);
37930Sstevel@tonic-gate 		SBD_DEVICE_TRANSITION(sbp, nodetype, unit,
37940Sstevel@tonic-gate 						SBD_STATE_UNCONFIGURED);
37950Sstevel@tonic-gate 	}
37960Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	bstate = SBD_BOARD_STATE(sbp);
37990Sstevel@tonic-gate 	if (bstate != SBD_STATE_UNCONFIGURED) {
38000Sstevel@tonic-gate 		if (SBD_DEVS_PRESENT(sbp) == SBD_DEVS_UNATTACHED(sbp)) {
38010Sstevel@tonic-gate 			/*
38020Sstevel@tonic-gate 			 * All devices are finally detached.
38030Sstevel@tonic-gate 			 */
38040Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_UNCONFIGURED);
38050Sstevel@tonic-gate 		} else if ((SBD_BOARD_STATE(sbp) != SBD_STATE_PARTIAL) &&
38060Sstevel@tonic-gate 				SBD_DEVS_ATTACHED(sbp)) {
38070Sstevel@tonic-gate 			/*
38080Sstevel@tonic-gate 			 * Some devices remain attached.
38090Sstevel@tonic-gate 			 */
38100Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_PARTIAL);
38110Sstevel@tonic-gate 		}
38120Sstevel@tonic-gate 	}
38130Sstevel@tonic-gate 
38140Sstevel@tonic-gate 	if (rv) {
38150Sstevel@tonic-gate 		PR_ALL("%s: errno %d, ecode %d during detach\n",
38160Sstevel@tonic-gate 			f, SBD_GET_ERRNO(SBD_HD2ERR(hp)),
38170Sstevel@tonic-gate 			SBD_GET_ERR(HD2MACHERR(hp)));
38180Sstevel@tonic-gate 	}
38190Sstevel@tonic-gate 
38200Sstevel@tonic-gate 	if (max_units && devlist) {
38210Sstevel@tonic-gate 		int	i;
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate 		for (i = 0; i < max_units; i++) {
38240Sstevel@tonic-gate 			if (SBD_GET_ERRSTR(&devlist[i].dv_error)) {
38250Sstevel@tonic-gate 				SBD_FREE_ERR(&devlist[i].dv_error);
38260Sstevel@tonic-gate 			} else {
38270Sstevel@tonic-gate 				break;
38280Sstevel@tonic-gate 			}
38290Sstevel@tonic-gate 		}
38300Sstevel@tonic-gate 		FREESTRUCT(devlist, sbd_devlist_t, max_units);
38310Sstevel@tonic-gate 	}
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate 	return (SBD_GET_ERRNO(SBD_HD2ERR(hp)) ? -1 : 0);
38340Sstevel@tonic-gate }
38350Sstevel@tonic-gate 
38360Sstevel@tonic-gate /*
38370Sstevel@tonic-gate  * Return the unit number of the respective dip if
38380Sstevel@tonic-gate  * it's found to be attached.
38390Sstevel@tonic-gate  */
38400Sstevel@tonic-gate static int
sbd_check_unit_attached(sbd_board_t * sbp,dev_info_t * dip,int unit,sbd_comp_type_t nodetype,sbderror_t * ep)38410Sstevel@tonic-gate sbd_check_unit_attached(sbd_board_t *sbp, dev_info_t *dip, int unit,
38420Sstevel@tonic-gate 	sbd_comp_type_t nodetype, sbderror_t *ep)
38430Sstevel@tonic-gate {
38440Sstevel@tonic-gate 	int		rv = -1;
38450Sstevel@tonic-gate 	processorid_t	cpuid;
38460Sstevel@tonic-gate 	uint64_t	basepa, endpa;
38470Sstevel@tonic-gate 	struct memlist	*ml;
38480Sstevel@tonic-gate 	extern struct memlist	*phys_install;
38490Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
38500Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
38510Sstevel@tonic-gate 	static fn_t	f = "sbd_check_unit_attached";
38520Sstevel@tonic-gate 
38530Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
38540Sstevel@tonic-gate 
38550Sstevel@tonic-gate 	switch (nodetype) {
38560Sstevel@tonic-gate 
38570Sstevel@tonic-gate 	case SBD_COMP_CPU:
38580Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
38590Sstevel@tonic-gate 		if (cpuid < 0) {
38600Sstevel@tonic-gate 			break;
38610Sstevel@tonic-gate 		}
38620Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
38630Sstevel@tonic-gate 		if (cpu_get(cpuid) != NULL)
38640Sstevel@tonic-gate 			rv = unit;
38650Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
38660Sstevel@tonic-gate 		break;
38670Sstevel@tonic-gate 
38680Sstevel@tonic-gate 	case SBD_COMP_MEM:
38690Sstevel@tonic-gate 		if (sbdphw_get_base_physaddr(hdp, dip, &basepa)) {
38700Sstevel@tonic-gate 			break;
38710Sstevel@tonic-gate 		}
38720Sstevel@tonic-gate 		if (sbdp_get_mem_alignment(hdp, dip, &endpa)) {
38730Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s sbdp_get_mem_alignment fail", f);
38740Sstevel@tonic-gate 			break;
38750Sstevel@tonic-gate 		}
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate 		basepa &= ~(endpa - 1);
38780Sstevel@tonic-gate 		endpa += basepa;
38790Sstevel@tonic-gate 		/*
38800Sstevel@tonic-gate 		 * Check if base address is in phys_install.
38810Sstevel@tonic-gate 		 */
38820Sstevel@tonic-gate 		memlist_read_lock();
3883*11474SJonathan.Adams@Sun.COM 		for (ml = phys_install; ml; ml = ml->ml_next)
3884*11474SJonathan.Adams@Sun.COM 			if ((endpa <= ml->ml_address) ||
3885*11474SJonathan.Adams@Sun.COM 			    (basepa >= (ml->ml_address + ml->ml_size)))
38860Sstevel@tonic-gate 				continue;
38870Sstevel@tonic-gate 			else
38880Sstevel@tonic-gate 				break;
38890Sstevel@tonic-gate 		memlist_read_unlock();
38900Sstevel@tonic-gate 		if (ml != NULL)
38910Sstevel@tonic-gate 			rv = unit;
38920Sstevel@tonic-gate 		break;
38930Sstevel@tonic-gate 
38940Sstevel@tonic-gate 	case SBD_COMP_IO:
38950Sstevel@tonic-gate 	{
38960Sstevel@tonic-gate 		dev_info_t	*tdip, *pdip;
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 		tdip = dip;
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 		/*
39010Sstevel@tonic-gate 		 * ddi_walk_devs() requires that topdip's parent be held.
39020Sstevel@tonic-gate 		 */
39030Sstevel@tonic-gate 		pdip = ddi_get_parent(sbp->sb_topdip);
39040Sstevel@tonic-gate 		if (pdip) {
39050Sstevel@tonic-gate 			ndi_hold_devi(pdip);
39060Sstevel@tonic-gate 			ndi_devi_enter(pdip, &rv);
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 		ddi_walk_devs(sbp->sb_topdip, sbd_check_io_attached,
39090Sstevel@tonic-gate 			(void *)&tdip);
39100Sstevel@tonic-gate 		if (pdip) {
39110Sstevel@tonic-gate 			ndi_devi_exit(pdip, rv);
39120Sstevel@tonic-gate 			ndi_rele_devi(pdip);
39130Sstevel@tonic-gate 		}
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 		if (tdip == NULL)
39160Sstevel@tonic-gate 			rv = unit;
39170Sstevel@tonic-gate 		else
39180Sstevel@tonic-gate 			rv = -1;
39190Sstevel@tonic-gate 		break;
39200Sstevel@tonic-gate 	}
39210Sstevel@tonic-gate 
39220Sstevel@tonic-gate 	default:
39230Sstevel@tonic-gate 		PR_ALL("%s: unexpected nodetype(%d) for dip 0x%p\n",
39240Sstevel@tonic-gate 			f, nodetype, (void *)dip);
39250Sstevel@tonic-gate 		rv = -1;
39260Sstevel@tonic-gate 		break;
39270Sstevel@tonic-gate 	}
39280Sstevel@tonic-gate 
39290Sstevel@tonic-gate 	/*
39300Sstevel@tonic-gate 	 * Save the error that sbdp sent us and report it
39310Sstevel@tonic-gate 	 */
39320Sstevel@tonic-gate 	if (rv == -1)
39330Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate 	return (rv);
39380Sstevel@tonic-gate }
39390Sstevel@tonic-gate 
39400Sstevel@tonic-gate /*
39410Sstevel@tonic-gate  * Return memhandle, if in fact, this memunit is the owner of
39420Sstevel@tonic-gate  * a scheduled memory delete.
39430Sstevel@tonic-gate  */
39440Sstevel@tonic-gate int
sbd_get_memhandle(sbd_handle_t * hp,dev_info_t * dip,memhandle_t * mhp)39450Sstevel@tonic-gate sbd_get_memhandle(sbd_handle_t *hp, dev_info_t *dip, memhandle_t *mhp)
39460Sstevel@tonic-gate {
39470Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
39480Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
39490Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
39500Sstevel@tonic-gate 	int		unit;
39510Sstevel@tonic-gate 	static fn_t	f = "sbd_get_memhandle";
39520Sstevel@tonic-gate 
39530Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
39540Sstevel@tonic-gate 
39550Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	unit = sbdp_get_unit_num(hdp, dip);
39580Sstevel@tonic-gate 	if (unit == -1) {
39590Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
39600Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
39610Sstevel@tonic-gate 		return (-1);
39620Sstevel@tonic-gate 	}
39630Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate 	if (mp->sbm_flags & SBD_MFLAG_RELOWNER) {
39680Sstevel@tonic-gate 		*mhp = mp->sbm_memhandle;
39690Sstevel@tonic-gate 		return (0);
39700Sstevel@tonic-gate 	} else {
39710Sstevel@tonic-gate 		SBD_SET_ERR(SBD_HD2ERR(hp), ESBD_INTERNAL);
39720Sstevel@tonic-gate 		SBD_SET_ERRSTR(SBD_HD2ERR(hp), sbp->sb_mempath[unit]);
39730Sstevel@tonic-gate 		return (-1);
39740Sstevel@tonic-gate 	}
39750Sstevel@tonic-gate 	/*NOTREACHED*/
39760Sstevel@tonic-gate }
39770Sstevel@tonic-gate 
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate static int
sbd_cpu_cnt(sbd_handle_t * hp,sbd_devset_t devset)39800Sstevel@tonic-gate sbd_cpu_cnt(sbd_handle_t *hp, sbd_devset_t devset)
39810Sstevel@tonic-gate {
39820Sstevel@tonic-gate 	int		c, cix;
39830Sstevel@tonic-gate 	sbd_board_t	*sbp;
39840Sstevel@tonic-gate 
39850Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
39860Sstevel@tonic-gate 
39870Sstevel@tonic-gate 	/*
39880Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
39890Sstevel@tonic-gate 	 */
39900Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
39910Sstevel@tonic-gate 
39920Sstevel@tonic-gate 	for (c = cix = 0; c < MAX_CMP_UNITS_PER_BOARD; c++) {
39930Sstevel@tonic-gate 		/*
39940Sstevel@tonic-gate 		 * Index for core 1 , if exists.
39950Sstevel@tonic-gate 		 * With the current implementation it is
39960Sstevel@tonic-gate 		 * MAX_CMP_UNITS_PER_BOARD off from core 0.
39970Sstevel@tonic-gate 		 * The calculation will need to change if
39980Sstevel@tonic-gate 		 * the assumption is no longer true.
39990Sstevel@tonic-gate 		 */
40000Sstevel@tonic-gate 		int		c1 = c + MAX_CMP_UNITS_PER_BOARD;
40010Sstevel@tonic-gate 
40020Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CMP, c) == 0) {
40030Sstevel@tonic-gate 			continue;
40040Sstevel@tonic-gate 		}
40050Sstevel@tonic-gate 
40060Sstevel@tonic-gate 		/*
40070Sstevel@tonic-gate 		 * Check to see if the dip(s) exist for this chip
40080Sstevel@tonic-gate 		 */
40090Sstevel@tonic-gate 		if ((sbp->sb_devlist[NIX(SBD_COMP_CMP)][c] == NULL) &&
40100Sstevel@tonic-gate 		    (sbp->sb_devlist[NIX(SBD_COMP_CMP)][c1] == NULL))
40110Sstevel@tonic-gate 			continue;
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 		cix++;
40140Sstevel@tonic-gate 	}
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 	return (cix);
40170Sstevel@tonic-gate }
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate static int
sbd_mem_cnt(sbd_handle_t * hp,sbd_devset_t devset)40200Sstevel@tonic-gate sbd_mem_cnt(sbd_handle_t *hp, sbd_devset_t devset)
40210Sstevel@tonic-gate {
40220Sstevel@tonic-gate 	int		i, ix;
40230Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 	/*
40260Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
40270Sstevel@tonic-gate 	 */
40280Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 	for (i = ix = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
40310Sstevel@tonic-gate 		dev_info_t	*dip;
40320Sstevel@tonic-gate 
40330Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, i) == 0) {
40340Sstevel@tonic-gate 			continue;
40350Sstevel@tonic-gate 		}
40360Sstevel@tonic-gate 
40370Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_MEM)][i];
40380Sstevel@tonic-gate 		if (dip == NULL)
40390Sstevel@tonic-gate 			continue;
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 		ix++;
40420Sstevel@tonic-gate 	}
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 	return (ix);
40450Sstevel@tonic-gate }
40460Sstevel@tonic-gate 
40470Sstevel@tonic-gate /*
40480Sstevel@tonic-gate  * NOTE: This routine is only partially smart about multiple
40490Sstevel@tonic-gate  *	 mem-units.  Need to make mem-status structure smart
40500Sstevel@tonic-gate  *	 about them also.
40510Sstevel@tonic-gate  */
40520Sstevel@tonic-gate static int
sbd_mem_status(sbd_handle_t * hp,sbd_devset_t devset,sbd_dev_stat_t * dsp)40530Sstevel@tonic-gate sbd_mem_status(sbd_handle_t *hp, sbd_devset_t devset, sbd_dev_stat_t *dsp)
40540Sstevel@tonic-gate {
40550Sstevel@tonic-gate 	int		m, mix, rv;
40560Sstevel@tonic-gate 	memdelstat_t	mdst;
40570Sstevel@tonic-gate 	memquery_t	mq;
40580Sstevel@tonic-gate 	sbd_board_t	*sbp;
40590Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
40600Sstevel@tonic-gate 	sbd_mem_stat_t	*msp;
40610Sstevel@tonic-gate 	extern int	kcage_on;
40620Sstevel@tonic-gate 	int		i;
40630Sstevel@tonic-gate 	static fn_t	f = "sbd_mem_status";
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
40660Sstevel@tonic-gate 
40670Sstevel@tonic-gate 	/*
40680Sstevel@tonic-gate 	 * Check the present devset and access the dip with
40690Sstevel@tonic-gate 	 * status lock held to protect agains a concurrent
40700Sstevel@tonic-gate 	 * unconfigure or disconnect thread.
40710Sstevel@tonic-gate 	 */
40720Sstevel@tonic-gate 	mutex_enter(&sbp->sb_slock);
40730Sstevel@tonic-gate 
40740Sstevel@tonic-gate 	/*
40750Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
40760Sstevel@tonic-gate 	 */
40770Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 	for (m = mix = 0; m < MAX_MEM_UNITS_PER_BOARD; m++) {
40800Sstevel@tonic-gate 		dev_info_t	*dip;
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, m) == 0)
40840Sstevel@tonic-gate 			continue;
40850Sstevel@tonic-gate 
40860Sstevel@tonic-gate 		/*
40870Sstevel@tonic-gate 		 * Check to make sure the memory unit is in a state
40880Sstevel@tonic-gate 		 * where its fully initialized.
40890Sstevel@tonic-gate 		 */
40900Sstevel@tonic-gate 		if (SBD_DEVICE_STATE(sbp, SBD_COMP_MEM, m) == SBD_STATE_EMPTY)
40910Sstevel@tonic-gate 			continue;
40920Sstevel@tonic-gate 
40930Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_MEM)][m];
40940Sstevel@tonic-gate 		if (dip == NULL)
40950Sstevel@tonic-gate 			continue;
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, m);
40980Sstevel@tonic-gate 
40990Sstevel@tonic-gate 		msp = &dsp->d_mem;
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate 		bzero((caddr_t)msp, sizeof (*msp));
41020Sstevel@tonic-gate 		msp->ms_type = SBD_COMP_MEM;
41030Sstevel@tonic-gate 
41040Sstevel@tonic-gate 		/*
41050Sstevel@tonic-gate 		 * The plugin expects -1 for the mem unit
41060Sstevel@tonic-gate 		 */
41070Sstevel@tonic-gate 		msp->ms_cm.c_id.c_unit = -1;
41080Sstevel@tonic-gate 
41090Sstevel@tonic-gate 		/*
41100Sstevel@tonic-gate 		 * Get the memory name from what sbdp gave us
41110Sstevel@tonic-gate 		 */
41120Sstevel@tonic-gate 		for (i = 0; SBD_COMP(i) != SBD_COMP_UNKNOWN; i++) {
41130Sstevel@tonic-gate 			if (SBD_COMP(i) == SBD_COMP_MEM) {
41140Sstevel@tonic-gate 				(void) strcpy(msp->ms_name, SBD_DEVNAME(i));
41150Sstevel@tonic-gate 			}
41160Sstevel@tonic-gate 		}
41170Sstevel@tonic-gate 		msp->ms_cm.c_cond = mp->sbm_cm.sbdev_cond;
41180Sstevel@tonic-gate 		msp->ms_cm.c_busy = mp->sbm_cm.sbdev_busy;
41190Sstevel@tonic-gate 		msp->ms_cm.c_time = mp->sbm_cm.sbdev_time;
41200Sstevel@tonic-gate 
41210Sstevel@tonic-gate 		/* XXX revisit this after memory conversion */
41220Sstevel@tonic-gate 		msp->ms_ostate = ostate_cvt(SBD_DEVICE_STATE(
41230Sstevel@tonic-gate 			sbp, SBD_COMP_MEM, m));
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 		msp->ms_basepfn = mp->sbm_basepfn;
41260Sstevel@tonic-gate 		msp->ms_pageslost = mp->sbm_pageslost;
41270Sstevel@tonic-gate 		msp->ms_cage_enabled = kcage_on;
41280Sstevel@tonic-gate 		msp->ms_interleave = mp->sbm_interleave;
41290Sstevel@tonic-gate 
41300Sstevel@tonic-gate 		if (mp->sbm_flags & SBD_MFLAG_RELOWNER)
41310Sstevel@tonic-gate 			rv = kphysm_del_status(mp->sbm_memhandle, &mdst);
41320Sstevel@tonic-gate 		else
41330Sstevel@tonic-gate 			rv = KPHYSM_EHANDLE;	/* force 'if' to fail */
41340Sstevel@tonic-gate 
41350Sstevel@tonic-gate 		if (rv == KPHYSM_OK) {
41360Sstevel@tonic-gate 			msp->ms_totpages += mdst.phys_pages;
41370Sstevel@tonic-gate 
41380Sstevel@tonic-gate 			/*
41390Sstevel@tonic-gate 			 * Any pages above managed is "free",
41400Sstevel@tonic-gate 			 * i.e. it's collected.
41410Sstevel@tonic-gate 			 */
41420Sstevel@tonic-gate 			msp->ms_detpages += (uint_t)(mdst.collected +
41430Sstevel@tonic-gate 							mdst.phys_pages -
41440Sstevel@tonic-gate 							mdst.managed);
41450Sstevel@tonic-gate 		} else {
41460Sstevel@tonic-gate 			msp->ms_totpages += (uint_t)mp->sbm_npages;
41470Sstevel@tonic-gate 
41480Sstevel@tonic-gate 			/*
41490Sstevel@tonic-gate 			 * If we're UNREFERENCED or UNCONFIGURED,
41500Sstevel@tonic-gate 			 * then the number of detached pages is
41510Sstevel@tonic-gate 			 * however many pages are on the board.
41520Sstevel@tonic-gate 			 * I.e. detached = not in use by OS.
41530Sstevel@tonic-gate 			 */
41540Sstevel@tonic-gate 			switch (msp->ms_cm.c_ostate) {
41550Sstevel@tonic-gate 			/*
41560Sstevel@tonic-gate 			 * changed to use cfgadm states
41570Sstevel@tonic-gate 			 *
41580Sstevel@tonic-gate 			 * was:
41590Sstevel@tonic-gate 			 *	case SFDR_STATE_UNREFERENCED:
41600Sstevel@tonic-gate 			 *	case SFDR_STATE_UNCONFIGURED:
41610Sstevel@tonic-gate 			 */
41620Sstevel@tonic-gate 			case SBD_STAT_UNCONFIGURED:
41630Sstevel@tonic-gate 				msp->ms_detpages = msp->ms_totpages;
41640Sstevel@tonic-gate 				break;
41650Sstevel@tonic-gate 
41660Sstevel@tonic-gate 			default:
41670Sstevel@tonic-gate 				break;
41680Sstevel@tonic-gate 			}
41690Sstevel@tonic-gate 		}
41700Sstevel@tonic-gate 
41710Sstevel@tonic-gate 		rv = kphysm_del_span_query(mp->sbm_basepfn,
41720Sstevel@tonic-gate 						mp->sbm_npages, &mq);
41730Sstevel@tonic-gate 		if (rv == KPHYSM_OK) {
41740Sstevel@tonic-gate 			msp->ms_managed_pages = mq.managed;
41750Sstevel@tonic-gate 			msp->ms_noreloc_pages = mq.nonrelocatable;
41760Sstevel@tonic-gate 			msp->ms_noreloc_first = mq.first_nonrelocatable;
41770Sstevel@tonic-gate 			msp->ms_noreloc_last = mq.last_nonrelocatable;
41780Sstevel@tonic-gate 			msp->ms_cm.c_sflags = 0;
41790Sstevel@tonic-gate 			if (mq.nonrelocatable) {
41800Sstevel@tonic-gate 				SBD_SET_SUSPEND(SBD_CMD_UNCONFIGURE,
41810Sstevel@tonic-gate 				    dsp->ds_suspend);
41820Sstevel@tonic-gate 			}
41830Sstevel@tonic-gate 		} else {
41840Sstevel@tonic-gate 			PR_MEM("%s: kphysm_del_span_query() = %d\n", f, rv);
41850Sstevel@tonic-gate 		}
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 		mix++;
41880Sstevel@tonic-gate 		dsp++;
41890Sstevel@tonic-gate 	}
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	mutex_exit(&sbp->sb_slock);
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 	return (mix);
41940Sstevel@tonic-gate }
41950Sstevel@tonic-gate 
41960Sstevel@tonic-gate static void
sbd_cancel(sbd_handle_t * hp)41970Sstevel@tonic-gate sbd_cancel(sbd_handle_t *hp)
41980Sstevel@tonic-gate {
41990Sstevel@tonic-gate 	int		i;
42000Sstevel@tonic-gate 	sbd_devset_t	devset;
42010Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
42020Sstevel@tonic-gate 	static fn_t	f = "sbd_cancel";
42030Sstevel@tonic-gate 	int		rv;
42040Sstevel@tonic-gate 
42050Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 	/*
42080Sstevel@tonic-gate 	 * Only devices which have been "released" are
42090Sstevel@tonic-gate 	 * subject to cancellation.
42100Sstevel@tonic-gate 	 */
42110Sstevel@tonic-gate 	devset = HD2MACHHD(hp)->sh_devset & SBD_DEVS_UNREFERENCED(sbp);
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 	/*
42140Sstevel@tonic-gate 	 * Nothing to do for CPUs or IO other than change back
42150Sstevel@tonic-gate 	 * their state.
42160Sstevel@tonic-gate 	 */
42170Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
42180Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
42190Sstevel@tonic-gate 			continue;
42200Sstevel@tonic-gate 		if (sbd_cancel_cpu(hp, i) != SBD_CPUERR_FATAL) {
42210Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(sbp, SBD_COMP_CPU, i,
42220Sstevel@tonic-gate 						SBD_STATE_CONFIGURED);
42230Sstevel@tonic-gate 		} else {
42240Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(sbp, SBD_COMP_CPU, i,
42250Sstevel@tonic-gate 						SBD_STATE_FATAL);
42260Sstevel@tonic-gate 		}
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
42300Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
42310Sstevel@tonic-gate 			continue;
42320Sstevel@tonic-gate 		SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO, i,
42330Sstevel@tonic-gate 					SBD_STATE_CONFIGURED);
42340Sstevel@tonic-gate 	}
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
42370Sstevel@tonic-gate 		if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
42380Sstevel@tonic-gate 			continue;
42390Sstevel@tonic-gate 		if ((rv = sbd_cancel_mem(hp, i)) == 0) {
42400Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, i,
42410Sstevel@tonic-gate 						SBD_STATE_CONFIGURED);
42420Sstevel@tonic-gate 		} else if (rv == -1) {
42430Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, i,
42440Sstevel@tonic-gate 						SBD_STATE_FATAL);
42450Sstevel@tonic-gate 		}
42460Sstevel@tonic-gate 	}
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate 	PR_ALL("%s: unreleasing devset (0x%x)\n", f, (uint_t)devset);
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 	SBD_DEVS_CANCEL(sbp, devset);
42510Sstevel@tonic-gate 
42520Sstevel@tonic-gate 	if (SBD_DEVS_UNREFERENCED(sbp) == 0) {
42530Sstevel@tonic-gate 		sbd_istate_t	new_state;
42540Sstevel@tonic-gate 		/*
42550Sstevel@tonic-gate 		 * If the board no longer has any released devices
42560Sstevel@tonic-gate 		 * than transfer it back to the CONFIG/PARTIAL state.
42570Sstevel@tonic-gate 		 */
42580Sstevel@tonic-gate 		if (SBD_DEVS_ATTACHED(sbp) == SBD_DEVS_PRESENT(sbp))
42590Sstevel@tonic-gate 			new_state = SBD_STATE_CONFIGURED;
42600Sstevel@tonic-gate 		else
42610Sstevel@tonic-gate 			new_state = SBD_STATE_PARTIAL;
42620Sstevel@tonic-gate 		if (SBD_BOARD_STATE(sbp) != new_state) {
42630Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, new_state);
42640Sstevel@tonic-gate 		}
42650Sstevel@tonic-gate 		sbp->sb_ostate = SBD_STAT_CONFIGURED;
42660Sstevel@tonic-gate 		(void) drv_getparm(TIME, (void *)&sbp->sb_time);
42670Sstevel@tonic-gate 	}
42680Sstevel@tonic-gate }
42690Sstevel@tonic-gate 
42700Sstevel@tonic-gate static void
sbd_get_ncm(sbd_handle_t * hp)42710Sstevel@tonic-gate sbd_get_ncm(sbd_handle_t *hp)
42720Sstevel@tonic-gate {
42730Sstevel@tonic-gate 	sbd_devset_t devset;
42740Sstevel@tonic-gate 	sbd_priv_handle_t	*shp = HD2MACHHD(hp);
42750Sstevel@tonic-gate 	sbd_cmd_t		*cmdp =  (sbd_cmd_t *)hp->h_iap;
42760Sstevel@tonic-gate 	int			error;
42770Sstevel@tonic-gate 
42780Sstevel@tonic-gate 	/* pre_op restricted the devices to those selected by the ioctl */
42790Sstevel@tonic-gate 	devset = shp->sh_devset;
42800Sstevel@tonic-gate 
42810Sstevel@tonic-gate 	cmdp->cmd_getncm.g_ncm = sbd_cpu_cnt(hp, devset)
42820Sstevel@tonic-gate 		+ sbd_io_cnt(hp, devset) + sbd_mem_cnt(hp, devset);
42830Sstevel@tonic-gate 
42840Sstevel@tonic-gate 	error = sbd_copyout_ioarg(hp->h_mode, hp->h_cmd, cmdp,
42850Sstevel@tonic-gate 		(sbd_ioctl_arg_t *)shp->sh_arg);
42860Sstevel@tonic-gate 
42870Sstevel@tonic-gate 	if (error != 0)
42880Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), error);
42890Sstevel@tonic-gate }
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate static void
sbd_status(sbd_handle_t * hp)42920Sstevel@tonic-gate sbd_status(sbd_handle_t *hp)
42930Sstevel@tonic-gate {
42940Sstevel@tonic-gate 	int			nstat, mode, ncm, sz, cksz;
42950Sstevel@tonic-gate 	sbd_priv_handle_t	*shp = HD2MACHHD(hp);
42960Sstevel@tonic-gate 	sbd_devset_t		devset;
42970Sstevel@tonic-gate 	sbd_board_t		*sbp = SBDH2BD(hp->h_sbd);
42980Sstevel@tonic-gate 	sbd_stat_t		*dstatp;
42990Sstevel@tonic-gate 	sbd_cmd_t		*cmdp =  (sbd_cmd_t *)hp->h_iap;
43000Sstevel@tonic-gate 	sbdp_handle_t		*hdp;
43010Sstevel@tonic-gate 	sbd_dev_stat_t		*devstatp;
43020Sstevel@tonic-gate 
43030Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
43040Sstevel@tonic-gate 	int			sz32;
43050Sstevel@tonic-gate 	sbd_stat32_t		*dstat32p;
43060Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 	static fn_t	f = "sbd_status";
43090Sstevel@tonic-gate 
43100Sstevel@tonic-gate 	mode = hp->h_mode;
43110Sstevel@tonic-gate 	devset = shp->sh_devset;
43120Sstevel@tonic-gate 
43130Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
43140Sstevel@tonic-gate 
43150Sstevel@tonic-gate 	if (cmdp->cmd_cm.c_id.c_type == SBD_COMP_NONE) {
43160Sstevel@tonic-gate 		if (cmdp->cmd_cm.c_flags & SBD_FLAG_ALLCMP) {
43170Sstevel@tonic-gate 			/*
43180Sstevel@tonic-gate 			 * Get the number of components "ncm" on the board.
43190Sstevel@tonic-gate 			 * Calculate size of buffer required to store one
43200Sstevel@tonic-gate 			 * sbd_stat_t structure plus ncm-1 sbd_dev_stat_t
43210Sstevel@tonic-gate 			 * structures. Note that sbd_stat_t already contains
43220Sstevel@tonic-gate 			 * one sbd_dev_stat_t, so only an additional ncm-1
43230Sstevel@tonic-gate 			 * sbd_dev_stat_t structures need to be accounted for
43240Sstevel@tonic-gate 			 * in the calculation when more than one component
43250Sstevel@tonic-gate 			 * is present.
43260Sstevel@tonic-gate 			 */
43270Sstevel@tonic-gate 			ncm = sbd_cpu_cnt(hp, devset) + sbd_io_cnt(hp, devset) +
43280Sstevel@tonic-gate 			    sbd_mem_cnt(hp, devset);
43290Sstevel@tonic-gate 
43300Sstevel@tonic-gate 		} else {
43310Sstevel@tonic-gate 			/*
43320Sstevel@tonic-gate 			 * In the case of c_type == SBD_COMP_NONE, and
43330Sstevel@tonic-gate 			 * SBD_FLAG_ALLCMP not specified, only the board
43340Sstevel@tonic-gate 			 * info is to be returned, no components.
43350Sstevel@tonic-gate 			 */
43360Sstevel@tonic-gate 			ncm = 0;
43370Sstevel@tonic-gate 			devset = 0;
43380Sstevel@tonic-gate 		}
43390Sstevel@tonic-gate 	} else {
43400Sstevel@tonic-gate 		/* Confirm that only one component is selected. */
43410Sstevel@tonic-gate 		ncm = sbd_cpu_cnt(hp, devset) + sbd_io_cnt(hp, devset) +
43420Sstevel@tonic-gate 		    sbd_mem_cnt(hp, devset);
43430Sstevel@tonic-gate 		if (ncm != 1) {
43440Sstevel@tonic-gate 			PR_ALL("%s: expected ncm of 1, got %d, devset 0x%x\n",
43450Sstevel@tonic-gate 			    f, ncm, devset);
43460Sstevel@tonic-gate 			SBD_SET_ERRNO(SBD_HD2ERR(hp), EINVAL);
43470Sstevel@tonic-gate 			return;
43480Sstevel@tonic-gate 		}
43490Sstevel@tonic-gate 	}
43500Sstevel@tonic-gate 
43510Sstevel@tonic-gate 	sz = sizeof (sbd_stat_t);
43520Sstevel@tonic-gate 	if (ncm > 1)
43530Sstevel@tonic-gate 		sz += sizeof (sbd_dev_stat_t) * (ncm - 1);
43540Sstevel@tonic-gate 
43550Sstevel@tonic-gate 	cksz = sz;
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 	/*
43580Sstevel@tonic-gate 	 * s_nbytes describes the size of the preallocated user
43590Sstevel@tonic-gate 	 * buffer into which the application is executing to
43600Sstevel@tonic-gate 	 * receive the sbd_stat_t and sbd_dev_stat_t structures.
43610Sstevel@tonic-gate 	 * This buffer must be at least the required (sz) size.
43620Sstevel@tonic-gate 	 */
43630Sstevel@tonic-gate 
43640Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
43650Sstevel@tonic-gate 
43660Sstevel@tonic-gate 	/*
43670Sstevel@tonic-gate 	 * More buffer space is required for the 64bit to 32bit
43680Sstevel@tonic-gate 	 * conversion of data structures.
43690Sstevel@tonic-gate 	 */
43700Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
43710Sstevel@tonic-gate 		sz32 = sizeof (sbd_stat32_t);
43720Sstevel@tonic-gate 		if (ncm > 1)
43730Sstevel@tonic-gate 			sz32  += sizeof (sbd_dev_stat32_t) * (ncm - 1);
43740Sstevel@tonic-gate 		cksz = sz32;
43750Sstevel@tonic-gate 	} else
43760Sstevel@tonic-gate 		sz32 = 0;
43770Sstevel@tonic-gate #endif
43780Sstevel@tonic-gate 
43790Sstevel@tonic-gate 	if ((int)cmdp->cmd_stat.s_nbytes < cksz) {
43800Sstevel@tonic-gate 		PR_ALL("%s: ncm=%d s_nbytes = 0x%x\n", f, ncm,
43810Sstevel@tonic-gate 		    cmdp->cmd_stat.s_nbytes);
43820Sstevel@tonic-gate 		PR_ALL("%s: expected size of 0x%x\n", f, cksz);
43830Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), EINVAL);
43840Sstevel@tonic-gate 		return;
43850Sstevel@tonic-gate 	}
43860Sstevel@tonic-gate 
43870Sstevel@tonic-gate 	dstatp = kmem_zalloc(sz, KM_SLEEP);
43880Sstevel@tonic-gate 	devstatp = &dstatp->s_stat[0];
43890Sstevel@tonic-gate 
43900Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
43910Sstevel@tonic-gate 	if (sz32 != 0)
43920Sstevel@tonic-gate 		dstat32p = kmem_zalloc(sz32, KM_SLEEP);
43930Sstevel@tonic-gate #endif
43940Sstevel@tonic-gate 
43950Sstevel@tonic-gate 	/*
43960Sstevel@tonic-gate 	 * if connected or better, provide cached status if available,
43970Sstevel@tonic-gate 	 * otherwise call sbdp for status
43980Sstevel@tonic-gate 	 */
43990Sstevel@tonic-gate 	mutex_enter(&sbp->sb_flags_mutex);
44000Sstevel@tonic-gate 	switch (sbp->sb_state) {
44010Sstevel@tonic-gate 
44020Sstevel@tonic-gate 	case	SBD_STATE_CONNECTED:
44030Sstevel@tonic-gate 	case	SBD_STATE_PARTIAL:
44040Sstevel@tonic-gate 	case	SBD_STATE_CONFIGURED:
44050Sstevel@tonic-gate 		if (sbp->sb_flags & SBD_BOARD_STATUS_CACHED) {
44060Sstevel@tonic-gate 			bcopy(&sbp->sb_stat, dstatp, sizeof (sbd_stat_t));
44070Sstevel@tonic-gate 			dstatp->s_rstate = rstate_cvt(sbp->sb_state);
44080Sstevel@tonic-gate 			dstatp->s_ostate = ostate_cvt(sbp->sb_state);
44090Sstevel@tonic-gate 			dstatp->s_busy = sbp->sb_busy;
44100Sstevel@tonic-gate 			dstatp->s_time = sbp->sb_time;
44110Sstevel@tonic-gate 			dstatp->s_cond = sbp->sb_cond;
44120Sstevel@tonic-gate 			break;
44130Sstevel@tonic-gate 		}
44140Sstevel@tonic-gate 	/*FALLTHROUGH*/
44150Sstevel@tonic-gate 
44160Sstevel@tonic-gate 	default:
44170Sstevel@tonic-gate 		sbp->sb_flags &= ~SBD_BOARD_STATUS_CACHED;
44180Sstevel@tonic-gate 		dstatp->s_board = sbp->sb_num;
44190Sstevel@tonic-gate 		dstatp->s_ostate = ostate_cvt(sbp->sb_state);
44200Sstevel@tonic-gate 		dstatp->s_time = sbp->sb_time;
44210Sstevel@tonic-gate 
44220Sstevel@tonic-gate 		hdp = sbd_get_sbdp_handle(sbp, hp);
44230Sstevel@tonic-gate 
44240Sstevel@tonic-gate 		if (sbdp_get_board_status(hdp, dstatp) != 0) {
44250Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
44260Sstevel@tonic-gate 			sbd_release_sbdp_handle(hdp);
44270Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
44280Sstevel@tonic-gate 			if (sz32 != 0)
44290Sstevel@tonic-gate 				kmem_free(dstat32p, sz32);
44300Sstevel@tonic-gate #endif
44310Sstevel@tonic-gate 			kmem_free(dstatp, sz);
44320Sstevel@tonic-gate 			mutex_exit(&sbp->sb_flags_mutex);
44330Sstevel@tonic-gate 			return;
44340Sstevel@tonic-gate 		}
44350Sstevel@tonic-gate 		/*
44360Sstevel@tonic-gate 		 * Do not cache status if the busy flag has
44370Sstevel@tonic-gate 		 * been set by the call to sbdp_get_board_status().
44380Sstevel@tonic-gate 		 */
44390Sstevel@tonic-gate 		if (!dstatp->s_busy) {
44400Sstevel@tonic-gate 			/* Can get board busy flag now */
44410Sstevel@tonic-gate 			dstatp->s_busy = sbp->sb_busy;
44420Sstevel@tonic-gate 			sbp->sb_cond = (sbd_cond_t)dstatp->s_cond;
44430Sstevel@tonic-gate 			bcopy(dstatp, &sbp->sb_stat,
44440Sstevel@tonic-gate 				sizeof (sbd_stat_t));
44450Sstevel@tonic-gate 			sbp->sb_flags |= SBD_BOARD_STATUS_CACHED;
44460Sstevel@tonic-gate 		}
44470Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
44480Sstevel@tonic-gate 		break;
44490Sstevel@tonic-gate 	}
44500Sstevel@tonic-gate 	mutex_exit(&sbp->sb_flags_mutex);
44510Sstevel@tonic-gate 
44520Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT))
44530Sstevel@tonic-gate 		if ((nstat = sbd_cpu_flags(hp, devset, devstatp)) > 0) {
44540Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
44550Sstevel@tonic-gate 			devstatp += nstat;
44560Sstevel@tonic-gate 		}
44570Sstevel@tonic-gate 
44580Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT))
44590Sstevel@tonic-gate 		if ((nstat = sbd_mem_status(hp, devset, devstatp)) > 0) {
44600Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
44610Sstevel@tonic-gate 			devstatp += nstat;
44620Sstevel@tonic-gate 		}
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT))
44650Sstevel@tonic-gate 		if ((nstat = sbd_io_status(hp, devset, devstatp)) > 0) {
44660Sstevel@tonic-gate 			dstatp->s_nstat += nstat;
44670Sstevel@tonic-gate 			devstatp += nstat;
44680Sstevel@tonic-gate 		}
44690Sstevel@tonic-gate 
44700Sstevel@tonic-gate 	/* paranoia: detect buffer overrun */
44710Sstevel@tonic-gate 	if ((caddr_t)devstatp > ((caddr_t)dstatp) + sz) {
44720Sstevel@tonic-gate 		PR_ALL("%s: buffer overrun\n", f);
44730Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
44740Sstevel@tonic-gate 		if (sz32 != 0)
44750Sstevel@tonic-gate 			kmem_free(dstat32p, sz32);
44760Sstevel@tonic-gate #endif
44770Sstevel@tonic-gate 		kmem_free(dstatp, sz);
44780Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), EINVAL);
44790Sstevel@tonic-gate 		return;
44800Sstevel@tonic-gate 	}
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate /* if necessary, move data into intermediate device status buffer */
44830Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
44840Sstevel@tonic-gate 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
44850Sstevel@tonic-gate 		int		i, j;
44860Sstevel@tonic-gate 
44870Sstevel@tonic-gate 		ASSERT(sz32 != 0);
44880Sstevel@tonic-gate 		/* paranoia: detect buffer overrun */
44890Sstevel@tonic-gate 		if ((caddr_t)&dstat32p->s_stat[dstatp->s_nstat] >
44900Sstevel@tonic-gate 		    ((caddr_t)dstat32p) + sz32) {
44910Sstevel@tonic-gate 			cmn_err(CE_WARN,
44920Sstevel@tonic-gate 				"sbd:%s: buffer32 overrun", f);
44930Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
44940Sstevel@tonic-gate 			if (sz32 != 0)
44950Sstevel@tonic-gate 				kmem_free(dstat32p, sz32);
44960Sstevel@tonic-gate #endif
44970Sstevel@tonic-gate 			kmem_free(dstatp, sz);
44980Sstevel@tonic-gate 			SBD_SET_ERRNO(SBD_HD2ERR(hp), EINVAL);
44990Sstevel@tonic-gate 			return;
45000Sstevel@tonic-gate 		}
45010Sstevel@tonic-gate 
45020Sstevel@tonic-gate 		/*
45030Sstevel@tonic-gate 		 * initialize 32 bit sbd board status structure
45040Sstevel@tonic-gate 		 */
45050Sstevel@tonic-gate 		dstat32p->s_board = (int32_t)dstatp->s_board;
45060Sstevel@tonic-gate 		dstat32p->s_nstat = (int32_t)dstatp->s_nstat;
45070Sstevel@tonic-gate 		dstat32p->s_rstate = dstatp->s_rstate;
45080Sstevel@tonic-gate 		dstat32p->s_ostate = dstatp->s_ostate;
45090Sstevel@tonic-gate 		dstat32p->s_cond = dstatp->s_cond;
45100Sstevel@tonic-gate 		dstat32p->s_busy = dstatp->s_busy;
45110Sstevel@tonic-gate 		dstat32p->s_time = dstatp->s_time;
45120Sstevel@tonic-gate 		dstat32p->s_assigned = dstatp->s_assigned;
45130Sstevel@tonic-gate 		dstat32p->s_power = dstatp->s_power;
45140Sstevel@tonic-gate 		dstat32p->s_platopts = (int32_t)dstatp->s_platopts;
45150Sstevel@tonic-gate 		(void) strcpy(dstat32p->s_type, dstatp->s_type);
45160Sstevel@tonic-gate 
45170Sstevel@tonic-gate 		for (i = 0; i < dstatp->s_nstat; i++) {
45180Sstevel@tonic-gate 			sbd_dev_stat_t	*dsp = &dstatp->s_stat[i];
45190Sstevel@tonic-gate 			sbd_dev_stat32_t	*ds32p = &dstat32p->s_stat[i];
45200Sstevel@tonic-gate 
45210Sstevel@tonic-gate 			/*
45220Sstevel@tonic-gate 			 * copy common data for the device
45230Sstevel@tonic-gate 			 */
45240Sstevel@tonic-gate 			ds32p->d_cm.ci_type = (int32_t)dsp->d_cm.ci_type;
45250Sstevel@tonic-gate 			ds32p->d_cm.ci_unit = (int32_t)dsp->d_cm.ci_unit;
45260Sstevel@tonic-gate 			ds32p->d_cm.c_ostate = (int32_t)dsp->d_cm.c_ostate;
45270Sstevel@tonic-gate 			ds32p->d_cm.c_cond = (int32_t)dsp->d_cm.c_cond;
45280Sstevel@tonic-gate 			ds32p->d_cm.c_busy = (int32_t)dsp->d_cm.c_busy;
45290Sstevel@tonic-gate 			ds32p->d_cm.c_time = (time32_t)dsp->d_cm.c_time;
45300Sstevel@tonic-gate 			ds32p->d_cm.c_sflags = (int32_t)dsp->d_cm.c_sflags;
45310Sstevel@tonic-gate 			(void) strcpy(ds32p->d_cm.ci_name, dsp->d_cm.ci_name);
45320Sstevel@tonic-gate 
45330Sstevel@tonic-gate 			/* copy type specific data for the device */
45340Sstevel@tonic-gate 			switch (dsp->d_cm.ci_type) {
45350Sstevel@tonic-gate 
45360Sstevel@tonic-gate 			case SBD_COMP_CPU:
45370Sstevel@tonic-gate 				ds32p->d_cpu.cs_isbootproc =
45380Sstevel@tonic-gate 					(int32_t)dsp->d_cpu.cs_isbootproc;
45390Sstevel@tonic-gate 				ds32p->d_cpu.cs_cpuid =
45400Sstevel@tonic-gate 					(int32_t)dsp->d_cpu.cs_cpuid;
45410Sstevel@tonic-gate 				ds32p->d_cpu.cs_speed =
45420Sstevel@tonic-gate 					(int32_t)dsp->d_cpu.cs_speed;
45430Sstevel@tonic-gate 				ds32p->d_cpu.cs_ecache =
45440Sstevel@tonic-gate 					(int32_t)dsp->d_cpu.cs_ecache;
45450Sstevel@tonic-gate 				break;
45460Sstevel@tonic-gate 
45470Sstevel@tonic-gate 			case SBD_COMP_MEM:
45480Sstevel@tonic-gate 				ds32p->d_mem.ms_type =
45490Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_type;
45500Sstevel@tonic-gate 				ds32p->d_mem.ms_ostate =
45510Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_ostate;
45520Sstevel@tonic-gate 				ds32p->d_mem.ms_cond =
45530Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_cond;
45540Sstevel@tonic-gate 				ds32p->d_mem.ms_interleave =
45550Sstevel@tonic-gate 					(uint32_t)dsp->d_mem.ms_interleave;
45560Sstevel@tonic-gate 				ds32p->d_mem.ms_basepfn =
45570Sstevel@tonic-gate 					(uint32_t)dsp->d_mem.ms_basepfn;
45580Sstevel@tonic-gate 				ds32p->d_mem.ms_totpages =
45590Sstevel@tonic-gate 					(uint32_t)dsp->d_mem.ms_totpages;
45600Sstevel@tonic-gate 				ds32p->d_mem.ms_detpages =
45610Sstevel@tonic-gate 					(uint32_t)dsp->d_mem.ms_detpages;
45620Sstevel@tonic-gate 				ds32p->d_mem.ms_pageslost =
45630Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_pageslost;
45640Sstevel@tonic-gate 				ds32p->d_mem.ms_managed_pages =
45650Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_managed_pages;
45660Sstevel@tonic-gate 				ds32p->d_mem.ms_noreloc_pages =
45670Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_noreloc_pages;
45680Sstevel@tonic-gate 				ds32p->d_mem.ms_noreloc_first =
45690Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_noreloc_first;
45700Sstevel@tonic-gate 				ds32p->d_mem.ms_noreloc_last =
45710Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_noreloc_last;
45720Sstevel@tonic-gate 				ds32p->d_mem.ms_cage_enabled =
45730Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_cage_enabled;
45740Sstevel@tonic-gate 				ds32p->d_mem.ms_peer_is_target =
45750Sstevel@tonic-gate 					(int32_t)dsp->d_mem.ms_peer_is_target;
45760Sstevel@tonic-gate 				(void) strcpy(ds32p->d_mem.ms_peer_ap_id,
45770Sstevel@tonic-gate 					dsp->d_mem.ms_peer_ap_id);
45780Sstevel@tonic-gate 				break;
45790Sstevel@tonic-gate 
45800Sstevel@tonic-gate 
45810Sstevel@tonic-gate 			case SBD_COMP_IO:
45820Sstevel@tonic-gate 
45830Sstevel@tonic-gate 				ds32p->d_io.is_type =
45840Sstevel@tonic-gate 					(int32_t)dsp->d_io.is_type;
45850Sstevel@tonic-gate 				ds32p->d_io.is_unsafe_count =
45860Sstevel@tonic-gate 					(int32_t)dsp->d_io.is_unsafe_count;
45870Sstevel@tonic-gate 				ds32p->d_io.is_referenced =
45880Sstevel@tonic-gate 					(int32_t)dsp->d_io.is_referenced;
45890Sstevel@tonic-gate 				for (j = 0; j < SBD_MAX_UNSAFE; j++)
45900Sstevel@tonic-gate 					ds32p->d_io.is_unsafe_list[j] =
45910Sstevel@tonic-gate 					    (int32_t)
45920Sstevel@tonic-gate 					    ds32p->d_io.is_unsafe_list[j];
45930Sstevel@tonic-gate 				bcopy(dsp->d_io.is_pathname,
45940Sstevel@tonic-gate 				    ds32p->d_io.is_pathname, MAXPATHLEN);
45950Sstevel@tonic-gate 				break;
45960Sstevel@tonic-gate 
45970Sstevel@tonic-gate 			case SBD_COMP_CMP:
45980Sstevel@tonic-gate 				/* copy sbd_cmp_stat_t structure members */
45990Sstevel@tonic-gate 				bcopy(&dsp->d_cmp.ps_cpuid[0],
46000Sstevel@tonic-gate 					&ds32p->d_cmp.ps_cpuid[0],
46010Sstevel@tonic-gate 					sizeof (ds32p->d_cmp.ps_cpuid));
46020Sstevel@tonic-gate 				ds32p->d_cmp.ps_ncores =
46030Sstevel@tonic-gate 					(int32_t)dsp->d_cmp.ps_ncores;
46040Sstevel@tonic-gate 				ds32p->d_cmp.ps_speed =
46050Sstevel@tonic-gate 					(int32_t)dsp->d_cmp.ps_speed;
46060Sstevel@tonic-gate 				ds32p->d_cmp.ps_ecache =
46070Sstevel@tonic-gate 					(int32_t)dsp->d_cmp.ps_ecache;
46080Sstevel@tonic-gate 				break;
46090Sstevel@tonic-gate 
46100Sstevel@tonic-gate 			default:
46110Sstevel@tonic-gate 				cmn_err(CE_WARN,
46120Sstevel@tonic-gate 				    "sbd:%s: unknown dev type (%d)", f,
46130Sstevel@tonic-gate 				    (int)dsp->d_cm.c_id.c_type);
46140Sstevel@tonic-gate 				break;
46150Sstevel@tonic-gate 			}
46160Sstevel@tonic-gate 		}
46170Sstevel@tonic-gate 
46180Sstevel@tonic-gate 		if (ddi_copyout((void *)dstat32p,
46190Sstevel@tonic-gate 		    cmdp->cmd_stat.s_statp, sz32, mode) != 0) {
46200Sstevel@tonic-gate 			cmn_err(CE_WARN,
46210Sstevel@tonic-gate 				"sbd:%s: failed to copyout status "
46220Sstevel@tonic-gate 				"for board %d", f, sbp->sb_num);
46230Sstevel@tonic-gate 			SBD_SET_ERRNO(SBD_HD2ERR(hp), EFAULT);
46240Sstevel@tonic-gate 		}
46250Sstevel@tonic-gate 	} else
46260Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
46270Sstevel@tonic-gate 	if (ddi_copyout((void *)dstatp, cmdp->cmd_stat.s_statp,
46280Sstevel@tonic-gate 	    sz, mode) != 0) {
46290Sstevel@tonic-gate 		cmn_err(CE_WARN,
46300Sstevel@tonic-gate 			"sbd:%s: failed to copyout status for board %d",
46310Sstevel@tonic-gate 			f, sbp->sb_num);
46320Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), EFAULT);
46330Sstevel@tonic-gate 	}
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
46360Sstevel@tonic-gate 	if (sz32 != 0)
46370Sstevel@tonic-gate 		kmem_free(dstat32p, sz32);
46380Sstevel@tonic-gate #endif
46390Sstevel@tonic-gate 	kmem_free(dstatp, sz);
46400Sstevel@tonic-gate }
46410Sstevel@tonic-gate 
46420Sstevel@tonic-gate /*
46430Sstevel@tonic-gate  * Called at driver load time to determine the state and condition
46440Sstevel@tonic-gate  * of an existing board in the system.
46450Sstevel@tonic-gate  */
46460Sstevel@tonic-gate static void
sbd_board_discovery(sbd_board_t * sbp)46470Sstevel@tonic-gate sbd_board_discovery(sbd_board_t *sbp)
46480Sstevel@tonic-gate {
46490Sstevel@tonic-gate 	int		i;
46500Sstevel@tonic-gate 	dev_info_t	*dip;
46510Sstevel@tonic-gate 	sbd_devset_t	devs_lost, devs_attached = 0;
46520Sstevel@tonic-gate 	extern kmutex_t	cpu_lock;
46530Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
46540Sstevel@tonic-gate 	static fn_t	f = "sbd_board_discovery";
46550Sstevel@tonic-gate 	sbderror_t	error, *ep;
46560Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
46570Sstevel@tonic-gate 
46580Sstevel@tonic-gate 	if (SBD_DEVS_PRESENT(sbp) == 0) {
46590Sstevel@tonic-gate 		PR_ALL("%s: board %d has no devices present\n",
46600Sstevel@tonic-gate 			f, sbp->sb_num);
46610Sstevel@tonic-gate 		return;
46620Sstevel@tonic-gate 	}
46630Sstevel@tonic-gate 
46640Sstevel@tonic-gate 	ep = &error;
46650Sstevel@tonic-gate 	bzero(ep, sizeof (sbderror_t));
46660Sstevel@tonic-gate 
46670Sstevel@tonic-gate 	/*
46680Sstevel@tonic-gate 	 * Check for existence of cpus.
46690Sstevel@tonic-gate 	 */
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
46740Sstevel@tonic-gate 		processorid_t	cpuid;
46750Sstevel@tonic-gate 
46760Sstevel@tonic-gate 		if (!SBD_DEV_IS_PRESENT(sbp, SBD_COMP_CPU, i))
46770Sstevel@tonic-gate 			continue;
46780Sstevel@tonic-gate 
46790Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_CPU)][i];
46800Sstevel@tonic-gate 
46810Sstevel@tonic-gate 		if (dip != NULL) {
46820Sstevel@tonic-gate 			cpuid = sbdp_get_cpuid(hdp, dip);
46830Sstevel@tonic-gate 
46840Sstevel@tonic-gate 			if (cpuid < 0) {
46850Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err,
46860Sstevel@tonic-gate 				    ep);
46870Sstevel@tonic-gate 				continue;
46880Sstevel@tonic-gate 			}
46890Sstevel@tonic-gate 
46900Sstevel@tonic-gate 			mutex_enter(&cpu_lock);	/* needed to call cpu_get() */
46910Sstevel@tonic-gate 			if (cpu_get(cpuid)) {
46920Sstevel@tonic-gate 				SBD_DEV_SET_ATTACHED(sbp, SBD_COMP_CPU, i);
46930Sstevel@tonic-gate 				DEVSET_ADD(devs_attached, SBD_COMP_CPU, i);
46940Sstevel@tonic-gate 				PR_ALL("%s: board %d, cpuid %d - attached\n",
46950Sstevel@tonic-gate 					f, sbp->sb_num, cpuid);
46960Sstevel@tonic-gate 			}
46970Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
46980Sstevel@tonic-gate 			sbd_init_cpu_unit(sbp, i);
46990Sstevel@tonic-gate 		}
47000Sstevel@tonic-gate 	}
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	/*
47030Sstevel@tonic-gate 	 * Check for existence of memory.
47040Sstevel@tonic-gate 	 */
47050Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
47060Sstevel@tonic-gate 		uint64_t	basepa, endpa;
47070Sstevel@tonic-gate 		struct memlist	*ml;
47080Sstevel@tonic-gate 		extern struct memlist	*phys_install;
47090Sstevel@tonic-gate 
47100Sstevel@tonic-gate 		if (!SBD_DEV_IS_PRESENT(sbp, SBD_COMP_MEM, i))
47110Sstevel@tonic-gate 			continue;
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_MEM)][i];
47140Sstevel@tonic-gate 		if (dip == NULL)
47150Sstevel@tonic-gate 			continue;
47160Sstevel@tonic-gate 
47170Sstevel@tonic-gate 		if (sbdphw_get_base_physaddr(hdp, dip, &basepa)) {
47180Sstevel@tonic-gate 			/* omit phantom memory controllers on I/O boards */
47190Sstevel@tonic-gate 			if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_MEM, i)) {
47200Sstevel@tonic-gate 				ASSERT(sbp->sb_ndev != 0);
47210Sstevel@tonic-gate 				SBD_DEV_CLR_PRESENT(sbp, SBD_COMP_MEM, i);
47220Sstevel@tonic-gate 				sbp->sb_ndev--;
47230Sstevel@tonic-gate 			}
47240Sstevel@tonic-gate 			sbp->sb_devlist[NIX(SBD_COMP_MEM)][i] = NULL;
47250Sstevel@tonic-gate 			continue;
47260Sstevel@tonic-gate 		}
47270Sstevel@tonic-gate 
47280Sstevel@tonic-gate 		/*
47290Sstevel@tonic-gate 		 * basepa may not be on a alignment boundary, make it so.
47300Sstevel@tonic-gate 		 */
47310Sstevel@tonic-gate 		if (sbdp_get_mem_alignment(hdp, dip, &endpa)) {
47320Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s sbdp_get_mem_alignment fail", f);
47330Sstevel@tonic-gate 			continue;
47340Sstevel@tonic-gate 		}
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 		basepa &= ~(endpa - 1);
47370Sstevel@tonic-gate 		endpa += basepa;
47380Sstevel@tonic-gate 
47390Sstevel@tonic-gate 		/*
47400Sstevel@tonic-gate 		 * Check if base address is in phys_install.
47410Sstevel@tonic-gate 		 */
47420Sstevel@tonic-gate 		memlist_read_lock();
4743*11474SJonathan.Adams@Sun.COM 		for (ml = phys_install; ml; ml = ml->ml_next)
4744*11474SJonathan.Adams@Sun.COM 			if ((endpa <= ml->ml_address) ||
4745*11474SJonathan.Adams@Sun.COM 			    (basepa >= (ml->ml_address + ml->ml_size)))
47460Sstevel@tonic-gate 				continue;
47470Sstevel@tonic-gate 			else
47480Sstevel@tonic-gate 				break;
47490Sstevel@tonic-gate 		memlist_read_unlock();
47500Sstevel@tonic-gate 
47510Sstevel@tonic-gate 		if (ml) {
47520Sstevel@tonic-gate 			SBD_DEV_SET_ATTACHED(sbp, SBD_COMP_MEM, i);
47530Sstevel@tonic-gate 			DEVSET_ADD(devs_attached, SBD_COMP_MEM, i);
47540Sstevel@tonic-gate 			PR_ALL("%s: board %d, mem-unit %d - attached\n",
47550Sstevel@tonic-gate 				f, sbp->sb_num, i);
47560Sstevel@tonic-gate 		}
47570Sstevel@tonic-gate 		sbd_init_mem_unit(sbp, i, ep);
47580Sstevel@tonic-gate 	}
47590Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 	/*
47620Sstevel@tonic-gate 	 * If so far we have found an error, we just log it but continue
47630Sstevel@tonic-gate 	 */
47640Sstevel@tonic-gate 	if (SBD_GET_ERRNO(ep) != 0)
47650Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s errno has occurred: errno %d", f,
47660Sstevel@tonic-gate 			SBD_GET_ERRNO(ep));
47670Sstevel@tonic-gate 
47680Sstevel@tonic-gate 	/*
47690Sstevel@tonic-gate 	 * Check for i/o state.
47700Sstevel@tonic-gate 	 */
47710Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
47720Sstevel@tonic-gate 
47730Sstevel@tonic-gate 		if (!SBD_DEV_IS_PRESENT(sbp, SBD_COMP_IO, i))
47740Sstevel@tonic-gate 			continue;
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_IO)][i];
47770Sstevel@tonic-gate 		if (dip == NULL)
47780Sstevel@tonic-gate 			continue;
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 		ASSERT(e_ddi_branch_held(dip));
47810Sstevel@tonic-gate 
47820Sstevel@tonic-gate 		/*
47830Sstevel@tonic-gate 		 * XXX Is the devstate check needed ?
47840Sstevel@tonic-gate 		 */
47851333Scth 		if (i_ddi_devi_attached(dip) ||
47860Sstevel@tonic-gate 		    ddi_get_devstate(dip) == DDI_DEVSTATE_UP) {
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 			/*
47890Sstevel@tonic-gate 			 * Found it!
47900Sstevel@tonic-gate 			 */
47910Sstevel@tonic-gate 			SBD_DEV_SET_ATTACHED(sbp, SBD_COMP_IO, i);
47920Sstevel@tonic-gate 			DEVSET_ADD(devs_attached, SBD_COMP_IO, i);
47930Sstevel@tonic-gate 			PR_ALL("%s: board %d, io-unit %d - attached\n",
47940Sstevel@tonic-gate 				f, sbp->sb_num, i);
47950Sstevel@tonic-gate 		}
47960Sstevel@tonic-gate 		sbd_init_io_unit(sbp, i);
47970Sstevel@tonic-gate 	}
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 	SBD_DEVS_CONFIGURE(sbp, devs_attached);
48000Sstevel@tonic-gate 	if (devs_attached && ((devs_lost = SBD_DEVS_UNATTACHED(sbp)) != 0)) {
48010Sstevel@tonic-gate 		int		ut;
48020Sstevel@tonic-gate 		/*
48030Sstevel@tonic-gate 		 * A prior comment stated that a partially configured
48040Sstevel@tonic-gate 		 * board was not permitted. The Serengeti architecture
48050Sstevel@tonic-gate 		 * makes this possible, so the SB_DEVS_DISCONNECT
48060Sstevel@tonic-gate 		 * at the end of this block has been removed.
48070Sstevel@tonic-gate 		 */
48080Sstevel@tonic-gate 
48090Sstevel@tonic-gate 		PR_ALL("%s: some devices not configured (0x%x)...\n",
48100Sstevel@tonic-gate 			f, devs_lost);
48110Sstevel@tonic-gate 
48120Sstevel@tonic-gate 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++)
48130Sstevel@tonic-gate 			if (DEVSET_IN_SET(devs_lost, SBD_COMP_CPU, ut)) {
48140Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_CPU,
48150Sstevel@tonic-gate 					ut, SBD_STATE_UNCONFIGURED);
48160Sstevel@tonic-gate 			}
48170Sstevel@tonic-gate 
48180Sstevel@tonic-gate 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++)
48190Sstevel@tonic-gate 			if (DEVSET_IN_SET(devs_lost, SBD_COMP_MEM, ut)) {
48200Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM,
48210Sstevel@tonic-gate 					ut, SBD_STATE_UNCONFIGURED);
48220Sstevel@tonic-gate 			}
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++)
48250Sstevel@tonic-gate 			if (DEVSET_IN_SET(devs_lost, SBD_COMP_IO, ut)) {
48260Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(sbp, SBD_COMP_IO,
48270Sstevel@tonic-gate 					ut, SBD_STATE_UNCONFIGURED);
48280Sstevel@tonic-gate 			}
48290Sstevel@tonic-gate 	}
48300Sstevel@tonic-gate }
48310Sstevel@tonic-gate 
48320Sstevel@tonic-gate static int
hold_rele_branch(dev_info_t * rdip,void * arg)48330Sstevel@tonic-gate hold_rele_branch(dev_info_t *rdip, void *arg)
48340Sstevel@tonic-gate {
48350Sstevel@tonic-gate 	walk_tree_t	*wp = (walk_tree_t *)arg;
48360Sstevel@tonic-gate 
48370Sstevel@tonic-gate 	ASSERT(wp && (wp->hold == 0 || wp->hold == 1));
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	switch (get_node_type(wp->sbp, rdip, NULL)) {
48400Sstevel@tonic-gate 		case SBD_COMP_CMP:
48410Sstevel@tonic-gate 		case SBD_COMP_MEM:
48420Sstevel@tonic-gate 		case SBD_COMP_IO:
48430Sstevel@tonic-gate 			break;
48440Sstevel@tonic-gate 		case SBD_COMP_CPU:
48450Sstevel@tonic-gate 
48460Sstevel@tonic-gate 			/*
48470Sstevel@tonic-gate 			 * All CPU nodes under CMP nodes should have
48480Sstevel@tonic-gate 			 * gotten pruned when the CMP node was first
48490Sstevel@tonic-gate 			 * encountered.
48500Sstevel@tonic-gate 			 */
48510Sstevel@tonic-gate 			ASSERT(!sbd_is_cmp_child(rdip));
48520Sstevel@tonic-gate 
48530Sstevel@tonic-gate 			break;
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 		case SBD_COMP_UNKNOWN:
48560Sstevel@tonic-gate 			/* Not of interest to us */
48570Sstevel@tonic-gate 			return (DDI_WALK_CONTINUE);
48580Sstevel@tonic-gate 		default:
48590Sstevel@tonic-gate 			ASSERT(0);
48600Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
48610Sstevel@tonic-gate 	}
48620Sstevel@tonic-gate 
48630Sstevel@tonic-gate 	if (wp->hold) {
48640Sstevel@tonic-gate 		ASSERT(!e_ddi_branch_held(rdip));
48650Sstevel@tonic-gate 		e_ddi_branch_hold(rdip);
48660Sstevel@tonic-gate 	} else {
48670Sstevel@tonic-gate 		ASSERT(e_ddi_branch_held(rdip));
48680Sstevel@tonic-gate 		e_ddi_branch_rele(rdip);
48690Sstevel@tonic-gate 	}
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
48720Sstevel@tonic-gate }
48730Sstevel@tonic-gate 
48740Sstevel@tonic-gate static void
sbd_board_init(sbd_board_t * sbp,sbd_softstate_t * softsp,int bd,dev_info_t * top_dip,int wnode)48750Sstevel@tonic-gate sbd_board_init(sbd_board_t *sbp, sbd_softstate_t *softsp,
48760Sstevel@tonic-gate 	int bd, dev_info_t *top_dip, int wnode)
48770Sstevel@tonic-gate {
48780Sstevel@tonic-gate 	int		i;
48790Sstevel@tonic-gate 	dev_info_t	*pdip;
48800Sstevel@tonic-gate 	int		circ;
48810Sstevel@tonic-gate 	walk_tree_t	walk = {0};
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate 	mutex_init(&sbp->sb_mutex, NULL, MUTEX_DRIVER, NULL);
48840Sstevel@tonic-gate 	mutex_init(&sbp->sb_flags_mutex, NULL, MUTEX_DRIVER, NULL);
48850Sstevel@tonic-gate 	mutex_init(&sbp->sb_slock, NULL, MUTEX_DRIVER, NULL);
48860Sstevel@tonic-gate 
48870Sstevel@tonic-gate 	sbp->sb_ref = 0;
48880Sstevel@tonic-gate 	sbp->sb_num = bd;
48890Sstevel@tonic-gate 	sbp->sb_time = gethrestime_sec();
48900Sstevel@tonic-gate 	/*
48910Sstevel@tonic-gate 	 * For serengeti, top_dip doesn't need to be held because
48920Sstevel@tonic-gate 	 * sbp i.e. sbd_board_t will be destroyed in sbd_teardown_instance()
48930Sstevel@tonic-gate 	 * before top_dip detaches. For Daktari, top_dip is the
48940Sstevel@tonic-gate 	 * root node which never has to be held.
48950Sstevel@tonic-gate 	 */
48960Sstevel@tonic-gate 	sbp->sb_topdip = top_dip;
48970Sstevel@tonic-gate 	sbp->sb_cpuid = -1;
48980Sstevel@tonic-gate 	sbp->sb_softsp = (void *) softsp;
48990Sstevel@tonic-gate 	sbp->sb_cond = SBD_COND_UNKNOWN;
49000Sstevel@tonic-gate 	sbp->sb_wnode = wnode;
49010Sstevel@tonic-gate 	sbp->sb_memaccess_ok = 1;
49020Sstevel@tonic-gate 
49030Sstevel@tonic-gate 	ASSERT(MAX_IO_UNITS_PER_BOARD <= SBD_MAX_UNITS_PER_BOARD);
49040Sstevel@tonic-gate 	ASSERT(MAX_CPU_UNITS_PER_BOARD <= SBD_MAX_UNITS_PER_BOARD);
49050Sstevel@tonic-gate 	ASSERT(MAX_MEM_UNITS_PER_BOARD <= SBD_MAX_UNITS_PER_BOARD);
49060Sstevel@tonic-gate 
49070Sstevel@tonic-gate 	/*
49080Sstevel@tonic-gate 	 * Allocate the devlist for cpus.
49090Sstevel@tonic-gate 	 */
49100Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_CPU)] = GETSTRUCT(dev_info_t *,
49110Sstevel@tonic-gate 						MAX_CPU_UNITS_PER_BOARD);
49120Sstevel@tonic-gate 
49130Sstevel@tonic-gate 	/*
49140Sstevel@tonic-gate 	 * Allocate the devlist for mem.
49150Sstevel@tonic-gate 	 */
49160Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_MEM)] = GETSTRUCT(dev_info_t *,
49170Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD);
49180Sstevel@tonic-gate 
49190Sstevel@tonic-gate 	/*
49200Sstevel@tonic-gate 	 * Allocate the devlist for io.
49210Sstevel@tonic-gate 	 */
49220Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_IO)] = GETSTRUCT(dev_info_t *,
49230Sstevel@tonic-gate 						MAX_IO_UNITS_PER_BOARD);
49240Sstevel@tonic-gate 
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_CPU)] = GETSTRUCT(sbd_dev_unit_t,
49270Sstevel@tonic-gate 						MAX_CPU_UNITS_PER_BOARD);
49280Sstevel@tonic-gate 
49290Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_MEM)] = GETSTRUCT(sbd_dev_unit_t,
49300Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD);
49310Sstevel@tonic-gate 
49320Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_IO)] = GETSTRUCT(sbd_dev_unit_t,
49330Sstevel@tonic-gate 						MAX_IO_UNITS_PER_BOARD);
49340Sstevel@tonic-gate 
49350Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
49360Sstevel@tonic-gate 		sbp->sb_cpupath[i] = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
49370Sstevel@tonic-gate 	}
49380Sstevel@tonic-gate 
49390Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
49400Sstevel@tonic-gate 		sbp->sb_mempath[i] = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
49410Sstevel@tonic-gate 	}
49420Sstevel@tonic-gate 
49430Sstevel@tonic-gate 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
49440Sstevel@tonic-gate 		sbp->sb_iopath[i] = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
49450Sstevel@tonic-gate 	}
49460Sstevel@tonic-gate 
49470Sstevel@tonic-gate 	/*
49480Sstevel@tonic-gate 	 * Walk the device tree, find all top dips on this board and
49490Sstevel@tonic-gate 	 * hold the branches rooted at them
49500Sstevel@tonic-gate 	 */
49510Sstevel@tonic-gate 	ASSERT(sbp->sb_topdip);
49520Sstevel@tonic-gate 	pdip = ddi_get_parent(sbp->sb_topdip);
49530Sstevel@tonic-gate 	if (pdip)
49540Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
49550Sstevel@tonic-gate 	walk.sbp = sbp;
49560Sstevel@tonic-gate 	walk.hold = 1;
49570Sstevel@tonic-gate 	ddi_walk_devs(sbp->sb_topdip, hold_rele_branch, (void *)&walk);
49580Sstevel@tonic-gate 	if (pdip)
49590Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
49600Sstevel@tonic-gate 
49610Sstevel@tonic-gate 	/*
49620Sstevel@tonic-gate 	 * Initialize the devlists
49630Sstevel@tonic-gate 	 */
49640Sstevel@tonic-gate 	if (sbd_init_devlists(sbp) == 0) {
49650Sstevel@tonic-gate 		SBD_BOARD_TRANSITION(sbp, SBD_STATE_EMPTY);
49660Sstevel@tonic-gate 	} else {
49670Sstevel@tonic-gate 		/*
49680Sstevel@tonic-gate 		 * Couldn't have made it down here without
49690Sstevel@tonic-gate 		 * having found at least one device.
49700Sstevel@tonic-gate 		 */
49710Sstevel@tonic-gate 		ASSERT(SBD_DEVS_PRESENT(sbp) != 0);
49720Sstevel@tonic-gate 		/*
49730Sstevel@tonic-gate 		 * Check the state of any possible devices on the
49740Sstevel@tonic-gate 		 * board.
49750Sstevel@tonic-gate 		 */
49760Sstevel@tonic-gate 		sbd_board_discovery(sbp);
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate 		if (SBD_DEVS_UNATTACHED(sbp) == 0) {
49790Sstevel@tonic-gate 			/*
49800Sstevel@tonic-gate 			 * The board has no unattached devices, therefore
49810Sstevel@tonic-gate 			 * by reason of insanity it must be configured!
49820Sstevel@tonic-gate 			 */
49830Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_CONFIGURED);
49840Sstevel@tonic-gate 			sbp->sb_cond = SBD_COND_OK;
49850Sstevel@tonic-gate 		} else if (SBD_DEVS_ATTACHED(sbp)) {
49860Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_PARTIAL);
49870Sstevel@tonic-gate 		} else {
49880Sstevel@tonic-gate 			SBD_BOARD_TRANSITION(sbp, SBD_STATE_CONNECTED);
49890Sstevel@tonic-gate 		}
49900Sstevel@tonic-gate 	}
49910Sstevel@tonic-gate }
49920Sstevel@tonic-gate 
49930Sstevel@tonic-gate static void
sbd_board_destroy(sbd_board_t * sbp)49940Sstevel@tonic-gate sbd_board_destroy(sbd_board_t *sbp)
49950Sstevel@tonic-gate {
49960Sstevel@tonic-gate 	int		i;
49970Sstevel@tonic-gate 	dev_info_t	*pdip;
49980Sstevel@tonic-gate 	int		circ;
49990Sstevel@tonic-gate 	walk_tree_t	walk = {0};
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	SBD_BOARD_TRANSITION(sbp, SBD_STATE_EMPTY);
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate #ifdef DEBUG
50040Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
50050Sstevel@tonic-gate 		sbd_mem_unit_t *mp;
50060Sstevel@tonic-gate 
50070Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, i);
50080Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist == NULL);
50090Sstevel@tonic-gate 	}
50100Sstevel@tonic-gate #endif /* DEBUG */
50110Sstevel@tonic-gate 
50120Sstevel@tonic-gate 	/*
50130Sstevel@tonic-gate 	 * Free up MEM unit structs.
50140Sstevel@tonic-gate 	 */
50150Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_dev[NIX(SBD_COMP_MEM)],
50160Sstevel@tonic-gate 			sbd_dev_unit_t, MAX_MEM_UNITS_PER_BOARD);
50170Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_MEM)] = NULL;
50180Sstevel@tonic-gate 
50190Sstevel@tonic-gate 	/*
50200Sstevel@tonic-gate 	 * Free up CPU unit structs.
50210Sstevel@tonic-gate 	 */
50220Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_dev[NIX(SBD_COMP_CPU)],
50230Sstevel@tonic-gate 			sbd_dev_unit_t, MAX_CPU_UNITS_PER_BOARD);
50240Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_CPU)] = NULL;
50250Sstevel@tonic-gate 
50260Sstevel@tonic-gate 	/*
50270Sstevel@tonic-gate 	 * Free up IO unit structs.
50280Sstevel@tonic-gate 	 */
50290Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_dev[NIX(SBD_COMP_IO)],
50300Sstevel@tonic-gate 			sbd_dev_unit_t, MAX_IO_UNITS_PER_BOARD);
50310Sstevel@tonic-gate 	sbp->sb_dev[NIX(SBD_COMP_IO)] = NULL;
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	/*
50340Sstevel@tonic-gate 	 * free up CPU devlists.
50350Sstevel@tonic-gate 	 */
50360Sstevel@tonic-gate 
50370Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
50380Sstevel@tonic-gate 		kmem_free((caddr_t)sbp->sb_cpupath[i], MAXPATHLEN);
50390Sstevel@tonic-gate 	}
50400Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_devlist[NIX(SBD_COMP_CPU)], dev_info_t *,
50410Sstevel@tonic-gate 		MAX_CPU_UNITS_PER_BOARD);
50420Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_CPU)] = NULL;
50430Sstevel@tonic-gate 
50440Sstevel@tonic-gate 	/*
50450Sstevel@tonic-gate 	 * free up MEM devlists.
50460Sstevel@tonic-gate 	 */
50470Sstevel@tonic-gate 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
50480Sstevel@tonic-gate 		kmem_free((caddr_t)sbp->sb_mempath[i], MAXPATHLEN);
50490Sstevel@tonic-gate 	}
50500Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_devlist[NIX(SBD_COMP_MEM)], dev_info_t *,
50510Sstevel@tonic-gate 		MAX_MEM_UNITS_PER_BOARD);
50520Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_MEM)] = NULL;
50530Sstevel@tonic-gate 
50540Sstevel@tonic-gate 	/*
50550Sstevel@tonic-gate 	 * free up IO devlists.
50560Sstevel@tonic-gate 	 */
50570Sstevel@tonic-gate 	for (i = 0; i <  MAX_IO_UNITS_PER_BOARD; i++) {
50580Sstevel@tonic-gate 		kmem_free((caddr_t)sbp->sb_iopath[i], MAXPATHLEN);
50590Sstevel@tonic-gate 	}
50600Sstevel@tonic-gate 	FREESTRUCT(sbp->sb_devlist[NIX(SBD_COMP_IO)], dev_info_t *,
50610Sstevel@tonic-gate 		MAX_IO_UNITS_PER_BOARD);
50620Sstevel@tonic-gate 	sbp->sb_devlist[NIX(SBD_COMP_IO)] = NULL;
50630Sstevel@tonic-gate 
50640Sstevel@tonic-gate 	/*
50650Sstevel@tonic-gate 	 * Release all branches held earlier
50660Sstevel@tonic-gate 	 */
50670Sstevel@tonic-gate 	ASSERT(sbp->sb_topdip);
50680Sstevel@tonic-gate 	pdip = ddi_get_parent(sbp->sb_topdip);
50690Sstevel@tonic-gate 	if (pdip)
50700Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
50710Sstevel@tonic-gate 	walk.sbp = sbp;
50720Sstevel@tonic-gate 	walk.hold = 0;
50730Sstevel@tonic-gate 	ddi_walk_devs(sbp->sb_topdip, hold_rele_branch, (void *)&walk);
50740Sstevel@tonic-gate 	if (pdip)
50750Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 	mutex_destroy(&sbp->sb_slock);
50780Sstevel@tonic-gate 	mutex_destroy(&sbp->sb_flags_mutex);
50790Sstevel@tonic-gate 	mutex_destroy(&sbp->sb_mutex);
50800Sstevel@tonic-gate }
50810Sstevel@tonic-gate 
50820Sstevel@tonic-gate sbd_comp_type_t
sbd_cm_type(char * name)50830Sstevel@tonic-gate sbd_cm_type(char *name)
50840Sstevel@tonic-gate {
50850Sstevel@tonic-gate 	sbd_comp_type_t type = SBD_COMP_UNKNOWN;
50860Sstevel@tonic-gate 	int i;
50870Sstevel@tonic-gate 
50880Sstevel@tonic-gate 	/* look up type in table */
50890Sstevel@tonic-gate 	for (i = 0; SBD_COMP(i) != SBD_COMP_UNKNOWN; i++) {
50900Sstevel@tonic-gate 		if (strcmp(name, SBD_OTYPE(i)) == 0) {
50910Sstevel@tonic-gate 			type = SBD_COMP(i);
50920Sstevel@tonic-gate 			break;
50930Sstevel@tonic-gate 		}
50940Sstevel@tonic-gate 	}
50950Sstevel@tonic-gate 
50960Sstevel@tonic-gate 	return (type);
50970Sstevel@tonic-gate }
50980Sstevel@tonic-gate 
50990Sstevel@tonic-gate /*
51000Sstevel@tonic-gate  * There are certain cases where obp marks components as failed
51010Sstevel@tonic-gate  * If the status is ok the node won't have any status property. It
51020Sstevel@tonic-gate  * is only there if the status is other than ok.
51030Sstevel@tonic-gate  *
51040Sstevel@tonic-gate  * The translation is as follows:
51050Sstevel@tonic-gate  * If there is no status prop, the the cond is SBD_COND_OK
51060Sstevel@tonic-gate  * If we find a status prop but can't get to it then cond is SBD_COND_UNKNOWN
51070Sstevel@tonic-gate  * if we find a stat and it is failed the cond is SBD_COND_FAILED
51080Sstevel@tonic-gate  * If the stat is disabled, the cond is SBD_COND_UNUSABLE
51090Sstevel@tonic-gate  * Otherwise we return con as SBD_COND_OK
51100Sstevel@tonic-gate  */
51110Sstevel@tonic-gate sbd_cond_t
sbd_get_comp_cond(dev_info_t * dip)51120Sstevel@tonic-gate sbd_get_comp_cond(dev_info_t *dip)
51130Sstevel@tonic-gate {
51140Sstevel@tonic-gate 	int			len;
51150Sstevel@tonic-gate 	char			*status_buf;
51160Sstevel@tonic-gate 	static const char	*status = "status";
51170Sstevel@tonic-gate 	static const char	*failed = "fail";
51180Sstevel@tonic-gate 	static const char	*disabled = "disabled";
51190Sstevel@tonic-gate 
51200Sstevel@tonic-gate 	if (dip == NULL) {
51210Sstevel@tonic-gate 		PR_BYP("dip is NULL\n");
51220Sstevel@tonic-gate 		return (SBD_COND_UNKNOWN);
51230Sstevel@tonic-gate 	}
51240Sstevel@tonic-gate 
51254845Svikram 	/*
51264845Svikram 	 * If retired, return FAILED
51274845Svikram 	 */
51284845Svikram 	if (DEVI(dip)->devi_flags & DEVI_RETIRED) {
51294845Svikram 		PR_CPU("dip is retired\n");
51304845Svikram 		return (SBD_COND_FAILED);
51314845Svikram 	}
51324845Svikram 
51330Sstevel@tonic-gate 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
51340Sstevel@tonic-gate 	    (char *)status, &len) != DDI_PROP_SUCCESS) {
51350Sstevel@tonic-gate 		PR_CPU("status in sbd is ok\n");
51360Sstevel@tonic-gate 		return (SBD_COND_OK);
51370Sstevel@tonic-gate 	}
51380Sstevel@tonic-gate 
51390Sstevel@tonic-gate 	status_buf = kmem_zalloc(sizeof (char) * OBP_MAXPROPNAME, KM_SLEEP);
5140506Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
51410Sstevel@tonic-gate 	    (char *)status, status_buf, &len) != DDI_PROP_SUCCESS) {
51420Sstevel@tonic-gate 		PR_CPU("status in sbd is unknown\n");
51430Sstevel@tonic-gate 		return (SBD_COND_UNKNOWN);
51440Sstevel@tonic-gate 	}
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate 	if (strncmp(status_buf, failed, strlen(failed)) == 0) {
51470Sstevel@tonic-gate 		PR_CPU("status in sbd is failed\n");
51480Sstevel@tonic-gate 		kmem_free(status_buf, sizeof (char) * OBP_MAXPROPNAME);
51490Sstevel@tonic-gate 		return (SBD_COND_FAILED);
51500Sstevel@tonic-gate 	}
51510Sstevel@tonic-gate 
51520Sstevel@tonic-gate 	if (strcmp(status_buf, disabled) == 0) {
51530Sstevel@tonic-gate 		PR_CPU("status in sbd is unusable\n");
51540Sstevel@tonic-gate 		kmem_free(status_buf, sizeof (char) * OBP_MAXPROPNAME);
51550Sstevel@tonic-gate 		return (SBD_COND_UNUSABLE);
51560Sstevel@tonic-gate 	}
51570Sstevel@tonic-gate 
51580Sstevel@tonic-gate 	kmem_free(status_buf, sizeof (char) * OBP_MAXPROPNAME);
51590Sstevel@tonic-gate 	return (SBD_COND_OK);
51600Sstevel@tonic-gate }
51610Sstevel@tonic-gate 
51620Sstevel@tonic-gate #ifdef SBD_DEBUG_ERRS
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate /* function to simulate errors throughout the sbd code */
51650Sstevel@tonic-gate void
sbd_inject_err(int error,sbderror_t * ep,int Errno,int ecode,char * rsc)51660Sstevel@tonic-gate sbd_inject_err(int error, sbderror_t *ep, int Errno, int ecode,
51670Sstevel@tonic-gate 	char *rsc)
51680Sstevel@tonic-gate {
51690Sstevel@tonic-gate 	static fn_t	f = "sbd_inject_err";
51700Sstevel@tonic-gate 
51710Sstevel@tonic-gate 	if (sbd_err_debug == 0)
51720Sstevel@tonic-gate 		return;
51730Sstevel@tonic-gate 
51740Sstevel@tonic-gate 	if (ep == NULL) {
51750Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ep is NULL", f);
51760Sstevel@tonic-gate 		return;
51770Sstevel@tonic-gate 	}
51780Sstevel@tonic-gate 
51790Sstevel@tonic-gate 	if (SBD_GET_ERRNO(ep) != 0) {
51800Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s errno already set to %d", f,
51810Sstevel@tonic-gate 			SBD_GET_ERRNO(ep));
51820Sstevel@tonic-gate 		return;
51830Sstevel@tonic-gate 	}
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate 	if (SBD_GET_ERR(ep) != 0) {
51860Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s code already set to %d", f,
51870Sstevel@tonic-gate 			SBD_GET_ERR(ep));
51880Sstevel@tonic-gate 		return;
51890Sstevel@tonic-gate 	}
51900Sstevel@tonic-gate 
51910Sstevel@tonic-gate 	if ((sbd_err_debug & (1 << error)) != 0) {
51920Sstevel@tonic-gate 		ep->e_errno = Errno;
51930Sstevel@tonic-gate 		ep->e_code = ecode;
51940Sstevel@tonic-gate 
51950Sstevel@tonic-gate 		if (rsc != NULL)
51960Sstevel@tonic-gate 			bcopy((caddr_t)rsc,
51970Sstevel@tonic-gate 			(caddr_t)ep->e_rsc,
51980Sstevel@tonic-gate 			sizeof (ep->e_rsc));
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate 		if (Errno != 0)
52010Sstevel@tonic-gate 			PR_ERR_ERRNO("%s set errno to %d", f, ep->e_errno);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 		if (ecode != 0)
52040Sstevel@tonic-gate 			PR_ERR_ECODE("%s set ecode to %d", f, ep->e_code);
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 		if (rsc != NULL)
52070Sstevel@tonic-gate 			PR_ERR_RSC("%s set rsc to %s", f, ep->e_rsc);
52080Sstevel@tonic-gate 	}
52090Sstevel@tonic-gate }
52100Sstevel@tonic-gate #endif
5211