xref: /illumos-gate/usr/src/cmd/devctl/devctl.c (revision 1376f373)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * devctl - device control utility
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * to compile:
307c478bd9Sstevel@tonic-gate  * cc -o devctl -ldevice -ldevinfo devctl.c
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * usage: devctl [-v] command [device/bus pathname]
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Commands:
35*1376f373SToomas Soome  *	list		- list all controllers exporting the devctl interface
367c478bd9Sstevel@tonic-gate  *	online		- online a device
377c478bd9Sstevel@tonic-gate  *	offline		- offline a device
38*1376f373SToomas Soome  *	remove		- remove a device from the device tree
39*1376f373SToomas Soome  *	quiesce		- quiesce the bus
407c478bd9Sstevel@tonic-gate  *	unquiesce	- resume bus activity
417c478bd9Sstevel@tonic-gate  *	configure	- configure a bus's child devices
427c478bd9Sstevel@tonic-gate  *	unconfigure	- unconfigure a bus's child devices
437c478bd9Sstevel@tonic-gate  *	bus-reset	- reset a bus
447c478bd9Sstevel@tonic-gate  *	dev-reset	- reset a device
45*1376f373SToomas Soome  *	bus-getstate	- return the current state of the bus
467c478bd9Sstevel@tonic-gate  *	dev-getstate	- return the current state of the device
477c478bd9Sstevel@tonic-gate  *	bus-devcreate	- create a new device, bus specific
487c478bd9Sstevel@tonic-gate  *	dev-raisepower		- power up a device via pm_raise_power() (pm)
497c478bd9Sstevel@tonic-gate  *	dev-idlecomp		- idle a device's component 0 (pm)
507c478bd9Sstevel@tonic-gate  *	dev-busycomp		- busy a device's component 0 (pm)
517c478bd9Sstevel@tonic-gate  *	dev-testbusy		- test a device's component 0's busy state (pm)
527c478bd9Sstevel@tonic-gate  *	dev-changepowerhigh	- power up a device via pm_power_has_changed()
537c478bd9Sstevel@tonic-gate  *				  (pm)
547c478bd9Sstevel@tonic-gate  *	dev-changepowerlow	- power off a device via pm_power_has_changed()
557c478bd9Sstevel@tonic-gate  *				  (pm)
567c478bd9Sstevel@tonic-gate  *	dev-failsuspend		- fail DDI_SUSPEND (pm)
577c478bd9Sstevel@tonic-gate  *	dev-changeonresume	- issue pm_power_has_changed() vs,
587c478bd9Sstevel@tonic-gate  *				  pm_raise_power() on device resume (pm)
597c478bd9Sstevel@tonic-gate  *	dev-nolowerpower	- don't call pm_lower_power() on detach (pm)
607c478bd9Sstevel@tonic-gate  *	dev-promprintf		- issue a prom_printf() call (pm)
617c478bd9Sstevel@tonic-gate  *	bus-raisepower		- power up a bus via pm_raise_power() (pm)
627c478bd9Sstevel@tonic-gate  *	bus-idlecomp		- idle a bus' component (pm)
637c478bd9Sstevel@tonic-gate  *	bus-busycomp		- busy a bus' component (pm)
647c478bd9Sstevel@tonic-gate  *	bus-testbusy		- test a bus' component busy state (pm)
657c478bd9Sstevel@tonic-gate  *	bus-changepowerhigh	- power up a bus via pm_power_has_changed() (pm)
667c478bd9Sstevel@tonic-gate  *	bus-changepowerlow	- power off a bus via pm_power_has_changed()
677c478bd9Sstevel@tonic-gate  *				  (pm)
687c478bd9Sstevel@tonic-gate  *	bus-failsuspend		- fail DDI_SUSPEND (pm)
697c478bd9Sstevel@tonic-gate  *	bus-teststrict		- test is bus driver is  strict or involved (pm)
707c478bd9Sstevel@tonic-gate  *	bus-noinvol		- mark idle twice when child detaches
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * Returns:
747c478bd9Sstevel@tonic-gate  *	- Success
757c478bd9Sstevel@tonic-gate  *	- Operation not supported by device
767c478bd9Sstevel@tonic-gate  *	- No Permission
777c478bd9Sstevel@tonic-gate  *	- No Such Device
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * Examples:
807c478bd9Sstevel@tonic-gate  *	devctl list - list all controllers exporting a :devctl node
817c478bd9Sstevel@tonic-gate  *	devctl offline /dev/dsk/c0t3d0s0  - offline disk
827c478bd9Sstevel@tonic-gate  *	devctl dev-getstate  /devices/sbus@1f,0/espdma@e,8400000/esp@e,8800000\
837c478bd9Sstevel@tonic-gate  * sd@3,0
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #include <stdio.h>
887c478bd9Sstevel@tonic-gate #include <string.h>
897c478bd9Sstevel@tonic-gate #include <unistd.h>
907c478bd9Sstevel@tonic-gate #include <stdlib.h>
917c478bd9Sstevel@tonic-gate #include <sys/types.h>
927c478bd9Sstevel@tonic-gate #include <sys/errno.h>
937c478bd9Sstevel@tonic-gate #include <sys/stat.h>
947c478bd9Sstevel@tonic-gate #include <sys/param.h>
957c478bd9Sstevel@tonic-gate #include <libdevice.h>
967c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
977c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef struct cmds {
1007c478bd9Sstevel@tonic-gate 	char *cmdname;
1017c478bd9Sstevel@tonic-gate 	int (*cmdf)(devctl_hdl_t);
1027c478bd9Sstevel@tonic-gate } cmds_t;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate extern int errno;
1057c478bd9Sstevel@tonic-gate 
10623a1cceaSRoger A. Faulkner static void setpname(char *name);
1077c478bd9Sstevel@tonic-gate static void print_bus_state(char *devname, uint_t state);
1087c478bd9Sstevel@tonic-gate static void print_dev_state(char *devname, uint_t state);
1097c478bd9Sstevel@tonic-gate static int dev_getstate(devctl_hdl_t);
1107c478bd9Sstevel@tonic-gate static int bus_getstate(devctl_hdl_t);
1117c478bd9Sstevel@tonic-gate static int bus_devcreate(devctl_hdl_t);
1127c478bd9Sstevel@tonic-gate static void run_list_ctlrs(void);
1137c478bd9Sstevel@tonic-gate static struct cmds *dc_cmd();
1147c478bd9Sstevel@tonic-gate static int nexif(di_node_t din, di_minor_t dim, void *arg);
1157c478bd9Sstevel@tonic-gate static void *s_malloc(size_t);
1167c478bd9Sstevel@tonic-gate static void *s_realloc(void *, size_t);
1177c478bd9Sstevel@tonic-gate static char *s_strdup(char *);
1187c478bd9Sstevel@tonic-gate static int dev_pm_testbusy(devctl_hdl_t);
1197c478bd9Sstevel@tonic-gate static int bus_pm_teststrict(devctl_hdl_t);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static char *devctl_device;
1227c478bd9Sstevel@tonic-gate static char *orig_path;
1237c478bd9Sstevel@tonic-gate static char *devctl_cmdname;
1247c478bd9Sstevel@tonic-gate static char *progname;
1257c478bd9Sstevel@tonic-gate static int  verbose;
1267c478bd9Sstevel@tonic-gate static int  debug;
1277c478bd9Sstevel@tonic-gate static char *dev_name;
1287c478bd9Sstevel@tonic-gate static char **dev_props;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static const char *usage = "%s [-v] list | online | offline | remove |\n"
1317c478bd9Sstevel@tonic-gate 	"\tquiesce | unquiesce | configure | unconfigure |\n"
1327c478bd9Sstevel@tonic-gate 	"\t{bus,dev}-reset {bus,dev}-getstate | {bus,dev}-raisepower |\n"
1337c478bd9Sstevel@tonic-gate 	"\t{bus,dev}-idlecomp | {bus,dev}-busycomp |\n"
1347c478bd9Sstevel@tonic-gate 	"\t{bus,dev}-changepowerhigh | {bus,dev}-changepowerlow |\n"
1357c478bd9Sstevel@tonic-gate 	"\t{bus,dev}-testbusy | {bus,dev}-failsuspend | dev-changeonresume |\n"
1367c478bd9Sstevel@tonic-gate 	"\tdev-promprintf | dev-nolowerpower | bus-teststrict |\n"
1377c478bd9Sstevel@tonic-gate 	"\tbus-noinvol [/dev/... | /devices/...]\n";
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct cmds device_cmds[] = {
1407c478bd9Sstevel@tonic-gate 	{"online", devctl_device_online},
1417c478bd9Sstevel@tonic-gate 	{"offline", devctl_device_offline},
1427c478bd9Sstevel@tonic-gate 	{"remove", devctl_device_remove},
1437c478bd9Sstevel@tonic-gate 	{"dev-reset", devctl_device_reset},
1447c478bd9Sstevel@tonic-gate 	{"dev-getstate", dev_getstate},
1457c478bd9Sstevel@tonic-gate 	{"dev-raisepower", devctl_pm_raisepower},
1467c478bd9Sstevel@tonic-gate 	{"dev-busycomp", devctl_pm_busycomponent},
1477c478bd9Sstevel@tonic-gate 	{"dev-idlecomp", devctl_pm_idlecomponent},
1487c478bd9Sstevel@tonic-gate 	{"dev-testbusy", dev_pm_testbusy},
1497c478bd9Sstevel@tonic-gate 	{"dev-changepowerlow", devctl_pm_changepowerlow},
1507c478bd9Sstevel@tonic-gate 	{"dev-changepowerhigh", devctl_pm_changepowerhigh},
1517c478bd9Sstevel@tonic-gate 	{"dev-failsuspend", devctl_pm_failsuspend},
1527c478bd9Sstevel@tonic-gate 	{"dev-changeonresume", devctl_pm_device_changeonresume},
1537c478bd9Sstevel@tonic-gate 	{"dev-promprintf", devctl_pm_device_promprintf},
1547c478bd9Sstevel@tonic-gate 	{"dev-nolowerpower", devctl_pm_device_no_lower_power},
1557c478bd9Sstevel@tonic-gate 	{NULL, NULL},
1567c478bd9Sstevel@tonic-gate };
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static struct cmds bus_cmds[] = {
1597c478bd9Sstevel@tonic-gate 	{"quiesce", devctl_bus_quiesce},
1607c478bd9Sstevel@tonic-gate 	{"unquiesce", devctl_bus_unquiesce},
1617c478bd9Sstevel@tonic-gate 	{"bus-reset", devctl_bus_reset},
1627c478bd9Sstevel@tonic-gate 	{"configure", devctl_bus_configure},
1637c478bd9Sstevel@tonic-gate 	{"unconfigure", devctl_bus_unconfigure},
1647c478bd9Sstevel@tonic-gate 	{"bus-getstate", bus_getstate},
1657c478bd9Sstevel@tonic-gate 	{"bus-devcreate", bus_devcreate},
1667c478bd9Sstevel@tonic-gate 	{"bus-raisepower", devctl_pm_raisepower},
1677c478bd9Sstevel@tonic-gate 	{"bus-busycomp", devctl_pm_busycomponent},
1687c478bd9Sstevel@tonic-gate 	{"bus-idlecomp", devctl_pm_idlecomponent},
1697c478bd9Sstevel@tonic-gate 	{"bus-changepowerlow", devctl_pm_changepowerlow},
1707c478bd9Sstevel@tonic-gate 	{"bus-changepowerhigh", devctl_pm_changepowerhigh},
1717c478bd9Sstevel@tonic-gate 	{"bus-testbusy", dev_pm_testbusy},
1727c478bd9Sstevel@tonic-gate 	{"bus-failsuspend", devctl_pm_failsuspend},
1737c478bd9Sstevel@tonic-gate 	{"bus-teststrict", bus_pm_teststrict},
1747c478bd9Sstevel@tonic-gate 	{"bus-noinvol", devctl_pm_bus_no_invol},
1757c478bd9Sstevel@tonic-gate 	{NULL, NULL},
1767c478bd9Sstevel@tonic-gate };
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
180e42d2a1cSjongkis int
main(int argc,char * argv[])1817c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int	c;
184*1376f373SToomas Soome 	int	rv;
1857c478bd9Sstevel@tonic-gate 	int	pathlen;
1867c478bd9Sstevel@tonic-gate 	struct cmds *dcmd;
1877c478bd9Sstevel@tonic-gate 	devctl_hdl_t dcp;
1887c478bd9Sstevel@tonic-gate 	struct stat stat_buf;
1897c478bd9Sstevel@tonic-gate 
19023a1cceaSRoger A. Faulkner 	setpname(argv[0]);
1917c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vd")) != -1)  {
1927c478bd9Sstevel@tonic-gate 		switch (c)  {
1937c478bd9Sstevel@tonic-gate 		case 'v':
1947c478bd9Sstevel@tonic-gate 			++verbose;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		case 'd':
1977c478bd9Sstevel@tonic-gate 			++debug;
1987c478bd9Sstevel@tonic-gate 			(void) putenv("LIBDEVICE_DEBUG");
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		default:
2017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, progname);
2027c478bd9Sstevel@tonic-gate 			exit(1);
2037c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (optind == argc) {
2087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, progname);
2097c478bd9Sstevel@tonic-gate 		exit(-1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	devctl_cmdname = argv[optind++];
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (strcmp(devctl_cmdname, "list") == 0) {
2157c478bd9Sstevel@tonic-gate 		run_list_ctlrs();
2167c478bd9Sstevel@tonic-gate 		exit(0);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * any command other than "list" requires a device path
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	if (((optind + 1) > argc)) {
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, progname);
2247c478bd9Sstevel@tonic-gate 		exit(-1);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	orig_path = s_strdup(argv[optind]);
2287c478bd9Sstevel@tonic-gate 	devctl_device = s_malloc(MAXPATHLEN);
2297c478bd9Sstevel@tonic-gate 	(void) strcpy(devctl_device, orig_path);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Additional properties follow for bus-devcreate
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	if ((optind + 1 < argc) &&
2357c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "bus-devcreate") == 0) {
2367c478bd9Sstevel@tonic-gate 		int i;
2377c478bd9Sstevel@tonic-gate 		optind++;
2387c478bd9Sstevel@tonic-gate 		dev_name = s_strdup(argv[optind]);
2397c478bd9Sstevel@tonic-gate 		i = argc - optind;
2407c478bd9Sstevel@tonic-gate 		dev_props = s_malloc(i * sizeof (char *));
2417c478bd9Sstevel@tonic-gate 		while (--i) {
2427c478bd9Sstevel@tonic-gate 			dev_props[i - 1] = s_strdup(argv[optind + i]);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * if the device is a logical name, get the physical name
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	if (lstat(orig_path, &stat_buf) == 0) {
2504bc0a2efScasper 		if (S_ISLNK(stat_buf.st_mode)) {
2517c478bd9Sstevel@tonic-gate 			if ((pathlen = readlink(orig_path, devctl_device,
2527c478bd9Sstevel@tonic-gate 			    MAXPATHLEN)) == -1)  {
2537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
25423a1cceaSRoger A. Faulkner 				    "devctl: readlink(%s) - %s\n",
25523a1cceaSRoger A. Faulkner 				    orig_path, strerror(errno));
2567c478bd9Sstevel@tonic-gate 				exit(-1);
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			devctl_device[pathlen] = '\0';
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if ((dcmd = dc_cmd(device_cmds, devctl_cmdname)) == NULL) {
2637c478bd9Sstevel@tonic-gate 		dcmd = dc_cmd(bus_cmds, devctl_cmdname);
2647c478bd9Sstevel@tonic-gate 		if (dcmd == NULL) {
2657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unrecognized command (%s)\n",
2667c478bd9Sstevel@tonic-gate 			    devctl_cmdname);
2677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, progname);
2687c478bd9Sstevel@tonic-gate 			exit(1);
2697c478bd9Sstevel@tonic-gate 		} else if (strcmp(devctl_cmdname, "bus-raisepower") == 0 ||
2707c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-changepowerlow") == 0 ||
2717c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-changepowerhigh") == 0 ||
2727c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-idlecomp") == 0 ||
2737c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-busycomp") == 0 ||
2747c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-testbusy") == 0 ||
2757c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-failsuspend") == 0 ||
2767c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-teststrict") == 0 ||
2777c478bd9Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-noinvol") == 0) {
2787c478bd9Sstevel@tonic-gate 			dcp = devctl_pm_bus_acquire(devctl_device, 0);
2797c478bd9Sstevel@tonic-gate 			if (dcp == NULL) {
2807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2817c478bd9Sstevel@tonic-gate 				    "devctl: device_pm_bus_acquire %s - %s\n",
2827c478bd9Sstevel@tonic-gate 				    devctl_device, strerror(errno));
2837c478bd9Sstevel@tonic-gate 				exit(-1);
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 		} else {
2867c478bd9Sstevel@tonic-gate 			dcp = devctl_bus_acquire(devctl_device, 0);
2877c478bd9Sstevel@tonic-gate 			if (dcp == NULL) {
2887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "devctl: bus_acquire "
2897c478bd9Sstevel@tonic-gate 				    "%s - %s\n",
2907c478bd9Sstevel@tonic-gate 				    devctl_device, strerror(errno));
2917c478bd9Sstevel@tonic-gate 				exit(-1);
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	} else if (strcmp(devctl_cmdname, "dev-raisepower") == 0 ||
2957c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changepowerlow") == 0 ||
2967c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changepowerhigh") == 0 ||
2977c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-idlecomp") == 0 ||
2987c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-busycomp") == 0 ||
2997c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-testbusy") == 0 ||
3007c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-failsuspend") == 0 ||
3017c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changeonresume") == 0 ||
3027c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-promprintf") == 0 ||
3037c478bd9Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-nolowerpower") == 0) {
3047c478bd9Sstevel@tonic-gate 		dcp = devctl_pm_dev_acquire(devctl_device, 0);
3057c478bd9Sstevel@tonic-gate 		if (dcp == NULL) {
3067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
30723a1cceaSRoger A. Faulkner 			    "devctl: device_pm_dev_acquire %s - %s\n",
30823a1cceaSRoger A. Faulkner 			    devctl_device, strerror(errno));
3097c478bd9Sstevel@tonic-gate 			exit(-1);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	} else {
3127c478bd9Sstevel@tonic-gate 		dcp = devctl_device_acquire(devctl_device, 0);
3137c478bd9Sstevel@tonic-gate 		if (dcp == NULL) {
3147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
31523a1cceaSRoger A. Faulkner 			    "devctl: device_acquire %s - %s\n",
31623a1cceaSRoger A. Faulkner 			    devctl_device, strerror(errno));
3177c478bd9Sstevel@tonic-gate 			exit(-1);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (verbose)
3227c478bd9Sstevel@tonic-gate 		(void) printf("devctl: cmd (%s) device (%s)\n",
3237c478bd9Sstevel@tonic-gate 		    devctl_cmdname, orig_path);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	(void) fflush(NULL);	/* get output out of the way */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	rv = (dcmd->cmdf)(dcp);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (rv == -1) {
3307c478bd9Sstevel@tonic-gate 		perror("devctl");
3317c478bd9Sstevel@tonic-gate 		exit(-1);
3327c478bd9Sstevel@tonic-gate 	}
333e42d2a1cSjongkis 	return (0);
3347c478bd9Sstevel@tonic-gate } /* main */
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate static int
dev_pm_testbusy(devctl_hdl_t dcp)3377c478bd9Sstevel@tonic-gate dev_pm_testbusy(devctl_hdl_t dcp)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	int rv;
3407c478bd9Sstevel@tonic-gate 	uint_t *busyp;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	busyp = s_malloc(sizeof (uint_t));
3437c478bd9Sstevel@tonic-gate 	rv = devctl_pm_testbusy(dcp, busyp);
3447c478bd9Sstevel@tonic-gate 	if (rv != -1)
3457c478bd9Sstevel@tonic-gate 		(void) printf("%s: busy state %d\n", orig_path, *busyp);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static int
bus_pm_teststrict(devctl_hdl_t dcp)3517c478bd9Sstevel@tonic-gate bus_pm_teststrict(devctl_hdl_t dcp)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	int rv;
3547c478bd9Sstevel@tonic-gate 	uint_t *strict;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	strict = s_malloc(sizeof (uint_t));
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	rv = devctl_pm_bus_teststrict(dcp, strict);
3597c478bd9Sstevel@tonic-gate 	if (rv != -1)
3607c478bd9Sstevel@tonic-gate 		(void) printf("%s: strict %d\n", orig_path, *strict);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static int
dev_getstate(devctl_hdl_t dcp)3667c478bd9Sstevel@tonic-gate dev_getstate(devctl_hdl_t dcp)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	int rv;
3697c478bd9Sstevel@tonic-gate 	uint_t state;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	rv = devctl_device_getstate(dcp, &state);
3727c478bd9Sstevel@tonic-gate 	if (rv != -1)
3737c478bd9Sstevel@tonic-gate 		print_dev_state(orig_path, state);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static int
bus_getstate(devctl_hdl_t dcp)3797c478bd9Sstevel@tonic-gate bus_getstate(devctl_hdl_t dcp)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	int rv;
3827c478bd9Sstevel@tonic-gate 	uint_t state;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	rv = devctl_bus_getstate(dcp, &state);
3857c478bd9Sstevel@tonic-gate 	if (rv != -1)
3867c478bd9Sstevel@tonic-gate 		print_bus_state(orig_path, state);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	return (0);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Only string property is supported now.
3937c478bd9Sstevel@tonic-gate  * Will add more later.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate static void
add_prop(devctl_ddef_t ddef_hdl,char * prop_str)3967c478bd9Sstevel@tonic-gate add_prop(devctl_ddef_t ddef_hdl, char *prop_str)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	char *pname, *pval, *tmp;
3997c478bd9Sstevel@tonic-gate 	char **strs = NULL;
4007c478bd9Sstevel@tonic-gate 	int nstr;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	tmp = strchr(prop_str, '=');
4037c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
4047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "invalid property %s", prop_str);
4057c478bd9Sstevel@tonic-gate 		exit(-1);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	(void) printf("prop string: %s\n", prop_str);
4097c478bd9Sstevel@tonic-gate 	pname = prop_str;
4107c478bd9Sstevel@tonic-gate 	*tmp++ = '\0';
4117c478bd9Sstevel@tonic-gate 	if (*tmp != '"') {
4127c478bd9Sstevel@tonic-gate 		(void) devctl_ddef_string(ddef_hdl, pname, tmp);
4137c478bd9Sstevel@tonic-gate 		return;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	nstr = 0;
4177c478bd9Sstevel@tonic-gate 	while (*tmp != '\0') {
4187c478bd9Sstevel@tonic-gate 		pval = tmp + 1;
4197c478bd9Sstevel@tonic-gate 		tmp = strchr(pval, '"');
4207c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
4217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "missing quote in %s", tmp);
4227c478bd9Sstevel@tonic-gate 			exit(-1);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		nstr++;
4257c478bd9Sstevel@tonic-gate 		strs = (char **)s_realloc(strs, nstr * sizeof (char *));
4267c478bd9Sstevel@tonic-gate 		strs[nstr - 1] = pval;
4277c478bd9Sstevel@tonic-gate 		*tmp++ = '\0';
4287c478bd9Sstevel@tonic-gate 		(void) printf("string[%d] = %s\n", nstr - 1, pval);
4297c478bd9Sstevel@tonic-gate 		if (*tmp)
4307c478bd9Sstevel@tonic-gate 			tmp = strchr(tmp, '"');
4317c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
4327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "string not ending with quote");
4337c478bd9Sstevel@tonic-gate 			exit(-1);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	(void) devctl_ddef_string_array(ddef_hdl, pname, nstr, strs);
4387c478bd9Sstevel@tonic-gate 	free(strs);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
bus_devcreate(devctl_hdl_t bus_dcp)4427c478bd9Sstevel@tonic-gate bus_devcreate(devctl_hdl_t bus_dcp)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int rv;
4457c478bd9Sstevel@tonic-gate 	char **propp = dev_props;
4467c478bd9Sstevel@tonic-gate 	devctl_ddef_t ddef_hdl = NULL;
4477c478bd9Sstevel@tonic-gate 	devctl_hdl_t dev_hdl = NULL;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	ddef_hdl = devctl_ddef_alloc(dev_name, 0);
4507c478bd9Sstevel@tonic-gate 	if (dev_props == NULL) {
4517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dev-create: missing device props\n");
4527c478bd9Sstevel@tonic-gate 		return (-1);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	while (*propp) {
4567c478bd9Sstevel@tonic-gate 		add_prop(ddef_hdl, *propp);
4577c478bd9Sstevel@tonic-gate 		propp++;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (devctl_bus_dev_create(bus_dcp, ddef_hdl, 0, &dev_hdl)) {
4617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4627c478bd9Sstevel@tonic-gate 		    "bus-devcreate: failed to create device node\n");
4637c478bd9Sstevel@tonic-gate 		rv = -1;
4647c478bd9Sstevel@tonic-gate 	} else if (devctl_get_pathname(dev_hdl, devctl_device, MAXPATHLEN)
4657c478bd9Sstevel@tonic-gate 	    == NULL) {
4667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4677c478bd9Sstevel@tonic-gate 		    "bus-devcreate: failed to get device path\n");
4687c478bd9Sstevel@tonic-gate 		rv = -1;
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		(void) printf("created device %s\n", devctl_device);
4717c478bd9Sstevel@tonic-gate 		rv = 0;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	devctl_ddef_free(ddef_hdl);
4757c478bd9Sstevel@tonic-gate 	if (dev_hdl)
4767c478bd9Sstevel@tonic-gate 		devctl_release(dev_hdl);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (rv);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static void
print_bus_state(char * devname,uint_t state)4827c478bd9Sstevel@tonic-gate print_bus_state(char *devname, uint_t state)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	(void) printf("\t%s: ", devname);
4857c478bd9Sstevel@tonic-gate 	if (state == BUS_QUIESCED)
4867c478bd9Sstevel@tonic-gate 		(void) printf("Quiesced");
4877c478bd9Sstevel@tonic-gate 	else if (state == BUS_ACTIVE)
4887c478bd9Sstevel@tonic-gate 		(void) printf("Active");
4897c478bd9Sstevel@tonic-gate 	else if (state == BUS_SHUTDOWN)
4907c478bd9Sstevel@tonic-gate 		(void) printf("Shutdown");
4917c478bd9Sstevel@tonic-gate 	(void) printf("\n");
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static void
print_dev_state(char * devname,uint_t state)4957c478bd9Sstevel@tonic-gate print_dev_state(char *devname, uint_t state)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	(void) printf("\t%s: ", devname);
4987c478bd9Sstevel@tonic-gate 	if (state & DEVICE_ONLINE) {
4997c478bd9Sstevel@tonic-gate 		(void) printf("Online");
5007c478bd9Sstevel@tonic-gate 		if (state & DEVICE_BUSY)
5017c478bd9Sstevel@tonic-gate 			(void) printf(" Busy");
5027c478bd9Sstevel@tonic-gate 		if (state & DEVICE_DOWN)
5037c478bd9Sstevel@tonic-gate 			(void) printf(" Down");
5047c478bd9Sstevel@tonic-gate 	} else {
5057c478bd9Sstevel@tonic-gate 		if (state & DEVICE_OFFLINE) {
5067c478bd9Sstevel@tonic-gate 			(void) printf("Offline");
5077c478bd9Sstevel@tonic-gate 			if (state & DEVICE_DOWN)
5087c478bd9Sstevel@tonic-gate 				(void) printf(" Down");
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 	(void) printf("\n");
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate static void
setpname(char * name)51523a1cceaSRoger A. Faulkner setpname(char *name)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	register char *p;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (p = strrchr(name, '/'))
5207c478bd9Sstevel@tonic-gate 		progname = p + 1;
5217c478bd9Sstevel@tonic-gate 	else
5227c478bd9Sstevel@tonic-gate 		progname = name;
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static struct cmds *
dc_cmd(struct cmds * cmd_tbl,char * devctl_cmdname)5267c478bd9Sstevel@tonic-gate dc_cmd(struct cmds *cmd_tbl, char *devctl_cmdname)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	int i;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	for (i = 0; cmd_tbl[i].cmdname != NULL; i++) {
5317c478bd9Sstevel@tonic-gate 		if (strcasecmp(cmd_tbl[i].cmdname, devctl_cmdname) == 0)
5327c478bd9Sstevel@tonic-gate 			return (&cmd_tbl[i]);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	return (NULL);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * list all nexus drivers exporting the :devctl minor device
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate static void
run_list_ctlrs(void)5427c478bd9Sstevel@tonic-gate run_list_ctlrs(void)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	di_node_t dinode;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if ((dinode = di_init("/", DINFOSUBTREE|DINFOMINOR)) == NULL) {
5477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: di_init() failed\n",
5487c478bd9Sstevel@tonic-gate 		    progname);
5497c478bd9Sstevel@tonic-gate 		exit(-1);
5507c478bd9Sstevel@tonic-gate 	}
551*1376f373SToomas Soome 	(void) di_walk_minor(dinode, DDI_NT_NEXUS, 0, NULL, &nexif);
5527c478bd9Sstevel@tonic-gate 	di_fini(dinode);
5537c478bd9Sstevel@tonic-gate 	exit(0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5577c478bd9Sstevel@tonic-gate static int
nexif(di_node_t din,di_minor_t dim,void * arg)5587c478bd9Sstevel@tonic-gate nexif(di_node_t din, di_minor_t dim, void *arg)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	char *devname;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if ((devname = di_devfs_path(din)) != NULL) {
5637c478bd9Sstevel@tonic-gate 		(void) printf("%s%d: /devices%s\n", di_driver_name(din),
5647c478bd9Sstevel@tonic-gate 		    di_instance(din), devname);
5657c478bd9Sstevel@tonic-gate 		di_devfs_path_free(devname);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate void *
s_malloc(size_t len)5727c478bd9Sstevel@tonic-gate s_malloc(size_t len)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	void *buf = malloc(len);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
5777c478bd9Sstevel@tonic-gate 		perror("s_malloc failed");
5787c478bd9Sstevel@tonic-gate 		exit(-1);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	return (buf);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate void *
s_realloc(void * ptr,size_t len)5847c478bd9Sstevel@tonic-gate s_realloc(void *ptr, size_t len)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	void *buf = realloc(ptr, len);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
5897c478bd9Sstevel@tonic-gate 		perror("s_realloc failed");
5907c478bd9Sstevel@tonic-gate 		exit(-1);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	return (buf);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate char *
s_strdup(char * str)5967c478bd9Sstevel@tonic-gate s_strdup(char *str)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	char *buf = strdup(str);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
6017c478bd9Sstevel@tonic-gate 		perror("s_malloc failed");
6027c478bd9Sstevel@tonic-gate 		exit(-1);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	return (buf);
6057c478bd9Sstevel@tonic-gate }
606