xref: /titanic_41/usr/src/uts/common/os/sunpm.c (revision 890e8ff10cfc85bc7d33064a9a30c3e8477b4813)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * sunpm.c builds sunpm.o	"power management framework"
30  *	kernel-resident power management code.  Implements power management
31  *	policy
32  *	Assumes: all backwards compat. device components wake up on &
33  *		 the pm_info pointer in dev_info is initially NULL
34  *
35  * PM - (device) Power Management
36  *
37  * Each device may have 0 or more components.  If a device has no components,
38  * then it can't be power managed.  Each component has 2 or more
39  * power states.
40  *
41  * "Backwards Compatible" (bc) devices:
42  * There are two different types of devices from the point of view of this
43  * code.  The original type, left over from the original PM implementation on
44  * the voyager platform are known in this code as "backwards compatible"
45  * devices (PM_ISBC(dip) returns true).
46  * They are recognized by the pm code by the lack of a pm-components property
47  * and a call made by the driver to pm_create_components(9F).
48  * For these devices, component 0 is special, and represents the power state
49  * of the device.  If component 0 is to be set to power level 0 (off), then
50  * the framework must first call into the driver's detach(9E) routine with
51  * DDI_PM_SUSPEND, to get the driver to save the hardware state of the device.
52  * After setting component 0 from 0 to a non-zero power level, a call must be
53  * made into the driver's attach(9E) routine with DDI_PM_RESUME.
54  *
55  * Currently, the only way to get a bc device power managed is via a set of
56  * ioctls (PM_DIRECT_PM, PM_SET_CURRENT_POWER) issued to /dev/pm.
57  *
58  * For non-bc devices, the driver describes the components by exporting a
59  * pm-components(9P) property that tells how many components there are,
60  * tells what each component's power state values are, and provides human
61  * readable strings (currently unused) for each component name and power state.
62  * Devices which export pm-components(9P) are automatically power managed
63  * whenever autopm is enabled (via PM_START_PM ioctl issued by pmconfig(1M)
64  * after parsing power.conf(4)).
65  * For these devices, all components are considered independent of each other,
66  * and it is up to the driver to decide when a transition requires saving or
67  * restoring hardware state.
68  *
69  * Each device component also has a threshold time associated with each power
70  * transition (see power.conf(4)), and a busy/idle state maintained by the
71  * driver calling pm_idle_component(9F) and pm_busy_component(9F).
72  * Components are created idle.
73  *
74  * The PM framework provides several functions:
75  * -implement PM policy as described in power.conf(4)
76  *  Policy is set by pmconfig(1M) issuing pm ioctls based on power.conf(4).
77  *  Policies consist of:
78  *    -set threshold values (defaults if none provided by pmconfig)
79  *    -set dependencies among devices
80  *    -enable/disable autopm
81  *    -turn down idle components based on thresholds (if autopm is enabled)
82  *     (aka scanning)
83  *    -maintain power states based on dependencies among devices
84  *    -upon request, or when the frame buffer powers off, attempt to turn off
85  *     all components that are idle or become idle over the next (10 sec)
86  *     period in an attempt to get down to an EnergyStar compliant state
87  *    -prevent powering off of a device which exported the
88  *     pm-no-involuntary-power-cycles property without active involvement of
89  *     the device's driver (so no removing power when the device driver is
90  *     not attached)
91  * -provide a mechanism for a device driver to request that a device's component
92  *  be brought back to the power level necessary for the use of the device
93  * -allow a process to directly control the power levels of device components
94  *  (via ioctls issued to /dev/pm--see usr/src/uts/common/io/pm.c)
95  * -ensure that the console frame buffer is powered up before being referenced
96  *  via prom_printf() or other prom calls that might generate console output
97  * -maintain implicit dependencies (e.g. parent must be powered up if child is)
98  * -provide "backwards compatible" behavior for devices without pm-components
99  *  property
100  *
101  * Scanning:
102  * Whenever autopm is enabled, the framework attempts to bring each component
103  * of each device to its lowest power based on the threshold of idleness
104  * associated with each transition and the busy/idle state of the component.
105  *
106  * The actual work of this is done by pm_scan_dev(), which cycles through each
107  * component of a device, checking its idleness against its current threshold,
108  * and calling pm_set_power() as appropriate to change the power level.
109  * This function also indicates when it would next be profitable to scan the
110  * device again, and a new scan is scheduled after that time.
111  *
112  * Dependencies:
113  * It is possible to establish a dependency between the power states of two
114  * otherwise unrelated devices.  This is currently done to ensure that the
115  * cdrom is always up whenever the console framebuffer is up, so that the user
116  * can insert a cdrom and see a popup as a result.
117  *
118  * The dependency terminology used in power.conf(4) is not easy to understand,
119  * so we've adopted a different terminology in the implementation.  We write
120  * of a "keeps up" and a "kept up" device.  A relationship can be established
121  * where one device keeps up another.  That means that if the keepsup device
122  * has any component that is at a non-zero power level, all components of the
123  * "kept up" device must be brought to full power.  This relationship is
124  * asynchronous.  When the keeping device is powered up, a request is queued
125  * to a worker thread to bring up the kept device.  The caller does not wait.
126  * Scan will not turn down a kept up device.
127  *
128  * Direct PM:
129  * A device may be directly power managed by a process.  If a device is
130  * directly pm'd, then it will not be scanned, and dependencies will not be
131  * enforced.  * If a directly pm'd device's driver requests a power change (via
132  * pm_raise_power(9F)), then the request is blocked and notification is sent
133  * to the controlling process, which must issue the requested power change for
134  * the driver to proceed.
135  *
136  */
137 
138 #include <sys/types.h>
139 #include <sys/errno.h>
140 #include <sys/callb.h>		/* callback registration during CPR */
141 #include <sys/conf.h>		/* driver flags and functions */
142 #include <sys/open.h>		/* OTYP_CHR definition */
143 #include <sys/stat.h>		/* S_IFCHR definition */
144 #include <sys/pathname.h>	/* name -> dev_info xlation */
145 #include <sys/ddi_impldefs.h>	/* dev_info node fields */
146 #include <sys/kmem.h>		/* memory alloc stuff */
147 #include <sys/debug.h>
148 #include <sys/archsystm.h>
149 #include <sys/pm.h>
150 #include <sys/ddi.h>
151 #include <sys/sunddi.h>
152 #include <sys/sunndi.h>
153 #include <sys/sunpm.h>
154 #include <sys/epm.h>
155 #include <sys/vfs.h>
156 #include <sys/mode.h>
157 #include <sys/mkdev.h>
158 #include <sys/promif.h>
159 #include <sys/consdev.h>
160 #include <sys/esunddi.h>
161 #include <sys/modctl.h>
162 #include <sys/fs/ufs_fs.h>
163 #include <sys/note.h>
164 #include <sys/taskq.h>
165 #include <sys/bootconf.h>
166 #include <sys/reboot.h>
167 #include <sys/spl.h>
168 #include <sys/disp.h>
169 #include <sys/sobject.h>
170 #include <sys/sunmdi.h>
171 
172 
173 /*
174  * PM LOCKING
175  *	The list of locks:
176  * Global pm mutex locks.
177  *
178  * pm_scan_lock:
179  *		It protects the timeout id of the scan thread, and the value
180  *		of autopm_enabled.  This lock is not held concurrently with
181  *		any other PM locks.
182  *
183  * pm_clone_lock:	Protects the clone list and count of poll events
184  *		pending for the pm driver.
185  *		Lock ordering:
186  *			pm_clone_lock -> pm_pscc_interest_rwlock,
187  *			pm_clone_lock -> pm_pscc_direct_rwlock.
188  *
189  * pm_rsvp_lock:
190  *		Used to synchronize the data structures used for processes
191  *		to rendezvous with state change information when doing
192  *		direct PM.
193  *		Lock ordering:
194  *			pm_rsvp_lock -> pm_pscc_interest_rwlock,
195  *			pm_rsvp_lock -> pm_pscc_direct_rwlock,
196  *			pm_rsvp_lock -> pm_clone_lock.
197  *
198  * ppm_lock:	protects the list of registered ppm drivers
199  *		Lock ordering:
200  *			ppm_lock -> ppm driver unit_lock
201  *
202  * pm_compcnt_lock:
203  *		Protects count of components that are not at their lowest
204  *		power level.
205  *		Lock ordering:
206  *			pm_compcnt_lock -> ppm_lock.
207  *
208  * pm_dep_thread_lock:
209  *		Protects work list for pm_dep_thread.  Not taken concurrently
210  *		with any other pm lock.
211  *
212  * pm_remdrv_lock:
213  *		Serializes the operation of removing noinvol data structure
214  *		entries for a branch of the tree when a driver has been
215  *		removed from the system (modctl_rem_major).
216  *		Lock ordering:
217  *			pm_remdrv_lock -> pm_noinvol_rwlock.
218  *
219  * pm_cfb_lock: (High level spin lock)
220  *		Protects the count of how many components of the console
221  *		frame buffer are off (so we know if we have to bring up the
222  *		console as a result of a prom_printf, etc.
223  *		No other locks are taken while holding this lock.
224  *
225  * pm_loan_lock:
226  *		Protects the lock_loan list.  List is used to record that one
227  *		thread has acquired a power lock but has launched another thread
228  *		to complete its processing.  An entry in the list indicates that
229  *		the worker thread can borrow the lock held by the other thread,
230  *		which must block on the completion of the worker.  Use is
231  *		specific to module loading.
232  *		No other locks are taken while holding this lock.
233  *
234  * Global PM rwlocks
235  *
236  * pm_thresh_rwlock:
237  *		Protects the list of thresholds recorded for future use (when
238  *		devices attach).
239  *		Lock ordering:
240  *			pm_thresh_rwlock -> devi_pm_lock
241  *
242  * pm_noinvol_rwlock:
243  *		Protects list of detached nodes that had noinvol registered.
244  *		No other PM locks are taken while holding pm_noinvol_rwlock.
245  *
246  * pm_pscc_direct_rwlock:
247  *		Protects the list that maps devices being directly power
248  *		managed to the processes that manage them.
249  *		Lock ordering:
250  *			pm_pscc_direct_rwlock -> psce_lock
251  *
252  * pm_pscc_interest_rwlock;
253  *		Protects the list that maps state change events to processes
254  *		that want to know about them.
255  *		Lock ordering:
256  *			pm_pscc_interest_rwlock -> psce_lock
257  *
258  * per-dip locks:
259  *
260  * Each node has these per-dip locks, which are only used if the device is
261  * a candidate for power management (e.g. has pm components)
262  *
263  * devi_pm_lock:
264  *		Protects all power management state of the node except for
265  *		power level, which is protected by ndi_devi_enter().
266  *		Encapsulated in macros PM_LOCK_DIP()/PM_UNLOCK_DIP().
267  *		Lock ordering:
268  *			devi_pm_lock -> pm_rsvp_lock,
269  *			devi_pm_lock -> pm_dep_thread_lock,
270  *			devi_pm_lock -> pm_noinvol_rwlock,
271  *			devi_pm_lock -> power lock
272  *
273  * power lock (ndi_devi_enter()):
274  *		Since changing power level is possibly a slow operation (30
275  *		seconds to spin up a disk drive), this is locked separately.
276  *		Since a call into the driver to change the power level of one
277  *		component may result in a call back into the framework to change
278  *		the power level of another, this lock allows re-entrancy by
279  *		the same thread (ndi_devi_enter is used for this because
280  *		the USB framework uses ndi_devi_enter in its power entry point,
281  *		and use of any other lock would produce a deadlock.
282  *
283  * devi_pm_busy_lock:
284  *		This lock protects the integrity of the busy count.  It is
285  *		only taken by pm_busy_component() and pm_idle_component and
286  *		some code that adjust the busy time after the timer gets set
287  *		up or after a CPR operation.  It is per-dip to keep from
288  *		single-threading all the disk drivers on a system.
289  *		It could be per component instead, but most devices have
290  *		only one component.
291  *		No other PM locks are taken while holding this lock.
292  *
293  */
294 
295 static int stdout_is_framebuffer;
296 static kmutex_t	e_pm_power_lock;
297 static kmutex_t pm_loan_lock;
298 kmutex_t	pm_scan_lock;
299 callb_id_t	pm_cpr_cb_id;
300 callb_id_t	pm_panic_cb_id;
301 callb_id_t	pm_halt_cb_id;
302 int		pm_comps_notlowest;	/* no. of comps not at lowest power */
303 int		pm_powering_down;	/* cpr is source of DDI_SUSPEND calls */
304 
305 clock_t pm_min_scan = PM_MIN_SCAN;
306 clock_t pm_id_ticks = 5;	/* ticks to wait before scan during idle-down */
307 
308 static int pm_busop_set_power(dev_info_t *,
309     void *, pm_bus_power_op_t, void *, void *);
310 static int pm_busop_match_request(dev_info_t *, void *);
311 static int pm_all_to_normal_nexus(dev_info_t *, pm_canblock_t);
312 
313 /*
314  * Dependency Processing is done thru a seperate thread.
315  */
316 kmutex_t	pm_dep_thread_lock;
317 kcondvar_t	pm_dep_thread_cv;
318 pm_dep_wk_t	*pm_dep_thread_workq = NULL;
319 pm_dep_wk_t	*pm_dep_thread_tail = NULL;
320 
321 /*
322  * Autopm  must be turned on by a PM_START_PM ioctl, so we don't end up
323  * power managing things in single user mode that have been suppressed via
324  * power.conf entries.  Protected by pm_scan_lock.
325  */
326 int		autopm_enabled;
327 
328 /*
329  * This flag is true while processes are stopped for a checkpoint/resume.
330  * Controlling processes of direct pm'd devices are not available to
331  * participate in power level changes, so we bypass them when this is set.
332  */
333 static int	pm_processes_stopped;
334 
335 #ifdef	DEBUG
336 
337 /*
338  * see common/sys/epm.h for PMD_* values
339  */
340 uint_t		pm_debug = 0;
341 
342 /*
343  * If pm_divertdebug is set, then no prom_printf calls will be made by
344  * PMD(), which will prevent debug output from bringing up the console
345  * frame buffer.  Clearing this variable before setting pm_debug will result
346  * in PMD output going to the console.
347  *
348  * pm_divertdebug is incremented in pm_set_power() if dip == cfb_dip to avoid
349  * deadlocks and decremented at the end of pm_set_power()
350  */
351 uint_t		pm_divertdebug = 1;
352 kmutex_t	pm_debug_lock;		/* protects pm_divertdebug */
353 
354 void prdeps(char *);
355 #endif
356 
357 /* Globals */
358 
359 /*
360  * List of recorded thresholds and dependencies
361  */
362 pm_thresh_rec_t *pm_thresh_head;
363 krwlock_t pm_thresh_rwlock;
364 
365 pm_pdr_t *pm_dep_head;
366 static int pm_unresolved_deps = 0;
367 static int pm_prop_deps = 0;
368 
369 /*
370  * List of devices that exported no-involuntary-power-cycles property
371  */
372 pm_noinvol_t *pm_noinvol_head;
373 
374 /*
375  * Locks used in noinvol processing
376  */
377 krwlock_t pm_noinvol_rwlock;
378 kmutex_t pm_remdrv_lock;
379 
380 int pm_default_idle_threshold = PM_DEFAULT_SYS_IDLENESS;
381 int pm_system_idle_threshold;
382 /*
383  * By default nexus has 0 threshold, and depends on its children to keep it up
384  */
385 int pm_default_nexus_threshold = 0;
386 
387 /*
388  * Data structures shared with common/io/pm.c
389  */
390 kmutex_t	pm_clone_lock;
391 kcondvar_t	pm_clones_cv[PM_MAX_CLONE];
392 uint_t		pm_poll_cnt[PM_MAX_CLONE];	/* count of events for poll */
393 unsigned char	pm_interest[PM_MAX_CLONE];
394 struct pollhead	pm_pollhead;
395 
396 extern int	hz;
397 extern char	*platform_module_list[];
398 
399 /*
400  * Wrappers for use in ddi_walk_devs
401  */
402 
403 static int		pm_set_dev_thr_walk(dev_info_t *, void *);
404 static int		pm_restore_direct_lvl_walk(dev_info_t *, void *);
405 static int		pm_save_direct_lvl_walk(dev_info_t *, void *);
406 static int		pm_discard_dep_walk(dev_info_t *, void *);
407 #ifdef DEBUG
408 static int		pm_desc_pwrchk_walk(dev_info_t *, void *);
409 #endif
410 
411 /*
412  * Routines for managing noinvol devices
413  */
414 int			pm_noinvol_update(int, int, int, char *, dev_info_t *);
415 void			pm_noinvol_update_node(dev_info_t *,
416 			    pm_bp_noinvol_t *req);
417 
418 kmutex_t pm_rsvp_lock;
419 kmutex_t pm_compcnt_lock;
420 krwlock_t pm_pscc_direct_rwlock;
421 krwlock_t pm_pscc_interest_rwlock;
422 
423 #define	PSC_INTEREST	0	/* belongs to interest psc list */
424 #define	PSC_DIRECT	1	/* belongs to direct psc list */
425 
426 pscc_t *pm_pscc_interest;
427 pscc_t *pm_pscc_direct;
428 
429 #define	PM_MAJOR(dip) ddi_name_to_major(ddi_binding_name(dip))
430 #define	PM_IS_NEXUS(dip) NEXUS_DRV(devopsp[PM_MAJOR(dip)])
431 #define	POWERING_ON(old, new) ((old) == 0 && (new) != 0)
432 #define	POWERING_OFF(old, new) ((old) != 0 && (new) == 0)
433 #define	PPM(dip) ((dev_info_t *)DEVI(dip)->devi_pm_ppm)
434 
435 #define	PM_INCR_NOTLOWEST(dip) {					\
436 	mutex_enter(&pm_compcnt_lock);					\
437 	if (!PM_IS_NEXUS(dip) ||					\
438 	    (DEVI(dip)->devi_pm_flags & (PMC_DEV_THRESH|PMC_COMP_THRESH))) {\
439 		if (pm_comps_notlowest == 0)				\
440 			pm_ppm_notify_all_lowest(dip, PM_NOT_ALL_LOWEST);\
441 		pm_comps_notlowest++;					\
442 		PMD(PMD_LEVEL, ("%s: %s@%s(%s#%d) incr notlowest->%d\n",\
443 		    pmf, PM_DEVICE(dip), pm_comps_notlowest))		\
444 	}								\
445 	mutex_exit(&pm_compcnt_lock);					\
446 }
447 #define	PM_DECR_NOTLOWEST(dip) {					\
448 	mutex_enter(&pm_compcnt_lock);					\
449 	if (!PM_IS_NEXUS(dip) ||					\
450 	    (DEVI(dip)->devi_pm_flags & (PMC_DEV_THRESH|PMC_COMP_THRESH))) {\
451 		ASSERT(pm_comps_notlowest);				\
452 		pm_comps_notlowest--;					\
453 		PMD(PMD_LEVEL, ("%s: %s@%s(%s#%d) decr notlowest to "	\
454 			    "%d\n", pmf, PM_DEVICE(dip), pm_comps_notlowest))\
455 		if (pm_comps_notlowest == 0)				\
456 			pm_ppm_notify_all_lowest(dip, PM_ALL_LOWEST);	\
457 	}								\
458 	mutex_exit(&pm_compcnt_lock);					\
459 }
460 
461 /*
462  * console frame-buffer power-management is not enabled when
463  * debugging services are present.  to override, set pm_cfb_override
464  * to non-zero.
465  */
466 uint_t pm_cfb_comps_off = 0;	/* PM_LEVEL_UNKNOWN is considered on */
467 kmutex_t pm_cfb_lock;
468 int pm_cfb_enabled = 1;		/* non-zero allows pm of console frame buffer */
469 #ifdef DEBUG
470 int pm_cfb_override = 1;	/* non-zero allows pm of cfb with debuggers */
471 #else
472 int pm_cfb_override = 0;	/* non-zero allows pm of cfb with debuggers */
473 #endif
474 
475 static dev_info_t *cfb_dip = 0;
476 static dev_info_t *cfb_dip_detaching = 0;
477 uint_t cfb_inuse = 0;
478 static ddi_softintr_t pm_soft_id;
479 static clock_t pm_soft_pending;
480 int	pm_scans_disabled = 0;
481 
482 /*
483  * A structure to record the fact that one thread has borrowed a lock held
484  * by another thread.  The context requires that the lender block on the
485  * completion of the borrower.
486  */
487 typedef struct lock_loan {
488 	struct lock_loan	*pmlk_next;
489 	kthread_t		*pmlk_borrower;
490 	kthread_t		*pmlk_lender;
491 	dev_info_t		*pmlk_dip;
492 } lock_loan_t;
493 static lock_loan_t lock_loan_head;	/* list head is a dummy element */
494 
495 #ifdef	DEBUG
496 #define	PMD_FUNC(func, name)	char *(func) = (name);
497 #else
498 #define	PMD_FUNC(func, name)
499 #endif
500 
501 
502 /*
503  * Must be called before first device (including pseudo) attach
504  */
505 void
506 pm_init_locks(void)
507 {
508 	mutex_init(&pm_scan_lock, NULL, MUTEX_DRIVER, NULL);
509 	mutex_init(&pm_rsvp_lock, NULL, MUTEX_DRIVER, NULL);
510 	mutex_init(&pm_compcnt_lock, NULL, MUTEX_DRIVER, NULL);
511 	mutex_init(&pm_dep_thread_lock, NULL, MUTEX_DRIVER, NULL);
512 	mutex_init(&pm_remdrv_lock, NULL, MUTEX_DRIVER, NULL);
513 	mutex_init(&pm_loan_lock, NULL, MUTEX_DRIVER, NULL);
514 	rw_init(&pm_thresh_rwlock, NULL, RW_DEFAULT, NULL);
515 	rw_init(&pm_noinvol_rwlock, NULL, RW_DEFAULT, NULL);
516 	cv_init(&pm_dep_thread_cv, NULL, CV_DEFAULT, NULL);
517 }
518 
519 static boolean_t
520 pm_cpr_callb(void *arg, int code)
521 {
522 	_NOTE(ARGUNUSED(arg))
523 	static int auto_save;
524 	static int pm_reset_timestamps(dev_info_t *, void *);
525 
526 	switch (code) {
527 	case CB_CODE_CPR_CHKPT:
528 		/*
529 		 * Cancel scan or wait for scan in progress to finish
530 		 * Other threads may be trying to restart the scan, so we
531 		 * have to keep at it unil it sticks
532 		 */
533 		mutex_enter(&pm_scan_lock);
534 		ASSERT(!pm_scans_disabled);
535 		pm_scans_disabled = 1;
536 		auto_save = autopm_enabled;
537 		autopm_enabled = 0;
538 		mutex_exit(&pm_scan_lock);
539 		ddi_walk_devs(ddi_root_node(), pm_scan_stop_walk, NULL);
540 		break;
541 
542 	case CB_CODE_CPR_RESUME:
543 		ASSERT(!autopm_enabled);
544 		ASSERT(pm_scans_disabled);
545 		pm_scans_disabled = 0;
546 		/*
547 		 * Call pm_reset_timestamps to reset timestamps of each
548 		 * device to the time when the system is resumed so that their
549 		 * idleness can be re-calculated. That's to avoid devices from
550 		 * being powered down right after resume if the system was in
551 		 * suspended mode long enough.
552 		 */
553 		ddi_walk_devs(ddi_root_node(), pm_reset_timestamps, NULL);
554 
555 		autopm_enabled = auto_save;
556 		/*
557 		 * If there is any auto-pm device, get the scanning
558 		 * going. Otherwise don't bother.
559 		 */
560 		ddi_walk_devs(ddi_root_node(), pm_rescan_walk, NULL);
561 		break;
562 	}
563 	return (B_TRUE);
564 }
565 
566 /*
567  * This callback routine is called when there is a system panic.  This function
568  * exists for prototype matching.
569  */
570 static boolean_t
571 pm_panic_callb(void *arg, int code)
572 {
573 	_NOTE(ARGUNUSED(arg, code))
574 	void pm_cfb_check_and_powerup(void);
575 	PMD(PMD_CFB, ("pm_panic_callb\n"))
576 	pm_cfb_check_and_powerup();
577 	return (B_TRUE);
578 }
579 
580 static boolean_t
581 pm_halt_callb(void *arg, int code)
582 {
583 	_NOTE(ARGUNUSED(arg, code))
584 	return (B_TRUE);	/* XXX for now */
585 }
586 
587 /*
588  * This needs to be called after the root and platform drivers are loaded
589  * and be single-threaded with respect to driver attach/detach
590  */
591 void
592 pm_init(void)
593 {
594 	PMD_FUNC(pmf, "pm_init")
595 	char **mod;
596 	extern pri_t minclsyspri;
597 	static void pm_dep_thread(void);
598 
599 	pm_comps_notlowest = 0;
600 	pm_system_idle_threshold = pm_default_idle_threshold;
601 
602 	pm_cpr_cb_id = callb_add(pm_cpr_callb, (void *)NULL,
603 	    CB_CL_CPR_PM, "pm_cpr");
604 	pm_panic_cb_id = callb_add(pm_panic_callb, (void *)NULL,
605 		    CB_CL_PANIC, "pm_panic");
606 	pm_halt_cb_id = callb_add(pm_halt_callb, (void *)NULL,
607 		    CB_CL_HALT, "pm_halt");
608 
609 	/*
610 	 * Create a thread to do dependency processing.
611 	 */
612 	(void) thread_create(NULL, 0, (void (*)())pm_dep_thread, NULL, 0, &p0,
613 	    TS_RUN, minclsyspri);
614 
615 	/*
616 	 * loadrootmodules already loaded these ppm drivers, now get them
617 	 * attached so they can claim the root drivers as they attach
618 	 */
619 	for (mod = platform_module_list; *mod; mod++) {
620 		if (i_ddi_attach_hw_nodes(*mod) != DDI_SUCCESS) {
621 			cmn_err(CE_WARN, "!cannot load platform pm driver %s\n",
622 			    *mod);
623 		} else {
624 			PMD(PMD_DHR, ("%s: %s (%s)\n", pmf, *mod,
625 			    ddi_major_to_name(ddi_name_to_major(*mod))))
626 		}
627 	}
628 }
629 
630 /*
631  * pm_scan_init - create pm scan data structure.  Called (if autopm enabled)
632  * when device becomes power managed or after a failed detach and when autopm
633  * is started via PM_START_PM ioctl, and after a CPR resume to get all the
634  * devices scanning again.
635  */
636 void
637 pm_scan_init(dev_info_t *dip)
638 {
639 	PMD_FUNC(pmf, "scan_init")
640 	pm_scan_t	*scanp;
641 
642 	ASSERT(!PM_ISBC(dip));
643 
644 	PM_LOCK_DIP(dip);
645 	scanp = PM_GET_PM_SCAN(dip);
646 	if (!scanp) {
647 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): create scan data\n",
648 		    pmf, PM_DEVICE(dip)))
649 		scanp =  kmem_zalloc(sizeof (pm_scan_t), KM_SLEEP);
650 		DEVI(dip)->devi_pm_scan = scanp;
651 	} else if (scanp->ps_scan_flags & PM_SCAN_STOP) {
652 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): "
653 		    "clear PM_SCAN_STOP flag\n", pmf, PM_DEVICE(dip)))
654 		scanp->ps_scan_flags &= ~PM_SCAN_STOP;
655 	}
656 	PM_UNLOCK_DIP(dip);
657 }
658 
659 /*
660  * pm_scan_fini - remove pm scan data structure when stopping pm on the device
661  */
662 void
663 pm_scan_fini(dev_info_t *dip)
664 {
665 	PMD_FUNC(pmf, "scan_fini")
666 	pm_scan_t	*scanp;
667 
668 	PMD(PMD_SCAN, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
669 	ASSERT(!PM_ISBC(dip));
670 	PM_LOCK_DIP(dip);
671 	scanp = PM_GET_PM_SCAN(dip);
672 	if (!scanp) {
673 		PM_UNLOCK_DIP(dip);
674 		return;
675 	}
676 
677 	ASSERT(!scanp->ps_scan_id && !(scanp->ps_scan_flags &
678 	    (PM_SCANNING | PM_SCAN_DISPATCHED | PM_SCAN_AGAIN)));
679 
680 	kmem_free(scanp, sizeof (pm_scan_t));
681 	DEVI(dip)->devi_pm_scan = NULL;
682 	PM_UNLOCK_DIP(dip);
683 }
684 
685 /*
686  * Given a pointer to a component struct, return the current power level
687  * (struct contains index unless it is a continuous level).
688  * Located here in hopes of getting both this and dev_is_needed into the
689  * cache together
690  */
691 static int
692 cur_power(pm_component_t *cp)
693 {
694 	if (cp->pmc_cur_pwr == PM_LEVEL_UNKNOWN)
695 		return (cp->pmc_cur_pwr);
696 
697 	return (cp->pmc_comp.pmc_lvals[cp->pmc_cur_pwr]);
698 }
699 
700 static char *
701 pm_decode_direction(int direction)
702 {
703 	switch (direction) {
704 	case PM_LEVEL_UPONLY:
705 		return ("up");
706 
707 	case PM_LEVEL_EXACT:
708 		return ("exact");
709 
710 	case PM_LEVEL_DOWNONLY:
711 		return ("down");
712 
713 	default:
714 		return ("INVALID DIRECTION");
715 	}
716 }
717 
718 char *
719 pm_decode_op(pm_bus_power_op_t op)
720 {
721 	switch (op) {
722 	case BUS_POWER_CHILD_PWRCHG:
723 		return ("CHILD_PWRCHG");
724 	case BUS_POWER_NEXUS_PWRUP:
725 		return ("NEXUS_PWRUP");
726 	case BUS_POWER_PRE_NOTIFICATION:
727 		return ("PRE_NOTIFICATION");
728 	case BUS_POWER_POST_NOTIFICATION:
729 		return ("POST_NOTIFICATION");
730 	case BUS_POWER_HAS_CHANGED:
731 		return ("HAS_CHANGED");
732 	case BUS_POWER_NOINVOL:
733 		return ("NOINVOL");
734 	default:
735 		return ("UNKNOWN OP");
736 	}
737 }
738 
739 /*
740  * Returns true if level is a possible (valid) power level for component
741  */
742 int
743 e_pm_valid_power(dev_info_t *dip, int cmpt, int level)
744 {
745 	PMD_FUNC(pmf, "e_pm_valid_power")
746 	pm_component_t *cp = PM_CP(dip, cmpt);
747 	int i;
748 	int *ip = cp->pmc_comp.pmc_lvals;
749 	int limit = cp->pmc_comp.pmc_numlevels;
750 
751 	if (level < 0)
752 		return (0);
753 	for (i = 0; i < limit; i++) {
754 		if (level == *ip++)
755 			return (1);
756 	}
757 #ifdef DEBUG
758 	if (pm_debug & PMD_FAIL) {
759 		ip = cp->pmc_comp.pmc_lvals;
760 
761 		for (i = 0; i < limit; i++)
762 			PMD(PMD_FAIL, ("%s: index=%d, level=%d\n",
763 			    pmf, i, *ip++))
764 	}
765 #endif
766 	return (0);
767 }
768 
769 /*
770  * Returns true if device is pm'd (after calling pm_start if need be)
771  */
772 int
773 e_pm_valid_info(dev_info_t *dip, pm_info_t **infop)
774 {
775 	pm_info_t *info;
776 	static int pm_start(dev_info_t *dip);
777 
778 	/*
779 	 * Check if the device is power managed if not.
780 	 * To make the common case (device is power managed already)
781 	 * fast, we check without the lock.  If device is not already
782 	 * power managed, then we take the lock and the long route through
783 	 * go get it managed.  Devices never go unmanaged until they
784 	 * detach.
785 	 */
786 	info = PM_GET_PM_INFO(dip);
787 	if (!info) {
788 		if (!DEVI_IS_ATTACHING(dip)) {
789 			return (0);
790 		}
791 		if (pm_start(dip) != DDI_SUCCESS) {
792 			return (0);
793 		}
794 		info = PM_GET_PM_INFO(dip);
795 	}
796 	ASSERT(info);
797 	if (infop != NULL)
798 		*infop = info;
799 	return (1);
800 }
801 
802 int
803 e_pm_valid_comp(dev_info_t *dip, int cmpt, pm_component_t **cpp)
804 {
805 	if (cmpt >= 0 && cmpt < PM_NUMCMPTS(dip)) {
806 		if (cpp != NULL)
807 			*cpp = PM_CP(dip, cmpt);
808 		return (1);
809 	} else {
810 		return (0);
811 	}
812 }
813 
814 /*
815  * Internal guts of ddi_dev_is_needed and pm_raise/lower_power
816  */
817 static int
818 dev_is_needed(dev_info_t *dip, int cmpt, int level, int direction)
819 {
820 	PMD_FUNC(pmf, "din")
821 	pm_component_t *cp;
822 	char *pathbuf;
823 	int result;
824 
825 	ASSERT(direction == PM_LEVEL_UPONLY || direction == PM_LEVEL_DOWNONLY);
826 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, cmpt, &cp) ||
827 	    !e_pm_valid_power(dip, cmpt, level))
828 		return (DDI_FAILURE);
829 
830 	PMD(PMD_DIN, ("%s: %s@%s(%s#%d) cmpt=%d, dir=%s, new=%d, cur=%d\n",
831 	    pmf, PM_DEVICE(dip), cmpt, pm_decode_direction(direction),
832 	    level, cur_power(cp)))
833 
834 	if (pm_set_power(dip, cmpt, level,  direction,
835 	    PM_CANBLOCK_BLOCK, 0, &result) != DDI_SUCCESS) {
836 		if (direction == PM_LEVEL_UPONLY) {
837 			pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
838 			(void) ddi_pathname(dip, pathbuf);
839 			cmn_err(CE_WARN, "Device %s failed to power up.",
840 			    pathbuf);
841 			kmem_free(pathbuf, MAXPATHLEN);
842 		}
843 		PMD(PMD_DIN | PMD_FAIL, ("%s: %s@%s(%s#%d) [%d] %s->%d failed, "
844 		    "errno %d\n", pmf, PM_DEVICE(dip), cmpt,
845 		    pm_decode_direction(direction), level, result))
846 		return (DDI_FAILURE);
847 	}
848 
849 	PMD(PMD_RESCAN | PMD_DIN, ("%s: pm_rescan %s@%s(%s#%d)\n", pmf,
850 	    PM_DEVICE(dip)))
851 	pm_rescan(dip);
852 	return (DDI_SUCCESS);
853 }
854 
855 /*
856  * We can get multiple pm_rescan() threads, if one of them discovers
857  * that no scan is running at the moment, it kicks it into action.
858  * Otherwise, it tells the current scanning thread to scan again when
859  * it is done by asserting the PM_SCAN_AGAIN flag. The PM_SCANNING and
860  * PM_SCAN_AGAIN flags are used to regulate scan, to make sure only one
861  * thread at a time runs the pm_scan_dev() code.
862  */
863 void
864 pm_rescan(void *arg)
865 {
866 	PMD_FUNC(pmf, "rescan")
867 	dev_info_t	*dip = (dev_info_t *)arg;
868 	pm_info_t	*info;
869 	pm_scan_t	*scanp;
870 	timeout_id_t	scanid;
871 
872 	PMD(PMD_SCAN, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
873 	PM_LOCK_DIP(dip);
874 	info = PM_GET_PM_INFO(dip);
875 	scanp = PM_GET_PM_SCAN(dip);
876 	if (pm_scans_disabled || !autopm_enabled || !info || !scanp ||
877 	    (scanp->ps_scan_flags & PM_SCAN_STOP)) {
878 		PM_UNLOCK_DIP(dip);
879 		return;
880 	}
881 	if (scanp->ps_scan_flags & PM_SCANNING) {
882 		scanp->ps_scan_flags |= PM_SCAN_AGAIN;
883 		PM_UNLOCK_DIP(dip);
884 		return;
885 	} else if (scanp->ps_scan_id) {
886 		scanid = scanp->ps_scan_id;
887 		scanp->ps_scan_id = 0;
888 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): cancel timeout scanid %lx\n",
889 		    pmf, PM_DEVICE(dip), (ulong_t)scanid))
890 		PM_UNLOCK_DIP(dip);
891 		(void) untimeout(scanid);
892 		PM_LOCK_DIP(dip);
893 	}
894 
895 	/*
896 	 * Dispatching pm_scan during attach time is risky due to the fact that
897 	 * attach might soon fail and dip dissolved, and panic may happen while
898 	 * attempting to stop scan. So schedule a pm_rescan instead.
899 	 * (Note that if either of the first two terms are true, taskq_dispatch
900 	 * will not be invoked).
901 	 *
902 	 * Multiple pm_scan dispatching is unecessary and costly to keep track
903 	 * of. The PM_SCAN_DISPATCHED flag is used between pm_rescan and pm_scan
904 	 * to regulate the dispatching.
905 	 *
906 	 * Scan is stopped before the device is detached (in pm_detaching())
907 	 * but it may get re-started during the post_detach processing if the
908 	 * driver fails to detach.
909 	 */
910 	if (DEVI_IS_ATTACHING(dip) ||
911 	    (scanp->ps_scan_flags & PM_SCAN_DISPATCHED) ||
912 	    !taskq_dispatch(system_taskq, pm_scan, (void *)dip, TQ_NOSLEEP)) {
913 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): attaching, pm_scan already "
914 		    "dispatched or dispatching failed\n", pmf, PM_DEVICE(dip)))
915 		if (scanp->ps_scan_id) {
916 			scanid = scanp->ps_scan_id;
917 			scanp->ps_scan_id = 0;
918 			PM_UNLOCK_DIP(dip);
919 			(void) untimeout(scanid);
920 			PM_LOCK_DIP(dip);
921 			if (scanp->ps_scan_id) {
922 				PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): a competing "
923 				    "thread scheduled pm_rescan, scanid %lx\n",
924 				    pmf, PM_DEVICE(dip),
925 				    (ulong_t)scanp->ps_scan_id))
926 				PM_UNLOCK_DIP(dip);
927 				return;
928 			}
929 		}
930 		scanp->ps_scan_id = timeout(pm_rescan, (void *)dip,
931 		    (scanp->ps_idle_down ? pm_id_ticks :
932 		    (pm_min_scan * hz)));
933 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): scheduled next pm_rescan, "
934 		    "scanid %lx\n", pmf, PM_DEVICE(dip),
935 		    (ulong_t)scanp->ps_scan_id))
936 	} else {
937 		PMD(PMD_SCAN, ("%s: dispatched pm_scan for %s@%s(%s#%d)\n",
938 		    pmf, PM_DEVICE(dip)))
939 		scanp->ps_scan_flags |= PM_SCAN_DISPATCHED;
940 	}
941 	PM_UNLOCK_DIP(dip);
942 }
943 
944 void
945 pm_scan(void *arg)
946 {
947 	PMD_FUNC(pmf, "scan")
948 	dev_info_t	*dip = (dev_info_t *)arg;
949 	pm_scan_t	*scanp;
950 	time_t		nextscan;
951 
952 	PMD(PMD_SCAN, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
953 
954 	PM_LOCK_DIP(dip);
955 	scanp = PM_GET_PM_SCAN(dip);
956 	ASSERT(scanp && PM_GET_PM_INFO(dip));
957 
958 	if (pm_scans_disabled || !autopm_enabled ||
959 	    (scanp->ps_scan_flags & PM_SCAN_STOP)) {
960 		scanp->ps_scan_flags &= ~(PM_SCAN_AGAIN | PM_SCAN_DISPATCHED);
961 		PM_UNLOCK_DIP(dip);
962 		return;
963 	}
964 
965 	if (scanp->ps_idle_down) {
966 		/*
967 		 * make sure we remember idledown was in affect until
968 		 * we've completed the scan
969 		 */
970 		PMID_SET_SCANS(scanp->ps_idle_down)
971 		PMD(PMD_IDLEDOWN, ("%s: %s@%s(%s#%d): idledown starts "
972 		    "(pmid %x)\n", pmf, PM_DEVICE(dip), scanp->ps_idle_down))
973 	}
974 
975 	/* possible having two threads running pm_scan() */
976 	if (scanp->ps_scan_flags & PM_SCANNING) {
977 		scanp->ps_scan_flags |= PM_SCAN_AGAIN;
978 		PMD(PMD_SCAN, ("%s: scanning, will scan %s@%s(%s#%d) again\n",
979 		    pmf, PM_DEVICE(dip)))
980 		scanp->ps_scan_flags &= ~PM_SCAN_DISPATCHED;
981 		PM_UNLOCK_DIP(dip);
982 		return;
983 	}
984 
985 	scanp->ps_scan_flags |= PM_SCANNING;
986 	scanp->ps_scan_flags &= ~PM_SCAN_DISPATCHED;
987 	do {
988 		scanp->ps_scan_flags &= ~PM_SCAN_AGAIN;
989 		PM_UNLOCK_DIP(dip);
990 		nextscan = pm_scan_dev(dip);
991 		PM_LOCK_DIP(dip);
992 	} while (scanp->ps_scan_flags & PM_SCAN_AGAIN);
993 
994 	ASSERT(scanp->ps_scan_flags & PM_SCANNING);
995 	scanp->ps_scan_flags &= ~PM_SCANNING;
996 
997 	if (scanp->ps_idle_down) {
998 		scanp->ps_idle_down &= ~PMID_SCANS;
999 		PMD(PMD_IDLEDOWN, ("%s: %s@%s(%s#%d): idledown ends "
1000 		    "(pmid %x)\n", pmf, PM_DEVICE(dip), scanp->ps_idle_down))
1001 	}
1002 
1003 	/* schedule for next idle check */
1004 	if (nextscan != LONG_MAX) {
1005 		if (nextscan > (LONG_MAX / hz))
1006 			nextscan = (LONG_MAX - 1) / hz;
1007 		if (scanp->ps_scan_id) {
1008 			PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): while scanning "
1009 			    "another rescan scheduled scanid(%lx)\n", pmf,
1010 			    PM_DEVICE(dip), (ulong_t)scanp->ps_scan_id))
1011 			PM_UNLOCK_DIP(dip);
1012 			return;
1013 		} else if (!(scanp->ps_scan_flags & PM_SCAN_STOP)) {
1014 			scanp->ps_scan_id = timeout(pm_rescan, (void *)dip,
1015 			    (clock_t)(nextscan * hz));
1016 			PMD(PMD_SCAN, ("%s: nextscan for %s@%s(%s#%d) in "
1017 			    "%lx sec, scanid(%lx) \n", pmf, PM_DEVICE(dip),
1018 			    (ulong_t)nextscan, (ulong_t)scanp->ps_scan_id))
1019 		}
1020 	}
1021 	PM_UNLOCK_DIP(dip);
1022 }
1023 
1024 void
1025 pm_get_timestamps(dev_info_t *dip, time_t *valuep)
1026 {
1027 	int components = PM_NUMCMPTS(dip);
1028 	int i;
1029 
1030 	ASSERT(components > 0);
1031 	PM_LOCK_BUSY(dip);	/* so we get a consistent view */
1032 	for (i = 0; i < components; i++) {
1033 		valuep[i] = PM_CP(dip, i)->pmc_timestamp;
1034 	}
1035 	PM_UNLOCK_BUSY(dip);
1036 }
1037 
1038 /*
1039  * Returns true if device needs to be kept up because it exported the
1040  * "no-involuntary-power-cycles" property or we're pretending it did (console
1041  * fb case) or it is an ancestor of such a device and has used up the "one
1042  * free cycle" allowed when all such leaf nodes have voluntarily powered down
1043  * upon detach
1044  */
1045 int
1046 pm_noinvol(dev_info_t *dip)
1047 {
1048 	PMD_FUNC(pmf, "noinvol")
1049 
1050 	/*
1051 	 * This doesn't change over the life of a driver, so no locking needed
1052 	 */
1053 	if (PM_IS_CFB(dip)) {
1054 		PMD(PMD_NOINVOL | PMD_CFB, ("%s: inhibits CFB %s@%s(%s#%d)\n",
1055 		    pmf, PM_DEVICE(dip)))
1056 		return (1);
1057 	}
1058 	/*
1059 	 * Not an issue if no such kids
1060 	 */
1061 	if (DEVI(dip)->devi_pm_noinvolpm == 0) {
1062 #ifdef DEBUG
1063 		if (DEVI(dip)->devi_pm_volpmd != 0) {
1064 			dev_info_t *pdip = dip;
1065 			do {
1066 				PMD(PMD_NOINVOL, ("%s: %s@%s(%s#%d) noinvol %d "
1067 				    "volpmd %d\n", pmf, PM_DEVICE(pdip),
1068 				    DEVI(pdip)->devi_pm_noinvolpm,
1069 				    DEVI(pdip)->devi_pm_volpmd))
1070 				pdip = ddi_get_parent(pdip);
1071 			} while (pdip);
1072 		}
1073 #endif
1074 		ASSERT(DEVI(dip)->devi_pm_volpmd == 0);
1075 		return (0);
1076 	}
1077 
1078 	/*
1079 	 * Since we now maintain the counts correct at every node, we no longer
1080 	 * need to look up the tree.  An ancestor cannot use up the free cycle
1081 	 * without the children getting their counts adjusted.
1082 	 */
1083 
1084 #ifdef	DEBUG
1085 	if (DEVI(dip)->devi_pm_noinvolpm != DEVI(dip)->devi_pm_volpmd)
1086 		PMD(PMD_NOINVOL, ("%s: (%d != %d) inhibits %s@%s(%s#%d)\n", pmf,
1087 		    DEVI(dip)->devi_pm_noinvolpm, DEVI(dip)->devi_pm_volpmd,
1088 		    PM_DEVICE(dip)))
1089 #endif
1090 	return (DEVI(dip)->devi_pm_noinvolpm != DEVI(dip)->devi_pm_volpmd);
1091 }
1092 
1093 /*
1094  * This function performs the actual scanning of the device.
1095  * It attempts to power off the indicated device's components if they have
1096  * been idle and other restrictions are met.
1097  * pm_scan_dev calculates and returns when the next scan should happen for
1098  * this device.
1099  */
1100 time_t
1101 pm_scan_dev(dev_info_t *dip)
1102 {
1103 	PMD_FUNC(pmf, "scan_dev")
1104 	pm_scan_t	*scanp;
1105 	time_t		*timestamp, idletime, now, thresh;
1106 	time_t		timeleft = 0;
1107 	int		i, nxtpwr, curpwr, pwrndx, unused;
1108 	size_t		size;
1109 	pm_component_t	 *cp;
1110 	dev_info_t	*pdip = ddi_get_parent(dip);
1111 	int		circ;
1112 	static int	cur_threshold(dev_info_t *, int);
1113 	static int	pm_next_lower_power(pm_component_t *, int);
1114 
1115 	/*
1116 	 * skip attaching device
1117 	 */
1118 	if (DEVI_IS_ATTACHING(dip)) {
1119 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) is attaching, timeleft(%lx)\n",
1120 		    pmf, PM_DEVICE(dip), pm_min_scan))
1121 		return (pm_min_scan);
1122 	}
1123 
1124 	PM_LOCK_DIP(dip);
1125 	scanp = PM_GET_PM_SCAN(dip);
1126 	ASSERT(scanp && PM_GET_PM_INFO(dip));
1127 
1128 	PMD(PMD_SCAN, ("%s: [BEGIN %s@%s(%s#%d)]\n", pmf, PM_DEVICE(dip)))
1129 	PMD(PMD_SCAN, ("%s: %s@%s(%s#%d): kuc is %d\n", pmf, PM_DEVICE(dip),
1130 	    PM_KUC(dip)))
1131 
1132 	/* no scan under the following conditions */
1133 	if (pm_scans_disabled || !autopm_enabled ||
1134 	    (scanp->ps_scan_flags & PM_SCAN_STOP) ||
1135 	    (PM_KUC(dip) != 0) ||
1136 	    PM_ISDIRECT(dip) || pm_noinvol(dip)) {
1137 		PM_UNLOCK_DIP(dip);
1138 		PMD(PMD_SCAN, ("%s: [END, %s@%s(%s#%d)] no scan, "
1139 		    "scan_disabled(%d), apm_enabled(%d), kuc(%d), "
1140 		    "%s directpm, %s pm_noinvol\n", pmf, PM_DEVICE(dip),
1141 		    pm_scans_disabled, autopm_enabled, PM_KUC(dip),
1142 		    PM_ISDIRECT(dip) ? "is" : "is not",
1143 		    pm_noinvol(dip) ? "is" : "is not"))
1144 		return (LONG_MAX);
1145 	}
1146 	PM_UNLOCK_DIP(dip);
1147 
1148 	if (!ndi_devi_tryenter(pdip, &circ)) {
1149 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) can't hold pdip",
1150 		    pmf, PM_DEVICE(pdip)))
1151 		return ((time_t)1);
1152 	}
1153 	now = gethrestime_sec();
1154 	size = PM_NUMCMPTS(dip) * sizeof (time_t);
1155 	timestamp = kmem_alloc(size, KM_SLEEP);
1156 	pm_get_timestamps(dip, timestamp);
1157 
1158 	/*
1159 	 * Since we removed support for backwards compatible devices,
1160 	 * (see big comment at top of file)
1161 	 * it is no longer required to deal with component 0 last.
1162 	 */
1163 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
1164 		/*
1165 		 * If already off (an optimization, perhaps)
1166 		 */
1167 		cp = PM_CP(dip, i);
1168 		pwrndx = cp->pmc_cur_pwr;
1169 		curpwr = (pwrndx == PM_LEVEL_UNKNOWN) ?
1170 		    PM_LEVEL_UNKNOWN :
1171 		    cp->pmc_comp.pmc_lvals[pwrndx];
1172 
1173 		if (pwrndx == 0) {
1174 			PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d off or "
1175 			    "lowest\n", pmf, PM_DEVICE(dip), i))
1176 			/* skip device if off or at its lowest */
1177 			continue;
1178 		}
1179 
1180 		thresh = cur_threshold(dip, i);		/* comp i threshold */
1181 		if ((timestamp[i] == 0) || (cp->pmc_busycount > 0)) {
1182 			/* were busy or newly became busy by another thread */
1183 			if (timeleft == 0)
1184 				timeleft = max(thresh, pm_min_scan);
1185 			else
1186 				timeleft = min(
1187 				    timeleft, max(thresh, pm_min_scan));
1188 			continue;
1189 		}
1190 
1191 		idletime = now - timestamp[i];		/* idle time */
1192 		PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d idle time %lx\n",
1193 		    pmf, PM_DEVICE(dip), i, idletime))
1194 		if (idletime >= thresh || PM_IS_PID(dip)) {
1195 			nxtpwr = pm_next_lower_power(cp, pwrndx);
1196 			PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d, %d->%d\n",
1197 			    pmf, PM_DEVICE(dip), i, curpwr, nxtpwr))
1198 			if (pm_set_power(dip, i, nxtpwr, PM_LEVEL_DOWNONLY,
1199 			    PM_CANBLOCK_FAIL, 1, &unused) != DDI_SUCCESS &&
1200 			    PM_CURPOWER(dip, i) != nxtpwr) {
1201 				PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d, "
1202 				    "%d->%d Failed\n", pmf, PM_DEVICE(dip),
1203 				    i, curpwr, nxtpwr))
1204 				timeleft = pm_min_scan;
1205 				continue;
1206 			} else {
1207 				PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d, "
1208 				    "%d->%d, GOOD curpwr %d\n", pmf,
1209 				    PM_DEVICE(dip), i, curpwr, nxtpwr,
1210 				    cur_power(cp)))
1211 
1212 				if (nxtpwr == 0)	/* component went off */
1213 					continue;
1214 
1215 				/*
1216 				 * scan to next lower level
1217 				 */
1218 				if (timeleft == 0)
1219 					timeleft = max(
1220 					    1, cur_threshold(dip, i));
1221 				else
1222 					timeleft = min(timeleft,
1223 					    max(1, cur_threshold(dip, i)));
1224 				PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d, "
1225 				    "timeleft(%lx)\n", pmf, PM_DEVICE(dip),
1226 				    i, timeleft))
1227 			}
1228 		} else {	/* comp not idle long enough */
1229 			if (timeleft == 0)
1230 				timeleft = thresh - idletime;
1231 			else
1232 				timeleft = min(timeleft, (thresh - idletime));
1233 			PMD(PMD_SCAN, ("%s: %s@%s(%s#%d) comp %d, timeleft="
1234 			    "%lx\n", pmf, PM_DEVICE(dip), i, timeleft))
1235 		}
1236 	}
1237 	ndi_devi_exit(pdip, circ);
1238 	kmem_free(timestamp, size);
1239 	PMD(PMD_SCAN, ("%s: [END %s@%s(%s#%d)] timeleft(%lx)\n", pmf,
1240 	    PM_DEVICE(dip), timeleft))
1241 
1242 	/*
1243 	 * if components are already at lowest level, timeleft is left 0
1244 	 */
1245 	return ((timeleft == 0) ? LONG_MAX : timeleft);
1246 }
1247 
1248 /*
1249  * pm_scan_stop - cancel scheduled pm_rescan,
1250  *                wait for termination of dispatched pm_scan thread
1251  *                     and active pm_scan_dev thread.
1252  */
1253 void
1254 pm_scan_stop(dev_info_t *dip)
1255 {
1256 	PMD_FUNC(pmf, "scan_stop")
1257 	pm_scan_t	*scanp;
1258 	timeout_id_t	scanid;
1259 
1260 	PMD(PMD_SCAN, ("%s: [BEGIN %s@%s(%s#%d)]\n", pmf, PM_DEVICE(dip)))
1261 	PM_LOCK_DIP(dip);
1262 	scanp = PM_GET_PM_SCAN(dip);
1263 	if (!scanp) {
1264 		PMD(PMD_SCAN, ("%s: [END %s@%s(%s#%d)] scan not initialized\n",
1265 		    pmf, PM_DEVICE(dip)))
1266 		PM_UNLOCK_DIP(dip);
1267 		return;
1268 	}
1269 	scanp->ps_scan_flags |= PM_SCAN_STOP;
1270 
1271 	/* cancel scheduled scan taskq */
1272 	while (scanp->ps_scan_id) {
1273 		scanid = scanp->ps_scan_id;
1274 		scanp->ps_scan_id = 0;
1275 		PM_UNLOCK_DIP(dip);
1276 		(void) untimeout(scanid);
1277 		PM_LOCK_DIP(dip);
1278 	}
1279 
1280 	while (scanp->ps_scan_flags & (PM_SCANNING | PM_SCAN_DISPATCHED)) {
1281 		PM_UNLOCK_DIP(dip);
1282 		delay(1);
1283 		PM_LOCK_DIP(dip);
1284 	}
1285 	PM_UNLOCK_DIP(dip);
1286 	PMD(PMD_SCAN, ("%s: [END %s@%s(%s#%d)]\n", pmf, PM_DEVICE(dip)))
1287 }
1288 
1289 int
1290 pm_scan_stop_walk(dev_info_t *dip, void *arg)
1291 {
1292 	_NOTE(ARGUNUSED(arg))
1293 
1294 	if (!PM_GET_PM_SCAN(dip))
1295 		return (DDI_WALK_CONTINUE);
1296 	ASSERT(!PM_ISBC(dip));
1297 	pm_scan_stop(dip);
1298 	return (DDI_WALK_CONTINUE);
1299 }
1300 
1301 /*
1302  * Converts a power level value to its index
1303  */
1304 static int
1305 power_val_to_index(pm_component_t *cp, int val)
1306 {
1307 	int limit, i, *ip;
1308 
1309 	ASSERT(val != PM_LEVEL_UPONLY && val != PM_LEVEL_DOWNONLY &&
1310 	    val != PM_LEVEL_EXACT);
1311 	/*  convert power value into index (i) */
1312 	limit = cp->pmc_comp.pmc_numlevels;
1313 	ip = cp->pmc_comp.pmc_lvals;
1314 	for (i = 0; i < limit; i++)
1315 		if (val == *ip++)
1316 			return (i);
1317 	return (-1);
1318 }
1319 
1320 /*
1321  * Converts a numeric power level to a printable string
1322  */
1323 static char *
1324 power_val_to_string(pm_component_t *cp, int val)
1325 {
1326 	int index;
1327 
1328 	if (val == PM_LEVEL_UPONLY)
1329 		return ("<UPONLY>");
1330 
1331 	if (val == PM_LEVEL_UNKNOWN ||
1332 	    (index = power_val_to_index(cp, val)) == -1)
1333 		return ("<LEVEL_UNKNOWN>");
1334 
1335 	return (cp->pmc_comp.pmc_lnames[index]);
1336 }
1337 
1338 /*
1339  * Return true if this node has been claimed by a ppm.
1340  */
1341 static int
1342 pm_ppm_claimed(dev_info_t *dip)
1343 {
1344 	return (PPM(dip) != NULL);
1345 }
1346 
1347 /*
1348  * A node which was voluntarily power managed has just used up its "free cycle"
1349  * and need is volpmd field cleared, and the same done to all its descendents
1350  */
1351 static void
1352 pm_clear_volpm_dip(dev_info_t *dip)
1353 {
1354 	PMD_FUNC(pmf, "clear_volpm_dip")
1355 
1356 	if (dip == NULL)
1357 		return;
1358 	PMD(PMD_NOINVOL, ("%s: clear volpm from %s@%s(%s#%d)\n", pmf,
1359 	    PM_DEVICE(dip)))
1360 	DEVI(dip)->devi_pm_volpmd = 0;
1361 	for (dip = ddi_get_child(dip); dip; dip = ddi_get_next_sibling(dip)) {
1362 		pm_clear_volpm_dip(dip);
1363 	}
1364 }
1365 
1366 /*
1367  * A node which was voluntarily power managed has used up the "free cycles"
1368  * for the subtree that it is the root of.  Scan through the list of detached
1369  * nodes and adjust the counts of any that are descendents of the node.
1370  */
1371 static void
1372 pm_clear_volpm_list(dev_info_t *dip)
1373 {
1374 	PMD_FUNC(pmf, "clear_volpm_list")
1375 	char	*pathbuf;
1376 	size_t	len;
1377 	pm_noinvol_t *ip;
1378 
1379 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1380 	(void) ddi_pathname(dip, pathbuf);
1381 	len = strlen(pathbuf);
1382 	PMD(PMD_NOINVOL, ("%s: clear volpm list %s\n", pmf, pathbuf))
1383 	rw_enter(&pm_noinvol_rwlock, RW_WRITER);
1384 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next) {
1385 		PMD(PMD_NOINVOL, ("%s: clear volpm: ni_path %s\n", pmf,
1386 		    ip->ni_path))
1387 		if (strncmp(pathbuf, ip->ni_path, len) == 0 &&
1388 		    ip->ni_path[len] == '/') {
1389 			PMD(PMD_NOINVOL, ("%s: clear volpm: %s\n", pmf,
1390 			    ip->ni_path))
1391 			ip->ni_volpmd = 0;
1392 			ip->ni_wasvolpmd = 0;
1393 		}
1394 	}
1395 	kmem_free(pathbuf, MAXPATHLEN);
1396 	rw_exit(&pm_noinvol_rwlock);
1397 }
1398 
1399 /*
1400  * Powers a device, suspending or resuming the driver if it is a backward
1401  * compatible device, calling into ppm to change power level.
1402  * Called with the component's power lock held.
1403  */
1404 static int
1405 power_dev(dev_info_t *dip, int comp, int level, int old_level,
1406     pm_canblock_t canblock, pm_ppm_devlist_t **devlist)
1407 {
1408 	PMD_FUNC(pmf, "power_dev")
1409 	power_req_t power_req;
1410 	int		power_op_ret;	/* DDI_SUCCESS or DDI_FAILURE */
1411 	int		resume_needed = 0;
1412 	int		suspended = 0;
1413 	int		result;
1414 	struct pm_component *cp = PM_CP(dip, comp);
1415 	int		bc = PM_ISBC(dip);
1416 	int pm_all_components_off(dev_info_t *);
1417 	int		clearvolpmd = 0;
1418 	char		pathbuf[MAXNAMELEN];
1419 #ifdef DEBUG
1420 	char *ppmname, *ppmaddr;
1421 #endif
1422 	/*
1423 	 * If this is comp 0 of a backwards compat device and we are
1424 	 * going to take the power away, we need to detach it with
1425 	 * DDI_PM_SUSPEND command.
1426 	 */
1427 	if (bc && comp == 0 && POWERING_OFF(old_level, level)) {
1428 		if (devi_detach(dip, DDI_PM_SUSPEND) != DDI_SUCCESS) {
1429 			/* We could not suspend before turning cmpt zero off */
1430 			PMD(PMD_ERROR, ("%s: could not suspend %s@%s(%s#%d)\n",
1431 			    pmf, PM_DEVICE(dip)))
1432 			return (DDI_FAILURE);
1433 		} else {
1434 			DEVI(dip)->devi_pm_flags |= PMC_SUSPENDED;
1435 			suspended++;
1436 		}
1437 	}
1438 	power_req.request_type = PMR_PPM_SET_POWER;
1439 	power_req.req.ppm_set_power_req.who = dip;
1440 	power_req.req.ppm_set_power_req.cmpt = comp;
1441 	power_req.req.ppm_set_power_req.old_level = old_level;
1442 	power_req.req.ppm_set_power_req.new_level = level;
1443 	power_req.req.ppm_set_power_req.canblock = canblock;
1444 	power_req.req.ppm_set_power_req.cookie = NULL;
1445 #ifdef DEBUG
1446 	if (pm_ppm_claimed(dip)) {
1447 		ppmname = PM_NAME(PPM(dip));
1448 		ppmaddr = PM_ADDR(PPM(dip));
1449 
1450 	} else {
1451 		ppmname = "noppm";
1452 		ppmaddr = "0";
1453 	}
1454 	PMD(PMD_PPM, ("%s: %s@%s(%s#%d):%s[%d] %s (%d) -> %s (%d) via %s@%s\n",
1455 	    pmf, PM_DEVICE(dip), cp->pmc_comp.pmc_name, comp,
1456 	    power_val_to_string(cp, old_level), old_level,
1457 	    power_val_to_string(cp, level), level, ppmname, ppmaddr))
1458 #endif
1459 	/*
1460 	 * If non-bc noinvolpm device is turning first comp on, or noinvolpm
1461 	 * bc device comp 0 is powering on, then we count it as a power cycle
1462 	 * against its voluntary count.
1463 	 */
1464 	if (DEVI(dip)->devi_pm_volpmd &&
1465 	    (!bc && pm_all_components_off(dip) && level != 0) ||
1466 	    (bc && comp == 0 && POWERING_ON(old_level, level)))
1467 		clearvolpmd = 1;
1468 	if ((power_op_ret = pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER,
1469 	    &power_req, &result)) == DDI_SUCCESS) {
1470 		/*
1471 		 * Now do involuntary pm accounting;  If we've just cycled power
1472 		 * on a voluntarily pm'd node, and by inference on its entire
1473 		 * subtree, we need to set the subtree (including those nodes
1474 		 * already detached) volpmd counts to 0, and subtract out the
1475 		 * value of the current node's volpmd count from the ancestors
1476 		 */
1477 		if (clearvolpmd) {
1478 			int volpmd = DEVI(dip)->devi_pm_volpmd;
1479 			pm_clear_volpm_dip(dip);
1480 			pm_clear_volpm_list(dip);
1481 			if (volpmd) {
1482 				(void) ddi_pathname(dip, pathbuf);
1483 				(void) pm_noinvol_update(PM_BP_NOINVOL_POWER,
1484 				    volpmd, 0, pathbuf, dip);
1485 			}
1486 		}
1487 	} else {
1488 		PMD(PMD_FAIL, ("%s: can't set comp %d (%s) of %s@%s(%s#%d) "
1489 		    "to level %d (%s)\n", pmf, comp, cp->pmc_comp.pmc_name,
1490 		    PM_DEVICE(dip), level, power_val_to_string(cp, level)))
1491 	}
1492 	/*
1493 	 * If some other devices were also powered up (e.g. other cpus in
1494 	 * the same domain) return a pointer to that list
1495 	 */
1496 	if (devlist) {
1497 		*devlist = (pm_ppm_devlist_t *)
1498 		    power_req.req.ppm_set_power_req.cookie;
1499 	}
1500 	/*
1501 	 * We will have to resume the device if the device is backwards compat
1502 	 * device and either of the following is true:
1503 	 * -This is comp 0 and we have successfully powered it up
1504 	 * -This is comp 0 and we have failed to power it down. Resume is
1505 	 *  needed because we have suspended it above
1506 	 */
1507 
1508 	if (bc && comp == 0) {
1509 		ASSERT(PM_ISDIRECT(dip) || DEVI_IS_DETACHING(dip));
1510 		if (power_op_ret == DDI_SUCCESS) {
1511 			if (POWERING_ON(old_level, level)) {
1512 				/*
1513 				 * It must be either suspended or resumed
1514 				 * via pm_power_has_changed path
1515 				 */
1516 				ASSERT((DEVI(dip)->devi_pm_flags &
1517 				    PMC_SUSPENDED) ||
1518 				    (PM_CP(dip, comp)->pmc_flags &
1519 				    PM_PHC_WHILE_SET_POWER));
1520 
1521 					resume_needed = suspended;
1522 			}
1523 		} else {
1524 			if (POWERING_OFF(old_level, level)) {
1525 				/*
1526 				 * It must be either suspended or resumed
1527 				 * via pm_power_has_changed path
1528 				 */
1529 				ASSERT((DEVI(dip)->devi_pm_flags &
1530 				    PMC_SUSPENDED) ||
1531 				    (PM_CP(dip, comp)->pmc_flags &
1532 				    PM_PHC_WHILE_SET_POWER));
1533 
1534 					resume_needed = suspended;
1535 			}
1536 		}
1537 	}
1538 	if (resume_needed) {
1539 		ASSERT(DEVI(dip)->devi_pm_flags & PMC_SUSPENDED);
1540 		/* ppm is not interested in DDI_PM_RESUME */
1541 		if ((power_op_ret = devi_attach(dip, DDI_PM_RESUME)) ==
1542 		    DDI_SUCCESS) {
1543 			DEVI(dip)->devi_pm_flags &= ~PMC_SUSPENDED;
1544 		} else
1545 			cmn_err(CE_WARN, "!pm: Can't resume %s@%s(%s#%d)",
1546 			    PM_DEVICE(dip));
1547 	}
1548 	return (power_op_ret);
1549 }
1550 
1551 /*
1552  * Return true if we are the owner or a borrower of the devi lock.  See
1553  * pm_lock_power_single() about borrowing the lock.
1554  */
1555 static int
1556 pm_devi_lock_held(dev_info_t *dip)
1557 {
1558 	lock_loan_t *cur;
1559 
1560 	if (DEVI_BUSY_OWNED(dip))
1561 	    return (1);
1562 
1563 	/* return false if no locks borrowed */
1564 	if (lock_loan_head.pmlk_next == NULL)
1565 		return (0);
1566 
1567 	mutex_enter(&pm_loan_lock);
1568 	/* see if our thread is registered as a lock borrower. */
1569 	for (cur = lock_loan_head.pmlk_next; cur; cur = cur->pmlk_next)
1570 		if (cur->pmlk_borrower == curthread)
1571 			break;
1572 	mutex_exit(&pm_loan_lock);
1573 
1574 	return (cur != NULL && cur->pmlk_lender == DEVI(dip)->devi_busy_thread);
1575 }
1576 
1577 /*
1578  * pm_set_power: adjusts power level of device.	 Assumes device is power
1579  * manageable & component exists.
1580  *
1581  * Cases which require us to bring up devices we keep up ("wekeepups") for
1582  * backwards compatible devices:
1583  *	component 0 is off and we're bringing it up from 0
1584  *		bring up wekeepup first
1585  *	and recursively when component 0 is off and we bring some other
1586  *	component up from 0
1587  * For devices which are not backward compatible, our dependency notion is much
1588  * simpler.  Unless all components are off, then wekeeps must be on.
1589  * We don't treat component 0 differently.
1590  * Canblock tells how to deal with a direct pm'd device.
1591  * Scan arg tells us if we were called from scan, in which case we don't need
1592  * to go back to the root node and walk down to change power.
1593  */
1594 int
1595 pm_set_power(dev_info_t *dip, int comp, int level, int direction,
1596     pm_canblock_t canblock, int scan, int *retp)
1597 {
1598 	PMD_FUNC(pmf, "set_power")
1599 	char		*pathbuf;
1600 	pm_bp_child_pwrchg_t bpc;
1601 	pm_sp_misc_t	pspm;
1602 	int		ret = DDI_SUCCESS;
1603 	int		unused = DDI_SUCCESS;
1604 	dev_info_t	*pdip = ddi_get_parent(dip);
1605 
1606 #ifdef DEBUG
1607 	int		diverted = 0;
1608 
1609 	/*
1610 	 * This prevents operations on the console from calling prom_printf and
1611 	 * either deadlocking or bringing up the console because of debug
1612 	 * output
1613 	 */
1614 	if (dip == cfb_dip) {
1615 		diverted++;
1616 		mutex_enter(&pm_debug_lock);
1617 		pm_divertdebug++;
1618 		mutex_exit(&pm_debug_lock);
1619 	}
1620 #endif
1621 	ASSERT(direction == PM_LEVEL_UPONLY || direction == PM_LEVEL_DOWNONLY ||
1622 	    direction == PM_LEVEL_EXACT);
1623 	PMD(PMD_SET, ("%s: %s@%s(%s#%d), comp=%d, dir=%s, new=%d\n",
1624 	    pmf, PM_DEVICE(dip), comp, pm_decode_direction(direction), level))
1625 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1626 	(void) ddi_pathname(dip, pathbuf);
1627 	bpc.bpc_dip = dip;
1628 	bpc.bpc_path = pathbuf;
1629 	bpc.bpc_comp = comp;
1630 	bpc.bpc_olevel = PM_CURPOWER(dip, comp);
1631 	bpc.bpc_nlevel = level;
1632 	pspm.pspm_direction = direction;
1633 	pspm.pspm_errnop = retp;
1634 	pspm.pspm_canblock = canblock;
1635 	pspm.pspm_scan = scan;
1636 	bpc.bpc_private = &pspm;
1637 
1638 	/*
1639 	 * If a config operation is being done (we've locked the parent) or
1640 	 * we already hold the power lock (we've locked the node)
1641 	 * then we can operate directly on the node because we have already
1642 	 * brought up all the ancestors, otherwise, we have to go back to the
1643 	 * top of the tree.
1644 	 */
1645 	if (pm_devi_lock_held(pdip) || pm_devi_lock_held(dip))
1646 		ret = pm_busop_set_power(dip, NULL, BUS_POWER_CHILD_PWRCHG,
1647 		    (void *)&bpc, (void *)&unused);
1648 	else
1649 		ret = pm_busop_bus_power(ddi_root_node(), NULL,
1650 		    BUS_POWER_CHILD_PWRCHG, (void *)&bpc, (void *)&unused);
1651 #ifdef DEBUG
1652 	if (ret != DDI_SUCCESS || *retp != DDI_SUCCESS) {
1653 		PMD(PMD_ERROR, ("%s: %s@%s(%s#%d) can't change power, ret=%d, "
1654 		    "errno=%d\n", pmf, PM_DEVICE(dip), ret, *retp))
1655 	}
1656 	if (diverted) {
1657 		mutex_enter(&pm_debug_lock);
1658 		pm_divertdebug--;
1659 		mutex_exit(&pm_debug_lock);
1660 	}
1661 #endif
1662 	kmem_free(pathbuf, MAXPATHLEN);
1663 	return (ret);
1664 }
1665 
1666 
1667 static dev_info_t *
1668 find_dip(dev_info_t *dip, char *dev_name, int holddip)
1669 {
1670 	PMD_FUNC(pmf, "find_dip")
1671 	dev_info_t	*cdip;
1672 	char		*child_dev, *addr;
1673 	char		*device;	/* writeable copy of path */
1674 	int		dev_len = strlen(dev_name)+1;
1675 	int		circ;
1676 
1677 	device = kmem_zalloc(dev_len, KM_SLEEP);
1678 	(void) strcpy(device, dev_name);
1679 	addr = strchr(device, '@');
1680 	child_dev = strchr(device, '/');
1681 	if ((addr != NULL) && (child_dev == NULL || addr < child_dev)) {
1682 		/*
1683 		 * We have device = "name@addr..." form
1684 		 */
1685 		*addr++ = '\0';			/* for strcmp (and skip '@') */
1686 		if (child_dev != NULL)
1687 			*child_dev++ = '\0';	/* for strcmp (and skip '/') */
1688 	} else {
1689 		/*
1690 		 * We have device = "name/..." or "name"
1691 		 */
1692 		addr = "";
1693 		if (child_dev != NULL)
1694 			*child_dev++ = '\0';	/* for strcmp (and skip '/') */
1695 	}
1696 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
1697 		if (strcmp(ddi_node_name(dip), device) == 0) {
1698 			/* If the driver isn't loaded, we prune the search */
1699 			if (!i_ddi_devi_attached(dip)) {
1700 				continue;
1701 			}
1702 			if (strcmp(ddi_get_name_addr(dip), addr) == 0) {
1703 				PMD(PMD_NAMETODIP, ("%s: matched %s@%s"
1704 				    "(%s#%d)\n", pmf, PM_DEVICE(dip)))
1705 				if (child_dev != NULL) {
1706 					PMD(PMD_NAMETODIP, ("%s: %s@%s(%s#%d): "
1707 					    "held, call find_dip %s\n", pmf,
1708 					    PM_DEVICE(dip), child_dev))
1709 					ndi_devi_enter(dip, &circ);
1710 					cdip = dip;
1711 					dip = find_dip(ddi_get_child(dip),
1712 					    child_dev, holddip);
1713 					ndi_devi_exit(cdip, circ);
1714 					PMD(PMD_NAMETODIP, ("%s: %s@%s(%s#%d): "
1715 					    "release, find_dip rets %s\n", pmf,
1716 					    PM_DEVICE(cdip), child_dev))
1717 				} else {
1718 					if (holddip) {
1719 						e_ddi_hold_devi(dip);
1720 						PMD(PMD_DHR | PMD_NAMETODIP,
1721 						    ("%s: held %s@%s(%s#%d), "
1722 						    "refcnt=%d\n", pmf,
1723 						    PM_DEVICE(dip),
1724 						    e_ddi_devi_holdcnt(dip)))
1725 					}
1726 				}
1727 				kmem_free(device, dev_len);
1728 				return (dip);
1729 			}
1730 		}
1731 	}
1732 	kmem_free(device, dev_len);
1733 	return (dip);
1734 }
1735 
1736 /*
1737  * If holddip is set, then if a dip is found we return with the node held
1738  */
1739 dev_info_t *
1740 pm_name_to_dip(char *pathname, int holddip)
1741 {
1742 	PMD_FUNC(pmf, "name_to_dip")
1743 	dev_info_t	*dip = NULL;
1744 	char		dev_name[MAXNAMELEN];
1745 	dev_info_t	*first_child;
1746 	int		circular;
1747 
1748 	if (!pathname)
1749 		return (NULL);
1750 
1751 	(void) strncpy(dev_name, pathname, MAXNAMELEN);
1752 
1753 	PMD(PMD_NAMETODIP, ("%s: devname: %s\n", pmf, dev_name))
1754 
1755 	/*
1756 	 * First we attempt to match the node in the tree.  If we succeed
1757 	 * we hold the driver and look up the dip again.
1758 	 * No need to hold the root as that node is always held.
1759 	 */
1760 	if (dev_name[0] == '/') {
1761 		ndi_devi_enter(ddi_root_node(), &circular);
1762 		first_child = ddi_get_child(ddi_root_node());
1763 		dip = find_dip(first_child, dev_name + 1, holddip);
1764 		ndi_devi_exit(ddi_root_node(), circular);
1765 
1766 	} else {
1767 		PMD(PMD_NAMETODIP, ("%s: physpath with unrooted "
1768 		    "search\n", pmf))
1769 		return (NULL);
1770 	}
1771 
1772 	ASSERT(!dip ||
1773 	    (ddi_name_to_major(ddi_binding_name(dip)) != (major_t)-1));
1774 
1775 	return (dip);
1776 }
1777 
1778 /*
1779  * Search for a dependency and mark it unsatisfied
1780  */
1781 static void
1782 pm_unsatisfy(char *keeper, char *kept)
1783 {
1784 	PMD_FUNC(pmf, "unsatisfy")
1785 	pm_pdr_t *dp;
1786 
1787 	PMD(PMD_KEEPS, ("%s: keeper=%s, kept=%s\n", pmf, keeper, kept))
1788 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
1789 		if (!dp->pdr_isprop) {
1790 			if (strcmp(dp->pdr_keeper, keeper) == 0 &&
1791 			    (dp->pdr_kept_count > 0) &&
1792 			    strcmp(dp->pdr_kept_paths[0], kept) == 0) {
1793 				if (dp->pdr_satisfied) {
1794 					dp->pdr_satisfied = 0;
1795 					pm_unresolved_deps++;
1796 					PMD(PMD_KEEPS, ("%s: clear satisfied, "
1797 					    "pm_unresolved_deps now %d\n", pmf,
1798 					    pm_unresolved_deps))
1799 				}
1800 			}
1801 		}
1802 	}
1803 }
1804 
1805 /*
1806  * Device dip is being un power managed, it keeps up count other devices.
1807  * We need to release any hold we have on the kept devices, and also
1808  * mark the dependency no longer satisfied.
1809  */
1810 static void
1811 pm_unkeeps(int count, char *keeper, char **keptpaths, int pwr)
1812 {
1813 	PMD_FUNC(pmf, "unkeeps")
1814 	int i, j;
1815 	dev_info_t *kept;
1816 	dev_info_t *dip;
1817 	struct pm_component *cp;
1818 	int keeper_on = 0, circ;
1819 
1820 	PMD(PMD_KEEPS, ("%s: count=%d, keeper=%s, keptpaths=%p\n", pmf, count,
1821 	    keeper, (void *)keptpaths))
1822 	/*
1823 	 * Try to grab keeper. Keeper may have gone away by now,
1824 	 * in this case, used the passed in value pwr
1825 	 */
1826 	dip = pm_name_to_dip(keeper, 1);
1827 	for (i = 0; i < count; i++) {
1828 		/* Release power hold */
1829 		kept = pm_name_to_dip(keptpaths[i], 1);
1830 		if (kept) {
1831 			PMD(PMD_KEEPS, ("%s: %s@%s(%s#%d)[%d]\n", pmf,
1832 			    PM_DEVICE(kept), i))
1833 			/*
1834 			 * We need to check if we skipped a bringup here
1835 			 * because we could have failed the bringup
1836 			 * (ie DIRECT PM device) and have
1837 			 * not increment the count.
1838 			 */
1839 			if ((dip != NULL) && (PM_GET_PM_INFO(dip) != NULL)) {
1840 				keeper_on = 0;
1841 				PM_LOCK_POWER(dip, &circ);
1842 				for (j = 0; j < PM_NUMCMPTS(dip); j++) {
1843 				    cp = &DEVI(dip)->devi_pm_components[j];
1844 					if (cur_power(cp)) {
1845 						keeper_on++;
1846 						break;
1847 					}
1848 				}
1849 				if (keeper_on && (PM_SKBU(kept) == 0)) {
1850 					pm_rele_power(kept);
1851 					DEVI(kept)->devi_pm_flags
1852 						&= ~PMC_SKIP_BRINGUP;
1853 				}
1854 				PM_UNLOCK_POWER(dip, circ);
1855 			} else if (pwr) {
1856 				if (PM_SKBU(kept) == 0) {
1857 					pm_rele_power(kept);
1858 					DEVI(kept)->devi_pm_flags
1859 					    &= ~PMC_SKIP_BRINGUP;
1860 				}
1861 			}
1862 			ddi_release_devi(kept);
1863 		}
1864 		/*
1865 		 * mark this dependency not satisfied
1866 		 */
1867 		pm_unsatisfy(keeper, keptpaths[i]);
1868 	}
1869 	if (dip)
1870 		ddi_release_devi(dip);
1871 }
1872 
1873 /*
1874  * Device kept is being un power managed, it is kept up by keeper.
1875  * We need to mark the dependency no longer satisfied.
1876  */
1877 static void
1878 pm_unkepts(char *kept, char *keeper)
1879 {
1880 	PMD_FUNC(pmf, "unkepts")
1881 	PMD(PMD_KEEPS, ("%s: kept=%s, keeper=%s\n", pmf, kept, keeper))
1882 	ASSERT(keeper != NULL);
1883 	/*
1884 	 * mark this dependency not satisfied
1885 	 */
1886 	pm_unsatisfy(keeper, kept);
1887 }
1888 
1889 /*
1890  * Removes dependency information and hold on the kepts, if the path is a
1891  * path of a keeper.
1892  */
1893 static void
1894 pm_free_keeper(char *path, int pwr)
1895 {
1896 	pm_pdr_t *dp;
1897 	int i;
1898 	size_t length;
1899 
1900 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
1901 		if (strcmp(dp->pdr_keeper, path) != 0)
1902 			continue;
1903 		/*
1904 		 * Remove all our kept holds and the dependency records,
1905 		 * then free up the kept lists.
1906 		 */
1907 		pm_unkeeps(dp->pdr_kept_count, path, dp->pdr_kept_paths, pwr);
1908 		if (dp->pdr_kept_count)  {
1909 			for (i = 0; i < dp->pdr_kept_count; i++) {
1910 				length = strlen(dp->pdr_kept_paths[i]);
1911 				kmem_free(dp->pdr_kept_paths[i], length + 1);
1912 			}
1913 			kmem_free(dp->pdr_kept_paths,
1914 			    dp->pdr_kept_count * sizeof (char **));
1915 			dp->pdr_kept_paths = NULL;
1916 			dp->pdr_kept_count = 0;
1917 		}
1918 	}
1919 }
1920 
1921 /*
1922  * Removes the device represented by path from the list of kepts, if the
1923  * path is a path of a kept
1924  */
1925 static void
1926 pm_free_kept(char *path)
1927 {
1928 	pm_pdr_t *dp;
1929 	int i;
1930 	int j, count;
1931 	size_t length;
1932 	char **paths;
1933 
1934 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
1935 		if (dp->pdr_kept_count == 0)
1936 			continue;
1937 		count = dp->pdr_kept_count;
1938 		/* Remove this device from the kept path lists */
1939 		for (i = 0; i < count; i++) {
1940 			if (strcmp(dp->pdr_kept_paths[i], path) == 0) {
1941 				pm_unkepts(path, dp->pdr_keeper);
1942 				length = strlen(dp->pdr_kept_paths[i]) + 1;
1943 				kmem_free(dp->pdr_kept_paths[i], length);
1944 				dp->pdr_kept_paths[i] = NULL;
1945 				dp->pdr_kept_count--;
1946 			}
1947 		}
1948 		/* Compact the kept paths array */
1949 		if (dp->pdr_kept_count) {
1950 			length = dp->pdr_kept_count * sizeof (char **);
1951 			paths = kmem_zalloc(length, KM_SLEEP);
1952 			j = 0;
1953 			for (i = 0; i < count; i++) {
1954 				if (dp->pdr_kept_paths[i] != NULL) {
1955 					paths[j] = dp->pdr_kept_paths[i];
1956 					j++;
1957 				}
1958 			}
1959 			ASSERT(j == dp->pdr_kept_count);
1960 		}
1961 		/* Now free the old array and point to the new one */
1962 		kmem_free(dp->pdr_kept_paths, count * sizeof (char **));
1963 		if (dp->pdr_kept_count)
1964 			dp->pdr_kept_paths = paths;
1965 		else
1966 			dp->pdr_kept_paths = NULL;
1967 	}
1968 }
1969 
1970 /*
1971  * Free the dependency information for a device.
1972  */
1973 void
1974 pm_free_keeps(char *path, int pwr)
1975 {
1976 	PMD_FUNC(pmf, "free_keeps")
1977 
1978 #ifdef DEBUG
1979 	int doprdeps = 0;
1980 	void prdeps(char *);
1981 
1982 	PMD(PMD_KEEPS, ("%s: %s\n", pmf, path))
1983 	if (pm_debug & PMD_KEEPS) {
1984 		doprdeps = 1;
1985 		prdeps("pm_free_keeps before");
1986 	}
1987 #endif
1988 	/*
1989 	 * First assume we are a keeper and remove all our kepts.
1990 	 */
1991 	pm_free_keeper(path, pwr);
1992 	/*
1993 	 * Now assume we a kept device, and remove all our records.
1994 	 */
1995 	pm_free_kept(path);
1996 #ifdef	DEBUG
1997 	if (doprdeps) {
1998 		prdeps("pm_free_keeps after");
1999 	}
2000 #endif
2001 }
2002 
2003 static int
2004 pm_is_kept(char *path)
2005 {
2006 	pm_pdr_t *dp;
2007 	int i;
2008 
2009 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
2010 		if (dp->pdr_kept_count == 0)
2011 			continue;
2012 		for (i = 0; i < dp->pdr_kept_count; i++) {
2013 			if (strcmp(dp->pdr_kept_paths[i], path) == 0)
2014 				return (1);
2015 		}
2016 	}
2017 	return (0);
2018 }
2019 
2020 static void
2021 e_pm_hold_rele_power(dev_info_t *dip, int cnt)
2022 {
2023 	PMD_FUNC(pmf, "hold_rele_power")
2024 	int circ;
2025 
2026 	if ((dip == NULL) ||
2027 	    (PM_GET_PM_INFO(dip) == NULL) || PM_ISBC(dip))
2028 		return;
2029 	PM_LOCK_POWER(dip, &circ);
2030 	ASSERT(cnt >= 0 && PM_KUC(dip) >= 0 || cnt < 0 && PM_KUC(dip) > 0);
2031 	PMD(PMD_KIDSUP, ("%s: kidsupcnt for %s@%s(%s#%d) %d->%d\n", pmf,
2032 	    PM_DEVICE(dip), PM_KUC(dip), (PM_KUC(dip) + cnt)))
2033 	PM_KUC(dip) += cnt;
2034 	ASSERT(PM_KUC(dip) >= 0);
2035 	PM_UNLOCK_POWER(dip, circ);
2036 	if (cnt < 0 && PM_KUC(dip) == 0)
2037 		pm_rescan(dip);
2038 }
2039 
2040 #define	MAX_PPM_HANDLERS	4
2041 
2042 kmutex_t ppm_lock;	/* in case we ever do multi-threaded startup */
2043 
2044 struct	ppm_callbacks {
2045 	int (*ppmc_func)(dev_info_t *);
2046 	dev_info_t	*ppmc_dip;
2047 } ppm_callbacks[MAX_PPM_HANDLERS + 1];
2048 
2049 
2050 /*
2051  * This routine calls into all the registered ppms to notify them
2052  * that either all components of power-managed devices are at their
2053  * lowest levels or no longer all are at their lowest levels.
2054  */
2055 static void
2056 pm_ppm_notify_all_lowest(dev_info_t *dip, int mode)
2057 {
2058 	struct ppm_callbacks *ppmcp;
2059 	power_req_t power_req;
2060 	int result = 0;
2061 
2062 	power_req.request_type = PMR_PPM_ALL_LOWEST;
2063 	power_req.req.ppm_all_lowest_req.mode = mode;
2064 	mutex_enter(&ppm_lock);
2065 	for (ppmcp = ppm_callbacks; ppmcp->ppmc_func; ppmcp++)
2066 		(void) pm_ctlops((dev_info_t *)ppmcp->ppmc_dip, dip,
2067 		    DDI_CTLOPS_POWER, &power_req, &result);
2068 	mutex_exit(&ppm_lock);
2069 }
2070 
2071 static void
2072 pm_set_pm_info(dev_info_t *dip, void *value)
2073 {
2074 	DEVI(dip)->devi_pm_info = value;
2075 }
2076 
2077 pm_rsvp_t *pm_blocked_list;
2078 
2079 /*
2080  * Look up an entry in the blocked list by dip and component
2081  */
2082 static pm_rsvp_t *
2083 pm_rsvp_lookup(dev_info_t *dip, int comp)
2084 {
2085 	pm_rsvp_t *p;
2086 	ASSERT(MUTEX_HELD(&pm_rsvp_lock));
2087 	for (p = pm_blocked_list; p; p = p->pr_next)
2088 		if (p->pr_dip == dip && p->pr_comp == comp) {
2089 			return (p);
2090 		}
2091 	return (NULL);
2092 }
2093 
2094 /*
2095  * Called when a device which is direct power managed (or the parent or
2096  * dependent of such a device) changes power, or when a pm clone is closed
2097  * that was direct power managing a device.  This call results in pm_blocked()
2098  * (below) returning.
2099  */
2100 void
2101 pm_proceed(dev_info_t *dip, int cmd, int comp, int newlevel)
2102 {
2103 	PMD_FUNC(pmf, "proceed")
2104 	pm_rsvp_t *found = NULL;
2105 	pm_rsvp_t *p;
2106 
2107 	mutex_enter(&pm_rsvp_lock);
2108 	switch (cmd) {
2109 	/*
2110 	 * we're giving up control, let any pending op continue
2111 	 */
2112 	case PMP_RELEASE:
2113 		for (p = pm_blocked_list; p; p = p->pr_next) {
2114 			if (dip == p->pr_dip) {
2115 				p->pr_retval = PMP_RELEASE;
2116 				PMD(PMD_DPM, ("%s: RELEASE %s@%s(%s#%d)\n",
2117 				    pmf, PM_DEVICE(dip)))
2118 				cv_signal(&p->pr_cv);
2119 			}
2120 		}
2121 		break;
2122 
2123 	/*
2124 	 * process has done PM_SET_CURRENT_POWER; let a matching request
2125 	 * succeed and a non-matching request for the same device fail
2126 	 */
2127 	case PMP_SETPOWER:
2128 		found = pm_rsvp_lookup(dip, comp);
2129 		if (!found)	/* if driver not waiting */
2130 			break;
2131 		/*
2132 		 * This cannot be pm_lower_power, since that can only happen
2133 		 * during detach or probe
2134 		 */
2135 		if (found->pr_newlevel <= newlevel) {
2136 			found->pr_retval = PMP_SUCCEED;
2137 			PMD(PMD_DPM, ("%s: SUCCEED %s@%s(%s#%d)\n", pmf,
2138 			    PM_DEVICE(dip)))
2139 		} else {
2140 			found->pr_retval = PMP_FAIL;
2141 			PMD(PMD_DPM, ("%s: FAIL %s@%s(%s#%d)\n", pmf,
2142 			    PM_DEVICE(dip)))
2143 		}
2144 		cv_signal(&found->pr_cv);
2145 		break;
2146 
2147 	default:
2148 		panic("pm_proceed unknown cmd %d", cmd);
2149 	}
2150 	mutex_exit(&pm_rsvp_lock);
2151 }
2152 
2153 /*
2154  * This routine dispatches new work to the dependency thread. Caller must
2155  * be prepared to block for memory if necessary.
2156  */
2157 void
2158 pm_dispatch_to_dep_thread(int cmd, char *keeper, char *kept, int wait,
2159     int *res, int cached_pwr)
2160 {
2161 	pm_dep_wk_t	*new_work;
2162 
2163 	new_work = kmem_zalloc(sizeof (pm_dep_wk_t), KM_SLEEP);
2164 	new_work->pdw_type = cmd;
2165 	new_work->pdw_wait = wait;
2166 	new_work->pdw_done = 0;
2167 	new_work->pdw_ret = 0;
2168 	new_work->pdw_pwr = cached_pwr;
2169 	cv_init(&new_work->pdw_cv, NULL, CV_DEFAULT, NULL);
2170 	if (keeper != NULL) {
2171 		new_work->pdw_keeper = kmem_zalloc(strlen(keeper) + 1,
2172 		    KM_SLEEP);
2173 		(void) strcpy(new_work->pdw_keeper, keeper);
2174 	}
2175 	if (kept != NULL) {
2176 		new_work->pdw_kept = kmem_zalloc(strlen(kept) + 1, KM_SLEEP);
2177 		(void) strcpy(new_work->pdw_kept, kept);
2178 	}
2179 	mutex_enter(&pm_dep_thread_lock);
2180 	if (pm_dep_thread_workq == NULL) {
2181 		pm_dep_thread_workq = new_work;
2182 		pm_dep_thread_tail = new_work;
2183 		new_work->pdw_next = NULL;
2184 	} else {
2185 		pm_dep_thread_tail->pdw_next = new_work;
2186 		pm_dep_thread_tail = new_work;
2187 		new_work->pdw_next = NULL;
2188 	}
2189 	cv_signal(&pm_dep_thread_cv);
2190 	/* If caller asked for it, wait till it is done. */
2191 	if (wait)  {
2192 		while (!new_work->pdw_done)
2193 			cv_wait(&new_work->pdw_cv, &pm_dep_thread_lock);
2194 		/*
2195 		 * Pass return status, if any, back.
2196 		 */
2197 		if (res != NULL)
2198 			*res = new_work->pdw_ret;
2199 		/*
2200 		 * If we asked to wait, it is our job to free the request
2201 		 * structure.
2202 		 */
2203 		if (new_work->pdw_keeper)
2204 			kmem_free(new_work->pdw_keeper,
2205 			    strlen(new_work->pdw_keeper) + 1);
2206 		if (new_work->pdw_kept)
2207 			kmem_free(new_work->pdw_kept,
2208 			    strlen(new_work->pdw_kept) + 1);
2209 		kmem_free(new_work, sizeof (pm_dep_wk_t));
2210 	}
2211 	mutex_exit(&pm_dep_thread_lock);
2212 }
2213 
2214 /*
2215  * Release the pm resource for this device.
2216  */
2217 void
2218 pm_rem_info(dev_info_t *dip)
2219 {
2220 	PMD_FUNC(pmf, "rem_info")
2221 	int		i, count = 0;
2222 	pm_info_t	*info = PM_GET_PM_INFO(dip);
2223 	dev_info_t	*pdip = ddi_get_parent(dip);
2224 	char		*pathbuf;
2225 	int		work_type = PM_DEP_WK_DETACH;
2226 
2227 	ASSERT(info);
2228 
2229 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
2230 	if (PM_ISDIRECT(dip)) {
2231 		info->pmi_dev_pm_state &= ~PM_DIRECT;
2232 		ASSERT(info->pmi_clone);
2233 		info->pmi_clone = 0;
2234 		pm_proceed(dip, PMP_RELEASE, -1, -1);
2235 	}
2236 	ASSERT(!PM_GET_PM_SCAN(dip));
2237 
2238 	/*
2239 	 * Now adjust parent's kidsupcnt.  BC nodes we check only comp 0,
2240 	 * Others we check all components.  BC node that has already
2241 	 * called pm_destroy_components() has zero component count.
2242 	 * Parents that get notification are not adjusted because their
2243 	 * kidsupcnt is always 0 (or 1 during configuration).
2244 	 */
2245 	PMD(PMD_KEEPS, ("%s: %s@%s(%s#%d) has %d components\n", pmf,
2246 	    PM_DEVICE(dip), PM_NUMCMPTS(dip)))
2247 
2248 	/* node is detached, so we can examine power without locking */
2249 	if (PM_ISBC(dip)) {
2250 		count = (PM_CURPOWER(dip, 0) != 0);
2251 	} else {
2252 		for (i = 0; i < PM_NUMCMPTS(dip); i++)
2253 			count += (PM_CURPOWER(dip, i) != 0);
2254 	}
2255 
2256 	if (PM_NUMCMPTS(dip) && pdip && !PM_WANTS_NOTIFICATION(pdip))
2257 		e_pm_hold_rele_power(pdip, -count);
2258 
2259 	/* Schedule a request to clean up dependency records */
2260 	pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
2261 	(void) ddi_pathname(dip, pathbuf);
2262 	pm_dispatch_to_dep_thread(work_type, pathbuf, pathbuf,
2263 	    PM_DEP_NOWAIT, NULL, (count > 0));
2264 	kmem_free(pathbuf, MAXPATHLEN);
2265 
2266 	/*
2267 	 * Adjust the pm_comps_notlowest count since this device is
2268 	 * not being power-managed anymore.
2269 	 */
2270 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
2271 		if (PM_CURPOWER(dip, i) != 0)
2272 			PM_DECR_NOTLOWEST(dip);
2273 	}
2274 	/*
2275 	 * Once we clear the info pointer, it looks like it is not power
2276 	 * managed to everybody else.
2277 	 */
2278 	pm_set_pm_info(dip, NULL);
2279 	kmem_free(info, sizeof (pm_info_t));
2280 }
2281 
2282 int
2283 pm_get_norm_pwrs(dev_info_t *dip, int **valuep, size_t *length)
2284 {
2285 	int components = PM_NUMCMPTS(dip);
2286 	int *bufp;
2287 	size_t size;
2288 	int i;
2289 
2290 	if (components <= 0) {
2291 		cmn_err(CE_NOTE, "!pm: %s@%s(%s#%d) has no components, "
2292 		    "can't get normal power values\n", PM_DEVICE(dip));
2293 		return (DDI_FAILURE);
2294 	} else {
2295 		size = components * sizeof (int);
2296 		bufp = kmem_alloc(size, KM_SLEEP);
2297 		for (i = 0; i < components; i++) {
2298 			bufp[i] = pm_get_normal_power(dip, i);
2299 		}
2300 	}
2301 	*length = size;
2302 	*valuep = bufp;
2303 	return (DDI_SUCCESS);
2304 }
2305 
2306 static int
2307 pm_reset_timestamps(dev_info_t *dip, void *arg)
2308 {
2309 	_NOTE(ARGUNUSED(arg))
2310 
2311 	int components;
2312 	int	i;
2313 
2314 	if (!PM_GET_PM_INFO(dip))
2315 		return (DDI_WALK_CONTINUE);
2316 	components = PM_NUMCMPTS(dip);
2317 	ASSERT(components > 0);
2318 	PM_LOCK_BUSY(dip);
2319 	for (i = 0; i < components; i++) {
2320 		struct pm_component *cp;
2321 		/*
2322 		 * If the component was not marked as busy,
2323 		 * reset its timestamp to now.
2324 		 */
2325 		cp = PM_CP(dip, i);
2326 		if (cp->pmc_timestamp)
2327 			cp->pmc_timestamp = gethrestime_sec();
2328 	}
2329 	PM_UNLOCK_BUSY(dip);
2330 	return (DDI_WALK_CONTINUE);
2331 }
2332 
2333 /*
2334  * Convert a power level to an index into the levels array (or
2335  * just PM_LEVEL_UNKNOWN in that special case).
2336  */
2337 static int
2338 pm_level_to_index(dev_info_t *dip, pm_component_t *cp, int level)
2339 {
2340 	PMD_FUNC(pmf, "level_to_index")
2341 	int i;
2342 	int limit = cp->pmc_comp.pmc_numlevels;
2343 	int *ip = cp->pmc_comp.pmc_lvals;
2344 
2345 	if (level == PM_LEVEL_UNKNOWN)
2346 		return (level);
2347 
2348 	for (i = 0; i < limit; i++) {
2349 		if (level == *ip++) {
2350 			PMD(PMD_LEVEL, ("%s: %s@%s(%s#%d)[%d] to %x\n",
2351 			    pmf, PM_DEVICE(dip),
2352 			    (int)(cp - DEVI(dip)->devi_pm_components), level))
2353 			return (i);
2354 		}
2355 	}
2356 	panic("pm_level_to_index: level %d not found for device "
2357 	    "%s@%s(%s#%d)", level, PM_DEVICE(dip));
2358 	/*NOTREACHED*/
2359 }
2360 
2361 /*
2362  * Internal function to set current power level
2363  */
2364 static void
2365 e_pm_set_cur_pwr(dev_info_t *dip, pm_component_t *cp, int level)
2366 {
2367 	PMD_FUNC(pmf, "set_cur_pwr")
2368 	int curpwr = (cp->pmc_flags & PM_PHC_WHILE_SET_POWER ?
2369 	    cp->pmc_phc_pwr : cp->pmc_cur_pwr);
2370 
2371 	/*
2372 	 * Nothing to adjust if current & new levels are the same.
2373 	 */
2374 	if (curpwr != PM_LEVEL_UNKNOWN &&
2375 	    level == cp->pmc_comp.pmc_lvals[curpwr])
2376 		return;
2377 
2378 	/*
2379 	 * Keep the count for comps doing transition to/from lowest
2380 	 * level.
2381 	 */
2382 	if (curpwr == 0) {
2383 		PM_INCR_NOTLOWEST(dip);
2384 	} else if (level == cp->pmc_comp.pmc_lvals[0]) {
2385 		PM_DECR_NOTLOWEST(dip);
2386 	}
2387 	cp->pmc_phc_pwr = PM_LEVEL_UNKNOWN;
2388 	cp->pmc_cur_pwr = pm_level_to_index(dip, cp, level);
2389 }
2390 
2391 /*
2392  * This is the default method of setting the power of a device if no ppm
2393  * driver has claimed it.
2394  */
2395 int
2396 pm_power(dev_info_t *dip, int comp, int level)
2397 {
2398 	PMD_FUNC(pmf, "power")
2399 	struct dev_ops	*ops;
2400 	int		(*fn)(dev_info_t *, int, int);
2401 	struct pm_component *cp = PM_CP(dip, comp);
2402 	int retval;
2403 	pm_info_t *info = PM_GET_PM_INFO(dip);
2404 	static int pm_phc_impl(dev_info_t *, int, int, int);
2405 
2406 	PMD(PMD_KIDSUP, ("%s: %s@%s(%s#%d), comp=%d, level=%d\n", pmf,
2407 	    PM_DEVICE(dip), comp, level))
2408 	if (!(ops = ddi_get_driver(dip))) {
2409 		PMD(PMD_FAIL, ("%s: %s@%s(%s#%d) has no ops\n", pmf,
2410 		    PM_DEVICE(dip)))
2411 		return (DDI_FAILURE);
2412 	}
2413 	if ((ops->devo_rev < 2) || !(fn = ops->devo_power)) {
2414 		PMD(PMD_FAIL, ("%s: %s%s\n", pmf,
2415 		    (ops->devo_rev < 2 ? " wrong devo_rev" : ""),
2416 		    (!fn ? " devo_power NULL" : "")))
2417 		return (DDI_FAILURE);
2418 	}
2419 	cp->pmc_flags |= PM_POWER_OP;
2420 	retval = (*fn)(dip, comp, level);
2421 	cp->pmc_flags &= ~PM_POWER_OP;
2422 	if (retval == DDI_SUCCESS) {
2423 		e_pm_set_cur_pwr(dip, PM_CP(dip, comp), level);
2424 		return (DDI_SUCCESS);
2425 	}
2426 
2427 	/*
2428 	 * If pm_power_has_changed() detected a deadlock with pm_power() it
2429 	 * updated only the power level of the component.  If our attempt to
2430 	 * set the device new to a power level above has failed we sync the
2431 	 * total power state via phc code now.
2432 	 */
2433 	if (cp->pmc_flags & PM_PHC_WHILE_SET_POWER) {
2434 		int phc_lvl =
2435 		    cp->pmc_comp.pmc_lvals[cp->pmc_cur_pwr];
2436 
2437 		ASSERT(info);
2438 		(void) pm_phc_impl(dip, comp, phc_lvl, 0);
2439 		PMD(PMD_PHC, ("%s: phc %s@%s(%s#%d) comp=%d level=%d\n",
2440 			pmf, PM_DEVICE(dip), comp, phc_lvl))
2441 	}
2442 
2443 	PMD(PMD_FAIL, ("%s: can't set comp=%d (%s) of %s@%s(%s#%d) to "
2444 	    "level=%d (%s)\n", pmf, comp, cp->pmc_comp.pmc_name, PM_DEVICE(dip),
2445 	    level, power_val_to_string(cp, level)));
2446 	return (DDI_FAILURE);
2447 }
2448 
2449 int
2450 pm_unmanage(dev_info_t *dip)
2451 {
2452 	PMD_FUNC(pmf, "unmanage")
2453 	power_req_t power_req;
2454 	int result, retval = 0;
2455 
2456 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
2457 	PMD(PMD_REMDEV | PMD_KIDSUP, ("%s: %s@%s(%s#%d)\n", pmf,
2458 	    PM_DEVICE(dip)))
2459 	power_req.request_type = PMR_PPM_UNMANAGE;
2460 	power_req.req.ppm_config_req.who = dip;
2461 	if (pm_ppm_claimed(dip))
2462 		retval = pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER,
2463 		    &power_req, &result);
2464 #ifdef DEBUG
2465 	else
2466 		retval = pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER,
2467 		    &power_req, &result);
2468 #endif
2469 	ASSERT(retval == DDI_SUCCESS);
2470 	pm_rem_info(dip);
2471 	return (retval);
2472 }
2473 
2474 int
2475 pm_raise_power(dev_info_t *dip, int comp, int level)
2476 {
2477 	if (level < 0)
2478 		return (DDI_FAILURE);
2479 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, comp, NULL) ||
2480 	    !e_pm_valid_power(dip, comp, level))
2481 		return (DDI_FAILURE);
2482 
2483 	return (dev_is_needed(dip, comp, level, PM_LEVEL_UPONLY));
2484 }
2485 
2486 int
2487 pm_lower_power(dev_info_t *dip, int comp, int level)
2488 {
2489 	PMD_FUNC(pmf, "pm_lower_power")
2490 
2491 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, comp, NULL) ||
2492 	    !e_pm_valid_power(dip, comp, level)) {
2493 		PMD(PMD_FAIL, ("%s: validation checks failed for %s@%s(%s#%d) "
2494 		    "comp=%d level=%d\n", pmf, PM_DEVICE(dip), comp, level))
2495 		return (DDI_FAILURE);
2496 	}
2497 
2498 	if (!DEVI_IS_DETACHING(dip)) {
2499 		PMD(PMD_FAIL, ("%s: %s@%s(%s#%d) not detaching\n",
2500 		    pmf, PM_DEVICE(dip)))
2501 		return (DDI_FAILURE);
2502 	}
2503 
2504 	/*
2505 	 * If we don't care about saving power, or we're treating this node
2506 	 * specially, then this is a no-op
2507 	 */
2508 	if (!autopm_enabled || pm_noinvol(dip)) {
2509 		PMD(PMD_FAIL, ("%s: %s@%s(%s#%d) %s%s\n", pmf, PM_DEVICE(dip),
2510 		    !autopm_enabled ? "!autopm_enabled " : "",
2511 		    pm_noinvol(dip) ? "pm_noinvol()" : ""))
2512 		return (DDI_SUCCESS);
2513 	}
2514 
2515 	if (dev_is_needed(dip, comp, level, PM_LEVEL_DOWNONLY) != DDI_SUCCESS) {
2516 		PMD(PMD_FAIL, ("%s: %s@%s(%s#%d) dev_is_needed failed\n", pmf,
2517 		    PM_DEVICE(dip)))
2518 		return (DDI_FAILURE);
2519 	}
2520 	return (DDI_SUCCESS);
2521 }
2522 
2523 /*
2524  * Find the entries struct for a given dip in the blocked list, return it locked
2525  */
2526 static psce_t *
2527 pm_psc_dip_to_direct(dev_info_t *dip, pscc_t **psccp)
2528 {
2529 	pscc_t *p;
2530 	psce_t *psce;
2531 
2532 	rw_enter(&pm_pscc_direct_rwlock, RW_READER);
2533 	for (p = pm_pscc_direct; p; p = p->pscc_next) {
2534 		if (p->pscc_dip == dip) {
2535 			*psccp = p;
2536 			psce = p->pscc_entries;
2537 			mutex_enter(&psce->psce_lock);
2538 			ASSERT(psce);
2539 			rw_exit(&pm_pscc_direct_rwlock);
2540 			return (psce);
2541 		}
2542 	}
2543 	rw_exit(&pm_pscc_direct_rwlock);
2544 	panic("sunpm: no entry for dip %p in direct list", (void *)dip);
2545 	/*NOTREACHED*/
2546 }
2547 
2548 /*
2549  * Write an entry indicating a power level change (to be passed to a process
2550  * later) in the given psce.
2551  * If we were called in the path that brings up the console fb in the
2552  * case of entering the prom, we don't want to sleep.  If the alloc fails, then
2553  * we create a record that has a size of -1, a physaddr of NULL, and that
2554  * has the overflow flag set.
2555  */
2556 static int
2557 psc_entry(ushort_t event, psce_t *psce, dev_info_t *dip, int comp, int new,
2558     int old, int which, pm_canblock_t canblock)
2559 {
2560 	char	buf[MAXNAMELEN];
2561 	pm_state_change_t *p;
2562 	size_t	size;
2563 	caddr_t physpath = NULL;
2564 	int	overrun = 0;
2565 
2566 	ASSERT(MUTEX_HELD(&psce->psce_lock));
2567 	(void) ddi_pathname(dip, buf);
2568 	size = strlen(buf) + 1;
2569 	p = psce->psce_in;
2570 	if (canblock == PM_CANBLOCK_BYPASS) {
2571 		physpath = kmem_alloc(size, KM_NOSLEEP);
2572 		if (physpath == NULL) {
2573 			/*
2574 			 * mark current entry as overrun
2575 			 */
2576 			p->flags |= PSC_EVENT_LOST;
2577 			size = (size_t)-1;
2578 		}
2579 	} else
2580 		physpath = kmem_alloc(size, KM_SLEEP);
2581 	if (p->size) {	/* overflow; mark the next entry */
2582 		if (p->size != (size_t)-1)
2583 			kmem_free(p->physpath, p->size);
2584 		ASSERT(psce->psce_out == p);
2585 		if (p == psce->psce_last) {
2586 			psce->psce_first->flags |= PSC_EVENT_LOST;
2587 			psce->psce_out = psce->psce_first;
2588 		} else {
2589 			(p + 1)->flags |= PSC_EVENT_LOST;
2590 			psce->psce_out = (p + 1);
2591 		}
2592 		overrun++;
2593 	} else if (physpath == NULL) {	/* alloc failed, mark this entry */
2594 		p->flags |= PSC_EVENT_LOST;
2595 		p->size = 0;
2596 		p->physpath = NULL;
2597 	}
2598 	if (which == PSC_INTEREST) {
2599 		mutex_enter(&pm_compcnt_lock);
2600 		if (pm_comps_notlowest == 0)
2601 			p->flags |= PSC_ALL_LOWEST;
2602 		else
2603 			p->flags &= ~PSC_ALL_LOWEST;
2604 		mutex_exit(&pm_compcnt_lock);
2605 	}
2606 	p->event = event;
2607 	p->timestamp = gethrestime_sec();
2608 	p->component = comp;
2609 	p->old_level = old;
2610 	p->new_level = new;
2611 	p->physpath = physpath;
2612 	p->size = size;
2613 	if (physpath != NULL)
2614 		(void) strcpy(p->physpath, buf);
2615 	if (p == psce->psce_last)
2616 		psce->psce_in = psce->psce_first;
2617 	else
2618 		psce->psce_in = ++p;
2619 	mutex_exit(&psce->psce_lock);
2620 	return (overrun);
2621 }
2622 
2623 /*
2624  * Find the next entry on the interest list.  We keep a pointer to the item we
2625  * last returned in the user's cooke.  Returns a locked entries struct.
2626  */
2627 static psce_t *
2628 psc_interest(void **cookie, pscc_t **psccp)
2629 {
2630 	pscc_t *pscc;
2631 	pscc_t **cookiep = (pscc_t **)cookie;
2632 
2633 	if (*cookiep == NULL)
2634 		pscc = pm_pscc_interest;
2635 	else
2636 		pscc = (*cookiep)->pscc_next;
2637 	if (pscc) {
2638 		*cookiep = pscc;
2639 		*psccp = pscc;
2640 		mutex_enter(&pscc->pscc_entries->psce_lock);
2641 		return (pscc->pscc_entries);
2642 	} else {
2643 		return (NULL);
2644 	}
2645 }
2646 
2647 /*
2648  * Create an entry for a process to pick up indicating a power level change.
2649  */
2650 static void
2651 pm_enqueue_notify(ushort_t cmd, dev_info_t *dip, int comp,
2652     int newlevel, int oldlevel, pm_canblock_t canblock)
2653 {
2654 	PMD_FUNC(pmf, "enqueue_notify")
2655 	pscc_t	*pscc;
2656 	psce_t	*psce;
2657 	void		*cookie = NULL;
2658 	int	overrun;
2659 
2660 	ASSERT(MUTEX_HELD(&pm_rsvp_lock));
2661 	switch (cmd) {
2662 	case PSC_PENDING_CHANGE:	/* only for controlling process */
2663 		PMD(PMD_DPM, ("%s: PENDING %s@%s(%s#%d), comp %d, %d -> %d\n",
2664 		    pmf, PM_DEVICE(dip), comp, oldlevel, newlevel))
2665 		psce = pm_psc_dip_to_direct(dip, &pscc);
2666 		ASSERT(psce);
2667 		PMD(PMD_IOCTL, ("%s: PENDING: %s@%s(%s#%d) pm_poll_cnt[%d] "
2668 		    "%d\n", pmf, PM_DEVICE(dip), pscc->pscc_clone,
2669 		    pm_poll_cnt[pscc->pscc_clone]))
2670 		overrun = psc_entry(cmd, psce, dip, comp, newlevel, oldlevel,
2671 		    PSC_DIRECT, canblock);
2672 		PMD(PMD_DPM, ("%s: sig %d\n", pmf, pscc->pscc_clone))
2673 		mutex_enter(&pm_clone_lock);
2674 		if (!overrun)
2675 			pm_poll_cnt[pscc->pscc_clone]++;
2676 		cv_signal(&pm_clones_cv[pscc->pscc_clone]);
2677 		pollwakeup(&pm_pollhead, (POLLRDNORM | POLLIN));
2678 		mutex_exit(&pm_clone_lock);
2679 		break;
2680 	case PSC_HAS_CHANGED:
2681 		PMD(PMD_DPM, ("%s: HAS %s@%s(%s#%d), comp %d, %d -> %d\n",
2682 		    pmf, PM_DEVICE(dip), comp, oldlevel, newlevel))
2683 		if (PM_ISDIRECT(dip) && canblock != PM_CANBLOCK_BYPASS) {
2684 			psce = pm_psc_dip_to_direct(dip, &pscc);
2685 			PMD(PMD_IOCTL, ("%s: HAS: %s@%s(%s#%d) pm_poll_cnt[%d] "
2686 			    "%d\n", pmf, PM_DEVICE(dip), pscc->pscc_clone,
2687 			    pm_poll_cnt[pscc->pscc_clone]))
2688 			overrun = psc_entry(cmd, psce, dip, comp, newlevel,
2689 			    oldlevel, PSC_DIRECT, canblock);
2690 			PMD(PMD_DPM, ("%s: sig %d\n", pmf, pscc->pscc_clone))
2691 			mutex_enter(&pm_clone_lock);
2692 			if (!overrun)
2693 				pm_poll_cnt[pscc->pscc_clone]++;
2694 			cv_signal(&pm_clones_cv[pscc->pscc_clone]);
2695 			pollwakeup(&pm_pollhead, (POLLRDNORM | POLLIN));
2696 			mutex_exit(&pm_clone_lock);
2697 		}
2698 		mutex_enter(&pm_clone_lock);
2699 		rw_enter(&pm_pscc_interest_rwlock, RW_READER);
2700 		while ((psce = psc_interest(&cookie, &pscc)) != NULL) {
2701 			(void) psc_entry(cmd, psce, dip, comp, newlevel,
2702 			    oldlevel, PSC_INTEREST, canblock);
2703 			cv_signal(&pm_clones_cv[pscc->pscc_clone]);
2704 		}
2705 		rw_exit(&pm_pscc_interest_rwlock);
2706 		mutex_exit(&pm_clone_lock);
2707 		break;
2708 #ifdef DEBUG
2709 	default:
2710 		ASSERT(0);
2711 #endif
2712 	}
2713 }
2714 
2715 static void
2716 pm_enqueue_notify_others(pm_ppm_devlist_t **listp, pm_canblock_t canblock)
2717 {
2718 	if (listp) {
2719 		pm_ppm_devlist_t *p, *next = NULL;
2720 
2721 		for (p = *listp; p; p = next) {
2722 			next = p->ppd_next;
2723 			pm_enqueue_notify(PSC_HAS_CHANGED, p->ppd_who,
2724 			    p->ppd_cmpt, p->ppd_new_level, p->ppd_old_level,
2725 			    canblock);
2726 			kmem_free(p, sizeof (pm_ppm_devlist_t));
2727 		}
2728 		*listp = NULL;
2729 	}
2730 }
2731 
2732 /*
2733  * Try to get the power locks of the parent node and target (child)
2734  * node.  Return true if successful (with both locks held) or false
2735  * (with no locks held).
2736  */
2737 static int
2738 pm_try_parent_child_locks(dev_info_t *pdip,
2739     dev_info_t *dip, int *pcircp, int *circp)
2740 {
2741 	if (ndi_devi_tryenter(pdip, pcircp))
2742 		if (PM_TRY_LOCK_POWER(dip, circp)) {
2743 			return (1);
2744 		} else {
2745 			ndi_devi_exit(pdip, *pcircp);
2746 		}
2747 	return (0);
2748 }
2749 
2750 /*
2751  * Determine if the power lock owner is blocked by current thread.
2752  * returns :
2753  * 	1 - If the thread owning the effective power lock (the first lock on
2754  *          which a thread blocks when it does PM_LOCK_POWER) is blocked by
2755  *          a mutex held by the current thread.
2756  *
2757  *	0 - otherwise
2758  *
2759  * Note : This function is called by pm_power_has_changed to determine whether
2760  * it is executing in parallel with pm_set_power.
2761  */
2762 static int
2763 pm_blocked_by_us(dev_info_t *dip)
2764 {
2765 	power_req_t power_req;
2766 	kthread_t *owner;
2767 	int result;
2768 	kmutex_t *mp;
2769 	dev_info_t *ppm = (dev_info_t *)DEVI(dip)->devi_pm_ppm;
2770 
2771 	power_req.request_type = PMR_PPM_POWER_LOCK_OWNER;
2772 	power_req.req.ppm_power_lock_owner_req.who = dip;
2773 	if (pm_ctlops(ppm, dip, DDI_CTLOPS_POWER, &power_req, &result) !=
2774 	    DDI_SUCCESS) {
2775 		/*
2776 		 * It is assumed that if the device is claimed by ppm, ppm
2777 		 * will always implement this request type and it'll always
2778 		 * return success. We panic here, if it fails.
2779 		 */
2780 		panic("pm: Can't determine power lock owner of %s@%s(%s#%d)\n",
2781 		    PM_DEVICE(dip));
2782 		/*NOTREACHED*/
2783 	}
2784 
2785 	if ((owner = power_req.req.ppm_power_lock_owner_req.owner) != NULL &&
2786 	    owner->t_state == TS_SLEEP &&
2787 	    owner->t_sobj_ops &&
2788 	    SOBJ_TYPE(owner->t_sobj_ops) == SOBJ_MUTEX &&
2789 	    (mp = (kmutex_t *)owner->t_wchan) &&
2790 	    mutex_owner(mp) == curthread)
2791 		return (1);
2792 
2793 	return (0);
2794 }
2795 
2796 /*
2797  * Notify parent which wants to hear about a child's power changes.
2798  */
2799 static void
2800 pm_notify_parent(dev_info_t *dip,
2801     dev_info_t *pdip, int comp, int old_level, int level)
2802 {
2803 	pm_bp_has_changed_t bphc;
2804 	pm_sp_misc_t pspm;
2805 	char *pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2806 	int result = DDI_SUCCESS;
2807 
2808 	bphc.bphc_dip = dip;
2809 	bphc.bphc_path = ddi_pathname(dip, pathbuf);
2810 	bphc.bphc_comp = comp;
2811 	bphc.bphc_olevel = old_level;
2812 	bphc.bphc_nlevel = level;
2813 	pspm.pspm_canblock = PM_CANBLOCK_BLOCK;
2814 	pspm.pspm_scan = 0;
2815 	bphc.bphc_private = &pspm;
2816 	(void) (*PM_BUS_POWER_FUNC(pdip))(pdip, NULL,
2817 	    BUS_POWER_HAS_CHANGED, (void *)&bphc, (void *)&result);
2818 	kmem_free(pathbuf, MAXPATHLEN);
2819 }
2820 
2821 /*
2822  * Check if we need to resume a BC device, and make the attach call as required.
2823  */
2824 static int
2825 pm_check_and_resume(dev_info_t *dip, int comp, int old_level, int level)
2826 {
2827 	int ret = DDI_SUCCESS;
2828 
2829 	if (PM_ISBC(dip) && comp == 0 && old_level == 0 && level != 0) {
2830 		ASSERT(DEVI(dip)->devi_pm_flags & PMC_SUSPENDED);
2831 		/* ppm is not interested in DDI_PM_RESUME */
2832 		if ((ret = devi_attach(dip, DDI_PM_RESUME)) != DDI_SUCCESS)
2833 			/* XXX Should we mark it resumed, */
2834 			/* even though it failed? */
2835 			cmn_err(CE_WARN, "!pm: Can't resume %s@%s",
2836 			    PM_NAME(dip), PM_ADDR(dip));
2837 		DEVI(dip)->devi_pm_flags &= ~PMC_SUSPENDED;
2838 	}
2839 
2840 	return (ret);
2841 }
2842 
2843 /*
2844  * Tests outside the lock to see if we should bother to enqueue an entry
2845  * for any watching process.  If yes, then caller will take the lock and
2846  * do the full protocol
2847  */
2848 static int
2849 pm_watchers()
2850 {
2851 	if (pm_processes_stopped)
2852 		return (0);
2853 	return (pm_pscc_direct || pm_pscc_interest);
2854 }
2855 
2856 /*
2857  * A driver is reporting that the power of one of its device's components
2858  * has changed.  Update the power state accordingly.
2859  */
2860 int
2861 pm_power_has_changed(dev_info_t *dip, int comp, int level)
2862 {
2863 	PMD_FUNC(pmf, "pm_power_has_changed")
2864 	int ret;
2865 	dev_info_t *pdip = ddi_get_parent(dip);
2866 	struct pm_component *cp;
2867 	int blocked, circ, pcirc, old_level;
2868 	static int pm_phc_impl(dev_info_t *, int, int, int);
2869 
2870 	if (level < 0) {
2871 		PMD(PMD_FAIL, ("%s: %s@%s(%s#%d): bad level=%d\n", pmf,
2872 		    PM_DEVICE(dip), level))
2873 		return (DDI_FAILURE);
2874 	}
2875 
2876 	PMD(PMD_KIDSUP | PMD_DEP, ("%s: %s@%s(%s#%d), comp=%d, level=%d\n", pmf,
2877 	    PM_DEVICE(dip), comp, level))
2878 
2879 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, comp, &cp) ||
2880 	    !e_pm_valid_power(dip, comp, level))
2881 		return (DDI_FAILURE);
2882 
2883 	/*
2884 	 * A driver thread calling pm_power_has_changed and another thread
2885 	 * calling pm_set_power can deadlock.  The problem is not resolvable
2886 	 * by changing lock order, so we use pm_blocked_by_us() to detect
2887 	 * this specific deadlock.  If we can't get the lock immediately
2888 	 * and we are deadlocked, just update the component's level, do
2889 	 * notifications, and return.  We intend to update the total power
2890 	 * state later (if the other thread fails to set power to the
2891 	 * desired level).  If we were called because of a power change on a
2892 	 * component that isn't involved in a set_power op, update all state
2893 	 * immediately.
2894 	 */
2895 	cp = PM_CP(dip, comp);
2896 	while (!pm_try_parent_child_locks(pdip, dip, &pcirc, &circ)) {
2897 		if (((blocked = pm_blocked_by_us(dip)) != 0) &&
2898 		    (cp->pmc_flags & PM_POWER_OP)) {
2899 			if (pm_watchers()) {
2900 				mutex_enter(&pm_rsvp_lock);
2901 				pm_enqueue_notify(PSC_HAS_CHANGED, dip, comp,
2902 				    level, cur_power(cp), PM_CANBLOCK_BLOCK);
2903 				mutex_exit(&pm_rsvp_lock);
2904 			}
2905 			if (pdip && PM_WANTS_NOTIFICATION(pdip))
2906 				pm_notify_parent(dip,
2907 				    pdip, comp, cur_power(cp), level);
2908 			(void) pm_check_and_resume(dip,
2909 			    comp, cur_power(cp), level);
2910 
2911 			/*
2912 			 * Stash the old power index, update curpwr, and flag
2913 			 * that the total power state needs to be synched.
2914 			 */
2915 			cp->pmc_flags |= PM_PHC_WHILE_SET_POWER;
2916 			/*
2917 			 * Several pm_power_has_changed calls could arrive
2918 			 * while the set power path remains blocked.  Keep the
2919 			 * oldest old power and the newest new power of any
2920 			 * sequence of phc calls which arrive during deadlock.
2921 			 */
2922 			if (cp->pmc_phc_pwr == PM_LEVEL_UNKNOWN)
2923 				cp->pmc_phc_pwr = cp->pmc_cur_pwr;
2924 			cp->pmc_cur_pwr =
2925 			    pm_level_to_index(dip, cp, level);
2926 			PMD(PMD_PHC, ("%s: deadlock for %s@%s(%s#%d), comp=%d, "
2927 			    "level=%d\n", pmf, PM_DEVICE(dip), comp, level))
2928 			return (DDI_SUCCESS);
2929 		} else
2930 			if (blocked) {	/* blocked, but different cmpt? */
2931 				if (!ndi_devi_tryenter(pdip, &pcirc)) {
2932 					cmn_err(CE_NOTE,
2933 					    "!pm: parent kuc not updated due "
2934 					    "to possible deadlock.\n");
2935 					return (pm_phc_impl(dip,
2936 						    comp, level, 1));
2937 				}
2938 				old_level = cur_power(cp);
2939 				if (pdip && !PM_WANTS_NOTIFICATION(pdip) &&
2940 				    (!PM_ISBC(dip) || comp == 0) &&
2941 				    POWERING_ON(old_level, level))
2942 					pm_hold_power(pdip);
2943 				ret = pm_phc_impl(dip, comp, level, 1);
2944 				if (pdip && !PM_WANTS_NOTIFICATION(pdip)) {
2945 					if ((!PM_ISBC(dip) ||
2946 					    comp == 0) && level == 0 &&
2947 					    old_level != PM_LEVEL_UNKNOWN)
2948 						pm_rele_power(pdip);
2949 				}
2950 				ndi_devi_exit(pdip, pcirc);
2951 				/* child lock not held: deadlock */
2952 				return (ret);
2953 			}
2954 		delay(1);
2955 		PMD(PMD_PHC, ("%s: try lock again\n", pmf))
2956 	}
2957 
2958 	/* non-deadlock case */
2959 	old_level = cur_power(cp);
2960 	if (pdip && !PM_WANTS_NOTIFICATION(pdip) &&
2961 	    (!PM_ISBC(dip) || comp == 0) && POWERING_ON(old_level, level))
2962 		pm_hold_power(pdip);
2963 	ret = pm_phc_impl(dip, comp, level, 1);
2964 	if (pdip && !PM_WANTS_NOTIFICATION(pdip)) {
2965 		if ((!PM_ISBC(dip) || comp == 0) && level == 0 &&
2966 		    old_level != PM_LEVEL_UNKNOWN)
2967 			pm_rele_power(pdip);
2968 	}
2969 	PM_UNLOCK_POWER(dip, circ);
2970 	ndi_devi_exit(pdip, pcirc);
2971 	return (ret);
2972 }
2973 
2974 /*
2975  * Account for power changes to a component of the the console frame buffer.
2976  * If lowering power from full (or "unkown", which is treatd as full)
2977  * we will increment the "components off" count of the fb device.
2978  * Subsequent lowering of the same component doesn't affect the count.  If
2979  * raising a component back to full power, we will decrement the count.
2980  *
2981  * Return: the increment value for pm_cfb_comps_off (-1, 0, or 1)
2982  */
2983 static int
2984 calc_cfb_comps_incr(dev_info_t *dip, int cmpt, int old, int new)
2985 {
2986 	struct pm_component *cp = PM_CP(dip, cmpt);
2987 	int on = (old == PM_LEVEL_UNKNOWN || old == cp->pmc_norm_pwr);
2988 	int want_normal = (new == cp->pmc_norm_pwr);
2989 	int incr = 0;
2990 
2991 	if (on && !want_normal)
2992 		incr = 1;
2993 	else if (!on && want_normal)
2994 		incr = -1;
2995 	return (incr);
2996 }
2997 
2998 /*
2999  * Adjust the count of console frame buffer components < full power.
3000  */
3001 static void
3002 update_comps_off(int incr, dev_info_t *dip)
3003 {
3004 		mutex_enter(&pm_cfb_lock);
3005 		pm_cfb_comps_off += incr;
3006 		ASSERT(pm_cfb_comps_off <= PM_NUMCMPTS(dip));
3007 		mutex_exit(&pm_cfb_lock);
3008 }
3009 
3010 /*
3011  * Update the power state in the framework (via the ppm).  The 'notify'
3012  * argument tells whether to notify watchers.  Power lock is already held.
3013  */
3014 static int
3015 pm_phc_impl(dev_info_t *dip, int comp, int level, int notify)
3016 {
3017 	PMD_FUNC(pmf, "phc_impl")
3018 	power_req_t power_req;
3019 	int i, dodeps = 0;
3020 	dev_info_t *pdip = ddi_get_parent(dip);
3021 	int result;
3022 	int old_level;
3023 	struct pm_component *cp;
3024 	int incr = 0;
3025 	dev_info_t *ppm = (dev_info_t *)DEVI(dip)->devi_pm_ppm;
3026 	int work_type = 0;
3027 	char *pathbuf;
3028 
3029 	/* Must use "official" power level for this test. */
3030 	cp = PM_CP(dip, comp);
3031 	old_level = (cp->pmc_flags & PM_PHC_WHILE_SET_POWER ?
3032 	    cp->pmc_phc_pwr : cp->pmc_cur_pwr);
3033 	if (old_level != PM_LEVEL_UNKNOWN)
3034 		old_level = cp->pmc_comp.pmc_lvals[old_level];
3035 
3036 	if (level == old_level) {
3037 		PMD(PMD_SET, ("%s: %s@%s(%s#%d), comp=%d is already at "
3038 		    "level=%d\n", pmf, PM_DEVICE(dip), comp, level))
3039 		return (DDI_SUCCESS);
3040 	}
3041 
3042 	/*
3043 	 * Tell ppm about this.
3044 	 */
3045 	power_req.request_type = PMR_PPM_POWER_CHANGE_NOTIFY;
3046 	power_req.req.ppm_notify_level_req.who = dip;
3047 	power_req.req.ppm_notify_level_req.cmpt = comp;
3048 	power_req.req.ppm_notify_level_req.new_level = level;
3049 	power_req.req.ppm_notify_level_req.old_level = old_level;
3050 	if (pm_ctlops(ppm, dip, DDI_CTLOPS_POWER, &power_req,
3051 	    &result) == DDI_FAILURE) {
3052 		PMD(PMD_FAIL, ("%s: pm_ctlops %s@%s(%s#%d) to %d failed\n",
3053 		    pmf, PM_DEVICE(dip), level))
3054 		return (DDI_FAILURE);
3055 	}
3056 
3057 	if (PM_IS_CFB(dip)) {
3058 		incr = calc_cfb_comps_incr(dip, comp, old_level, level);
3059 
3060 		if (incr) {
3061 			update_comps_off(incr, dip);
3062 			PMD(PMD_CFB, ("%s: %s@%s(%s#%d) comp=%d %d->%d "
3063 			    "cfb_comps_off->%d\n", pmf, PM_DEVICE(dip),
3064 			    comp, old_level, level, pm_cfb_comps_off))
3065 		}
3066 	}
3067 	e_pm_set_cur_pwr(dip, PM_CP(dip, comp), level);
3068 	result = DDI_SUCCESS;
3069 
3070 	if (notify) {
3071 		if (pdip && PM_WANTS_NOTIFICATION(pdip))
3072 			pm_notify_parent(dip, pdip, comp, old_level, level);
3073 		(void) pm_check_and_resume(dip, comp, old_level, level);
3074 	}
3075 
3076 	/*
3077 	 * Decrement the dependency kidsup count if we turn a device
3078 	 * off.
3079 	 */
3080 	if (POWERING_OFF(old_level, level)) {
3081 		dodeps = 1;
3082 		for (i = 0; i < PM_NUMCMPTS(dip); i++) {
3083 			cp = PM_CP(dip, i);
3084 			if (cur_power(cp)) {
3085 				dodeps = 0;
3086 				break;
3087 			}
3088 		}
3089 		if (dodeps)
3090 			work_type = PM_DEP_WK_POWER_OFF;
3091 	}
3092 
3093 	/*
3094 	 * Increment if we turn it on. Check to see
3095 	 * if other comps are already on, if so,
3096 	 * dont increment.
3097 	 */
3098 	if (POWERING_ON(old_level, level)) {
3099 		dodeps = 1;
3100 		for (i = 0; i < PM_NUMCMPTS(dip); i++) {
3101 			cp = PM_CP(dip, i);
3102 			if (comp == i)
3103 				continue;
3104 			/* -1 also treated as 0 in this case */
3105 			if (cur_power(cp) > 0) {
3106 				dodeps = 0;
3107 				break;
3108 			}
3109 		}
3110 		if (dodeps)
3111 			work_type = PM_DEP_WK_POWER_ON;
3112 	}
3113 
3114 	if (dodeps) {
3115 		pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3116 		(void) ddi_pathname(dip, pathbuf);
3117 		pm_dispatch_to_dep_thread(work_type, pathbuf, NULL,
3118 		    PM_DEP_NOWAIT, NULL, 0);
3119 		kmem_free(pathbuf, MAXPATHLEN);
3120 	}
3121 
3122 	if (notify && (level != old_level) && pm_watchers()) {
3123 		mutex_enter(&pm_rsvp_lock);
3124 		pm_enqueue_notify(PSC_HAS_CHANGED, dip, comp, level, old_level,
3125 		    PM_CANBLOCK_BLOCK);
3126 		mutex_exit(&pm_rsvp_lock);
3127 	}
3128 
3129 	PMD(PMD_RESCAN, ("%s: %s@%s(%s#%d): pm_rescan\n", pmf, PM_DEVICE(dip)))
3130 	pm_rescan(dip);
3131 	return (DDI_SUCCESS);
3132 }
3133 
3134 /*
3135  * This function is called at startup time to notify pm of the existence
3136  * of any platform power managers for this platform.  As a result of
3137  * this registration, each function provided will be called each time
3138  * a device node is attached, until one returns true, and it must claim the
3139  * device node (by returning non-zero) if it wants to be involved in the
3140  * node's power management.  If it does claim the node, then it will
3141  * subsequently be notified of attach and detach events.
3142  *
3143  */
3144 
3145 int
3146 pm_register_ppm(int (*func)(dev_info_t *), dev_info_t *dip)
3147 {
3148 	PMD_FUNC(pmf, "register_ppm")
3149 	struct ppm_callbacks *ppmcp;
3150 	pm_component_t *cp;
3151 	int i, pwr, result, circ;
3152 	power_req_t power_req;
3153 	struct ppm_notify_level_req *p = &power_req.req.ppm_notify_level_req;
3154 	void pm_ppm_claim(dev_info_t *);
3155 
3156 	mutex_enter(&ppm_lock);
3157 	ppmcp = ppm_callbacks;
3158 	for (i = 0; i < MAX_PPM_HANDLERS; i++, ppmcp++) {
3159 		if (ppmcp->ppmc_func == NULL) {
3160 			ppmcp->ppmc_func = func;
3161 			ppmcp->ppmc_dip = dip;
3162 			break;
3163 		}
3164 	}
3165 	mutex_exit(&ppm_lock);
3166 
3167 	if (i >= MAX_PPM_HANDLERS)
3168 		return (DDI_FAILURE);
3169 	while ((dip = ddi_get_parent(dip)) != NULL) {
3170 		if (PM_GET_PM_INFO(dip) == NULL)
3171 			continue;
3172 		pm_ppm_claim(dip);
3173 		if (pm_ppm_claimed(dip)) {
3174 			/*
3175 			 * Tell ppm about this.
3176 			 */
3177 			power_req.request_type = PMR_PPM_POWER_CHANGE_NOTIFY;
3178 			p->old_level = PM_LEVEL_UNKNOWN;
3179 			p->who = dip;
3180 			PM_LOCK_POWER(dip, &circ);
3181 			for (i = 0; i < PM_NUMCMPTS(dip); i++) {
3182 				cp = PM_CP(dip, i);
3183 				pwr = cp->pmc_cur_pwr;
3184 				if (pwr != PM_LEVEL_UNKNOWN) {
3185 					p->cmpt = i;
3186 					p->new_level = cur_power(cp);
3187 					p->old_level = PM_LEVEL_UNKNOWN;
3188 					if (pm_ctlops(PPM(dip), dip,
3189 					    DDI_CTLOPS_POWER, &power_req,
3190 					    &result) == DDI_FAILURE) {
3191 						PMD(PMD_FAIL, ("%s: pc "
3192 						    "%s@%s(%s#%d) to %d "
3193 						    "fails\n", pmf,
3194 						    PM_DEVICE(dip), pwr))
3195 					}
3196 				}
3197 			}
3198 			PM_UNLOCK_POWER(dip, circ);
3199 		}
3200 	}
3201 	return (DDI_SUCCESS);
3202 }
3203 
3204 /*
3205  * Call the ppm's that have registered and adjust the devinfo struct as
3206  * appropriate.  First one to claim it gets it.  The sets of devices claimed
3207  * by each ppm are assumed to be disjoint.
3208  */
3209 void
3210 pm_ppm_claim(dev_info_t *dip)
3211 {
3212 	struct ppm_callbacks *ppmcp;
3213 
3214 	if (PPM(dip)) {
3215 		return;
3216 	}
3217 	mutex_enter(&ppm_lock);
3218 	for (ppmcp = ppm_callbacks; ppmcp->ppmc_func; ppmcp++) {
3219 		if ((*ppmcp->ppmc_func)(dip)) {
3220 			DEVI(dip)->devi_pm_ppm =
3221 			    (struct dev_info *)ppmcp->ppmc_dip;
3222 			mutex_exit(&ppm_lock);
3223 			return;
3224 		}
3225 	}
3226 	mutex_exit(&ppm_lock);
3227 }
3228 
3229 /*
3230  * Node is being detached so stop autopm until we see if it succeeds, in which
3231  * case pm_stop will be called.  For backwards compatible devices we bring the
3232  * device up to full power on the assumption the detach will succeed.
3233  */
3234 void
3235 pm_detaching(dev_info_t *dip)
3236 {
3237 	PMD_FUNC(pmf, "detaching")
3238 	pm_info_t *info = PM_GET_PM_INFO(dip);
3239 	int iscons;
3240 
3241 	PMD(PMD_REMDEV, ("%s: %s@%s(%s#%d), %d comps\n", pmf, PM_DEVICE(dip),
3242 	    PM_NUMCMPTS(dip)))
3243 	if (info == NULL)
3244 		return;
3245 	ASSERT(DEVI_IS_DETACHING(dip));
3246 	PM_LOCK_DIP(dip);
3247 	info->pmi_dev_pm_state |= PM_DETACHING;
3248 	PM_UNLOCK_DIP(dip);
3249 	if (!PM_ISBC(dip))
3250 		pm_scan_stop(dip);
3251 
3252 	/*
3253 	 * console and old-style devices get brought up when detaching.
3254 	 */
3255 	iscons = PM_IS_CFB(dip);
3256 	if (iscons || PM_ISBC(dip)) {
3257 		(void) pm_all_to_normal(dip, PM_CANBLOCK_BYPASS);
3258 		if (iscons) {
3259 			mutex_enter(&pm_cfb_lock);
3260 			while (cfb_inuse) {
3261 				mutex_exit(&pm_cfb_lock);
3262 				PMD(PMD_CFB, ("%s: delay; cfb_inuse\n", pmf))
3263 				delay(1);
3264 				mutex_enter(&pm_cfb_lock);
3265 			}
3266 			ASSERT(cfb_dip_detaching == NULL);
3267 			ASSERT(cfb_dip);
3268 			cfb_dip_detaching = cfb_dip;	/* case detach fails */
3269 			cfb_dip = NULL;
3270 			mutex_exit(&pm_cfb_lock);
3271 		}
3272 	}
3273 }
3274 
3275 /*
3276  * Node failed to detach.  If it used to be autopm'd, make it so again.
3277  */
3278 void
3279 pm_detach_failed(dev_info_t *dip)
3280 {
3281 	PMD_FUNC(pmf, "detach_failed")
3282 	pm_info_t *info = PM_GET_PM_INFO(dip);
3283 	int pm_all_at_normal(dev_info_t *);
3284 
3285 	if (info == NULL)
3286 		return;
3287 	ASSERT(DEVI_IS_DETACHING(dip));
3288 	if (info->pmi_dev_pm_state & PM_DETACHING) {
3289 		info->pmi_dev_pm_state &= ~PM_DETACHING;
3290 		if (info->pmi_dev_pm_state & PM_ALLNORM_DEFERRED) {
3291 			/* Make sure the operation is still needed */
3292 			if (!pm_all_at_normal(dip)) {
3293 				if (pm_all_to_normal(dip,
3294 				    PM_CANBLOCK_FAIL) != DDI_SUCCESS) {
3295 					PMD(PMD_ERROR, ("%s: could not bring "
3296 					    "%s@%s(%s#%d) to normal\n", pmf,
3297 					    PM_DEVICE(dip)))
3298 				}
3299 			}
3300 			info->pmi_dev_pm_state &= ~PM_ALLNORM_DEFERRED;
3301 		}
3302 	}
3303 	if (!PM_ISBC(dip)) {
3304 		mutex_enter(&pm_scan_lock);
3305 		if (autopm_enabled)
3306 			pm_scan_init(dip);
3307 		mutex_exit(&pm_scan_lock);
3308 		pm_rescan(dip);
3309 	}
3310 }
3311 
3312 /* generic Backwards Compatible component */
3313 static char *bc_names[] = {"off", "on"};
3314 
3315 static pm_comp_t bc_comp = {"unknown", 2, NULL, NULL, &bc_names[0]};
3316 
3317 static void
3318 e_pm_default_levels(dev_info_t *dip, pm_component_t *cp, int norm)
3319 {
3320 	pm_comp_t *pmc;
3321 	pmc = &cp->pmc_comp;
3322 	pmc->pmc_numlevels = 2;
3323 	pmc->pmc_lvals[0] = 0;
3324 	pmc->pmc_lvals[1] = norm;
3325 	e_pm_set_cur_pwr(dip, cp, norm);
3326 }
3327 
3328 static void
3329 e_pm_default_components(dev_info_t *dip, int cmpts)
3330 {
3331 	int i;
3332 	pm_component_t *p = DEVI(dip)->devi_pm_components;
3333 
3334 	p = DEVI(dip)->devi_pm_components;
3335 	for (i = 0; i < cmpts; i++, p++) {
3336 		p->pmc_comp = bc_comp;	/* struct assignment */
3337 		p->pmc_comp.pmc_lvals = kmem_zalloc(2 * sizeof (int),
3338 		    KM_SLEEP);
3339 		p->pmc_comp.pmc_thresh = kmem_alloc(2 * sizeof (int),
3340 		    KM_SLEEP);
3341 		p->pmc_comp.pmc_numlevels = 2;
3342 		p->pmc_comp.pmc_thresh[0] = INT_MAX;
3343 		p->pmc_comp.pmc_thresh[1] = INT_MAX;
3344 	}
3345 }
3346 
3347 /*
3348  * Called from functions that require components to exist already to allow
3349  * for their creation by parsing the pm-components property.
3350  * Device will not be power managed as a result of this call
3351  * No locking needed because we're single threaded by the ndi_devi_enter
3352  * done while attaching, and the device isn't visible until after it has
3353  * attached
3354  */
3355 int
3356 pm_premanage(dev_info_t *dip, int style)
3357 {
3358 	PMD_FUNC(pmf, "premanage")
3359 	pm_comp_t	*pcp, *compp;
3360 	int		cmpts, i, norm, error;
3361 	pm_component_t *p = DEVI(dip)->devi_pm_components;
3362 	pm_comp_t *pm_autoconfig(dev_info_t *, int *);
3363 
3364 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
3365 	/*
3366 	 * If this dip has already been processed, don't mess with it
3367 	 */
3368 	if (DEVI(dip)->devi_pm_flags & PMC_COMPONENTS_DONE)
3369 		return (DDI_SUCCESS);
3370 	if (DEVI(dip)->devi_pm_flags & PMC_COMPONENTS_FAILED) {
3371 		return (DDI_FAILURE);
3372 	}
3373 	/*
3374 	 * Look up pm-components property and create components accordingly
3375 	 * If that fails, fall back to backwards compatibility
3376 	 */
3377 	if ((compp = pm_autoconfig(dip, &error)) == NULL) {
3378 		/*
3379 		 * If error is set, the property existed but was not well formed
3380 		 */
3381 		if (error || (style == PM_STYLE_NEW)) {
3382 			DEVI(dip)->devi_pm_flags |= PMC_COMPONENTS_FAILED;
3383 			return (DDI_FAILURE);
3384 		}
3385 		/*
3386 		 * If they don't have the pm-components property, then we
3387 		 * want the old "no pm until PM_SET_DEVICE_THRESHOLDS ioctl"
3388 		 * behavior driver must have called pm_create_components, and
3389 		 * we need to flesh out dummy components
3390 		 */
3391 		if ((cmpts = PM_NUMCMPTS(dip)) == 0) {
3392 			/*
3393 			 * Not really failure, but we don't want the
3394 			 * caller to treat it as success
3395 			 */
3396 			return (DDI_FAILURE);
3397 		}
3398 		DEVI(dip)->devi_pm_flags |= PMC_BC;
3399 		e_pm_default_components(dip, cmpts);
3400 		for (i = 0; i < cmpts; i++) {
3401 			/*
3402 			 * if normal power not set yet, we don't really know
3403 			 * what *ANY* of the power values are.  If normal
3404 			 * power is set, then we assume for this backwards
3405 			 * compatible case that the values are 0, normal power.
3406 			 */
3407 			norm = pm_get_normal_power(dip, i);
3408 			if (norm == (uint_t)-1) {
3409 				PMD(PMD_ERROR, ("%s: %s@%s(%s#%d)[%d]\n", pmf,
3410 				    PM_DEVICE(dip), i))
3411 				return (DDI_FAILURE);
3412 			}
3413 			/*
3414 			 * Components of BC devices start at their normal power,
3415 			 * so count them to be not at their lowest power.
3416 			 */
3417 			PM_INCR_NOTLOWEST(dip);
3418 			e_pm_default_levels(dip, PM_CP(dip, i), norm);
3419 		}
3420 	} else {
3421 		/*
3422 		 * e_pm_create_components was called from pm_autoconfig(), it
3423 		 * creates components with no descriptions (or known levels)
3424 		 */
3425 		cmpts = PM_NUMCMPTS(dip);
3426 		ASSERT(cmpts != 0);
3427 		pcp = compp;
3428 		p = DEVI(dip)->devi_pm_components;
3429 		for (i = 0; i < cmpts; i++, p++) {
3430 			p->pmc_comp = *pcp++;   /* struct assignment */
3431 			ASSERT(PM_CP(dip, i)->pmc_cur_pwr == 0);
3432 			e_pm_set_cur_pwr(dip, PM_CP(dip, i), PM_LEVEL_UNKNOWN);
3433 		}
3434 		pm_set_device_threshold(dip, pm_system_idle_threshold,
3435 		    PMC_DEF_THRESH);
3436 		kmem_free(compp, cmpts * sizeof (pm_comp_t));
3437 	}
3438 	return (DDI_SUCCESS);
3439 }
3440 
3441 /*
3442  * Called from during or after the device's attach to let us know it is ready
3443  * to play autopm.   Look up the pm model and manage the device accordingly.
3444  * Returns system call errno value.
3445  * If DDI_ATTACH and DDI_DETACH were in same namespace, this would be
3446  * a little cleaner
3447  *
3448  * Called with dip lock held, return with dip lock unheld.
3449  */
3450 
3451 int
3452 e_pm_manage(dev_info_t *dip, int style)
3453 {
3454 	PMD_FUNC(pmf, "e_manage")
3455 	pm_info_t	*info;
3456 	dev_info_t	*pdip = ddi_get_parent(dip);
3457 	int	pm_thresh_specd(dev_info_t *);
3458 	int	count;
3459 	char	*pathbuf;
3460 
3461 	if (pm_premanage(dip, style) != DDI_SUCCESS) {
3462 		return (DDI_FAILURE);
3463 	}
3464 	PMD(PMD_KIDSUP, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
3465 	ASSERT(PM_GET_PM_INFO(dip) == NULL);
3466 	info = kmem_zalloc(sizeof (pm_info_t), KM_SLEEP);
3467 
3468 	/*
3469 	 * Now set up parent's kidsupcnt.  BC nodes are assumed to start
3470 	 * out at their normal power, so they are "up", others start out
3471 	 * unknown, which is effectively "up".  Parent which want notification
3472 	 * get kidsupcnt of 0 always.
3473 	 */
3474 	count = (PM_ISBC(dip)) ? 1 : PM_NUMCMPTS(dip);
3475 	if (count && pdip && !PM_WANTS_NOTIFICATION(pdip))
3476 		e_pm_hold_rele_power(pdip, count);
3477 
3478 	pm_set_pm_info(dip, info);
3479 	/*
3480 	 * Apply any recorded thresholds
3481 	 */
3482 	(void) pm_thresh_specd(dip);
3483 
3484 	/*
3485 	 * Do dependency processing.
3486 	 */
3487 	pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3488 	(void) ddi_pathname(dip, pathbuf);
3489 	pm_dispatch_to_dep_thread(PM_DEP_WK_ATTACH, pathbuf, pathbuf,
3490 	    PM_DEP_NOWAIT, NULL, 0);
3491 	kmem_free(pathbuf, MAXPATHLEN);
3492 
3493 	if (!PM_ISBC(dip)) {
3494 		mutex_enter(&pm_scan_lock);
3495 		if (autopm_enabled) {
3496 			pm_scan_init(dip);
3497 			mutex_exit(&pm_scan_lock);
3498 			pm_rescan(dip);
3499 		} else {
3500 			mutex_exit(&pm_scan_lock);
3501 		}
3502 	}
3503 	return (0);
3504 }
3505 
3506 /*
3507  * This is the obsolete exported interface for a driver to find out its
3508  * "normal" (max) power.
3509  * We only get components destroyed while no power management is
3510  * going on (and the device is detached), so we don't need a mutex here
3511  */
3512 int
3513 pm_get_normal_power(dev_info_t *dip, int comp)
3514 {
3515 
3516 	if (comp >= 0 && comp < PM_NUMCMPTS(dip)) {
3517 		return (PM_CP(dip, comp)->pmc_norm_pwr);
3518 	}
3519 	return (DDI_FAILURE);
3520 }
3521 
3522 /*
3523  * Fetches the current power level.  Return DDI_SUCCESS or DDI_FAILURE.
3524  */
3525 int
3526 pm_get_current_power(dev_info_t *dip, int comp, int *levelp)
3527 {
3528 	if (comp >= 0 && comp < PM_NUMCMPTS(dip)) {
3529 		*levelp = PM_CURPOWER(dip, comp);
3530 		return (DDI_SUCCESS);
3531 	}
3532 	return (DDI_FAILURE);
3533 }
3534 
3535 /*
3536  * Returns current threshold of indicated component
3537  */
3538 static int
3539 cur_threshold(dev_info_t *dip, int comp)
3540 {
3541 	pm_component_t *cp = PM_CP(dip, comp);
3542 	int pwr;
3543 
3544 	if (PM_ISBC(dip)) {
3545 		/*
3546 		 * backwards compatible nodes only have one threshold
3547 		 */
3548 		return (cp->pmc_comp.pmc_thresh[1]);
3549 	}
3550 	pwr = cp->pmc_cur_pwr;
3551 	if (pwr == PM_LEVEL_UNKNOWN) {
3552 		int thresh;
3553 		if (DEVI(dip)->devi_pm_flags & PMC_NEXDEF_THRESH)
3554 			thresh = pm_default_nexus_threshold;
3555 		else
3556 			thresh = pm_system_idle_threshold;
3557 		return (thresh);
3558 	}
3559 	ASSERT(cp->pmc_comp.pmc_thresh);
3560 	return (cp->pmc_comp.pmc_thresh[pwr]);
3561 }
3562 
3563 /*
3564  * Compute next lower component power level given power index.
3565  */
3566 static int
3567 pm_next_lower_power(pm_component_t *cp, int pwrndx)
3568 {
3569 	int nxt_pwr;
3570 
3571 	if (pwrndx == PM_LEVEL_UNKNOWN) {
3572 		nxt_pwr = cp->pmc_comp.pmc_lvals[0];
3573 	} else {
3574 		pwrndx--;
3575 		ASSERT(pwrndx >= 0);
3576 		nxt_pwr = cp->pmc_comp.pmc_lvals[pwrndx];
3577 	}
3578 	return (nxt_pwr);
3579 }
3580 
3581 /*
3582  * Bring all components of device to normal power
3583  */
3584 int
3585 pm_all_to_normal(dev_info_t *dip, pm_canblock_t canblock)
3586 {
3587 	PMD_FUNC(pmf, "all_to_normal")
3588 	int		*normal;
3589 	int		i, ncomps, result;
3590 	size_t		size;
3591 	int		changefailed = 0;
3592 
3593 	PMD(PMD_ALLNORM, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
3594 	ASSERT(PM_GET_PM_INFO(dip));
3595 	if (pm_get_norm_pwrs(dip, &normal, &size) != DDI_SUCCESS) {
3596 		PMD(PMD_ALLNORM, ("%s: can't get norm pwrs for "
3597 		    "%s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
3598 		return (DDI_FAILURE);
3599 	}
3600 	ncomps = PM_NUMCMPTS(dip);
3601 	for (i = 0; i < ncomps; i++) {
3602 		if (pm_set_power(dip, i, normal[i],
3603 		    PM_LEVEL_UPONLY, canblock, 0, &result) != DDI_SUCCESS) {
3604 			changefailed++;
3605 			PMD(PMD_ALLNORM | PMD_FAIL, ("%s: failed to set "
3606 			    "%s@%s(%s#%d)[%d] to %d, errno %d\n", pmf,
3607 			    PM_DEVICE(dip), i, normal[i], result))
3608 		}
3609 	}
3610 	kmem_free(normal, size);
3611 	if (changefailed) {
3612 		PMD(PMD_FAIL, ("%s: failed to set %d comps %s@%s(%s#%d) "
3613 		    "to full power\n", pmf, changefailed, PM_DEVICE(dip)))
3614 		return (DDI_FAILURE);
3615 	}
3616 	return (DDI_SUCCESS);
3617 }
3618 
3619 /*
3620  * Returns true if all components of device are at normal power
3621  */
3622 int
3623 pm_all_at_normal(dev_info_t *dip)
3624 {
3625 	PMD_FUNC(pmf, "all_at_normal")
3626 	int		*normal;
3627 	int		i;
3628 	size_t		size;
3629 
3630 	PMD(PMD_ALLNORM, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
3631 	if (pm_get_norm_pwrs(dip, &normal, &size) != DDI_SUCCESS) {
3632 		PMD(PMD_ALLNORM, ("%s: can't get normal power\n", pmf))
3633 		return (DDI_FAILURE);
3634 	}
3635 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
3636 		int current = PM_CURPOWER(dip, i);
3637 		if (normal[i] > current) {
3638 			PMD(PMD_ALLNORM, ("%s: %s@%s(%s#%d) comp=%d, "
3639 			    "norm=%d, cur=%d\n", pmf, PM_DEVICE(dip), i,
3640 			    normal[i], current))
3641 			break;
3642 		}
3643 	}
3644 	kmem_free(normal, size);
3645 	if (i != PM_NUMCMPTS(dip)) {
3646 		return (0);
3647 	}
3648 	return (1);
3649 }
3650 
3651 static void
3652 bring_wekeeps_up(char *keeper)
3653 {
3654 	PMD_FUNC(pmf, "bring_wekeeps_up")
3655 	int i;
3656 	pm_pdr_t *dp;
3657 	pm_info_t *wku_info;
3658 	char *kept_path;
3659 	dev_info_t *kept;
3660 	static void bring_pmdep_up(dev_info_t *, int);
3661 
3662 	if (panicstr) {
3663 		return;
3664 	}
3665 	/*
3666 	 * We process the request even if the keeper detaches because
3667 	 * detach processing expects this to increment kidsupcnt of kept.
3668 	 */
3669 	PMD(PMD_BRING, ("%s: keeper= %s\n", pmf, keeper))
3670 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
3671 		if (strcmp(dp->pdr_keeper, keeper) != 0)
3672 			continue;
3673 		for (i = 0; i < dp->pdr_kept_count; i++) {
3674 			kept_path = dp->pdr_kept_paths[i];
3675 			if (kept_path == NULL)
3676 				continue;
3677 			ASSERT(kept_path[0] != '\0');
3678 			if ((kept = pm_name_to_dip(kept_path, 1)) == NULL)
3679 				continue;
3680 			wku_info = PM_GET_PM_INFO(kept);
3681 			if (wku_info == NULL) {
3682 				if (kept)
3683 					ddi_release_devi(kept);
3684 				continue;
3685 			}
3686 			/*
3687 			 * Don't mess with it if it is being detached, it isn't
3688 			 * safe to call its power entry point
3689 			 */
3690 			if (wku_info->pmi_dev_pm_state & PM_DETACHING) {
3691 				if (kept)
3692 					ddi_release_devi(kept);
3693 				continue;
3694 			}
3695 			bring_pmdep_up(kept, 1);
3696 			ddi_release_devi(kept);
3697 		}
3698 	}
3699 }
3700 
3701 /*
3702  * Bring up the 'kept' device passed as argument
3703  */
3704 static void
3705 bring_pmdep_up(dev_info_t *kept_dip, int hold)
3706 {
3707 	PMD_FUNC(pmf, "bring_pmdep_up")
3708 	int is_all_at_normal = 0;
3709 
3710 	/*
3711 	 * If the kept device has been unmanaged, do nothing.
3712 	 */
3713 	if (!PM_GET_PM_INFO(kept_dip))
3714 		return;
3715 
3716 	/* Just ignore DIRECT PM device till they are released. */
3717 	if (!pm_processes_stopped && PM_ISDIRECT(kept_dip) &&
3718 	    !(is_all_at_normal = pm_all_at_normal(kept_dip))) {
3719 		PMD(PMD_BRING, ("%s: can't bring up PM_DIRECT %s@%s(%s#%d) "
3720 		    "controlling process did something else\n", pmf,
3721 		    PM_DEVICE(kept_dip)))
3722 		DEVI(kept_dip)->devi_pm_flags |= PMC_SKIP_BRINGUP;
3723 		return;
3724 	}
3725 	/* if we got here the keeper had a transition from OFF->ON */
3726 	if (hold)
3727 		pm_hold_power(kept_dip);
3728 
3729 	if (!is_all_at_normal)
3730 		(void) pm_all_to_normal(kept_dip, PM_CANBLOCK_FAIL);
3731 }
3732 
3733 /*
3734  * A bunch of stuff that belongs only to the next routine (or two)
3735  */
3736 
3737 static const char namestr[] = "NAME=";
3738 static const int nameln = sizeof (namestr) - 1;
3739 static const char pmcompstr[] = "pm-components";
3740 
3741 struct pm_comp_pkg {
3742 	pm_comp_t		*comp;
3743 	struct pm_comp_pkg	*next;
3744 };
3745 
3746 #define	isdigit(ch)	((ch) >= '0' && (ch) <= '9')
3747 
3748 #define	isxdigit(ch)	(isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \
3749 			((ch) >= 'A' && (ch) <= 'F'))
3750 
3751 /*
3752  * Rather than duplicate this code ...
3753  * (this code excerpted from the function that follows it)
3754  */
3755 #define	FINISH_COMP { \
3756 	ASSERT(compp); \
3757 	compp->pmc_lnames_sz = size; \
3758 	tp = compp->pmc_lname_buf = kmem_alloc(size, KM_SLEEP); \
3759 	compp->pmc_numlevels = level; \
3760 	compp->pmc_lnames = kmem_alloc(level * sizeof (char *), KM_SLEEP); \
3761 	compp->pmc_lvals = kmem_alloc(level * sizeof (int), KM_SLEEP); \
3762 	compp->pmc_thresh = kmem_alloc(level * sizeof (int), KM_SLEEP); \
3763 	/* copy string out of prop array into buffer */ \
3764 	for (j = 0; j < level; j++) { \
3765 		compp->pmc_thresh[j] = INT_MAX;		/* only [0] sticks */ \
3766 		compp->pmc_lvals[j] = lvals[j]; \
3767 		(void) strcpy(tp, lnames[j]); \
3768 		compp->pmc_lnames[j] = tp; \
3769 		tp += lszs[j]; \
3770 	} \
3771 	ASSERT(tp > compp->pmc_lname_buf && tp <= \
3772 	    compp->pmc_lname_buf + compp->pmc_lnames_sz); \
3773 	}
3774 
3775 /*
3776  * Create (empty) component data structures.
3777  */
3778 static void
3779 e_pm_create_components(dev_info_t *dip, int num_components)
3780 {
3781 	struct pm_component *compp, *ocompp;
3782 	int i, size = 0;
3783 
3784 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
3785 	ASSERT(!DEVI(dip)->devi_pm_components);
3786 	ASSERT(!(DEVI(dip)->devi_pm_flags & PMC_COMPONENTS_DONE));
3787 	size = sizeof (struct pm_component) * num_components;
3788 
3789 	compp = kmem_zalloc(size, KM_SLEEP);
3790 	ocompp = compp;
3791 	DEVI(dip)->devi_pm_comp_size = size;
3792 	DEVI(dip)->devi_pm_num_components = num_components;
3793 	PM_LOCK_BUSY(dip);
3794 	for (i = 0; i < num_components;  i++) {
3795 		compp->pmc_timestamp = gethrestime_sec();
3796 		compp->pmc_norm_pwr = (uint_t)-1;
3797 		compp++;
3798 	}
3799 	PM_UNLOCK_BUSY(dip);
3800 	DEVI(dip)->devi_pm_components = ocompp;
3801 	DEVI(dip)->devi_pm_flags |= PMC_COMPONENTS_DONE;
3802 }
3803 
3804 /*
3805  * Parse hex or decimal value from char string
3806  */
3807 static char *
3808 pm_parsenum(char *cp, int *valp)
3809 {
3810 	int ch, offset;
3811 	char numbuf[256];
3812 	char *np = numbuf;
3813 	int value = 0;
3814 
3815 	ch = *cp++;
3816 	if (isdigit(ch)) {
3817 		if (ch == '0') {
3818 			if ((ch = *cp++) == 'x' || ch == 'X') {
3819 				ch = *cp++;
3820 				while (isxdigit(ch)) {
3821 					*np++ = (char)ch;
3822 					ch = *cp++;
3823 				}
3824 				*np = 0;
3825 				cp--;
3826 				goto hexval;
3827 			} else {
3828 				goto digit;
3829 			}
3830 		} else {
3831 digit:
3832 			while (isdigit(ch)) {
3833 				*np++ = (char)ch;
3834 				ch = *cp++;
3835 			}
3836 			*np = 0;
3837 			cp--;
3838 			goto decval;
3839 		}
3840 	} else
3841 		return (NULL);
3842 
3843 hexval:
3844 	for (np = numbuf; *np; np++) {
3845 		if (*np >= 'a' && *np <= 'f')
3846 			offset = 'a' - 10;
3847 		else if (*np >= 'A' && *np <= 'F')
3848 			offset = 'A' - 10;
3849 		else if (*np >= '0' && *np <= '9')
3850 			offset = '0';
3851 		value *= 16;
3852 		value += *np - offset;
3853 	}
3854 	*valp = value;
3855 	return (cp);
3856 
3857 decval:
3858 	offset = '0';
3859 	for (np = numbuf; *np; np++) {
3860 		value *= 10;
3861 		value += *np - offset;
3862 	}
3863 	*valp = value;
3864 	return (cp);
3865 }
3866 
3867 /*
3868  * Set max (previously documented as "normal") power.
3869  */
3870 static void
3871 e_pm_set_max_power(dev_info_t *dip, int component_number, int level)
3872 {
3873 	PM_CP(dip, component_number)->pmc_norm_pwr = level;
3874 }
3875 
3876 /*
3877  * Internal routine for destroying components
3878  * It is called even when there might not be any, so it must be forgiving.
3879  */
3880 static void
3881 e_pm_destroy_components(dev_info_t *dip)
3882 {
3883 	int i;
3884 	struct pm_component *cp;
3885 
3886 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
3887 	if (PM_NUMCMPTS(dip) == 0)
3888 		return;
3889 	cp = DEVI(dip)->devi_pm_components;
3890 	ASSERT(cp);
3891 	for (i = 0; i < PM_NUMCMPTS(dip); i++, cp++) {
3892 		int nlevels = cp->pmc_comp.pmc_numlevels;
3893 		kmem_free(cp->pmc_comp.pmc_lvals, nlevels * sizeof (int));
3894 		kmem_free(cp->pmc_comp.pmc_thresh, nlevels * sizeof (int));
3895 		/*
3896 		 * For BC nodes, the rest is static in bc_comp, so skip it
3897 		 */
3898 		if (PM_ISBC(dip))
3899 			continue;
3900 		kmem_free(cp->pmc_comp.pmc_name, cp->pmc_comp.pmc_name_sz);
3901 		kmem_free(cp->pmc_comp.pmc_lnames, nlevels * sizeof (char *));
3902 		kmem_free(cp->pmc_comp.pmc_lname_buf,
3903 				cp->pmc_comp.pmc_lnames_sz);
3904 	}
3905 	kmem_free(DEVI(dip)->devi_pm_components, DEVI(dip)->devi_pm_comp_size);
3906 	DEVI(dip)->devi_pm_components = NULL;
3907 	DEVI(dip)->devi_pm_num_components = 0;
3908 	DEVI(dip)->devi_pm_flags &=
3909 	    ~(PMC_COMPONENTS_DONE | PMC_COMPONENTS_FAILED);
3910 }
3911 
3912 /*
3913  * Read the pm-components property (if there is one) and use it to set up
3914  * components.  Returns a pointer to an array of component structures if
3915  * pm-components found and successfully parsed, else returns NULL.
3916  * Sets error return *errp to true to indicate a failure (as opposed to no
3917  * property being present).
3918  */
3919 pm_comp_t *
3920 pm_autoconfig(dev_info_t *dip, int *errp)
3921 {
3922 	PMD_FUNC(pmf, "autoconfig")
3923 	uint_t nelems;
3924 	char **pp;
3925 	pm_comp_t *compp = NULL;
3926 	int i, j, level, components = 0;
3927 	size_t size = 0;
3928 	struct pm_comp_pkg *p, *ptail;
3929 	struct pm_comp_pkg *phead = NULL;
3930 	int *lvals = NULL;
3931 	int *lszs = NULL;
3932 	int *np = NULL;
3933 	int npi = 0;
3934 	char **lnames = NULL;
3935 	char *cp, *tp;
3936 	pm_comp_t *ret = NULL;
3937 
3938 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
3939 	*errp = 0;	/* assume success */
3940 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3941 	    (char *)pmcompstr, &pp, &nelems) != DDI_PROP_SUCCESS) {
3942 		return (NULL);
3943 	}
3944 
3945 	if (nelems < 3) {	/* need at least one name and two levels */
3946 		goto errout;
3947 	}
3948 
3949 	/*
3950 	 * pm_create_components is no longer allowed
3951 	 */
3952 	if (PM_NUMCMPTS(dip) != 0) {
3953 		PMD(PMD_ERROR, ("%s: %s@%s(%s#%d) has %d comps\n",
3954 		    pmf, PM_DEVICE(dip), PM_NUMCMPTS(dip)))
3955 		goto errout;
3956 	}
3957 
3958 	lvals = kmem_alloc(nelems * sizeof (int), KM_SLEEP);
3959 	lszs = kmem_alloc(nelems * sizeof (int), KM_SLEEP);
3960 	lnames = kmem_alloc(nelems * sizeof (char *), KM_SLEEP);
3961 	np = kmem_alloc(nelems * sizeof (int), KM_SLEEP);
3962 
3963 	level = 0;
3964 	phead = NULL;
3965 	for (i = 0; i < nelems; i++) {
3966 		cp = pp[i];
3967 		if (!isdigit(*cp)) {	/*  must be name */
3968 			if (strncmp(cp, namestr, nameln) != 0) {
3969 				goto errout;
3970 			}
3971 			if (i != 0) {
3972 				if (level == 0) {	/* no level spec'd */
3973 					PMD(PMD_ERROR, ("%s: no level spec'd\n",
3974 					    pmf))
3975 					goto errout;
3976 				}
3977 				np[npi++] = lvals[level - 1];
3978 				/* finish up previous component levels */
3979 				FINISH_COMP;
3980 			}
3981 			cp += nameln;
3982 			if (!*cp) {
3983 				PMD(PMD_ERROR, ("%s: nsa\n", pmf))
3984 				goto errout;
3985 			}
3986 			p = kmem_zalloc(sizeof (*phead), KM_SLEEP);
3987 			if (phead == NULL) {
3988 				phead = ptail = p;
3989 			} else {
3990 				ptail->next = p;
3991 				ptail = p;
3992 			}
3993 			compp = p->comp = kmem_zalloc(sizeof (pm_comp_t),
3994 			    KM_SLEEP);
3995 			compp->pmc_name_sz = strlen(cp) + 1;
3996 			compp->pmc_name = kmem_zalloc(compp->pmc_name_sz,
3997 			    KM_SLEEP);
3998 			(void) strncpy(compp->pmc_name, cp, compp->pmc_name_sz);
3999 			components++;
4000 			level = 0;
4001 		} else {	/* better be power level <num>=<name> */
4002 #ifdef DEBUG
4003 			tp = cp;
4004 #endif
4005 			if (i == 0 ||
4006 			    (cp = pm_parsenum(cp, &lvals[level])) == NULL) {
4007 				PMD(PMD_ERROR, ("%s: parsenum(%s)\n", pmf, tp))
4008 				goto errout;
4009 			}
4010 #ifdef DEBUG
4011 			tp = cp;
4012 #endif
4013 			if (*cp++ != '=' || !*cp) {
4014 				PMD(PMD_ERROR, ("%s: ex =, got %s\n", pmf, tp))
4015 				goto errout;
4016 			}
4017 
4018 			lszs[level] = strlen(cp) + 1;
4019 			size += lszs[level];
4020 			lnames[level] = cp;	/* points into prop string */
4021 			level++;
4022 		}
4023 	}
4024 	np[npi++] = lvals[level - 1];
4025 	if (level == 0) {	/* ended with a name */
4026 		PMD(PMD_ERROR, ("%s: ewn\n", pmf))
4027 		goto errout;
4028 	}
4029 	FINISH_COMP;
4030 
4031 
4032 	/*
4033 	 * Now we have a list of components--we have to return instead an
4034 	 * array of them, but we can just copy the top level and leave
4035 	 * the rest as is
4036 	 */
4037 	(void) e_pm_create_components(dip, components);
4038 	for (i = 0; i < components; i++)
4039 		e_pm_set_max_power(dip, i, np[i]);
4040 
4041 	ret = kmem_zalloc(components * sizeof (pm_comp_t), KM_SLEEP);
4042 	for (i = 0, p = phead; i < components; i++) {
4043 		ASSERT(p);
4044 		/*
4045 		 * Now sanity-check values:  levels must be monotonically
4046 		 * increasing
4047 		 */
4048 		if (p->comp->pmc_numlevels < 2) {
4049 			PMD(PMD_ERROR, ("%s: comp %s of %s@%s(%s#%d) only %d "
4050 			    "levels\n", pmf,
4051 			    p->comp->pmc_name, PM_DEVICE(dip),
4052 			    p->comp->pmc_numlevels))
4053 			goto errout;
4054 		}
4055 		for (j = 0; j < p->comp->pmc_numlevels; j++) {
4056 			if ((p->comp->pmc_lvals[j] < 0) || ((j > 0) &&
4057 			    (p->comp->pmc_lvals[j] <=
4058 			    p->comp->pmc_lvals[j - 1]))) {
4059 				PMD(PMD_ERROR, ("%s: comp %s of %s@%s(%s#%d) "
4060 				    "not mono. incr, %d follows %d\n", pmf,
4061 				    p->comp->pmc_name, PM_DEVICE(dip),
4062 				    p->comp->pmc_lvals[j],
4063 				    p->comp->pmc_lvals[j - 1]))
4064 				goto errout;
4065 			}
4066 		}
4067 		ret[i] = *p->comp;	/* struct assignment */
4068 		for (j = 0; j < i; j++) {
4069 			/*
4070 			 * Test for unique component names
4071 			 */
4072 			if (strcmp(ret[j].pmc_name, ret[i].pmc_name) == 0) {
4073 				PMD(PMD_ERROR, ("%s: %s of %s@%s(%s#%d) not "
4074 				    "unique\n", pmf, ret[j].pmc_name,
4075 				    PM_DEVICE(dip)))
4076 				goto errout;
4077 			}
4078 		}
4079 		ptail = p;
4080 		p = p->next;
4081 		phead = p;	/* errout depends on phead making sense */
4082 		kmem_free(ptail->comp, sizeof (*ptail->comp));
4083 		kmem_free(ptail, sizeof (*ptail));
4084 	}
4085 out:
4086 	ddi_prop_free(pp);
4087 	if (lvals)
4088 		kmem_free(lvals, nelems * sizeof (int));
4089 	if (lszs)
4090 		kmem_free(lszs, nelems * sizeof (int));
4091 	if (lnames)
4092 		kmem_free(lnames, nelems * sizeof (char *));
4093 	if (np)
4094 		kmem_free(np, nelems * sizeof (int));
4095 	return (ret);
4096 
4097 errout:
4098 	e_pm_destroy_components(dip);
4099 	*errp = 1;	/* signal failure */
4100 	cmn_err(CE_CONT, "!pm: %s property ", pmcompstr);
4101 	for (i = 0; i < nelems - 1; i++)
4102 		cmn_err(CE_CONT, "!'%s', ", pp[i]);
4103 	if (nelems != 0)
4104 		cmn_err(CE_CONT, "!'%s'", pp[nelems - 1]);
4105 	cmn_err(CE_CONT, "! for %s@%s(%s#%d) is ill-formed.\n", PM_DEVICE(dip));
4106 	for (p = phead; p; ) {
4107 		pm_comp_t *pp;
4108 		int n;
4109 
4110 		ptail = p;
4111 		/*
4112 		 * Free component data structures
4113 		 */
4114 		pp = p->comp;
4115 		n = pp->pmc_numlevels;
4116 		if (pp->pmc_name_sz) {
4117 			kmem_free(pp->pmc_name, pp->pmc_name_sz);
4118 		}
4119 		if (pp->pmc_lnames_sz) {
4120 			kmem_free(pp->pmc_lname_buf, pp->pmc_lnames_sz);
4121 		}
4122 		if (pp->pmc_lnames) {
4123 			kmem_free(pp->pmc_lnames, n * (sizeof (char *)));
4124 		}
4125 		if (pp->pmc_thresh) {
4126 			kmem_free(pp->pmc_thresh, n * (sizeof (int)));
4127 		}
4128 		if (pp->pmc_lvals) {
4129 			kmem_free(pp->pmc_lvals, n * (sizeof (int)));
4130 		}
4131 		p = ptail->next;
4132 		kmem_free(ptail, sizeof (*ptail));
4133 	}
4134 	if (ret != NULL)
4135 		kmem_free(ret, components * sizeof (pm_comp_t));
4136 	ret = NULL;
4137 	goto out;
4138 }
4139 
4140 /*
4141  * Set threshold values for a devices components by dividing the target
4142  * threshold (base) by the number of transitions and assign each transition
4143  * that threshold.  This will get the entire device down in the target time if
4144  * all components are idle and even if there are dependencies among components.
4145  *
4146  * Devices may well get powered all the way down before the target time, but
4147  * at least the EPA will be happy.
4148  */
4149 void
4150 pm_set_device_threshold(dev_info_t *dip, int base, int flag)
4151 {
4152 	PMD_FUNC(pmf, "set_device_threshold")
4153 	int target_threshold = (base * 95) / 100;
4154 	int level, comp;		/* loop counters */
4155 	int transitions = 0;
4156 	int ncomp = PM_NUMCMPTS(dip);
4157 	int thresh;
4158 	int remainder;
4159 	pm_comp_t *pmc;
4160 	int i, circ;
4161 
4162 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
4163 	PM_LOCK_DIP(dip);
4164 	/*
4165 	 * First we handle the easy one.  If we're setting the default
4166 	 * threshold for a node with children, then we set it to the
4167 	 * default nexus threshold (currently 0) and mark it as default
4168 	 * nexus threshold instead
4169 	 */
4170 	if (PM_IS_NEXUS(dip)) {
4171 		if (flag == PMC_DEF_THRESH) {
4172 			PMD(PMD_THRESH, ("%s: [%s@%s(%s#%d) NEXDEF]\n", pmf,
4173 			    PM_DEVICE(dip)))
4174 			thresh = pm_default_nexus_threshold;
4175 			for (comp = 0; comp < ncomp; comp++) {
4176 				pmc = &PM_CP(dip, comp)->pmc_comp;
4177 				for (level = 1; level < pmc->pmc_numlevels;
4178 				    level++) {
4179 					pmc->pmc_thresh[level] = thresh;
4180 				}
4181 			}
4182 			DEVI(dip)->devi_pm_dev_thresh =
4183 			    pm_default_nexus_threshold;
4184 			/*
4185 			 * If the nexus node is being reconfigured back to
4186 			 * the default threshold, adjust the notlowest count.
4187 			 */
4188 			if (DEVI(dip)->devi_pm_flags &
4189 			    (PMC_DEV_THRESH|PMC_COMP_THRESH)) {
4190 				PM_LOCK_POWER(dip, &circ);
4191 				for (i = 0; i < PM_NUMCMPTS(dip); i++) {
4192 					if (PM_CURPOWER(dip, i) == 0)
4193 						continue;
4194 					mutex_enter(&pm_compcnt_lock);
4195 					ASSERT(pm_comps_notlowest);
4196 					pm_comps_notlowest--;
4197 					PMD(PMD_LEVEL, ("%s: %s@%s(%s#%d) decr "
4198 					    "notlowest to %d\n", pmf,
4199 					    PM_DEVICE(dip), pm_comps_notlowest))
4200 					if (pm_comps_notlowest == 0)
4201 						pm_ppm_notify_all_lowest(dip,
4202 						    PM_ALL_LOWEST);
4203 					mutex_exit(&pm_compcnt_lock);
4204 				}
4205 				PM_UNLOCK_POWER(dip, circ);
4206 			}
4207 			DEVI(dip)->devi_pm_flags &= PMC_THRESH_NONE;
4208 			DEVI(dip)->devi_pm_flags |= PMC_NEXDEF_THRESH;
4209 			PM_UNLOCK_DIP(dip);
4210 			return;
4211 		} else if (DEVI(dip)->devi_pm_flags & PMC_NEXDEF_THRESH) {
4212 			/*
4213 			 * If the nexus node is being configured for a
4214 			 * non-default threshold, include that node in
4215 			 * the notlowest accounting.
4216 			 */
4217 			PM_LOCK_POWER(dip, &circ);
4218 			for (i = 0; i < PM_NUMCMPTS(dip); i++) {
4219 				if (PM_CURPOWER(dip, i) == 0)
4220 					continue;
4221 				mutex_enter(&pm_compcnt_lock);
4222 				if (pm_comps_notlowest == 0)
4223 					pm_ppm_notify_all_lowest(dip,
4224 					    PM_NOT_ALL_LOWEST);
4225 				pm_comps_notlowest++;
4226 				PMD(PMD_LEVEL, ("%s: %s@%s(%s#%d) incr "
4227 				    "notlowest to %d\n", pmf,
4228 				    PM_DEVICE(dip), pm_comps_notlowest))
4229 				mutex_exit(&pm_compcnt_lock);
4230 			}
4231 			PM_UNLOCK_POWER(dip, circ);
4232 		}
4233 	}
4234 	/*
4235 	 * Compute the total number of transitions for all components
4236 	 * of the device.  Distribute the threshold evenly over them
4237 	 */
4238 	for (comp = 0; comp < ncomp; comp++) {
4239 		pmc = &PM_CP(dip, comp)->pmc_comp;
4240 		ASSERT(pmc->pmc_numlevels > 1);
4241 		transitions += pmc->pmc_numlevels - 1;
4242 	}
4243 	ASSERT(transitions);
4244 	thresh = target_threshold / transitions;
4245 
4246 	for (comp = 0; comp < ncomp; comp++) {
4247 		pmc = &PM_CP(dip, comp)->pmc_comp;
4248 		for (level = 1; level < pmc->pmc_numlevels; level++) {
4249 			pmc->pmc_thresh[level] = thresh;
4250 		}
4251 	}
4252 
4253 #ifdef DEBUG
4254 	for (comp = 0; comp < ncomp; comp++) {
4255 		pmc = &PM_CP(dip, comp)->pmc_comp;
4256 		for (level = 1; level < pmc->pmc_numlevels; level++) {
4257 			PMD(PMD_THRESH, ("%s: thresh before %s@%s(%s#%d) "
4258 			    "comp=%d, level=%d, %d\n", pmf, PM_DEVICE(dip),
4259 			    comp, level, pmc->pmc_thresh[level]))
4260 		}
4261 	}
4262 #endif
4263 	/*
4264 	 * Distribute any remainder till they are all gone
4265 	 */
4266 	remainder = target_threshold - thresh * transitions;
4267 	level = 1;
4268 #ifdef DEBUG
4269 	PMD(PMD_THRESH, ("%s: remainder=%d target_threshold=%d thresh=%d "
4270 	    "trans=%d\n", pmf, remainder, target_threshold, thresh,
4271 	    transitions))
4272 #endif
4273 	while (remainder > 0) {
4274 		comp = 0;
4275 		while (remainder && (comp < ncomp)) {
4276 			pmc = &PM_CP(dip, comp)->pmc_comp;
4277 			if (level < pmc->pmc_numlevels) {
4278 				pmc->pmc_thresh[level] += 1;
4279 				remainder--;
4280 			}
4281 			comp++;
4282 		}
4283 		level++;
4284 	}
4285 #ifdef DEBUG
4286 	for (comp = 0; comp < ncomp; comp++) {
4287 		pmc = &PM_CP(dip, comp)->pmc_comp;
4288 		for (level = 1; level < pmc->pmc_numlevels; level++) {
4289 			PMD(PMD_THRESH, ("%s: thresh after %s@%s(%s#%d) "
4290 			    "comp=%d level=%d, %d\n", pmf, PM_DEVICE(dip),
4291 			    comp, level, pmc->pmc_thresh[level]))
4292 		}
4293 	}
4294 #endif
4295 	ASSERT(PM_IAM_LOCKING_DIP(dip));
4296 	DEVI(dip)->devi_pm_dev_thresh = base;
4297 	DEVI(dip)->devi_pm_flags &= PMC_THRESH_NONE;
4298 	DEVI(dip)->devi_pm_flags |= flag;
4299 	PM_UNLOCK_DIP(dip);
4300 }
4301 
4302 /*
4303  * Called when there is no old-style platform power management driver
4304  */
4305 static int
4306 ddi_no_platform_power(power_req_t *req)
4307 {
4308 	_NOTE(ARGUNUSED(req))
4309 	return (DDI_FAILURE);
4310 }
4311 
4312 /*
4313  * This function calls the entry point supplied by the platform-specific
4314  * pm driver to bring the device component 'pm_cmpt' to power level 'pm_level'.
4315  * The use of global for getting the  function name from platform-specific
4316  * pm driver is not ideal, but it is simple and efficient.
4317  * The previous property lookup was being done in the idle loop on swift
4318  * systems without pmc chips and hurt deskbench performance as well as
4319  * violating scheduler locking rules
4320  */
4321 int	(*pm_platform_power)(power_req_t *) = ddi_no_platform_power;
4322 
4323 /*
4324  * Old obsolete interface for a device to request a power change (but only
4325  * an increase in power)
4326  */
4327 int
4328 ddi_dev_is_needed(dev_info_t *dip, int cmpt, int level)
4329 {
4330 	return (pm_raise_power(dip, cmpt, level));
4331 }
4332 
4333 /*
4334  * The old obsolete interface to platform power management.  Only used by
4335  * Gypsy platform and APM on X86.
4336  */
4337 int
4338 ddi_power(dev_info_t *dip, int pm_cmpt, int pm_level)
4339 {
4340 	power_req_t	request;
4341 
4342 	request.request_type = PMR_SET_POWER;
4343 	request.req.set_power_req.who = dip;
4344 	request.req.set_power_req.cmpt = pm_cmpt;
4345 	request.req.set_power_req.level = pm_level;
4346 	return (ddi_ctlops(dip, dip, DDI_CTLOPS_POWER, &request, NULL));
4347 }
4348 
4349 /*
4350  * A driver can invoke this from its detach routine when DDI_SUSPEND is
4351  * passed.  Returns true if subsequent processing could result in power being
4352  * removed from the device.  The arg is not currently used because it is
4353  * implicit in the operation of cpr/DR.
4354  */
4355 int
4356 ddi_removing_power(dev_info_t *dip)
4357 {
4358 	_NOTE(ARGUNUSED(dip))
4359 	return (pm_powering_down);
4360 }
4361 
4362 /*
4363  * Returns true if a device indicates that its parent handles suspend/resume
4364  * processing for it.
4365  */
4366 int
4367 e_ddi_parental_suspend_resume(dev_info_t *dip)
4368 {
4369 	return (DEVI(dip)->devi_pm_flags & PMC_PARENTAL_SR);
4370 }
4371 
4372 /*
4373  * Called for devices which indicate that their parent does suspend/resume
4374  * handling for them
4375  */
4376 int
4377 e_ddi_suspend(dev_info_t *dip, ddi_detach_cmd_t cmd)
4378 {
4379 	power_req_t	request;
4380 	request.request_type = PMR_SUSPEND;
4381 	request.req.suspend_req.who = dip;
4382 	request.req.suspend_req.cmd = cmd;
4383 	return (ddi_ctlops(dip, dip, DDI_CTLOPS_POWER, &request, NULL));
4384 }
4385 
4386 /*
4387  * Called for devices which indicate that their parent does suspend/resume
4388  * handling for them
4389  */
4390 int
4391 e_ddi_resume(dev_info_t *dip, ddi_attach_cmd_t cmd)
4392 {
4393 	power_req_t	request;
4394 	request.request_type = PMR_RESUME;
4395 	request.req.resume_req.who = dip;
4396 	request.req.resume_req.cmd = cmd;
4397 	return (ddi_ctlops(dip, dip, DDI_CTLOPS_POWER, &request, NULL));
4398 }
4399 
4400 /*
4401  * Old obsolete exported interface for drivers to create components.
4402  * This is now handled by exporting the pm-components property.
4403  */
4404 int
4405 pm_create_components(dev_info_t *dip, int num_components)
4406 {
4407 	PMD_FUNC(pmf, "pm_create_components")
4408 
4409 	if (num_components < 1)
4410 		return (DDI_FAILURE);
4411 
4412 	if (!DEVI_IS_ATTACHING(dip)) {
4413 		return (DDI_FAILURE);
4414 	}
4415 
4416 	/* don't need to lock dip because attach is single threaded */
4417 	if (DEVI(dip)->devi_pm_components) {
4418 		PMD(PMD_ERROR, ("%s: %s@%s(%s#%d) already has %d\n", pmf,
4419 		    PM_DEVICE(dip), PM_NUMCMPTS(dip)))
4420 		return (DDI_FAILURE);
4421 	}
4422 	e_pm_create_components(dip, num_components);
4423 	DEVI(dip)->devi_pm_flags |= PMC_BC;
4424 	e_pm_default_components(dip, num_components);
4425 	return (DDI_SUCCESS);
4426 }
4427 
4428 /*
4429  * Obsolete interface previously called by drivers to destroy their components
4430  * at detach time.  This is now done automatically.  However, we need to keep
4431  * this for the old drivers.
4432  */
4433 void
4434 pm_destroy_components(dev_info_t *dip)
4435 {
4436 	PMD_FUNC(pmf, "pm_destroy_components")
4437 	dev_info_t *pdip = ddi_get_parent(dip);
4438 
4439 	PMD(PMD_REMDEV | PMD_KIDSUP, ("%s: %s@%s(%s#%d)\n", pmf,
4440 	    PM_DEVICE(dip)))
4441 	ASSERT(DEVI_IS_DETACHING(dip));
4442 #ifdef DEBUG
4443 	if (!PM_ISBC(dip))
4444 		cmn_err(CE_WARN, "!driver exporting pm-components property "
4445 		    "(%s@%s) calls pm_destroy_components", PM_NAME(dip),
4446 		    PM_ADDR(dip));
4447 #endif
4448 	/*
4449 	 * We ignore this unless this is an old-style driver, except for
4450 	 * printing the message above
4451 	 */
4452 	if (PM_NUMCMPTS(dip) == 0 || !PM_ISBC(dip)) {
4453 		PMD(PMD_REMDEV, ("%s: ignore %s@%s(%s#%d)\n", pmf,
4454 		    PM_DEVICE(dip)))
4455 		return;
4456 	}
4457 	ASSERT(PM_GET_PM_INFO(dip));
4458 
4459 	/*
4460 	 * pm_unmanage will clear info pointer later, after dealing with
4461 	 * dependencies
4462 	 */
4463 	ASSERT(!PM_GET_PM_SCAN(dip));	/* better be gone already */
4464 	/*
4465 	 * Now adjust parent's kidsupcnt.  We check only comp 0.
4466 	 * Parents that get notification are not adjusted because their
4467 	 * kidsupcnt is always 0 (or 1 during probe and attach).
4468 	 */
4469 	if ((PM_CURPOWER(dip, 0) != 0) && pdip && !PM_WANTS_NOTIFICATION(pdip))
4470 		pm_rele_power(pdip);
4471 #ifdef DEBUG
4472 	else {
4473 		PMD(PMD_KIDSUP, ("%s: kuc stays %s@%s(%s#%d) comps gone\n",
4474 		    pmf, PM_DEVICE(dip)))
4475 	}
4476 #endif
4477 	e_pm_destroy_components(dip);
4478 	/*
4479 	 * Forget we ever knew anything about the components of this  device
4480 	 */
4481 	DEVI(dip)->devi_pm_flags &=
4482 	    ~(PMC_BC | PMC_COMPONENTS_DONE | PMC_COMPONENTS_FAILED);
4483 }
4484 
4485 /*
4486  * Exported interface for a driver to set a component busy.
4487  */
4488 int
4489 pm_busy_component(dev_info_t *dip, int cmpt)
4490 {
4491 	struct pm_component *cp;
4492 
4493 	ASSERT(dip != NULL);
4494 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, cmpt, &cp))
4495 		return (DDI_FAILURE);
4496 	PM_LOCK_BUSY(dip);
4497 	cp->pmc_busycount++;
4498 	cp->pmc_timestamp = 0;
4499 	PM_UNLOCK_BUSY(dip);
4500 	return (DDI_SUCCESS);
4501 }
4502 
4503 /*
4504  * Exported interface for a driver to set a component idle.
4505  */
4506 int
4507 pm_idle_component(dev_info_t *dip, int cmpt)
4508 {
4509 	PMD_FUNC(pmf, "pm_idle_component")
4510 	struct pm_component *cp;
4511 	pm_scan_t	*scanp = PM_GET_PM_SCAN(dip);
4512 
4513 	if (!e_pm_valid_info(dip, NULL) || !e_pm_valid_comp(dip, cmpt, &cp))
4514 		return (DDI_FAILURE);
4515 
4516 	PM_LOCK_BUSY(dip);
4517 	if (cp->pmc_busycount) {
4518 		if (--(cp->pmc_busycount) == 0)
4519 			cp->pmc_timestamp = gethrestime_sec();
4520 	} else {
4521 		cp->pmc_timestamp = gethrestime_sec();
4522 	}
4523 
4524 	PM_UNLOCK_BUSY(dip);
4525 
4526 	/*
4527 	 * if device becomes idle during idle down period, try scan it down
4528 	 */
4529 	if (scanp && PM_IS_PID(dip)) {
4530 		PMD(PMD_IDLEDOWN, ("%s: %s@%s(%s#%d) idle.\n", pmf,
4531 		    PM_DEVICE(dip)))
4532 		pm_rescan(dip);
4533 		return (DDI_SUCCESS);
4534 	}
4535 
4536 	/*
4537 	 * handle scan not running with nexus threshold == 0
4538 	 */
4539 
4540 	if (PM_IS_NEXUS(dip) && (cp->pmc_busycount == 0)) {
4541 		pm_rescan(dip);
4542 	}
4543 
4544 	return (DDI_SUCCESS);
4545 }
4546 
4547 /*
4548  * This is the old  obsolete interface called by drivers to set their normal
4549  * power.  Thus we can't fix its behavior or return a value.
4550  * This functionality is replaced by the pm-component property.
4551  * We'll only get components destroyed while no power management is
4552  * going on (and the device is detached), so we don't need a mutex here
4553  */
4554 void
4555 pm_set_normal_power(dev_info_t *dip, int comp, int level)
4556 {
4557 	PMD_FUNC(pmf, "set_normal_power")
4558 #ifdef DEBUG
4559 	if (!PM_ISBC(dip))
4560 		cmn_err(CE_WARN, "!call to pm_set_normal_power() by %s@%s "
4561 		    "(driver exporting pm-components property) ignored",
4562 		    PM_NAME(dip), PM_ADDR(dip));
4563 #endif
4564 	if (PM_ISBC(dip)) {
4565 		PMD(PMD_NORM, ("%s: %s@%s(%s#%d) set normal power comp=%d, "
4566 		    "level=%d\n", pmf, PM_DEVICE(dip), comp, level))
4567 		e_pm_set_max_power(dip, comp, level);
4568 		e_pm_default_levels(dip, PM_CP(dip, comp), level);
4569 	}
4570 }
4571 
4572 /*
4573  * Called on a successfully detached driver to free pm resources
4574  */
4575 static void
4576 pm_stop(dev_info_t *dip)
4577 {
4578 	PMD_FUNC(pmf, "stop")
4579 	dev_info_t *pdip = ddi_get_parent(dip);
4580 
4581 	ASSERT(!PM_IAM_LOCKING_DIP(dip));
4582 	/* stopping scan, destroy scan data structure */
4583 	if (!PM_ISBC(dip)) {
4584 		pm_scan_stop(dip);
4585 		pm_scan_fini(dip);
4586 	}
4587 
4588 	if (PM_GET_PM_INFO(dip) != NULL) {
4589 		if (pm_unmanage(dip) == DDI_SUCCESS) {
4590 			/*
4591 			 * Old style driver may have called
4592 			 * pm_destroy_components already, but just in case ...
4593 			 */
4594 			e_pm_destroy_components(dip);
4595 		} else {
4596 			PMD(PMD_FAIL, ("%s: can't pm_unmanage %s@%s(%s#%d)\n",
4597 			    pmf, PM_DEVICE(dip)))
4598 		}
4599 	} else {
4600 		if (PM_NUMCMPTS(dip))
4601 			e_pm_destroy_components(dip);
4602 		else {
4603 			if (DEVI(dip)->devi_pm_flags & PMC_NOPMKID) {
4604 				DEVI(dip)->devi_pm_flags &= ~PMC_NOPMKID;
4605 				if (pdip && !PM_WANTS_NOTIFICATION(pdip)) {
4606 					pm_rele_power(pdip);
4607 				} else if (pdip && MDI_VHCI(pdip)) {
4608 					(void) mdi_power(pdip,
4609 					    MDI_PM_RELE_POWER,
4610 					    (void *)dip, NULL, 0);
4611 				}
4612 			}
4613 		}
4614 	}
4615 }
4616 
4617 /*
4618  * The node is the subject of a reparse pm props ioctl. Throw away the old
4619  * info and start over.
4620  */
4621 int
4622 e_new_pm_props(dev_info_t *dip)
4623 {
4624 	if (PM_GET_PM_INFO(dip) != NULL) {
4625 		pm_stop(dip);
4626 
4627 		if (e_pm_manage(dip, PM_STYLE_NEW) != DDI_SUCCESS) {
4628 			return (DDI_FAILURE);
4629 		}
4630 	}
4631 	e_pm_props(dip);
4632 	return (DDI_SUCCESS);
4633 }
4634 
4635 /*
4636  * Device has been attached, so process its pm properties
4637  */
4638 void
4639 e_pm_props(dev_info_t *dip)
4640 {
4641 	char *pp;
4642 	int len;
4643 	int flags = 0;
4644 	int propflag = DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP;
4645 
4646 	/*
4647 	 * It doesn't matter if we do this more than once, we should always
4648 	 * get the same answers, and if not, then the last one in is the
4649 	 * best one.
4650 	 */
4651 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, propflag, "pm-hardware-state",
4652 	    (caddr_t)&pp, &len) == DDI_PROP_SUCCESS) {
4653 		if (strcmp(pp, "needs-suspend-resume") == 0) {
4654 			flags = PMC_NEEDS_SR;
4655 		} else if (strcmp(pp, "no-suspend-resume") == 0) {
4656 			flags = PMC_NO_SR;
4657 		} else if (strcmp(pp, "parental-suspend-resume") == 0) {
4658 			flags = PMC_PARENTAL_SR;
4659 		} else {
4660 			cmn_err(CE_NOTE, "!device %s@%s has unrecognized "
4661 			    "%s property value '%s'", PM_NAME(dip),
4662 			    PM_ADDR(dip), "pm-hardware-state", pp);
4663 		}
4664 		kmem_free(pp, len);
4665 	}
4666 	/*
4667 	 * This next segment (PMC_WANTS_NOTIFY) is in
4668 	 * support of nexus drivers which will want to be involved in
4669 	 * (or at least notified of) their child node's power level transitions.
4670 	 * "pm-want-child-notification?" is defined by the parent.
4671 	 */
4672 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, propflag,
4673 	    "pm-want-child-notification?") && PM_HAS_BUS_POWER(dip))
4674 		flags |= PMC_WANTS_NOTIFY;
4675 	ASSERT(PM_HAS_BUS_POWER(dip) || !ddi_prop_exists(DDI_DEV_T_ANY,
4676 	    dip, propflag, "pm-want-child-notification?"));
4677 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, propflag,
4678 	    "no-involuntary-power-cycles"))
4679 		flags |= PMC_NO_INVOL;
4680 	/* devfs single threads us */
4681 	DEVI(dip)->devi_pm_flags |= flags;
4682 }
4683 
4684 /*
4685  * This is the DDI_CTLOPS_POWER handler that is used when there is no ppm
4686  * driver which has claimed a node.
4687  * Sets old_power in arg struct.
4688  */
4689 static int
4690 pm_default_ctlops(dev_info_t *dip, dev_info_t *rdip,
4691     ddi_ctl_enum_t ctlop, void *arg, void *result)
4692 {
4693 	_NOTE(ARGUNUSED(dip))
4694 	PMD_FUNC(pmf, "ctlops")
4695 	power_req_t *reqp = (power_req_t *)arg;
4696 	int retval;
4697 	dev_info_t *target_dip;
4698 	int new_level, old_level, cmpt;
4699 #ifdef DEBUG
4700 	char *format;
4701 #endif
4702 
4703 	/*
4704 	 * The interface for doing the actual power level changes is now
4705 	 * through the DDI_CTLOPS_POWER bus_ctl, so that we can plug in
4706 	 * different platform-specific power control drivers.
4707 	 *
4708 	 * This driver implements the "default" version of this interface.
4709 	 * If no ppm driver has been installed then this interface is called
4710 	 * instead.
4711 	 */
4712 	ASSERT(dip == NULL);
4713 	switch (ctlop) {
4714 	case DDI_CTLOPS_POWER:
4715 		switch (reqp->request_type) {
4716 		case PMR_PPM_SET_POWER:
4717 		{
4718 			target_dip = reqp->req.ppm_set_power_req.who;
4719 			ASSERT(target_dip == rdip);
4720 			new_level = reqp->req.ppm_set_power_req.new_level;
4721 			cmpt = reqp->req.ppm_set_power_req.cmpt;
4722 			/* pass back old power for the PM_LEVEL_UNKNOWN case */
4723 			old_level = PM_CURPOWER(target_dip, cmpt);
4724 			reqp->req.ppm_set_power_req.old_level = old_level;
4725 			retval = pm_power(target_dip, cmpt, new_level);
4726 			PMD(PMD_PPM, ("%s: PPM_SET_POWER %s@%s(%s#%d)[%d] %d->"
4727 			    "%d %s\n", pmf, PM_DEVICE(target_dip), cmpt,
4728 			    old_level, new_level, (retval == DDI_SUCCESS ?
4729 			    "chd" : "no chg")))
4730 			return (retval);
4731 		}
4732 
4733 		case PMR_PPM_PRE_DETACH:
4734 		case PMR_PPM_POST_DETACH:
4735 		case PMR_PPM_PRE_ATTACH:
4736 		case PMR_PPM_POST_ATTACH:
4737 		case PMR_PPM_PRE_PROBE:
4738 		case PMR_PPM_POST_PROBE:
4739 		case PMR_PPM_PRE_RESUME:
4740 		case PMR_PPM_INIT_CHILD:
4741 		case PMR_PPM_UNINIT_CHILD:
4742 #ifdef DEBUG
4743 			switch (reqp->request_type) {
4744 				case PMR_PPM_PRE_DETACH:
4745 					format = "%s: PMR_PPM_PRE_DETACH "
4746 					    "%s@%s(%s#%d)\n";
4747 					break;
4748 				case PMR_PPM_POST_DETACH:
4749 					format = "%s: PMR_PPM_POST_DETACH "
4750 					    "%s@%s(%s#%d) rets %d\n";
4751 					break;
4752 				case PMR_PPM_PRE_ATTACH:
4753 					format = "%s: PMR_PPM_PRE_ATTACH "
4754 					    "%s@%s(%s#%d)\n";
4755 					break;
4756 				case PMR_PPM_POST_ATTACH:
4757 					format = "%s: PMR_PPM_POST_ATTACH "
4758 					    "%s@%s(%s#%d) rets %d\n";
4759 					break;
4760 				case PMR_PPM_PRE_PROBE:
4761 					format = "%s: PMR_PPM_PRE_PROBE "
4762 					    "%s@%s(%s#%d)\n";
4763 					break;
4764 				case PMR_PPM_POST_PROBE:
4765 					format = "%s: PMR_PPM_POST_PROBE "
4766 					    "%s@%s(%s#%d) rets %d\n";
4767 					break;
4768 				case PMR_PPM_PRE_RESUME:
4769 					format = "%s: PMR_PPM_PRE_RESUME "
4770 					    "%s@%s(%s#%d) rets %d\n";
4771 					break;
4772 				case PMR_PPM_INIT_CHILD:
4773 					format = "%s: PMR_PPM_INIT_CHILD "
4774 					    "%s@%s(%s#%d)\n";
4775 					break;
4776 				case PMR_PPM_UNINIT_CHILD:
4777 					format = "%s: PMR_PPM_UNINIT_CHILD "
4778 					    "%s@%s(%s#%d)\n";
4779 					break;
4780 				default:
4781 					break;
4782 			}
4783 			PMD(PMD_PPM, (format, pmf, PM_DEVICE(rdip),
4784 			    reqp->req.ppm_config_req.result))
4785 #endif
4786 			return (DDI_SUCCESS);
4787 
4788 		case PMR_PPM_POWER_CHANGE_NOTIFY:
4789 			/*
4790 			 * Nothing for us to do
4791 			 */
4792 			ASSERT(reqp->req.ppm_notify_level_req.who == rdip);
4793 			PMD(PMD_PPM, ("%s: PMR_PPM_POWER_CHANGE_NOTIFY "
4794 			    "%s@%s(%s#%d)[%d] %d->%d\n", pmf,
4795 			    PM_DEVICE(reqp->req.ppm_notify_level_req.who),
4796 			    reqp->req.ppm_notify_level_req.cmpt,
4797 			    PM_CURPOWER(reqp->req.ppm_notify_level_req.who,
4798 			    reqp->req.ppm_notify_level_req.cmpt),
4799 			    reqp->req.ppm_notify_level_req.new_level))
4800 			return (DDI_SUCCESS);
4801 
4802 		case PMR_PPM_UNMANAGE:
4803 			PMD(PMD_PPM, ("%s: PMR_PPM_UNMANAGE %s@%s(%s#%d)\n",
4804 			    pmf, PM_DEVICE(rdip)))
4805 			return (DDI_SUCCESS);
4806 
4807 		case PMR_PPM_LOCK_POWER:
4808 			pm_lock_power_single(reqp->req.ppm_lock_power_req.who,
4809 			    reqp->req.ppm_lock_power_req.circp);
4810 			return (DDI_SUCCESS);
4811 
4812 		case PMR_PPM_UNLOCK_POWER:
4813 			pm_unlock_power_single(
4814 			    reqp->req.ppm_unlock_power_req.who,
4815 			    reqp->req.ppm_unlock_power_req.circ);
4816 			return (DDI_SUCCESS);
4817 
4818 		case PMR_PPM_TRY_LOCK_POWER:
4819 			*(int *)result = pm_try_locking_power_single(
4820 			    reqp->req.ppm_lock_power_req.who,
4821 			    reqp->req.ppm_lock_power_req.circp);
4822 			return (DDI_SUCCESS);
4823 
4824 		case PMR_PPM_POWER_LOCK_OWNER:
4825 			target_dip = reqp->req.ppm_power_lock_owner_req.who;
4826 			ASSERT(target_dip == rdip);
4827 			reqp->req.ppm_power_lock_owner_req.owner =
4828 			    DEVI(rdip)->devi_busy_thread;
4829 			return (DDI_SUCCESS);
4830 		default:
4831 			PMD(PMD_ERROR, ("%s: default!\n", pmf))
4832 			return (DDI_FAILURE);
4833 		}
4834 
4835 	default:
4836 		PMD(PMD_ERROR, ("%s: unknown\n", pmf))
4837 		return (DDI_FAILURE);
4838 	}
4839 }
4840 
4841 /*
4842  * We overload the bus_ctl ops here--perhaps we ought to have a distinct
4843  * power_ops struct for this functionality instead?
4844  * However, we only ever do this on a ppm driver.
4845  */
4846 int
4847 pm_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
4848 {
4849 	int (*fp)();
4850 
4851 	/* if no ppm handler, call the default routine */
4852 	if (d == NULL) {
4853 		return (pm_default_ctlops(d, r, op, a, v));
4854 	}
4855 	if (!d || !r)
4856 		return (DDI_FAILURE);
4857 	ASSERT(DEVI(d)->devi_ops && DEVI(d)->devi_ops->devo_bus_ops &&
4858 		DEVI(d)->devi_ops->devo_bus_ops->bus_ctl);
4859 
4860 	fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
4861 	return ((*fp)(d, r, op, a, v));
4862 }
4863 
4864 /*
4865  * Called on a node when attach completes or the driver makes its first pm
4866  * call (whichever comes first).
4867  * In the attach case, device may not be power manageable at all.
4868  * Don't need to lock the dip because we're single threaded by the devfs code
4869  */
4870 static int
4871 pm_start(dev_info_t *dip)
4872 {
4873 	PMD_FUNC(pmf, "start")
4874 	int ret;
4875 	dev_info_t *pdip = ddi_get_parent(dip);
4876 	int e_pm_manage(dev_info_t *, int);
4877 	void pm_noinvol_specd(dev_info_t *dip);
4878 
4879 	e_pm_props(dip);
4880 	pm_noinvol_specd(dip);
4881 	/*
4882 	 * If this dip has already been processed, don't mess with it
4883 	 * (but decrement the speculative count we did above, as whatever
4884 	 * code put it under pm already will have dealt with it)
4885 	 */
4886 	if (PM_GET_PM_INFO(dip)) {
4887 		PMD(PMD_KIDSUP, ("%s: pm already done for %s@%s(%s#%d)\n",
4888 		    pmf, PM_DEVICE(dip)))
4889 		return (0);
4890 	}
4891 	ret = e_pm_manage(dip, PM_STYLE_UNKNOWN);
4892 
4893 	if (PM_GET_PM_INFO(dip) == NULL) {
4894 		/*
4895 		 * keep the kidsupcount increment as is
4896 		 */
4897 		DEVI(dip)->devi_pm_flags |= PMC_NOPMKID;
4898 		if (pdip && !PM_WANTS_NOTIFICATION(pdip)) {
4899 			pm_hold_power(pdip);
4900 		} else if (pdip && MDI_VHCI(pdip)) {
4901 			(void) mdi_power(pdip, MDI_PM_HOLD_POWER,
4902 			    (void *)dip, NULL, 0);
4903 		}
4904 
4905 		PMD(PMD_KIDSUP, ("%s: pm of %s@%s(%s#%d) failed, parent "
4906 		    "left up\n", pmf, PM_DEVICE(dip)))
4907 	}
4908 
4909 	return (ret);
4910 }
4911 
4912 /*
4913  * Keep a list of recorded thresholds.  For now we just keep a list and
4914  * search it linearly.  We don't expect too many entries.  Can always hash it
4915  * later if we need to.
4916  */
4917 void
4918 pm_record_thresh(pm_thresh_rec_t *rp)
4919 {
4920 	pm_thresh_rec_t *pptr, *ptr;
4921 
4922 	ASSERT(*rp->ptr_physpath);
4923 	rw_enter(&pm_thresh_rwlock, RW_WRITER);
4924 	for (pptr = NULL, ptr = pm_thresh_head;
4925 	    ptr; pptr = ptr,  ptr = ptr->ptr_next) {
4926 		if (strcmp(rp->ptr_physpath, ptr->ptr_physpath) == 0) {
4927 			/* replace this one */
4928 			rp->ptr_next = ptr->ptr_next;
4929 			if (pptr) {
4930 				pptr->ptr_next = rp;
4931 			} else {
4932 				pm_thresh_head = rp;
4933 			}
4934 			rw_exit(&pm_thresh_rwlock);
4935 			kmem_free(ptr, ptr->ptr_size);
4936 			return;
4937 		}
4938 		continue;
4939 	}
4940 	/*
4941 	 * There was not a match in the list, insert this one in front
4942 	 */
4943 	if (pm_thresh_head) {
4944 		rp->ptr_next = pm_thresh_head;
4945 		pm_thresh_head = rp;
4946 	} else {
4947 		rp->ptr_next = NULL;
4948 		pm_thresh_head = rp;
4949 	}
4950 	rw_exit(&pm_thresh_rwlock);
4951 }
4952 
4953 /*
4954  * Create a new dependency record and hang a new dependency entry off of it
4955  */
4956 pm_pdr_t *
4957 newpdr(char *kept, char *keeps, int isprop)
4958 {
4959 	size_t size = strlen(kept) + strlen(keeps) + 2 + sizeof (pm_pdr_t);
4960 	pm_pdr_t *p = kmem_zalloc(size, KM_SLEEP);
4961 	p->pdr_size = size;
4962 	p->pdr_isprop = isprop;
4963 	p->pdr_kept_paths = NULL;
4964 	p->pdr_kept_count = 0;
4965 	p->pdr_kept = (char *)((intptr_t)p + sizeof (pm_pdr_t));
4966 	(void) strcpy(p->pdr_kept, kept);
4967 	p->pdr_keeper = (char *)((intptr_t)p->pdr_kept + strlen(kept) + 1);
4968 	(void) strcpy(p->pdr_keeper, keeps);
4969 	ASSERT((intptr_t)p->pdr_keeper + strlen(p->pdr_keeper) + 1 <=
4970 	    (intptr_t)p + size);
4971 	ASSERT((intptr_t)p->pdr_kept + strlen(p->pdr_kept) + 1 <=
4972 	    (intptr_t)p + size);
4973 	return (p);
4974 }
4975 
4976 /*
4977  * Keep a list of recorded dependencies.  We only keep the
4978  * keeper -> kept list for simplification. At this point We do not
4979  * care about whether the devices are attached or not yet,
4980  * this would be done in pm_keeper() and pm_kept().
4981  * If a PM_RESET_PM happens, then we tear down and forget the dependencies,
4982  * and it is up to the user to issue the ioctl again if they want it
4983  * (e.g. pmconfig)
4984  * Returns true if dependency already exists in the list.
4985  */
4986 int
4987 pm_record_keeper(char *kept, char *keeper, int isprop)
4988 {
4989 	PMD_FUNC(pmf, "record_keeper")
4990 	pm_pdr_t *npdr, *ppdr, *pdr;
4991 
4992 	PMD(PMD_KEEPS, ("%s: %s, %s\n", pmf, kept, keeper))
4993 	ASSERT(kept && keeper);
4994 #ifdef DEBUG
4995 	if (pm_debug & PMD_KEEPS)
4996 		prdeps("pm_record_keeper entry");
4997 #endif
4998 	for (ppdr = NULL, pdr = pm_dep_head; pdr;
4999 	    ppdr = pdr, pdr = pdr->pdr_next) {
5000 		PMD(PMD_KEEPS, ("%s: check %s, %s\n", pmf, pdr->pdr_kept,
5001 		    pdr->pdr_keeper))
5002 		if (strcmp(kept, pdr->pdr_kept) == 0 &&
5003 		    strcmp(keeper, pdr->pdr_keeper) == 0) {
5004 			PMD(PMD_KEEPS, ("%s: match\n", pmf))
5005 			return (1);
5006 		}
5007 	}
5008 	/*
5009 	 * We did not find any match, so we have to make an entry
5010 	 */
5011 	npdr = newpdr(kept, keeper, isprop);
5012 	if (ppdr) {
5013 		ASSERT(ppdr->pdr_next == NULL);
5014 		ppdr->pdr_next = npdr;
5015 	} else {
5016 		ASSERT(pm_dep_head == NULL);
5017 		pm_dep_head = npdr;
5018 	}
5019 #ifdef DEBUG
5020 	if (pm_debug & PMD_KEEPS)
5021 		prdeps("pm_record_keeper after new record");
5022 #endif
5023 	if (!isprop)
5024 		pm_unresolved_deps++;
5025 	else
5026 		pm_prop_deps++;
5027 	return (0);
5028 }
5029 
5030 /*
5031  * Look up this device in the set of devices we've seen ioctls for
5032  * to see if we are holding a threshold spec for it.  If so, make it so.
5033  * At ioctl time, we were given the physical path of the device.
5034  */
5035 int
5036 pm_thresh_specd(dev_info_t *dip)
5037 {
5038 	void pm_apply_recorded_thresh(dev_info_t *, pm_thresh_rec_t *);
5039 	char *path = 0;
5040 	char pathbuf[MAXNAMELEN];
5041 	pm_thresh_rec_t *rp;
5042 
5043 	path = ddi_pathname(dip, pathbuf);
5044 
5045 	rw_enter(&pm_thresh_rwlock, RW_READER);
5046 	for (rp = pm_thresh_head; rp; rp = rp->ptr_next) {
5047 		if (strcmp(rp->ptr_physpath, path) != 0)
5048 			continue;
5049 		pm_apply_recorded_thresh(dip, rp);
5050 		rw_exit(&pm_thresh_rwlock);
5051 		return (1);
5052 	}
5053 	rw_exit(&pm_thresh_rwlock);
5054 	return (0);
5055 }
5056 
5057 static int
5058 pm_set_keeping(dev_info_t *keeper, dev_info_t *kept)
5059 {
5060 	PMD_FUNC(pmf, "set_keeping")
5061 	pm_info_t *kept_info;
5062 	int j, up = 0, circ;
5063 	void prdeps(char *);
5064 
5065 	PMD(PMD_KEEPS, ("%s: keeper=%s@%s(%s#%d), kept=%s@%s(%s#%d)\n", pmf,
5066 	    PM_DEVICE(keeper), PM_DEVICE(kept)))
5067 #ifdef DEBUG
5068 	if (pm_debug & PMD_KEEPS)
5069 		prdeps("Before PAD\n");
5070 #endif
5071 	ASSERT(keeper != kept);
5072 	if (PM_GET_PM_INFO(keeper) == NULL) {
5073 		cmn_err(CE_CONT, "!device %s@%s(%s#%d) keeps up device "
5074 		    "%s@%s(%s#%d), but the latter is not power managed",
5075 		    PM_DEVICE(keeper), PM_DEVICE(kept));
5076 		PMD((PMD_FAIL | PMD_KEEPS), ("%s: keeper %s@%s(%s#%d) is not"
5077 		    "power managed\n", pmf, PM_DEVICE(keeper)))
5078 		return (0);
5079 	}
5080 	kept_info = PM_GET_PM_INFO(kept);
5081 	ASSERT(kept_info);
5082 	PM_LOCK_POWER(keeper, &circ);
5083 	for (j = 0; j < PM_NUMCMPTS(keeper); j++) {
5084 		if (PM_CURPOWER(keeper, j)) {
5085 			up++;
5086 			break;
5087 		}
5088 	}
5089 	if (up) {
5090 		/* Bringup and maintain a hold on the kept */
5091 		PMD(PMD_KEEPS, ("%s: place a hold on kept %s@%s(%s#%d)\n", pmf,
5092 		    PM_DEVICE(kept)))
5093 		bring_pmdep_up(kept, 1);
5094 	}
5095 	PM_UNLOCK_POWER(keeper, circ);
5096 #ifdef DEBUG
5097 	if (pm_debug & PMD_KEEPS)
5098 		prdeps("After PAD\n");
5099 #endif
5100 	return (1);
5101 }
5102 
5103 /*
5104  * Should this device keep up another device?
5105  * Look up this device in the set of devices we've seen ioctls for
5106  * to see if we are holding a dependency spec for it.  If so, make it so.
5107  * Because we require the kept device to be attached already in order to
5108  * make the list entry (and hold it), we only need to look for keepers.
5109  * At ioctl time, we were given the physical path of the device.
5110  */
5111 int
5112 pm_keeper(char *keeper)
5113 {
5114 	PMD_FUNC(pmf, "keeper")
5115 	int pm_apply_recorded_dep(dev_info_t *, pm_pdr_t *);
5116 	dev_info_t *dip;
5117 	pm_pdr_t *dp;
5118 	dev_info_t *kept = NULL;
5119 	int ret = 0;
5120 	int i;
5121 
5122 	if (!pm_unresolved_deps && !pm_prop_deps)
5123 		return (0);
5124 	ASSERT(keeper != NULL);
5125 	dip = pm_name_to_dip(keeper, 1);
5126 	if (dip == NULL)
5127 		return (0);
5128 	PMD(PMD_KEEPS, ("%s: keeper=%s\n", pmf, keeper))
5129 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
5130 		if (!dp->pdr_isprop) {
5131 			if (!pm_unresolved_deps)
5132 				continue;
5133 			PMD(PMD_KEEPS, ("%s: keeper %s\n", pmf, dp->pdr_keeper))
5134 			if (dp->pdr_satisfied) {
5135 				PMD(PMD_KEEPS, ("%s: satisfied\n", pmf))
5136 				continue;
5137 			}
5138 			if (strcmp(dp->pdr_keeper, keeper) == 0) {
5139 				ret += pm_apply_recorded_dep(dip, dp);
5140 			}
5141 		} else {
5142 			if (strcmp(dp->pdr_keeper, keeper) != 0)
5143 				continue;
5144 			for (i = 0; i < dp->pdr_kept_count; i++) {
5145 				if (dp->pdr_kept_paths[i] == NULL)
5146 					continue;
5147 				kept = pm_name_to_dip(dp->pdr_kept_paths[i], 1);
5148 				if (kept == NULL)
5149 					continue;
5150 				ASSERT(ddi_prop_exists(DDI_DEV_T_ANY, kept,
5151 				    DDI_PROP_DONTPASS, dp->pdr_kept));
5152 				PMD(PMD_KEEPS, ("%s: keeper=%s@%s(%s#%d), "
5153 				    "kept=%s@%s(%s#%d) keptcnt=%d\n",
5154 				    pmf, PM_DEVICE(dip), PM_DEVICE(kept),
5155 				    dp->pdr_kept_count))
5156 				if (kept != dip) {
5157 					ret += pm_set_keeping(dip, kept);
5158 				}
5159 				ddi_release_devi(kept);
5160 			}
5161 
5162 		}
5163 	}
5164 	ddi_release_devi(dip);
5165 	return (ret);
5166 }
5167 
5168 /*
5169  * Should this device be kept up by another device?
5170  * Look up all dependency recorded from PM_ADD_DEPENDENT and
5171  * PM_ADD_DEPENDENT_PROPERTY ioctls. Record down on the keeper's
5172  * kept device lists.
5173  */
5174 static int
5175 pm_kept(char *keptp)
5176 {
5177 	PMD_FUNC(pmf, "kept")
5178 	pm_pdr_t *dp;
5179 	int found = 0;
5180 	int ret = 0;
5181 	dev_info_t *keeper;
5182 	dev_info_t *kept;
5183 	size_t length;
5184 	int i;
5185 	char **paths;
5186 	char *path;
5187 
5188 	ASSERT(keptp != NULL);
5189 	kept = pm_name_to_dip(keptp, 1);
5190 	if (kept == NULL)
5191 		return (0);
5192 	PMD(PMD_KEEPS, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(kept)))
5193 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
5194 		if (dp->pdr_isprop) {
5195 			PMD(PMD_KEEPS, ("%s: property %s\n", pmf, dp->pdr_kept))
5196 			if (ddi_prop_exists(DDI_DEV_T_ANY, kept,
5197 			    DDI_PROP_DONTPASS, dp->pdr_kept)) {
5198 				/*
5199 				 * Dont allow self dependency.
5200 				 */
5201 				if (strcmp(dp->pdr_keeper, keptp) == 0)
5202 					continue;
5203 				keeper = pm_name_to_dip(dp->pdr_keeper, 1);
5204 				if (keeper == NULL)
5205 					continue;
5206 				PMD(PMD_KEEPS, ("%s: adding to kepts path list "
5207 				    "%p\n", pmf, (void *)kept))
5208 #ifdef DEBUG
5209 				if (pm_debug & PMD_DEP)
5210 					prdeps("Before Adding from pm_kept\n");
5211 #endif
5212 				/*
5213 				 * Add ourselves to the dip list.
5214 				 */
5215 				if (dp->pdr_kept_count == 0) {
5216 					length = strlen(keptp) + 1;
5217 					path =
5218 					    kmem_alloc(length, KM_SLEEP);
5219 					paths = kmem_alloc(sizeof (char **),
5220 						    KM_SLEEP);
5221 					(void) strcpy(path, keptp);
5222 					paths[0] = path;
5223 					dp->pdr_kept_paths = paths;
5224 					dp->pdr_kept_count++;
5225 				} else {
5226 					/* Check to see if already on list */
5227 					for (i = 0; i < dp->pdr_kept_count;
5228 					    i++) {
5229 						if (strcmp(keptp,
5230 						    dp->pdr_kept_paths[i])
5231 						    == 0) {
5232 							found++;
5233 							break;
5234 						}
5235 					}
5236 					if (found) {
5237 						ddi_release_devi(keeper);
5238 						continue;
5239 					}
5240 					length = dp->pdr_kept_count *
5241 					    sizeof (char **);
5242 					paths = kmem_alloc(
5243 					    length + sizeof (char **),
5244 					    KM_SLEEP);
5245 					if (dp->pdr_kept_count) {
5246 						bcopy(dp->pdr_kept_paths,
5247 						    paths, length);
5248 						kmem_free(dp->pdr_kept_paths,
5249 							length);
5250 					}
5251 					dp->pdr_kept_paths = paths;
5252 					length = strlen(keptp) + 1;
5253 					path =
5254 					    kmem_alloc(length, KM_SLEEP);
5255 					(void) strcpy(path, keptp);
5256 					dp->pdr_kept_paths[i] = path;
5257 					dp->pdr_kept_count++;
5258 				}
5259 #ifdef DEBUG
5260 				if (pm_debug & PMD_DEP)
5261 					prdeps("After from pm_kept\n");
5262 #endif
5263 				if (keeper) {
5264 					ret += pm_set_keeping(keeper, kept);
5265 					ddi_release_devi(keeper);
5266 				}
5267 			}
5268 		} else {
5269 			/*
5270 			 * pm_keeper would be called later to do
5271 			 * the actual pm_set_keeping.
5272 			 */
5273 			PMD(PMD_KEEPS, ("%s: adding to kepts path list %p\n",
5274 			    pmf, (void *)kept))
5275 #ifdef DEBUG
5276 			if (pm_debug & PMD_DEP)
5277 				prdeps("Before Adding from pm_kept\n");
5278 #endif
5279 			if (strcmp(keptp, dp->pdr_kept) == 0) {
5280 				if (dp->pdr_kept_paths == NULL) {
5281 					length = strlen(keptp) + 1;
5282 					path =
5283 					    kmem_alloc(length, KM_SLEEP);
5284 					paths = kmem_alloc(sizeof (char **),
5285 						KM_SLEEP);
5286 					(void) strcpy(path, keptp);
5287 					paths[0] = path;
5288 					dp->pdr_kept_paths = paths;
5289 					dp->pdr_kept_count++;
5290 				}
5291 			}
5292 #ifdef DEBUG
5293 			if (pm_debug & PMD_DEP)
5294 			    prdeps("After from pm_kept\n");
5295 #endif
5296 		}
5297 	}
5298 	ddi_release_devi(kept);
5299 	return (ret);
5300 }
5301 
5302 /*
5303  * Apply a recorded dependency.  dp specifies the dependency, and
5304  * keeper is already known to be the device that keeps up the other (kept) one.
5305  * We have to the whole tree for the "kept" device, then apply
5306  * the dependency (which may already be applied).
5307  */
5308 int
5309 pm_apply_recorded_dep(dev_info_t *keeper, pm_pdr_t *dp)
5310 {
5311 	PMD_FUNC(pmf, "apply_recorded_dep")
5312 	dev_info_t *kept = NULL;
5313 	int ret = 0;
5314 	char *keptp = NULL;
5315 
5316 	/*
5317 	 * Device to Device dependency can only be 1 to 1.
5318 	 */
5319 	if (dp->pdr_kept_paths == NULL)
5320 		return (0);
5321 	keptp = dp->pdr_kept_paths[0];
5322 	if (keptp == NULL)
5323 		return (0);
5324 	ASSERT(*keptp != '\0');
5325 	kept = pm_name_to_dip(keptp, 1);
5326 	if (kept == NULL)
5327 		return (0);
5328 	if (kept) {
5329 		PMD(PMD_KEEPS, ("%s: keeper=%s, kept=%s\n", pmf,
5330 		    dp->pdr_keeper, keptp))
5331 		if (pm_set_keeping(keeper, kept)) {
5332 			ASSERT(dp->pdr_satisfied == 0);
5333 			dp->pdr_satisfied = 1;
5334 			ASSERT(pm_unresolved_deps);
5335 			pm_unresolved_deps--;
5336 			ret++;
5337 		}
5338 	}
5339 	ddi_release_devi(kept);
5340 
5341 	return (ret);
5342 }
5343 
5344 /*
5345  * Called from common/io/pm.c
5346  */
5347 int
5348 pm_cur_power(pm_component_t *cp)
5349 {
5350 	return (cur_power(cp));
5351 }
5352 
5353 /*
5354  * External interface to sanity-check a power level.
5355  */
5356 int
5357 pm_valid_power(dev_info_t *dip, int comp, int level)
5358 {
5359 	PMD_FUNC(pmf, "valid_power")
5360 
5361 	if (comp >= 0 && comp < PM_NUMCMPTS(dip) && level >= 0)
5362 		return (e_pm_valid_power(dip, comp, level));
5363 	else {
5364 		PMD(PMD_FAIL, ("%s: comp=%d, ncomp=%d, level=%d\n",
5365 		    pmf, comp, PM_NUMCMPTS(dip), level))
5366 		return (0);
5367 	}
5368 }
5369 
5370 /*
5371  * Called when a device that is direct power managed needs to change state.
5372  * This routine arranges to block the request until the process managing
5373  * the device makes the change (or some other incompatible change) or
5374  * the process closes /dev/pm.
5375  */
5376 static int
5377 pm_block(dev_info_t *dip, int comp, int newpower, int oldpower)
5378 {
5379 	pm_rsvp_t *new = kmem_zalloc(sizeof (*new), KM_SLEEP);
5380 	int ret = 0;
5381 	void pm_dequeue_blocked(pm_rsvp_t *);
5382 	void pm_enqueue_blocked(pm_rsvp_t *);
5383 
5384 	ASSERT(!pm_processes_stopped);
5385 	ASSERT(PM_IAM_LOCKING_DIP(dip));
5386 	new->pr_dip = dip;
5387 	new->pr_comp = comp;
5388 	new->pr_newlevel = newpower;
5389 	new->pr_oldlevel = oldpower;
5390 	cv_init(&new->pr_cv, NULL, CV_DEFAULT, NULL);
5391 	mutex_enter(&pm_rsvp_lock);
5392 	pm_enqueue_blocked(new);
5393 	pm_enqueue_notify(PSC_PENDING_CHANGE, dip, comp, newpower, oldpower,
5394 	    PM_CANBLOCK_BLOCK);
5395 	PM_UNLOCK_DIP(dip);
5396 	/*
5397 	 * truss may make the cv_wait_sig return prematurely
5398 	 */
5399 	while (ret == 0) {
5400 		/*
5401 		 * Normally there will be no user context involved, but if
5402 		 * there is (e.g. we are here via an ioctl call to a driver)
5403 		 * then we should allow the process to abort the request,
5404 		 * or we get an unkillable process if the same thread does
5405 		 * PM_DIRECT_PM and pm_raise_power
5406 		 */
5407 		if (cv_wait_sig(&new->pr_cv, &pm_rsvp_lock) == 0) {
5408 			ret = PMP_FAIL;
5409 		} else {
5410 			ret = new->pr_retval;
5411 		}
5412 	}
5413 	pm_dequeue_blocked(new);
5414 	mutex_exit(&pm_rsvp_lock);
5415 	cv_destroy(&new->pr_cv);
5416 	kmem_free(new, sizeof (*new));
5417 	return (ret);
5418 }
5419 
5420 /*
5421  * Returns true if the process is interested in power level changes (has issued
5422  * PM_GET_STATE_CHANGE ioctl).
5423  */
5424 int
5425 pm_interest_registered(int clone)
5426 {
5427 	ASSERT(clone >= 0 && clone < PM_MAX_CLONE - 1);
5428 	return (pm_interest[clone]);
5429 }
5430 
5431 /*
5432  * Process with clone has just done PM_DIRECT_PM on dip, or has asked to
5433  * watch all state transitions (dip == NULL).  Set up data
5434  * structs to communicate with process about state changes.
5435  */
5436 void
5437 pm_register_watcher(int clone, dev_info_t *dip)
5438 {
5439 	pscc_t	*p;
5440 	psce_t	*psce;
5441 	static void pm_enqueue_pscc(pscc_t *, pscc_t **);
5442 
5443 	/*
5444 	 * We definitely need a control struct, then we have to search to see
5445 	 * there is already an entries struct (in the dip != NULL case).
5446 	 */
5447 	pscc_t	*pscc = kmem_zalloc(sizeof (*pscc), KM_SLEEP);
5448 	pscc->pscc_clone = clone;
5449 	pscc->pscc_dip = dip;
5450 
5451 	if (dip) {
5452 		int found = 0;
5453 		rw_enter(&pm_pscc_direct_rwlock, RW_WRITER);
5454 		for (p = pm_pscc_direct; p; p = p->pscc_next) {
5455 			/*
5456 			 * Already an entry for this clone, so just use it
5457 			 * for the new one (for the case where a single
5458 			 * process is watching multiple devices)
5459 			 */
5460 			if (p->pscc_clone == clone) {
5461 				ASSERT(p->pscc_dip != dip);
5462 				pscc->pscc_entries = p->pscc_entries;
5463 				pscc->pscc_entries->psce_references++;
5464 				found++;
5465 			}
5466 		}
5467 		if (!found) {		/* create a new one */
5468 			psce = kmem_zalloc(sizeof (psce_t), KM_SLEEP);
5469 			mutex_init(&psce->psce_lock, NULL, MUTEX_DEFAULT, NULL);
5470 			psce->psce_first =
5471 			    kmem_zalloc(sizeof (pm_state_change_t) * PSCCOUNT,
5472 			    KM_SLEEP);
5473 			psce->psce_in = psce->psce_out = psce->psce_first;
5474 			psce->psce_last = &psce->psce_first[PSCCOUNT - 1];
5475 			psce->psce_references = 1;
5476 			pscc->pscc_entries = psce;
5477 		}
5478 		pm_enqueue_pscc(pscc, &pm_pscc_direct);
5479 		rw_exit(&pm_pscc_direct_rwlock);
5480 	} else {
5481 		ASSERT(!pm_interest_registered(clone));
5482 		rw_enter(&pm_pscc_interest_rwlock, RW_WRITER);
5483 #ifdef DEBUG
5484 		for (p = pm_pscc_interest; p; p = p->pscc_next) {
5485 			/*
5486 			 * Should not be an entry for this clone!
5487 			 */
5488 			ASSERT(p->pscc_clone != clone);
5489 		}
5490 #endif
5491 		psce = kmem_zalloc(sizeof (psce_t), KM_SLEEP);
5492 		psce->psce_first = kmem_zalloc(sizeof (pm_state_change_t) *
5493 		    PSCCOUNT, KM_SLEEP);
5494 		psce->psce_in = psce->psce_out = psce->psce_first;
5495 		psce->psce_last = &psce->psce_first[PSCCOUNT - 1];
5496 		psce->psce_references = 1;
5497 		pscc->pscc_entries = psce;
5498 		pm_enqueue_pscc(pscc, &pm_pscc_interest);
5499 		pm_interest[clone] = 1;
5500 		rw_exit(&pm_pscc_interest_rwlock);
5501 	}
5502 }
5503 
5504 /*
5505  * Remove the given entry from the blocked list
5506  */
5507 void
5508 pm_dequeue_blocked(pm_rsvp_t *p)
5509 {
5510 	ASSERT(MUTEX_HELD(&pm_rsvp_lock));
5511 	if (pm_blocked_list == p) {
5512 		ASSERT(p->pr_prev == NULL);
5513 		if (p->pr_next != NULL)
5514 			p->pr_next->pr_prev = NULL;
5515 		pm_blocked_list = p->pr_next;
5516 	} else {
5517 		ASSERT(p->pr_prev != NULL);
5518 		p->pr_prev->pr_next = p->pr_next;
5519 		if (p->pr_next != NULL)
5520 			p->pr_next->pr_prev = p->pr_prev;
5521 	}
5522 }
5523 
5524 /*
5525  * Remove the given control struct from the given list
5526  */
5527 static void
5528 pm_dequeue_pscc(pscc_t *p, pscc_t **list)
5529 {
5530 	if (*list == p) {
5531 		ASSERT(p->pscc_prev == NULL);
5532 		if (p->pscc_next != NULL)
5533 			p->pscc_next->pscc_prev = NULL;
5534 		*list = p->pscc_next;
5535 	} else {
5536 		ASSERT(p->pscc_prev != NULL);
5537 		p->pscc_prev->pscc_next = p->pscc_next;
5538 		if (p->pscc_next != NULL)
5539 			p->pscc_next->pscc_prev = p->pscc_prev;
5540 	}
5541 }
5542 
5543 /*
5544  * Stick the control struct specified on the front of the list
5545  */
5546 static void
5547 pm_enqueue_pscc(pscc_t *p, pscc_t **list)
5548 {
5549 	pscc_t *h;	/* entry at head of list */
5550 	if ((h = *list) == NULL) {
5551 		*list = p;
5552 		ASSERT(p->pscc_next == NULL);
5553 		ASSERT(p->pscc_prev == NULL);
5554 	} else {
5555 		p->pscc_next = h;
5556 		ASSERT(h->pscc_prev == NULL);
5557 		h->pscc_prev = p;
5558 		ASSERT(p->pscc_prev == NULL);
5559 		*list = p;
5560 	}
5561 }
5562 
5563 /*
5564  * If dip is NULL, process is closing "clone" clean up all its registrations.
5565  * Otherwise only clean up those for dip because process is just giving up
5566  * control of a direct device.
5567  */
5568 void
5569 pm_deregister_watcher(int clone, dev_info_t *dip)
5570 {
5571 	pscc_t	*p, *pn;
5572 	psce_t	*psce;
5573 	int found = 0;
5574 
5575 	if (dip == NULL) {
5576 		rw_enter(&pm_pscc_interest_rwlock, RW_WRITER);
5577 		for (p = pm_pscc_interest; p; p = pn) {
5578 			pn = p->pscc_next;
5579 			if (p->pscc_clone == clone) {
5580 				pm_dequeue_pscc(p, &pm_pscc_interest);
5581 				psce = p->pscc_entries;
5582 				ASSERT(psce->psce_references == 1);
5583 				mutex_destroy(&psce->psce_lock);
5584 				kmem_free(psce->psce_first,
5585 				    sizeof (pm_state_change_t) * PSCCOUNT);
5586 				kmem_free(psce, sizeof (*psce));
5587 				kmem_free(p, sizeof (*p));
5588 			}
5589 		}
5590 		pm_interest[clone] = 0;
5591 		rw_exit(&pm_pscc_interest_rwlock);
5592 	}
5593 	found = 0;
5594 	rw_enter(&pm_pscc_direct_rwlock, RW_WRITER);
5595 	for (p = pm_pscc_direct; p; p = pn) {
5596 		pn = p->pscc_next;
5597 		if ((dip && p->pscc_dip == dip) ||
5598 		    (dip == NULL && clone == p->pscc_clone)) {
5599 			ASSERT(clone == p->pscc_clone);
5600 			found++;
5601 			/*
5602 			 * Remove from control list
5603 			 */
5604 			pm_dequeue_pscc(p, &pm_pscc_direct);
5605 			/*
5606 			 * If we're the last reference, free the
5607 			 * entries struct.
5608 			 */
5609 			psce = p->pscc_entries;
5610 			ASSERT(psce);
5611 			if (psce->psce_references == 1) {
5612 				kmem_free(psce->psce_first,
5613 				    PSCCOUNT * sizeof (pm_state_change_t));
5614 				kmem_free(psce, sizeof (*psce));
5615 			} else {
5616 				psce->psce_references--;
5617 			}
5618 			kmem_free(p, sizeof (*p));
5619 		}
5620 	}
5621 	ASSERT(dip == NULL || found);
5622 	rw_exit(&pm_pscc_direct_rwlock);
5623 }
5624 
5625 /*
5626  * Search the indicated list for an entry that matches clone, and return a
5627  * pointer to it.  To be interesting, the entry must have something ready to
5628  * be passed up to the controlling process.
5629  * The returned entry will be locked upon return from this call.
5630  */
5631 static psce_t *
5632 pm_psc_find_clone(int clone, pscc_t **list, krwlock_t *lock)
5633 {
5634 	pscc_t	*p;
5635 	psce_t	*psce;
5636 	rw_enter(lock, RW_READER);
5637 	for (p = *list; p; p = p->pscc_next) {
5638 		if (clone == p->pscc_clone) {
5639 			psce = p->pscc_entries;
5640 			mutex_enter(&psce->psce_lock);
5641 			if (psce->psce_out->size) {
5642 				rw_exit(lock);
5643 				return (psce);
5644 			} else {
5645 				mutex_exit(&psce->psce_lock);
5646 			}
5647 		}
5648 	}
5649 	rw_exit(lock);
5650 	return (NULL);
5651 }
5652 
5653 /*
5654  * Find an entry for a particular clone in the direct list.
5655  */
5656 psce_t *
5657 pm_psc_clone_to_direct(int clone)
5658 {
5659 	static psce_t *pm_psc_find_clone(int, pscc_t **, krwlock_t *);
5660 	return (pm_psc_find_clone(clone, &pm_pscc_direct,
5661 	    &pm_pscc_direct_rwlock));
5662 }
5663 
5664 /*
5665  * Find an entry for a particular clone in the interest list.
5666  */
5667 psce_t *
5668 pm_psc_clone_to_interest(int clone)
5669 {
5670 	static psce_t *pm_psc_find_clone(int, pscc_t **, krwlock_t *);
5671 	return (pm_psc_find_clone(clone, &pm_pscc_interest,
5672 	    &pm_pscc_interest_rwlock));
5673 }
5674 
5675 /*
5676  * Put the given entry at the head of the blocked list
5677  */
5678 void
5679 pm_enqueue_blocked(pm_rsvp_t *p)
5680 {
5681 	ASSERT(MUTEX_HELD(&pm_rsvp_lock));
5682 	ASSERT(p->pr_next == NULL);
5683 	ASSERT(p->pr_prev == NULL);
5684 	if (pm_blocked_list != NULL) {
5685 		p->pr_next = pm_blocked_list;
5686 		ASSERT(pm_blocked_list->pr_prev == NULL);
5687 		pm_blocked_list->pr_prev = p;
5688 		pm_blocked_list = p;
5689 	} else {
5690 		pm_blocked_list = p;
5691 	}
5692 }
5693 
5694 /*
5695  * Sets every power managed device back to its default threshold
5696  */
5697 void
5698 pm_all_to_default_thresholds(void)
5699 {
5700 	ddi_walk_devs(ddi_root_node(), pm_set_dev_thr_walk,
5701 	    (void *) &pm_system_idle_threshold);
5702 }
5703 
5704 static int
5705 pm_set_dev_thr_walk(dev_info_t *dip, void *arg)
5706 {
5707 	int thr = (int)(*(int *)arg);
5708 
5709 	if (!PM_GET_PM_INFO(dip))
5710 		return (DDI_WALK_CONTINUE);
5711 	pm_set_device_threshold(dip, thr, PMC_DEF_THRESH);
5712 	return (DDI_WALK_CONTINUE);
5713 }
5714 
5715 /*
5716  * Returns the current threshold value (in seconds) for the indicated component
5717  */
5718 int
5719 pm_current_threshold(dev_info_t *dip, int comp, int *threshp)
5720 {
5721 	if (comp < 0 || comp >= PM_NUMCMPTS(dip)) {
5722 		return (DDI_FAILURE);
5723 	} else {
5724 		*threshp = cur_threshold(dip, comp);
5725 		return (DDI_SUCCESS);
5726 	}
5727 }
5728 
5729 /*
5730  * To be called when changing the power level of a component of a device.
5731  * On some platforms, changing power on one device may require that power
5732  * be changed on other, related devices in the same transaction.  Thus, we
5733  * always pass this request to the platform power manager so that all the
5734  * affected devices will be locked.
5735  */
5736 void
5737 pm_lock_power(dev_info_t *dip, int *circp)
5738 {
5739 	power_req_t power_req;
5740 	int result;
5741 
5742 	power_req.request_type = PMR_PPM_LOCK_POWER;
5743 	power_req.req.ppm_lock_power_req.who = dip;
5744 	power_req.req.ppm_lock_power_req.circp = circp;
5745 	(void) pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER, &power_req, &result);
5746 }
5747 
5748 /*
5749  * Release the lock (or locks) acquired to change the power of a device.
5750  * See comments for pm_lock_power.
5751  */
5752 void
5753 pm_unlock_power(dev_info_t *dip, int circ)
5754 {
5755 	power_req_t power_req;
5756 	int result;
5757 
5758 	power_req.request_type = PMR_PPM_UNLOCK_POWER;
5759 	power_req.req.ppm_unlock_power_req.who = dip;
5760 	power_req.req.ppm_unlock_power_req.circ = circ;
5761 	(void) pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER, &power_req, &result);
5762 }
5763 
5764 
5765 /*
5766  * Attempt (without blocking) to acquire the lock(s) needed to change the
5767  * power of a component of a device.  See comments for pm_lock_power.
5768  *
5769  * Return: 1 if lock(s) acquired, 0 if not.
5770  */
5771 int
5772 pm_try_locking_power(dev_info_t *dip, int *circp)
5773 {
5774 	power_req_t power_req;
5775 	int result;
5776 
5777 	power_req.request_type = PMR_PPM_TRY_LOCK_POWER;
5778 	power_req.req.ppm_lock_power_req.who = dip;
5779 	power_req.req.ppm_lock_power_req.circp = circp;
5780 	(void) pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER, &power_req, &result);
5781 	return (result);
5782 }
5783 
5784 
5785 /*
5786  * Lock power state of a device.
5787  *
5788  * The implementation handles a special case where another thread may have
5789  * acquired the lock and created/launched this thread to do the work.  If
5790  * the lock cannot be acquired immediately, we check to see if this thread
5791  * is registered as a borrower of the lock.  If so, we may proceed without
5792  * the lock.  This assumes that the lending thread blocks on the completion
5793  * of this thread.
5794  *
5795  * Note 1: for use by ppm only.
5796  *
5797  * Note 2: On failing to get the lock immediately, we search lock_loan list
5798  * for curthread (as borrower of the lock).  On a hit, we check that the
5799  * lending thread already owns the lock we want.  It is safe to compare
5800  * devi_busy_thread and thread id of the lender because in the == case (the
5801  * only one we care about) we know that the owner is blocked.  Similarly,
5802  * If we find that curthread isn't registered as a lock borrower, it is safe
5803  * to use the blocking call (ndi_devi_enter) because we know that if we
5804  * weren't already listed as a borrower (upstream on the call stack) we won't
5805  * become one.
5806  */
5807 void
5808 pm_lock_power_single(dev_info_t *dip, int *circp)
5809 {
5810 	lock_loan_t *cur;
5811 
5812 	/* if the lock is available, we are done. */
5813 	if (ndi_devi_tryenter(dip, circp))
5814 		return;
5815 
5816 	mutex_enter(&pm_loan_lock);
5817 	/* see if our thread is registered as a lock borrower. */
5818 	for (cur = lock_loan_head.pmlk_next; cur; cur = cur->pmlk_next)
5819 		if (cur->pmlk_borrower == curthread)
5820 			break;
5821 	mutex_exit(&pm_loan_lock);
5822 
5823 	/* if this thread not already registered, it is safe to block */
5824 	if (cur == NULL)
5825 		ndi_devi_enter(dip, circp);
5826 	else {
5827 		/* registered: does lender own the lock we want? */
5828 		if (cur->pmlk_lender == DEVI(dip)->devi_busy_thread) {
5829 			ASSERT(cur->pmlk_dip == NULL || cur->pmlk_dip == dip);
5830 			cur->pmlk_dip = dip;
5831 		} else /* no: just block for it */
5832 			ndi_devi_enter(dip, circp);
5833 
5834 	}
5835 }
5836 
5837 /*
5838  * Drop the lock on the device's power state.  See comment for
5839  * pm_lock_power_single() for special implementation considerations.
5840  *
5841  * Note: for use by ppm only.
5842  */
5843 void
5844 pm_unlock_power_single(dev_info_t *dip, int circ)
5845 {
5846 	lock_loan_t *cur;
5847 
5848 	/* optimization: mutex not needed to check empty list */
5849 	if (lock_loan_head.pmlk_next == NULL) {
5850 		ndi_devi_exit(dip, circ);
5851 		return;
5852 	}
5853 
5854 	mutex_enter(&pm_loan_lock);
5855 	/* see if our thread is registered as a lock borrower. */
5856 	for (cur = lock_loan_head.pmlk_next; cur; cur = cur->pmlk_next)
5857 		if (cur->pmlk_borrower == curthread)
5858 			break;
5859 	mutex_exit(&pm_loan_lock);
5860 
5861 	if (cur == NULL || cur->pmlk_dip != dip)
5862 		/* we acquired the lock directly, so return it */
5863 		ndi_devi_exit(dip, circ);
5864 }
5865 
5866 /*
5867  * Try to take the lock for changing the power level of a component.
5868  *
5869  * Note: for use by ppm only.
5870  */
5871 int
5872 pm_try_locking_power_single(dev_info_t *dip, int *circp)
5873 {
5874 	return (ndi_devi_tryenter(dip, circp));
5875 }
5876 
5877 #ifdef	DEBUG
5878 /*
5879  * The following are used only to print out data structures for debugging
5880  */
5881 void
5882 prdeps(char *msg)
5883 {
5884 
5885 	pm_pdr_t *rp;
5886 	int i;
5887 
5888 	pm_log("pm_dep_head %s %p\n", msg, (void *)pm_dep_head);
5889 	for (rp = pm_dep_head; rp; rp = rp->pdr_next) {
5890 		pm_log("%p: %s keeper %s, kept %s, kept count %d, next %p\n",
5891 		    (void *)rp, (rp->pdr_isprop ? "property" : "device"),
5892 		    rp->pdr_keeper, rp->pdr_kept, rp->pdr_kept_count,
5893 		    (void *)rp->pdr_next);
5894 		if (rp->pdr_kept_count != 0) {
5895 			pm_log("kept list = ");
5896 			i = 0;
5897 			while (i < rp->pdr_kept_count) {
5898 				pm_log("%s ", rp->pdr_kept_paths[i]);
5899 				i++;
5900 			}
5901 			pm_log("\n");
5902 		}
5903 	}
5904 }
5905 
5906 void
5907 pr_noinvol(char *hdr)
5908 {
5909 	pm_noinvol_t *ip;
5910 
5911 	pm_log("%s\n", hdr);
5912 	rw_enter(&pm_noinvol_rwlock, RW_READER);
5913 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next)
5914 		pm_log("\tmaj %d, flags %x, noinvolpm %d %s\n",
5915 		    ip->ni_major, ip->ni_flags, ip->ni_noinvolpm, ip->ni_path);
5916 	rw_exit(&pm_noinvol_rwlock);
5917 }
5918 #endif
5919 
5920 /*
5921  * Attempt to apply the thresholds indicated by rp to the node specified by
5922  * dip.
5923  */
5924 void
5925 pm_apply_recorded_thresh(dev_info_t *dip, pm_thresh_rec_t *rp)
5926 {
5927 	PMD_FUNC(pmf, "apply_recorded_thresh")
5928 	int i, j;
5929 	int comps = PM_NUMCMPTS(dip);
5930 	struct pm_component *cp;
5931 	pm_pte_t *ep;
5932 	int pm_valid_thresh(dev_info_t *, pm_thresh_rec_t *);
5933 
5934 	PMD(PMD_THRESH, ("%s: part: %s@%s(%s#%d), rp %p, %s\n", pmf,
5935 	    PM_DEVICE(dip), (void *)rp, rp->ptr_physpath))
5936 	PM_LOCK_DIP(dip);
5937 	if (!PM_GET_PM_INFO(dip) || PM_ISBC(dip) || !pm_valid_thresh(dip, rp)) {
5938 		PMD(PMD_FAIL, ("%s: part: %s@%s(%s#%d) PM_GET_PM_INFO %p\n",
5939 		    pmf, PM_DEVICE(dip), (void*)PM_GET_PM_INFO(dip)))
5940 		PMD(PMD_FAIL, ("%s: part: %s@%s(%s#%d) PM_ISBC %d\n",
5941 		    pmf, PM_DEVICE(dip), PM_ISBC(dip)))
5942 		PMD(PMD_FAIL, ("%s: part: %s@%s(%s#%d) pm_valid_thresh %d\n",
5943 		    pmf, PM_DEVICE(dip), pm_valid_thresh(dip, rp)))
5944 		PM_UNLOCK_DIP(dip);
5945 		return;
5946 	}
5947 
5948 	ep = rp->ptr_entries;
5949 	/*
5950 	 * Here we do the special case of a device threshold
5951 	 */
5952 	if (rp->ptr_numcomps == 0) {	/* PM_SET_DEVICE_THRESHOLD product */
5953 		ASSERT(ep && ep->pte_numthresh == 1);
5954 		PMD(PMD_THRESH, ("%s: set dev thr %s@%s(%s#%d) to 0x%x\n",
5955 		    pmf, PM_DEVICE(dip), ep->pte_thresh[0]))
5956 		PM_UNLOCK_DIP(dip);
5957 		pm_set_device_threshold(dip, ep->pte_thresh[0], PMC_DEV_THRESH);
5958 		if (autopm_enabled)
5959 			pm_rescan(dip);
5960 		return;
5961 	}
5962 	for (i = 0; i < comps; i++) {
5963 		cp = PM_CP(dip, i);
5964 		for (j = 0; j < ep->pte_numthresh; j++) {
5965 			PMD(PMD_THRESH, ("%s: set thr %d for %s@%s(%s#%d)[%d] "
5966 			    "to %x\n", pmf, j, PM_DEVICE(dip),
5967 			    i, ep->pte_thresh[j]))
5968 			cp->pmc_comp.pmc_thresh[j + 1] = ep->pte_thresh[j];
5969 		}
5970 		ep++;
5971 	}
5972 	DEVI(dip)->devi_pm_flags &= PMC_THRESH_NONE;
5973 	DEVI(dip)->devi_pm_flags |= PMC_COMP_THRESH;
5974 	PM_UNLOCK_DIP(dip);
5975 
5976 	if (autopm_enabled)
5977 		pm_rescan(dip);
5978 }
5979 
5980 /*
5981  * Returns true if the threshold specified by rp could be applied to dip
5982  * (that is, the number of components and transitions are the same)
5983  */
5984 int
5985 pm_valid_thresh(dev_info_t *dip, pm_thresh_rec_t *rp)
5986 {
5987 	PMD_FUNC(pmf, "valid_thresh")
5988 	int comps, i;
5989 	pm_component_t *cp;
5990 	pm_pte_t *ep;
5991 
5992 	if (!PM_GET_PM_INFO(dip) || PM_ISBC(dip)) {
5993 		PMD(PMD_ERROR, ("%s: %s: no pm_info or BC\n", pmf,
5994 		    rp->ptr_physpath))
5995 		return (0);
5996 	}
5997 	/*
5998 	 * Special case: we represent the PM_SET_DEVICE_THRESHOLD case by
5999 	 * an entry with numcomps == 0, (since we don't know how many
6000 	 * components there are in advance).  This is always a valid
6001 	 * spec.
6002 	 */
6003 	if (rp->ptr_numcomps == 0) {
6004 		ASSERT(rp->ptr_entries && rp->ptr_entries->pte_numthresh == 1);
6005 		return (1);
6006 	}
6007 	if (rp->ptr_numcomps != (comps = PM_NUMCMPTS(dip))) {
6008 		PMD(PMD_ERROR, ("%s: comp # mm (dip %d cmd %d) for %s\n",
6009 		    pmf, PM_NUMCMPTS(dip), rp->ptr_numcomps, rp->ptr_physpath))
6010 		return (0);
6011 	}
6012 	ep = rp->ptr_entries;
6013 	for (i = 0; i < comps; i++) {
6014 		cp = PM_CP(dip, i);
6015 		if ((ep + i)->pte_numthresh !=
6016 		    cp->pmc_comp.pmc_numlevels - 1) {
6017 			PMD(PMD_ERROR, ("%s: %s[%d]: thresh=%d, record=%d\n",
6018 			    pmf, rp->ptr_physpath, i,
6019 			    cp->pmc_comp.pmc_numlevels - 1,
6020 			    (ep + i)->pte_numthresh))
6021 			return (0);
6022 		}
6023 	}
6024 	return (1);
6025 }
6026 
6027 /*
6028  * Remove any recorded threshold for device physpath
6029  * We know there will be at most one.
6030  */
6031 void
6032 pm_unrecord_threshold(char *physpath)
6033 {
6034 	pm_thresh_rec_t *pptr, *ptr;
6035 
6036 	rw_enter(&pm_thresh_rwlock, RW_WRITER);
6037 	for (pptr = NULL, ptr = pm_thresh_head; ptr; ptr = ptr->ptr_next) {
6038 		if (strcmp(physpath, ptr->ptr_physpath) == 0) {
6039 			if (pptr) {
6040 				pptr->ptr_next = ptr->ptr_next;
6041 			} else {
6042 				ASSERT(pm_thresh_head == ptr);
6043 				pm_thresh_head = ptr->ptr_next;
6044 			}
6045 			kmem_free(ptr, ptr->ptr_size);
6046 			break;
6047 		}
6048 		pptr = ptr;
6049 	}
6050 	rw_exit(&pm_thresh_rwlock);
6051 }
6052 
6053 /*
6054  * Discard all recorded thresholds.  We are returning to the default pm state.
6055  */
6056 void
6057 pm_discard_thresholds(void)
6058 {
6059 	pm_thresh_rec_t *rp;
6060 	rw_enter(&pm_thresh_rwlock, RW_WRITER);
6061 	while (pm_thresh_head) {
6062 		rp = pm_thresh_head;
6063 		pm_thresh_head = rp->ptr_next;
6064 		kmem_free(rp, rp->ptr_size);
6065 	}
6066 	rw_exit(&pm_thresh_rwlock);
6067 }
6068 
6069 /*
6070  * Discard all recorded dependencies.  We are returning to the default pm state.
6071  */
6072 void
6073 pm_discard_dependencies(void)
6074 {
6075 	pm_pdr_t *rp;
6076 	int i;
6077 	size_t length;
6078 
6079 #ifdef DEBUG
6080 	if (pm_debug & PMD_DEP)
6081 		prdeps("Before discard\n");
6082 #endif
6083 	ddi_walk_devs(ddi_root_node(), pm_discard_dep_walk, NULL);
6084 
6085 #ifdef DEBUG
6086 	if (pm_debug & PMD_DEP)
6087 		prdeps("After discard\n");
6088 #endif
6089 	while (pm_dep_head) {
6090 		rp = pm_dep_head;
6091 		if (!rp->pdr_isprop) {
6092 			ASSERT(rp->pdr_satisfied == 0);
6093 			ASSERT(pm_unresolved_deps);
6094 			pm_unresolved_deps--;
6095 		} else {
6096 			ASSERT(pm_prop_deps);
6097 			pm_prop_deps--;
6098 		}
6099 		pm_dep_head = rp->pdr_next;
6100 		if (rp->pdr_kept_count)  {
6101 			for (i = 0; i < rp->pdr_kept_count; i++) {
6102 				length = strlen(rp->pdr_kept_paths[i]) + 1;
6103 				kmem_free(rp->pdr_kept_paths[i], length);
6104 			}
6105 			kmem_free(rp->pdr_kept_paths,
6106 				rp->pdr_kept_count * sizeof (char **));
6107 		}
6108 		kmem_free(rp, rp->pdr_size);
6109 	}
6110 }
6111 
6112 
6113 static int
6114 pm_discard_dep_walk(dev_info_t *dip, void *arg)
6115 {
6116 	_NOTE(ARGUNUSED(arg))
6117 	char *pathbuf;
6118 
6119 	if (PM_GET_PM_INFO(dip) == NULL)
6120 		return (DDI_WALK_CONTINUE);
6121 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6122 	(void) ddi_pathname(dip, pathbuf);
6123 	pm_free_keeper(pathbuf, 0);
6124 	kmem_free(pathbuf, MAXPATHLEN);
6125 	return (DDI_WALK_CONTINUE);
6126 }
6127 
6128 static int
6129 pm_kept_walk(dev_info_t *dip, void *arg)
6130 {
6131 	_NOTE(ARGUNUSED(arg))
6132 	char *pathbuf;
6133 
6134 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6135 	(void) ddi_pathname(dip, pathbuf);
6136 	(void) pm_kept(pathbuf);
6137 	kmem_free(pathbuf, MAXPATHLEN);
6138 
6139 	return (DDI_WALK_CONTINUE);
6140 }
6141 
6142 static int
6143 pm_keeper_walk(dev_info_t *dip, void *arg)
6144 {
6145 	_NOTE(ARGUNUSED(arg))
6146 	char *pathbuf;
6147 
6148 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6149 	(void) ddi_pathname(dip, pathbuf);
6150 	(void) pm_keeper(pathbuf);
6151 	kmem_free(pathbuf, MAXPATHLEN);
6152 
6153 	return (DDI_WALK_CONTINUE);
6154 }
6155 
6156 static char *
6157 pdw_type_decode(int type)
6158 {
6159 	switch (type) {
6160 	case PM_DEP_WK_POWER_ON:
6161 		return ("power on");
6162 	case PM_DEP_WK_POWER_OFF:
6163 		return ("power off");
6164 	case PM_DEP_WK_DETACH:
6165 		return ("detach");
6166 	case PM_DEP_WK_REMOVE_DEP:
6167 		return ("remove dep");
6168 	case PM_DEP_WK_BRINGUP_SELF:
6169 		return ("bringup self");
6170 	case PM_DEP_WK_RECORD_KEEPER:
6171 		return ("add dependent");
6172 	case PM_DEP_WK_RECORD_KEEPER_PROP:
6173 		return ("add dependent property");
6174 	case PM_DEP_WK_KEPT:
6175 		return ("kept");
6176 	case PM_DEP_WK_KEEPER:
6177 		return ("keeper");
6178 	case PM_DEP_WK_ATTACH:
6179 		return ("attach");
6180 	case PM_DEP_WK_CHECK_KEPT:
6181 		return ("check kept");
6182 	case PM_DEP_WK_CPR_SUSPEND:
6183 		return ("suspend");
6184 	case PM_DEP_WK_CPR_RESUME:
6185 		return ("resume");
6186 	default:
6187 		return ("unknown");
6188 	}
6189 
6190 }
6191 
6192 static void
6193 pm_rele_dep(char *keeper)
6194 {
6195 	PMD_FUNC(pmf, "rele_dep")
6196 	pm_pdr_t *dp;
6197 	char *kept_path = NULL;
6198 	dev_info_t *kept = NULL;
6199 	int count = 0;
6200 
6201 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
6202 		if (strcmp(dp->pdr_keeper, keeper) != 0)
6203 			continue;
6204 		for (count = 0; count < dp->pdr_kept_count; count++) {
6205 			kept_path = dp->pdr_kept_paths[count];
6206 			if (kept_path == NULL)
6207 				continue;
6208 			kept = pm_name_to_dip(kept_path, 1);
6209 			if (kept) {
6210 				PMD(PMD_KEEPS, ("%s: release kept=%s@%s(%s#%d) "
6211 				    "of keeper=%s\n", pmf, PM_DEVICE(kept),
6212 				    keeper))
6213 				ASSERT(DEVI(kept)->devi_pm_kidsupcnt > 0);
6214 				pm_rele_power(kept);
6215 				ddi_release_devi(kept);
6216 			}
6217 		}
6218 	}
6219 }
6220 
6221 /*
6222  * Called when we are just released from direct PM.  Bring ourself up
6223  * if our keeper is up since dependency is not honored while a kept
6224  * device is under direct PM.
6225  */
6226 static void
6227 pm_bring_self_up(char *keptpath)
6228 {
6229 	PMD_FUNC(pmf, "bring_self_up")
6230 	dev_info_t *kept;
6231 	dev_info_t *keeper;
6232 	pm_pdr_t *dp;
6233 	int i, j;
6234 	int up = 0, circ;
6235 
6236 	kept = pm_name_to_dip(keptpath, 1);
6237 	if (kept == NULL)
6238 		return;
6239 	PMD(PMD_KEEPS, ("%s: kept=%s@%s(%s#%d)\n", pmf, PM_DEVICE(kept)))
6240 	for (dp = pm_dep_head; dp; dp = dp->pdr_next) {
6241 		if (dp->pdr_kept_count == 0)
6242 			continue;
6243 		for (i = 0; i < dp->pdr_kept_count; i++) {
6244 			if (strcmp(dp->pdr_kept_paths[i], keptpath) != 0)
6245 				continue;
6246 			keeper = pm_name_to_dip(dp->pdr_keeper, 1);
6247 			if (keeper) {
6248 				PMD(PMD_KEEPS, ("%s: keeper=%s@%s(%s#%d)\n",
6249 				    pmf, PM_DEVICE(keeper)))
6250 				PM_LOCK_POWER(keeper, &circ);
6251 				for (j = 0; j < PM_NUMCMPTS(keeper);
6252 				    j++) {
6253 					if (PM_CURPOWER(keeper, j)) {
6254 						PMD(PMD_KEEPS, ("%s: comp="
6255 						    "%d is up\n", pmf, j))
6256 						up++;
6257 					}
6258 				}
6259 				if (up) {
6260 					if (PM_SKBU(kept))
6261 						DEVI(kept)->devi_pm_flags &=
6262 						    ~PMC_SKIP_BRINGUP;
6263 					bring_pmdep_up(kept, 1);
6264 				}
6265 				PM_UNLOCK_POWER(keeper, circ);
6266 				ddi_release_devi(keeper);
6267 			}
6268 		}
6269 	}
6270 	ddi_release_devi(kept);
6271 }
6272 
6273 static void
6274 pm_process_dep_request(pm_dep_wk_t *work)
6275 {
6276 	PMD_FUNC(pmf, "dep_req")
6277 	int ret;
6278 
6279 	PMD(PMD_DEP, ("%s: work=%s\n", pmf,
6280 	    pdw_type_decode(work->pdw_type)))
6281 	PMD(PMD_DEP, ("%s: keeper=%s, kept=%s\n", pmf,
6282 	    (work->pdw_keeper ? work->pdw_keeper : "NULL"),
6283 	    (work->pdw_kept ? work->pdw_kept : "NULL")))
6284 
6285 	switch (work->pdw_type) {
6286 	case PM_DEP_WK_POWER_ON:
6287 		/* Bring up the kept devices and put a hold on them */
6288 		bring_wekeeps_up(work->pdw_keeper);
6289 		break;
6290 	case PM_DEP_WK_POWER_OFF:
6291 		/* Release the kept devices */
6292 		pm_rele_dep(work->pdw_keeper);
6293 		break;
6294 	case PM_DEP_WK_DETACH:
6295 		pm_free_keeps(work->pdw_keeper, work->pdw_pwr);
6296 		break;
6297 	case PM_DEP_WK_REMOVE_DEP:
6298 		pm_discard_dependencies();
6299 		break;
6300 	case PM_DEP_WK_BRINGUP_SELF:
6301 		/*
6302 		 * We deferred satisfying our dependency till now, so satisfy
6303 		 * it again and bring ourselves up.
6304 		 */
6305 		pm_bring_self_up(work->pdw_kept);
6306 		break;
6307 	case PM_DEP_WK_RECORD_KEEPER:
6308 		(void) pm_record_keeper(work->pdw_kept, work->pdw_keeper, 0);
6309 		ddi_walk_devs(ddi_root_node(), pm_kept_walk, NULL);
6310 		ddi_walk_devs(ddi_root_node(), pm_keeper_walk, NULL);
6311 		break;
6312 	case PM_DEP_WK_RECORD_KEEPER_PROP:
6313 		(void) pm_record_keeper(work->pdw_kept, work->pdw_keeper, 1);
6314 		ddi_walk_devs(ddi_root_node(), pm_keeper_walk, NULL);
6315 		ddi_walk_devs(ddi_root_node(), pm_kept_walk, NULL);
6316 		break;
6317 	case PM_DEP_WK_KEPT:
6318 		ret = pm_kept(work->pdw_kept);
6319 		PMD(PMD_DEP, ("%s: PM_DEP_WK_KEPT: pm_kept returns %d\n", pmf,
6320 		    ret))
6321 		break;
6322 	case PM_DEP_WK_KEEPER:
6323 		ret = pm_keeper(work->pdw_keeper);
6324 		PMD(PMD_DEP, ("%s: PM_DEP_WK_KEEPER: pm_keeper returns %d\n",
6325 		    pmf, ret))
6326 		break;
6327 	case PM_DEP_WK_ATTACH:
6328 		ret = pm_keeper(work->pdw_keeper);
6329 		PMD(PMD_DEP, ("%s: PM_DEP_WK_ATTACH: pm_keeper returns %d\n",
6330 		    pmf, ret))
6331 		ret = pm_kept(work->pdw_kept);
6332 		PMD(PMD_DEP, ("%s: PM_DEP_WK_ATTACH: pm_kept returns %d\n",
6333 		    pmf, ret))
6334 		break;
6335 	case PM_DEP_WK_CHECK_KEPT:
6336 		ret = pm_is_kept(work->pdw_kept);
6337 		PMD(PMD_DEP, ("%s: PM_DEP_WK_CHECK_KEPT: kept=%s, ret=%d\n",
6338 		    pmf, work->pdw_kept, ret))
6339 		break;
6340 	case PM_DEP_WK_CPR_SUSPEND:
6341 		pm_discard_dependencies();
6342 		break;
6343 	case PM_DEP_WK_CPR_RESUME:
6344 		ddi_walk_devs(ddi_root_node(), pm_kept_walk, NULL);
6345 		ddi_walk_devs(ddi_root_node(), pm_keeper_walk, NULL);
6346 		break;
6347 	default:
6348 		ASSERT(0);
6349 		break;
6350 	}
6351 	/*
6352 	 * Free the work structure if the requester is not waiting
6353 	 * Otherwise it is the requester's responsiblity to free it.
6354 	 */
6355 	if (!work->pdw_wait) {
6356 		if (work->pdw_keeper)
6357 			kmem_free(work->pdw_keeper,
6358 			    strlen(work->pdw_keeper) + 1);
6359 		if (work->pdw_kept)
6360 			kmem_free(work->pdw_kept, strlen(work->pdw_kept) + 1);
6361 		kmem_free(work, sizeof (pm_dep_wk_t));
6362 	} else {
6363 		/*
6364 		 * Notify requester if it is waiting for it.
6365 		 */
6366 		work->pdw_ret = ret;
6367 		work->pdw_done = 1;
6368 		cv_signal(&work->pdw_cv);
6369 	}
6370 }
6371 
6372 /*
6373  * Process PM dependency requests.
6374  */
6375 static void
6376 pm_dep_thread(void)
6377 {
6378 	pm_dep_wk_t *work;
6379 	callb_cpr_t cprinfo;
6380 
6381 	CALLB_CPR_INIT(&cprinfo, &pm_dep_thread_lock, callb_generic_cpr,
6382 	    "pm_dep_thread");
6383 	for (;;) {
6384 		mutex_enter(&pm_dep_thread_lock);
6385 		if (pm_dep_thread_workq == NULL) {
6386 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
6387 			cv_wait(&pm_dep_thread_cv, &pm_dep_thread_lock);
6388 			CALLB_CPR_SAFE_END(&cprinfo, &pm_dep_thread_lock);
6389 		}
6390 		work = pm_dep_thread_workq;
6391 		pm_dep_thread_workq = work->pdw_next;
6392 		if (pm_dep_thread_tail == work)
6393 			pm_dep_thread_tail = work->pdw_next;
6394 		mutex_exit(&pm_dep_thread_lock);
6395 		pm_process_dep_request(work);
6396 
6397 	}
6398 	/*NOTREACHED*/
6399 }
6400 
6401 /*
6402  * Set the power level of the indicated device to unknown (if it is not a
6403  * backwards compatible device), as it has just been resumed, and it won't
6404  * know if the power was removed or not. Adjust parent's kidsupcnt if necessary.
6405  */
6406 void
6407 pm_forget_power_level(dev_info_t *dip)
6408 {
6409 	dev_info_t *pdip = ddi_get_parent(dip);
6410 	int i, count = 0;
6411 
6412 	if (!PM_ISBC(dip)) {
6413 		for (i = 0; i < PM_NUMCMPTS(dip); i++)
6414 			count += (PM_CURPOWER(dip, i) == 0);
6415 
6416 		if (count && pdip && !PM_WANTS_NOTIFICATION(pdip))
6417 			e_pm_hold_rele_power(pdip, count);
6418 
6419 		/*
6420 		 * Count this as a power cycle if we care
6421 		 */
6422 		if (DEVI(dip)->devi_pm_volpmd &&
6423 		    PM_CP(dip, 0)->pmc_cur_pwr == 0)
6424 			DEVI(dip)->devi_pm_volpmd = 0;
6425 		for (i = 0; i < PM_NUMCMPTS(dip); i++)
6426 			e_pm_set_cur_pwr(dip, PM_CP(dip, i), PM_LEVEL_UNKNOWN);
6427 	}
6428 }
6429 
6430 /*
6431  * This function advises the caller whether it should make a power-off
6432  * transition at this time or not.  If the transition is not advised
6433  * at this time, the time that the next power-off transition can
6434  * be made from now is returned through "intervalp" pointer.
6435  * This function returns:
6436  *
6437  *  1  power-off advised
6438  *  0  power-off not advised, intervalp will point to seconds from
6439  *	  now that a power-off is advised.  If it is passed the number
6440  *	  of years that policy specifies the device should last,
6441  *	  a large number is returned as the time interval.
6442  *  -1  error
6443  */
6444 int
6445 pm_trans_check(struct pm_trans_data *datap, time_t *intervalp)
6446 {
6447 	PMD_FUNC(pmf, "pm_trans_check")
6448 	char dbuf[DC_SCSI_MFR_LEN];
6449 	struct pm_scsi_cycles *scp;
6450 	int service_years, service_weeks, full_years;
6451 	time_t now, service_seconds, tdiff;
6452 	time_t within_year, when_allowed;
6453 	char *ptr;
6454 	int lower_bound_cycles, upper_bound_cycles, cycles_allowed;
6455 	int cycles_diff, cycles_over;
6456 
6457 	if (datap == NULL) {
6458 		PMD(PMD_TCHECK, ("%s: NULL data pointer!\n", pmf))
6459 		return (-1);
6460 	}
6461 
6462 	if (datap->format == DC_SCSI_FORMAT) {
6463 		/*
6464 		 * Power cycles of the scsi drives are distributed
6465 		 * over 5 years with the following percentage ratio:
6466 		 *
6467 		 *	30%, 25%, 20%, 15%, and 10%
6468 		 *
6469 		 * The power cycle quota for each year is distributed
6470 		 * linearly through out the year.  The equation for
6471 		 * determining the expected cycles is:
6472 		 *
6473 		 *	e = a * (n / y)
6474 		 *
6475 		 * e = expected cycles
6476 		 * a = allocated cycles for this year
6477 		 * n = number of seconds since beginning of this year
6478 		 * y = number of seconds in a year
6479 		 *
6480 		 * Note that beginning of the year starts the day that
6481 		 * the drive has been put on service.
6482 		 *
6483 		 * If the drive has passed its expected cycles, we
6484 		 * can determine when it can start to power cycle
6485 		 * again to keep it on track to meet the 5-year
6486 		 * life expectancy.  The equation for determining
6487 		 * when to power cycle is:
6488 		 *
6489 		 *	w = y * (c / a)
6490 		 *
6491 		 * w = when it can power cycle again
6492 		 * y = number of seconds in a year
6493 		 * c = current number of cycles
6494 		 * a = allocated cycles for the year
6495 		 *
6496 		 */
6497 		char pcnt[DC_SCSI_NPY] = { 30, 55, 75, 90, 100 };
6498 
6499 		scp = &datap->un.scsi_cycles;
6500 		PMD(PMD_TCHECK, ("%s: format=%d, lifemax=%d, ncycles=%d, "
6501 		    "svc_date=%s, svc_flag=%d\n", pmf, datap->format,
6502 		    scp->lifemax, scp->ncycles, scp->svc_date, scp->flag))
6503 		if (scp->ncycles < 0 || scp->flag != 0) {
6504 			PMD(PMD_TCHECK, ("%s: ncycles < 0 || flag != 0\n", pmf))
6505 			return (-1);
6506 		}
6507 
6508 		if (scp->ncycles > scp->lifemax) {
6509 			*intervalp = (LONG_MAX / hz);
6510 			return (0);
6511 		}
6512 
6513 		/*
6514 		 * convert service date to time_t
6515 		 */
6516 		bcopy(scp->svc_date, dbuf, DC_SCSI_YEAR_LEN);
6517 		dbuf[DC_SCSI_YEAR_LEN] = '\0';
6518 		ptr = dbuf;
6519 		service_years = stoi(&ptr) - EPOCH_YEAR;
6520 		bcopy(&scp->svc_date[DC_SCSI_YEAR_LEN], dbuf,
6521 		    DC_SCSI_WEEK_LEN);
6522 		dbuf[DC_SCSI_WEEK_LEN] = '\0';
6523 
6524 		/*
6525 		 * scsi standard does not specify WW data,
6526 		 * could be (00-51) or (01-52)
6527 		 */
6528 		ptr = dbuf;
6529 		service_weeks = stoi(&ptr);
6530 		if (service_years < 0 ||
6531 		    service_weeks < 0 || service_weeks > 52) {
6532 			PMD(PMD_TCHECK, ("%s: service year %d and week %d\n",
6533 			    pmf, service_years, service_weeks))
6534 			return (-1);
6535 		}
6536 
6537 		/*
6538 		 * calculate service date in seconds-since-epoch,
6539 		 * adding one day for each leap-year.
6540 		 *
6541 		 * (years-since-epoch + 2) fixes integer truncation,
6542 		 * example: (8) leap-years during [1972, 2000]
6543 		 * (2000 - 1970) = 30;  and  (30 + 2) / 4 = 8;
6544 		 */
6545 		service_seconds = (service_years * DC_SPY) +
6546 		    (service_weeks * DC_SPW) +
6547 		    (((service_years + 2) / 4) * DC_SPD);
6548 
6549 		now = gethrestime_sec();
6550 		/*
6551 		 * since the granularity of 'svc_date' is day not second,
6552 		 * 'now' should be rounded up to full day.
6553 		 */
6554 		now = ((now + DC_SPD -1) / DC_SPD) * DC_SPD;
6555 		if (service_seconds > now) {
6556 			PMD(PMD_TCHECK, ("%s: service date (%ld) later "
6557 			    "than now (%ld)!\n", pmf, service_seconds, now))
6558 			return (-1);
6559 		}
6560 
6561 		tdiff = now - service_seconds;
6562 		PMD(PMD_TCHECK, ("%s: age is %ld sec\n", pmf, tdiff))
6563 
6564 		/*
6565 		 * NOTE - Leap years are not considered in the calculations
6566 		 * below.
6567 		 */
6568 		full_years = (tdiff / DC_SPY);
6569 		if ((full_years >= DC_SCSI_NPY) &&
6570 		    (scp->ncycles <= scp->lifemax))
6571 			return (1);
6572 
6573 		/*
6574 		 * Determine what is the normal cycle usage for the
6575 		 * device at the beginning and the end of this year.
6576 		 */
6577 		lower_bound_cycles = (!full_years) ? 0 :
6578 		    ((scp->lifemax * pcnt[full_years - 1]) / 100);
6579 		upper_bound_cycles = (scp->lifemax * pcnt[full_years]) / 100;
6580 
6581 		if (scp->ncycles <= lower_bound_cycles)
6582 			return (1);
6583 
6584 		/*
6585 		 * The linear slope that determines how many cycles
6586 		 * are allowed this year is number of seconds
6587 		 * passed this year over total number of seconds in a year.
6588 		 */
6589 		cycles_diff = (upper_bound_cycles - lower_bound_cycles);
6590 		within_year = (tdiff % DC_SPY);
6591 		cycles_allowed = lower_bound_cycles +
6592 		    (((uint64_t)cycles_diff * (uint64_t)within_year) / DC_SPY);
6593 		PMD(PMD_TCHECK, ("%s: lived %d yrs and %ld secs\n", pmf,
6594 		    full_years, within_year))
6595 		PMD(PMD_TCHECK, ("%s: # of cycles allowed %d\n", pmf,
6596 		    cycles_allowed))
6597 
6598 		if (scp->ncycles <= cycles_allowed)
6599 			return (1);
6600 
6601 		/*
6602 		 * The transition is not advised now but we can
6603 		 * determine when the next transition can be made.
6604 		 *
6605 		 * Depending on how many cycles the device has been
6606 		 * over-used, we may need to skip years with
6607 		 * different percentage quota in order to determine
6608 		 * when the next transition can be made.
6609 		 */
6610 		cycles_over = (scp->ncycles - lower_bound_cycles);
6611 		while (cycles_over > cycles_diff) {
6612 			full_years++;
6613 			if (full_years >= DC_SCSI_NPY) {
6614 				*intervalp = (LONG_MAX / hz);
6615 				return (0);
6616 			}
6617 			cycles_over -= cycles_diff;
6618 			lower_bound_cycles = upper_bound_cycles;
6619 			upper_bound_cycles =
6620 			    (scp->lifemax * pcnt[full_years]) / 100;
6621 			cycles_diff = (upper_bound_cycles - lower_bound_cycles);
6622 		}
6623 
6624 		/*
6625 		 * The linear slope that determines when the next transition
6626 		 * can be made is the relative position of used cycles within a
6627 		 * year over total number of cycles within that year.
6628 		 */
6629 		when_allowed = service_seconds + (full_years * DC_SPY) +
6630 		    (((uint64_t)DC_SPY * (uint64_t)cycles_over) / cycles_diff);
6631 		*intervalp = (when_allowed - now);
6632 		if (*intervalp > (LONG_MAX / hz))
6633 			*intervalp = (LONG_MAX / hz);
6634 		PMD(PMD_TCHECK, ("%s: no cycle is allowed in %ld secs\n", pmf,
6635 		    *intervalp))
6636 		return (0);
6637 	}
6638 
6639 	PMD(PMD_TCHECK, ("%s: unknown format!\n", pmf))
6640 	return (-1);
6641 }
6642 
6643 /*
6644  * Nexus drivers call into pm framework to indicate which child driver is about
6645  * to be installed.  In some platforms, ppm may need to configure the hardware
6646  * for successful installation of a driver.
6647  */
6648 int
6649 pm_init_child(dev_info_t *dip)
6650 {
6651 	power_req_t power_req;
6652 
6653 	ASSERT(ddi_binding_name(dip));
6654 	ASSERT(ddi_get_name_addr(dip));
6655 	pm_ppm_claim(dip);
6656 	if (pm_ppm_claimed(dip)) {	/* if ppm driver claims the node */
6657 		power_req.request_type = PMR_PPM_INIT_CHILD;
6658 		power_req.req.ppm_config_req.who = dip;
6659 		ASSERT(PPM(dip) != NULL);
6660 		return (pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER, &power_req,
6661 		    NULL));
6662 	} else {
6663 #ifdef DEBUG
6664 		/* pass it to the default handler so we can debug things */
6665 		power_req.request_type = PMR_PPM_INIT_CHILD;
6666 		power_req.req.ppm_config_req.who = dip;
6667 		(void) pm_ctlops(NULL, dip,
6668 		    DDI_CTLOPS_POWER, &power_req, NULL);
6669 #endif
6670 	}
6671 	return (DDI_SUCCESS);
6672 }
6673 
6674 /*
6675  * Bring parent of a node that is about to be probed up to full power, and
6676  * arrange for it to stay up until pm_post_probe() or pm_post_attach() decide
6677  * it is time to let it go down again
6678  */
6679 void
6680 pm_pre_probe(dev_info_t *dip, pm_ppm_cookie_t *cp)
6681 {
6682 	int result;
6683 	power_req_t power_req;
6684 
6685 	bzero(cp, sizeof (*cp));
6686 	cp->ppc_dip = dip;
6687 
6688 	pm_ppm_claim(dip);
6689 	if (pm_ppm_claimed(dip)) {	/* if ppm driver claims the node */
6690 		power_req.request_type = PMR_PPM_PRE_PROBE;
6691 		power_req.req.ppm_config_req.who = dip;
6692 		ASSERT(PPM(dip) != NULL);
6693 		(void) pm_ctlops(PPM(dip), dip,
6694 		    DDI_CTLOPS_POWER, &power_req, &result);
6695 		cp->ppc_ppm = PPM(dip);
6696 	} else {
6697 #ifdef DEBUG
6698 		/* pass it to the default handler so we can debug things */
6699 		power_req.request_type = PMR_PPM_PRE_PROBE;
6700 		power_req.req.ppm_config_req.who = dip;
6701 		(void) pm_ctlops(NULL, dip,
6702 		    DDI_CTLOPS_POWER, &power_req, &result);
6703 #endif
6704 		cp->ppc_ppm = NULL;
6705 	}
6706 }
6707 
6708 int
6709 pm_pre_config(dev_info_t *dip, char *devnm)
6710 {
6711 	PMD_FUNC(pmf, "pre_config")
6712 	int ret;
6713 
6714 	if (MDI_VHCI(dip)) {
6715 		PMD(PMD_SET, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
6716 		ret = mdi_power(dip, MDI_PM_PRE_CONFIG, NULL, devnm, 0);
6717 		return (ret == MDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
6718 	} else if (!PM_GET_PM_INFO(dip))
6719 		return (DDI_SUCCESS);
6720 
6721 	PMD(PMD_SET, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
6722 	pm_hold_power(dip);
6723 	ret = pm_all_to_normal(dip, PM_CANBLOCK_BLOCK);
6724 	if (ret != DDI_SUCCESS)
6725 		pm_rele_power(dip);
6726 	return (ret);
6727 }
6728 
6729 /*
6730  * This routine is called by devfs during its walk to unconfigue a node.
6731  * If the call is due to auto mod_unloads and the dip is not at its
6732  * full power, we return DDI_FAILURE to terminate the walk, otherwise
6733  * return DDI_SUCCESS.
6734  */
6735 int
6736 pm_pre_unconfig(dev_info_t *dip, int flags, int *held, char *devnm)
6737 {
6738 	PMD_FUNC(pmf, "pre_unconfig")
6739 	int ret;
6740 
6741 	if (MDI_VHCI(dip)) {
6742 		PMD(PMD_SET, ("%s: %s@%s(%s#%d), flags=%x\n", pmf,
6743 		    PM_DEVICE(dip), flags))
6744 		ret = mdi_power(dip, MDI_PM_PRE_UNCONFIG, held, devnm, flags);
6745 		return (ret == MDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
6746 	} else if (!PM_GET_PM_INFO(dip))
6747 		return (DDI_SUCCESS);
6748 
6749 	PMD(PMD_SET, ("%s: %s@%s(%s#%d), flags=%x\n", pmf, PM_DEVICE(dip),
6750 	    flags))
6751 	*held = 0;
6752 
6753 	/*
6754 	 * If the dip is a leaf node, don't power it up.
6755 	 */
6756 	if (!ddi_get_child(dip))
6757 		return (DDI_SUCCESS);
6758 
6759 	/*
6760 	 * Do not power up the node if it is called due to auto-modunload.
6761 	 */
6762 	if ((flags & NDI_AUTODETACH) && !pm_all_at_normal(dip))
6763 		return (DDI_FAILURE);
6764 
6765 	pm_hold_power(dip);
6766 	*held = 1;
6767 	ret = pm_all_to_normal(dip, PM_CANBLOCK_BLOCK);
6768 	if (ret != DDI_SUCCESS) {
6769 		pm_rele_power(dip);
6770 		*held = 0;
6771 	}
6772 	return (ret);
6773 }
6774 
6775 /*
6776  * Notify ppm of attach action.  Parent is already held at full power by
6777  * probe action.
6778  */
6779 void
6780 pm_pre_attach(dev_info_t *dip, pm_ppm_cookie_t *cp, ddi_attach_cmd_t cmd)
6781 {
6782 	static char *me = "pm_pre_attach";
6783 	power_req_t power_req;
6784 	int result;
6785 
6786 	/*
6787 	 * Initialize and fill in the PPM cookie
6788 	 */
6789 	bzero(cp, sizeof (*cp));
6790 	cp->ppc_cmd = (int)cmd;
6791 	cp->ppc_ppm = PPM(dip);
6792 	cp->ppc_dip = dip;
6793 
6794 	/*
6795 	 * DDI_ATTACH and DDI_RESUME cmds need to call platform specific
6796 	 * Power Management stuff. DDI_RESUME also has to purge it's
6797 	 * powerlevel information.
6798 	 */
6799 	switch (cmd) {
6800 	case DDI_ATTACH:
6801 		if (cp->ppc_ppm) {	/* if ppm driver claims the node */
6802 			power_req.request_type = PMR_PPM_PRE_ATTACH;
6803 			power_req.req.ppm_config_req.who = dip;
6804 			ASSERT(PPM(dip));
6805 			(void) pm_ctlops(cp->ppc_ppm, dip, DDI_CTLOPS_POWER,
6806 			    &power_req, &result);
6807 		}
6808 #ifdef DEBUG
6809 		else {
6810 			power_req.request_type = PMR_PPM_PRE_ATTACH;
6811 			power_req.req.ppm_config_req.who = dip;
6812 			(void) pm_ctlops(NULL, dip,
6813 			    DDI_CTLOPS_POWER, &power_req, &result);
6814 		}
6815 #endif
6816 		break;
6817 	case DDI_RESUME:
6818 		pm_forget_power_level(dip);
6819 
6820 		if (cp->ppc_ppm) {	/* if ppm driver claims the node */
6821 			power_req.request_type = PMR_PPM_PRE_RESUME;
6822 			power_req.req.resume_req.who = cp->ppc_dip;
6823 			power_req.req.resume_req.cmd =
6824 			    (ddi_attach_cmd_t)cp->ppc_cmd;
6825 			ASSERT(PPM(cp->ppc_dip) == cp->ppc_ppm);
6826 			(void) pm_ctlops(cp->ppc_ppm, cp->ppc_dip,
6827 			    DDI_CTLOPS_POWER, &power_req, &result);
6828 		}
6829 #ifdef DEBUG
6830 		else {
6831 			power_req.request_type = PMR_PPM_PRE_RESUME;
6832 			power_req.req.resume_req.who = cp->ppc_dip;
6833 			power_req.req.resume_req.cmd =
6834 			    (ddi_attach_cmd_t)cp->ppc_cmd;
6835 			(void) pm_ctlops(NULL, cp->ppc_dip,
6836 			    DDI_CTLOPS_POWER, &power_req, &result);
6837 		}
6838 #endif
6839 		break;
6840 
6841 	case DDI_PM_RESUME:
6842 		break;
6843 
6844 	default:
6845 		panic(me);
6846 	}
6847 }
6848 
6849 /*
6850  * Nexus drivers call into pm framework to indicate which child driver is
6851  * being uninstalled.  In some platforms, ppm may need to reconfigure the
6852  * hardware since the device driver is no longer installed.
6853  */
6854 int
6855 pm_uninit_child(dev_info_t *dip)
6856 {
6857 	power_req_t power_req;
6858 
6859 	ASSERT(ddi_binding_name(dip));
6860 	ASSERT(ddi_get_name_addr(dip));
6861 	pm_ppm_claim(dip);
6862 	if (pm_ppm_claimed(dip)) {	/* if ppm driver claims the node */
6863 		power_req.request_type = PMR_PPM_UNINIT_CHILD;
6864 		power_req.req.ppm_config_req.who = dip;
6865 		ASSERT(PPM(dip));
6866 		return (pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER, &power_req,
6867 		    NULL));
6868 	} else {
6869 #ifdef DEBUG
6870 		/* pass it to the default handler so we can debug things */
6871 		power_req.request_type = PMR_PPM_UNINIT_CHILD;
6872 		power_req.req.ppm_config_req.who = dip;
6873 		(void) pm_ctlops(NULL, dip, DDI_CTLOPS_POWER, &power_req, NULL);
6874 #endif
6875 	}
6876 	return (DDI_SUCCESS);
6877 }
6878 /*
6879  * Decrement kidsupcnt so scan can turn the parent back off if it is idle
6880  * Also notify ppm of result of probe if there is a ppm that cares
6881  */
6882 void
6883 pm_post_probe(pm_ppm_cookie_t *cp, int ret, int probe_failed)
6884 {
6885 	_NOTE(ARGUNUSED(probe_failed))
6886 	int result;
6887 	power_req_t power_req;
6888 
6889 	if (cp->ppc_ppm) {	/* if ppm driver claims the node */
6890 		power_req.request_type = PMR_PPM_POST_PROBE;
6891 		power_req.req.ppm_config_req.who = cp->ppc_dip;
6892 		power_req.req.ppm_config_req.result = ret;
6893 		ASSERT(PPM(cp->ppc_dip) == cp->ppc_ppm);
6894 		(void) pm_ctlops(cp->ppc_ppm, cp->ppc_dip, DDI_CTLOPS_POWER,
6895 		    &power_req, &result);
6896 	}
6897 #ifdef DEBUG
6898 	else {
6899 		power_req.request_type = PMR_PPM_POST_PROBE;
6900 		power_req.req.ppm_config_req.who = cp->ppc_dip;
6901 		power_req.req.ppm_config_req.result = ret;
6902 		(void) pm_ctlops(NULL, cp->ppc_dip, DDI_CTLOPS_POWER,
6903 		    &power_req, &result);
6904 	}
6905 #endif
6906 }
6907 
6908 void
6909 pm_post_config(dev_info_t *dip, char *devnm)
6910 {
6911 	PMD_FUNC(pmf, "post_config")
6912 
6913 	if (MDI_VHCI(dip)) {
6914 		PMD(PMD_SET, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
6915 		(void) mdi_power(dip, MDI_PM_POST_CONFIG, NULL, devnm, 0);
6916 		return;
6917 	} else if (!PM_GET_PM_INFO(dip))
6918 		return;
6919 
6920 	PMD(PMD_SET, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
6921 	pm_rele_power(dip);
6922 }
6923 
6924 void
6925 pm_post_unconfig(dev_info_t *dip, int held, char *devnm)
6926 {
6927 	PMD_FUNC(pmf, "post_unconfig")
6928 
6929 	if (MDI_VHCI(dip)) {
6930 		PMD(PMD_SET, ("%s: %s@%s(%s#%d), held = %d\n", pmf,
6931 		    PM_DEVICE(dip), held))
6932 		(void) mdi_power(dip, MDI_PM_POST_UNCONFIG, &held, devnm, 0);
6933 		return;
6934 	} else if (!PM_GET_PM_INFO(dip))
6935 		return;
6936 
6937 	PMD(PMD_SET, ("%s: %s@%s(%s#%d), held = %d\n", pmf, PM_DEVICE(dip),
6938 	    held))
6939 	if (!held)
6940 		return;
6941 	/*
6942 	 * We have held power in pre_unconfig, release it here.
6943 	 */
6944 	pm_rele_power(dip);
6945 }
6946 
6947 /*
6948  * Notify ppm of result of attach if there is a ppm that cares
6949  */
6950 void
6951 pm_post_attach(pm_ppm_cookie_t *cp, int ret)
6952 {
6953 	int result;
6954 	power_req_t power_req;
6955 	dev_info_t	*dip;
6956 
6957 	if (cp->ppc_cmd != DDI_ATTACH)
6958 		return;
6959 
6960 	dip = cp->ppc_dip;
6961 
6962 	if (ret == DDI_SUCCESS) {
6963 		/*
6964 		 * Attach succeeded, so proceed to doing post-attach pm tasks
6965 		 */
6966 		if (PM_GET_PM_INFO(dip) == NULL)
6967 			(void) pm_start(dip);
6968 	} else {
6969 		/*
6970 		 * Attach may have got pm started before failing
6971 		 */
6972 		pm_stop(dip);
6973 	}
6974 
6975 	if (cp->ppc_ppm) {	/* if ppm driver claims the node */
6976 		power_req.request_type = PMR_PPM_POST_ATTACH;
6977 		power_req.req.ppm_config_req.who = cp->ppc_dip;
6978 		power_req.req.ppm_config_req.result = ret;
6979 		ASSERT(PPM(cp->ppc_dip) == cp->ppc_ppm);
6980 		(void) pm_ctlops(cp->ppc_ppm, cp->ppc_dip,
6981 		    DDI_CTLOPS_POWER, &power_req, &result);
6982 	}
6983 #ifdef DEBUG
6984 	else {
6985 		power_req.request_type = PMR_PPM_POST_ATTACH;
6986 		power_req.req.ppm_config_req.who = cp->ppc_dip;
6987 		power_req.req.ppm_config_req.result = ret;
6988 		(void) pm_ctlops(NULL, cp->ppc_dip,
6989 		    DDI_CTLOPS_POWER, &power_req, &result);
6990 	}
6991 #endif
6992 }
6993 
6994 /*
6995  * Notify ppm of attach action.  Parent is already held at full power by
6996  * probe action.
6997  */
6998 void
6999 pm_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, pm_ppm_cookie_t *cp)
7000 {
7001 	int result;
7002 	power_req_t power_req;
7003 
7004 	bzero(cp, sizeof (*cp));
7005 	cp->ppc_dip = dip;
7006 	cp->ppc_cmd = (int)cmd;
7007 
7008 	switch (cmd) {
7009 	case DDI_DETACH:
7010 		pm_detaching(dip);		/* suspend pm while detaching */
7011 		if (pm_ppm_claimed(dip)) {	/* if ppm driver claims node */
7012 			power_req.request_type = PMR_PPM_PRE_DETACH;
7013 			power_req.req.ppm_config_req.who = dip;
7014 			ASSERT(PPM(dip));
7015 			(void) pm_ctlops(PPM(dip), dip, DDI_CTLOPS_POWER,
7016 			    &power_req, &result);
7017 			cp->ppc_ppm = PPM(dip);
7018 		} else {
7019 #ifdef DEBUG
7020 			/* pass to the default handler so we can debug things */
7021 			power_req.request_type = PMR_PPM_PRE_DETACH;
7022 			power_req.req.ppm_config_req.who = dip;
7023 			(void) pm_ctlops(NULL, dip,
7024 			    DDI_CTLOPS_POWER, &power_req, &result);
7025 #endif
7026 			cp->ppc_ppm = NULL;
7027 		}
7028 		break;
7029 
7030 	default:
7031 		break;
7032 	}
7033 }
7034 
7035 /*
7036  * Dip is either a leaf node that exported "no-involuntary-power-cycles" prop.,
7037  * (if devi_pm_noinvol count is 0) or an ancestor of such a node.  We need to
7038  * make an entry to record the details, which includes certain flag settings.
7039  */
7040 static void
7041 pm_record_invol_path(char *path, int flags, int noinvolpm, int volpmd,
7042     int wasvolpmd, major_t major)
7043 {
7044 	PMD_FUNC(pmf, "record_invol_path")
7045 	major_t pm_path_to_major(char *);
7046 	size_t plen;
7047 	pm_noinvol_t *ip, *np, *pp;
7048 	pp = NULL;
7049 
7050 	plen = strlen(path) + 1;
7051 	np = kmem_zalloc(sizeof (*np), KM_SLEEP);
7052 	np->ni_size = plen;
7053 	np->ni_path = kmem_alloc(plen, KM_SLEEP);
7054 	np->ni_noinvolpm = noinvolpm;
7055 	np->ni_volpmd = volpmd;
7056 	np->ni_wasvolpmd = wasvolpmd;
7057 	np->ni_flags = flags;
7058 	(void) strcpy(np->ni_path, path);
7059 	/*
7060 	 * If we haven't actually seen the node attached, it is hard to figure
7061 	 * out its major.  If we could hold the node by path, we would be much
7062 	 * happier here.
7063 	 */
7064 	if (major == (major_t)-1) {
7065 		np->ni_major = pm_path_to_major(path);
7066 	} else {
7067 		np->ni_major = major;
7068 	}
7069 	rw_enter(&pm_noinvol_rwlock, RW_WRITER);
7070 	for (ip = pm_noinvol_head; ip; pp = ip, ip = ip->ni_next) {
7071 		int comp = strcmp(path, ip->ni_path);
7072 		if (comp < 0) {
7073 			PMD(PMD_NOINVOL, ("%s: %s insert before %s\n",
7074 			    pmf, path, ip->ni_path))
7075 			/* insert before current entry */
7076 			np->ni_next = ip;
7077 			if (pp) {
7078 				pp->ni_next = np;
7079 			} else {
7080 				pm_noinvol_head = np;
7081 			}
7082 			rw_exit(&pm_noinvol_rwlock);
7083 #ifdef DEBUG
7084 			if (pm_debug & PMD_NOINVOL)
7085 				pr_noinvol("record_invol_path exit0");
7086 #endif
7087 			return;
7088 		} else if (comp == 0) {
7089 			panic("%s already in pm_noinvol list", path);
7090 		}
7091 	}
7092 	/*
7093 	 * If we did not find an entry in the list that this should go before,
7094 	 * then it must go at the end
7095 	 */
7096 	if (pp) {
7097 		PMD(PMD_NOINVOL, ("%s: %s append after %s\n", pmf, path,
7098 		    pp->ni_path))
7099 		ASSERT(pp->ni_next == 0);
7100 		pp->ni_next = np;
7101 	} else {
7102 		PMD(PMD_NOINVOL, ("%s: %s added to end-of-list\n", pmf, path))
7103 		ASSERT(!pm_noinvol_head);
7104 		pm_noinvol_head = np;
7105 	}
7106 	rw_exit(&pm_noinvol_rwlock);
7107 #ifdef DEBUG
7108 	if (pm_debug & PMD_NOINVOL)
7109 		pr_noinvol("record_invol_path exit");
7110 #endif
7111 }
7112 
7113 void
7114 pm_record_invol(dev_info_t *dip)
7115 {
7116 	char *pathbuf;
7117 	int pm_all_components_off(dev_info_t *);
7118 	int volpmd = (PM_NUMCMPTS(dip) > 0) && pm_all_components_off(dip);
7119 
7120 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7121 	(void) ddi_pathname(dip, pathbuf);
7122 
7123 	pm_record_invol_path(pathbuf, (DEVI(dip)->devi_pm_flags &
7124 	    (PMC_NO_INVOL | PMC_CONSOLE_FB)), DEVI(dip)->devi_pm_noinvolpm,
7125 	    DEVI(dip)->devi_pm_volpmd, volpmd, PM_MAJOR(dip));
7126 
7127 	/*
7128 	 * If this child's detach will be holding up its ancestors, then we
7129 	 * allow for an exception to that if all children of this type have
7130 	 * gone down voluntarily.
7131 	 * Now walk down the tree incrementing devi_pm_noinvolpm
7132 	 */
7133 	(void) pm_noinvol_update(PM_BP_NOINVOL_DETACH, 0, volpmd, pathbuf,
7134 	    dip);
7135 	kmem_free(pathbuf, MAXPATHLEN);
7136 }
7137 
7138 void
7139 pm_post_detach(pm_ppm_cookie_t *cp, int ret)
7140 {
7141 	dev_info_t *dip = cp->ppc_dip;
7142 	int result;
7143 	power_req_t power_req;
7144 
7145 	switch (cp->ppc_cmd) {
7146 	case DDI_DETACH:
7147 		if (cp->ppc_ppm) {	/* if ppm driver claims the node */
7148 			power_req.request_type = PMR_PPM_POST_DETACH;
7149 			power_req.req.ppm_config_req.who = cp->ppc_dip;
7150 			power_req.req.ppm_config_req.result = ret;
7151 			ASSERT(PPM(cp->ppc_dip) == cp->ppc_ppm);
7152 			(void) pm_ctlops(cp->ppc_ppm, cp->ppc_dip,
7153 			    DDI_CTLOPS_POWER, &power_req, &result);
7154 		}
7155 #ifdef DEBUG
7156 		else {
7157 			power_req.request_type = PMR_PPM_POST_DETACH;
7158 			power_req.req.ppm_config_req.who = cp->ppc_dip;
7159 			power_req.req.ppm_config_req.result = ret;
7160 			(void) pm_ctlops(NULL, cp->ppc_dip,
7161 			    DDI_CTLOPS_POWER, &power_req, &result);
7162 		}
7163 #endif
7164 		if (ret == DDI_SUCCESS) {
7165 			/*
7166 			 * For hotplug detach we assume it is *really* gone
7167 			 */
7168 			if (cp->ppc_cmd == DDI_DETACH &&
7169 			    ((DEVI(dip)->devi_pm_flags &
7170 			    (PMC_NO_INVOL | PMC_CONSOLE_FB)) ||
7171 			    DEVI(dip)->devi_pm_noinvolpm))
7172 				pm_record_invol(dip);
7173 			DEVI(dip)->devi_pm_flags &=
7174 			    ~(PMC_NO_INVOL | PMC_NOINVOL_DONE);
7175 
7176 			/*
7177 			 * If console fb is detaching, then we don't need to
7178 			 * worry any more about it going off (pm_detaching has
7179 			 * brought up all components)
7180 			 */
7181 			if (PM_IS_CFB(dip)) {
7182 				mutex_enter(&pm_cfb_lock);
7183 				ASSERT(cfb_dip_detaching);
7184 				ASSERT(cfb_dip == NULL);
7185 				ASSERT(pm_cfb_comps_off == 0);
7186 				cfb_dip_detaching = NULL;
7187 				mutex_exit(&pm_cfb_lock);
7188 			}
7189 			pm_stop(dip);	/* make it permanent */
7190 		} else {
7191 			if (PM_IS_CFB(dip)) {
7192 				mutex_enter(&pm_cfb_lock);
7193 				ASSERT(cfb_dip_detaching);
7194 				ASSERT(cfb_dip == NULL);
7195 				ASSERT(pm_cfb_comps_off == 0);
7196 				cfb_dip = cfb_dip_detaching;
7197 				cfb_dip_detaching = NULL;
7198 				mutex_exit(&pm_cfb_lock);
7199 			}
7200 			pm_detach_failed(dip);	/* resume power management */
7201 		}
7202 		break;
7203 	case DDI_PM_SUSPEND:
7204 		break;
7205 	case DDI_SUSPEND:
7206 		break;				/* legal, but nothing to do */
7207 	default:
7208 #ifdef DEBUG
7209 		panic("pm_post_detach: unrecognized cmd %d for detach",
7210 		    cp->ppc_cmd);
7211 		/*NOTREACHED*/
7212 #else
7213 		break;
7214 #endif
7215 	}
7216 }
7217 
7218 /*
7219  * Called after vfs_mountroot has got the clock started to fix up timestamps
7220  * that were set when root bush drivers attached.  hresttime was 0 then, so the
7221  * devices look busy but have a 0 busycnt
7222  */
7223 int
7224 pm_adjust_timestamps(dev_info_t *dip, void *arg)
7225 {
7226 	_NOTE(ARGUNUSED(arg))
7227 
7228 	pm_info_t *info = PM_GET_PM_INFO(dip);
7229 	struct pm_component *cp;
7230 	int i;
7231 
7232 	if (!info)
7233 		return (DDI_WALK_CONTINUE);
7234 	PM_LOCK_BUSY(dip);
7235 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
7236 		cp = PM_CP(dip, i);
7237 		if (cp->pmc_timestamp == 0 && cp->pmc_busycount == 0)
7238 			cp->pmc_timestamp = gethrestime_sec();
7239 	}
7240 	PM_UNLOCK_BUSY(dip);
7241 	return (DDI_WALK_CONTINUE);
7242 }
7243 
7244 /*
7245  * Called at attach time to see if the device being attached has a record in
7246  * the no involuntary power cycles list.  If so, we do some bookkeeping on the
7247  * parents and set a flag in the dip
7248  */
7249 void
7250 pm_noinvol_specd(dev_info_t *dip)
7251 {
7252 	PMD_FUNC(pmf, "noinvol_specd")
7253 	char *pathbuf;
7254 	pm_noinvol_t *ip, *pp = NULL;
7255 	int wasvolpmd;
7256 	int found = 0;
7257 
7258 	if (DEVI(dip)->devi_pm_flags & PMC_NOINVOL_DONE)
7259 		return;
7260 	DEVI(dip)->devi_pm_flags |=  PMC_NOINVOL_DONE;
7261 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7262 	(void) ddi_pathname(dip, pathbuf);
7263 
7264 	PM_LOCK_DIP(dip);
7265 	DEVI(dip)->devi_pm_volpmd = 0;
7266 	DEVI(dip)->devi_pm_noinvolpm = 0;
7267 	rw_enter(&pm_noinvol_rwlock, RW_READER);
7268 	for (ip = pm_noinvol_head; ip; pp = ip, ip = ip->ni_next) {
7269 		PMD(PMD_NOINVOL, ("%s: comparing '%s' to '%s'\n",
7270 		    pmf, pathbuf, ip->ni_path))
7271 		if (strcmp(pathbuf, ip->ni_path) == 0) {
7272 			found++;
7273 			break;
7274 		}
7275 	}
7276 	rw_exit(&pm_noinvol_rwlock);
7277 	if (!found) {
7278 		PM_UNLOCK_DIP(dip);
7279 		kmem_free(pathbuf, MAXPATHLEN);
7280 		return;
7281 	}
7282 	rw_enter(&pm_noinvol_rwlock, RW_WRITER);
7283 	pp = NULL;
7284 	for (ip = pm_noinvol_head; ip; pp = ip, ip = ip->ni_next) {
7285 		PMD(PMD_NOINVOL, ("%s: comparing '%s' to '%s'\n",
7286 		    pmf, pathbuf, ip->ni_path))
7287 		if (strcmp(pathbuf, ip->ni_path) == 0) {
7288 			ip->ni_flags &= ~PMC_DRIVER_REMOVED;
7289 			DEVI(dip)->devi_pm_flags |= ip->ni_flags;
7290 			/*
7291 			 * Handle special case of console fb
7292 			 */
7293 			if (PM_IS_CFB(dip)) {
7294 				mutex_enter(&pm_cfb_lock);
7295 				cfb_dip = dip;
7296 				PMD(PMD_CFB, ("%s: %s@%s(%s#%d) setting "
7297 				    "cfb_dip\n", pmf, PM_DEVICE(dip)))
7298 				mutex_exit(&pm_cfb_lock);
7299 			}
7300 			DEVI(dip)->devi_pm_noinvolpm = ip->ni_noinvolpm;
7301 			ASSERT((DEVI(dip)->devi_pm_flags &
7302 			    (PMC_NO_INVOL | PMC_CONSOLE_FB)) ||
7303 			    DEVI(dip)->devi_pm_noinvolpm);
7304 			DEVI(dip)->devi_pm_volpmd = ip->ni_volpmd;
7305 			PMD(PMD_NOINVOL, ("%s: noinvol=%d, volpmd=%d, "
7306 			    "wasvolpmd=%d, flags=%x, path=%s\n", pmf,
7307 			    ip->ni_noinvolpm, ip->ni_volpmd,
7308 			    ip->ni_wasvolpmd, ip->ni_flags, ip->ni_path))
7309 			/*
7310 			 * free the entry in hopes the list will now be empty
7311 			 * and we won't have to search it any more until the
7312 			 * device detaches
7313 			 */
7314 			if (pp) {
7315 				PMD(PMD_NOINVOL, ("%s: free %s, prev %s\n",
7316 				    pmf, ip->ni_path, pp->ni_path))
7317 				pp->ni_next = ip->ni_next;
7318 			} else {
7319 				PMD(PMD_NOINVOL, ("%s: free %s head\n",
7320 				    pmf, ip->ni_path))
7321 				ASSERT(pm_noinvol_head == ip);
7322 				pm_noinvol_head = ip->ni_next;
7323 			}
7324 			PM_UNLOCK_DIP(dip);
7325 			wasvolpmd = ip->ni_wasvolpmd;
7326 			rw_exit(&pm_noinvol_rwlock);
7327 			kmem_free(ip->ni_path, ip->ni_size);
7328 			kmem_free(ip, sizeof (*ip));
7329 			/*
7330 			 * Now walk up the tree decrementing devi_pm_noinvolpm
7331 			 * (and volpmd if appropriate)
7332 			 */
7333 			(void) pm_noinvol_update(PM_BP_NOINVOL_ATTACH, 0,
7334 			    wasvolpmd, pathbuf, dip);
7335 #ifdef DEBUG
7336 			if (pm_debug & PMD_NOINVOL)
7337 				pr_noinvol("noinvol_specd exit");
7338 #endif
7339 			kmem_free(pathbuf, MAXPATHLEN);
7340 			return;
7341 		}
7342 	}
7343 	kmem_free(pathbuf, MAXPATHLEN);
7344 	rw_exit(&pm_noinvol_rwlock);
7345 	PM_UNLOCK_DIP(dip);
7346 }
7347 
7348 int
7349 pm_all_components_off(dev_info_t *dip)
7350 {
7351 	int i;
7352 	pm_component_t *cp;
7353 
7354 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
7355 		cp = PM_CP(dip, i);
7356 		if (cp->pmc_cur_pwr == PM_LEVEL_UNKNOWN ||
7357 		    cp->pmc_comp.pmc_lvals[cp->pmc_cur_pwr])
7358 			return (0);
7359 	}
7360 	return (1);	/* all off */
7361 }
7362 
7363 /*
7364  * Make sure that all "no involuntary power cycles" devices are attached.
7365  * Called before doing a cpr suspend to make sure the driver has a say about
7366  * the power cycle
7367  */
7368 int
7369 pm_reattach_noinvol(void)
7370 {
7371 	PMD_FUNC(pmf, "reattach_noinvol")
7372 	pm_noinvol_t *ip;
7373 	char *path;
7374 	dev_info_t *dip;
7375 
7376 	/*
7377 	 * Prevent the modunload thread from unloading any modules until we
7378 	 * have completely stopped all kernel threads.
7379 	 */
7380 	modunload_disable();
7381 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next) {
7382 		/*
7383 		 * Forget we'v ever seen any entry
7384 		 */
7385 		ip->ni_persistent = 0;
7386 	}
7387 restart:
7388 	rw_enter(&pm_noinvol_rwlock, RW_READER);
7389 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next) {
7390 		major_t maj;
7391 		maj = ip->ni_major;
7392 		path = ip->ni_path;
7393 		if (path != NULL && !(ip->ni_flags & PMC_DRIVER_REMOVED)) {
7394 			if (ip->ni_persistent) {
7395 				/*
7396 				 * If we weren't able to make this entry
7397 				 * go away, then we give up, as
7398 				 * holding/attaching the driver ought to have
7399 				 * resulted in this entry being deleted
7400 				 */
7401 				PMD(PMD_NOINVOL, ("%s: can't reattach %s "
7402 				    "(%s|%d)\n", pmf, ip->ni_path,
7403 				    ddi_major_to_name(maj), (int)maj))
7404 				cmn_err(CE_WARN, "cpr: unable to reattach %s ",
7405 				    ip->ni_path);
7406 				modunload_enable();
7407 				rw_exit(&pm_noinvol_rwlock);
7408 				return (0);
7409 			}
7410 			ip->ni_persistent++;
7411 			rw_exit(&pm_noinvol_rwlock);
7412 			PMD(PMD_NOINVOL, ("%s: holding %s\n", pmf, path))
7413 			dip = e_ddi_hold_devi_by_path(path, 0);
7414 			if (dip == NULL) {
7415 				PMD(PMD_NOINVOL, ("%s: can't hold (%s|%d)\n",
7416 				    pmf, path, (int)maj))
7417 				cmn_err(CE_WARN, "cpr: unable to hold %s "
7418 				    "driver", path);
7419 				modunload_enable();
7420 				return (0);
7421 			} else {
7422 				PMD(PMD_DHR, ("%s: release %s\n", pmf, path))
7423 				/*
7424 				 * Since the modunload thread is stopped, we
7425 				 * don't have to keep the driver held, which
7426 				 * saves a ton of bookkeeping
7427 				 */
7428 				ddi_release_devi(dip);
7429 				goto restart;
7430 			}
7431 		} else {
7432 			PMD(PMD_NOINVOL, ("%s: skip %s; unknown major\n",
7433 			    pmf, ip->ni_path))
7434 			continue;
7435 		}
7436 	}
7437 	rw_exit(&pm_noinvol_rwlock);
7438 	return (1);
7439 }
7440 
7441 void
7442 pm_reattach_noinvol_fini(void)
7443 {
7444 	modunload_enable();
7445 }
7446 
7447 /*
7448  * Display pm support code
7449  */
7450 
7451 
7452 /*
7453  * console frame-buffer power-mgmt gets enabled when debugging
7454  * services are not present or console fbpm override is set
7455  */
7456 void
7457 pm_cfb_setup(const char *stdout_path)
7458 {
7459 	PMD_FUNC(pmf, "cfb_setup")
7460 	extern int obpdebug;
7461 	char *devname;
7462 	dev_info_t *dip;
7463 	int devname_len;
7464 	extern dev_info_t *fbdip;
7465 
7466 	/*
7467 	 * By virtue of this function being called (from consconfig),
7468 	 * we know stdout is a framebuffer.
7469 	 */
7470 	stdout_is_framebuffer = 1;
7471 
7472 	if (obpdebug || (boothowto & RB_DEBUG)) {
7473 		if (pm_cfb_override == 0) {
7474 			/*
7475 			 * Console is frame buffer, but we want to suppress
7476 			 * pm on it because of debugging setup
7477 			 */
7478 			pm_cfb_enabled = 0;
7479 			cmn_err(CE_NOTE, "Kernel debugger present: disabling "
7480 			    "console power management.");
7481 			/*
7482 			 * however, we still need to know which is the console
7483 			 * fb in order to suppress pm on it
7484 			 */
7485 		} else {
7486 			cmn_err(CE_WARN, "Kernel debugger present: see "
7487 			    "kmdb(1M) for interaction with power management.");
7488 		}
7489 	}
7490 #ifdef DEBUG
7491 	/*
7492 	 * IF console is fb and is power managed, don't do prom_printfs from
7493 	 * pm debug macro
7494 	 */
7495 	if (pm_cfb_enabled) {
7496 		if (pm_debug)
7497 			prom_printf("pm debug output will be to log only\n");
7498 		pm_divertdebug++;
7499 	}
7500 #endif
7501 	devname = i_ddi_strdup((char *)stdout_path, KM_SLEEP);
7502 	devname_len = strlen(devname) + 1;
7503 	PMD(PMD_CFB, ("%s: stripped %s\n", pmf, devname))
7504 	/* if the driver is attached */
7505 	if ((dip = fbdip) != NULL) {
7506 		PMD(PMD_CFB, ("%s: attached: %s@%s(%s#%d)\n", pmf,
7507 		    PM_DEVICE(dip)))
7508 		/*
7509 		 * We set up here as if the driver were power manageable in case
7510 		 * we get a later attach of a pm'able driver (which would result
7511 		 * in a panic later)
7512 		 */
7513 		cfb_dip = dip;
7514 		DEVI(dip)->devi_pm_flags |= (PMC_CONSOLE_FB | PMC_NO_INVOL);
7515 		PMD(PMD_CFB, ("%s: cfb_dip -> %s@%s(%s#%d)\n", pmf,
7516 		    PM_DEVICE(dip)))
7517 #ifdef DEBUG
7518 		if (!(PM_GET_PM_INFO(dip) != NULL && PM_NUMCMPTS(dip))) {
7519 			PMD(PMD_CFB, ("%s: %s@%s(%s#%d) not power-managed\n",
7520 			    pmf, PM_DEVICE(dip)))
7521 		}
7522 #endif
7523 	} else {
7524 		char *ep;
7525 		PMD(PMD_CFB, ("%s: pntd %s failed\n", pmf, devname))
7526 		pm_record_invol_path(devname,
7527 		    (PMC_CONSOLE_FB | PMC_NO_INVOL), 1, 0, 0,
7528 		    (major_t)-1);
7529 		for (ep = strrchr(devname, '/'); ep != devname;
7530 		    ep = strrchr(devname, '/')) {
7531 			PMD(PMD_CFB, ("%s: devname %s\n", pmf, devname))
7532 			*ep = '\0';
7533 			dip = pm_name_to_dip(devname, 0);
7534 			if (dip != NULL) {
7535 				/*
7536 				 * Walk up the tree incrementing
7537 				 * devi_pm_noinvolpm
7538 				 */
7539 				(void) pm_noinvol_update(PM_BP_NOINVOL_CFB,
7540 				    0, 0, devname, dip);
7541 				break;
7542 			} else {
7543 				pm_record_invol_path(devname,
7544 				    PMC_NO_INVOL, 1, 0, 0, (major_t)-1);
7545 			}
7546 		}
7547 	}
7548 	kmem_free(devname, devname_len);
7549 }
7550 
7551 void
7552 pm_cfb_rele(void)
7553 {
7554 	mutex_enter(&pm_cfb_lock);
7555 	/*
7556 	 * this call isn't using the console any  more, it is ok to take it
7557 	 * down if the count goes to 0
7558 	 */
7559 	cfb_inuse--;
7560 	mutex_exit(&pm_cfb_lock);
7561 }
7562 
7563 /*
7564  * software interrupt handler for fbpm; this function exists because we can't
7565  * bring up the frame buffer power from above lock level.  So if we need to,
7566  * we instead schedule a softint that runs this routine and takes us into
7567  * debug_enter (a bit delayed from the original request, but avoiding a panic).
7568  */
7569 static uint_t
7570 pm_cfb_softint(caddr_t int_handler_arg)
7571 {
7572 	_NOTE(ARGUNUSED(int_handler_arg))
7573 	int rval = DDI_INTR_UNCLAIMED;
7574 
7575 	mutex_enter(&pm_cfb_lock);
7576 	if (pm_soft_pending) {
7577 		mutex_exit(&pm_cfb_lock);
7578 		debug_enter((char *)NULL);
7579 		/* acquired in debug_enter before calling pm_cfb_trigger */
7580 		pm_cfb_rele();
7581 		mutex_enter(&pm_cfb_lock);
7582 		pm_soft_pending = 0;
7583 		mutex_exit(&pm_cfb_lock);
7584 		rval = DDI_INTR_CLAIMED;
7585 	} else
7586 		mutex_exit(&pm_cfb_lock);
7587 
7588 	return (rval);
7589 }
7590 
7591 void
7592 pm_cfb_setup_intr(void)
7593 {
7594 	PMD_FUNC(pmf, "cfb_setup_intr")
7595 	extern void prom_set_outfuncs(void (*)(void), void (*)(void));
7596 	void pm_cfb_check_and_powerup(void);
7597 
7598 	if (!stdout_is_framebuffer) {
7599 		PMD(PMD_CFB, ("%s: console not fb\n", pmf))
7600 		return;
7601 	}
7602 	mutex_init(&pm_cfb_lock, NULL, MUTEX_SPIN, (void *)ipltospl(SPL8));
7603 #ifdef DEBUG
7604 	mutex_init(&pm_debug_lock, NULL, MUTEX_SPIN, (void *)ipltospl(SPL8));
7605 #endif
7606 	/*
7607 	 * setup software interrupt handler
7608 	 */
7609 	if (ddi_add_softintr(ddi_root_node(), DDI_SOFTINT_HIGH, &pm_soft_id,
7610 	    NULL, NULL, pm_cfb_softint, NULL) != DDI_SUCCESS)
7611 		panic("pm: unable to register soft intr.");
7612 
7613 	prom_set_outfuncs(pm_cfb_check_and_powerup, pm_cfb_rele);
7614 }
7615 
7616 /*
7617  * Checks to see if it is safe to write to the console wrt power management
7618  * (i.e. if the console is a framebuffer, then it must be at full power)
7619  * returns 1 when power is off (power-up is needed)
7620  * returns 0 when power is on (power-up not needed)
7621  */
7622 int
7623 pm_cfb_check_and_hold(void)
7624 {
7625 	/*
7626 	 * cfb_dip is set iff console is a power manageable frame buffer
7627 	 * device
7628 	 */
7629 	extern int modrootloaded;
7630 
7631 	mutex_enter(&pm_cfb_lock);
7632 	cfb_inuse++;
7633 	ASSERT(cfb_inuse);	/* wrap? */
7634 	if (modrootloaded && cfb_dip) {
7635 		/*
7636 		 * don't power down the frame buffer, the prom is using it
7637 		 */
7638 		if (pm_cfb_comps_off) {
7639 			mutex_exit(&pm_cfb_lock);
7640 			return (1);
7641 		}
7642 	}
7643 	mutex_exit(&pm_cfb_lock);
7644 	return (0);
7645 }
7646 
7647 /*
7648  * turn on cfb power (which is known to be off).
7649  * Must be called below lock level!
7650  */
7651 void
7652 pm_cfb_powerup(void)
7653 {
7654 	pm_info_t *info;
7655 	int norm;
7656 	int ccount, ci;
7657 	int unused;
7658 #ifdef DEBUG
7659 	/*
7660 	 * Can't reenter prom_prekern, so suppress pm debug messages
7661 	 * (still go to circular buffer).
7662 	 */
7663 	mutex_enter(&pm_debug_lock);
7664 	pm_divertdebug++;
7665 	mutex_exit(&pm_debug_lock);
7666 #endif
7667 	info = PM_GET_PM_INFO(cfb_dip);
7668 	ASSERT(info);
7669 
7670 	ccount = PM_NUMCMPTS(cfb_dip);
7671 	for (ci = 0; ci < ccount; ci++) {
7672 		norm = pm_get_normal_power(cfb_dip, ci);
7673 		(void) pm_set_power(cfb_dip, ci, norm, PM_LEVEL_UPONLY,
7674 		    PM_CANBLOCK_BYPASS, 0, &unused);
7675 	}
7676 #ifdef DEBUG
7677 	mutex_enter(&pm_debug_lock);
7678 	pm_divertdebug--;
7679 	mutex_exit(&pm_debug_lock);
7680 #endif
7681 }
7682 
7683 /*
7684  * Check if the console framebuffer is powered up.  If not power it up.
7685  * Note: Calling pm_cfb_check_and_hold has put a hold on the power state which
7686  * must be released by calling pm_cfb_rele when the console fb operation
7687  * is completed.
7688  */
7689 void
7690 pm_cfb_check_and_powerup(void)
7691 {
7692 	if (pm_cfb_check_and_hold())
7693 		pm_cfb_powerup();
7694 }
7695 
7696 /*
7697  * Trigger a low level interrupt to power up console frame buffer.
7698  */
7699 void
7700 pm_cfb_trigger(void)
7701 {
7702 	if (cfb_dip == NULL)
7703 		return;
7704 
7705 	mutex_enter(&pm_cfb_lock);
7706 	/*
7707 	 * If machine appears to be hung, pulling the keyboard connector of
7708 	 * the console will cause a high level interrupt and go to debug_enter.
7709 	 * But, if the fb is powered down, this routine will be called to bring
7710 	 * it up (by generating a softint to do the work).  If soft interrupts
7711 	 * are not running, and the keyboard connector is pulled again, the
7712 	 * following code detects this condition and calls panic which allows
7713 	 * the fb to be brought up from high level.
7714 	 *
7715 	 * If two nearly simultaneous calls to debug_enter occur (both from
7716 	 * high level) the code described above will cause a panic.
7717 	 */
7718 	if (lbolt <= pm_soft_pending) {
7719 		panicstr = "pm_cfb_trigger: lbolt not advancing";
7720 		panic(panicstr);	/* does a power up at any intr level */
7721 		/* NOTREACHED */
7722 	}
7723 	pm_soft_pending = lbolt;
7724 	mutex_exit(&pm_cfb_lock);
7725 	ddi_trigger_softintr(pm_soft_id);
7726 }
7727 
7728 major_t
7729 pm_path_to_major(char *path)
7730 {
7731 	PMD_FUNC(pmf, "path_to_major")
7732 	char *np, *ap, *bp;
7733 	major_t ret;
7734 	size_t len;
7735 	static major_t i_path_to_major(char *, char *);
7736 
7737 	PMD(PMD_NOINVOL, ("%s: %s\n", pmf, path))
7738 
7739 	np = strrchr(path, '/');
7740 	if (np != NULL)
7741 		np++;
7742 	else
7743 		np = path;
7744 	len = strlen(np) + 1;
7745 	bp = kmem_alloc(len, KM_SLEEP);
7746 	(void) strcpy(bp, np);
7747 	if ((ap = strchr(bp, '@')) != NULL) {
7748 		*ap = '\0';
7749 	}
7750 	PMD(PMD_NOINVOL, ("%s: %d\n", pmf, ddi_name_to_major(np)))
7751 	ret = i_path_to_major(path, np);
7752 	kmem_free(bp, len);
7753 	return (ret);
7754 }
7755 
7756 #ifdef DEBUG
7757 
7758 char *pm_msgp;
7759 char *pm_bufend;
7760 char *pm_msgbuf = NULL;
7761 int   pm_logpages = 2;
7762 
7763 #define	PMLOGPGS	pm_logpages
7764 
7765 /*PRINTFLIKE1*/
7766 void
7767 pm_log(const char *fmt, ...)
7768 {
7769 	va_list adx;
7770 	size_t size;
7771 
7772 	mutex_enter(&pm_debug_lock);
7773 	if (pm_msgbuf == NULL) {
7774 		pm_msgbuf = kmem_zalloc(mmu_ptob(PMLOGPGS), KM_SLEEP);
7775 		pm_bufend = pm_msgbuf + mmu_ptob(PMLOGPGS) - 1;
7776 		pm_msgp = pm_msgbuf;
7777 	}
7778 	va_start(adx, fmt);
7779 	size = vsnprintf(NULL, 0, fmt, adx) + 1;
7780 	va_end(adx);
7781 	va_start(adx, fmt);
7782 	if (size > (pm_bufend - pm_msgp)) {		/* wraps */
7783 		bzero(pm_msgp, pm_bufend - pm_msgp);
7784 		(void) vsnprintf(pm_msgbuf, size, fmt, adx);
7785 		if (!pm_divertdebug)
7786 			prom_printf("%s", pm_msgp);
7787 		pm_msgp = pm_msgbuf + size;
7788 	} else {
7789 		(void) vsnprintf(pm_msgp, size, fmt, adx);
7790 		if (!pm_divertdebug)
7791 			prom_printf("%s", pm_msgp);
7792 		pm_msgp += size;
7793 	}
7794 	va_end(adx);
7795 	mutex_exit(&pm_debug_lock);
7796 }
7797 #endif	/* DEBUG */
7798 
7799 /*
7800  * We want to save the state of any directly pm'd devices over the suspend/
7801  * resume process so that we can put them back the way the controlling
7802  * process left them.
7803  */
7804 void
7805 pm_save_direct_levels(void)
7806 {
7807 	pm_processes_stopped = 1;
7808 	ddi_walk_devs(ddi_root_node(), pm_save_direct_lvl_walk, 0);
7809 }
7810 
7811 static int
7812 pm_save_direct_lvl_walk(dev_info_t *dip, void *arg)
7813 {
7814 	_NOTE(ARGUNUSED(arg))
7815 	int i;
7816 	int *ip;
7817 	pm_info_t *info = PM_GET_PM_INFO(dip);
7818 
7819 	if (!info)
7820 		return (DDI_WALK_CONTINUE);
7821 
7822 	if (PM_ISDIRECT(dip) && !PM_ISBC(dip)) {
7823 		if (PM_NUMCMPTS(dip) > 2) {
7824 			info->pmi_lp = kmem_alloc(PM_NUMCMPTS(dip) *
7825 			    sizeof (int), KM_SLEEP);
7826 			ip = info->pmi_lp;
7827 		} else {
7828 			ip = info->pmi_levels;
7829 		}
7830 		/* autopm and processes are stopped, ok not to lock power */
7831 		for (i = 0; i < PM_NUMCMPTS(dip); i++)
7832 			*ip++ = PM_CURPOWER(dip, i);
7833 		/*
7834 		 * There is a small window between stopping the
7835 		 * processes and setting pm_processes_stopped where
7836 		 * a driver could get hung up in a pm_raise_power()
7837 		 * call.  Free any such driver now.
7838 		 */
7839 		pm_proceed(dip, PMP_RELEASE, -1, -1);
7840 	}
7841 
7842 	return (DDI_WALK_CONTINUE);
7843 }
7844 
7845 void
7846 pm_restore_direct_levels(void)
7847 {
7848 	/*
7849 	 * If cpr didn't call pm_save_direct_levels, (because stopping user
7850 	 * threads failed) then we don't want to try to restore them
7851 	 */
7852 	if (!pm_processes_stopped)
7853 		return;
7854 
7855 	ddi_walk_devs(ddi_root_node(), pm_restore_direct_lvl_walk, 0);
7856 	pm_processes_stopped = 0;
7857 }
7858 
7859 static int
7860 pm_restore_direct_lvl_walk(dev_info_t *dip, void *arg)
7861 {
7862 	_NOTE(ARGUNUSED(arg))
7863 	PMD_FUNC(pmf, "restore_direct_lvl_walk")
7864 	int i, nc, result;
7865 	int *ip;
7866 
7867 	pm_info_t *info = PM_GET_PM_INFO(dip);
7868 	if (!info)
7869 		return (DDI_WALK_CONTINUE);
7870 
7871 	if (PM_ISDIRECT(dip) && !PM_ISBC(dip)) {
7872 		if ((nc = PM_NUMCMPTS(dip)) > 2) {
7873 			ip = &info->pmi_lp[nc - 1];
7874 		} else {
7875 			ip = &info->pmi_levels[nc - 1];
7876 		}
7877 		/*
7878 		 * Because fb drivers fail attempts to turn off the
7879 		 * fb when the monitor is on, but treat a request to
7880 		 * turn on the monitor as a request to turn on the
7881 		 * fb too, we process components in descending order
7882 		 * Because autopm is disabled and processes aren't
7883 		 * running, it is ok to examine current power outside
7884 		 * of the power lock
7885 		 */
7886 		for (i = nc - 1; i >= 0; i--, ip--) {
7887 			if (PM_CURPOWER(dip, i) == *ip)
7888 				continue;
7889 			if (pm_set_power(dip, i, *ip, PM_LEVEL_EXACT,
7890 			    PM_CANBLOCK_BYPASS, 0, &result) !=
7891 				DDI_SUCCESS) {
7892 				cmn_err(CE_WARN, "cpr: unable "
7893 				    "to restore power level of "
7894 				    "component %d of directly "
7895 				    "power manged device %s@%s"
7896 				    " to %d",
7897 				    i, PM_NAME(dip),
7898 				    PM_ADDR(dip), *ip);
7899 				PMD(PMD_FAIL, ("%s: failed to restore "
7900 				    "%s@%s(%s#%d)[%d] exact(%d)->%d, "
7901 				    "errno %d\n", pmf, PM_DEVICE(dip), i,
7902 				    PM_CURPOWER(dip, i), *ip, result))
7903 			}
7904 		}
7905 		if (nc > 2) {
7906 			kmem_free(info->pmi_lp, nc * sizeof (int));
7907 			info->pmi_lp = NULL;
7908 		}
7909 	}
7910 	return (DDI_WALK_CONTINUE);
7911 }
7912 
7913 /*
7914  * Stolen from the bootdev module
7915  * attempt to convert a path to a major number
7916  */
7917 static major_t
7918 i_path_to_major(char *path, char *leaf_name)
7919 {
7920 	extern major_t path_to_major(char *pathname);
7921 	major_t maj;
7922 
7923 	if ((maj = path_to_major(path)) == (major_t)-1) {
7924 		maj = ddi_name_to_major(leaf_name);
7925 	}
7926 
7927 	return (maj);
7928 }
7929 
7930 /*
7931  * When user calls rem_drv, we need to forget no-involuntary-power-cycles state
7932  * An entry in the list means that the device is detached, so we need to
7933  * adjust its ancestors as if they had just seen this attach, and any detached
7934  * ancestors need to have their list entries adjusted.
7935  */
7936 void
7937 pm_driver_removed(major_t major)
7938 {
7939 	static void i_pm_driver_removed(major_t major);
7940 
7941 	/*
7942 	 * Serialize removal of drivers. This is to keep ancestors of
7943 	 * a node that is being deleted from getting deleted and added back
7944 	 * with different counters.
7945 	 */
7946 	mutex_enter(&pm_remdrv_lock);
7947 	i_pm_driver_removed(major);
7948 	mutex_exit(&pm_remdrv_lock);
7949 }
7950 
7951 /*
7952  * This routine is called recursively by pm_noinvol_process_ancestors()
7953  */
7954 static void
7955 i_pm_driver_removed(major_t major)
7956 {
7957 	PMD_FUNC(pmf, "driver_removed")
7958 	static void adjust_ancestors(char *, int);
7959 	static int pm_is_noinvol_ancestor(pm_noinvol_t *);
7960 	static void pm_noinvol_process_ancestors(char *);
7961 	pm_noinvol_t *ip, *pp = NULL;
7962 	int wasvolpmd;
7963 	ASSERT(major != (major_t)-1);
7964 	PMD(PMD_NOINVOL, ("%s: %s\n", pmf, ddi_major_to_name(major)))
7965 again:
7966 	rw_enter(&pm_noinvol_rwlock, RW_WRITER);
7967 	for (ip = pm_noinvol_head; ip; pp = ip, ip = ip->ni_next) {
7968 		if (major != ip->ni_major)
7969 			continue;
7970 		/*
7971 		 * If it is an ancestor of no-invol node, which is
7972 		 * not removed, skip it. This is to cover the case of
7973 		 * ancestor removed without removing its descendants.
7974 		 */
7975 		if (pm_is_noinvol_ancestor(ip)) {
7976 			ip->ni_flags |= PMC_DRIVER_REMOVED;
7977 			continue;
7978 		}
7979 		wasvolpmd = ip->ni_wasvolpmd;
7980 		/*
7981 		 * remove the entry from the list
7982 		 */
7983 		if (pp) {
7984 			PMD(PMD_NOINVOL, ("%s: freeing %s, prev is %s\n",
7985 			    pmf, ip->ni_path, pp->ni_path))
7986 			pp->ni_next = ip->ni_next;
7987 		} else {
7988 			PMD(PMD_NOINVOL, ("%s: free %s head\n", pmf,
7989 			    ip->ni_path))
7990 			ASSERT(pm_noinvol_head == ip);
7991 			pm_noinvol_head = ip->ni_next;
7992 		}
7993 		rw_exit(&pm_noinvol_rwlock);
7994 		adjust_ancestors(ip->ni_path, wasvolpmd);
7995 		/*
7996 		 * Had an ancestor been removed before this node, it would have
7997 		 * been skipped. Adjust the no-invol counters for such skipped
7998 		 * ancestors.
7999 		 */
8000 		pm_noinvol_process_ancestors(ip->ni_path);
8001 		kmem_free(ip->ni_path, ip->ni_size);
8002 		kmem_free(ip, sizeof (*ip));
8003 		goto again;
8004 	}
8005 	rw_exit(&pm_noinvol_rwlock);
8006 }
8007 
8008 /*
8009  * returns 1, if *aip is a ancestor of a no-invol node
8010  *	   0, otherwise
8011  */
8012 static int
8013 pm_is_noinvol_ancestor(pm_noinvol_t *aip)
8014 {
8015 	pm_noinvol_t *ip;
8016 
8017 	ASSERT(strlen(aip->ni_path) != 0);
8018 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next) {
8019 		if (ip == aip)
8020 			continue;
8021 		/*
8022 		 * To be an ancestor, the path must be an initial substring of
8023 		 * the descendent, and end just before a '/' in the
8024 		 * descendent's path.
8025 		 */
8026 		if ((strstr(ip->ni_path, aip->ni_path) == ip->ni_path) &&
8027 		    (ip->ni_path[strlen(aip->ni_path)] == '/'))
8028 			return (1);
8029 	}
8030 	return (0);
8031 }
8032 
8033 #define	PM_MAJOR(dip) ddi_name_to_major(ddi_binding_name(dip))
8034 /*
8035  * scan through the pm_noinvolpm list adjusting ancestors of the current
8036  * node;  Modifies string *path.
8037  */
8038 static void
8039 adjust_ancestors(char *path, int wasvolpmd)
8040 {
8041 	PMD_FUNC(pmf, "adjust_ancestors")
8042 	char *cp;
8043 	pm_noinvol_t *lp;
8044 	pm_noinvol_t *pp = NULL;
8045 	major_t locked = (major_t)UINT_MAX;
8046 	dev_info_t *dip;
8047 	char	*pathbuf;
8048 	size_t pathbuflen = strlen(path) + 1;
8049 
8050 	/*
8051 	 * First we look up the ancestor's dip.  If we find it, then we
8052 	 * adjust counts up the tree
8053 	 */
8054 	PMD(PMD_NOINVOL, ("%s: %s wasvolpmd %d\n", pmf, path, wasvolpmd))
8055 	pathbuf = kmem_alloc(pathbuflen, KM_SLEEP);
8056 	(void) strcpy(pathbuf, path);
8057 	cp = strrchr(pathbuf, '/');
8058 	if (cp == NULL)	{
8059 		/* if no ancestors, then nothing to do */
8060 		kmem_free(pathbuf, pathbuflen);
8061 		return;
8062 	}
8063 	*cp = '\0';
8064 	dip = pm_name_to_dip(pathbuf, 1);
8065 	if (dip != NULL) {
8066 		locked = PM_MAJOR(dip);
8067 
8068 		(void) pm_noinvol_update(PM_BP_NOINVOL_REMDRV, 0, wasvolpmd,
8069 		    path, dip);
8070 
8071 		if (locked != (major_t)UINT_MAX)
8072 			ddi_release_devi(dip);
8073 	} else {
8074 		char *apath;
8075 		size_t len = strlen(pathbuf) + 1;
8076 		int  lock_held = 1;
8077 
8078 		/*
8079 		 * Now check for ancestors that exist only in the list
8080 		 */
8081 		apath = kmem_alloc(len, KM_SLEEP);
8082 		(void) strcpy(apath, pathbuf);
8083 		rw_enter(&pm_noinvol_rwlock, RW_WRITER);
8084 		for (lp = pm_noinvol_head; lp; pp = lp, lp = lp->ni_next) {
8085 			/*
8086 			 * This can only happen once.  Since we have to drop
8087 			 * the lock, we need to extract the relevant info.
8088 			 */
8089 			if (strcmp(pathbuf, lp->ni_path) == 0) {
8090 				PMD(PMD_NOINVOL, ("%s: %s no %d -> %d\n", pmf,
8091 				    lp->ni_path, lp->ni_noinvolpm,
8092 				    lp->ni_noinvolpm - 1))
8093 				lp->ni_noinvolpm--;
8094 				if (wasvolpmd && lp->ni_volpmd) {
8095 					PMD(PMD_NOINVOL, ("%s: %s vol %d -> "
8096 					    "%d\n", pmf, lp->ni_path,
8097 					    lp->ni_volpmd, lp->ni_volpmd - 1))
8098 					lp->ni_volpmd--;
8099 				}
8100 				/*
8101 				 * remove the entry from the list, if there
8102 				 * are no more no-invol descendants and node
8103 				 * itself is not a no-invol node.
8104 				 */
8105 				if (!(lp->ni_noinvolpm ||
8106 				    (lp->ni_flags & PMC_NO_INVOL))) {
8107 					ASSERT(lp->ni_volpmd == 0);
8108 					if (pp) {
8109 						PMD(PMD_NOINVOL, ("%s: freeing "
8110 						    "%s, prev is %s\n", pmf,
8111 						    lp->ni_path, pp->ni_path))
8112 						pp->ni_next = lp->ni_next;
8113 					} else {
8114 						PMD(PMD_NOINVOL, ("%s: free %s "
8115 						    "head\n", pmf, lp->ni_path))
8116 						ASSERT(pm_noinvol_head == lp);
8117 						pm_noinvol_head = lp->ni_next;
8118 					}
8119 					lock_held = 0;
8120 					rw_exit(&pm_noinvol_rwlock);
8121 					adjust_ancestors(apath, wasvolpmd);
8122 					/* restore apath */
8123 					(void) strcpy(apath, pathbuf);
8124 					kmem_free(lp->ni_path, lp->ni_size);
8125 					kmem_free(lp, sizeof (*lp));
8126 				}
8127 				break;
8128 			}
8129 		}
8130 		if (lock_held)
8131 			rw_exit(&pm_noinvol_rwlock);
8132 		adjust_ancestors(apath, wasvolpmd);
8133 		kmem_free(apath, len);
8134 	}
8135 	kmem_free(pathbuf, pathbuflen);
8136 }
8137 
8138 /*
8139  * Do no-invol processing for any ancestors i.e. adjust counters of ancestors,
8140  * which were skipped even though their drivers were removed.
8141  */
8142 static void
8143 pm_noinvol_process_ancestors(char *path)
8144 {
8145 	pm_noinvol_t *lp;
8146 
8147 	rw_enter(&pm_noinvol_rwlock, RW_READER);
8148 	for (lp = pm_noinvol_head; lp; lp = lp->ni_next) {
8149 		if (strstr(path, lp->ni_path) &&
8150 		    (lp->ni_flags & PMC_DRIVER_REMOVED)) {
8151 			rw_exit(&pm_noinvol_rwlock);
8152 			i_pm_driver_removed(lp->ni_major);
8153 			return;
8154 		}
8155 	}
8156 	rw_exit(&pm_noinvol_rwlock);
8157 }
8158 
8159 /*
8160  * Returns true if (detached) device needs to be kept up because it exported the
8161  * "no-involuntary-power-cycles" property or we're pretending it did (console
8162  * fb case) or it is an ancestor of such a device and has used up the "one
8163  * free cycle" allowed when all such leaf nodes have voluntarily powered down
8164  * upon detach.  In any event, we need an exact hit on the path or we return
8165  * false.
8166  */
8167 int
8168 pm_noinvol_detached(char *path)
8169 {
8170 	PMD_FUNC(pmf, "noinvol_detached")
8171 	pm_noinvol_t *ip;
8172 	int ret = 0;
8173 
8174 	rw_enter(&pm_noinvol_rwlock, RW_READER);
8175 	for (ip = pm_noinvol_head; ip; ip = ip->ni_next) {
8176 		if (strcmp(path, ip->ni_path) == 0) {
8177 			if (ip->ni_flags & PMC_CONSOLE_FB) {
8178 				PMD(PMD_NOINVOL | PMD_CFB, ("%s: inhibits CFB "
8179 				    "%s\n", pmf, path))
8180 				ret = 1;
8181 				break;
8182 			}
8183 #ifdef	DEBUG
8184 			if (ip->ni_noinvolpm != ip->ni_volpmd)
8185 				PMD(PMD_NOINVOL, ("%s: (%d != %d) inhibits %s"
8186 				    "\n", pmf, ip->ni_noinvolpm, ip->ni_volpmd,
8187 				    path))
8188 #endif
8189 			ret = (ip->ni_noinvolpm != ip->ni_volpmd);
8190 			break;
8191 		}
8192 	}
8193 	rw_exit(&pm_noinvol_rwlock);
8194 	return (ret);
8195 }
8196 
8197 int
8198 pm_is_cfb(dev_info_t *dip)
8199 {
8200 	return (dip == cfb_dip);
8201 }
8202 
8203 #ifdef	DEBUG
8204 /*
8205  * Return true if all components of the console frame buffer are at
8206  * "normal" power, i.e., fully on.  For the case where the console is not
8207  * a framebuffer, we also return true
8208  */
8209 int
8210 pm_cfb_is_up(void)
8211 {
8212 	return (pm_cfb_comps_off == 0);
8213 }
8214 #endif
8215 
8216 /*
8217  * Preventing scan from powering down the node by incrementing the
8218  * kidsupcnt.
8219  */
8220 void
8221 pm_hold_power(dev_info_t *dip)
8222 {
8223 	e_pm_hold_rele_power(dip, 1);
8224 }
8225 
8226 /*
8227  * Releasing the hold by decrementing the kidsupcnt allowing scan
8228  * to power down the node if all conditions are met.
8229  */
8230 void
8231 pm_rele_power(dev_info_t *dip)
8232 {
8233 	e_pm_hold_rele_power(dip, -1);
8234 }
8235 
8236 /*
8237  * A wrapper of pm_all_to_normal() to power up a dip
8238  * to its normal level
8239  */
8240 int
8241 pm_powerup(dev_info_t *dip)
8242 {
8243 	PMD_FUNC(pmf, "pm_powerup")
8244 
8245 	PMD(PMD_ALLNORM, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
8246 	ASSERT(!(servicing_interrupt()));
8247 
8248 	/*
8249 	 * in case this node is not already participating pm
8250 	 */
8251 	if (!PM_GET_PM_INFO(dip)) {
8252 		if (!DEVI_IS_ATTACHING(dip))
8253 			return (DDI_SUCCESS);
8254 		if (pm_start(dip) != DDI_SUCCESS)
8255 			return (DDI_FAILURE);
8256 		if (!PM_GET_PM_INFO(dip))
8257 			return (DDI_SUCCESS);
8258 	}
8259 
8260 	return (pm_all_to_normal(dip, PM_CANBLOCK_BLOCK));
8261 }
8262 
8263 int
8264 pm_rescan_walk(dev_info_t *dip, void *arg)
8265 {
8266 	_NOTE(ARGUNUSED(arg))
8267 
8268 	if (!PM_GET_PM_INFO(dip) || PM_ISBC(dip))
8269 		return (DDI_WALK_CONTINUE);
8270 
8271 	/*
8272 	 * Currently pm_cpr_callb/resume code is the only caller
8273 	 * and it needs to make sure that stopped scan get
8274 	 * reactivated. Otherwise, rescan walk needn't reactive
8275 	 * stopped scan.
8276 	 */
8277 	pm_scan_init(dip);
8278 
8279 	(void) pm_rescan(dip);
8280 	return (DDI_WALK_CONTINUE);
8281 }
8282 
8283 static dev_info_t *
8284 pm_get_next_descendent(dev_info_t *dip, dev_info_t *tdip)
8285 {
8286 	dev_info_t *wdip, *pdip;
8287 
8288 	for (wdip = tdip; wdip != dip; wdip = pdip) {
8289 		pdip = ddi_get_parent(wdip);
8290 		if (pdip == dip)
8291 			return (wdip);
8292 	}
8293 	return (NULL);
8294 }
8295 
8296 int
8297 pm_busop_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
8298     void *arg, void *result)
8299 {
8300 	PMD_FUNC(pmf, "bp_bus_power")
8301 	dev_info_t	*cdip;
8302 	pm_info_t	*cinfo;
8303 	pm_bp_child_pwrchg_t	*bpc;
8304 	pm_sp_misc_t		*pspm;
8305 	pm_bp_nexus_pwrup_t *bpn;
8306 	pm_bp_child_pwrchg_t new_bpc;
8307 	pm_bp_noinvol_t *bpi;
8308 	dev_info_t *tdip;
8309 	char *pathbuf;
8310 	int		ret = DDI_SUCCESS;
8311 	int		errno = 0;
8312 	pm_component_t *cp;
8313 
8314 	PMD(PMD_SET, ("%s: %s@%s(%s#%d) %s\n", pmf, PM_DEVICE(dip),
8315 	    pm_decode_op(op)))
8316 	switch (op) {
8317 	case BUS_POWER_CHILD_PWRCHG:
8318 		bpc = (pm_bp_child_pwrchg_t *)arg;
8319 		pspm = (pm_sp_misc_t *)bpc->bpc_private;
8320 		tdip = bpc->bpc_dip;
8321 		cdip = pm_get_next_descendent(dip, tdip);
8322 		cinfo = PM_GET_PM_INFO(cdip);
8323 		if (cdip != tdip) {
8324 			/*
8325 			 * If the node is an involved parent, it needs to
8326 			 * power up the node as it is needed.  There is nothing
8327 			 * else the framework can do here.
8328 			 */
8329 			if (PM_WANTS_NOTIFICATION(cdip)) {
8330 				PMD(PMD_SET, ("%s: call bus_power for "
8331 				    "%s@%s(%s#%d)\n", pmf, PM_DEVICE(cdip)))
8332 				return ((*PM_BUS_POWER_FUNC(cdip))(cdip,
8333 				    impl_arg, op, arg, result));
8334 			}
8335 			ASSERT(pspm->pspm_direction == PM_LEVEL_UPONLY ||
8336 			    pspm->pspm_direction == PM_LEVEL_DOWNONLY ||
8337 			    pspm->pspm_direction == PM_LEVEL_EXACT);
8338 			/*
8339 			 * we presume that the parent needs to be up in
8340 			 * order for the child to change state (either
8341 			 * because it must already be on if the child is on
8342 			 * (and the pm_all_to_normal_nexus() will be a nop)
8343 			 * or because it will need to be on for the child
8344 			 * to come on; so we make the call regardless
8345 			 */
8346 			pm_hold_power(cdip);
8347 			if (cinfo) {
8348 				pm_canblock_t canblock = pspm->pspm_canblock;
8349 				ret = pm_all_to_normal_nexus(cdip, canblock);
8350 				if (ret != DDI_SUCCESS) {
8351 					pm_rele_power(cdip);
8352 					return (ret);
8353 				}
8354 			}
8355 			PMD(PMD_SET, ("%s: walk down to %s@%s(%s#%d)\n", pmf,
8356 			    PM_DEVICE(cdip)))
8357 			ret = pm_busop_bus_power(cdip, impl_arg, op, arg,
8358 			    result);
8359 			pm_rele_power(cdip);
8360 		} else {
8361 			ret = pm_busop_set_power(cdip, impl_arg, op, arg,
8362 			    result);
8363 		}
8364 		return (ret);
8365 
8366 	case BUS_POWER_NEXUS_PWRUP:
8367 		bpn = (pm_bp_nexus_pwrup_t *)arg;
8368 		pspm = (pm_sp_misc_t *)bpn->bpn_private;
8369 
8370 		if (!e_pm_valid_info(dip, NULL) ||
8371 		    !e_pm_valid_comp(dip, bpn->bpn_comp, &cp) ||
8372 		    !e_pm_valid_power(dip, bpn->bpn_comp, bpn->bpn_level)) {
8373 			PMD(PMD_SET, ("%s: %s@%s(%s#%d) has no pm info; EIO\n",
8374 			    pmf, PM_DEVICE(dip)))
8375 			*pspm->pspm_errnop = EIO;
8376 			*(int *)result = DDI_FAILURE;
8377 			return (DDI_FAILURE);
8378 		}
8379 
8380 		ASSERT(bpn->bpn_dip == dip);
8381 		PMD(PMD_SET, ("%s: nexus powerup for %s@%s(%s#%d)\n", pmf,
8382 		    PM_DEVICE(dip)))
8383 		new_bpc.bpc_dip = dip;
8384 		pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8385 		new_bpc.bpc_path = ddi_pathname(dip, pathbuf);
8386 		new_bpc.bpc_comp = bpn->bpn_comp;
8387 		new_bpc.bpc_olevel = PM_CURPOWER(dip, bpn->bpn_comp);
8388 		new_bpc.bpc_nlevel = bpn->bpn_level;
8389 		new_bpc.bpc_private = bpn->bpn_private;
8390 		((pm_sp_misc_t *)(new_bpc.bpc_private))->pspm_direction =
8391 		    PM_LEVEL_UPONLY;
8392 		((pm_sp_misc_t *)(new_bpc.bpc_private))->pspm_errnop =
8393 		    &errno;
8394 		ret = pm_busop_set_power(dip, impl_arg, BUS_POWER_CHILD_PWRCHG,
8395 		    (void *)&new_bpc, result);
8396 		kmem_free(pathbuf, MAXPATHLEN);
8397 		return (ret);
8398 
8399 	case BUS_POWER_NOINVOL:
8400 		bpi = (pm_bp_noinvol_t *)arg;
8401 		tdip = bpi->bpni_dip;
8402 		cdip = pm_get_next_descendent(dip, tdip);
8403 
8404 		/* In case of rem_drv, the leaf node has been removed */
8405 		if (cdip == NULL)
8406 			return (DDI_SUCCESS);
8407 
8408 		cinfo = PM_GET_PM_INFO(cdip);
8409 		if (cdip != tdip) {
8410 			if (PM_WANTS_NOTIFICATION(cdip)) {
8411 				PMD(PMD_NOINVOL,
8412 				    ("%s: call bus_power for %s@%s(%s#%d)\n",
8413 				    pmf, PM_DEVICE(cdip)))
8414 				ret = (*PM_BUS_POWER_FUNC(cdip))
8415 				    (cdip, NULL, op, arg, result);
8416 				if ((cinfo) && (ret == DDI_SUCCESS))
8417 					(void) pm_noinvol_update_node(cdip,
8418 					    bpi);
8419 				return (ret);
8420 			} else {
8421 				PMD(PMD_NOINVOL,
8422 				    ("%s: walk down to %s@%s(%s#%d)\n", pmf,
8423 				    PM_DEVICE(cdip)))
8424 				ret = pm_busop_bus_power(cdip, NULL, op,
8425 				    arg, result);
8426 				/*
8427 				 * Update the current node.
8428 				 */
8429 				if ((cinfo) && (ret == DDI_SUCCESS))
8430 					(void) pm_noinvol_update_node(cdip,
8431 					    bpi);
8432 				return (ret);
8433 			}
8434 		} else {
8435 			/*
8436 			 * For attach, detach, power up:
8437 			 * Do nothing for leaf node since its
8438 			 * counts are already updated.
8439 			 * For CFB and driver removal, since the
8440 			 * path and the target dip passed in is up to and incl.
8441 			 * the immediate ancestor, need to do the update.
8442 			 */
8443 			PMD(PMD_NOINVOL, ("%s: target %s@%s(%s#%d) is "
8444 			    "reached\n", pmf, PM_DEVICE(cdip)))
8445 			if (cinfo && ((bpi->bpni_cmd == PM_BP_NOINVOL_REMDRV) ||
8446 			    (bpi->bpni_cmd == PM_BP_NOINVOL_CFB)))
8447 				(void) pm_noinvol_update_node(cdip, bpi);
8448 			return (DDI_SUCCESS);
8449 		}
8450 
8451 	default:
8452 		PMD(PMD_SET, ("%s: operation %d is not supported!\n", pmf, op))
8453 		return (DDI_FAILURE);
8454 	}
8455 }
8456 
8457 static int
8458 pm_busop_set_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
8459     void *arg, void *resultp)
8460 {
8461 	_NOTE(ARGUNUSED(impl_arg))
8462 	PMD_FUNC(pmf, "bp_set_power")
8463 	pm_ppm_devlist_t *devl;
8464 	int clevel, circ;
8465 #ifdef	DEBUG
8466 	int circ_db, ccirc_db;
8467 #endif
8468 	int ret = DDI_SUCCESS;
8469 	dev_info_t *cdip;
8470 	pm_bp_child_pwrchg_t *bpc = (pm_bp_child_pwrchg_t *)arg;
8471 	pm_sp_misc_t *pspm = (pm_sp_misc_t *)bpc->bpc_private;
8472 	pm_canblock_t canblock = pspm->pspm_canblock;
8473 	int scan = pspm->pspm_scan;
8474 	int comp = bpc->bpc_comp;
8475 	int olevel = bpc->bpc_olevel;
8476 	int nlevel = bpc->bpc_nlevel;
8477 	int comps_off_incr = 0;
8478 	dev_info_t *pdip = ddi_get_parent(dip);
8479 	int dodeps;
8480 	int direction = pspm->pspm_direction;
8481 	int *errnop = pspm->pspm_errnop;
8482 	char *dir = pm_decode_direction(direction);
8483 	int *iresp = (int *)resultp;
8484 	time_t	idletime, thresh;
8485 	pm_component_t *cp = PM_CP(dip, comp);
8486 	int work_type;
8487 
8488 	*iresp = DDI_SUCCESS;
8489 	*errnop = 0;
8490 	ASSERT(op == BUS_POWER_CHILD_PWRCHG);
8491 	PMD(PMD_SET, ("%s: %s@%s(%s#%d) %s\n", pmf, PM_DEVICE(dip),
8492 	    pm_decode_op(op)))
8493 
8494 	/*
8495 	 * The following set of conditions indicate we are here to handle a
8496 	 * driver's pm_[raise|lower]_power request, but the device is being
8497 	 * power managed (PM_DIRECT_PM) by a user process.  For that case
8498 	 * we want to pm_block and pass a status back to the caller based
8499 	 * on whether the controlling process's next activity on the device
8500 	 * matches the current request or not.  This distinction tells
8501 	 * downstream functions to avoid calling into a driver or changing
8502 	 * the framework's power state.  To actually block, we need:
8503 	 *
8504 	 * PM_ISDIRECT(dip)
8505 	 *	no reason to block unless a process is directly controlling dev
8506 	 * direction != PM_LEVEL_EXACT
8507 	 *	EXACT is used by controlling proc's PM_SET_CURRENT_POWER ioctl
8508 	 * !pm_processes_stopped
8509 	 *	don't block if controlling proc already be stopped for cpr
8510 	 * canblock != PM_CANBLOCK_BYPASS
8511 	 *	our caller must not have explicitly prevented blocking
8512 	 */
8513 	if (direction != PM_LEVEL_EXACT && canblock != PM_CANBLOCK_BYPASS) {
8514 		PM_LOCK_DIP(dip);
8515 		while (PM_ISDIRECT(dip) && !pm_processes_stopped) {
8516 			/* releases dip lock */
8517 			ret = pm_busop_match_request(dip, bpc);
8518 			if (ret == EAGAIN) {
8519 				PM_LOCK_DIP(dip);
8520 				continue;
8521 			}
8522 			return (*iresp = ret);
8523 		}
8524 		PM_UNLOCK_DIP(dip);
8525 	}
8526 	/* BC device is never scanned, so power will stick until we are done */
8527 	if (PM_ISBC(dip) && comp != 0 && nlevel != 0 &&
8528 	    direction != PM_LEVEL_DOWNONLY) {
8529 		int nrmpwr0 = pm_get_normal_power(dip, 0);
8530 		if (pm_set_power(dip, 0, nrmpwr0, direction,
8531 		    canblock, 0, resultp) != DDI_SUCCESS) {
8532 			/* *resultp set by pm_set_power */
8533 			return (DDI_FAILURE);
8534 		}
8535 	}
8536 	if (PM_WANTS_NOTIFICATION(pdip)) {
8537 		PMD(PMD_SET, ("%s: pre_notify %s@%s(%s#%d) for child "
8538 		    "%s@%s(%s#%d)\n", pmf, PM_DEVICE(pdip), PM_DEVICE(dip)))
8539 		ret = (*PM_BUS_POWER_FUNC(pdip))(pdip, NULL,
8540 		    BUS_POWER_PRE_NOTIFICATION, bpc, resultp);
8541 		if (ret != DDI_SUCCESS) {
8542 			PMD(PMD_SET, ("%s: failed to pre_notify %s@%s(%s#%d)\n",
8543 			    pmf, PM_DEVICE(pdip)))
8544 			return (DDI_FAILURE);
8545 		}
8546 	} else {
8547 		/*
8548 		 * Since we don't know what the actual power level is,
8549 		 * we place a power hold on the parent no matter what
8550 		 * component and level is changing.
8551 		 */
8552 		pm_hold_power(pdip);
8553 	}
8554 	PM_LOCK_POWER(dip, &circ);
8555 	clevel = PM_CURPOWER(dip, comp);
8556 	PMD(PMD_SET, ("%s: %s@%s(%s#%d), cmp=%d, olvl=%d, nlvl=%d, clvl=%d, "
8557 	    "dir=%s\n", pmf, PM_DEVICE(dip), comp, bpc->bpc_olevel, nlevel,
8558 	    clevel, dir))
8559 	switch (direction) {
8560 	case PM_LEVEL_UPONLY:
8561 		/* Powering up */
8562 		if (clevel >= nlevel) {
8563 			PMD(PMD_SET, ("%s: current level is already "
8564 			    "at or above the requested level.\n", pmf))
8565 			*iresp = DDI_SUCCESS;
8566 			ret = DDI_SUCCESS;
8567 			goto post_notify;
8568 		}
8569 		break;
8570 	case PM_LEVEL_EXACT:
8571 		/* specific level request */
8572 		if (clevel == nlevel && !PM_ISBC(dip)) {
8573 			PMD(PMD_SET, ("%s: current level is already "
8574 			    "at the requested level.\n", pmf))
8575 			*iresp = DDI_SUCCESS;
8576 			ret = DDI_SUCCESS;
8577 			goto post_notify;
8578 		} else if (PM_IS_CFB(dip) && (nlevel < clevel)) {
8579 			PMD(PMD_CFB, ("%s: powerdown of console\n", pmf))
8580 			if (!pm_cfb_enabled) {
8581 				PMD(PMD_ERROR | PMD_CFB,
8582 				    ("%s: !pm_cfb_enabled, fails\n", pmf))
8583 				*errnop = EINVAL;
8584 				*iresp = DDI_FAILURE;
8585 				ret = DDI_FAILURE;
8586 				goto post_notify;
8587 			}
8588 			mutex_enter(&pm_cfb_lock);
8589 			while (cfb_inuse) {
8590 				mutex_exit(&pm_cfb_lock);
8591 				if (delay_sig(1) == EINTR) {
8592 					ret = DDI_FAILURE;
8593 					*iresp = DDI_FAILURE;
8594 					*errnop = EINTR;
8595 					goto post_notify;
8596 				}
8597 				mutex_enter(&pm_cfb_lock);
8598 			}
8599 			mutex_exit(&pm_cfb_lock);
8600 		}
8601 		break;
8602 	case PM_LEVEL_DOWNONLY:
8603 		/* Powering down */
8604 		thresh = cur_threshold(dip, comp);
8605 		idletime = gethrestime_sec() - cp->pmc_timestamp;
8606 		if (scan && ((PM_KUC(dip) != 0) ||
8607 		    (cp->pmc_busycount > 0) ||
8608 		    ((idletime < thresh) && !PM_IS_PID(dip)))) {
8609 #ifdef	DEBUG
8610 			if (DEVI(dip)->devi_pm_kidsupcnt != 0)
8611 				PMD(PMD_SET, ("%s: scan failed: "
8612 				    "kidsupcnt != 0\n", pmf))
8613 			if (cp->pmc_busycount > 0)
8614 				PMD(PMD_SET, ("%s: scan failed: "
8615 				    "device become busy\n", pmf))
8616 			if (idletime < thresh)
8617 				PMD(PMD_SET, ("%s: scan failed: device "
8618 				    "hasn't been idle long enough\n", pmf))
8619 #endif
8620 			*iresp = DDI_FAILURE;
8621 			*errnop = EBUSY;
8622 			ret = DDI_FAILURE;
8623 			goto post_notify;
8624 		} else if (clevel != PM_LEVEL_UNKNOWN && clevel <= nlevel) {
8625 			PMD(PMD_SET, ("%s: current level is already at "
8626 			    "or below the requested level.\n", pmf))
8627 			*iresp = DDI_SUCCESS;
8628 			ret = DDI_SUCCESS;
8629 			goto post_notify;
8630 		}
8631 		break;
8632 	}
8633 
8634 	if (PM_IS_CFB(dip) && (comps_off_incr =
8635 	    calc_cfb_comps_incr(dip, comp, clevel, nlevel)) > 0) {
8636 		/*
8637 		 * Pre-adjust pm_cfb_comps_off if lowering a console fb
8638 		 * component from full power.  Remember that we tried to
8639 		 * lower power in case it fails and we need to back out
8640 		 * the adjustment.
8641 		 */
8642 		update_comps_off(comps_off_incr, dip);
8643 		PMD(PMD_CFB, ("%s: %s@%s(%s#%d)[%d] %d->%d cfb_comps_off->%d\n",
8644 		    pmf, PM_DEVICE(dip), comp, clevel, nlevel,
8645 		    pm_cfb_comps_off))
8646 	}
8647 
8648 	if ((*iresp = power_dev(dip,
8649 	    comp, nlevel, clevel, canblock, &devl)) == DDI_SUCCESS) {
8650 #ifdef DEBUG
8651 		/*
8652 		 * All descendents of this node should already be powered off.
8653 		 */
8654 		if (PM_CURPOWER(dip, comp) == 0) {
8655 			pm_desc_pwrchk_t pdpchk;
8656 			pdpchk.pdpc_dip = dip;
8657 			pdpchk.pdpc_par_involved = PM_WANTS_NOTIFICATION(dip);
8658 			ndi_devi_enter(dip, &circ_db);
8659 			for (cdip = ddi_get_child(dip); cdip != NULL;
8660 			    cdip = ddi_get_next_sibling(cdip)) {
8661 				ndi_devi_enter(cdip, &ccirc_db);
8662 				ddi_walk_devs(cdip, pm_desc_pwrchk_walk,
8663 				    (void *)&pdpchk);
8664 				ndi_devi_exit(cdip, ccirc_db);
8665 			}
8666 			ndi_devi_exit(dip, circ_db);
8667 		}
8668 #endif
8669 		/*
8670 		 * Post-adjust pm_cfb_comps_off if we brought an fb component
8671 		 * back up to full power.
8672 		 */
8673 		if (PM_IS_CFB(dip) && comps_off_incr < 0) {
8674 			update_comps_off(comps_off_incr, dip);
8675 			PMD(PMD_CFB, ("%s: %s@%s(%s#%d)[%d] %d->%d "
8676 			    "cfb_comps_off->%d\n", pmf, PM_DEVICE(dip),
8677 			    comp, clevel, nlevel, pm_cfb_comps_off))
8678 		}
8679 		dodeps = 0;
8680 		if (POWERING_OFF(clevel, nlevel)) {
8681 			if (PM_ISBC(dip)) {
8682 				dodeps = (comp == 0);
8683 			} else {
8684 				int i;
8685 				dodeps = 1;
8686 				for (i = 0; i < PM_NUMCMPTS(dip); i++) {
8687 					/* if some component still on */
8688 					if (PM_CURPOWER(dip, i)) {
8689 						dodeps = 0;
8690 						break;
8691 					}
8692 				}
8693 			}
8694 			if (dodeps)
8695 				work_type = PM_DEP_WK_POWER_OFF;
8696 		} else if (POWERING_ON(clevel, nlevel)) {
8697 			if (PM_ISBC(dip)) {
8698 				dodeps = (comp == 0);
8699 			} else {
8700 				int i;
8701 				dodeps = 1;
8702 				for (i = 0; i < PM_NUMCMPTS(dip); i++) {
8703 					if (i == comp)
8704 						continue;
8705 					if (PM_CURPOWER(dip, i) > 0) {
8706 						dodeps = 0;
8707 						break;
8708 					}
8709 				}
8710 			}
8711 			if (dodeps)
8712 				work_type = PM_DEP_WK_POWER_ON;
8713 		}
8714 
8715 		if (dodeps) {
8716 			char *pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8717 
8718 			(void) ddi_pathname(dip, pathbuf);
8719 			pm_dispatch_to_dep_thread(work_type, pathbuf, NULL,
8720 			    PM_DEP_NOWAIT, NULL, 0);
8721 			kmem_free(pathbuf, MAXPATHLEN);
8722 		}
8723 		if ((PM_CURPOWER(dip, comp) == nlevel) && pm_watchers()) {
8724 			int old;
8725 
8726 			/* If old power cached during deadlock, use it. */
8727 			old = (cp->pmc_flags & PM_PHC_WHILE_SET_POWER ?
8728 			    cp->pmc_phc_pwr : olevel);
8729 			mutex_enter(&pm_rsvp_lock);
8730 			pm_enqueue_notify(PSC_HAS_CHANGED, dip, comp, nlevel,
8731 			    old, canblock);
8732 			pm_enqueue_notify_others(&devl, canblock);
8733 			mutex_exit(&pm_rsvp_lock);
8734 		}
8735 
8736 		/*
8737 		 * If we are coming from a scan, don't do it again,
8738 		 * else we can have infinite loops.
8739 		 */
8740 		if (!scan)
8741 			pm_rescan(dip);
8742 	} else {
8743 		/* if we incremented pm_comps_off_count, but failed */
8744 		if (comps_off_incr > 0) {
8745 			update_comps_off(-comps_off_incr, dip);
8746 			PMD(PMD_CFB, ("%s: %s@%s(%s#%d)[%d] %d->%d "
8747 			    "cfb_comps_off->%d\n", pmf, PM_DEVICE(dip),
8748 			    comp, clevel, nlevel, pm_cfb_comps_off))
8749 		}
8750 		*errnop = EIO;
8751 	}
8752 
8753 post_notify:
8754 	/*
8755 	 * This thread may have been in deadlock with pm_power_has_changed.
8756 	 * Before releasing power lock, clear the flag which marks this
8757 	 * condition.
8758 	 */
8759 	cp->pmc_flags &= ~PM_PHC_WHILE_SET_POWER;
8760 
8761 	/*
8762 	 * Update the old power level in the bus power structure with the
8763 	 * actual power level before the transition was made to the new level.
8764 	 * Some involved parents depend on this information to keep track of
8765 	 * their children's power transition.
8766 	 */
8767 	if (*iresp != DDI_FAILURE)
8768 		bpc->bpc_olevel = clevel;
8769 
8770 	if (PM_WANTS_NOTIFICATION(pdip)) {
8771 		ret = (*PM_BUS_POWER_FUNC(pdip))(pdip, NULL,
8772 		    BUS_POWER_POST_NOTIFICATION, bpc, resultp);
8773 		PM_UNLOCK_POWER(dip, circ);
8774 		PMD(PMD_SET, ("%s: post_notify %s@%s(%s#%d) for "
8775 		    "child %s@%s(%s#%d), ret=%d\n", pmf, PM_DEVICE(pdip),
8776 		    PM_DEVICE(dip), ret))
8777 	} else {
8778 		nlevel = cur_power(cp); /* in case phc deadlock updated pwr */
8779 		PM_UNLOCK_POWER(dip, circ);
8780 		/*
8781 		 * Now that we know what power transition has occurred
8782 		 * (if any), release the power hold.  Leave the hold
8783 		 * in effect in the case of OFF->ON transition.
8784 		 */
8785 		if (!(clevel == 0 && nlevel > 0 &&
8786 		    (!PM_ISBC(dip) || comp == 0)))
8787 			pm_rele_power(pdip);
8788 		/*
8789 		 * If the power transition was an ON->OFF transition,
8790 		 * remove the power hold from the parent.
8791 		 */
8792 		if ((clevel > 0 || clevel == PM_LEVEL_UNKNOWN) &&
8793 		    nlevel == 0 && (!PM_ISBC(dip) || comp == 0))
8794 			pm_rele_power(pdip);
8795 	}
8796 	if (*iresp != DDI_SUCCESS || ret != DDI_SUCCESS)
8797 		return (DDI_FAILURE);
8798 	else
8799 		return (DDI_SUCCESS);
8800 }
8801 
8802 /*
8803  * If an app (SunVTS or Xsun) has taken control, then block until it
8804  * gives it up or makes the requested power level change, unless
8805  * we have other instructions about blocking.  Returns DDI_SUCCESS,
8806  * DDI_FAILURE or EAGAIN (owner released device from directpm).
8807  */
8808 static int
8809 pm_busop_match_request(dev_info_t *dip, void *arg)
8810 {
8811 	PMD_FUNC(pmf, "bp_match_request")
8812 	pm_bp_child_pwrchg_t *bpc = (pm_bp_child_pwrchg_t *)arg;
8813 	pm_sp_misc_t *pspm = (pm_sp_misc_t *)bpc->bpc_private;
8814 	int comp = bpc->bpc_comp;
8815 	int nlevel = bpc->bpc_nlevel;
8816 	pm_canblock_t canblock = pspm->pspm_canblock;
8817 	int direction = pspm->pspm_direction;
8818 	int clevel, circ;
8819 
8820 	ASSERT(PM_IAM_LOCKING_DIP(dip));
8821 	PM_LOCK_POWER(dip, &circ);
8822 	clevel = PM_CURPOWER(dip, comp);
8823 	PMD(PMD_SET, ("%s: %s@%s(%s#%d), cmp=%d, nlvl=%d, clvl=%d\n",
8824 	    pmf, PM_DEVICE(dip), comp, nlevel, clevel))
8825 	if (direction == PM_LEVEL_UPONLY) {
8826 		if (clevel >= nlevel) {
8827 			PM_UNLOCK_POWER(dip, circ);
8828 			PM_UNLOCK_DIP(dip);
8829 			return (DDI_SUCCESS);
8830 		}
8831 	} else if (clevel == nlevel) {
8832 		PM_UNLOCK_POWER(dip, circ);
8833 		PM_UNLOCK_DIP(dip);
8834 		return (DDI_SUCCESS);
8835 	}
8836 	if (canblock == PM_CANBLOCK_FAIL) {
8837 		PM_UNLOCK_POWER(dip, circ);
8838 		PM_UNLOCK_DIP(dip);
8839 		return (DDI_FAILURE);
8840 	}
8841 	if (canblock == PM_CANBLOCK_BLOCK) {
8842 		/*
8843 		 * To avoid a deadlock, we must not hold the
8844 		 * power lock when we pm_block.
8845 		 */
8846 		PM_UNLOCK_POWER(dip, circ);
8847 		PMD(PMD_SET, ("%s: blocking\n", pmf))
8848 		    /* pm_block releases dip lock */
8849 		    switch (pm_block(dip, comp, nlevel, clevel)) {
8850 		    case PMP_RELEASE:
8851 				return (EAGAIN);
8852 		    case PMP_SUCCEED:
8853 				return (DDI_SUCCESS);
8854 		    case PMP_FAIL:
8855 				return (DDI_FAILURE);
8856 		    }
8857 	} else {
8858 		ASSERT(0);
8859 	}
8860 	_NOTE(NOTREACHED);
8861 	return (DDI_FAILURE);	/* keep gcc happy */
8862 }
8863 
8864 static int
8865 pm_all_to_normal_nexus(dev_info_t *dip, pm_canblock_t canblock)
8866 {
8867 	PMD_FUNC(pmf, "all_to_normal_nexus")
8868 	int		*normal;
8869 	int		i, ncomps;
8870 	size_t		size;
8871 	int		changefailed = 0;
8872 	int		ret, result = DDI_SUCCESS;
8873 	pm_bp_nexus_pwrup_t	bpn;
8874 	pm_sp_misc_t	pspm;
8875 
8876 	ASSERT(PM_GET_PM_INFO(dip));
8877 	PMD(PMD_ALLNORM, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
8878 	if (pm_get_norm_pwrs(dip, &normal, &size) != DDI_SUCCESS) {
8879 		PMD(PMD_ALLNORM, ("%s: can't get norm pwrs\n", pmf))
8880 		return (DDI_FAILURE);
8881 	}
8882 	ncomps = PM_NUMCMPTS(dip);
8883 	for (i = 0; i < ncomps; i++) {
8884 		bpn.bpn_dip = dip;
8885 		bpn.bpn_comp = i;
8886 		bpn.bpn_level = normal[i];
8887 		pspm.pspm_canblock = canblock;
8888 		pspm.pspm_scan = 0;
8889 		bpn.bpn_private = &pspm;
8890 		ret = pm_busop_bus_power(dip, NULL, BUS_POWER_NEXUS_PWRUP,
8891 		    (void *)&bpn, (void *)&result);
8892 		if (ret != DDI_SUCCESS || result != DDI_SUCCESS) {
8893 			PMD(PMD_FAIL | PMD_ALLNORM, ("%s: %s@%s(%s#%d)[%d] "
8894 			    "->%d failure result %d\n", pmf, PM_DEVICE(dip),
8895 			    i, normal[i], result))
8896 			changefailed++;
8897 		}
8898 	}
8899 	kmem_free(normal, size);
8900 	if (changefailed) {
8901 		PMD(PMD_FAIL, ("%s: failed to set %d comps %s@%s(%s#%d) "
8902 		    "full power\n", pmf, changefailed, PM_DEVICE(dip)))
8903 		return (DDI_FAILURE);
8904 	}
8905 	return (DDI_SUCCESS);
8906 }
8907 
8908 int
8909 pm_noinvol_update(int subcmd, int volpmd, int wasvolpmd, char *path,
8910     dev_info_t *tdip)
8911 {
8912 	PMD_FUNC(pmf, "noinvol_update")
8913 	pm_bp_noinvol_t args;
8914 	int ret;
8915 	int result = DDI_SUCCESS;
8916 
8917 	args.bpni_path = path;
8918 	args.bpni_dip = tdip;
8919 	args.bpni_cmd = subcmd;
8920 	args.bpni_wasvolpmd = wasvolpmd;
8921 	args.bpni_volpmd = volpmd;
8922 	PMD(PMD_NOINVOL, ("%s: update for path %s tdip %p subcmd %d "
8923 	    "volpmd %d wasvolpmd %d\n", pmf,
8924 	    path, (void *)tdip, subcmd, wasvolpmd, volpmd))
8925 	ret = pm_busop_bus_power(ddi_root_node(), NULL, BUS_POWER_NOINVOL,
8926 	    &args, &result);
8927 	return (ret);
8928 }
8929 
8930 void
8931 pm_noinvol_update_node(dev_info_t *dip, pm_bp_noinvol_t *req)
8932 {
8933 	PMD_FUNC(pmf, "noinvol_update_node")
8934 
8935 	PMD(PMD_NOINVOL, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
8936 	switch (req->bpni_cmd) {
8937 	case PM_BP_NOINVOL_ATTACH:
8938 		PMD(PMD_NOINVOL, ("%s: PM_PB_NOINVOL_ATTACH %s@%s(%s#%d) "
8939 		    "noinvol %d->%d\n", pmf, PM_DEVICE(dip),
8940 		    DEVI(dip)->devi_pm_noinvolpm,
8941 		    DEVI(dip)->devi_pm_noinvolpm - 1))
8942 		ASSERT(DEVI(dip)->devi_pm_noinvolpm);
8943 		PM_LOCK_DIP(dip);
8944 		DEVI(dip)->devi_pm_noinvolpm--;
8945 		if (req->bpni_wasvolpmd) {
8946 			PMD(PMD_NOINVOL, ("%s: PM_BP_NOINVOL_ATTACH "
8947 			    "%s@%s(%s#%d) volpmd %d->%d\n", pmf,
8948 			    PM_DEVICE(dip), DEVI(dip)->devi_pm_volpmd,
8949 			    DEVI(dip)->devi_pm_volpmd - 1))
8950 			if (DEVI(dip)->devi_pm_volpmd)
8951 				DEVI(dip)->devi_pm_volpmd--;
8952 		}
8953 		PM_UNLOCK_DIP(dip);
8954 		break;
8955 
8956 	case PM_BP_NOINVOL_DETACH:
8957 		PMD(PMD_NOINVOL, ("%s: PM_BP_NOINVOL_DETACH %s@%s(%s#%d) "
8958 		    "noinvolpm %d->%d\n", pmf, PM_DEVICE(dip),
8959 		    DEVI(dip)->devi_pm_noinvolpm,
8960 		    DEVI(dip)->devi_pm_noinvolpm + 1))
8961 		PM_LOCK_DIP(dip);
8962 		DEVI(dip)->devi_pm_noinvolpm++;
8963 		if (req->bpni_wasvolpmd) {
8964 			PMD(PMD_NOINVOL, ("%s: PM_BP_NOINVOL_DETACH "
8965 			    "%s@%s(%s#%d) volpmd %d->%d\n", pmf,
8966 			    PM_DEVICE(dip), DEVI(dip)->devi_pm_volpmd,
8967 			    DEVI(dip)->devi_pm_volpmd + 1))
8968 			DEVI(dip)->devi_pm_volpmd++;
8969 		}
8970 		PM_UNLOCK_DIP(dip);
8971 		break;
8972 
8973 	case PM_BP_NOINVOL_REMDRV:
8974 		PMD(PMD_NOINVOL, ("%s: PM_BP_NOINVOL_REMDRV %s@%s(%s#%d) "
8975 		    "noinvol %d->%d\n", pmf, PM_DEVICE(dip),
8976 		    DEVI(dip)->devi_pm_noinvolpm,
8977 		    DEVI(dip)->devi_pm_noinvolpm - 1))
8978 		ASSERT(DEVI(dip)->devi_pm_noinvolpm);
8979 		PM_LOCK_DIP(dip);
8980 		DEVI(dip)->devi_pm_noinvolpm--;
8981 		if (req->bpni_wasvolpmd) {
8982 			PMD(PMD_NOINVOL,
8983 			    ("%s: PM_BP_NOINVOL_REMDRV %s@%s(%s#%d) "
8984 			    "volpmd %d->%d\n", pmf, PM_DEVICE(dip),
8985 			    DEVI(dip)->devi_pm_volpmd,
8986 			    DEVI(dip)->devi_pm_volpmd - 1))
8987 			/*
8988 			 * A power up could come in between and
8989 			 * clear the volpmd, if that's the case,
8990 			 * volpmd would be clear.
8991 			 */
8992 			if (DEVI(dip)->devi_pm_volpmd)
8993 				DEVI(dip)->devi_pm_volpmd--;
8994 		}
8995 		PM_UNLOCK_DIP(dip);
8996 		break;
8997 
8998 	case PM_BP_NOINVOL_CFB:
8999 		PMD(PMD_NOINVOL,
9000 		    ("%s: PM_BP_NOIVOL_CFB %s@%s(%s#%d) noinvol %d->%d\n",
9001 		    pmf, PM_DEVICE(dip), DEVI(dip)->devi_pm_noinvolpm,
9002 		    DEVI(dip)->devi_pm_noinvolpm + 1))
9003 		PM_LOCK_DIP(dip);
9004 		DEVI(dip)->devi_pm_noinvolpm++;
9005 		PM_UNLOCK_DIP(dip);
9006 		break;
9007 
9008 	case PM_BP_NOINVOL_POWER:
9009 		PMD(PMD_NOINVOL,
9010 		    ("%s: PM_BP_NOIVOL_PWR %s@%s(%s#%d) volpmd %d->%d\n",
9011 		    pmf, PM_DEVICE(dip),
9012 		    DEVI(dip)->devi_pm_volpmd, DEVI(dip)->devi_pm_volpmd -
9013 		    req->bpni_volpmd))
9014 		PM_LOCK_DIP(dip);
9015 		DEVI(dip)->devi_pm_volpmd -= req->bpni_volpmd;
9016 		PM_UNLOCK_DIP(dip);
9017 		break;
9018 
9019 	default:
9020 		break;
9021 	}
9022 
9023 }
9024 
9025 #ifdef DEBUG
9026 static int
9027 pm_desc_pwrchk_walk(dev_info_t *dip, void *arg)
9028 {
9029 	PMD_FUNC(pmf, "desc_pwrchk")
9030 	pm_desc_pwrchk_t *pdpchk = (pm_desc_pwrchk_t *)arg;
9031 	pm_info_t *info = PM_GET_PM_INFO(dip);
9032 	int i, curpwr, ce_level;
9033 
9034 	if (!info)
9035 		return (DDI_WALK_CONTINUE);
9036 
9037 	PMD(PMD_SET, ("%s: %s@%s(%s#%d)\n", pmf, PM_DEVICE(dip)))
9038 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
9039 		if ((curpwr = PM_CURPOWER(dip, i)) == 0)
9040 			continue;
9041 		ce_level = (pdpchk->pdpc_par_involved == 0) ? CE_PANIC :
9042 		    CE_WARN;
9043 		PMD(PMD_SET, ("%s: %s@%s(%s#%d) is powered off while desc "
9044 		    "%s@%s(%s#%d)[%d] is at %d\n", pmf,
9045 		    PM_DEVICE(pdpchk->pdpc_dip), PM_DEVICE(dip), i, curpwr))
9046 		cmn_err(ce_level, "!device %s@%s(%s#%d) is powered on, "
9047 		    "while its ancestor, %s@%s(%s#%d), is powering off!",
9048 		    PM_DEVICE(dip), PM_DEVICE(pdpchk->pdpc_dip));
9049 	}
9050 	return (DDI_WALK_CONTINUE);
9051 }
9052 #endif
9053 
9054 /*
9055  * Record the fact that one thread is borrowing the lock on a device node.
9056  * Use is restricted to the case where the lending thread will block until
9057  * the borrowing thread (always curthread) completes.
9058  */
9059 void
9060 pm_borrow_lock(kthread_t *lender)
9061 {
9062 	lock_loan_t *prev = &lock_loan_head;
9063 	lock_loan_t *cur = (lock_loan_t *)kmem_zalloc(sizeof (*cur), KM_SLEEP);
9064 
9065 	cur->pmlk_borrower = curthread;
9066 	cur->pmlk_lender = lender;
9067 	mutex_enter(&pm_loan_lock);
9068 	cur->pmlk_next = prev->pmlk_next;
9069 	prev->pmlk_next = cur;
9070 	mutex_exit(&pm_loan_lock);
9071 }
9072 
9073 /*
9074  * Return the borrowed lock.  A thread can borrow only one.
9075  */
9076 void
9077 pm_return_lock(void)
9078 {
9079 	lock_loan_t *cur;
9080 	lock_loan_t *prev = &lock_loan_head;
9081 
9082 	mutex_enter(&pm_loan_lock);
9083 	ASSERT(prev->pmlk_next != NULL);
9084 	for (cur = prev->pmlk_next; cur; prev = cur, cur = cur->pmlk_next)
9085 		if (cur->pmlk_borrower == curthread)
9086 			break;
9087 
9088 	ASSERT(cur != NULL);
9089 	prev->pmlk_next = cur->pmlk_next;
9090 	mutex_exit(&pm_loan_lock);
9091 	kmem_free(cur, sizeof (*cur));
9092 }
9093