xref: /illumos-gate/usr/src/uts/sun4u/io/sbd.c (revision 56f33205)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
525e8c5aaSvikram  * Common Development and Distribution License (the "License").
625e8c5aaSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2107d06da5SSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
23*56f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * safari system board DR module.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
367c478bd9Sstevel@tonic-gate #include <sys/devops.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/poll.h>
397c478bd9Sstevel@tonic-gate #include <sys/conf.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
437c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
487c478bd9Sstevel@tonic-gate #include <sys/mem_cage.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
547c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <sys/sbdpriv.h>
587c478bd9Sstevel@tonic-gate #include <sys/sbd_io.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* start sbd includes */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <sys/systm.h>
637c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
647c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
657c478bd9Sstevel@tonic-gate #include <sys/membar.h>
667c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern int nulldev();
697c478bd9Sstevel@tonic-gate extern int nodev();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate typedef struct {		/* arg to sbd_get_handle */
727c478bd9Sstevel@tonic-gate 	dev_t	dev;
737c478bd9Sstevel@tonic-gate 	int	cmd;
747c478bd9Sstevel@tonic-gate 	int	mode;
757c478bd9Sstevel@tonic-gate 	sbd_ioctl_arg_t *ioargp;
767c478bd9Sstevel@tonic-gate } sbd_init_arg_t;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * sbd support operations.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static void	sbd_exec_op(sbd_handle_t *hp);
837c478bd9Sstevel@tonic-gate static void	sbd_dev_configure(sbd_handle_t *hp);
847c478bd9Sstevel@tonic-gate static int	sbd_dev_release(sbd_handle_t *hp);
857c478bd9Sstevel@tonic-gate static int	sbd_dev_unconfigure(sbd_handle_t *hp);
867c478bd9Sstevel@tonic-gate static void	sbd_attach_cpu(sbd_handle_t *hp, sbderror_t *ep,
877c478bd9Sstevel@tonic-gate 				dev_info_t *dip, int unit);
887c478bd9Sstevel@tonic-gate static void	sbd_detach_cpu(sbd_handle_t *hp, sbderror_t *ep,
897c478bd9Sstevel@tonic-gate 				dev_info_t *dip, int unit);
907c478bd9Sstevel@tonic-gate static int	sbd_detach_mem(sbd_handle_t *hp, sbderror_t *ep, int unit);
917c478bd9Sstevel@tonic-gate static void	sbd_cancel(sbd_handle_t *hp);
927c478bd9Sstevel@tonic-gate void 	sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip);
937c478bd9Sstevel@tonic-gate int		sbd_dealloc_instance(sbd_board_t *sbp, int max_boards);
947c478bd9Sstevel@tonic-gate int		sbd_errno2ecode(int error);
957c478bd9Sstevel@tonic-gate #pragma weak sbdp_cpu_get_impl
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #ifdef DEBUG
987c478bd9Sstevel@tonic-gate uint_t	sbd_debug	=	(uint_t)0x0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #ifdef SBD_DEBUG_ERRS
1017c478bd9Sstevel@tonic-gate /* controls which errors are injected */
1027c478bd9Sstevel@tonic-gate uint_t	sbd_err_debug	=	(uint_t)0x0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* controls printing about error injection */
1057c478bd9Sstevel@tonic-gate uint_t	sbd_print_errs	=	(uint_t)0x0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #endif /* SBD_DEBUG_ERRS */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate char	*sbd_state_str[] = {
1127c478bd9Sstevel@tonic-gate 	"EMPTY", "OCCUPIED", "CONNECTED", "UNCONFIGURED",
1137c478bd9Sstevel@tonic-gate 	"PARTIAL", "CONFIGURED", "RELEASE", "UNREFERENCED",
1147c478bd9Sstevel@tonic-gate 	"FATAL"
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*	Note: this must be changed in tandem with sbd_ioctl.h	*/
1187c478bd9Sstevel@tonic-gate char	*sbd_ct_str[] = {
1197c478bd9Sstevel@tonic-gate 	"NONE", "CPU", "MEM", "IO", "UNKNOWN"
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*	Note: this must also be changed in tandem with sbd_ioctl.h */
1237c478bd9Sstevel@tonic-gate #define	SBD_CMD_STR(c) \
1247c478bd9Sstevel@tonic-gate 	(((c) == SBD_CMD_ASSIGN)	? "ASSIGN"	: \
1257c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_UNASSIGN)	? "UNASSIGN"	: \
1267c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_POWERON)	? "POWERON"	: \
1277c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_POWEROFF)	? "POWEROFF"	: \
1287c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_TEST)		? "TEST"	: \
1297c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_CONNECT)	? "CONNECT"	: \
1307c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_CONFIGURE)	? "CONFIGURE"	: \
1317c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_UNCONFIGURE)	? "UNCONFIGURE"	: \
1327c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_DISCONNECT)	? "DISCONNECT"	: \
1337c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_STATUS)		? "STATUS"	: \
1347c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_GETNCM)		? "GETNCM"	: \
1357c478bd9Sstevel@tonic-gate 	((c) == SBD_CMD_PASSTHRU)	? "PASSTHRU"	: "unknown")
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Defines and structures for device tree naming and mapping
1397c478bd9Sstevel@tonic-gate  * to node types
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate sbd_devattr_t *sbd_devattr;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* defines to access the attribute struct */
1457c478bd9Sstevel@tonic-gate #define	SBD_DEVNAME(i)		sbd_devattr[i].s_devname
1467c478bd9Sstevel@tonic-gate #define	SBD_OTYPE(i)		sbd_devattr[(i)].s_obp_type
1477c478bd9Sstevel@tonic-gate #define	SBD_COMP(i)		sbd_devattr[i].s_dnodetype
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * State transition table.  States valid transitions for "board" state.
1517c478bd9Sstevel@tonic-gate  * Recall that non-zero return value terminates operation, however
1527c478bd9Sstevel@tonic-gate  * the herrno value is what really indicates an error , if any.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate static int
_cmd2index(int c)1557c478bd9Sstevel@tonic-gate _cmd2index(int c)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * Translate DR CMD to index into sbd_state_transition.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	switch (c) {
1617c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:		return (0);
1627c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:	return (1);
1637c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:		return (2);
1647c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:	return (3);
1657c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:		return (4);
1667c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWERON:		return (5);
1677c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:		return (6);
1687c478bd9Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:		return (7);
1697c478bd9Sstevel@tonic-gate 	case SBD_CMD_TEST:		return (8);
1707c478bd9Sstevel@tonic-gate 	default:			return (-1);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	CMD2INDEX(c)	_cmd2index(c)
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static struct sbd_state_trans {
1777c478bd9Sstevel@tonic-gate 	int	x_cmd;
1787c478bd9Sstevel@tonic-gate 	struct {
1797c478bd9Sstevel@tonic-gate 		int	x_rv;		/* return value of pre_op */
1807c478bd9Sstevel@tonic-gate 		int	x_err;		/* errno, if any */
1817c478bd9Sstevel@tonic-gate 	} x_op[SBD_NUM_STATES];
1827c478bd9Sstevel@tonic-gate } sbd_state_transition[] = {
1837c478bd9Sstevel@tonic-gate 	{ SBD_CMD_CONNECT,
1847c478bd9Sstevel@tonic-gate 		{
1857c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* empty */
1867c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
1877c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
1887c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
1897c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
1907c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
1917c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
1927c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
1937c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	},
1967c478bd9Sstevel@tonic-gate 	{ SBD_CMD_DISCONNECT,
1977c478bd9Sstevel@tonic-gate 		{
1987c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
1997c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2007c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* connected */
2017c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* unconfigured */
2027c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2037c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2047c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2057c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2067c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 	},
2097c478bd9Sstevel@tonic-gate 	{ SBD_CMD_CONFIGURE,
2107c478bd9Sstevel@tonic-gate 		{
2117c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2127c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* occupied */
2137c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* connected */
2147c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* unconfigured */
2157c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* partial */
2167c478bd9Sstevel@tonic-gate 			{ 1, 0 },	/* configured */
2177c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* release */
2187c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* unreferenced */
2197c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	},
2227c478bd9Sstevel@tonic-gate 	{ SBD_CMD_UNCONFIGURE,
2237c478bd9Sstevel@tonic-gate 		{
2247c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2257c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* occupied */
2267c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2277c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2287c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2297c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* configured */
2307c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* release */
2317c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* unreferenced */
2327c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	},
2357c478bd9Sstevel@tonic-gate 	{ SBD_CMD_POWEROFF,
2367c478bd9Sstevel@tonic-gate 		{
2377c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2387c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2397c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2407c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2417c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2427c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2437c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2447c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2457c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	},
2487c478bd9Sstevel@tonic-gate 	{ SBD_CMD_POWERON,
2497c478bd9Sstevel@tonic-gate 		{
2507c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2517c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2527c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2537c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2547c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2557c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2567c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2577c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2587c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	},
2617c478bd9Sstevel@tonic-gate 	{ SBD_CMD_UNASSIGN,
2627c478bd9Sstevel@tonic-gate 		{
2637c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2647c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2657c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2667c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2677c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2687c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2697c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2707c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2717c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	},
2747c478bd9Sstevel@tonic-gate 	{ SBD_CMD_ASSIGN,
2757c478bd9Sstevel@tonic-gate 		{
2767c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2777c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2787c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2797c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2807c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2817c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2827c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2837c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2847c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	},
2877c478bd9Sstevel@tonic-gate 	{ SBD_CMD_TEST,
2887c478bd9Sstevel@tonic-gate 		{
2897c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* empty */
2907c478bd9Sstevel@tonic-gate 			{ 0, 0 },	/* occupied */
2917c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* connected */
2927c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unconfigured */
2937c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* partial */
2947c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* configured */
2957c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* release */
2967c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* unreferenced */
2977c478bd9Sstevel@tonic-gate 			{ 1, EIO },	/* fatal */
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 	},
3007c478bd9Sstevel@tonic-gate };
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Global R/W lock to synchronize access across
3047c478bd9Sstevel@tonic-gate  * multiple boards.  Users wanting multi-board access
3057c478bd9Sstevel@tonic-gate  * must grab WRITE lock, others must grab READ lock.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate krwlock_t	sbd_grwlock;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * Global to determine if an event needs to be sent
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate char send_event = 0;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Required/Expected functions.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static sbd_handle_t	*sbd_get_handle(dev_t dev, sbd_softstate_t *softsp,
3197c478bd9Sstevel@tonic-gate 				intptr_t arg, sbd_init_arg_t *iap);
3207c478bd9Sstevel@tonic-gate static void		sbd_release_handle(sbd_handle_t *hp);
3217c478bd9Sstevel@tonic-gate static int		sbd_pre_op(sbd_handle_t *hp);
3227c478bd9Sstevel@tonic-gate static void		sbd_post_op(sbd_handle_t *hp);
3237c478bd9Sstevel@tonic-gate static int		sbd_probe_board(sbd_handle_t *hp);
3247c478bd9Sstevel@tonic-gate static int		sbd_deprobe_board(sbd_handle_t *hp);
3257c478bd9Sstevel@tonic-gate static void		sbd_connect(sbd_handle_t *hp);
3267c478bd9Sstevel@tonic-gate static void		sbd_assign_board(sbd_handle_t *hp);
3277c478bd9Sstevel@tonic-gate static void		sbd_unassign_board(sbd_handle_t *hp);
3287c478bd9Sstevel@tonic-gate static void		sbd_poweron_board(sbd_handle_t *hp);
3297c478bd9Sstevel@tonic-gate static void		sbd_poweroff_board(sbd_handle_t *hp);
3307c478bd9Sstevel@tonic-gate static void		sbd_test_board(sbd_handle_t *hp);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate static int		sbd_disconnect(sbd_handle_t *hp);
3337c478bd9Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_attach_devlist(sbd_handle_t *hp,
3347c478bd9Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3357c478bd9Sstevel@tonic-gate static int		sbd_pre_attach_devlist(sbd_handle_t *hp,
3367c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3377c478bd9Sstevel@tonic-gate static int		sbd_post_attach_devlist(sbd_handle_t *hp,
3387c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3397c478bd9Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_release_devlist(sbd_handle_t *hp,
3407c478bd9Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3417c478bd9Sstevel@tonic-gate static int		sbd_pre_release_devlist(sbd_handle_t *hp,
3427c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3437c478bd9Sstevel@tonic-gate static int		sbd_post_release_devlist(sbd_handle_t *hp,
3447c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3457c478bd9Sstevel@tonic-gate static void		sbd_release_done(sbd_handle_t *hp,
3467c478bd9Sstevel@tonic-gate 					sbd_comp_type_t nodetype,
3477c478bd9Sstevel@tonic-gate 					dev_info_t *dip);
3487c478bd9Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_detach_devlist(sbd_handle_t *hp,
3497c478bd9Sstevel@tonic-gate 					int32_t *devnump, int32_t pass);
3507c478bd9Sstevel@tonic-gate static int		sbd_pre_detach_devlist(sbd_handle_t *hp,
3517c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3527c478bd9Sstevel@tonic-gate static int		sbd_post_detach_devlist(sbd_handle_t *hp,
3537c478bd9Sstevel@tonic-gate 					sbd_devlist_t *devlist, int32_t devnum);
3547c478bd9Sstevel@tonic-gate static void		sbd_status(sbd_handle_t *hp);
3557c478bd9Sstevel@tonic-gate static void		sbd_get_ncm(sbd_handle_t *hp);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Support functions.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate static sbd_devset_t	sbd_dev2devset(sbd_comp_id_t *cid);
3627c478bd9Sstevel@tonic-gate static int		sbd_copyin_ioarg(sbd_handle_t *hp, int mode, int cmd,
3637c478bd9Sstevel@tonic-gate 				sbd_cmd_t *cmdp, sbd_ioctl_arg_t *iap);
3647c478bd9Sstevel@tonic-gate static int		sbd_copyout_errs(int mode, sbd_ioctl_arg_t *iap,
3657c478bd9Sstevel@tonic-gate 					void *arg);
3667c478bd9Sstevel@tonic-gate static int		sbd_copyout_ioarg(int mode, int cmd, sbd_cmd_t *scp,
3677c478bd9Sstevel@tonic-gate 				sbd_ioctl_arg_t *iap);
3687c478bd9Sstevel@tonic-gate static int		sbd_check_transition(sbd_board_t *sbp,
3697c478bd9Sstevel@tonic-gate 					sbd_devset_t *devsetp,
3707c478bd9Sstevel@tonic-gate 					struct sbd_state_trans *transp);
3717c478bd9Sstevel@tonic-gate static sbd_devlist_t	*sbd_get_devlist(sbd_handle_t *hp,
3727c478bd9Sstevel@tonic-gate 					sbd_board_t *sbp,
3737c478bd9Sstevel@tonic-gate 					sbd_comp_type_t nodetype,
3747c478bd9Sstevel@tonic-gate 					int max_units, uint_t uset,
3757c478bd9Sstevel@tonic-gate 					int *count, int present_only);
3767c478bd9Sstevel@tonic-gate static int		sbd_mem_status(sbd_handle_t *hp, sbd_devset_t devset,
3777c478bd9Sstevel@tonic-gate 					sbd_dev_stat_t *dsp);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static int		sbd_init_devlists(sbd_board_t *sbp);
3807c478bd9Sstevel@tonic-gate static int		sbd_name_to_idx(char *name);
3817c478bd9Sstevel@tonic-gate static int		sbd_otype_to_idx(char *otpye);
3827c478bd9Sstevel@tonic-gate static int		sbd_setup_devlists(dev_info_t *dip, void *arg);
3837c478bd9Sstevel@tonic-gate static void		sbd_init_mem_devlists(sbd_board_t *sbp);
3847c478bd9Sstevel@tonic-gate static void		sbd_init_cpu_unit(sbd_board_t *sbp, int unit);
3857c478bd9Sstevel@tonic-gate static void		sbd_board_discovery(sbd_board_t *sbp);
3867c478bd9Sstevel@tonic-gate static void		sbd_board_init(sbd_board_t *sbp,
3877c478bd9Sstevel@tonic-gate 				sbd_softstate_t *softsp,
3887c478bd9Sstevel@tonic-gate 				int bd, dev_info_t *dip, int wnode);
3897c478bd9Sstevel@tonic-gate static void		sbd_board_destroy(sbd_board_t *sbp);
3907c478bd9Sstevel@tonic-gate static int		sbd_check_unit_attached(sbd_board_t *sbp,
3917c478bd9Sstevel@tonic-gate 				dev_info_t *dip, int unit,
3927c478bd9Sstevel@tonic-gate 				sbd_comp_type_t nodetype, sbderror_t *ep);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static sbd_state_t 	rstate_cvt(sbd_istate_t state);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Autoconfiguration data structures
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
4037c478bd9Sstevel@tonic-gate 	&mod_miscops,
404903a11ebSrh 	"System Board DR"
4057c478bd9Sstevel@tonic-gate };
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
4087c478bd9Sstevel@tonic-gate 	MODREV_1,
4097c478bd9Sstevel@tonic-gate 	(void *)&modlmisc,
4107c478bd9Sstevel@tonic-gate 	NULL
4117c478bd9Sstevel@tonic-gate };
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static int sbd_instances = 0;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * dr Global data elements
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate sbd_global sbd_g;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * We want to be able to unload the module when we wish to do so, but we don't
4227c478bd9Sstevel@tonic-gate  * want anything else to unload it.  Unloading cannot occur until
4237c478bd9Sstevel@tonic-gate  * sbd_teardown_instance is called by an explicit IOCTL into the parent node.
4247c478bd9Sstevel@tonic-gate  * This support is for debugging purposes and should it be expected to work
4257c478bd9Sstevel@tonic-gate  * on the field, it should be enhanced:
4267c478bd9Sstevel@tonic-gate  * Currently, there is still a window where sbd_teardow_instance gets called,
4277c478bd9Sstevel@tonic-gate  * sbd_prevent_unloading now = 0, the driver doesn't get unloaded, and
4287c478bd9Sstevel@tonic-gate  * sbd_setup_instance gets called.  This may cause a panic.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate int sbd_prevent_unloading = 1;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Driver entry points.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate int
_init(void)4367c478bd9Sstevel@tonic-gate _init(void)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	int	err;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * If you need to support multiple nodes (instances), then
4427c478bd9Sstevel@tonic-gate 	 * whatever the maximum number of supported nodes is would
4437c478bd9Sstevel@tonic-gate 	 * need to passed as the third parameter to ddi_soft_state_init().
4447c478bd9Sstevel@tonic-gate 	 * Alternative would be to dynamically fini and re-init the
4457c478bd9Sstevel@tonic-gate 	 * soft state structure each time a node is attached.
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	err = ddi_soft_state_init((void **)&sbd_g.softsp,
4487c478bd9Sstevel@tonic-gate 		sizeof (sbd_softstate_t), SBD_MAX_INSTANCES);
4497c478bd9Sstevel@tonic-gate 	if (err)
4507c478bd9Sstevel@tonic-gate 		return (err);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0) {
4537c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini((void **)&sbd_g.softsp);
4547c478bd9Sstevel@tonic-gate 		return (err);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* Get the array of names from platform helper routine */
4587c478bd9Sstevel@tonic-gate 	sbd_devattr = sbdp_get_devattr();
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (err);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate int
_fini(void)4647c478bd9Sstevel@tonic-gate _fini(void)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	int	err;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (sbd_prevent_unloading)
4697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	ASSERT(sbd_instances == 0);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
4747c478bd9Sstevel@tonic-gate 		return (err);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini((void **)&sbd_g.softsp);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (0);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4827c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate int
sbd_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,char * event)4887c478bd9Sstevel@tonic-gate sbd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, char *event)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	int		rv = 0, instance;
4917c478bd9Sstevel@tonic-gate 	sbd_handle_t	*hp;
4927c478bd9Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
4937c478bd9Sstevel@tonic-gate 	sbd_init_arg_t	init_arg;
4947c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_ioctl";
4957c478bd9Sstevel@tonic-gate 	int		dr_avail;
4967c478bd9Sstevel@tonic-gate 
49704580fdfSmathue 	PR_BYP("sbd_ioctl cmd=%x, arg=%lx\n", cmd, arg);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/* Note: this must also be changed in tandem with sbd_ioctl.h */
5007c478bd9Sstevel@tonic-gate 	switch (cmd) {
5017c478bd9Sstevel@tonic-gate 		case SBD_CMD_ASSIGN:
5027c478bd9Sstevel@tonic-gate 		case SBD_CMD_UNASSIGN:
5037c478bd9Sstevel@tonic-gate 		case SBD_CMD_POWERON:
5047c478bd9Sstevel@tonic-gate 		case SBD_CMD_POWEROFF:
5057c478bd9Sstevel@tonic-gate 		case SBD_CMD_TEST:
5067c478bd9Sstevel@tonic-gate 		case SBD_CMD_CONNECT:
5077c478bd9Sstevel@tonic-gate 		case SBD_CMD_CONFIGURE:
5087c478bd9Sstevel@tonic-gate 		case SBD_CMD_UNCONFIGURE:
5097c478bd9Sstevel@tonic-gate 		case SBD_CMD_DISCONNECT:
5107c478bd9Sstevel@tonic-gate 		case SBD_CMD_STATUS:
5117c478bd9Sstevel@tonic-gate 		case SBD_CMD_GETNCM:
5127c478bd9Sstevel@tonic-gate 		case SBD_CMD_PASSTHRU:
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 		default:
5157c478bd9Sstevel@tonic-gate 			return (ENOTTY);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	instance = SBD_GET_MINOR2INST(getminor(dev));
5197c478bd9Sstevel@tonic-gate 	if ((softsp = (sbd_softstate_t *)GET_SOFTC(instance)) == NULL) {
5207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
5217c478bd9Sstevel@tonic-gate 			"sbd:%s:%d: module not yet attached",
5227c478bd9Sstevel@tonic-gate 			f, instance);
5237c478bd9Sstevel@tonic-gate 		return (ENXIO);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	init_arg.dev = dev;
5277c478bd9Sstevel@tonic-gate 	init_arg.cmd = cmd;
5287c478bd9Sstevel@tonic-gate 	init_arg.mode = mode;
5297c478bd9Sstevel@tonic-gate 	init_arg.ioargp = (sbd_ioctl_arg_t *)arg;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	hp = sbd_get_handle(dev, softsp, arg, &init_arg);
5327c478bd9Sstevel@tonic-gate 	/* Check to see if we support dr */
5337c478bd9Sstevel@tonic-gate 	dr_avail = sbdp_dr_avail();
5347c478bd9Sstevel@tonic-gate 	if (dr_avail != 1) {
5357c478bd9Sstevel@tonic-gate 		switch (hp->h_cmd) {
5367c478bd9Sstevel@tonic-gate 			case SBD_CMD_STATUS:
5377c478bd9Sstevel@tonic-gate 			case SBD_CMD_GETNCM:
5387c478bd9Sstevel@tonic-gate 			case SBD_CMD_PASSTHRU:
5397c478bd9Sstevel@tonic-gate 				break;
5407c478bd9Sstevel@tonic-gate 			default:
5417c478bd9Sstevel@tonic-gate 				sbd_release_handle(hp);
5427c478bd9Sstevel@tonic-gate 				return (ENOTSUP);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
5477c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
5487c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
5497c478bd9Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
5507c478bd9Sstevel@tonic-gate 		/* no locks needed for these commands */
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	default:
5547c478bd9Sstevel@tonic-gate 		rw_enter(&sbd_grwlock, RW_WRITER);
5557c478bd9Sstevel@tonic-gate 		mutex_enter(&SBDH2BD(hp->h_sbd)->sb_mutex);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		/*
5587c478bd9Sstevel@tonic-gate 		 * If we're dealing with memory at all, then we have
5597c478bd9Sstevel@tonic-gate 		 * to keep the "exclusive" global lock held.  This is
5607c478bd9Sstevel@tonic-gate 		 * necessary since we will probably need to look at
5617c478bd9Sstevel@tonic-gate 		 * multiple board structs.  Otherwise, we only have
5627c478bd9Sstevel@tonic-gate 		 * to deal with the board in question and so can drop
5637c478bd9Sstevel@tonic-gate 		 * the global lock to "shared".
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * XXX This is incorrect. The sh_devset has not
5677c478bd9Sstevel@tonic-gate 		 * been set at this point - it is 0.
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		rv = DEVSET_IN_SET(HD2MACHHD(hp)->sh_devset,
5707c478bd9Sstevel@tonic-gate 		    SBD_COMP_MEM, DEVSET_ANYUNIT);
5717c478bd9Sstevel@tonic-gate 		if (rv == 0)
5727c478bd9Sstevel@tonic-gate 			rw_downgrade(&sbd_grwlock);
5737c478bd9Sstevel@tonic-gate 		break;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * Before any operations happen, reset the event flag
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	send_event = 0;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (sbd_pre_op(hp) == 0) {
5827c478bd9Sstevel@tonic-gate 		sbd_exec_op(hp);
5837c478bd9Sstevel@tonic-gate 		sbd_post_op(hp);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	rv = SBD_GET_ERRNO(SBD_HD2ERR(hp));
5877c478bd9Sstevel@tonic-gate 	*event = send_event;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* undo locking, if any, done before sbd_pre_op */
5907c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
5917c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
5927c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
5937c478bd9Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
5947c478bd9Sstevel@tonic-gate 		break;
5957c478bd9Sstevel@tonic-gate 	default:
5967c478bd9Sstevel@tonic-gate 		mutex_exit(&SBDH2BD(hp->h_sbd)->sb_mutex);
5977c478bd9Sstevel@tonic-gate 		rw_exit(&sbd_grwlock);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	sbd_release_handle(hp);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	return (rv);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate int
sbd_setup_instance(int instance,dev_info_t * root,int max_boards,int wnode,caddr_t sbdp_arg)6067c478bd9Sstevel@tonic-gate sbd_setup_instance(int instance, dev_info_t *root, int max_boards, int wnode,
6077c478bd9Sstevel@tonic-gate 		caddr_t sbdp_arg)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	int 		b;
6107c478bd9Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
6117c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbd_boardlist;
6127c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_setup_instance";
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	sbd_instances++;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (sbdp_setup_instance(sbdp_arg) != DDI_SUCCESS) {
6177c478bd9Sstevel@tonic-gate 		sbd_instances--;
6187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (ALLOC_SOFTC(instance) != DDI_SUCCESS) {
6227c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6237c478bd9Sstevel@tonic-gate 			"sbd:%s:%d: failed to alloc soft-state",
6247c478bd9Sstevel@tonic-gate 			f, instance);
62507d06da5SSurya Prakki 		(void) sbdp_teardown_instance(sbdp_arg);
6267c478bd9Sstevel@tonic-gate 		sbd_instances--;
6277c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	softsp = (sbd_softstate_t *)GET_SOFTC(instance);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (softsp == NULL) {
6337c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6347c478bd9Sstevel@tonic-gate 			"sbd:%s:%d: failed to get soft-state instance",
6357c478bd9Sstevel@tonic-gate 			f, instance);
6367c478bd9Sstevel@tonic-gate 		goto exit;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	sbd_boardlist = GETSTRUCT(sbd_board_t, max_boards);
6407c478bd9Sstevel@tonic-gate 	if (sbd_boardlist == NULL) {
6417c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6427c478bd9Sstevel@tonic-gate 			"sbd:%s: failed to alloc board list %d",
6437c478bd9Sstevel@tonic-gate 			f, instance);
6447c478bd9Sstevel@tonic-gate 		goto exit;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	softsp->sbd_boardlist  = (void *)sbd_boardlist;
6497c478bd9Sstevel@tonic-gate 	softsp->max_boards  = max_boards;
6507c478bd9Sstevel@tonic-gate 	softsp->wnode  = wnode;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	for (b = 0; b < max_boards; b++) {
6547c478bd9Sstevel@tonic-gate 		sbd_board_init(sbd_boardlist++, softsp, b, root, wnode);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6597c478bd9Sstevel@tonic-gate exit:
6607c478bd9Sstevel@tonic-gate 	(void) sbdp_teardown_instance(sbdp_arg);
6617c478bd9Sstevel@tonic-gate 	FREE_SOFTC(instance);
6627c478bd9Sstevel@tonic-gate 	sbd_instances--;
6637c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate int
sbd_teardown_instance(int instance,caddr_t sbdp_arg)6677c478bd9Sstevel@tonic-gate sbd_teardown_instance(int instance, caddr_t sbdp_arg)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (sbdp_teardown_instance(sbdp_arg) != DDI_SUCCESS)
6727c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	softsp = (sbd_softstate_t *)GET_SOFTC(instance);
6757c478bd9Sstevel@tonic-gate 	if (softsp == NULL) {
6767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	(void) sbd_dealloc_instance((sbd_board_t *)softsp->sbd_boardlist,
6807c478bd9Sstevel@tonic-gate 		softsp->max_boards);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	FREE_SOFTC(instance);
6837c478bd9Sstevel@tonic-gate 	sbd_instances--;
6847c478bd9Sstevel@tonic-gate 	sbd_prevent_unloading = 0;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate static void
sbd_exec_op(sbd_handle_t * hp)6907c478bd9Sstevel@tonic-gate sbd_exec_op(sbd_handle_t *hp)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
6937c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_exec_op";
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	switch (hp->h_cmd) {
6967c478bd9Sstevel@tonic-gate 		int	dev_canceled;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONNECT:
6997c478bd9Sstevel@tonic-gate 		if (sbd_probe_board(hp))
7007c478bd9Sstevel@tonic-gate 			break;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		sbd_connect(hp);
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	case SBD_CMD_CONFIGURE:
7067c478bd9Sstevel@tonic-gate 		sbd_dev_configure(hp);
7077c478bd9Sstevel@tonic-gate 		break;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNCONFIGURE:
7107c478bd9Sstevel@tonic-gate 		if (((dev_canceled = sbd_dev_release(hp)) == 0) &&
7117c478bd9Sstevel@tonic-gate 		    (SBD_GET_ERRNO(SBD_HD2ERR(hp)) == 0 &&
7127c478bd9Sstevel@tonic-gate 		    SBD_GET_ERR(SBD_HD2ERR(hp)) == 0))
7137c478bd9Sstevel@tonic-gate 			dev_canceled = sbd_dev_unconfigure(hp);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (dev_canceled)
7167c478bd9Sstevel@tonic-gate 			sbd_cancel(hp);
7177c478bd9Sstevel@tonic-gate 		break;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	case SBD_CMD_DISCONNECT:
7207c478bd9Sstevel@tonic-gate 		mutex_enter(&sbp->sb_slock);
7217c478bd9Sstevel@tonic-gate 		if (sbd_disconnect(hp) == 0)
7227c478bd9Sstevel@tonic-gate 			(void) sbd_deprobe_board(hp);
7237c478bd9Sstevel@tonic-gate 		mutex_exit(&sbp->sb_slock);
7247c478bd9Sstevel@tonic-gate 		break;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	case SBD_CMD_STATUS:
7277c478bd9Sstevel@tonic-gate 		sbd_status(hp);
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	case SBD_CMD_GETNCM:
7317c478bd9Sstevel@tonic-gate 		sbd_get_ncm(hp);
7327c478bd9Sstevel@tonic-gate 		break;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	case SBD_CMD_ASSIGN:
7357c478bd9Sstevel@tonic-gate 		sbd_assign_board(hp);
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	case SBD_CMD_UNASSIGN:
7397c478bd9Sstevel@tonic-gate 		sbd_unassign_board(hp);
7407c478bd9Sstevel@tonic-gate 		break;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWEROFF:
7437c478bd9Sstevel@tonic-gate 		sbd_poweroff_board(hp);
7447c478bd9Sstevel@tonic-gate 		break;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	case SBD_CMD_POWERON:
7477c478bd9Sstevel@tonic-gate 		sbd_poweron_board(hp);
7487c478bd9Sstevel@tonic-gate 		break;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	case SBD_CMD_TEST:
7517c478bd9Sstevel@tonic-gate 		sbd_test_board(hp);
7527c478bd9Sstevel@tonic-gate 		break;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	case SBD_CMD_PASSTHRU:
7557c478bd9Sstevel@tonic-gate 	{
7567c478bd9Sstevel@tonic-gate 		int			rv;
7577c478bd9Sstevel@tonic-gate 		sbdp_handle_t		*hdp;
7587c478bd9Sstevel@tonic-gate 		sbderror_t		*ep = SBD_HD2ERR(hp);
7597c478bd9Sstevel@tonic-gate 		sbdp_ioctl_arg_t	ia, *iap;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		iap = &ia;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		iap->h_dev = hp->h_dev;
7647c478bd9Sstevel@tonic-gate 		iap->h_cmd = hp->h_cmd;
7657c478bd9Sstevel@tonic-gate 		iap->h_iap = (intptr_t)hp->h_iap;
7667c478bd9Sstevel@tonic-gate 		iap->h_mode = hp->h_mode;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		hdp = sbd_get_sbdp_handle(sbp, hp);
7697c478bd9Sstevel@tonic-gate 		rv = sbdp_ioctl(hdp, iap);
7707c478bd9Sstevel@tonic-gate 		if (rv != 0) {
7717c478bd9Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
7727c478bd9Sstevel@tonic-gate 			ep->e_errno = rv;
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
7757c478bd9Sstevel@tonic-gate 		break;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	default:
7797c478bd9Sstevel@tonic-gate 		SBD_SET_ERRNO(SBD_HD2ERR(hp), ENOTTY);
7807c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
7817c478bd9Sstevel@tonic-gate 			"sbd:%s: unknown command (%d)",
7827c478bd9Sstevel@tonic-gate 			f, hp->h_cmd);
7837c478bd9Sstevel@tonic-gate 		break;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (SBD_GET_ERR(SBD_HD2ERR(hp)))
7887c478bd9Sstevel@tonic-gate 		PR_BYP("XXX e_code=%d", SBD_GET_ERR(SBD_HD2ERR(hp)));
7897c478bd9Sstevel@tonic-gate 	if (SBD_GET_ERRNO(SBD_HD2ERR(hp)))
7907c478bd9Sstevel@tonic-gate 		PR_BYP("XXX errno=%d", SBD_GET_ERRNO(SBD_HD2ERR(hp)));
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate sbd_comp_type_t
sbd_get_devtype(sbd_handle_t * hp,dev_info_t * dip)7947c478bd9Sstevel@tonic-gate sbd_get_devtype(sbd_handle_t *hp, dev_info_t *dip)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = hp ? SBDH2BD(hp->h_sbd) : NULL;
7977c478bd9Sstevel@tonic-gate 	sbd_istate_t	bstate;
7987c478bd9Sstevel@tonic-gate 	dev_info_t	**devlist;
7997c478bd9Sstevel@tonic-gate 	int		i;
8007c478bd9Sstevel@tonic-gate 	char		device[OBP_MAXDRVNAME];
8017c478bd9Sstevel@tonic-gate 	int		devicelen;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	devicelen = sizeof (device);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	bstate = sbp ? SBD_BOARD_STATE(sbp) : SBD_STATE_EMPTY;
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * if the board's connected or configured, search the
8087c478bd9Sstevel@tonic-gate 	 * devlists.  Otherwise check the device tree
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	switch (bstate) {
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	case SBD_STATE_CONNECTED:
8137c478bd9Sstevel@tonic-gate 	case SBD_STATE_CONFIGURED:
8147c478bd9Sstevel@tonic-gate 	case SBD_STATE_UNREFERENCED:
8157c478bd9Sstevel@tonic-gate 	case SBD_STATE_UNCONFIGURED:
8167c478bd9Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_MEM)];
8177c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++)
8187c478bd9Sstevel@tonic-gate 			if (devlist[i] == dip)
8197c478bd9Sstevel@tonic-gate 				return (SBD_COMP_MEM);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_CPU)];
8227c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++)
8237c478bd9Sstevel@tonic-gate 			if (devlist[i] == dip)
8247c478bd9Sstevel@tonic-gate 				return (SBD_COMP_CPU);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		devlist = sbp->sb_devlist[NIX(SBD_COMP_IO)];
8277c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++)
8287c478bd9Sstevel@tonic-gate 			if (devlist[i] == dip)
8297c478bd9Sstevel@tonic-gate 				return (SBD_COMP_IO);
8307c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	default:
8337c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
8347c478bd9Sstevel@tonic-gate 		    OBP_DEVICETYPE,  (caddr_t)device, &devicelen))
8357c478bd9Sstevel@tonic-gate 			break;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		for (i = 0; SBD_COMP(i) != SBD_COMP_UNKNOWN; i++) {
8387c478bd9Sstevel@tonic-gate 			if (strcmp(device, SBD_OTYPE(i)) != 0)
8397c478bd9Sstevel@tonic-gate 				continue;
8407c478bd9Sstevel@tonic-gate 			return (SBD_COMP(i));
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		break;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	return (SBD_COMP_UNKNOWN);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static void
sbd_dev_configure(sbd_handle_t * hp)8497c478bd9Sstevel@tonic-gate sbd_dev_configure(sbd_handle_t *hp)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	int		n, unit;
8527c478bd9Sstevel@tonic-gate 	int32_t		pass, devnum;
8537c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
8547c478bd9Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
8557c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
8567c478bd9Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
8577c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	pass = 1;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
8627c478bd9Sstevel@tonic-gate 	while ((devlist = sbd_get_attach_devlist(hp, &devnum, pass)) != NULL) {
8637c478bd9Sstevel@tonic-gate 		int	err;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		err = sbd_pre_attach_devlist(hp, devlist, devnum);
8667c478bd9Sstevel@tonic-gate 		if (err < 0) {
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 		} else if (err > 0) {
8697c478bd9Sstevel@tonic-gate 			pass++;
8707c478bd9Sstevel@tonic-gate 			continue;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
8747c478bd9Sstevel@tonic-gate 			sbderror_t	*ep;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			ep = &devlist[n].dv_error;
8777c478bd9Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, 0);
8787c478bd9Sstevel@tonic-gate 			SBD_SET_ERR(ep, 0);
8797c478bd9Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
8807c478bd9Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
8837c478bd9Sstevel@tonic-gate 			if (unit < 0) {
8847c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
8857c478bd9Sstevel@tonic-gate 				break;
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 			switch (nodetype) {
8897c478bd9Sstevel@tonic-gate 			case SBD_COMP_MEM:
8907c478bd9Sstevel@tonic-gate 				sbd_attach_mem(hp, ep);
8917c478bd9Sstevel@tonic-gate 				if (SBD_GET_ERR(ep) == ESBD_CPUONLINE) {
8927c478bd9Sstevel@tonic-gate 					FREESTRUCT(devlist, sbd_devlist_t,
8937c478bd9Sstevel@tonic-gate 						MAX_MEM_UNITS_PER_BOARD);
8947c478bd9Sstevel@tonic-gate 					sbd_release_sbdp_handle(hdp);
8957c478bd9Sstevel@tonic-gate 					return;
8967c478bd9Sstevel@tonic-gate 				}
8977c478bd9Sstevel@tonic-gate 				break;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 			case SBD_COMP_CPU:
9007c478bd9Sstevel@tonic-gate 				sbd_attach_cpu(hp, ep, dip, unit);
9017c478bd9Sstevel@tonic-gate 				break;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 			case SBD_COMP_IO:
9047c478bd9Sstevel@tonic-gate 				sbd_attach_io(hp, ep, dip, unit);
9057c478bd9Sstevel@tonic-gate 				break;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 			default:
9087c478bd9Sstevel@tonic-gate 				SBD_SET_ERRNO(ep, ENOTTY);
9097c478bd9Sstevel@tonic-gate 				break;
9107c478bd9Sstevel@tonic-gate 			}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			if (sbd_set_err_in_hdl(hp, ep) == 0)
9137c478bd9Sstevel@tonic-gate 				continue;
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		err = sbd_post_attach_devlist(hp, devlist, devnum);
9177c478bd9Sstevel@tonic-gate 		if (err < 0)
9187c478bd9Sstevel@tonic-gate 			break;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		pass++;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate static int
sbd_dev_release(sbd_handle_t * hp)9267c478bd9Sstevel@tonic-gate sbd_dev_release(sbd_handle_t *hp)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	int		n, unit;
9297c478bd9Sstevel@tonic-gate 	int32_t		pass, devnum;
9307c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
9317c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
9327c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
9337c478bd9Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
9347c478bd9Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
9357c478bd9Sstevel@tonic-gate 	int		err = 0;
9367c478bd9Sstevel@tonic-gate 	int		dev_canceled;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	pass = 1;
9397c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	sbp->sb_busy = 1;
9427c478bd9Sstevel@tonic-gate 	while ((devlist =
9437c478bd9Sstevel@tonic-gate 		sbd_get_release_devlist(hp, &devnum, pass)) != NULL) {
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		err = sbd_pre_release_devlist(hp, devlist, devnum);
9467c478bd9Sstevel@tonic-gate 		if (err < 0) {
9477c478bd9Sstevel@tonic-gate 			dev_canceled = 1;
9487c478bd9Sstevel@tonic-gate 			break;
9497c478bd9Sstevel@tonic-gate 		} else if (err > 0) {
9507c478bd9Sstevel@tonic-gate 			pass++;
9517c478bd9Sstevel@tonic-gate 			continue;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		dev_canceled = 0;
9557c478bd9Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
9567c478bd9Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
9577c478bd9Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
9607c478bd9Sstevel@tonic-gate 			if (unit < 0) {
9617c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
9627c478bd9Sstevel@tonic-gate 				break;
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 			if ((nodetype == SBD_COMP_MEM) &&
9667c478bd9Sstevel@tonic-gate 			    sbd_release_mem(hp, dip, unit)) {
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 				dev_canceled++;
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 			sbd_release_done(hp, nodetype, dip);
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		err = sbd_post_release_devlist(hp, devlist, devnum);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		if (err < 0)
9777c478bd9Sstevel@tonic-gate 			break;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		if (dev_canceled)
9807c478bd9Sstevel@tonic-gate 			break;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 		pass++;
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	sbp->sb_busy = 0;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (dev_canceled)
9897c478bd9Sstevel@tonic-gate 		return (dev_canceled);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	return (err);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate static int
sbd_dev_unconfigure(sbd_handle_t * hp)9957c478bd9Sstevel@tonic-gate sbd_dev_unconfigure(sbd_handle_t *hp)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	int		n, unit;
9987c478bd9Sstevel@tonic-gate 	int32_t		pass, devnum;
9997c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
10007c478bd9Sstevel@tonic-gate 	sbd_devlist_t	*devlist;
10017c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
10027c478bd9Sstevel@tonic-gate 	sbd_comp_type_t	nodetype;
10037c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
10047c478bd9Sstevel@tonic-gate 	int		dev_canceled = 0;
10057c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_dev_unconfigure";
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	pass = 1;
10107c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	while ((devlist = sbd_get_detach_devlist(hp, &devnum, pass)) != NULL) {
10137c478bd9Sstevel@tonic-gate 		int	err, detach_err = 0;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		err = sbd_pre_detach_devlist(hp, devlist, devnum);
10167c478bd9Sstevel@tonic-gate 		if (err) {
10177c478bd9Sstevel@tonic-gate 			/*
10187c478bd9Sstevel@tonic-gate 			 * Only cancel the operation for memory in
10197c478bd9Sstevel@tonic-gate 			 * case of failure.
10207c478bd9Sstevel@tonic-gate 			 */
10217c478bd9Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, devlist->dv_dip);
10227c478bd9Sstevel@tonic-gate 			if (nodetype == SBD_COMP_MEM)
10237c478bd9Sstevel@tonic-gate 				dev_canceled = 1;
10247c478bd9Sstevel@tonic-gate 			(void) sbd_post_detach_devlist(hp, devlist, devnum);
10257c478bd9Sstevel@tonic-gate 			break;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		for (n = 0; n < devnum; n++) {
10297c478bd9Sstevel@tonic-gate 			sbderror_t	*ep;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			ep = &devlist[n].dv_error;
10327c478bd9Sstevel@tonic-gate 			SBD_SET_ERRNO(ep, 0);
10337c478bd9Sstevel@tonic-gate 			SBD_SET_ERR(ep, 0);
10347c478bd9Sstevel@tonic-gate 			dip = devlist[n].dv_dip;
10357c478bd9Sstevel@tonic-gate 			nodetype = sbd_get_devtype(hp, dip);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
10387c478bd9Sstevel@tonic-gate 			if (unit < 0) {
10397c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
10407c478bd9Sstevel@tonic-gate 				break;
10417c478bd9Sstevel@tonic-gate 			}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 			switch (nodetype) {
10447c478bd9Sstevel@tonic-gate 			case SBD_COMP_MEM:
10457c478bd9Sstevel@tonic-gate 				dev_canceled = sbd_detach_mem(hp, ep, unit);
10467c478bd9Sstevel@tonic-gate 				break;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			case SBD_COMP_CPU:
10497c478bd9Sstevel@tonic-gate 				sbd_detach_cpu(hp, ep, dip, unit);
10507c478bd9Sstevel@tonic-gate 				break;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 			case SBD_COMP_IO:
10537c478bd9Sstevel@tonic-gate 				sbd_detach_io(hp, ep, dip, unit);
10547c478bd9Sstevel@tonic-gate 				break;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 			default:
10577c478bd9Sstevel@tonic-gate 				SBD_SET_ERRNO(ep, ENOTTY);
10587c478bd9Sstevel@tonic-gate 				break;
10597c478bd9Sstevel@tonic-gate 			}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 			if (sbd_set_err_in_hdl(hp, ep) == 0) {
10627c478bd9Sstevel@tonic-gate 				detach_err = -1;
10637c478bd9Sstevel@tonic-gate 				break;
10647c478bd9Sstevel@tonic-gate 			}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 		err = sbd_post_detach_devlist(hp, devlist, devnum);
10687c478bd9Sstevel@tonic-gate 		if ((err < 0) || (detach_err < 0))
10697c478bd9Sstevel@tonic-gate 			break;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		pass++;
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
10757c478bd9Sstevel@tonic-gate 	return (dev_canceled);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate int
sbd_errno2ecode(int error)10797c478bd9Sstevel@tonic-gate sbd_errno2ecode(int error)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	int	rv;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	switch (error) {
10847c478bd9Sstevel@tonic-gate 	case EBUSY:
10857c478bd9Sstevel@tonic-gate 		rv = ESBD_BUSY;
10867c478bd9Sstevel@tonic-gate 		break;
10877c478bd9Sstevel@tonic-gate 	case EINVAL:
10887c478bd9Sstevel@tonic-gate 		rv = ESBD_INVAL;
10897c478bd9Sstevel@tonic-gate 		break;
10907c478bd9Sstevel@tonic-gate 	case EALREADY:
10917c478bd9Sstevel@tonic-gate 		rv = ESBD_ALREADY;
10927c478bd9Sstevel@tonic-gate 		break;
10937c478bd9Sstevel@tonic-gate 	case ENODEV:
10947c478bd9Sstevel@tonic-gate 		rv = ESBD_NODEV;
10957c478bd9Sstevel@tonic-gate 		break;
10967c478bd9Sstevel@tonic-gate 	case ENOMEM:
10977c478bd9Sstevel@tonic-gate 		rv = ESBD_NOMEM;
10987c478bd9Sstevel@tonic-gate 		break;
10997c478bd9Sstevel@tonic-gate 	default:
11007c478bd9Sstevel@tonic-gate 		rv = ESBD_INVAL;
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	return (rv);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate static void
sbd_attach_cpu(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)11077c478bd9Sstevel@tonic-gate sbd_attach_cpu(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
11087c478bd9Sstevel@tonic-gate {
11097c478bd9Sstevel@tonic-gate 	int rv = 0;
11107c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
11117c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
11127c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
11137c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_attach_cpu";
11147c478bd9Sstevel@tonic-gate 	char		*pathname;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	ASSERT(dip);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * With the introduction of CMP devices, the CPU nodes
11227c478bd9Sstevel@tonic-gate 	 * are no longer directly under the top node. Since
11237c478bd9Sstevel@tonic-gate 	 * there is no plan to support CPU attach in the near
11247c478bd9Sstevel@tonic-gate 	 * future, a branch configure operation is not required.
11257c478bd9Sstevel@tonic-gate 	 */
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
11287c478bd9Sstevel@tonic-gate 	cpuid = sbdp_get_cpuid(hdp, dip);
11297c478bd9Sstevel@tonic-gate 	if (cpuid < 0) {
11307c478bd9Sstevel@tonic-gate 		rv = -1;
11317c478bd9Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
11327c478bd9Sstevel@tonic-gate 	} else if ((rv = cpu_configure(cpuid)) != 0) {
11337c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
11347c478bd9Sstevel@tonic-gate 			"sbd:%s: cpu_configure for cpuid %d failed",
11357c478bd9Sstevel@tonic-gate 			f, cpuid);
11367c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, sbd_errno2ecode(rv));
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	if (rv == 0) {
11417c478bd9Sstevel@tonic-gate 		ASSERT(sbp->sb_cpupath[unit] != NULL);
11427c478bd9Sstevel@tonic-gate 		pathname = sbp->sb_cpupath[unit];
11437c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip, pathname);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  *	translate errno
11497c478bd9Sstevel@tonic-gate  */
11507c478bd9Sstevel@tonic-gate void
sbd_errno_decode(int err,sbderror_t * ep,dev_info_t * dip)11517c478bd9Sstevel@tonic-gate sbd_errno_decode(int err, sbderror_t *ep, dev_info_t *dip)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	ASSERT(err != 0);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	switch (err) {
11567c478bd9Sstevel@tonic-gate 	case ENOMEM:
11577c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NOMEM);
11587c478bd9Sstevel@tonic-gate 		break;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	case EBUSY:
11617c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_BUSY);
11627c478bd9Sstevel@tonic-gate 		break;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	case EIO:
11657c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_IO);
11667c478bd9Sstevel@tonic-gate 		break;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	case ENXIO:
11697c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_NODEV);
11707c478bd9Sstevel@tonic-gate 		break;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	case EINVAL:
11737c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_INVAL);
11747c478bd9Sstevel@tonic-gate 		break;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	case EFAULT:
11777c478bd9Sstevel@tonic-gate 	default:
11787c478bd9Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_FAULT);
11797c478bd9Sstevel@tonic-gate 		break;
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, SBD_GET_ERRSTR(ep));
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate static void
sbd_detach_cpu(sbd_handle_t * hp,sbderror_t * ep,dev_info_t * dip,int unit)11867c478bd9Sstevel@tonic-gate sbd_detach_cpu(sbd_handle_t *hp, sbderror_t *ep, dev_info_t *dip, int unit)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
11897c478bd9Sstevel@tonic-gate 	int		rv;
11907c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
11917c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
11927c478bd9Sstevel@tonic-gate 	sbd_error_t	*spe;
11937c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_detach_cpu";
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11967c478bd9