xref: /illumos-gate/usr/src/uts/common/io/pshot.c (revision 8c4267180173328ebba9487634f0f232387d067f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
27  * Copyright (c) 2016 by Delphix. All rights reserved.
28  * Copyright 2023 Oxide Computer Company
29  */
30 
31 /*
32  * pseudo bus nexus driver
33  * hotplug framework test facility
34  */
35 
36 /*
37  * The pshot driver can be used to exercise the i/o framework together
38  * with devfs by configuring an arbitrarily complex device tree.
39  *
40  * The pshot driver is rooted at /devices/pshot.  The following commands
41  * illustrate the operation of devfs together with pshot's bus_config.
42  * The first command demonstrates that, like the magician showing there's
43  * nothing up their sleeve, /devices/pshot is empty.  The second command
44  * conjures up a branch of pshot nodes.  Note that pshot's bus_config is
45  * called sequentially by devfs for each node, as part of the pathname
46  * resolution, and that each pshot node is fully configured and
47  * attached before that node's bus_config is called to configure the
48  * next child down the tree.  The final result is a "disk" node configured
49  * at the bottom of the named hierarchy of pshot nodes.
50  *
51  *	#
52  *	# ls /devices/pshot
53  *	#
54  *	# ls -ld /devices/pshot/pshot@0/pshot@1/pshot@2/disk@3,0
55  *	drwxr-xr-x   2 root     sys          512 Feb  6 15:10
56  *		/devices/pshot/pshot@0/pshot@1/pshot@2/disk@3,0
57  *
58  * pshot supports some unique behaviors as aids for test error cases.
59  *
60  * Match these special address formats to behavior:
61  *
62  *	err.*		- induce bus_config error
63  *	delay		- induce 1 second of bus_config delay time
64  *	delay,n		- induce n seconds of bus_config delay time
65  *	wait		- induce 1 second of bus_config wait time
66  *	wait,n		- induce n seconds of bus_config wait time
67  *	failinit.*	- induce error at INITCHILD
68  *	failprobe.*	- induce error at probe
69  *	failattach.*	- induce error at attach
70  */
71 
72 #if defined(lint) && !defined(DEBUG)
73 #define	DEBUG	1
74 #endif
75 
76 #include <sys/types.h>
77 #include <sys/cmn_err.h>
78 #include <sys/conf.h>
79 #include <sys/ddi_impldefs.h>
80 #include <sys/autoconf.h>
81 #include <sys/open.h>
82 #include <sys/stat.h>
83 #include <sys/file.h>
84 #include <sys/errno.h>
85 #include <sys/systm.h>
86 #include <sys/modctl.h>
87 #include <sys/kmem.h>
88 #include <sys/ddi.h>
89 #include <sys/sunddi.h>
90 #include <sys/sunndi.h>
91 #include <sys/devctl.h>
92 #include <sys/disp.h>
93 #include <sys/utsname.h>
94 #include <sys/pshot.h>
95 #include <sys/debug.h>
96 
97 static int pshot_log		= 0;
98 static int pshot_devctl_debug	= 0;
99 static int pshot_debug_busy	= 0;
100 
101 static void *pshot_softstatep;
102 
103 static int pshot_prop_autoattach;
104 
105 #define	MAXPWR	3
106 
107 
108 /*
109  * device configuration data
110  */
111 
112 /* should keep in sync with current release */
113 static struct {
114 	char *name;
115 	char *val;
116 } pshot_nodetypes[] = {
117 	{"DDI_NT_SERIAL", DDI_NT_SERIAL},
118 	{"DDI_NT_SERIAL_MB", DDI_NT_SERIAL_MB},
119 	{"DDI_NT_SERIAL_DO", DDI_NT_SERIAL_DO},
120 	{"DDI_NT_SERIAL_MB_DO", DDI_NT_SERIAL_MB_DO},
121 	{"DDI_NT_SERIAL_LOMCON", DDI_NT_SERIAL_LOMCON},
122 	{"DDI_NT_BLOCK", DDI_NT_BLOCK},
123 	{"DDI_NT_BLOCK_CHAN", DDI_NT_BLOCK_CHAN},
124 	{"DDI_NT_BLOCK_WWN", DDI_NT_BLOCK_WWN},
125 	{"DDI_NT_BLOCK_SAS", DDI_NT_BLOCK_SAS},
126 	{"DDI_NT_CD", DDI_NT_CD},
127 	{"DDI_NT_CD_CHAN", DDI_NT_CD_CHAN},
128 	{"DDI_NT_FD", DDI_NT_FD},
129 	{"DDI_NT_ENCLOSURE", DDI_NT_ENCLOSURE},
130 	{"DDI_NT_SCSI_ENCLOSURE", DDI_NT_SCSI_ENCLOSURE},
131 	{"DDI_NT_TAPE", DDI_NT_TAPE},
132 	{"DDI_NT_NET", DDI_NT_NET},
133 	{"DDI_NT_DISPLAY", DDI_NT_DISPLAY},
134 	{"DDI_PSEUDO", DDI_PSEUDO},
135 	{"DDI_NT_AUDIO", DDI_NT_AUDIO},
136 	{"DDI_NT_MOUSE", DDI_NT_MOUSE},
137 	{"DDI_NT_KEYBOARD", DDI_NT_KEYBOARD},
138 	{"DDI_NT_PARALLEL", DDI_NT_PARALLEL},
139 	{"DDI_NT_PRINTER", DDI_NT_PRINTER},
140 	{"DDI_NT_UGEN", DDI_NT_UGEN},
141 	{"DDI_NT_NEXUS", DDI_NT_NEXUS},
142 	{"DDI_NT_SCSI_NEXUS", DDI_NT_SCSI_NEXUS},
143 	{"DDI_NT_ATTACHMENT_POINT", DDI_NT_ATTACHMENT_POINT},
144 	{"DDI_NT_SCSI_ATTACHMENT_POINT", DDI_NT_SCSI_ATTACHMENT_POINT},
145 	{"DDI_NT_PCI_ATTACHMENT_POINT", DDI_NT_PCI_ATTACHMENT_POINT},
146 	{"DDI_NT_SBD_ATTACHMENT_POINT", DDI_NT_SBD_ATTACHMENT_POINT},
147 	{"DDI_NT_FC_ATTACHMENT_POINT", DDI_NT_FC_ATTACHMENT_POINT},
148 	{"DDI_NT_USB_ATTACHMENT_POINT", DDI_NT_USB_ATTACHMENT_POINT},
149 	{"DDI_NT_BLOCK_FABRIC", DDI_NT_BLOCK_FABRIC},
150 	{"DDI_NT_AV_ASYNC", DDI_NT_AV_ASYNC},
151 	{"DDI_NT_AV_ISOCH", DDI_NT_AV_ISOCH},
152 	{ NULL, NULL }
153 };
154 
155 /* Node name */
156 static char *pshot_compat_diskname = "cdisk";
157 
158 /* Compatible names... */
159 static char *pshot_compat_psramdisks[] = {
160 	"psramhead",
161 	"psramrom",
162 	"psramdisk",
163 	"psramd",
164 	"psramwhat"
165 };
166 
167 /*
168  * devices "natively" supported by pshot (i.e. included with SUNWiotu)
169  * used to initialize pshot_devices with
170  */
171 static pshot_device_t pshot_stock_devices[] = {
172 	{"disk",	DDI_NT_BLOCK,		"gen_drv"},
173 	{"disk_chan",	DDI_NT_BLOCK_CHAN,	"gen_drv"},
174 	{"disk_wwn",	DDI_NT_BLOCK_WWN,	"gen_drv"},
175 	{"disk_cdrom",	DDI_NT_CD,		"gen_drv"},
176 	{"disk_cdrom.chan", DDI_NT_CD_CHAN,	"gen_drv"},
177 /* Note: use bad_drv to force attach errors */
178 	{"disk_fd",	DDI_NT_FD,		"bad_drv"},
179 	{"tape",	DDI_NT_TAPE,		"gen_drv"},
180 	{"net",		DDI_NT_NET,		"gen_drv"},
181 	{"display",	DDI_NT_DISPLAY,		"gen_drv"},
182 	{"pseudo",	DDI_PSEUDO,		"gen_drv"},
183 	{"audio",	DDI_NT_AUDIO,		"gen_drv"},
184 	{"mouse",	DDI_NT_MOUSE,		"gen_drv"},
185 	{"keyboard",	DDI_NT_KEYBOARD,	"gen_drv"},
186 	{"nexus",	DDI_NT_NEXUS,		"pshot"}
187 };
188 #define	PSHOT_N_STOCK_DEVICES \
189 	(sizeof (pshot_stock_devices) / sizeof (pshot_device_t))
190 
191 static pshot_device_t *pshot_devices = NULL;
192 static size_t pshot_devices_len = 0;
193 
194 /* protects <pshot_devices>, <pshot_devices_len> */
195 static kmutex_t pshot_devices_lock;
196 
197 
198 /*
199  * event testing
200  */
201 
202 static ndi_event_definition_t pshot_ndi_event_defs[] = {
203 { PSHOT_EVENT_TAG_OFFLINE, PSHOT_EVENT_NAME_DEV_OFFLINE,
204 	EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
205 
206 { PSHOT_EVENT_TAG_DEV_RESET, PSHOT_EVENT_NAME_DEV_RESET,
207 	EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT },
208 
209 { PSHOT_EVENT_TAG_BUS_RESET, PSHOT_EVENT_NAME_BUS_RESET,
210 	EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
211 
212 { PSHOT_EVENT_TAG_BUS_QUIESCE, PSHOT_EVENT_NAME_BUS_QUIESCE,
213 	EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
214 
215 { PSHOT_EVENT_TAG_BUS_UNQUIESCE, PSHOT_EVENT_NAME_BUS_UNQUIESCE,
216 	EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
217 
218 { PSHOT_EVENT_TAG_TEST_POST, PSHOT_EVENT_NAME_BUS_TEST_POST,
219 	EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT }
220 };
221 
222 
223 #define	PSHOT_N_NDI_EVENTS \
224 	(sizeof (pshot_ndi_event_defs) / sizeof (ndi_event_definition_t))
225 
226 #ifdef DEBUG
227 
228 static ndi_event_definition_t pshot_test_events[] = {
229 { 10, "test event 0", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
230 { 11, "test event 1", EPL_KERNEL, NDI_EVENT_POST_TO_TGT },
231 { 12, "test event 2", EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT },
232 { 13, "test event 3", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
233 { 14, "test event 4", EPL_KERNEL, NDI_EVENT_POST_TO_ALL},
234 { 15, "test event 5", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
235 { 16, "test event 6", EPL_KERNEL, NDI_EVENT_POST_TO_ALL },
236 { 17, "test event 7", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL }
237 };
238 
239 static ndi_event_definition_t pshot_test_events_high[] = {
240 { 20, "test event high 0", EPL_HIGHLEVEL, NDI_EVENT_POST_TO_ALL}
241 };
242 
243 #define	PSHOT_N_TEST_EVENTS \
244 	(sizeof (pshot_test_events)/sizeof (ndi_event_definition_t))
245 #endif
246 
247 struct register_events {
248 	char		*event_name;
249 	ddi_eventcookie_t event_cookie;
250 	void	(*event_callback)
251 			(dev_info_t *,
252 			ddi_eventcookie_t,
253 			void *arg,
254 			void *impldata);
255 	ddi_callback_id_t cb_id;
256 };
257 
258 struct register_events pshot_register_events[] = {
259 { PSHOT_EVENT_NAME_DEV_OFFLINE, 0, pshot_event_cb, 0 },
260 { PSHOT_EVENT_NAME_DEV_RESET, 0, pshot_event_cb, 0 },
261 { PSHOT_EVENT_NAME_BUS_RESET, 0, pshot_event_cb, 0 },
262 { PSHOT_EVENT_NAME_BUS_QUIESCE, 0, pshot_event_cb, 0 },
263 { PSHOT_EVENT_NAME_BUS_UNQUIESCE, 0, pshot_event_cb, 0 },
264 { PSHOT_EVENT_NAME_BUS_TEST_POST, 0, pshot_event_cb, 0 }
265 };
266 
267 #define	PSHOT_N_DDI_EVENTS \
268 	(sizeof (pshot_register_events) / sizeof (struct register_events))
269 
270 
271 #ifdef DEBUG
272 
273 static struct register_events pshot_register_test[] = {
274 { "test event 0", 0, pshot_event_cb_test, 0},
275 { "test event 1", 0, pshot_event_cb_test, 0},
276 { "test event 2", 0, pshot_event_cb_test, 0},
277 { "test event 3", 0, pshot_event_cb_test, 0},
278 { "test event 4", 0, pshot_event_cb_test, 0},
279 { "test event 5", 0, pshot_event_cb_test, 0},
280 { "test event 6", 0, pshot_event_cb_test, 0},
281 { "test event 7", 0, pshot_event_cb_test, 0}
282 };
283 
284 
285 static struct register_events pshot_register_high_test[] = {
286 	{"test event high 0", 0, pshot_event_cb_test, 0}
287 };
288 
289 #endif /* DEBUG */
290 
291 static struct {
292 	int ioctl_int;
293 	char *ioctl_char;
294 } pshot_devctls[] = {
295 	{DEVCTL_DEVICE_GETSTATE, "DEVCTL_DEVICE_GETSTATE"},
296 	{DEVCTL_DEVICE_ONLINE, "DEVCTL_DEVICE_ONLINE"},
297 	{DEVCTL_DEVICE_OFFLINE, "DEVCTL_DEVICE_OFFLINE"},
298 	{DEVCTL_DEVICE_REMOVE, "DEVCTL_DEVICE_REMOVE"},
299 	{DEVCTL_BUS_GETSTATE, "DEVCTL_BUS_GETSTATE"},
300 	{DEVCTL_BUS_DEV_CREATE, "DEVCTL_BUS_DEV_CREATE"},
301 	{DEVCTL_BUS_RESET, "DEVCTL_BUS_RESET"},
302 	{DEVCTL_BUS_RESETALL, "DEVCTL_BUS_RESETALL"},
303 	{0, NULL}
304 };
305 
306 static struct bus_ops pshot_bus_ops = {
307 	BUSO_REV,			/* busops_rev */
308 	nullbusmap,			/* bus_map */
309 	NULL,				/* bus_get_intrspec */
310 	NULL,				/* bus_add_interspec */
311 	NULL,				/* bus_remove_interspec */
312 	i_ddi_map_fault,		/* bus_map_fault */
313 	NULL,				/* bus_dma_map */
314 	ddi_dma_allochdl,		/* bus_dma_allochdl */
315 	ddi_dma_freehdl,		/* bus_dma_freehdl */
316 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
317 	ddi_dma_unbindhdl,		/* bus_dma_unbindhdl */
318 	ddi_dma_flush,			/* bus_dma_flush */
319 	ddi_dma_win,			/* bus_dma_win */
320 	ddi_dma_mctl,			/* bus_dma_ctl */
321 	pshot_ctl,			/* bus_ctl */
322 	ddi_bus_prop_op,		/* bus_prop_op */
323 	pshot_get_eventcookie,		/* bus_get_eventcookie */
324 	pshot_add_eventcall,		/* bus_add_eventcall */
325 	pshot_remove_eventcall,		/* bus_remove_event */
326 	pshot_post_event,		/* bus_post_event */
327 	NULL,				/* bus_intr_ctl */
328 	pshot_bus_config,		/* bus_config */
329 	pshot_bus_unconfig,		/* bus_unconfig */
330 	NULL,				/* bus_fm_init */
331 	NULL,				/* bus_fm_fini */
332 	NULL,				/* bus_fm_access_enter */
333 	NULL,				/* bus_fm_access_exit */
334 	pshot_bus_power,		/* bus_power */
335 	pshot_bus_introp		/* bus_intr_op */
336 };
337 
338 static struct cb_ops pshot_cb_ops = {
339 	pshot_open,			/* open */
340 	pshot_close,			/* close */
341 	nodev,				/* strategy */
342 	nodev,				/* print */
343 	nodev,				/* dump */
344 	nodev,				/* read */
345 	nodev,				/* write */
346 	pshot_ioctl,			/* ioctl */
347 	nodev,				/* devmap */
348 	nodev,				/* mmap */
349 	nodev,				/* segmap */
350 	nochpoll,			/* poll */
351 	ddi_prop_op,			/* prop_op */
352 	NULL,				/* streamtab */
353 	D_NEW | D_MP | D_HOTPLUG,	/* flags */
354 	CB_REV,				/* cb_rev */
355 	nodev,				/* aread */
356 	nodev,				/* awrite */
357 };
358 
359 static struct dev_ops pshot_ops = {
360 	DEVO_REV,		/* devo_rev, */
361 	0,			/* refcnt  */
362 	pshot_info,		/* getinfo */
363 	nulldev,		/* identify */
364 	pshot_probe,		/* probe */
365 	pshot_attach,		/* attach */
366 	pshot_detach,		/* detach */
367 	nodev,			/* reset */
368 	&pshot_cb_ops,		/* driver operations */
369 	&pshot_bus_ops,		/* bus operations */
370 	pshot_power,		/* power */
371 	ddi_quiesce_not_supported,	/* devo_quiesce */
372 
373 };
374 
375 
376 /*
377  * Module linkage information for the kernel.
378  */
379 static struct modldrv modldrv = {
380 	&mod_driverops,
381 	"pshotnex",
382 	&pshot_ops,
383 };
384 
385 static struct modlinkage modlinkage = {
386 	MODREV_1, &modldrv, NULL
387 };
388 
389 
390 /*
391  * pshot_devices is set up on the first attach and destroyed on fini
392  *
393  * therefore PSHOT_PROP_DEV* properties may be set just for the root device,
394  * instead of being set globably, in pshot.conf by specifying the properties
395  * on a single line in the form:
396  *	name="pshot" parent="/" <dev props ..>
397  * to unclutter a device tree snapshot.
398  * this of course produces a long single line that may wrap around several
399  * times on screen
400  */
401 
402 int
403 _init(void)
404 {
405 	int rv;
406 
407 	rv = ddi_soft_state_init(&pshot_softstatep, sizeof (pshot_t), 0);
408 
409 	if (rv != DDI_SUCCESS)
410 		return (rv);
411 
412 	mutex_init(&pshot_devices_lock, NULL, MUTEX_DRIVER, NULL);
413 	pshot_devices = NULL;
414 	pshot_devices_len = 0;
415 
416 	if ((rv = mod_install(&modlinkage)) != 0) {
417 		ddi_soft_state_fini(&pshot_softstatep);
418 		mutex_destroy(&pshot_devices_lock);
419 	}
420 	return (rv);
421 }
422 
423 int
424 _fini(void)
425 {
426 	int rv;
427 
428 	if ((rv = mod_remove(&modlinkage)) != 0)
429 		return (rv);
430 
431 	ddi_soft_state_fini(&pshot_softstatep);
432 	mutex_destroy(&pshot_devices_lock);
433 	if (pshot_devices)
434 		pshot_devices_free(pshot_devices, pshot_devices_len);
435 	return (0);
436 }
437 
438 int
439 _info(struct modinfo *modinfop)
440 {
441 	return (mod_info(&modlinkage, modinfop));
442 }
443 
444 
445 /*ARGSUSED*/
446 static int
447 pshot_probe(dev_info_t *devi)
448 {
449 	int	instance = ddi_get_instance(devi);
450 	char	*bus_addr;
451 
452 	/*
453 	 * Hook for tests to force probe fail
454 	 */
455 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "bus-addr",
456 	    &bus_addr) == DDI_PROP_SUCCESS) {
457 		if (strncmp(bus_addr, "failprobe", 9) == 0) {
458 			if (pshot_debug)
459 				cmn_err(CE_CONT, "pshot%d: "
460 				    "%s forced probe failure\n",
461 				    instance, bus_addr);
462 			ddi_prop_free(bus_addr);
463 			return (DDI_PROBE_FAILURE);
464 		}
465 		ddi_prop_free(bus_addr);
466 	}
467 
468 	return (DDI_PROBE_SUCCESS);
469 }
470 
471 
472 /*ARGSUSED*/
473 static int
474 pshot_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
475 {
476 	int instance;
477 	minor_t minor;
478 	pshot_t *pshot;
479 
480 	minor = getminor((dev_t)arg);
481 	instance = pshot_minor_decode_inst(minor);
482 	switch (infocmd) {
483 	case DDI_INFO_DEVT2DEVINFO:
484 		pshot = ddi_get_soft_state(pshot_softstatep, instance);
485 		if (pshot == NULL) {
486 			cmn_err(CE_WARN, "pshot_info: get soft state failed "
487 			    "on minor %u, instance %d", minor, instance);
488 			return (DDI_FAILURE);
489 		}
490 		*result = (void *)pshot->dip;
491 		break;
492 	case DDI_INFO_DEVT2INSTANCE:
493 		*result = (void *)(uintptr_t)instance;
494 		break;
495 	default:
496 		cmn_err(CE_WARN, "pshot_info: unrecognized cmd 0x%x on "
497 		    "minor %u, instance %d", infocmd, minor, instance);
498 		return (DDI_FAILURE);
499 	}
500 
501 	return (DDI_SUCCESS);
502 }
503 
504 
505 static int
506 pshot_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
507 {
508 	int instance = ddi_get_instance(devi);
509 	pshot_t *pshot;
510 	int rval, i;
511 	int prop_flags = DDI_PROP_DONTPASS | DDI_PROP_NOTPROM;
512 	char *bus_addr;
513 	char *pm_comp[] = {
514 		"NAME=bus",
515 		"0=B3",
516 		"1=B2",
517 		"2=B1",
518 		"3=B0"};
519 	char *pm_hw_state = {"needs-suspend-resume"};
520 
521 	pshot_prop_autoattach = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
522 	    prop_flags, "autoattach", 0);
523 
524 	switch (cmd) {
525 
526 	case DDI_ATTACH:
527 		if (pshot_debug)
528 			cmn_err(CE_CONT, "attach: %s%d/pshot%d\n",
529 			    ddi_get_name(ddi_get_parent(devi)),
530 			    ddi_get_instance(ddi_get_parent(devi)),
531 			    instance);
532 
533 		/*
534 		 * Hook for tests to force attach fail
535 		 */
536 		if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "bus-addr",
537 		    &bus_addr) == DDI_PROP_SUCCESS) && bus_addr != NULL) {
538 			if (strncmp(bus_addr, "failattach", 10) == 0) {
539 				if (pshot_debug)
540 					cmn_err(CE_CONT, "pshot%d: "
541 					    "%s forced attach failure\n",
542 					    instance, bus_addr);
543 				ddi_prop_free(bus_addr);
544 				return (DDI_FAILURE);
545 			}
546 			ddi_prop_free(bus_addr);
547 		}
548 
549 		/*
550 		 * minor nodes setup
551 		 */
552 		if (ddi_soft_state_zalloc(pshot_softstatep, instance) !=
553 		    DDI_SUCCESS) {
554 			return (DDI_FAILURE);
555 		}
556 		pshot = ddi_get_soft_state(pshot_softstatep, instance);
557 		pshot->dip = devi;
558 		pshot->instance = instance;
559 		mutex_init(&pshot->lock, NULL, MUTEX_DRIVER, NULL);
560 
561 		/* set each minor, then create on dip all together */
562 
563 		i = PSHOT_NODENUM_DEVCTL;
564 		pshot->nodes[i].pshot = pshot;
565 		pshot->nodes[i].minor = pshot_minor_encode(instance, i);
566 		(void) strncpy(pshot->nodes[i].name, PSHOT_NODENAME_DEVCTL,
567 		    PSHOT_MAX_MINOR_NAMELEN);
568 
569 		i = PSHOT_NODENUM_TESTCTL;
570 		pshot->nodes[i].pshot = pshot;
571 		pshot->nodes[i].minor = pshot_minor_encode(instance, i);
572 		(void) strncpy(pshot->nodes[i].name, PSHOT_NODENAME_TESTCTL,
573 		    PSHOT_MAX_MINOR_NAMELEN);
574 
575 		/* this assumes contiguous a filling */
576 		for (i = 0; i <= PSHOT_MAX_NODENUM; i++) {
577 			if (ddi_create_minor_node(devi, pshot->nodes[i].name,
578 			    S_IFCHR, pshot->nodes[i].minor, DDI_NT_NEXUS, 0) !=
579 			    DDI_SUCCESS) {
580 				cmn_err(CE_WARN, "attach: cannot create "
581 				    "minor %s", pshot->nodes[i].name);
582 				goto FAIL_ATTACH;
583 			}
584 		}
585 
586 		/*
587 		 * pshot_devices setup
588 		 */
589 		if (pshot_devices_setup(devi)) {
590 			cmn_err(CE_WARN, "attach: pshot devices setup "
591 			    "failed");
592 			goto FAIL_ATTACH;
593 		}
594 
595 		/*
596 		 * events setup
597 		 */
598 		for (i = 0; i < PSHOT_N_DDI_EVENTS; i++) {
599 			rval =	ddi_get_eventcookie(devi,
600 			    pshot_register_events[i].event_name,
601 			    &pshot_register_events[i].event_cookie);
602 
603 			if (pshot_debug)
604 				cmn_err(CE_CONT, "pshot%d: event=%s:"
605 				    "ddi_get_eventcookie rval=%d\n",
606 				    instance,
607 				    pshot_register_events[i].event_name, rval);
608 
609 			if (rval == DDI_SUCCESS) {
610 				rval = ddi_add_event_handler(devi,
611 				    pshot_register_events[i].event_cookie,
612 				    pshot_register_events[i].event_callback,
613 				    (void *)pshot,
614 				    &pshot->callback_cache[i]);
615 
616 				if (pshot_debug)
617 					cmn_err(CE_CONT, "pshot%d: event=%s: "
618 					    "ddi_add_event_handler rval=%d\n",
619 					    instance,
620 					    pshot_register_events[i].event_name,
621 					    rval);
622 			}
623 		}
624 
625 #ifdef DEBUG
626 		if (pshot_event_test_enable) {
627 			pshot_event_test((void *)pshot);
628 			(void) timeout(pshot_event_test_post_one, (void *)pshot,
629 			    instance * drv_usectohz(60000000));
630 		}
631 #endif
632 
633 		/*
634 		 * allocate an ndi event handle
635 		 */
636 		if (ndi_event_alloc_hdl(devi, NULL, &pshot->ndi_event_hdl,
637 		    NDI_SLEEP) != NDI_SUCCESS) {
638 			goto FAIL_ATTACH;
639 		}
640 
641 		pshot->ndi_events.ndi_events_version = NDI_EVENTS_REV1;
642 		pshot->ndi_events.ndi_n_events = PSHOT_N_NDI_EVENTS;
643 		pshot->ndi_events.ndi_event_defs = pshot_ndi_event_defs;
644 
645 		if (ndi_event_bind_set(pshot->ndi_event_hdl, &pshot->ndi_events,
646 		    NDI_SLEEP) != NDI_SUCCESS) {
647 			cmn_err(CE_CONT, "pshot%d bind set failed\n",
648 			    instance);
649 		}
650 
651 		/*
652 		 * setup a test for nexus auto-attach iff we are
653 		 * a second level pshot node (parent == /SUNW,pshot)
654 		 * enable by setting "autoattach=1" in pshot.conf
655 		 */
656 		if ((PARENT_IS_PSHOT(devi)) && (pshot_prop_autoattach != 0) &&
657 		    (ddi_get_instance(ddi_get_parent(devi))) == 0)
658 			pshot_setup_autoattach(devi);
659 
660 		/*
661 		 * initialize internal state to idle: busy = 0,
662 		 * power level = -1
663 		 */
664 		mutex_enter(&pshot->lock);
665 		pshot->busy = 0;
666 		pshot->busy_ioctl = 0;
667 		pshot->level = -1;
668 		pshot->state &= ~STRICT_PARENT;
669 		pshot->state |= PM_SUPPORTED;
670 		mutex_exit(&pshot->lock);
671 
672 		/*
673 		 * Create the "pm-want-child-notification?" property
674 		 * for the root node /devices/pshot
675 		 */
676 		if (instance == 0) {
677 			if (pshot_debug) {
678 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:\n\t"
679 				    " create the"
680 				    " \"pm-want-child-notification?\" property"
681 				    " for the root node\n", instance);
682 			}
683 			if (ddi_prop_create(DDI_DEV_T_NONE, devi, 0,
684 			    "pm-want-child-notification?", NULL, 0)
685 			    != DDI_PROP_SUCCESS) {
686 				cmn_err(CE_WARN, "%s%d:\n\t"
687 				    " unable to create the"
688 				    " \"pm-want-child-notification?\""
689 				    " property", ddi_get_name(devi),
690 				    ddi_get_instance(devi));
691 
692 				goto FAIL_ATTACH;
693 			}
694 		}
695 
696 		/*
697 		 * Check if the pm-want-child-notification? property was
698 		 * created in pshot_bus_config_setup_nexus() by the parent.
699 		 * Set the STRICT_PARENT flag if not.
700 		 */
701 		if (ddi_prop_exists(DDI_DEV_T_ANY, devi,
702 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
703 		    "pm-want-child-notification?") != 1) {
704 			if (pshot_debug) {
705 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
706 				    " STRICT PARENT\n", instance);
707 			}
708 			mutex_enter(&pshot->lock);
709 			pshot->state |= STRICT_PARENT;
710 			mutex_exit(&pshot->lock);
711 		} else {
712 			if (pshot_debug) {
713 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
714 				    " INVOLVED PARENT\n", instance);
715 			}
716 			mutex_enter(&pshot->lock);
717 			pshot->state &= ~STRICT_PARENT;
718 			mutex_exit(&pshot->lock);
719 		}
720 
721 		/*
722 		 * create the pm-components property: one component
723 		 * with 4 power levels.
724 		 * - skip for pshot@XXX,nopm and pshot@XXX,nopm_strict:
725 		 * "no-pm-components" property
726 		 */
727 		if (ddi_prop_exists(DDI_DEV_T_ANY, devi,
728 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
729 		    "no-pm-components") == 0) {
730 			if (pshot_debug) {
731 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
732 				    " create the \"pm_components\" property\n",
733 				    instance);
734 			}
735 			if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
736 			    "pm-components", pm_comp, 5) != DDI_PROP_SUCCESS) {
737 				cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t"
738 				    " unable to create the \"pm-components\""
739 				    " property", ddi_get_name(devi),
740 				    ddi_get_instance(devi));
741 
742 				goto FAIL_ATTACH;
743 			}
744 		} else {
745 			if (pshot_debug) {
746 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
747 				    " NO-PM_COMPONENTS PARENT\n", instance);
748 			}
749 			mutex_enter(&pshot->lock);
750 			pshot->state &= ~PM_SUPPORTED;
751 			mutex_exit(&pshot->lock);
752 		}
753 
754 		/*
755 		 * create the property needed to get DDI_SUSPEND
756 		 * and DDI_RESUME calls
757 		 */
758 		if (pshot_debug) {
759 			cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
760 			    " create pm-hardware-state property\n",
761 			    instance);
762 		}
763 		if (ddi_prop_update_string(DDI_DEV_T_NONE, devi,
764 		    "pm-hardware-state", pm_hw_state) != DDI_PROP_SUCCESS) {
765 			cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t"
766 			    " unable to create the \"pm-hardware-state\""
767 			    " property", ddi_get_name(devi),
768 			    ddi_get_instance(devi));
769 
770 			goto FAIL_ATTACH;
771 		}
772 
773 		/*
774 		 * set power level to max via pm_raise_power(),
775 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
776 		 */
777 		if (pshot->state & PM_SUPPORTED) {
778 			if (pshot_debug) {
779 				cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
780 				    " raise power to MAXPWR\n", instance);
781 			}
782 			if (pm_raise_power(pshot->dip, 0, MAXPWR) !=
783 			    DDI_SUCCESS) {
784 				cmn_err(CE_WARN, "%s%d: DDI_ATTACH:"
785 				    " pm_raise_power failed",
786 				    ddi_get_name(devi),
787 				    ddi_get_instance(devi));
788 
789 				goto FAIL_ATTACH;
790 
791 			}
792 		}
793 
794 		if (pshot_log)
795 			cmn_err(CE_CONT, "pshot%d attached\n", instance);
796 		ddi_report_dev(devi);
797 
798 		return (DDI_SUCCESS);
799 		/*NOTREACHED*/
800 FAIL_ATTACH:
801 		ddi_remove_minor_node(devi, NULL);
802 		mutex_destroy(&pshot->lock);
803 		ddi_soft_state_free(pshot_softstatep, instance);
804 		return (DDI_FAILURE);
805 
806 	case DDI_RESUME:
807 		if (pshot_debug) {
808 			cmn_err(CE_CONT, "pshot%d: DDI_RESUME: resuming\n",
809 			    instance);
810 		}
811 		pshot = ddi_get_soft_state(pshot_softstatep, instance);
812 
813 		/*
814 		 * set power level to max via pm_raise_power(),
815 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
816 		 */
817 		if (pshot->state & PM_SUPPORTED) {
818 			if (pshot_debug) {
819 				cmn_err(CE_CONT, "pshot%d: DDI_RESUME:"
820 				    " raise power to MAXPWR\n", instance);
821 			}
822 			if (pm_raise_power(pshot->dip, 0, MAXPWR) !=
823 			    DDI_SUCCESS) {
824 				cmn_err(CE_WARN, "%s%d: DDI_RESUME:"
825 				    " pm_raise_power failed",
826 				    ddi_get_name(devi),
827 				    ddi_get_instance(devi));
828 			}
829 		}
830 
831 		if (pshot_debug) {
832 			cmn_err(CE_CONT, "pshot%d: DDI_RESUME: resumed\n",
833 			    instance);
834 		}
835 		return (DDI_SUCCESS);
836 
837 	default:
838 		return (DDI_FAILURE);
839 	}
840 }
841 
842 static int
843 pshot_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
844 {
845 	int instance = ddi_get_instance(devi);
846 	int i, rval;
847 	pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
848 	int level_tmp;
849 
850 	if (pshot == NULL)
851 		return (DDI_FAILURE);
852 
853 	switch (cmd) {
854 
855 	case DDI_DETACH:
856 		if (pshot_debug)
857 			cmn_err(CE_CONT, "pshot%d: DDI_DETACH\n", instance);
858 		/*
859 		 * power off component 0
860 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
861 		 */
862 		if (pshot->state & PM_SUPPORTED) {
863 			if (pshot_debug) {
864 				cmn_err(CE_CONT, "pshot%d: DDI_DETACH:"
865 				    " power off\n", instance);
866 			}
867 			if (pm_lower_power(pshot->dip, 0, 0) != DDI_SUCCESS) {
868 				cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
869 				    "pm_lower_power failed for comp 0 to"
870 				    " level 0", ddi_get_name(devi),
871 				    ddi_get_instance(devi));
872 
873 				return (DDI_FAILURE);
874 			}
875 
876 			/*
877 			 * Check if the power level is actually OFF.
878 			 * Issue pm_power_has_changed if not.
879 			 */
880 			mutex_enter(&pshot->lock);
881 			if (pshot->level != 0) {
882 				if (pshot_debug) {
883 					cmn_err(CE_NOTE, "pshot%d:"
884 					    " DDI_DETACH: power off via"
885 					    " pm_power_has_changed instead\n",
886 					    instance);
887 				}
888 				level_tmp = pshot->level;
889 				pshot->level = 0;
890 				if (pm_power_has_changed(pshot->dip, 0, 0) !=
891 				    DDI_SUCCESS) {
892 					if (pshot_debug) {
893 						cmn_err(CE_NOTE, "pshot%d:"
894 						    " DDI_DETACH:"
895 						    " pm_power_has_changed"
896 						    " failed\n", instance);
897 					}
898 					pshot->level = level_tmp;
899 					mutex_exit(&pshot->lock);
900 
901 					return (DDI_FAILURE);
902 				}
903 			}
904 			mutex_exit(&pshot->lock);
905 		}
906 
907 		for (i = 0; i < PSHOT_N_DDI_EVENTS; i++) {
908 			if (pshot->callback_cache[i] != NULL) {
909 				rval = ddi_remove_event_handler(
910 				    pshot->callback_cache[i]);
911 				ASSERT(rval == DDI_SUCCESS);
912 			}
913 		}
914 
915 #ifdef DEBUG
916 		for (i = 0; i < PSHOT_N_TEST_EVENTS; i++) {
917 			if (pshot->test_callback_cache[i] != NULL) {
918 				rval = ddi_remove_event_handler(
919 				    pshot->test_callback_cache[i]);
920 				ASSERT(rval == DDI_SUCCESS);
921 			}
922 		}
923 #endif
924 		rval = ndi_event_free_hdl(pshot->ndi_event_hdl);
925 		ASSERT(rval == DDI_SUCCESS);
926 
927 		if (pshot_log)
928 			cmn_err(CE_CONT, "pshot%d detached\n", instance);
929 
930 		ddi_remove_minor_node(devi, NULL);
931 		mutex_destroy(&pshot->lock);
932 		ddi_soft_state_free(pshot_softstatep, instance);
933 		break;
934 
935 	case DDI_SUSPEND:
936 		if (pshot_debug)
937 			cmn_err(CE_CONT, "pshot%d: DDI_SUSPEND\n", instance);
938 		/*
939 		 * fail the suspend if FAIL_SUSPEND_FLAG is set.
940 		 * clear the FAIL_SUSPEND_FLAG flag
941 		 */
942 		mutex_enter(&pshot->lock);
943 		if (pshot->state & FAIL_SUSPEND_FLAG) {
944 			if (pshot_debug) {
945 				cmn_err(CE_CONT, "pshot%d:"
946 				    " FAIL_SUSPEND_FLAG set, fail suspend\n",
947 				    ddi_get_instance(devi));
948 			}
949 			pshot->state &= ~FAIL_SUSPEND_FLAG;
950 			rval = DDI_FAILURE;
951 		} else {
952 			rval = DDI_SUCCESS;
953 		}
954 		mutex_exit(&pshot->lock);
955 
956 		/*
957 		 * power OFF via pm_power_has_changed
958 		 */
959 		mutex_enter(&pshot->lock);
960 		if (pshot->state & PM_SUPPORTED) {
961 			if (pshot_debug) {
962 				cmn_err(CE_CONT, "pshot%d: DDI_SUSPEND:"
963 				    " power off via pm_power_has_changed\n",
964 				    instance);
965 			}
966 			level_tmp = pshot->level;
967 			pshot->level = 0;
968 			if (pm_power_has_changed(pshot->dip, 0, 0) !=
969 			    DDI_SUCCESS) {
970 				if (pshot_debug) {
971 					cmn_err(CE_NOTE, "pshot%d:"
972 					    " DDI_SUSPEND:"
973 					    " pm_power_has_changed failed\n",
974 					    instance);
975 				}
976 				pshot->level = level_tmp;
977 				rval = DDI_FAILURE;
978 			}
979 		}
980 		mutex_exit(&pshot->lock);
981 		return (rval);
982 
983 	default:
984 		break;
985 	}
986 
987 	return (DDI_SUCCESS);
988 }
989 
990 
991 /*
992  * returns number of bits to represent <val>
993  */
994 static size_t
995 pshot_numbits(size_t val)
996 {
997 	size_t bitcnt;
998 
999 	if (val == 0)
1000 		return (0);
1001 	for (bitcnt = 1; 1 << bitcnt < val; bitcnt++)
1002 		;
1003 	return (bitcnt);
1004 }
1005 
1006 /*
1007  * returns a minor number encoded with instance <inst> and an index <nodenum>
1008  * that identifies the minor node for this instance
1009  */
1010 static minor_t
1011 pshot_minor_encode(int inst, minor_t nodenum)
1012 {
1013 	return (((minor_t)inst << PSHOT_NODENUM_BITS()) |
1014 	    (((1 << PSHOT_NODENUM_BITS()) - 1) & nodenum));
1015 }
1016 
1017 /*
1018  * returns instance of <minor>
1019  */
1020 static int
1021 pshot_minor_decode_inst(minor_t minor)
1022 {
1023 	return (minor >> PSHOT_NODENUM_BITS());
1024 }
1025 
1026 /*
1027  * returns node number indexing a minor node for the instance in <minor>
1028  */
1029 static minor_t
1030 pshot_minor_decode_nodenum(minor_t minor)
1031 {
1032 	return (minor & ((1 << PSHOT_NODENUM_BITS()) - 1));
1033 }
1034 
1035 
1036 /*
1037  * pshot_bus_introp: pshot convert an interrupt number to an
1038  *			   interrupt. NO OP for pseudo drivers.
1039  */
1040 /*ARGSUSED*/
1041 static int
1042 pshot_bus_introp(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1043     ddi_intr_handle_impl_t *hdlp, void *result)
1044 {
1045 	return (DDI_FAILURE);
1046 }
1047 static int
1048 pshot_ctl(dev_info_t *dip, dev_info_t *rdip,
1049     ddi_ctl_enum_t ctlop, void *arg, void *result)
1050 {
1051 	int instance;
1052 	pshot_t *pshot;
1053 	char *childname;
1054 	int childinstance;
1055 	char *name;
1056 	struct attachspec *as;
1057 	struct detachspec *ds;
1058 	int rval = DDI_SUCCESS;
1059 	int no_pm_components_child;
1060 
1061 	name = ddi_get_name(dip);
1062 	instance = ddi_get_instance(dip);
1063 	pshot = ddi_get_soft_state(pshot_softstatep, instance);
1064 	if (pshot == NULL) {
1065 		return (ENXIO);
1066 	}
1067 
1068 	switch (ctlop) {
1069 	case DDI_CTLOPS_REPORTDEV:
1070 		if (rdip == (dev_info_t *)0)
1071 			return (DDI_FAILURE);
1072 		cmn_err(CE_CONT, "?pshot-device: %s%d\n",
1073 		    ddi_get_name(rdip), ddi_get_instance(rdip));
1074 		return (DDI_SUCCESS);
1075 
1076 	case DDI_CTLOPS_INITCHILD:
1077 	{
1078 		dev_info_t *child = (dev_info_t *)arg;
1079 
1080 		if (pshot_debug) {
1081 			cmn_err(CE_CONT, "initchild %s%d/%s%d state 0x%x\n",
1082 			    ddi_get_name(dip), ddi_get_instance(dip),
1083 			    ddi_node_name(child), ddi_get_instance(child),
1084 			    DEVI(child)->devi_state);
1085 		}
1086 
1087 		return (pshot_initchild(dip, child));
1088 	}
1089 
1090 	case DDI_CTLOPS_UNINITCHILD:
1091 	{
1092 		dev_info_t *child = (dev_info_t *)arg;
1093 
1094 		if (pshot_debug) {
1095 			cmn_err(CE_CONT, "uninitchild %s%d/%s%d state 0x%x\n",
1096 			    ddi_get_name(dip), ddi_get_instance(dip),
1097 			    ddi_node_name(child), ddi_get_instance(child),
1098 			    DEVI(child)->devi_state);
1099 		}
1100 
1101 		return (pshot_uninitchild(dip, child));
1102 	}
1103 
1104 	case DDI_CTLOPS_DMAPMAPC:
1105 	case DDI_CTLOPS_REPORTINT:
1106 	case DDI_CTLOPS_REGSIZE:
1107 	case DDI_CTLOPS_NREGS:
1108 	case DDI_CTLOPS_SIDDEV:
1109 	case DDI_CTLOPS_SLAVEONLY:
1110 	case DDI_CTLOPS_AFFINITY:
1111 	case DDI_CTLOPS_POKE:
1112 	case DDI_CTLOPS_PEEK:
1113 		/*
1114 		 * These ops correspond to functions that "shouldn't" be called
1115 		 * by a pseudo driver.  So we whine when we're called.
1116 		 */
1117 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
1118 		    ddi_get_name(dip), ddi_get_instance(dip),
1119 		    ctlop, ddi_get_name(rdip), ddi_get_instance(rdip));
1120 		return (DDI_FAILURE);
1121 
1122 	case DDI_CTLOPS_ATTACH:
1123 	{
1124 		dev_info_t *child = (dev_info_t *)rdip;
1125 		childname = ddi_node_name(child);
1126 		childinstance = ddi_get_instance(child);
1127 		as = (struct attachspec *)arg;
1128 
1129 		no_pm_components_child = 0;
1130 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
1131 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
1132 		    "no-pm-components") == 1) {
1133 			no_pm_components_child = 1;
1134 		}
1135 		if (pshot_debug) {
1136 			cmn_err(CE_CONT, "%s%d: ctl_attach %s%d [%d]\n",
1137 			    name, instance, childname, childinstance,
1138 			    no_pm_components_child);
1139 		}
1140 
1141 		ndi_devi_enter(dip);
1142 
1143 		switch (as->when) {
1144 		case DDI_PRE:
1145 			/*
1146 			 * Mark nexus busy before a child attaches.
1147 			 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm
1148 			 * - pshot@XXX,nopm_strict)
1149 			 */
1150 			if (!(pshot->state & PM_SUPPORTED))
1151 				break;
1152 			mutex_enter(&pshot->lock);
1153 			++(pshot->busy);
1154 			if (pshot_debug_busy) {
1155 				cmn_err(CE_CONT, "%s%d:"
1156 				    " ctl_attach_pre: busy for %s%d:"
1157 				    " busy = %d\n", name, instance,
1158 				    childname, childinstance,
1159 				    pshot->busy);
1160 			}
1161 			mutex_exit(&pshot->lock);
1162 			rval = pm_busy_component(dip, 0);
1163 			ASSERT(rval == DDI_SUCCESS);
1164 			break;
1165 		case DDI_POST:
1166 			/*
1167 			 * Mark nexus idle after a child attaches.
1168 			 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm).
1169 			 * - also skip if this is not a stict parent and
1170 			 * - the child is a tape device or a no-pm-components
1171 			 * - nexus node.
1172 			 */
1173 			if (!(pshot->state & PM_SUPPORTED) ||
1174 			    (strcmp(childname, "tape") == 0 &&
1175 			    !(pshot->state & STRICT_PARENT)) ||
1176 			    no_pm_components_child)
1177 				break;
1178 			mutex_enter(&pshot->lock);
1179 			ASSERT(pshot->busy > 0);
1180 			--pshot->busy;
1181 			if (pshot_debug_busy) {
1182 				cmn_err(CE_CONT, "%s%d:"
1183 				    " ctl_attach_post: idle for %s%d:"
1184 				    " busy = %d\n", name, instance,
1185 				    childname, childinstance,
1186 				    pshot->busy);
1187 			}
1188 			mutex_exit(&pshot->lock);
1189 			rval = pm_idle_component(dip, 0);
1190 			ASSERT(rval == DDI_SUCCESS);
1191 			break;
1192 		}
1193 
1194 		ndi_devi_exit(dip);
1195 
1196 		return (rval);
1197 	}
1198 	case DDI_CTLOPS_DETACH:
1199 		{
1200 		dev_info_t *child = (dev_info_t *)rdip;
1201 		childname = ddi_node_name(child);
1202 		childinstance = ddi_get_instance(child);
1203 		ds = (struct detachspec *)arg;
1204 
1205 		no_pm_components_child = 0;
1206 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
1207 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
1208 		    "no-pm-components") == 1) {
1209 			no_pm_components_child = 1;
1210 		}
1211 		if (pshot_debug) {
1212 			cmn_err(CE_CONT,
1213 			    "%s%d: ctl_detach %s%d [%d]\n",
1214 			    name, instance, childname, childinstance,
1215 			    no_pm_components_child);
1216 		}
1217 
1218 		ndi_devi_enter(dip);
1219 
1220 		switch (ds->when) {
1221 		case DDI_PRE:
1222 			/*
1223 			 * Mark nexus busy before a child detaches.
1224 			 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm
1225 			 * - pshot@XXX,nopm_strict), or if the child is a
1226 			 * - no-pm-components nexus node.
1227 			 */
1228 			if (!(pshot->state & PM_SUPPORTED) ||
1229 			    (strcmp(childname, "tape") == 0 &&
1230 			    !(pshot->state & STRICT_PARENT)) ||
1231 			    no_pm_components_child)
1232 				break;
1233 			mutex_enter(&pshot->lock);
1234 			++(pshot->busy);
1235 			if (pshot_debug_busy) {
1236 				cmn_err(CE_CONT, "%s%d:"
1237 				    " ctl_detach_pre: busy for %s%d:"
1238 				    " busy = %d\n", name, instance,
1239 				    childname, childinstance,
1240 				    pshot->busy);
1241 			}
1242 			mutex_exit(&pshot->lock);
1243 			rval = pm_busy_component(dip, 0);
1244 			ASSERT(rval == DDI_SUCCESS);
1245 
1246 			break;
1247 		case DDI_POST:
1248 			/*
1249 			 * Mark nexus idle after a child detaches.
1250 			 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1251 			 */
1252 			if (!(pshot->state & PM_SUPPORTED))
1253 				break;
1254 			mutex_enter(&pshot->lock);
1255 			ASSERT(pshot->busy > 0);
1256 			--pshot->busy;
1257 			if (pshot_debug_busy) {
1258 				cmn_err(CE_CONT, "%s%d:"
1259 				    " ctl_detach_post: idle for %s%d:"
1260 				    " busy = %d\n", name, instance,
1261 				    childname, childinstance,
1262 				    pshot->busy);
1263 			}
1264 			mutex_exit(&pshot->lock);
1265 			rval = pm_idle_component(dip, 0);
1266 			ASSERT(rval == DDI_SUCCESS);
1267 
1268 			/*
1269 			 * Mark the driver idle if the NO_INVOL_FLAG
1270 			 * is set. This is needed to make sure the
1271 			 * parent is idle after the child detaches
1272 			 * without calling pm_lower_power().
1273 			 * Clear the NO_INVOL_FLAG.
1274 			 * - also mark idle if a tape device has detached
1275 			 */
1276 			if (!(pshot->state & NO_INVOL_FLAG))
1277 				break;
1278 			mutex_enter(&pshot->lock);
1279 			ASSERT(pshot->busy > 0);
1280 			--pshot->busy;
1281 			if (pshot_debug_busy) {
1282 				cmn_err(CE_CONT, "%s%d:"
1283 				    " ctl_detach_post: NO_INVOL:"
1284 				    " idle for %s%d: busy = %d\n",
1285 				    name, instance, childname,
1286 				    childinstance, pshot->busy);
1287 			}
1288 			pshot->state &= ~NO_INVOL_FLAG;
1289 			mutex_exit(&pshot->lock);
1290 			rval = pm_idle_component(dip, 0);
1291 			ASSERT(rval == DDI_SUCCESS);
1292 
1293 			break;
1294 		}
1295 
1296 		ndi_devi_exit(dip);
1297 
1298 		return (rval);
1299 	}
1300 
1301 	case DDI_CTLOPS_BTOP:
1302 	case DDI_CTLOPS_BTOPR:
1303 	case DDI_CTLOPS_DVMAPAGESIZE:
1304 	case DDI_CTLOPS_IOMIN:
1305 	case DDI_CTLOPS_PTOB:
1306 	default:
1307 		/*
1308 		 * The ops that we pass up (default).  We pass up memory
1309 		 * allocation oriented ops that we receive - these may be
1310 		 * associated with pseudo HBA drivers below us with target
1311 		 * drivers below them that use ddi memory allocation
1312 		 * interfaces like scsi_alloc_consistent_buf.
1313 		 */
1314 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
1315 	}
1316 }
1317 
1318 /*ARGSUSED0*/
1319 static int
1320 pshot_power(dev_info_t *dip, int cmpt, int level)
1321 {
1322 	pshot_t *pshot;
1323 	int instance = ddi_get_instance(dip);
1324 	char *name = ddi_node_name(dip);
1325 	int rv;
1326 
1327 	pshot = ddi_get_soft_state(pshot_softstatep, instance);
1328 	if (pshot == NULL) {
1329 
1330 		return (DDI_FAILURE);
1331 	}
1332 
1333 	ndi_devi_enter(dip);
1334 
1335 	/*
1336 	 * set POWER_FLAG when power() is called.
1337 	 * ioctl(DEVCT_PM_POWER) is a clear on read call.
1338 	 */
1339 	mutex_enter(&pshot->lock);
1340 	pshot->state |= POWER_FLAG;
1341 	/*
1342 	 * refuse to power OFF if the component is busy
1343 	 */
1344 	if (pshot->busy != 0 && pshot->level > level) {
1345 		cmn_err(CE_WARN, "%s%d: power: REFUSING POWER LEVEL CHANGE"
1346 		    " (%d->%d), DEVICE NOT IDLE: busy = %d",
1347 		    name, instance, pshot->level, level, pshot->busy);
1348 		rv = DDI_FAILURE;
1349 	} else {
1350 		if (pshot_debug) {
1351 			cmn_err(CE_CONT, "%s%d: power: comp %d (%d->%d)\n",
1352 			    name, instance, cmpt, pshot->level, level);
1353 		}
1354 		pshot->level = level;
1355 		rv = DDI_SUCCESS;
1356 	}
1357 	mutex_exit(&pshot->lock);
1358 
1359 	ndi_devi_exit(dip);
1360 
1361 	return (rv);
1362 }
1363 
1364 /*ARGSUSED0*/
1365 static int
1366 pshot_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1367     void *arg, void *result)
1368 
1369 {
1370 	int 				ret;
1371 	int 				instance = ddi_get_instance(dip);
1372 	char				*name = ddi_node_name(dip);
1373 	pshot_t 			*pshot;
1374 	pm_bp_child_pwrchg_t		*bpc;
1375 	pm_bp_nexus_pwrup_t		bpn;
1376 	pm_bp_has_changed_t		*bphc;
1377 	int				pwrup_res;
1378 	int				ret_failed = 0;
1379 	int				pwrup_res_failed = 0;
1380 
1381 	pshot = ddi_get_soft_state(pshot_softstatep, instance);
1382 	if (pshot == NULL) {
1383 
1384 		return (DDI_FAILURE);
1385 	}
1386 
1387 	switch (op) {
1388 	case BUS_POWER_PRE_NOTIFICATION:
1389 		bpc = (pm_bp_child_pwrchg_t *)arg;
1390 		if (pshot_debug) {
1391 			cmn_err(CE_CONT, "%s%d: pre_bus_power:"
1392 			    " %s%d comp %d (%d->%d)\n",
1393 			    name, instance, ddi_node_name(bpc->bpc_dip),
1394 			    ddi_get_instance(bpc->bpc_dip),
1395 			    bpc->bpc_comp, bpc->bpc_olevel,
1396 			    bpc->bpc_nlevel);
1397 		}
1398 
1399 		/*
1400 		 * mark parent busy if old_level is either -1 or 0,
1401 		 * and new level is == MAXPWR
1402 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1403 		 */
1404 		if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == MAXPWR &&
1405 		    bpc->bpc_olevel <= 0) && (pshot->state & PM_SUPPORTED)) {
1406 			mutex_enter(&pshot->lock);
1407 			++(pshot->busy);
1408 			if (pshot_debug_busy) {
1409 				cmn_err(CE_CONT,
1410 				    "%s%d: pre_bus_power:"
1411 				    " busy parent for %s%d (%d->%d): "
1412 				    " busy = %d\n",
1413 				    name, instance,
1414 				    ddi_node_name(bpc->bpc_dip),
1415 				    ddi_get_instance(bpc->bpc_dip),
1416 				    bpc->bpc_olevel, bpc->bpc_nlevel,
1417 				    pshot->busy);
1418 			}
1419 			mutex_exit(&pshot->lock);
1420 			ret = pm_busy_component(dip, 0);
1421 			ASSERT(ret == DDI_SUCCESS);
1422 		}
1423 
1424 		/*
1425 		 * if new_level > 0, power up parent, if not already at
1426 		 * MAXPWR, via pm_busop_bus_power
1427 		 * - skip for the no-pm nexus (pshot@XXX,nopm)
1428 		 */
1429 		if (bpc->bpc_comp == 0 && bpc->bpc_nlevel > 0 &&
1430 		    pshot->level < MAXPWR && (pshot->state & PM_SUPPORTED)) {
1431 			/*
1432 			 * stuff the bpn struct
1433 			 */
1434 			bpn.bpn_comp = 0;
1435 			bpn.bpn_level = MAXPWR;
1436 			bpn.bpn_private = bpc->bpc_private;
1437 			bpn.bpn_dip = dip;
1438 
1439 			/*
1440 			 * ask pm to power parent up
1441 			 */
1442 			if (pshot_debug) {
1443 				cmn_err(CE_CONT, "%s%d: pre_bus_power:"
1444 				    " pm_busop_bus_power on parent for %s%d"
1445 				    " (%d->%d): enter", name, instance,
1446 				    ddi_node_name(bpc->bpc_dip),
1447 				    ddi_get_instance(bpc->bpc_dip),
1448 				    bpc->bpc_olevel, bpc->bpc_nlevel);
1449 			}
1450 			ret = pm_busop_bus_power(dip, impl_arg,
1451 			    BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
1452 			    (void *)&pwrup_res);
1453 
1454 			/*
1455 			 * check the return status individually,
1456 			 * idle parent and exit if either failed.
1457 			 */
1458 			if (ret != DDI_SUCCESS) {
1459 				cmn_err(CE_WARN,
1460 				    "%s%d: pre_bus_power:"
1461 				    " pm_busop_bus_power FAILED (ret) FOR"
1462 				    " %s%d (%d->%d)",
1463 				    name, instance,
1464 				    ddi_node_name(bpc->bpc_dip),
1465 				    ddi_get_instance(bpc->bpc_dip),
1466 				    bpc->bpc_olevel, bpc->bpc_nlevel);
1467 				ret_failed = 1;
1468 			}
1469 			if (pwrup_res != DDI_SUCCESS) {
1470 				cmn_err(CE_WARN,
1471 				    "%s%d: pre_bus_power:"
1472 				    " pm_busop_bus_power FAILED (pwrup_res)"
1473 				    " FOR %s%d (%d->%d)",
1474 				    name, instance,
1475 				    ddi_node_name(bpc->bpc_dip),
1476 				    ddi_get_instance(bpc->bpc_dip),
1477 				    bpc->bpc_olevel, bpc->bpc_nlevel);
1478 				pwrup_res_failed = 1;
1479 			}
1480 			if (ret_failed || pwrup_res_failed) {
1481 				/*
1482 				 * decrement the busy count if it
1483 				 * had been incremented.
1484 				 */
1485 				if ((bpc->bpc_comp == 0 &&
1486 				    bpc->bpc_nlevel == MAXPWR &&
1487 				    bpc->bpc_olevel <= 0) &&
1488 				    (pshot->state & PM_SUPPORTED)) {
1489 					mutex_enter(&pshot->lock);
1490 					ASSERT(pshot->busy > 0);
1491 					--(pshot->busy);
1492 					if (pshot_debug_busy) {
1493 						cmn_err(CE_CONT, "%s%d:"
1494 						    " pm_busop_bus_power"
1495 						    " failed: idle parent for"
1496 						    " %s%d (%d->%d):"
1497 						    " busy = %d\n",
1498 						    name, instance,
1499 						    ddi_node_name(
1500 						    bpc->bpc_dip),
1501 						    ddi_get_instance(
1502 						    bpc->bpc_dip),
1503 						    bpc->bpc_olevel,
1504 						    bpc->bpc_nlevel,
1505 						    pshot->busy);
1506 					}
1507 					mutex_exit(&pshot->lock);
1508 					ret = pm_idle_component(dip, 0);
1509 					ASSERT(ret == DDI_SUCCESS);
1510 				}
1511 				return (DDI_FAILURE);
1512 
1513 			} else {
1514 				if (pshot_debug) {
1515 					cmn_err(CE_CONT,
1516 					    "%s%d: pre_bus_power:"
1517 					    " pm_busop_bus_power on parent"
1518 					    " for %s%d (%d->%d)\n",
1519 					    name, instance,
1520 					    ddi_node_name(bpc->bpc_dip),
1521 					    ddi_get_instance(bpc->bpc_dip),
1522 					    bpc->bpc_olevel, bpc->bpc_nlevel);
1523 				}
1524 			}
1525 		}
1526 		break;
1527 
1528 	case BUS_POWER_POST_NOTIFICATION:
1529 		bpc = (pm_bp_child_pwrchg_t *)arg;
1530 		if (pshot_debug) {
1531 			cmn_err(CE_CONT, "%s%d: post_bus_power:"
1532 			    " %s%d comp %d (%d->%d) result %d\n",
1533 			    name, instance, ddi_node_name(bpc->bpc_dip),
1534 			    ddi_get_instance(bpc->bpc_dip),
1535 			    bpc->bpc_comp, bpc->bpc_olevel,
1536 			    bpc->bpc_nlevel, *(int *)result);
1537 		}
1538 
1539 		/*
1540 		 * handle pm_busop_bus_power() failure case.
1541 		 * mark parent idle if had been marked busy.
1542 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1543 		 */
1544 		if (*(int *)result != DDI_SUCCESS) {
1545 			cmn_err(CE_WARN,
1546 			    "pshot%d: post_bus_power_failed:"
1547 			    " pm_busop_bus_power FAILED FOR %s%d (%d->%d)",
1548 			    instance, ddi_node_name(bpc->bpc_dip),
1549 			    ddi_get_instance(bpc->bpc_dip),
1550 			    bpc->bpc_olevel, bpc->bpc_nlevel);
1551 
1552 			if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == MAXPWR &&
1553 			    bpc->bpc_olevel <= 0) &&
1554 			    (pshot->state & PM_SUPPORTED)) {
1555 				mutex_enter(&pshot->lock);
1556 				ASSERT(pshot->busy > 0);
1557 				--(pshot->busy);
1558 				if (pshot_debug_busy) {
1559 					cmn_err(CE_CONT, "%s%d:"
1560 					    " post_bus_power_failed:"
1561 					    " idle parent for %s%d"
1562 					    " (%d->%d): busy = %d\n",
1563 					    name, instance,
1564 					    ddi_node_name(bpc->bpc_dip),
1565 					    ddi_get_instance(bpc->bpc_dip),
1566 					    bpc->bpc_olevel, bpc->bpc_nlevel,
1567 					    pshot->busy);
1568 				}
1569 				mutex_exit(&pshot->lock);
1570 				ret = pm_idle_component(dip, 0);
1571 				ASSERT(ret == DDI_SUCCESS);
1572 			}
1573 		}
1574 
1575 		/*
1576 		 * Mark nexus idle when a child's comp 0
1577 		 * is set to level 0 from level 1, 2, or 3 only.
1578 		 * And only if result arg == DDI_SUCCESS.
1579 		 * This will leave the parent busy when the child
1580 		 * does not call pm_lower_power() on detach after
1581 		 * unsetting the NO_LOWER_POWER flag.
1582 		 * If so, need to notify the parent to mark itself
1583 		 * idle anyway, else the no-involumtary-power-cycles
1584 		 * test cases will report false passes!
1585 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1586 		 */
1587 		if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == 0 &&
1588 		    !(bpc->bpc_olevel <= 0) &&
1589 		    *(int *)result == DDI_SUCCESS) &&
1590 		    (pshot->state & PM_SUPPORTED)) {
1591 			mutex_enter(&pshot->lock);
1592 			ASSERT(pshot->busy > 0);
1593 			--(pshot->busy);
1594 			if (pshot_debug_busy) {
1595 				cmn_err(CE_CONT,
1596 				    "%s%d: post_bus_power:"
1597 				    " idle parent for %s%d (%d->%d):"
1598 				    " busy = %d\n", name, instance,
1599 				    ddi_node_name(bpc->bpc_dip),
1600 				    ddi_get_instance(bpc->bpc_dip),
1601 				    bpc->bpc_olevel, bpc->bpc_nlevel,
1602 				    pshot->busy);
1603 			}
1604 			mutex_exit(&pshot->lock);
1605 			ret = pm_idle_component(dip, 0);
1606 			ASSERT(ret == DDI_SUCCESS);
1607 		}
1608 		break;
1609 
1610 	case BUS_POWER_HAS_CHANGED:
1611 		bphc = (pm_bp_has_changed_t *)arg;
1612 		if (pshot_debug) {
1613 			cmn_err(CE_CONT, "%s%d: has_changed_bus_power:"
1614 			    " %s%d comp %d (%d->%d) result %d\n",
1615 			    name, instance, ddi_node_name(bphc->bphc_dip),
1616 			    ddi_get_instance(bphc->bphc_dip),
1617 			    bphc->bphc_comp, bphc->bphc_olevel,
1618 			    bphc->bphc_nlevel, *(int *)result);
1619 		}
1620 
1621 		/*
1622 		 * Mark nexus idle when a child's comp 0
1623 		 * is set to level 0 from levels 1, 2, or 3 only.
1624 		 *
1625 		 * If powering up child leaf/nexus nodes via
1626 		 * pm_power_has_changed() calls, first issue
1627 		 * DEVCTL_PM_BUSY_COMP ioctl to mark parent busy
1628 		 * before powering the parent up, then power up the
1629 		 * child node.
1630 		 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1631 		 */
1632 		if ((bphc->bphc_comp == 0 && bphc->bphc_nlevel == 0 &&
1633 		    !(bphc->bphc_olevel <= 0)) &&
1634 		    pshot->state & PM_SUPPORTED) {
1635 			mutex_enter(&pshot->lock);
1636 			ASSERT(pshot->busy > 0);
1637 			--(pshot->busy);
1638 			if (pshot_debug_busy) {
1639 				cmn_err(CE_CONT,
1640 				    "%s%d: has_changed_bus_power:"
1641 				    " idle parent for %s%d (%d->%d):"
1642 				    " busy = %d\n", name, instance,
1643 				    ddi_node_name(bphc->bphc_dip),
1644 				    ddi_get_instance(bphc->bphc_dip),
1645 				    bphc->bphc_olevel,
1646 				    bphc->bphc_nlevel, pshot->busy);
1647 			}
1648 			mutex_exit(&pshot->lock);
1649 			ret = pm_idle_component(dip, 0);
1650 			ASSERT(ret == DDI_SUCCESS);
1651 		}
1652 		break;
1653 
1654 	default:
1655 		return (pm_busop_bus_power(dip, impl_arg, op, arg, result));
1656 
1657 	}
1658 
1659 	return (DDI_SUCCESS);
1660 }
1661 
1662 static int
1663 pshot_initchild(dev_info_t *dip, dev_info_t *child)
1664 {
1665 	char	name[64];
1666 	char	*bus_addr;
1667 	char	*c_nodename;
1668 	int	bus_id;
1669 	dev_info_t *enum_child;
1670 	int	enum_base;
1671 	int	enum_extent;
1672 
1673 
1674 	/* check for bus_enum node */
1675 
1676 #ifdef	NOT_USED
1677 	if (impl_ddi_merge_child(child) != DDI_SUCCESS)
1678 		return (DDI_FAILURE);
1679 #endif
1680 
1681 	enum_base = ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1682 	    "busid_ebase", 0);
1683 
1684 	enum_extent = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1685 	    DDI_PROP_DONTPASS, "busid_range", 0);
1686 
1687 	/*
1688 	 * bus enumeration node
1689 	 */
1690 	if ((enum_base != 0) && (enum_extent != 0))	{
1691 		c_nodename = ddi_node_name(child);
1692 		bus_id = enum_base;
1693 		for (; bus_id < enum_extent; bus_id++) {
1694 			if (ndi_devi_alloc(dip, c_nodename, DEVI_PSEUDO_NODEID,
1695 			    &enum_child) != NDI_SUCCESS)
1696 				return (DDI_FAILURE);
1697 
1698 			(void) sprintf(name, "%d", bus_id);
1699 			if (ndi_prop_update_string(DDI_DEV_T_NONE, enum_child,
1700 			    "bus-addr", name) != DDI_PROP_SUCCESS) {
1701 				(void) ndi_devi_free(enum_child);
1702 				return (DDI_FAILURE);
1703 			}
1704 
1705 			if (ndi_devi_online(enum_child, 0) !=
1706 			    DDI_SUCCESS) {
1707 				(void) ndi_devi_free(enum_child);
1708 				return (DDI_FAILURE);
1709 			}
1710 		}
1711 		/*
1712 		 * fail the enumeration node itself
1713 		 */
1714 		return (DDI_FAILURE);
1715 	}
1716 
1717 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, 0, "bus-addr",
1718 	    &bus_addr) != DDI_PROP_SUCCESS) {
1719 		cmn_err(CE_WARN, "pshot_initchild: bus-addr not defined (%s)",
1720 		    ddi_node_name(child));
1721 		return (DDI_NOT_WELL_FORMED);
1722 	}
1723 
1724 	if (strlen(bus_addr) == 0) {
1725 		cmn_err(CE_WARN, "pshot_initchild: NULL bus-addr (%s)",
1726 		    ddi_node_name(child));
1727 		ddi_prop_free(bus_addr);
1728 		return (DDI_FAILURE);
1729 	}
1730 
1731 	if (strncmp(bus_addr, "failinit", 8) == 0) {
1732 		if (pshot_debug)
1733 			cmn_err(CE_CONT,
1734 			    "pshot%d: %s forced INITCHILD failure\n",
1735 			    ddi_get_instance(dip), bus_addr);
1736 		ddi_prop_free(bus_addr);
1737 		return (DDI_FAILURE);
1738 	}
1739 
1740 	if (pshot_log) {
1741 		cmn_err(CE_CONT, "initchild %s%d/%s@%s\n",
1742 		    ddi_get_name(dip), ddi_get_instance(dip),
1743 		    ddi_node_name(child), bus_addr);
1744 	}
1745 
1746 	ddi_set_name_addr(child, bus_addr);
1747 	ddi_prop_free(bus_addr);
1748 	return (DDI_SUCCESS);
1749 }
1750 
1751 /*ARGSUSED*/
1752 static int
1753 pshot_uninitchild(dev_info_t *dip, dev_info_t *child)
1754 {
1755 	ddi_set_name_addr(child, NULL);
1756 	return (DDI_SUCCESS);
1757 }
1758 
1759 
1760 /*
1761  * devctl IOCTL support
1762  */
1763 /* ARGSUSED */
1764 static int
1765 pshot_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1766 {
1767 	int instance;
1768 	pshot_t *pshot;
1769 
1770 	if (otyp != OTYP_CHR)
1771 		return (EINVAL);
1772 
1773 	instance = pshot_minor_decode_inst(getminor(*devp));
1774 	if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1775 		return (ENXIO);
1776 
1777 	/*
1778 	 * Access is currently determined on a per-instance basis.
1779 	 * If we want per-node, then need to add state and lock members to
1780 	 * pshot_minor_t
1781 	 */
1782 	mutex_enter(&pshot->lock);
1783 	if (((flags & FEXCL) && (pshot->state & IS_OPEN)) ||
1784 	    (!(flags & FEXCL) && (pshot->state & IS_OPEN_EXCL))) {
1785 		mutex_exit(&pshot->lock);
1786 		return (EBUSY);
1787 	}
1788 	pshot->state |= IS_OPEN;
1789 	if (flags & FEXCL)
1790 		pshot->state |= IS_OPEN_EXCL;
1791 
1792 	if (pshot_debug)
1793 		cmn_err(CE_CONT, "pshot%d open\n", instance);
1794 
1795 	mutex_exit(&pshot->lock);
1796 	return (0);
1797 }
1798 
1799 /*
1800  * pshot_close
1801  */
1802 /* ARGSUSED */
1803 static int
1804 pshot_close(dev_t dev, int flag, int otyp, cred_t *credp)
1805 {
1806 	int instance;
1807 	pshot_t *pshot;
1808 
1809 	if (otyp != OTYP_CHR)
1810 		return (EINVAL);
1811 
1812 	instance = pshot_minor_decode_inst(getminor(dev));
1813 	if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1814 		return (ENXIO);
1815 
1816 	mutex_enter(&pshot->lock);
1817 	pshot->state &= ~(IS_OPEN | IS_OPEN_EXCL);
1818 	mutex_exit(&pshot->lock);
1819 	if (pshot_debug)
1820 		cmn_err(CE_CONT, "pshot%d closed\n", instance);
1821 	return (0);
1822 }
1823 
1824 
1825 /*
1826  * pshot_ioctl: redirects to appropriate command handler based on various
1827  * 	criteria
1828  */
1829 /* ARGSUSED */
1830 static int
1831 pshot_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1832     int *rvalp)
1833 {
1834 	pshot_t *pshot;
1835 	int instance;
1836 	minor_t nodenum;
1837 	char *nodename;
1838 
1839 	instance = pshot_minor_decode_inst(getminor(dev));
1840 	if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1841 		return (ENXIO);
1842 
1843 	nodenum = pshot_minor_decode_nodenum(getminor(dev));
1844 	nodename = pshot->nodes[nodenum].name;
1845 
1846 	if (pshot_debug)
1847 		cmn_err(CE_CONT,
1848 		    "pshot%d ioctl: dev=%p, cmd=%x, arg=%p, mode=%x\n",
1849 		    instance, (void *)dev, cmd, (void *)arg, mode);
1850 
1851 	if (strcmp(nodename, PSHOT_NODENAME_DEVCTL) == 0)
1852 		return (pshot_devctl(pshot, nodenum, cmd, arg, mode, credp,
1853 		    rvalp));
1854 
1855 	if (strcmp(nodename, PSHOT_NODENAME_TESTCTL) == 0)
1856 		return (pshot_testctl(pshot, nodenum, cmd, arg, mode, credp,
1857 		    rvalp));
1858 
1859 	cmn_err(CE_WARN, "pshot_ioctl: unmatched nodename on minor %u",
1860 	    pshot->nodes[nodenum].minor);
1861 	return (ENXIO);
1862 }
1863 
1864 
1865 /*
1866  * pshot_devctl: handle DEVCTL operations
1867  */
1868 /* ARGSUSED */
1869 static int
1870 pshot_devctl(pshot_t *pshot, minor_t nodenum, int cmd, intptr_t arg, int mode,
1871     cred_t *credp, int *rvalp)
1872 {
1873 	dev_info_t *self;
1874 	dev_info_t *child = NULL;
1875 	struct devctl_iocdata *dcp;
1876 	uint_t state;
1877 	int rv = 0;
1878 	uint_t flags;
1879 	int instance;
1880 	int i;
1881 	int ret;
1882 
1883 	self = pshot->dip;
1884 
1885 	flags = (pshot_devctl_debug) ? NDI_DEVI_DEBUG : 0;
1886 	instance = pshot->instance;
1887 
1888 	/*
1889 	 * We can use the generic implementation for these ioctls
1890 	 */
1891 	for (i = 0; pshot_devctls[i].ioctl_int != 0; i++) {
1892 		if (pshot_devctls[i].ioctl_int == cmd) {
1893 			if (pshot_debug)
1894 				cmn_err(CE_CONT, "pshot%d devctl: %s",
1895 				    instance, pshot_devctls[i].ioctl_char);
1896 		}
1897 	}
1898 	switch (cmd) {
1899 	case DEVCTL_DEVICE_GETSTATE:
1900 	case DEVCTL_DEVICE_ONLINE:
1901 	case DEVCTL_DEVICE_OFFLINE:
1902 	case DEVCTL_DEVICE_REMOVE:
1903 	case DEVCTL_BUS_GETSTATE:
1904 	case DEVCTL_BUS_DEV_CREATE:
1905 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, flags);
1906 		if (pshot_debug && rv != 0) {
1907 			cmn_err(CE_CONT, "pshot%d ndi_devctl_ioctl:"
1908 			    " failed, rv = %d", instance, rv);
1909 		}
1910 
1911 		return (rv);
1912 	}
1913 
1914 	/*
1915 	 * read devctl ioctl data
1916 	 */
1917 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1918 		return (EFAULT);
1919 
1920 	switch (cmd) {
1921 
1922 	case DEVCTL_DEVICE_RESET:
1923 		if (pshot_debug)
1924 			cmn_err(CE_CONT, "pshot%d devctl:"
1925 			    " DEVCTL_DEVICE_RESET\n", instance);
1926 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_DEV_RESET,
1927 		    child, (void *)self);
1928 		ASSERT(rv == NDI_SUCCESS);
1929 		break;
1930 
1931 	case DEVCTL_BUS_QUIESCE:
1932 		if (pshot_debug)
1933 			cmn_err(CE_CONT, "pshot%d devctl:"
1934 			    " DEVCTL_BUS_QUIESCE\n", instance);
1935 		if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
1936 			if (state == BUS_QUIESCED) {
1937 				break;
1938 			}
1939 			(void) ndi_set_bus_state(self, BUS_QUIESCED);
1940 		}
1941 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_QUIESCE,
1942 		    child, (void *)self);
1943 		ASSERT(rv == NDI_SUCCESS);
1944 
1945 		break;
1946 
1947 	case DEVCTL_BUS_UNQUIESCE:
1948 		if (pshot_debug)
1949 			cmn_err(CE_CONT, "pshot%d devctl:"
1950 			    " DEVCTL_BUS_UNQUIESCE\n", instance);
1951 		if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
1952 			if (state == BUS_ACTIVE) {
1953 				break;
1954 			}
1955 		}
1956 
1957 		/*
1958 		 * quiesce the bus through bus-specific means
1959 		 */
1960 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
1961 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_UNQUIESCE,
1962 		    child, (void *)self);
1963 		ASSERT(rv == NDI_SUCCESS);
1964 		break;
1965 
1966 	case DEVCTL_BUS_RESET:
1967 	case DEVCTL_BUS_RESETALL:
1968 		/*
1969 		 * no reset support for the pseudo bus
1970 		 * but if there were....
1971 		 */
1972 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_RESET,
1973 		    child, (void *)self);
1974 		ASSERT(rv == NDI_SUCCESS);
1975 		break;
1976 
1977 	/*
1978 	 * PM related ioctls
1979 	 */
1980 	case DEVCTL_PM_BUSY_COMP:
1981 		/*
1982 		 * mark component 0 busy.
1983 		 * Keep track of ioctl updates to the busy count
1984 		 * via pshot->busy_ioctl.
1985 		 */
1986 		if (pshot_debug) {
1987 			cmn_err(CE_CONT, "pshot%d devctl:"
1988 			    " DEVCTL_PM_BUSY_COMP\n", instance);
1989 		}
1990 		mutex_enter(&pshot->lock);
1991 		++(pshot->busy);
1992 		++(pshot->busy_ioctl);
1993 		if (pshot_debug_busy) {
1994 			cmn_err(CE_CONT, "pshot%d:"
1995 			    " DEVCTL_PM_BUSY_COMP comp 0 busy"
1996 			    " %d busy_ioctl %d\n", instance, pshot->busy,
1997 			    pshot->busy_ioctl);
1998 		}
1999 		mutex_exit(&pshot->lock);
2000 		ret = pm_busy_component(pshot->dip, 0);
2001 		ASSERT(ret == DDI_SUCCESS);
2002 
2003 		break;
2004 
2005 	case DEVCTL_PM_BUSY_COMP_TEST:
2006 		/*
2007 		 * test bus's busy state
2008 		 */
2009 		if (pshot_debug) {
2010 			cmn_err(CE_CONT, "pshot%d devctl:"
2011 			    " DEVCTL_PM_BUSY_COMP_TEST\n", instance);
2012 		}
2013 		mutex_enter(&pshot->lock);
2014 		state = pshot->busy;
2015 		if (copyout(&state, dcp->cpyout_buf,
2016 		    sizeof (uint_t)) != 0) {
2017 			cmn_err(CE_WARN, "pshot%d devctl:"
2018 			    " DEVCTL_PM_BUSY_COMP_TEST: copyout failed",
2019 			    instance);
2020 			rv = EINVAL;
2021 		}
2022 		if (pshot_debug_busy) {
2023 			cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_BUSY_COMP_TEST:"
2024 			    " comp 0 busy %d busy_ioctl %d\n", instance,
2025 			    state, pshot->busy_ioctl);
2026 		}
2027 		mutex_exit(&pshot->lock);
2028 		break;
2029 
2030 	case DEVCTL_PM_IDLE_COMP:
2031 		/*
2032 		 * mark component 0 idle.
2033 		 * NOP if pshot->busy_ioctl <= 0.
2034 		 */
2035 		if (pshot_debug) {
2036 			cmn_err(CE_CONT, "pshot%d devctl:"
2037 			    " DEVCTL_PM_IDLE_COMP\n", instance);
2038 		}
2039 		mutex_enter(&pshot->lock);
2040 		if (pshot->busy_ioctl > 0) {
2041 			ASSERT(pshot->busy > 0);
2042 			--(pshot->busy);
2043 			--(pshot->busy_ioctl);
2044 			if (pshot_debug_busy) {
2045 				cmn_err(CE_CONT, "pshot%d:"
2046 				    " DEVCTL_PM_IDLE_COM: comp 0"
2047 				    " busy %d busy_ioctl %d\n", instance,
2048 				    pshot->busy, pshot->busy_ioctl);
2049 			}
2050 			mutex_exit(&pshot->lock);
2051 			ret = pm_idle_component(pshot->dip, 0);
2052 			ASSERT(ret == DDI_SUCCESS);
2053 
2054 		} else {
2055 			mutex_exit(&pshot->lock);
2056 		}
2057 		break;
2058 
2059 	case DEVCTL_PM_RAISE_PWR:
2060 		/*
2061 		 * raise component 0 to full power level MAXPWR via a
2062 		 * pm_raise_power() call
2063 		 */
2064 		if (pshot_debug) {
2065 			cmn_err(CE_CONT, "pshot%d devctl:"
2066 			    " DEVCTL_PM_RAISE_PWR\n", instance);
2067 		}
2068 		if (pm_raise_power(pshot->dip, 0, MAXPWR) != DDI_SUCCESS) {
2069 			rv = EINVAL;
2070 		} else {
2071 			mutex_enter(&pshot->lock);
2072 			if (pshot_debug) {
2073 				cmn_err(CE_CONT, "pshot%d:"
2074 				    " DEVCTL_PM_RAISE_POWER: comp 0"
2075 				    " to level %d\n", instance, pshot->level);
2076 			}
2077 			mutex_exit(&pshot->lock);
2078 		}
2079 		break;
2080 
2081 	case DEVCTL_PM_LOWER_PWR:
2082 		/*
2083 		 * pm_lower_power() call for negative testing
2084 		 * expected to fail.
2085 		 */
2086 		if (pshot_debug) {
2087 			cmn_err(CE_CONT, "pshot%d devctl:"
2088 			    " DEVCTL_PM_LOWER_PWR\n", instance);
2089 		}
2090 		if (pm_lower_power(pshot->dip, 0, 0) != DDI_SUCCESS) {
2091 			rv = EINVAL;
2092 		} else {
2093 			mutex_enter(&pshot->lock);
2094 			if (pshot_debug) {
2095 				cmn_err(CE_CONT, "pshot%d:"
2096 				    " DEVCTL_PM_LOWER_POWER comp 0"
2097 				    " to level %d\n", instance, pshot->level);
2098 			}
2099 			mutex_exit(&pshot->lock);
2100 		}
2101 		break;
2102 
2103 	case DEVCTL_PM_CHANGE_PWR_LOW:
2104 		/*
2105 		 * inform the PM framework that component 0 has changed
2106 		 * power level to 0 via a pm_power_has_changed() call
2107 		 */
2108 		if (pshot_debug) {
2109 			cmn_err(CE_CONT, "pshot%d devctl:"
2110 			    " DEVCTL_PM_CHANGE_PWR_LOW\n", instance);
2111 		}
2112 		mutex_enter(&pshot->lock);
2113 		pshot->level = 0;
2114 		if (pm_power_has_changed(pshot->dip, 0, 0) != DDI_SUCCESS) {
2115 			rv = EINVAL;
2116 		} else {
2117 			if (pshot_debug) {
2118 				cmn_err(CE_CONT, "pshot%d:"
2119 				    " DEVCTL_PM_CHANGE_PWR_LOW comp 0 to"
2120 				    " level %d\n", instance, pshot->level);
2121 			}
2122 		}
2123 		mutex_exit(&pshot->lock);
2124 		break;
2125 
2126 	case DEVCTL_PM_CHANGE_PWR_HIGH:
2127 		/*
2128 		 * inform the PM framework that component 0 has changed
2129 		 * power level to MAXPWR via a pm_power_has_changed() call
2130 		 */
2131 		if (pshot_debug) {
2132 			cmn_err(CE_CONT, "pshot%d devctl:"
2133 			    " DEVCTL_PM_CHANGE_PWR_HIGH\n", instance);
2134 		}
2135 		mutex_enter(&pshot->lock);
2136 		pshot->level = MAXPWR;
2137 		if (pm_power_has_changed(pshot->dip, 0, MAXPWR)
2138 		    != DDI_SUCCESS) {
2139 			rv = EINVAL;
2140 		} else {
2141 			if (pshot_debug) {
2142 				cmn_err(CE_CONT, "pshot%d:"
2143 				    " DEVCTL_PM_CHANGE_PWR_HIGH comp 0 to"
2144 				    " level %d\n", instance, pshot->level);
2145 			}
2146 		}
2147 		mutex_exit(&pshot->lock);
2148 		break;
2149 
2150 	case DEVCTL_PM_POWER:
2151 		/*
2152 		 * test if the pshot_power() routine has been called,
2153 		 * then clear
2154 		 */
2155 		if (pshot_debug) {
2156 			cmn_err(CE_CONT, "pshot%d devctl:"
2157 			    " DEVCTL_PM_POWER\n", instance);
2158 		}
2159 		mutex_enter(&pshot->lock);
2160 		state = (pshot->state & POWER_FLAG) ? 1 : 0;
2161 		if (copyout(&state, dcp->cpyout_buf,
2162 		    sizeof (uint_t)) != 0) {
2163 			cmn_err(CE_WARN, "pshot%d devctl:"
2164 			    " DEVCTL_PM_POWER: copyout failed",
2165 			    instance);
2166 			rv = EINVAL;
2167 		}
2168 		if (pshot_debug) {
2169 			cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_POWER:"
2170 			    " POWER_FLAG = %d\n", instance, state);
2171 		}
2172 		pshot->state &= ~POWER_FLAG;
2173 		mutex_exit(&pshot->lock);
2174 		break;
2175 
2176 	case DEVCTL_PM_FAIL_SUSPEND:
2177 		/*
2178 		 * fail DDI_SUSPEND
2179 		 */
2180 		if (pshot_debug) {
2181 			cmn_err(CE_CONT, "pshot%d devctl:"
2182 			    " DEVCTL_PM_FAIL_SUSPEND\n", instance);
2183 		}
2184 		mutex_enter(&pshot->lock);
2185 		pshot->state |= FAIL_SUSPEND_FLAG;
2186 		mutex_exit(&pshot->lock);
2187 		if (pshot_debug) {
2188 			cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_FAIL_SUSPEND\n",
2189 			    instance);
2190 		}
2191 		break;
2192 
2193 	case DEVCTL_PM_BUS_STRICT_TEST:
2194 		/*
2195 		 * test the STRICT_PARENT flag:
2196 		 *	set => STRICT PARENT
2197 		 *	not set => INVOLVED PARENT
2198 		 */
2199 		mutex_enter(&pshot->lock);
2200 		state = (pshot->state & STRICT_PARENT) ? 1 : 0;
2201 		if (copyout(&state, dcp->cpyout_buf,
2202 		    sizeof (uint_t)) != 0) {
2203 			cmn_err(CE_WARN, "pshot%d devctl:"
2204 			    " DEVCTL_PM_BUS_STRICT_TEST: copyout failed",
2205 			    instance);
2206 			rv = EINVAL;
2207 		}
2208 		if (pshot_debug) {
2209 			cmn_err(CE_CONT, "pshot%d devctl:"
2210 			    " DEVCTL_PM_BUS_STRICT_TEST: type = %s\n",
2211 			    instance, ((state == 0) ? "INVOLVED" : "STRICT"));
2212 		}
2213 		mutex_exit(&pshot->lock);
2214 		break;
2215 
2216 	case DEVCTL_PM_BUS_NO_INVOL:
2217 		/*
2218 		 * Set the NO_INVOL_FLAG flag to
2219 		 * notify the driver that the child will not
2220 		 * call pm_lower_power() on detach.
2221 		 * The driver needs to mark itself idle twice
2222 		 * during DDI_CTLOPS_DETACH (post).
2223 		 */
2224 		if (pshot_debug) {
2225 			cmn_err(CE_CONT, "pshot%d devctl:"
2226 			    " DEVCTL_PM_BUS_NO_INVOL\n", instance);
2227 		}
2228 		mutex_enter(&pshot->lock);
2229 		pshot->state |= NO_INVOL_FLAG;
2230 		mutex_exit(&pshot->lock);
2231 		break;
2232 
2233 	default:
2234 		rv = ENOTTY;
2235 	}
2236 
2237 	ndi_dc_freehdl(dcp);
2238 	return (rv);
2239 }
2240 
2241 
2242 /*
2243  * pshot_testctl: handle other test operations
2244  *	- If <cmd> is a DEVCTL cmd, then <arg> is a dev_t indicating which
2245  *	  child to direct the DEVCTL to, if applicable;
2246  *	  furthermore, any cmd here can be sent by layered ioctls (unlike
2247  *	  those to pshot_devctl() which must come from userland)
2248  */
2249 /* ARGSUSED */
2250 static int
2251 pshot_testctl(pshot_t *pshot, minor_t nodenum, int cmd, intptr_t arg, int mode,
2252     cred_t *credp, int *rvalp)
2253 {
2254 	dev_info_t *self;
2255 	dev_info_t *child = NULL;
2256 	uint_t state;
2257 	int rv = 0;
2258 	int instance;
2259 	int i;
2260 
2261 	/* uint_t flags; */
2262 
2263 	/* flags = (pshot_devctl_debug) ? NDI_DEVI_DEBUG : 0; */
2264 	self = pshot->dip;
2265 	instance = pshot->instance;
2266 
2267 	if (cmd & DEVCTL_IOC) {
2268 		child = e_ddi_hold_devi_by_dev((dev_t)arg, 0);
2269 	}
2270 
2271 	for (i = 0; pshot_devctls[i].ioctl_int != 0; i++) {
2272 		if (pshot_devctls[i].ioctl_int == cmd) {
2273 			if (pshot_debug)
2274 				cmn_err(CE_CONT, "pshot%d devctl: %s",
2275 				    instance, pshot_devctls[i].ioctl_char);
2276 		}
2277 	}
2278 	switch (cmd) {
2279 	case DEVCTL_DEVICE_RESET:
2280 		if (pshot_debug)
2281 			cmn_err(CE_CONT, "pshot%d testctl:"
2282 			    " DEVCTL_PM_POWER\n", instance);
2283 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_DEV_RESET,
2284 		    child, (void *)self);
2285 		ASSERT(rv == NDI_SUCCESS);
2286 		break;
2287 
2288 	case DEVCTL_BUS_QUIESCE:
2289 		if (pshot_debug)
2290 			cmn_err(CE_CONT, "pshot%d testctl:"
2291 			    " DEVCTL_PM_POWER\n", instance);
2292 		if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
2293 			if (state == BUS_QUIESCED) {
2294 				break;
2295 			}
2296 			(void) ndi_set_bus_state(self, BUS_QUIESCED);
2297 		}
2298 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_QUIESCE,
2299 		    child, (void *)self);
2300 		ASSERT(rv == NDI_SUCCESS);
2301 
2302 		break;
2303 
2304 	case DEVCTL_BUS_UNQUIESCE:
2305 		if (pshot_debug)
2306 			cmn_err(CE_CONT, "pshot%d testctl:"
2307 			    " DEVCTL_PM_POWER\n", instance);
2308 		if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
2309 			if (state == BUS_ACTIVE) {
2310 				break;
2311 			}
2312 		}
2313 
2314 		/*
2315 		 * quiesce the bus through bus-specific means
2316 		 */
2317 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
2318 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_UNQUIESCE,
2319 		    child, (void *)self);
2320 		ASSERT(rv == NDI_SUCCESS);
2321 		break;
2322 
2323 	case DEVCTL_BUS_RESET:
2324 	case DEVCTL_BUS_RESETALL:
2325 		/*
2326 		 * no reset support for the pseudo bus
2327 		 * but if there were....
2328 		 */
2329 		rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_RESET,
2330 		    child, (void *)self);
2331 		ASSERT(rv == NDI_SUCCESS);
2332 		break;
2333 
2334 	default:
2335 		rv = ENOTTY;
2336 	}
2337 
2338 	if (child != NULL)
2339 		ddi_release_devi(child);
2340 	return (rv);
2341 }
2342 
2343 
2344 static int
2345 pshot_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2346 	char *eventname, ddi_eventcookie_t *event_cookiep)
2347 {
2348 	int	instance = ddi_get_instance(dip);
2349 	pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2350 
2351 	if (pshot_debug)
2352 		cmn_err(CE_CONT, "pshot%d: "
2353 		    "pshot_get_eventcookie:\n\t"
2354 		    "dip = 0x%p rdip = 0x%p (%s/%d) eventname = %s\n",
2355 		    instance, (void *)dip, (void *)rdip,
2356 		    ddi_node_name(rdip), ddi_get_instance(rdip),
2357 		    eventname);
2358 
2359 
2360 	return (ndi_event_retrieve_cookie(pshot->ndi_event_hdl,
2361 	    rdip, eventname, event_cookiep, NDI_EVENT_NOPASS));
2362 }
2363 
2364 static int
2365 pshot_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2366 	ddi_eventcookie_t cookie,
2367 	void (*callback)(), void *arg, ddi_callback_id_t *cb_id)
2368 {
2369 	int	instance = ddi_get_instance(dip);
2370 	pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2371 
2372 	if (pshot_debug)
2373 		cmn_err(CE_CONT, "pshot%d: "
2374 		    "pshot_add_eventcall:\n\t"
2375 		    "dip = 0x%p rdip = 0x%p (%s%d)\n\tcookie = 0x%p (%s)\n\t"
2376 		    "cb = 0x%p, arg = 0x%p\n",
2377 		    instance, (void *)dip, (void *)rdip,
2378 		    ddi_node_name(rdip), ddi_get_instance(rdip), (void *)cookie,
2379 		    NDI_EVENT_NAME(cookie), (void *)callback, arg);
2380 
2381 	/* add callback to our event handle */
2382 	return (ndi_event_add_callback(pshot->ndi_event_hdl, rdip,
2383 	    cookie, callback, arg, NDI_SLEEP, cb_id));
2384 }
2385 
2386 static int
2387 pshot_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2388 {
2389 
2390 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
2391 
2392 	int instance = ddi_get_instance(dip);
2393 	pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2394 
2395 	ASSERT(cb);
2396 
2397 	if (pshot_debug)
2398 		cmn_err(CE_CONT, "pshot%d: "
2399 		    "pshot_remove_eventcall:\n\t"
2400 		    "dip = 0x%p rdip = 0x%p (%s%d)\n\tcookie = 0x%p (%s)\n",
2401 		    instance, (void *)dip, (void *)cb->ndi_evtcb_dip,
2402 		    ddi_node_name(cb->ndi_evtcb_dip),
2403 		    ddi_get_instance(cb->ndi_evtcb_dip),
2404 		    (void *)cb->ndi_evtcb_cookie,
2405 		    NDI_EVENT_NAME(cb->ndi_evtcb_cookie));
2406 
2407 	return (ndi_event_remove_callback(pshot->ndi_event_hdl, cb_id));
2408 }
2409 
2410 static int
2411 pshot_post_event(dev_info_t *dip, dev_info_t *rdip,
2412 	ddi_eventcookie_t cookie, void *impl_data)
2413 {
2414 	int	instance = ddi_get_instance(dip);
2415 	pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2416 
2417 	if (pshot_debug) {
2418 		if (rdip) {
2419 			cmn_err(CE_CONT, "pshot%d: "
2420 			    "pshot_post_event:\n\t"
2421 			    "dip = 0x%p rdip = 0x%p (%s%d\n\t"
2422 			    "cookie = 0x%p (%s)\n\tbus_impl = 0x%p\n",
2423 			    instance, (void *)dip, (void *)rdip,
2424 			    ddi_node_name(rdip), ddi_get_instance(rdip),
2425 			    (void *)cookie,
2426 			    NDI_EVENT_NAME(cookie), impl_data);
2427 		} else {
2428 			cmn_err(CE_CONT, "pshot%d: "
2429 			    "pshot_post_event:\n\t"
2430 			    "dip = 0x%p cookie = 0x%p (%s) bus_impl = 0x%p\n",
2431 			    instance, (void *)dip, (void *)cookie,
2432 			    NDI_EVENT_NAME(cookie), impl_data);
2433 		}
2434 	}
2435 
2436 	/*  run callbacks for this event */
2437 	return (ndi_event_run_callbacks(pshot->ndi_event_hdl, rdip,
2438 	    cookie, impl_data));
2439 }
2440 
2441 /*
2442  * the nexus driver will generate events
2443  * that need to go to children
2444  */
2445 static int
2446 pshot_event(pshot_t *pshot, int event_tag, dev_info_t *child,
2447 	void *bus_impldata)
2448 {
2449 	ddi_eventcookie_t cookie = ndi_event_tag_to_cookie(
2450 	    pshot->ndi_event_hdl, event_tag);
2451 
2452 	if (pshot_debug) {
2453 		if (child) {
2454 			cmn_err(CE_CONT, "pshot%d: "
2455 			    "pshot_event: event_tag = 0x%x (%s)\n\t"
2456 			    "child = 0x%p (%s%d) bus_impl = 0x%p (%s%d)\n",
2457 			    pshot->instance, event_tag,
2458 			    ndi_event_tag_to_name(pshot->ndi_event_hdl,
2459 			    event_tag),
2460 			    (void *)child, ddi_node_name(child),
2461 			    ddi_get_instance(child), bus_impldata,
2462 			    ddi_node_name((dev_info_t *)bus_impldata),
2463 			    ddi_get_instance((dev_info_t *)bus_impldata));
2464 		} else {
2465 			cmn_err(CE_CONT, "pshot%d: "
2466 			    "pshot_event: event_tag = 0x%x (%s)\n\t"
2467 			    "child = NULL,  bus_impl = 0x%p (%s%d)\n",
2468 			    pshot->instance, event_tag,
2469 			    ndi_event_tag_to_name(pshot->ndi_event_hdl,
2470 			    event_tag),
2471 			    bus_impldata,
2472 			    ddi_node_name((dev_info_t *)bus_impldata),
2473 			    ddi_get_instance((dev_info_t *)bus_impldata));
2474 		}
2475 	}
2476 
2477 	return (ndi_event_run_callbacks(pshot->ndi_event_hdl,
2478 	    child, cookie, bus_impldata));
2479 }
2480 
2481 
2482 /*
2483  * the pshot driver event notification callback
2484  */
2485 static void
2486 pshot_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
2487 	void *arg, void *bus_impldata)
2488 {
2489 	pshot_t *pshot = (pshot_t *)arg;
2490 	int event_tag;
2491 
2492 	/* look up the event */
2493 	event_tag = NDI_EVENT_TAG(cookie);
2494 
2495 	if (pshot_debug) {
2496 		cmn_err(CE_CONT, "pshot%d: "
2497 		    "pshot_event_cb:\n\t"
2498 		    "dip = 0x%p cookie = 0x%p (%s), tag = 0x%x\n\t"
2499 		    "arg = 0x%p bus_impl = 0x%p (%s%d)\n",
2500 		    pshot->instance, (void *)dip, (void *)cookie,
2501 		    NDI_EVENT_NAME(cookie), event_tag, arg, bus_impldata,
2502 		    ddi_node_name((dev_info_t *)bus_impldata),
2503 		    ddi_get_instance((dev_info_t *)bus_impldata));
2504 	}
2505 
2506 	switch (event_tag) {
2507 	case PSHOT_EVENT_TAG_OFFLINE:
2508 	case PSHOT_EVENT_TAG_BUS_RESET:
2509 	case PSHOT_EVENT_TAG_BUS_QUIESCE:
2510 	case PSHOT_EVENT_TAG_BUS_UNQUIESCE:
2511 		/* notify all subscribers of the this event */
2512 		(void) ndi_event_run_callbacks(pshot->ndi_event_hdl,
2513 		    NULL, cookie, bus_impldata);
2514 		if (pshot_debug) {
2515 			cmn_err(CE_CONT, "pshot%d: event=%s\n\t"
2516 			    "pshot_event_cb\n", pshot->instance,
2517 			    NDI_EVENT_NAME(cookie));
2518 		}
2519 		/*FALLTHRU*/
2520 	case PSHOT_EVENT_TAG_TEST_POST:
2521 	case PSHOT_EVENT_TAG_DEV_RESET:
2522 	default:
2523 		return;
2524 	}
2525 }
2526 
2527 static int
2528 pshot_bus_config(dev_info_t *parent, uint_t flags,
2529     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
2530 {
2531 	int		rval;
2532 	char		*devname;
2533 	char		*devstr, *cname, *caddr;
2534 	int		devstrlen;
2535 	pshot_t		*pshot;
2536 	int		instance = ddi_get_instance(parent);
2537 
2538 	if (pshot_debug) {
2539 		flags |= NDI_DEVI_DEBUG;
2540 		cmn_err(CE_CONT,
2541 		    "pshot%d: bus_config %s flags=0x%x\n",
2542 		    ddi_get_instance(parent),
2543 		    (op == BUS_CONFIG_ONE) ? (char *)arg : "", flags);
2544 	}
2545 
2546 	pshot = ddi_get_soft_state(pshot_softstatep, instance);
2547 	if (pshot == NULL) {
2548 
2549 		return (NDI_FAILURE);
2550 	}
2551 
2552 	/*
2553 	 * Hold the nexus across the bus_config
2554 	 */
2555 	ndi_devi_enter(parent);
2556 
2557 	switch (op) {
2558 	case BUS_CONFIG_ONE:
2559 
2560 		/*
2561 		 * lookup and hold child device, create if not found
2562 		 */
2563 		devname = (char *)arg;
2564 		devstrlen = strlen(devname) + 1;
2565 		devstr = i_ddi_strdup(devname, KM_SLEEP);
2566 		i_ddi_parse_name(devstr, &cname, &caddr, NULL);
2567 
2568 		/*
2569 		 * The framework ensures that the node has
2570 		 * a name but each nexus is responsible for
2571 		 * the bus address name space.  This driver
2572 		 * requires that a bus address be specified,
2573 		 * as will most nexus drivers.
2574 		 */
2575 		ASSERT(cname && strlen(cname) > 0);
2576 		if (caddr == NULL || strlen(caddr) == 0) {
2577 			cmn_err(CE_WARN,
2578 			    "pshot%d: malformed name %s (no bus address)",
2579 			    ddi_get_instance(parent), devname);
2580 			kmem_free(devstr, devstrlen);
2581 			ndi_devi_exit(parent);
2582 			return (NDI_FAILURE);
2583 		}
2584 
2585 		/*
2586 		 * Handle a few special cases for testing purposes
2587 		 */
2588 		rval = pshot_bus_config_test_specials(parent,
2589 		    devname, cname, caddr);
2590 
2591 		if (rval == NDI_SUCCESS) {
2592 			/*
2593 			 * Set up either a leaf or nexus device
2594 			 */
2595 			if (strcmp(cname, "pshot") == 0) {
2596 				rval = pshot_bus_config_setup_nexus(parent,
2597 				    cname, caddr);
2598 			} else {
2599 				rval = pshot_bus_config_setup_leaf(parent,
2600 				    cname, caddr);
2601 			}
2602 		}
2603 
2604 		kmem_free(devstr, devstrlen);
2605 		break;
2606 
2607 	case BUS_CONFIG_DRIVER:
2608 	case BUS_CONFIG_ALL:
2609 		rval = NDI_SUCCESS;
2610 		break;
2611 
2612 	default:
2613 		rval = NDI_FAILURE;
2614 		break;
2615 	}
2616 
2617 	if (rval == NDI_SUCCESS)
2618 		rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
2619 
2620 	ndi_devi_exit(parent);
2621 
2622 	if (pshot_debug)
2623 		cmn_err(CE_CONT, "pshot%d: bus_config %s\n",
2624 		    ddi_get_instance(parent),
2625 		    (rval == NDI_SUCCESS) ? "ok" : "failed");
2626 
2627 	return (rval);
2628 }
2629 
2630 static int
2631 pshot_bus_unconfig(dev_info_t *parent, uint_t flags,
2632     ddi_bus_config_op_t op, void *arg)
2633 {
2634 	major_t		major;
2635 	int		rval = NDI_SUCCESS;
2636 
2637 	if (pshot_debug) {
2638 		flags |= NDI_DEVI_DEBUG;
2639 		cmn_err(CE_CONT,
2640 		    "pshot%d: bus_unconfig %s flags=0x%x\n",
2641 		    ddi_get_instance(parent),
2642 		    (op == BUS_UNCONFIG_ONE) ? (char *)arg : "", flags);
2643 	}
2644 
2645 	/*
2646 	 * Hold the nexus across the bus_unconfig
2647 	 */
2648 	ndi_devi_enter(parent);
2649 
2650 	switch (op) {
2651 	case BUS_UNCONFIG_ONE:
2652 		/*
2653 		 * Nothing special required here
2654 		 */
2655 		if (pshot_debug) {
2656 			cmn_err(CE_CONT, "pshot%d: bus_unconfig:"
2657 			    " BUS_UNCONFIG_ONE\n", ddi_get_instance(parent));
2658 		}
2659 		break;
2660 
2661 	case BUS_UNCONFIG_DRIVER:
2662 		if (pshot_debug > 0) {
2663 			major = (major_t)(uintptr_t)arg;
2664 			cmn_err(CE_CONT,
2665 			    "pshot%d: BUS_UNCONFIG_DRIVER: %s\n",
2666 			    ddi_get_instance(parent),
2667 			    ddi_major_to_name(major));
2668 		}
2669 		break;
2670 
2671 	case BUS_UNCONFIG_ALL:
2672 		if (pshot_debug) {
2673 			cmn_err(CE_CONT, "pshot%d: bus_unconfig:"
2674 			    " BUS_UNCONFIG_ALL\n", ddi_get_instance(parent));
2675 		}
2676 		break;
2677 
2678 	default:
2679 		if (pshot_debug) {
2680 			cmn_err(CE_CONT, "pshot%d: bus_unconfig: DEFAULT\n",
2681 			    ddi_get_instance(parent));
2682 		}
2683 		rval = NDI_FAILURE;
2684 	}
2685 
2686 	if (rval == NDI_SUCCESS)
2687 		rval = ndi_busop_bus_unconfig(parent, flags, op, arg);
2688 
2689 	ndi_devi_exit(parent);
2690 
2691 	if (pshot_debug)
2692 		cmn_err(CE_CONT, "pshot%d: bus_unconfig %s\n",
2693 		    ddi_get_instance(parent),
2694 		    (rval == NDI_SUCCESS) ? "ok" : "failed");
2695 
2696 	return (rval);
2697 }
2698 
2699 static dev_info_t *
2700 pshot_findchild(dev_info_t *pdip, char *cname, char *caddr)
2701 {
2702 	dev_info_t *dip;
2703 	char *addr;
2704 
2705 	ASSERT(cname != NULL && caddr != NULL);
2706 	ASSERT(DEVI_BUSY_OWNED(pdip));
2707 
2708 	for (dip = ddi_get_child(pdip); dip != NULL;
2709 	    dip = ddi_get_next_sibling(dip)) {
2710 		if (strcmp(cname, ddi_node_name(dip)) != 0)
2711 			continue;
2712 
2713 		if ((addr = ddi_get_name_addr(dip)) == NULL) {
2714 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
2715 			    "bus-addr", &addr) == DDI_PROP_SUCCESS) {
2716 				if (strcmp(caddr, addr) == 0) {
2717 					ddi_prop_free(addr);
2718 					return (dip);
2719 				}
2720 				ddi_prop_free(addr);
2721 			}
2722 		} else {
2723 			if (strcmp(caddr, addr) == 0)
2724 				return (dip);
2725 		}
2726 	}
2727 
2728 	return (NULL);
2729 }
2730 
2731 static void
2732 pshot_nexus_properties(dev_info_t *parent, dev_info_t *child, char *cname,
2733     char *caddr)
2734 {
2735 	char *extension;
2736 
2737 	/*
2738 	 * extract the address extension
2739 	 */
2740 	extension = strstr(caddr, ",");
2741 	if (extension != NULL) {
2742 		++extension;
2743 	} else {
2744 		extension = "null";
2745 	}
2746 
2747 	/*
2748 	 * Create the "pm-want-child-notification?" property for all
2749 	 * nodes that do not have the "pm_strict" or "nopm_strict"
2750 	 * extension
2751 	 */
2752 	if (strcmp(extension, "pm_strict") != 0 &&
2753 	    strcmp(extension, "nopm_strict") != 0) {
2754 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2755 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2756 		    "pm-want-child-notification?") == 0) {
2757 			if (pshot_debug) {
2758 				cmn_err(CE_CONT, "pshot%d:"
2759 				    " nexus_properties:\n\tcreate the"
2760 				    " \"pm-want-child-notification?\""
2761 				    " property for %s@%s\n",
2762 				    ddi_get_instance(parent), cname, caddr);
2763 			}
2764 			if (ddi_prop_create(DDI_DEV_T_NONE, child, 0,
2765 			    "pm-want-child-notification?", NULL, 0)
2766 			    != DDI_PROP_SUCCESS) {
2767 				cmn_err(CE_WARN, "pshot%d:"
2768 				    " nexus_properties:\n\tunable to create"
2769 				    " the \"pm-want-child-notification?\""
2770 				    " property for %s@%s",
2771 				    ddi_get_instance(parent), cname, caddr);
2772 			}
2773 		}
2774 	}
2775 
2776 	/*
2777 	 * Create the "no-pm-components" property for all nodes
2778 	 * with extension "nopm" or "nopm_strict"
2779 	 */
2780 	if (strcmp(extension, "nopm") == 0 ||
2781 	    strcmp(extension, "nopm_strict") == 0) {
2782 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2783 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2784 		    "no-pm-components") == 0) {
2785 			if (pshot_debug) {
2786 				cmn_err(CE_CONT, "pshot%d:"
2787 				    " nexus_properties:\n\tcreate the"
2788 				    " \"no-pm-components\""
2789 				    " property for %s@%s\n",
2790 				    ddi_get_instance(parent), cname, caddr);
2791 			}
2792 			if (ddi_prop_create(DDI_DEV_T_NONE, child, 0,
2793 			    "no-pm-components", NULL, 0)
2794 			    != DDI_PROP_SUCCESS) {
2795 				cmn_err(CE_WARN, "pshot%d:"
2796 				    " nexus_properties:\n\tunable to create"
2797 				    " the \"no-pm-components\""
2798 				    " property for %s@%s",
2799 				    ddi_get_instance(parent), cname, caddr);
2800 			}
2801 		}
2802 	}
2803 }
2804 
2805 static void
2806 pshot_leaf_properties(dev_info_t *parent, dev_info_t *child, char *cname,
2807     char *caddr)
2808 {
2809 	char *extension;
2810 
2811 	/*
2812 	 * extract the address extension
2813 	 */
2814 	extension = strstr(caddr, ",");
2815 	if (extension != NULL) {
2816 		++extension;
2817 	} else {
2818 		extension = "null";
2819 	}
2820 
2821 	/*
2822 	 * Create the "no-involuntary-power-cycles" property for
2823 	 * all leaf nodes with extension "no_invol"
2824 	 */
2825 	if (strcmp(extension, "no_invol") == 0) {
2826 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2827 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2828 		    "no-involuntary-power-cycles") == 0) {
2829 			if (pshot_debug) {
2830 				cmn_err(CE_CONT, "pshot%d:"
2831 				    " leaf_properties:\n\tcreate the"
2832 				    " \"no-involuntary-power-cycles\""
2833 				    " property for %s@%s\n",
2834 				    ddi_get_instance(parent), cname, caddr);
2835 			}
2836 			if (ddi_prop_create(DDI_DEV_T_NONE, child,
2837 			    DDI_PROP_CANSLEEP,
2838 			    "no-involuntary-power-cycles", NULL, 0)
2839 			    != DDI_PROP_SUCCESS) {
2840 				cmn_err(CE_WARN, "pshot%d:"
2841 				    " leaf_properties:\n\tunable to create the"
2842 				    " \"no-involuntary-power-cycles\""
2843 				    " property for %s@%s",
2844 				    ddi_get_instance(parent), cname, caddr);
2845 			}
2846 		}
2847 	}
2848 
2849 	/*
2850 	 * Create the "dependency-property" property for all leaf
2851 	 * nodes with extension "dep_prop"
2852 	 * to be used with the PM_ADD_DEPENDENT_PROPERTY ioctl
2853 	 */
2854 	if (strcmp(extension, "dep_prop") == 0) {
2855 		if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2856 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2857 		    "dependency-property") == 0) {
2858 			if (pshot_debug) {
2859 				cmn_err(CE_CONT, "pshot%d:"
2860 				    " leaf_properties:\n\tcreate the"
2861 				    " \"dependency-property\""
2862 				    " property for %s@%s\n",
2863 				    ddi_get_instance(parent), cname, caddr);
2864 			}
2865 			if (ddi_prop_create(DDI_DEV_T_NONE, child,
2866 			    DDI_PROP_CANSLEEP, "dependency-property", NULL, 0)
2867 			    != DDI_PROP_SUCCESS) {
2868 				cmn_err(CE_WARN, "pshot%d:"
2869 				    " leaf_properties:\n\tunable to create the"
2870 				    " \"dependency-property\" property for"
2871 				    " %s@%s", ddi_get_instance(parent),
2872 				    cname, caddr);
2873 			}
2874 		}
2875 	}
2876 }
2877 
2878 /*
2879  * BUS_CONFIG_ONE: setup a child nexus instance.
2880  */
2881 static int
2882 pshot_bus_config_setup_nexus(dev_info_t *parent, char *cname, char *caddr)
2883 {
2884 	dev_info_t *child;
2885 	int rval;
2886 
2887 	ASSERT(parent != 0);
2888 	ASSERT(cname != NULL);
2889 	ASSERT(caddr != NULL);
2890 
2891 	child = pshot_findchild(parent, cname, caddr);
2892 	if (child) {
2893 		if (pshot_debug) {
2894 			cmn_err(CE_CONT,
2895 			    "pshot%d: bus_config one %s@%s found\n",
2896 			    ddi_get_instance(parent), cname, caddr);
2897 		}
2898 
2899 		/*
2900 		 * create the "pm-want-child-notification?" property
2901 		 * for this child, if it doesn't already exist
2902 		 */
2903 		(void) pshot_nexus_properties(parent, child, cname, caddr);
2904 
2905 		return (NDI_SUCCESS);
2906 	}
2907 
2908 	ndi_devi_alloc_sleep(parent, cname, DEVI_SID_NODEID, &child);
2909 	ASSERT(child != NULL);
2910 
2911 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
2912 	    "bus-addr", caddr) != DDI_PROP_SUCCESS) {
2913 		cmn_err(CE_WARN, "pshot%d: _prop_update %s@%s failed",
2914 		    ddi_get_instance(parent), cname, caddr);
2915 		(void) ndi_devi_free(child);
2916 		return (NDI_FAILURE);
2917 	}
2918 
2919 	rval = ndi_devi_bind_driver(child, 0);
2920 	if (rval != NDI_SUCCESS) {
2921 		cmn_err(CE_WARN, "pshot%d: bind_driver %s failed",
2922 		    ddi_get_instance(parent), cname);
2923 		(void) ndi_devi_free(child);
2924 		return (NDI_FAILURE);
2925 	}
2926 
2927 	/*
2928 	 * create the "pm-want-child-notification?" property
2929 	 */
2930 	(void) pshot_nexus_properties(parent, child, cname, caddr);
2931 
2932 	return (NDI_SUCCESS);
2933 }
2934 
2935 /*
2936  * BUS_CONFIG_ONE: setup a child leaf device instance.
2937  * for testing purposes, we will create nodes of a variety of types.
2938  */
2939 static int
2940 pshot_bus_config_setup_leaf(dev_info_t *parent, char *cname, char *caddr)
2941 {
2942 	dev_info_t *child;
2943 	char *compat_name;
2944 	char *nodetype;
2945 	int rval;
2946 	int i;
2947 
2948 	ASSERT(parent != 0);
2949 	ASSERT(cname != NULL);
2950 	ASSERT(caddr != NULL);
2951 
2952 	/*
2953 	 * if we already have a node with this name, return it
2954 	 */
2955 	if ((child = pshot_findchild(parent, cname, caddr)) != NULL) {
2956 		/*
2957 		 * create the "no-involuntary-power-cycles" or
2958 		 * the "dependency-property" property, if they
2959 		 * don't already exit
2960 		 */
2961 		(void) pshot_leaf_properties(parent, child, cname, caddr);
2962 
2963 		return (NDI_SUCCESS);
2964 	}
2965 
2966 	ndi_devi_alloc_sleep(parent, cname, DEVI_SID_NODEID, &child);
2967 	ASSERT(child != NULL);
2968 
2969 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child, "bus-addr",
2970 	    caddr) != DDI_PROP_SUCCESS) {
2971 		(void) ndi_devi_free(child);
2972 		return (NDI_FAILURE);
2973 	}
2974 
2975 	/*
2976 	 * test compatible naming
2977 	 * if the child nodename is "cdisk", attach the list of compatible
2978 	 * named disks
2979 	 */
2980 	if (strcmp(cname, pshot_compat_diskname) == 0) {
2981 		if ((ndi_prop_update_string_array(DDI_DEV_T_NONE,
2982 		    child, "compatible", (char **)pshot_compat_psramdisks,
2983 		    5)) != DDI_PROP_SUCCESS) {
2984 			(void) ndi_devi_free(child);
2985 			return (NDI_FAILURE);
2986 		}
2987 	} else {
2988 		for (i = 0; i < pshot_devices_len && pshot_devices[i].name;
2989 		    i++) {
2990 			if (strcmp(cname, pshot_devices[i].name) == 0) {
2991 				compat_name = pshot_devices[i].compat;
2992 				nodetype = pshot_devices[i].nodetype;
2993 				if (pshot_debug) {
2994 					cmn_err(CE_CONT, "pshot%d: %s %s %s\n",
2995 					    ddi_get_instance(parent), cname,
2996 					    compat_name, nodetype);
2997 				}
2998 				if ((ndi_prop_update_string_array(
2999 				    DDI_DEV_T_NONE, child, "compatible",
3000 				    &compat_name, 1)) != DDI_PROP_SUCCESS) {
3001 					(void) ndi_devi_free(child);
3002 					return (NDI_FAILURE);
3003 				}
3004 				if ((ndi_prop_update_string(
3005 				    DDI_DEV_T_NONE, child, "node-type",
3006 				    nodetype)) != DDI_PROP_SUCCESS) {
3007 					(void) ndi_devi_free(child);
3008 					return (NDI_FAILURE);
3009 				}
3010 			}
3011 		}
3012 	}
3013 
3014 	rval = ndi_devi_bind_driver(child, 0);
3015 	if (rval != NDI_SUCCESS) {
3016 		cmn_err(CE_WARN, "pshot%d: bind_driver %s failed",
3017 		    ddi_get_instance(parent), cname);
3018 		(void) ndi_devi_free(child);
3019 		return (NDI_FAILURE);
3020 	}
3021 
3022 	/*
3023 	 * create the "no-involuntary-power-cycles" or
3024 	 * the "dependency-property" property
3025 	 */
3026 	(void) pshot_leaf_properties(parent, child, cname, caddr);
3027 
3028 	return (NDI_SUCCESS);
3029 }
3030 
3031 /*
3032  * Handle some special cases for testing bus_config via pshot
3033  *
3034  * Match these special address formats to behavior:
3035  *
3036  *	err.*		- induce bus_config error
3037  *	delay		- induce 1 second of bus_config delay time
3038  *	delay,n		- induce n seconds of bus_config delay time
3039  *	wait		- induce 1 second of bus_config wait time
3040  *	wait,n		- induce n seconds of bus_config wait time
3041  *	failinit.*	- induce error at INITCHILD
3042  *	failprobe.*	- induce error at probe
3043  *	failattach.*	- induce error at attach
3044  */
3045 /*ARGSUSED*/
3046 static int
3047 pshot_bus_config_test_specials(dev_info_t *parent, char *devname,
3048 	char *cname, char *caddr)
3049 {
3050 	char	*p;
3051 	int	n;
3052 
3053 	if (strncmp(caddr, "err", 3) == 0) {
3054 		if (pshot_debug)
3055 			cmn_err(CE_CONT,
3056 			    "pshot%d: %s forced failure\n",
3057 			    ddi_get_instance(parent), devname);
3058 		return (NDI_FAILURE);
3059 	}
3060 
3061 	/*
3062 	 * The delay and wait strings have the same effect.
3063 	 * The "wait[,]" support should be removed once the
3064 	 * devfs test suites are fixed.
3065 	 * NOTE: delay should not be called from interrupt context
3066 	 */
3067 	ASSERT(!servicing_interrupt());
3068 
3069 	if (strncmp(caddr, "delay,", 6) == 0) {
3070 		p = caddr+6;
3071 		n = stoi(&p);
3072 		if (*p != 0)
3073 			n = 1;
3074 		if (pshot_debug)
3075 			cmn_err(CE_CONT,
3076 			    "pshot%d: %s delay %d second\n",
3077 			    ddi_get_instance(parent), devname, n);
3078 		delay(n * drv_usectohz(1000000));
3079 	} else if (strncmp(caddr, "delay", 5) == 0) {
3080 		if (pshot_debug)
3081 			cmn_err(CE_CONT,
3082 			    "pshot%d: %s delay 1 second\n",
3083 			    ddi_get_instance(parent), devname);
3084 		delay(drv_usectohz(1000000));
3085 	} else if (strncmp(caddr, "wait,", 5) == 0) {
3086 		p = caddr+5;
3087 		n = stoi(&p);
3088 		if (*p != 0)
3089 			n = 1;
3090 		if (pshot_debug)
3091 			cmn_err(CE_CONT,
3092 			    "pshot%d: %s wait %d second\n",
3093 			    ddi_get_instance(parent), devname, n);
3094 		delay(n * drv_usectohz(1000000));
3095 	} else if (strncmp(caddr, "wait", 4) == 0) {
3096 		if (pshot_debug)
3097 			cmn_err(CE_CONT,
3098 			    "pshot%d: %s wait 1 second\n",
3099 			    ddi_get_instance(parent), devname);
3100 		delay(drv_usectohz(1000000));
3101 	}
3102 
3103 	return (NDI_SUCCESS);
3104 }
3105 
3106 /*
3107  * translate nodetype name to actual value
3108  */
3109 static char *
3110 pshot_str2nt(char *str)
3111 {
3112 	int i;
3113 
3114 	for (i = 0; pshot_nodetypes[i].name; i++) {
3115 		if (strcmp(pshot_nodetypes[i].name, str) == 0)
3116 			return (pshot_nodetypes[i].val);
3117 	}
3118 	return (NULL);
3119 }
3120 
3121 /*
3122  * grows array pointed to by <dstp>, with <src> data
3123  * <dstlen> = # elements of the original <*dstp>
3124  * <srclen> = # elements of <src>
3125  *
3126  * on success, returns 0 and a pointer to the new array through <dstp> with
3127  * <srclen> + <dstlen> number of elements;
3128  * else returns non-zero
3129  *
3130  * a NULL <*dstp> is OK (a NULL <dstp> is not) and so is a zero <dstlen>
3131  */
3132 static int
3133 pshot_devices_grow(pshot_device_t **dstp, size_t dstlen,
3134     const pshot_device_t *src, size_t srclen)
3135 {
3136 	size_t i;
3137 	pshot_device_t *newdst;
3138 
3139 	newdst = kmem_alloc((srclen + dstlen) * sizeof (*src),
3140 	    KM_SLEEP);
3141 
3142 	/* keep old pointers and dup new ones */
3143 	if (*dstp)
3144 		bcopy(*dstp, newdst, dstlen * sizeof (*src));
3145 	for (i = 0; i < srclen; i++) {
3146 		newdst[i + dstlen].name =
3147 		    i_ddi_strdup(src[i].name, KM_SLEEP);
3148 
3149 		newdst[i + dstlen].nodetype =
3150 		    i_ddi_strdup(src[i].nodetype, KM_SLEEP);
3151 
3152 		newdst[i + dstlen].compat =
3153 		    i_ddi_strdup(src[i].compat, KM_SLEEP);
3154 	}
3155 
3156 	/* do last */
3157 	if (*dstp)
3158 		kmem_free(*dstp, dstlen * sizeof (*src));
3159 	*dstp = newdst;
3160 	return (0);
3161 }
3162 
3163 /*
3164  * free a pshot_device_t array <dp> with <len> elements
3165  * null pointers within the elements are ok
3166  */
3167 static void
3168 pshot_devices_free(pshot_device_t *dp, size_t len)
3169 {
3170 	size_t i;
3171 
3172 	for (i = 0; i < len; i++) {
3173 		if (dp[i].name)
3174 			kmem_free(dp[i].name, strlen(dp[i].name) + 1);
3175 		if (dp[i].nodetype)
3176 			kmem_free(dp[i].nodetype, strlen(dp[i].nodetype) + 1);
3177 		if (dp[i].compat)
3178 			kmem_free(dp[i].compat, strlen(dp[i].compat) + 1);
3179 	}
3180 	kmem_free(dp, len * sizeof (*dp));
3181 }
3182 
3183 /*
3184  * returns an array of pshot_device_t parsed from <dip>'s properties
3185  *
3186  * property structure (i.e. pshot.conf) for pshot:
3187  *
3188  * corresponding         |   pshot_device_t array elements
3189  * pshot_device_t        |
3190  * member by prop name   |   [0]            [1]           [2]
3191  * ----------------------|--------------|-------------|-----------------------
3192  * <PSHOT_PROP_DEVNAME>  ="disk",        "tape",       "testdev";
3193  * <PSHOT_PROP_DEVNT>    ="DDI_NT_BLOCK","DDI_NT_TAPE","ddi_testdev_nodetype";
3194  * <PSHOT_PROP_DEVCOMPAT>="testdrv",     "testdrv",    "testdrv";
3195  *
3196  *
3197  * if any of these properties are specified, then:
3198  * - all the members must be specified
3199  * - the number of elements for each string array property must be the same
3200  * - no empty strings allowed
3201  * - nodetypes (PSHOT_PROP_DEVNT) must be the nodetype name as specified in
3202  *   sys/sunddi.h
3203  *
3204  * NOTE: the pshot_nodetypes[] table should be kept in sync with the list
3205  * of ddi nodetypes.  It's not normally critical to always be in sync so
3206  * keeping this up-to-date can usually be done "on-demand".
3207  *
3208  * if <flags> & PSHOT_DEV_ANYNT, then custom nodetype strings are allowed.
3209  * these will be duplicated verbatim
3210  */
3211 static pshot_device_t *
3212 pshot_devices_from_props(dev_info_t *dip, size_t *lenp, int flags)
3213 {
3214 	pshot_device_t *devarr = NULL;
3215 	char **name_arr = NULL, **nt_arr = NULL, **compat_arr = NULL;
3216 	uint_t name_arr_len, nt_arr_len, compat_arr_len;
3217 	uint_t i;
3218 	char *str;
3219 
3220 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 0,
3221 	    PSHOT_PROP_DEVNAME, &name_arr, &name_arr_len) !=
3222 	    DDI_PROP_SUCCESS)
3223 		name_arr = NULL;
3224 
3225 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 0,
3226 	    PSHOT_PROP_DEVNT, &nt_arr, &nt_arr_len) !=
3227 	    DDI_PROP_SUCCESS)
3228 		nt_arr = NULL;
3229 
3230 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 0,
3231 	    PSHOT_PROP_DEVCOMPAT, &compat_arr, &compat_arr_len) !=
3232 	    DDI_PROP_SUCCESS)
3233 		compat_arr = NULL;
3234 
3235 	/*
3236 	 * warn about any incorrect usage, if specified
3237 	 */
3238 	if (!(name_arr || nt_arr || compat_arr))
3239 		return (NULL);
3240 
3241 	if (!(name_arr && nt_arr && compat_arr) ||
3242 	    (name_arr_len != nt_arr_len) ||
3243 	    (name_arr_len != compat_arr_len))
3244 		goto FAIL;
3245 
3246 	for (i = 0; i < name_arr_len; i++) {
3247 		if (*name_arr[i] == '\0' ||
3248 		    *nt_arr[i] == '\0' ||
3249 		    *compat_arr[i] == '\0')
3250 			goto FAIL;
3251 	}
3252 
3253 	devarr = kmem_zalloc(name_arr_len * sizeof (*devarr), KM_SLEEP);
3254 	for (i = 0; i < name_arr_len; i++) {
3255 		devarr[i].name = i_ddi_strdup(name_arr[i], KM_SLEEP);
3256 		devarr[i].compat = i_ddi_strdup(compat_arr[i], KM_SLEEP);
3257 
3258 		if ((str = pshot_str2nt(nt_arr[i])) == NULL)
3259 			if (flags & PSHOT_DEV_ANYNT)
3260 				str = nt_arr[i];
3261 			else
3262 				goto FAIL;
3263 		devarr[i].nodetype = i_ddi_strdup(str, KM_SLEEP);
3264 	}
3265 	ddi_prop_free(name_arr);
3266 	ddi_prop_free(nt_arr);
3267 	ddi_prop_free(compat_arr);
3268 
3269 	/* set <*lenp> ONLY on success */
3270 	*lenp = name_arr_len;
3271 
3272 	return (devarr);
3273 	/*NOTREACHED*/
3274 FAIL:
3275 	cmn_err(CE_WARN, "malformed device specification property");
3276 	if (name_arr)
3277 		ddi_prop_free(name_arr);
3278 	if (nt_arr)
3279 		ddi_prop_free(nt_arr);
3280 	if (compat_arr)
3281 		ddi_prop_free(compat_arr);
3282 	if (devarr)
3283 		pshot_devices_free(devarr, name_arr_len);
3284 	return (NULL);
3285 }
3286 
3287 /*
3288  * if global <pshot_devices> was not set up already (i.e. is NULL):
3289  *	sets up global <pshot_devices> and <pshot_devices_len>,
3290  *	using device properties	from <dip> and global <pshot_stock_devices>.
3291  *	device properties, if any, overrides pshot_stock_devices.
3292  *
3293  * returns 0 on success (or if pshot_devices already set up)
3294  *
3295  * INTERNAL LOCKING: <pshot_devices_lock>
3296  */
3297 static int
3298 pshot_devices_setup(dev_info_t *dip)
3299 {
3300 	pshot_device_t *newdevs = NULL;
3301 	size_t newdevs_len = 0;
3302 	int rv = 0;
3303 
3304 	mutex_enter(&pshot_devices_lock);
3305 	if (pshot_devices != NULL)
3306 		goto FAIL;
3307 
3308 	ASSERT(pshot_devices_len == 0);
3309 
3310 	newdevs = pshot_devices_from_props(dip, &newdevs_len, PSHOT_DEV_ANYNT);
3311 	rv = pshot_devices_grow(&newdevs, newdevs_len, pshot_stock_devices,
3312 	    PSHOT_N_STOCK_DEVICES);
3313 	if (rv != 0) {
3314 		cmn_err(CE_WARN, "pshot_devices_setup: pshot_devices_grow "
3315 		    "failed");
3316 		goto FAIL;
3317 	}
3318 	newdevs_len += PSHOT_N_STOCK_DEVICES;
3319 
3320 	pshot_devices = newdevs;
3321 	pshot_devices_len = newdevs_len;
3322 	rv = 0;
3323 FAIL:
3324 	if (rv && newdevs)
3325 		pshot_devices_free(newdevs, newdevs_len);
3326 	mutex_exit(&pshot_devices_lock);
3327 	return (rv);
3328 }
3329 
3330 
3331 #ifdef NOTNEEDED
3332 /* ARGSUSED */
3333 static int
3334 pshot_probe_family(dev_info_t *self, ddi_probe_method_t probe_how,
3335     dev_info_t **return_dip)
3336 {
3337 	char name[64];
3338 	uint_t bus_id;
3339 	dev_info_t *child;
3340 
3341 	for (bus_id = 10; bus_id < 20; bus_id++) {
3342 		(void) sprintf(name, "%d", bus_id);
3343 		if ((ndi_devi_alloc(self, "psramd", DEVI_SID_NODEID,
3344 		    &child)) != NDI_SUCCESS) {
3345 			return (DDI_FAILURE);
3346 		}
3347 
3348 		if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
3349 		    "bus-addr", name) != DDI_PROP_SUCCESS) {
3350 			(void) ndi_devi_free(child);
3351 			if (return_dip != NULL)
3352 				*return_dip = (dev_info_t *)NULL;
3353 			return (DDI_FAILURE);
3354 		}
3355 
3356 		if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
3357 			return (DDI_FAILURE);
3358 		}
3359 	}
3360 	return (DDI_SUCCESS);
3361 }
3362 
3363 static int
3364 strtoi(char *str)
3365 {
3366 	int c;
3367 	int val;
3368 
3369 	for (val = 0, c = *str++; c >= '0' && c <= '9'; c = *str++) {
3370 		val *= 10;
3371 		val += c - '0';
3372 	}
3373 	return (val);
3374 }
3375 
3376 #endif
3377 
3378 static void
3379 pshot_setup_autoattach(dev_info_t *devi)
3380 {
3381 	dev_info_t *l1child, *l2child;
3382 	int rv;
3383 
3384 	rv = ndi_devi_alloc(devi, "pshot", DEVI_SID_NODEID, &l1child);
3385 	if (rv == NDI_SUCCESS) {
3386 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, l1child,
3387 		    "bus-addr", "0");
3388 		rv =  ndi_devi_alloc(l1child, "port", DEVI_SID_NODEID,
3389 		    &l2child);
3390 		if (rv == NDI_SUCCESS)
3391 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
3392 			    l2child, "bus-addr", "99");
3393 	}
3394 
3395 	rv = ndi_devi_alloc(devi, "port", DEVI_SID_NODEID, &l1child);
3396 	if (rv == NDI_SUCCESS)
3397 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, l1child,
3398 		    "bus-addr", "99");
3399 
3400 	rv = ndi_devi_alloc(devi, "gen_drv", DEVI_SID_NODEID, &l1child);
3401 	if (rv == NDI_SUCCESS)
3402 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, l1child,
3403 		    "bus-addr", "99");
3404 
3405 	rv = ndi_devi_alloc(devi, "no_driver", DEVI_SID_NODEID, &l1child);
3406 	if (rv == NDI_SUCCESS)
3407 		(void) ndi_devi_alloc(l1child, "no_driver", DEVI_SID_NODEID,
3408 		    &l2child);
3409 }
3410 
3411 #ifdef PRUNE_SNUBS
3412 
3413 #define	PRUNE_THIS_NODE(d) (((d)->devi_node_name != NULL) && \
3414 	(DEVI_PROM_NODE((d)->devi_nodeid)) && \
3415 	((d)->devi_addr == NULL))
3416 /*
3417  * test code to remove OBP nodes that have not attached
3418  */
3419 static void
3420 prune_snubs(const char *name)
3421 {
3422 	struct dev_info *nex_dip, *cdip, *cndip;
3423 	int maj;
3424 	int rv;
3425 
3426 	maj = ddi_name_to_major((char *)name);
3427 	if (maj != -1) {
3428 		nex_dip = (struct dev_info *)devnamesp[maj].dn_head;
3429 		while (nex_dip != NULL) {
3430 			cndip = ddi_get_child(nex_dip);
3431 			while ((cdip = cndip) != NULL) {
3432 				cndip = cdip->devi_sibling;
3433 				if (PRUNE_THIS_NODE(cdip)) {
3434 					cmn_err(CE_NOTE,
3435 					    "parent %s@%s pruning node %s",
3436 					    nex_dip->devi_node_name,
3437 					    nex_dip->devi_addr,
3438 					    cdip->devi_node_name);
3439 					rv = ndi_devi_offline(cdip,
3440 					    NDI_DEVI_REMOVE);
3441 					if (rv != NDI_SUCCESS)
3442 						cmn_err(CE_NOTE,
3443 						    "failed to prune node, "
3444 						    "err %d", rv);
3445 				}
3446 			}
3447 		nex_dip = nex_dip->devi_next;
3448 		}
3449 	}
3450 }
3451 
3452 #endif /* PRUBE_SNUBS */
3453 
3454 #ifdef KERNEL_DEVICE_TREE_WALKER
3455 static kthread_id_t pwt;
3456 static kmutex_t pwl;
3457 static kcondvar_t pwcv;
3458 
3459 static void
3460 pshot_walk_tree()
3461 {
3462 	static int pshot_devnode(dev_info_t *dip, void * arg);
3463 
3464 	dev_info_t *root = ddi_root_node();
3465 	ddi_walk_devs(root, pshot_devnode, NULL);
3466 }
3467 
3468 static void
3469 pshot_walk_thread()
3470 {
3471 	static void pshot_timeout(void *arg);
3472 	static kthread_id_t pwt;
3473 
3474 	pwt = curthread;
3475 	mutex_init(&pwl, NULL, MUTEX_DRIVER, NULL);
3476 	cv_init(&pwcv, NULL, CV_DRIVER, NULL);
3477 
3478 	while (1) {
3479 		pshot_walk_tree();
3480 		mutex_enter(&pwl);
3481 		(void) timeout(pshot_timeout, NULL, 5 * drv_usectohz(1000000));
3482 		cv_wait(&pwcv, &pwl);
3483 		mutex_exit(&pwl);
3484 	}
3485 }
3486 
3487 static void
3488 pshot_timeout(void *arg)
3489 {
3490 	mutex_enter(&pwl);
3491 	cv_signal(&pwcv);
3492 	mutex_exit(&pwl);
3493 }
3494 
3495 static int
3496 pshot_devnode(dev_info_t *dip, void *arg)
3497 {
3498 	dev_info_t *f_dip;
3499 
3500 	if (dip != ddi_root_node()) {
3501 		f_dip = ndi_devi_find((dev_info_t *)DEVI(dip)->devi_parent,
3502 		    DEVI(dip)->devi_node_name, DEVI(dip)->devi_addr);
3503 		if (f_dip != dip) {
3504 			cmn_err(CE_NOTE, "!pshot_devnode: failed lookup"
3505 			    "node (%s/%s@%s)\n",
3506 			    DEVI(DEVI(dip)->devi_parent)->devi_node_name,
3507 			    (DEVI(dip)->devi_node_name ?
3508 			    DEVI(dip)->devi_node_name : "NULL"),
3509 			    (DEVI(dip)->devi_addr ? DEVI(dip)->devi_addr :
3510 			    "NULL"));
3511 		}
3512 	}
3513 	return (DDI_WALK_CONTINUE);
3514 }
3515 #endif /* KERNEL_DEVICE_TREE_WALKER */
3516 
3517 #ifdef DEBUG
3518 static void
3519 pshot_event_cb_test(dev_info_t *dip, ddi_eventcookie_t cookie,
3520 	void *arg, void *bus_impldata)
3521 {
3522 	pshot_t *softstate = (pshot_t *)arg;
3523 	int event_tag;
3524 
3525 	/* look up the event */
3526 	event_tag = NDI_EVENT_TAG(cookie);
3527 	cmn_err(CE_CONT, "pshot_event_cb_test:\n\t"
3528 	    "dip = 0x%p cookie = 0x%p (%s), tag = %d\n\t"
3529 	    "arg = 0x%p bus_impl = 0x%p\n",
3530 	    (void *)dip, (void *)cookie, NDI_EVENT_NAME(cookie),
3531 	    event_tag, (void *)softstate, (void *)bus_impldata);
3532 
3533 }
3534 
3535 static void
3536 pshot_event_test(void *arg)
3537 {
3538 	pshot_t *pshot = (pshot_t *)arg;
3539 	ndi_event_hdl_t hdl;
3540 	ndi_event_set_t	events;
3541 	int i, rval;
3542 
3543 	(void) ndi_event_alloc_hdl(pshot->dip, NULL, &hdl, NDI_SLEEP);
3544 
3545 	events.ndi_events_version = NDI_EVENTS_REV1;
3546 	events.ndi_n_events = PSHOT_N_TEST_EVENTS;
3547 	events.ndi_event_defs = pshot_test_events;
3548 
3549 	cmn_err(CE_CONT, "pshot: binding set of 8 events\n");
3550 	delay(drv_usectohz(1000000));
3551 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3552 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3553 
3554 	cmn_err(CE_CONT, "pshot: binding the same set of 8 events\n");
3555 	delay(drv_usectohz(1000000));
3556 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3557 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3558 
3559 	cmn_err(CE_CONT, "pshot: unbinding  all events\n");
3560 	delay(drv_usectohz(1000000));
3561 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3562 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3563 
3564 
3565 	cmn_err(CE_CONT, "pshot: binding one highlevel event\n");
3566 	delay(drv_usectohz(1000000));
3567 	events.ndi_n_events = 1;
3568 	events.ndi_event_defs = pshot_test_events_high;
3569 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3570 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3571 
3572 	cmn_err(CE_CONT, "pshot: binding the same set of 8 events\n");
3573 	delay(drv_usectohz(1000000));
3574 	events.ndi_n_events = PSHOT_N_TEST_EVENTS;
3575 	events.ndi_event_defs = pshot_test_events;
3576 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3577 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3578 
3579 	cmn_err(CE_CONT, "pshot: unbinding one highlevel event\n");
3580 	delay(drv_usectohz(1000000));
3581 	events.ndi_n_events = 1;
3582 	events.ndi_event_defs = pshot_test_events_high;
3583 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3584 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3585 
3586 	cmn_err(CE_CONT, "pshot: binding one highlevel event\n");
3587 	delay(drv_usectohz(1000000));
3588 	events.ndi_n_events = 1;
3589 	events.ndi_event_defs = pshot_test_events_high;
3590 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3591 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3592 
3593 	cmn_err(CE_CONT, "pshot: unbinding one highlevel event\n");
3594 	delay(drv_usectohz(1000000));
3595 	events.ndi_n_events = 1;
3596 	events.ndi_event_defs = pshot_test_events_high;
3597 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3598 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3599 
3600 	cmn_err(CE_CONT, "pshot: binding the same set of 8 events\n");
3601 	delay(drv_usectohz(1000000));
3602 	events.ndi_n_events = PSHOT_N_TEST_EVENTS;
3603 	events.ndi_event_defs = pshot_test_events;
3604 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3605 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3606 
3607 	cmn_err(CE_CONT, "pshot: unbinding first 2 events\n");
3608 	delay(drv_usectohz(1000000));
3609 	events.ndi_n_events = 2;
3610 	events.ndi_event_defs = pshot_test_events;
3611 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3612 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3613 
3614 	cmn_err(CE_CONT, "pshot: unbinding first 2 events again\n");
3615 	delay(drv_usectohz(1000000));
3616 	events.ndi_n_events = 2;
3617 	events.ndi_event_defs = pshot_test_events;
3618 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3619 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3620 
3621 	cmn_err(CE_CONT, "pshot: unbinding  middle 2 events\n");
3622 	delay(drv_usectohz(1000000));
3623 	events.ndi_n_events = 2;
3624 	events.ndi_event_defs = &pshot_test_events[4];
3625 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3626 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3627 
3628 	cmn_err(CE_CONT, "pshot: binding those 2 events back\n");
3629 	delay(drv_usectohz(1000000));
3630 	events.ndi_n_events = 2;
3631 	events.ndi_event_defs = &pshot_test_events[4];
3632 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3633 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3634 
3635 	cmn_err(CE_CONT, "pshot: unbinding  2 events\n");
3636 	delay(drv_usectohz(1000000));
3637 	events.ndi_n_events = 2;
3638 	events.ndi_event_defs = &pshot_test_events[4];
3639 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3640 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3641 
3642 	cmn_err(CE_CONT, "pshot: unbinding  all events\n");
3643 	delay(drv_usectohz(1000000));
3644 	events.ndi_n_events = PSHOT_N_TEST_EVENTS;
3645 	events.ndi_event_defs = pshot_test_events;
3646 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3647 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3648 
3649 	cmn_err(CE_CONT, "pshot: unbinding  1 event\n");
3650 	delay(drv_usectohz(1000000));
3651 	events.ndi_n_events = 1;
3652 	events.ndi_event_defs = &pshot_test_events[2];
3653 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3654 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3655 
3656 	cmn_err(CE_CONT, "pshot: unbinding  1 event\n");
3657 	delay(drv_usectohz(1000000));
3658 	events.ndi_n_events = 1;
3659 	events.ndi_event_defs = &pshot_test_events[3];
3660 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3661 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3662 
3663 	cmn_err(CE_CONT, "pshot: unbinding  1 event\n");
3664 	delay(drv_usectohz(1000000));
3665 	events.ndi_n_events = 1;
3666 	events.ndi_event_defs = &pshot_test_events[6];
3667 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3668 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3669 
3670 	cmn_err(CE_CONT, "pshot: unbinding  1 event\n");
3671 	delay(drv_usectohz(1000000));
3672 	events.ndi_n_events = 1;
3673 	events.ndi_event_defs = &pshot_test_events[7];
3674 	rval = ndi_event_unbind_set(hdl, &events, NDI_SLEEP);
3675 	cmn_err(CE_CONT, "pshot: ndi_event_unbind_set rval = %d\n", rval);
3676 
3677 	events.ndi_n_events = PSHOT_N_TEST_EVENTS;
3678 	events.ndi_event_defs = pshot_test_events;
3679 
3680 	cmn_err(CE_CONT, "pshot: binding set of 8 events\n");
3681 	delay(drv_usectohz(1000000));
3682 	rval = ndi_event_bind_set(hdl, &events, NDI_SLEEP);
3683 	cmn_err(CE_CONT, "pshot: ndi_event_bind_set rval = %d\n", rval);
3684 
3685 	cmn_err(CE_CONT, "pshot: adding 8 callbacks\n");
3686 	delay(drv_usectohz(1000000));
3687 	for (i = 0; i < 8; i++) {
3688 		rval = ndi_event_add_callback(hdl, pshot->dip,
3689 		    ndi_event_tag_to_cookie(hdl,
3690 		    pshot_test_events[i].ndi_event_tag),
3691 		    pshot_event_cb_test,
3692 		    (void *)(uintptr_t)pshot_test_events[i].ndi_event_tag,
3693 		    NDI_SLEEP, &pshot->test_callback_cache[i]);
3694 		ASSERT(rval == NDI_SUCCESS);
3695 	}
3696 
3697 	cmn_err(CE_CONT, "pshot: event callbacks\n");
3698 
3699 	for (i = 10; i < 18; i++) {
3700 		ddi_eventcookie_t cookie = ndi_event_tag_to_cookie(hdl, i);
3701 
3702 		rval = ndi_event_run_callbacks(hdl, pshot->dip, cookie,
3703 		    (void *)hdl);
3704 
3705 		cmn_err(CE_CONT, "pshot: callback, tag=%d rval=%d\n",
3706 		    i, rval);
3707 		delay(drv_usectohz(1000000));
3708 	}
3709 
3710 	cmn_err(CE_CONT, "pshot: redo event callbacks\n");
3711 
3712 	for (i = 10; i < 18; i++) {
3713 		ddi_eventcookie_t cookie = ndi_event_tag_to_cookie(hdl, i);
3714 
3715 		rval = ndi_event_run_callbacks(hdl,
3716 		    pshot->dip, cookie, (void *)hdl);
3717 
3718 		cmn_err(CE_CONT, "pshot: callback, tag=%d rval=%d\n",
3719 		    i, rval);
3720 		delay(drv_usectohz(1000000));
3721 	}
3722 
3723 	cmn_err(CE_CONT, "pshot: removing 8 callbacks\n");
3724 	delay(drv_usectohz(1000000));
3725 
3726 	for (i = 0; i < 8; i++) {
3727 		(void) ndi_event_remove_callback(hdl,
3728 		    pshot->test_callback_cache[i]);
3729 
3730 		pshot->test_callback_cache[i] = 0;
3731 	}
3732 
3733 	cmn_err(CE_CONT, "pshot: freeing handle with bound set\n");
3734 	delay(drv_usectohz(1000000));
3735 
3736 	rval =	ndi_event_free_hdl(hdl);
3737 
3738 	ASSERT(rval == NDI_SUCCESS);
3739 
3740 }
3741 
3742 void
3743 pshot_event_test_post_one(void *arg)
3744 {
3745 	pshot_t	*pshot = (pshot_t *)arg;
3746 	int rval;
3747 	ddi_eventcookie_t cookie;
3748 
3749 	cmn_err(CE_CONT, "pshot%d: pshot_event_post_one event\n",
3750 	    pshot->instance);
3751 
3752 	if (ddi_get_eventcookie(pshot->dip, PSHOT_EVENT_NAME_BUS_TEST_POST,
3753 	    &cookie) != DDI_SUCCESS) {
3754 		cmn_err(CE_NOTE, "pshot_bus_test_post cookie not found");
3755 		return;
3756 	}
3757 
3758 	rval = ndi_post_event(pshot->dip, pshot->dip, cookie, NULL);
3759 
3760 	cmn_err(CE_CONT, "pshot%d: pshot_event_post_one rval=%d\n",
3761 	    pshot->instance, rval);
3762 
3763 	(void) timeout(pshot_event_test_post_one, (void *)pshot,
3764 	    pshot->instance * drv_usectohz(60000000));
3765 
3766 }
3767 #endif /* DEBUG */
3768