xref: /onnv-gate/usr/src/cmd/devctl/devctl.c (revision 513)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*513Sjongkis  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * devctl - device control utility
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * to compile:
340Sstevel@tonic-gate  * cc -o devctl -ldevice -ldevinfo devctl.c
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * usage: devctl [-v] command [device/bus pathname]
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * Commands:
390Sstevel@tonic-gate  * 	list		- list all controllers exporting the devctl interface
400Sstevel@tonic-gate  *	online		- online a device
410Sstevel@tonic-gate  *	offline		- offline a device
420Sstevel@tonic-gate  *	remove  	- remove a device from the device tree
430Sstevel@tonic-gate  * 	quiesce		- quiesce the bus
440Sstevel@tonic-gate  *	unquiesce	- resume bus activity
450Sstevel@tonic-gate  *	configure	- configure a bus's child devices
460Sstevel@tonic-gate  *	unconfigure	- unconfigure a bus's child devices
470Sstevel@tonic-gate  *	bus-reset	- reset a bus
480Sstevel@tonic-gate  *	dev-reset	- reset a device
490Sstevel@tonic-gate  * 	bus-getstate	- return the current state of the bus
500Sstevel@tonic-gate  *	dev-getstate	- return the current state of the device
510Sstevel@tonic-gate  *	bus-devcreate	- create a new device, bus specific
520Sstevel@tonic-gate  *	dev-raisepower		- power up a device via pm_raise_power() (pm)
530Sstevel@tonic-gate  *	dev-idlecomp		- idle a device's component 0 (pm)
540Sstevel@tonic-gate  *	dev-busycomp		- busy a device's component 0 (pm)
550Sstevel@tonic-gate  *	dev-testbusy		- test a device's component 0's busy state (pm)
560Sstevel@tonic-gate  *	dev-changepowerhigh	- power up a device via pm_power_has_changed()
570Sstevel@tonic-gate  *				  (pm)
580Sstevel@tonic-gate  *	dev-changepowerlow	- power off a device via pm_power_has_changed()
590Sstevel@tonic-gate  *				  (pm)
600Sstevel@tonic-gate  *	dev-failsuspend		- fail DDI_SUSPEND (pm)
610Sstevel@tonic-gate  *	dev-changeonresume	- issue pm_power_has_changed() vs,
620Sstevel@tonic-gate  *				  pm_raise_power() on device resume (pm)
630Sstevel@tonic-gate  *	dev-nolowerpower	- don't call pm_lower_power() on detach (pm)
640Sstevel@tonic-gate  *	dev-promprintf		- issue a prom_printf() call (pm)
650Sstevel@tonic-gate  *	bus-raisepower		- power up a bus via pm_raise_power() (pm)
660Sstevel@tonic-gate  *	bus-idlecomp		- idle a bus' component (pm)
670Sstevel@tonic-gate  *	bus-busycomp		- busy a bus' component (pm)
680Sstevel@tonic-gate  *	bus-testbusy		- test a bus' component busy state (pm)
690Sstevel@tonic-gate  *	bus-changepowerhigh	- power up a bus via pm_power_has_changed() (pm)
700Sstevel@tonic-gate  *	bus-changepowerlow	- power off a bus via pm_power_has_changed()
710Sstevel@tonic-gate  *				  (pm)
720Sstevel@tonic-gate  *	bus-failsuspend		- fail DDI_SUSPEND (pm)
730Sstevel@tonic-gate  *	bus-teststrict		- test is bus driver is  strict or involved (pm)
740Sstevel@tonic-gate  *	bus-noinvol		- mark idle twice when child detaches
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Returns:
780Sstevel@tonic-gate  *	- Success
790Sstevel@tonic-gate  *	- Operation not supported by device
800Sstevel@tonic-gate  *	- No Permission
810Sstevel@tonic-gate  *	- No Such Device
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Examples:
840Sstevel@tonic-gate  *	devctl list - list all controllers exporting a :devctl node
850Sstevel@tonic-gate  *	devctl offline /dev/dsk/c0t3d0s0  - offline disk
860Sstevel@tonic-gate  *	devctl dev-getstate  /devices/sbus@1f,0/espdma@e,8400000/esp@e,8800000\
870Sstevel@tonic-gate  * sd@3,0
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #include <stdio.h>
920Sstevel@tonic-gate #include <string.h>
930Sstevel@tonic-gate #include <unistd.h>
940Sstevel@tonic-gate #include <stdlib.h>
950Sstevel@tonic-gate #include <sys/types.h>
960Sstevel@tonic-gate #include <sys/errno.h>
970Sstevel@tonic-gate #include <sys/stat.h>
980Sstevel@tonic-gate #include <sys/param.h>
990Sstevel@tonic-gate #include <libdevice.h>
1000Sstevel@tonic-gate #include <libdevinfo.h>
1010Sstevel@tonic-gate #include <sys/sunddi.h>
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate typedef struct cmds {
1040Sstevel@tonic-gate 	char *cmdname;
1050Sstevel@tonic-gate 	int (*cmdf)(devctl_hdl_t);
1060Sstevel@tonic-gate } cmds_t;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate extern int errno;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static void setprogname(char *name);
1110Sstevel@tonic-gate static void print_bus_state(char *devname, uint_t state);
1120Sstevel@tonic-gate static void print_dev_state(char *devname, uint_t state);
1130Sstevel@tonic-gate static int dev_getstate(devctl_hdl_t);
1140Sstevel@tonic-gate static int bus_getstate(devctl_hdl_t);
1150Sstevel@tonic-gate static int bus_devcreate(devctl_hdl_t);
1160Sstevel@tonic-gate static void run_list_ctlrs(void);
1170Sstevel@tonic-gate static struct cmds *dc_cmd();
1180Sstevel@tonic-gate static int nexif(di_node_t din, di_minor_t dim, void *arg);
1190Sstevel@tonic-gate static void *s_malloc(size_t);
1200Sstevel@tonic-gate static void *s_realloc(void *, size_t);
1210Sstevel@tonic-gate static char *s_strdup(char *);
1220Sstevel@tonic-gate static int dev_pm_testbusy(devctl_hdl_t);
1230Sstevel@tonic-gate static int bus_pm_teststrict(devctl_hdl_t);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static char *devctl_device;
1260Sstevel@tonic-gate static char *orig_path;
1270Sstevel@tonic-gate static char *devctl_cmdname;
1280Sstevel@tonic-gate static char *progname;
1290Sstevel@tonic-gate static int  verbose;
1300Sstevel@tonic-gate static int  debug;
1310Sstevel@tonic-gate static char *dev_name;
1320Sstevel@tonic-gate static char **dev_props;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static const char *usage = "%s [-v] list | online | offline | remove |\n"
1350Sstevel@tonic-gate 	"\tquiesce | unquiesce | configure | unconfigure |\n"
1360Sstevel@tonic-gate 	"\t{bus,dev}-reset {bus,dev}-getstate | {bus,dev}-raisepower |\n"
1370Sstevel@tonic-gate 	"\t{bus,dev}-idlecomp | {bus,dev}-busycomp |\n"
1380Sstevel@tonic-gate 	"\t{bus,dev}-changepowerhigh | {bus,dev}-changepowerlow |\n"
1390Sstevel@tonic-gate 	"\t{bus,dev}-testbusy | {bus,dev}-failsuspend | dev-changeonresume |\n"
1400Sstevel@tonic-gate 	"\tdev-promprintf | dev-nolowerpower | bus-teststrict |\n"
1410Sstevel@tonic-gate 	"\tbus-noinvol [/dev/... | /devices/...]\n";
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static struct cmds device_cmds[] = {
1440Sstevel@tonic-gate 	{"online", devctl_device_online},
1450Sstevel@tonic-gate 	{"offline", devctl_device_offline},
1460Sstevel@tonic-gate 	{"remove", devctl_device_remove},
1470Sstevel@tonic-gate 	{"dev-reset", devctl_device_reset},
1480Sstevel@tonic-gate 	{"dev-getstate", dev_getstate},
1490Sstevel@tonic-gate 	{"dev-raisepower", devctl_pm_raisepower},
1500Sstevel@tonic-gate 	{"dev-busycomp", devctl_pm_busycomponent},
1510Sstevel@tonic-gate 	{"dev-idlecomp", devctl_pm_idlecomponent},
1520Sstevel@tonic-gate 	{"dev-testbusy", dev_pm_testbusy},
1530Sstevel@tonic-gate 	{"dev-changepowerlow", devctl_pm_changepowerlow},
1540Sstevel@tonic-gate 	{"dev-changepowerhigh", devctl_pm_changepowerhigh},
1550Sstevel@tonic-gate 	{"dev-failsuspend", devctl_pm_failsuspend},
1560Sstevel@tonic-gate 	{"dev-changeonresume", devctl_pm_device_changeonresume},
1570Sstevel@tonic-gate 	{"dev-promprintf", devctl_pm_device_promprintf},
1580Sstevel@tonic-gate 	{"dev-nolowerpower", devctl_pm_device_no_lower_power},
1590Sstevel@tonic-gate 	{NULL, NULL},
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static struct cmds bus_cmds[] = {
1630Sstevel@tonic-gate 	{"quiesce", devctl_bus_quiesce},
1640Sstevel@tonic-gate 	{"unquiesce", devctl_bus_unquiesce},
1650Sstevel@tonic-gate 	{"bus-reset", devctl_bus_reset},
1660Sstevel@tonic-gate 	{"configure", devctl_bus_configure},
1670Sstevel@tonic-gate 	{"unconfigure", devctl_bus_unconfigure},
1680Sstevel@tonic-gate 	{"bus-getstate", bus_getstate},
1690Sstevel@tonic-gate 	{"bus-devcreate", bus_devcreate},
1700Sstevel@tonic-gate 	{"bus-raisepower", devctl_pm_raisepower},
1710Sstevel@tonic-gate 	{"bus-busycomp", devctl_pm_busycomponent},
1720Sstevel@tonic-gate 	{"bus-idlecomp", devctl_pm_idlecomponent},
1730Sstevel@tonic-gate 	{"bus-changepowerlow", devctl_pm_changepowerlow},
1740Sstevel@tonic-gate 	{"bus-changepowerhigh", devctl_pm_changepowerhigh},
1750Sstevel@tonic-gate 	{"bus-testbusy", dev_pm_testbusy},
1760Sstevel@tonic-gate 	{"bus-failsuspend", devctl_pm_failsuspend},
1770Sstevel@tonic-gate 	{"bus-teststrict", bus_pm_teststrict},
1780Sstevel@tonic-gate 	{"bus-noinvol", devctl_pm_bus_no_invol},
1790Sstevel@tonic-gate 	{NULL, NULL},
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 
184*513Sjongkis int
1850Sstevel@tonic-gate main(int argc, char *argv[])
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	int	c;
1880Sstevel@tonic-gate 	int 	rv;
1890Sstevel@tonic-gate 	int	pathlen;
1900Sstevel@tonic-gate 	struct cmds *dcmd;
1910Sstevel@tonic-gate 	devctl_hdl_t dcp;
1920Sstevel@tonic-gate 	struct stat stat_buf;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	setprogname(argv[0]);
1950Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vd")) != -1)  {
1960Sstevel@tonic-gate 		switch (c)  {
1970Sstevel@tonic-gate 		case 'v':
1980Sstevel@tonic-gate 			++verbose;
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		case 'd':
2010Sstevel@tonic-gate 			++debug;
2020Sstevel@tonic-gate 			(void) putenv("LIBDEVICE_DEBUG");
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate 		default:
2050Sstevel@tonic-gate 			(void) fprintf(stderr, usage, progname);
2060Sstevel@tonic-gate 			exit(1);
2070Sstevel@tonic-gate 			/*NOTREACHED*/
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (optind == argc) {
2120Sstevel@tonic-gate 		(void) fprintf(stderr, usage, progname);
2130Sstevel@tonic-gate 		exit(-1);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	devctl_cmdname = argv[optind++];
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (strcmp(devctl_cmdname, "list") == 0) {
2190Sstevel@tonic-gate 		run_list_ctlrs();
2200Sstevel@tonic-gate 		exit(0);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * any command other than "list" requires a device path
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	if (((optind + 1) > argc)) {
2270Sstevel@tonic-gate 		(void) fprintf(stderr, usage, progname);
2280Sstevel@tonic-gate 		exit(-1);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	orig_path = s_strdup(argv[optind]);
2320Sstevel@tonic-gate 	devctl_device = s_malloc(MAXPATHLEN);
2330Sstevel@tonic-gate 	(void) strcpy(devctl_device, orig_path);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * Additional properties follow for bus-devcreate
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	if ((optind + 1 < argc) &&
2390Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "bus-devcreate") == 0) {
2400Sstevel@tonic-gate 		int i;
2410Sstevel@tonic-gate 		optind++;
2420Sstevel@tonic-gate 		dev_name = s_strdup(argv[optind]);
2430Sstevel@tonic-gate 		i = argc - optind;
2440Sstevel@tonic-gate 		dev_props = s_malloc(i * sizeof (char *));
2450Sstevel@tonic-gate 		while (--i) {
2460Sstevel@tonic-gate 			dev_props[i - 1] = s_strdup(argv[optind + i]);
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * if the device is a logical name, get the physical name
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	if (lstat(orig_path, &stat_buf) == 0) {
2540Sstevel@tonic-gate 		if ((stat_buf.st_mode & S_IFLNK) == S_IFLNK) {
2550Sstevel@tonic-gate 			if ((pathlen = readlink(orig_path, devctl_device,
2560Sstevel@tonic-gate 			    MAXPATHLEN)) == -1)  {
2570Sstevel@tonic-gate 				(void) fprintf(stderr,
2580Sstevel@tonic-gate 					"devctl: readlink(%s) - %s\n",
2590Sstevel@tonic-gate 					orig_path, strerror(errno));
2600Sstevel@tonic-gate 				exit(-1);
2610Sstevel@tonic-gate 			}
2620Sstevel@tonic-gate 			devctl_device[pathlen] = '\0';
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if ((dcmd = dc_cmd(device_cmds, devctl_cmdname)) == NULL) {
2670Sstevel@tonic-gate 		dcmd = dc_cmd(bus_cmds, devctl_cmdname);
2680Sstevel@tonic-gate 		if (dcmd == NULL) {
2690Sstevel@tonic-gate 			(void) fprintf(stderr, "unrecognized command (%s)\n",
2700Sstevel@tonic-gate 			    devctl_cmdname);
2710Sstevel@tonic-gate 			(void) fprintf(stderr, usage, progname);
2720Sstevel@tonic-gate 			exit(1);
2730Sstevel@tonic-gate 		} else if (strcmp(devctl_cmdname, "bus-raisepower") == 0 ||
2740Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-changepowerlow") == 0 ||
2750Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-changepowerhigh") == 0 ||
2760Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-idlecomp") == 0 ||
2770Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-busycomp") == 0 ||
2780Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-testbusy") == 0 ||
2790Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-failsuspend") == 0 ||
2800Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-teststrict") == 0 ||
2810Sstevel@tonic-gate 		    strcmp(devctl_cmdname, "bus-noinvol") == 0) {
2820Sstevel@tonic-gate 			dcp = devctl_pm_bus_acquire(devctl_device, 0);
2830Sstevel@tonic-gate 			if (dcp == NULL) {
2840Sstevel@tonic-gate 				(void) fprintf(stderr,
2850Sstevel@tonic-gate 				    "devctl: device_pm_bus_acquire %s - %s\n",
2860Sstevel@tonic-gate 				    devctl_device, strerror(errno));
2870Sstevel@tonic-gate 				exit(-1);
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 		} else {
2900Sstevel@tonic-gate 			dcp = devctl_bus_acquire(devctl_device, 0);
2910Sstevel@tonic-gate 			if (dcp == NULL) {
2920Sstevel@tonic-gate 				(void) fprintf(stderr, "devctl: bus_acquire "
2930Sstevel@tonic-gate 				    "%s - %s\n",
2940Sstevel@tonic-gate 				    devctl_device, strerror(errno));
2950Sstevel@tonic-gate 				exit(-1);
2960Sstevel@tonic-gate 			}
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 	} else if (strcmp(devctl_cmdname, "dev-raisepower") == 0 ||
2990Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changepowerlow") == 0 ||
3000Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changepowerhigh") == 0 ||
3010Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-idlecomp") == 0 ||
3020Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-busycomp") == 0 ||
3030Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-testbusy") == 0 ||
3040Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-failsuspend") == 0 ||
3050Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-changeonresume") == 0 ||
3060Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-promprintf") == 0 ||
3070Sstevel@tonic-gate 	    strcmp(devctl_cmdname, "dev-nolowerpower") == 0) {
3080Sstevel@tonic-gate 		dcp = devctl_pm_dev_acquire(devctl_device, 0);
3090Sstevel@tonic-gate 		if (dcp == NULL) {
3100Sstevel@tonic-gate 			(void) fprintf(stderr,
3110Sstevel@tonic-gate 				"devctl: device_pm_dev_acquire %s - %s\n",
3120Sstevel@tonic-gate 				devctl_device, strerror(errno));
3130Sstevel@tonic-gate 			exit(-1);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	} else {
3160Sstevel@tonic-gate 		dcp = devctl_device_acquire(devctl_device, 0);
3170Sstevel@tonic-gate 		if (dcp == NULL) {
3180Sstevel@tonic-gate 			(void) fprintf(stderr,
3190Sstevel@tonic-gate 				"devctl: device_acquire %s - %s\n",
3200Sstevel@tonic-gate 				devctl_device, strerror(errno));
3210Sstevel@tonic-gate 			exit(-1);
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (verbose)
3260Sstevel@tonic-gate 		(void) printf("devctl: cmd (%s) device (%s)\n",
3270Sstevel@tonic-gate 		    devctl_cmdname, orig_path);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	(void) fflush(NULL);	/* get output out of the way */
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	rv = (dcmd->cmdf)(dcp);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (rv == -1) {
3340Sstevel@tonic-gate 		perror("devctl");
3350Sstevel@tonic-gate 		exit(-1);
3360Sstevel@tonic-gate 	}
337*513Sjongkis 	return (0);
3380Sstevel@tonic-gate } /* main */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate static int
3410Sstevel@tonic-gate dev_pm_testbusy(devctl_hdl_t dcp)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	int rv;
3440Sstevel@tonic-gate 	uint_t *busyp;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	busyp = s_malloc(sizeof (uint_t));
3470Sstevel@tonic-gate 	rv = devctl_pm_testbusy(dcp, busyp);
3480Sstevel@tonic-gate 	if (rv != -1)
3490Sstevel@tonic-gate 		(void) printf("%s: busy state %d\n", orig_path, *busyp);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	return (0);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate bus_pm_teststrict(devctl_hdl_t dcp)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	int rv;
3580Sstevel@tonic-gate 	uint_t *strict;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	strict = s_malloc(sizeof (uint_t));
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	rv = devctl_pm_bus_teststrict(dcp, strict);
3630Sstevel@tonic-gate 	if (rv != -1)
3640Sstevel@tonic-gate 		(void) printf("%s: strict %d\n", orig_path, *strict);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	return (0);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static int
3700Sstevel@tonic-gate dev_getstate(devctl_hdl_t dcp)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	int rv;
3730Sstevel@tonic-gate 	uint_t state;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	rv = devctl_device_getstate(dcp, &state);
3760Sstevel@tonic-gate 	if (rv != -1)
3770Sstevel@tonic-gate 		print_dev_state(orig_path, state);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate static int
3830Sstevel@tonic-gate bus_getstate(devctl_hdl_t dcp)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	int rv;
3860Sstevel@tonic-gate 	uint_t state;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	rv = devctl_bus_getstate(dcp, &state);
3890Sstevel@tonic-gate 	if (rv != -1)
3900Sstevel@tonic-gate 		print_bus_state(orig_path, state);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Only string property is supported now.
3970Sstevel@tonic-gate  * Will add more later.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate static void
4000Sstevel@tonic-gate add_prop(devctl_ddef_t ddef_hdl, char *prop_str)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	char *pname, *pval, *tmp;
4030Sstevel@tonic-gate 	char **strs = NULL;
4040Sstevel@tonic-gate 	int nstr;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	tmp = strchr(prop_str, '=');
4070Sstevel@tonic-gate 	if (tmp == NULL) {
4080Sstevel@tonic-gate 		(void) fprintf(stderr, "invalid property %s", prop_str);
4090Sstevel@tonic-gate 		exit(-1);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	(void) printf("prop string: %s\n", prop_str);
4130Sstevel@tonic-gate 	pname = prop_str;
4140Sstevel@tonic-gate 	*tmp++ = '\0';
4150Sstevel@tonic-gate 	if (*tmp != '"') {
4160Sstevel@tonic-gate 		(void) devctl_ddef_string(ddef_hdl, pname, tmp);
4170Sstevel@tonic-gate 		return;
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	nstr = 0;
4210Sstevel@tonic-gate 	while (*tmp != '\0') {
4220Sstevel@tonic-gate 		pval = tmp + 1;
4230Sstevel@tonic-gate 		tmp = strchr(pval, '"');
4240Sstevel@tonic-gate 		if (tmp == NULL) {
4250Sstevel@tonic-gate 			(void) fprintf(stderr, "missing quote in %s", tmp);
4260Sstevel@tonic-gate 			exit(-1);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 		nstr++;
4290Sstevel@tonic-gate 		strs = (char **)s_realloc(strs, nstr * sizeof (char *));
4300Sstevel@tonic-gate 		strs[nstr - 1] = pval;
4310Sstevel@tonic-gate 		*tmp++ = '\0';
4320Sstevel@tonic-gate 		(void) printf("string[%d] = %s\n", nstr - 1, pval);
4330Sstevel@tonic-gate 		if (*tmp)
4340Sstevel@tonic-gate 			tmp = strchr(tmp, '"');
4350Sstevel@tonic-gate 		if (tmp == NULL) {
4360Sstevel@tonic-gate 			(void) fprintf(stderr, "string not ending with quote");
4370Sstevel@tonic-gate 			exit(-1);
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	(void) devctl_ddef_string_array(ddef_hdl, pname, nstr, strs);
4420Sstevel@tonic-gate 	free(strs);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static int
4460Sstevel@tonic-gate bus_devcreate(devctl_hdl_t bus_dcp)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	int rv;
4490Sstevel@tonic-gate 	char **propp = dev_props;
4500Sstevel@tonic-gate 	devctl_ddef_t ddef_hdl = NULL;
4510Sstevel@tonic-gate 	devctl_hdl_t dev_hdl = NULL;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	ddef_hdl = devctl_ddef_alloc(dev_name, 0);
4540Sstevel@tonic-gate 	if (dev_props == NULL) {
4550Sstevel@tonic-gate 		(void) fprintf(stderr, "dev-create: missing device props\n");
4560Sstevel@tonic-gate 		return (-1);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	while (*propp) {
4600Sstevel@tonic-gate 		add_prop(ddef_hdl, *propp);
4610Sstevel@tonic-gate 		propp++;
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (devctl_bus_dev_create(bus_dcp, ddef_hdl, 0, &dev_hdl)) {
4650Sstevel@tonic-gate 		(void) fprintf(stderr,
4660Sstevel@tonic-gate 		    "bus-devcreate: failed to create device node\n");
4670Sstevel@tonic-gate 		rv = -1;
4680Sstevel@tonic-gate 	} else if (devctl_get_pathname(dev_hdl, devctl_device, MAXPATHLEN)
4690Sstevel@tonic-gate 	    == NULL) {
4700Sstevel@tonic-gate 		(void) fprintf(stderr,
4710Sstevel@tonic-gate 		    "bus-devcreate: failed to get device path\n");
4720Sstevel@tonic-gate 		rv = -1;
4730Sstevel@tonic-gate 	} else {
4740Sstevel@tonic-gate 		(void) printf("created device %s\n", devctl_device);
4750Sstevel@tonic-gate 		rv = 0;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	devctl_ddef_free(ddef_hdl);
4790Sstevel@tonic-gate 	if (dev_hdl)
4800Sstevel@tonic-gate 		devctl_release(dev_hdl);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	return (rv);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate static void
4860Sstevel@tonic-gate print_bus_state(char *devname, uint_t state)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	(void) printf("\t%s: ", devname);
4890Sstevel@tonic-gate 	if (state == BUS_QUIESCED)
4900Sstevel@tonic-gate 		(void) printf("Quiesced");
4910Sstevel@tonic-gate 	else if (state == BUS_ACTIVE)
4920Sstevel@tonic-gate 		(void) printf("Active");
4930Sstevel@tonic-gate 	else if (state == BUS_SHUTDOWN)
4940Sstevel@tonic-gate 		(void) printf("Shutdown");
4950Sstevel@tonic-gate 	(void) printf("\n");
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate static void
4990Sstevel@tonic-gate print_dev_state(char *devname, uint_t state)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	(void) printf("\t%s: ", devname);
5020Sstevel@tonic-gate 	if (state & DEVICE_ONLINE) {
5030Sstevel@tonic-gate 		(void) printf("Online");
5040Sstevel@tonic-gate 		if (state & DEVICE_BUSY)
5050Sstevel@tonic-gate 			(void) printf(" Busy");
5060Sstevel@tonic-gate 		if (state & DEVICE_DOWN)
5070Sstevel@tonic-gate 			(void) printf(" Down");
5080Sstevel@tonic-gate 	} else {
5090Sstevel@tonic-gate 		if (state & DEVICE_OFFLINE) {
5100Sstevel@tonic-gate 			(void) printf("Offline");
5110Sstevel@tonic-gate 			if (state & DEVICE_DOWN)
5120Sstevel@tonic-gate 				(void) printf(" Down");
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	(void) printf("\n");
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate static void
5190Sstevel@tonic-gate setprogname(char *name)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	register char *p;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (p = strrchr(name, '/'))
5240Sstevel@tonic-gate 		progname = p + 1;
5250Sstevel@tonic-gate 	else
5260Sstevel@tonic-gate 		progname = name;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate static struct cmds *
5300Sstevel@tonic-gate dc_cmd(struct cmds *cmd_tbl, char *devctl_cmdname)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	int i;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	for (i = 0; cmd_tbl[i].cmdname != NULL; i++) {
5350Sstevel@tonic-gate 		if (strcasecmp(cmd_tbl[i].cmdname, devctl_cmdname) == 0)
5360Sstevel@tonic-gate 			return (&cmd_tbl[i]);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	return (NULL);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * list all nexus drivers exporting the :devctl minor device
5440Sstevel@tonic-gate  */
5450Sstevel@tonic-gate static void
5460Sstevel@tonic-gate run_list_ctlrs(void)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	di_node_t dinode;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if ((dinode = di_init("/", DINFOSUBTREE|DINFOMINOR)) == NULL) {
5510Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: di_init() failed\n",
5520Sstevel@tonic-gate 		    progname);
5530Sstevel@tonic-gate 		exit(-1);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	(void) di_walk_minor(dinode, DDI_NT_NEXUS, NULL, 0, &nexif);
5560Sstevel@tonic-gate 	di_fini(dinode);
5570Sstevel@tonic-gate 	exit(0);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate /*ARGSUSED*/
5610Sstevel@tonic-gate static int
5620Sstevel@tonic-gate nexif(di_node_t din, di_minor_t dim, void *arg)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	char *devname;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if ((devname = di_devfs_path(din)) != NULL) {
5670Sstevel@tonic-gate 		(void) printf("%s%d: /devices%s\n", di_driver_name(din),
5680Sstevel@tonic-gate 		    di_instance(din), devname);
5690Sstevel@tonic-gate 		di_devfs_path_free(devname);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate void *
5760Sstevel@tonic-gate s_malloc(size_t len)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	void *buf = malloc(len);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (buf == NULL) {
5810Sstevel@tonic-gate 		perror("s_malloc failed");
5820Sstevel@tonic-gate 		exit(-1);
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	return (buf);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate void *
5880Sstevel@tonic-gate s_realloc(void *ptr, size_t len)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	void *buf = realloc(ptr, len);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (buf == NULL) {
5930Sstevel@tonic-gate 		perror("s_realloc failed");
5940Sstevel@tonic-gate 		exit(-1);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	return (buf);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate char *
6000Sstevel@tonic-gate s_strdup(char *str)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	char *buf = strdup(str);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (buf == NULL) {
6050Sstevel@tonic-gate 		perror("s_malloc failed");
6060Sstevel@tonic-gate 		exit(-1);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	return (buf);
6090Sstevel@tonic-gate }
610