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