1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* common code for ppm drivers
*/
#include <sys/modctl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/ppmvar.h>
#include <sys/ppmio.h>
#include <sys/epm.h>
#include <sys/open.h>
#include <sys/file.h>
#include <sys/policy.h>
#ifdef DEBUG
uint_t ppm_debug = 0;
#endif
int ppm_inst = -1;
char *ppm_prefix;
void *ppm_statep;
/*
* common module _init
*/
int
ppm_init(struct modlinkage *mlp, size_t size, char *prefix)
{
#ifdef DEBUG
char *str = "ppm_init";
#endif
int error;
ppm_prefix = prefix;
error = ddi_soft_state_init(&ppm_statep, size, 1);
DPRINTF(D_INIT, ("%s: ss init %d\n", str, error));
if (error != DDI_SUCCESS)
return (error);
if (error = mod_install(mlp))
ddi_soft_state_fini(&ppm_statep);
DPRINTF(D_INIT, ("%s: mod_install %d\n", str, error));
return (error);
}
/* ARGSUSED */
int
ppm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
struct ppm_unit *overlay;
int rval;
if (ppm_inst == -1)
return (DDI_FAILURE);
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
if (overlay = ddi_get_soft_state(ppm_statep, ppm_inst)) {
*resultp = overlay->dip;
rval = DDI_SUCCESS;
} else
rval = DDI_FAILURE;
return (rval);
case DDI_INFO_DEVT2INSTANCE:
*resultp = (void *)(uintptr_t)ppm_inst;
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/* ARGSUSED */
int
ppm_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
if (otyp != OTYP_CHR)
return (EINVAL);
DPRINTF(D_OPEN, ("ppm_open: \"%s\", devp 0x%p, flag 0x%x, otyp %d\n",
ppm_prefix, (void *)devp, flag, otyp));
return (0);
}
/* ARGSUSED */
int
ppm_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
DPRINTF(D_CLOSE, ("ppm_close: \"%s\", dev 0x%lx, flag 0x%x, otyp %d\n",
ppm_prefix, dev, flag, otyp));
return (DDI_SUCCESS);
}
/*
* lookup arrays of strings from configuration data (XXppm.conf)
*/
static int
ppm_get_confdata(struct ppm_cdata **cdp, dev_info_t *dip)
{
struct ppm_cdata *cinfo;
int err;
for (; (cinfo = *cdp) != NULL; cdp++) {
err = ddi_prop_lookup_string_array(
DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
cinfo->name, &cinfo->strings, &cinfo->cnt);
if (err != DDI_PROP_SUCCESS) {
DPRINTF(D_ERROR,
("ppm_get_confdata: no %s found\n", cinfo->name));
break;
}
}
return (err);
}
/*
* free allocated ddi prop strings, and free
* ppm_db_t lists where there's an error.
*/
static int
ppm_attach_err(struct ppm_cdata **cdp, int err)
{
ppm_domain_t **dompp;
ppm_db_t *db, *tmp;
if (cdp) {
for (; *cdp; cdp++) {
if ((*cdp)->strings) {
ddi_prop_free((*cdp)->strings);
(*cdp)->strings = NULL;
}
}
}
if (err != DDI_SUCCESS) {
for (dompp = ppm_domains; *dompp; dompp++) {
for (db = (*dompp)->conflist; (tmp = db) != NULL; ) {
db = db->next;
kmem_free(tmp->name, strlen(tmp->name) + 1);
kmem_free(tmp, sizeof (*tmp));
}
(*dompp)->conflist = NULL;
}
err = DDI_FAILURE;
}
return (err);
}
ppm_domain_t *
ppm_lookup_domain(char *dname)
{
ppm_domain_t **dompp;
for (dompp = ppm_domains; *dompp; dompp++)
if (strcmp(dname, (*dompp)->name) == 0)
break;
return (*dompp);
}
/*
* create a ppm-private database from parsed .conf data; we start with
* two string arrays (device pathnames and domain names) and treat them
* as matched pairs where device[N] is part of domain[N]
*/
int
ppm_create_db(dev_info_t *dip)
{
#ifdef DEBUG
char *str = "ppm_create_db";
#endif
struct ppm_cdata devdata, domdata, *cdata[3];
ppm_domain_t *domp;
ppm_db_t *new;
char **dev_namep, **dom_namep;
char *wild;
int err;
bzero(&devdata, sizeof (devdata));
bzero(&domdata, sizeof (domdata));
devdata.name = "ppm-devices";
domdata.name = "ppm-domains";
cdata[0] = &devdata;
cdata[1] = &domdata;
cdata[2] = NULL;
if (err = ppm_get_confdata(cdata, dip))
return (ppm_attach_err(cdata, err));
else if (devdata.cnt != domdata.cnt) {
DPRINTF(D_ERROR,
("%s: %sppm.conf has a mismatched number of %s and %s\n",
str, ppm_prefix, devdata.name, domdata.name));
return (ppm_attach_err(cdata, DDI_FAILURE));
}
/*
* loop through device/domain pairs and build
* a linked list of devices within known domains
*/
for (dev_namep = devdata.strings, dom_namep = domdata.strings;
*dev_namep; dev_namep++, dom_namep++) {
domp = ppm_lookup_domain(*dom_namep);
if (domp == NULL) {
DPRINTF(D_ERROR, ("%s: invalid domain \"%s\" for "
"device \"%s\"\n", str, *dom_namep, *dev_namep));
return (ppm_attach_err(cdata, DDI_FAILURE));
}
/*
* allocate a new ppm db entry and link it to
* the front of conflist within this domain
*/
new = kmem_zalloc(sizeof (*new), KM_SLEEP);
new->name = kmem_zalloc(strlen(*dev_namep) + 1, KM_SLEEP);
(void) strcpy(new->name, *dev_namep);
new->next = domp->conflist;
domp->conflist = new;
/*
* when the device name contains a wildcard,
* save the length of the preceding string
*/
if (wild = strchr(new->name, '*'))
new->plen = (wild - new->name);
DPRINTF(D_CREATEDB, ("%s: \"%s\", added \"%s\"\n",
str, domp->name, new->name));
}
return (ppm_attach_err(cdata, DDI_SUCCESS));
}
/*
* scan conf devices within each domain for a matching device name
*/
ppm_domain_t *
ppm_lookup_dev(dev_info_t *dip)
{
char path[MAXNAMELEN];
ppm_domain_t **dompp;
ppm_db_t *dbp;
(void) ddi_pathname(dip, path);
for (dompp = ppm_domains; *dompp; dompp++) {
for (dbp = (*dompp)->conflist; dbp; dbp = dbp->next) {
if (dbp->plen == 0) {
if (strcmp(path, dbp->name) == 0)
return (*dompp);
} else if (strncmp(path, dbp->name, dbp->plen) == 0)
return (*dompp);
}
}
return (NULL);
}
/*
* returns 1 (claimed), 0 (not claimed)
*/
int
ppm_claim_dev(dev_info_t *dip)
{
ppm_domain_t *domp;
domp = ppm_lookup_dev(dip);
#ifdef DEBUG
if (domp) {
char path[MAXNAMELEN];
DPRINTF(D_CLAIMDEV,
("ppm_claim_dev: \"%s\", matched \"%s\"\n",
domp->name, ddi_pathname(dip, path)));
}
#endif
return (domp != NULL);
}
/*
* create/init a new ppm device and link into the domain
*/
ppm_dev_t *
ppm_add_dev(dev_info_t *dip, ppm_domain_t *domp)
{
char path[MAXNAMELEN];
ppm_dev_t *new = NULL;
int cmpt;
ASSERT(MUTEX_HELD(&domp->lock));
(void) ddi_pathname(dip, path);
/*
* For devs which have exported "pm-components" we want to create
* a data structure for each component. When a driver chooses not
* to export the prop we treat its device as having a single
* component and build a structure for it anyway. All other ppm
* logic will act as if this device were always up and can thus
* make correct decisions about it in relation to other devices
* in its domain.
*/
for (cmpt = PM_GET_PM_INFO(dip) ? PM_NUMCMPTS(dip) : 1; cmpt--; ) {
new = kmem_zalloc(sizeof (*new), KM_SLEEP);
new->path = kmem_zalloc(strlen(path) + 1, KM_SLEEP);
(void) strcpy(new->path, path);
new->domp = domp;
new->dip = dip;
new->cmpt = cmpt;
if (ppmf.dev_init)
(*ppmf.dev_init)(new);
new->next = domp->devlist;
domp->devlist = new;
DPRINTF(D_ADDDEV,
("ppm_add_dev: \"%s\", \"%s\", ppm_dev 0x%p\n",
new->path, domp->name, (void *)new));
}
ASSERT(new != NULL);
/*
* devi_pm_ppm_private should be set only after all
* ppm_dev s related to all components have been
* initialized and domain's pwr_cnt is incremented
* for each of them.
*/
PPM_SET_PRIVATE(dip, new);
return (new);
}
/*
* returns an existing or newly created ppm device reference
*/
ppm_dev_t *
ppm_get_dev(dev_info_t *dip, ppm_domain_t *domp)
{
ppm_dev_t *pdp;
mutex_enter(&domp->lock);
pdp = PPM_GET_PRIVATE(dip);
if (pdp == NULL)
pdp = ppm_add_dev(dip, domp);
mutex_exit(&domp->lock);
return (pdp);
}
/*
* scan a domain's device list and remove those with .dip
* matching the arg *dip; we need to scan the entire list
* for the case of devices with multiple components
*/
void
ppm_rem_dev(dev_info_t *dip)
{
ppm_dev_t *pdp, **devpp;
ppm_domain_t *domp;
pdp = PPM_GET_PRIVATE(dip);
ASSERT(pdp);
domp = pdp->domp;
ASSERT(domp);
mutex_enter(&domp->lock);
for (devpp = &domp->devlist; (pdp = *devpp) != NULL; ) {
if (pdp->dip != dip) {
devpp = &pdp->next;
continue;
}
DPRINTF(D_REMDEV, ("ppm_rem_dev: path \"%s\", ppm_dev 0x%p\n",
pdp->path, (void *)pdp));
PPM_SET_PRIVATE(dip, NULL);
*devpp = pdp->next;
if (ppmf.dev_fini)
(*ppmf.dev_fini)(pdp);
kmem_free(pdp->path, strlen(pdp->path) + 1);
kmem_free(pdp, sizeof (*pdp));
}
mutex_exit(&domp->lock);
}
/* ARGSUSED */
int
ppm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
cred_t *cred_p, int *rval_p)
{
#ifdef DEBUG
char *str = "ppm_ioctl";
char *rwfmt = "%s: mode error: 0x%x is missing %s perm, cmd 0x%x\n";
char *iofmt = "%s: copy%s error, arg 0x%p\n";
#endif
ppmreq_t req;
uint8_t level;
DPRINTF(D_IOCTL, ("%s: dev 0x%lx, cmd 0x%x, arg 0x%lx, mode 0x%x\n",
str, dev, cmd, arg, mode));
if (ddi_copyin((caddr_t)arg, &req, sizeof (req), mode)) {
DPRINTF(D_IOCTL, (iofmt, str, "in", arg));
return (EFAULT);
}
/*
* Currently, only PPM_INTERNAL_DEVICE_POWER device type is supported
*/
if (req.ppmdev != PPM_INTERNAL_DEVICE_POWER) {
DPRINTF(D_IOCTL, ("%s: unrecognized device type %d\n",
str, req.ppmdev));
return (EINVAL);
}
switch (cmd) {
case PPMIOCSET:
if (secpolicy_power_mgmt(cred_p) != 0) {
DPRINTF(D_IOCTL, ("%s: bad cred for cmd 0x%x\n",
str, cmd));
return (EPERM);
} else if (!(mode & FWRITE)) {
DPRINTF(D_IOCTL, (rwfmt, str, mode, "write"));
return (EPERM);
}
level = req.ppmop.idev_power.level;
if ((level != PPM_IDEV_POWER_ON) &&
(level != PPM_IDEV_POWER_OFF)) {
DPRINTF(D_IOCTL,
("%s: invalid power level %d, cmd 0x%x\n",
str, level, cmd));
return (EINVAL);
}
if (ppmf.iocset == NULL)
return (ENOTSUP);
(*ppmf.iocset)(level);
break;
case PPMIOCGET:
if (!(mode & FREAD)) {
DPRINTF(D_IOCTL, (rwfmt, str, mode, "read"));
return (EPERM);
}
if (ppmf.iocget == NULL)
return (ENOTSUP);
req.ppmop.idev_power.level = (*ppmf.iocget)();
if (ddi_copyout((const void *)&req, (void *)arg,
sizeof (req), mode)) {
DPRINTF(D_ERROR, (iofmt, str, "out", arg));
return (EFAULT);
}
break;
default:
DPRINTF(D_IOCTL, ("%s: unrecognized cmd 0x%x\n", str, cmd));
return (EINVAL);
}
return (0);
}
#ifdef DEBUG
#define FLINTSTR(flags, sym) { flags, sym, #sym }
#define PMR_UNKNOWN -1
/*
* convert a ctlop integer to a char string. this helps printing
* meaningful info when cltops are received from the pm framework.
* since some ctlops are so frequent, we use mask to limit output:
* a valid string is returned when ctlop is found and when
* (cmd.flags & mask) is true; otherwise NULL is returned.
*/
char *
ppm_get_ctlstr(int ctlop, uint_t mask)
{
struct ctlop_cmd {
uint_t flags;
int ctlop;
char *str;
};
struct ctlop_cmd *ccp;
static struct ctlop_cmd cmds[] = {
FLINTSTR(D_SETPWR, PMR_SET_POWER),
FLINTSTR(D_CTLOPS2, PMR_SUSPEND),
FLINTSTR(D_CTLOPS2, PMR_RESUME),
FLINTSTR(D_CTLOPS2, PMR_PRE_SET_POWER),
FLINTSTR(D_CTLOPS2, PMR_POST_SET_POWER),
FLINTSTR(D_CTLOPS2, PMR_PPM_SET_POWER),
FLINTSTR(0, PMR_PPM_ATTACH),
FLINTSTR(0, PMR_PPM_DETACH),
FLINTSTR(D_CTLOPS1, PMR_PPM_POWER_CHANGE_NOTIFY),
FLINTSTR(D_CTLOPS1, PMR_REPORT_PMCAP),
FLINTSTR(D_CTLOPS1, PMR_CHANGED_POWER),
FLINTSTR(D_CTLOPS2, PMR_PPM_PRE_PROBE),
FLINTSTR(D_CTLOPS2, PMR_PPM_POST_PROBE),
FLINTSTR(D_CTLOPS2, PMR_PPM_PRE_ATTACH),
FLINTSTR(D_CTLOPS2, PMR_PPM_POST_ATTACH),
FLINTSTR(D_CTLOPS2, PMR_PPM_PRE_DETACH),
FLINTSTR(D_CTLOPS2, PMR_PPM_POST_DETACH),
FLINTSTR(D_CTLOPS1, PMR_PPM_UNMANAGE),
FLINTSTR(D_CTLOPS2, PMR_PPM_PRE_RESUME),
FLINTSTR(D_CTLOPS1, PMR_PPM_ALL_LOWEST),
FLINTSTR(D_LOCKS, PMR_PPM_LOCK_POWER),
FLINTSTR(D_LOCKS, PMR_PPM_UNLOCK_POWER),
FLINTSTR(D_LOCKS, PMR_PPM_TRY_LOCK_POWER),
FLINTSTR(D_CTLOPS1 | D_CTLOPS2, PMR_UNKNOWN),
};
for (ccp = cmds; ccp->ctlop != PMR_UNKNOWN; ccp++)
if (ctlop == ccp->ctlop)
break;
if (ccp->flags & mask)
return (ccp->str);
return (NULL);
}
#endif
|