Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/gen_drv.c
+++ new/usr/src/uts/common/io/gen_drv.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26
27 27
28 28 /*
29 29 * generic character driver
30 30 */
31 31 #include <sys/types.h>
32 32 #include <sys/param.h>
33 33 #include <sys/errno.h>
34 34 #include <sys/uio.h>
35 35 #include <sys/buf.h>
36 36 #include <sys/modctl.h>
37 37 #include <sys/open.h>
38 38 #include <sys/kmem.h>
39 39 #include <sys/conf.h>
40 40 #include <sys/cmn_err.h>
41 41 #include <sys/stat.h>
42 42 #include <sys/ddi.h>
43 43 #include <sys/sunddi.h>
44 44 #include <sys/sunndi.h>
45 45
46 46
47 47 #define NUMEVENTS 6
48 48 #define COMPONENTS 2
49 49 #define COMP_0_MAXPWR 3
50 50 #define COMP_1_MAXPWR 2
51 51 #define MINPWR 0
52 52 static int maxpwr[] = { COMP_0_MAXPWR, COMP_1_MAXPWR };
53 53
54 54 /*
55 55 * The state for each generic device.
56 56 * NOTE: We save the node_type in the state structure. The node_type string
57 57 * (and not a copy) is stashed in a minor node by ddi_create_minor_node(),
58 58 * so ddi_remove_minor_node() must occur prior to state free.
59 59 */
60 60 typedef struct dstate {
61 61 uint_t flag;
62 62 dev_info_t *dip; /* my devinfo handle */
63 63 char *node_type; /* stable node_type copy */
64 64 ddi_callback_id_t gen_cb_ids[NUMEVENTS];
65 65 kmutex_t lock;
66 66 char *nodename;
67 67 int level[COMPONENTS]; /* pm level */
68 68 int busy[COMPONENTS]; /* busy state */
69 69 } dstate_t;
70 70
71 71
72 72 static void *dstates;
73 73
74 74 static int gen_debug = 0;
75 75
76 76 #ifdef DEBUG
77 77 #define gen_debug gen_debug_on
78 78 static int gen_debug_on = 0;
79 79 #define GEN_DEBUG(args) if (gen_debug) cmn_err args
80 80 #else
81 81 #define GEN_DEBUG(args)
82 82 #endif
83 83
84 84 extern void prom_printf(const char *fmt, ...);
85 85
86 86 static int gen_open(dev_t *devp, int flag, int otyp, cred_t *cred);
87 87 static int gen_close(dev_t devp, int flag, int otyp, cred_t *cred);
88 88 static int gen_read(dev_t dev, struct uio *uiop, cred_t *credp);
89 89 static int gen_write(dev_t dev, struct uio *uiop, cred_t *credp);
90 90 static int gen_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
91 91 cred_t *credp, int *rvalp);
92 92 static int gen_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
93 93 static int gen_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
94 94 static void gen_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
95 95 void *arg, void *impl_data);
96 96
97 97 static int gen_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
98 98 void **result);
99 99 static int gen_create_minor_nodes(dev_info_t *, struct dstate *);
100 100 static int gen_power(dev_info_t *, int, int);
101 101
102 102 static struct cb_ops gen_cb_ops = {
103 103 gen_open, /* open */
104 104 gen_close, /* close */
105 105 nodev, /* strategy */
106 106 nodev, /* print */
107 107 nodev, /* dump */
108 108 gen_read, /* read */
109 109 gen_write, /* write */
110 110 gen_ioctl, /* ioctl */
111 111 nodev, /* devmap */
112 112 nodev, /* mmap */
113 113 nodev, /* segmap */
114 114 nochpoll, /* poll */
115 115 ddi_prop_op, /* prop_op */
116 116 NULL, /* streamtab */
117 117 D_NEW | D_MP | D_HOTPLUG, /* flag */
118 118 CB_REV, /* cb_rev */
119 119 nodev, /* aread */
120 120 nodev /* awrite */
121 121 };
122 122
123 123
124 124 static struct dev_ops gen_ops = {
125 125 DEVO_REV, /* devo_rev */
126 126 0, /* refcnt */
127 127 gen_info, /* getinfo */
128 128 nulldev, /* identify */
129 129 nulldev, /* probe */
130 130 gen_attach, /* attach */
131 131 gen_detach, /* detach */
132 132 nodev, /* reset */
133 133 &gen_cb_ops, /* driver ops */
134 134 (struct bus_ops *)0, /* bus ops */
135 135 gen_power, /* power */
136 136 ddi_quiesce_not_supported, /* devo_quiesce */
137 137 };
138 138
139 139 /*
140 140 * INST_TO_MINOR() gives the starting minor number for a given gen_drv driver
141 141 * instance. A shift left by 6 bits allows for each instance to have upto
142 142 * 64 (2^6) minor numbers. The maximum minor number allowed by the system
143 143 * is L_MAXMIN32 (0x3ffff). This effectively limits the gen_drv instance
144 144 * numbers from 0 to 0xfff for a total of 4096 instances.
145 145 */
146 146 #define INST_TO_MINOR(i) (i << 6)
147 147 #define MINOR_TO_INST(mn) (mn >> 6)
148 148
149 149 static char *mnodetypes[] = {
150 150 "ddi_nt",
151 151 "ddi_nt:device_type",
152 152 "ddi_nt:device_class:bus_class",
153 153 "ddi_nt2",
154 154 "ddi_nt2:device_type",
155 155 "ddi_nt2:device_type:bus_class",
↓ open down ↓ |
155 lines elided |
↑ open up ↑ |
156 156 };
157 157 #define N_NTYPES (sizeof (mnodetypes) / sizeof (char *))
158 158
159 159 static struct modldrv modldrv = {
160 160 &mod_driverops,
161 161 "generic test driver",
162 162 &gen_ops
163 163 };
164 164
165 165 static struct modlinkage modlinkage = {
166 - MODREV_1, &modldrv, NULL
166 + MODREV_1, { &modldrv, NULL }
167 167 };
168 168
169 169
170 170 /*
171 171 * flags
172 172 */
173 173 #define OPEN_FLAG 0x001
174 174 #define PWR_HAS_CHANGED_ON_RESUME_FLAG 0x002
175 175 #define FAIL_SUSPEND_FLAG 0x004
176 176 #define PUP_WITH_PWR_HAS_CHANGED_FLAG 0x008
177 177 #define POWER_FLAG 0x010
178 178 #define LOWER_POWER_FLAG 0x020
179 179 #define NO_INVOL_FLAG 0x040
180 180 #define PM_SUPPORTED_FLAG 0x080
181 181
182 182 /*
183 183 * ioctl commands (non-devctl ioctl commands)
184 184 */
185 185 #define GENDRV_IOCTL ('P' << 8)
186 186 #define GENDRV_IOFAULT_SIMULATE (GENDRV_IOCTL | 0)
187 187 #define GENDRV_NDI_EVENT_TEST (GENDRV_IOCTL | 1)
188 188
189 189 int
190 190 _init(void)
191 191 {
192 192 int e;
193 193
194 194 if ((e = ddi_soft_state_init(&dstates,
195 195 sizeof (struct dstate), 0)) != 0) {
196 196 return (e);
197 197 }
198 198
199 199 if ((e = mod_install(&modlinkage)) != 0) {
200 200 ddi_soft_state_fini(&dstates);
201 201 }
202 202
203 203 return (e);
204 204 }
205 205
206 206 int
207 207 _fini(void)
208 208 {
209 209 int e;
210 210
211 211 if ((e = mod_remove(&modlinkage)) != 0) {
212 212 return (e);
213 213 }
214 214 ddi_soft_state_fini(&dstates);
215 215 return (e);
216 216 }
217 217
218 218 int
219 219 _info(struct modinfo *modinfop)
220 220 {
221 221 return (mod_info(&modlinkage, modinfop));
222 222 }
223 223
224 224 static int
225 225 gen_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
226 226 {
227 227 int instance = ddi_get_instance(devi);
228 228 struct dstate *dstatep;
229 229 int rval;
230 230 int n_devs;
231 231 int n_minorcomps;
232 232 int isclone;
233 233 ddi_eventcookie_t dev_offline_cookie, dev_reset_cookie;
234 234 ddi_eventcookie_t bus_reset_cookie, bus_quiesce_cookie;
235 235 ddi_eventcookie_t bus_unquiesce_cookie, bus_test_post_cookie;
236 236 int i_init = 0;
237 237 int level_tmp;
238 238
239 239 int i;
240 240 char *pm_comp[] = {
241 241 "NAME=leaf0",
242 242 "0=D0",
243 243 "1=D1",
244 244 "2=D2",
245 245 "3=D3",
246 246 "NAME=leaf1",
247 247 "0=off",
248 248 "1=blank",
249 249 "2=on"};
250 250 char *pm_hw_state = {"needs-suspend-resume"};
251 251
252 252
253 253 switch (cmd) {
254 254 case DDI_ATTACH:
255 255
256 256 if (ddi_soft_state_zalloc(dstates, instance) !=
257 257 DDI_SUCCESS) {
258 258 cmn_err(CE_CONT, "%s%d: can't allocate state\n",
259 259 ddi_get_name(devi), instance);
260 260
261 261 return (DDI_FAILURE);
262 262 }
263 263
264 264 dstatep = ddi_get_soft_state(dstates, instance);
265 265 dstatep->dip = devi;
266 266 mutex_init(&dstatep->lock, NULL, MUTEX_DRIVER, NULL);
267 267
268 268 n_devs = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
269 269 "ndevs", 1);
270 270
271 271 isclone = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
272 272 "isclone", 0);
273 273
274 274 n_minorcomps = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
275 275 "ncomps", 1);
276 276
277 277 GEN_DEBUG((CE_CONT,
278 278 "%s%d attaching: n_devs=%d n_minorcomps=%d isclone=%d",
279 279 ddi_get_name(devi), ddi_get_instance(devi),
280 280 n_devs, n_minorcomps, isclone));
281 281
282 282 if (isclone) {
283 283 if (ddi_create_minor_node(devi, "gen", S_IFCHR,
284 284 INST_TO_MINOR(instance), mnodetypes[0],
285 285 isclone) != DDI_SUCCESS) {
286 286 ddi_remove_minor_node(devi, NULL);
287 287 ddi_soft_state_free(dstates, instance);
288 288 cmn_err(CE_WARN, "%s%d: can't create minor "
289 289 "node", ddi_get_name(devi), instance);
290 290
291 291 return (DDI_FAILURE);
292 292 }
293 293 rval = DDI_SUCCESS;
294 294 } else {
295 295 rval = gen_create_minor_nodes(devi, dstatep);
296 296 if (rval != DDI_SUCCESS) {
297 297 ddi_prop_remove_all(devi);
298 298 ddi_remove_minor_node(devi, NULL);
299 299 ddi_soft_state_free(dstates, instance);
300 300 cmn_err(CE_WARN, "%s%d: can't create minor "
301 301 "nodes", ddi_get_name(devi), instance);
302 302
303 303 return (DDI_FAILURE);
304 304 }
305 305 }
306 306
307 307 if (ddi_get_eventcookie(devi, "pshot_dev_offline",
308 308 &dev_offline_cookie) == DDI_SUCCESS) {
309 309 (void) ddi_add_event_handler(devi, dev_offline_cookie,
310 310 gen_event_cb, NULL, &(dstatep->gen_cb_ids[0]));
311 311 }
312 312
313 313 if (ddi_get_eventcookie(devi, "pshot_dev_reset",
314 314 &dev_reset_cookie) == DDI_SUCCESS) {
315 315 (void) ddi_add_event_handler(devi, dev_reset_cookie,
316 316 gen_event_cb, NULL, &(dstatep->gen_cb_ids[1]));
317 317 }
318 318
319 319 if (ddi_get_eventcookie(devi, "pshot_bus_reset",
320 320 &bus_reset_cookie) == DDI_SUCCESS) {
321 321 (void) ddi_add_event_handler(devi, bus_reset_cookie,
322 322 gen_event_cb, NULL, &(dstatep->gen_cb_ids[2]));
323 323 }
324 324
325 325 if (ddi_get_eventcookie(devi, "pshot_bus_quiesce",
326 326 &bus_quiesce_cookie) == DDI_SUCCESS) {
327 327 (void) ddi_add_event_handler(devi, bus_quiesce_cookie,
328 328 gen_event_cb, NULL, &(dstatep->gen_cb_ids[3]));
329 329 }
330 330
331 331 if (ddi_get_eventcookie(devi, "pshot_bus_unquiesce",
332 332 &bus_unquiesce_cookie) == DDI_SUCCESS) {
333 333 (void) ddi_add_event_handler(devi,
334 334 bus_unquiesce_cookie, gen_event_cb,
335 335 NULL, &(dstatep->gen_cb_ids[4]));
336 336 }
337 337
338 338 if (ddi_get_eventcookie(devi, "pshot_bus_test_post",
339 339 &bus_test_post_cookie) == DDI_SUCCESS) {
340 340 (void) ddi_add_event_handler(devi,
341 341 bus_test_post_cookie, gen_event_cb,
342 342 NULL, &(dstatep->gen_cb_ids[5]));
343 343 }
344 344
345 345 /*
346 346 * initialize the devices' pm state
347 347 */
348 348 mutex_enter(&dstatep->lock);
349 349 dstatep->flag &= ~OPEN_FLAG;
350 350 dstatep->flag &= ~PWR_HAS_CHANGED_ON_RESUME_FLAG;
351 351 dstatep->flag &= ~FAIL_SUSPEND_FLAG;
352 352 dstatep->flag &= ~PUP_WITH_PWR_HAS_CHANGED_FLAG;
353 353 dstatep->flag |= LOWER_POWER_FLAG;
354 354 dstatep->flag &= ~NO_INVOL_FLAG;
355 355 dstatep->flag |= PM_SUPPORTED_FLAG;
356 356 dstatep->busy[0] = 0;
357 357 dstatep->busy[1] = 0;
358 358 dstatep->level[0] = -1;
359 359 dstatep->level[1] = -1;
360 360 mutex_exit(&dstatep->lock);
361 361
362 362 /*
363 363 * stash the nodename
364 364 */
365 365 dstatep->nodename = ddi_node_name(devi);
366 366
367 367 /*
368 368 * Check if the no-involuntary-power-cycles property
369 369 * was created. Set NO_INVOL_FLAG if so.
370 370 */
371 371 if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
372 372 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
373 373 "no-involuntary-power-cycles") == 1) {
374 374 GEN_DEBUG((CE_CONT,
375 375 "%s%d: DDI_ATTACH:\n\tno-involuntary-power-cycles"
376 376 " property was created",
377 377 ddi_node_name(devi), ddi_get_instance(devi)));
378 378 mutex_enter(&dstatep->lock);
379 379 dstatep->flag |= NO_INVOL_FLAG;
380 380 mutex_exit(&dstatep->lock);
381 381 }
382 382
383 383 /*
384 384 * Check if the dependency-property property
385 385 * was created.
386 386 */
387 387 if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
388 388 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
389 389 "dependency-property") == 1) {
390 390 GEN_DEBUG((CE_CONT,
391 391 "%s%d: DDI_ATTACH:\n\tdependency-property"
392 392 " property was created",
393 393 ddi_node_name(devi), ddi_get_instance(devi)));
394 394 }
395 395
396 396 /*
397 397 * create the pm-components property. two comps:
398 398 * 4 levels on comp0, 3 on comp 1.
399 399 * - skip for a "tape" device, clear PM_SUPPORTED_FLAG
400 400 */
401 401 if (strcmp(ddi_node_name(devi), "tape") != 0) {
402 402 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
403 403 "pm-components", pm_comp, 9) != DDI_PROP_SUCCESS) {
404 404 cmn_err(CE_WARN, "%s%d: %s\n",
405 405 ddi_node_name(devi),
406 406 ddi_get_instance(devi),
407 407 "unable to create \"pm-components\" "
408 408 " property.");
409 409
410 410 return (DDI_FAILURE);
411 411 }
412 412 } else {
413 413 mutex_enter(&dstatep->lock);
414 414 dstatep->flag &= ~PM_SUPPORTED_FLAG;
415 415 mutex_exit(&dstatep->lock);
416 416 }
417 417
418 418 /*
419 419 * Check if the pm-components property was created
420 420 */
421 421 if (dstatep->flag & PM_SUPPORTED_FLAG) {
422 422 if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
423 423 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
424 424 "pm-components") != 1) {
425 425 cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t%s",
426 426 ddi_node_name(devi),
427 427 ddi_get_instance(devi),
428 428 "\"pm-components\" property does"
429 429 " not exist");
430 430
431 431 return (DDI_FAILURE);
432 432
433 433 } else {
434 434 GEN_DEBUG((CE_CONT, "%s%d: DDI_ATTACH:"
435 435 " created pm-components property",
436 436 ddi_node_name(devi),
437 437 ddi_get_instance(devi)));
438 438 }
439 439 }
440 440
441 441 /*
442 442 * create the pm-hardware-state property.
443 443 * needed to get DDI_SUSPEND and DDI_RESUME calls
444 444 */
445 445 if (ddi_prop_update_string(DDI_DEV_T_NONE, devi,
446 446 "pm-hardware-state", pm_hw_state) != DDI_PROP_SUCCESS) {
447 447 cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t%s\n",
448 448 ddi_node_name(devi), ddi_get_instance(devi),
449 449 "unable to create \"pm-hardware-state\" "
450 450 " property.");
451 451
452 452 return (DDI_FAILURE);
453 453 }
454 454
455 455 /*
456 456 * set power levels to max via pm_raise_power(),
457 457 */
458 458 mutex_enter(&dstatep->lock);
459 459 i_init = (dstatep->flag & PM_SUPPORTED_FLAG) ? 0 : COMPONENTS;
460 460 mutex_exit(&dstatep->lock);
461 461 for (i = i_init; i < COMPONENTS; i++) {
462 462 GEN_DEBUG((CE_CONT,
463 463 "%s%d: DDI_ATTACH: pm_raise_power comp %d "
464 464 "to level %d", ddi_node_name(devi),
465 465 ddi_get_instance(devi), i, maxpwr[i]));
466 466 if (pm_raise_power(dstatep->dip, i, maxpwr[i]) !=
467 467 DDI_SUCCESS) {
468 468 cmn_err(CE_WARN,
469 469 "%s%d: DDI_ATTACH: pm_raise_power failed\n",
470 470 ddi_node_name(devi),
471 471 ddi_get_instance(devi));
472 472 dstatep->level[i] = -1;
473 473
474 474 return (DDI_FAILURE);
475 475 }
476 476 }
477 477
478 478 if (rval == DDI_SUCCESS) {
479 479 ddi_report_dev(devi);
480 480 }
481 481 return (rval);
482 482
483 483
484 484 case DDI_RESUME:
485 485 GEN_DEBUG((CE_CONT, "%s%d: DDI_RESUME", ddi_node_name(devi),
486 486 ddi_get_instance(devi)));
487 487
488 488 dstatep = ddi_get_soft_state(dstates, ddi_get_instance(devi));
489 489 if (dstatep == NULL) {
490 490
491 491 return (DDI_FAILURE);
492 492 }
493 493
494 494 /*
495 495 * Call pm_power_has_changed() if flag
496 496 * PWR_HAS_CHANGED_ON_RESUME_FLAG is set,
497 497 * then clear the flag
498 498 */
499 499 mutex_enter(&dstatep->lock);
500 500 i_init = (dstatep->flag & PM_SUPPORTED_FLAG) ? 0 : COMPONENTS;
501 501 mutex_exit(&dstatep->lock);
502 502 if (dstatep->flag & PWR_HAS_CHANGED_ON_RESUME_FLAG) {
503 503 for (i = i_init; i < COMPONENTS; i++) {
504 504 GEN_DEBUG((CE_CONT,
505 505 "%s%d: DDI_RESUME: pm_power_has_changed "
506 506 "comp %d to level %d", ddi_node_name(devi),
507 507 ddi_get_instance(devi), i, maxpwr[i]));
508 508 mutex_enter(&dstatep->lock);
509 509 level_tmp = dstatep->level[i];
510 510 dstatep->level[i] = maxpwr[i];
511 511 if (pm_power_has_changed(dstatep->dip, i,
512 512 maxpwr[i]) != DDI_SUCCESS) {
513 513 cmn_err(CE_WARN,
514 514 "%s%d: DDI_RESUME:\n\t"
515 515 " pm_power_has_changed"
516 516 " failed: comp %d to level %d\n",
517 517 ddi_node_name(devi),
518 518 ddi_get_instance(devi),
519 519 i, maxpwr[i]);
520 520 dstatep->level[i] = level_tmp;
521 521 }
522 522 mutex_exit(&dstatep->lock);
523 523 }
524 524 } else {
525 525 /*
526 526 * Call pm_raise_power() instead
527 527 */
528 528 for (i = i_init; i < COMPONENTS; i++) {
529 529 GEN_DEBUG((CE_CONT,
530 530 "%s%d: DDI_RESUME: pm_raise_power"
531 531 " comp %d to level %d",
532 532 ddi_node_name(devi), ddi_get_instance(devi),
533 533 i, maxpwr[i]));
534 534 if (pm_raise_power(dstatep->dip, i, maxpwr[i])
535 535 != DDI_SUCCESS) {
536 536 cmn_err(CE_WARN,
537 537 "%s%d: DDI_RESUME:"
538 538 "\n\tpm_raise_power"
539 539 "failed: comp %d to level %d\n",
540 540 ddi_node_name(devi),
541 541 ddi_get_instance(devi),
542 542 i, maxpwr[i]);
543 543 }
544 544 }
545 545 }
546 546
547 547 return (DDI_SUCCESS);
548 548
549 549 default:
550 550 GEN_DEBUG((CE_WARN, "attach: default"));
551 551 return (DDI_FAILURE);
552 552 }
553 553 }
554 554
555 555 static int
556 556 gen_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
557 557 {
558 558 struct dstate *dstatep;
559 559 int instance;
560 560 int i;
561 561 int rv;
562 562 int rm_power;
563 563 int level_tmp;
564 564
565 565 #ifdef DEBUG
566 566 int n_devs;
567 567 int n_minorcomps;
568 568 int isclone;
569 569 #endif
570 570
571 571 switch (cmd) {
572 572 case DDI_DETACH:
573 573 GEN_DEBUG((CE_CONT, "%s%d: DDI_DETACH", ddi_node_name(devi),
574 574 ddi_get_instance(devi)));
575 575
576 576 instance = ddi_get_instance(devi);
577 577 dstatep = ddi_get_soft_state(dstates, instance);
578 578 if (dstatep == NULL) {
579 579
580 580 return (DDI_FAILURE);
581 581 }
582 582
583 583 #ifdef DEBUG
584 584 n_devs = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
585 585 "ndevs", 1);
586 586
587 587 isclone = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
588 588 "isclone", 0);
589 589
590 590 n_minorcomps = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
591 591 "ncomps", 1);
592 592 #endif /* DEBUG */
593 593
594 594 /*
595 595 * power off component 1.
596 596 */
597 597 if (dstatep->flag & PM_SUPPORTED_FLAG) {
598 598 GEN_DEBUG((CE_CONT,
599 599 "%s%d: DDI_DETACH: pm_lower_power comp 1 level %d",
600 600 ddi_node_name(devi), ddi_get_instance(devi),
601 601 MINPWR));
602 602 if (pm_lower_power(dstatep->dip, 1, MINPWR)
603 603 != DDI_SUCCESS) {
604 604 cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
605 605 "pm_lower_power failed for comp 1 to"
606 606 " level %d\n", ddi_node_name(devi),
607 607 ddi_get_instance(devi), MINPWR);
608 608
609 609 return (DDI_FAILURE);
610 610 }
611 611
612 612 /*
613 613 * check power level. Issue pm_power_has_changed
614 614 * if not at MINPWR.
615 615 */
616 616 mutex_enter(&dstatep->lock);
617 617 level_tmp = dstatep->level[1];
618 618 dstatep->level[1] = MINPWR;
619 619 if (dstatep->level[1] != MINPWR) {
620 620 GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
621 621 " power off via pm_power_has_changed"
622 622 " instead", ddi_node_name(devi),
623 623 ddi_get_instance(devi)));
624 624 if (pm_power_has_changed(dstatep->dip,
625 625 1, MINPWR) != DDI_SUCCESS) {
626 626 GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
627 627 " pm_power_has_changed failed for"
628 628 " comp 1 to level %d",
629 629 ddi_node_name(devi),
630 630 ddi_get_instance(devi),
631 631 MINPWR));
632 632 dstatep->level[1] = level_tmp;
633 633 mutex_exit(&dstatep->lock);
634 634
635 635 return (DDI_FAILURE);
636 636 }
637 637 }
638 638 mutex_exit(&dstatep->lock);
639 639 }
640 640
641 641 /*
642 642 * If the LOWER_POWER_FLAG flag is not set,
643 643 * don't call pm_lowr_power() for comp 0.
644 644 * This should be used only for the XXXXX@XX,no_invol
645 645 * devices that export the
646 646 * no-involuntary-power-cycles property
647 647 */
648 648 if (!(dstatep->flag & LOWER_POWER_FLAG) &&
649 649 dstatep->flag & PM_SUPPORTED_FLAG) {
650 650 cmn_err(CE_NOTE, "%s%d: DDI_DETACH:\n\t"
651 651 " NOT CALLING PM_LOWER_POWER():"
652 652 " LOWER_POWER_FLAG NOT SET\n",
653 653 ddi_node_name(devi), ddi_get_instance(devi));
654 654 } else if (dstatep->flag & PM_SUPPORTED_FLAG) {
655 655 GEN_DEBUG((CE_CONT,
656 656 "%s%d: DDI_DETACH: pm_lower_power comp 0 level %d",
657 657 ddi_node_name(devi), ddi_get_instance(devi),
658 658 MINPWR));
659 659 if (pm_lower_power(dstatep->dip, 0, MINPWR)
660 660 != DDI_SUCCESS) {
661 661 cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
662 662 "pm_lower_power failed for comp 0 to"
663 663 " level %d\n", ddi_node_name(devi),
664 664 ddi_get_instance(devi), MINPWR);
665 665
666 666 return (DDI_FAILURE);
667 667 }
668 668
669 669 /*
670 670 * check power level. Issue pm_power_has_changed
671 671 * if not at MINPWR.
672 672 */
673 673 mutex_enter(&dstatep->lock);
674 674 level_tmp = dstatep->level[0];
675 675 dstatep->level[0] = MINPWR;
676 676 if (dstatep->level[0] != MINPWR) {
677 677 GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
678 678 " power off via pm_power_has_changed"
679 679 " instead", ddi_node_name(devi),
680 680 ddi_get_instance(devi)));
681 681 if (pm_power_has_changed(dstatep->dip,
682 682 0, MINPWR) != DDI_SUCCESS) {
683 683 GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
684 684 " pm_power_has_changed failed for"
685 685 " comp 0 to level %d",
686 686 ddi_node_name(devi),
687 687 ddi_get_instance(devi),
688 688 MINPWR));
689 689 dstatep->level[0] = level_tmp;
690 690 mutex_exit(&dstatep->lock);
691 691
692 692 return (DDI_FAILURE);
693 693 }
694 694 }
695 695 mutex_exit(&dstatep->lock);
696 696 }
697 697
698 698 GEN_DEBUG((CE_CONT,
699 699 "%s%d detaching: n_devs=%d n_minorcomps=%d isclone=%d",
700 700 ddi_node_name(devi), ddi_get_instance(devi),
701 701 n_devs, n_minorcomps, isclone));
702 702
703 703 for (i = 0; i < NUMEVENTS; i++) {
704 704 if (dstatep->gen_cb_ids[i]) {
705 705 (void) ddi_remove_event_handler(dstatep->gen_cb_ids[i]);
706 706 dstatep->gen_cb_ids[i] = NULL;
707 707 }
708 708 }
709 709
710 710 ddi_prop_remove_all(devi);
711 711 ddi_remove_minor_node(devi, NULL);
712 712 if (dstatep->node_type)
713 713 kmem_free(dstatep->node_type,
714 714 strlen(dstatep->node_type) + 1);
715 715 ddi_soft_state_free(dstates, instance);
716 716 return (DDI_SUCCESS);
717 717
718 718 case DDI_SUSPEND:
719 719 GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND",
720 720 ddi_node_name(devi), ddi_get_instance(devi)));
721 721
722 722 instance = ddi_get_instance(devi);
723 723 dstatep = ddi_get_soft_state(dstates, instance);
724 724 if (dstatep == NULL) {
725 725
726 726 return (DDI_FAILURE);
727 727 }
728 728
729 729 /*
730 730 * fail the suspend if FAIL_SUSPEND_FLAG is set.
731 731 * clear the FAIL_SUSPEND_FLAG flag
732 732 */
733 733 mutex_enter(&dstatep->lock);
734 734 if (dstatep->flag & FAIL_SUSPEND_FLAG) {
735 735 GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:"
736 736 " FAIL_SUSPEND_FLAG is set,"
737 737 " fail suspend",
738 738 ddi_node_name(devi), ddi_get_instance(devi)));
739 739 dstatep->flag &= ~FAIL_SUSPEND_FLAG;
740 740 rv = DDI_FAILURE;
741 741 } else {
742 742 rv = DDI_SUCCESS;
743 743 }
744 744 mutex_exit(&dstatep->lock);
745 745
746 746 /*
747 747 * Issue ddi_removing_power() to determine if the suspend
748 748 * was initiated by either CPR or DR. If CPR, the system
749 749 * will be powered OFF; if this driver has set the
750 750 * NO_INVOL_FLAG, then refuse to suspend. If DR, power
751 751 * will not be removed, thus allow the suspend.
752 752 */
753 753 if (dstatep->flag & NO_INVOL_FLAG &&
754 754 dstatep->flag & PM_SUPPORTED_FLAG) {
755 755 GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:"
756 756 " check via ddi_removing_power()",
757 757 ddi_node_name(devi), ddi_get_instance(devi)));
758 758
759 759 rm_power = ddi_removing_power(dstatep->dip);
760 760
761 761 if (rm_power < 0) {
762 762 cmn_err(CE_WARN, "%s%d: DDI_SUSPEND:"
763 763 " ddi_removing_power() failed\n",
764 764 ddi_node_name(devi),
765 765 ddi_get_instance(devi));
766 766 } else if (rm_power == 1) {
767 767 /*
768 768 * CPR: power will be removed
769 769 */
770 770 GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:\n\t"
771 771 " CPR: POWER WILL BE REMOVED, THEREFORE"
772 772 " REFUSE TO SUSPEND", ddi_node_name(devi),
773 773 ddi_get_instance(devi)));
774 774 rv = DDI_FAILURE;
775 775 } else if (rm_power == 0) {
776 776 /*
777 777 * DR: power will not be removed
778 778 */
779 779 GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:\n\t"
780 780 " DR: POWER WILL NOT BE REMOVED, THEREFORE"
781 781 " ALLOW THE SUSPEND", ddi_node_name(devi),
782 782 ddi_get_instance(devi)));
783 783 rv = DDI_SUCCESS;
784 784 }
785 785 }
786 786
787 787 /*
788 788 * power OFF via pm_power_has_changed()
789 789 */
790 790 mutex_enter(&dstatep->lock);
791 791 if (dstatep->flag & PM_SUPPORTED_FLAG &&
792 792 !(dstatep->flag & NO_INVOL_FLAG)) {
793 793 level_tmp = dstatep->level[0];
794 794 dstatep->level[0] = MINPWR;
795 795 GEN_DEBUG((CE_CONT,
796 796 "%s%d: DDI_SUSPEND: pm_power_has_changed comp 0"
797 797 " level %d", ddi_node_name(devi),
798 798 ddi_get_instance(devi), MINPWR));
799 799 if (pm_power_has_changed(dstatep->dip, 0, MINPWR)
800 800 != DDI_SUCCESS) {
801 801 cmn_err(CE_WARN, "%s%d: DDI_SUSPEND:\n\t"
802 802 "pm_power_has_changed failed for comp 0 to"
803 803 " level %d\n", ddi_node_name(devi),
804 804 ddi_get_instance(devi), MINPWR);
805 805 dstatep->level[0] = level_tmp;
806 806 mutex_exit(&dstatep->lock);
807 807
808 808 return (DDI_FAILURE);
809 809 }
810 810 }
811 811 mutex_exit(&dstatep->lock);
812 812
813 813 return (rv);
814 814
815 815 default:
816 816
817 817 return (DDI_FAILURE);
818 818 }
819 819 }
820 820
821 821 /* ARGSUSED */
822 822 static int
823 823 gen_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
824 824 {
825 825 dev_t dev;
826 826 int instance;
827 827
828 828 if (infocmd != DDI_INFO_DEVT2INSTANCE)
829 829 return (DDI_FAILURE);
830 830
831 831 dev = (dev_t)arg;
832 832 instance = MINOR_TO_INST(getminor(dev));
833 833 *result = (void *)(uintptr_t)instance;
834 834 return (DDI_SUCCESS);
835 835 }
836 836
837 837
838 838 /*ARGSUSED*/
839 839 static int
840 840 gen_open(dev_t *devp, int flag, int otyp, cred_t *cred)
841 841 {
842 842 minor_t minor;
843 843 struct dstate *dstatep;
844 844
845 845 if (otyp != OTYP_BLK && otyp != OTYP_CHR)
846 846 return (EINVAL);
847 847
848 848 minor = getminor(*devp);
849 849 if ((dstatep = ddi_get_soft_state(dstates,
850 850 MINOR_TO_INST(minor))) == NULL)
851 851 return (ENXIO);
852 852
853 853 mutex_enter(&dstatep->lock);
854 854 dstatep->flag |= OPEN_FLAG;
855 855 mutex_exit(&dstatep->lock);
856 856
857 857 GEN_DEBUG((CE_CONT,
858 858 "%s%d open",
859 859 dstatep->nodename, MINOR_TO_INST(minor)));
860 860
861 861 return (0);
862 862 }
863 863
864 864 /*ARGSUSED*/
865 865 static int
866 866 gen_close(dev_t dev, int flag, int otyp, cred_t *cred)
867 867 {
868 868 struct dstate *dstatep;
869 869 minor_t minor = getminor(dev);
870 870
871 871 if (otyp != OTYP_BLK && otyp != OTYP_CHR)
872 872 return (EINVAL);
873 873
874 874 dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
875 875
876 876 if (dstatep == NULL)
877 877 return (ENXIO);
878 878
879 879 mutex_enter(&dstatep->lock);
880 880 dstatep->flag &= ~OPEN_FLAG;
881 881 mutex_exit(&dstatep->lock);
882 882
883 883 GEN_DEBUG((CE_CONT,
884 884 "%s%d close",
885 885 dstatep->nodename, MINOR_TO_INST(minor)));
886 886
887 887 return (0);
888 888 }
889 889
890 890 /*ARGSUSED*/
891 891 static int
892 892 gen_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
893 893 {
894 894 struct dstate *dstatep;
895 895 ddi_eventcookie_t cookie;
896 896 int instance;
897 897 int rval = 0;
898 898 char *nodename;
899 899 int i;
900 900 struct devctl_iocdata *dcp;
901 901 uint_t state;
902 902 int ret;
903 903 int level_tmp;
904 904
905 905 instance = MINOR_TO_INST(getminor(dev));
906 906 dstatep = ddi_get_soft_state(dstates, instance);
907 907 nodename = dstatep->nodename;
908 908
909 909 if (dstatep == NULL)
910 910 return (ENXIO);
911 911
912 912 /*
913 913 * read devctl ioctl data
914 914 */
915 915 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
916 916 return (EFAULT);
917 917
918 918 switch (cmd) {
919 919 case GENDRV_IOFAULT_SIMULATE:
920 920 if (ddi_get_eventcookie(dstatep->dip, DDI_DEVI_FAULT_EVENT,
921 921 &(cookie)) != NDI_SUCCESS)
922 922 return (DDI_FAILURE);
923 923
924 924 return (ndi_post_event(dstatep->dip, dstatep->dip, cookie,
925 925 NULL));
926 926
927 927 case GENDRV_NDI_EVENT_TEST:
928 928 if (ddi_get_eventcookie(dstatep->dip, "pshot_dev_offline",
929 929 &cookie) == NDI_SUCCESS) {
930 930 (void) ndi_post_event(dstatep->dip, dstatep->dip,
931 931 cookie, NULL);
932 932 }
933 933
934 934 if (ddi_get_eventcookie(dstatep->dip, "pshot_dev_reset",
935 935 &cookie) == NDI_SUCCESS) {
936 936 (void) ndi_post_event(dstatep->dip, dstatep->dip,
937 937 cookie, NULL);
938 938 }
939 939
940 940 if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_reset",
941 941 &cookie) == NDI_SUCCESS) {
942 942 (void) ndi_post_event(dstatep->dip, dstatep->dip,
943 943 cookie, NULL);
944 944 }
945 945
946 946 if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_quiesce",
947 947 &cookie) == NDI_SUCCESS) {
948 948 (void) ndi_post_event(dstatep->dip, dstatep->dip,
949 949 cookie, NULL);
950 950 }
951 951
952 952 if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_unquiesce",
953 953 &cookie) == NDI_SUCCESS) {
954 954 (void) ndi_post_event(dstatep->dip, dstatep->dip,
955 955 cookie, NULL);
956 956 }
957 957
958 958 if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_test_post",
959 959 &cookie) == NDI_SUCCESS) {
960 960 (void) ndi_post_event(dstatep->dip, dstatep->dip,
961 961 cookie, NULL);
962 962 }
963 963
964 964 break;
965 965
966 966 case DEVCTL_PM_PWR_HAS_CHANGED_ON_RESUME:
967 967 /*
968 968 * Issue pm_power_has_changed() call on DDI_RESUME
969 969 */
970 970 mutex_enter(&dstatep->lock);
971 971 dstatep->flag |= PWR_HAS_CHANGED_ON_RESUME_FLAG;
972 972 mutex_exit(&dstatep->lock);
973 973 GEN_DEBUG((CE_CONT, "%s%d:"
974 974 " DEVCTL_PM_PWR_HAS_CHANGED_ON_RESUME", nodename,
975 975 instance));
976 976
977 977 break;
978 978
979 979 case DEVCTL_PM_FAIL_SUSPEND:
980 980 /*
981 981 * Fail the suspend attempt in DDI_SUSPEND
982 982 */
983 983 mutex_enter(&dstatep->lock);
984 984 dstatep->flag |= FAIL_SUSPEND_FLAG;
985 985 mutex_exit(&dstatep->lock);
986 986 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_FAIL_SUSPEND",
987 987 nodename, instance));
988 988
989 989 break;
990 990
991 991 case DEVCTL_PM_PUP_WITH_PWR_HAS_CHANGED:
992 992 /*
993 993 * Use pm_power_has_changed() to power up comp 0 when
994 994 * enforcing the comp 0 vs comp-not 0 dependency:
995 995 * Power up comp 0 first, if request for comp-not-0
996 996 * comes in.
997 997 * Else, default to pm_raise_power().
998 998 */
999 999 mutex_enter(&dstatep->lock);
1000 1000 dstatep->flag |= PUP_WITH_PWR_HAS_CHANGED_FLAG;
1001 1001 mutex_exit(&dstatep->lock);
1002 1002 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_PUP_WITH_PWR_HAS_CHANGED",
1003 1003 nodename, instance));
1004 1004
1005 1005 break;
1006 1006
1007 1007 case DEVCTL_PM_BUSY_COMP:
1008 1008 /*
1009 1009 * mark component 0 busy via a pm_busy_component() call.
1010 1010 * update the busy[] array.
1011 1011 */
1012 1012 mutex_enter(&dstatep->lock);
1013 1013 ++dstatep->busy[0];
1014 1014 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_BUSY_COMP: comp 0:"
1015 1015 " busy=%d", nodename, instance, dstatep->busy[0]));
1016 1016 mutex_exit(&dstatep->lock);
1017 1017 ret = pm_busy_component(dstatep->dip, 0);
1018 1018 ASSERT(ret == DDI_SUCCESS);
1019 1019
1020 1020 break;
1021 1021
1022 1022 case DEVCTL_PM_BUSY_COMP_TEST:
1023 1023 /*
1024 1024 * test busy state on component 0
1025 1025 */
1026 1026 mutex_enter(&dstatep->lock);
1027 1027 state = dstatep->busy[0];
1028 1028 if (copyout(&state, dcp->cpyout_buf,
1029 1029 sizeof (uint_t)) != 0) {
1030 1030 cmn_err(CE_WARN, "%s%d:"
1031 1031 " DEVCTL_PM_BUSY_COMP_TEST: copyout failed\n",
1032 1032 nodename, instance);
1033 1033 rval = EINVAL;
1034 1034 }
1035 1035 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_BUSY_COMP_TEST:"
1036 1036 " comp 0 busy %d",
1037 1037 nodename, instance, state));
1038 1038 mutex_exit(&dstatep->lock);
1039 1039
1040 1040 break;
1041 1041
1042 1042 case DEVCTL_PM_IDLE_COMP:
1043 1043 /*
1044 1044 * mark component 0 idle via a pm_idle_component() call.
1045 1045 * NOP if dstatep->busy[0] == 0.
1046 1046 */
1047 1047 mutex_enter(&dstatep->lock);
1048 1048 if (dstatep->busy[0] > 0) {
1049 1049 --dstatep->busy[0];
1050 1050 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_IDLE_COMP:"
1051 1051 " comp 0: busy=%d", nodename, instance,
1052 1052 dstatep->busy[0]));
1053 1053 mutex_exit(&dstatep->lock);
1054 1054 ret = pm_idle_component(dstatep->dip, 0);
1055 1055 ASSERT(ret == DDI_SUCCESS);
1056 1056 } else {
1057 1057 mutex_exit(&dstatep->lock);
1058 1058 }
1059 1059
1060 1060 break;
1061 1061
1062 1062 case DEVCTL_PM_PROM_PRINTF:
1063 1063 (void) prom_printf("%s%d: PROM_PRINTF FROM GEN_DRV\n",
1064 1064 nodename, instance);
1065 1065
1066 1066 break;
1067 1067
1068 1068 case DEVCTL_PM_RAISE_PWR:
1069 1069 /*
1070 1070 * power up both components to MAXPWR via
1071 1071 * pm_raise_power() calls. this ioctl() cmd
1072 1072 * assumes that the current level is 0
1073 1073 */
1074 1074 for (i = 0; i < COMPONENTS; i++) {
1075 1075 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_RAISE_PWR:"
1076 1076 " comp %d old 0 new %d",
1077 1077 nodename, instance, i, maxpwr[i]));
1078 1078 if (pm_raise_power(dstatep->dip, 0, maxpwr[i])
1079 1079 != DDI_SUCCESS) {
1080 1080 rval = EINVAL;
1081 1081 }
1082 1082 }
1083 1083
1084 1084 break;
1085 1085
1086 1086 case DEVCTL_PM_CHANGE_PWR_LOW:
1087 1087 /*
1088 1088 * power off both components via pm_power_has_changed() calls
1089 1089 */
1090 1090 for (i = (COMPONENTS - 1); i >= 0; --i) {
1091 1091 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_CHANGE_PWR_LOW:"
1092 1092 " comp %d new 0",
1093 1093 nodename, instance, i));
1094 1094 mutex_enter(&dstatep->lock);
1095 1095 level_tmp = dstatep->level[i];
1096 1096 dstatep->level[i] = 0;
1097 1097 if (pm_power_has_changed(dstatep->dip, i, 0)
1098 1098 != DDI_SUCCESS) {
1099 1099 dstatep->level[i] = level_tmp;
1100 1100 rval = EINVAL;
1101 1101 }
1102 1102 mutex_exit(&dstatep->lock);
1103 1103 }
1104 1104
1105 1105 break;
1106 1106
1107 1107 case DEVCTL_PM_CHANGE_PWR_HIGH:
1108 1108 /*
1109 1109 * power up both components to MAXPWR via
1110 1110 * pm_power_has_changed() calls
1111 1111 */
1112 1112 for (i = 0; i < COMPONENTS; i++) {
1113 1113 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_CHANGE_PWR_HIGH:"
1114 1114 " comp %d new %d",
1115 1115 nodename, instance, i, maxpwr[i]));
1116 1116 mutex_enter(&dstatep->lock);
1117 1117 level_tmp = dstatep->level[i];
1118 1118 dstatep->level[i] = maxpwr[i];
1119 1119 if (pm_power_has_changed(dstatep->dip, i, maxpwr[i])
1120 1120 != DDI_SUCCESS) {
1121 1121 dstatep->level[i] = level_tmp;
1122 1122 rval = EINVAL;
1123 1123 }
1124 1124 mutex_exit(&dstatep->lock);
1125 1125 }
1126 1126
1127 1127 break;
1128 1128
1129 1129 case DEVCTL_PM_POWER:
1130 1130 /*
1131 1131 * test if the gen_drv_power() routine has been called,
1132 1132 * then clear
1133 1133 */
1134 1134 mutex_enter(&dstatep->lock);
1135 1135 state = (dstatep->flag & POWER_FLAG) ? 1 : 0;
1136 1136 if (copyout(&state, dcp->cpyout_buf,
1137 1137 sizeof (uint_t)) != 0) {
1138 1138 cmn_err(CE_WARN, "%s%d: DEVCTL_PM_POWER:"
1139 1139 " copyout failed\n", nodename, instance);
1140 1140 rval = EINVAL;
1141 1141 }
1142 1142 GEN_DEBUG((CE_CONT, "%s%d: %s POWER_FLAG: %d",
1143 1143 nodename, instance, "DEVCTL_PM_POWER", state));
1144 1144 dstatep->flag &= ~POWER_FLAG;
1145 1145 mutex_exit(&dstatep->lock);
1146 1146 break;
1147 1147
1148 1148 case DEVCTL_PM_NO_LOWER_POWER:
1149 1149 /*
1150 1150 * issue to not invoke pm_lower_power() on detach
1151 1151 */
1152 1152 mutex_enter(&dstatep->lock);
1153 1153 dstatep->flag &= ~LOWER_POWER_FLAG;
1154 1154 mutex_exit(&dstatep->lock);
1155 1155 GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_NO_LOWER_POWER",
1156 1156 nodename, instance));
1157 1157 break;
1158 1158
1159 1159 default:
1160 1160 return (ENOTTY);
1161 1161 }
1162 1162
1163 1163 return (rval);
1164 1164 }
1165 1165
1166 1166 /*ARGSUSED*/
1167 1167 static int
1168 1168 gen_read(dev_t dev, struct uio *uiop, cred_t *credp)
1169 1169 {
1170 1170 return (0);
1171 1171 }
1172 1172
1173 1173 /*ARGSUSED*/
1174 1174 static int
1175 1175 gen_write(dev_t dev, struct uio *uiop, cred_t *credp)
1176 1176 {
1177 1177 return (0);
1178 1178 }
1179 1179
1180 1180 /*ARGSUSED0*/
1181 1181 static int
1182 1182 gen_power(dev_info_t *dip, int cmpt, int level)
1183 1183 {
1184 1184 struct dstate *dstatep;
1185 1185 int instance = ddi_get_instance(dip);
1186 1186 char *nodename = ddi_node_name(dip);
1187 1187 int level_tmp;
1188 1188
1189 1189 GEN_DEBUG((CE_CONT, "%s%d: power: cmpt %d to level %d",
1190 1190 nodename, instance, cmpt, level));
1191 1191
1192 1192 dstatep = ddi_get_soft_state(dstates, instance);
1193 1193 if (dstatep == NULL) {
1194 1194
1195 1195 return (DDI_FAILURE);
1196 1196 }
1197 1197
1198 1198 /*
1199 1199 * Keep track of the power levels for both components
1200 1200 * in the dstatep->comp[] array.
1201 1201 * Set comp 0 to full level if non-zero comps
1202 1202 * are being set to a higher, non-zero level.
1203 1203 */
1204 1204 if (cmpt == 0) {
1205 1205 mutex_enter(&dstatep->lock);
1206 1206 dstatep->level[cmpt] = level;
1207 1207 mutex_exit(&dstatep->lock);
1208 1208 } else if (level > dstatep->level[cmpt] && level != 0 &&
1209 1209 dstatep->level[0] != COMP_0_MAXPWR) {
1210 1210 /*
1211 1211 * If component 0 is not at COMP_0_MAXPWR, and component 1
1212 1212 * is being powered ON, invoke pm_raise_power() or
1213 1213 * pm_power_has_changed() based on the
1214 1214 * PUP_WITH_PWR_HAS_CHANGED_FLAG flag.
1215 1215 * PUP_WITH_PWR_HAS_CHANGED_FLAG = FALSE by default, invoking
1216 1216 * pm_raise_power().
1217 1217 */
1218 1218 if (!(dstatep->flag & PUP_WITH_PWR_HAS_CHANGED_FLAG)) {
1219 1219 /*
1220 1220 * first set comp 0 to level COMP_0_MAXPWR
1221 1221 */
1222 1222 GEN_DEBUG((CE_CONT, "%s%d: power: "
1223 1223 "pm_raise_power: comp 0 to level %d",
1224 1224 nodename, instance, COMP_0_MAXPWR));
1225 1225 if (pm_raise_power(dip, 0, COMP_0_MAXPWR) !=
1226 1226 DDI_SUCCESS) {
1227 1227 cmn_err(CE_WARN,
1228 1228 "%s%d: power: pm_raise_power() "
1229 1229 "failed: comp 0 to level %d\n",
1230 1230 nodename, instance, COMP_0_MAXPWR);
1231 1231
1232 1232 return (DDI_FAILURE);
1233 1233
1234 1234 } else {
1235 1235 mutex_enter(&dstatep->lock);
1236 1236 dstatep->level[0] = COMP_0_MAXPWR;
1237 1237 /*
1238 1238 * now set the level on the non-zero comp
1239 1239 */
1240 1240 dstatep->level[cmpt] = level;
1241 1241 mutex_exit(&dstatep->lock);
1242 1242 GEN_DEBUG((CE_CONT, "%s%d: power: "
1243 1243 "comp %d to level %d",
1244 1244 nodename, instance, cmpt, level));
1245 1245 }
1246 1246 } else {
1247 1247 GEN_DEBUG((CE_CONT, "%s%d: power: "
1248 1248 "pm_power_has_changed: comp 0 to level %d",
1249 1249 nodename, instance, COMP_0_MAXPWR));
1250 1250 mutex_enter(&dstatep->lock);
1251 1251 level_tmp = dstatep->level[0];
1252 1252 dstatep->level[0] = COMP_0_MAXPWR;
1253 1253 if (pm_power_has_changed(dip, 0, COMP_0_MAXPWR) !=
1254 1254 DDI_SUCCESS) {
1255 1255 cmn_err(CE_WARN,
1256 1256 "%s%d: power: pm_power_has_changed() "
1257 1257 "failed: comp 0 to level %d\n",
1258 1258 nodename, instance, COMP_0_MAXPWR);
1259 1259 dstatep->level[0] = level_tmp;
1260 1260 } else {
1261 1261 /*
1262 1262 * now set the level on the non-zero comp
1263 1263 */
1264 1264 GEN_DEBUG((CE_CONT, "%s%d: power:"
1265 1265 " pm_power_has_changed: comp %d"
1266 1266 " to level %d", nodename, instance,
1267 1267 cmpt, level));
1268 1268 dstatep->level[cmpt] = level;
1269 1269 }
1270 1270 mutex_exit(&dstatep->lock);
1271 1271 }
1272 1272 } else {
1273 1273 mutex_enter(&dstatep->lock);
1274 1274 dstatep->level[cmpt] = level;
1275 1275 mutex_exit(&dstatep->lock);
1276 1276 }
1277 1277
1278 1278 return (DDI_SUCCESS);
1279 1279 }
1280 1280
1281 1281
1282 1282 /*
1283 1283 * Create properties of various data types for testing devfs events.
1284 1284 */
1285 1285 static int
1286 1286 gen_create_properties(dev_info_t *devi)
1287 1287 {
1288 1288 int int_val = 3023;
1289 1289 int int_array[] = { 3, 10, 304, 230, 4};
1290 1290 int64_t int64_val = 20;
1291 1291 int64_t int64_array[] = { 12, 24, 36, 48};
1292 1292 char *string_val = "Dev_node_prop";
1293 1293 char *string_array[] = {"Dev_node_prop:0",
1294 1294 "Dev_node_prop:1", "Dev_node_prop:2", "Dev_node_prop:3"};
1295 1295 uchar_t byte_array[] = { (uchar_t)0xaa, (uchar_t)0x55,
1296 1296 (uchar_t)0x12, (uchar_t)0xcd };
1297 1297 char bytes[] = { (char)0x00, (char)0xef, (char)0xff };
1298 1298
1299 1299 if (ddi_prop_update_int(DDI_DEV_T_NONE, devi, "int", int_val)
1300 1300 != DDI_PROP_SUCCESS)
1301 1301 return (DDI_FAILURE);
1302 1302
1303 1303 if (ddi_prop_update_int_array(DDI_DEV_T_NONE, devi, "int-array",
1304 1304 int_array, sizeof (int_array) / sizeof (int)) != DDI_PROP_SUCCESS)
1305 1305 return (DDI_FAILURE);
1306 1306
1307 1307 if (ddi_prop_update_int64(DDI_DEV_T_NONE, devi, "int64", int64_val)
1308 1308 != DDI_PROP_SUCCESS)
1309 1309 return (DDI_FAILURE);
1310 1310
1311 1311 if (ddi_prop_update_int64_array(DDI_DEV_T_NONE, devi, "int64-array",
1312 1312 int64_array, sizeof (int64_array) / sizeof (int64_t))
1313 1313 != DDI_PROP_SUCCESS)
1314 1314 return (DDI_FAILURE);
1315 1315
1316 1316 if (ddi_prop_update_string(DDI_DEV_T_NONE, devi, "string", string_val)
1317 1317 != DDI_PROP_SUCCESS)
1318 1318 return (DDI_FAILURE);
1319 1319
1320 1320 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, "string-array",
1321 1321 string_array, sizeof (string_array) / sizeof (char *))
1322 1322 != DDI_PROP_SUCCESS)
1323 1323 return (DDI_FAILURE);
1324 1324
1325 1325 if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
1326 1326 "boolean", NULL, 0) != DDI_PROP_SUCCESS)
1327 1327 return (DDI_FAILURE);
1328 1328
1329 1329 if (ddi_prop_update_byte_array(DDI_DEV_T_NONE, devi, "byte-array",
1330 1330 byte_array, sizeof (byte_array)) != DDI_PROP_SUCCESS)
1331 1331 return (DDI_FAILURE);
1332 1332
1333 1333 /* untyped property */
1334 1334 if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, "untyped",
1335 1335 (caddr_t)bytes, sizeof (bytes)) != DDI_PROP_SUCCESS)
1336 1336 return (DDI_FAILURE);
1337 1337
1338 1338 return (DDI_SUCCESS);
1339 1339 }
1340 1340
1341 1341 static struct driver_minor_data {
1342 1342 char *name;
1343 1343 minor_t minor;
1344 1344 int type;
1345 1345 } disk_minor_data[] = {
1346 1346 {"a", 0, S_IFBLK},
1347 1347 {"b", 1, S_IFBLK},
1348 1348 {"c", 2, S_IFBLK},
1349 1349 {"d", 3, S_IFBLK},
1350 1350 {"e", 4, S_IFBLK},
1351 1351 {"f", 5, S_IFBLK},
1352 1352 {"g", 6, S_IFBLK},
1353 1353 {"h", 7, S_IFBLK},
1354 1354 {"a,raw", 0, S_IFCHR},
1355 1355 {"b,raw", 1, S_IFCHR},
1356 1356 {"c,raw", 2, S_IFCHR},
1357 1357 {"d,raw", 3, S_IFCHR},
1358 1358 {"e,raw", 4, S_IFCHR},
1359 1359 {"f,raw", 5, S_IFCHR},
1360 1360 {"g,raw", 6, S_IFCHR},
1361 1361 {"h,raw", 7, S_IFCHR},
1362 1362 {0}
1363 1363 };
1364 1364
1365 1365
1366 1366 static struct driver_serial_minor_data {
1367 1367 char *name;
1368 1368 minor_t minor;
1369 1369 int type;
1370 1370 char *node_type;
1371 1371 } serial_minor_data[] = {
1372 1372 {"0", 0, S_IFCHR, "ddi_serial"},
1373 1373 {"1", 1, S_IFCHR, "ddi_serial"},
1374 1374 {"0,cu", 2, S_IFCHR, "ddi_serial:dialout"},
1375 1375 {"1,cu", 3, S_IFCHR, "ddi_serial:dialout"},
1376 1376 {0}
1377 1377 };
1378 1378
1379 1379
1380 1380 static int
1381 1381 gen_create_display(dev_info_t *devi)
1382 1382 {
1383 1383
1384 1384 int instance = ddi_get_instance(devi);
1385 1385 char minor_name[15];
1386 1386
1387 1387 (void) sprintf(minor_name, "cgtwenty%d", instance);
1388 1388
1389 1389 return (ddi_create_minor_node(devi, minor_name, S_IFCHR,
1390 1390 INST_TO_MINOR(instance), DDI_NT_DISPLAY, NULL));
1391 1391 }
1392 1392
1393 1393 static int
1394 1394 gen_create_mn_disk_chan(dev_info_t *devi)
1395 1395 {
1396 1396 struct driver_minor_data *dmdp;
1397 1397 int instance = ddi_get_instance(devi);
1398 1398
1399 1399 if (gen_create_properties(devi) != DDI_SUCCESS)
1400 1400 return (DDI_FAILURE);
1401 1401
1402 1402 for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
1403 1403 if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
1404 1404 (INST_TO_MINOR(instance)) | dmdp->minor,
1405 1405 DDI_NT_BLOCK_CHAN, NULL) != DDI_SUCCESS) {
1406 1406
1407 1407 return (DDI_FAILURE);
1408 1408 }
1409 1409 }
1410 1410 return (DDI_SUCCESS);
1411 1411 }
1412 1412
1413 1413 static uint_t
1414 1414 atod(char *s)
1415 1415 {
1416 1416 uint_t val = 0;
1417 1417 uint_t digit;
1418 1418
1419 1419 while (*s) {
1420 1420 if (*s >= '0' && *s <= '9')
1421 1421 digit = *s++ - '0';
1422 1422 else
1423 1423 break;
1424 1424 val = (val * 10) + digit;
1425 1425 }
1426 1426 return (val);
1427 1427 }
1428 1428
1429 1429
1430 1430 static int
1431 1431 gen_create_mn_disk_wwn(dev_info_t *devi)
1432 1432 {
1433 1433 struct driver_minor_data *dmdp;
1434 1434 int instance = ddi_get_instance(devi);
1435 1435 char *address = ddi_get_name_addr(devi);
1436 1436 int target, lun;
1437 1437
1438 1438 if (address[0] >= '0' && address[0] <= '9' &&
1439 1439 strchr(address, ',')) {
1440 1440 target = atod(address);
1441 1441 address = strchr(address, ',');
1442 1442 lun = atod(++address);
1443 1443 } else { /* this hack is for rm_stale_link() testing */
1444 1444 target = 10;
1445 1445 lun = 5;
1446 1446 }
1447 1447
1448 1448 if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
1449 1449 "target", (caddr_t)&target, sizeof (int))
1450 1450 != DDI_PROP_SUCCESS) {
1451 1451 return (DDI_FAILURE);
1452 1452 }
1453 1453 if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
1454 1454 "lun", (caddr_t)&lun, sizeof (int))
1455 1455 != DDI_PROP_SUCCESS) {
1456 1456 return (DDI_FAILURE);
1457 1457 }
1458 1458
1459 1459 for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
1460 1460 if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
1461 1461 (INST_TO_MINOR(instance)) | dmdp->minor,
1462 1462 DDI_NT_BLOCK_WWN, NULL) != DDI_SUCCESS) {
1463 1463
1464 1464 return (DDI_FAILURE);
1465 1465 }
1466 1466 }
1467 1467 return (DDI_SUCCESS);
1468 1468 }
1469 1469
1470 1470 static int
1471 1471 gen_create_mn_disk_cdrom(dev_info_t *devi)
1472 1472 {
1473 1473 struct driver_minor_data *dmdp;
1474 1474 int instance = ddi_get_instance(devi);
1475 1475
1476 1476 for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
1477 1477 if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
1478 1478 (INST_TO_MINOR(instance)) | dmdp->minor,
1479 1479 DDI_NT_CD_CHAN, NULL) != DDI_SUCCESS) {
1480 1480
1481 1481 return (DDI_FAILURE);
1482 1482 }
1483 1483 }
1484 1484 return (DDI_SUCCESS);
1485 1485 }
1486 1486
1487 1487 static int
1488 1488 gen_create_mn_disk_fd(dev_info_t *devi)
1489 1489 {
1490 1490 struct driver_minor_data *dmdp;
1491 1491 int instance = ddi_get_instance(devi);
1492 1492
1493 1493 for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
1494 1494 if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
1495 1495 (INST_TO_MINOR(instance)) | dmdp->minor,
1496 1496 DDI_NT_BLOCK_CHAN, NULL) != DDI_SUCCESS) {
1497 1497
1498 1498 return (DDI_FAILURE);
1499 1499 }
1500 1500 }
1501 1501 return (DDI_SUCCESS);
1502 1502 }
1503 1503
1504 1504 static int
1505 1505 gen_create_serial(dev_info_t *devi)
1506 1506 {
1507 1507 struct driver_serial_minor_data *dmdp;
1508 1508 int instance = ddi_get_instance(devi);
1509 1509
1510 1510 for (dmdp = serial_minor_data; dmdp->name != NULL; dmdp++) {
1511 1511 if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
1512 1512 (INST_TO_MINOR(instance)) | dmdp->minor,
1513 1513 dmdp->node_type, NULL) != DDI_SUCCESS) {
1514 1514
1515 1515 return (DDI_FAILURE);
1516 1516 }
1517 1517 }
1518 1518 return (DDI_SUCCESS);
1519 1519 }
1520 1520
1521 1521 static int
1522 1522 gen_create_net(dev_info_t *devi)
1523 1523 {
1524 1524 int instance = ddi_get_instance(devi);
1525 1525 char minorname[32];
1526 1526
1527 1527 if (gen_create_properties(devi) != DDI_SUCCESS)
1528 1528 return (DDI_FAILURE);
1529 1529
1530 1530 (void) snprintf(minorname, sizeof (minorname), "gen_drv%d", instance);
1531 1531 return (ddi_create_minor_node(devi, minorname, S_IFCHR,
1532 1532 INST_TO_MINOR(instance), DDI_NT_NET, 0));
1533 1533 }
1534 1534
1535 1535 static int
1536 1536 gen_create_minor_nodes(dev_info_t *devi, struct dstate *dstatep)
1537 1537 {
1538 1538 int rval = DDI_SUCCESS;
1539 1539 char *node_name;
1540 1540
1541 1541 node_name = ddi_node_name(devi);
1542 1542
1543 1543 if (strcmp(node_name, "disk_chan") == 0) {
1544 1544 rval = gen_create_mn_disk_chan(devi);
1545 1545 } else if (strcmp(node_name, "disk_wwn") == 0) {
1546 1546 rval = gen_create_mn_disk_wwn(devi);
1547 1547 } else if (strcmp(node_name, "disk_cdrom") == 0) {
1548 1548 rval = gen_create_mn_disk_cdrom(devi);
1549 1549 } else if (strcmp(node_name, "disk_fd") == 0) {
1550 1550 rval = gen_create_mn_disk_fd(devi);
1551 1551 } else if (strcmp(node_name, "cgtwenty") == 0) {
1552 1552 rval = gen_create_display(devi);
1553 1553 } else if (strcmp(node_name, "genzs") == 0) {
1554 1554 rval = gen_create_serial(devi);
1555 1555 } else if (strcmp(node_name, "net") == 0) {
1556 1556 rval = gen_create_net(devi);
1557 1557 } else {
1558 1558 int instance = ddi_get_instance(devi);
1559 1559 char *node_type;
1560 1560
1561 1561 /*
1562 1562 * Solaris may directly hang the node_type off the minor node
1563 1563 * (without making a copy). Since we free the node_type
1564 1564 * property below we need to make a private copy to pass
1565 1565 * to ddi_create_minor_node to avoid devinfo snapshot panics.
1566 1566 * We store a pointer to our copy in dstate and free it in
1567 1567 * gen_detach after the minor nodes have been deleted by
1568 1568 * ddi_remove_minor_node.
1569 1569 */
1570 1570 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1571 1571 DDI_PROP_DONTPASS, "node-type", &node_type) != 0) {
1572 1572 cmn_err(CE_WARN, "couldn't get node-type\n");
1573 1573 return (DDI_FAILURE);
1574 1574 }
1575 1575 if (node_type) {
1576 1576 dstatep->node_type = kmem_alloc(
1577 1577 strlen(node_type) + 1, KM_SLEEP);
1578 1578 (void) strcpy(dstatep->node_type, node_type);
1579 1579 }
1580 1580 ddi_prop_free(node_type);
1581 1581
1582 1582 /* the minor name is the same as the node name */
1583 1583 if (ddi_create_minor_node(devi, node_name, S_IFCHR,
1584 1584 (INST_TO_MINOR(instance)), dstatep->node_type, NULL) !=
1585 1585 DDI_SUCCESS) {
1586 1586 if (dstatep->node_type) {
1587 1587 kmem_free(dstatep->node_type,
1588 1588 strlen(dstatep->node_type) + 1);
1589 1589 dstatep->node_type = NULL;
1590 1590 }
1591 1591 return (DDI_FAILURE);
1592 1592 }
1593 1593 return (DDI_SUCCESS);
1594 1594 }
1595 1595
1596 1596 if (rval != DDI_SUCCESS) {
1597 1597 ddi_prop_remove_all(devi);
1598 1598 ddi_remove_minor_node(devi, NULL);
1599 1599 }
1600 1600
1601 1601 return (rval);
1602 1602 }
1603 1603
1604 1604 /*ARGSUSED*/
1605 1605 static void
1606 1606 gen_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie, void *arg,
1607 1607 void *impl_data)
1608 1608 {
1609 1609 if (gen_debug)
1610 1610 cmn_err(CE_NOTE, "gen_event_cb invoked");
1611 1611
1612 1612 }
↓ open down ↓ |
1436 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX