xref: /onnv-gate/usr/src/uts/common/io/gen_drv.c (revision 7656:2621e50fdf4a)
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*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.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  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * generic character driver
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/uio.h>
350Sstevel@tonic-gate #include <sys/buf.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/open.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/conf.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/ddi.h>
430Sstevel@tonic-gate #include <sys/sunddi.h>
440Sstevel@tonic-gate #include <sys/sunndi.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	NUMEVENTS 6
480Sstevel@tonic-gate #define	COMPONENTS 2
490Sstevel@tonic-gate #define	COMP_0_MAXPWR	3
500Sstevel@tonic-gate #define	COMP_1_MAXPWR	2
510Sstevel@tonic-gate #define	MINPWR		0
520Sstevel@tonic-gate static int maxpwr[] = { COMP_0_MAXPWR, COMP_1_MAXPWR };
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * The state for each generic device.
560Sstevel@tonic-gate  * NOTE: We save the node_type in the state structure. The node_type string
570Sstevel@tonic-gate  * (and not a copy) is stashed in a minor node by  ddi_create_minor_node(),
580Sstevel@tonic-gate  * so ddi_remove_minor_node() must occur prior to state free.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate typedef struct dstate {
610Sstevel@tonic-gate 	uint_t		flag;
620Sstevel@tonic-gate 	dev_info_t	*dip;			/* my devinfo handle */
630Sstevel@tonic-gate 	char		*node_type;	/* stable node_type copy */
640Sstevel@tonic-gate 	ddi_callback_id_t gen_cb_ids[NUMEVENTS];
650Sstevel@tonic-gate 	kmutex_t	lock;
660Sstevel@tonic-gate 	char		*nodename;
670Sstevel@tonic-gate 	int		level[COMPONENTS];	/* pm level */
680Sstevel@tonic-gate 	int		busy[COMPONENTS];	/* busy state */
690Sstevel@tonic-gate } dstate_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static void *dstates;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int gen_debug = 0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifdef DEBUG
770Sstevel@tonic-gate #define	gen_debug gen_debug_on
780Sstevel@tonic-gate static int gen_debug_on = 0;
790Sstevel@tonic-gate #define	GEN_DEBUG(args) if (gen_debug) cmn_err args
800Sstevel@tonic-gate #else
810Sstevel@tonic-gate #define	GEN_DEBUG(args)
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern void prom_printf(const char *fmt, ...);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int gen_open(dev_t *devp, int flag, int otyp, cred_t *cred);
870Sstevel@tonic-gate static int gen_close(dev_t devp, int flag, int otyp, cred_t *cred);
880Sstevel@tonic-gate static int gen_read(dev_t dev, struct uio *uiop, cred_t *credp);
890Sstevel@tonic-gate static int gen_write(dev_t dev, struct uio *uiop, cred_t *credp);
900Sstevel@tonic-gate static int gen_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
910Sstevel@tonic-gate     cred_t *credp, int *rvalp);
920Sstevel@tonic-gate static int gen_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
930Sstevel@tonic-gate static int gen_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
940Sstevel@tonic-gate static void gen_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
950Sstevel@tonic-gate 	void *arg, void *impl_data);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static int gen_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
980Sstevel@tonic-gate     void **result);
990Sstevel@tonic-gate static int gen_create_minor_nodes(dev_info_t *, struct dstate *);
1000Sstevel@tonic-gate static int gen_power(dev_info_t *, int, int);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static struct cb_ops gen_cb_ops = {
1030Sstevel@tonic-gate 	gen_open,			/* open */
1040Sstevel@tonic-gate 	gen_close,			/* close */
1050Sstevel@tonic-gate 	nodev,				/* strategy */
1060Sstevel@tonic-gate 	nodev,				/* print */
1070Sstevel@tonic-gate 	nodev,				/* dump */
1080Sstevel@tonic-gate 	gen_read,			/* read */
1090Sstevel@tonic-gate 	gen_write,			/* write */
1100Sstevel@tonic-gate 	gen_ioctl,			/* ioctl */
1110Sstevel@tonic-gate 	nodev,				/* devmap */
1120Sstevel@tonic-gate 	nodev,				/* mmap */
1130Sstevel@tonic-gate 	nodev,				/* segmap */
1140Sstevel@tonic-gate 	nochpoll,			/* poll */
1150Sstevel@tonic-gate 	ddi_prop_op,			/* prop_op */
1160Sstevel@tonic-gate 	NULL,				/* streamtab */
1170Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* flag */
1180Sstevel@tonic-gate 	CB_REV,				/* cb_rev */
1190Sstevel@tonic-gate 	nodev,				/* aread */
1200Sstevel@tonic-gate 	nodev				/* awrite */
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static struct dev_ops gen_ops = {
1250Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1260Sstevel@tonic-gate 	0,			/* refcnt */
1270Sstevel@tonic-gate 	gen_info,		/* getinfo */
1280Sstevel@tonic-gate 	nulldev,		/* identify */
1290Sstevel@tonic-gate 	nulldev,		/* probe */
1300Sstevel@tonic-gate 	gen_attach,		/* attach */
1310Sstevel@tonic-gate 	gen_detach,		/* detach */
1320Sstevel@tonic-gate 	nodev,			/* reset */
1330Sstevel@tonic-gate 	&gen_cb_ops,		/* driver ops */
1340Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus ops */
135*7656SSherry.Moore@Sun.COM 	gen_power,		/* power */
136*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * INST_TO_MINOR() gives the starting minor number for a given gen_drv driver
1410Sstevel@tonic-gate  * instance. A shift left by 6 bits allows for each instance to have upto
1420Sstevel@tonic-gate  * 64 (2^6) minor numbers. The maximum minor number allowed by the system
1430Sstevel@tonic-gate  * is L_MAXMIN32 (0x3ffff). This effectively limits the gen_drv instance
1440Sstevel@tonic-gate  * numbers from 0 to 0xfff for a total of 4096 instances.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate #define	INST_TO_MINOR(i)	(i << 6)
1470Sstevel@tonic-gate #define	MINOR_TO_INST(mn)	(mn >> 6)
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static char *mnodetypes[] = {
1500Sstevel@tonic-gate 	"ddi_nt",
1510Sstevel@tonic-gate 	"ddi_nt:device_type",
1520Sstevel@tonic-gate 	"ddi_nt:device_class:bus_class",
1530Sstevel@tonic-gate 	"ddi_nt2",
1540Sstevel@tonic-gate 	"ddi_nt2:device_type",
1550Sstevel@tonic-gate 	"ddi_nt2:device_type:bus_class",
1560Sstevel@tonic-gate };
1570Sstevel@tonic-gate #define	N_NTYPES	(sizeof (mnodetypes) / sizeof (char *))
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static struct modldrv modldrv = {
1600Sstevel@tonic-gate 	&mod_driverops,
161*7656SSherry.Moore@Sun.COM 	"generic test driver",
1620Sstevel@tonic-gate 	&gen_ops
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static struct modlinkage modlinkage = {
1660Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * flags
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate #define	OPEN_FLAG			0x001
1740Sstevel@tonic-gate #define	PWR_HAS_CHANGED_ON_RESUME_FLAG	0x002
1750Sstevel@tonic-gate #define	FAIL_SUSPEND_FLAG		0x004
1760Sstevel@tonic-gate #define	PUP_WITH_PWR_HAS_CHANGED_FLAG	0x008
1770Sstevel@tonic-gate #define	POWER_FLAG			0x010
1780Sstevel@tonic-gate #define	LOWER_POWER_FLAG		0x020
1790Sstevel@tonic-gate #define	NO_INVOL_FLAG			0x040
1800Sstevel@tonic-gate #define	PM_SUPPORTED_FLAG		0x080
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * ioctl commands (non-devctl ioctl commands)
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate #define	GENDRV_IOCTL				('P' << 8)
1860Sstevel@tonic-gate #define	GENDRV_IOFAULT_SIMULATE			(GENDRV_IOCTL | 0)
1870Sstevel@tonic-gate #define	GENDRV_NDI_EVENT_TEST			(GENDRV_IOCTL | 1)
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate int
_init(void)1900Sstevel@tonic-gate _init(void)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	int e;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&dstates,
1950Sstevel@tonic-gate 	    sizeof (struct dstate), 0)) != 0) {
1960Sstevel@tonic-gate 		return (e);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
2000Sstevel@tonic-gate 		ddi_soft_state_fini(&dstates);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	return (e);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int
_fini(void)2070Sstevel@tonic-gate _fini(void)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	int e;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
2120Sstevel@tonic-gate 		return (e);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	ddi_soft_state_fini(&dstates);
2150Sstevel@tonic-gate 	return (e);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2190Sstevel@tonic-gate _info(struct modinfo *modinfop)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static int
gen_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2250Sstevel@tonic-gate gen_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
2280Sstevel@tonic-gate 	struct dstate *dstatep;
2290Sstevel@tonic-gate 	int rval;
2300Sstevel@tonic-gate 	int n_devs;
2310Sstevel@tonic-gate 	int n_minorcomps;
2320Sstevel@tonic-gate 	int isclone;
2330Sstevel@tonic-gate 	ddi_eventcookie_t dev_offline_cookie, dev_reset_cookie;
2340Sstevel@tonic-gate 	ddi_eventcookie_t bus_reset_cookie, bus_quiesce_cookie;
2350Sstevel@tonic-gate 	ddi_eventcookie_t bus_unquiesce_cookie, bus_test_post_cookie;
2360Sstevel@tonic-gate 	int i_init = 0;
2370Sstevel@tonic-gate 	int level_tmp;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	int i;
2400Sstevel@tonic-gate 	char *pm_comp[] = {
2410Sstevel@tonic-gate 		"NAME=leaf0",
2420Sstevel@tonic-gate 		"0=D0",
2430Sstevel@tonic-gate 		"1=D1",
2440Sstevel@tonic-gate 		"2=D2",
2450Sstevel@tonic-gate 		"3=D3",
2460Sstevel@tonic-gate 		"NAME=leaf1",
2470Sstevel@tonic-gate 		"0=off",
2480Sstevel@tonic-gate 		"1=blank",
2490Sstevel@tonic-gate 		"2=on"};
2500Sstevel@tonic-gate 	char *pm_hw_state = {"needs-suspend-resume"};
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	switch (cmd) {
2540Sstevel@tonic-gate 	case DDI_ATTACH:
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(dstates, instance) !=
2570Sstevel@tonic-gate 		    DDI_SUCCESS) {
2580Sstevel@tonic-gate 			cmn_err(CE_CONT, "%s%d: can't allocate state\n",
2590Sstevel@tonic-gate 			    ddi_get_name(devi), instance);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 			return (DDI_FAILURE);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		dstatep = ddi_get_soft_state(dstates, instance);
2650Sstevel@tonic-gate 		dstatep->dip = devi;
2660Sstevel@tonic-gate 		mutex_init(&dstatep->lock, NULL, MUTEX_DRIVER, NULL);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		n_devs = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
2690Sstevel@tonic-gate 		    "ndevs", 1);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		isclone = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
2720Sstevel@tonic-gate 		    "isclone", 0);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		n_minorcomps = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
2750Sstevel@tonic-gate 		    "ncomps", 1);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT,
2780Sstevel@tonic-gate 		    "%s%d attaching: n_devs=%d n_minorcomps=%d isclone=%d",
2790Sstevel@tonic-gate 		    ddi_get_name(devi), ddi_get_instance(devi),
2800Sstevel@tonic-gate 		    n_devs, n_minorcomps, isclone));
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		if (isclone) {
2830Sstevel@tonic-gate 			if (ddi_create_minor_node(devi, "gen", S_IFCHR,
2840Sstevel@tonic-gate 			    INST_TO_MINOR(instance), mnodetypes[0],
2850Sstevel@tonic-gate 			    isclone) != DDI_SUCCESS) {
2860Sstevel@tonic-gate 				ddi_remove_minor_node(devi, NULL);
2870Sstevel@tonic-gate 				ddi_soft_state_free(dstates, instance);
2880Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: can't create minor "
2890Sstevel@tonic-gate 				"node", ddi_get_name(devi), instance);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 				return (DDI_FAILURE);
2920Sstevel@tonic-gate 			}
2930Sstevel@tonic-gate 			rval = DDI_SUCCESS;
2940Sstevel@tonic-gate 		} else {
2950Sstevel@tonic-gate 			rval = gen_create_minor_nodes(devi, dstatep);
2960Sstevel@tonic-gate 			if (rval != DDI_SUCCESS) {
2970Sstevel@tonic-gate 				ddi_prop_remove_all(devi);
2980Sstevel@tonic-gate 				ddi_remove_minor_node(devi, NULL);
2990Sstevel@tonic-gate 				ddi_soft_state_free(dstates, instance);
3000Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: can't create minor "
3010Sstevel@tonic-gate 				"nodes", ddi_get_name(devi), instance);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 				return (DDI_FAILURE);
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_dev_offline",
3080Sstevel@tonic-gate 		    &dev_offline_cookie) == DDI_SUCCESS) {
3090Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi, dev_offline_cookie,
3100Sstevel@tonic-gate 			    gen_event_cb, NULL, &(dstatep->gen_cb_ids[0]));
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_dev_reset",
3140Sstevel@tonic-gate 		    &dev_reset_cookie) == DDI_SUCCESS) {
3150Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi, dev_reset_cookie,
3160Sstevel@tonic-gate 			    gen_event_cb, NULL, &(dstatep->gen_cb_ids[1]));
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_bus_reset",
3200Sstevel@tonic-gate 		    &bus_reset_cookie) == DDI_SUCCESS) {
3210Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi, bus_reset_cookie,
3220Sstevel@tonic-gate 			    gen_event_cb, NULL, &(dstatep->gen_cb_ids[2]));
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_bus_quiesce",
3260Sstevel@tonic-gate 		    &bus_quiesce_cookie) == DDI_SUCCESS) {
3270Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi, bus_quiesce_cookie,
3280Sstevel@tonic-gate 			    gen_event_cb, NULL, &(dstatep->gen_cb_ids[3]));
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_bus_unquiesce",
3320Sstevel@tonic-gate 		    &bus_unquiesce_cookie) == DDI_SUCCESS) {
3330Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi,
3340Sstevel@tonic-gate 			    bus_unquiesce_cookie, gen_event_cb,
3350Sstevel@tonic-gate 			    NULL, &(dstatep->gen_cb_ids[4]));
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		if (ddi_get_eventcookie(devi, "pshot_bus_test_post",
3390Sstevel@tonic-gate 		    &bus_test_post_cookie) == DDI_SUCCESS) {
3400Sstevel@tonic-gate 			(void) ddi_add_event_handler(devi,
3410Sstevel@tonic-gate 			    bus_test_post_cookie, gen_event_cb,
3420Sstevel@tonic-gate 			    NULL, &(dstatep->gen_cb_ids[5]));
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		/*
3460Sstevel@tonic-gate 		 * initialize the devices' pm state
3470Sstevel@tonic-gate 		 */
3480Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
3490Sstevel@tonic-gate 		dstatep->flag &= ~OPEN_FLAG;
3500Sstevel@tonic-gate 		dstatep->flag &= ~PWR_HAS_CHANGED_ON_RESUME_FLAG;
3510Sstevel@tonic-gate 		dstatep->flag &= ~FAIL_SUSPEND_FLAG;
3520Sstevel@tonic-gate 		dstatep->flag &= ~PUP_WITH_PWR_HAS_CHANGED_FLAG;
3530Sstevel@tonic-gate 		dstatep->flag |= LOWER_POWER_FLAG;
3540Sstevel@tonic-gate 		dstatep->flag &= ~NO_INVOL_FLAG;
3550Sstevel@tonic-gate 		dstatep->flag |= PM_SUPPORTED_FLAG;
3560Sstevel@tonic-gate 		dstatep->busy[0] = 0;
3570Sstevel@tonic-gate 		dstatep->busy[1] = 0;
3580Sstevel@tonic-gate 		dstatep->level[0] = -1;
3590Sstevel@tonic-gate 		dstatep->level[1] = -1;
3600Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		/*
3630Sstevel@tonic-gate 		 * stash the nodename
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		dstatep->nodename = ddi_node_name(devi);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		/*
3680Sstevel@tonic-gate 		 * Check if the no-involuntary-power-cycles property
3690Sstevel@tonic-gate 		 * was created. Set NO_INVOL_FLAG if so.
3700Sstevel@tonic-gate 		 */
3710Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
3720Sstevel@tonic-gate 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
3730Sstevel@tonic-gate 		    "no-involuntary-power-cycles") == 1) {
3740Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
3750Sstevel@tonic-gate 			    "%s%d: DDI_ATTACH:\n\tno-involuntary-power-cycles"
3760Sstevel@tonic-gate 			    " property was created",
3770Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi)));
3780Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
3790Sstevel@tonic-gate 			dstatep->flag |= NO_INVOL_FLAG;
3800Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		/*
3840Sstevel@tonic-gate 		 * Check if the dependency-property property
3850Sstevel@tonic-gate 		 * was created.
3860Sstevel@tonic-gate 		 */
3870Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
3880Sstevel@tonic-gate 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
3890Sstevel@tonic-gate 		    "dependency-property") == 1) {
3900Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
3910Sstevel@tonic-gate 			    "%s%d: DDI_ATTACH:\n\tdependency-property"
3920Sstevel@tonic-gate 			    " property was created",
3930Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi)));
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * create the pm-components property. two comps:
3980Sstevel@tonic-gate 		 * 4 levels on comp0, 3 on comp 1.
3990Sstevel@tonic-gate 		 * - skip for a "tape" device, clear PM_SUPPORTED_FLAG
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		if (strcmp(ddi_node_name(devi), "tape") != 0) {
4020Sstevel@tonic-gate 			if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
4030Sstevel@tonic-gate 			    "pm-components", pm_comp, 9) != DDI_PROP_SUCCESS) {
4040Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: %s\n",
4050Sstevel@tonic-gate 				    ddi_node_name(devi),
4060Sstevel@tonic-gate 				    ddi_get_instance(devi),
4070Sstevel@tonic-gate 				    "unable to create \"pm-components\" "
4080Sstevel@tonic-gate 				    " property.");
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 				return (DDI_FAILURE);
4110Sstevel@tonic-gate 			}
4120Sstevel@tonic-gate 		} else {
4130Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
4140Sstevel@tonic-gate 			dstatep->flag &= ~PM_SUPPORTED_FLAG;
4150Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * Check if the pm-components property was created
4200Sstevel@tonic-gate 		 */
4210Sstevel@tonic-gate 		if (dstatep->flag & PM_SUPPORTED_FLAG) {
4220Sstevel@tonic-gate 			if (ddi_prop_exists(DDI_DEV_T_ANY, dstatep->dip,
4230Sstevel@tonic-gate 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
4240Sstevel@tonic-gate 			    "pm-components") != 1) {
4250Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t%s",
4260Sstevel@tonic-gate 				    ddi_node_name(devi),
4270Sstevel@tonic-gate 				    ddi_get_instance(devi),
4280Sstevel@tonic-gate 				    "\"pm-components\" property does"
4290Sstevel@tonic-gate 				    " not exist");
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 				return (DDI_FAILURE);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 			} else {
4340Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT, "%s%d: DDI_ATTACH:"
4350Sstevel@tonic-gate 				    " created pm-components property",
4360Sstevel@tonic-gate 				    ddi_node_name(devi),
4370Sstevel@tonic-gate 				    ddi_get_instance(devi)));
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		/*
4420Sstevel@tonic-gate 		 * create the pm-hardware-state property.
4430Sstevel@tonic-gate 		 * needed to get DDI_SUSPEND and DDI_RESUME calls
4440Sstevel@tonic-gate 		 */
4450Sstevel@tonic-gate 		if (ddi_prop_update_string(DDI_DEV_T_NONE, devi,
4460Sstevel@tonic-gate 		    "pm-hardware-state", pm_hw_state) != DDI_PROP_SUCCESS) {
4470Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: DDI_ATTACH:\n\t%s\n",
4480Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi),
4490Sstevel@tonic-gate 			    "unable to create \"pm-hardware-state\" "
4500Sstevel@tonic-gate 			    " property.");
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 			return (DDI_FAILURE);
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * set power levels to max via pm_raise_power(),
4570Sstevel@tonic-gate 		 */
4580Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
4590Sstevel@tonic-gate 		i_init = (dstatep->flag & PM_SUPPORTED_FLAG) ? 0 : COMPONENTS;
4600Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
4610Sstevel@tonic-gate 		for (i = i_init; i < COMPONENTS; i++) {
4620Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
4630Sstevel@tonic-gate 			    "%s%d: DDI_ATTACH: pm_raise_power comp %d "
4640Sstevel@tonic-gate 			    "to level %d", ddi_node_name(devi),
4650Sstevel@tonic-gate 			    ddi_get_instance(devi), i, maxpwr[i]));
4660Sstevel@tonic-gate 			if (pm_raise_power(dstatep->dip, i, maxpwr[i]) !=
4670Sstevel@tonic-gate 			    DDI_SUCCESS) {
4680Sstevel@tonic-gate 				cmn_err(CE_WARN,
4690Sstevel@tonic-gate 				    "%s%d: DDI_ATTACH: pm_raise_power failed\n",
4700Sstevel@tonic-gate 				    ddi_node_name(devi),
4710Sstevel@tonic-gate 				    ddi_get_instance(devi));
4720Sstevel@tonic-gate 				dstatep->level[i] = -1;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 				return (DDI_FAILURE);
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		if (rval == DDI_SUCCESS) {
4790Sstevel@tonic-gate 			ddi_report_dev(devi);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		return (rval);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	case DDI_RESUME:
4850Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DDI_RESUME", ddi_node_name(devi),
4860Sstevel@tonic-gate 		    ddi_get_instance(devi)));
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		dstatep = ddi_get_soft_state(dstates, ddi_get_instance(devi));
4890Sstevel@tonic-gate 		if (dstatep == NULL) {
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 			return (DDI_FAILURE);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		/*
4950Sstevel@tonic-gate 		 * Call pm_power_has_changed() if flag
4960Sstevel@tonic-gate 		 * PWR_HAS_CHANGED_ON_RESUME_FLAG is set,
4970Sstevel@tonic-gate 		 * then clear the flag
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
5000Sstevel@tonic-gate 		i_init = (dstatep->flag & PM_SUPPORTED_FLAG) ? 0 : COMPONENTS;
5010Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
5020Sstevel@tonic-gate 		if (dstatep->flag & PWR_HAS_CHANGED_ON_RESUME_FLAG) {
5030Sstevel@tonic-gate 			for (i = i_init; i < COMPONENTS; i++) {
5040Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT,
5050Sstevel@tonic-gate 				    "%s%d: DDI_RESUME: pm_power_has_changed "
5060Sstevel@tonic-gate 				    "comp %d to level %d", ddi_node_name(devi),
5070Sstevel@tonic-gate 				    ddi_get_instance(devi), i, maxpwr[i]));
5080Sstevel@tonic-gate 				mutex_enter(&dstatep->lock);
5090Sstevel@tonic-gate 				level_tmp = dstatep->level[i];
5100Sstevel@tonic-gate 				dstatep->level[i] = maxpwr[i];
5110Sstevel@tonic-gate 				if (pm_power_has_changed(dstatep->dip, i,
5120Sstevel@tonic-gate 				    maxpwr[i]) != DDI_SUCCESS) {
5130Sstevel@tonic-gate 					cmn_err(CE_WARN,
5140Sstevel@tonic-gate 					    "%s%d: DDI_RESUME:\n\t"
5150Sstevel@tonic-gate 					    " pm_power_has_changed"
5160Sstevel@tonic-gate 					    " failed: comp %d to level %d\n",
5170Sstevel@tonic-gate 					    ddi_node_name(devi),
5180Sstevel@tonic-gate 					    ddi_get_instance(devi),
5190Sstevel@tonic-gate 					    i, maxpwr[i]);
5200Sstevel@tonic-gate 					dstatep->level[i] = level_tmp;
5210Sstevel@tonic-gate 				}
5220Sstevel@tonic-gate 				mutex_exit(&dstatep->lock);
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		} else {
5250Sstevel@tonic-gate 			/*
5260Sstevel@tonic-gate 			 * Call pm_raise_power() instead
5270Sstevel@tonic-gate 			 */
5280Sstevel@tonic-gate 			for (i = i_init; i < COMPONENTS; i++) {
5290Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT,
5300Sstevel@tonic-gate 				    "%s%d: DDI_RESUME: pm_raise_power"
5310Sstevel@tonic-gate 				    " comp %d to level %d",
5320Sstevel@tonic-gate 				    ddi_node_name(devi), ddi_get_instance(devi),
5330Sstevel@tonic-gate 				    i, maxpwr[i]));
5340Sstevel@tonic-gate 				if (pm_raise_power(dstatep->dip, i, maxpwr[i])
5350Sstevel@tonic-gate 				    != DDI_SUCCESS) {
5360Sstevel@tonic-gate 					cmn_err(CE_WARN,
5370Sstevel@tonic-gate 					    "%s%d: DDI_RESUME:"
5380Sstevel@tonic-gate 					    "\n\tpm_raise_power"
5390Sstevel@tonic-gate 					    "failed: comp %d to level %d\n",
5400Sstevel@tonic-gate 					    ddi_node_name(devi),
5410Sstevel@tonic-gate 					    ddi_get_instance(devi),
5420Sstevel@tonic-gate 					    i, maxpwr[i]);
5430Sstevel@tonic-gate 				}
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		return (DDI_SUCCESS);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	default:
5500Sstevel@tonic-gate 		GEN_DEBUG((CE_WARN, "attach: default"));
5510Sstevel@tonic-gate 		return (DDI_FAILURE);
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate static int
gen_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)5560Sstevel@tonic-gate gen_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	struct dstate *dstatep;
5590Sstevel@tonic-gate 	int instance;
5600Sstevel@tonic-gate 	int i;
5610Sstevel@tonic-gate 	int rv;
5620Sstevel@tonic-gate 	int rm_power;
5630Sstevel@tonic-gate 	int level_tmp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate #ifdef DEBUG
5660Sstevel@tonic-gate 	int n_devs;
5670Sstevel@tonic-gate 	int n_minorcomps;
5680Sstevel@tonic-gate 	int isclone;
5690Sstevel@tonic-gate #endif
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	switch (cmd) {
5720Sstevel@tonic-gate 	case DDI_DETACH:
5730Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DDI_DETACH", ddi_node_name(devi),
5740Sstevel@tonic-gate 		    ddi_get_instance(devi)));
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
5770Sstevel@tonic-gate 		dstatep = ddi_get_soft_state(dstates, instance);
5780Sstevel@tonic-gate 		if (dstatep == NULL) {
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 			return (DDI_FAILURE);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate #ifdef DEBUG
5840Sstevel@tonic-gate 		n_devs = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
5850Sstevel@tonic-gate 		    "ndevs", 1);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		isclone = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
5880Sstevel@tonic-gate 		    "isclone", 0);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		n_minorcomps = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0,
5910Sstevel@tonic-gate 		    "ncomps", 1);
5920Sstevel@tonic-gate #endif /* DEBUG */
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * power off component 1.
5960Sstevel@tonic-gate 		 */
5970Sstevel@tonic-gate 		if (dstatep->flag & PM_SUPPORTED_FLAG) {
5980Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
5990Sstevel@tonic-gate 			    "%s%d: DDI_DETACH: pm_lower_power comp 1 level %d",
6000Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi),
6010Sstevel@tonic-gate 			    MINPWR));
6020Sstevel@tonic-gate 			if (pm_lower_power(dstatep->dip, 1, MINPWR)
6030Sstevel@tonic-gate 			    != DDI_SUCCESS) {
6040Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
6050Sstevel@tonic-gate 				    "pm_lower_power failed for comp 1 to"
6060Sstevel@tonic-gate 				    " level %d\n", ddi_node_name(devi),
6070Sstevel@tonic-gate 				    ddi_get_instance(devi), MINPWR);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 				return (DDI_FAILURE);
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 			/*
6130Sstevel@tonic-gate 			 * check power level. Issue pm_power_has_changed
6140Sstevel@tonic-gate 			 * if not at MINPWR.
6150Sstevel@tonic-gate 			 */
6160Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
6170Sstevel@tonic-gate 			level_tmp = dstatep->level[1];
6180Sstevel@tonic-gate 			dstatep->level[1] = MINPWR;
6190Sstevel@tonic-gate 			if (dstatep->level[1] != MINPWR) {
6200Sstevel@tonic-gate 				GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
6210Sstevel@tonic-gate 				    " power off via pm_power_has_changed"
6220Sstevel@tonic-gate 				    " instead", ddi_node_name(devi),
6230Sstevel@tonic-gate 				    ddi_get_instance(devi)));
6240Sstevel@tonic-gate 				if (pm_power_has_changed(dstatep->dip,
6250Sstevel@tonic-gate 				    1, MINPWR) != DDI_SUCCESS) {
6260Sstevel@tonic-gate 					GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
6270Sstevel@tonic-gate 					    " pm_power_has_changed failed for"
6280Sstevel@tonic-gate 					    " comp 1 to level %d",
6290Sstevel@tonic-gate 					    ddi_node_name(devi),
6300Sstevel@tonic-gate 					    ddi_get_instance(devi),
6310Sstevel@tonic-gate 					    MINPWR));
6320Sstevel@tonic-gate 					dstatep->level[1] = level_tmp;
6330Sstevel@tonic-gate 					mutex_exit(&dstatep->lock);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 					return (DDI_FAILURE);
6360Sstevel@tonic-gate 				}
6370Sstevel@tonic-gate 			}
6380Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * If the LOWER_POWER_FLAG flag is not set,
6430Sstevel@tonic-gate 		 * don't call pm_lowr_power() for comp 0.
6440Sstevel@tonic-gate 		 * This should be used only for the XXXXX@XX,no_invol
6450Sstevel@tonic-gate 		 * devices that export the
6460Sstevel@tonic-gate 		 * no-involuntary-power-cycles property
6470Sstevel@tonic-gate 		 */
6480Sstevel@tonic-gate 		if (!(dstatep->flag & LOWER_POWER_FLAG) &&
6490Sstevel@tonic-gate 		    dstatep->flag & PM_SUPPORTED_FLAG) {
6500Sstevel@tonic-gate 			cmn_err(CE_NOTE, "%s%d: DDI_DETACH:\n\t"
6510Sstevel@tonic-gate 			    " NOT CALLING PM_LOWER_POWER():"
6520Sstevel@tonic-gate 			    " LOWER_POWER_FLAG NOT SET\n",
6530Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi));
6540Sstevel@tonic-gate 		} else if (dstatep->flag & PM_SUPPORTED_FLAG) {
6550Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
6560Sstevel@tonic-gate 			    "%s%d: DDI_DETACH: pm_lower_power comp 0 level %d",
6570Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi),
6580Sstevel@tonic-gate 			    MINPWR));
6590Sstevel@tonic-gate 			if (pm_lower_power(dstatep->dip, 0, MINPWR)
6600Sstevel@tonic-gate 			    != DDI_SUCCESS) {
6610Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: DDI_DETACH:\n\t"
6620Sstevel@tonic-gate 				    "pm_lower_power failed for comp 0 to"
6630Sstevel@tonic-gate 				    " level %d\n", ddi_node_name(devi),
6640Sstevel@tonic-gate 				    ddi_get_instance(devi), MINPWR);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 				return (DDI_FAILURE);
6670Sstevel@tonic-gate 			}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 			/*
6700Sstevel@tonic-gate 			 * check power level. Issue pm_power_has_changed
6710Sstevel@tonic-gate 			 * if not at MINPWR.
6720Sstevel@tonic-gate 			 */
6730Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
6740Sstevel@tonic-gate 			level_tmp = dstatep->level[0];
6750Sstevel@tonic-gate 			dstatep->level[0] = MINPWR;
6760Sstevel@tonic-gate 			if (dstatep->level[0] != MINPWR) {
6770Sstevel@tonic-gate 				GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
6780Sstevel@tonic-gate 				    " power off via pm_power_has_changed"
6790Sstevel@tonic-gate 				    " instead", ddi_node_name(devi),
6800Sstevel@tonic-gate 				    ddi_get_instance(devi)));
6810Sstevel@tonic-gate 				if (pm_power_has_changed(dstatep->dip,
6820Sstevel@tonic-gate 				    0, MINPWR) != DDI_SUCCESS) {
6830Sstevel@tonic-gate 					GEN_DEBUG((CE_NOTE, "%s%d: DDI_DETACH:"
6840Sstevel@tonic-gate 					    " pm_power_has_changed failed for"
6850Sstevel@tonic-gate 					    " comp 0 to level %d",
6860Sstevel@tonic-gate 					    ddi_node_name(devi),
6870Sstevel@tonic-gate 					    ddi_get_instance(devi),
6880Sstevel@tonic-gate 					    MINPWR));
6890Sstevel@tonic-gate 					dstatep->level[0] = level_tmp;
6900Sstevel@tonic-gate 					mutex_exit(&dstatep->lock);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 					return (DDI_FAILURE);
6930Sstevel@tonic-gate 				}
6940Sstevel@tonic-gate 			}
6950Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT,
6990Sstevel@tonic-gate 		    "%s%d detaching: n_devs=%d n_minorcomps=%d isclone=%d",
7000Sstevel@tonic-gate 		    ddi_node_name(devi), ddi_get_instance(devi),
7010Sstevel@tonic-gate 		    n_devs, n_minorcomps, isclone));
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		for (i = 0; i < NUMEVENTS; i++) {
7040Sstevel@tonic-gate 			if (dstatep->gen_cb_ids[i]) {
7050Sstevel@tonic-gate 		(void) ddi_remove_event_handler(dstatep->gen_cb_ids[i]);
7060Sstevel@tonic-gate 				dstatep->gen_cb_ids[i] = NULL;
7070Sstevel@tonic-gate 			}
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		ddi_prop_remove_all(devi);
7110Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
7120Sstevel@tonic-gate 		if (dstatep->node_type)
7130Sstevel@tonic-gate 			kmem_free(dstatep->node_type,
7140Sstevel@tonic-gate 			    strlen(dstatep->node_type) + 1);
7150Sstevel@tonic-gate 		ddi_soft_state_free(dstates, instance);
7160Sstevel@tonic-gate 		return (DDI_SUCCESS);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	case DDI_SUSPEND:
7190Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND",
7200Sstevel@tonic-gate 		    ddi_node_name(devi), ddi_get_instance(devi)));
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
7230Sstevel@tonic-gate 		dstatep = ddi_get_soft_state(dstates, instance);
7240Sstevel@tonic-gate 		if (dstatep == NULL) {
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 			return (DDI_FAILURE);
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		/*
7300Sstevel@tonic-gate 		 * fail the suspend if FAIL_SUSPEND_FLAG is set.
7310Sstevel@tonic-gate 		 * clear the FAIL_SUSPEND_FLAG flag
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
7340Sstevel@tonic-gate 		if (dstatep->flag & FAIL_SUSPEND_FLAG) {
7350Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:"
7360Sstevel@tonic-gate 			    " FAIL_SUSPEND_FLAG is set,"
7370Sstevel@tonic-gate 			    " fail suspend",
7380Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi)));
7390Sstevel@tonic-gate 			dstatep->flag &= ~FAIL_SUSPEND_FLAG;
7400Sstevel@tonic-gate 			rv = DDI_FAILURE;
7410Sstevel@tonic-gate 		} else {
7420Sstevel@tonic-gate 			rv = DDI_SUCCESS;
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * Issue ddi_removing_power() to determine if the suspend
7480Sstevel@tonic-gate 		 * was initiated by either CPR or DR. If CPR, the system
7490Sstevel@tonic-gate 		 * will be powered OFF; if this driver has set the
7500Sstevel@tonic-gate 		 * NO_INVOL_FLAG, then refuse to suspend. If DR, power
7510Sstevel@tonic-gate 		 * will not be removed, thus allow the suspend.
7520Sstevel@tonic-gate 		 */
7530Sstevel@tonic-gate 		if (dstatep->flag & NO_INVOL_FLAG &&
7540Sstevel@tonic-gate 		    dstatep->flag & PM_SUPPORTED_FLAG) {
7550Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:"
7560Sstevel@tonic-gate 			    " check via ddi_removing_power()",
7570Sstevel@tonic-gate 			    ddi_node_name(devi), ddi_get_instance(devi)));
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 			rm_power = ddi_removing_power(dstatep->dip);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 			if (rm_power < 0) {
7620Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: DDI_SUSPEND:"
7630Sstevel@tonic-gate 				    " ddi_removing_power() failed\n",
7640Sstevel@tonic-gate 				    ddi_node_name(devi),
7650Sstevel@tonic-gate 				    ddi_get_instance(devi));
7660Sstevel@tonic-gate 			} else if (rm_power == 1) {
7670Sstevel@tonic-gate 				/*
7680Sstevel@tonic-gate 				 * CPR: power will be removed
7690Sstevel@tonic-gate 				 */
7700Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:\n\t"
7710Sstevel@tonic-gate 				    " CPR: POWER WILL BE REMOVED, THEREFORE"
7720Sstevel@tonic-gate 				    " REFUSE TO SUSPEND", ddi_node_name(devi),
7730Sstevel@tonic-gate 				    ddi_get_instance(devi)));
7740Sstevel@tonic-gate 				rv = DDI_FAILURE;
7750Sstevel@tonic-gate 			} else if (rm_power == 0) {
7760Sstevel@tonic-gate 				/*
7770Sstevel@tonic-gate 				 * DR: power will not be removed
7780Sstevel@tonic-gate 				 */
7790Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT, "%s%d: DDI_SUSPEND:\n\t"
7800Sstevel@tonic-gate 				    " DR: POWER WILL NOT BE REMOVED, THEREFORE"
7810Sstevel@tonic-gate 				    " ALLOW THE SUSPEND", ddi_node_name(devi),
7820Sstevel@tonic-gate 				    ddi_get_instance(devi)));
7830Sstevel@tonic-gate 				rv = DDI_SUCCESS;
7840Sstevel@tonic-gate 			}
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		/*
7880Sstevel@tonic-gate 		 * power OFF via pm_power_has_changed()
7890Sstevel@tonic-gate 		 */
7900Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
7910Sstevel@tonic-gate 		if (dstatep->flag & PM_SUPPORTED_FLAG &&
7920Sstevel@tonic-gate 		    !(dstatep->flag & NO_INVOL_FLAG)) {
7930Sstevel@tonic-gate 			level_tmp = dstatep->level[0];
7940Sstevel@tonic-gate 			dstatep->level[0] = MINPWR;
7950Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT,
7960Sstevel@tonic-gate 			    "%s%d: DDI_SUSPEND: pm_power_has_changed comp 0"
7970Sstevel@tonic-gate 			    " level %d", ddi_node_name(devi),
7980Sstevel@tonic-gate 			    ddi_get_instance(devi), MINPWR));
7990Sstevel@tonic-gate 			if (pm_power_has_changed(dstatep->dip, 0, MINPWR)
8000Sstevel@tonic-gate 			    != DDI_SUCCESS) {
8010Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: DDI_SUSPEND:\n\t"
8020Sstevel@tonic-gate 				    "pm_power_has_changed failed for comp 0 to"
8030Sstevel@tonic-gate 				    " level %d\n", ddi_node_name(devi),
8040Sstevel@tonic-gate 				    ddi_get_instance(devi), MINPWR);
8050Sstevel@tonic-gate 				dstatep->level[0] = level_tmp;
8060Sstevel@tonic-gate 				mutex_exit(&dstatep->lock);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 				return (DDI_FAILURE);
8090Sstevel@tonic-gate 			}
8100Sstevel@tonic-gate 		}
8110Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		return (rv);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	default:
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		return (DDI_FAILURE);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate /* ARGSUSED */
8220Sstevel@tonic-gate static int
gen_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)8230Sstevel@tonic-gate gen_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	dev_t	dev;
8260Sstevel@tonic-gate 	int	instance;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
8290Sstevel@tonic-gate 		return (DDI_FAILURE);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	dev = (dev_t)arg;
8320Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
8330Sstevel@tonic-gate 	*result = (void *)(uintptr_t)instance;
8340Sstevel@tonic-gate 	return (DDI_SUCCESS);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*ARGSUSED*/
8390Sstevel@tonic-gate static int
gen_open(dev_t * devp,int flag,int otyp,cred_t * cred)8400Sstevel@tonic-gate gen_open(dev_t *devp, int flag, int otyp, cred_t *cred)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	minor_t minor;
8430Sstevel@tonic-gate 	struct dstate *dstatep;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
8460Sstevel@tonic-gate 		return (EINVAL);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	minor = getminor(*devp);
8490Sstevel@tonic-gate 	if ((dstatep = ddi_get_soft_state(dstates,
8500Sstevel@tonic-gate 	    MINOR_TO_INST(minor))) == NULL)
8510Sstevel@tonic-gate 		return (ENXIO);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	mutex_enter(&dstatep->lock);
8540Sstevel@tonic-gate 	dstatep->flag |= OPEN_FLAG;
8550Sstevel@tonic-gate 	mutex_exit(&dstatep->lock);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	GEN_DEBUG((CE_CONT,
8580Sstevel@tonic-gate 	    "%s%d open",
8590Sstevel@tonic-gate 	    dstatep->nodename, MINOR_TO_INST(minor)));
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	return (0);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate static int
gen_close(dev_t dev,int flag,int otyp,cred_t * cred)8660Sstevel@tonic-gate gen_close(dev_t dev, int flag, int otyp, cred_t *cred)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	struct dstate *dstatep;
8690Sstevel@tonic-gate 	minor_t minor = getminor(dev);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
8720Sstevel@tonic-gate 		return (EINVAL);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if (dstatep == NULL)
8770Sstevel@tonic-gate 		return (ENXIO);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	mutex_enter(&dstatep->lock);
8800Sstevel@tonic-gate 	dstatep->flag &= ~OPEN_FLAG;
8810Sstevel@tonic-gate 	mutex_exit(&dstatep->lock);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	GEN_DEBUG((CE_CONT,
8840Sstevel@tonic-gate 	    "%s%d close",
8850Sstevel@tonic-gate 	    dstatep->nodename, MINOR_TO_INST(minor)));
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	return (0);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*ARGSUSED*/
8910Sstevel@tonic-gate static int
gen_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)8920Sstevel@tonic-gate gen_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
8930Sstevel@tonic-gate {
8940Sstevel@tonic-gate 	struct dstate *dstatep;
8950Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
8960Sstevel@tonic-gate 	int instance;
8970Sstevel@tonic-gate 	int rval = 0;
8980Sstevel@tonic-gate 	char *nodename;
8990Sstevel@tonic-gate 	int i;
9000Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
9010Sstevel@tonic-gate 	uint_t state;
9020Sstevel@tonic-gate 	int ret;
9030Sstevel@tonic-gate 	int level_tmp;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
9060Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
9070Sstevel@tonic-gate 	nodename = dstatep->nodename;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (dstatep == NULL)
9100Sstevel@tonic-gate 		return (ENXIO);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * read devctl ioctl data
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
9160Sstevel@tonic-gate 		return (EFAULT);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	switch (cmd) {
9190Sstevel@tonic-gate 	case GENDRV_IOFAULT_SIMULATE:
9200Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, DDI_DEVI_FAULT_EVENT,
921*7656SSherry.Moore@Sun.COM 		    &(cookie)) != NDI_SUCCESS)
9220Sstevel@tonic-gate 			return (DDI_FAILURE);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		return (ndi_post_event(dstatep->dip, dstatep->dip, cookie,
925*7656SSherry.Moore@Sun.COM 		    NULL));
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	case GENDRV_NDI_EVENT_TEST:
9280Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_dev_offline",
9290Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9300Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9310Sstevel@tonic-gate 			    cookie, NULL);
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_dev_reset",
9350Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9360Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9370Sstevel@tonic-gate 			    cookie, NULL);
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_reset",
9410Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9420Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9430Sstevel@tonic-gate 			    cookie, NULL);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_quiesce",
9470Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9480Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9490Sstevel@tonic-gate 			    cookie, NULL);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_unquiesce",
9530Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9540Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9550Sstevel@tonic-gate 			    cookie, NULL);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		if (ddi_get_eventcookie(dstatep->dip, "pshot_bus_test_post",
9590Sstevel@tonic-gate 		    &cookie) == NDI_SUCCESS) {
9600Sstevel@tonic-gate 			(void) ndi_post_event(dstatep->dip, dstatep->dip,
9610Sstevel@tonic-gate 			    cookie, NULL);
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		break;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	case DEVCTL_PM_PWR_HAS_CHANGED_ON_RESUME:
9670Sstevel@tonic-gate 		/*
9680Sstevel@tonic-gate 		 * Issue pm_power_has_changed() call on DDI_RESUME
9690Sstevel@tonic-gate 		 */
9700Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
9710Sstevel@tonic-gate 		dstatep->flag |= PWR_HAS_CHANGED_ON_RESUME_FLAG;
9720Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
9730Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d:"
9740Sstevel@tonic-gate 		    " DEVCTL_PM_PWR_HAS_CHANGED_ON_RESUME", nodename,
9750Sstevel@tonic-gate 		    instance));
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		break;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	case DEVCTL_PM_FAIL_SUSPEND:
9800Sstevel@tonic-gate 		/*
9810Sstevel@tonic-gate 		 * Fail the suspend attempt in DDI_SUSPEND
9820Sstevel@tonic-gate 		 */
9830Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
9840Sstevel@tonic-gate 		dstatep->flag |= FAIL_SUSPEND_FLAG;
9850Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
9860Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_FAIL_SUSPEND",
9870Sstevel@tonic-gate 		    nodename, instance));
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		break;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	case DEVCTL_PM_PUP_WITH_PWR_HAS_CHANGED:
9920Sstevel@tonic-gate 		/*
9930Sstevel@tonic-gate 		 * Use pm_power_has_changed() to power up comp 0 when
9940Sstevel@tonic-gate 		 * enforcing the comp 0 vs comp-not 0 dependency:
9950Sstevel@tonic-gate 		 * Power up comp 0 first, if request for comp-not-0
9960Sstevel@tonic-gate 		 * comes in.
9970Sstevel@tonic-gate 		 * Else, default to pm_raise_power().
9980Sstevel@tonic-gate 		 */
9990Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
10000Sstevel@tonic-gate 		dstatep->flag |= PUP_WITH_PWR_HAS_CHANGED_FLAG;
10010Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
10020Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_PUP_WITH_PWR_HAS_CHANGED",
10030Sstevel@tonic-gate 		    nodename, instance));
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		break;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	case DEVCTL_PM_BUSY_COMP:
10080Sstevel@tonic-gate 		/*
10090Sstevel@tonic-gate 		 * mark component 0 busy via a pm_busy_component() call.
10100Sstevel@tonic-gate 		 * update the busy[] array.
10110Sstevel@tonic-gate 		 */
10120Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
10130Sstevel@tonic-gate 		++dstatep->busy[0];
10140Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_BUSY_COMP: comp 0:"
10150Sstevel@tonic-gate 		    " busy=%d", nodename, instance, dstatep->busy[0]));
10160Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
10170Sstevel@tonic-gate 		ret = pm_busy_component(dstatep->dip, 0);
10180Sstevel@tonic-gate 		ASSERT(ret == DDI_SUCCESS);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		break;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	case DEVCTL_PM_BUSY_COMP_TEST:
10230Sstevel@tonic-gate 		/*
10240Sstevel@tonic-gate 		 * test busy state on component 0
10250Sstevel@tonic-gate 		 */
10260Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
10270Sstevel@tonic-gate 		state = dstatep->busy[0];
10280Sstevel@tonic-gate 		if (copyout(&state, dcp->cpyout_buf,
10290Sstevel@tonic-gate 		    sizeof (uint_t)) != 0) {
10300Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d:"
10310Sstevel@tonic-gate 			    " DEVCTL_PM_BUSY_COMP_TEST: copyout failed\n",
10320Sstevel@tonic-gate 			    nodename, instance);
10330Sstevel@tonic-gate 			rval = EINVAL;
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_BUSY_COMP_TEST:"
10360Sstevel@tonic-gate 		    " comp 0 busy %d",
10370Sstevel@tonic-gate 		    nodename, instance, state));
10380Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 		break;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	case DEVCTL_PM_IDLE_COMP:
10430Sstevel@tonic-gate 		/*
10440Sstevel@tonic-gate 		 * mark component 0 idle via a pm_idle_component() call.
10450Sstevel@tonic-gate 		 * NOP if dstatep->busy[0] == 0.
10460Sstevel@tonic-gate 		 */
10470Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
10480Sstevel@tonic-gate 		if (dstatep->busy[0] > 0) {
10490Sstevel@tonic-gate 			--dstatep->busy[0];
10500Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_IDLE_COMP:"
10510Sstevel@tonic-gate 			    " comp 0: busy=%d", nodename, instance,
10520Sstevel@tonic-gate 			    dstatep->busy[0]));
10530Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
10540Sstevel@tonic-gate 			ret = pm_idle_component(dstatep->dip, 0);
10550Sstevel@tonic-gate 			ASSERT(ret == DDI_SUCCESS);
10560Sstevel@tonic-gate 		} else {
10570Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		break;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	case DEVCTL_PM_PROM_PRINTF:
10630Sstevel@tonic-gate 		(void) prom_printf("%s%d: PROM_PRINTF FROM GEN_DRV\n",
10640Sstevel@tonic-gate 		    nodename, instance);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 		break;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	case DEVCTL_PM_RAISE_PWR:
10690Sstevel@tonic-gate 		/*
10700Sstevel@tonic-gate 		 * power up both components to MAXPWR via
10710Sstevel@tonic-gate 		 * pm_raise_power() calls. this ioctl() cmd
10720Sstevel@tonic-gate 		 * assumes that the current level is 0
10730Sstevel@tonic-gate 		 */
10740Sstevel@tonic-gate 		for (i = 0; i < COMPONENTS; i++) {
10750Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_RAISE_PWR:"
10760Sstevel@tonic-gate 			    " comp %d old 0 new %d",
10770Sstevel@tonic-gate 			    nodename, instance, i, maxpwr[i]));
10780Sstevel@tonic-gate 			if (pm_raise_power(dstatep->dip, 0, maxpwr[i])
10790Sstevel@tonic-gate 			    != DDI_SUCCESS) {
10800Sstevel@tonic-gate 				rval = EINVAL;
10810Sstevel@tonic-gate 			}
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		break;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	case DEVCTL_PM_CHANGE_PWR_LOW:
10870Sstevel@tonic-gate 		/*
10880Sstevel@tonic-gate 		 * power off both components via pm_power_has_changed() calls
10890Sstevel@tonic-gate 		 */
10900Sstevel@tonic-gate 		for (i = (COMPONENTS - 1); i >= 0; --i) {
10910Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_CHANGE_PWR_LOW:"
10920Sstevel@tonic-gate 			    " comp %d new 0",
10930Sstevel@tonic-gate 			    nodename, instance, i));
10940Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
10950Sstevel@tonic-gate 			level_tmp = dstatep->level[i];
10960Sstevel@tonic-gate 			dstatep->level[i] = 0;
10970Sstevel@tonic-gate 			if (pm_power_has_changed(dstatep->dip, i, 0)
10980Sstevel@tonic-gate 			    != DDI_SUCCESS) {
10990Sstevel@tonic-gate 				dstatep->level[i] = level_tmp;
11000Sstevel@tonic-gate 				rval = EINVAL;
11010Sstevel@tonic-gate 			}
11020Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
11030Sstevel@tonic-gate 		}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		break;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	case DEVCTL_PM_CHANGE_PWR_HIGH:
11080Sstevel@tonic-gate 		/*
11090Sstevel@tonic-gate 		 * power up both components to MAXPWR via
11100Sstevel@tonic-gate 		 * pm_power_has_changed() calls
11110Sstevel@tonic-gate 		 */
11120Sstevel@tonic-gate 		for (i = 0; i < COMPONENTS; i++) {
11130Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_CHANGE_PWR_HIGH:"
11140Sstevel@tonic-gate 			    " comp %d new %d",
11150Sstevel@tonic-gate 			    nodename, instance, i, maxpwr[i]));
11160Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
11170Sstevel@tonic-gate 			level_tmp = dstatep->level[i];
11180Sstevel@tonic-gate 			dstatep->level[i] = maxpwr[i];
11190Sstevel@tonic-gate 			if (pm_power_has_changed(dstatep->dip, i, maxpwr[i])
11200Sstevel@tonic-gate 			    != DDI_SUCCESS) {
11210Sstevel@tonic-gate 				dstatep->level[i] = level_tmp;
11220Sstevel@tonic-gate 				rval = EINVAL;
11230Sstevel@tonic-gate 			}
11240Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		break;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	case DEVCTL_PM_POWER:
11300Sstevel@tonic-gate 		/*
11310Sstevel@tonic-gate 		 * test if the gen_drv_power() routine has been called,
11320Sstevel@tonic-gate 		 * then clear
11330Sstevel@tonic-gate 		 */
11340Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
11350Sstevel@tonic-gate 		state = (dstatep->flag & POWER_FLAG) ? 1 : 0;
11360Sstevel@tonic-gate 		if (copyout(&state, dcp->cpyout_buf,
11370Sstevel@tonic-gate 		    sizeof (uint_t)) != 0) {
11380Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: DEVCTL_PM_POWER:"
11390Sstevel@tonic-gate 			    " copyout failed\n", nodename, instance);
11400Sstevel@tonic-gate 			rval = EINVAL;
11410Sstevel@tonic-gate 		}
11420Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: %s POWER_FLAG: %d",
11430Sstevel@tonic-gate 		    nodename, instance, "DEVCTL_PM_POWER", state));
11440Sstevel@tonic-gate 		dstatep->flag &= ~POWER_FLAG;
11450Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
11460Sstevel@tonic-gate 		break;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	case DEVCTL_PM_NO_LOWER_POWER:
11490Sstevel@tonic-gate 		/*
11500Sstevel@tonic-gate 		 * issue to not invoke pm_lower_power() on detach
11510Sstevel@tonic-gate 		 */
11520Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
11530Sstevel@tonic-gate 		dstatep->flag &= ~LOWER_POWER_FLAG;
11540Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
11550Sstevel@tonic-gate 		GEN_DEBUG((CE_CONT, "%s%d: DEVCTL_PM_NO_LOWER_POWER",
1156*7656SSherry.Moore@Sun.COM 		    nodename, instance));
11570Sstevel@tonic-gate 		break;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	default:
11600Sstevel@tonic-gate 		return (ENOTTY);
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	return (rval);
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate /*ARGSUSED*/
11670Sstevel@tonic-gate static int
gen_read(dev_t dev,struct uio * uiop,cred_t * credp)11680Sstevel@tonic-gate gen_read(dev_t dev, struct uio *uiop, cred_t *credp)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate 	return (0);
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate /*ARGSUSED*/
11740Sstevel@tonic-gate static int
gen_write(dev_t dev,struct uio * uiop,cred_t * credp)11750Sstevel@tonic-gate gen_write(dev_t dev, struct uio *uiop, cred_t *credp)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	return (0);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate /*ARGSUSED0*/
11810Sstevel@tonic-gate static int
gen_power(dev_info_t * dip,int cmpt,int level)11820Sstevel@tonic-gate gen_power(dev_info_t *dip, int cmpt, int level)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	struct dstate *dstatep;
11850Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
11860Sstevel@tonic-gate 	char *nodename = ddi_node_name(dip);
11870Sstevel@tonic-gate 	int level_tmp;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	GEN_DEBUG((CE_CONT, "%s%d: power: cmpt %d to level %d",
11900Sstevel@tonic-gate 	    nodename, instance, cmpt, level));
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
11930Sstevel@tonic-gate 	if (dstatep == NULL) {
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		return (DDI_FAILURE);
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	/*
11990Sstevel@tonic-gate 	 * Keep track of the power levels for both components
12000Sstevel@tonic-gate 	 * in the dstatep->comp[] array.
12010Sstevel@tonic-gate 	 * Set comp 0 to full level if non-zero comps
12020Sstevel@tonic-gate 	 * are being set to a higher, non-zero level.
12030Sstevel@tonic-gate 	 */
12040Sstevel@tonic-gate 	if (cmpt == 0) {
12050Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
12060Sstevel@tonic-gate 		dstatep->level[cmpt] = level;
12070Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
12080Sstevel@tonic-gate 	} else if (level > dstatep->level[cmpt] && level != 0 &&
12090Sstevel@tonic-gate 	    dstatep->level[0] != COMP_0_MAXPWR) {
12100Sstevel@tonic-gate 		/*
12110Sstevel@tonic-gate 		 * If component 0 is not at COMP_0_MAXPWR, and component 1
12120Sstevel@tonic-gate 		 * is being powered ON, invoke pm_raise_power() or
12130Sstevel@tonic-gate 		 * pm_power_has_changed() based on the
12140Sstevel@tonic-gate 		 * PUP_WITH_PWR_HAS_CHANGED_FLAG flag.
12150Sstevel@tonic-gate 		 * PUP_WITH_PWR_HAS_CHANGED_FLAG = FALSE by default, invoking
12160Sstevel@tonic-gate 		 * pm_raise_power().
12170Sstevel@tonic-gate 		 */
12180Sstevel@tonic-gate 		if (!(dstatep->flag & PUP_WITH_PWR_HAS_CHANGED_FLAG)) {
12190Sstevel@tonic-gate 			/*
12200Sstevel@tonic-gate 			 * first set comp 0 to level COMP_0_MAXPWR
12210Sstevel@tonic-gate 			 */
12220Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: power:  "
12230Sstevel@tonic-gate 			    "pm_raise_power: comp 0 to level %d",
12240Sstevel@tonic-gate 			    nodename, instance, COMP_0_MAXPWR));
12250Sstevel@tonic-gate 			if (pm_raise_power(dip, 0, COMP_0_MAXPWR) !=
12260Sstevel@tonic-gate 			    DDI_SUCCESS) {
12270Sstevel@tonic-gate 				cmn_err(CE_WARN,
12280Sstevel@tonic-gate 				    "%s%d: power: pm_raise_power() "
12290Sstevel@tonic-gate 				    "failed: comp 0 to level %d\n",
12300Sstevel@tonic-gate 				    nodename, instance, COMP_0_MAXPWR);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 				return (DDI_FAILURE);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 			} else {
12350Sstevel@tonic-gate 				mutex_enter(&dstatep->lock);
12360Sstevel@tonic-gate 				dstatep->level[0] = COMP_0_MAXPWR;
12370Sstevel@tonic-gate 				/*
12380Sstevel@tonic-gate 				 * now set the level on the non-zero comp
12390Sstevel@tonic-gate 				 */
12400Sstevel@tonic-gate 				dstatep->level[cmpt] = level;
12410Sstevel@tonic-gate 				mutex_exit(&dstatep->lock);
12420Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT, "%s%d: power: "
12430Sstevel@tonic-gate 				    "comp %d to level %d",
12440Sstevel@tonic-gate 				    nodename, instance, cmpt, level));
12450Sstevel@tonic-gate 			}
12460Sstevel@tonic-gate 		} else {
12470Sstevel@tonic-gate 			GEN_DEBUG((CE_CONT, "%s%d: power: "
12480Sstevel@tonic-gate 			    "pm_power_has_changed: comp 0 to level %d",
12490Sstevel@tonic-gate 			    nodename, instance, COMP_0_MAXPWR));
12500Sstevel@tonic-gate 			mutex_enter(&dstatep->lock);
12510Sstevel@tonic-gate 			level_tmp = dstatep->level[0];
12520Sstevel@tonic-gate 			dstatep->level[0] = COMP_0_MAXPWR;
12530Sstevel@tonic-gate 			if (pm_power_has_changed(dip, 0, COMP_0_MAXPWR) !=
12540Sstevel@tonic-gate 			    DDI_SUCCESS) {
12550Sstevel@tonic-gate 				cmn_err(CE_WARN,
12560Sstevel@tonic-gate 				    "%s%d: power: pm_power_has_changed() "
12570Sstevel@tonic-gate 				    "failed: comp 0 to level %d\n",
12580Sstevel@tonic-gate 				    nodename, instance, COMP_0_MAXPWR);
12590Sstevel@tonic-gate 				dstatep->level[0] = level_tmp;
12600Sstevel@tonic-gate 			} else {
12610Sstevel@tonic-gate 				/*
12620Sstevel@tonic-gate 				 * now set the level on the non-zero comp
12630Sstevel@tonic-gate 				 */
12640Sstevel@tonic-gate 				GEN_DEBUG((CE_CONT, "%s%d: power:"
12650Sstevel@tonic-gate 				    " pm_power_has_changed: comp %d"
12660Sstevel@tonic-gate 				    " to level %d", nodename, instance,
12670Sstevel@tonic-gate 				    cmpt, level));
12680Sstevel@tonic-gate 				dstatep->level[cmpt] = level;
12690Sstevel@tonic-gate 			}
12700Sstevel@tonic-gate 			mutex_exit(&dstatep->lock);
12710Sstevel@tonic-gate 		}
12720Sstevel@tonic-gate 	} else {
12730Sstevel@tonic-gate 		mutex_enter(&dstatep->lock);
12740Sstevel@tonic-gate 		dstatep->level[cmpt] = level;
12750Sstevel@tonic-gate 		mutex_exit(&dstatep->lock);
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (DDI_SUCCESS);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * Create properties of various data types for testing devfs events.
12840Sstevel@tonic-gate  */
12850Sstevel@tonic-gate static int
gen_create_properties(dev_info_t * devi)12860Sstevel@tonic-gate gen_create_properties(dev_info_t *devi)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	int int_val = 3023;
12890Sstevel@tonic-gate 	int int_array[] = { 3, 10, 304, 230, 4};
12900Sstevel@tonic-gate 	int64_t int64_val = 20;
12910Sstevel@tonic-gate 	int64_t int64_array[] = { 12, 24, 36, 48};
12920Sstevel@tonic-gate 	char *string_val = "Dev_node_prop";
12930Sstevel@tonic-gate 	char *string_array[] = {"Dev_node_prop:0",
12940Sstevel@tonic-gate 	    "Dev_node_prop:1", "Dev_node_prop:2", "Dev_node_prop:3"};
12950Sstevel@tonic-gate 	uchar_t byte_array[] = { (uchar_t)0xaa, (uchar_t)0x55,
12960Sstevel@tonic-gate 	    (uchar_t)0x12, (uchar_t)0xcd };
12970Sstevel@tonic-gate 	char bytes[] = { (char)0x00, (char)0xef, (char)0xff };
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (ddi_prop_update_int(DDI_DEV_T_NONE, devi, "int", int_val)
13000Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
13010Sstevel@tonic-gate 		return (DDI_FAILURE);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	if (ddi_prop_update_int_array(DDI_DEV_T_NONE, devi, "int-array",
13040Sstevel@tonic-gate 	    int_array, sizeof (int_array) / sizeof (int)) != DDI_PROP_SUCCESS)
13050Sstevel@tonic-gate 		return (DDI_FAILURE);
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if (ddi_prop_update_int64(DDI_DEV_T_NONE, devi, "int64", int64_val)
13080Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
13090Sstevel@tonic-gate 		return (DDI_FAILURE);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	if (ddi_prop_update_int64_array(DDI_DEV_T_NONE, devi, "int64-array",
13120Sstevel@tonic-gate 	    int64_array, sizeof (int64_array) / sizeof (int64_t))
13130Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
13140Sstevel@tonic-gate 		return (DDI_FAILURE);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	if (ddi_prop_update_string(DDI_DEV_T_NONE, devi, "string", string_val)
13170Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
13180Sstevel@tonic-gate 		return (DDI_FAILURE);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, "string-array",
13210Sstevel@tonic-gate 	    string_array, sizeof (string_array) / sizeof (char *))
13220Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
13230Sstevel@tonic-gate 		return (DDI_FAILURE);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
13260Sstevel@tonic-gate 	    "boolean", NULL, 0) != DDI_PROP_SUCCESS)
13270Sstevel@tonic-gate 		return (DDI_FAILURE);
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if (ddi_prop_update_byte_array(DDI_DEV_T_NONE, devi, "byte-array",
13300Sstevel@tonic-gate 	    byte_array, sizeof (byte_array)) != DDI_PROP_SUCCESS)
13310Sstevel@tonic-gate 		return (DDI_FAILURE);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/* untyped property */
13340Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, "untyped",
13350Sstevel@tonic-gate 	    (caddr_t)bytes, sizeof (bytes)) != DDI_PROP_SUCCESS)
13360Sstevel@tonic-gate 		return (DDI_FAILURE);
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	return (DDI_SUCCESS);
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate static struct driver_minor_data {
13420Sstevel@tonic-gate 	char	*name;
13430Sstevel@tonic-gate 	minor_t	minor;
13440Sstevel@tonic-gate 	int	type;
13450Sstevel@tonic-gate } disk_minor_data[] = {
13460Sstevel@tonic-gate 	{"a", 0, S_IFBLK},
13470Sstevel@tonic-gate 	{"b", 1, S_IFBLK},
13480Sstevel@tonic-gate 	{"c", 2, S_IFBLK},
13490Sstevel@tonic-gate 	{"d", 3, S_IFBLK},
13500Sstevel@tonic-gate 	{"e", 4, S_IFBLK},
13510Sstevel@tonic-gate 	{"f", 5, S_IFBLK},
13520Sstevel@tonic-gate 	{"g", 6, S_IFBLK},
13530Sstevel@tonic-gate 	{"h", 7, S_IFBLK},
13540Sstevel@tonic-gate 	{"a,raw", 0, S_IFCHR},
13550Sstevel@tonic-gate 	{"b,raw", 1, S_IFCHR},
13560Sstevel@tonic-gate 	{"c,raw", 2, S_IFCHR},
13570Sstevel@tonic-gate 	{"d,raw", 3, S_IFCHR},
13580Sstevel@tonic-gate 	{"e,raw", 4, S_IFCHR},
13590Sstevel@tonic-gate 	{"f,raw", 5, S_IFCHR},
13600Sstevel@tonic-gate 	{"g,raw", 6, S_IFCHR},
13610Sstevel@tonic-gate 	{"h,raw", 7, S_IFCHR},
13620Sstevel@tonic-gate 	{0}
13630Sstevel@tonic-gate };
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate static struct driver_serial_minor_data {
13670Sstevel@tonic-gate 	char	*name;
13680Sstevel@tonic-gate 	minor_t minor;
13690Sstevel@tonic-gate 	int	type;
13700Sstevel@tonic-gate 	char	*node_type;
13710Sstevel@tonic-gate }  serial_minor_data[] = {
13720Sstevel@tonic-gate 	{"0", 0, S_IFCHR, "ddi_serial"},
13730Sstevel@tonic-gate 	{"1", 1, S_IFCHR, "ddi_serial"},
13740Sstevel@tonic-gate 	{"0,cu", 2, S_IFCHR, "ddi_serial:dialout"},
13750Sstevel@tonic-gate 	{"1,cu", 3, S_IFCHR, "ddi_serial:dialout"},
13760Sstevel@tonic-gate 	{0}
13770Sstevel@tonic-gate };
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate static int
gen_create_display(dev_info_t * devi)13810Sstevel@tonic-gate gen_create_display(dev_info_t *devi)
13820Sstevel@tonic-gate {
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
13850Sstevel@tonic-gate 	char minor_name[15];
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	(void) sprintf(minor_name, "cgtwenty%d", instance);
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, minor_name, S_IFCHR,
13900Sstevel@tonic-gate 	    INST_TO_MINOR(instance), DDI_NT_DISPLAY, NULL));
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate static int
gen_create_mn_disk_chan(dev_info_t * devi)13940Sstevel@tonic-gate gen_create_mn_disk_chan(dev_info_t *devi)
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate 	struct driver_minor_data *dmdp;
13970Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (gen_create_properties(devi) != DDI_SUCCESS)
14000Sstevel@tonic-gate 		return (DDI_FAILURE);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
14030Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
14040Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)) | dmdp->minor,
14050Sstevel@tonic-gate 		    DDI_NT_BLOCK_CHAN, NULL) != DDI_SUCCESS) {
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 			return (DDI_FAILURE);
14080Sstevel@tonic-gate 		}
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 	return (DDI_SUCCESS);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate static uint_t
atod(char * s)14140Sstevel@tonic-gate atod(char *s)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	uint_t val = 0;
14170Sstevel@tonic-gate 	uint_t digit;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	while (*s) {
14200Sstevel@tonic-gate 		if (*s >= '0' && *s <= '9')
14210Sstevel@tonic-gate 			digit = *s++ - '0';
14220Sstevel@tonic-gate 		else
14230Sstevel@tonic-gate 			break;
14240Sstevel@tonic-gate 		val = (val * 10) + digit;
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 	return (val);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate static int
gen_create_mn_disk_wwn(dev_info_t * devi)14310Sstevel@tonic-gate gen_create_mn_disk_wwn(dev_info_t *devi)
14320Sstevel@tonic-gate {
14330Sstevel@tonic-gate 	struct driver_minor_data *dmdp;
14340Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
14350Sstevel@tonic-gate 	char *address = ddi_get_name_addr(devi);
14360Sstevel@tonic-gate 	int target, lun;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (address[0] >= '0' && address[0] <= '9' &&
1439*7656SSherry.Moore@Sun.COM 	    strchr(address, ',')) {
14400Sstevel@tonic-gate 		target = atod(address);
14410Sstevel@tonic-gate 		address = strchr(address, ',');
14420Sstevel@tonic-gate 		lun = atod(++address);
14430Sstevel@tonic-gate 	} else { /* this hack is for rm_stale_link() testing */
14440Sstevel@tonic-gate 		target = 10;
14450Sstevel@tonic-gate 		lun = 5;
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
14490Sstevel@tonic-gate 	    "target", (caddr_t)&target, sizeof (int))
14500Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
14510Sstevel@tonic-gate 		return (DDI_FAILURE);
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
14540Sstevel@tonic-gate 	    "lun", (caddr_t)&lun, sizeof (int))
14550Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
14560Sstevel@tonic-gate 		return (DDI_FAILURE);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
14600Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
14610Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)) | dmdp->minor,
14620Sstevel@tonic-gate 		    DDI_NT_BLOCK_WWN, NULL) != DDI_SUCCESS) {
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 			return (DDI_FAILURE);
14650Sstevel@tonic-gate 		}
14660Sstevel@tonic-gate 	}
14670Sstevel@tonic-gate 	return (DDI_SUCCESS);
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate static int
gen_create_mn_disk_cdrom(dev_info_t * devi)14710Sstevel@tonic-gate gen_create_mn_disk_cdrom(dev_info_t *devi)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate 	struct driver_minor_data *dmdp;
14740Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
14770Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
14780Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)) | dmdp->minor,
14790Sstevel@tonic-gate 		    DDI_NT_CD_CHAN, NULL) != DDI_SUCCESS) {
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 			return (DDI_FAILURE);
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 	return (DDI_SUCCESS);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate static int
gen_create_mn_disk_fd(dev_info_t * devi)14880Sstevel@tonic-gate gen_create_mn_disk_fd(dev_info_t *devi)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	struct driver_minor_data *dmdp;
14910Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	for (dmdp = disk_minor_data; dmdp->name != NULL; dmdp++) {
14940Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
14950Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)) | dmdp->minor,
14960Sstevel@tonic-gate 		    DDI_NT_BLOCK_CHAN, NULL) != DDI_SUCCESS) {
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 			return (DDI_FAILURE);
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 	return (DDI_SUCCESS);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate static int
gen_create_serial(dev_info_t * devi)15050Sstevel@tonic-gate gen_create_serial(dev_info_t *devi)
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	struct driver_serial_minor_data *dmdp;
15080Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	for (dmdp = serial_minor_data; dmdp->name != NULL; dmdp++) {
15110Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, dmdp->name, dmdp->type,
15120Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)) | dmdp->minor,
15130Sstevel@tonic-gate 		    dmdp->node_type, NULL) != DDI_SUCCESS) {
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 			return (DDI_FAILURE);
15160Sstevel@tonic-gate 		}
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 	return (DDI_SUCCESS);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate static int
gen_create_net(dev_info_t * devi)15220Sstevel@tonic-gate gen_create_net(dev_info_t *devi)
15230Sstevel@tonic-gate {
15240Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
15250Sstevel@tonic-gate 	char minorname[32];
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	if (gen_create_properties(devi) != DDI_SUCCESS)
15280Sstevel@tonic-gate 		return (DDI_FAILURE);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	(void) snprintf(minorname, sizeof (minorname), "gen_drv%d", instance);
15310Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, minorname, S_IFCHR,
15320Sstevel@tonic-gate 	    INST_TO_MINOR(instance), DDI_NT_NET, 0));
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate static int
gen_create_minor_nodes(dev_info_t * devi,struct dstate * dstatep)15360Sstevel@tonic-gate gen_create_minor_nodes(dev_info_t *devi, struct dstate *dstatep)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
15390Sstevel@tonic-gate 	char *node_name;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	node_name = ddi_node_name(devi);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (strcmp(node_name, "disk_chan") == 0) {
15440Sstevel@tonic-gate 		rval = gen_create_mn_disk_chan(devi);
15450Sstevel@tonic-gate 	} else if (strcmp(node_name, "disk_wwn") == 0) {
15460Sstevel@tonic-gate 		rval = gen_create_mn_disk_wwn(devi);
15470Sstevel@tonic-gate 	} else if (strcmp(node_name, "disk_cdrom") == 0) {
15480Sstevel@tonic-gate 		rval = gen_create_mn_disk_cdrom(devi);
15490Sstevel@tonic-gate 	} else if (strcmp(node_name, "disk_fd") == 0) {
15500Sstevel@tonic-gate 		rval = gen_create_mn_disk_fd(devi);
15510Sstevel@tonic-gate 	} else if (strcmp(node_name, "cgtwenty") == 0) {
15520Sstevel@tonic-gate 		rval = gen_create_display(devi);
15530Sstevel@tonic-gate 	} else if (strcmp(node_name, "genzs") == 0) {
15540Sstevel@tonic-gate 		rval = gen_create_serial(devi);
15550Sstevel@tonic-gate 	} else if (strcmp(node_name, "net") == 0) {
15560Sstevel@tonic-gate 		rval = gen_create_net(devi);
15570Sstevel@tonic-gate 	} else {
15580Sstevel@tonic-gate 		int instance = ddi_get_instance(devi);
15590Sstevel@tonic-gate 		char *node_type;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		/*
15620Sstevel@tonic-gate 		 * Solaris may directly hang the node_type off the minor node
15630Sstevel@tonic-gate 		 * (without making a copy).  Since we free the node_type
15640Sstevel@tonic-gate 		 * property below we need to make a private copy to pass
15650Sstevel@tonic-gate 		 * to ddi_create_minor_node to avoid devinfo snapshot panics.
15660Sstevel@tonic-gate 		 * We store a pointer to our copy in dstate and free it in
15670Sstevel@tonic-gate 		 * gen_detach after the minor nodes have been deleted by
15680Sstevel@tonic-gate 		 * ddi_remove_minor_node.
15690Sstevel@tonic-gate 		 */
15700Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
15710Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "node-type", &node_type) != 0) {
15720Sstevel@tonic-gate 			cmn_err(CE_WARN, "couldn't get node-type\n");
15730Sstevel@tonic-gate 			return (DDI_FAILURE);
15740Sstevel@tonic-gate 		}
15750Sstevel@tonic-gate 		if (node_type) {
15760Sstevel@tonic-gate 			dstatep->node_type = kmem_alloc(
15770Sstevel@tonic-gate 			    strlen(node_type) + 1, KM_SLEEP);
15780Sstevel@tonic-gate 			(void) strcpy(dstatep->node_type, node_type);
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 		ddi_prop_free(node_type);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		/* the minor name is the same as the node name */
15830Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, node_name, S_IFCHR,
15840Sstevel@tonic-gate 		    (INST_TO_MINOR(instance)), dstatep->node_type, NULL) !=
15850Sstevel@tonic-gate 		    DDI_SUCCESS) {
15860Sstevel@tonic-gate 			if (dstatep->node_type) {
15870Sstevel@tonic-gate 				kmem_free(dstatep->node_type,
15880Sstevel@tonic-gate 				    strlen(dstatep->node_type) + 1);
15890Sstevel@tonic-gate 				dstatep->node_type = NULL;
15900Sstevel@tonic-gate 			}
15910Sstevel@tonic-gate 			return (DDI_FAILURE);
15920Sstevel@tonic-gate 		}
15930Sstevel@tonic-gate 		return (DDI_SUCCESS);
15940Sstevel@tonic-gate 	}
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	if (rval != DDI_SUCCESS) {
15970Sstevel@tonic-gate 		ddi_prop_remove_all(devi);
15980Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	return (rval);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate /*ARGSUSED*/
16050Sstevel@tonic-gate static void
gen_event_cb(dev_info_t * dip,ddi_eventcookie_t cookie,void * arg,void * impl_data)16060Sstevel@tonic-gate gen_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie, void *arg,
16070Sstevel@tonic-gate     void *impl_data)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	if (gen_debug)
16100Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gen_event_cb invoked");
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate }
1613