xref: /illumos-gate/usr/src/uts/i86pc/io/dr/dr.c (revision 584b574a)
1a3114836SGerry Liu /*
2a3114836SGerry Liu  * CDDL HEADER START
3a3114836SGerry Liu  *
4a3114836SGerry Liu  * The contents of this file are subject to the terms of the
5a3114836SGerry Liu  * Common Development and Distribution License (the "License").
6a3114836SGerry Liu  * You may not use this file except in compliance with the License.
7a3114836SGerry Liu  *
8a3114836SGerry Liu  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a3114836SGerry Liu  * or http://www.opensolaris.org/os/licensing.
10a3114836SGerry Liu  * See the License for the specific language governing permissions
11a3114836SGerry Liu  * and limitations under the License.
12a3114836SGerry Liu  *
13a3114836SGerry Liu  * When distributing Covered Code, include this CDDL HEADER in each
14a3114836SGerry Liu  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a3114836SGerry Liu  * If applicable, add the following below this CDDL HEADER, with the
16a3114836SGerry Liu  * fields enclosed by brackets "[]" replaced with your own identifying
17a3114836SGerry Liu  * information: Portions Copyright [yyyy] [name of copyright owner]
18a3114836SGerry Liu  *
19a3114836SGerry Liu  * CDDL HEADER END
20a3114836SGerry Liu  */
21a3114836SGerry Liu 
22a3114836SGerry Liu /*
23a3114836SGerry Liu  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24a3114836SGerry Liu  * Use is subject to license terms.
25a3114836SGerry Liu  */
26a3114836SGerry Liu /*
27a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
28a3114836SGerry Liu  * All rights reserved.
29a3114836SGerry Liu  */
30a3114836SGerry Liu 
31a3114836SGerry Liu /*
32a3114836SGerry Liu  * PIM-DR layer of DR driver.  Provides interface between user
33a3114836SGerry Liu  * level applications and the PSM-DR layer.
34a3114836SGerry Liu  */
35a3114836SGerry Liu 
36a3114836SGerry Liu #include <sys/note.h>
37a3114836SGerry Liu #include <sys/debug.h>
38a3114836SGerry Liu #include <sys/types.h>
39a3114836SGerry Liu #include <sys/errno.h>
40a3114836SGerry Liu #include <sys/cred.h>
41a3114836SGerry Liu #include <sys/dditypes.h>
42a3114836SGerry Liu #include <sys/devops.h>
43a3114836SGerry Liu #include <sys/modctl.h>
44a3114836SGerry Liu #include <sys/poll.h>
45a3114836SGerry Liu #include <sys/conf.h>
46a3114836SGerry Liu #include <sys/ddi.h>
47a3114836SGerry Liu #include <sys/sunddi.h>
48a3114836SGerry Liu #include <sys/sunndi.h>
49a3114836SGerry Liu #include <sys/stat.h>
50a3114836SGerry Liu #include <sys/kmem.h>
51a3114836SGerry Liu #include <sys/processor.h>
52a3114836SGerry Liu #include <sys/cpuvar.h>
53a3114836SGerry Liu #include <sys/mem_config.h>
54a3114836SGerry Liu 
55a3114836SGerry Liu #include <sys/autoconf.h>
56a3114836SGerry Liu #include <sys/cmn_err.h>
57a3114836SGerry Liu 
58a3114836SGerry Liu #include <sys/ddi_impldefs.h>
59a3114836SGerry Liu #include <sys/promif.h>
60a3114836SGerry Liu #include <sys/machsystm.h>
61a3114836SGerry Liu 
62a3114836SGerry Liu #include <sys/dr.h>
63a3114836SGerry Liu #include <sys/drmach.h>
64a3114836SGerry Liu #include <sys/dr_util.h>
65a3114836SGerry Liu 
66a3114836SGerry Liu extern int		 nulldev();
67a3114836SGerry Liu extern int		 nodev();
68a3114836SGerry Liu extern struct memlist	*phys_install;
69a3114836SGerry Liu 
70a3114836SGerry Liu #ifdef DEBUG
71a3114836SGerry Liu uint_t	dr_debug = 0;			/* dr.h for bit values */
72a3114836SGerry Liu #endif /* DEBUG */
73a3114836SGerry Liu 
748682d1efSRichard Lowe static int	dr_dev_type_to_nt(char *);
758682d1efSRichard Lowe 
76a3114836SGerry Liu /*
77a3114836SGerry Liu  * NOTE: state_str, nt_str and SBD_CMD_STR are only used in a debug
78a3114836SGerry Liu  * kernel.  They are, however, referenced during both debug and non-debug
79a3114836SGerry Liu  * compiles.
80a3114836SGerry Liu  */
81a3114836SGerry Liu 
82a3114836SGerry Liu static char *state_str[] = {
83a3114836SGerry Liu 	"EMPTY", "OCCUPIED", "CONNECTED", "UNCONFIGURED",
84a3114836SGerry Liu 	"PARTIAL", "CONFIGURED", "RELEASE", "UNREFERENCED",
85a3114836SGerry Liu 	"FATAL"
86a3114836SGerry Liu };
87a3114836SGerry Liu 
88a3114836SGerry Liu #define	SBD_CMD_STR(c) \
89a3114836SGerry Liu 	(((c) == SBD_CMD_ASSIGN)	? "ASSIGN"	: \
90a3114836SGerry Liu 	((c) == SBD_CMD_UNASSIGN)	? "UNASSIGN"	: \
91a3114836SGerry Liu 	((c) == SBD_CMD_POWERON)	? "POWERON"	: \
92a3114836SGerry Liu 	((c) == SBD_CMD_POWEROFF)	? "POWEROFF"	: \
93a3114836SGerry Liu 	((c) == SBD_CMD_TEST)		? "TEST"	: \
94a3114836SGerry Liu 	((c) == SBD_CMD_CONNECT)	? "CONNECT"	: \
95a3114836SGerry Liu 	((c) == SBD_CMD_DISCONNECT)	? "DISCONNECT"	: \
96a3114836SGerry Liu 	((c) == SBD_CMD_CONFIGURE)	? "CONFIGURE"	: \
97a3114836SGerry Liu 	((c) == SBD_CMD_UNCONFIGURE)	? "UNCONFIGURE"	: \
98a3114836SGerry Liu 	((c) == SBD_CMD_GETNCM)		? "GETNCM"	: \
99a3114836SGerry Liu 	((c) == SBD_CMD_PASSTHRU)	? "PASSTHRU"	: \
100a3114836SGerry Liu 	((c) == SBD_CMD_STATUS)		? "STATUS"	: "unknown")
101a3114836SGerry Liu 
102a3114836SGerry Liu #define	DR_GET_BOARD_DEVUNIT(sb, ut, un) (&((sb)->b_dev[DEVSET_NIX(ut)][un]))
103a3114836SGerry Liu 
104a3114836SGerry Liu #define	DR_MAKE_MINOR(i, b)	(((i) << 16) | (b))
105a3114836SGerry Liu #define	DR_MINOR2INST(m)	(((m) >> 16) & 0xffff)
106a3114836SGerry Liu #define	DR_MINOR2BNUM(m)	((m) & 0xffff)
107a3114836SGerry Liu 
108a3114836SGerry Liu /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
109a3114836SGerry Liu static char *dr_ie_fmt = "dr.c %d";
110a3114836SGerry Liu 
111a3114836SGerry Liu /* struct for drmach device name to sbd_comp_type_t mapping */
112a3114836SGerry Liu typedef	struct {
113a3114836SGerry Liu 	char		*s_devtype;
114a3114836SGerry Liu 	sbd_comp_type_t	s_nodetype;
115a3114836SGerry Liu } dr_devname_t;
116a3114836SGerry Liu 
117a3114836SGerry Liu /* struct to map starfire device attributes - name:sbd_comp_type_t */
118a3114836SGerry Liu static	dr_devname_t	dr_devattr[] = {
119a3114836SGerry Liu 	{ DRMACH_DEVTYPE_MEM,	SBD_COMP_MEM },
120a3114836SGerry Liu 	{ DRMACH_DEVTYPE_CPU,	SBD_COMP_CPU },
121a3114836SGerry Liu 	{ DRMACH_DEVTYPE_PCI,	SBD_COMP_IO },
122a3114836SGerry Liu #if defined(DRMACH_DEVTYPE_SBUS)
123a3114836SGerry Liu 	{ DRMACH_DEVTYPE_SBUS,	SBD_COMP_IO },
124a3114836SGerry Liu #endif
125a3114836SGerry Liu #if defined(DRMACH_DEVTYPE_WCI)
126a3114836SGerry Liu 	{ DRMACH_DEVTYPE_WCI,	SBD_COMP_IO },
127a3114836SGerry Liu #endif
128a3114836SGerry Liu 	/* last s_devtype must be NULL, s_nodetype must be SBD_COMP_UNKNOWN */
129a3114836SGerry Liu 	{ NULL,			SBD_COMP_UNKNOWN }
130a3114836SGerry Liu };
131a3114836SGerry Liu 
132a3114836SGerry Liu /*
133a3114836SGerry Liu  * Per instance soft-state structure.
134a3114836SGerry Liu  */
135a3114836SGerry Liu typedef struct dr_softstate {
136a3114836SGerry Liu 	dev_info_t	*dip;
137a3114836SGerry Liu 	dr_board_t	*boards;
138a3114836SGerry Liu 	kmutex_t	 i_lock;
139a3114836SGerry Liu 	int		 dr_initialized;
140a3114836SGerry Liu } dr_softstate_t;
141a3114836SGerry Liu 
142a3114836SGerry Liu /*
143a3114836SGerry Liu  * dr Global data elements
144a3114836SGerry Liu  */
145a3114836SGerry Liu struct dr_global {
146a3114836SGerry Liu 	dr_softstate_t	*softsp;	/* pointer to initialize soft state */
147a3114836SGerry Liu 	kmutex_t	lock;
148a3114836SGerry Liu } dr_g;
149a3114836SGerry Liu 
150a3114836SGerry Liu dr_unsafe_devs_t	dr_unsafe_devs;
151a3114836SGerry Liu 
152a3114836SGerry Liu /*
153a3114836SGerry Liu  * Table of known passthru commands.
154a3114836SGerry Liu  */
155a3114836SGerry Liu struct {
156a3114836SGerry Liu 	char	*pt_name;
157a3114836SGerry Liu 	int	(*pt_func)(dr_handle_t *);
158a3114836SGerry Liu } pt_arr[] = {
159a3114836SGerry Liu 	"quiesce",		dr_pt_test_suspend,
160a3114836SGerry Liu };
161a3114836SGerry Liu 
162a3114836SGerry Liu int dr_modunload_okay = 0;		/* set to non-zero to allow unload */
163a3114836SGerry Liu 
164a3114836SGerry Liu /*
165a3114836SGerry Liu  * State transition table.  States valid transitions for "board" state.
166a3114836SGerry Liu  * Recall that non-zero return value terminates operation, however
167a3114836SGerry Liu  * the herrno value is what really indicates an error , if any.
168a3114836SGerry Liu  */
169a3114836SGerry Liu static int
_cmd2index(int c)170a3114836SGerry Liu _cmd2index(int c)
171a3114836SGerry Liu {
172a3114836SGerry Liu 	/*
173a3114836SGerry Liu 	 * Translate DR CMD to index into dr_state_transition.
174a3114836SGerry Liu 	 */
175a3114836SGerry Liu 	switch (c) {
176a3114836SGerry Liu 	case SBD_CMD_CONNECT:		return (0);
177a3114836SGerry Liu 	case SBD_CMD_DISCONNECT:	return (1);
178a3114836SGerry Liu 	case SBD_CMD_CONFIGURE:		return (2);
179a3114836SGerry Liu 	case SBD_CMD_UNCONFIGURE:	return (3);
180a3114836SGerry Liu 	case SBD_CMD_ASSIGN:		return (4);
181a3114836SGerry Liu 	case SBD_CMD_UNASSIGN:		return (5);
182a3114836SGerry Liu 	case SBD_CMD_POWERON:		return (6);
183a3114836SGerry Liu 	case SBD_CMD_POWEROFF:		return (7);
184a3114836SGerry Liu 	case SBD_CMD_TEST:		return (8);
185a3114836SGerry Liu 	default:			return (-1);
186a3114836SGerry Liu 	}
187a3114836SGerry Liu }
188a3114836SGerry Liu 
189a3114836SGerry Liu #define	CMD2INDEX(c)	_cmd2index(c)
190a3114836SGerry Liu 
191a3114836SGerry Liu static struct dr_state_trans {
192a3114836SGerry Liu 	int	x_cmd;
193a3114836SGerry Liu 	struct {
194a3114836SGerry Liu 		int	x_rv;		/* return value of pre_op */
195a3114836SGerry Liu 		int	x_err;		/* error, if any */
196a3114836SGerry Liu 	} x_op[DR_STATE_MAX];
197a3114836SGerry Liu } dr_state_transition[] = {
198a3114836SGerry Liu 	{ SBD_CMD_CONNECT,
199a3114836SGerry Liu 		{
200a3114836SGerry Liu 			{ 0, 0 },			/* empty */
201a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
202a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
203a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
204a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
205a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
206a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
207a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
208a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
209a3114836SGerry Liu 		}
210a3114836SGerry Liu 	},
211a3114836SGerry Liu 	{ SBD_CMD_DISCONNECT,
212a3114836SGerry Liu 		{
213a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* empty */
214a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
215a3114836SGerry Liu 			{ 0, 0 },			/* connected */
216a3114836SGerry Liu 			{ 0, 0 },			/* unconfigured */
217a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
218a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
219a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
220a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
221a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
222a3114836SGerry Liu 		}
223a3114836SGerry Liu 	},
224a3114836SGerry Liu 	{ SBD_CMD_CONFIGURE,
225a3114836SGerry Liu 		{
226a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* empty */
227a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* occupied */
228a3114836SGerry Liu 			{ 0, 0 },			/* connected */
229a3114836SGerry Liu 			{ 0, 0 },			/* unconfigured */
230a3114836SGerry Liu 			{ 0, 0 },			/* partial */
231a3114836SGerry Liu 			{ 0, 0 },			/* configured */
232a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
233a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
234a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
235a3114836SGerry Liu 		}
236a3114836SGerry Liu 	},
237a3114836SGerry Liu 	{ SBD_CMD_UNCONFIGURE,
238a3114836SGerry Liu 		{
239a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* empty */
240a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* occupied */
241a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
242a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
243a3114836SGerry Liu 			{ 0, 0 },			/* partial */
244a3114836SGerry Liu 			{ 0, 0 },			/* configured */
245a3114836SGerry Liu 			{ 0, 0 },			/* release */
246a3114836SGerry Liu 			{ 0, 0 },			/* unreferenced */
247a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
248a3114836SGerry Liu 		}
249a3114836SGerry Liu 	},
250a3114836SGerry Liu 	{ SBD_CMD_ASSIGN,
251a3114836SGerry Liu 		{
252a3114836SGerry Liu 			{ 0, 0 },			/* empty */
253a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
254a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
255a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
256a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
257a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
258a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
259a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
260a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
261a3114836SGerry Liu 		}
262a3114836SGerry Liu 	},
263a3114836SGerry Liu 	{ SBD_CMD_UNASSIGN,
264a3114836SGerry Liu 		{
265a3114836SGerry Liu 			{ 0, 0 },			/* empty */
266a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
267a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
268a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
269a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
270a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
271a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
272a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
273a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
274a3114836SGerry Liu 		}
275a3114836SGerry Liu 	},
276a3114836SGerry Liu 	{ SBD_CMD_POWERON,
277a3114836SGerry Liu 		{
278a3114836SGerry Liu 			{ 0, 0 },			/* empty */
279a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
280a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
281a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
282a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
283a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
284a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
285a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
286a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
287a3114836SGerry Liu 		}
288a3114836SGerry Liu 	},
289a3114836SGerry Liu 	{ SBD_CMD_POWEROFF,
290a3114836SGerry Liu 		{
291a3114836SGerry Liu 			{ 0, 0 },			/* empty */
292a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
293a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
294a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
295a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
296a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
297a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
298a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
299a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
300a3114836SGerry Liu 		}
301a3114836SGerry Liu 	},
302a3114836SGerry Liu 	{ SBD_CMD_TEST,
303a3114836SGerry Liu 		{
304a3114836SGerry Liu 			{ 0, 0 },			/* empty */
305a3114836SGerry Liu 			{ 0, 0 },			/* occupied */
306a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* connected */
307a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unconfigured */
308a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* partial */
309a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* configured */
310a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* release */
311a3114836SGerry Liu 			{ -1, ESBD_STATE },		/* unreferenced */
312a3114836SGerry Liu 			{ -1, ESBD_FATAL_STATE },	/* fatal */
313a3114836SGerry Liu 		}
314a3114836SGerry Liu 	},
315a3114836SGerry Liu };
316a3114836SGerry Liu 
317a3114836SGerry Liu /*
318a3114836SGerry Liu  * Global R/W lock to synchronize access across
319a3114836SGerry Liu  * multiple boards.  Users wanting multi-board access
320a3114836SGerry Liu  * must grab WRITE lock, others must grab READ lock.
321a3114836SGerry Liu  */
322a3114836SGerry Liu krwlock_t	dr_grwlock;
323a3114836SGerry Liu 
324a3114836SGerry Liu /*
325a3114836SGerry Liu  * Head of the boardlist used as a reference point for
326a3114836SGerry Liu  * locating board structs.
327a3114836SGerry Liu  * TODO: eliminate dr_boardlist
328a3114836SGerry Liu  */
329a3114836SGerry Liu dr_board_t	*dr_boardlist;
330a3114836SGerry Liu 
331a3114836SGerry Liu /*
332a3114836SGerry Liu  * DR support functions.
333a3114836SGerry Liu  */
334a3114836SGerry Liu static dr_devset_t	dr_dev2devset(sbd_comp_id_t *cid);
335a3114836SGerry Liu static int		dr_check_transition(dr_board_t *bp,
336a3114836SGerry Liu 					dr_devset_t *devsetp,
337a3114836SGerry Liu 					struct dr_state_trans *transp,
338a3114836SGerry Liu 					int cmd);
339a3114836SGerry Liu static int		dr_check_unit_attached(dr_common_unit_t *dp);
340a3114836SGerry Liu static sbd_error_t	*dr_init_devlists(dr_board_t *bp);
341a3114836SGerry Liu static void		dr_board_discovery(dr_board_t *bp);
342a3114836SGerry Liu static int		dr_board_init(dr_board_t *bp, dev_info_t *dip, int bd);
343a3114836SGerry Liu static void		dr_board_destroy(dr_board_t *bp);
344a3114836SGerry Liu static void		dr_board_transition(dr_board_t *bp, dr_state_t st);
345a3114836SGerry Liu 
346a3114836SGerry Liu /*
347a3114836SGerry Liu  * DR driver (DDI) entry points.
348a3114836SGerry Liu  */
349a3114836SGerry Liu static int	dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
350a3114836SGerry Liu 				void *arg, void **result);
351a3114836SGerry Liu static int	dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
352a3114836SGerry Liu static int	dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
353a3114836SGerry Liu static int	dr_probe(dev_info_t *dip);
354a3114836SGerry Liu static int	dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
355a3114836SGerry Liu 				cred_t *cred_p, int *rval_p);
356a3114836SGerry Liu static int	dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
357a3114836SGerry Liu static int	dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p);
358a3114836SGerry Liu 
359a3114836SGerry Liu /*
360a3114836SGerry Liu  * DR command processing operations.
361a3114836SGerry Liu  */
362a3114836SGerry Liu static int	dr_copyin_iocmd(dr_handle_t *hp);
363a3114836SGerry Liu static int	dr_copyout_iocmd(dr_handle_t *hp);
364a3114836SGerry Liu static int	dr_copyout_errs(dr_handle_t *hp);
365a3114836SGerry Liu static int	dr_pre_op(dr_handle_t *hp);
366a3114836SGerry Liu static int	dr_post_op(dr_handle_t *hp, int rv);
367a3114836SGerry Liu static int	dr_exec_op(dr_handle_t *hp);
368a3114836SGerry Liu static void	dr_assign_board(dr_handle_t *hp);
369a3114836SGerry Liu static void	dr_unassign_board(dr_handle_t *hp);
370a3114836SGerry Liu static void	dr_connect(dr_handle_t *hp);
371a3114836SGerry Liu static int	dr_disconnect(dr_handle_t *hp);
372a3114836SGerry Liu static void	dr_dev_configure(dr_handle_t *hp);
373a3114836SGerry Liu static void	dr_dev_release(dr_handle_t *hp);
374a3114836SGerry Liu static int	dr_dev_unconfigure(dr_handle_t *hp);
375a3114836SGerry Liu static void	dr_dev_cancel(dr_handle_t *hp);
376a3114836SGerry Liu static int	dr_dev_status(dr_handle_t *hp);
377a3114836SGerry Liu static int	dr_get_ncm(dr_handle_t *hp);
378a3114836SGerry Liu static int	dr_pt_ioctl(dr_handle_t *hp);
379a3114836SGerry Liu static void	dr_poweron_board(dr_handle_t *hp);
380a3114836SGerry Liu static void	dr_poweroff_board(dr_handle_t *hp);
381a3114836SGerry Liu static void	dr_test_board(dr_handle_t *hp);
382a3114836SGerry Liu 
383a3114836SGerry Liu /*
384a3114836SGerry Liu  * Autoconfiguration data structures
385a3114836SGerry Liu  */
386a3114836SGerry Liu struct cb_ops dr_cb_ops = {
387a3114836SGerry Liu 	dr_open,	/* open */
388a3114836SGerry Liu 	dr_close,	/* close */
389a3114836SGerry Liu 	nodev,		/* strategy */
390a3114836SGerry Liu 	nodev,		/* print */
391a3114836SGerry Liu 	nodev,		/* dump */
392a3114836SGerry Liu 	nodev,		/* read */
393a3114836SGerry Liu 	nodev,		/* write */
394a3114836SGerry Liu 	dr_ioctl,	/* ioctl */
395a3114836SGerry Liu 	nodev,		/* devmap */
396a3114836SGerry Liu 	nodev,		/* mmap */
397a3114836SGerry Liu 	nodev,		/* segmap */
398a3114836SGerry Liu 	nochpoll,	/* chpoll */
399a3114836SGerry Liu 	ddi_prop_op,	/* cb_prop_op */
400a3114836SGerry Liu 	NULL,		/* struct streamtab */
401a3114836SGerry Liu 	D_NEW | D_MP | D_MTSAFE,	/* compatibility flags */
402a3114836SGerry Liu 	CB_REV,		/* Rev */
403a3114836SGerry Liu 	nodev,		/* cb_aread */
404a3114836SGerry Liu 	nodev		/* cb_awrite */
405a3114836SGerry Liu };
406a3114836SGerry Liu 
407a3114836SGerry Liu struct dev_ops dr_dev_ops = {
408a3114836SGerry Liu 	DEVO_REV,	/* build version */
409a3114836SGerry Liu 	0,		/* dev ref count */
410a3114836SGerry Liu 	dr_getinfo,	/* getinfo */
411a3114836SGerry Liu 	nulldev,	/* identify */
412a3114836SGerry Liu 	dr_probe,	/* probe */
413a3114836SGerry Liu 	dr_attach,	/* attach */
414a3114836SGerry Liu 	dr_detach,	/* detach */
415a3114836SGerry Liu 	nodev,		/* reset */
416a3114836SGerry Liu 	&dr_cb_ops,	/* cb_ops */
417a3114836SGerry Liu 	(struct bus_ops *)NULL, /* bus ops */
418a3114836SGerry Liu 	NULL,		/* power */
419a3114836SGerry Liu 	ddi_quiesce_not_needed,	/* quiesce */
420a3114836SGerry Liu };
421a3114836SGerry Liu 
422a3114836SGerry Liu extern struct mod_ops mod_driverops;
423a3114836SGerry Liu 
424a3114836SGerry Liu static struct modldrv modldrv = {
425a3114836SGerry Liu 	&mod_driverops,
426a3114836SGerry Liu 	"Dynamic Reconfiguration",
427a3114836SGerry Liu 	&dr_dev_ops
428a3114836SGerry Liu };
429a3114836SGerry Liu 
430a3114836SGerry Liu static struct modlinkage modlinkage = {
431a3114836SGerry Liu 	MODREV_1,
432a3114836SGerry Liu 	(void *)&modldrv,
433a3114836SGerry Liu 	NULL
434a3114836SGerry Liu };
435a3114836SGerry Liu 
436a3114836SGerry Liu /*
437a3114836SGerry Liu  * Driver entry points.
438a3114836SGerry Liu  */
439a3114836SGerry Liu int
_init(void)440a3114836SGerry Liu _init(void)
441a3114836SGerry Liu {
442a3114836SGerry Liu 	int	err;
443a3114836SGerry Liu 
444a3114836SGerry Liu 	/*
445a3114836SGerry Liu 	 * If you need to support multiple nodes (instances), then
446a3114836SGerry Liu 	 * whatever the maximum number of supported nodes is would
447a3114836SGerry Liu 	 * need to passed as the third parameter to ddi_soft_state_init().
448a3114836SGerry Liu 	 * Alternative would be to dynamically fini and re-init the
449a3114836SGerry Liu 	 * soft state structure each time a node is attached.
450a3114836SGerry Liu 	 */
451a3114836SGerry Liu 	err = ddi_soft_state_init((void **)&dr_g.softsp,
452a3114836SGerry Liu 	    sizeof (dr_softstate_t), 1);
453a3114836SGerry Liu 	if (err)
454a3114836SGerry Liu 		return (err);
455a3114836SGerry Liu 
456a3114836SGerry Liu 	mutex_init(&dr_g.lock, NULL, MUTEX_DRIVER, NULL);
457a3114836SGerry Liu 	rw_init(&dr_grwlock, NULL, RW_DEFAULT, NULL);
458a3114836SGerry Liu 
459a3114836SGerry Liu 	return (mod_install(&modlinkage));
460a3114836SGerry Liu }
461a3114836SGerry Liu 
462a3114836SGerry Liu int
_fini(void)463a3114836SGerry Liu _fini(void)
464a3114836SGerry Liu {
465a3114836SGerry Liu 	int	err;
466a3114836SGerry Liu 
467a3114836SGerry Liu 	if ((err = mod_remove(&modlinkage)) != 0)
468a3114836SGerry Liu 		return (err);
469a3114836SGerry Liu 
470a3114836SGerry Liu 	mutex_destroy(&dr_g.lock);
471a3114836SGerry Liu 	rw_destroy(&dr_grwlock);
472a3114836SGerry Liu 
473a3114836SGerry Liu 	ddi_soft_state_fini((void **)&dr_g.softsp);
474a3114836SGerry Liu 
475a3114836SGerry Liu 	return (0);
476a3114836SGerry Liu }
477a3114836SGerry Liu 
478a3114836SGerry Liu int
_info(struct modinfo * modinfop)479a3114836SGerry Liu _info(struct modinfo *modinfop)
480a3114836SGerry Liu {
481a3114836SGerry Liu 	return (mod_info(&modlinkage, modinfop));
482a3114836SGerry Liu }
483a3114836SGerry Liu 
484a3114836SGerry Liu /*ARGSUSED1*/
485a3114836SGerry Liu static int
dr_open(dev_t * dev,int flag,int otyp,cred_t * cred_p)486a3114836SGerry Liu dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p)
487a3114836SGerry Liu {
488a3114836SGerry Liu 	int		 instance;
489a3114836SGerry Liu 	dr_softstate_t	*softsp;
490a3114836SGerry Liu 	dr_board_t	*bp;
491a3114836SGerry Liu 
492a3114836SGerry Liu 	/*
493a3114836SGerry Liu 	 * Don't open unless we've attached.
494a3114836SGerry Liu 	 */
495a3114836SGerry Liu 	instance = DR_MINOR2INST(getminor(*dev));
496a3114836SGerry Liu 	softsp = ddi_get_soft_state(dr_g.softsp, instance);
497a3114836SGerry Liu 	if (softsp == NULL)
498a3114836SGerry Liu 		return (ENXIO);
499a3114836SGerry Liu 
500a3114836SGerry Liu 	mutex_enter(&softsp->i_lock);
501a3114836SGerry Liu 	if (!softsp->dr_initialized) {
502a3114836SGerry Liu 		int		 bd;
503a3114836SGerry Liu 		int		 rv = 0;
504a3114836SGerry Liu 
505a3114836SGerry Liu 		bp = softsp->boards;
506a3114836SGerry Liu 
507a3114836SGerry Liu 		/* initialize each array element */
508a3114836SGerry Liu 		for (bd = 0; bd < MAX_BOARDS; bd++, bp++) {
509a3114836SGerry Liu 			rv = dr_board_init(bp, softsp->dip, bd);
510a3114836SGerry Liu 			if (rv)
511a3114836SGerry Liu 				break;
512a3114836SGerry Liu 		}
513a3114836SGerry Liu 
514a3114836SGerry Liu 		if (rv == 0) {
515a3114836SGerry Liu 			softsp->dr_initialized = 1;
516a3114836SGerry Liu 		} else {
517a3114836SGerry Liu 			/* destroy elements initialized thus far */
518a3114836SGerry Liu 			while (--bp >= softsp->boards)
519a3114836SGerry Liu 				dr_board_destroy(bp);
520a3114836SGerry Liu 
521a3114836SGerry Liu 			/* TODO: should this be another errno val ? */
522a3114836SGerry Liu 			mutex_exit(&softsp->i_lock);
523a3114836SGerry Liu 			return (ENXIO);
524a3114836SGerry Liu 		}
525a3114836SGerry Liu 	}
526a3114836SGerry Liu 	mutex_exit(&softsp->i_lock);
527a3114836SGerry Liu 
528a3114836SGerry Liu 	bp = &softsp->boards[DR_MINOR2BNUM(getminor(*dev))];
529a3114836SGerry Liu 
530a3114836SGerry Liu 	/*
531a3114836SGerry Liu 	 * prevent opening of a dyn-ap for a board
532a3114836SGerry Liu 	 * that does not exist
533a3114836SGerry Liu 	 */
534a3114836SGerry Liu 	if (!bp->b_assigned) {
535a3114836SGerry Liu 		if (drmach_board_lookup(bp->b_num, &bp->b_id) != 0)
536a3114836SGerry Liu 			return (ENODEV);
537a3114836SGerry Liu 	}
538a3114836SGerry Liu 
539a3114836SGerry Liu 	return (0);
540a3114836SGerry Liu }
541a3114836SGerry Liu 
542a3114836SGerry Liu /*ARGSUSED*/
543a3114836SGerry Liu static int
dr_close(dev_t dev,int flag,int otyp,cred_t * cred_p)544a3114836SGerry Liu dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
545a3114836SGerry Liu {
546a3114836SGerry Liu 	return (0);
547a3114836SGerry Liu }
548a3114836SGerry Liu 
549a3114836SGerry Liu /*
550a3114836SGerry Liu  * Enable/disable DR features.
551a3114836SGerry Liu  */
552a3114836SGerry Liu int dr_enable = 1;
553a3114836SGerry Liu 
554a3114836SGerry Liu /*ARGSUSED3*/
555a3114836SGerry Liu static int
dr_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)556a3114836SGerry Liu dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
5572002b186SToomas Soome     cred_t *cred_p, int *rval_p)
558a3114836SGerry Liu {
559a3114836SGerry Liu 	int		rv = 0;
560a3114836SGerry Liu 	int		instance;
561a3114836SGerry Liu 	int		bd;
562a3114836SGerry Liu 	dr_handle_t	*hp;
563a3114836SGerry Liu 	dr_softstate_t	*softsp;
564a3114836SGerry Liu 	static fn_t	f = "dr_ioctl";
565a3114836SGerry Liu 
566a3114836SGerry Liu 	PR_ALL("%s...\n", f);
567a3114836SGerry Liu 
568a3114836SGerry Liu 	instance = DR_MINOR2INST(getminor(dev));
569a3114836SGerry Liu 	softsp = ddi_get_soft_state(dr_g.softsp, instance);
570a3114836SGerry Liu 	if (softsp == NULL) {
571a3114836SGerry Liu 		cmn_err(CE_WARN, "dr%d: module not yet attached", instance);
572a3114836SGerry Liu 		return (ENXIO);
573a3114836SGerry Liu 	}
574a3114836SGerry Liu 
575a3114836SGerry Liu 	if (!dr_enable) {
576a3114836SGerry Liu 		switch (cmd) {
577a3114836SGerry Liu 			case SBD_CMD_STATUS:
578a3114836SGerry Liu 			case SBD_CMD_GETNCM:
579a3114836SGerry Liu 			case SBD_CMD_PASSTHRU:
580a3114836SGerry Liu 				break;
581a3114836SGerry Liu 			default:
582a3114836SGerry Liu 				return (ENOTSUP);
583a3114836SGerry Liu 		}
584a3114836SGerry Liu 	}
585a3114836SGerry Liu 
586a3114836SGerry Liu 	bd = DR_MINOR2BNUM(getminor(dev));
587a3114836SGerry Liu 	if (bd >= MAX_BOARDS)
588a3114836SGerry Liu 		return (ENXIO);
589a3114836SGerry Liu 
590a3114836SGerry Liu 	/* get and initialize storage for new handle */
591a3114836SGerry Liu 	hp = GETSTRUCT(dr_handle_t, 1);
592a3114836SGerry Liu 	hp->h_bd = &softsp->boards[bd];
593a3114836SGerry Liu 	hp->h_err = NULL;
594a3114836SGerry Liu 	hp->h_dev = getminor(dev);
595a3114836SGerry Liu 	hp->h_cmd = cmd;
596a3114836SGerry Liu 	hp->h_mode = mode;
597a3114836SGerry Liu 	hp->h_iap = (sbd_ioctl_arg_t *)arg;
598a3114836SGerry Liu 
599a3114836SGerry Liu 	/* copy sbd command into handle */
600a3114836SGerry Liu 	rv = dr_copyin_iocmd(hp);
601a3114836SGerry Liu 	if (rv) {
602a3114836SGerry Liu 		FREESTRUCT(hp, dr_handle_t, 1);
603a3114836SGerry Liu 		return (EINVAL);
604a3114836SGerry Liu 	}
605a3114836SGerry Liu 
606a3114836SGerry Liu 	/* translate canonical name to component type */
607a3114836SGerry Liu 	if (hp->h_sbdcmd.cmd_cm.c_id.c_name[0] != '\0') {
608a3114836SGerry Liu 		hp->h_sbdcmd.cmd_cm.c_id.c_type =
609a3114836SGerry Liu 		    dr_dev_type_to_nt(hp->h_sbdcmd.cmd_cm.c_id.c_name);
610a3114836SGerry Liu 
611a3114836SGerry Liu 		PR_ALL("%s: c_name = %s, c_type = %d\n",
612a3114836SGerry Liu 		    f,
613a3114836SGerry Liu 		    hp->h_sbdcmd.cmd_cm.c_id.c_name,
614a3114836SGerry Liu 		    hp->h_sbdcmd.cmd_cm.c_id.c_type);
615a3114836SGerry Liu 	} else {
616a3114836SGerry Liu 		/*EMPTY*/
617a3114836SGerry Liu 		PR_ALL("%s: c_name is NULL\n", f);
618a3114836SGerry Liu 	}
619a3114836SGerry Liu 
620a3114836SGerry Liu 	/* determine scope of operation */
621a3114836SGerry Liu 	hp->h_devset = dr_dev2devset(&hp->h_sbdcmd.cmd_cm.c_id);
622a3114836SGerry Liu 
623a3114836SGerry Liu 	switch (hp->h_cmd) {
624a3114836SGerry Liu 	case SBD_CMD_STATUS:
625a3114836SGerry Liu 	case SBD_CMD_GETNCM:
626a3114836SGerry Liu 		/* no locks needed for these commands */
627a3114836SGerry Liu 		break;
628a3114836SGerry Liu 
629a3114836SGerry Liu 	default:
630a3114836SGerry Liu 		rw_enter(&dr_grwlock, RW_WRITER);
631a3114836SGerry Liu 		mutex_enter(&hp->h_bd->b_lock);
632a3114836SGerry Liu 
633a3114836SGerry Liu 		/*
634a3114836SGerry Liu 		 * If we're dealing with memory at all, then we have
635a3114836SGerry Liu 		 * to keep the "exclusive" global lock held.  This is
636a3114836SGerry Liu 		 * necessary since we will probably need to look at
637a3114836SGerry Liu 		 * multiple board structs.  Otherwise, we only have
638a3114836SGerry Liu 		 * to deal with the board in question and so can drop
639a3114836SGerry Liu 		 * the global lock to "shared".
640a3114836SGerry Liu 		 */
641a3114836SGerry Liu 		rv = DEVSET_IN_SET(hp->h_devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
642a3114836SGerry Liu 		if (rv == 0)
643a3114836SGerry Liu 			rw_downgrade(&dr_grwlock);
644a3114836SGerry Liu 		break;
645a3114836SGerry Liu 	}
646a3114836SGerry Liu 	rv = 0;
647a3114836SGerry Liu 
648a3114836SGerry Liu 	if (rv == 0)
649a3114836SGerry Liu 		rv = dr_pre_op(hp);
650a3114836SGerry Liu 	if (rv == 0) {
651a3114836SGerry Liu 		rv = dr_exec_op(hp);
652a3114836SGerry Liu 		rv = dr_post_op(hp, rv);
653a3114836SGerry Liu 	}
654a3114836SGerry Liu 
655a3114836SGerry Liu 	if (rv == -1)
656a3114836SGerry Liu 		rv = EIO;
657a3114836SGerry Liu 
658a3114836SGerry Liu 	if (hp->h_err != NULL)
659a3114836SGerry Liu 		if (!(rv = dr_copyout_errs(hp)))
660a3114836SGerry Liu 			rv = EIO;
661a3114836SGerry Liu 
662a3114836SGerry Liu 	/* undo locking, if any, done before dr_pre_op */
663a3114836SGerry Liu 	switch (hp->h_cmd) {
664a3114836SGerry Liu 	case SBD_CMD_STATUS:
665a3114836SGerry Liu 	case SBD_CMD_GETNCM:
666a3114836SGerry Liu 		break;
667a3114836SGerry Liu 
668a3114836SGerry Liu 	case SBD_CMD_ASSIGN:
669a3114836SGerry Liu 	case SBD_CMD_UNASSIGN:
670a3114836SGerry Liu 	case SBD_CMD_POWERON:
671a3114836SGerry Liu 	case SBD_CMD_POWEROFF:
672a3114836SGerry Liu 	case SBD_CMD_CONNECT:
673a3114836SGerry Liu 	case SBD_CMD_CONFIGURE:
674a3114836SGerry Liu 	case SBD_CMD_UNCONFIGURE:
675a3114836SGerry Liu 	case SBD_CMD_DISCONNECT:
676a3114836SGerry Liu 		/* Board changed state. Log a sysevent. */
677a3114836SGerry Liu 		if (rv == 0)
678a3114836SGerry Liu 			(void) drmach_log_sysevent(hp->h_bd->b_num, "",
679a3114836SGerry Liu 			    SE_SLEEP, 0);
680a3114836SGerry Liu 		/* Fall through */
681a3114836SGerry Liu 
682a3114836SGerry Liu 	default:
683a3114836SGerry Liu 		mutex_exit(&hp->h_bd->b_lock);
684a3114836SGerry Liu 		rw_exit(&dr_grwlock);
685a3114836SGerry Liu 	}
686a3114836SGerry Liu 
687a3114836SGerry Liu 	if (hp->h_opts.size != 0)
688a3114836SGerry Liu 		FREESTRUCT(hp->h_opts.copts, char, hp->h_opts.size);
689a3114836SGerry Liu 
690a3114836SGerry Liu 	FREESTRUCT(hp, dr_handle_t, 1);
691a3114836SGerry Liu 
692a3114836SGerry Liu 	return (rv);
693a3114836SGerry Liu }
694a3114836SGerry Liu 
695a3114836SGerry Liu /*ARGSUSED*/
696a3114836SGerry Liu static int
dr_probe(dev_info_t * dip)697a3114836SGerry Liu dr_probe(dev_info_t *dip)
698a3114836SGerry Liu {
699a3114836SGerry Liu 	return (DDI_PROBE_SUCCESS);
700a3114836SGerry Liu }
701a3114836SGerry Liu 
702a3114836SGerry Liu static int
dr_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)703a3114836SGerry Liu dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
704a3114836SGerry Liu {
705a3114836SGerry Liu 	int		rv, rv2;
706a3114836SGerry Liu 	int		bd;
7072002b186SToomas Soome 	int		instance;
708a3114836SGerry Liu 	sbd_error_t	*err;
709a3114836SGerry Liu 	dr_softstate_t	*softsp;
710a3114836SGerry Liu 
711a3114836SGerry Liu 	instance = ddi_get_instance(dip);
712a3114836SGerry Liu 
713a3114836SGerry Liu 	switch (cmd) {
714a3114836SGerry Liu 	case DDI_ATTACH:
715a3114836SGerry Liu 		rw_enter(&dr_grwlock, RW_WRITER);
716a3114836SGerry Liu 
717a3114836SGerry Liu 		rv = ddi_soft_state_zalloc(dr_g.softsp, instance);
718a3114836SGerry Liu 		if (rv != DDI_SUCCESS) {
719a3114836SGerry Liu 			cmn_err(CE_WARN, "dr%d: failed to alloc soft-state",
720a3114836SGerry Liu 			    instance);
721a3114836SGerry Liu 			return (DDI_FAILURE);
722a3114836SGerry Liu 		}
723a3114836SGerry Liu 
724a3114836SGerry Liu 		/* initialize softstate structure */
725a3114836SGerry Liu 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
726a3114836SGerry Liu 		softsp->dip = dip;
727a3114836SGerry Liu 
728a3114836SGerry Liu 		mutex_init(&softsp->i_lock, NULL, MUTEX_DRIVER, NULL);
729a3114836SGerry Liu 
730a3114836SGerry Liu 		/* allocate board array (aka boardlist) */
731a3114836SGerry Liu 		softsp->boards = GETSTRUCT(dr_board_t, MAX_BOARDS);
732a3114836SGerry Liu 
733a3114836SGerry Liu 		/* TODO: eliminate dr_boardlist */
734a3114836SGerry Liu 		dr_boardlist = softsp->boards;
735a3114836SGerry Liu 
736a3114836SGerry Liu 		/* initialize each array element */
737a3114836SGerry Liu 		rv = DDI_SUCCESS;
738a3114836SGerry Liu 		for (bd = 0; bd < MAX_BOARDS; bd++) {
739a3114836SGerry Liu 			dr_board_t	*bp = &softsp->boards[bd];
740a3114836SGerry Liu 			char		*p, *name;
741a3114836SGerry Liu 			int		 l, minor_num;
742a3114836SGerry Liu 
743a3114836SGerry Liu 			/*
744a3114836SGerry Liu 			 * initialized board attachment point path
745a3114836SGerry Liu 			 * (relative to pseudo) in a form immediately
746a3114836SGerry Liu 			 * reusable as an cfgadm command argument.
747a3114836SGerry Liu 			 * TODO: clean this up
748a3114836SGerry Liu 			 */
749a3114836SGerry Liu 			p = bp->b_path;
750a3114836SGerry Liu 			l = sizeof (bp->b_path);
751a3114836SGerry Liu 			(void) snprintf(p, l, "dr@%d:", instance);
752a3114836SGerry Liu 			while (*p != '\0') {
753a3114836SGerry Liu 				l--;
754a3114836SGerry Liu 				p++;
755a3114836SGerry Liu 			}
756a3114836SGerry Liu 
757a3114836SGerry Liu 			name = p;
758a3114836SGerry Liu 			err = drmach_board_name(bd, p, l);
759a3114836SGerry Liu 			if (err) {
760a3114836SGerry Liu 				sbd_err_clear(&err);
761a3114836SGerry Liu 				rv = DDI_FAILURE;
762a3114836SGerry Liu 				break;
763a3114836SGerry Liu 			}
764a3114836SGerry Liu 
765a3114836SGerry Liu 			minor_num = DR_MAKE_MINOR(instance, bd);
766a3114836SGerry Liu 			rv = ddi_create_minor_node(dip, name, S_IFCHR,
7672002b186SToomas Soome 			    minor_num, DDI_NT_SBD_ATTACHMENT_POINT, 0);
768a3114836SGerry Liu 			if (rv != DDI_SUCCESS)
769a3114836SGerry Liu 				rv = DDI_FAILURE;
770a3114836SGerry Liu 		}
771a3114836SGerry Liu 
772a3114836SGerry Liu 		if (rv == DDI_SUCCESS) {
773a3114836SGerry Liu 			/*
774a3114836SGerry Liu 			 * Announce the node's presence.
775a3114836SGerry Liu 			 */
776a3114836SGerry Liu 			ddi_report_dev(dip);
777a3114836SGerry Liu 		} else {
778a3114836SGerry Liu 			ddi_remove_minor_node(dip, NULL);
779a3114836SGerry Liu 		}
780a3114836SGerry Liu 		/*
781a3114836SGerry Liu 		 * Init registered unsafe devs.
782a3114836SGerry Liu 		 */
783a3114836SGerry Liu 		dr_unsafe_devs.devnames = NULL;
784a3114836SGerry Liu 		rv2 = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
785a3114836SGerry Liu 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
786a3114836SGerry Liu 		    "unsupported-io-drivers", &dr_unsafe_devs.devnames,
787a3114836SGerry Liu 		    &dr_unsafe_devs.ndevs);
788a3114836SGerry Liu 
789a3114836SGerry Liu 		if (rv2 != DDI_PROP_SUCCESS)
790a3114836SGerry Liu 			dr_unsafe_devs.ndevs = 0;
791a3114836SGerry Liu 
792a3114836SGerry Liu 		rw_exit(&dr_grwlock);
793a3114836SGerry Liu 		return (rv);
794a3114836SGerry Liu 
795a3114836SGerry Liu 	default:
796a3114836SGerry Liu 		return (DDI_FAILURE);
797a3114836SGerry Liu 	}
798a3114836SGerry Liu 
799a3114836SGerry Liu 	/*NOTREACHED*/
800a3114836SGerry Liu }
801a3114836SGerry Liu 
802a3114836SGerry Liu static int
dr_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)803a3114836SGerry Liu dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
804a3114836SGerry Liu {
8052002b186SToomas Soome 	int		instance;
806a3114836SGerry Liu 	dr_softstate_t	*softsp;
807a3114836SGerry Liu 
808a3114836SGerry Liu 	switch (cmd) {
809a3114836SGerry Liu 	case DDI_DETACH:
810a3114836SGerry Liu 		if (!dr_modunload_okay)
811a3114836SGerry Liu 			return (DDI_FAILURE);
812a3114836SGerry Liu 
813a3114836SGerry Liu 		rw_enter(&dr_grwlock, RW_WRITER);
814a3114836SGerry Liu 
815a3114836SGerry Liu 		instance = ddi_get_instance(dip);
816a3114836SGerry Liu 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
817a3114836SGerry Liu 
818a3114836SGerry Liu 		/* TODO: eliminate dr_boardlist */
819a3114836SGerry Liu 		ASSERT(softsp->boards == dr_boardlist);
820a3114836SGerry Liu 
821a3114836SGerry Liu 		/* remove all minor nodes */
822a3114836SGerry Liu 		ddi_remove_minor_node(dip, NULL);
823a3114836SGerry Liu 
824a3114836SGerry Liu 		if (softsp->dr_initialized) {
825a3114836SGerry Liu 			int bd;
826a3114836SGerry Liu 
827a3114836SGerry Liu 			for (bd = 0; bd < MAX_BOARDS; bd++)
828a3114836SGerry Liu 				dr_board_destroy(&softsp->boards[bd]);
829a3114836SGerry Liu 		}
830a3114836SGerry Liu 
831a3114836SGerry Liu 		FREESTRUCT(softsp->boards, dr_board_t, MAX_BOARDS);
832a3114836SGerry Liu 		mutex_destroy(&softsp->i_lock);
833a3114836SGerry Liu 		ddi_soft_state_free(dr_g.softsp, instance);
834a3114836SGerry Liu 
835a3114836SGerry Liu 		rw_exit(&dr_grwlock);
836a3114836SGerry Liu 		return (DDI_SUCCESS);
837a3114836SGerry Liu 
838a3114836SGerry Liu 	default:
839a3114836SGerry Liu 		return (DDI_FAILURE);
840a3114836SGerry Liu 	}
841a3114836SGerry Liu 	/*NOTREACHED*/
842a3114836SGerry Liu }
843a3114836SGerry Liu 
844a3114836SGerry Liu static int
dr_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)845a3114836SGerry Liu dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
846a3114836SGerry Liu {
847a3114836SGerry Liu 	_NOTE(ARGUNUSED(dip))
848a3114836SGerry Liu 
849a3114836SGerry Liu 	dev_t		dev = (dev_t)arg;
850a3114836SGerry Liu 	int		instance, error;
851a3114836SGerry Liu 	dr_softstate_t	*softsp;
852a3114836SGerry Liu 
853a3114836SGerry Liu 	*result = NULL;
854a3114836SGerry Liu 	error = DDI_SUCCESS;
855a3114836SGerry Liu 	instance = DR_MINOR2INST(getminor(dev));
856a3114836SGerry Liu 
857a3114836SGerry Liu 	switch (cmd) {
858a3114836SGerry Liu 	case DDI_INFO_DEVT2DEVINFO:
859a3114836SGerry Liu 		softsp = ddi_get_soft_state(dr_g.softsp, instance);
860a3114836SGerry Liu 		if (softsp == NULL)
861a3114836SGerry Liu 			return (DDI_FAILURE);
862a3114836SGerry Liu 		*result = (void *)softsp->dip;
863a3114836SGerry Liu 		break;
864a3114836SGerry Liu 
865a3114836SGerry Liu 	case DDI_INFO_DEVT2INSTANCE:
866a3114836SGerry Liu 		*result = (void *)(uintptr_t)instance;
867a3114836SGerry Liu 		break;
868a3114836SGerry Liu 
869a3114836SGerry Liu 	default:
870a3114836SGerry Liu 		error = DDI_FAILURE;
871a3114836SGerry Liu 		break;
872a3114836SGerry Liu 	}
873a3114836SGerry Liu 
874a3114836SGerry Liu 	return (error);
875a3114836SGerry Liu }
876a3114836SGerry Liu 
877a3114836SGerry Liu /*
878a3114836SGerry Liu  * DR operations.
879a3114836SGerry Liu  */
880a3114836SGerry Liu 
881a3114836SGerry Liu static int
dr_copyin_iocmd(dr_handle_t * hp)882a3114836SGerry Liu dr_copyin_iocmd(dr_handle_t *hp)
883a3114836SGerry Liu {
884a3114836SGerry Liu 	static fn_t	f = "dr_copyin_iocmd";
885a3114836SGerry Liu 	sbd_cmd_t	*scp = &hp->h_sbdcmd;
886a3114836SGerry Liu 
887a3114836SGerry Liu 	if (hp->h_iap == NULL)
888a3114836SGerry Liu 		return (EINVAL);
889a3114836SGerry Liu 
890a3114836SGerry Liu 	bzero((caddr_t)scp, sizeof (sbd_cmd_t));
891a3114836SGerry Liu 
892a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
893a3114836SGerry Liu 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
894a3114836SGerry Liu 		sbd_cmd32_t	scmd32;
895a3114836SGerry Liu 
896a3114836SGerry Liu 		bzero((caddr_t)&scmd32, sizeof (sbd_cmd32_t));
897a3114836SGerry Liu 
898a3114836SGerry Liu 		if (ddi_copyin((void *)hp->h_iap, (void *)&scmd32,
899a3114836SGerry Liu 		    sizeof (sbd_cmd32_t), hp->h_mode)) {
900a3114836SGerry Liu 			cmn_err(CE_WARN,
901a3114836SGerry Liu 			    "%s: (32bit) failed to copyin "
902a3114836SGerry Liu 			    "sbdcmd-struct", f);
903a3114836SGerry Liu 			return (EFAULT);
904a3114836SGerry Liu 		}
905a3114836SGerry Liu 		scp->cmd_cm.c_id.c_type = scmd32.cmd_cm.c_id.c_type;
906a3114836SGerry Liu 		scp->cmd_cm.c_id.c_unit = scmd32.cmd_cm.c_id.c_unit;
907a3114836SGerry Liu 		bcopy(&scmd32.cmd_cm.c_id.c_name[0],
908a3114836SGerry Liu 		    &scp->cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
909a3114836SGerry Liu 		scp->cmd_cm.c_flags = scmd32.cmd_cm.c_flags;
910a3114836SGerry Liu 		scp->cmd_cm.c_len = scmd32.cmd_cm.c_len;
911a3114836SGerry Liu 		scp->cmd_cm.c_opts = (caddr_t)(uintptr_t)scmd32.cmd_cm.c_opts;
912a3114836SGerry Liu 
913a3114836SGerry Liu 		switch (hp->h_cmd) {
914a3114836SGerry Liu 		case SBD_CMD_STATUS:
915a3114836SGerry Liu 			scp->cmd_stat.s_nbytes = scmd32.cmd_stat.s_nbytes;
916a3114836SGerry Liu 			scp->cmd_stat.s_statp =
917a3114836SGerry Liu 			    (caddr_t)(uintptr_t)scmd32.cmd_stat.s_statp;
918a3114836SGerry Liu 			break;
919a3114836SGerry Liu 		default:
920a3114836SGerry Liu 			break;
921a3114836SGerry Liu 
922a3114836SGerry Liu 		}
923a3114836SGerry Liu 	} else
924a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
925a3114836SGerry Liu 	if (ddi_copyin((void *)hp->h_iap, (void *)scp,
926a3114836SGerry Liu 	    sizeof (sbd_cmd_t), hp->h_mode) != 0) {
927a3114836SGerry Liu 		cmn_err(CE_WARN,
928a3114836SGerry Liu 		    "%s: failed to copyin sbdcmd-struct", f);
929a3114836SGerry Liu 		return (EFAULT);
930a3114836SGerry Liu 	}
931a3114836SGerry Liu 
932a3114836SGerry Liu 	if ((hp->h_opts.size = scp->cmd_cm.c_len) != 0) {
933a3114836SGerry Liu 		hp->h_opts.copts = GETSTRUCT(char, scp->cmd_cm.c_len + 1);
934a3114836SGerry Liu 		++hp->h_opts.size;
935a3114836SGerry Liu 		if (ddi_copyin((void *)scp->cmd_cm.c_opts,
936a3114836SGerry Liu 		    (void *)hp->h_opts.copts,
937a3114836SGerry Liu 		    scp->cmd_cm.c_len, hp->h_mode) != 0) {
938a3114836SGerry Liu 			cmn_err(CE_WARN, "%s: failed to copyin options", f);
939a3114836SGerry Liu 			return (EFAULT);
940a3114836SGerry Liu 		}
941a3114836SGerry Liu 	}
942a3114836SGerry Liu 
943a3114836SGerry Liu 	return (0);
944a3114836SGerry Liu }
945a3114836SGerry Liu 
946a3114836SGerry Liu static int
dr_copyout_iocmd(dr_handle_t * hp)947a3114836SGerry Liu dr_copyout_iocmd(dr_handle_t *hp)
948a3114836SGerry Liu {
949a3114836SGerry Liu 	static fn_t	f = "dr_copyout_iocmd";
950a3114836SGerry Liu 	sbd_cmd_t	*scp = &hp->h_sbdcmd;
951a3114836SGerry Liu 
952a3114836SGerry Liu 	if (hp->h_iap == NULL)
953a3114836SGerry Liu 		return (EINVAL);
954a3114836SGerry Liu 
955a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
956a3114836SGerry Liu 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
957a3114836SGerry Liu 		sbd_cmd32_t	scmd32;
958a3114836SGerry Liu 
959a3114836SGerry Liu 		scmd32.cmd_cm.c_id.c_type = scp->cmd_cm.c_id.c_type;
960a3114836SGerry Liu 		scmd32.cmd_cm.c_id.c_unit = scp->cmd_cm.c_id.c_unit;
961a3114836SGerry Liu 		bcopy(&scp->cmd_cm.c_id.c_name[0],
962a3114836SGerry Liu 		    &scmd32.cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
963a3114836SGerry Liu 
964a3114836SGerry Liu 		scmd32.cmd_cm.c_flags = scp->cmd_cm.c_flags;
965a3114836SGerry Liu 		scmd32.cmd_cm.c_len = scp->cmd_cm.c_len;
966a3114836SGerry Liu 		scmd32.cmd_cm.c_opts = (caddr32_t)(uintptr_t)scp->cmd_cm.c_opts;
967a3114836SGerry Liu 
968a3114836SGerry Liu 		switch (hp->h_cmd) {
969a3114836SGerry Liu 		case SBD_CMD_GETNCM:
970a3114836SGerry Liu 			scmd32.cmd_getncm.g_ncm = scp->cmd_getncm.g_ncm;
971a3114836SGerry Liu 			break;
972a3114836SGerry Liu 		default:
973a3114836SGerry Liu 			break;
974a3114836SGerry Liu 		}
975a3114836SGerry Liu 
976a3114836SGerry Liu 		if (ddi_copyout((void *)&scmd32, (void *)hp->h_iap,
977a3114836SGerry Liu 		    sizeof (sbd_cmd32_t), hp->h_mode)) {
978a3114836SGerry Liu 			cmn_err(CE_WARN,
979a3114836SGerry Liu 			    "%s: (32bit) failed to copyout "
980a3114836SGerry Liu 			    "sbdcmd-struct", f);
981a3114836SGerry Liu 			return (EFAULT);
982a3114836SGerry Liu 		}
983a3114836SGerry Liu 	} else
984a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
985a3114836SGerry Liu 	if (ddi_copyout((void *)scp, (void *)hp->h_iap,
986a3114836SGerry Liu 	    sizeof (sbd_cmd_t), hp->h_mode) != 0) {
987a3114836SGerry Liu 		cmn_err(CE_WARN,
988a3114836SGerry Liu 		    "%s: failed to copyout sbdcmd-struct", f);
989a3114836SGerry Liu 		return (EFAULT);
990a3114836SGerry Liu 	}
991a3114836SGerry Liu 
992a3114836SGerry Liu 	return (0);
993a3114836SGerry Liu }
994a3114836SGerry Liu 
995a3114836SGerry Liu static int
dr_copyout_errs(dr_handle_t * hp)996a3114836SGerry Liu dr_copyout_errs(dr_handle_t *hp)
997a3114836SGerry Liu {
998a3114836SGerry Liu 	static fn_t	f = "dr_copyout_errs";
999a3114836SGerry Liu 
1000a3114836SGerry Liu 	if (hp->h_err == NULL)
1001a3114836SGerry Liu 		return (0);
1002a3114836SGerry Liu 
1003a3114836SGerry Liu 	if (hp->h_err->e_code) {
1004a3114836SGerry Liu 		PR_ALL("%s: error %d %s",
1005a3114836SGerry Liu 		    f, hp->h_err->e_code, hp->h_err->e_rsc);
1006a3114836SGerry Liu 	}
1007a3114836SGerry Liu 
1008a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
1009a3114836SGerry Liu 	if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
1010a3114836SGerry Liu 		sbd_error32_t	*serr32p;
1011a3114836SGerry Liu 
1012a3114836SGerry Liu 		serr32p = GETSTRUCT(sbd_error32_t, 1);
1013a3114836SGerry Liu 
1014a3114836SGerry Liu 		serr32p->e_code = hp->h_err->e_code;
1015a3114836SGerry Liu 		bcopy(&hp->h_err->e_rsc[0], &serr32p->e_rsc[0],
1016a3114836SGerry Liu 		    MAXPATHLEN);
1017a3114836SGerry Liu 		if (ddi_copyout((void *)serr32p,
1018a3114836SGerry Liu 		    (void *)&((sbd_ioctl_arg32_t *)hp->h_iap)->i_err,
1019a3114836SGerry Liu 		    sizeof (sbd_error32_t), hp->h_mode)) {
1020a3114836SGerry Liu 			cmn_err(CE_WARN,
1021a3114836SGerry Liu 			    "%s: (32bit) failed to copyout", f);
1022a3114836SGerry Liu 			return (EFAULT);
1023a3114836SGerry Liu 		}
1024a3114836SGerry Liu 		FREESTRUCT(serr32p, sbd_error32_t, 1);
1025a3114836SGerry Liu 	} else
1026a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
1027a3114836SGerry Liu 	if (ddi_copyout((void *)hp->h_err,
1028a3114836SGerry Liu 	    (void *)&hp->h_iap->i_err,
1029a3114836SGerry Liu 	    sizeof (sbd_error_t), hp->h_mode)) {
1030a3114836SGerry Liu 		cmn_err(CE_WARN,
1031a3114836SGerry Liu 		    "%s: failed to copyout", f);
1032a3114836SGerry Liu 		return (EFAULT);
1033a3114836SGerry Liu 	}
1034a3114836SGerry Liu 
1035a3114836SGerry Liu 	sbd_err_clear(&hp->h_err);
1036a3114836SGerry Liu 
1037a3114836SGerry Liu 	return (0);
1038a3114836SGerry Liu 
1039a3114836SGerry Liu }
1040a3114836SGerry Liu 
1041a3114836SGerry Liu /*
1042a3114836SGerry Liu  * pre-op entry point must sbd_err_set_c(), if needed.
1043a3114836SGerry Liu  * Return value of non-zero indicates failure.
1044a3114836SGerry Liu  */
1045a3114836SGerry Liu static int
dr_pre_op(dr_handle_t * hp)1046a3114836SGerry Liu dr_pre_op(dr_handle_t *hp)
1047a3114836SGerry Liu {
1048a3114836SGerry Liu 	int		rv = 0, t;
1049a3114836SGerry Liu 	int		cmd, serr = 0;
1050a3114836SGerry Liu 	dr_devset_t	devset;
1051a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1052a3114836SGerry Liu 	dr_handle_t	*shp = hp;
1053a3114836SGerry Liu 	static fn_t	f = "dr_pre_op";
1054a3114836SGerry Liu 
1055a3114836SGerry Liu 	cmd = hp->h_cmd;
1056a3114836SGerry Liu 	devset = shp->h_devset;
1057a3114836SGerry Liu 
1058a3114836SGerry Liu 	PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
1059a3114836SGerry Liu 
1060a3114836SGerry Liu 	devset = DEVSET_AND(devset, DR_DEVS_PRESENT(bp));
1061a3114836SGerry Liu 	hp->h_err = drmach_pre_op(cmd, bp->b_id, &hp->h_opts, &devset);
1062a3114836SGerry Liu 	if (hp->h_err != NULL) {
1063a3114836SGerry Liu 		PR_ALL("drmach_pre_op failed for cmd %s(%d)\n",
1064a3114836SGerry Liu 		    SBD_CMD_STR(cmd), cmd);
1065a3114836SGerry Liu 		return (-1);
1066a3114836SGerry Liu 	}
1067a3114836SGerry Liu 
1068a3114836SGerry Liu 	/*
1069a3114836SGerry Liu 	 * Check for valid state transitions.
1070a3114836SGerry Liu 	 */
1071a3114836SGerry Liu 	if ((t = CMD2INDEX(cmd)) != -1) {
1072a3114836SGerry Liu 		struct dr_state_trans	*transp;
1073a3114836SGerry Liu 		int			state_err;
1074a3114836SGerry Liu 
1075a3114836SGerry Liu 		transp = &dr_state_transition[t];
1076a3114836SGerry Liu 		ASSERT(transp->x_cmd == cmd);
1077a3114836SGerry Liu 
1078a3114836SGerry Liu 		state_err = dr_check_transition(bp, &devset, transp, cmd);
1079a3114836SGerry Liu 
1080a3114836SGerry Liu 		if (state_err < 0) {
1081a3114836SGerry Liu 			/*
1082a3114836SGerry Liu 			 * Invalidate device.
1083a3114836SGerry Liu 			 */
1084a3114836SGerry Liu 			dr_op_err(CE_IGNORE, hp, ESBD_INVAL, NULL);
1085a3114836SGerry Liu 			serr = -1;
1086a3114836SGerry Liu 			PR_ALL("%s: invalid devset (0x%x)\n",
1087a3114836SGerry Liu 			    f, (uint_t)devset);
1088a3114836SGerry Liu 		} else if (state_err != 0) {
1089a3114836SGerry Liu 			/*
1090a3114836SGerry Liu 			 * State transition is not a valid one.
1091a3114836SGerry Liu 			 */
1092a3114836SGerry Liu 			dr_op_err(CE_IGNORE, hp,
1093a3114836SGerry Liu 			    transp->x_op[state_err].x_err, NULL);
1094a3114836SGerry Liu 
1095a3114836SGerry Liu 			serr = transp->x_op[state_err].x_rv;
1096a3114836SGerry Liu 
1097a3114836SGerry Liu 			PR_ALL("%s: invalid state %s(%d) for cmd %s(%d)\n",
1098a3114836SGerry Liu 			    f, state_str[state_err], state_err,
1099a3114836SGerry Liu 			    SBD_CMD_STR(cmd), cmd);
1100a3114836SGerry Liu 		} else {
1101a3114836SGerry Liu 			shp->h_devset = devset;
1102a3114836SGerry Liu 		}
1103a3114836SGerry Liu 	}
1104a3114836SGerry Liu 
1105a3114836SGerry Liu 	if (serr) {
1106a3114836SGerry Liu 		rv = -1;
1107a3114836SGerry Liu 	}
1108a3114836SGerry Liu 
1109a3114836SGerry Liu 	return (rv);
1110a3114836SGerry Liu }
1111a3114836SGerry Liu 
1112a3114836SGerry Liu static int
dr_post_op(dr_handle_t * hp,int rv)1113a3114836SGerry Liu dr_post_op(dr_handle_t *hp, int rv)
1114a3114836SGerry Liu {
1115a3114836SGerry Liu 	int		cmd;
1116a3114836SGerry Liu 	sbd_error_t	*err;
1117a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1118a3114836SGerry Liu 	static fn_t	f = "dr_post_op";
1119a3114836SGerry Liu 
1120a3114836SGerry Liu 	cmd = hp->h_cmd;
1121a3114836SGerry Liu 
1122a3114836SGerry Liu 	PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
1123a3114836SGerry Liu 
1124a3114836SGerry Liu 	err = drmach_post_op(cmd, bp->b_id, &hp->h_opts, rv);
1125a3114836SGerry Liu 	if (err != NULL) {
1126a3114836SGerry Liu 		PR_ALL("drmach_post_op failed for cmd %s(%d)\n",
1127a3114836SGerry Liu 		    SBD_CMD_STR(cmd), cmd);
1128a3114836SGerry Liu 		if (rv == 0) {
1129a3114836SGerry Liu 			ASSERT(hp->h_err == NULL);
1130a3114836SGerry Liu 			hp->h_err = err;
1131a3114836SGerry Liu 			rv = -1;
1132a3114836SGerry Liu 		} else if (hp->h_err == NULL) {
1133a3114836SGerry Liu 			hp->h_err = err;
1134a3114836SGerry Liu 		} else {
1135a3114836SGerry Liu 			sbd_err_clear(&err);
1136a3114836SGerry Liu 		}
1137a3114836SGerry Liu 	}
1138a3114836SGerry Liu 
1139a3114836SGerry Liu 	return (rv);
1140a3114836SGerry Liu }
1141a3114836SGerry Liu 
1142a3114836SGerry Liu static int
dr_exec_op(dr_handle_t * hp)1143a3114836SGerry Liu dr_exec_op(dr_handle_t *hp)
1144a3114836SGerry Liu {
1145a3114836SGerry Liu 	int		rv = 0;
1146a3114836SGerry Liu 	static fn_t	f = "dr_exec_op";
1147a3114836SGerry Liu 
1148a3114836SGerry Liu 	/* errors should have been caught by now */
1149a3114836SGerry Liu 	ASSERT(hp->h_err == NULL);
1150a3114836SGerry Liu 
1151a3114836SGerry Liu 	switch (hp->h_cmd) {
1152a3114836SGerry Liu 	case SBD_CMD_ASSIGN:
1153a3114836SGerry Liu 		dr_assign_board(hp);
1154a3114836SGerry Liu 		break;
1155a3114836SGerry Liu 
1156a3114836SGerry Liu 	case SBD_CMD_UNASSIGN:
1157a3114836SGerry Liu 		dr_unassign_board(hp);
1158a3114836SGerry Liu 		break;
1159a3114836SGerry Liu 
1160a3114836SGerry Liu 	case SBD_CMD_POWEROFF:
1161a3114836SGerry Liu 		dr_poweroff_board(hp);
1162a3114836SGerry Liu 		break;
1163a3114836SGerry Liu 
1164a3114836SGerry Liu 	case SBD_CMD_POWERON:
1165a3114836SGerry Liu 		dr_poweron_board(hp);
1166a3114836SGerry Liu 		break;
1167a3114836SGerry Liu 
1168a3114836SGerry Liu 	case SBD_CMD_TEST:
1169a3114836SGerry Liu 		dr_test_board(hp);
1170a3114836SGerry Liu 		break;
1171a3114836SGerry Liu 
1172a3114836SGerry Liu 	case SBD_CMD_CONNECT:
1173a3114836SGerry Liu 		dr_connect(hp);
1174a3114836SGerry Liu 		break;
1175a3114836SGerry Liu 
1176a3114836SGerry Liu 	case SBD_CMD_CONFIGURE:
1177a3114836SGerry Liu 		dr_dev_configure(hp);
1178a3114836SGerry Liu 		break;
1179a3114836SGerry Liu 
1180a3114836SGerry Liu 	case SBD_CMD_UNCONFIGURE:
1181a3114836SGerry Liu 		dr_dev_release(hp);
1182a3114836SGerry Liu 		if (hp->h_err == NULL)
1183a3114836SGerry Liu 			rv = dr_dev_unconfigure(hp);
1184a3114836SGerry Liu 		else
1185a3114836SGerry Liu 			dr_dev_cancel(hp);
1186a3114836SGerry Liu 		break;
1187a3114836SGerry Liu 
1188a3114836SGerry Liu 	case SBD_CMD_DISCONNECT:
1189a3114836SGerry Liu 		rv = dr_disconnect(hp);
1190a3114836SGerry Liu 		break;
1191a3114836SGerry Liu 
1192a3114836SGerry Liu 	case SBD_CMD_STATUS:
1193a3114836SGerry Liu 		rv = dr_dev_status(hp);
1194a3114836SGerry Liu 		break;
1195a3114836SGerry Liu 
1196a3114836SGerry Liu 	case SBD_CMD_GETNCM:
1197a3114836SGerry Liu 		hp->h_sbdcmd.cmd_getncm.g_ncm = dr_get_ncm(hp);
1198a3114836SGerry Liu 		rv = dr_copyout_iocmd(hp);
1199a3114836SGerry Liu 		break;
1200a3114836SGerry Liu 
1201a3114836SGerry Liu 	case SBD_CMD_PASSTHRU:
1202a3114836SGerry Liu 		rv = dr_pt_ioctl(hp);
1203a3114836SGerry Liu 		break;
1204a3114836SGerry Liu 
1205a3114836SGerry Liu 	default:
1206a3114836SGerry Liu 		cmn_err(CE_WARN,
1207a3114836SGerry Liu 		    "%s: unknown command (%d)",
1208a3114836SGerry Liu 		    f, hp->h_cmd);
1209a3114836SGerry Liu 		break;
1210a3114836SGerry Liu 	}
1211a3114836SGerry Liu 
1212a3114836SGerry Liu 	if (hp->h_err != NULL) {
1213a3114836SGerry Liu 		rv = -1;
1214a3114836SGerry Liu 	}
1215a3114836SGerry Liu 
1216a3114836SGerry Liu 	return (rv);
1217a3114836SGerry Liu }
1218a3114836SGerry Liu 
1219a3114836SGerry Liu static void
dr_assign_board(dr_handle_t * hp)1220a3114836SGerry Liu dr_assign_board(dr_handle_t *hp)
1221a3114836SGerry Liu {
1222a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1223a3114836SGerry Liu 
1224a3114836SGerry Liu 	hp->h_err = drmach_board_assign(bp->b_num, &bp->b_id);
1225a3114836SGerry Liu 	if (hp->h_err == NULL) {
1226a3114836SGerry Liu 		bp->b_assigned = 1;
1227a3114836SGerry Liu 	}
1228a3114836SGerry Liu }
1229a3114836SGerry Liu 
1230a3114836SGerry Liu static void
dr_unassign_board(dr_handle_t * hp)1231a3114836SGerry Liu dr_unassign_board(dr_handle_t *hp)
1232a3114836SGerry Liu {
1233a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1234a3114836SGerry Liu 
1235a3114836SGerry Liu 	/*
1236a3114836SGerry Liu 	 * Block out status during unassign.
1237a3114836SGerry Liu 	 * Not doing cv_wait_sig here as starfire SSP software
1238a3114836SGerry Liu 	 * ignores unassign failure and removes board from
1239a3114836SGerry Liu 	 * domain mask causing system panic.
1240a3114836SGerry Liu 	 * TODO: Change cv_wait to cv_wait_sig when SSP software
1241a3114836SGerry Liu 	 * handles unassign failure.
1242a3114836SGerry Liu 	 */
1243a3114836SGerry Liu 	dr_lock_status(bp);
1244a3114836SGerry Liu 
1245a3114836SGerry Liu 	hp->h_err = drmach_board_unassign(bp->b_id);
1246a3114836SGerry Liu 	if (hp->h_err == NULL) {
1247a3114836SGerry Liu 		/*
1248a3114836SGerry Liu 		 * clear drmachid_t handle; not valid after board unassign
1249a3114836SGerry Liu 		 */
1250a3114836SGerry Liu 		bp->b_id = 0;
1251a3114836SGerry Liu 		bp->b_assigned = 0;
1252a3114836SGerry Liu 	}
1253a3114836SGerry Liu 
1254a3114836SGerry Liu 	dr_unlock_status(bp);
1255a3114836SGerry Liu }
1256a3114836SGerry Liu 
1257a3114836SGerry Liu static void
dr_poweron_board(dr_handle_t * hp)1258a3114836SGerry Liu dr_poweron_board(dr_handle_t *hp)
1259a3114836SGerry Liu {
1260a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1261a3114836SGerry Liu 
1262a3114836SGerry Liu 	hp->h_err = drmach_board_poweron(bp->b_id);
1263a3114836SGerry Liu }
1264a3114836SGerry Liu 
1265a3114836SGerry Liu static void
dr_poweroff_board(dr_handle_t * hp)1266a3114836SGerry Liu dr_poweroff_board(dr_handle_t *hp)
1267a3114836SGerry Liu {
1268a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1269a3114836SGerry Liu 
1270a3114836SGerry Liu 	hp->h_err = drmach_board_poweroff(bp->b_id);
1271a3114836SGerry Liu }
1272a3114836SGerry Liu 
1273a3114836SGerry Liu static void
dr_test_board(dr_handle_t * hp)1274a3114836SGerry Liu dr_test_board(dr_handle_t *hp)
1275a3114836SGerry Liu {
1276a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1277a3114836SGerry Liu 	hp->h_err = drmach_board_test(bp->b_id, &hp->h_opts,
1278a3114836SGerry Liu 	    dr_cmd_flags(hp) & SBD_FLAG_FORCE);
1279a3114836SGerry Liu }
1280a3114836SGerry Liu 
1281a3114836SGerry Liu /*
1282a3114836SGerry Liu  * Create and populate the component nodes for a board.  Assumes that the
1283a3114836SGerry Liu  * devlists for the board have been initialized.
1284a3114836SGerry Liu  */
1285a3114836SGerry Liu static void
dr_make_comp_nodes(dr_board_t * bp)1286a3114836SGerry Liu dr_make_comp_nodes(dr_board_t *bp)
1287a3114836SGerry Liu {
1288a3114836SGerry Liu 	int	i;
1289a3114836SGerry Liu 
1290a3114836SGerry Liu 	/*
1291a3114836SGerry Liu 	 * Make nodes for the individual components on the board.
1292a3114836SGerry Liu 	 * First we need to initialize memory unit data structures of board
1293a3114836SGerry Liu 	 * structure.
1294a3114836SGerry Liu 	 */
1295a3114836SGerry Liu 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
1296a3114836SGerry Liu 		dr_mem_unit_t *mp;
1297a3114836SGerry Liu 
1298a3114836SGerry Liu 		mp = dr_get_mem_unit(bp, i);
1299a3114836SGerry Liu 		dr_init_mem_unit(mp);
1300a3114836SGerry Liu 	}
1301a3114836SGerry Liu 
1302a3114836SGerry Liu 	/*
1303a3114836SGerry Liu 	 * Initialize cpu unit data structures.
1304a3114836SGerry Liu 	 */
1305a3114836SGerry Liu 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
1306a3114836SGerry Liu 		dr_cpu_unit_t *cp;
1307a3114836SGerry Liu 
1308a3114836SGerry Liu 		cp = dr_get_cpu_unit(bp, i);
1309a3114836SGerry Liu 		dr_init_cpu_unit(cp);
1310a3114836SGerry Liu 	}
1311a3114836SGerry Liu 
1312a3114836SGerry Liu 	/*
1313a3114836SGerry Liu 	 * Initialize io unit data structures.
1314a3114836SGerry Liu 	 */
1315a3114836SGerry Liu 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
1316a3114836SGerry Liu 		dr_io_unit_t *ip;
1317a3114836SGerry Liu 
1318a3114836SGerry Liu 		ip = dr_get_io_unit(bp, i);
1319a3114836SGerry Liu 		dr_init_io_unit(ip);
1320a3114836SGerry Liu 	}
1321a3114836SGerry Liu 
1322a3114836SGerry Liu 	dr_board_transition(bp, DR_STATE_CONNECTED);
1323a3114836SGerry Liu 
1324a3114836SGerry Liu 	bp->b_rstate = SBD_STAT_CONNECTED;
1325a3114836SGerry Liu 	bp->b_ostate = SBD_STAT_UNCONFIGURED;
1326a3114836SGerry Liu 	bp->b_cond = SBD_COND_OK;
1327a3114836SGerry Liu 	(void) drv_getparm(TIME, (void *)&bp->b_time);
1328a3114836SGerry Liu 
1329a3114836SGerry Liu }
1330a3114836SGerry Liu 
1331a3114836SGerry Liu /*
1332a3114836SGerry Liu  * Only do work if called to operate on an entire board
1333a3114836SGerry Liu  * which doesn't already have components present.
1334a3114836SGerry Liu  */
1335a3114836SGerry Liu static void
dr_connect(dr_handle_t * hp)1336a3114836SGerry Liu dr_connect(dr_handle_t *hp)
1337a3114836SGerry Liu {
1338a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1339a3114836SGerry Liu 	static fn_t	f = "dr_connect";
1340a3114836SGerry Liu 
1341a3114836SGerry Liu 	PR_ALL("%s...\n", f);
1342a3114836SGerry Liu 
1343a3114836SGerry Liu 	if (DR_DEVS_PRESENT(bp)) {
1344a3114836SGerry Liu 		/*
1345a3114836SGerry Liu 		 * Board already has devices present.
1346a3114836SGerry Liu 		 */
1347a3114836SGerry Liu 		PR_ALL("%s: devices already present (" DEVSET_FMT_STR ")\n",
1348a3114836SGerry Liu 		    f, DEVSET_FMT_ARG(DR_DEVS_PRESENT(bp)));
1349a3114836SGerry Liu 		return;
1350a3114836SGerry Liu 	}
1351a3114836SGerry Liu 
1352a3114836SGerry Liu 	hp->h_err = drmach_board_connect(bp->b_id, &hp->h_opts);
1353a3114836SGerry Liu 	if (hp->h_err)
1354a3114836SGerry Liu 		return;
1355a3114836SGerry Liu 
1356a3114836SGerry Liu 	hp->h_err = dr_init_devlists(bp);
1357a3114836SGerry Liu 	if (hp->h_err)
1358a3114836SGerry Liu 		return;
1359a3114836SGerry Liu 	else if (bp->b_ndev == 0) {
1360a3114836SGerry Liu 		dr_op_err(CE_WARN, hp, ESBD_EMPTY_BD, bp->b_path);
1361a3114836SGerry Liu 		return;
1362a3114836SGerry Liu 	} else {
1363a3114836SGerry Liu 		dr_make_comp_nodes(bp);
1364a3114836SGerry Liu 		return;
1365a3114836SGerry Liu 	}
1366a3114836SGerry Liu 	/*NOTREACHED*/
1367a3114836SGerry Liu }
1368a3114836SGerry Liu 
1369a3114836SGerry Liu static int
dr_disconnect(dr_handle_t * hp)1370a3114836SGerry Liu dr_disconnect(dr_handle_t *hp)
1371a3114836SGerry Liu {
1372a3114836SGerry Liu 	int		i;
1373a3114836SGerry Liu 	dr_devset_t	devset;
1374a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1375a3114836SGerry Liu 	static fn_t	f = "dr_disconnect";
1376a3114836SGerry Liu 
1377a3114836SGerry Liu 	PR_ALL("%s...\n", f);
1378a3114836SGerry Liu 
1379a3114836SGerry Liu 	/*
1380a3114836SGerry Liu 	 * Only devices which are present, but
1381a3114836SGerry Liu 	 * unattached can be disconnected.
1382a3114836SGerry Liu 	 */
1383a3114836SGerry Liu 	devset = hp->h_devset & DR_DEVS_PRESENT(bp) &
1384a3114836SGerry Liu 	    DR_DEVS_UNATTACHED(bp);
1385a3114836SGerry Liu 
1386a3114836SGerry Liu 	if ((devset == 0) && DR_DEVS_PRESENT(bp)) {
1387a3114836SGerry Liu 		dr_op_err(CE_IGNORE, hp, ESBD_EMPTY_BD, bp->b_path);
1388a3114836SGerry Liu 		return (0);
1389a3114836SGerry Liu 	}
1390a3114836SGerry Liu 
1391a3114836SGerry Liu 	/*
1392a3114836SGerry Liu 	 * Block out status during disconnect.
1393a3114836SGerry Liu 	 */
1394a3114836SGerry Liu 	mutex_enter(&bp->b_slock);
1395a3114836SGerry Liu 	while (bp->b_sflags & DR_BSLOCK) {
1396a3114836SGerry Liu 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
1397a3114836SGerry Liu 			mutex_exit(&bp->b_slock);
1398a3114836SGerry Liu 			return (EINTR);
1399a3114836SGerry Liu 		}
1400a3114836SGerry Liu 	}
1401a3114836SGerry Liu 	bp->b_sflags |= DR_BSLOCK;
1402a3114836SGerry Liu 	mutex_exit(&bp->b_slock);
1403a3114836SGerry Liu 
1404a3114836SGerry Liu 	hp->h_err = drmach_board_disconnect(bp->b_id, &hp->h_opts);
1405a3114836SGerry Liu 	if (hp->h_err && hp->h_err->e_code == EX86_WALK_DEPENDENCY) {
1406a3114836SGerry Liu 		/*
1407a3114836SGerry Liu 		 * Other boards have dependency on this board. No device nodes
1408a3114836SGerry Liu 		 * have been destroyed so keep current board status.
1409a3114836SGerry Liu 		 */
1410a3114836SGerry Liu 		goto disconnect_done;
1411a3114836SGerry Liu 	}
1412a3114836SGerry Liu 
1413a3114836SGerry Liu 	DR_DEVS_DISCONNECT(bp, devset);
1414a3114836SGerry Liu 
1415a3114836SGerry Liu 	ASSERT((DR_DEVS_ATTACHED(bp) & devset) == 0);
1416a3114836SGerry Liu 
1417a3114836SGerry Liu 	/*
1418a3114836SGerry Liu 	 * Update per-device state transitions.
1419a3114836SGerry Liu 	 */
1420a3114836SGerry Liu 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
1421a3114836SGerry Liu 		dr_cpu_unit_t *cp;
1422a3114836SGerry Liu 
1423a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
1424a3114836SGerry Liu 			continue;
1425a3114836SGerry Liu 
1426a3114836SGerry Liu 		cp = dr_get_cpu_unit(bp, i);
1427a3114836SGerry Liu 		if (dr_disconnect_cpu(cp) == 0)
1428a3114836SGerry Liu 			dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
1429a3114836SGerry Liu 		else if (cp->sbc_cm.sbdev_error != NULL)
1430a3114836SGerry Liu 			DRERR_SET_C(&hp->h_err, &cp->sbc_cm.sbdev_error);
1431a3114836SGerry Liu 
1432a3114836SGerry Liu 		ASSERT(cp->sbc_cm.sbdev_error == NULL);
1433a3114836SGerry Liu 	}
1434a3114836SGerry Liu 
1435a3114836SGerry Liu 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
1436a3114836SGerry Liu 		dr_mem_unit_t *mp;
1437a3114836SGerry Liu 
1438a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
1439a3114836SGerry Liu 			continue;
1440a3114836SGerry Liu 
1441a3114836SGerry Liu 		mp = dr_get_mem_unit(bp, i);
1442a3114836SGerry Liu 		if (dr_disconnect_mem(mp) == 0)
1443a3114836SGerry Liu 			dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
1444a3114836SGerry Liu 		else if (mp->sbm_cm.sbdev_error != NULL)
1445a3114836SGerry Liu 			DRERR_SET_C(&hp->h_err, &mp->sbm_cm.sbdev_error);
1446a3114836SGerry Liu 
1447a3114836SGerry Liu 		ASSERT(mp->sbm_cm.sbdev_error == NULL);
1448a3114836SGerry Liu 	}
1449a3114836SGerry Liu 
1450a3114836SGerry Liu 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
1451a3114836SGerry Liu 		dr_io_unit_t *ip;
1452a3114836SGerry Liu 
1453a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
1454a3114836SGerry Liu 			continue;
1455a3114836SGerry Liu 
1456a3114836SGerry Liu 		ip = dr_get_io_unit(bp, i);
1457a3114836SGerry Liu 		if (dr_disconnect_io(ip) == 0)
1458a3114836SGerry Liu 			dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
1459a3114836SGerry Liu 		else if (ip->sbi_cm.sbdev_error != NULL)
1460a3114836SGerry Liu 			DRERR_SET_C(&hp->h_err, &ip->sbi_cm.sbdev_error);
1461a3114836SGerry Liu 
1462a3114836SGerry Liu 		ASSERT(ip->sbi_cm.sbdev_error == NULL);
1463a3114836SGerry Liu 	}
1464a3114836SGerry Liu 
1465a3114836SGerry Liu 	if (hp->h_err) {
1466a3114836SGerry Liu 		/*
1467a3114836SGerry Liu 		 * For certain errors, drmach_board_disconnect will mark
1468a3114836SGerry Liu 		 * the board as unusable; in these cases the devtree must
1469a3114836SGerry Liu 		 * be purged so that status calls will succeed.
1470a3114836SGerry Liu 		 * XXX
1471a3114836SGerry Liu 		 * This implementation checks for discrete error codes -
1472a3114836SGerry Liu 		 * someday, the i/f to drmach_board_disconnect should be
1473a3114836SGerry Liu 		 * changed to avoid the e_code testing.
1474a3114836SGerry Liu 		 */
1475a3114836SGerry Liu 		if (hp->h_err->e_code == EX86_DEPROBE) {
1476a3114836SGerry Liu 			bp->b_ostate = SBD_STAT_UNCONFIGURED;
1477a3114836SGerry Liu 			bp->b_busy = 0;
1478a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&bp->b_time);
1479a3114836SGerry Liu 
1480a3114836SGerry Liu 			if (drmach_board_deprobe(bp->b_id))
1481a3114836SGerry Liu 				goto disconnect_done;
1482a3114836SGerry Liu 			else
1483a3114836SGerry Liu 				bp->b_ndev = 0;
1484a3114836SGerry Liu 		}
1485a3114836SGerry Liu 	}
1486a3114836SGerry Liu 
1487a3114836SGerry Liu 	/*
1488a3114836SGerry Liu 	 * Once all the components on a board have been disconnect
1489a3114836SGerry Liu 	 * the board's state can transition to disconnected and
1490a3114836SGerry Liu 	 * we can allow the deprobe to take place.
1491a3114836SGerry Liu 	 */
1492a3114836SGerry Liu 	if (hp->h_err == NULL && DR_DEVS_PRESENT(bp) == 0) {
1493a3114836SGerry Liu 		dr_board_transition(bp, DR_STATE_OCCUPIED);
1494a3114836SGerry Liu 		bp->b_rstate = SBD_STAT_DISCONNECTED;
1495a3114836SGerry Liu 		bp->b_ostate = SBD_STAT_UNCONFIGURED;
1496a3114836SGerry Liu 		bp->b_busy = 0;
1497a3114836SGerry Liu 		(void) drv_getparm(TIME, (void *)&bp->b_time);
1498a3114836SGerry Liu 
1499a3114836SGerry Liu 		hp->h_err = drmach_board_deprobe(bp->b_id);
1500a3114836SGerry Liu 
1501a3114836SGerry Liu 		if (hp->h_err == NULL) {
1502a3114836SGerry Liu 			bp->b_ndev = 0;
1503a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_EMPTY);
1504a3114836SGerry Liu 			bp->b_rstate = SBD_STAT_EMPTY;
1505a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&bp->b_time);
1506a3114836SGerry Liu 		}
1507a3114836SGerry Liu 	}
1508a3114836SGerry Liu 
1509a3114836SGerry Liu disconnect_done:
1510a3114836SGerry Liu 	dr_unlock_status(bp);
1511a3114836SGerry Liu 
1512a3114836SGerry Liu 	return (0);
1513a3114836SGerry Liu }
1514a3114836SGerry Liu 
1515a3114836SGerry Liu /*
1516a3114836SGerry Liu  * Check if a particular device is a valid target of the current
1517a3114836SGerry Liu  * operation. Return 1 if it is a valid target, and 0 otherwise.
1518a3114836SGerry Liu  */
1519a3114836SGerry Liu static int
dr_dev_is_target(dr_dev_unit_t * dp,int present_only,uint_t uset)1520a3114836SGerry Liu dr_dev_is_target(dr_dev_unit_t *dp, int present_only, uint_t uset)
1521a3114836SGerry Liu {
1522a3114836SGerry Liu 	dr_common_unit_t *cp;
1523a3114836SGerry Liu 	int		 is_present;
1524a3114836SGerry Liu 	int		 is_attached;
1525a3114836SGerry Liu 
1526a3114836SGerry Liu 	cp = &dp->du_common;
1527a3114836SGerry Liu 
1528a3114836SGerry Liu 	/* check if the user requested this device */
1529a3114836SGerry Liu 	if ((uset & (1 << cp->sbdev_unum)) == 0) {
1530a3114836SGerry Liu 		return (0);
1531a3114836SGerry Liu 	}
1532a3114836SGerry Liu 
1533a3114836SGerry Liu 	is_present = DR_DEV_IS_PRESENT(cp) ? 1 : 0;
1534a3114836SGerry Liu 	is_attached = DR_DEV_IS_ATTACHED(cp) ? 1 : 0;
1535a3114836SGerry Liu 
1536a3114836SGerry Liu 	/*
1537a3114836SGerry Liu 	 * If the present_only flag is set, a valid target
1538a3114836SGerry Liu 	 * must be present but not attached. Otherwise, it
1539a3114836SGerry Liu 	 * must be both present and attached.
1540a3114836SGerry Liu 	 */
1541a3114836SGerry Liu 	if (is_present && (present_only ^ is_attached)) {
1542a3114836SGerry Liu 		/* sanity check */
1543a3114836SGerry Liu 		ASSERT(cp->sbdev_id != (drmachid_t)0);
1544a3114836SGerry Liu 
1545a3114836SGerry Liu 		return (1);
1546a3114836SGerry Liu 	}
1547a3114836SGerry Liu 
1548a3114836SGerry Liu 	return (0);
1549a3114836SGerry Liu }
1550a3114836SGerry Liu 
1551a3114836SGerry Liu static void
dr_dev_make_list(dr_handle_t * hp,sbd_comp_type_t type,int present_only,dr_common_unit_t *** devlist,int * devnum)1552a3114836SGerry Liu dr_dev_make_list(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
15532002b186SToomas Soome     dr_common_unit_t ***devlist, int *devnum)
1554a3114836SGerry Liu {
1555a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1556a3114836SGerry Liu 	int		 unum;
1557a3114836SGerry Liu 	int		 nunits;
1558a3114836SGerry Liu 	uint_t		 uset;
1559a3114836SGerry Liu 	int		 len;
1560a3114836SGerry Liu 	dr_common_unit_t **list, **wp;
1561a3114836SGerry Liu 
1562a3114836SGerry Liu 	switch (type) {
1563a3114836SGerry Liu 	case SBD_COMP_CPU:
1564a3114836SGerry Liu 		nunits = MAX_CPU_UNITS_PER_BOARD;
1565a3114836SGerry Liu 		break;
1566a3114836SGerry Liu 	case SBD_COMP_MEM:
1567a3114836SGerry Liu 		nunits = MAX_MEM_UNITS_PER_BOARD;
1568a3114836SGerry Liu 		break;
1569a3114836SGerry Liu 	case SBD_COMP_IO:
1570a3114836SGerry Liu 		nunits = MAX_IO_UNITS_PER_BOARD;
1571a3114836SGerry Liu 		break;
1572a3114836SGerry Liu 	default:
1573*584b574aSToomas Soome 		nunits = 0;
1574a3114836SGerry Liu 		/* catch this in debug kernels */
1575a3114836SGerry Liu 		ASSERT(0);
1576a3114836SGerry Liu 		break;
1577a3114836SGerry Liu 	}
1578a3114836SGerry Liu 
1579a3114836SGerry Liu 	/* allocate list storage. */
1580a3114836SGerry Liu 	len = sizeof (dr_common_unit_t *) * (nunits + 1);
1581a3114836SGerry Liu 	list = kmem_zalloc(len, KM_SLEEP);
1582a3114836SGerry Liu 
1583a3114836SGerry Liu 	/* record length of storage in first element */
1584a3114836SGerry Liu 	*list++ = (dr_common_unit_t *)(uintptr_t)len;
1585a3114836SGerry Liu 
1586a3114836SGerry Liu 	/* get bit array signifying which units are to be involved */
1587a3114836SGerry Liu 	uset = DEVSET_GET_UNITSET(hp->h_devset, type);
1588a3114836SGerry Liu 
1589a3114836SGerry Liu 	/*
1590a3114836SGerry Liu 	 * Adjust the loop count for CPU devices since all cores
1591a3114836SGerry Liu 	 * in a CMP will be examined in a single iteration.
1592a3114836SGerry Liu 	 */
1593a3114836SGerry Liu 	if (type == SBD_COMP_CPU) {
1594a3114836SGerry Liu 		nunits = MAX_CMP_UNITS_PER_BOARD;
1595a3114836SGerry Liu 	}
1596a3114836SGerry Liu 
1597a3114836SGerry Liu 	/* populate list */
1598a3114836SGerry Liu 	for (wp = list, unum = 0; unum < nunits; unum++) {
1599a3114836SGerry Liu 		dr_dev_unit_t	*dp;
1600a3114836SGerry Liu 		int		core;
1601a3114836SGerry Liu 		int		cunum;
1602a3114836SGerry Liu 
1603a3114836SGerry Liu 		dp = DR_GET_BOARD_DEVUNIT(bp, type, unum);
1604a3114836SGerry Liu 		if (dr_dev_is_target(dp, present_only, uset)) {
1605a3114836SGerry Liu 			*wp++ = &dp->du_common;
1606a3114836SGerry Liu 		}
1607a3114836SGerry Liu 
1608a3114836SGerry Liu 		/* further processing is only required for CPUs */
1609a3114836SGerry Liu 		if (type != SBD_COMP_CPU) {
1610a3114836SGerry Liu 			continue;
1611a3114836SGerry Liu 		}
1612a3114836SGerry Liu 
1613a3114836SGerry Liu 		/*
1614a3114836SGerry Liu 		 * Add any additional cores from the current CPU
1615a3114836SGerry Liu 		 * device. This is to ensure that all the cores
1616a3114836SGerry Liu 		 * are grouped together in the device list, and
1617a3114836SGerry Liu 		 * consequently sequenced together during the actual
1618a3114836SGerry Liu 		 * operation.
1619a3114836SGerry Liu 		 */
1620a3114836SGerry Liu 		for (core = 1; core < MAX_CORES_PER_CMP; core++) {
1621a3114836SGerry Liu 			cunum = DR_CMP_CORE_UNUM(unum, core);
1622a3114836SGerry Liu 			dp = DR_GET_BOARD_DEVUNIT(bp, type, cunum);
1623a3114836SGerry Liu 
1624a3114836SGerry Liu 			if (dr_dev_is_target(dp, present_only, uset)) {
1625a3114836SGerry Liu 				*wp++ = &dp->du_common;
1626a3114836SGerry Liu 			}
1627a3114836SGerry Liu 		}
1628a3114836SGerry Liu 	}
1629a3114836SGerry Liu 
1630a3114836SGerry Liu 	/* calculate number of units in list, return result and list pointer */
1631a3114836SGerry Liu 	*devnum = wp - list;
1632a3114836SGerry Liu 	*devlist = list;
1633a3114836SGerry Liu }
1634a3114836SGerry Liu 
1635a3114836SGerry Liu static void
dr_dev_clean_up(dr_handle_t * hp,dr_common_unit_t ** list,int devnum)1636a3114836SGerry Liu dr_dev_clean_up(dr_handle_t *hp, dr_common_unit_t **list, int devnum)
1637a3114836SGerry Liu {
1638a3114836SGerry Liu 	int len;
1639a3114836SGerry Liu 	int n = 0;
1640a3114836SGerry Liu 	dr_common_unit_t *cp, **rp = list;
1641a3114836SGerry Liu 
1642a3114836SGerry Liu 	/*
1643a3114836SGerry Liu 	 * move first encountered unit error to handle if handle
1644a3114836SGerry Liu 	 * does not yet have a recorded error.
1645a3114836SGerry Liu 	 */
1646a3114836SGerry Liu 	if (hp->h_err == NULL) {
1647a3114836SGerry Liu 		while (n++ < devnum) {
1648a3114836SGerry Liu 			cp = *rp++;
1649a3114836SGerry Liu 			if (cp->sbdev_error != NULL) {
1650a3114836SGerry Liu 				hp->h_err = cp->sbdev_error;
1651a3114836SGerry Liu 				cp->sbdev_error = NULL;
1652a3114836SGerry Liu 				break;
1653a3114836SGerry Liu 			}
1654a3114836SGerry Liu 		}
1655a3114836SGerry Liu 	}
1656a3114836SGerry Liu 
1657a3114836SGerry Liu 	/* free remaining unit errors */
1658a3114836SGerry Liu 	while (n++ < devnum) {
1659a3114836SGerry Liu 		cp = *rp++;
1660a3114836SGerry Liu 		if (cp->sbdev_error != NULL) {
1661a3114836SGerry Liu 			sbd_err_clear(&cp->sbdev_error);
1662a3114836SGerry Liu 			cp->sbdev_error = NULL;
1663a3114836SGerry Liu 		}
1664a3114836SGerry Liu 	}
1665a3114836SGerry Liu 
1666a3114836SGerry Liu 	/* free list */
1667a3114836SGerry Liu 	list -= 1;
1668a3114836SGerry Liu 	len = (int)(uintptr_t)list[0];
1669a3114836SGerry Liu 	kmem_free(list, len);
1670a3114836SGerry Liu }
1671a3114836SGerry Liu 
1672a3114836SGerry Liu static int
dr_dev_walk(dr_handle_t * hp,sbd_comp_type_t type,int present_only,int (* pre_op)(dr_handle_t *,dr_common_unit_t **,int),void (* op)(dr_handle_t *,dr_common_unit_t *),int (* post_op)(dr_handle_t *,dr_common_unit_t **,int),void (* board_op)(dr_handle_t *,dr_common_unit_t **,int))1673a3114836SGerry Liu dr_dev_walk(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
16742002b186SToomas Soome     int (*pre_op)(dr_handle_t *, dr_common_unit_t **, int),
16752002b186SToomas Soome     void (*op)(dr_handle_t *, dr_common_unit_t *),
16762002b186SToomas Soome     int (*post_op)(dr_handle_t *, dr_common_unit_t **, int),
16772002b186SToomas Soome     void (*board_op)(dr_handle_t *, dr_common_unit_t **, int))
1678a3114836SGerry Liu {
1679a3114836SGerry Liu 	int			  devnum, rv;
1680a3114836SGerry Liu 	dr_common_unit_t	**devlist;
1681a3114836SGerry Liu 
1682a3114836SGerry Liu 	dr_dev_make_list(hp, type, present_only, &devlist, &devnum);
1683a3114836SGerry Liu 
1684a3114836SGerry Liu 	rv = 0;
1685a3114836SGerry Liu 	if (devnum > 0) {
1686a3114836SGerry Liu 		rv = (*pre_op)(hp, devlist, devnum);
1687a3114836SGerry Liu 		if (rv == 0) {
1688a3114836SGerry Liu 			int n;
1689a3114836SGerry Liu 
1690a3114836SGerry Liu 			for (n = 0; n < devnum; n++)
1691a3114836SGerry Liu 				(*op)(hp, devlist[n]);
1692a3114836SGerry Liu 
1693a3114836SGerry Liu 			rv = (*post_op)(hp, devlist, devnum);
1694a3114836SGerry Liu 
1695a3114836SGerry Liu 			(*board_op)(hp, devlist, devnum);
1696a3114836SGerry Liu 		}
1697a3114836SGerry Liu 	}
1698a3114836SGerry Liu 
1699a3114836SGerry Liu 	dr_dev_clean_up(hp, devlist, devnum);
1700a3114836SGerry Liu 	return (rv);
1701a3114836SGerry Liu }
1702a3114836SGerry Liu 
1703a3114836SGerry Liu /*ARGSUSED*/
1704a3114836SGerry Liu static int
dr_dev_noop(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1705a3114836SGerry Liu dr_dev_noop(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
1706a3114836SGerry Liu {
1707a3114836SGerry Liu 	return (0);
1708a3114836SGerry Liu }
1709a3114836SGerry Liu 
1710a3114836SGerry Liu static void
dr_attach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1711a3114836SGerry Liu dr_attach_update_state(dr_handle_t *hp,
17122002b186SToomas Soome     dr_common_unit_t **devlist, int devnum)
1713a3114836SGerry Liu {
1714a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1715a3114836SGerry Liu 	int		i;
1716a3114836SGerry Liu 	dr_devset_t	devs_unattached, devs_present;
1717a3114836SGerry Liu 	static fn_t	f = "dr_attach_update_state";
1718a3114836SGerry Liu 
1719a3114836SGerry Liu 	for (i = 0; i < devnum; i++) {
1720a3114836SGerry Liu 		dr_common_unit_t *cp = devlist[i];
1721a3114836SGerry Liu 
1722a3114836SGerry Liu 		if (dr_check_unit_attached(cp) == -1) {
1723a3114836SGerry Liu 			PR_ALL("%s: ERROR %s not attached\n",
1724a3114836SGerry Liu 			    f, cp->sbdev_path);
1725a3114836SGerry Liu 			continue;
1726a3114836SGerry Liu 		}
1727a3114836SGerry Liu 
1728a3114836SGerry Liu 		DR_DEV_SET_ATTACHED(cp);
1729a3114836SGerry Liu 
1730a3114836SGerry Liu 		dr_device_transition(cp, DR_STATE_CONFIGURED);
1731a3114836SGerry Liu 		cp->sbdev_cond = SBD_COND_OK;
1732a3114836SGerry Liu 	}
1733a3114836SGerry Liu 
1734a3114836SGerry Liu 	devs_present = DR_DEVS_PRESENT(bp);
1735a3114836SGerry Liu 	devs_unattached = DR_DEVS_UNATTACHED(bp);
1736a3114836SGerry Liu 
1737a3114836SGerry Liu 	switch (bp->b_state) {
1738a3114836SGerry Liu 	case DR_STATE_CONNECTED:
1739a3114836SGerry Liu 	case DR_STATE_UNCONFIGURED:
1740a3114836SGerry Liu 		ASSERT(devs_present);
1741a3114836SGerry Liu 
1742a3114836SGerry Liu 		if (devs_unattached == 0) {
1743a3114836SGerry Liu 			/*
1744a3114836SGerry Liu 			 * All devices finally attached.
1745a3114836SGerry Liu 			 */
1746a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_CONFIGURED);
1747a3114836SGerry Liu 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1748a3114836SGerry Liu 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1749a3114836SGerry Liu 			hp->h_bd->b_cond = SBD_COND_OK;
1750a3114836SGerry Liu 			hp->h_bd->b_busy = 0;
1751a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1752a3114836SGerry Liu 		} else if (devs_present != devs_unattached) {
1753a3114836SGerry Liu 			/*
1754a3114836SGerry Liu 			 * Only some devices are fully attached.
1755a3114836SGerry Liu 			 */
1756a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_PARTIAL);
1757a3114836SGerry Liu 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1758a3114836SGerry Liu 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1759a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1760a3114836SGerry Liu 		}
1761a3114836SGerry Liu 		break;
1762a3114836SGerry Liu 
1763a3114836SGerry Liu 	case DR_STATE_PARTIAL:
1764a3114836SGerry Liu 		ASSERT(devs_present);
1765a3114836SGerry Liu 		/*
1766a3114836SGerry Liu 		 * All devices finally attached.
1767a3114836SGerry Liu 		 */
1768a3114836SGerry Liu 		if (devs_unattached == 0) {
1769a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_CONFIGURED);
1770a3114836SGerry Liu 			hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1771a3114836SGerry Liu 			hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1772a3114836SGerry Liu 			hp->h_bd->b_cond = SBD_COND_OK;
1773a3114836SGerry Liu 			hp->h_bd->b_busy = 0;
1774a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1775a3114836SGerry Liu 		}
1776a3114836SGerry Liu 		break;
1777a3114836SGerry Liu 
1778a3114836SGerry Liu 	default:
1779a3114836SGerry Liu 		break;
1780a3114836SGerry Liu 	}
1781a3114836SGerry Liu }
1782a3114836SGerry Liu 
1783a3114836SGerry Liu static void
dr_dev_configure(dr_handle_t * hp)1784a3114836SGerry Liu dr_dev_configure(dr_handle_t *hp)
1785a3114836SGerry Liu {
1786a3114836SGerry Liu 	int rv;
1787a3114836SGerry Liu 
1788a3114836SGerry Liu 	rv = dr_dev_walk(hp, SBD_COMP_CPU, 1,
1789a3114836SGerry Liu 	    dr_pre_attach_cpu,
1790a3114836SGerry Liu 	    dr_attach_cpu,
1791a3114836SGerry Liu 	    dr_post_attach_cpu,
1792a3114836SGerry Liu 	    dr_attach_update_state);
1793a3114836SGerry Liu 
1794a3114836SGerry Liu 	if (rv >= 0) {
1795a3114836SGerry Liu 		rv = dr_dev_walk(hp, SBD_COMP_MEM, 1,
1796a3114836SGerry Liu 		    dr_pre_attach_mem,
1797a3114836SGerry Liu 		    dr_attach_mem,
1798a3114836SGerry Liu 		    dr_post_attach_mem,
1799a3114836SGerry Liu 		    dr_attach_update_state);
1800a3114836SGerry Liu 	}
1801a3114836SGerry Liu 
1802a3114836SGerry Liu 	if (rv >= 0) {
1803a3114836SGerry Liu 		(void) dr_dev_walk(hp, SBD_COMP_IO, 1,
1804a3114836SGerry Liu 		    dr_pre_attach_io,
1805a3114836SGerry Liu 		    dr_attach_io,
1806a3114836SGerry Liu 		    dr_post_attach_io,
1807a3114836SGerry Liu 		    dr_attach_update_state);
1808a3114836SGerry Liu 	}
1809a3114836SGerry Liu }
1810a3114836SGerry Liu 
1811a3114836SGerry Liu static void
dr_release_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1812a3114836SGerry Liu dr_release_update_state(dr_handle_t *hp,
18132002b186SToomas Soome     dr_common_unit_t **devlist, int devnum)
1814a3114836SGerry Liu {
1815a3114836SGerry Liu 	_NOTE(ARGUNUSED(devlist))
1816a3114836SGerry Liu 	_NOTE(ARGUNUSED(devnum))
1817a3114836SGerry Liu 
1818a3114836SGerry Liu 	dr_board_t *bp = hp->h_bd;
1819a3114836SGerry Liu 
1820a3114836SGerry Liu 	/*
1821a3114836SGerry Liu 	 * If the entire board was released and all components
1822a3114836SGerry Liu 	 * unreferenced then transfer it to the UNREFERENCED state.
1823a3114836SGerry Liu 	 */
1824a3114836SGerry Liu 	if ((bp->b_state != DR_STATE_RELEASE) &&
1825a3114836SGerry Liu 	    (DR_DEVS_RELEASED(bp) == DR_DEVS_ATTACHED(bp))) {
1826a3114836SGerry Liu 		dr_board_transition(bp, DR_STATE_RELEASE);
1827a3114836SGerry Liu 		hp->h_bd->b_busy = 1;
1828a3114836SGerry Liu 	}
1829a3114836SGerry Liu }
1830a3114836SGerry Liu 
1831a3114836SGerry Liu /* called by dr_release_done [below] and dr_release_mem_done [dr_mem.c] */
1832a3114836SGerry Liu int
dr_release_dev_done(dr_common_unit_t * cp)1833a3114836SGerry Liu dr_release_dev_done(dr_common_unit_t *cp)
1834a3114836SGerry Liu {
1835a3114836SGerry Liu 	if (cp->sbdev_state == DR_STATE_RELEASE) {
1836a3114836SGerry Liu 		ASSERT(DR_DEV_IS_RELEASED(cp));
1837a3114836SGerry Liu 
1838a3114836SGerry Liu 		DR_DEV_SET_UNREFERENCED(cp);
1839a3114836SGerry Liu 
1840a3114836SGerry Liu 		dr_device_transition(cp, DR_STATE_UNREFERENCED);
1841a3114836SGerry Liu 
1842a3114836SGerry Liu 		return (0);
1843a3114836SGerry Liu 	} else {
1844a3114836SGerry Liu 		return (-1);
1845a3114836SGerry Liu 	}
1846a3114836SGerry Liu }
1847a3114836SGerry Liu 
1848a3114836SGerry Liu static void
dr_release_done(dr_handle_t * hp,dr_common_unit_t * cp)1849a3114836SGerry Liu dr_release_done(dr_handle_t *hp, dr_common_unit_t *cp)
1850a3114836SGerry Liu {
1851a3114836SGerry Liu 	_NOTE(ARGUNUSED(hp))
1852a3114836SGerry Liu 
1853a3114836SGerry Liu 	dr_board_t		*bp;
1854a3114836SGerry Liu 	static fn_t		f = "dr_release_done";
1855a3114836SGerry Liu 
1856a3114836SGerry Liu 	PR_ALL("%s...\n", f);
1857a3114836SGerry Liu 
1858a3114836SGerry Liu 	/* get board pointer & sanity check */
1859a3114836SGerry Liu 	bp = cp->sbdev_bp;
1860a3114836SGerry Liu 	ASSERT(bp == hp->h_bd);
1861a3114836SGerry Liu 
1862a3114836SGerry Liu 	/*
1863a3114836SGerry Liu 	 * Transfer the device which just completed its release
1864a3114836SGerry Liu 	 * to the UNREFERENCED state.
1865a3114836SGerry Liu 	 */
1866a3114836SGerry Liu 	switch (cp->sbdev_type) {
1867a3114836SGerry Liu 	case SBD_COMP_MEM:
1868a3114836SGerry Liu 		dr_release_mem_done(cp);
1869a3114836SGerry Liu 		break;
1870a3114836SGerry Liu 
1871a3114836SGerry Liu 	default:
1872a3114836SGerry Liu 		DR_DEV_SET_RELEASED(cp);
1873a3114836SGerry Liu 
1874a3114836SGerry Liu 		dr_device_transition(cp, DR_STATE_RELEASE);
1875a3114836SGerry Liu 
1876a3114836SGerry Liu 		(void) dr_release_dev_done(cp);
1877a3114836SGerry Liu 		break;
1878a3114836SGerry Liu 	}
1879a3114836SGerry Liu 
1880a3114836SGerry Liu 	/*
1881a3114836SGerry Liu 	 * If we're not already in the RELEASE state for this
1882a3114836SGerry Liu 	 * board and we now have released all that were previously
1883a3114836SGerry Liu 	 * attached, then transfer the board to the RELEASE state.
1884a3114836SGerry Liu 	 */
1885a3114836SGerry Liu 	if ((bp->b_state == DR_STATE_RELEASE) &&
1886a3114836SGerry Liu 	    (DR_DEVS_RELEASED(bp) == DR_DEVS_UNREFERENCED(bp))) {
1887a3114836SGerry Liu 		dr_board_transition(bp, DR_STATE_UNREFERENCED);
1888a3114836SGerry Liu 		bp->b_busy = 1;
1889a3114836SGerry Liu 		(void) drv_getparm(TIME, (void *)&bp->b_time);
1890a3114836SGerry Liu 	}
1891a3114836SGerry Liu }
1892a3114836SGerry Liu 
1893a3114836SGerry Liu static void
dr_dev_release_mem(dr_handle_t * hp,dr_common_unit_t * dv)1894a3114836SGerry Liu dr_dev_release_mem(dr_handle_t *hp, dr_common_unit_t *dv)
1895a3114836SGerry Liu {
1896a3114836SGerry Liu 	dr_release_mem(dv);
1897a3114836SGerry Liu 	dr_release_done(hp, dv);
1898a3114836SGerry Liu }
1899a3114836SGerry Liu 
1900a3114836SGerry Liu static void
dr_dev_release(dr_handle_t * hp)1901a3114836SGerry Liu dr_dev_release(dr_handle_t *hp)
1902a3114836SGerry Liu {
1903a3114836SGerry Liu 	int rv;
1904a3114836SGerry Liu 
1905a3114836SGerry Liu 	hp->h_bd->b_busy = 1;
1906a3114836SGerry Liu 
1907a3114836SGerry Liu 	rv = dr_dev_walk(hp, SBD_COMP_CPU, 0,
1908a3114836SGerry Liu 	    dr_pre_release_cpu,
1909a3114836SGerry Liu 	    dr_release_done,
1910a3114836SGerry Liu 	    dr_dev_noop,
1911a3114836SGerry Liu 	    dr_release_update_state);
1912a3114836SGerry Liu 
1913a3114836SGerry Liu 	if (rv >= 0) {
1914a3114836SGerry Liu 		rv = dr_dev_walk(hp, SBD_COMP_MEM, 0,
1915a3114836SGerry Liu 		    dr_pre_release_mem,
1916a3114836SGerry Liu 		    dr_dev_release_mem,
1917a3114836SGerry Liu 		    dr_dev_noop,
1918a3114836SGerry Liu 		    dr_release_update_state);
1919a3114836SGerry Liu 	}
1920a3114836SGerry Liu 
1921a3114836SGerry Liu 	if (rv >= 0) {
1922a3114836SGerry Liu 		rv = dr_dev_walk(hp, SBD_COMP_IO, 0,
1923a3114836SGerry Liu 		    dr_pre_release_io,
1924a3114836SGerry Liu 		    dr_release_done,
1925a3114836SGerry Liu 		    dr_dev_noop,
1926a3114836SGerry Liu 		    dr_release_update_state);
1927a3114836SGerry Liu 
1928a3114836SGerry Liu 	}
1929a3114836SGerry Liu 
1930a3114836SGerry Liu 	if (rv < 0)
1931a3114836SGerry Liu 		hp->h_bd->b_busy = 0;
1932a3114836SGerry Liu 	/* else, b_busy will be cleared in dr_detach_update_state() */
1933a3114836SGerry Liu }
1934a3114836SGerry Liu 
1935a3114836SGerry Liu static void
dr_detach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1936a3114836SGerry Liu dr_detach_update_state(dr_handle_t *hp,
19372002b186SToomas Soome     dr_common_unit_t **devlist, int devnum)
1938a3114836SGerry Liu {
1939a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1940a3114836SGerry Liu 	int		i;
1941a3114836SGerry Liu 	dr_state_t	bstate;
1942a3114836SGerry Liu 	static fn_t	f = "dr_detach_update_state";
1943a3114836SGerry Liu 
1944a3114836SGerry Liu 	for (i = 0; i < devnum; i++) {
1945a3114836SGerry Liu 		dr_common_unit_t *cp = devlist[i];
1946a3114836SGerry Liu 
1947a3114836SGerry Liu 		if (dr_check_unit_attached(cp) >= 0) {
1948a3114836SGerry Liu 			/*
1949a3114836SGerry Liu 			 * Device is still attached probably due
1950a3114836SGerry Liu 			 * to an error.  Need to keep track of it.
1951a3114836SGerry Liu 			 */
1952a3114836SGerry Liu 			PR_ALL("%s: ERROR %s not detached\n",
1953a3114836SGerry Liu 			    f, cp->sbdev_path);
1954a3114836SGerry Liu 
1955a3114836SGerry Liu 			continue;
1956a3114836SGerry Liu 		}
1957a3114836SGerry Liu 
1958a3114836SGerry Liu 		DR_DEV_CLR_ATTACHED(cp);
1959a3114836SGerry Liu 		DR_DEV_CLR_RELEASED(cp);
1960a3114836SGerry Liu 		DR_DEV_CLR_UNREFERENCED(cp);
1961a3114836SGerry Liu 		dr_device_transition(cp, DR_STATE_UNCONFIGURED);
1962a3114836SGerry Liu 	}
1963a3114836SGerry Liu 
1964a3114836SGerry Liu 	bstate = bp->b_state;
1965a3114836SGerry Liu 	if (bstate != DR_STATE_UNCONFIGURED) {
1966a3114836SGerry Liu 		if (DR_DEVS_PRESENT(bp) == DR_DEVS_UNATTACHED(bp)) {
1967a3114836SGerry Liu 			/*
1968a3114836SGerry Liu 			 * All devices are finally detached.
1969a3114836SGerry Liu 			 */
1970a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_UNCONFIGURED);
1971a3114836SGerry Liu 			hp->h_bd->b_ostate = SBD_STAT_UNCONFIGURED;
1972a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1973a3114836SGerry Liu 		} else if ((bp->b_state != DR_STATE_PARTIAL) &&
1974a3114836SGerry Liu 		    (DR_DEVS_ATTACHED(bp) !=
1975a3114836SGerry Liu 		    DR_DEVS_PRESENT(bp))) {
1976a3114836SGerry Liu 			/*
1977a3114836SGerry Liu 			 * Some devices remain attached.
1978a3114836SGerry Liu 			 */
1979a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_PARTIAL);
1980a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1981a3114836SGerry Liu 		}
1982a3114836SGerry Liu 
1983a3114836SGerry Liu 		if ((hp->h_devset & DR_DEVS_UNATTACHED(bp)) == hp->h_devset)
1984a3114836SGerry Liu 			hp->h_bd->b_busy = 0;
1985a3114836SGerry Liu 	}
1986a3114836SGerry Liu }
1987a3114836SGerry Liu 
1988a3114836SGerry Liu static int
dr_dev_unconfigure(dr_handle_t * hp)1989a3114836SGerry Liu dr_dev_unconfigure(dr_handle_t *hp)
1990a3114836SGerry Liu {
1991a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
1992a3114836SGerry Liu 
1993a3114836SGerry Liu 	/*
1994a3114836SGerry Liu 	 * Block out status during IO unconfig.
1995a3114836SGerry Liu 	 */
1996a3114836SGerry Liu 	mutex_enter(&bp->b_slock);
1997a3114836SGerry Liu 	while (bp->b_sflags & DR_BSLOCK) {
1998a3114836SGerry Liu 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
1999a3114836SGerry Liu 			mutex_exit(&bp->b_slock);
2000a3114836SGerry Liu 			return (EINTR);
2001a3114836SGerry Liu 		}
2002a3114836SGerry Liu 	}
2003a3114836SGerry Liu 	bp->b_sflags |= DR_BSLOCK;
2004a3114836SGerry Liu 	mutex_exit(&bp->b_slock);
2005a3114836SGerry Liu 
2006a3114836SGerry Liu 	(void) dr_dev_walk(hp, SBD_COMP_IO, 0,
2007a3114836SGerry Liu 	    dr_pre_detach_io,
2008a3114836SGerry Liu 	    dr_detach_io,
2009a3114836SGerry Liu 	    dr_post_detach_io,
2010a3114836SGerry Liu 	    dr_detach_update_state);
2011a3114836SGerry Liu 
2012a3114836SGerry Liu 	dr_unlock_status(bp);
2013a3114836SGerry Liu 
2014a3114836SGerry Liu 	(void) dr_dev_walk(hp, SBD_COMP_CPU, 0,
2015a3114836SGerry Liu 	    dr_pre_detach_cpu,
2016a3114836SGerry Liu 	    dr_detach_cpu,
2017a3114836SGerry Liu 	    dr_post_detach_cpu,
2018a3114836SGerry Liu 	    dr_detach_update_state);
2019a3114836SGerry Liu 
2020a3114836SGerry Liu 	(void) dr_dev_walk(hp, SBD_COMP_MEM, 0,
2021a3114836SGerry Liu 	    dr_pre_detach_mem,
2022a3114836SGerry Liu 	    dr_detach_mem,
2023a3114836SGerry Liu 	    dr_post_detach_mem,
2024a3114836SGerry Liu 	    dr_detach_update_state);
2025a3114836SGerry Liu 
2026a3114836SGerry Liu 	return (0);
2027a3114836SGerry Liu }
2028a3114836SGerry Liu 
2029a3114836SGerry Liu static void
dr_dev_cancel(dr_handle_t * hp)2030a3114836SGerry Liu dr_dev_cancel(dr_handle_t *hp)
2031a3114836SGerry Liu {
2032a3114836SGerry Liu 	int		i;
2033a3114836SGerry Liu 	dr_devset_t	devset;
2034a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
2035a3114836SGerry Liu 	static fn_t	f = "dr_dev_cancel";
2036a3114836SGerry Liu 
2037a3114836SGerry Liu 	PR_ALL("%s...\n", f);
2038a3114836SGerry Liu 
2039a3114836SGerry Liu 	/*
2040a3114836SGerry Liu 	 * Only devices which have been "released" are
2041a3114836SGerry Liu 	 * subject to cancellation.
2042a3114836SGerry Liu 	 */
2043a3114836SGerry Liu 	devset = hp->h_devset & DR_DEVS_RELEASED(bp);
2044a3114836SGerry Liu 
2045a3114836SGerry Liu 	/*
2046a3114836SGerry Liu 	 * Nothing to do for CPUs or IO other than change back
2047a3114836SGerry Liu 	 * their state.
2048a3114836SGerry Liu 	 */
2049a3114836SGerry Liu 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
2050a3114836SGerry Liu 		dr_cpu_unit_t	*cp;
2051a3114836SGerry Liu 		dr_state_t	nstate;
2052a3114836SGerry Liu 
2053a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
2054a3114836SGerry Liu 			continue;
2055a3114836SGerry Liu 
2056a3114836SGerry Liu 		cp = dr_get_cpu_unit(bp, i);
2057a3114836SGerry Liu 		if (dr_cancel_cpu(cp) == 0)
2058a3114836SGerry Liu 			nstate = DR_STATE_CONFIGURED;
2059a3114836SGerry Liu 		else
2060a3114836SGerry Liu 			nstate = DR_STATE_FATAL;
2061a3114836SGerry Liu 
2062a3114836SGerry Liu 		dr_device_transition(&cp->sbc_cm, nstate);
2063a3114836SGerry Liu 	}
2064a3114836SGerry Liu 
2065a3114836SGerry Liu 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
2066a3114836SGerry Liu 		dr_io_unit_t *ip;
2067a3114836SGerry Liu 
2068a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
2069a3114836SGerry Liu 			continue;
2070a3114836SGerry Liu 		ip = dr_get_io_unit(bp, i);
2071a3114836SGerry Liu 		dr_device_transition(&ip->sbi_cm, DR_STATE_CONFIGURED);
2072a3114836SGerry Liu 	}
2073a3114836SGerry Liu 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
2074a3114836SGerry Liu 		dr_mem_unit_t	*mp;
2075a3114836SGerry Liu 		dr_state_t	nstate;
2076a3114836SGerry Liu 
2077a3114836SGerry Liu 		if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
2078a3114836SGerry Liu 			continue;
2079a3114836SGerry Liu 
2080a3114836SGerry Liu 		mp = dr_get_mem_unit(bp, i);
2081a3114836SGerry Liu 		if (dr_cancel_mem(mp) == 0)
2082a3114836SGerry Liu 			nstate = DR_STATE_CONFIGURED;
2083a3114836SGerry Liu 		else
2084a3114836SGerry Liu 			nstate = DR_STATE_FATAL;
2085a3114836SGerry Liu 
2086a3114836SGerry Liu 		dr_device_transition(&mp->sbm_cm, nstate);
2087a3114836SGerry Liu 	}
2088a3114836SGerry Liu 
2089a3114836SGerry Liu 	PR_ALL("%s: unreleasing devset (0x%x)\n", f, (uint_t)devset);
2090a3114836SGerry Liu 
2091a3114836SGerry Liu 	DR_DEVS_CANCEL(bp, devset);
2092a3114836SGerry Liu 
2093a3114836SGerry Liu 	if (DR_DEVS_RELEASED(bp) == 0) {
2094a3114836SGerry Liu 		dr_state_t	new_state;
2095a3114836SGerry Liu 		/*
2096a3114836SGerry Liu 		 * If the board no longer has any released devices
2097a3114836SGerry Liu 		 * than transfer it back to the CONFIG/PARTIAL state.
2098a3114836SGerry Liu 		 */
2099a3114836SGerry Liu 		if (DR_DEVS_ATTACHED(bp) == DR_DEVS_PRESENT(bp))
2100a3114836SGerry Liu 			new_state = DR_STATE_CONFIGURED;
2101a3114836SGerry Liu 		else
2102a3114836SGerry Liu 			new_state = DR_STATE_PARTIAL;
2103a3114836SGerry Liu 		if (bp->b_state != new_state) {
2104a3114836SGerry Liu 			dr_board_transition(bp, new_state);
2105a3114836SGerry Liu 		}
2106a3114836SGerry Liu 		hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
2107a3114836SGerry Liu 		hp->h_bd->b_busy = 0;
2108a3114836SGerry Liu 		(void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
2109a3114836SGerry Liu 	}
2110a3114836SGerry Liu }
2111a3114836SGerry Liu 
2112a3114836SGerry Liu static int
dr_dev_status(dr_handle_t * hp)2113a3114836SGerry Liu dr_dev_status(dr_handle_t *hp)
2114a3114836SGerry Liu {
2115a3114836SGerry Liu 	int		nstat, mode, ncm, sz, pbsz, pnstat;
2116a3114836SGerry Liu 	dr_handle_t	*shp;
2117a3114836SGerry Liu 	dr_devset_t	devset = 0;
2118a3114836SGerry Liu 	sbd_stat_t	*dstatp = NULL;
2119a3114836SGerry Liu 	sbd_dev_stat_t	*devstatp;
2120a3114836SGerry Liu 	dr_board_t	*bp;
2121a3114836SGerry Liu 	drmach_status_t	 pstat;
2122a3114836SGerry Liu 	int		rv = 0;
2123a3114836SGerry Liu 
2124a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2125a3114836SGerry Liu 	int sz32 = 0;
2126a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
2127a3114836SGerry Liu 
2128a3114836SGerry Liu 	static fn_t	f = "dr_dev_status";
2129a3114836SGerry Liu 
2130a3114836SGerry Liu 	PR_ALL("%s...\n", f);
2131a3114836SGerry Liu 
2132a3114836SGerry Liu 	mode = hp->h_mode;
2133a3114836SGerry Liu 	shp = hp;
2134a3114836SGerry Liu 	devset = shp->h_devset;
2135a3114836SGerry Liu 	bp = hp->h_bd;
2136a3114836SGerry Liu 
2137a3114836SGerry Liu 	/*
2138a3114836SGerry Liu 	 * Block out disconnect, unassign, IO unconfigure and
2139a3114836SGerry Liu 	 * devinfo branch creation during status.
2140a3114836SGerry Liu 	 */
2141a3114836SGerry Liu 	mutex_enter(&bp->b_slock);
2142a3114836SGerry Liu 	while (bp->b_sflags & DR_BSLOCK) {
2143a3114836SGerry Liu 		if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
2144a3114836SGerry Liu 			mutex_exit(&bp->b_slock);
2145a3114836SGerry Liu 			return (EINTR);
2146a3114836SGerry Liu 		}
2147a3114836SGerry Liu 	}
2148a3114836SGerry Liu 	bp->b_sflags |= DR_BSLOCK;
2149a3114836SGerry Liu 	mutex_exit(&bp->b_slock);
2150a3114836SGerry Liu 
2151a3114836SGerry Liu 	ncm = 1;
2152a3114836SGerry Liu 	if (hp->h_sbdcmd.cmd_cm.c_id.c_type == SBD_COMP_NONE) {
2153a3114836SGerry Liu 		if (dr_cmd_flags(hp) & SBD_FLAG_ALLCMP) {
2154a3114836SGerry Liu 		/*
2155a3114836SGerry Liu 		 * Calculate the maximum number of components possible
2156a3114836SGerry Liu 		 * for a board.  This number will be used to size the
2157a3114836SGerry Liu 		 * status scratch buffer used by board and component
2158a3114836SGerry Liu 		 * status functions.
2159a3114836SGerry Liu 		 * This buffer may differ in size from what is provided
2160a3114836SGerry Liu 		 * by the plugin, since the known component set on the
2161a3114836SGerry Liu 		 * board may change between the plugin's GETNCM call, and
2162a3114836SGerry Liu 		 * the status call.  Sizing will be adjusted to the plugin's
2163a3114836SGerry Liu 		 * receptacle buffer at copyout time.
2164a3114836SGerry Liu 		 */
2165a3114836SGerry Liu 			ncm = MAX_CPU_UNITS_PER_BOARD +
2166a3114836SGerry Liu 			    MAX_MEM_UNITS_PER_BOARD +
2167a3114836SGerry Liu 			    MAX_IO_UNITS_PER_BOARD;
2168a3114836SGerry Liu 
2169a3114836SGerry Liu 		} else {
2170a3114836SGerry Liu 			/*
2171a3114836SGerry Liu 			 * In the case of c_type == SBD_COMP_NONE, and
2172a3114836SGerry Liu 			 * SBD_FLAG_ALLCMP not specified, only the board
2173a3114836SGerry Liu 			 * info is to be returned, no components.
2174a3114836SGerry Liu 			 */
2175a3114836SGerry Liu 			ncm = 0;
2176a3114836SGerry Liu 			devset = 0;
2177a3114836SGerry Liu 		}
2178a3114836SGerry Liu 	}
2179a3114836SGerry Liu 
2180a3114836SGerry Liu 	sz = sizeof (sbd_stat_t);
2181a3114836SGerry Liu 	if (ncm > 1)
2182a3114836SGerry Liu 		sz += sizeof (sbd_dev_stat_t) * (ncm - 1);
2183a3114836SGerry Liu 
2184a3114836SGerry Liu 
2185a3114836SGerry Liu 	pbsz = (int)hp->h_sbdcmd.cmd_stat.s_nbytes;
2186a3114836SGerry Liu 	pnstat = (pbsz - sizeof (sbd_stat_t)) / sizeof (sbd_dev_stat_t);
2187a3114836SGerry Liu 
2188a3114836SGerry Liu 	/*
2189a3114836SGerry Liu 	 * s_nbytes describes the size of the preallocated user
2190a3114836SGerry Liu 	 * buffer into which the application is execting to
2191a3114836SGerry Liu 	 * receive the sbd_stat_t and sbd_dev_stat_t structures.
2192a3114836SGerry Liu 	 */
2193a3114836SGerry Liu 
2194a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2195a3114836SGerry Liu 
2196a3114836SGerry Liu 	/*
2197a3114836SGerry Liu 	 * More buffer space is required for the 64bit to 32bit
2198a3114836SGerry Liu 	 * conversion of data structures.
2199a3114836SGerry Liu 	 */
2200a3114836SGerry Liu 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
2201a3114836SGerry Liu 		sz32 = sizeof (sbd_stat32_t);
2202a3114836SGerry Liu 		if (ncm > 1)
2203a3114836SGerry Liu 			sz32  += sizeof (sbd_dev_stat32_t) * (ncm - 1);
2204a3114836SGerry Liu 		pnstat = (pbsz - sizeof (sbd_stat32_t))/
2205a3114836SGerry Liu 		    sizeof (sbd_dev_stat32_t);
2206a3114836SGerry Liu 	}
2207a3114836SGerry Liu 
2208a3114836SGerry Liu 	sz += sz32;
2209a3114836SGerry Liu #endif
2210a3114836SGerry Liu 	/*
2211a3114836SGerry Liu 	 * Since one sbd_dev_stat_t is included in the sbd_stat_t,
2212a3114836SGerry Liu 	 * increment the plugin's nstat count.
2213a3114836SGerry Liu 	 */
2214a3114836SGerry Liu 	++pnstat;
2215a3114836SGerry Liu 
2216a3114836SGerry Liu 	if (bp->b_id == 0) {
2217a3114836SGerry Liu 		bzero(&pstat, sizeof (pstat));
2218a3114836SGerry Liu 	} else {
2219a3114836SGerry Liu 		sbd_error_t *err;
2220a3114836SGerry Liu 
2221a3114836SGerry Liu 		err = drmach_status(bp->b_id, &pstat);
2222a3114836SGerry Liu 		if (err) {
2223a3114836SGerry Liu 			DRERR_SET_C(&hp->h_err, &err);
2224a3114836SGerry Liu 			rv = EIO;
2225a3114836SGerry Liu 			goto status_done;
2226a3114836SGerry Liu 		}
2227a3114836SGerry Liu 	}
2228a3114836SGerry Liu 
2229a3114836SGerry Liu 	dstatp = (sbd_stat_t *)(void *)GETSTRUCT(char, sz);
2230a3114836SGerry Liu 
2231a3114836SGerry Liu 	devstatp = &dstatp->s_stat[0];
2232a3114836SGerry Liu 
2233a3114836SGerry Liu 	dstatp->s_board = bp->b_num;
2234a3114836SGerry Liu 
2235a3114836SGerry Liu 	/*
2236a3114836SGerry Liu 	 * Detect transitions between empty and disconnected.
2237a3114836SGerry Liu 	 */
2238a3114836SGerry Liu 	if (!pstat.empty && (bp->b_rstate == SBD_STAT_EMPTY))
2239a3114836SGerry Liu 		bp->b_rstate = SBD_STAT_DISCONNECTED;
2240a3114836SGerry Liu 	else if (pstat.empty && (bp->b_rstate == SBD_STAT_DISCONNECTED))
2241a3114836SGerry Liu 		bp->b_rstate = SBD_STAT_EMPTY;
2242a3114836SGerry Liu 
2243a3114836SGerry Liu 	dstatp->s_rstate = bp->b_rstate;
2244a3114836SGerry Liu 	dstatp->s_ostate = bp->b_ostate;
2245a3114836SGerry Liu 	dstatp->s_cond = bp->b_cond = pstat.cond;
2246a3114836SGerry Liu 	dstatp->s_busy = bp->b_busy | pstat.busy;
2247a3114836SGerry Liu 	dstatp->s_time = bp->b_time;
2248a3114836SGerry Liu 	dstatp->s_power = pstat.powered;
2249a3114836SGerry Liu 	dstatp->s_assigned = bp->b_assigned = pstat.assigned;
2250a3114836SGerry Liu 	dstatp->s_nstat = nstat = 0;
2251a3114836SGerry Liu 	bcopy(&pstat.type[0], &dstatp->s_type[0], SBD_TYPE_LEN);
2252a3114836SGerry Liu 	bcopy(&pstat.info[0], &dstatp->s_info[0], SBD_MAX_INFO);
2253a3114836SGerry Liu 
2254a3114836SGerry Liu 	devset &= DR_DEVS_PRESENT(bp);
2255a3114836SGerry Liu 	if (devset == 0) {
2256a3114836SGerry Liu 		/*
2257a3114836SGerry Liu 		 * No device chosen.
2258a3114836SGerry Liu 		 */
2259a3114836SGerry Liu 		PR_ALL("%s: no device present\n", f);
2260a3114836SGerry Liu 	}
2261a3114836SGerry Liu 
2262a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT))
2263a3114836SGerry Liu 		if ((nstat = dr_cpu_status(hp, devset, devstatp)) > 0) {
2264a3114836SGerry Liu 			dstatp->s_nstat += nstat;
2265a3114836SGerry Liu 			devstatp += nstat;
2266a3114836SGerry Liu 		}
2267a3114836SGerry Liu 
2268a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT))
2269a3114836SGerry Liu 		if ((nstat = dr_mem_status(hp, devset, devstatp)) > 0) {
2270a3114836SGerry Liu 			dstatp->s_nstat += nstat;
2271a3114836SGerry Liu 			devstatp += nstat;
2272a3114836SGerry Liu 		}
2273a3114836SGerry Liu 
2274a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT))
2275a3114836SGerry Liu 		if ((nstat = dr_io_status(hp, devset, devstatp)) > 0) {
2276a3114836SGerry Liu 			dstatp->s_nstat += nstat;
2277a3114836SGerry Liu 			devstatp += nstat;
2278a3114836SGerry Liu 		}
2279a3114836SGerry Liu 
2280a3114836SGerry Liu 	/*
2281a3114836SGerry Liu 	 * Due to a possible change in number of components between
2282a3114836SGerry Liu 	 * the time of plugin's GETNCM call and now, there may be
2283a3114836SGerry Liu 	 * more or less components than the plugin's buffer can
2284a3114836SGerry Liu 	 * hold.  Adjust s_nstat accordingly.
2285a3114836SGerry Liu 	 */
2286a3114836SGerry Liu 
2287a3114836SGerry Liu 	dstatp->s_nstat = dstatp->s_nstat > pnstat ? pnstat : dstatp->s_nstat;
2288a3114836SGerry Liu 
2289a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2290a3114836SGerry Liu 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
2291a3114836SGerry Liu 		int		i, j;
2292a3114836SGerry Liu 		sbd_stat32_t	*dstat32p;
2293a3114836SGerry Liu 
2294a3114836SGerry Liu 		dstat32p = (sbd_stat32_t *)devstatp;
2295a3114836SGerry Liu 
2296a3114836SGerry Liu 		/* Alignment Paranoia */
2297a3114836SGerry Liu 		if ((ulong_t)dstat32p & 0x1) {
2298a3114836SGerry Liu 			PR_ALL("%s: alignment: sz=0x%lx dstat32p=0x%p\n",
2299a3114836SGerry Liu 			    f, sizeof (sbd_stat32_t), (void *)dstat32p);
2300a3114836SGerry Liu 			DR_OP_INTERNAL_ERROR(hp);
2301a3114836SGerry Liu 			rv = EINVAL;
2302a3114836SGerry Liu 			goto status_done;
2303a3114836SGerry Liu 		}
2304a3114836SGerry Liu 
2305a3114836SGerry Liu 		/* paranoia: detect buffer overrun */
2306a3114836SGerry Liu 		if ((caddr_t)&dstat32p->s_stat[dstatp->s_nstat] >
2307a3114836SGerry Liu 		    ((caddr_t)dstatp) + sz) {
2308a3114836SGerry Liu 			DR_OP_INTERNAL_ERROR(hp);
2309a3114836SGerry Liu 			rv = EINVAL;
2310a3114836SGerry Liu 			goto status_done;
2311a3114836SGerry Liu 		}
2312a3114836SGerry Liu 
2313a3114836SGerry Liu 		/* copy sbd_stat_t structure members */
2314a3114836SGerry Liu #define	_SBD_STAT(t, m) dstat32p->m = (t)dstatp->m
2315a3114836SGerry Liu 		_SBD_STAT(int32_t, s_board);
2316a3114836SGerry Liu 		_SBD_STAT(int32_t, s_rstate);
2317a3114836SGerry Liu 		_SBD_STAT(int32_t, s_ostate);
2318a3114836SGerry Liu 		_SBD_STAT(int32_t, s_cond);
2319a3114836SGerry Liu 		_SBD_STAT(int32_t, s_busy);
2320a3114836SGerry Liu 		_SBD_STAT(time32_t, s_time);
2321a3114836SGerry Liu 		_SBD_STAT(uint32_t, s_power);
2322a3114836SGerry Liu 		_SBD_STAT(uint32_t, s_assigned);
2323a3114836SGerry Liu 		_SBD_STAT(int32_t, s_nstat);
2324a3114836SGerry Liu 		bcopy(&dstatp->s_type[0], &dstat32p->s_type[0],
2325a3114836SGerry Liu 		    SBD_TYPE_LEN);
2326a3114836SGerry Liu 		bcopy(&dstatp->s_info[0], &dstat32p->s_info[0],
2327a3114836SGerry Liu 		    SBD_MAX_INFO);
2328a3114836SGerry Liu #undef _SBD_STAT
2329a3114836SGerry Liu 
2330a3114836SGerry Liu 		for (i = 0; i < dstatp->s_nstat; i++) {
2331a3114836SGerry Liu 			sbd_dev_stat_t		*dsp = &dstatp->s_stat[i];
2332a3114836SGerry Liu 			sbd_dev_stat32_t	*ds32p = &dstat32p->s_stat[i];
2333a3114836SGerry Liu #define	_SBD_DEV_STAT(t, m) ds32p->m = (t)dsp->m
2334a3114836SGerry Liu 
2335a3114836SGerry Liu 			/* copy sbd_cm_stat_t structure members */
2336a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_type);
2337a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_unit);
2338a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_ostate);
2339a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_cond);
2340a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_busy);
2341a3114836SGerry Liu 			_SBD_DEV_STAT(int32_t, ds_suspend);
2342a3114836SGerry Liu 			_SBD_DEV_STAT(time32_t, ds_time);
2343a3114836SGerry Liu 			bcopy(&dsp->ds_name[0], &ds32p->ds_name[0],
2344a3114836SGerry Liu 			    OBP_MAXPROPNAME);
2345a3114836SGerry Liu 
2346a3114836SGerry Liu 			switch (dsp->ds_type) {
2347a3114836SGerry Liu 			case SBD_COMP_CPU:
2348a3114836SGerry Liu 				/* copy sbd_cpu_stat_t structure members */
2349a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cpu.cs_isbootproc);
2350a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cpu.cs_cpuid);
2351a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cpu.cs_speed);
2352a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cpu.cs_ecache);
2353a3114836SGerry Liu 				break;
2354a3114836SGerry Liu 
2355a3114836SGerry Liu 			case SBD_COMP_MEM:
2356a3114836SGerry Liu 				/* copy sbd_mem_stat_t structure members */
2357a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_mem.ms_interleave);
2358a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_basepfn);
2359a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_totpages);
2360a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_detpages);
2361a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_mem.ms_pageslost);
2362a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_managed_pages);
2363a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_pages);
2364a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_first);
2365a3114836SGerry Liu 				_SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_last);
2366a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_mem.ms_cage_enabled);
2367a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_mem.ms_peer_is_target);
2368a3114836SGerry Liu 				bcopy(&dsp->d_mem.ms_peer_ap_id[0],
2369a3114836SGerry Liu 				    &ds32p->d_mem.ms_peer_ap_id[0],
2370a3114836SGerry Liu 				    sizeof (ds32p->d_mem.ms_peer_ap_id));
2371a3114836SGerry Liu 				break;
2372a3114836SGerry Liu 
2373a3114836SGerry Liu 			case SBD_COMP_IO:
2374a3114836SGerry Liu 				/* copy sbd_io_stat_t structure members */
2375a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_io.is_referenced);
2376a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_io.is_unsafe_count);
2377a3114836SGerry Liu 
2378a3114836SGerry Liu 				for (j = 0; j < SBD_MAX_UNSAFE; j++)
2379a3114836SGerry Liu 					_SBD_DEV_STAT(int32_t,
2380a3114836SGerry Liu 					    d_io.is_unsafe_list[j]);
2381a3114836SGerry Liu 
2382a3114836SGerry Liu 				bcopy(&dsp->d_io.is_pathname[0],
2383a3114836SGerry Liu 				    &ds32p->d_io.is_pathname[0], MAXPATHLEN);
2384a3114836SGerry Liu 				break;
2385a3114836SGerry Liu 
2386a3114836SGerry Liu 			case SBD_COMP_CMP:
2387a3114836SGerry Liu 				/* copy sbd_cmp_stat_t structure members */
2388a3114836SGerry Liu 				bcopy(&dsp->d_cmp.ps_cpuid[0],
2389a3114836SGerry Liu 				    &ds32p->d_cmp.ps_cpuid[0],
2390a3114836SGerry Liu 				    sizeof (ds32p->d_cmp.ps_cpuid));
2391a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cmp.ps_ncores);
2392a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cmp.ps_speed);
2393a3114836SGerry Liu 				_SBD_DEV_STAT(int32_t, d_cmp.ps_ecache);
2394a3114836SGerry Liu 				break;
2395a3114836SGerry Liu 
2396a3114836SGerry Liu 			default:
2397a3114836SGerry Liu 				cmn_err(CE_WARN, "%s: unknown dev type (%d)",
2398a3114836SGerry Liu 				    f, (int)dsp->ds_type);
2399a3114836SGerry Liu 				rv = EFAULT;
2400a3114836SGerry Liu 				goto status_done;
2401a3114836SGerry Liu 			}
2402a3114836SGerry Liu #undef _SBD_DEV_STAT
2403a3114836SGerry Liu 		}
2404a3114836SGerry Liu 
2405a3114836SGerry Liu 
2406a3114836SGerry Liu 		if (ddi_copyout((void *)dstat32p,
2407a3114836SGerry Liu 		    hp->h_sbdcmd.cmd_stat.s_statp, pbsz, mode) != 0) {
2408a3114836SGerry Liu 			cmn_err(CE_WARN,
2409a3114836SGerry Liu 			    "%s: failed to copyout status "
2410a3114836SGerry Liu 			    "for board %d", f, bp->b_num);
2411a3114836SGerry Liu 			rv = EFAULT;
2412a3114836SGerry Liu 			goto status_done;
2413a3114836SGerry Liu 		}
2414a3114836SGerry Liu 	} else
2415a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
2416a3114836SGerry Liu 
2417a3114836SGerry Liu 	if (ddi_copyout((void *)dstatp, hp->h_sbdcmd.cmd_stat.s_statp,
2418a3114836SGerry Liu 	    pbsz, mode) != 0) {
2419a3114836SGerry Liu 		cmn_err(CE_WARN,
2420a3114836SGerry Liu 		    "%s: failed to copyout status for board %d",
2421a3114836SGerry Liu 		    f, bp->b_num);
2422a3114836SGerry Liu 		rv = EFAULT;
2423a3114836SGerry Liu 		goto status_done;
2424a3114836SGerry Liu 	}
2425a3114836SGerry Liu 
2426a3114836SGerry Liu status_done:
2427a3114836SGerry Liu 	if (dstatp != NULL)
2428a3114836SGerry Liu 		FREESTRUCT(dstatp, char, sz);
2429a3114836SGerry Liu 
2430a3114836SGerry Liu 	dr_unlock_status(bp);
2431a3114836SGerry Liu 
2432a3114836SGerry Liu 	return (rv);
2433a3114836SGerry Liu }
2434a3114836SGerry Liu 
2435a3114836SGerry Liu static int
dr_get_ncm(dr_handle_t * hp)2436a3114836SGerry Liu dr_get_ncm(dr_handle_t *hp)
2437a3114836SGerry Liu {
2438a3114836SGerry Liu 	int		i;
2439a3114836SGerry Liu 	int		ncm = 0;
2440a3114836SGerry Liu 	dr_devset_t	devset;
2441a3114836SGerry Liu 
2442a3114836SGerry Liu 	devset = DR_DEVS_PRESENT(hp->h_bd);
2443a3114836SGerry Liu 	if (hp->h_sbdcmd.cmd_cm.c_id.c_type != SBD_COMP_NONE)
2444a3114836SGerry Liu 		devset &= DEVSET(hp->h_sbdcmd.cmd_cm.c_id.c_type,
2445a3114836SGerry Liu 		    DEVSET_ANYUNIT);
2446a3114836SGerry Liu 
2447a3114836SGerry Liu 	/*
2448a3114836SGerry Liu 	 * Handle CPUs first to deal with possible CMP
2449a3114836SGerry Liu 	 * devices. If the CPU is a CMP, we need to only
2450a3114836SGerry Liu 	 * increment ncm once even if there are multiple
2451a3114836SGerry Liu 	 * cores for that CMP present in the devset.
2452a3114836SGerry Liu 	 */
2453a3114836SGerry Liu 	for (i = 0; i < MAX_CMP_UNITS_PER_BOARD; i++) {
2454a3114836SGerry Liu 		if (devset & DEVSET(SBD_COMP_CMP, i)) {
2455a3114836SGerry Liu 			ncm++;
2456a3114836SGerry Liu 		}
2457a3114836SGerry Liu 	}
2458a3114836SGerry Liu 
2459a3114836SGerry Liu 	/* eliminate the CPU information from the devset */
2460a3114836SGerry Liu 	devset &= ~(DEVSET(SBD_COMP_CMP, DEVSET_ANYUNIT));
2461a3114836SGerry Liu 
2462a3114836SGerry Liu 	for (i = 0; i < (sizeof (dr_devset_t) * 8); i++) {
2463a3114836SGerry Liu 		ncm += devset & 0x1;
2464a3114836SGerry Liu 		devset >>= 1;
2465a3114836SGerry Liu 	}
2466a3114836SGerry Liu 
2467a3114836SGerry Liu 	return (ncm);
2468a3114836SGerry Liu }
2469a3114836SGerry Liu 
2470a3114836SGerry Liu /* used by dr_mem.c */
2471a3114836SGerry Liu /* TODO: eliminate dr_boardlist */
2472a3114836SGerry Liu dr_board_t *
dr_lookup_board(int board_num)2473a3114836SGerry Liu dr_lookup_board(int board_num)
2474a3114836SGerry Liu {
2475a3114836SGerry Liu 	dr_board_t *bp;
2476a3114836SGerry Liu 
2477a3114836SGerry Liu 	ASSERT(board_num >= 0 && board_num < MAX_BOARDS);
2478a3114836SGerry Liu 
2479a3114836SGerry Liu 	bp = &dr_boardlist[board_num];
2480a3114836SGerry Liu 	ASSERT(bp->b_num == board_num);
2481a3114836SGerry Liu 
2482a3114836SGerry Liu 	return (bp);
2483a3114836SGerry Liu }
2484a3114836SGerry Liu 
2485a3114836SGerry Liu static dr_dev_unit_t *
dr_get_dev_unit(dr_board_t * bp,sbd_comp_type_t nt,int unit_num)2486a3114836SGerry Liu dr_get_dev_unit(dr_board_t *bp, sbd_comp_type_t nt, int unit_num)
2487a3114836SGerry Liu {
2488a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2489a3114836SGerry Liu 
2490a3114836SGerry Liu 	dp = DR_GET_BOARD_DEVUNIT(bp, nt, unit_num);
2491a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_bp == bp);
2492a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_unum == unit_num);
2493a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_type == nt);
2494a3114836SGerry Liu 
2495a3114836SGerry Liu 	return (dp);
2496a3114836SGerry Liu }
2497a3114836SGerry Liu 
2498a3114836SGerry Liu dr_cpu_unit_t *
dr_get_cpu_unit(dr_board_t * bp,int unit_num)2499a3114836SGerry Liu dr_get_cpu_unit(dr_board_t *bp, int unit_num)
2500a3114836SGerry Liu {
2501a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2502a3114836SGerry Liu 
2503a3114836SGerry Liu 	ASSERT(unit_num >= 0 && unit_num < MAX_CPU_UNITS_PER_BOARD);
2504a3114836SGerry Liu 
2505a3114836SGerry Liu 	dp = dr_get_dev_unit(bp, SBD_COMP_CPU, unit_num);
2506a3114836SGerry Liu 	return (&dp->du_cpu);
2507a3114836SGerry Liu }
2508a3114836SGerry Liu 
2509a3114836SGerry Liu dr_mem_unit_t *
dr_get_mem_unit(dr_board_t * bp,int unit_num)2510a3114836SGerry Liu dr_get_mem_unit(dr_board_t *bp, int unit_num)
2511a3114836SGerry Liu {
2512a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2513a3114836SGerry Liu 
2514a3114836SGerry Liu 	ASSERT(unit_num >= 0 && unit_num < MAX_MEM_UNITS_PER_BOARD);
2515a3114836SGerry Liu 
2516a3114836SGerry Liu 	dp = dr_get_dev_unit(bp, SBD_COMP_MEM, unit_num);
2517a3114836SGerry Liu 	return (&dp->du_mem);
2518a3114836SGerry Liu }
2519a3114836SGerry Liu 
2520a3114836SGerry Liu dr_io_unit_t *
dr_get_io_unit(dr_board_t * bp,int unit_num)2521a3114836SGerry Liu dr_get_io_unit(dr_board_t *bp, int unit_num)
2522a3114836SGerry Liu {
2523a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2524a3114836SGerry Liu 
2525a3114836SGerry Liu 	ASSERT(unit_num >= 0 && unit_num < MAX_IO_UNITS_PER_BOARD);
2526a3114836SGerry Liu 
2527a3114836SGerry Liu 	dp = dr_get_dev_unit(bp, SBD_COMP_IO, unit_num);
2528a3114836SGerry Liu 	return (&dp->du_io);
2529a3114836SGerry Liu }
2530a3114836SGerry Liu 
2531a3114836SGerry Liu dr_common_unit_t *
dr_get_common_unit(dr_board_t * bp,sbd_comp_type_t nt,int unum)2532a3114836SGerry Liu dr_get_common_unit(dr_board_t *bp, sbd_comp_type_t nt, int unum)
2533a3114836SGerry Liu {
2534a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2535a3114836SGerry Liu 
2536a3114836SGerry Liu 	dp = dr_get_dev_unit(bp, nt, unum);
2537a3114836SGerry Liu 	return (&dp->du_common);
2538a3114836SGerry Liu }
2539a3114836SGerry Liu 
2540a3114836SGerry Liu static dr_devset_t
dr_dev2devset(sbd_comp_id_t * cid)2541a3114836SGerry Liu dr_dev2devset(sbd_comp_id_t *cid)
2542a3114836SGerry Liu {
2543a3114836SGerry Liu 	static fn_t	f = "dr_dev2devset";
2544a3114836SGerry Liu 
2545a3114836SGerry Liu 	dr_devset_t	devset;
2546a3114836SGerry Liu 	int		unit = cid->c_unit;
2547a3114836SGerry Liu 
2548a3114836SGerry Liu 	switch (cid->c_type) {
2549a3114836SGerry Liu 		case SBD_COMP_NONE:
2550a3114836SGerry Liu 			devset =  DEVSET(SBD_COMP_CPU, DEVSET_ANYUNIT);
2551a3114836SGerry Liu 			devset |= DEVSET(SBD_COMP_MEM, DEVSET_ANYUNIT);
2552a3114836SGerry Liu 			devset |= DEVSET(SBD_COMP_IO,  DEVSET_ANYUNIT);
2553a3114836SGerry Liu 			PR_ALL("%s: COMP_NONE devset = " DEVSET_FMT_STR "\n",
2554a3114836SGerry Liu 			    f, DEVSET_FMT_ARG(devset));
2555a3114836SGerry Liu 			break;
2556a3114836SGerry Liu 
2557a3114836SGerry Liu 		case SBD_COMP_CPU:
2558a3114836SGerry Liu 			if ((unit > MAX_CPU_UNITS_PER_BOARD) || (unit < 0)) {
2559a3114836SGerry Liu 				cmn_err(CE_WARN,
2560a3114836SGerry Liu 				    "%s: invalid cpu unit# = %d",
2561a3114836SGerry Liu 				    f, unit);
2562a3114836SGerry Liu 				devset = 0;
2563a3114836SGerry Liu 			} else {
2564a3114836SGerry Liu 				/*
2565a3114836SGerry Liu 				 * Generate a devset that includes all the
2566a3114836SGerry Liu 				 * cores of a CMP device. If this is not a
2567a3114836SGerry Liu 				 * CMP, the extra cores will be eliminated
2568a3114836SGerry Liu 				 * later since they are not present. This is
2569a3114836SGerry Liu 				 * also true for CMP devices that do not have
2570a3114836SGerry Liu 				 * all cores active.
2571a3114836SGerry Liu 				 */
2572a3114836SGerry Liu 				devset = DEVSET(SBD_COMP_CMP, unit);
2573a3114836SGerry Liu 			}
2574a3114836SGerry Liu 
2575a3114836SGerry Liu 			PR_ALL("%s: CPU devset = " DEVSET_FMT_STR "\n",
2576a3114836SGerry Liu 			    f, DEVSET_FMT_ARG(devset));
2577a3114836SGerry Liu 			break;
2578a3114836SGerry Liu 
2579a3114836SGerry Liu 		case SBD_COMP_MEM:
2580a3114836SGerry Liu 			if (unit == SBD_NULL_UNIT) {
2581a3114836SGerry Liu 				unit = 0;
2582a3114836SGerry Liu 				cid->c_unit = 0;
2583a3114836SGerry Liu 			}
2584a3114836SGerry Liu 
2585a3114836SGerry Liu 			if ((unit > MAX_MEM_UNITS_PER_BOARD) || (unit < 0)) {
2586a3114836SGerry Liu 				cmn_err(CE_WARN,
2587a3114836SGerry Liu 				    "%s: invalid mem unit# = %d",
2588a3114836SGerry Liu 				    f, unit);
2589a3114836SGerry Liu 				devset = 0;
2590a3114836SGerry Liu 			} else
2591a3114836SGerry Liu 				devset = DEVSET(cid->c_type, unit);
2592a3114836SGerry Liu 
2593a3114836SGerry Liu 			PR_ALL("%s: MEM devset = " DEVSET_FMT_STR "\n",
2594a3114836SGerry Liu 			    f, DEVSET_FMT_ARG(devset));
2595a3114836SGerry Liu 			break;
2596a3114836SGerry Liu 
2597a3114836SGerry Liu 		case SBD_COMP_IO:
2598a3114836SGerry Liu 			if ((unit > MAX_IO_UNITS_PER_BOARD) || (unit < 0)) {
2599a3114836SGerry Liu 				cmn_err(CE_WARN,
2600a3114836SGerry Liu 				    "%s: invalid io unit# = %d",
2601a3114836SGerry Liu 				    f, unit);
2602a3114836SGerry Liu 				devset = 0;
2603a3114836SGerry Liu 			} else
2604a3114836SGerry Liu 				devset = DEVSET(cid->c_type, unit);
2605a3114836SGerry Liu 
2606a3114836SGerry Liu 			PR_ALL("%s: IO devset = " DEVSET_FMT_STR "\n",
2607a3114836SGerry Liu 			    f, DEVSET_FMT_ARG(devset));
2608a3114836SGerry Liu 			break;
2609a3114836SGerry Liu 
2610a3114836SGerry Liu 		default:
2611a3114836SGerry Liu 		case SBD_COMP_UNKNOWN:
2612a3114836SGerry Liu 			devset = 0;
2613a3114836SGerry Liu 			break;
2614a3114836SGerry Liu 	}
2615a3114836SGerry Liu 
2616a3114836SGerry Liu 	return (devset);
2617a3114836SGerry Liu }
2618a3114836SGerry Liu 
2619a3114836SGerry Liu /*
2620a3114836SGerry Liu  * Converts a dynamic attachment point name to a SBD_COMP_* type.
2621a3114836SGerry Liu  * Returns SDB_COMP_UNKNOWN if name is not recognized.
2622a3114836SGerry Liu  */
2623a3114836SGerry Liu static int
dr_dev_type_to_nt(char * type)2624a3114836SGerry Liu dr_dev_type_to_nt(char *type)
2625a3114836SGerry Liu {
2626a3114836SGerry Liu 	int i;
2627a3114836SGerry Liu 
2628a3114836SGerry Liu 	for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
2629a3114836SGerry Liu 		if (strcmp(dr_devattr[i].s_devtype, type) == 0)
2630a3114836SGerry Liu 			break;
2631a3114836SGerry Liu 
2632a3114836SGerry Liu 	return (dr_devattr[i].s_nodetype);
2633a3114836SGerry Liu }
2634a3114836SGerry Liu 
2635a3114836SGerry Liu /*
2636a3114836SGerry Liu  * Converts a SBD_COMP_* type to a dynamic attachment point name.
2637a3114836SGerry Liu  * Return NULL if SBD_COMP_ type is not recognized.
2638a3114836SGerry Liu  */
2639a3114836SGerry Liu char *
dr_nt_to_dev_type(int nt)2640a3114836SGerry Liu dr_nt_to_dev_type(int nt)
2641a3114836SGerry Liu {
2642a3114836SGerry Liu 	int i;
2643a3114836SGerry Liu 
2644a3114836SGerry Liu 	for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
2645a3114836SGerry Liu 		if (dr_devattr[i].s_nodetype == nt)
2646a3114836SGerry Liu 			break;
2647a3114836SGerry Liu 
2648a3114836SGerry Liu 	return (dr_devattr[i].s_devtype);
2649a3114836SGerry Liu }
2650a3114836SGerry Liu 
2651a3114836SGerry Liu /*
2652a3114836SGerry Liu  * State transition policy is that if there is some component for which
2653a3114836SGerry Liu  * the state transition is valid, then let it through. The exception is
2654a3114836SGerry Liu  * SBD_CMD_DISCONNECT. On disconnect, the state transition must be valid
2655a3114836SGerry Liu  * for ALL components.
2656a3114836SGerry Liu  * Returns the state that is in error, if any.
2657a3114836SGerry Liu  */
2658a3114836SGerry Liu static int
dr_check_transition(dr_board_t * bp,dr_devset_t * devsetp,struct dr_state_trans * transp,int cmd)2659a3114836SGerry Liu dr_check_transition(dr_board_t *bp, dr_devset_t *devsetp,
26602002b186SToomas Soome     struct dr_state_trans *transp, int cmd)
2661a3114836SGerry Liu {
2662a3114836SGerry Liu 	int			s, ut;
2663a3114836SGerry Liu 	int			state_err = 0;
2664a3114836SGerry Liu 	dr_devset_t		devset;
2665a3114836SGerry Liu 	dr_common_unit_t	*cp;
2666a3114836SGerry Liu 	static fn_t		f = "dr_check_transition";
2667a3114836SGerry Liu 
2668a3114836SGerry Liu 	devset = *devsetp;
2669a3114836SGerry Liu 
2670a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
2671a3114836SGerry Liu 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
2672a3114836SGerry Liu 			if (DEVSET_IN_SET(devset, SBD_COMP_CPU, ut) == 0)
2673a3114836SGerry Liu 				continue;
2674a3114836SGerry Liu 
2675a3114836SGerry Liu 			cp = dr_get_common_unit(bp, SBD_COMP_CPU, ut);
2676a3114836SGerry Liu 			s = (int)cp->sbdev_state;
2677a3114836SGerry Liu 			if (!DR_DEV_IS_PRESENT(cp)) {
2678a3114836SGerry Liu 				DEVSET_DEL(devset, SBD_COMP_CPU, ut);
2679a3114836SGerry Liu 			} else {
2680a3114836SGerry Liu 				if (transp->x_op[s].x_rv) {
2681a3114836SGerry Liu 					if (!state_err)
2682a3114836SGerry Liu 						state_err = s;
2683a3114836SGerry Liu 					DEVSET_DEL(devset, SBD_COMP_CPU, ut);
2684a3114836SGerry Liu 				}
2685a3114836SGerry Liu 			}
2686a3114836SGerry Liu 		}
2687a3114836SGerry Liu 	}
2688a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
2689a3114836SGerry Liu 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
2690a3114836SGerry Liu 			if (DEVSET_IN_SET(devset, SBD_COMP_MEM, ut) == 0)
2691a3114836SGerry Liu 				continue;
2692a3114836SGerry Liu 
2693a3114836SGerry Liu 			cp = dr_get_common_unit(bp, SBD_COMP_MEM, ut);
2694a3114836SGerry Liu 			s = (int)cp->sbdev_state;
2695a3114836SGerry Liu 			if (!DR_DEV_IS_PRESENT(cp)) {
2696a3114836SGerry Liu 				DEVSET_DEL(devset, SBD_COMP_MEM, ut);
2697a3114836SGerry Liu 			} else {
2698a3114836SGerry Liu 				if (transp->x_op[s].x_rv) {
2699a3114836SGerry Liu 					if (!state_err)
2700a3114836SGerry Liu 						state_err = s;
2701a3114836SGerry Liu 					DEVSET_DEL(devset, SBD_COMP_MEM, ut);
2702a3114836SGerry Liu 				}
2703a3114836SGerry Liu 			}
2704a3114836SGerry Liu 		}
2705a3114836SGerry Liu 	}
2706a3114836SGerry Liu 	if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
2707a3114836SGerry Liu 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
2708a3114836SGerry Liu 			if (DEVSET_IN_SET(devset, SBD_COMP_IO, ut) == 0)
2709a3114836SGerry Liu 				continue;
2710a3114836SGerry Liu 
2711a3114836SGerry Liu 			cp = dr_get_common_unit(bp, SBD_COMP_IO, ut);
2712a3114836SGerry Liu 			s = (int)cp->sbdev_state;
2713a3114836SGerry Liu 			if (!DR_DEV_IS_PRESENT(cp)) {
2714a3114836SGerry Liu 				DEVSET_DEL(devset, SBD_COMP_IO, ut);
2715a3114836SGerry Liu 			} else {
2716a3114836SGerry Liu 				if (transp->x_op[s].x_rv) {
2717a3114836SGerry Liu 					if (!state_err)
2718a3114836SGerry Liu 						state_err = s;
2719a3114836SGerry Liu 					DEVSET_DEL(devset, SBD_COMP_IO, ut);
2720a3114836SGerry Liu 				}
2721a3114836SGerry Liu 			}
2722a3114836SGerry Liu 		}
2723a3114836SGerry Liu 	}
2724a3114836SGerry Liu 
2725a3114836SGerry Liu 	PR_ALL("%s: requested devset = 0x%x, final devset = 0x%x\n",
2726a3114836SGerry Liu 	    f, (uint_t)*devsetp, (uint_t)devset);
2727a3114836SGerry Liu 
2728a3114836SGerry Liu 	*devsetp = devset;
2729a3114836SGerry Liu 	/*
2730a3114836SGerry Liu 	 * If there are some remaining components for which
2731a3114836SGerry Liu 	 * this state transition is valid, then allow them
2732a3114836SGerry Liu 	 * through, otherwise if none are left then return
2733a3114836SGerry Liu 	 * the state error. The exception is SBD_CMD_DISCONNECT.
2734a3114836SGerry Liu 	 * On disconnect, the state transition must be valid for ALL
2735a3114836SGerry Liu 	 * components.
2736a3114836SGerry Liu 	 */
2737a3114836SGerry Liu 	if (cmd == SBD_CMD_DISCONNECT)
2738a3114836SGerry Liu 		return (state_err);
2739a3114836SGerry Liu 	return (devset ? 0 : state_err);
2740a3114836SGerry Liu }
2741a3114836SGerry Liu 
2742a3114836SGerry Liu void
dr_device_transition(dr_common_unit_t * cp,dr_state_t st)2743a3114836SGerry Liu dr_device_transition(dr_common_unit_t *cp, dr_state_t st)
2744a3114836SGerry Liu {
2745a3114836SGerry Liu 	PR_STATE("%s STATE %s(%d) -> %s(%d)\n",
2746a3114836SGerry Liu 	    cp->sbdev_path,
2747a3114836SGerry Liu 	    state_str[cp->sbdev_state], cp->sbdev_state,
2748a3114836SGerry Liu 	    state_str[st], st);
2749a3114836SGerry Liu 
2750a3114836SGerry Liu 	cp->sbdev_state = st;
2751a3114836SGerry Liu 	if (st == DR_STATE_CONFIGURED) {
2752a3114836SGerry Liu 		cp->sbdev_ostate = SBD_STAT_CONFIGURED;
2753a3114836SGerry Liu 		if (cp->sbdev_bp->b_ostate != SBD_STAT_CONFIGURED) {
2754a3114836SGerry Liu 			cp->sbdev_bp->b_ostate = SBD_STAT_CONFIGURED;
2755a3114836SGerry Liu 			(void) drv_getparm(TIME,
2756a3114836SGerry Liu 			    (void *) &cp->sbdev_bp->b_time);
2757a3114836SGerry Liu 		}
2758a3114836SGerry Liu 	} else
2759a3114836SGerry Liu 		cp->sbdev_ostate = SBD_STAT_UNCONFIGURED;
2760a3114836SGerry Liu 
2761a3114836SGerry Liu 	(void) drv_getparm(TIME, (void *) &cp->sbdev_time);
2762a3114836SGerry Liu }
2763a3114836SGerry Liu 
2764a3114836SGerry Liu static void
dr_board_transition(dr_board_t * bp,dr_state_t st)2765a3114836SGerry Liu dr_board_transition(dr_board_t *bp, dr_state_t st)
2766a3114836SGerry Liu {
2767a3114836SGerry Liu 	PR_STATE("BOARD %d STATE: %s(%d) -> %s(%d)\n",
2768a3114836SGerry Liu 	    bp->b_num,
2769a3114836SGerry Liu 	    state_str[bp->b_state], bp->b_state,
2770a3114836SGerry Liu 	    state_str[st], st);
2771a3114836SGerry Liu 
2772a3114836SGerry Liu 	bp->b_state = st;
2773a3114836SGerry Liu }
2774a3114836SGerry Liu 
2775a3114836SGerry Liu void
dr_op_err(int ce,dr_handle_t * hp,int code,char * fmt,...)2776a3114836SGerry Liu dr_op_err(int ce, dr_handle_t *hp, int code, char *fmt, ...)
2777a3114836SGerry Liu {
2778a3114836SGerry Liu 	sbd_error_t	*err;
2779a3114836SGerry Liu 	va_list		args;
2780a3114836SGerry Liu 
2781a3114836SGerry Liu 	va_start(args, fmt);
2782a3114836SGerry Liu 	err = drerr_new_v(code, fmt, args);
2783a3114836SGerry Liu 	va_end(args);
2784a3114836SGerry Liu 
2785a3114836SGerry Liu 	if (ce != CE_IGNORE)
2786a3114836SGerry Liu 		sbd_err_log(err, ce);
2787a3114836SGerry Liu 
2788a3114836SGerry Liu 	DRERR_SET_C(&hp->h_err, &err);
2789a3114836SGerry Liu }
2790a3114836SGerry Liu 
2791a3114836SGerry Liu void
dr_dev_err(int ce,dr_common_unit_t * cp,int code)2792a3114836SGerry Liu dr_dev_err(int ce, dr_common_unit_t *cp, int code)
2793a3114836SGerry Liu {
2794a3114836SGerry Liu 	sbd_error_t	*err;
2795a3114836SGerry Liu 
2796a3114836SGerry Liu 	err = drerr_new(0, code, cp->sbdev_path, NULL);
2797a3114836SGerry Liu 
2798a3114836SGerry Liu 	if (ce != CE_IGNORE)
2799a3114836SGerry Liu 		sbd_err_log(err, ce);
2800a3114836SGerry Liu 
2801a3114836SGerry Liu 	DRERR_SET_C(&cp->sbdev_error, &err);
2802a3114836SGerry Liu }
2803a3114836SGerry Liu 
2804a3114836SGerry Liu /*
2805a3114836SGerry Liu  * A callback routine.  Called from the drmach layer as a result of
2806a3114836SGerry Liu  * call to drmach_board_find_devices from dr_init_devlists.
2807a3114836SGerry Liu  */
2808a3114836SGerry Liu static sbd_error_t *
dr_dev_found(void * data,const char * name,int unum,drmachid_t id)2809a3114836SGerry Liu dr_dev_found(void *data, const char *name, int unum, drmachid_t id)
2810a3114836SGerry Liu {
2811a3114836SGerry Liu 	dr_board_t	*bp = data;
2812a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2813a3114836SGerry Liu 	int		 nt;
2814a3114836SGerry Liu 	static fn_t	f = "dr_dev_found";
2815a3114836SGerry Liu 
2816a3114836SGerry Liu 	PR_ALL("%s (board = %d, name = %s, unum = %d, id = %p)...\n",
2817a3114836SGerry Liu 	    f, bp->b_num, name, unum, id);
2818a3114836SGerry Liu 
2819a3114836SGerry Liu 	nt = dr_dev_type_to_nt((char *)name);
2820a3114836SGerry Liu 	if (nt == SBD_COMP_UNKNOWN) {
2821a3114836SGerry Liu 		/*
2822a3114836SGerry Liu 		 * this should not happen.  When it does, it indicates
2823a3114836SGerry Liu 		 * a missmatch in devices supported by the drmach layer
2824a3114836SGerry Liu 		 * vs devices supported by this layer.
2825a3114836SGerry Liu 		 */
2826a3114836SGerry Liu 		return (DR_INTERNAL_ERROR());
2827a3114836SGerry Liu 	}
2828a3114836SGerry Liu 
2829a3114836SGerry Liu 	dp = DR_GET_BOARD_DEVUNIT(bp, nt, unum);
2830a3114836SGerry Liu 
2831a3114836SGerry Liu 	/* sanity check */
2832a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_bp == bp);
2833a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_unum == unum);
2834a3114836SGerry Liu 	ASSERT(dp->du_common.sbdev_type == nt);
2835a3114836SGerry Liu 
2836a3114836SGerry Liu 	/* render dynamic attachment point path of this unit */
2837a3114836SGerry Liu 	(void) snprintf(dp->du_common.sbdev_path,
2838a3114836SGerry Liu 	    sizeof (dp->du_common.sbdev_path), "%s::%s%d",
2839a3114836SGerry Liu 	    bp->b_path, name, DR_UNUM2SBD_UNUM(unum, nt));
2840a3114836SGerry Liu 
2841a3114836SGerry Liu 	dp->du_common.sbdev_id = id;
2842a3114836SGerry Liu 	DR_DEV_SET_PRESENT(&dp->du_common);
2843a3114836SGerry Liu 
2844a3114836SGerry Liu 	bp->b_ndev++;
2845a3114836SGerry Liu 
2846a3114836SGerry Liu 	return (NULL);
2847a3114836SGerry Liu }
2848a3114836SGerry Liu 
2849a3114836SGerry Liu static sbd_error_t *
dr_init_devlists(dr_board_t * bp)2850a3114836SGerry Liu dr_init_devlists(dr_board_t *bp)
2851a3114836SGerry Liu {
2852a3114836SGerry Liu 	int		i;
2853a3114836SGerry Liu 	sbd_error_t	*err;
2854a3114836SGerry Liu 	dr_dev_unit_t	*dp;
2855a3114836SGerry Liu 	static fn_t	f = "dr_init_devlists";
2856a3114836SGerry Liu 
2857a3114836SGerry Liu 	PR_ALL("%s (%s)...\n", f, bp->b_path);
2858a3114836SGerry Liu 
2859a3114836SGerry Liu 	/* sanity check */
2860a3114836SGerry Liu 	ASSERT(bp->b_ndev == 0);
2861a3114836SGerry Liu 
2862a3114836SGerry Liu 	DR_DEVS_DISCONNECT(bp, (uint_t)-1);
2863a3114836SGerry Liu 
2864a3114836SGerry Liu 	/*
2865a3114836SGerry Liu 	 * This routine builds the board's devlist and initializes
2866a3114836SGerry Liu 	 * the common portion of the unit data structures.
2867a3114836SGerry Liu 	 * Note: because the common portion is considered
2868a3114836SGerry Liu 	 * uninitialized, the dr_get_*_unit() routines can not
2869a3114836SGerry Liu 	 * be used.
2870a3114836SGerry Liu 	 */
2871a3114836SGerry Liu 
2872a3114836SGerry Liu 	/*
2873a3114836SGerry Liu 	 * Clear out old entries, if any.
2874a3114836SGerry Liu 	 */
2875a3114836SGerry Liu 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
2876a3114836SGerry Liu 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_CPU, i);
2877a3114836SGerry Liu 
2878a3114836SGerry Liu 		bzero(dp, sizeof (*dp));
2879a3114836SGerry Liu 		dp->du_common.sbdev_bp = bp;
2880a3114836SGerry Liu 		dp->du_common.sbdev_unum = i;
2881a3114836SGerry Liu 		dp->du_common.sbdev_type = SBD_COMP_CPU;
2882a3114836SGerry Liu 	}
2883a3114836SGerry Liu 
2884a3114836SGerry Liu 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
2885a3114836SGerry Liu 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_MEM, i);
2886a3114836SGerry Liu 
2887a3114836SGerry Liu 		bzero(dp, sizeof (*dp));
2888a3114836SGerry Liu 		dp->du_common.sbdev_bp = bp;
2889a3114836SGerry Liu 		dp->du_common.sbdev_unum = i;
2890a3114836SGerry Liu 		dp->du_common.sbdev_type = SBD_COMP_MEM;
2891a3114836SGerry Liu 	}
2892a3114836SGerry Liu 
2893a3114836SGerry Liu 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
2894a3114836SGerry Liu 		dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_IO, i);
2895a3114836SGerry Liu 
2896a3114836SGerry Liu 		bzero(dp, sizeof (*dp));
2897a3114836SGerry Liu 		dp->du_common.sbdev_bp = bp;
2898a3114836SGerry Liu 		dp->du_common.sbdev_unum = i;
2899a3114836SGerry Liu 		dp->du_common.sbdev_type = SBD_COMP_IO;
2900a3114836SGerry Liu 	}
2901a3114836SGerry Liu 
2902a3114836SGerry Liu 	err = NULL;
2903a3114836SGerry Liu 	if (bp->b_id) {
2904a3114836SGerry Liu 		/* find devices on this board */
2905a3114836SGerry Liu 		err = drmach_board_find_devices(
2906a3114836SGerry Liu 		    bp->b_id, bp, dr_dev_found);
2907a3114836SGerry Liu 	}
2908a3114836SGerry Liu 
2909a3114836SGerry Liu 	return (err);
2910a3114836SGerry Liu }
2911a3114836SGerry Liu 
2912a3114836SGerry Liu /*
2913a3114836SGerry Liu  * Return the unit number of the respective drmachid if
2914a3114836SGerry Liu  * it's found to be attached.
2915a3114836SGerry Liu  */
2916a3114836SGerry Liu static int
dr_check_unit_attached(dr_common_unit_t * cp)2917a3114836SGerry Liu dr_check_unit_attached(dr_common_unit_t *cp)
2918a3114836SGerry Liu {
2919a3114836SGerry Liu 	int		rv = 0;
2920a3114836SGerry Liu 	processorid_t	cpuid;
2921a3114836SGerry Liu 	uint64_t	basepa, endpa;
2922a3114836SGerry Liu 	struct memlist	*ml;
2923a3114836SGerry Liu 	extern struct memlist	*phys_install;
2924a3114836SGerry Liu 	sbd_error_t	*err;
2925a3114836SGerry Liu 	int		yes;
2926a3114836SGerry Liu 	static fn_t	f = "dr_check_unit_attached";
2927a3114836SGerry Liu 
2928a3114836SGerry Liu 	switch (cp->sbdev_type) {
2929a3114836SGerry Liu 	case SBD_COMP_CPU:
2930a3114836SGerry Liu 		err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
2931a3114836SGerry Liu 		if (err) {
2932a3114836SGerry Liu 			DRERR_SET_C(&cp->sbdev_error, &err);
2933a3114836SGerry Liu 			rv = -1;
2934a3114836SGerry Liu 			break;
2935a3114836SGerry Liu 		}
2936a3114836SGerry Liu 		mutex_enter(&cpu_lock);
2937a3114836SGerry Liu 		if (cpu_get(cpuid) == NULL)
2938a3114836SGerry Liu 			rv = -1;
2939a3114836SGerry Liu 		mutex_exit(&cpu_lock);
2940a3114836SGerry Liu 		break;
2941a3114836SGerry Liu 
2942a3114836SGerry Liu 	case SBD_COMP_MEM:
2943a3114836SGerry Liu 		err = drmach_mem_get_slice_info(cp->sbdev_id,
2944a3114836SGerry Liu 		    &basepa, &endpa, NULL);
2945a3114836SGerry Liu 		if (err) {
2946a3114836SGerry Liu 			DRERR_SET_C(&cp->sbdev_error, &err);
2947a3114836SGerry Liu 			rv = -1;
2948a3114836SGerry Liu 			break;
2949a3114836SGerry Liu 		}
2950a3114836SGerry Liu 
2951a3114836SGerry Liu 		/*
2952a3114836SGerry Liu 		 * Check if base address is in phys_install.
2953a3114836SGerry Liu 		 */
2954a3114836SGerry Liu 		memlist_read_lock();
2955a3114836SGerry Liu 		for (ml = phys_install; ml; ml = ml->ml_next)
2956a3114836SGerry Liu 			if ((endpa <= ml->ml_address) ||
2957a3114836SGerry Liu 			    (basepa >= (ml->ml_address + ml->ml_size)))
2958a3114836SGerry Liu 				continue;
2959a3114836SGerry Liu 			else
2960a3114836SGerry Liu 				break;
2961a3114836SGerry Liu 		memlist_read_unlock();
2962a3114836SGerry Liu 		if (ml == NULL)
2963a3114836SGerry Liu 			rv = -1;
2964a3114836SGerry Liu 		break;
2965a3114836SGerry Liu 
2966a3114836SGerry Liu 	case SBD_COMP_IO:
2967a3114836SGerry Liu 		err = drmach_io_is_attached(cp->sbdev_id, &yes);
2968a3114836SGerry Liu 		if (err) {
2969a3114836SGerry Liu 			DRERR_SET_C(&cp->sbdev_error, &err);
2970a3114836SGerry Liu 			rv = -1;
2971a3114836SGerry Liu 			break;
2972a3114836SGerry Liu 		} else if (!yes)
2973a3114836SGerry Liu 			rv = -1;
2974a3114836SGerry Liu 		break;
2975a3114836SGerry Liu 
2976a3114836SGerry Liu 	default:
2977a3114836SGerry Liu 		PR_ALL("%s: unexpected nodetype(%d) for id 0x%p\n",
2978a3114836SGerry Liu 		    f, cp->sbdev_type, cp->sbdev_id);
2979a3114836SGerry Liu 		rv = -1;
2980a3114836SGerry Liu 		break;
2981a3114836SGerry Liu 	}
2982a3114836SGerry Liu 
2983a3114836SGerry Liu 	return (rv);
2984a3114836SGerry Liu }
2985a3114836SGerry Liu 
2986a3114836SGerry Liu /*
2987a3114836SGerry Liu  * See if drmach recognizes the passthru command.  DRMACH expects the
2988a3114836SGerry Liu  * id to identify the thing to which the command is being applied.  Using
2989a3114836SGerry Liu  * nonsense SBD terms, that information has been perversely encoded in the
2990a3114836SGerry Liu  * c_id member of the sbd_cmd_t structure.  This logic reads those tea
2991a3114836SGerry Liu  * leaves, finds the associated drmach id, then calls drmach to process
2992a3114836SGerry Liu  * the passthru command.
2993a3114836SGerry Liu  */
2994a3114836SGerry Liu static int
dr_pt_try_drmach(dr_handle_t * hp)2995a3114836SGerry Liu dr_pt_try_drmach(dr_handle_t *hp)
2996a3114836SGerry Liu {
2997a3114836SGerry Liu 	dr_board_t	*bp = hp->h_bd;
2998a3114836SGerry Liu 	sbd_comp_id_t	*comp_id = &hp->h_sbdcmd.cmd_cm.c_id;
2999a3114836SGerry Liu 	drmachid_t	 id;
3000a3114836SGerry Liu 
3001a3114836SGerry Liu 	if (comp_id->c_type == SBD_COMP_NONE) {
3002a3114836SGerry Liu 		id = bp->b_id;
3003a3114836SGerry Liu 	} else {
3004a3114836SGerry Liu 		sbd_comp_type_t	 nt;
3005a3114836SGerry Liu 
3006a3114836SGerry Liu 		nt = dr_dev_type_to_nt(comp_id->c_name);
3007a3114836SGerry Liu 		if (nt == SBD_COMP_UNKNOWN) {
3008a3114836SGerry Liu 			dr_op_err(CE_IGNORE, hp, ESBD_INVAL, comp_id->c_name);
3009a3114836SGerry Liu 			id = 0;
3010a3114836SGerry Liu 		} else {
3011a3114836SGerry Liu 			/* pt command applied to dynamic attachment point */
3012a3114836SGerry Liu 			dr_common_unit_t *cp;
3013a3114836SGerry Liu 			cp = dr_get_common_unit(bp, nt, comp_id->c_unit);
3014a3114836SGerry Liu 			id = cp->sbdev_id;
3015a3114836SGerry Liu 		}
3016a3114836SGerry Liu 	}
3017a3114836SGerry Liu 
3018a3114836SGerry Liu 	if (hp->h_err == NULL)
3019a3114836SGerry Liu 		hp->h_err = drmach_passthru(id, &hp->h_opts);
3020a3114836SGerry Liu 
3021a3114836SGerry Liu 	return (hp->h_err == NULL ? 0 : -1);
3022a3114836SGerry Liu }
3023a3114836SGerry Liu 
3024a3114836SGerry Liu static int
dr_pt_ioctl(dr_handle_t * hp)3025a3114836SGerry Liu dr_pt_ioctl(dr_handle_t *hp)
3026a3114836SGerry Liu {
3027a3114836SGerry Liu 	int		cmd, rv, len;
3028a3114836SGerry Liu 	int32_t		sz;
3029a3114836SGerry Liu 	int		found;
3030a3114836SGerry Liu 	char		*copts;
3031a3114836SGerry Liu 	static fn_t	f = "dr_pt_ioctl";
3032a3114836SGerry Liu 
3033a3114836SGerry Liu 	PR_ALL("%s...\n", f);
3034a3114836SGerry Liu 
3035a3114836SGerry Liu 	sz = hp->h_opts.size;
3036a3114836SGerry Liu 	copts = hp->h_opts.copts;
3037a3114836SGerry Liu 
3038a3114836SGerry Liu 	if (sz == 0 || copts == (char *)NULL) {
3039a3114836SGerry Liu 		cmn_err(CE_WARN, "%s: invalid passthru args", f);
3040a3114836SGerry Liu 		return (EINVAL);
3041a3114836SGerry Liu 	}
3042a3114836SGerry Liu 
3043a3114836SGerry Liu 	found = 0;
3044a3114836SGerry Liu 	for (cmd = 0; cmd < (sizeof (pt_arr) / sizeof (pt_arr[0])); cmd++) {
3045a3114836SGerry Liu 		len = strlen(pt_arr[cmd].pt_name);
3046a3114836SGerry Liu 		found = (strncmp(pt_arr[cmd].pt_name, copts, len) == 0);
3047a3114836SGerry Liu 		if (found)
3048a3114836SGerry Liu 			break;
3049a3114836SGerry Liu 	}
3050a3114836SGerry Liu 
3051a3114836SGerry Liu 	if (found)
3052a3114836SGerry Liu 		rv = (*pt_arr[cmd].pt_func)(hp);
3053a3114836SGerry Liu 	else
3054a3114836SGerry Liu 		rv = dr_pt_try_drmach(hp);
3055a3114836SGerry Liu 
3056a3114836SGerry Liu 	return (rv);
3057a3114836SGerry Liu }
3058a3114836SGerry Liu 
3059a3114836SGerry Liu /*
3060a3114836SGerry Liu  * Called at driver load time to determine the state and condition
3061a3114836SGerry Liu  * of an existing board in the system.
3062a3114836SGerry Liu  */
3063a3114836SGerry Liu static void
dr_board_discovery(dr_board_t * bp)3064a3114836SGerry Liu dr_board_discovery(dr_board_t *bp)
3065a3114836SGerry Liu {
3066a3114836SGerry Liu 	int			i;
3067a3114836SGerry Liu 	dr_devset_t		devs_lost, devs_attached = 0;
3068a3114836SGerry Liu 	dr_cpu_unit_t		*cp;
3069a3114836SGerry Liu 	dr_mem_unit_t		*mp;
3070a3114836SGerry Liu 	dr_io_unit_t		*ip;
3071a3114836SGerry Liu 	static fn_t		f = "dr_board_discovery";
3072a3114836SGerry Liu 
3073a3114836SGerry Liu 	if (DR_DEVS_PRESENT(bp) == 0) {
3074a3114836SGerry Liu 		PR_ALL("%s: board %d has no devices present\n",
3075a3114836SGerry Liu 		    f, bp->b_num);
3076a3114836SGerry Liu 		return;
3077a3114836SGerry Liu 	}
3078a3114836SGerry Liu 
3079a3114836SGerry Liu 	/*
3080a3114836SGerry Liu 	 * Check for existence of cpus.
3081a3114836SGerry Liu 	 */
3082a3114836SGerry Liu 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
3083a3114836SGerry Liu 		cp = dr_get_cpu_unit(bp, i);
3084a3114836SGerry Liu 
3085a3114836SGerry Liu 		if (!DR_DEV_IS_PRESENT(&cp->sbc_cm))
3086a3114836SGerry Liu 			continue;
3087a3114836SGerry Liu 
3088a3114836SGerry Liu 		if (dr_check_unit_attached(&cp->sbc_cm) >= 0) {
3089a3114836SGerry Liu 			DR_DEV_SET_ATTACHED(&cp->sbc_cm);
3090a3114836SGerry Liu 			DEVSET_ADD(devs_attached, SBD_COMP_CPU, i);
3091a3114836SGerry Liu 			PR_ALL("%s: board %d, cpu-unit %d - attached\n",
3092a3114836SGerry Liu 			    f, bp->b_num, i);
3093a3114836SGerry Liu 		}
3094a3114836SGerry Liu 		dr_init_cpu_unit(cp);
3095a3114836SGerry Liu 	}
3096a3114836SGerry Liu 
3097a3114836SGerry Liu 	/*
3098a3114836SGerry Liu 	 * Check for existence of memory.
3099a3114836SGerry Liu 	 */
3100a3114836SGerry Liu 	for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
3101a3114836SGerry Liu 		mp = dr_get_mem_unit(bp, i);
3102a3114836SGerry Liu 
3103a3114836SGerry Liu 		if (!DR_DEV_IS_PRESENT(&mp->sbm_cm))
3104a3114836SGerry Liu 			continue;
3105a3114836SGerry Liu 
3106a3114836SGerry Liu 		if (dr_check_unit_attached(&mp->sbm_cm) >= 0) {
3107a3114836SGerry Liu 			DR_DEV_SET_ATTACHED(&mp->sbm_cm);
3108a3114836SGerry Liu 			DEVSET_ADD(devs_attached, SBD_COMP_MEM, i);
3109a3114836SGerry Liu 			PR_ALL("%s: board %d, mem-unit %d - attached\n",
3110a3114836SGerry Liu 			    f, bp->b_num, i);
3111a3114836SGerry Liu 		}
3112a3114836SGerry Liu 		dr_init_mem_unit(mp);
3113a3114836SGerry Liu 	}
3114a3114836SGerry Liu 
3115a3114836SGerry Liu 	/*
3116a3114836SGerry Liu 	 * Check for i/o state.
3117a3114836SGerry Liu 	 */
3118a3114836SGerry Liu 	for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
3119a3114836SGerry Liu 		ip = dr_get_io_unit(bp, i);
3120a3114836SGerry Liu 
3121a3114836SGerry Liu 		if (!DR_DEV_IS_PRESENT(&ip->sbi_cm))
3122a3114836SGerry Liu 			continue;
3123a3114836SGerry Liu 
3124a3114836SGerry Liu 		if (dr_check_unit_attached(&ip->sbi_cm) >= 0) {
3125a3114836SGerry Liu 			/*
3126a3114836SGerry Liu 			 * Found it!
3127a3114836SGerry Liu 			 */
3128a3114836SGerry Liu 			DR_DEV_SET_ATTACHED(&ip->sbi_cm);
3129a3114836SGerry Liu 			DEVSET_ADD(devs_attached, SBD_COMP_IO, i);
3130a3114836SGerry Liu 			PR_ALL("%s: board %d, io-unit %d - attached\n",
3131a3114836SGerry Liu 			    f, bp->b_num, i);
3132a3114836SGerry Liu 		}
3133a3114836SGerry Liu 		dr_init_io_unit(ip);
3134a3114836SGerry Liu 	}
3135a3114836SGerry Liu 
3136a3114836SGerry Liu 	DR_DEVS_CONFIGURE(bp, devs_attached);
3137a3114836SGerry Liu 	if (devs_attached && ((devs_lost = DR_DEVS_UNATTACHED(bp)) != 0)) {
3138a3114836SGerry Liu 		int		ut;
3139a3114836SGerry Liu 
3140a3114836SGerry Liu 		/*
3141a3114836SGerry Liu 		 * It is not legal on board discovery to have a
3142a3114836SGerry Liu 		 * board that is only partially attached.  A board
3143a3114836SGerry Liu 		 * is either all attached or all connected.  If a
3144a3114836SGerry Liu 		 * board has at least one attached device, then
3145a3114836SGerry Liu 		 * the the remaining devices, if any, must have
3146a3114836SGerry Liu 		 * been lost or disconnected.  These devices can
3147a3114836SGerry Liu 		 * only be recovered by a full attach from scratch.
3148a3114836SGerry Liu 		 * Note that devices previously in the unreferenced
3149a3114836SGerry Liu 		 * state are subsequently lost until the next full
3150a3114836SGerry Liu 		 * attach.  This is necessary since the driver unload
3151a3114836SGerry Liu 		 * that must have occurred would have wiped out the
3152a3114836SGerry Liu 		 * information necessary to re-configure the device
3153a3114836SGerry Liu 		 * back online, e.g. memlist.
3154a3114836SGerry Liu 		 */
3155a3114836SGerry Liu 		PR_ALL("%s: some devices LOST (" DEVSET_FMT_STR ")...\n",
3156a3114836SGerry Liu 		    f, DEVSET_FMT_ARG(devs_lost));
3157a3114836SGerry Liu 
3158a3114836SGerry Liu 		for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
3159a3114836SGerry Liu 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_CPU, ut))
3160a3114836SGerry Liu 				continue;
3161a3114836SGerry Liu 
3162a3114836SGerry Liu 			cp = dr_get_cpu_unit(bp, ut);
3163a3114836SGerry Liu 			dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
3164a3114836SGerry Liu 		}
3165a3114836SGerry Liu 
3166a3114836SGerry Liu 		for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
3167a3114836SGerry Liu 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_MEM, ut))
3168a3114836SGerry Liu 				continue;
3169a3114836SGerry Liu 
3170a3114836SGerry Liu 			mp = dr_get_mem_unit(bp, ut);
3171a3114836SGerry Liu 			dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
3172a3114836SGerry Liu 		}
3173a3114836SGerry Liu 
3174a3114836SGerry Liu 		for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
3175a3114836SGerry Liu 			if (!DEVSET_IN_SET(devs_lost, SBD_COMP_IO, ut))
3176a3114836SGerry Liu 				continue;
3177a3114836SGerry Liu 
3178a3114836SGerry Liu 			ip = dr_get_io_unit(bp, ut);
3179a3114836SGerry Liu 			dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
3180a3114836SGerry Liu 		}
3181a3114836SGerry Liu 
3182a3114836SGerry Liu 		DR_DEVS_DISCONNECT(bp, devs_lost);
3183a3114836SGerry Liu 	}
3184a3114836SGerry Liu }
3185a3114836SGerry Liu 
3186a3114836SGerry Liu static int
dr_board_init(dr_board_t * bp,dev_info_t * dip,int bd)3187a3114836SGerry Liu dr_board_init(dr_board_t *bp, dev_info_t *dip, int bd)
3188a3114836SGerry Liu {
3189a3114836SGerry Liu 	sbd_error_t	*err;
3190a3114836SGerry Liu 
3191a3114836SGerry Liu 	mutex_init(&bp->b_lock, NULL, MUTEX_DRIVER, NULL);
3192a3114836SGerry Liu 	mutex_init(&bp->b_slock, NULL, MUTEX_DRIVER, NULL);
3193a3114836SGerry Liu 	cv_init(&bp->b_scv, NULL, CV_DRIVER, NULL);
3194a3114836SGerry Liu 	bp->b_rstate = SBD_STAT_EMPTY;
3195a3114836SGerry Liu 	bp->b_ostate = SBD_STAT_UNCONFIGURED;
3196a3114836SGerry Liu 	bp->b_cond = SBD_COND_UNKNOWN;
3197a3114836SGerry Liu 	(void) drv_getparm(TIME, (void *)&bp->b_time);
3198a3114836SGerry Liu 
3199a3114836SGerry Liu 	(void) drmach_board_lookup(bd, &bp->b_id);
3200a3114836SGerry Liu 	bp->b_num = bd;
3201a3114836SGerry Liu 	bp->b_dip = dip;
3202a3114836SGerry Liu 
3203a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)] = GETSTRUCT(dr_dev_unit_t,
3204a3114836SGerry Liu 	    MAX_CPU_UNITS_PER_BOARD);
3205a3114836SGerry Liu 
3206a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)] = GETSTRUCT(dr_dev_unit_t,
3207a3114836SGerry Liu 	    MAX_MEM_UNITS_PER_BOARD);
3208a3114836SGerry Liu 
3209a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_IO)] = GETSTRUCT(dr_dev_unit_t,
3210a3114836SGerry Liu 	    MAX_IO_UNITS_PER_BOARD);
3211a3114836SGerry Liu 
3212a3114836SGerry Liu 	/*
3213a3114836SGerry Liu 	 * Initialize the devlists
3214a3114836SGerry Liu 	 */
3215a3114836SGerry Liu 	err = dr_init_devlists(bp);
3216a3114836SGerry Liu 	if (err) {
3217a3114836SGerry Liu 		sbd_err_clear(&err);
3218a3114836SGerry Liu 		dr_board_destroy(bp);
3219a3114836SGerry Liu 		return (-1);
3220a3114836SGerry Liu 	} else if (bp->b_ndev == 0) {
3221a3114836SGerry Liu 		dr_board_transition(bp, DR_STATE_EMPTY);
3222a3114836SGerry Liu 	} else {
3223a3114836SGerry Liu 		/*
3224a3114836SGerry Liu 		 * Couldn't have made it down here without
3225a3114836SGerry Liu 		 * having found at least one device.
3226a3114836SGerry Liu 		 */
3227a3114836SGerry Liu 		ASSERT(DR_DEVS_PRESENT(bp) != 0);
3228a3114836SGerry Liu 		/*
3229a3114836SGerry Liu 		 * Check the state of any possible devices on the
3230a3114836SGerry Liu 		 * board.
3231a3114836SGerry Liu 		 */
3232a3114836SGerry Liu 		dr_board_discovery(bp);
3233a3114836SGerry Liu 
3234a3114836SGerry Liu 		bp->b_assigned = 1;
3235a3114836SGerry Liu 
3236a3114836SGerry Liu 		if (DR_DEVS_UNATTACHED(bp) == 0) {
3237a3114836SGerry Liu 			/*
3238a3114836SGerry Liu 			 * The board has no unattached devices, therefore
3239a3114836SGerry Liu 			 * by reason of insanity it must be configured!
3240a3114836SGerry Liu 			 */
3241a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_CONFIGURED);
3242a3114836SGerry Liu 			bp->b_ostate = SBD_STAT_CONFIGURED;
3243a3114836SGerry Liu 			bp->b_rstate = SBD_STAT_CONNECTED;
3244a3114836SGerry Liu 			bp->b_cond = SBD_COND_OK;
3245a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&bp->b_time);
3246a3114836SGerry Liu 		} else if (DR_DEVS_ATTACHED(bp)) {
3247a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_PARTIAL);
3248a3114836SGerry Liu 			bp->b_ostate = SBD_STAT_CONFIGURED;
3249a3114836SGerry Liu 			bp->b_rstate = SBD_STAT_CONNECTED;
3250a3114836SGerry Liu 			bp->b_cond = SBD_COND_OK;
3251a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&bp->b_time);
3252a3114836SGerry Liu 		} else {
3253a3114836SGerry Liu 			dr_board_transition(bp, DR_STATE_CONNECTED);
3254a3114836SGerry Liu 			bp->b_rstate = SBD_STAT_CONNECTED;
3255a3114836SGerry Liu 			(void) drv_getparm(TIME, (void *)&bp->b_time);
3256a3114836SGerry Liu 		}
3257a3114836SGerry Liu 	}
3258a3114836SGerry Liu 
3259a3114836SGerry Liu 	return (0);
3260a3114836SGerry Liu }
3261a3114836SGerry Liu 
3262a3114836SGerry Liu static void
dr_board_destroy(dr_board_t * bp)3263a3114836SGerry Liu dr_board_destroy(dr_board_t *bp)
3264a3114836SGerry Liu {
3265a3114836SGerry Liu 	PR_ALL("dr_board_destroy: num %d, path %s\n",
3266a3114836SGerry Liu 	    bp->b_num, bp->b_path);
3267a3114836SGerry Liu 
3268a3114836SGerry Liu 	dr_board_transition(bp, DR_STATE_EMPTY);
3269a3114836SGerry Liu 	bp->b_rstate = SBD_STAT_EMPTY;
3270a3114836SGerry Liu 	(void) drv_getparm(TIME, (void *)&bp->b_time);
3271a3114836SGerry Liu 
3272a3114836SGerry Liu 	/*
3273a3114836SGerry Liu 	 * Free up MEM unit structs.
3274a3114836SGerry Liu 	 */
3275a3114836SGerry Liu 	FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)],
3276a3114836SGerry Liu 	    dr_dev_unit_t, MAX_MEM_UNITS_PER_BOARD);
3277a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)] = NULL;
3278a3114836SGerry Liu 	/*
3279a3114836SGerry Liu 	 * Free up CPU unit structs.
3280a3114836SGerry Liu 	 */
3281a3114836SGerry Liu 	FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)],
3282a3114836SGerry Liu 	    dr_dev_unit_t, MAX_CPU_UNITS_PER_BOARD);
3283a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)] = NULL;
3284a3114836SGerry Liu 	/*
3285a3114836SGerry Liu 	 * Free up IO unit structs.
3286a3114836SGerry Liu 	 */
3287a3114836SGerry Liu 	FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_IO)],
3288a3114836SGerry Liu 	    dr_dev_unit_t, MAX_IO_UNITS_PER_BOARD);
3289a3114836SGerry Liu 	bp->b_dev[DEVSET_NIX(SBD_COMP_IO)] = NULL;
3290a3114836SGerry Liu 
3291a3114836SGerry Liu 	mutex_destroy(&bp->b_lock);
3292a3114836SGerry Liu 	mutex_destroy(&bp->b_slock);
3293a3114836SGerry Liu 	cv_destroy(&bp->b_scv);
3294a3114836SGerry Liu 
3295a3114836SGerry Liu 	/*
3296a3114836SGerry Liu 	 * Reset the board structure to its initial state, otherwise it will
3297a3114836SGerry Liu 	 * cause trouble on the next call to dr_board_init() for the same board.
3298a3114836SGerry Liu 	 * dr_board_init() may be called multiple times for the same board
3299a3114836SGerry Liu 	 * if DR driver fails to initialize some boards.
3300a3114836SGerry Liu 	 */
3301a3114836SGerry Liu 	bzero(bp, sizeof (*bp));
3302a3114836SGerry Liu }
3303a3114836SGerry Liu 
3304a3114836SGerry Liu void
dr_lock_status(dr_board_t * bp)3305a3114836SGerry Liu dr_lock_status(dr_board_t *bp)
3306a3114836SGerry Liu {
3307a3114836SGerry Liu 	mutex_enter(&bp->b_slock);
3308a3114836SGerry Liu 	while (bp->b_sflags & DR_BSLOCK)
3309a3114836SGerry Liu 		cv_wait(&bp->b_scv, &bp->b_slock);
3310a3114836SGerry Liu 	bp->b_sflags |= DR_BSLOCK;
3311a3114836SGerry Liu 	mutex_exit(&bp->b_slock);
3312a3114836SGerry Liu }
3313a3114836SGerry Liu 
3314a3114836SGerry Liu void
dr_unlock_status(dr_board_t * bp)3315a3114836SGerry Liu dr_unlock_status(dr_board_t *bp)
3316a3114836SGerry Liu {
3317a3114836SGerry Liu 	mutex_enter(&bp->b_slock);
3318a3114836SGerry Liu 	bp->b_sflags &= ~DR_BSLOCK;
3319a3114836SGerry Liu 	cv_signal(&bp->b_scv);
3320a3114836SGerry Liu 	mutex_exit(&bp->b_slock);
3321a3114836SGerry Liu }
3322a3114836SGerry Liu 
3323a3114836SGerry Liu /*
3324a3114836SGerry Liu  * Extract flags passed via ioctl.
3325a3114836SGerry Liu  */
3326a3114836SGerry Liu int
dr_cmd_flags(dr_handle_t * hp)3327a3114836SGerry Liu dr_cmd_flags(dr_handle_t *hp)
3328a3114836SGerry Liu {
3329a3114836SGerry Liu 	return (hp->h_sbdcmd.cmd_cm.c_flags);
3330a3114836SGerry Liu }
3331