blob: f141d541e3796da94765059916246c242c785faf [file] [log] [blame]
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
cth5e3986c2006-06-08 16:03:08 -07005 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07007 *
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/*
Vikram Hegde08492e32009-01-28 11:08:43 -080022 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070023 * Use is subject to license terms.
24 */
Garrett D'Amorecd21e7c2012-03-11 22:00:47 -070025/*
26 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
Daniel Hoffman48bbca82017-02-17 11:48:20 -080027 * Copyright (c) 2016 by Delphix. All rights reserved.
Garrett D'Amorecd21e7c2012-03-11 22:00:47 -070028 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070029
30/*
31 * pseudo bus nexus driver
32 * hotplug framework test facility
33 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070034
35/*
36 * The pshot driver can be used to exercise the i/o framework together
37 * with devfs by configuring an arbitrarily complex device tree.
38 *
39 * The pshot driver is rooted at /devices/pshot. The following commands
40 * illustrate the operation of devfs together with pshot's bus_config.
41 * The first command demonstrates that, like the magician showing there's
Daniel Hoffman48bbca82017-02-17 11:48:20 -080042 * nothing up their sleeve, /devices/pshot is empty. The second command
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070043 * conjures up a branch of pshot nodes. Note that pshot's bus_config is
44 * called sequentially by devfs for each node, as part of the pathname
45 * resolution, and that each pshot node is fully configured and
46 * attached before that node's bus_config is called to configure the
47 * next child down the tree. The final result is a "disk" node configured
48 * at the bottom of the named hierarchy of pshot nodes.
49 *
50 * #
51 * # ls /devices/pshot
52 * #
53 * # ls -ld /devices/pshot/pshot@0/pshot@1/pshot@2/disk@3,0
54 * drwxr-xr-x 2 root sys 512 Feb 6 15:10
55 * /devices/pshot/pshot@0/pshot@1/pshot@2/disk@3,0
56 *
57 * pshot supports some unique behaviors as aids for test error cases.
58 *
59 * Match these special address formats to behavior:
60 *
61 * err.* - induce bus_config error
62 * delay - induce 1 second of bus_config delay time
63 * delay,n - induce n seconds of bus_config delay time
64 * wait - induce 1 second of bus_config wait time
65 * wait,n - induce n seconds of bus_config wait time
66 * failinit.* - induce error at INITCHILD
67 * failprobe.* - induce error at probe
68 * failattach.* - induce error at attach
69 */
70
71#if defined(lint) && !defined(DEBUG)
72#define DEBUG 1
73#endif
74
75#include <sys/types.h>
76#include <sys/cmn_err.h>
77#include <sys/conf.h>
78#include <sys/ddi_impldefs.h>
79#include <sys/autoconf.h>
80#include <sys/open.h>
81#include <sys/stat.h>
82#include <sys/file.h>
83#include <sys/errno.h>
84#include <sys/systm.h>
85#include <sys/modctl.h>
86#include <sys/kmem.h>
87#include <sys/ddi.h>
88#include <sys/sunddi.h>
89#include <sys/sunndi.h>
90#include <sys/devctl.h>
91#include <sys/disp.h>
92#include <sys/utsname.h>
93#include <sys/pshot.h>
94#include <sys/debug.h>
95
96static int pshot_log = 0;
97static int pshot_devctl_debug = 0;
98static int pshot_debug_busy = 0;
99
100static void *pshot_softstatep;
101
102static int pshot_prop_autoattach;
103
104#define MAXPWR 3
105
106
107/*
108 * device configuration data
109 */
110
111/* should keep in sync with current release */
112static struct {
113 char *name;
114 char *val;
115} pshot_nodetypes[] = {
116 {"DDI_NT_SERIAL", DDI_NT_SERIAL},
117 {"DDI_NT_SERIAL_MB", DDI_NT_SERIAL_MB},
118 {"DDI_NT_SERIAL_DO", DDI_NT_SERIAL_DO},
119 {"DDI_NT_SERIAL_MB_DO", DDI_NT_SERIAL_MB_DO},
120 {"DDI_NT_SERIAL_LOMCON", DDI_NT_SERIAL_LOMCON},
121 {"DDI_NT_BLOCK", DDI_NT_BLOCK},
122 {"DDI_NT_BLOCK_CHAN", DDI_NT_BLOCK_CHAN},
123 {"DDI_NT_BLOCK_WWN", DDI_NT_BLOCK_WWN},
jiang wu - Sun Microsystems - Beijing Chinad88e4982009-04-01 15:39:25 +0800124 {"DDI_NT_BLOCK_SAS", DDI_NT_BLOCK_SAS},
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700125 {"DDI_NT_CD", DDI_NT_CD},
126 {"DDI_NT_CD_CHAN", DDI_NT_CD_CHAN},
127 {"DDI_NT_FD", DDI_NT_FD},
128 {"DDI_NT_ENCLOSURE", DDI_NT_ENCLOSURE},
129 {"DDI_NT_SCSI_ENCLOSURE", DDI_NT_SCSI_ENCLOSURE},
130 {"DDI_NT_TAPE", DDI_NT_TAPE},
131 {"DDI_NT_NET", DDI_NT_NET},
132 {"DDI_NT_DISPLAY", DDI_NT_DISPLAY},
133 {"DDI_PSEUDO", DDI_PSEUDO},
134 {"DDI_NT_AUDIO", DDI_NT_AUDIO},
135 {"DDI_NT_MOUSE", DDI_NT_MOUSE},
136 {"DDI_NT_KEYBOARD", DDI_NT_KEYBOARD},
137 {"DDI_NT_PARALLEL", DDI_NT_PARALLEL},
138 {"DDI_NT_PRINTER", DDI_NT_PRINTER},
139 {"DDI_NT_UGEN", DDI_NT_UGEN},
140 {"DDI_NT_NEXUS", DDI_NT_NEXUS},
141 {"DDI_NT_SCSI_NEXUS", DDI_NT_SCSI_NEXUS},
142 {"DDI_NT_ATTACHMENT_POINT", DDI_NT_ATTACHMENT_POINT},
143 {"DDI_NT_SCSI_ATTACHMENT_POINT", DDI_NT_SCSI_ATTACHMENT_POINT},
144 {"DDI_NT_PCI_ATTACHMENT_POINT", DDI_NT_PCI_ATTACHMENT_POINT},
145 {"DDI_NT_SBD_ATTACHMENT_POINT", DDI_NT_SBD_ATTACHMENT_POINT},
146 {"DDI_NT_FC_ATTACHMENT_POINT", DDI_NT_FC_ATTACHMENT_POINT},
147 {"DDI_NT_USB_ATTACHMENT_POINT", DDI_NT_USB_ATTACHMENT_POINT},
148 {"DDI_NT_BLOCK_FABRIC", DDI_NT_BLOCK_FABRIC},
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700149 {"DDI_NT_AV_ASYNC", DDI_NT_AV_ASYNC},
150 {"DDI_NT_AV_ISOCH", DDI_NT_AV_ISOCH},
151 { NULL, NULL }
152};
153
154/* Node name */
155static char *pshot_compat_diskname = "cdisk";
156
157/* Compatible names... */
158static char *pshot_compat_psramdisks[] = {
159 "psramhead",
160 "psramrom",
161 "psramdisk",
162 "psramd",
163 "psramwhat"
164};
165
166/*
167 * devices "natively" supported by pshot (i.e. included with SUNWiotu)
168 * used to initialize pshot_devices with
169 */
170static pshot_device_t pshot_stock_devices[] = {
171 {"disk", DDI_NT_BLOCK, "gen_drv"},
172 {"disk_chan", DDI_NT_BLOCK_CHAN, "gen_drv"},
173 {"disk_wwn", DDI_NT_BLOCK_WWN, "gen_drv"},
174 {"disk_cdrom", DDI_NT_CD, "gen_drv"},
175 {"disk_cdrom.chan", DDI_NT_CD_CHAN, "gen_drv"},
176/* Note: use bad_drv to force attach errors */
177 {"disk_fd", DDI_NT_FD, "bad_drv"},
178 {"tape", DDI_NT_TAPE, "gen_drv"},
179 {"net", DDI_NT_NET, "gen_drv"},
180 {"display", DDI_NT_DISPLAY, "gen_drv"},
181 {"pseudo", DDI_PSEUDO, "gen_drv"},
182 {"audio", DDI_NT_AUDIO, "gen_drv"},
183 {"mouse", DDI_NT_MOUSE, "gen_drv"},
184 {"keyboard", DDI_NT_KEYBOARD, "gen_drv"},
185 {"nexus", DDI_NT_NEXUS, "pshot"}
186};
187#define PSHOT_N_STOCK_DEVICES \
188 (sizeof (pshot_stock_devices) / sizeof (pshot_device_t))
189
190static pshot_device_t *pshot_devices = NULL;
191static size_t pshot_devices_len = 0;
192
193/* protects <pshot_devices>, <pshot_devices_len> */
194static kmutex_t pshot_devices_lock;
195
196
197/*
198 * event testing
199 */
200
201static ndi_event_definition_t pshot_ndi_event_defs[] = {
202{ PSHOT_EVENT_TAG_OFFLINE, PSHOT_EVENT_NAME_DEV_OFFLINE,
203 EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
204
205{ PSHOT_EVENT_TAG_DEV_RESET, PSHOT_EVENT_NAME_DEV_RESET,
206 EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT },
207
208{ PSHOT_EVENT_TAG_BUS_RESET, PSHOT_EVENT_NAME_BUS_RESET,
209 EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
210
211{ PSHOT_EVENT_TAG_BUS_QUIESCE, PSHOT_EVENT_NAME_BUS_QUIESCE,
212 EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
213
214{ PSHOT_EVENT_TAG_BUS_UNQUIESCE, PSHOT_EVENT_NAME_BUS_UNQUIESCE,
215 EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
216
217{ PSHOT_EVENT_TAG_TEST_POST, PSHOT_EVENT_NAME_BUS_TEST_POST,
218 EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT }
219};
220
221
222#define PSHOT_N_NDI_EVENTS \
223 (sizeof (pshot_ndi_event_defs) / sizeof (ndi_event_definition_t))
224
225#ifdef DEBUG
226
227static ndi_event_definition_t pshot_test_events[] = {
228{ 10, "test event 0", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
229{ 11, "test event 1", EPL_KERNEL, NDI_EVENT_POST_TO_TGT },
230{ 12, "test event 2", EPL_INTERRUPT, NDI_EVENT_POST_TO_TGT },
231{ 13, "test event 3", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
232{ 14, "test event 4", EPL_KERNEL, NDI_EVENT_POST_TO_ALL},
233{ 15, "test event 5", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL },
234{ 16, "test event 6", EPL_KERNEL, NDI_EVENT_POST_TO_ALL },
235{ 17, "test event 7", EPL_INTERRUPT, NDI_EVENT_POST_TO_ALL }
236};
237
238static ndi_event_definition_t pshot_test_events_high[] = {
239{ 20, "test event high 0", EPL_HIGHLEVEL, NDI_EVENT_POST_TO_ALL}
240};
241
242#define PSHOT_N_TEST_EVENTS \
243 (sizeof (pshot_test_events)/sizeof (ndi_event_definition_t))
244#endif
245
246struct register_events {
247 char *event_name;
248 ddi_eventcookie_t event_cookie;
249 void (*event_callback)
250 (dev_info_t *,
251 ddi_eventcookie_t,
252 void *arg,
253 void *impldata);
254 ddi_callback_id_t cb_id;
255};
256
257struct register_events pshot_register_events[] = {
258{ PSHOT_EVENT_NAME_DEV_OFFLINE, 0, pshot_event_cb, 0 },
259{ PSHOT_EVENT_NAME_DEV_RESET, 0, pshot_event_cb, 0 },
260{ PSHOT_EVENT_NAME_BUS_RESET, 0, pshot_event_cb, 0 },
261{ PSHOT_EVENT_NAME_BUS_QUIESCE, 0, pshot_event_cb, 0 },
262{ PSHOT_EVENT_NAME_BUS_UNQUIESCE, 0, pshot_event_cb, 0 },
263{ PSHOT_EVENT_NAME_BUS_TEST_POST, 0, pshot_event_cb, 0 }
264};
265
266#define PSHOT_N_DDI_EVENTS \
267 (sizeof (pshot_register_events) / sizeof (struct register_events))
268
269
270#ifdef DEBUG
271
272static struct register_events pshot_register_test[] = {
273{ "test event 0", 0, pshot_event_cb_test, 0},
274{ "test event 1", 0, pshot_event_cb_test, 0},
275{ "test event 2", 0, pshot_event_cb_test, 0},
276{ "test event 3", 0, pshot_event_cb_test, 0},
277{ "test event 4", 0, pshot_event_cb_test, 0},
278{ "test event 5", 0, pshot_event_cb_test, 0},
279{ "test event 6", 0, pshot_event_cb_test, 0},
280{ "test event 7", 0, pshot_event_cb_test, 0}
281};
282
283
284static struct register_events pshot_register_high_test[] = {
285 {"test event high 0", 0, pshot_event_cb_test, 0}
286};
287
288#endif /* DEBUG */
289
290static struct {
291 int ioctl_int;
292 char *ioctl_char;
293} pshot_devctls[] = {
294 {DEVCTL_DEVICE_GETSTATE, "DEVCTL_DEVICE_GETSTATE"},
295 {DEVCTL_DEVICE_ONLINE, "DEVCTL_DEVICE_ONLINE"},
296 {DEVCTL_DEVICE_OFFLINE, "DEVCTL_DEVICE_OFFLINE"},
297 {DEVCTL_DEVICE_REMOVE, "DEVCTL_DEVICE_REMOVE"},
298 {DEVCTL_BUS_GETSTATE, "DEVCTL_BUS_GETSTATE"},
299 {DEVCTL_BUS_DEV_CREATE, "DEVCTL_BUS_DEV_CREATE"},
300 {DEVCTL_BUS_RESET, "DEVCTL_BUS_RESET"},
301 {DEVCTL_BUS_RESETALL, "DEVCTL_BUS_RESETALL"},
302 {0, NULL}
303};
304
305static struct bus_ops pshot_bus_ops = {
306 BUSO_REV, /* busops_rev */
307 nullbusmap, /* bus_map */
308 NULL, /* bus_get_intrspec */
309 NULL, /* bus_add_interspec */
310 NULL, /* bus_remove_interspec */
311 i_ddi_map_fault, /* bus_map_fault */
Garrett D'Amorecd21e7c2012-03-11 22:00:47 -0700312 NULL, /* bus_dma_map */
cthb1dd9582005-06-27 15:47:48 -0700313 ddi_dma_allochdl, /* bus_dma_allochdl */
314 ddi_dma_freehdl, /* bus_dma_freehdl */
315 ddi_dma_bindhdl, /* bus_dma_bindhdl */
316 ddi_dma_unbindhdl, /* bus_dma_unbindhdl */
317 ddi_dma_flush, /* bus_dma_flush */
318 ddi_dma_win, /* bus_dma_win */
319 ddi_dma_mctl, /* bus_dma_ctl */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700320 pshot_ctl, /* bus_ctl */
321 ddi_bus_prop_op, /* bus_prop_op */
322 pshot_get_eventcookie, /* bus_get_eventcookie */
323 pshot_add_eventcall, /* bus_add_eventcall */
324 pshot_remove_eventcall, /* bus_remove_event */
325 pshot_post_event, /* bus_post_event */
326 NULL, /* bus_intr_ctl */
327 pshot_bus_config, /* bus_config */
328 pshot_bus_unconfig, /* bus_unconfig */
329 NULL, /* bus_fm_init */
330 NULL, /* bus_fm_fini */
331 NULL, /* bus_fm_access_enter */
332 NULL, /* bus_fm_access_exit */
333 pshot_bus_power, /* bus_power */
334 pshot_bus_introp /* bus_intr_op */
335};
336
337static struct cb_ops pshot_cb_ops = {
338 pshot_open, /* open */
339 pshot_close, /* close */
340 nodev, /* strategy */
341 nodev, /* print */
342 nodev, /* dump */
343 nodev, /* read */
344 nodev, /* write */
345 pshot_ioctl, /* ioctl */
346 nodev, /* devmap */
347 nodev, /* mmap */
348 nodev, /* segmap */
349 nochpoll, /* poll */
350 ddi_prop_op, /* prop_op */
351 NULL, /* streamtab */
352 D_NEW | D_MP | D_HOTPLUG, /* flags */
353 CB_REV, /* cb_rev */
354 nodev, /* aread */
355 nodev, /* awrite */
356};
357
358static struct dev_ops pshot_ops = {
359 DEVO_REV, /* devo_rev, */
360 0, /* refcnt */
361 pshot_info, /* getinfo */
362 nulldev, /* identify */
363 pshot_probe, /* probe */
364 pshot_attach, /* attach */
365 pshot_detach, /* detach */
366 nodev, /* reset */
367 &pshot_cb_ops, /* driver operations */
368 &pshot_bus_ops, /* bus operations */
Sherry Moore19397402008-09-22 16:30:26 -0700369 pshot_power, /* power */
370 ddi_quiesce_not_supported, /* devo_quiesce */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700371
372};
373
374
375/*
376 * Module linkage information for the kernel.
377 */
378static struct modldrv modldrv = {
379 &mod_driverops,
Sherry Moore19397402008-09-22 16:30:26 -0700380 "pshotnex",
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700381 &pshot_ops,
382};
383
384static struct modlinkage modlinkage = {
385 MODREV_1, &modldrv, NULL
386};
387
388
389/*
390 * pshot_devices is set up on the first attach and destroyed on fini
391 *
392 * therefore PSHOT_PROP_DEV* properties may be set just for the root device,
393 * instead of being set globably, in pshot.conf by specifying the properties
394 * on a single line in the form:
395 * name="pshot" parent="/" <dev props ..>
396 * to unclutter a device tree snapshot.
397 * this of course produces a long single line that may wrap around several
398 * times on screen
399 */
400
401int
402_init(void)
403{
404 int rv;
405
406 rv = ddi_soft_state_init(&pshot_softstatep, sizeof (pshot_t), 0);
407
408 if (rv != DDI_SUCCESS)
409 return (rv);
410
411 mutex_init(&pshot_devices_lock, NULL, MUTEX_DRIVER, NULL);
412 pshot_devices = NULL;
413 pshot_devices_len = 0;
414
415 if ((rv = mod_install(&modlinkage)) != 0) {
416 ddi_soft_state_fini(&pshot_softstatep);
417 mutex_destroy(&pshot_devices_lock);
418 }
419 return (rv);
420}
421
422int
423_fini(void)
424{
425 int rv;
426
427 if ((rv = mod_remove(&modlinkage)) != 0)
428 return (rv);
429
430 ddi_soft_state_fini(&pshot_softstatep);
431 mutex_destroy(&pshot_devices_lock);
432 if (pshot_devices)
433 pshot_devices_free(pshot_devices, pshot_devices_len);
434 return (0);
435}
436
437int
438_info(struct modinfo *modinfop)
439{
440 return (mod_info(&modlinkage, modinfop));
441}
442
443
444/*ARGSUSED*/
445static int
446pshot_probe(dev_info_t *devi)
447{
448 int instance = ddi_get_instance(devi);
449 char *bus_addr;
450
451 /*
452 * Hook for tests to force probe fail
453 */
454 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "bus-addr",
455 &bus_addr) == DDI_PROP_SUCCESS) {
456 if (strncmp(bus_addr, "failprobe", 9) == 0) {
457 if (pshot_debug)
458 cmn_err(CE_CONT, "pshot%d: "
459 "%s forced probe failure\n",
460 instance, bus_addr);
461 ddi_prop_free(bus_addr);
462 return (DDI_PROBE_FAILURE);
463 }
464 ddi_prop_free(bus_addr);
465 }
466
467 return (DDI_PROBE_SUCCESS);
468}
469
470
471/*ARGSUSED*/
472static int
473pshot_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
474{
475 int instance;
476 minor_t minor;
477 pshot_t *pshot;
478
479 minor = getminor((dev_t)arg);
480 instance = pshot_minor_decode_inst(minor);
481 switch (infocmd) {
482 case DDI_INFO_DEVT2DEVINFO:
483 pshot = ddi_get_soft_state(pshot_softstatep, instance);
484 if (pshot == NULL) {
485 cmn_err(CE_WARN, "pshot_info: get soft state failed "
486 "on minor %u, instance %d", minor, instance);
487 return (DDI_FAILURE);
488 }
489 *result = (void *)pshot->dip;
490 break;
491 case DDI_INFO_DEVT2INSTANCE:
492 *result = (void *)(uintptr_t)instance;
493 break;
494 default:
495 cmn_err(CE_WARN, "pshot_info: unrecognized cmd 0x%x on "
496 "minor %u, instance %d", infocmd, minor, instance);
497 return (DDI_FAILURE);
498 }
499
500 return (DDI_SUCCESS);
501}
502
503
504static int
505pshot_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
506{
507 int instance = ddi_get_instance(devi);
508 pshot_t *pshot;
509 int rval, i;
510 int prop_flags = DDI_PROP_DONTPASS | DDI_PROP_NOTPROM;
511 char *bus_addr;
512 char *pm_comp[] = {
513 "NAME=bus",
514 "0=B3",
515 "1=B2",
516 "2=B1",
517 "3=B0"};
518 char *pm_hw_state = {"needs-suspend-resume"};
519
520 pshot_prop_autoattach = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
521 prop_flags, "autoattach", 0);
522
523 switch (cmd) {
524
525 case DDI_ATTACH:
526 if (pshot_debug)
527 cmn_err(CE_CONT, "attach: %s%d/pshot%d\n",
Sherry Moore19397402008-09-22 16:30:26 -0700528 ddi_get_name(ddi_get_parent(devi)),
529 ddi_get_instance(ddi_get_parent(devi)),
530 instance);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700531
532 /*
533 * Hook for tests to force attach fail
534 */
535 if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "bus-addr",
536 &bus_addr) == DDI_PROP_SUCCESS) && bus_addr != NULL) {
537 if (strncmp(bus_addr, "failattach", 10) == 0) {
538 if (pshot_debug)
539 cmn_err(CE_CONT, "pshot%d: "
540 "%s forced attach failure\n",
541 instance, bus_addr);
542 ddi_prop_free(bus_addr);
543 return (DDI_FAILURE);
544 }
545 ddi_prop_free(bus_addr);
546 }
547
548 /*
549 * minor nodes setup
550 */
551 if (ddi_soft_state_zalloc(pshot_softstatep, instance) !=
552 DDI_SUCCESS) {
553 return (DDI_FAILURE);
554 }
555 pshot = ddi_get_soft_state(pshot_softstatep, instance);
556 pshot->dip = devi;
557 pshot->instance = instance;
558 mutex_init(&pshot->lock, NULL, MUTEX_DRIVER, NULL);
559
560 /* set each minor, then create on dip all together */
561
562 i = PSHOT_NODENUM_DEVCTL;
563 pshot->nodes[i].pshot = pshot;
564 pshot->nodes[i].minor = pshot_minor_encode(instance, i);
565 (void) strncpy(pshot->nodes[i].name, PSHOT_NODENAME_DEVCTL,
566 PSHOT_MAX_MINOR_NAMELEN);
567
568 i = PSHOT_NODENUM_TESTCTL;
569 pshot->nodes[i].pshot = pshot;
570 pshot->nodes[i].minor = pshot_minor_encode(instance, i);
571 (void) strncpy(pshot->nodes[i].name, PSHOT_NODENAME_TESTCTL,
572 PSHOT_MAX_MINOR_NAMELEN);
573
574 /* this assumes contiguous a filling */
575 for (i = 0; i <= PSHOT_MAX_NODENUM; i++) {
576 if (ddi_create_minor_node(devi, pshot->nodes[i].name,
577 S_IFCHR, pshot->nodes[i].minor, DDI_NT_NEXUS, 0) !=
578 DDI_SUCCESS) {
579 cmn_err(CE_WARN, "attach: cannot create "
580 "minor %s", pshot->nodes[i].name);
581 goto FAIL_ATTACH;
582 }
583 }
584
585 /*
586 * pshot_devices setup
587 */
588 if (pshot_devices_setup(devi)) {
589 cmn_err(CE_WARN, "attach: pshot devices setup "
590 "failed");
591 goto FAIL_ATTACH;
592 }
593
594 /*
595 * events setup
596 */
597 for (i = 0; i < PSHOT_N_DDI_EVENTS; i++) {
598 rval = ddi_get_eventcookie(devi,
599 pshot_register_events[i].event_name,
600 &pshot_register_events[i].event_cookie);
601
602 if (pshot_debug)
603 cmn_err(CE_CONT, "pshot%d: event=%s:"
604 "ddi_get_eventcookie rval=%d\n",
605 instance,
606 pshot_register_events[i].event_name, rval);
607
608 if (rval == DDI_SUCCESS) {
609 rval = ddi_add_event_handler(devi,
610 pshot_register_events[i].event_cookie,
611 pshot_register_events[i].event_callback,
612 (void *)pshot,
613 &pshot->callback_cache[i]);
614
615 if (pshot_debug)
616 cmn_err(CE_CONT, "pshot%d: event=%s: "
617 "ddi_add_event_handler rval=%d\n",
618 instance,
619 pshot_register_events[i].event_name,
620 rval);
621 }
622 }
623
624#ifdef DEBUG
625 if (pshot_event_test_enable) {
626 pshot_event_test((void *)pshot);
627 (void) timeout(pshot_event_test_post_one, (void *)pshot,
628 instance * drv_usectohz(60000000));
629 }
630#endif
631
632 /*
633 * allocate an ndi event handle
634 */
635 if (ndi_event_alloc_hdl(devi, NULL, &pshot->ndi_event_hdl,
636 NDI_SLEEP) != NDI_SUCCESS) {
637 goto FAIL_ATTACH;
638 }
639
640 pshot->ndi_events.ndi_events_version = NDI_EVENTS_REV1;
641 pshot->ndi_events.ndi_n_events = PSHOT_N_NDI_EVENTS;
642 pshot->ndi_events.ndi_event_defs = pshot_ndi_event_defs;
643
644 if (ndi_event_bind_set(pshot->ndi_event_hdl, &pshot->ndi_events,
645 NDI_SLEEP) != NDI_SUCCESS) {
646 cmn_err(CE_CONT, "pshot%d bind set failed\n",
Sherry Moore19397402008-09-22 16:30:26 -0700647 instance);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700648 }
649
650 /*
651 * setup a test for nexus auto-attach iff we are
652 * a second level pshot node (parent == /SUNW,pshot)
653 * enable by setting "autoattach=1" in pshot.conf
654 */
655 if ((PARENT_IS_PSHOT(devi)) && (pshot_prop_autoattach != 0) &&
656 (ddi_get_instance(ddi_get_parent(devi))) == 0)
657 pshot_setup_autoattach(devi);
658
659 /*
660 * initialize internal state to idle: busy = 0,
661 * power level = -1
662 */
663 mutex_enter(&pshot->lock);
664 pshot->busy = 0;
665 pshot->busy_ioctl = 0;
666 pshot->level = -1;
667 pshot->state &= ~STRICT_PARENT;
668 pshot->state |= PM_SUPPORTED;
669 mutex_exit(&pshot->lock);
670
671 /*
672 * Create the "pm-want-child-notification?" property
673 * for the root node /devices/pshot
674 */
675 if (instance == 0) {
676 if (pshot_debug) {
677 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:\n\t"
678 " create the"
679 " \"pm-want-child-notification?\" property"
680 " for the root node\n", instance);
681 }
682 if (ddi_prop_create(DDI_DEV_T_NONE, devi, 0,
683 "pm-want-child-notification?", NULL, 0)
684 != DDI_PROP_SUCCESS) {
685 cmn_err(CE_WARN, "%s%d:\n\t"
686 " unable to create the"
687 " \"pm-want-child-notification?\""
688 " property", ddi_get_name(devi),
689 ddi_get_instance(devi));
690
691 goto FAIL_ATTACH;
692 }
693 }
694
695 /*
696 * Check if the pm-want-child-notification? property was
697 * created in pshot_bus_config_setup_nexus() by the parent.
698 * Set the STRICT_PARENT flag if not.
699 */
700 if (ddi_prop_exists(DDI_DEV_T_ANY, devi,
701 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
702 "pm-want-child-notification?") != 1) {
703 if (pshot_debug) {
704 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
705 " STRICT PARENT\n", instance);
706 }
707 mutex_enter(&pshot->lock);
708 pshot->state |= STRICT_PARENT;
709 mutex_exit(&pshot->lock);
710 } else {
711 if (pshot_debug) {
712 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
713 " INVOLVED PARENT\n", instance);
714 }
715 mutex_enter(&pshot->lock);
716 pshot->state &= ~STRICT_PARENT;
717 mutex_exit(&pshot->lock);
718 }
719
720 /*
721 * create the pm-components property: one component
722 * with 4 power levels.
723 * - skip for pshot@XXX,nopm and pshot@XXX,nopm_strict:
724 * "no-pm-components" property
725 */
726 if (ddi_prop_exists(DDI_DEV_T_ANY, devi,
727 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
728 "no-pm-components") == 0) {
729 if (pshot_debug) {
730 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
731 " create the \"pm_components\" property\n",
732 instance);
733 }
734 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
735 "pm-components", pm_comp, 5) != DDI_PROP_SUCCESS) {
736 cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t"
737 " unable to create the \"pm-components\""
738 " property", ddi_get_name(devi),
739 ddi_get_instance(devi));
740
741 goto FAIL_ATTACH;
742 }
743 } else {
744 if (pshot_debug) {
745 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
746 " NO-PM_COMPONENTS PARENT\n", instance);
747 }
748 mutex_enter(&pshot->lock);
749 pshot->state &= ~PM_SUPPORTED;
750 mutex_exit(&pshot->lock);
751 }
752
753 /*
754 * create the property needed to get DDI_SUSPEND
755 * and DDI_RESUME calls
756 */
757 if (pshot_debug) {
758 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
759 " create pm-hardware-state property\n",
760 instance);
761 }
762 if (ddi_prop_update_string(DDI_DEV_T_NONE, devi,
763 "pm-hardware-state", pm_hw_state) != DDI_PROP_SUCCESS) {
764 cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t"
765 " unable to create the \"pm-hardware-state\""
766 " property", ddi_get_name(devi),
767 ddi_get_instance(devi));
768
769 goto FAIL_ATTACH;
770 }
771
772 /*
773 * set power level to max via pm_raise_power(),
774 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
775 */
776 if (pshot->state & PM_SUPPORTED) {
777 if (pshot_debug) {
778 cmn_err(CE_CONT, "pshot%d: DDI_ATTACH:"
779 " raise power to MAXPWR\n", instance);
780 }
781 if (pm_raise_power(pshot->dip, 0, MAXPWR) !=
782 DDI_SUCCESS) {
783 cmn_err(CE_WARN, "%s%d: DDI_ATTACH:"
784 " pm_raise_power failed",
785 ddi_get_name(devi),
786 ddi_get_instance(devi));
787
788 goto FAIL_ATTACH;
789
790 }
791 }
792
793 if (pshot_log)
794 cmn_err(CE_CONT, "pshot%d attached\n", instance);
795 ddi_report_dev(devi);
796
797 return (DDI_SUCCESS);
798 /*NOTREACHED*/
799FAIL_ATTACH:
800 ddi_remove_minor_node(devi, NULL);
801 mutex_destroy(&pshot->lock);
802 ddi_soft_state_free(pshot_softstatep, instance);
803 return (DDI_FAILURE);
804
805 case DDI_RESUME:
806 if (pshot_debug) {
807 cmn_err(CE_CONT, "pshot%d: DDI_RESUME: resuming\n",
808 instance);
809 }
810 pshot = ddi_get_soft_state(pshot_softstatep, instance);
811
812 /*
813 * set power level to max via pm_raise_power(),
814 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
815 */
816 if (pshot->state & PM_SUPPORTED) {
817 if (pshot_debug) {
818 cmn_err(CE_CONT, "pshot%d: DDI_RESUME:"
819 " raise power to MAXPWR\n", instance);
820 }
821 if (pm_raise_power(pshot->dip, 0, MAXPWR) !=
822 DDI_SUCCESS) {
823 cmn_err(CE_WARN, "%s%d: DDI_RESUME:"
824 " pm_raise_power failed",
825 ddi_get_name(devi),
826 ddi_get_instance(devi));
827 }
828 }
829
830 if (pshot_debug) {
831 cmn_err(CE_CONT, "pshot%d: DDI_RESUME: resumed\n",
832 instance);
833 }
834 return (DDI_SUCCESS);
835
836 default:
837 return (DDI_FAILURE);
838 }
839}
840
841static int
842pshot_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
843{
844 int instance = ddi_get_instance(devi);
845 int i, rval;
846 pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
847 int level_tmp;
848
849 if (pshot == NULL)
850 return (DDI_FAILURE);
851
852 switch (cmd) {
853
854 case DDI_DETACH:
855 if (pshot_debug)
856 cmn_err(CE_CONT, "pshot%d: DDI_DETACH\n", instance);
857 /*
858 * power off component 0
859 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
860 */
861 if (pshot->state & PM_SUPPORTED) {
862 if (pshot_debug) {
863 cmn_err(CE_CONT, "pshot%d: DDI_DETACH:"
864 " power off\n", instance);
865 }
866 if (pm_lower_power(pshot->dip, 0, 0) != DDI_SUCCESS) {
867 cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
868 "pm_lower_power failed for comp 0 to"
869 " level 0", ddi_get_name(devi),
870 ddi_get_instance(devi));
871
872 return (DDI_FAILURE);
873 }
874
875 /*
876 * Check if the power level is actually OFF.
877 * Issue pm_power_has_changed if not.
878 */
879 mutex_enter(&pshot->lock);
880 if (pshot->level != 0) {
881 if (pshot_debug) {
882 cmn_err(CE_NOTE, "pshot%d:"
883 " DDI_DETACH: power off via"
884 " pm_power_has_changed instead\n",
885 instance);
886 }
887 level_tmp = pshot->level;
888 pshot->level = 0;
889 if (pm_power_has_changed(pshot->dip, 0, 0) !=
890 DDI_SUCCESS) {
891 if (pshot_debug) {
892 cmn_err(CE_NOTE, "pshot%d:"
893 " DDI_DETACH:"
894 " pm_power_has_changed"
895 " failed\n", instance);
896 }
897 pshot->level = level_tmp;
898 mutex_exit(&pshot->lock);
899
900 return (DDI_FAILURE);
901 }
902 }
903 mutex_exit(&pshot->lock);
904 }
905
906 for (i = 0; i < PSHOT_N_DDI_EVENTS; i++) {
907 if (pshot->callback_cache[i] != NULL) {
908 rval = ddi_remove_event_handler(
909 pshot->callback_cache[i]);
910 ASSERT(rval == DDI_SUCCESS);
911 }
912 }
913
914#ifdef DEBUG
915 for (i = 0; i < PSHOT_N_TEST_EVENTS; i++) {
916 if (pshot->test_callback_cache[i] != NULL) {
917 rval = ddi_remove_event_handler(
918 pshot->test_callback_cache[i]);
919 ASSERT(rval == DDI_SUCCESS);
920 }
921 }
922#endif
923 rval = ndi_event_free_hdl(pshot->ndi_event_hdl);
924 ASSERT(rval == DDI_SUCCESS);
925
926 if (pshot_log)
927 cmn_err(CE_CONT, "pshot%d detached\n", instance);
928
929 ddi_remove_minor_node(devi, NULL);
930 mutex_destroy(&pshot->lock);
931 ddi_soft_state_free(pshot_softstatep, instance);
932 break;
933
934 case DDI_SUSPEND:
935 if (pshot_debug)
936 cmn_err(CE_CONT, "pshot%d: DDI_SUSPEND\n", instance);
937 /*
938 * fail the suspend if FAIL_SUSPEND_FLAG is set.
939 * clear the FAIL_SUSPEND_FLAG flag
940 */
941 mutex_enter(&pshot->lock);
942 if (pshot->state & FAIL_SUSPEND_FLAG) {
943 if (pshot_debug) {
944 cmn_err(CE_CONT, "pshot%d:"
Sherry Moore19397402008-09-22 16:30:26 -0700945 " FAIL_SUSPEND_FLAG set, fail suspend\n",
946 ddi_get_instance(devi));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700947 }
948 pshot->state &= ~FAIL_SUSPEND_FLAG;
949 rval = DDI_FAILURE;
950 } else {
951 rval = DDI_SUCCESS;
952 }
953 mutex_exit(&pshot->lock);
954
955 /*
956 * power OFF via pm_power_has_changed
957 */
958 mutex_enter(&pshot->lock);
959 if (pshot->state & PM_SUPPORTED) {
960 if (pshot_debug) {
961 cmn_err(CE_CONT, "pshot%d: DDI_SUSPEND:"
962 " power off via pm_power_has_changed\n",
963 instance);
964 }
965 level_tmp = pshot->level;
966 pshot->level = 0;
967 if (pm_power_has_changed(pshot->dip, 0, 0) !=
968 DDI_SUCCESS) {
969 if (pshot_debug) {
970 cmn_err(CE_NOTE, "pshot%d:"
971 " DDI_SUSPEND:"
972 " pm_power_has_changed failed\n",
973 instance);
974 }
975 pshot->level = level_tmp;
976 rval = DDI_FAILURE;
977 }
978 }
979 mutex_exit(&pshot->lock);
980 return (rval);
981
982 default:
983 break;
984 }
985
986 return (DDI_SUCCESS);
987}
988
989
990/*
991 * returns number of bits to represent <val>
992 */
993static size_t
994pshot_numbits(size_t val)
995{
996 size_t bitcnt;
997
998 if (val == 0)
999 return (0);
1000 for (bitcnt = 1; 1 << bitcnt < val; bitcnt++)
1001 ;
1002 return (bitcnt);
1003}
1004
1005/*
1006 * returns a minor number encoded with instance <inst> and an index <nodenum>
1007 * that identifies the minor node for this instance
1008 */
1009static minor_t
1010pshot_minor_encode(int inst, minor_t nodenum)
1011{
1012 return (((minor_t)inst << PSHOT_NODENUM_BITS()) |
1013 (((1 << PSHOT_NODENUM_BITS()) - 1) & nodenum));
1014}
1015
1016/*
1017 * returns instance of <minor>
1018 */
1019static int
1020pshot_minor_decode_inst(minor_t minor)
1021{
1022 return (minor >> PSHOT_NODENUM_BITS());
1023}
1024
1025/*
1026 * returns node number indexing a minor node for the instance in <minor>
1027 */
1028static minor_t
1029pshot_minor_decode_nodenum(minor_t minor)
1030{
1031 return (minor & ((1 << PSHOT_NODENUM_BITS()) - 1));
1032}
1033
1034
1035/*
1036 * pshot_bus_introp: pshot convert an interrupt number to an
1037 * interrupt. NO OP for pseudo drivers.
1038 */
1039/*ARGSUSED*/
1040static int
1041pshot_bus_introp(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1042 ddi_intr_handle_impl_t *hdlp, void *result)
1043{
1044 return (DDI_FAILURE);
1045}
1046static int
1047pshot_ctl(dev_info_t *dip, dev_info_t *rdip,
1048 ddi_ctl_enum_t ctlop, void *arg, void *result)
1049{
1050 int instance;
1051 pshot_t *pshot;
1052 char *childname;
1053 int childinstance;
1054 char *name;
1055 int circ;
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:
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001108 case DDI_CTLOPS_SIDDEV:
1109 case DDI_CTLOPS_SLAVEONLY:
1110 case DDI_CTLOPS_AFFINITY:
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001111 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",
Sherry Moore19397402008-09-22 16:30:26 -07001118 ddi_get_name(dip), ddi_get_instance(dip),
1119 ctlop, ddi_get_name(rdip), ddi_get_instance(rdip));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001120 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, &circ);
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, circ);
1195
1196 return (rval);
1197 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001198 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, &circ);
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, circ);
1297
1298 return (rval);
1299 }
1300
cth5e3986c2006-06-08 16:03:08 -07001301 case DDI_CTLOPS_BTOP:
1302 case DDI_CTLOPS_BTOPR:
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001303 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*/
1319static int
1320pshot_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 circ;
1326 int rv;
1327
1328 pshot = ddi_get_soft_state(pshot_softstatep, instance);
1329 if (pshot == NULL) {
1330
1331 return (DDI_FAILURE);
1332 }
1333
1334 ndi_devi_enter(dip, &circ);
1335
1336 /*
1337 * set POWER_FLAG when power() is called.
1338 * ioctl(DEVCT_PM_POWER) is a clear on read call.
1339 */
1340 mutex_enter(&pshot->lock);
1341 pshot->state |= POWER_FLAG;
1342 /*
1343 * refuse to power OFF if the component is busy
1344 */
1345 if (pshot->busy != 0 && pshot->level > level) {
1346 cmn_err(CE_WARN, "%s%d: power: REFUSING POWER LEVEL CHANGE"
1347 " (%d->%d), DEVICE NOT IDLE: busy = %d",
1348 name, instance, pshot->level, level, pshot->busy);
1349 rv = DDI_FAILURE;
1350 } else {
1351 if (pshot_debug) {
1352 cmn_err(CE_CONT, "%s%d: power: comp %d (%d->%d)\n",
1353 name, instance, cmpt, pshot->level, level);
1354 }
1355 pshot->level = level;
1356 rv = DDI_SUCCESS;
1357 }
1358 mutex_exit(&pshot->lock);
1359
1360 ndi_devi_exit(dip, circ);
1361
1362 return (rv);
1363}
1364
1365/*ARGSUSED0*/
1366static int
1367pshot_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1368 void *arg, void *result)
1369
1370{
1371 int ret;
1372 int instance = ddi_get_instance(dip);
1373 char *name = ddi_node_name(dip);
1374 pshot_t *pshot;
1375 pm_bp_child_pwrchg_t *bpc;
1376 pm_bp_nexus_pwrup_t bpn;
1377 pm_bp_has_changed_t *bphc;
1378 int pwrup_res;
1379 int ret_failed = 0;
1380 int pwrup_res_failed = 0;
1381
1382 pshot = ddi_get_soft_state(pshot_softstatep, instance);
1383 if (pshot == NULL) {
1384
1385 return (DDI_FAILURE);
1386 }
1387
1388 switch (op) {
1389 case BUS_POWER_PRE_NOTIFICATION:
1390 bpc = (pm_bp_child_pwrchg_t *)arg;
1391 if (pshot_debug) {
1392 cmn_err(CE_CONT, "%s%d: pre_bus_power:"
1393 " %s%d comp %d (%d->%d)\n",
1394 name, instance, ddi_node_name(bpc->bpc_dip),
1395 ddi_get_instance(bpc->bpc_dip),
1396 bpc->bpc_comp, bpc->bpc_olevel,
1397 bpc->bpc_nlevel);
1398 }
1399
1400 /*
1401 * mark parent busy if old_level is either -1 or 0,
1402 * and new level is == MAXPWR
1403 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1404 */
1405 if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == MAXPWR &&
1406 bpc->bpc_olevel <= 0) && (pshot->state & PM_SUPPORTED)) {
1407 mutex_enter(&pshot->lock);
1408 ++(pshot->busy);
1409 if (pshot_debug_busy) {
1410 cmn_err(CE_CONT,
1411 "%s%d: pre_bus_power:"
1412 " busy parent for %s%d (%d->%d): "
1413 " busy = %d\n",
1414 name, instance,
1415 ddi_node_name(bpc->bpc_dip),
1416 ddi_get_instance(bpc->bpc_dip),
1417 bpc->bpc_olevel, bpc->bpc_nlevel,
1418 pshot->busy);
1419 }
1420 mutex_exit(&pshot->lock);
1421 ret = pm_busy_component(dip, 0);
1422 ASSERT(ret == DDI_SUCCESS);
1423 }
1424
1425 /*
1426 * if new_level > 0, power up parent, if not already at
1427 * MAXPWR, via pm_busop_bus_power
1428 * - skip for the no-pm nexus (pshot@XXX,nopm)
1429 */
1430 if (bpc->bpc_comp == 0 && bpc->bpc_nlevel > 0 &&
1431 pshot->level < MAXPWR && (pshot->state & PM_SUPPORTED)) {
1432 /*
1433 * stuff the bpn struct
1434 */
1435 bpn.bpn_comp = 0;
1436 bpn.bpn_level = MAXPWR;
1437 bpn.bpn_private = bpc->bpc_private;
1438 bpn.bpn_dip = dip;
1439
1440 /*
1441 * ask pm to power parent up
1442 */
1443 if (pshot_debug) {
1444 cmn_err(CE_CONT, "%s%d: pre_bus_power:"
1445 " pm_busop_bus_power on parent for %s%d"
1446 " (%d->%d): enter", name, instance,
1447 ddi_node_name(bpc->bpc_dip),
1448 ddi_get_instance(bpc->bpc_dip),
1449 bpc->bpc_olevel, bpc->bpc_nlevel);
1450 }
1451 ret = pm_busop_bus_power(dip, impl_arg,
1452 BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
1453 (void *)&pwrup_res);
1454
1455 /*
1456 * check the return status individually,
1457 * idle parent and exit if either failed.
1458 */
1459 if (ret != DDI_SUCCESS) {
1460 cmn_err(CE_WARN,
1461 "%s%d: pre_bus_power:"
1462 " pm_busop_bus_power FAILED (ret) FOR"
1463 " %s%d (%d->%d)",
1464 name, instance,
1465 ddi_node_name(bpc->bpc_dip),
1466 ddi_get_instance(bpc->bpc_dip),
1467 bpc->bpc_olevel, bpc->bpc_nlevel);
1468 ret_failed = 1;
1469 }
1470 if (pwrup_res != DDI_SUCCESS) {
1471 cmn_err(CE_WARN,
1472 "%s%d: pre_bus_power:"
1473 " pm_busop_bus_power FAILED (pwrup_res)"
1474 " FOR %s%d (%d->%d)",
1475 name, instance,
1476 ddi_node_name(bpc->bpc_dip),
1477 ddi_get_instance(bpc->bpc_dip),
1478 bpc->bpc_olevel, bpc->bpc_nlevel);
1479 pwrup_res_failed = 1;
1480 }
1481 if (ret_failed || pwrup_res_failed) {
1482 /*
1483 * decrement the busy count if it
1484 * had been incremented.
1485 */
1486 if ((bpc->bpc_comp == 0 &&
1487 bpc->bpc_nlevel == MAXPWR &&
1488 bpc->bpc_olevel <= 0) &&
1489 (pshot->state & PM_SUPPORTED)) {
1490 mutex_enter(&pshot->lock);
1491 ASSERT(pshot->busy > 0);
1492 --(pshot->busy);
1493 if (pshot_debug_busy) {
1494 cmn_err(CE_CONT, "%s%d:"
1495 " pm_busop_bus_power"
1496 " failed: idle parent for"
1497 " %s%d (%d->%d):"
1498 " busy = %d\n",
1499 name, instance,
1500 ddi_node_name(
1501 bpc->bpc_dip),
1502 ddi_get_instance(
1503 bpc->bpc_dip),
1504 bpc->bpc_olevel,
1505 bpc->bpc_nlevel,
1506 pshot->busy);
1507 }
1508 mutex_exit(&pshot->lock);
1509 ret = pm_idle_component(dip, 0);
1510 ASSERT(ret == DDI_SUCCESS);
1511 }
1512 return (DDI_FAILURE);
1513
1514 } else {
1515 if (pshot_debug) {
1516 cmn_err(CE_CONT,
1517 "%s%d: pre_bus_power:"
1518 " pm_busop_bus_power on parent"
1519 " for %s%d (%d->%d)\n",
1520 name, instance,
1521 ddi_node_name(bpc->bpc_dip),
1522 ddi_get_instance(bpc->bpc_dip),
1523 bpc->bpc_olevel, bpc->bpc_nlevel);
1524 }
1525 }
1526 }
1527 break;
1528
1529 case BUS_POWER_POST_NOTIFICATION:
1530 bpc = (pm_bp_child_pwrchg_t *)arg;
1531 if (pshot_debug) {
1532 cmn_err(CE_CONT, "%s%d: post_bus_power:"
1533 " %s%d comp %d (%d->%d) result %d\n",
1534 name, instance, ddi_node_name(bpc->bpc_dip),
1535 ddi_get_instance(bpc->bpc_dip),
1536 bpc->bpc_comp, bpc->bpc_olevel,
1537 bpc->bpc_nlevel, *(int *)result);
1538 }
1539
1540 /*
1541 * handle pm_busop_bus_power() failure case.
1542 * mark parent idle if had been marked busy.
1543 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1544 */
1545 if (*(int *)result != DDI_SUCCESS) {
1546 cmn_err(CE_WARN,
1547 "pshot%d: post_bus_power_failed:"
1548 " pm_busop_bus_power FAILED FOR %s%d (%d->%d)",
1549 instance, ddi_node_name(bpc->bpc_dip),
1550 ddi_get_instance(bpc->bpc_dip),
1551 bpc->bpc_olevel, bpc->bpc_nlevel);
1552
1553 if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == MAXPWR &&
1554 bpc->bpc_olevel <= 0) &&
1555 (pshot->state & PM_SUPPORTED)) {
1556 mutex_enter(&pshot->lock);
1557 ASSERT(pshot->busy > 0);
1558 --(pshot->busy);
1559 if (pshot_debug_busy) {
1560 cmn_err(CE_CONT, "%s%d:"
1561 " post_bus_power_failed:"
1562 " idle parent for %s%d"
1563 " (%d->%d): busy = %d\n",
1564 name, instance,
1565 ddi_node_name(bpc->bpc_dip),
1566 ddi_get_instance(bpc->bpc_dip),
1567 bpc->bpc_olevel, bpc->bpc_nlevel,
1568 pshot->busy);
1569 }
1570 mutex_exit(&pshot->lock);
1571 ret = pm_idle_component(dip, 0);
1572 ASSERT(ret == DDI_SUCCESS);
1573 }
1574 }
1575
1576 /*
1577 * Mark nexus idle when a child's comp 0
1578 * is set to level 0 from level 1, 2, or 3 only.
1579 * And only if result arg == DDI_SUCCESS.
1580 * This will leave the parent busy when the child
1581 * does not call pm_lower_power() on detach after
1582 * unsetting the NO_LOWER_POWER flag.
1583 * If so, need to notify the parent to mark itself
1584 * idle anyway, else the no-involumtary-power-cycles
1585 * test cases will report false passes!
1586 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1587 */
1588 if ((bpc->bpc_comp == 0 && bpc->bpc_nlevel == 0 &&
1589 !(bpc->bpc_olevel <= 0) &&
1590 *(int *)result == DDI_SUCCESS) &&
1591 (pshot->state & PM_SUPPORTED)) {
1592 mutex_enter(&pshot->lock);
1593 ASSERT(pshot->busy > 0);
1594 --(pshot->busy);
1595 if (pshot_debug_busy) {
1596 cmn_err(CE_CONT,
1597 "%s%d: post_bus_power:"
1598 " idle parent for %s%d (%d->%d):"
1599 " busy = %d\n", name, instance,
1600 ddi_node_name(bpc->bpc_dip),
1601 ddi_get_instance(bpc->bpc_dip),
1602 bpc->bpc_olevel, bpc->bpc_nlevel,
1603 pshot->busy);
1604 }
1605 mutex_exit(&pshot->lock);
1606 ret = pm_idle_component(dip, 0);
1607 ASSERT(ret == DDI_SUCCESS);
1608 }
1609 break;
1610
1611 case BUS_POWER_HAS_CHANGED:
1612 bphc = (pm_bp_has_changed_t *)arg;
1613 if (pshot_debug) {
1614 cmn_err(CE_CONT, "%s%d: has_changed_bus_power:"
1615 " %s%d comp %d (%d->%d) result %d\n",
1616 name, instance, ddi_node_name(bphc->bphc_dip),
1617 ddi_get_instance(bphc->bphc_dip),
1618 bphc->bphc_comp, bphc->bphc_olevel,
1619 bphc->bphc_nlevel, *(int *)result);
1620 }
1621
1622 /*
1623 * Mark nexus idle when a child's comp 0
1624 * is set to level 0 from levels 1, 2, or 3 only.
1625 *
1626 * If powering up child leaf/nexus nodes via
1627 * pm_power_has_changed() calls, first issue
1628 * DEVCTL_PM_BUSY_COMP ioctl to mark parent busy
1629 * before powering the parent up, then power up the
1630 * child node.
1631 * - skip if PM_SUPPORTED is not set (pshot@XXX,nopm)
1632 */
1633 if ((bphc->bphc_comp == 0 && bphc->bphc_nlevel == 0 &&
1634 !(bphc->bphc_olevel <= 0)) &&
1635 pshot->state & PM_SUPPORTED) {
1636 mutex_enter(&pshot->lock);
1637 ASSERT(pshot->busy > 0);
1638 --(pshot->busy);
1639 if (pshot_debug_busy) {
1640 cmn_err(CE_CONT,
1641 "%s%d: has_changed_bus_power:"
1642 " idle parent for %s%d (%d->%d):"
1643 " busy = %d\n", name, instance,
1644 ddi_node_name(bphc->bphc_dip),
1645 ddi_get_instance(bphc->bphc_dip),
1646 bphc->bphc_olevel,
1647 bphc->bphc_nlevel, pshot->busy);
1648 }
1649 mutex_exit(&pshot->lock);
1650 ret = pm_idle_component(dip, 0);
1651 ASSERT(ret == DDI_SUCCESS);
1652 }
1653 break;
1654
1655 default:
1656 return (pm_busop_bus_power(dip, impl_arg, op, arg, result));
1657
1658 }
1659
1660 return (DDI_SUCCESS);
1661}
1662
1663static int
1664pshot_initchild(dev_info_t *dip, dev_info_t *child)
1665{
1666 char name[64];
1667 char *bus_addr;
1668 char *c_nodename;
1669 int bus_id;
1670 dev_info_t *enum_child;
1671 int enum_base;
1672 int enum_extent;
1673
1674
1675 /* check for bus_enum node */
1676
1677#ifdef NOT_USED
1678 if (impl_ddi_merge_child(child) != DDI_SUCCESS)
1679 return (DDI_FAILURE);
1680#endif
1681
1682 enum_base = ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1683 "busid_ebase", 0);
1684
1685 enum_extent = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1686 DDI_PROP_DONTPASS, "busid_range", 0);
1687
1688 /*
1689 * bus enumeration node
1690 */
1691 if ((enum_base != 0) && (enum_extent != 0)) {
1692 c_nodename = ddi_node_name(child);
1693 bus_id = enum_base;
1694 for (; bus_id < enum_extent; bus_id++) {
1695 if (ndi_devi_alloc(dip, c_nodename, DEVI_PSEUDO_NODEID,
1696 &enum_child) != NDI_SUCCESS)
1697 return (DDI_FAILURE);
1698
1699 (void) sprintf(name, "%d", bus_id);
1700 if (ndi_prop_update_string(DDI_DEV_T_NONE, enum_child,
1701 "bus-addr", name) != DDI_PROP_SUCCESS) {
1702 (void) ndi_devi_free(enum_child);
1703 return (DDI_FAILURE);
1704 }
1705
1706 if (ndi_devi_online(enum_child, 0) !=
1707 DDI_SUCCESS) {
1708 (void) ndi_devi_free(enum_child);
1709 return (DDI_FAILURE);
1710 }
1711 }
1712 /*
1713 * fail the enumeration node itself
1714 */
1715 return (DDI_FAILURE);
1716 }
1717
1718 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, 0, "bus-addr",
1719 &bus_addr) != DDI_PROP_SUCCESS) {
1720 cmn_err(CE_WARN, "pshot_initchild: bus-addr not defined (%s)",
1721 ddi_node_name(child));
1722 return (DDI_NOT_WELL_FORMED);
1723 }
1724
1725 if (strlen(bus_addr) == 0) {
1726 cmn_err(CE_WARN, "pshot_initchild: NULL bus-addr (%s)",
1727 ddi_node_name(child));
1728 ddi_prop_free(bus_addr);
1729 return (DDI_FAILURE);
1730 }
1731
1732 if (strncmp(bus_addr, "failinit", 8) == 0) {
1733 if (pshot_debug)
1734 cmn_err(CE_CONT,
1735 "pshot%d: %s forced INITCHILD failure\n",
Sherry Moore19397402008-09-22 16:30:26 -07001736 ddi_get_instance(dip), bus_addr);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001737 ddi_prop_free(bus_addr);
1738 return (DDI_FAILURE);
1739 }
1740
1741 if (pshot_log) {
1742 cmn_err(CE_CONT, "initchild %s%d/%s@%s\n",
Sherry Moore19397402008-09-22 16:30:26 -07001743 ddi_get_name(dip), ddi_get_instance(dip),
1744 ddi_node_name(child), bus_addr);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001745 }
1746
1747 ddi_set_name_addr(child, bus_addr);
1748 ddi_prop_free(bus_addr);
1749 return (DDI_SUCCESS);
1750}
1751
1752/*ARGSUSED*/
1753static int
1754pshot_uninitchild(dev_info_t *dip, dev_info_t *child)
1755{
1756 ddi_set_name_addr(child, NULL);
1757 return (DDI_SUCCESS);
1758}
1759
1760
1761/*
1762 * devctl IOCTL support
1763 */
1764/* ARGSUSED */
1765static int
1766pshot_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1767{
1768 int instance;
1769 pshot_t *pshot;
1770
1771 if (otyp != OTYP_CHR)
1772 return (EINVAL);
1773
1774 instance = pshot_minor_decode_inst(getminor(*devp));
1775 if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1776 return (ENXIO);
1777
1778 /*
1779 * Access is currently determined on a per-instance basis.
1780 * If we want per-node, then need to add state and lock members to
1781 * pshot_minor_t
1782 */
1783 mutex_enter(&pshot->lock);
1784 if (((flags & FEXCL) && (pshot->state & IS_OPEN)) ||
1785 (!(flags & FEXCL) && (pshot->state & IS_OPEN_EXCL))) {
1786 mutex_exit(&pshot->lock);
1787 return (EBUSY);
1788 }
1789 pshot->state |= IS_OPEN;
1790 if (flags & FEXCL)
1791 pshot->state |= IS_OPEN_EXCL;
1792
1793 if (pshot_debug)
1794 cmn_err(CE_CONT, "pshot%d open\n", instance);
1795
1796 mutex_exit(&pshot->lock);
1797 return (0);
1798}
1799
1800/*
1801 * pshot_close
1802 */
1803/* ARGSUSED */
1804static int
1805pshot_close(dev_t dev, int flag, int otyp, cred_t *credp)
1806{
1807 int instance;
1808 pshot_t *pshot;
1809
1810 if (otyp != OTYP_CHR)
1811 return (EINVAL);
1812
1813 instance = pshot_minor_decode_inst(getminor(dev));
1814 if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1815 return (ENXIO);
1816
1817 mutex_enter(&pshot->lock);
1818 pshot->state &= ~(IS_OPEN | IS_OPEN_EXCL);
1819 mutex_exit(&pshot->lock);
1820 if (pshot_debug)
1821 cmn_err(CE_CONT, "pshot%d closed\n", instance);
1822 return (0);
1823}
1824
1825
1826/*
1827 * pshot_ioctl: redirects to appropriate command handler based on various
1828 * criteria
1829 */
1830/* ARGSUSED */
1831static int
1832pshot_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1833 int *rvalp)
1834{
1835 pshot_t *pshot;
1836 int instance;
1837 minor_t nodenum;
1838 char *nodename;
1839
1840 instance = pshot_minor_decode_inst(getminor(dev));
1841 if ((pshot = ddi_get_soft_state(pshot_softstatep, instance)) == NULL)
1842 return (ENXIO);
1843
1844 nodenum = pshot_minor_decode_nodenum(getminor(dev));
1845 nodename = pshot->nodes[nodenum].name;
1846
1847 if (pshot_debug)
1848 cmn_err(CE_CONT,
1849 "pshot%d ioctl: dev=%p, cmd=%x, arg=%p, mode=%x\n",
1850 instance, (void *)dev, cmd, (void *)arg, mode);
1851
1852 if (strcmp(nodename, PSHOT_NODENAME_DEVCTL) == 0)
1853 return (pshot_devctl(pshot, nodenum, cmd, arg, mode, credp,
1854 rvalp));
1855
1856 if (strcmp(nodename, PSHOT_NODENAME_TESTCTL) == 0)
1857 return (pshot_testctl(pshot, nodenum, cmd, arg, mode, credp,
1858 rvalp));
1859
1860 cmn_err(CE_WARN, "pshot_ioctl: unmatched nodename on minor %u",
1861 pshot->nodes[nodenum].minor);
1862 return (ENXIO);
1863}
1864
1865
1866/*
1867 * pshot_devctl: handle DEVCTL operations
1868 */
1869/* ARGSUSED */
1870static int
1871pshot_devctl(pshot_t *pshot, minor_t nodenum, int cmd, intptr_t arg, int mode,
1872 cred_t *credp, int *rvalp)
1873{
1874 dev_info_t *self;
1875 dev_info_t *child = NULL;
1876 struct devctl_iocdata *dcp;
1877 uint_t state;
1878 int rv = 0;
1879 uint_t flags;
1880 int instance;
1881 int i;
1882 int ret;
1883
1884 self = pshot->dip;
1885
1886 flags = (pshot_devctl_debug) ? NDI_DEVI_DEBUG : 0;
1887 instance = pshot->instance;
1888
1889 /*
1890 * We can use the generic implementation for these ioctls
1891 */
1892 for (i = 0; pshot_devctls[i].ioctl_int != 0; i++) {
1893 if (pshot_devctls[i].ioctl_int == cmd) {
1894 if (pshot_debug)
1895 cmn_err(CE_CONT, "pshot%d devctl: %s",
1896 instance, pshot_devctls[i].ioctl_char);
1897 }
1898 }
1899 switch (cmd) {
1900 case DEVCTL_DEVICE_GETSTATE:
1901 case DEVCTL_DEVICE_ONLINE:
1902 case DEVCTL_DEVICE_OFFLINE:
1903 case DEVCTL_DEVICE_REMOVE:
1904 case DEVCTL_BUS_GETSTATE:
1905 case DEVCTL_BUS_DEV_CREATE:
1906 rv = ndi_devctl_ioctl(self, cmd, arg, mode, flags);
1907 if (pshot_debug && rv != 0) {
1908 cmn_err(CE_CONT, "pshot%d ndi_devctl_ioctl:"
1909 " failed, rv = %d", instance, rv);
1910 }
1911
1912 return (rv);
1913 }
1914
1915 /*
1916 * read devctl ioctl data
1917 */
1918 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1919 return (EFAULT);
1920
1921 switch (cmd) {
1922
1923 case DEVCTL_DEVICE_RESET:
1924 if (pshot_debug)
1925 cmn_err(CE_CONT, "pshot%d devctl:"
1926 " DEVCTL_DEVICE_RESET\n", instance);
1927 rv = pshot_event(pshot, PSHOT_EVENT_TAG_DEV_RESET,
1928 child, (void *)self);
1929 ASSERT(rv == NDI_SUCCESS);
1930 break;
1931
1932 case DEVCTL_BUS_QUIESCE:
1933 if (pshot_debug)
1934 cmn_err(CE_CONT, "pshot%d devctl:"
1935 " DEVCTL_BUS_QUIESCE\n", instance);
1936 if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
1937 if (state == BUS_QUIESCED) {
1938 break;
1939 }
1940 (void) ndi_set_bus_state(self, BUS_QUIESCED);
1941 }
1942 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_QUIESCE,
1943 child, (void *)self);
1944 ASSERT(rv == NDI_SUCCESS);
1945
1946 break;
1947
1948 case DEVCTL_BUS_UNQUIESCE:
1949 if (pshot_debug)
1950 cmn_err(CE_CONT, "pshot%d devctl:"
1951 " DEVCTL_BUS_UNQUIESCE\n", instance);
1952 if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
1953 if (state == BUS_ACTIVE) {
1954 break;
1955 }
1956 }
1957
1958 /*
1959 * quiesce the bus through bus-specific means
1960 */
1961 (void) ndi_set_bus_state(self, BUS_ACTIVE);
1962 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_UNQUIESCE,
1963 child, (void *)self);
1964 ASSERT(rv == NDI_SUCCESS);
1965 break;
1966
1967 case DEVCTL_BUS_RESET:
1968 case DEVCTL_BUS_RESETALL:
1969 /*
1970 * no reset support for the pseudo bus
1971 * but if there were....
1972 */
1973 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_RESET,
Sherry Moore19397402008-09-22 16:30:26 -07001974 child, (void *)self);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001975 ASSERT(rv == NDI_SUCCESS);
1976 break;
1977
1978 /*
1979 * PM related ioctls
1980 */
1981 case DEVCTL_PM_BUSY_COMP:
1982 /*
1983 * mark component 0 busy.
1984 * Keep track of ioctl updates to the busy count
1985 * via pshot->busy_ioctl.
1986 */
1987 if (pshot_debug) {
1988 cmn_err(CE_CONT, "pshot%d devctl:"
1989 " DEVCTL_PM_BUSY_COMP\n", instance);
1990 }
1991 mutex_enter(&pshot->lock);
1992 ++(pshot->busy);
1993 ++(pshot->busy_ioctl);
1994 if (pshot_debug_busy) {
1995 cmn_err(CE_CONT, "pshot%d:"
1996 " DEVCTL_PM_BUSY_COMP comp 0 busy"
1997 " %d busy_ioctl %d\n", instance, pshot->busy,
1998 pshot->busy_ioctl);
1999 }
2000 mutex_exit(&pshot->lock);
2001 ret = pm_busy_component(pshot->dip, 0);
2002 ASSERT(ret == DDI_SUCCESS);
2003
2004 break;
2005
2006 case DEVCTL_PM_BUSY_COMP_TEST:
2007 /*
2008 * test bus's busy state
2009 */
2010 if (pshot_debug) {
2011 cmn_err(CE_CONT, "pshot%d devctl:"
2012 " DEVCTL_PM_BUSY_COMP_TEST\n", instance);
2013 }
2014 mutex_enter(&pshot->lock);
2015 state = pshot->busy;
2016 if (copyout(&state, dcp->cpyout_buf,
2017 sizeof (uint_t)) != 0) {
2018 cmn_err(CE_WARN, "pshot%d devctl:"
2019 " DEVCTL_PM_BUSY_COMP_TEST: copyout failed",
2020 instance);
2021 rv = EINVAL;
2022 }
2023 if (pshot_debug_busy) {
2024 cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_BUSY_COMP_TEST:"
2025 " comp 0 busy %d busy_ioctl %d\n", instance,
2026 state, pshot->busy_ioctl);
2027 }
2028 mutex_exit(&pshot->lock);
2029 break;
2030
2031 case DEVCTL_PM_IDLE_COMP:
2032 /*
2033 * mark component 0 idle.
2034 * NOP if pshot->busy_ioctl <= 0.
2035 */
2036 if (pshot_debug) {
2037 cmn_err(CE_CONT, "pshot%d devctl:"
2038 " DEVCTL_PM_IDLE_COMP\n", instance);
2039 }
2040 mutex_enter(&pshot->lock);
2041 if (pshot->busy_ioctl > 0) {
2042 ASSERT(pshot->busy > 0);
2043 --(pshot->busy);
2044 --(pshot->busy_ioctl);
2045 if (pshot_debug_busy) {
2046 cmn_err(CE_CONT, "pshot%d:"
2047 " DEVCTL_PM_IDLE_COM: comp 0"
2048 " busy %d busy_ioctl %d\n", instance,
2049 pshot->busy, pshot->busy_ioctl);
2050 }
2051 mutex_exit(&pshot->lock);
2052 ret = pm_idle_component(pshot->dip, 0);
2053 ASSERT(ret == DDI_SUCCESS);
2054
2055 } else {
2056 mutex_exit(&pshot->lock);
2057 }
2058 break;
2059
2060 case DEVCTL_PM_RAISE_PWR:
2061 /*
2062 * raise component 0 to full power level MAXPWR via a
2063 * pm_raise_power() call
2064 */
2065 if (pshot_debug) {
2066 cmn_err(CE_CONT, "pshot%d devctl:"
2067 " DEVCTL_PM_RAISE_PWR\n", instance);
2068 }
2069 if (pm_raise_power(pshot->dip, 0, MAXPWR) != DDI_SUCCESS) {
2070 rv = EINVAL;
2071 } else {
2072 mutex_enter(&pshot->lock);
2073 if (pshot_debug) {
2074 cmn_err(CE_CONT, "pshot%d:"
2075 " DEVCTL_PM_RAISE_POWER: comp 0"
2076 " to level %d\n", instance, pshot->level);
2077 }
2078 mutex_exit(&pshot->lock);
2079 }
2080 break;
2081
2082 case DEVCTL_PM_LOWER_PWR:
2083 /*
2084 * pm_lower_power() call for negative testing
2085 * expected to fail.
2086 */
2087 if (pshot_debug) {
2088 cmn_err(CE_CONT, "pshot%d devctl:"
2089 " DEVCTL_PM_LOWER_PWR\n", instance);
2090 }
2091 if (pm_lower_power(pshot->dip, 0, 0) != DDI_SUCCESS) {
2092 rv = EINVAL;
2093 } else {
2094 mutex_enter(&pshot->lock);
2095 if (pshot_debug) {
2096 cmn_err(CE_CONT, "pshot%d:"
2097 " DEVCTL_PM_LOWER_POWER comp 0"
2098 " to level %d\n", instance, pshot->level);
2099 }
2100 mutex_exit(&pshot->lock);
2101 }
2102 break;
2103
2104 case DEVCTL_PM_CHANGE_PWR_LOW:
2105 /*
2106 * inform the PM framework that component 0 has changed
2107 * power level to 0 via a pm_power_has_changed() call
2108 */
2109 if (pshot_debug) {
2110 cmn_err(CE_CONT, "pshot%d devctl:"
2111 " DEVCTL_PM_CHANGE_PWR_LOW\n", instance);
2112 }
2113 mutex_enter(&pshot->lock);
2114 pshot->level = 0;
2115 if (pm_power_has_changed(pshot->dip, 0, 0) != DDI_SUCCESS) {
2116 rv = EINVAL;
2117 } else {
2118 if (pshot_debug) {
2119 cmn_err(CE_CONT, "pshot%d:"
2120 " DEVCTL_PM_CHANGE_PWR_LOW comp 0 to"
2121 " level %d\n", instance, pshot->level);
2122 }
2123 }
2124 mutex_exit(&pshot->lock);
2125 break;
2126
2127 case DEVCTL_PM_CHANGE_PWR_HIGH:
2128 /*
2129 * inform the PM framework that component 0 has changed
2130 * power level to MAXPWR via a pm_power_has_changed() call
2131 */
2132 if (pshot_debug) {
2133 cmn_err(CE_CONT, "pshot%d devctl:"
2134 " DEVCTL_PM_CHANGE_PWR_HIGH\n", instance);
2135 }
2136 mutex_enter(&pshot->lock);
2137 pshot->level = MAXPWR;
2138 if (pm_power_has_changed(pshot->dip, 0, MAXPWR)
2139 != DDI_SUCCESS) {
2140 rv = EINVAL;
2141 } else {
2142 if (pshot_debug) {
2143 cmn_err(CE_CONT, "pshot%d:"
2144 " DEVCTL_PM_CHANGE_PWR_HIGH comp 0 to"
2145 " level %d\n", instance, pshot->level);
2146 }
2147 }
2148 mutex_exit(&pshot->lock);
2149 break;
2150
2151 case DEVCTL_PM_POWER:
2152 /*
2153 * test if the pshot_power() routine has been called,
2154 * then clear
2155 */
2156 if (pshot_debug) {
2157 cmn_err(CE_CONT, "pshot%d devctl:"
2158 " DEVCTL_PM_POWER\n", instance);
2159 }
2160 mutex_enter(&pshot->lock);
2161 state = (pshot->state & POWER_FLAG) ? 1 : 0;
2162 if (copyout(&state, dcp->cpyout_buf,
2163 sizeof (uint_t)) != 0) {
2164 cmn_err(CE_WARN, "pshot%d devctl:"
2165 " DEVCTL_PM_POWER: copyout failed",
2166 instance);
2167 rv = EINVAL;
2168 }
2169 if (pshot_debug) {
2170 cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_POWER:"
2171 " POWER_FLAG = %d\n", instance, state);
2172 }
2173 pshot->state &= ~POWER_FLAG;
2174 mutex_exit(&pshot->lock);
2175 break;
2176
2177 case DEVCTL_PM_FAIL_SUSPEND:
2178 /*
2179 * fail DDI_SUSPEND
2180 */
2181 if (pshot_debug) {
2182 cmn_err(CE_CONT, "pshot%d devctl:"
2183 " DEVCTL_PM_FAIL_SUSPEND\n", instance);
2184 }
2185 mutex_enter(&pshot->lock);
2186 pshot->state |= FAIL_SUSPEND_FLAG;
2187 mutex_exit(&pshot->lock);
2188 if (pshot_debug) {
2189 cmn_err(CE_CONT, "pshot%d: DEVCTL_PM_FAIL_SUSPEND\n",
2190 instance);
2191 }
2192 break;
2193
2194 case DEVCTL_PM_BUS_STRICT_TEST:
2195 /*
2196 * test the STRICT_PARENT flag:
2197 * set => STRICT PARENT
2198 * not set => INVOLVED PARENT
2199 */
2200 mutex_enter(&pshot->lock);
2201 state = (pshot->state & STRICT_PARENT) ? 1 : 0;
2202 if (copyout(&state, dcp->cpyout_buf,
2203 sizeof (uint_t)) != 0) {
2204 cmn_err(CE_WARN, "pshot%d devctl:"
2205 " DEVCTL_PM_BUS_STRICT_TEST: copyout failed",
2206 instance);
2207 rv = EINVAL;
2208 }
2209 if (pshot_debug) {
2210 cmn_err(CE_CONT, "pshot%d devctl:"
2211 " DEVCTL_PM_BUS_STRICT_TEST: type = %s\n",
2212 instance, ((state == 0) ? "INVOLVED" : "STRICT"));
2213 }
2214 mutex_exit(&pshot->lock);
2215 break;
2216
2217 case DEVCTL_PM_BUS_NO_INVOL:
2218 /*
2219 * Set the NO_INVOL_FLAG flag to
2220 * notify the driver that the child will not
2221 * call pm_lower_power() on detach.
2222 * The driver needs to mark itself idle twice
2223 * during DDI_CTLOPS_DETACH (post).
2224 */
2225 if (pshot_debug) {
2226 cmn_err(CE_CONT, "pshot%d devctl:"
2227 " DEVCTL_PM_BUS_NO_INVOL\n", instance);
2228 }
2229 mutex_enter(&pshot->lock);
2230 pshot->state |= NO_INVOL_FLAG;
2231 mutex_exit(&pshot->lock);
2232 break;
2233
2234 default:
2235 rv = ENOTTY;
2236 }
2237
2238 ndi_dc_freehdl(dcp);
2239 return (rv);
2240}
2241
2242
2243/*
2244 * pshot_testctl: handle other test operations
2245 * - If <cmd> is a DEVCTL cmd, then <arg> is a dev_t indicating which
2246 * child to direct the DEVCTL to, if applicable;
2247 * furthermore, any cmd here can be sent by layered ioctls (unlike
2248 * those to pshot_devctl() which must come from userland)
2249 */
2250/* ARGSUSED */
2251static int
2252pshot_testctl(pshot_t *pshot, minor_t nodenum, int cmd, intptr_t arg, int mode,
2253 cred_t *credp, int *rvalp)
2254{
2255 dev_info_t *self;
2256 dev_info_t *child = NULL;
2257 uint_t state;
2258 int rv = 0;
2259 int instance;
2260 int i;
2261
2262 /* uint_t flags; */
2263
2264 /* flags = (pshot_devctl_debug) ? NDI_DEVI_DEBUG : 0; */
2265 self = pshot->dip;
2266 instance = pshot->instance;
2267
2268 if (cmd & DEVCTL_IOC) {
2269 child = e_ddi_hold_devi_by_dev((dev_t)arg, 0);
2270 }
2271
2272 for (i = 0; pshot_devctls[i].ioctl_int != 0; i++) {
2273 if (pshot_devctls[i].ioctl_int == cmd) {
2274 if (pshot_debug)
2275 cmn_err(CE_CONT, "pshot%d devctl: %s",
2276 instance, pshot_devctls[i].ioctl_char);
2277 }
2278 }
2279 switch (cmd) {
2280 case DEVCTL_DEVICE_RESET:
2281 if (pshot_debug)
2282 cmn_err(CE_CONT, "pshot%d testctl:"
2283 " DEVCTL_PM_POWER\n", instance);
2284 rv = pshot_event(pshot, PSHOT_EVENT_TAG_DEV_RESET,
2285 child, (void *)self);
2286 ASSERT(rv == NDI_SUCCESS);
2287 break;
2288
2289 case DEVCTL_BUS_QUIESCE:
2290 if (pshot_debug)
2291 cmn_err(CE_CONT, "pshot%d testctl:"
2292 " DEVCTL_PM_POWER\n", instance);
2293 if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
2294 if (state == BUS_QUIESCED) {
2295 break;
2296 }
2297 (void) ndi_set_bus_state(self, BUS_QUIESCED);
2298 }
2299 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_QUIESCE,
2300 child, (void *)self);
2301 ASSERT(rv == NDI_SUCCESS);
2302
2303 break;
2304
2305 case DEVCTL_BUS_UNQUIESCE:
2306 if (pshot_debug)
2307 cmn_err(CE_CONT, "pshot%d testctl:"
2308 " DEVCTL_PM_POWER\n", instance);
2309 if (ndi_get_bus_state(self, &state) == NDI_SUCCESS) {
2310 if (state == BUS_ACTIVE) {
2311 break;
2312 }
2313 }
2314
2315 /*
2316 * quiesce the bus through bus-specific means
2317 */
2318 (void) ndi_set_bus_state(self, BUS_ACTIVE);
2319 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_UNQUIESCE,
2320 child, (void *)self);
2321 ASSERT(rv == NDI_SUCCESS);
2322 break;
2323
2324 case DEVCTL_BUS_RESET:
2325 case DEVCTL_BUS_RESETALL:
2326 /*
2327 * no reset support for the pseudo bus
2328 * but if there were....
2329 */
2330 rv = pshot_event(pshot, PSHOT_EVENT_TAG_BUS_RESET,
Sherry Moore19397402008-09-22 16:30:26 -07002331 child, (void *)self);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002332 ASSERT(rv == NDI_SUCCESS);
2333 break;
2334
2335 default:
2336 rv = ENOTTY;
2337 }
2338
2339 if (child != NULL)
2340 ddi_release_devi(child);
2341 return (rv);
2342}
2343
2344
2345static int
2346pshot_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2347 char *eventname, ddi_eventcookie_t *event_cookiep)
2348{
2349 int instance = ddi_get_instance(dip);
2350 pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2351
2352 if (pshot_debug)
Sherry Moore19397402008-09-22 16:30:26 -07002353 cmn_err(CE_CONT, "pshot%d: "
2354 "pshot_get_eventcookie:\n\t"
2355 "dip = 0x%p rdip = 0x%p (%s/%d) eventname = %s\n",
2356 instance, (void *)dip, (void *)rdip,
2357 ddi_node_name(rdip), ddi_get_instance(rdip),
2358 eventname);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002359
2360
2361 return (ndi_event_retrieve_cookie(pshot->ndi_event_hdl,
Sherry Moore19397402008-09-22 16:30:26 -07002362 rdip, eventname, event_cookiep, NDI_EVENT_NOPASS));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002363}
2364
2365static int
2366pshot_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2367 ddi_eventcookie_t cookie,
2368 void (*callback)(), void *arg, ddi_callback_id_t *cb_id)
2369{
2370 int instance = ddi_get_instance(dip);
2371 pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2372
2373 if (pshot_debug)
Sherry Moore19397402008-09-22 16:30:26 -07002374 cmn_err(CE_CONT, "pshot%d: "
2375 "pshot_add_eventcall:\n\t"
2376 "dip = 0x%p rdip = 0x%p (%s%d)\n\tcookie = 0x%p (%s)\n\t"
2377 "cb = 0x%p, arg = 0x%p\n",
2378 instance, (void *)dip, (void *)rdip,
2379 ddi_node_name(rdip), ddi_get_instance(rdip), (void *)cookie,
2380 NDI_EVENT_NAME(cookie), (void *)callback, arg);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002381
2382 /* add callback to our event handle */
2383 return (ndi_event_add_callback(pshot->ndi_event_hdl, rdip,
Sherry Moore19397402008-09-22 16:30:26 -07002384 cookie, callback, arg, NDI_SLEEP, cb_id));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002385}
2386
2387static int
2388pshot_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2389{
2390
2391 ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
2392
2393 int instance = ddi_get_instance(dip);
2394 pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2395
2396 ASSERT(cb);
2397
2398 if (pshot_debug)
Sherry Moore19397402008-09-22 16:30:26 -07002399 cmn_err(CE_CONT, "pshot%d: "
2400 "pshot_remove_eventcall:\n\t"
2401 "dip = 0x%p rdip = 0x%p (%s%d)\n\tcookie = 0x%p (%s)\n",
2402 instance, (void *)dip, (void *)cb->ndi_evtcb_dip,
2403 ddi_node_name(cb->ndi_evtcb_dip),
2404 ddi_get_instance(cb->ndi_evtcb_dip),
2405 (void *)cb->ndi_evtcb_cookie,
2406 NDI_EVENT_NAME(cb->ndi_evtcb_cookie));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002407
2408 return (ndi_event_remove_callback(pshot->ndi_event_hdl, cb_id));
2409}
2410
2411static int
2412pshot_post_event(dev_info_t *dip, dev_info_t *rdip,
2413 ddi_eventcookie_t cookie, void *impl_data)
2414{
2415 int instance = ddi_get_instance(dip);
2416 pshot_t *pshot = ddi_get_soft_state(pshot_softstatep, instance);
2417
2418 if (pshot_debug) {
Sherry Moore19397402008-09-22 16:30:26 -07002419 if (rdip) {
2420 cmn_err(CE_CONT, "pshot%d: "
2421 "pshot_post_event:\n\t"
2422 "dip = 0x%p rdip = 0x%p (%s%d\n\t"
2423 "cookie = 0x%p (%s)\n\tbus_impl = 0x%p\n",
2424 instance, (void *)dip, (void *)rdip,
2425 ddi_node_name(rdip), ddi_get_instance(rdip),
2426 (void *)cookie,
2427 NDI_EVENT_NAME(cookie), impl_data);
2428 } else {
2429 cmn_err(CE_CONT, "pshot%d: "
2430 "pshot_post_event:\n\t"
2431 "dip = 0x%p cookie = 0x%p (%s) bus_impl = 0x%p\n",
2432 instance, (void *)dip, (void *)cookie,
2433 NDI_EVENT_NAME(cookie), impl_data);
2434 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002435 }
2436
2437 /* run callbacks for this event */
2438 return (ndi_event_run_callbacks(pshot->ndi_event_hdl, rdip,
2439 cookie, impl_data));
2440}
2441
2442/*
2443 * the nexus driver will generate events
2444 * that need to go to children
2445 */
2446static int
2447pshot_event(pshot_t *pshot, int event_tag, dev_info_t *child,
2448 void *bus_impldata)
2449{
2450 ddi_eventcookie_t cookie = ndi_event_tag_to_cookie(
Sherry Moore19397402008-09-22 16:30:26 -07002451 pshot->ndi_event_hdl, event_tag);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002452
2453 if (pshot_debug) {
2454 if (child) {
Sherry Moore19397402008-09-22 16:30:26 -07002455 cmn_err(CE_CONT, "pshot%d: "
2456 "pshot_event: event_tag = 0x%x (%s)\n\t"
2457 "child = 0x%p (%s%d) bus_impl = 0x%p (%s%d)\n",
2458 pshot->instance, event_tag,
2459 ndi_event_tag_to_name(pshot->ndi_event_hdl,
2460 event_tag),
2461 (void *)child, ddi_node_name(child),
2462 ddi_get_instance(child), bus_impldata,
2463 ddi_node_name((dev_info_t *)bus_impldata),
2464 ddi_get_instance((dev_info_t *)bus_impldata));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002465 } else {
Sherry Moore19397402008-09-22 16:30:26 -07002466 cmn_err(CE_CONT, "pshot%d: "
2467 "pshot_event: event_tag = 0x%x (%s)\n\t"
2468 "child = NULL, bus_impl = 0x%p (%s%d)\n",
2469 pshot->instance, event_tag,
2470 ndi_event_tag_to_name(pshot->ndi_event_hdl,
2471 event_tag),
2472 bus_impldata,
2473 ddi_node_name((dev_info_t *)bus_impldata),
2474 ddi_get_instance((dev_info_t *)bus_impldata));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002475 }
2476 }
2477
2478 return (ndi_event_run_callbacks(pshot->ndi_event_hdl,
Sherry Moore19397402008-09-22 16:30:26 -07002479 child, cookie, bus_impldata));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002480}
2481
2482
2483/*
2484 * the pshot driver event notification callback
2485 */
2486static void
2487pshot_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
2488 void *arg, void *bus_impldata)
2489{
2490 pshot_t *pshot = (pshot_t *)arg;
2491 int event_tag;
2492
2493 /* look up the event */
2494 event_tag = NDI_EVENT_TAG(cookie);
2495
2496 if (pshot_debug) {
2497 cmn_err(CE_CONT, "pshot%d: "
2498 "pshot_event_cb:\n\t"
2499 "dip = 0x%p cookie = 0x%p (%s), tag = 0x%x\n\t"
2500 "arg = 0x%p bus_impl = 0x%p (%s%d)\n",
2501 pshot->instance, (void *)dip, (void *)cookie,
2502 NDI_EVENT_NAME(cookie), event_tag, arg, bus_impldata,
2503 ddi_node_name((dev_info_t *)bus_impldata),
2504 ddi_get_instance((dev_info_t *)bus_impldata));
2505 }
2506
2507 switch (event_tag) {
2508 case PSHOT_EVENT_TAG_OFFLINE:
2509 case PSHOT_EVENT_TAG_BUS_RESET:
2510 case PSHOT_EVENT_TAG_BUS_QUIESCE:
2511 case PSHOT_EVENT_TAG_BUS_UNQUIESCE:
2512 /* notify all subscribers of the this event */
2513 (void) ndi_event_run_callbacks(pshot->ndi_event_hdl,
Sherry Moore19397402008-09-22 16:30:26 -07002514 NULL, cookie, bus_impldata);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002515 if (pshot_debug) {
2516 cmn_err(CE_CONT, "pshot%d: event=%s\n\t"
Sherry Moore19397402008-09-22 16:30:26 -07002517 "pshot_event_cb\n", pshot->instance,
2518 NDI_EVENT_NAME(cookie));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002519 }
2520 /*FALLTHRU*/
2521 case PSHOT_EVENT_TAG_TEST_POST:
2522 case PSHOT_EVENT_TAG_DEV_RESET:
2523 default:
2524 return;
2525 }
2526}
2527
2528static int
2529pshot_bus_config(dev_info_t *parent, uint_t flags,
2530 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
2531{
2532 int rval;
2533 char *devname;
2534 char *devstr, *cname, *caddr;
2535 int devstrlen;
2536 int circ;
2537 pshot_t *pshot;
2538 int instance = ddi_get_instance(parent);
2539
2540 if (pshot_debug) {
2541 flags |= NDI_DEVI_DEBUG;
2542 cmn_err(CE_CONT,
2543 "pshot%d: bus_config %s flags=0x%x\n",
2544 ddi_get_instance(parent),
2545 (op == BUS_CONFIG_ONE) ? (char *)arg : "", flags);
2546 }
2547
2548 pshot = ddi_get_soft_state(pshot_softstatep, instance);
2549 if (pshot == NULL) {
2550
2551 return (NDI_FAILURE);
2552 }
2553
2554 /*
2555 * Hold the nexus across the bus_config
2556 */
2557 ndi_devi_enter(parent, &circ);
2558
2559 switch (op) {
2560 case BUS_CONFIG_ONE:
2561
2562 /*
2563 * lookup and hold child device, create if not found
2564 */
2565 devname = (char *)arg;
2566 devstrlen = strlen(devname) + 1;
2567 devstr = i_ddi_strdup(devname, KM_SLEEP);
2568 i_ddi_parse_name(devstr, &cname, &caddr, NULL);
2569
2570 /*
2571 * The framework ensures that the node has
2572 * a name but each nexus is responsible for
2573 * the bus address name space. This driver
2574 * requires that a bus address be specified,
2575 * as will most nexus drivers.
2576 */
2577 ASSERT(cname && strlen(cname) > 0);
2578 if (caddr == NULL || strlen(caddr) == 0) {
2579 cmn_err(CE_WARN,
2580 "pshot%d: malformed name %s (no bus address)",
2581 ddi_get_instance(parent), devname);
2582 kmem_free(devstr, devstrlen);
2583 ndi_devi_exit(parent, circ);
2584 return (NDI_FAILURE);
2585 }
2586
2587 /*
2588 * Handle a few special cases for testing purposes
2589 */
2590 rval = pshot_bus_config_test_specials(parent,
Sherry Moore19397402008-09-22 16:30:26 -07002591 devname, cname, caddr);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002592
2593 if (rval == NDI_SUCCESS) {
2594 /*
2595 * Set up either a leaf or nexus device
2596 */
2597 if (strcmp(cname, "pshot") == 0) {
2598 rval = pshot_bus_config_setup_nexus(parent,
2599 cname, caddr);
2600 } else {
2601 rval = pshot_bus_config_setup_leaf(parent,
2602 cname, caddr);
2603 }
2604 }
2605
2606 kmem_free(devstr, devstrlen);
2607 break;
2608
2609 case BUS_CONFIG_DRIVER:
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002610 case BUS_CONFIG_ALL:
2611 rval = NDI_SUCCESS;
2612 break;
2613
2614 default:
2615 rval = NDI_FAILURE;
2616 break;
2617 }
2618
2619 if (rval == NDI_SUCCESS)
2620 rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
2621
2622 ndi_devi_exit(parent, circ);
2623
2624 if (pshot_debug)
2625 cmn_err(CE_CONT, "pshot%d: bus_config %s\n",
2626 ddi_get_instance(parent),
2627 (rval == NDI_SUCCESS) ? "ok" : "failed");
2628
2629 return (rval);
2630}
2631
2632static int
2633pshot_bus_unconfig(dev_info_t *parent, uint_t flags,
2634 ddi_bus_config_op_t op, void *arg)
2635{
2636 major_t major;
2637 int rval = NDI_SUCCESS;
2638 int circ;
2639
2640 if (pshot_debug) {
2641 flags |= NDI_DEVI_DEBUG;
2642 cmn_err(CE_CONT,
2643 "pshot%d: bus_unconfig %s flags=0x%x\n",
2644 ddi_get_instance(parent),
2645 (op == BUS_UNCONFIG_ONE) ? (char *)arg : "", flags);
2646 }
2647
2648 /*
2649 * Hold the nexus across the bus_unconfig
2650 */
2651 ndi_devi_enter(parent, &circ);
2652
2653 switch (op) {
2654 case BUS_UNCONFIG_ONE:
2655 /*
2656 * Nothing special required here
2657 */
2658 if (pshot_debug) {
2659 cmn_err(CE_CONT, "pshot%d: bus_unconfig:"
2660 " BUS_UNCONFIG_ONE\n", ddi_get_instance(parent));
2661 }
2662 break;
2663
2664 case BUS_UNCONFIG_DRIVER:
2665 if (pshot_debug > 0) {
2666 major = (major_t)(uintptr_t)arg;
2667 cmn_err(CE_CONT,
2668 "pshot%d: BUS_UNCONFIG_DRIVER: %s\n",
2669 ddi_get_instance(parent),
2670 ddi_major_to_name(major));
2671 }
2672 break;
2673
2674 case BUS_UNCONFIG_ALL:
2675 if (pshot_debug) {
2676 cmn_err(CE_CONT, "pshot%d: bus_unconfig:"
2677 " BUS_UNCONFIG_ALL\n", ddi_get_instance(parent));
2678 }
2679 break;
2680
2681 default:
2682 if (pshot_debug) {
2683 cmn_err(CE_CONT, "pshot%d: bus_unconfig: DEFAULT\n",
2684 ddi_get_instance(parent));
2685 }
2686 rval = NDI_FAILURE;
2687 }
2688
2689 if (rval == NDI_SUCCESS)
2690 rval = ndi_busop_bus_unconfig(parent, flags, op, arg);
2691
2692 ndi_devi_exit(parent, circ);
2693
2694 if (pshot_debug)
2695 cmn_err(CE_CONT, "pshot%d: bus_unconfig %s\n",
2696 ddi_get_instance(parent),
2697 (rval == NDI_SUCCESS) ? "ok" : "failed");
2698
2699 return (rval);
2700}
2701
2702static dev_info_t *
2703pshot_findchild(dev_info_t *pdip, char *cname, char *caddr)
2704{
2705 dev_info_t *dip;
2706 char *addr;
2707
2708 ASSERT(cname != NULL && caddr != NULL);
2709 ASSERT(DEVI_BUSY_OWNED(pdip));
2710
2711 for (dip = ddi_get_child(pdip); dip != NULL;
2712 dip = ddi_get_next_sibling(dip)) {
2713 if (strcmp(cname, ddi_node_name(dip)) != 0)
2714 continue;
2715
2716 if ((addr = ddi_get_name_addr(dip)) == NULL) {
2717 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
2718 "bus-addr", &addr) == DDI_PROP_SUCCESS) {
2719 if (strcmp(caddr, addr) == 0) {
2720 ddi_prop_free(addr);
2721 return (dip);
2722 }
2723 ddi_prop_free(addr);
2724 }
2725 } else {
2726 if (strcmp(caddr, addr) == 0)
2727 return (dip);
2728 }
2729 }
2730
2731 return (NULL);
2732}
2733
2734static void
2735pshot_nexus_properties(dev_info_t *parent, dev_info_t *child, char *cname,
2736 char *caddr)
2737{
2738 char *extension;
2739
2740 /*
cthb1dd9582005-06-27 15:47:48 -07002741 * extract the address extension
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002742 */
2743 extension = strstr(caddr, ",");
2744 if (extension != NULL) {
2745 ++extension;
2746 } else {
2747 extension = "null";
2748 }
2749
2750 /*
2751 * Create the "pm-want-child-notification?" property for all
2752 * nodes that do not have the "pm_strict" or "nopm_strict"
2753 * extension
2754 */
2755 if (strcmp(extension, "pm_strict") != 0 &&
2756 strcmp(extension, "nopm_strict") != 0) {
2757 if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2758 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2759 "pm-want-child-notification?") == 0) {
2760 if (pshot_debug) {
2761 cmn_err(CE_CONT, "pshot%d:"
2762 " nexus_properties:\n\tcreate the"
2763 " \"pm-want-child-notification?\""
2764 " property for %s@%s\n",
2765 ddi_get_instance(parent), cname, caddr);
2766 }
2767 if (ddi_prop_create(DDI_DEV_T_NONE, child, 0,
2768 "pm-want-child-notification?", NULL, 0)
2769 != DDI_PROP_SUCCESS) {
2770 cmn_err(CE_WARN, "pshot%d:"
2771 " nexus_properties:\n\tunable to create"
2772 " the \"pm-want-child-notification?\""
2773 " property for %s@%s",
2774 ddi_get_instance(parent), cname, caddr);
2775 }
2776 }
2777 }
2778
2779 /*
2780 * Create the "no-pm-components" property for all nodes
2781 * with extension "nopm" or "nopm_strict"
2782 */
2783 if (strcmp(extension, "nopm") == 0 ||
2784 strcmp(extension, "nopm_strict") == 0) {
2785 if (ddi_prop_exists(DDI_DEV_T_ANY, child,
2786 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2787 "no-pm-components") == 0) {
2788 if (pshot_debug) {
2789