xref: /titanic_52/usr/src/cmd/devctl/devctl.c (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 /*
31  * devctl - device control utility
32  *
33  * to compile:
34  * cc -o devctl -ldevice -ldevinfo devctl.c
35  *
36  * usage: devctl [-v] command [device/bus pathname]
37  *
38  * Commands:
39  * 	list		- list all controllers exporting the devctl interface
40  *	online		- online a device
41  *	offline		- offline a device
42  *	remove  	- remove a device from the device tree
43  * 	quiesce		- quiesce the bus
44  *	unquiesce	- resume bus activity
45  *	configure	- configure a bus's child devices
46  *	unconfigure	- unconfigure a bus's child devices
47  *	bus-reset	- reset a bus
48  *	dev-reset	- reset a device
49  * 	bus-getstate	- return the current state of the bus
50  *	dev-getstate	- return the current state of the device
51  *	bus-devcreate	- create a new device, bus specific
52  *	dev-raisepower		- power up a device via pm_raise_power() (pm)
53  *	dev-idlecomp		- idle a device's component 0 (pm)
54  *	dev-busycomp		- busy a device's component 0 (pm)
55  *	dev-testbusy		- test a device's component 0's busy state (pm)
56  *	dev-changepowerhigh	- power up a device via pm_power_has_changed()
57  *				  (pm)
58  *	dev-changepowerlow	- power off a device via pm_power_has_changed()
59  *				  (pm)
60  *	dev-failsuspend		- fail DDI_SUSPEND (pm)
61  *	dev-changeonresume	- issue pm_power_has_changed() vs,
62  *				  pm_raise_power() on device resume (pm)
63  *	dev-nolowerpower	- don't call pm_lower_power() on detach (pm)
64  *	dev-promprintf		- issue a prom_printf() call (pm)
65  *	bus-raisepower		- power up a bus via pm_raise_power() (pm)
66  *	bus-idlecomp		- idle a bus' component (pm)
67  *	bus-busycomp		- busy a bus' component (pm)
68  *	bus-testbusy		- test a bus' component busy state (pm)
69  *	bus-changepowerhigh	- power up a bus via pm_power_has_changed() (pm)
70  *	bus-changepowerlow	- power off a bus via pm_power_has_changed()
71  *				  (pm)
72  *	bus-failsuspend		- fail DDI_SUSPEND (pm)
73  *	bus-teststrict		- test is bus driver is  strict or involved (pm)
74  *	bus-noinvol		- mark idle twice when child detaches
75  *
76  *
77  * Returns:
78  *	- Success
79  *	- Operation not supported by device
80  *	- No Permission
81  *	- No Such Device
82  *
83  * Examples:
84  *	devctl list - list all controllers exporting a :devctl node
85  *	devctl offline /dev/dsk/c0t3d0s0  - offline disk
86  *	devctl dev-getstate  /devices/sbus@1f,0/espdma@e,8400000/esp@e,8800000\
87  * sd@3,0
88  *
89  */
90 
91 #include <stdio.h>
92 #include <string.h>
93 #include <unistd.h>
94 #include <stdlib.h>
95 #include <sys/types.h>
96 #include <sys/errno.h>
97 #include <sys/stat.h>
98 #include <sys/param.h>
99 #include <libdevice.h>
100 #include <libdevinfo.h>
101 #include <sys/sunddi.h>
102 
103 typedef struct cmds {
104 	char *cmdname;
105 	int (*cmdf)(devctl_hdl_t);
106 } cmds_t;
107 
108 extern int errno;
109 
110 static void setprogname(char *name);
111 static void print_bus_state(char *devname, uint_t state);
112 static void print_dev_state(char *devname, uint_t state);
113 static int dev_getstate(devctl_hdl_t);
114 static int bus_getstate(devctl_hdl_t);
115 static int bus_devcreate(devctl_hdl_t);
116 static void run_list_ctlrs(void);
117 static struct cmds *dc_cmd();
118 static int nexif(di_node_t din, di_minor_t dim, void *arg);
119 static void *s_malloc(size_t);
120 static void *s_realloc(void *, size_t);
121 static char *s_strdup(char *);
122 static int dev_pm_testbusy(devctl_hdl_t);
123 static int bus_pm_teststrict(devctl_hdl_t);
124 
125 static char *devctl_device;
126 static char *orig_path;
127 static char *devctl_cmdname;
128 static char *progname;
129 static int  verbose;
130 static int  debug;
131 static char *dev_name;
132 static char **dev_props;
133 
134 static const char *usage = "%s [-v] list | online | offline | remove |\n"
135 	"\tquiesce | unquiesce | configure | unconfigure |\n"
136 	"\t{bus,dev}-reset {bus,dev}-getstate | {bus,dev}-raisepower |\n"
137 	"\t{bus,dev}-idlecomp | {bus,dev}-busycomp |\n"
138 	"\t{bus,dev}-changepowerhigh | {bus,dev}-changepowerlow |\n"
139 	"\t{bus,dev}-testbusy | {bus,dev}-failsuspend | dev-changeonresume |\n"
140 	"\tdev-promprintf | dev-nolowerpower | bus-teststrict |\n"
141 	"\tbus-noinvol [/dev/... | /devices/...]\n";
142 
143 static struct cmds device_cmds[] = {
144 	{"online", devctl_device_online},
145 	{"offline", devctl_device_offline},
146 	{"remove", devctl_device_remove},
147 	{"dev-reset", devctl_device_reset},
148 	{"dev-getstate", dev_getstate},
149 	{"dev-raisepower", devctl_pm_raisepower},
150 	{"dev-busycomp", devctl_pm_busycomponent},
151 	{"dev-idlecomp", devctl_pm_idlecomponent},
152 	{"dev-testbusy", dev_pm_testbusy},
153 	{"dev-changepowerlow", devctl_pm_changepowerlow},
154 	{"dev-changepowerhigh", devctl_pm_changepowerhigh},
155 	{"dev-failsuspend", devctl_pm_failsuspend},
156 	{"dev-changeonresume", devctl_pm_device_changeonresume},
157 	{"dev-promprintf", devctl_pm_device_promprintf},
158 	{"dev-nolowerpower", devctl_pm_device_no_lower_power},
159 	{NULL, NULL},
160 };
161 
162 static struct cmds bus_cmds[] = {
163 	{"quiesce", devctl_bus_quiesce},
164 	{"unquiesce", devctl_bus_unquiesce},
165 	{"bus-reset", devctl_bus_reset},
166 	{"configure", devctl_bus_configure},
167 	{"unconfigure", devctl_bus_unconfigure},
168 	{"bus-getstate", bus_getstate},
169 	{"bus-devcreate", bus_devcreate},
170 	{"bus-raisepower", devctl_pm_raisepower},
171 	{"bus-busycomp", devctl_pm_busycomponent},
172 	{"bus-idlecomp", devctl_pm_idlecomponent},
173 	{"bus-changepowerlow", devctl_pm_changepowerlow},
174 	{"bus-changepowerhigh", devctl_pm_changepowerhigh},
175 	{"bus-testbusy", dev_pm_testbusy},
176 	{"bus-failsuspend", devctl_pm_failsuspend},
177 	{"bus-teststrict", bus_pm_teststrict},
178 	{"bus-noinvol", devctl_pm_bus_no_invol},
179 	{NULL, NULL},
180 };
181 
182 
183 
184 int
185 main(int argc, char *argv[])
186 {
187 	int	c;
188 	int 	rv;
189 	int	pathlen;
190 	struct cmds *dcmd;
191 	devctl_hdl_t dcp;
192 	struct stat stat_buf;
193 
194 	setprogname(argv[0]);
195 	while ((c = getopt(argc, argv, "vd")) != -1)  {
196 		switch (c)  {
197 		case 'v':
198 			++verbose;
199 			break;
200 		case 'd':
201 			++debug;
202 			(void) putenv("LIBDEVICE_DEBUG");
203 			break;
204 		default:
205 			(void) fprintf(stderr, usage, progname);
206 			exit(1);
207 			/*NOTREACHED*/
208 		}
209 	}
210 
211 	if (optind == argc) {
212 		(void) fprintf(stderr, usage, progname);
213 		exit(-1);
214 	}
215 
216 	devctl_cmdname = argv[optind++];
217 
218 	if (strcmp(devctl_cmdname, "list") == 0) {
219 		run_list_ctlrs();
220 		exit(0);
221 	}
222 
223 	/*
224 	 * any command other than "list" requires a device path
225 	 */
226 	if (((optind + 1) > argc)) {
227 		(void) fprintf(stderr, usage, progname);
228 		exit(-1);
229 	}
230 
231 	orig_path = s_strdup(argv[optind]);
232 	devctl_device = s_malloc(MAXPATHLEN);
233 	(void) strcpy(devctl_device, orig_path);
234 
235 	/*
236 	 * Additional properties follow for bus-devcreate
237 	 */
238 	if ((optind + 1 < argc) &&
239 	    strcmp(devctl_cmdname, "bus-devcreate") == 0) {
240 		int i;
241 		optind++;
242 		dev_name = s_strdup(argv[optind]);
243 		i = argc - optind;
244 		dev_props = s_malloc(i * sizeof (char *));
245 		while (--i) {
246 			dev_props[i - 1] = s_strdup(argv[optind + i]);
247 		}
248 	}
249 
250 	/*
251 	 * if the device is a logical name, get the physical name
252 	 */
253 	if (lstat(orig_path, &stat_buf) == 0) {
254 		if (S_ISLNK(stat_buf.st_mode)) {
255 			if ((pathlen = readlink(orig_path, devctl_device,
256 			    MAXPATHLEN)) == -1)  {
257 				(void) fprintf(stderr,
258 					"devctl: readlink(%s) - %s\n",
259 					orig_path, strerror(errno));
260 				exit(-1);
261 			}
262 			devctl_device[pathlen] = '\0';
263 		}
264 	}
265 
266 	if ((dcmd = dc_cmd(device_cmds, devctl_cmdname)) == NULL) {
267 		dcmd = dc_cmd(bus_cmds, devctl_cmdname);
268 		if (dcmd == NULL) {
269 			(void) fprintf(stderr, "unrecognized command (%s)\n",
270 			    devctl_cmdname);
271 			(void) fprintf(stderr, usage, progname);
272 			exit(1);
273 		} else if (strcmp(devctl_cmdname, "bus-raisepower") == 0 ||
274 		    strcmp(devctl_cmdname, "bus-changepowerlow") == 0 ||
275 		    strcmp(devctl_cmdname, "bus-changepowerhigh") == 0 ||
276 		    strcmp(devctl_cmdname, "bus-idlecomp") == 0 ||
277 		    strcmp(devctl_cmdname, "bus-busycomp") == 0 ||
278 		    strcmp(devctl_cmdname, "bus-testbusy") == 0 ||
279 		    strcmp(devctl_cmdname, "bus-failsuspend") == 0 ||
280 		    strcmp(devctl_cmdname, "bus-teststrict") == 0 ||
281 		    strcmp(devctl_cmdname, "bus-noinvol") == 0) {
282 			dcp = devctl_pm_bus_acquire(devctl_device, 0);
283 			if (dcp == NULL) {
284 				(void) fprintf(stderr,
285 				    "devctl: device_pm_bus_acquire %s - %s\n",
286 				    devctl_device, strerror(errno));
287 				exit(-1);
288 			}
289 		} else {
290 			dcp = devctl_bus_acquire(devctl_device, 0);
291 			if (dcp == NULL) {
292 				(void) fprintf(stderr, "devctl: bus_acquire "
293 				    "%s - %s\n",
294 				    devctl_device, strerror(errno));
295 				exit(-1);
296 			}
297 		}
298 	} else if (strcmp(devctl_cmdname, "dev-raisepower") == 0 ||
299 	    strcmp(devctl_cmdname, "dev-changepowerlow") == 0 ||
300 	    strcmp(devctl_cmdname, "dev-changepowerhigh") == 0 ||
301 	    strcmp(devctl_cmdname, "dev-idlecomp") == 0 ||
302 	    strcmp(devctl_cmdname, "dev-busycomp") == 0 ||
303 	    strcmp(devctl_cmdname, "dev-testbusy") == 0 ||
304 	    strcmp(devctl_cmdname, "dev-failsuspend") == 0 ||
305 	    strcmp(devctl_cmdname, "dev-changeonresume") == 0 ||
306 	    strcmp(devctl_cmdname, "dev-promprintf") == 0 ||
307 	    strcmp(devctl_cmdname, "dev-nolowerpower") == 0) {
308 		dcp = devctl_pm_dev_acquire(devctl_device, 0);
309 		if (dcp == NULL) {
310 			(void) fprintf(stderr,
311 				"devctl: device_pm_dev_acquire %s - %s\n",
312 				devctl_device, strerror(errno));
313 			exit(-1);
314 		}
315 	} else {
316 		dcp = devctl_device_acquire(devctl_device, 0);
317 		if (dcp == NULL) {
318 			(void) fprintf(stderr,
319 				"devctl: device_acquire %s - %s\n",
320 				devctl_device, strerror(errno));
321 			exit(-1);
322 		}
323 	}
324 
325 	if (verbose)
326 		(void) printf("devctl: cmd (%s) device (%s)\n",
327 		    devctl_cmdname, orig_path);
328 
329 	(void) fflush(NULL);	/* get output out of the way */
330 
331 	rv = (dcmd->cmdf)(dcp);
332 
333 	if (rv == -1) {
334 		perror("devctl");
335 		exit(-1);
336 	}
337 	return (0);
338 } /* main */
339 
340 static int
341 dev_pm_testbusy(devctl_hdl_t dcp)
342 {
343 	int rv;
344 	uint_t *busyp;
345 
346 	busyp = s_malloc(sizeof (uint_t));
347 	rv = devctl_pm_testbusy(dcp, busyp);
348 	if (rv != -1)
349 		(void) printf("%s: busy state %d\n", orig_path, *busyp);
350 
351 	return (0);
352 }
353 
354 static int
355 bus_pm_teststrict(devctl_hdl_t dcp)
356 {
357 	int rv;
358 	uint_t *strict;
359 
360 	strict = s_malloc(sizeof (uint_t));
361 
362 	rv = devctl_pm_bus_teststrict(dcp, strict);
363 	if (rv != -1)
364 		(void) printf("%s: strict %d\n", orig_path, *strict);
365 
366 	return (0);
367 }
368 
369 static int
370 dev_getstate(devctl_hdl_t dcp)
371 {
372 	int rv;
373 	uint_t state;
374 
375 	rv = devctl_device_getstate(dcp, &state);
376 	if (rv != -1)
377 		print_dev_state(orig_path, state);
378 
379 	return (0);
380 }
381 
382 static int
383 bus_getstate(devctl_hdl_t dcp)
384 {
385 	int rv;
386 	uint_t state;
387 
388 	rv = devctl_bus_getstate(dcp, &state);
389 	if (rv != -1)
390 		print_bus_state(orig_path, state);
391 
392 	return (0);
393 }
394 
395 /*
396  * Only string property is supported now.
397  * Will add more later.
398  */
399 static void
400 add_prop(devctl_ddef_t ddef_hdl, char *prop_str)
401 {
402 	char *pname, *pval, *tmp;
403 	char **strs = NULL;
404 	int nstr;
405 
406 	tmp = strchr(prop_str, '=');
407 	if (tmp == NULL) {
408 		(void) fprintf(stderr, "invalid property %s", prop_str);
409 		exit(-1);
410 	}
411 
412 	(void) printf("prop string: %s\n", prop_str);
413 	pname = prop_str;
414 	*tmp++ = '\0';
415 	if (*tmp != '"') {
416 		(void) devctl_ddef_string(ddef_hdl, pname, tmp);
417 		return;
418 	}
419 
420 	nstr = 0;
421 	while (*tmp != '\0') {
422 		pval = tmp + 1;
423 		tmp = strchr(pval, '"');
424 		if (tmp == NULL) {
425 			(void) fprintf(stderr, "missing quote in %s", tmp);
426 			exit(-1);
427 		}
428 		nstr++;
429 		strs = (char **)s_realloc(strs, nstr * sizeof (char *));
430 		strs[nstr - 1] = pval;
431 		*tmp++ = '\0';
432 		(void) printf("string[%d] = %s\n", nstr - 1, pval);
433 		if (*tmp)
434 			tmp = strchr(tmp, '"');
435 		if (tmp == NULL) {
436 			(void) fprintf(stderr, "string not ending with quote");
437 			exit(-1);
438 		}
439 	}
440 
441 	(void) devctl_ddef_string_array(ddef_hdl, pname, nstr, strs);
442 	free(strs);
443 }
444 
445 static int
446 bus_devcreate(devctl_hdl_t bus_dcp)
447 {
448 	int rv;
449 	char **propp = dev_props;
450 	devctl_ddef_t ddef_hdl = NULL;
451 	devctl_hdl_t dev_hdl = NULL;
452 
453 	ddef_hdl = devctl_ddef_alloc(dev_name, 0);
454 	if (dev_props == NULL) {
455 		(void) fprintf(stderr, "dev-create: missing device props\n");
456 		return (-1);
457 	}
458 
459 	while (*propp) {
460 		add_prop(ddef_hdl, *propp);
461 		propp++;
462 	}
463 
464 	if (devctl_bus_dev_create(bus_dcp, ddef_hdl, 0, &dev_hdl)) {
465 		(void) fprintf(stderr,
466 		    "bus-devcreate: failed to create device node\n");
467 		rv = -1;
468 	} else if (devctl_get_pathname(dev_hdl, devctl_device, MAXPATHLEN)
469 	    == NULL) {
470 		(void) fprintf(stderr,
471 		    "bus-devcreate: failed to get device path\n");
472 		rv = -1;
473 	} else {
474 		(void) printf("created device %s\n", devctl_device);
475 		rv = 0;
476 	}
477 
478 	devctl_ddef_free(ddef_hdl);
479 	if (dev_hdl)
480 		devctl_release(dev_hdl);
481 
482 	return (rv);
483 }
484 
485 static void
486 print_bus_state(char *devname, uint_t state)
487 {
488 	(void) printf("\t%s: ", devname);
489 	if (state == BUS_QUIESCED)
490 		(void) printf("Quiesced");
491 	else if (state == BUS_ACTIVE)
492 		(void) printf("Active");
493 	else if (state == BUS_SHUTDOWN)
494 		(void) printf("Shutdown");
495 	(void) printf("\n");
496 }
497 
498 static void
499 print_dev_state(char *devname, uint_t state)
500 {
501 	(void) printf("\t%s: ", devname);
502 	if (state & DEVICE_ONLINE) {
503 		(void) printf("Online");
504 		if (state & DEVICE_BUSY)
505 			(void) printf(" Busy");
506 		if (state & DEVICE_DOWN)
507 			(void) printf(" Down");
508 	} else {
509 		if (state & DEVICE_OFFLINE) {
510 			(void) printf("Offline");
511 			if (state & DEVICE_DOWN)
512 				(void) printf(" Down");
513 		}
514 	}
515 	(void) printf("\n");
516 }
517 
518 static void
519 setprogname(char *name)
520 {
521 	register char *p;
522 
523 	if (p = strrchr(name, '/'))
524 		progname = p + 1;
525 	else
526 		progname = name;
527 }
528 
529 static struct cmds *
530 dc_cmd(struct cmds *cmd_tbl, char *devctl_cmdname)
531 {
532 	int i;
533 
534 	for (i = 0; cmd_tbl[i].cmdname != NULL; i++) {
535 		if (strcasecmp(cmd_tbl[i].cmdname, devctl_cmdname) == 0)
536 			return (&cmd_tbl[i]);
537 	}
538 
539 	return (NULL);
540 }
541 
542 /*
543  * list all nexus drivers exporting the :devctl minor device
544  */
545 static void
546 run_list_ctlrs(void)
547 {
548 	di_node_t dinode;
549 
550 	if ((dinode = di_init("/", DINFOSUBTREE|DINFOMINOR)) == NULL) {
551 		(void) fprintf(stderr, "%s: di_init() failed\n",
552 		    progname);
553 		exit(-1);
554 	}
555 	(void) di_walk_minor(dinode, DDI_NT_NEXUS, NULL, 0, &nexif);
556 	di_fini(dinode);
557 	exit(0);
558 }
559 
560 /*ARGSUSED*/
561 static int
562 nexif(di_node_t din, di_minor_t dim, void *arg)
563 {
564 	char *devname;
565 
566 	if ((devname = di_devfs_path(din)) != NULL) {
567 		(void) printf("%s%d: /devices%s\n", di_driver_name(din),
568 		    di_instance(din), devname);
569 		di_devfs_path_free(devname);
570 	}
571 
572 	return (DI_WALK_CONTINUE);
573 }
574 
575 void *
576 s_malloc(size_t len)
577 {
578 	void *buf = malloc(len);
579 
580 	if (buf == NULL) {
581 		perror("s_malloc failed");
582 		exit(-1);
583 	}
584 	return (buf);
585 }
586 
587 void *
588 s_realloc(void *ptr, size_t len)
589 {
590 	void *buf = realloc(ptr, len);
591 
592 	if (buf == NULL) {
593 		perror("s_realloc failed");
594 		exit(-1);
595 	}
596 	return (buf);
597 }
598 
599 char *
600 s_strdup(char *str)
601 {
602 	char *buf = strdup(str);
603 
604 	if (buf == NULL) {
605 		perror("s_malloc failed");
606 		exit(-1);
607 	}
608 	return (buf);
609 }
610