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