xref: /onnv-gate/usr/src/uts/common/io/pm.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * pm	This driver now only handles the ioctl interface.  The scanning
31*0Sstevel@tonic-gate  *	and policy stuff now lives in common/os/sunpm.c.
32*0Sstevel@tonic-gate  *	Not DDI compliant
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/errno.h>
37*0Sstevel@tonic-gate #include <sys/modctl.h>
38*0Sstevel@tonic-gate #include <sys/conf.h>		/* driver flags and functions */
39*0Sstevel@tonic-gate #include <sys/open.h>		/* OTYP_CHR definition */
40*0Sstevel@tonic-gate #include <sys/stat.h>		/* S_IFCHR definition */
41*0Sstevel@tonic-gate #include <sys/pathname.h>	/* name -> dev_info xlation */
42*0Sstevel@tonic-gate #include <sys/kmem.h>		/* memory alloc stuff */
43*0Sstevel@tonic-gate #include <sys/debug.h>
44*0Sstevel@tonic-gate #include <sys/pm.h>
45*0Sstevel@tonic-gate #include <sys/ddi.h>
46*0Sstevel@tonic-gate #include <sys/sunddi.h>
47*0Sstevel@tonic-gate #include <sys/epm.h>
48*0Sstevel@tonic-gate #include <sys/vfs.h>
49*0Sstevel@tonic-gate #include <sys/mode.h>
50*0Sstevel@tonic-gate #include <sys/mkdev.h>
51*0Sstevel@tonic-gate #include <sys/promif.h>
52*0Sstevel@tonic-gate #include <sys/consdev.h>
53*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
54*0Sstevel@tonic-gate #include <sys/poll.h>
55*0Sstevel@tonic-gate #include <sys/note.h>
56*0Sstevel@tonic-gate #include <sys/taskq.h>
57*0Sstevel@tonic-gate #include <sys/policy.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Minor number is instance<<8 + clone minor from range 1-255; (0 reserved
61*0Sstevel@tonic-gate  * for "original"
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate #define	PM_MINOR_TO_CLONE(minor) ((minor) & (PM_MAX_CLONE - 1))
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	PM_NUMCMPTS(dip) (DEVI(dip)->devi_pm_num_components)
66*0Sstevel@tonic-gate #define	PM_IS_CFB(dip) (DEVI(dip)->devi_pm_flags & PMC_CONSOLE_FB)
67*0Sstevel@tonic-gate #define	PM_MAJOR(dip) ddi_driver_major(dip)
68*0Sstevel@tonic-gate #define	PM_RELE(dip) ddi_release_devi(dip)
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define	PM_IDLEDOWN_TIME	10
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate extern kmutex_t	pm_scan_lock;	/* protects autopm_enable, pm_scans_disabled */
73*0Sstevel@tonic-gate extern kmutex_t	pm_clone_lock;	/* protects pm_clones array */
74*0Sstevel@tonic-gate extern int	autopm_enabled;
75*0Sstevel@tonic-gate extern kcondvar_t pm_clones_cv[PM_MAX_CLONE];
76*0Sstevel@tonic-gate extern uint_t	pm_poll_cnt[PM_MAX_CLONE];
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * The soft state of the power manager.  Since there will only
80*0Sstevel@tonic-gate  * one of these, just reference it through a static pointer.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate static struct pmstate {
83*0Sstevel@tonic-gate 	dev_info_t	*pm_dip;		/* ptr to our dev_info node */
84*0Sstevel@tonic-gate 	int		pm_instance;		/* for ddi_get_instance() */
85*0Sstevel@tonic-gate 	timeout_id_t	pm_idledown_id;		/* pm idledown timeout id */
86*0Sstevel@tonic-gate 	uchar_t		pm_clones[PM_MAX_CLONE]; /* uniqueify multiple opens */
87*0Sstevel@tonic-gate 	struct cred	*pm_cred[PM_MAX_CLONE];	/* cred for each unique open */
88*0Sstevel@tonic-gate } pm_state = { NULL, -1, (timeout_id_t)0 };
89*0Sstevel@tonic-gate typedef struct pmstate *pm_state_t;
90*0Sstevel@tonic-gate static pm_state_t pmstp = &pm_state;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static int	pm_open(dev_t *, int, int, cred_t *);
93*0Sstevel@tonic-gate static int	pm_close(dev_t, int, int, cred_t *);
94*0Sstevel@tonic-gate static int	pm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
95*0Sstevel@tonic-gate static int	pm_chpoll(dev_t, short, int, short *, struct pollhead **);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static struct cb_ops pm_cb_ops = {
98*0Sstevel@tonic-gate 	pm_open,	/* open */
99*0Sstevel@tonic-gate 	pm_close,	/* close */
100*0Sstevel@tonic-gate 	nodev,		/* strategy */
101*0Sstevel@tonic-gate 	nodev,		/* print */
102*0Sstevel@tonic-gate 	nodev,		/* dump */
103*0Sstevel@tonic-gate 	nodev,		/* read */
104*0Sstevel@tonic-gate 	nodev,		/* write */
105*0Sstevel@tonic-gate 	pm_ioctl,	/* ioctl */
106*0Sstevel@tonic-gate 	nodev,		/* devmap */
107*0Sstevel@tonic-gate 	nodev,		/* mmap */
108*0Sstevel@tonic-gate 	nodev,		/* segmap */
109*0Sstevel@tonic-gate 	pm_chpoll,	/* poll */
110*0Sstevel@tonic-gate 	ddi_prop_op,	/* prop_op */
111*0Sstevel@tonic-gate 	NULL,		/* streamtab */
112*0Sstevel@tonic-gate 	D_NEW | D_MP	/* driver compatibility flag */
113*0Sstevel@tonic-gate };
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static int pm_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
116*0Sstevel@tonic-gate     void **result);
117*0Sstevel@tonic-gate static int pm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
118*0Sstevel@tonic-gate static int pm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static struct dev_ops pm_ops = {
121*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
122*0Sstevel@tonic-gate 	0,			/* refcnt */
123*0Sstevel@tonic-gate 	pm_getinfo,		/* info */
124*0Sstevel@tonic-gate 	nulldev,		/* identify */
125*0Sstevel@tonic-gate 	nulldev,		/* probe */
126*0Sstevel@tonic-gate 	pm_attach,		/* attach */
127*0Sstevel@tonic-gate 	pm_detach,		/* detach */
128*0Sstevel@tonic-gate 	nodev,			/* reset */
129*0Sstevel@tonic-gate 	&pm_cb_ops,		/* driver operations */
130*0Sstevel@tonic-gate 	NULL,			/* bus operations */
131*0Sstevel@tonic-gate 	NULL			/* power */
132*0Sstevel@tonic-gate };
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate static struct modldrv modldrv = {
135*0Sstevel@tonic-gate 	&mod_driverops,
136*0Sstevel@tonic-gate 	"power management driver v%I%",
137*0Sstevel@tonic-gate 	&pm_ops
138*0Sstevel@tonic-gate };
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
141*0Sstevel@tonic-gate 	MODREV_1, &modldrv, 0
142*0Sstevel@tonic-gate };
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /* Local functions */
145*0Sstevel@tonic-gate #ifdef DEBUG
146*0Sstevel@tonic-gate static int	print_info(dev_info_t *, void *);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate #endif
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate int
151*0Sstevel@tonic-gate _init(void)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate int
157*0Sstevel@tonic-gate _fini(void)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate int
163*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate static int
169*0Sstevel@tonic-gate pm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	int		i;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	switch (cmd) {
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	case DDI_ATTACH:
176*0Sstevel@tonic-gate 		if (pmstp->pm_instance != -1)	/* Only allow one instance */
177*0Sstevel@tonic-gate 			return (DDI_FAILURE);
178*0Sstevel@tonic-gate 		pmstp->pm_instance = ddi_get_instance(dip);
179*0Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "pm", S_IFCHR,
180*0Sstevel@tonic-gate 		    (pmstp->pm_instance << 8) + 0,
181*0Sstevel@tonic-gate 			DDI_PSEUDO, 0) != DDI_SUCCESS) {
182*0Sstevel@tonic-gate 			return (DDI_FAILURE);
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		pmstp->pm_dip = dip;	/* pm_init and getinfo depend on it */
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		for (i = 0; i < PM_MAX_CLONE; i++)
187*0Sstevel@tonic-gate 			cv_init(&pm_clones_cv[i], NULL, CV_DEFAULT, NULL);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		ddi_report_dev(dip);
190*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	default:
193*0Sstevel@tonic-gate 		return (DDI_FAILURE);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate /* ARGSUSED */
198*0Sstevel@tonic-gate static int
199*0Sstevel@tonic-gate pm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	int i;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	switch (cmd) {
204*0Sstevel@tonic-gate 	case DDI_DETACH:
205*0Sstevel@tonic-gate 		/*
206*0Sstevel@tonic-gate 		 * Don't detach while idledown timeout is pending.  Note that
207*0Sstevel@tonic-gate 		 * we already know we're not in pm_ioctl() due to framework
208*0Sstevel@tonic-gate 		 * synchronization, so this is a sufficient test
209*0Sstevel@tonic-gate 		 */
210*0Sstevel@tonic-gate 		if (pmstp->pm_idledown_id)
211*0Sstevel@tonic-gate 			return (DDI_FAILURE);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 		for (i = 0; i < PM_MAX_CLONE; i++)
214*0Sstevel@tonic-gate 			cv_destroy(&pm_clones_cv[i]);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
217*0Sstevel@tonic-gate 		pmstp->pm_instance = -1;
218*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	default:
221*0Sstevel@tonic-gate 		return (DDI_FAILURE);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate static int
226*0Sstevel@tonic-gate pm_close_direct_pm_device(dev_info_t *dip, void *arg)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	int clone;
229*0Sstevel@tonic-gate 	char *pathbuf;
230*0Sstevel@tonic-gate 	pm_info_t *info = PM_GET_PM_INFO(dip);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	clone = *((int *)arg);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (!info)
235*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
238*0Sstevel@tonic-gate 	PM_LOCK_DIP(dip);
239*0Sstevel@tonic-gate 	if (clone == info->pmi_clone) {
240*0Sstevel@tonic-gate 		PMD(PMD_CLOSE, ("pm_close: found %s@%s(%s#%d)\n",
241*0Sstevel@tonic-gate 		    PM_DEVICE(dip)))
242*0Sstevel@tonic-gate 		ASSERT(PM_ISDIRECT(dip));
243*0Sstevel@tonic-gate 		info->pmi_dev_pm_state &= ~PM_DIRECT;
244*0Sstevel@tonic-gate 		PM_UNLOCK_DIP(dip);
245*0Sstevel@tonic-gate 		pm_proceed(dip, PMP_RELEASE, -1, -1);
246*0Sstevel@tonic-gate 		/* Bring ourselves up if there is a keeper that is up */
247*0Sstevel@tonic-gate 		(void) ddi_pathname(dip, pathbuf);
248*0Sstevel@tonic-gate 		pm_dispatch_to_dep_thread(PM_DEP_WK_BRINGUP_SELF, NULL,
249*0Sstevel@tonic-gate 		    pathbuf, PM_DEP_NOWAIT, NULL, 0);
250*0Sstevel@tonic-gate 		PM_LOCK_DIP(dip);
251*0Sstevel@tonic-gate 		info->pmi_clone = 0;
252*0Sstevel@tonic-gate 		PM_UNLOCK_DIP(dip);
253*0Sstevel@tonic-gate 	} else {
254*0Sstevel@tonic-gate 		PM_UNLOCK_DIP(dip);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 	kmem_free(pathbuf, MAXPATHLEN);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/* restart autopm on device released from direct pm */
259*0Sstevel@tonic-gate 	pm_rescan(dip);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate #define	PM_REQ		1
265*0Sstevel@tonic-gate #define	NOSTRUCT	2
266*0Sstevel@tonic-gate #define	DIP		3
267*0Sstevel@tonic-gate #define	NODIP		4
268*0Sstevel@tonic-gate #define	NODEP		5
269*0Sstevel@tonic-gate #define	DEP		6
270*0Sstevel@tonic-gate #define	PM_PSC		7
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate #define	CHECKPERMS	0x001
273*0Sstevel@tonic-gate #define	SU		0x002
274*0Sstevel@tonic-gate #define	SG		0x004
275*0Sstevel@tonic-gate #define	OWNER		0x008
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate #define	INWHO		0x001
278*0Sstevel@tonic-gate #define	INDATAINT	0x002
279*0Sstevel@tonic-gate #define	INDATASTRING	0x004
280*0Sstevel@tonic-gate #define	INDEP		0x008
281*0Sstevel@tonic-gate #define	INDATAOUT	0x010
282*0Sstevel@tonic-gate #define	INDATA	(INDATAOUT | INDATAINT | INDATASTRING | INDEP)
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate struct pm_cmd_info {
285*0Sstevel@tonic-gate 	int cmd;		/* command code */
286*0Sstevel@tonic-gate 	char *name;		/* printable string */
287*0Sstevel@tonic-gate 	int supported;		/* true if still supported */
288*0Sstevel@tonic-gate 	int str_type;		/* PM_REQ or NOSTRUCT */
289*0Sstevel@tonic-gate 	int inargs;		/* INWHO, INDATAINT, INDATASTRING, INDEP, */
290*0Sstevel@tonic-gate 				/* INDATAOUT */
291*0Sstevel@tonic-gate 	int diptype;		/* DIP or NODIP */
292*0Sstevel@tonic-gate 	int deptype;		/* DEP or NODEP */
293*0Sstevel@tonic-gate 	int permission;		/* SU, GU, or CHECKPERMS */
294*0Sstevel@tonic-gate };
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate #ifdef DEBUG
297*0Sstevel@tonic-gate char *pm_cmd_string;
298*0Sstevel@tonic-gate int pm_cmd;
299*0Sstevel@tonic-gate #endif
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  * Returns true if permission granted by credentials
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate static int
305*0Sstevel@tonic-gate pm_perms(int perm, cred_t *cr)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate 	if (perm == 0)			/* no restrictions */
308*0Sstevel@tonic-gate 		return (1);
309*0Sstevel@tonic-gate 	if (perm == CHECKPERMS)		/* ok for now (is checked later) */
310*0Sstevel@tonic-gate 		return (1);
311*0Sstevel@tonic-gate 	if ((perm & SU) && secpolicy_power_mgmt(cr) == 0) /* privileged? */
312*0Sstevel@tonic-gate 		return (1);
313*0Sstevel@tonic-gate 	if ((perm & SG) && (crgetgid(cr) == 0))	/* group 0 is ok */
314*0Sstevel@tonic-gate 		return (1);
315*0Sstevel@tonic-gate 	return (0);
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate #ifdef DEBUG
319*0Sstevel@tonic-gate static int
320*0Sstevel@tonic-gate print_info(dev_info_t *dip, void *arg)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
323*0Sstevel@tonic-gate 	pm_info_t	*info;
324*0Sstevel@tonic-gate 	int		i, j;
325*0Sstevel@tonic-gate 	struct pm_component *cp;
326*0Sstevel@tonic-gate 	extern int pm_cur_power(pm_component_t *cp);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	info = PM_GET_PM_INFO(dip);
329*0Sstevel@tonic-gate 	if (!info)
330*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
331*0Sstevel@tonic-gate 	cmn_err(CE_CONT, "pm_info for %s\n", ddi_node_name(dip));
332*0Sstevel@tonic-gate 	for (i = 0; i < PM_NUMCMPTS(dip); i++) {
333*0Sstevel@tonic-gate 		cp = PM_CP(dip, i);
334*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tThresholds[%d] =",  i);
335*0Sstevel@tonic-gate 		for (j = 0; j < cp->pmc_comp.pmc_numlevels; j++)
336*0Sstevel@tonic-gate 			cmn_err(CE_CONT, " %d", cp->pmc_comp.pmc_thresh[i]);
337*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "\n");
338*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tCurrent power[%d] = %d\n", i,
339*0Sstevel@tonic-gate 		    pm_cur_power(cp));
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 	if (PM_ISDIRECT(dip))
342*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tDirect power management\n");
343*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate #endif
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate /*
348*0Sstevel@tonic-gate  * command, name, supported, str_type, inargs, diptype, deptype, permission
349*0Sstevel@tonic-gate  */
350*0Sstevel@tonic-gate static struct pm_cmd_info pmci[] = {
351*0Sstevel@tonic-gate 	{PM_SCHEDULE, "PM_SCHEDULE", 0},
352*0Sstevel@tonic-gate 	{PM_GET_IDLE_TIME, "PM_GET_IDLE_TIME", 0},
353*0Sstevel@tonic-gate 	{PM_GET_NUM_CMPTS, "PM_GET_NUM_CMPTS", 0},
354*0Sstevel@tonic-gate 	{PM_GET_THRESHOLD, "PM_GET_THRESHOLD", 0},
355*0Sstevel@tonic-gate 	{PM_SET_THRESHOLD, "PM_SET_THRESHOLD", 0},
356*0Sstevel@tonic-gate 	{PM_GET_NORM_PWR, "PM_GET_NORM_PWR", 0},
357*0Sstevel@tonic-gate 	{PM_SET_CUR_PWR, "PM_SET_CUR_PWR", 0},
358*0Sstevel@tonic-gate 	{PM_GET_CUR_PWR, "PM_GET_CUR_PWR", 0},
359*0Sstevel@tonic-gate 	{PM_GET_NUM_DEPS, "PM_GET_NUM_DEPS", 0},
360*0Sstevel@tonic-gate 	{PM_GET_DEP, "PM_GET_DEP", 0},
361*0Sstevel@tonic-gate 	{PM_ADD_DEP, "PM_ADD_DEP", 0},
362*0Sstevel@tonic-gate 	{PM_REM_DEP, "PM_REM_DEP", 0},
363*0Sstevel@tonic-gate 	{PM_REM_DEVICE, "PM_REM_DEVICE", 0},
364*0Sstevel@tonic-gate 	{PM_REM_DEVICES, "PM_REM_DEVICES", 0},
365*0Sstevel@tonic-gate 	{PM_REPARSE_PM_PROPS, "PM_REPARSE_PM_PROPS", 1, PM_REQ, INWHO, DIP,
366*0Sstevel@tonic-gate 	    NODEP},
367*0Sstevel@tonic-gate 	{PM_DISABLE_AUTOPM, "PM_DISABLE_AUTOPM", 0},
368*0Sstevel@tonic-gate 	{PM_REENABLE_AUTOPM, "PM_REENABLE_AUTOPM", 0},
369*0Sstevel@tonic-gate 	{PM_SET_NORM_PWR, "PM_SET_NORM_PWR", 0 },
370*0Sstevel@tonic-gate 	{PM_SET_DEVICE_THRESHOLD, "PM_SET_DEVICE_THRESHOLD", 1, PM_REQ,
371*0Sstevel@tonic-gate 	    INWHO, NODIP, NODEP, SU},
372*0Sstevel@tonic-gate 	{PM_GET_SYSTEM_THRESHOLD, "PM_GET_SYSTEM_THRESHOLD", 1, NOSTRUCT},
373*0Sstevel@tonic-gate 	{PM_GET_DEFAULT_SYSTEM_THRESHOLD, "PM_GET_DEFAULT_SYSTEM_THRESHOLD",
374*0Sstevel@tonic-gate 	    1, NOSTRUCT},
375*0Sstevel@tonic-gate 	{PM_SET_SYSTEM_THRESHOLD, "PM_SET_SYSTEM_THRESHOLD", 1, NOSTRUCT,
376*0Sstevel@tonic-gate 	    0, 0, 0, SU},
377*0Sstevel@tonic-gate 	{PM_START_PM, "PM_START_PM", 1, NOSTRUCT, 0, 0, 0, SU},
378*0Sstevel@tonic-gate 	{PM_STOP_PM, "PM_STOP_PM", 1, NOSTRUCT, 0, 0, 0, SU},
379*0Sstevel@tonic-gate 	{PM_RESET_PM, "PM_RESET_PM", 1, NOSTRUCT, 0, 0, 0, SU},
380*0Sstevel@tonic-gate 	{PM_GET_STATS, "PM_GET_STATS", 1, PM_REQ, INWHO | INDATAOUT,
381*0Sstevel@tonic-gate 	    DIP, NODEP},
382*0Sstevel@tonic-gate 	{PM_GET_DEVICE_THRESHOLD, "PM_GET_DEVICE_THRESHOLD", 1, PM_REQ, INWHO,
383*0Sstevel@tonic-gate 	    DIP, NODEP},
384*0Sstevel@tonic-gate 	{PM_GET_POWER_NAME, "PM_GET_POWER_NAME", 1, PM_REQ, INWHO | INDATAOUT,
385*0Sstevel@tonic-gate 	    DIP, NODEP},
386*0Sstevel@tonic-gate 	{PM_GET_POWER_LEVELS, "PM_GET_POWER_LEVELS", 1, PM_REQ,
387*0Sstevel@tonic-gate 	    INWHO | INDATAOUT, DIP, NODEP},
388*0Sstevel@tonic-gate 	{PM_GET_NUM_COMPONENTS, "PM_GET_NUM_COMPONENTS", 1, PM_REQ, INWHO,
389*0Sstevel@tonic-gate 	    DIP, NODEP},
390*0Sstevel@tonic-gate 	{PM_GET_COMPONENT_NAME, "PM_GET_COMPONENT_NAME", 1, PM_REQ,
391*0Sstevel@tonic-gate 	    INWHO | INDATAOUT, DIP, NODEP},
392*0Sstevel@tonic-gate 	{PM_GET_NUM_POWER_LEVELS, "PM_GET_NUM_POWER_LEVELS", 1, PM_REQ, INWHO,
393*0Sstevel@tonic-gate 	    DIP, NODEP},
394*0Sstevel@tonic-gate 	{PM_GET_STATE_CHANGE, "PM_GET_STATE_CHANGE", 1, PM_PSC},
395*0Sstevel@tonic-gate 	{PM_GET_STATE_CHANGE_WAIT, "PM_GET_STATE_CHANGE_WAIT", 1, PM_PSC},
396*0Sstevel@tonic-gate 	{PM_DIRECT_PM, "PM_DIRECT_PM", 1, PM_REQ, INWHO, DIP, NODEP,
397*0Sstevel@tonic-gate 	    (SU | SG)},
398*0Sstevel@tonic-gate 	{PM_RELEASE_DIRECT_PM, "PM_RELEASE_DIRECT_PM", 1, PM_REQ, INWHO,
399*0Sstevel@tonic-gate 	    DIP, NODEP},
400*0Sstevel@tonic-gate 	{PM_DIRECT_NOTIFY, "PM_DIRECT_NOTIFY", 1, PM_PSC},
401*0Sstevel@tonic-gate 	{PM_DIRECT_NOTIFY_WAIT, "PM_DIRECT_NOTIFY_WAIT", 1, PM_PSC},
402*0Sstevel@tonic-gate 	{PM_RESET_DEVICE_THRESHOLD, "PM_RESET_DEVICE_THRESHOLD", 1, PM_REQ,
403*0Sstevel@tonic-gate 	    INWHO, DIP, NODEP, SU},
404*0Sstevel@tonic-gate 	{PM_GET_PM_STATE, "PM_GET_PM_STATE", 1, NOSTRUCT},
405*0Sstevel@tonic-gate 	{PM_GET_DEVICE_TYPE, "PM_GET_DEVICE_TYPE", 1, PM_REQ, INWHO,
406*0Sstevel@tonic-gate 	    DIP, NODEP},
407*0Sstevel@tonic-gate 	{PM_SET_COMPONENT_THRESHOLDS, "PM_SET_COMPONENT_THRESHOLDS", 1, PM_REQ,
408*0Sstevel@tonic-gate 	    INWHO | INDATAINT, NODIP, NODEP, SU},
409*0Sstevel@tonic-gate 	{PM_GET_COMPONENT_THRESHOLDS, "PM_GET_COMPONENT_THRESHOLDS", 1, PM_REQ,
410*0Sstevel@tonic-gate 	    INWHO | INDATAOUT, DIP, NODEP},
411*0Sstevel@tonic-gate 	{PM_IDLE_DOWN, "PM_IDLE_DOWN", 1, NOSTRUCT, 0, 0, 0, SU},
412*0Sstevel@tonic-gate 	{PM_GET_DEVICE_THRESHOLD_BASIS, "PM_GET_DEVICE_THRESHOLD_BASIS", 1,
413*0Sstevel@tonic-gate 	    PM_REQ, INWHO, DIP, NODEP},
414*0Sstevel@tonic-gate 	{PM_SET_CURRENT_POWER, "PM_SET_CURRENT_POWER", 1, PM_REQ, INWHO, DIP,
415*0Sstevel@tonic-gate 	    NODEP},
416*0Sstevel@tonic-gate 	{PM_GET_CURRENT_POWER, "PM_GET_CURRENT_POWER", 1, PM_REQ, INWHO, DIP,
417*0Sstevel@tonic-gate 	    NODEP},
418*0Sstevel@tonic-gate 	{PM_GET_FULL_POWER, "PM_GET_FULL_POWER", 1, PM_REQ, INWHO, DIP,
419*0Sstevel@tonic-gate 	    NODEP},
420*0Sstevel@tonic-gate 	{PM_ADD_DEPENDENT, "PM_ADD_DEPENDENT", 1, PM_REQ, INWHO | INDATASTRING,
421*0Sstevel@tonic-gate 	    DIP, DEP, SU},
422*0Sstevel@tonic-gate 	{PM_GET_TIME_IDLE, "PM_GET_TIME_IDLE", 1, PM_REQ, INWHO, DIP, NODEP},
423*0Sstevel@tonic-gate 	{PM_ADD_DEPENDENT_PROPERTY, "PM_ADD_DEPENDENT_PROPERTY", 1, PM_REQ,
424*0Sstevel@tonic-gate 	    INWHO | INDATASTRING, NODIP, DEP, SU},
425*0Sstevel@tonic-gate 	{0, NULL}
426*0Sstevel@tonic-gate };
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate struct pm_cmd_info *
429*0Sstevel@tonic-gate pc_info(int cmd)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	struct pm_cmd_info *pcip;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	for (pcip = pmci; pcip->name; pcip++) {
434*0Sstevel@tonic-gate 		if (cmd == pcip->cmd)
435*0Sstevel@tonic-gate 			return (pcip);
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 	return (NULL);
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate static char *
441*0Sstevel@tonic-gate pm_decode_cmd(int cmd)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate 	static char invbuf[64];
444*0Sstevel@tonic-gate 	struct pm_cmd_info *pcip = pc_info(cmd);
445*0Sstevel@tonic-gate 	if (pcip != NULL)
446*0Sstevel@tonic-gate 		return (pcip->name);
447*0Sstevel@tonic-gate 	(void) sprintf(invbuf, "ioctl: invalid command %d\n", cmd);
448*0Sstevel@tonic-gate 	return (invbuf);
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate /*
452*0Sstevel@tonic-gate  * Allocate scan resource, create taskq, then dispatch scan,
453*0Sstevel@tonic-gate  * called only if autopm is enabled.
454*0Sstevel@tonic-gate  */
455*0Sstevel@tonic-gate int
456*0Sstevel@tonic-gate pm_start_pm_walk(dev_info_t *dip, void *arg)
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	char *cmdstr = pm_decode_cmd(*((int *)arg));
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	if (!PM_GET_PM_INFO(dip) || PM_ISBC(dip))
461*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	/*
464*0Sstevel@tonic-gate 	 * Construct per dip scan taskq
465*0Sstevel@tonic-gate 	 */
466*0Sstevel@tonic-gate 	mutex_enter(&pm_scan_lock);
467*0Sstevel@tonic-gate 	if (autopm_enabled)
468*0Sstevel@tonic-gate 		pm_scan_init(dip);
469*0Sstevel@tonic-gate 	mutex_exit(&pm_scan_lock);
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	/*
472*0Sstevel@tonic-gate 	 * Start doing pm on device: ensure pm_scan data structure initiated,
473*0Sstevel@tonic-gate 	 * no need to gurantee a successful scan run.
474*0Sstevel@tonic-gate 	 */
475*0Sstevel@tonic-gate 	PMD(PMD_SCAN | PMD_IOCTL, ("ioctl: %s: scan %s@%s(%s#%d)\n", cmdstr,
476*0Sstevel@tonic-gate 	    PM_DEVICE(dip)))
477*0Sstevel@tonic-gate 	pm_rescan(dip);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate /*
483*0Sstevel@tonic-gate  * Bring devices to full power level, then stop scan
484*0Sstevel@tonic-gate  */
485*0Sstevel@tonic-gate int
486*0Sstevel@tonic-gate pm_stop_pm_walk(dev_info_t *dip, void *arg)
487*0Sstevel@tonic-gate {
488*0Sstevel@tonic-gate 	pm_info_t *info = PM_GET_PM_INFO(dip);
489*0Sstevel@tonic-gate 	char *cmdstr = pm_decode_cmd(*((int *)arg));
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	if (!info)
492*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
493*0Sstevel@tonic-gate 	/*
494*0Sstevel@tonic-gate 	 * Stop the current scan, and then bring it back to normal power.
495*0Sstevel@tonic-gate 	 */
496*0Sstevel@tonic-gate 	if (!PM_ISBC(dip)) {
497*0Sstevel@tonic-gate 		PMD(PMD_SCAN | PMD_IOCTL, ("ioctl: %s: stop scan for "
498*0Sstevel@tonic-gate 		    "%s@%s(%s#%d)\n", cmdstr, PM_DEVICE(dip)))
499*0Sstevel@tonic-gate 		pm_scan_stop(dip);
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	if (!PM_ISBC(dip) && !PM_ISDIRECT(dip) &&
503*0Sstevel@tonic-gate 	    !pm_all_at_normal(dip)) {
504*0Sstevel@tonic-gate 		PM_LOCK_DIP(dip);
505*0Sstevel@tonic-gate 		if (info->pmi_dev_pm_state & PM_DETACHING) {
506*0Sstevel@tonic-gate 			PMD(PMD_ALLNORM, ("ioctl: %s: deferring "
507*0Sstevel@tonic-gate 			    "all_to_normal because %s@%s(%s#%d) is detaching\n",
508*0Sstevel@tonic-gate 			    cmdstr, PM_DEVICE(dip)))
509*0Sstevel@tonic-gate 			info->pmi_dev_pm_state |= PM_ALLNORM_DEFERRED;
510*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
511*0Sstevel@tonic-gate 			return (DDI_WALK_CONTINUE);
512*0Sstevel@tonic-gate 		}
513*0Sstevel@tonic-gate 		PM_UNLOCK_DIP(dip);
514*0Sstevel@tonic-gate 		if (pm_all_to_normal(dip, PM_CANBLOCK_FAIL) != DDI_SUCCESS) {
515*0Sstevel@tonic-gate 			PMD(PMD_ERROR, ("ioctl: %s: could not bring %s@%s"
516*0Sstevel@tonic-gate 			    "(%s#%d) to normal\n", cmdstr, PM_DEVICE(dip)))
517*0Sstevel@tonic-gate 		}
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate static int
524*0Sstevel@tonic-gate pm_start_idledown(dev_info_t *dip, void *arg)
525*0Sstevel@tonic-gate {
526*0Sstevel@tonic-gate 	int		flag = (int)(intptr_t)arg;
527*0Sstevel@tonic-gate 	pm_scan_t	*scanp = PM_GET_PM_SCAN(dip);
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	if (!scanp)
530*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	PM_LOCK_DIP(dip);
533*0Sstevel@tonic-gate 	scanp->ps_idle_down |= flag;
534*0Sstevel@tonic-gate 	PM_UNLOCK_DIP(dip);
535*0Sstevel@tonic-gate 	pm_rescan(dip);
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*ARGSUSED*/
541*0Sstevel@tonic-gate static int
542*0Sstevel@tonic-gate pm_end_idledown(dev_info_t *dip, void *ignore)
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	pm_scan_t	*scanp = PM_GET_PM_SCAN(dip);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (!scanp)
547*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	PM_LOCK_DIP(dip);
550*0Sstevel@tonic-gate 	/*
551*0Sstevel@tonic-gate 	 * The PMID_TIMERS bits are place holder till idledown expires.
552*0Sstevel@tonic-gate 	 * The bits are also the base for regenerating PMID_SCANS bits.
553*0Sstevel@tonic-gate 	 * While it's up to scan thread to clear up the PMID_SCANS bits
554*0Sstevel@tonic-gate 	 * after each scan run, PMID_TIMERS ensure aggressive scan down
555*0Sstevel@tonic-gate 	 * performance throughout the idledown period.
556*0Sstevel@tonic-gate 	 */
557*0Sstevel@tonic-gate 	scanp->ps_idle_down &= ~PMID_TIMERS;
558*0Sstevel@tonic-gate 	PM_UNLOCK_DIP(dip);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
561*0Sstevel@tonic-gate }
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate /*ARGSUSED*/
564*0Sstevel@tonic-gate static void
565*0Sstevel@tonic-gate pm_end_idledown_walk(void *ignore)
566*0Sstevel@tonic-gate {
567*0Sstevel@tonic-gate 	PMD(PMD_IDLEDOWN, ("ioctl: end_idledown: idledown_id(%lx) timer is "
568*0Sstevel@tonic-gate 	    "off\n", (ulong_t)pmstp->pm_idledown_id));
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	mutex_enter(&pm_scan_lock);
571*0Sstevel@tonic-gate 	pmstp->pm_idledown_id = 0;
572*0Sstevel@tonic-gate 	mutex_exit(&pm_scan_lock);
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), pm_end_idledown, NULL);
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate /*
578*0Sstevel@tonic-gate  * pm_timeout_idledown - keep idledown effect for 10 seconds.
579*0Sstevel@tonic-gate  *
580*0Sstevel@tonic-gate  * Return 0 if another competing caller scheduled idledown timeout,
581*0Sstevel@tonic-gate  * otherwise, return idledown timeout_id.
582*0Sstevel@tonic-gate  */
583*0Sstevel@tonic-gate static timeout_id_t
584*0Sstevel@tonic-gate pm_timeout_idledown(void)
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate 	timeout_id_t	to_id;
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	/*
589*0Sstevel@tonic-gate 	 * Keep idle-down in effect for either 10 seconds
590*0Sstevel@tonic-gate 	 * or length of a scan interval, which ever is greater.
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	mutex_enter(&pm_scan_lock);
593*0Sstevel@tonic-gate 	if (pmstp->pm_idledown_id != 0) {
594*0Sstevel@tonic-gate 		to_id = pmstp->pm_idledown_id;
595*0Sstevel@tonic-gate 		pmstp->pm_idledown_id = 0;
596*0Sstevel@tonic-gate 		mutex_exit(&pm_scan_lock);
597*0Sstevel@tonic-gate 		(void) untimeout(to_id);
598*0Sstevel@tonic-gate 		mutex_enter(&pm_scan_lock);
599*0Sstevel@tonic-gate 		if (pmstp->pm_idledown_id != 0) {
600*0Sstevel@tonic-gate 			PMD(PMD_IDLEDOWN, ("ioctl: timeout_idledown: "
601*0Sstevel@tonic-gate 			    "another caller got it, idledown_id(%lx)!\n",
602*0Sstevel@tonic-gate 			    (ulong_t)pmstp->pm_idledown_id))
603*0Sstevel@tonic-gate 			mutex_exit(&pm_scan_lock);
604*0Sstevel@tonic-gate 			return (0);
605*0Sstevel@tonic-gate 		}
606*0Sstevel@tonic-gate 	}
607*0Sstevel@tonic-gate 	pmstp->pm_idledown_id = timeout(pm_end_idledown_walk, NULL,
608*0Sstevel@tonic-gate 	    PM_IDLEDOWN_TIME * hz);
609*0Sstevel@tonic-gate 	PMD(PMD_IDLEDOWN, ("ioctl: timeout_idledown: idledown_id(%lx)\n",
610*0Sstevel@tonic-gate 	    (ulong_t)pmstp->pm_idledown_id))
611*0Sstevel@tonic-gate 	mutex_exit(&pm_scan_lock);
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	return (pmstp->pm_idledown_id);
614*0Sstevel@tonic-gate }
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate static int
617*0Sstevel@tonic-gate pm_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
618*0Sstevel@tonic-gate 	struct pollhead **phpp)
619*0Sstevel@tonic-gate {
620*0Sstevel@tonic-gate 	extern struct pollhead pm_pollhead;	/* common/os/sunpm.c */
621*0Sstevel@tonic-gate 	int	clone;
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	clone = PM_MINOR_TO_CLONE(getminor(dev));
624*0Sstevel@tonic-gate 	PMD(PMD_IOCTL, ("ioctl: pm_chpoll: clone %d\n", clone))
625*0Sstevel@tonic-gate 	if ((events & (POLLIN | POLLRDNORM)) && pm_poll_cnt[clone]) {
626*0Sstevel@tonic-gate 		*reventsp |= (POLLIN | POLLRDNORM);
627*0Sstevel@tonic-gate 		PMD(PMD_IOCTL, ("ioctl: pm_chpoll: reventsp set\n"))
628*0Sstevel@tonic-gate 	} else {
629*0Sstevel@tonic-gate 		*reventsp = 0;
630*0Sstevel@tonic-gate 		if (!anyyet) {
631*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: pm_chpoll: not anyyet\n"))
632*0Sstevel@tonic-gate 			*phpp = &pm_pollhead;
633*0Sstevel@tonic-gate 		}
634*0Sstevel@tonic-gate #ifdef DEBUG
635*0Sstevel@tonic-gate 		else {
636*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: pm_chpoll: anyyet\n"))
637*0Sstevel@tonic-gate 		}
638*0Sstevel@tonic-gate #endif
639*0Sstevel@tonic-gate 	}
640*0Sstevel@tonic-gate 	return (0);
641*0Sstevel@tonic-gate }
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate /*
644*0Sstevel@tonic-gate  * called by pm_dicard_entries to free up the memory. It also decrements
645*0Sstevel@tonic-gate  * pm_poll_cnt, if direct is non zero.
646*0Sstevel@tonic-gate  */
647*0Sstevel@tonic-gate static void
648*0Sstevel@tonic-gate pm_free_entries(psce_t *pscep, int clone, int direct)
649*0Sstevel@tonic-gate {
650*0Sstevel@tonic-gate 	pm_state_change_t	*p;
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	if (pscep) {
653*0Sstevel@tonic-gate 		p = pscep->psce_out;
654*0Sstevel@tonic-gate 		while (p->size) {
655*0Sstevel@tonic-gate 			if (direct) {
656*0Sstevel@tonic-gate 				PMD(PMD_IOCTL, ("ioctl: discard: "
657*0Sstevel@tonic-gate 				    "pm_poll_cnt[%d] is %d before "
658*0Sstevel@tonic-gate 				    "ASSERT\n", clone,
659*0Sstevel@tonic-gate 				    pm_poll_cnt[clone]))
660*0Sstevel@tonic-gate 				ASSERT(pm_poll_cnt[clone]);
661*0Sstevel@tonic-gate 				pm_poll_cnt[clone]--;
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 			kmem_free(p->physpath, p->size);
664*0Sstevel@tonic-gate 			p->size = 0;
665*0Sstevel@tonic-gate 			if (p == pscep->psce_last)
666*0Sstevel@tonic-gate 				p = pscep->psce_first;
667*0Sstevel@tonic-gate 			else
668*0Sstevel@tonic-gate 				p++;
669*0Sstevel@tonic-gate 		}
670*0Sstevel@tonic-gate 		pscep->psce_out = pscep->psce_first;
671*0Sstevel@tonic-gate 		pscep->psce_in = pscep->psce_first;
672*0Sstevel@tonic-gate 		mutex_exit(&pscep->psce_lock);
673*0Sstevel@tonic-gate 	}
674*0Sstevel@tonic-gate }
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate /*
677*0Sstevel@tonic-gate  * Discard entries for this clone. Calls pm_free_entries to free up memory.
678*0Sstevel@tonic-gate  */
679*0Sstevel@tonic-gate static void
680*0Sstevel@tonic-gate pm_discard_entries(int clone)
681*0Sstevel@tonic-gate {
682*0Sstevel@tonic-gate 	psce_t	*pscep;
683*0Sstevel@tonic-gate 	psce_t			*pm_psc_clone_to_direct(int);
684*0Sstevel@tonic-gate 	psce_t			*pm_psc_clone_to_interest(int);
685*0Sstevel@tonic-gate 	int			direct = 0;
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	mutex_enter(&pm_clone_lock);
688*0Sstevel@tonic-gate 	if ((pscep = pm_psc_clone_to_direct(clone)) != NULL)
689*0Sstevel@tonic-gate 		direct = 1;
690*0Sstevel@tonic-gate 	pm_free_entries(pscep, clone, direct);
691*0Sstevel@tonic-gate 	pscep = pm_psc_clone_to_interest(clone);
692*0Sstevel@tonic-gate 	pm_free_entries(pscep, clone, 0);
693*0Sstevel@tonic-gate 	mutex_exit(&pm_clone_lock);
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate int
697*0Sstevel@tonic-gate pm_set_sys_threshold(dev_info_t *dip, void *arg)
698*0Sstevel@tonic-gate {
699*0Sstevel@tonic-gate 	int pm_system_idle_threshold = *((int *)arg);
700*0Sstevel@tonic-gate 	pm_info_t *info = PM_GET_PM_INFO(dip);
701*0Sstevel@tonic-gate 	int	processed = 0;
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	if (!info)
704*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	if (!PM_ISBC(dip) && !PM_ISDIRECT(dip)) {
707*0Sstevel@tonic-gate 		switch (DEVI(dip)->devi_pm_flags & PMC_THRESH_ALL) {
708*0Sstevel@tonic-gate 		case PMC_DEF_THRESH:
709*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: set_sys_threshold: set "
710*0Sstevel@tonic-gate 			    "%s@%s(%s#%d) default thresh to 0t%d\n",
711*0Sstevel@tonic-gate 			    PM_DEVICE(dip), pm_system_idle_threshold))
712*0Sstevel@tonic-gate 			pm_set_device_threshold(dip, pm_system_idle_threshold,
713*0Sstevel@tonic-gate 			    PMC_DEF_THRESH);
714*0Sstevel@tonic-gate 			processed++;
715*0Sstevel@tonic-gate 			break;
716*0Sstevel@tonic-gate 		default:
717*0Sstevel@tonic-gate 			break;
718*0Sstevel@tonic-gate 		}
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	if (processed && autopm_enabled)
722*0Sstevel@tonic-gate 		pm_rescan(dip);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
725*0Sstevel@tonic-gate }
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate /*ARGSUSED*/
728*0Sstevel@tonic-gate static int
729*0Sstevel@tonic-gate pm_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
730*0Sstevel@tonic-gate {
731*0Sstevel@tonic-gate 	dev_t	dev;
732*0Sstevel@tonic-gate 	int	instance;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	switch (infocmd) {
735*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
736*0Sstevel@tonic-gate 		if (pmstp->pm_instance == -1)
737*0Sstevel@tonic-gate 			return (DDI_FAILURE);
738*0Sstevel@tonic-gate 		*result = pmstp->pm_dip;
739*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
742*0Sstevel@tonic-gate 		dev = (dev_t)arg;
743*0Sstevel@tonic-gate 		instance = getminor(dev) >> 8;
744*0Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
745*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	default:
748*0Sstevel@tonic-gate 		return (DDI_FAILURE);
749*0Sstevel@tonic-gate 	}
750*0Sstevel@tonic-gate }
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate /*ARGSUSED1*/
754*0Sstevel@tonic-gate static int
755*0Sstevel@tonic-gate pm_open(dev_t *devp, int flag, int otyp, cred_t *cr)
756*0Sstevel@tonic-gate {
757*0Sstevel@tonic-gate 	int		clone;
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
760*0Sstevel@tonic-gate 		return (EINVAL);
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	mutex_enter(&pm_clone_lock);
763*0Sstevel@tonic-gate 	for (clone = 1; clone < PM_MAX_CLONE; clone++)
764*0Sstevel@tonic-gate 		if (!pmstp->pm_clones[clone])
765*0Sstevel@tonic-gate 			break;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	if (clone == PM_MAX_CLONE) {
768*0Sstevel@tonic-gate 		mutex_exit(&pm_clone_lock);
769*0Sstevel@tonic-gate 		return (ENXIO);
770*0Sstevel@tonic-gate 	}
771*0Sstevel@tonic-gate 	pmstp->pm_cred[clone] = cr;
772*0Sstevel@tonic-gate 	crhold(cr);
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (pmstp->pm_instance << 8) + clone);
775*0Sstevel@tonic-gate 	pmstp->pm_clones[clone] = 1;
776*0Sstevel@tonic-gate 	mutex_exit(&pm_clone_lock);
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	return (0);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate /*ARGSUSED1*/
782*0Sstevel@tonic-gate static int
783*0Sstevel@tonic-gate pm_close(dev_t dev, int flag, int otyp, cred_t *cr)
784*0Sstevel@tonic-gate {
785*0Sstevel@tonic-gate 	int clone;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
788*0Sstevel@tonic-gate 		return (EINVAL);
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	clone = PM_MINOR_TO_CLONE(getminor(dev));
791*0Sstevel@tonic-gate 	PMD(PMD_CLOSE, ("pm_close: minor %x, clone %x\n", getminor(dev),
792*0Sstevel@tonic-gate 	    clone))
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 	/*
795*0Sstevel@tonic-gate 	 * Walk the entire device tree to find the corresponding
796*0Sstevel@tonic-gate 	 * device and operate on it.
797*0Sstevel@tonic-gate 	 */
798*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), pm_close_direct_pm_device,
799*0Sstevel@tonic-gate 	    (void *) &clone);
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	crfree(pmstp->pm_cred[clone]);
802*0Sstevel@tonic-gate 	pmstp->pm_cred[clone] = 0;
803*0Sstevel@tonic-gate 	pmstp->pm_clones[clone] = 0;
804*0Sstevel@tonic-gate 	pm_discard_entries(clone);
805*0Sstevel@tonic-gate 	ASSERT(pm_poll_cnt[clone] == 0);
806*0Sstevel@tonic-gate 	pm_deregister_watcher(clone, NULL);
807*0Sstevel@tonic-gate 	return (0);
808*0Sstevel@tonic-gate }
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate /*ARGSUSED*/
811*0Sstevel@tonic-gate static int
812*0Sstevel@tonic-gate pm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval_p)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	struct pm_cmd_info *pc_info(int);
815*0Sstevel@tonic-gate 	struct pm_cmd_info *pcip = pc_info(cmd);
816*0Sstevel@tonic-gate 	pm_req_t	req;
817*0Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
818*0Sstevel@tonic-gate 	pm_info_t	*info = NULL;
819*0Sstevel@tonic-gate 	int		clone;
820*0Sstevel@tonic-gate 	char		*cmdstr = pm_decode_cmd(cmd);
821*0Sstevel@tonic-gate 	/*
822*0Sstevel@tonic-gate 	 * To keep devinfo nodes from going away while we're holding a
823*0Sstevel@tonic-gate 	 * pointer to their dip, pm_name_to_dip() optionally holds
824*0Sstevel@tonic-gate 	 * the devinfo node.  If we've done that, we set dipheld
825*0Sstevel@tonic-gate 	 * so we know at the end of the ioctl processing to release the
826*0Sstevel@tonic-gate 	 * node again.
827*0Sstevel@tonic-gate 	 */
828*0Sstevel@tonic-gate 	int		dipheld = 0;
829*0Sstevel@tonic-gate 	int		icount = 0;
830*0Sstevel@tonic-gate 	int		i;
831*0Sstevel@tonic-gate 	int		comps;
832*0Sstevel@tonic-gate 	size_t		lencopied;
833*0Sstevel@tonic-gate 	int		ret = ENOTTY;
834*0Sstevel@tonic-gate 	int		curpower;
835*0Sstevel@tonic-gate 	char		who[MAXNAMELEN];
836*0Sstevel@tonic-gate 	size_t		wholen;			/* copyinstr length */
837*0Sstevel@tonic-gate 	size_t		deplen = MAXNAMELEN;
838*0Sstevel@tonic-gate 	char		*dep, i_dep_buf[MAXNAMELEN];
839*0Sstevel@tonic-gate 	char		*pathbuf;
840*0Sstevel@tonic-gate 	struct pm_component *cp;
841*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
842*0Sstevel@tonic-gate 	pm_state_change32_t		*pscp32;
843*0Sstevel@tonic-gate 	pm_state_change32_t		psc32;
844*0Sstevel@tonic-gate 	size_t				copysize32;
845*0Sstevel@tonic-gate #endif
846*0Sstevel@tonic-gate 	pm_state_change_t		*pscp;
847*0Sstevel@tonic-gate 	pm_state_change_t		psc;
848*0Sstevel@tonic-gate 	size_t		copysize;
849*0Sstevel@tonic-gate 	extern int	pm_default_idle_threshold;
850*0Sstevel@tonic-gate 	extern int	pm_system_idle_threshold;
851*0Sstevel@tonic-gate 	extern void	pm_record_thresh(pm_thresh_rec_t *);
852*0Sstevel@tonic-gate 	psce_t		*pm_psc_clone_to_direct(int);
853*0Sstevel@tonic-gate 	psce_t		*pm_psc_clone_to_interest(int);
854*0Sstevel@tonic-gate 	extern	void	pm_register_watcher(int, dev_info_t *);
855*0Sstevel@tonic-gate 	extern	int	pm_get_current_power(dev_info_t *, int, int *);
856*0Sstevel@tonic-gate 	extern	int	pm_interest_registered(int);
857*0Sstevel@tonic-gate 	extern	void	pm_all_to_default_thresholds(void);
858*0Sstevel@tonic-gate 	extern	int	pm_current_threshold(dev_info_t *, int, int *);
859*0Sstevel@tonic-gate 	extern void	pm_deregister_watcher(int, dev_info_t *);
860*0Sstevel@tonic-gate 	extern void	pm_unrecord_threshold(char *);
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	PMD(PMD_IOCTL, ("ioctl: %s: begin\n", cmdstr))
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate #ifdef DEBUG
865*0Sstevel@tonic-gate 	if (cmd == 666) {
866*0Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), print_info, NULL);
867*0Sstevel@tonic-gate 		return (0);
868*0Sstevel@tonic-gate 	}
869*0Sstevel@tonic-gate 	ret = 0x0badcafe;			/* sanity checking */
870*0Sstevel@tonic-gate 	pm_cmd = cmd;				/* for ASSERT debugging */
871*0Sstevel@tonic-gate 	pm_cmd_string = cmdstr;	/* for ASSERT debugging */
872*0Sstevel@tonic-gate #endif
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	if (pcip == NULL) {
876*0Sstevel@tonic-gate 		PMD(PMD_ERROR, ("ioctl: unknown command %d\n", cmd))
877*0Sstevel@tonic-gate 		return (ENOTTY);
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate 	if (pcip == NULL || pcip->supported == 0) {
880*0Sstevel@tonic-gate 		PMD(PMD_ERROR, ("ioctl: command %s no longer supported\n",
881*0Sstevel@tonic-gate 		    pcip->name))
882*0Sstevel@tonic-gate 		return (ENOTTY);
883*0Sstevel@tonic-gate 	}
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	wholen = 0;
886*0Sstevel@tonic-gate 	dep = i_dep_buf;
887*0Sstevel@tonic-gate 	i_dep_buf[0] = 0;
888*0Sstevel@tonic-gate 	clone = PM_MINOR_TO_CLONE(getminor(dev));
889*0Sstevel@tonic-gate 	if (!pm_perms(pcip->permission, pmstp->pm_cred[clone])) {
890*0Sstevel@tonic-gate 		ret = EPERM;
891*0Sstevel@tonic-gate 		return (ret);
892*0Sstevel@tonic-gate 	}
893*0Sstevel@tonic-gate 	switch (pcip->str_type) {
894*0Sstevel@tonic-gate 	case PM_REQ:
895*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
896*0Sstevel@tonic-gate 		if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
897*0Sstevel@tonic-gate 			pm_req32_t	req32;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, &req32,
900*0Sstevel@tonic-gate 			    sizeof (req32), mode) != 0) {
901*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: ddi_copyin "
902*0Sstevel@tonic-gate 				    "EFAULT\n\n", cmdstr))
903*0Sstevel@tonic-gate 				ret = EFAULT;
904*0Sstevel@tonic-gate 				break;
905*0Sstevel@tonic-gate 			}
906*0Sstevel@tonic-gate 			req.component = req32.component;
907*0Sstevel@tonic-gate 			req.value = req32.value;
908*0Sstevel@tonic-gate 			req.datasize = req32.datasize;
909*0Sstevel@tonic-gate 			if (pcip->inargs & INWHO) {
910*0Sstevel@tonic-gate 				ret = copyinstr((char *)(uintptr_t)
911*0Sstevel@tonic-gate 				    req32.physpath, who, MAXNAMELEN, &wholen);
912*0Sstevel@tonic-gate 				if (ret) {
913*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: "
914*0Sstevel@tonic-gate 					    "copyinstr fails returning %d\n",
915*0Sstevel@tonic-gate 					    cmdstr, ret))
916*0Sstevel@tonic-gate 					break;
917*0Sstevel@tonic-gate 				}
918*0Sstevel@tonic-gate 				req.physpath = who;
919*0Sstevel@tonic-gate 			}
920*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: %s: physpath=%s\n", cmdstr,
921*0Sstevel@tonic-gate 			    req.physpath))
922*0Sstevel@tonic-gate 			if (pcip->inargs & INDATA) {
923*0Sstevel@tonic-gate 				req.data = (void *)(uintptr_t)req32.data;
924*0Sstevel@tonic-gate 				req.datasize = req32.datasize;
925*0Sstevel@tonic-gate 			} else {
926*0Sstevel@tonic-gate 				req.data = NULL;
927*0Sstevel@tonic-gate 				req.datasize = 0;
928*0Sstevel@tonic-gate 			}
929*0Sstevel@tonic-gate 			switch (pcip->diptype) {
930*0Sstevel@tonic-gate 			case DIP:
931*0Sstevel@tonic-gate 				if (!(dip =
932*0Sstevel@tonic-gate 				    pm_name_to_dip(req.physpath, 1))) {
933*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: "
934*0Sstevel@tonic-gate 					    "pm_name_to_dip for %s failed\n",
935*0Sstevel@tonic-gate 					    cmdstr, req.physpath))
936*0Sstevel@tonic-gate 					return (ENODEV);
937*0Sstevel@tonic-gate 				}
938*0Sstevel@tonic-gate 				ASSERT(!dipheld);
939*0Sstevel@tonic-gate 				dipheld++;
940*0Sstevel@tonic-gate 				break;
941*0Sstevel@tonic-gate 			case NODIP:
942*0Sstevel@tonic-gate 				break;
943*0Sstevel@tonic-gate 			default:
944*0Sstevel@tonic-gate 				/*
945*0Sstevel@tonic-gate 				 * Internal error, invalid ioctl description
946*0Sstevel@tonic-gate 				 * force debug entry even if pm_debug not set
947*0Sstevel@tonic-gate 				 */
948*0Sstevel@tonic-gate #ifdef	DEBUG
949*0Sstevel@tonic-gate 				pm_log("invalid diptype %d for cmd %d (%s)\n",
950*0Sstevel@tonic-gate 				    pcip->diptype, cmd, pcip->name);
951*0Sstevel@tonic-gate #endif
952*0Sstevel@tonic-gate 				ASSERT(0);
953*0Sstevel@tonic-gate 				return (EIO);
954*0Sstevel@tonic-gate 			}
955*0Sstevel@tonic-gate 			if (pcip->inargs & INDATAINT) {
956*0Sstevel@tonic-gate 				int32_t int32buf;
957*0Sstevel@tonic-gate 				int32_t *i32p;
958*0Sstevel@tonic-gate 				int *ip;
959*0Sstevel@tonic-gate 				icount = req32.datasize / sizeof (int32_t);
960*0Sstevel@tonic-gate 				if (icount <= 0) {
961*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: datasize"
962*0Sstevel@tonic-gate 					    " 0 or neg EFAULT\n\n", cmdstr))
963*0Sstevel@tonic-gate 					ret = EFAULT;
964*0Sstevel@tonic-gate 					break;
965*0Sstevel@tonic-gate 				}
966*0Sstevel@tonic-gate 				ASSERT(!(pcip->inargs & INDATASTRING));
967*0Sstevel@tonic-gate 				req.datasize = icount * sizeof (int);
968*0Sstevel@tonic-gate 				req.data = kmem_alloc(req.datasize, KM_SLEEP);
969*0Sstevel@tonic-gate 				ip = req.data;
970*0Sstevel@tonic-gate 				ret = 0;
971*0Sstevel@tonic-gate 				for (i = 0,
972*0Sstevel@tonic-gate 				    i32p = (int32_t *)(uintptr_t)req32.data;
973*0Sstevel@tonic-gate 				    i < icount; i++, i32p++) {
974*0Sstevel@tonic-gate 					if (ddi_copyin((void *)i32p, &int32buf,
975*0Sstevel@tonic-gate 					    sizeof (int32_t), mode)) {
976*0Sstevel@tonic-gate 						kmem_free(req.data,
977*0Sstevel@tonic-gate 						    req.datasize);
978*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s: "
979*0Sstevel@tonic-gate 						    "entry %d EFAULT\n",
980*0Sstevel@tonic-gate 						    cmdstr, i))
981*0Sstevel@tonic-gate 						ret = EFAULT;
982*0Sstevel@tonic-gate 						break;
983*0Sstevel@tonic-gate 					}
984*0Sstevel@tonic-gate 					*ip++ = (int)int32buf;
985*0Sstevel@tonic-gate 				}
986*0Sstevel@tonic-gate 				if (ret)
987*0Sstevel@tonic-gate 					break;
988*0Sstevel@tonic-gate 			}
989*0Sstevel@tonic-gate 			if (pcip->inargs & INDATASTRING) {
990*0Sstevel@tonic-gate 				ASSERT(!(pcip->inargs & INDATAINT));
991*0Sstevel@tonic-gate 				ASSERT(pcip->deptype == DEP);
992*0Sstevel@tonic-gate 				if (req32.data != NULL) {
993*0Sstevel@tonic-gate 					size_t dummy;
994*0Sstevel@tonic-gate 					if (copyinstr((void *)(uintptr_t)
995*0Sstevel@tonic-gate 					    req32.data, dep, deplen, &dummy)) {
996*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s: "
997*0Sstevel@tonic-gate 						    "0x%p dep size %lx, EFAULT"
998*0Sstevel@tonic-gate 						    "\n", cmdstr,
999*0Sstevel@tonic-gate 						    (void *)req.data, deplen))
1000*0Sstevel@tonic-gate 						ret = EFAULT;
1001*0Sstevel@tonic-gate 						break;
1002*0Sstevel@tonic-gate 					}
1003*0Sstevel@tonic-gate #ifdef DEBUG
1004*0Sstevel@tonic-gate 					else {
1005*0Sstevel@tonic-gate 						PMD(PMD_DEP, ("ioctl: %s: "
1006*0Sstevel@tonic-gate 						    "dep %s\n", cmdstr, dep))
1007*0Sstevel@tonic-gate 					}
1008*0Sstevel@tonic-gate #endif
1009*0Sstevel@tonic-gate 				} else {
1010*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: no "
1011*0Sstevel@tonic-gate 					    "dependent\n", cmdstr))
1012*0Sstevel@tonic-gate 					ret = EINVAL;
1013*0Sstevel@tonic-gate 					break;
1014*0Sstevel@tonic-gate 				}
1015*0Sstevel@tonic-gate 			}
1016*0Sstevel@tonic-gate 		} else
1017*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1018*0Sstevel@tonic-gate 		{
1019*0Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg,
1020*0Sstevel@tonic-gate 			    &req, sizeof (req), mode) != 0) {
1021*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: ddi_copyin "
1022*0Sstevel@tonic-gate 				    "EFAULT\n\n", cmdstr))
1023*0Sstevel@tonic-gate 				ret = EFAULT;
1024*0Sstevel@tonic-gate 				break;
1025*0Sstevel@tonic-gate 			}
1026*0Sstevel@tonic-gate 			if (pcip->inargs & INWHO) {
1027*0Sstevel@tonic-gate 				ret = copyinstr((char *)req.physpath, who,
1028*0Sstevel@tonic-gate 				    MAXNAMELEN, &wholen);
1029*0Sstevel@tonic-gate 				if (ret) {
1030*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s copyinstr"
1031*0Sstevel@tonic-gate 					    " fails returning %d\n", cmdstr,
1032*0Sstevel@tonic-gate 					    ret))
1033*0Sstevel@tonic-gate 					break;
1034*0Sstevel@tonic-gate 				}
1035*0Sstevel@tonic-gate 				req.physpath = who;
1036*0Sstevel@tonic-gate 			}
1037*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: %s: physpath=%s\n", cmdstr,
1038*0Sstevel@tonic-gate 			    req.physpath))
1039*0Sstevel@tonic-gate 			if (!(pcip->inargs & INDATA)) {
1040*0Sstevel@tonic-gate 				req.data = NULL;
1041*0Sstevel@tonic-gate 				req.datasize = 0;
1042*0Sstevel@tonic-gate 			}
1043*0Sstevel@tonic-gate 			switch (pcip->diptype) {
1044*0Sstevel@tonic-gate 			case DIP:
1045*0Sstevel@tonic-gate 				if (!(dip =
1046*0Sstevel@tonic-gate 				    pm_name_to_dip(req.physpath, 1))) {
1047*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: "
1048*0Sstevel@tonic-gate 					    "pm_name_to_dip for %s failed\n",
1049*0Sstevel@tonic-gate 					    cmdstr, req.physpath))
1050*0Sstevel@tonic-gate 					return (ENODEV);
1051*0Sstevel@tonic-gate 				}
1052*0Sstevel@tonic-gate 				ASSERT(!dipheld);
1053*0Sstevel@tonic-gate 				dipheld++;
1054*0Sstevel@tonic-gate 				break;
1055*0Sstevel@tonic-gate 			case NODIP:
1056*0Sstevel@tonic-gate 				break;
1057*0Sstevel@tonic-gate 			default:
1058*0Sstevel@tonic-gate 				/*
1059*0Sstevel@tonic-gate 				 * Internal error, invalid ioctl description
1060*0Sstevel@tonic-gate 				 * force debug entry even if pm_debug not set
1061*0Sstevel@tonic-gate 				 */
1062*0Sstevel@tonic-gate #ifdef	DEBUG
1063*0Sstevel@tonic-gate 				pm_log("invalid diptype %d for cmd %d (%s)\n",
1064*0Sstevel@tonic-gate 				    pcip->diptype, cmd, pcip->name);
1065*0Sstevel@tonic-gate #endif
1066*0Sstevel@tonic-gate 				ASSERT(0);
1067*0Sstevel@tonic-gate 				return (EIO);
1068*0Sstevel@tonic-gate 			}
1069*0Sstevel@tonic-gate 			if (pcip->inargs & INDATAINT) {
1070*0Sstevel@tonic-gate 				int *ip;
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 				ASSERT(!(pcip->inargs & INDATASTRING));
1073*0Sstevel@tonic-gate 				ip = req.data;
1074*0Sstevel@tonic-gate 				icount = req.datasize / sizeof (int);
1075*0Sstevel@tonic-gate 				if (icount <= 0) {
1076*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: datasize"
1077*0Sstevel@tonic-gate 					    " 0 or neg EFAULT\n\n", cmdstr))
1078*0Sstevel@tonic-gate 					ret = EFAULT;
1079*0Sstevel@tonic-gate 					break;
1080*0Sstevel@tonic-gate 				}
1081*0Sstevel@tonic-gate 				req.data = kmem_alloc(req.datasize, KM_SLEEP);
1082*0Sstevel@tonic-gate 				if (ddi_copyin((caddr_t)ip, req.data,
1083*0Sstevel@tonic-gate 				    req.datasize, mode) != 0) {
1084*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: ddi_copyin "
1085*0Sstevel@tonic-gate 					    "EFAULT\n\n", cmdstr))
1086*0Sstevel@tonic-gate 					ret = EFAULT;
1087*0Sstevel@tonic-gate 					break;
1088*0Sstevel@tonic-gate 				}
1089*0Sstevel@tonic-gate 			}
1090*0Sstevel@tonic-gate 			if (pcip->inargs & INDATASTRING) {
1091*0Sstevel@tonic-gate 				ASSERT(!(pcip->inargs & INDATAINT));
1092*0Sstevel@tonic-gate 				ASSERT(pcip->deptype == DEP);
1093*0Sstevel@tonic-gate 				if (req.data != NULL) {
1094*0Sstevel@tonic-gate 					size_t dummy;
1095*0Sstevel@tonic-gate 					if (copyinstr((caddr_t)req.data,
1096*0Sstevel@tonic-gate 					    dep, deplen, &dummy)) {
1097*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s: "
1098*0Sstevel@tonic-gate 						    "0x%p dep size %lu, "
1099*0Sstevel@tonic-gate 						    "EFAULT\n", cmdstr,
1100*0Sstevel@tonic-gate 						    (void *)req.data, deplen))
1101*0Sstevel@tonic-gate 						ret = EFAULT;
1102*0Sstevel@tonic-gate 						break;
1103*0Sstevel@tonic-gate 					}
1104*0Sstevel@tonic-gate #ifdef DEBUG
1105*0Sstevel@tonic-gate 					else {
1106*0Sstevel@tonic-gate 						PMD(PMD_DEP, ("ioctl: %s: "
1107*0Sstevel@tonic-gate 						    "dep %s\n", cmdstr, dep))
1108*0Sstevel@tonic-gate 					}
1109*0Sstevel@tonic-gate #endif
1110*0Sstevel@tonic-gate 				} else {
1111*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: no "
1112*0Sstevel@tonic-gate 					    "dependent\n", cmdstr))
1113*0Sstevel@tonic-gate 					ret = EINVAL;
1114*0Sstevel@tonic-gate 					break;
1115*0Sstevel@tonic-gate 				}
1116*0Sstevel@tonic-gate 			}
1117*0Sstevel@tonic-gate 		}
1118*0Sstevel@tonic-gate 		/*
1119*0Sstevel@tonic-gate 		 * Now we've got all the args in for the commands that
1120*0Sstevel@tonic-gate 		 * use the new pm_req struct.
1121*0Sstevel@tonic-gate 		 */
1122*0Sstevel@tonic-gate 		switch (cmd) {
1123*0Sstevel@tonic-gate 		case PM_REPARSE_PM_PROPS:
1124*0Sstevel@tonic-gate 		{
1125*0Sstevel@tonic-gate 			struct dev_ops	*drv;
1126*0Sstevel@tonic-gate 			struct cb_ops	*cb;
1127*0Sstevel@tonic-gate 			void		*propval;
1128*0Sstevel@tonic-gate 			int length;
1129*0Sstevel@tonic-gate 			/*
1130*0Sstevel@tonic-gate 			 * This ioctl is provided only for the ddivs pm test.
1131*0Sstevel@tonic-gate 			 * We only do it to a driver which explicitly allows
1132*0Sstevel@tonic-gate 			 * us to do so by exporting a pm-reparse-ok property.
1133*0Sstevel@tonic-gate 			 * We only care whether the property exists or not.
1134*0Sstevel@tonic-gate 			 */
1135*0Sstevel@tonic-gate 			if ((drv = ddi_get_driver(dip)) == NULL) {
1136*0Sstevel@tonic-gate 				ret = EINVAL;
1137*0Sstevel@tonic-gate 				break;
1138*0Sstevel@tonic-gate 			}
1139*0Sstevel@tonic-gate 			if ((cb = drv->devo_cb_ops) != NULL) {
1140*0Sstevel@tonic-gate 				if ((*cb->cb_prop_op)(DDI_DEV_T_ANY, dip,
1141*0Sstevel@tonic-gate 				    PROP_LEN_AND_VAL_ALLOC, (DDI_PROP_CANSLEEP |
1142*0Sstevel@tonic-gate 				    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
1143*0Sstevel@tonic-gate 				    "pm-reparse-ok", (caddr_t)&propval,
1144*0Sstevel@tonic-gate 				    &length) != DDI_SUCCESS) {
1145*0Sstevel@tonic-gate 					ret = EINVAL;
1146*0Sstevel@tonic-gate 					break;
1147*0Sstevel@tonic-gate 				}
1148*0Sstevel@tonic-gate 			} else if (ddi_prop_op(DDI_DEV_T_ANY, dip,
1149*0Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_ALLOC, (DDI_PROP_CANSLEEP |
1150*0Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
1151*0Sstevel@tonic-gate 			    "pm-reparse-ok", (caddr_t)&propval,
1152*0Sstevel@tonic-gate 			    &length) != DDI_SUCCESS) {
1153*0Sstevel@tonic-gate 				ret = EINVAL;
1154*0Sstevel@tonic-gate 				break;
1155*0Sstevel@tonic-gate 			}
1156*0Sstevel@tonic-gate 			kmem_free(propval, length);
1157*0Sstevel@tonic-gate 			ret =  e_new_pm_props(dip);
1158*0Sstevel@tonic-gate 			break;
1159*0Sstevel@tonic-gate 		}
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 		case PM_GET_DEVICE_THRESHOLD:
1162*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1163*0Sstevel@tonic-gate 			if (!PM_GET_PM_INFO(dip) || PM_ISBC(dip)) {
1164*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
1165*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: ENODEV\n",
1166*0Sstevel@tonic-gate 				    cmdstr))
1167*0Sstevel@tonic-gate 				ret = ENODEV;
1168*0Sstevel@tonic-gate 				break;
1169*0Sstevel@tonic-gate 			}
1170*0Sstevel@tonic-gate 			*rval_p = DEVI(dip)->devi_pm_dev_thresh;
1171*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1172*0Sstevel@tonic-gate 			ret = 0;
1173*0Sstevel@tonic-gate 			break;
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 		case PM_DIRECT_PM:
1176*0Sstevel@tonic-gate 		{
1177*0Sstevel@tonic-gate 			int has_dep;
1178*0Sstevel@tonic-gate 			if ((info = PM_GET_PM_INFO(dip)) == NULL) {
1179*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1180*0Sstevel@tonic-gate 				    "ENODEV\n", cmdstr))
1181*0Sstevel@tonic-gate 				ret = ENODEV;
1182*0Sstevel@tonic-gate 				break;
1183*0Sstevel@tonic-gate 			}
1184*0Sstevel@tonic-gate 			/*
1185*0Sstevel@tonic-gate 			 * Check to see if we are there is a dependency on
1186*0Sstevel@tonic-gate 			 * this kept device, if so, return EBUSY.
1187*0Sstevel@tonic-gate 			 */
1188*0Sstevel@tonic-gate 			pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1189*0Sstevel@tonic-gate 			(void) ddi_pathname(dip, pathbuf);
1190*0Sstevel@tonic-gate 			pm_dispatch_to_dep_thread(PM_DEP_WK_CHECK_KEPT,
1191*0Sstevel@tonic-gate 			    NULL, pathbuf, PM_DEP_WAIT, &has_dep, 0);
1192*0Sstevel@tonic-gate 			kmem_free(pathbuf, MAXPATHLEN);
1193*0Sstevel@tonic-gate 			if (has_dep) {
1194*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("%s EBUSY\n",
1195*0Sstevel@tonic-gate 				    cmdstr))
1196*0Sstevel@tonic-gate 				ret = EBUSY;
1197*0Sstevel@tonic-gate 				break;
1198*0Sstevel@tonic-gate 			}
1199*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1200*0Sstevel@tonic-gate 			if (PM_ISDIRECT(dip) || (info->pmi_clone != 0)) {
1201*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1202*0Sstevel@tonic-gate 				    "%s@%s(%s#%d): EBUSY\n", cmdstr,
1203*0Sstevel@tonic-gate 				    PM_DEVICE(dip)))
1204*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
1205*0Sstevel@tonic-gate 				ret = EBUSY;
1206*0Sstevel@tonic-gate 				break;
1207*0Sstevel@tonic-gate 			}
1208*0Sstevel@tonic-gate 			info->pmi_dev_pm_state |= PM_DIRECT;
1209*0Sstevel@tonic-gate 			info->pmi_clone = clone;
1210*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1211*0Sstevel@tonic-gate 			PMD(PMD_DPM, ("ioctl: %s: info %p, pmi_clone %d\n",
1212*0Sstevel@tonic-gate 			    cmdstr, (void *)info, clone))
1213*0Sstevel@tonic-gate 			mutex_enter(&pm_clone_lock);
1214*0Sstevel@tonic-gate 			pm_register_watcher(clone, dip);
1215*0Sstevel@tonic-gate 			mutex_exit(&pm_clone_lock);
1216*0Sstevel@tonic-gate 			ret = 0;
1217*0Sstevel@tonic-gate 			break;
1218*0Sstevel@tonic-gate 		}
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 		case PM_RELEASE_DIRECT_PM:
1221*0Sstevel@tonic-gate 		{
1222*0Sstevel@tonic-gate 			if ((info = PM_GET_PM_INFO(dip)) == NULL) {
1223*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1224*0Sstevel@tonic-gate 				    "ENODEV\n", cmdstr))
1225*0Sstevel@tonic-gate 				ret = ENODEV;
1226*0Sstevel@tonic-gate 				break;
1227*0Sstevel@tonic-gate 			}
1228*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1229*0Sstevel@tonic-gate 			if (info->pmi_clone != clone) {
1230*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1231*0Sstevel@tonic-gate 				    "%s@%s(%s#%d) EINVAL\n", cmdstr,
1232*0Sstevel@tonic-gate 				    PM_DEVICE(dip)))
1233*0Sstevel@tonic-gate 				ret = EINVAL;
1234*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
1235*0Sstevel@tonic-gate 				break;
1236*0Sstevel@tonic-gate 			}
1237*0Sstevel@tonic-gate 			ASSERT(PM_ISDIRECT(dip));
1238*0Sstevel@tonic-gate 			info->pmi_dev_pm_state &= ~PM_DIRECT;
1239*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1240*0Sstevel@tonic-gate 			/* Bring ourselves up if there is a keeper. */
1241*0Sstevel@tonic-gate 			pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1242*0Sstevel@tonic-gate 			(void) ddi_pathname(dip, pathbuf);
1243*0Sstevel@tonic-gate 			pm_dispatch_to_dep_thread(PM_DEP_WK_BRINGUP_SELF,
1244*0Sstevel@tonic-gate 			    NULL, pathbuf, PM_DEP_WAIT, NULL, 0);
1245*0Sstevel@tonic-gate 			kmem_free(pathbuf, MAXPATHLEN);
1246*0Sstevel@tonic-gate 			pm_discard_entries(clone);
1247*0Sstevel@tonic-gate 			pm_deregister_watcher(clone, dip);
1248*0Sstevel@tonic-gate 			/*
1249*0Sstevel@tonic-gate 			 * Now we could let the other threads that are
1250*0Sstevel@tonic-gate 			 * trying to do a DIRECT_PM thru
1251*0Sstevel@tonic-gate 			 */
1252*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1253*0Sstevel@tonic-gate 			info->pmi_clone = 0;
1254*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1255*0Sstevel@tonic-gate 			pm_proceed(dip, PMP_RELEASE, -1, -1);
1256*0Sstevel@tonic-gate 			PMD(PMD_RESCAN | PMD_DPM, ("ioctl: %s: rescan\n",
1257*0Sstevel@tonic-gate 			    cmdstr))
1258*0Sstevel@tonic-gate 			pm_rescan(dip);
1259*0Sstevel@tonic-gate 			ret = 0;
1260*0Sstevel@tonic-gate 			break;
1261*0Sstevel@tonic-gate 		}
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 		case PM_SET_CURRENT_POWER:
1264*0Sstevel@tonic-gate 		{
1265*0Sstevel@tonic-gate 			int comp = req.component;
1266*0Sstevel@tonic-gate 			int  value = req.value;
1267*0Sstevel@tonic-gate 			PMD(PMD_DPM, ("ioctl: %s: %s component %d to value "
1268*0Sstevel@tonic-gate 			    "%d\n", cmdstr, req.physpath, comp, value))
1269*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, comp, NULL) ||
1270*0Sstevel@tonic-gate 			    !e_pm_valid_power(dip, comp, value)) {
1271*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1272*0Sstevel@tonic-gate 				    "physpath=%s, comp=%d, level=%d, fails\n",
1273*0Sstevel@tonic-gate 				    cmdstr, req.physpath, comp, value))
1274*0Sstevel@tonic-gate 				ret = EINVAL;
1275*0Sstevel@tonic-gate 				break;
1276*0Sstevel@tonic-gate 			}
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 			if ((info = PM_GET_PM_INFO(dip)) == NULL) {
1279*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1280*0Sstevel@tonic-gate 				    "ENODEV\n", cmdstr))
1281*0Sstevel@tonic-gate 				ret = ENODEV;
1282*0Sstevel@tonic-gate 				break;
1283*0Sstevel@tonic-gate 			}
1284*0Sstevel@tonic-gate 			if (info->pmi_clone != clone) {
1285*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1286*0Sstevel@tonic-gate 				    "(not owner) %s fails; clone %d, owner %d"
1287*0Sstevel@tonic-gate 				    "\n", cmdstr, req.physpath, clone,
1288*0Sstevel@tonic-gate 				    info->pmi_clone))
1289*0Sstevel@tonic-gate 				ret = EINVAL;
1290*0Sstevel@tonic-gate 				break;
1291*0Sstevel@tonic-gate 			}
1292*0Sstevel@tonic-gate 			ASSERT(PM_ISDIRECT(dip));
1293*0Sstevel@tonic-gate 
1294*0Sstevel@tonic-gate 			if (pm_set_power(dip, comp, value, PM_LEVEL_EXACT,
1295*0Sstevel@tonic-gate 			    PM_CANBLOCK_BLOCK, 0, &ret) != DDI_SUCCESS) {
1296*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s: "
1297*0Sstevel@tonic-gate 				    "pm_set_power for %s fails, errno=%d\n",
1298*0Sstevel@tonic-gate 				    cmdstr, req.physpath, ret))
1299*0Sstevel@tonic-gate 				break;
1300*0Sstevel@tonic-gate 			}
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 			pm_proceed(dip, PMP_SETPOWER, comp, value);
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 			/*
1305*0Sstevel@tonic-gate 			 * Power down all idle components if console framebuffer
1306*0Sstevel@tonic-gate 			 * is powered off.
1307*0Sstevel@tonic-gate 			 */
1308*0Sstevel@tonic-gate 			if (PM_IS_CFB(dip) && (pm_system_idle_threshold ==
1309*0Sstevel@tonic-gate 			    pm_default_idle_threshold)) {
1310*0Sstevel@tonic-gate 				dev_info_t	*root = ddi_root_node();
1311*0Sstevel@tonic-gate 				if (PM_ISBC(dip)) {
1312*0Sstevel@tonic-gate 					if (comp == 0 && value == 0 &&
1313*0Sstevel@tonic-gate 					    (pm_timeout_idledown() != 0)) {
1314*0Sstevel@tonic-gate 						ddi_walk_devs(root,
1315*0Sstevel@tonic-gate 						    pm_start_idledown,
1316*0Sstevel@tonic-gate 						    (void *)PMID_CFB);
1317*0Sstevel@tonic-gate 					}
1318*0Sstevel@tonic-gate 				} else {
1319*0Sstevel@tonic-gate 					int count = 0;
1320*0Sstevel@tonic-gate 					for (i = 0; i < PM_NUMCMPTS(dip); i++) {
1321*0Sstevel@tonic-gate 						ret = pm_get_current_power(dip,
1322*0Sstevel@tonic-gate 						    i, &curpower);
1323*0Sstevel@tonic-gate 						if (ret == DDI_SUCCESS &&
1324*0Sstevel@tonic-gate 						    curpower == 0)
1325*0Sstevel@tonic-gate 							count++;
1326*0Sstevel@tonic-gate 					}
1327*0Sstevel@tonic-gate 					if ((count == PM_NUMCMPTS(dip)) &&
1328*0Sstevel@tonic-gate 					    (pm_timeout_idledown() != 0)) {
1329*0Sstevel@tonic-gate 						ddi_walk_devs(root,
1330*0Sstevel@tonic-gate 						    pm_start_idledown,
1331*0Sstevel@tonic-gate 						    (void *)PMID_CFB);
1332*0Sstevel@tonic-gate 					}
1333*0Sstevel@tonic-gate 				}
1334*0Sstevel@tonic-gate 			}
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate 			PMD(PMD_RESCAN | PMD_DPM, ("ioctl: %s: rescan\n",
1337*0Sstevel@tonic-gate 			    cmdstr))
1338*0Sstevel@tonic-gate 			pm_rescan(dip);
1339*0Sstevel@tonic-gate 			*rval_p = 0;
1340*0Sstevel@tonic-gate 			ret = 0;
1341*0Sstevel@tonic-gate 			break;
1342*0Sstevel@tonic-gate 		}
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 		case PM_GET_FULL_POWER:
1345*0Sstevel@tonic-gate 		{
1346*0Sstevel@tonic-gate 			int normal;
1347*0Sstevel@tonic-gate 			ASSERT(dip);
1348*0Sstevel@tonic-gate 			PMD(PMD_NORM, ("ioctl: %s: %s component %d\n",
1349*0Sstevel@tonic-gate 			    cmdstr, req.physpath, req.component))
1350*0Sstevel@tonic-gate 			normal =  pm_get_normal_power(dip, req.component);
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 			if (normal == DDI_FAILURE) {
1353*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_NORM, ("ioctl: %s: "
1354*0Sstevel@tonic-gate 				    "returns EINVAL\n", cmdstr))
1355*0Sstevel@tonic-gate 				ret = EINVAL;
1356*0Sstevel@tonic-gate 				break;
1357*0Sstevel@tonic-gate 			}
1358*0Sstevel@tonic-gate 			*rval_p = normal;
1359*0Sstevel@tonic-gate 			PMD(PMD_NORM, ("ioctl: %s: returns %d\n",
1360*0Sstevel@tonic-gate 			    cmdstr, normal))
1361*0Sstevel@tonic-gate 			ret = 0;
1362*0Sstevel@tonic-gate 			break;
1363*0Sstevel@tonic-gate 		}
1364*0Sstevel@tonic-gate 
1365*0Sstevel@tonic-gate 		case PM_GET_CURRENT_POWER:
1366*0Sstevel@tonic-gate 			if (pm_get_current_power(dip, req.component,
1367*0Sstevel@tonic-gate 			    rval_p) != DDI_SUCCESS) {
1368*0Sstevel@tonic-gate 				PMD(PMD_ERROR | PMD_DPM, ("ioctl: %s "
1369*0Sstevel@tonic-gate 				    "EINVAL\n", cmdstr))
1370*0Sstevel@tonic-gate 				ret = EINVAL;
1371*0Sstevel@tonic-gate 				break;
1372*0Sstevel@tonic-gate 			}
1373*0Sstevel@tonic-gate 			PMD(PMD_DPM, ("ioctl: %s: %s comp %d returns %d\n",
1374*0Sstevel@tonic-gate 			    cmdstr, req.physpath, req.component, *rval_p))
1375*0Sstevel@tonic-gate 			if (*rval_p == PM_LEVEL_UNKNOWN)
1376*0Sstevel@tonic-gate 				ret = EAGAIN;
1377*0Sstevel@tonic-gate 			else
1378*0Sstevel@tonic-gate 				ret = 0;
1379*0Sstevel@tonic-gate 			break;
1380*0Sstevel@tonic-gate 
1381*0Sstevel@tonic-gate 		case PM_GET_TIME_IDLE:
1382*0Sstevel@tonic-gate 		{
1383*0Sstevel@tonic-gate 			time_t timestamp;
1384*0Sstevel@tonic-gate 			int comp = req.component;
1385*0Sstevel@tonic-gate 			pm_component_t *cp;
1386*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, comp, &cp)) {
1387*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1388*0Sstevel@tonic-gate 				    "component %d > numcmpts - 1 %d--EINVAL\n",
1389*0Sstevel@tonic-gate 				    cmdstr, PM_DEVICE(dip), comp,
1390*0Sstevel@tonic-gate 				    PM_NUMCMPTS(dip) - 1))
1391*0Sstevel@tonic-gate 				ret = EINVAL;
1392*0Sstevel@tonic-gate 				break;
1393*0Sstevel@tonic-gate 			}
1394*0Sstevel@tonic-gate 			timestamp = cp->pmc_timestamp;
1395*0Sstevel@tonic-gate 			if (timestamp) {
1396*0Sstevel@tonic-gate 				time_t now;
1397*0Sstevel@tonic-gate 				(void) drv_getparm(TIME, &now);
1398*0Sstevel@tonic-gate 				*rval_p = (now - timestamp);
1399*0Sstevel@tonic-gate 			} else {
1400*0Sstevel@tonic-gate 				*rval_p = 0;
1401*0Sstevel@tonic-gate 			}
1402*0Sstevel@tonic-gate 			ret = 0;
1403*0Sstevel@tonic-gate 			break;
1404*0Sstevel@tonic-gate 		}
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate 		case PM_ADD_DEPENDENT:
1407*0Sstevel@tonic-gate 		{
1408*0Sstevel@tonic-gate 			dev_info_t	*kept_dip;
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate 			PMD(PMD_KEEPS, ("%s, kept %s, keeper %s\n", cmdstr,
1411*0Sstevel@tonic-gate 			    dep, req.physpath))
1412*0Sstevel@tonic-gate 
1413*0Sstevel@tonic-gate 			/*
1414*0Sstevel@tonic-gate 			 * hold and install kept while processing dependency
1415*0Sstevel@tonic-gate 			 * keeper (in .physpath) has already been held.
1416*0Sstevel@tonic-gate 			 */
1417*0Sstevel@tonic-gate 			if (dep[0] == '\0') {
1418*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("kept NULL or null\n"))
1419*0Sstevel@tonic-gate 				ret = EINVAL;
1420*0Sstevel@tonic-gate 				break;
1421*0Sstevel@tonic-gate 			} else if ((kept_dip =
1422*0Sstevel@tonic-gate 			    pm_name_to_dip(dep, 1)) == NULL) {
1423*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("no dip for kept %s\n", dep))
1424*0Sstevel@tonic-gate 				ret = ENODEV;
1425*0Sstevel@tonic-gate 				break;
1426*0Sstevel@tonic-gate 			} else if (kept_dip == dip) {
1427*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("keeper(%s, %p) - kept(%s, %p) "
1428*0Sstevel@tonic-gate 				    "self-dependency not allowed.\n",
1429*0Sstevel@tonic-gate 				    dep, (void *)kept_dip, req.physpath,
1430*0Sstevel@tonic-gate 				    (void *) dip))
1431*0Sstevel@tonic-gate 				PM_RELE(dip);	/* release "double" hold */
1432*0Sstevel@tonic-gate 				ret = EINVAL;
1433*0Sstevel@tonic-gate 				break;
1434*0Sstevel@tonic-gate 			}
1435*0Sstevel@tonic-gate 			ASSERT(!(strcmp(req.physpath, (char *)dep) == 0));
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 			/*
1438*0Sstevel@tonic-gate 			 * record dependency, then walk through device tree
1439*0Sstevel@tonic-gate 			 * independently on behalf of kept and keeper to
1440*0Sstevel@tonic-gate 			 * establish newly created dependency.
1441*0Sstevel@tonic-gate 			 */
1442*0Sstevel@tonic-gate 			pm_dispatch_to_dep_thread(PM_DEP_WK_RECORD_KEEPER,
1443*0Sstevel@tonic-gate 			    req.physpath, dep, PM_DEP_WAIT, NULL, 0);
1444*0Sstevel@tonic-gate 
1445*0Sstevel@tonic-gate 			/*
1446*0Sstevel@tonic-gate 			 * release kept after establishing dependency, keeper
1447*0Sstevel@tonic-gate 			 * is released as part of ioctl exit processing.
1448*0Sstevel@tonic-gate 			 */
1449*0Sstevel@tonic-gate 			PM_RELE(kept_dip);
1450*0Sstevel@tonic-gate 			*rval_p = 0;
1451*0Sstevel@tonic-gate 			ret = 0;
1452*0Sstevel@tonic-gate 			break;
1453*0Sstevel@tonic-gate 		}
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 		case PM_ADD_DEPENDENT_PROPERTY:
1456*0Sstevel@tonic-gate 		{
1457*0Sstevel@tonic-gate 			char *keeper, *kept;
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 			if (dep[0] == '\0') {
1460*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: dep NULL or "
1461*0Sstevel@tonic-gate 				    "null\n", cmdstr))
1462*0Sstevel@tonic-gate 				ret = EINVAL;
1463*0Sstevel@tonic-gate 				break;
1464*0Sstevel@tonic-gate 			}
1465*0Sstevel@tonic-gate 			kept = dep;
1466*0Sstevel@tonic-gate 			keeper = req.physpath;
1467*0Sstevel@tonic-gate 			/*
1468*0Sstevel@tonic-gate 			 * record keeper - kept dependency, then walk through
1469*0Sstevel@tonic-gate 			 * device tree to find out all attached keeper, walk
1470*0Sstevel@tonic-gate 			 * through again to apply dependency to all the
1471*0Sstevel@tonic-gate 			 * potential kept.
1472*0Sstevel@tonic-gate 			 */
1473*0Sstevel@tonic-gate 			pm_dispatch_to_dep_thread(
1474*0Sstevel@tonic-gate 			    PM_DEP_WK_RECORD_KEEPER_PROP, keeper, kept,
1475*0Sstevel@tonic-gate 			    PM_DEP_WAIT, NULL, 0);
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 			*rval_p = 0;
1478*0Sstevel@tonic-gate 			ret = 0;
1479*0Sstevel@tonic-gate 			break;
1480*0Sstevel@tonic-gate 		}
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 		case PM_SET_DEVICE_THRESHOLD:
1483*0Sstevel@tonic-gate 		{
1484*0Sstevel@tonic-gate 			pm_thresh_rec_t *rp;
1485*0Sstevel@tonic-gate 			pm_pte_t *ep;	/* threshold header storage */
1486*0Sstevel@tonic-gate 			int *tp;	/* threshold storage */
1487*0Sstevel@tonic-gate 			size_t size;
1488*0Sstevel@tonic-gate 			extern int pm_thresh_specd(dev_info_t *);
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate 			/*
1491*0Sstevel@tonic-gate 			 * The header struct plus one entry struct plus one
1492*0Sstevel@tonic-gate 			 * threshold plus the length of the string
1493*0Sstevel@tonic-gate 			 */
1494*0Sstevel@tonic-gate 			size = sizeof (pm_thresh_rec_t) +
1495*0Sstevel@tonic-gate 			    (sizeof (pm_pte_t) * 1) +
1496*0Sstevel@tonic-gate 			    (1 * sizeof (int)) +
1497*0Sstevel@tonic-gate 			    strlen(req.physpath) + 1;
1498*0Sstevel@tonic-gate 
1499*0Sstevel@tonic-gate 			rp = kmem_zalloc(size, KM_SLEEP);
1500*0Sstevel@tonic-gate 			rp->ptr_size = size;
1501*0Sstevel@tonic-gate 			rp->ptr_numcomps = 0;	/* means device threshold */
1502*0Sstevel@tonic-gate 			ep = (pm_pte_t *)((intptr_t)rp + sizeof (*rp));
1503*0Sstevel@tonic-gate 			rp->ptr_entries = ep;
1504*0Sstevel@tonic-gate 			tp = (int *)((intptr_t)ep +
1505*0Sstevel@tonic-gate 			    (1 * sizeof (pm_pte_t)));
1506*0Sstevel@tonic-gate 			ep->pte_numthresh = 1;
1507*0Sstevel@tonic-gate 			ep->pte_thresh = tp;
1508*0Sstevel@tonic-gate 			*tp++ = req.value;
1509*0Sstevel@tonic-gate 			(void) strcat((char *)tp, req.physpath);
1510*0Sstevel@tonic-gate 			rp->ptr_physpath = (char *)tp;
1511*0Sstevel@tonic-gate 			ASSERT((intptr_t)tp + strlen(req.physpath) + 1 ==
1512*0Sstevel@tonic-gate 			    (intptr_t)rp + rp->ptr_size);
1513*0Sstevel@tonic-gate 			PMD(PMD_THRESH, ("ioctl: %s: record thresh %d for "
1514*0Sstevel@tonic-gate 			    "%s\n", cmdstr, req.value, req.physpath))
1515*0Sstevel@tonic-gate 			pm_record_thresh(rp);
1516*0Sstevel@tonic-gate 			/*
1517*0Sstevel@tonic-gate 			 * Don't free rp, pm_record_thresh() keeps it.
1518*0Sstevel@tonic-gate 			 * We don't try to apply it ourselves because we'd need
1519*0Sstevel@tonic-gate 			 * to know too much about locking.  Since we don't
1520*0Sstevel@tonic-gate 			 * hold a lock the entry could be removed before
1521*0Sstevel@tonic-gate 			 * we get here
1522*0Sstevel@tonic-gate 			 */
1523*0Sstevel@tonic-gate 			ASSERT(dip == NULL);
1524*0Sstevel@tonic-gate 			ret = 0;		/* can't fail now */
1525*0Sstevel@tonic-gate 			if (!(dip = pm_name_to_dip(req.physpath, 1))) {
1526*0Sstevel@tonic-gate 				break;
1527*0Sstevel@tonic-gate 			}
1528*0Sstevel@tonic-gate 			(void) pm_thresh_specd(dip);
1529*0Sstevel@tonic-gate 			PMD(PMD_DHR, ("ioctl: %s: releasing %s@%s(%s#%d)\n",
1530*0Sstevel@tonic-gate 			    cmdstr, PM_DEVICE(dip)))
1531*0Sstevel@tonic-gate 			PM_RELE(dip);
1532*0Sstevel@tonic-gate 			break;
1533*0Sstevel@tonic-gate 		}
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate 		case PM_RESET_DEVICE_THRESHOLD:
1536*0Sstevel@tonic-gate 		{
1537*0Sstevel@tonic-gate 			/*
1538*0Sstevel@tonic-gate 			 * This only applies to a currently attached and power
1539*0Sstevel@tonic-gate 			 * managed node
1540*0Sstevel@tonic-gate 			 */
1541*0Sstevel@tonic-gate 			/*
1542*0Sstevel@tonic-gate 			 * We don't do this to old-style drivers
1543*0Sstevel@tonic-gate 			 */
1544*0Sstevel@tonic-gate 			info = PM_GET_PM_INFO(dip);
1545*0Sstevel@tonic-gate 			if (info == NULL) {
1546*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s not power "
1547*0Sstevel@tonic-gate 				    "managed\n", cmdstr, req.physpath))
1548*0Sstevel@tonic-gate 				ret = EINVAL;
1549*0Sstevel@tonic-gate 				break;
1550*0Sstevel@tonic-gate 			}
1551*0Sstevel@tonic-gate 			if (PM_ISBC(dip)) {
1552*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s is BC\n",
1553*0Sstevel@tonic-gate 				    cmdstr, req.physpath))
1554*0Sstevel@tonic-gate 				ret = EINVAL;
1555*0Sstevel@tonic-gate 				break;
1556*0Sstevel@tonic-gate 			}
1557*0Sstevel@tonic-gate 			pm_unrecord_threshold(req.physpath);
1558*0Sstevel@tonic-gate 			pm_set_device_threshold(dip, pm_system_idle_threshold,
1559*0Sstevel@tonic-gate 			    PMC_DEF_THRESH);
1560*0Sstevel@tonic-gate 			ret = 0;
1561*0Sstevel@tonic-gate 			break;
1562*0Sstevel@tonic-gate 		}
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 		case PM_GET_NUM_COMPONENTS:
1565*0Sstevel@tonic-gate 			ret = 0;
1566*0Sstevel@tonic-gate 			*rval_p = PM_NUMCMPTS(dip);
1567*0Sstevel@tonic-gate 			break;
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate 		case PM_GET_DEVICE_TYPE:
1570*0Sstevel@tonic-gate 			ret = 0;
1571*0Sstevel@tonic-gate 			if ((info = PM_GET_PM_INFO(dip)) == NULL) {
1572*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: "
1573*0Sstevel@tonic-gate 				    "PM_NO_PM_COMPONENTS\n", cmdstr))
1574*0Sstevel@tonic-gate 				*rval_p = PM_NO_PM_COMPONENTS;
1575*0Sstevel@tonic-gate 				break;
1576*0Sstevel@tonic-gate 			}
1577*0Sstevel@tonic-gate 			if (PM_ISBC(dip)) {
1578*0Sstevel@tonic-gate 				*rval_p = PM_CREATE_COMPONENTS;
1579*0Sstevel@tonic-gate 			} else {
1580*0Sstevel@tonic-gate 				*rval_p = PM_AUTOPM;
1581*0Sstevel@tonic-gate 			}
1582*0Sstevel@tonic-gate 			break;
1583*0Sstevel@tonic-gate 
1584*0Sstevel@tonic-gate 		case PM_SET_COMPONENT_THRESHOLDS:
1585*0Sstevel@tonic-gate 		{
1586*0Sstevel@tonic-gate 			int comps = 0;
1587*0Sstevel@tonic-gate 			int *end = (int *)req.data + icount;
1588*0Sstevel@tonic-gate 			pm_thresh_rec_t *rp;
1589*0Sstevel@tonic-gate 			pm_pte_t *ep;	/* threshold header storage */
1590*0Sstevel@tonic-gate 			int *tp;	/* threshold storage */
1591*0Sstevel@tonic-gate 			int *ip;
1592*0Sstevel@tonic-gate 			int j;
1593*0Sstevel@tonic-gate 			size_t size;
1594*0Sstevel@tonic-gate 			extern int pm_thresh_specd(dev_info_t *);
1595*0Sstevel@tonic-gate 			extern int pm_valid_thresh(dev_info_t *,
1596*0Sstevel@tonic-gate 			    pm_thresh_rec_t *);
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 			for (ip = req.data; *ip; ip++) {
1599*0Sstevel@tonic-gate 				if (ip >= end) {
1600*0Sstevel@tonic-gate 					ret = EFAULT;
1601*0Sstevel@tonic-gate 					break;
1602*0Sstevel@tonic-gate 				}
1603*0Sstevel@tonic-gate 				comps++;
1604*0Sstevel@tonic-gate 				/* skip over indicated number of entries */
1605*0Sstevel@tonic-gate 				for (j = *ip; j; j--) {
1606*0Sstevel@tonic-gate 					if (++ip >= end) {
1607*0Sstevel@tonic-gate 						ret = EFAULT;
1608*0Sstevel@tonic-gate 						break;
1609*0Sstevel@tonic-gate 					}
1610*0Sstevel@tonic-gate 				}
1611*0Sstevel@tonic-gate 				if (ret)
1612*0Sstevel@tonic-gate 					break;
1613*0Sstevel@tonic-gate 			}
1614*0Sstevel@tonic-gate 			if (ret)
1615*0Sstevel@tonic-gate 				break;
1616*0Sstevel@tonic-gate 			if ((intptr_t)ip != (intptr_t)end - sizeof (int)) {
1617*0Sstevel@tonic-gate 				/* did not exactly fill buffer */
1618*0Sstevel@tonic-gate 				ret = EINVAL;
1619*0Sstevel@tonic-gate 				break;
1620*0Sstevel@tonic-gate 			}
1621*0Sstevel@tonic-gate 			if (comps == 0) {
1622*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s 0 components"
1623*0Sstevel@tonic-gate 				    "--EINVAL\n", cmdstr, req.physpath))
1624*0Sstevel@tonic-gate 				ret = EINVAL;
1625*0Sstevel@tonic-gate 				break;
1626*0Sstevel@tonic-gate 			}
1627*0Sstevel@tonic-gate 			/*
1628*0Sstevel@tonic-gate 			 * The header struct plus one entry struct per component
1629*0Sstevel@tonic-gate 			 * plus the size of the lists minus the counts
1630*0Sstevel@tonic-gate 			 * plus the length of the string
1631*0Sstevel@tonic-gate 			 */
1632*0Sstevel@tonic-gate 			size = sizeof (pm_thresh_rec_t) +
1633*0Sstevel@tonic-gate 			    (sizeof (pm_pte_t) * comps) + req.datasize -
1634*0Sstevel@tonic-gate 			    ((comps + 1) * sizeof (int)) +
1635*0Sstevel@tonic-gate 			    strlen(req.physpath) + 1;
1636*0Sstevel@tonic-gate 
1637*0Sstevel@tonic-gate 			rp = kmem_zalloc(size, KM_SLEEP);
1638*0Sstevel@tonic-gate 			rp->ptr_size = size;
1639*0Sstevel@tonic-gate 			rp->ptr_numcomps = comps;
1640*0Sstevel@tonic-gate 			ep = (pm_pte_t *)((intptr_t)rp + sizeof (*rp));
1641*0Sstevel@tonic-gate 			rp->ptr_entries = ep;
1642*0Sstevel@tonic-gate 			tp = (int *)((intptr_t)ep +
1643*0Sstevel@tonic-gate 			    (comps * sizeof (pm_pte_t)));
1644*0Sstevel@tonic-gate 			for (ip = req.data; *ip; ep++) {
1645*0Sstevel@tonic-gate 				ep->pte_numthresh = *ip;
1646*0Sstevel@tonic-gate 				ep->pte_thresh = tp;
1647*0Sstevel@tonic-gate 				for (j = *ip++; j; j--) {
1648*0Sstevel@tonic-gate 					*tp++ = *ip++;
1649*0Sstevel@tonic-gate 				}
1650*0Sstevel@tonic-gate 			}
1651*0Sstevel@tonic-gate 			(void) strcat((char *)tp, req.physpath);
1652*0Sstevel@tonic-gate 			rp->ptr_physpath = (char *)tp;
1653*0Sstevel@tonic-gate 			ASSERT((intptr_t)end == (intptr_t)ip + sizeof (int));
1654*0Sstevel@tonic-gate 			ASSERT((intptr_t)tp + strlen(req.physpath) + 1 ==
1655*0Sstevel@tonic-gate 			    (intptr_t)rp + rp->ptr_size);
1656*0Sstevel@tonic-gate 
1657*0Sstevel@tonic-gate 			ASSERT(dip == NULL);
1658*0Sstevel@tonic-gate 			/*
1659*0Sstevel@tonic-gate 			 * If this is not a currently power managed node,
1660*0Sstevel@tonic-gate 			 * then we can't check for validity of the thresholds
1661*0Sstevel@tonic-gate 			 */
1662*0Sstevel@tonic-gate 			if (!(dip = pm_name_to_dip(req.physpath, 1))) {
1663*0Sstevel@tonic-gate 				/* don't free rp, pm_record_thresh uses it */
1664*0Sstevel@tonic-gate 				pm_record_thresh(rp);
1665*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: pm_name_to_dip "
1666*0Sstevel@tonic-gate 				    "for %s failed\n", cmdstr, req.physpath))
1667*0Sstevel@tonic-gate 				ret = 0;
1668*0Sstevel@tonic-gate 				break;
1669*0Sstevel@tonic-gate 			}
1670*0Sstevel@tonic-gate 			ASSERT(!dipheld);
1671*0Sstevel@tonic-gate 			dipheld++;
1672*0Sstevel@tonic-gate 
1673*0Sstevel@tonic-gate 			if (!pm_valid_thresh(dip, rp)) {
1674*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: invalid thresh "
1675*0Sstevel@tonic-gate 				    "for %s@%s(%s#%d)\n", cmdstr,
1676*0Sstevel@tonic-gate 				    PM_DEVICE(dip)))
1677*0Sstevel@tonic-gate 				kmem_free(rp, size);
1678*0Sstevel@tonic-gate 				ret = EINVAL;
1679*0Sstevel@tonic-gate 				break;
1680*0Sstevel@tonic-gate 			}
1681*0Sstevel@tonic-gate 			/*
1682*0Sstevel@tonic-gate 			 * We don't just apply it ourselves because we'd need
1683*0Sstevel@tonic-gate 			 * to know too much about locking.  Since we don't
1684*0Sstevel@tonic-gate 			 * hold a lock the entry could be removed before
1685*0Sstevel@tonic-gate 			 * we get here
1686*0Sstevel@tonic-gate 			 */
1687*0Sstevel@tonic-gate 			pm_record_thresh(rp);
1688*0Sstevel@tonic-gate 			(void) pm_thresh_specd(dip);
1689*0Sstevel@tonic-gate 			ret = 0;
1690*0Sstevel@tonic-gate 			break;
1691*0Sstevel@tonic-gate 		}
1692*0Sstevel@tonic-gate 
1693*0Sstevel@tonic-gate 		case PM_GET_COMPONENT_THRESHOLDS:
1694*0Sstevel@tonic-gate 		{
1695*0Sstevel@tonic-gate 			int musthave;
1696*0Sstevel@tonic-gate 			int numthresholds = 0;
1697*0Sstevel@tonic-gate 			int wordsize;
1698*0Sstevel@tonic-gate 			int numcomps;
1699*0Sstevel@tonic-gate 			caddr_t uaddr = req.data;	/* user address */
1700*0Sstevel@tonic-gate 			int val;	/* int value to be copied out */
1701*0Sstevel@tonic-gate 			int32_t val32;	/* int32 value to be copied out */
1702*0Sstevel@tonic-gate 			caddr_t vaddr;	/* address to copyout from */
1703*0Sstevel@tonic-gate 			int j;
1704*0Sstevel@tonic-gate 
1705*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
1706*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
1707*0Sstevel@tonic-gate 				wordsize = sizeof (int32_t);
1708*0Sstevel@tonic-gate 			} else
1709*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1710*0Sstevel@tonic-gate 			{
1711*0Sstevel@tonic-gate 				wordsize = sizeof (int);
1712*0Sstevel@tonic-gate 			}
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 			ASSERT(dip);
1715*0Sstevel@tonic-gate 
1716*0Sstevel@tonic-gate 			numcomps = PM_NUMCMPTS(dip);
1717*0Sstevel@tonic-gate 			for (i = 0; i < numcomps; i++) {
1718*0Sstevel@tonic-gate 				cp = PM_CP(dip, i);
1719*0Sstevel@tonic-gate 				numthresholds += cp->pmc_comp.pmc_numlevels - 1;
1720*0Sstevel@tonic-gate 			}
1721*0Sstevel@tonic-gate 			musthave = (numthresholds + numcomps + 1) *  wordsize;
1722*0Sstevel@tonic-gate 			if (req.datasize < musthave) {
1723*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: size %ld, need "
1724*0Sstevel@tonic-gate 				    "%d--EINVAL\n", cmdstr, req.datasize,
1725*0Sstevel@tonic-gate 				    musthave))
1726*0Sstevel@tonic-gate 				ret = EINVAL;
1727*0Sstevel@tonic-gate 				break;
1728*0Sstevel@tonic-gate 			}
1729*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1730*0Sstevel@tonic-gate 			for (i = 0; i < numcomps; i++) {
1731*0Sstevel@tonic-gate 				int *thp;
1732*0Sstevel@tonic-gate 				cp = PM_CP(dip, i);
1733*0Sstevel@tonic-gate 				thp = cp->pmc_comp.pmc_thresh;
1734*0Sstevel@tonic-gate 				/* first copyout the count */
1735*0Sstevel@tonic-gate 				if (wordsize == sizeof (int32_t)) {
1736*0Sstevel@tonic-gate 					val32 = cp->pmc_comp.pmc_numlevels - 1;
1737*0Sstevel@tonic-gate 					vaddr = (caddr_t)&val32;
1738*0Sstevel@tonic-gate 				} else {
1739*0Sstevel@tonic-gate 					val = cp->pmc_comp.pmc_numlevels - 1;
1740*0Sstevel@tonic-gate 					vaddr = (caddr_t)&val;
1741*0Sstevel@tonic-gate 				}
1742*0Sstevel@tonic-gate 				if (ddi_copyout(vaddr, (void *)uaddr,
1743*0Sstevel@tonic-gate 				    wordsize, mode) != 0) {
1744*0Sstevel@tonic-gate 					PM_UNLOCK_DIP(dip);
1745*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: %s@%s"
1746*0Sstevel@tonic-gate 					    "(%s#%d) vaddr %p EFAULT\n",
1747*0Sstevel@tonic-gate 					    cmdstr, PM_DEVICE(dip),
1748*0Sstevel@tonic-gate 					    (void*)vaddr))
1749*0Sstevel@tonic-gate 					ret = EFAULT;
1750*0Sstevel@tonic-gate 					break;
1751*0Sstevel@tonic-gate 				}
1752*0Sstevel@tonic-gate 				vaddr = uaddr;
1753*0Sstevel@tonic-gate 				vaddr += wordsize;
1754*0Sstevel@tonic-gate 				uaddr = (caddr_t)vaddr;
1755*0Sstevel@tonic-gate 				/* then copyout each threshold value */
1756*0Sstevel@tonic-gate 				for (j = 0; j < cp->pmc_comp.pmc_numlevels - 1;
1757*0Sstevel@tonic-gate 				    j++) {
1758*0Sstevel@tonic-gate 					if (wordsize == sizeof (int32_t)) {
1759*0Sstevel@tonic-gate 						val32 = thp[j + 1];
1760*0Sstevel@tonic-gate 						vaddr = (caddr_t)&val32;
1761*0Sstevel@tonic-gate 					} else {
1762*0Sstevel@tonic-gate 						val = thp[i + 1];
1763*0Sstevel@tonic-gate 						vaddr = (caddr_t)&val;
1764*0Sstevel@tonic-gate 					}
1765*0Sstevel@tonic-gate 					if (ddi_copyout(vaddr, (void *) uaddr,
1766*0Sstevel@tonic-gate 					    wordsize, mode) != 0) {
1767*0Sstevel@tonic-gate 						PM_UNLOCK_DIP(dip);
1768*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s: "
1769*0Sstevel@tonic-gate 						    "%s@%s(%s#%d) uaddr %p "
1770*0Sstevel@tonic-gate 						    "EFAULT\n", cmdstr,
1771*0Sstevel@tonic-gate 						    PM_DEVICE(dip),
1772*0Sstevel@tonic-gate 						    (void *)uaddr))
1773*0Sstevel@tonic-gate 						ret = EFAULT;
1774*0Sstevel@tonic-gate 						break;
1775*0Sstevel@tonic-gate 					}
1776*0Sstevel@tonic-gate 					vaddr = uaddr;
1777*0Sstevel@tonic-gate 					vaddr += wordsize;
1778*0Sstevel@tonic-gate 					uaddr = (caddr_t)vaddr;
1779*0Sstevel@tonic-gate 				}
1780*0Sstevel@tonic-gate 			}
1781*0Sstevel@tonic-gate 			if (ret)
1782*0Sstevel@tonic-gate 				break;
1783*0Sstevel@tonic-gate 			/* last copyout a terminating 0 count */
1784*0Sstevel@tonic-gate 			if (wordsize == sizeof (int32_t)) {
1785*0Sstevel@tonic-gate 				val32 = 0;
1786*0Sstevel@tonic-gate 				vaddr = (caddr_t)&val32;
1787*0Sstevel@tonic-gate 			} else {
1788*0Sstevel@tonic-gate 				ASSERT(wordsize == sizeof (int));
1789*0Sstevel@tonic-gate 				val = 0;
1790*0Sstevel@tonic-gate 				vaddr = (caddr_t)&val;
1791*0Sstevel@tonic-gate 			}
1792*0Sstevel@tonic-gate 			if (ddi_copyout(vaddr, uaddr, wordsize, mode) != 0) {
1793*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
1794*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1795*0Sstevel@tonic-gate 				    "vaddr %p (0 count) EFAULT\n", cmdstr,
1796*0Sstevel@tonic-gate 				    PM_DEVICE(dip), (void *)vaddr))
1797*0Sstevel@tonic-gate 				ret = EFAULT;
1798*0Sstevel@tonic-gate 				break;
1799*0Sstevel@tonic-gate 			}
1800*0Sstevel@tonic-gate 			/* finished, so don't need to increment addresses */
1801*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1802*0Sstevel@tonic-gate 			ret = 0;
1803*0Sstevel@tonic-gate 			break;
1804*0Sstevel@tonic-gate 		}
1805*0Sstevel@tonic-gate 
1806*0Sstevel@tonic-gate 		case PM_GET_STATS:
1807*0Sstevel@tonic-gate 		{
1808*0Sstevel@tonic-gate 			time_t now;
1809*0Sstevel@tonic-gate 			time_t *timestamp;
1810*0Sstevel@tonic-gate 			extern int pm_cur_power(pm_component_t *cp);
1811*0Sstevel@tonic-gate 			int musthave;
1812*0Sstevel@tonic-gate 			int wordsize;
1813*0Sstevel@tonic-gate 
1814*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
1815*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
1816*0Sstevel@tonic-gate 				wordsize = sizeof (int32_t);
1817*0Sstevel@tonic-gate 			} else
1818*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1819*0Sstevel@tonic-gate 			{
1820*0Sstevel@tonic-gate 				wordsize = sizeof (int);
1821*0Sstevel@tonic-gate 			}
1822*0Sstevel@tonic-gate 
1823*0Sstevel@tonic-gate 			comps = PM_NUMCMPTS(dip);
1824*0Sstevel@tonic-gate 			if (comps == 0 || PM_GET_PM_INFO(dip) == NULL) {
1825*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s no components"
1826*0Sstevel@tonic-gate 				    " or not power managed--EINVAL\n", cmdstr,
1827*0Sstevel@tonic-gate 				    req.physpath))
1828*0Sstevel@tonic-gate 				ret = EINVAL;
1829*0Sstevel@tonic-gate 				break;
1830*0Sstevel@tonic-gate 			}
1831*0Sstevel@tonic-gate 			musthave = comps * 2 * wordsize;
1832*0Sstevel@tonic-gate 			if (req.datasize < musthave) {
1833*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: size %lu, need "
1834*0Sstevel@tonic-gate 				    "%d--EINVAL\n", cmdstr, req.datasize,
1835*0Sstevel@tonic-gate 				    musthave))
1836*0Sstevel@tonic-gate 				ret = EINVAL;
1837*0Sstevel@tonic-gate 				break;
1838*0Sstevel@tonic-gate 			}
1839*0Sstevel@tonic-gate 
1840*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
1841*0Sstevel@tonic-gate 			(void) drv_getparm(TIME, &now);
1842*0Sstevel@tonic-gate 			timestamp = kmem_zalloc(comps * sizeof (time_t),
1843*0Sstevel@tonic-gate 			    KM_SLEEP);
1844*0Sstevel@tonic-gate 			pm_get_timestamps(dip, timestamp);
1845*0Sstevel@tonic-gate 			/*
1846*0Sstevel@tonic-gate 			 * First the current power levels
1847*0Sstevel@tonic-gate 			 */
1848*0Sstevel@tonic-gate 			for (i = 0; i < comps; i++) {
1849*0Sstevel@tonic-gate 				int curpwr;
1850*0Sstevel@tonic-gate 				int32_t curpwr32;
1851*0Sstevel@tonic-gate 				caddr_t cpaddr;
1852*0Sstevel@tonic-gate 
1853*0Sstevel@tonic-gate 				cp = PM_CP(dip, i);
1854*0Sstevel@tonic-gate 				if (wordsize == sizeof (int)) {
1855*0Sstevel@tonic-gate 					curpwr = pm_cur_power(cp);
1856*0Sstevel@tonic-gate 					cpaddr = (caddr_t)&curpwr;
1857*0Sstevel@tonic-gate 				} else {
1858*0Sstevel@tonic-gate 					ASSERT(wordsize == sizeof (int32_t));
1859*0Sstevel@tonic-gate 					curpwr32 = pm_cur_power(cp);
1860*0Sstevel@tonic-gate 					cpaddr = (caddr_t)&curpwr32;
1861*0Sstevel@tonic-gate 				}
1862*0Sstevel@tonic-gate 				if (ddi_copyout(cpaddr, (void *) req.data,
1863*0Sstevel@tonic-gate 				    wordsize, mode) != 0) {
1864*0Sstevel@tonic-gate 					PM_UNLOCK_DIP(dip);
1865*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: %s@%s"
1866*0Sstevel@tonic-gate 					    "(%s#%d) req.data %p EFAULT\n",
1867*0Sstevel@tonic-gate 					    cmdstr, PM_DEVICE(dip),
1868*0Sstevel@tonic-gate 					    (void *)req.data))
1869*0Sstevel@tonic-gate 					ASSERT(!dipheld);
1870*0Sstevel@tonic-gate 					return (EFAULT);
1871*0Sstevel@tonic-gate 				}
1872*0Sstevel@tonic-gate 				cpaddr = (caddr_t)req.data;
1873*0Sstevel@tonic-gate 				cpaddr += wordsize;
1874*0Sstevel@tonic-gate 				req.data = cpaddr;
1875*0Sstevel@tonic-gate 			}
1876*0Sstevel@tonic-gate 			/*
1877*0Sstevel@tonic-gate 			 * Then the times remaining
1878*0Sstevel@tonic-gate 			 */
1879*0Sstevel@tonic-gate 			for (i = 0; i < comps; i++) {
1880*0Sstevel@tonic-gate 				int retval;
1881*0Sstevel@tonic-gate 				int32_t retval32;
1882*0Sstevel@tonic-gate 				caddr_t rvaddr;
1883*0Sstevel@tonic-gate 				int curpwr;
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate 				cp = PM_CP(dip, i);
1886*0Sstevel@tonic-gate 				curpwr = cp->pmc_cur_pwr;
1887*0Sstevel@tonic-gate 				if (curpwr == 0 || timestamp[i] == 0) {
1888*0Sstevel@tonic-gate 					PMD(PMD_STATS, ("ioctl: %s: "
1889*0Sstevel@tonic-gate 					    "cur_pwer %x, timestamp %lx\n",
1890*0Sstevel@tonic-gate 					    cmdstr, curpwr, timestamp[i]))
1891*0Sstevel@tonic-gate 					retval = INT_MAX;
1892*0Sstevel@tonic-gate 				} else {
1893*0Sstevel@tonic-gate 					int thresh;
1894*0Sstevel@tonic-gate 					(void) pm_current_threshold(dip, i,
1895*0Sstevel@tonic-gate 					    &thresh);
1896*0Sstevel@tonic-gate 					retval = thresh - (now - timestamp[i]);
1897*0Sstevel@tonic-gate 					PMD(PMD_STATS, ("ioctl: %s: current "
1898*0Sstevel@tonic-gate 					    "thresh %x, now %lx, timestamp %lx,"
1899*0Sstevel@tonic-gate 					    " retval %x\n", cmdstr, thresh, now,
1900*0Sstevel@tonic-gate 					    timestamp[i], retval))
1901*0Sstevel@tonic-gate 				}
1902*0Sstevel@tonic-gate 				if (wordsize == sizeof (int)) {
1903*0Sstevel@tonic-gate 					rvaddr = (caddr_t)&retval;
1904*0Sstevel@tonic-gate 				} else {
1905*0Sstevel@tonic-gate 					ASSERT(wordsize == sizeof (int32_t));
1906*0Sstevel@tonic-gate 					retval32 = retval;
1907*0Sstevel@tonic-gate 					rvaddr = (caddr_t)&retval32;
1908*0Sstevel@tonic-gate 				}
1909*0Sstevel@tonic-gate 				if (ddi_copyout(rvaddr, (void *) req.data,
1910*0Sstevel@tonic-gate 				    wordsize, mode) != 0) {
1911*0Sstevel@tonic-gate 					PM_UNLOCK_DIP(dip);
1912*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: %s@%s"
1913*0Sstevel@tonic-gate 					    "(%s#%d) req.data %p EFAULT\n",
1914*0Sstevel@tonic-gate 					    cmdstr, PM_DEVICE(dip),
1915*0Sstevel@tonic-gate 					    (void *)req.data))
1916*0Sstevel@tonic-gate 					ASSERT(!dipheld);
1917*0Sstevel@tonic-gate 					return (EFAULT);
1918*0Sstevel@tonic-gate 				}
1919*0Sstevel@tonic-gate 				rvaddr = (caddr_t)req.data;
1920*0Sstevel@tonic-gate 				rvaddr += wordsize;
1921*0Sstevel@tonic-gate 				req.data = (int *)rvaddr;
1922*0Sstevel@tonic-gate 			}
1923*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
1924*0Sstevel@tonic-gate 			*rval_p = comps;
1925*0Sstevel@tonic-gate 			ret = 0;
1926*0Sstevel@tonic-gate 			kmem_free(timestamp, comps * sizeof (time_t));
1927*0Sstevel@tonic-gate 			break;
1928*0Sstevel@tonic-gate 		}
1929*0Sstevel@tonic-gate 
1930*0Sstevel@tonic-gate 		case PM_GET_COMPONENT_NAME:
1931*0Sstevel@tonic-gate 			ASSERT(dip);
1932*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, req.component, &cp)) {
1933*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1934*0Sstevel@tonic-gate 				    "component %d > numcmpts - 1 %d--EINVAL\n",
1935*0Sstevel@tonic-gate 				    cmdstr, PM_DEVICE(dip), req.component,
1936*0Sstevel@tonic-gate 				    PM_NUMCMPTS(dip) - 1))
1937*0Sstevel@tonic-gate 				ret = EINVAL;
1938*0Sstevel@tonic-gate 				break;
1939*0Sstevel@tonic-gate 			}
1940*0Sstevel@tonic-gate 			if (ret = copyoutstr(cp->pmc_comp.pmc_name,
1941*0Sstevel@tonic-gate 			    (char *)req.data, req.datasize, &lencopied)) {
1942*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1943*0Sstevel@tonic-gate 				    "copyoutstr %p failed--EFAULT\n", cmdstr,
1944*0Sstevel@tonic-gate 				    PM_DEVICE(dip), (void *)req.data))
1945*0Sstevel@tonic-gate 				break;
1946*0Sstevel@tonic-gate 			}
1947*0Sstevel@tonic-gate 			*rval_p = lencopied;
1948*0Sstevel@tonic-gate 			ret = 0;
1949*0Sstevel@tonic-gate 			break;
1950*0Sstevel@tonic-gate 
1951*0Sstevel@tonic-gate 		case PM_GET_POWER_NAME:
1952*0Sstevel@tonic-gate 		{
1953*0Sstevel@tonic-gate 			int i;
1954*0Sstevel@tonic-gate 
1955*0Sstevel@tonic-gate 			ASSERT(dip);
1956*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, req.component, &cp)) {
1957*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1958*0Sstevel@tonic-gate 				    "component %d > numcmpts - 1 %d--EINVAL\n",
1959*0Sstevel@tonic-gate 				    cmdstr, PM_DEVICE(dip), req.component,
1960*0Sstevel@tonic-gate 				    PM_NUMCMPTS(dip) - 1))
1961*0Sstevel@tonic-gate 				ret = EINVAL;
1962*0Sstevel@tonic-gate 				break;
1963*0Sstevel@tonic-gate 			}
1964*0Sstevel@tonic-gate 			if ((i = req.value) < 0 ||
1965*0Sstevel@tonic-gate 			    i > cp->pmc_comp.pmc_numlevels - 1) {
1966*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1967*0Sstevel@tonic-gate 				    "value %d > num_levels - 1 %d--EINVAL\n",
1968*0Sstevel@tonic-gate 				    cmdstr, PM_DEVICE(dip), req.value,
1969*0Sstevel@tonic-gate 				    cp->pmc_comp.pmc_numlevels - 1))
1970*0Sstevel@tonic-gate 				ret = EINVAL;
1971*0Sstevel@tonic-gate 				break;
1972*0Sstevel@tonic-gate 			}
1973*0Sstevel@tonic-gate 			dep = cp->pmc_comp.pmc_lnames[req.value];
1974*0Sstevel@tonic-gate 			if (ret = copyoutstr(dep,
1975*0Sstevel@tonic-gate 			    req.data, req.datasize, &lencopied)) {
1976*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
1977*0Sstevel@tonic-gate 				    "copyoutstr %p failed--EFAULT\n", cmdstr,
1978*0Sstevel@tonic-gate 				    PM_DEVICE(dip), (void *)req.data))
1979*0Sstevel@tonic-gate 				break;
1980*0Sstevel@tonic-gate 			}
1981*0Sstevel@tonic-gate 			*rval_p = lencopied;
1982*0Sstevel@tonic-gate 			ret = 0;
1983*0Sstevel@tonic-gate 			break;
1984*0Sstevel@tonic-gate 		}
1985*0Sstevel@tonic-gate 
1986*0Sstevel@tonic-gate 		case PM_GET_POWER_LEVELS:
1987*0Sstevel@tonic-gate 		{
1988*0Sstevel@tonic-gate 			int musthave;
1989*0Sstevel@tonic-gate 			int numlevels;
1990*0Sstevel@tonic-gate 			int wordsize;
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
1993*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
1994*0Sstevel@tonic-gate 				wordsize = sizeof (int32_t);
1995*0Sstevel@tonic-gate 			} else
1996*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1997*0Sstevel@tonic-gate 			{
1998*0Sstevel@tonic-gate 				wordsize = sizeof (int);
1999*0Sstevel@tonic-gate 			}
2000*0Sstevel@tonic-gate 			ASSERT(dip);
2001*0Sstevel@tonic-gate 
2002*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, req.component, &cp)) {
2003*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
2004*0Sstevel@tonic-gate 				    "has %d components, component %d requested"
2005*0Sstevel@tonic-gate 				    "--EINVAL\n", cmdstr, PM_DEVICE(dip),
2006*0Sstevel@tonic-gate 				    PM_NUMCMPTS(dip), req.component))
2007*0Sstevel@tonic-gate 				ret = EINVAL;
2008*0Sstevel@tonic-gate 				break;
2009*0Sstevel@tonic-gate 			}
2010*0Sstevel@tonic-gate 			numlevels = cp->pmc_comp.pmc_numlevels;
2011*0Sstevel@tonic-gate 			musthave = numlevels *  wordsize;
2012*0Sstevel@tonic-gate 			if (req.datasize < musthave) {
2013*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: size %lu, need "
2014*0Sstevel@tonic-gate 				    "%d--EINVAL\n", cmdstr, req.datasize,
2015*0Sstevel@tonic-gate 				    musthave))
2016*0Sstevel@tonic-gate 				ret = EINVAL;
2017*0Sstevel@tonic-gate 				break;
2018*0Sstevel@tonic-gate 			}
2019*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
2020*0Sstevel@tonic-gate 			for (i = 0; i < numlevels; i++) {
2021*0Sstevel@tonic-gate 				int level;
2022*0Sstevel@tonic-gate 				int32_t level32;
2023*0Sstevel@tonic-gate 				caddr_t laddr;
2024*0Sstevel@tonic-gate 
2025*0Sstevel@tonic-gate 				if (wordsize == sizeof (int)) {
2026*0Sstevel@tonic-gate 					level = cp->pmc_comp.pmc_lvals[i];
2027*0Sstevel@tonic-gate 					laddr = (caddr_t)&level;
2028*0Sstevel@tonic-gate 				} else {
2029*0Sstevel@tonic-gate 					level32 = cp->pmc_comp.pmc_lvals[i];
2030*0Sstevel@tonic-gate 					laddr = (caddr_t)&level32;
2031*0Sstevel@tonic-gate 				}
2032*0Sstevel@tonic-gate 				if (ddi_copyout(laddr, (void *) req.data,
2033*0Sstevel@tonic-gate 				    wordsize, mode) != 0) {
2034*0Sstevel@tonic-gate 					PM_UNLOCK_DIP(dip);
2035*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: %s@%s"
2036*0Sstevel@tonic-gate 					    "(%s#%d) laddr %p EFAULT\n",
2037*0Sstevel@tonic-gate 					    cmdstr, PM_DEVICE(dip),
2038*0Sstevel@tonic-gate 					    (void *)laddr))
2039*0Sstevel@tonic-gate 					ASSERT(!dipheld);
2040*0Sstevel@tonic-gate 					return (EFAULT);
2041*0Sstevel@tonic-gate 				}
2042*0Sstevel@tonic-gate 				laddr = (caddr_t)req.data;
2043*0Sstevel@tonic-gate 				laddr += wordsize;
2044*0Sstevel@tonic-gate 				req.data = (int *)laddr;
2045*0Sstevel@tonic-gate 			}
2046*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
2047*0Sstevel@tonic-gate 			*rval_p = numlevels;
2048*0Sstevel@tonic-gate 			ret = 0;
2049*0Sstevel@tonic-gate 			break;
2050*0Sstevel@tonic-gate 		}
2051*0Sstevel@tonic-gate 
2052*0Sstevel@tonic-gate 
2053*0Sstevel@tonic-gate 		case PM_GET_NUM_POWER_LEVELS:
2054*0Sstevel@tonic-gate 			if (!e_pm_valid_comp(dip, req.component, &cp)) {
2055*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: %s@%s(%s#%d) "
2056*0Sstevel@tonic-gate 				    "component %d > numcmpts - 1 %d--EINVAL\n",
2057*0Sstevel@tonic-gate 				    cmdstr, PM_DEVICE(dip), req.component,
2058*0Sstevel@tonic-gate 				    PM_NUMCMPTS(dip) - 1))
2059*0Sstevel@tonic-gate 				ret = EINVAL;
2060*0Sstevel@tonic-gate 				break;
2061*0Sstevel@tonic-gate 			}
2062*0Sstevel@tonic-gate 			*rval_p = cp->pmc_comp.pmc_numlevels;
2063*0Sstevel@tonic-gate 			ret = 0;
2064*0Sstevel@tonic-gate 			break;
2065*0Sstevel@tonic-gate 
2066*0Sstevel@tonic-gate 		case PM_GET_DEVICE_THRESHOLD_BASIS:
2067*0Sstevel@tonic-gate 			ret = 0;
2068*0Sstevel@tonic-gate 			PM_LOCK_DIP(dip);
2069*0Sstevel@tonic-gate 			if ((info = PM_GET_PM_INFO(dip)) == NULL) {
2070*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
2071*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: "
2072*0Sstevel@tonic-gate 				    "PM_NO_PM_COMPONENTS\n", cmdstr))
2073*0Sstevel@tonic-gate 				*rval_p = PM_NO_PM_COMPONENTS;
2074*0Sstevel@tonic-gate 				break;
2075*0Sstevel@tonic-gate 			}
2076*0Sstevel@tonic-gate 			if (PM_ISDIRECT(dip)) {
2077*0Sstevel@tonic-gate 				PM_UNLOCK_DIP(dip);
2078*0Sstevel@tonic-gate 				*rval_p = PM_DIRECTLY_MANAGED;
2079*0Sstevel@tonic-gate 				break;
2080*0Sstevel@tonic-gate 			}
2081*0Sstevel@tonic-gate 			switch (DEVI(dip)->devi_pm_flags & PMC_THRESH_ALL) {
2082*0Sstevel@tonic-gate 			case PMC_DEF_THRESH:
2083*0Sstevel@tonic-gate 			case PMC_NEXDEF_THRESH:
2084*0Sstevel@tonic-gate 				*rval_p = PM_DEFAULT_THRESHOLD;
2085*0Sstevel@tonic-gate 				break;
2086*0Sstevel@tonic-gate 			case PMC_DEV_THRESH:
2087*0Sstevel@tonic-gate 				*rval_p = PM_DEVICE_THRESHOLD;
2088*0Sstevel@tonic-gate 				break;
2089*0Sstevel@tonic-gate 			case PMC_COMP_THRESH:
2090*0Sstevel@tonic-gate 				*rval_p = PM_COMPONENT_THRESHOLD;
2091*0Sstevel@tonic-gate 				break;
2092*0Sstevel@tonic-gate 			default:
2093*0Sstevel@tonic-gate 				if (PM_ISBC(dip)) {
2094*0Sstevel@tonic-gate 					*rval_p = PM_OLD_THRESHOLD;
2095*0Sstevel@tonic-gate 					break;
2096*0Sstevel@tonic-gate 				}
2097*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: default, not "
2098*0Sstevel@tonic-gate 				    "BC--EINVAL", cmdstr))
2099*0Sstevel@tonic-gate 				ret = EINVAL;
2100*0Sstevel@tonic-gate 				break;
2101*0Sstevel@tonic-gate 			}
2102*0Sstevel@tonic-gate 			PM_UNLOCK_DIP(dip);
2103*0Sstevel@tonic-gate 			break;
2104*0Sstevel@tonic-gate 		}
2105*0Sstevel@tonic-gate 		break;
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 	case PM_PSC:
2108*0Sstevel@tonic-gate 		/*
2109*0Sstevel@tonic-gate 		 * Commands that require pm_state_change_t as arg
2110*0Sstevel@tonic-gate 		 */
2111*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
2112*0Sstevel@tonic-gate 		if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2113*0Sstevel@tonic-gate 			pscp32 = (pm_state_change32_t *)arg;
2114*0Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, &psc32,
2115*0Sstevel@tonic-gate 			    sizeof (psc32), mode) != 0) {
2116*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: ddi_copyin "
2117*0Sstevel@tonic-gate 				    "EFAULT\n\n", cmdstr))
2118*0Sstevel@tonic-gate 				ASSERT(!dipheld);
2119*0Sstevel@tonic-gate 				return (EFAULT);
2120*0Sstevel@tonic-gate 			}
2121*0Sstevel@tonic-gate 			psc.physpath = (caddr_t)(uintptr_t)psc32.physpath;
2122*0Sstevel@tonic-gate 			psc.size = psc32.size;
2123*0Sstevel@tonic-gate 		} else
2124*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
2125*0Sstevel@tonic-gate 		{
2126*0Sstevel@tonic-gate 			pscp = (pm_state_change_t *)arg;
2127*0Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, &psc,
2128*0Sstevel@tonic-gate 			    sizeof (psc), mode) != 0) {
2129*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: ddi_copyin "
2130*0Sstevel@tonic-gate 				    "EFAULT\n\n", cmdstr))
2131*0Sstevel@tonic-gate 				ASSERT(!dipheld);
2132*0Sstevel@tonic-gate 				return (EFAULT);
2133*0Sstevel@tonic-gate 			}
2134*0Sstevel@tonic-gate 		}
2135*0Sstevel@tonic-gate 		switch (cmd) {
2136*0Sstevel@tonic-gate 
2137*0Sstevel@tonic-gate 		case PM_GET_STATE_CHANGE:
2138*0Sstevel@tonic-gate 		case PM_GET_STATE_CHANGE_WAIT:
2139*0Sstevel@tonic-gate 		{
2140*0Sstevel@tonic-gate 			psce_t			*pscep;
2141*0Sstevel@tonic-gate 			pm_state_change_t	*p;
2142*0Sstevel@tonic-gate 			caddr_t			physpath;
2143*0Sstevel@tonic-gate 			size_t			physlen;
2144*0Sstevel@tonic-gate 
2145*0Sstevel@tonic-gate 			/*
2146*0Sstevel@tonic-gate 			 * We want to know if any device has changed state.
2147*0Sstevel@tonic-gate 			 * We look up by clone.  In case we have another thread
2148*0Sstevel@tonic-gate 			 * from the same process, we loop.
2149*0Sstevel@tonic-gate 			 * pm_psc_clone_to_interest() returns a locked entry.
2150*0Sstevel@tonic-gate 			 * We create an internal copy of the event entry prior
2151*0Sstevel@tonic-gate 			 * to copyout to user space because we don't want to
2152*0Sstevel@tonic-gate 			 * hold the psce_lock while doing copyout as we might
2153*0Sstevel@tonic-gate 			 * hit page fault  which eventually brings us back
2154*0Sstevel@tonic-gate 			 * here requesting the same lock.
2155*0Sstevel@tonic-gate 			 */
2156*0Sstevel@tonic-gate 			mutex_enter(&pm_clone_lock);
2157*0Sstevel@tonic-gate 			if (!pm_interest_registered(clone))
2158*0Sstevel@tonic-gate 				pm_register_watcher(clone, NULL);
2159*0Sstevel@tonic-gate 			while ((pscep =
2160*0Sstevel@tonic-gate 			    pm_psc_clone_to_interest(clone)) == NULL) {
2161*0Sstevel@tonic-gate 				if (cmd == PM_GET_STATE_CHANGE) {
2162*0Sstevel@tonic-gate 					PMD(PMD_IOCTL, ("ioctl: %s: "
2163*0Sstevel@tonic-gate 					    "EWOULDBLOCK\n", cmdstr))
2164*0Sstevel@tonic-gate 					mutex_exit(&pm_clone_lock);
2165*0Sstevel@tonic-gate 					ASSERT(!dipheld);
2166*0Sstevel@tonic-gate 					return (EWOULDBLOCK);
2167*0Sstevel@tonic-gate 				} else {
2168*0Sstevel@tonic-gate 					if (cv_wait_sig(&pm_clones_cv[clone],
2169*0Sstevel@tonic-gate 					    &pm_clone_lock) == 0) {
2170*0Sstevel@tonic-gate 						mutex_exit(&pm_clone_lock);
2171*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s "
2172*0Sstevel@tonic-gate 						    "EINTR\n", cmdstr))
2173*0Sstevel@tonic-gate 						ASSERT(!dipheld);
2174*0Sstevel@tonic-gate 						return (EINTR);
2175*0Sstevel@tonic-gate 					}
2176*0Sstevel@tonic-gate 				}
2177*0Sstevel@tonic-gate 			}
2178*0Sstevel@tonic-gate 			mutex_exit(&pm_clone_lock);
2179*0Sstevel@tonic-gate 
2180*0Sstevel@tonic-gate 			physlen = pscep->psce_out->size;
2181*0Sstevel@tonic-gate 			physpath = NULL;
2182*0Sstevel@tonic-gate 			/*
2183*0Sstevel@tonic-gate 			 * If we were unable to store the path while bringing
2184*0Sstevel@tonic-gate 			 * up the console fb upon entering the prom, we give
2185*0Sstevel@tonic-gate 			 * a "" name with the overrun event set
2186*0Sstevel@tonic-gate 			 */
2187*0Sstevel@tonic-gate 			if (physlen == (size_t)-1) {	/* kmemalloc failed */
2188*0Sstevel@tonic-gate 				physpath = kmem_zalloc(1, KM_SLEEP);
2189*0Sstevel@tonic-gate 				physlen = 1;
2190*0Sstevel@tonic-gate 			}
2191*0Sstevel@tonic-gate 			if ((psc.physpath == NULL) || (psc.size < physlen)) {
2192*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: EFAULT\n", cmdstr))
2193*0Sstevel@tonic-gate 				mutex_exit(&pscep->psce_lock);
2194*0Sstevel@tonic-gate 				ret = EFAULT;
2195*0Sstevel@tonic-gate 				break;
2196*0Sstevel@tonic-gate 			}
2197*0Sstevel@tonic-gate 			if (physpath == NULL) {
2198*0Sstevel@tonic-gate 				physpath = kmem_zalloc(physlen, KM_SLEEP);
2199*0Sstevel@tonic-gate 				bcopy((const void *) pscep->psce_out->physpath,
2200*0Sstevel@tonic-gate 				    (void *) physpath, physlen);
2201*0Sstevel@tonic-gate 			}
2202*0Sstevel@tonic-gate 
2203*0Sstevel@tonic-gate 			p = pscep->psce_out;
2204*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
2205*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2206*0Sstevel@tonic-gate #ifdef DEBUG
2207*0Sstevel@tonic-gate 				size_t usrcopysize;
2208*0Sstevel@tonic-gate #endif
2209*0Sstevel@tonic-gate 				psc32.flags = (ushort_t)p->flags;
2210*0Sstevel@tonic-gate 				psc32.event = (ushort_t)p->event;
2211*0Sstevel@tonic-gate 				psc32.timestamp = (int32_t)p->timestamp;
2212*0Sstevel@tonic-gate 				psc32.component = (int32_t)p->component;
2213*0Sstevel@tonic-gate 				psc32.old_level = (int32_t)p->old_level;
2214*0Sstevel@tonic-gate 				psc32.new_level = (int32_t)p->new_level;
2215*0Sstevel@tonic-gate 				copysize32 = ((intptr_t)&psc32.size -
2216*0Sstevel@tonic-gate 				    (intptr_t)&psc32.component);
2217*0Sstevel@tonic-gate #ifdef DEBUG
2218*0Sstevel@tonic-gate 				usrcopysize = ((intptr_t)&pscp32->size -
2219*0Sstevel@tonic-gate 				    (intptr_t)&pscp32->component);
2220*0Sstevel@tonic-gate 				ASSERT(usrcopysize == copysize32);
2221*0Sstevel@tonic-gate #endif
2222*0Sstevel@tonic-gate 			} else
2223*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
2224*0Sstevel@tonic-gate 			{
2225*0Sstevel@tonic-gate 				psc.flags = p->flags;
2226*0Sstevel@tonic-gate 				psc.event = p->event;
2227*0Sstevel@tonic-gate 				psc.timestamp = p->timestamp;
2228*0Sstevel@tonic-gate 				psc.component = p->component;
2229*0Sstevel@tonic-gate 				psc.old_level = p->old_level;
2230*0Sstevel@tonic-gate 				psc.new_level = p->new_level;
2231*0Sstevel@tonic-gate 				copysize = ((long)&p->size -
2232*0Sstevel@tonic-gate 				    (long)&p->component);
2233*0Sstevel@tonic-gate 			}
2234*0Sstevel@tonic-gate 			if (p->size != (size_t)-1)
2235*0Sstevel@tonic-gate 				kmem_free(p->physpath, p->size);
2236*0Sstevel@tonic-gate 			p->size = 0;
2237*0Sstevel@tonic-gate 			p->physpath = NULL;
2238*0Sstevel@tonic-gate 			if (pscep->psce_out == pscep->psce_last)
2239*0Sstevel@tonic-gate 				p = pscep->psce_first;
2240*0Sstevel@tonic-gate 			else
2241*0Sstevel@tonic-gate 				p++;
2242*0Sstevel@tonic-gate 			pscep->psce_out = p;
2243*0Sstevel@tonic-gate 			mutex_exit(&pscep->psce_lock);
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate 			ret = copyoutstr(physpath, psc.physpath,
2246*0Sstevel@tonic-gate 			    physlen, &lencopied);
2247*0Sstevel@tonic-gate 			kmem_free(physpath, physlen);
2248*0Sstevel@tonic-gate 			if (ret) {
2249*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: copyoutstr %p "
2250*0Sstevel@tonic-gate 				    "failed--EFAULT\n", cmdstr,
2251*0Sstevel@tonic-gate 				    (void *)psc.physpath))
2252*0Sstevel@tonic-gate 				break;
2253*0Sstevel@tonic-gate 			}
2254*0Sstevel@tonic-gate 
2255*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
2256*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2257*0Sstevel@tonic-gate 				if (ddi_copyout(&psc32.component,
2258*0Sstevel@tonic-gate 				    &pscp32->component, copysize32, mode)
2259*0Sstevel@tonic-gate 				    != 0) {
2260*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: copyout "
2261*0Sstevel@tonic-gate 					    "failed--EFAULT\n", cmdstr))
2262*0Sstevel@tonic-gate 					ret = EFAULT;
2263*0Sstevel@tonic-gate 					break;
2264*0Sstevel@tonic-gate 				}
2265*0Sstevel@tonic-gate 			} else
2266*0Sstevel@tonic-gate #endif	/* _MULTI_DATAMODEL */
2267*0Sstevel@tonic-gate 			{
2268*0Sstevel@tonic-gate 				if (ddi_copyout(&psc.component,
2269*0Sstevel@tonic-gate 				    &pscp->component, copysize, mode) != 0) {
2270*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: copyout "
2271*0Sstevel@tonic-gate 					    "failed--EFAULT\n", cmdstr))
2272*0Sstevel@tonic-gate 					ret = EFAULT;
2273*0Sstevel@tonic-gate 					break;
2274*0Sstevel@tonic-gate 				}
2275*0Sstevel@tonic-gate 			}
2276*0Sstevel@tonic-gate 			ret = 0;
2277*0Sstevel@tonic-gate 			break;
2278*0Sstevel@tonic-gate 		}
2279*0Sstevel@tonic-gate 
2280*0Sstevel@tonic-gate 		case PM_DIRECT_NOTIFY:
2281*0Sstevel@tonic-gate 		case PM_DIRECT_NOTIFY_WAIT:
2282*0Sstevel@tonic-gate 		{
2283*0Sstevel@tonic-gate 			psce_t			*pscep;
2284*0Sstevel@tonic-gate 			pm_state_change_t	*p;
2285*0Sstevel@tonic-gate 			caddr_t			physpath;
2286*0Sstevel@tonic-gate 			size_t			physlen;
2287*0Sstevel@tonic-gate 			/*
2288*0Sstevel@tonic-gate 			 * We want to know if any direct device of ours has
2289*0Sstevel@tonic-gate 			 * something we should know about.  We look up by clone.
2290*0Sstevel@tonic-gate 			 * In case we have another thread from the same process,
2291*0Sstevel@tonic-gate 			 * we loop.
2292*0Sstevel@tonic-gate 			 * pm_psc_clone_to_direct() returns a locked entry.
2293*0Sstevel@tonic-gate 			 */
2294*0Sstevel@tonic-gate 			mutex_enter(&pm_clone_lock);
2295*0Sstevel@tonic-gate 			while (pm_poll_cnt[clone] == 0 ||
2296*0Sstevel@tonic-gate 			    (pscep = pm_psc_clone_to_direct(clone)) == NULL) {
2297*0Sstevel@tonic-gate 				if (cmd == PM_DIRECT_NOTIFY) {
2298*0Sstevel@tonic-gate 					PMD(PMD_IOCTL, ("ioctl: %s: "
2299*0Sstevel@tonic-gate 					    "EWOULDBLOCK\n", cmdstr))
2300*0Sstevel@tonic-gate 					mutex_exit(&pm_clone_lock);
2301*0Sstevel@tonic-gate 					ASSERT(!dipheld);
2302*0Sstevel@tonic-gate 					return (EWOULDBLOCK);
2303*0Sstevel@tonic-gate 				} else {
2304*0Sstevel@tonic-gate 					if (cv_wait_sig(&pm_clones_cv[clone],
2305*0Sstevel@tonic-gate 					    &pm_clone_lock) == 0) {
2306*0Sstevel@tonic-gate 						mutex_exit(&pm_clone_lock);
2307*0Sstevel@tonic-gate 						PMD(PMD_ERROR, ("ioctl: %s: "
2308*0Sstevel@tonic-gate 						    "EINTR\n", cmdstr))
2309*0Sstevel@tonic-gate 						ASSERT(!dipheld);
2310*0Sstevel@tonic-gate 						return (EINTR);
2311*0Sstevel@tonic-gate 					}
2312*0Sstevel@tonic-gate 				}
2313*0Sstevel@tonic-gate 			}
2314*0Sstevel@tonic-gate 			mutex_exit(&pm_clone_lock);
2315*0Sstevel@tonic-gate 			physlen = pscep->psce_out->size;
2316*0Sstevel@tonic-gate 			if ((psc.physpath == NULL) || (psc.size < physlen)) {
2317*0Sstevel@tonic-gate 				mutex_exit(&pscep->psce_lock);
2318*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: EFAULT\n",
2319*0Sstevel@tonic-gate 				    cmdstr))
2320*0Sstevel@tonic-gate 				ret = EFAULT;
2321*0Sstevel@tonic-gate 				break;
2322*0Sstevel@tonic-gate 			}
2323*0Sstevel@tonic-gate 			physpath = kmem_zalloc(physlen, KM_SLEEP);
2324*0Sstevel@tonic-gate 			bcopy((const void *) pscep->psce_out->physpath,
2325*0Sstevel@tonic-gate 			    (void *) physpath, physlen);
2326*0Sstevel@tonic-gate 
2327*0Sstevel@tonic-gate 			p = pscep->psce_out;
2328*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
2329*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2330*0Sstevel@tonic-gate #ifdef DEBUG
2331*0Sstevel@tonic-gate 				size_t usrcopysize;
2332*0Sstevel@tonic-gate #endif
2333*0Sstevel@tonic-gate 				psc32.component = (int32_t)p->component;
2334*0Sstevel@tonic-gate 				psc32.flags = (ushort_t)p->flags;
2335*0Sstevel@tonic-gate 				psc32.event = (ushort_t)p->event;
2336*0Sstevel@tonic-gate 				psc32.timestamp = (int32_t)p->timestamp;
2337*0Sstevel@tonic-gate 				psc32.old_level = (int32_t)p->old_level;
2338*0Sstevel@tonic-gate 				psc32.new_level = (int32_t)p->new_level;
2339*0Sstevel@tonic-gate 				copysize32 = (intptr_t)&psc32.size -
2340*0Sstevel@tonic-gate 				    (intptr_t)&psc32.component;
2341*0Sstevel@tonic-gate 				PMD(PMD_DPM, ("ioctl: %s: PDN32 %s, comp %d "
2342*0Sstevel@tonic-gate 				    "%d -> %d\n", cmdstr, physpath,
2343*0Sstevel@tonic-gate 				    p->component, p->old_level, p->new_level))
2344*0Sstevel@tonic-gate #ifdef DEBUG
2345*0Sstevel@tonic-gate 				usrcopysize = (intptr_t)&pscp32->size -
2346*0Sstevel@tonic-gate 				    (intptr_t)&pscp32->component;
2347*0Sstevel@tonic-gate 				ASSERT(usrcopysize == copysize32);
2348*0Sstevel@tonic-gate #endif
2349*0Sstevel@tonic-gate 			} else
2350*0Sstevel@tonic-gate #endif
2351*0Sstevel@tonic-gate 			{
2352*0Sstevel@tonic-gate 				psc.component = p->component;
2353*0Sstevel@tonic-gate 				psc.flags = p->flags;
2354*0Sstevel@tonic-gate 				psc.event = p->event;
2355*0Sstevel@tonic-gate 				psc.timestamp = p->timestamp;
2356*0Sstevel@tonic-gate 				psc.old_level = p->old_level;
2357*0Sstevel@tonic-gate 				psc.new_level = p->new_level;
2358*0Sstevel@tonic-gate 				copysize = (intptr_t)&p->size -
2359*0Sstevel@tonic-gate 				    (intptr_t)&p->component;
2360*0Sstevel@tonic-gate 				PMD(PMD_DPM, ("ioctl: %s: PDN %s, comp %d "
2361*0Sstevel@tonic-gate 				    "%d -> %d\n", cmdstr, physpath,
2362*0Sstevel@tonic-gate 				    p->component, p->old_level, p->new_level))
2363*0Sstevel@tonic-gate 			}
2364*0Sstevel@tonic-gate 			mutex_enter(&pm_clone_lock);
2365*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: %s: pm_poll_cnt[%d] is %d "
2366*0Sstevel@tonic-gate 			    "before decrement\n", cmdstr, clone,
2367*0Sstevel@tonic-gate 			    pm_poll_cnt[clone]))
2368*0Sstevel@tonic-gate 			pm_poll_cnt[clone]--;
2369*0Sstevel@tonic-gate 			mutex_exit(&pm_clone_lock);
2370*0Sstevel@tonic-gate 			kmem_free(p->physpath, p->size);
2371*0Sstevel@tonic-gate 			p->size = 0;
2372*0Sstevel@tonic-gate 			p->physpath = NULL;
2373*0Sstevel@tonic-gate 			if (pscep->psce_out == pscep->psce_last)
2374*0Sstevel@tonic-gate 				p = pscep->psce_first;
2375*0Sstevel@tonic-gate 			else
2376*0Sstevel@tonic-gate 				p++;
2377*0Sstevel@tonic-gate 			pscep->psce_out = p;
2378*0Sstevel@tonic-gate 			mutex_exit(&pscep->psce_lock);
2379*0Sstevel@tonic-gate 
2380*0Sstevel@tonic-gate 			ret = copyoutstr(physpath, psc.physpath,
2381*0Sstevel@tonic-gate 			    physlen, &lencopied);
2382*0Sstevel@tonic-gate 			kmem_free(physpath, physlen);
2383*0Sstevel@tonic-gate 			if (ret) {
2384*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: copyoutstr %p "
2385*0Sstevel@tonic-gate 				    "failed--EFAULT\n", cmdstr,
2386*0Sstevel@tonic-gate 				    (void *)psc.physpath))
2387*0Sstevel@tonic-gate 				break;
2388*0Sstevel@tonic-gate 			}
2389*0Sstevel@tonic-gate 
2390*0Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
2391*0Sstevel@tonic-gate 			if ((mode & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2392*0Sstevel@tonic-gate 				if (ddi_copyout(&psc32.component,
2393*0Sstevel@tonic-gate 				    &pscp32->component, copysize32, mode)
2394*0Sstevel@tonic-gate 					!= 0) {
2395*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: copyout "
2396*0Sstevel@tonic-gate 					    "failed--EFAULT\n", cmdstr))
2397*0Sstevel@tonic-gate 					ret = EFAULT;
2398*0Sstevel@tonic-gate 					break;
2399*0Sstevel@tonic-gate 				}
2400*0Sstevel@tonic-gate 			} else
2401*0Sstevel@tonic-gate #endif	/* _MULTI_DATAMODEL */
2402*0Sstevel@tonic-gate 			{
2403*0Sstevel@tonic-gate 				if (ddi_copyout(&psc.component,
2404*0Sstevel@tonic-gate 				    &pscp->component, copysize, mode) != 0) {
2405*0Sstevel@tonic-gate 					PMD(PMD_ERROR, ("ioctl: %s: copyout "
2406*0Sstevel@tonic-gate 					    "failed--EFAULT\n", cmdstr))
2407*0Sstevel@tonic-gate 					ret = EFAULT;
2408*0Sstevel@tonic-gate 					break;
2409*0Sstevel@tonic-gate 				}
2410*0Sstevel@tonic-gate 			}
2411*0Sstevel@tonic-gate 			ret = 0;
2412*0Sstevel@tonic-gate 			break;
2413*0Sstevel@tonic-gate 		}
2414*0Sstevel@tonic-gate 		default:
2415*0Sstevel@tonic-gate 			ASSERT(0);
2416*0Sstevel@tonic-gate 		}
2417*0Sstevel@tonic-gate 		break;
2418*0Sstevel@tonic-gate 
2419*0Sstevel@tonic-gate 	case NOSTRUCT:
2420*0Sstevel@tonic-gate 		switch (cmd) {
2421*0Sstevel@tonic-gate 		case PM_START_PM:
2422*0Sstevel@tonic-gate 			mutex_enter(&pm_scan_lock);
2423*0Sstevel@tonic-gate 			if (autopm_enabled) {
2424*0Sstevel@tonic-gate 				mutex_exit(&pm_scan_lock);
2425*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: EBUSY\n",
2426*0Sstevel@tonic-gate 				    cmdstr))
2427*0Sstevel@tonic-gate 				ret = EBUSY;
2428*0Sstevel@tonic-gate 				break;
2429*0Sstevel@tonic-gate 			}
2430*0Sstevel@tonic-gate 			autopm_enabled = 1;
2431*0Sstevel@tonic-gate 			mutex_exit(&pm_scan_lock);
2432*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_root_node(), pm_start_pm_walk, &cmd);
2433*0Sstevel@tonic-gate 			ret = 0;
2434*0Sstevel@tonic-gate 			break;
2435*0Sstevel@tonic-gate 
2436*0Sstevel@tonic-gate 		case PM_RESET_PM:
2437*0Sstevel@tonic-gate 		case PM_STOP_PM:
2438*0Sstevel@tonic-gate 		{
2439*0Sstevel@tonic-gate 			extern void pm_discard_thresholds(void);
2440*0Sstevel@tonic-gate 
2441*0Sstevel@tonic-gate 			mutex_enter(&pm_scan_lock);
2442*0Sstevel@tonic-gate 			if (!autopm_enabled && cmd != PM_RESET_PM) {
2443*0Sstevel@tonic-gate 				mutex_exit(&pm_scan_lock);
2444*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: EINVAL\n",
2445*0Sstevel@tonic-gate 				    cmdstr))
2446*0Sstevel@tonic-gate 				ret = EINVAL;
2447*0Sstevel@tonic-gate 				break;
2448*0Sstevel@tonic-gate 			}
2449*0Sstevel@tonic-gate 			autopm_enabled = 0;
2450*0Sstevel@tonic-gate 			mutex_exit(&pm_scan_lock);
2451*0Sstevel@tonic-gate 			/*
2452*0Sstevel@tonic-gate 			 * bring devices to full power level, stop scan
2453*0Sstevel@tonic-gate 			 */
2454*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_root_node(), pm_stop_pm_walk, &cmd);
2455*0Sstevel@tonic-gate 			ret = 0;
2456*0Sstevel@tonic-gate 			if (cmd == PM_STOP_PM)
2457*0Sstevel@tonic-gate 				break;
2458*0Sstevel@tonic-gate 			/*
2459*0Sstevel@tonic-gate 			 * Now do only PM_RESET_PM stuff.
2460*0Sstevel@tonic-gate 			 */
2461*0Sstevel@tonic-gate 			pm_system_idle_threshold = pm_default_idle_threshold;
2462*0Sstevel@tonic-gate 			pm_discard_thresholds();
2463*0Sstevel@tonic-gate 			pm_all_to_default_thresholds();
2464*0Sstevel@tonic-gate 			pm_dispatch_to_dep_thread(PM_DEP_WK_REMOVE_DEP,
2465*0Sstevel@tonic-gate 			    NULL, NULL, PM_DEP_WAIT, NULL, 0);
2466*0Sstevel@tonic-gate 			break;
2467*0Sstevel@tonic-gate 		}
2468*0Sstevel@tonic-gate 
2469*0Sstevel@tonic-gate 		case PM_GET_SYSTEM_THRESHOLD:
2470*0Sstevel@tonic-gate 			*rval_p = pm_system_idle_threshold;
2471*0Sstevel@tonic-gate 			ret = 0;
2472*0Sstevel@tonic-gate 			break;
2473*0Sstevel@tonic-gate 
2474*0Sstevel@tonic-gate 		case PM_GET_DEFAULT_SYSTEM_THRESHOLD:
2475*0Sstevel@tonic-gate 			*rval_p = pm_default_idle_threshold;
2476*0Sstevel@tonic-gate 			ret = 0;
2477*0Sstevel@tonic-gate 			break;
2478*0Sstevel@tonic-gate 
2479*0Sstevel@tonic-gate 		case PM_SET_SYSTEM_THRESHOLD:
2480*0Sstevel@tonic-gate 			if ((int)arg < 0) {
2481*0Sstevel@tonic-gate 				PMD(PMD_ERROR, ("ioctl: %s: arg 0x%x < 0"
2482*0Sstevel@tonic-gate 				    "--EINVAL\n", cmdstr, (int)arg))
2483*0Sstevel@tonic-gate 				ret = EINVAL;
2484*0Sstevel@tonic-gate 				break;
2485*0Sstevel@tonic-gate 			}
2486*0Sstevel@tonic-gate 			PMD(PMD_IOCTL, ("ioctl: %s: 0x%x 0t%d\n", cmdstr,
2487*0Sstevel@tonic-gate 			    (int)arg, (int)arg))
2488*0Sstevel@tonic-gate 			pm_system_idle_threshold = (int)arg;
2489*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_root_node(), pm_set_sys_threshold,
2490*0Sstevel@tonic-gate 			    (void *) &pm_system_idle_threshold);
2491*0Sstevel@tonic-gate 			ret = 0;
2492*0Sstevel@tonic-gate 			break;
2493*0Sstevel@tonic-gate 
2494*0Sstevel@tonic-gate 		case PM_IDLE_DOWN:
2495*0Sstevel@tonic-gate 			if (pm_timeout_idledown() != 0) {
2496*0Sstevel@tonic-gate 				ddi_walk_devs(ddi_root_node(),
2497*0Sstevel@tonic-gate 				    pm_start_idledown, (void *)PMID_IOC);
2498*0Sstevel@tonic-gate 			}
2499*0Sstevel@tonic-gate 			ret = 0;
2500*0Sstevel@tonic-gate 			break;
2501*0Sstevel@tonic-gate 
2502*0Sstevel@tonic-gate 		case PM_GET_PM_STATE:
2503*0Sstevel@tonic-gate 			if (autopm_enabled) {
2504*0Sstevel@tonic-gate 				*rval_p = PM_SYSTEM_PM_ENABLED;
2505*0Sstevel@tonic-gate 			} else {
2506*0Sstevel@tonic-gate 				*rval_p = PM_SYSTEM_PM_DISABLED;
2507*0Sstevel@tonic-gate 			}
2508*0Sstevel@tonic-gate 			ret = 0;
2509*0Sstevel@tonic-gate 			break;
2510*0Sstevel@tonic-gate 		}
2511*0Sstevel@tonic-gate 		break;
2512*0Sstevel@tonic-gate 
2513*0Sstevel@tonic-gate 
2514*0Sstevel@tonic-gate 	default:
2515*0Sstevel@tonic-gate 		/*
2516*0Sstevel@tonic-gate 		 * Internal error, invalid ioctl description
2517*0Sstevel@tonic-gate 		 * force debug entry even if pm_debug not set
2518*0Sstevel@tonic-gate 		 */
2519*0Sstevel@tonic-gate #ifdef	DEBUG
2520*0Sstevel@tonic-gate 		pm_log("ioctl: invalid str_type %d for cmd %d (%s)\n",
2521*0Sstevel@tonic-gate 		    pcip->str_type, cmd, pcip->name);
2522*0Sstevel@tonic-gate #endif
2523*0Sstevel@tonic-gate 		ASSERT(0);
2524*0Sstevel@tonic-gate 		return (EIO);
2525*0Sstevel@tonic-gate 	}
2526*0Sstevel@tonic-gate 	ASSERT(ret != 0x0badcafe);	/* some cmd in wrong case! */
2527*0Sstevel@tonic-gate 	if (dipheld) {
2528*0Sstevel@tonic-gate 		ASSERT(dip);
2529*0Sstevel@tonic-gate 		PMD(PMD_DHR, ("ioctl: %s: releasing %s@%s(%s#%d) for "
2530*0Sstevel@tonic-gate 		    "exiting pm_ioctl\n", cmdstr, PM_DEVICE(dip)))
2531*0Sstevel@tonic-gate 		PM_RELE(dip);
2532*0Sstevel@tonic-gate 	}
2533*0Sstevel@tonic-gate 	PMD(PMD_IOCTL, ("ioctl: %s: end, ret=%d\n", cmdstr, ret))
2534*0Sstevel@tonic-gate 	return (ret);
2535*0Sstevel@tonic-gate }
2536