xref: /onnv-gate/usr/src/uts/common/io/pseudonex.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  * Pseudo devices are devices implemented entirely in software; pseudonex
290Sstevel@tonic-gate  * (pseudo) is the traditional nexus for pseudodevices.  Instances are
300Sstevel@tonic-gate  * typically specified via driver.conf files; e.g. a leaf device which
310Sstevel@tonic-gate  * should be attached below pseudonex will have an entry like:
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *	name="foo" parent="/pseudo" instance=0;
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * pseudonex also supports the devctl (see <sys/devctl.h>) interface via
360Sstevel@tonic-gate  * its :devctl minor node.  This allows priveleged userland applications to
370Sstevel@tonic-gate  * online/offline children of pseudo as needed.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * In general, we discourage widespread use of this tactic, as it may lead to a
400Sstevel@tonic-gate  * proliferation of nodes in /pseudo.  It is preferred that implementors update
410Sstevel@tonic-gate  * pseudo.conf, adding another 'pseudo' nexus child of /pseudo, and then use
420Sstevel@tonic-gate  * that for their collection of device nodes.  To do so, add a driver alias
430Sstevel@tonic-gate  * for the name of the nexus child and a line in pseudo.conf such as:
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * 	name="foo" parent="/pseudo" instance=<n> valid-children="bar","baz";
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Setting 'valid-children' is important because we have an annoying problem;
480Sstevel@tonic-gate  * we need to prevent pseudo devices with 'parent="pseudo"' set from binding
490Sstevel@tonic-gate  * to our new pseudonex child node.  A better way might be to teach the
500Sstevel@tonic-gate  * spec-node code to understand that parent="pseudo" really means
510Sstevel@tonic-gate  * parent="/pseudo".
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * At some point in the future, it would be desirable to extend the instance
540Sstevel@tonic-gate  * database to include nexus children of pseudo.  Then we could use devctl
550Sstevel@tonic-gate  * or devfs to online nexus children of pseudo, auto-selecting an instance #,
560Sstevel@tonic-gate  * and the instance number selected would be preserved across reboot in
570Sstevel@tonic-gate  * path_to_inst.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/types.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/conf.h>
630Sstevel@tonic-gate #include <sys/ddi.h>
640Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
650Sstevel@tonic-gate #include <sys/devops.h>
660Sstevel@tonic-gate #include <sys/instance.h>
670Sstevel@tonic-gate #include <sys/modctl.h>
680Sstevel@tonic-gate #include <sys/open.h>
690Sstevel@tonic-gate #include <sys/stat.h>
700Sstevel@tonic-gate #include <sys/sunddi.h>
710Sstevel@tonic-gate #include <sys/sunndi.h>
720Sstevel@tonic-gate #include <sys/systm.h>
730Sstevel@tonic-gate #include <sys/mkdev.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Config information
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate static int pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip,
790Sstevel@tonic-gate 	    ddi_intr_op_t op, ddi_intr_handle_impl_t *hdlp, void *result);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int pseudonex_attach(dev_info_t *, ddi_attach_cmd_t);
820Sstevel@tonic-gate static int pseudonex_detach(dev_info_t *, ddi_detach_cmd_t);
830Sstevel@tonic-gate static int pseudonex_open(dev_t *, int, int, cred_t *);
840Sstevel@tonic-gate static int pseudonex_close(dev_t, int, int, cred_t *);
850Sstevel@tonic-gate static int pseudonex_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
860Sstevel@tonic-gate static int pseudonex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
870Sstevel@tonic-gate     void *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static void *pseudonex_state;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate typedef struct pseudonex_state {
920Sstevel@tonic-gate 	dev_info_t *pnx_devi;
930Sstevel@tonic-gate } pseudonex_state_t;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static struct bus_ops pseudonex_bus_ops = {
960Sstevel@tonic-gate 	BUSO_REV,
970Sstevel@tonic-gate 	nullbusmap,		/* bus_map */
980Sstevel@tonic-gate 	NULL,			/* bus_get_intrspec */
990Sstevel@tonic-gate 	NULL,			/* bus_add_intrspec */
1000Sstevel@tonic-gate 	NULL,			/* bus_remove_intrspec */
1010Sstevel@tonic-gate 	i_ddi_map_fault,	/* bus_map_fault */
1020Sstevel@tonic-gate 	ddi_no_dma_map,		/* bus_dma_map */
1030Sstevel@tonic-gate 	ddi_no_dma_allochdl,	/* bus_dma_allochdl */
1040Sstevel@tonic-gate 	NULL,			/* bus_dma_freehdl */
1050Sstevel@tonic-gate 	NULL,			/* bus_dma_bindhdl */
1060Sstevel@tonic-gate 	NULL,			/* bus_dma_unbindhdl */
1070Sstevel@tonic-gate 	NULL,			/* bus_dma_flush */
1080Sstevel@tonic-gate 	NULL,			/* bus_dma_win */
1090Sstevel@tonic-gate 	NULL,			/* bus_dma_ctl */
1100Sstevel@tonic-gate 	pseudonex_ctl,		/* bus_ctl */
1110Sstevel@tonic-gate 	ddi_bus_prop_op,	/* bus_prop_op */
1120Sstevel@tonic-gate 	0,			/* bus_get_eventcookie */
1130Sstevel@tonic-gate 	0,			/* bus_add_eventcall */
1140Sstevel@tonic-gate 	0,			/* bus_remove_eventcall	*/
1150Sstevel@tonic-gate 	0,			/* bus_post_event */
1160Sstevel@tonic-gate 	NULL,			/* bus_intr_ctl */
1170Sstevel@tonic-gate 	NULL,			/* bus_config */
1180Sstevel@tonic-gate 	NULL,			/* bus_unconfig */
1190Sstevel@tonic-gate 	NULL,			/* bus_fm_init */
1200Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
1210Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
1220Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
1230Sstevel@tonic-gate 	NULL,			/* bus_power */
1240Sstevel@tonic-gate 	pseudonex_intr_op	/* bus_intr_op */
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static struct cb_ops pseudonex_cb_ops = {
1280Sstevel@tonic-gate 	pseudonex_open,			/* open */
1290Sstevel@tonic-gate 	pseudonex_close,		/* close */
1300Sstevel@tonic-gate 	nodev,				/* strategy */
1310Sstevel@tonic-gate 	nodev,				/* print */
1320Sstevel@tonic-gate 	nodev,				/* dump */
1330Sstevel@tonic-gate 	nodev,				/* read */
1340Sstevel@tonic-gate 	nodev,				/* write */
1350Sstevel@tonic-gate 	pseudonex_ioctl,		/* ioctl */
1360Sstevel@tonic-gate 	nodev,				/* devmap */
1370Sstevel@tonic-gate 	nodev,				/* mmap */
1380Sstevel@tonic-gate 	nodev,				/* segmap */
1390Sstevel@tonic-gate 	nochpoll,			/* poll */
1400Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1410Sstevel@tonic-gate 	0,				/* streamtab  */
1420Sstevel@tonic-gate 	D_MP | D_NEW | D_HOTPLUG	/* Driver compatibility flag */
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static struct dev_ops pseudo_ops = {
1460Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1470Sstevel@tonic-gate 	0,			/* refcnt  */
1480Sstevel@tonic-gate 	ddi_getinfo_1to1,	/* info */
1490Sstevel@tonic-gate 	nulldev,		/* identify */
1500Sstevel@tonic-gate 	nulldev,		/* probe */
1510Sstevel@tonic-gate 	pseudonex_attach,	/* attach */
1520Sstevel@tonic-gate 	pseudonex_detach,	/* detach */
1530Sstevel@tonic-gate 	nodev,			/* reset */
1540Sstevel@tonic-gate 	&pseudonex_cb_ops,	/* driver operations */
1550Sstevel@tonic-gate 	&pseudonex_bus_ops,	/* bus operations */
156*7656SSherry.Moore@Sun.COM 	nulldev,		/* power */
157*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Module linkage information for the kernel.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate static struct modldrv modldrv = {
1640Sstevel@tonic-gate 	&mod_driverops,
165*7656SSherry.Moore@Sun.COM 	"nexus driver for 'pseudo' 1.31",
1660Sstevel@tonic-gate 	&pseudo_ops,
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static struct modlinkage modlinkage = {
1700Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate int
_init(void)1740Sstevel@tonic-gate _init(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	int err;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&pseudonex_state,
1790Sstevel@tonic-gate 	    sizeof (pseudonex_state_t), 0)) != 0) {
1800Sstevel@tonic-gate 		return (err);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0) {
1830Sstevel@tonic-gate 		ddi_soft_state_fini(&pseudonex_state);
1840Sstevel@tonic-gate 		return (err);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	return (0);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate int
_fini(void)1900Sstevel@tonic-gate _fini(void)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	int err;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
1950Sstevel@tonic-gate 		return (err);
1960Sstevel@tonic-gate 	ddi_soft_state_fini(&pseudonex_state);
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2010Sstevel@tonic-gate _info(struct modinfo *modinfop)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*ARGSUSED*/
2070Sstevel@tonic-gate static int
pseudonex_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2080Sstevel@tonic-gate pseudonex_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	int instance;
2110Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	switch (cmd) {
2140Sstevel@tonic-gate 	case DDI_ATTACH:
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 	case DDI_RESUME:
2170Sstevel@tonic-gate 		return (DDI_SUCCESS);
2180Sstevel@tonic-gate 	default:
2190Sstevel@tonic-gate 		return (DDI_FAILURE);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Save the devi for this instance in the soft_state data.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
2260Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pseudonex_state, instance) != DDI_SUCCESS)
2270Sstevel@tonic-gate 		return (DDI_FAILURE);
2280Sstevel@tonic-gate 	pnx_state = ddi_get_soft_state(pseudonex_state, instance);
2290Sstevel@tonic-gate 	pnx_state->pnx_devi = devi;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "devctl", S_IFCHR, instance,
2320Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
2330Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2340Sstevel@tonic-gate 		ddi_soft_state_free(pseudonex_state, instance);
2350Sstevel@tonic-gate 		return (DDI_FAILURE);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	ddi_report_dev(devi);
2380Sstevel@tonic-gate 	return (DDI_SUCCESS);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*ARGSUSED*/
2420Sstevel@tonic-gate static int
pseudonex_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2430Sstevel@tonic-gate pseudonex_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (cmd == DDI_SUSPEND)
2480Sstevel@tonic-gate 		return (DDI_SUCCESS);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2510Sstevel@tonic-gate 	ddi_soft_state_free(pseudonex_state, instance);
2520Sstevel@tonic-gate 	return (DDI_SUCCESS);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*ARGSUSED*/
2560Sstevel@tonic-gate static int
pseudonex_open(dev_t * devp,int flags,int otyp,cred_t * credp)2570Sstevel@tonic-gate pseudonex_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	int instance;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
2620Sstevel@tonic-gate 		return (EINVAL);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	instance = getminor(*devp);
2650Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
2660Sstevel@tonic-gate 		return (ENXIO);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	return (0);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*ARGSUSED*/
2720Sstevel@tonic-gate static int
pseudonex_close(dev_t dev,int flags,int otyp,cred_t * credp)2730Sstevel@tonic-gate pseudonex_close(dev_t dev, int flags, int otyp, cred_t *credp)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	int instance;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
2780Sstevel@tonic-gate 		return (EINVAL);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	instance = getminor(dev);
2810Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
2820Sstevel@tonic-gate 		return (ENXIO);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	return (0);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*ARGSUSED*/
2880Sstevel@tonic-gate static int
pseudonex_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)2890Sstevel@tonic-gate pseudonex_ioctl(dev_t dev,
2900Sstevel@tonic-gate     int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	int instance;
2930Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	instance = getminor(dev);
2960Sstevel@tonic-gate 	if ((pnx_state = ddi_get_soft_state(pseudonex_state, instance)) == NULL)
2970Sstevel@tonic-gate 		return (ENXIO);
2980Sstevel@tonic-gate 	ASSERT(pnx_state->pnx_devi);
2990Sstevel@tonic-gate 	return (ndi_devctl_ioctl(pnx_state->pnx_devi, cmd, arg, mode, 0));
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * pseudonex_intr_op: pseudonex convert an interrupt number to an
3040Sstevel@tonic-gate  *			interrupt. NO OP for pseudo drivers.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate /*ARGSUSED*/
3070Sstevel@tonic-gate static int
pseudonex_intr_op(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t op,ddi_intr_handle_impl_t * hdlp,void * result)3080Sstevel@tonic-gate pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
3090Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	return (DDI_FAILURE);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate static int
pseudonex_check_assignment(dev_info_t * child,int test_inst)3150Sstevel@tonic-gate pseudonex_check_assignment(dev_info_t *child, int test_inst)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	dev_info_t	*tdip;
3180Sstevel@tonic-gate 	kmutex_t	*dmp;
3190Sstevel@tonic-gate 	const char 	*childname = ddi_driver_name(child);
3200Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
3230Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
3240Sstevel@tonic-gate 	for (tdip = devnamesp[childmaj].dn_head;
3250Sstevel@tonic-gate 	    tdip != NULL; tdip = ddi_get_next(tdip)) {
3260Sstevel@tonic-gate 		/* is this the current node? */
3270Sstevel@tonic-gate 		if (tdip == child)
3280Sstevel@tonic-gate 			continue;
3290Sstevel@tonic-gate 		/* is this a duplicate instance? */
3300Sstevel@tonic-gate 		if (test_inst == ddi_get_instance(tdip)) {
3310Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
3320Sstevel@tonic-gate 			return (DDI_FAILURE);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
3360Sstevel@tonic-gate 	return (DDI_SUCCESS);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate  * This is a nasty, slow hack.  But we're stuck with it until we do some
3410Sstevel@tonic-gate  * major surgery on the instance assignment subsystem, to allow pseudonode
3420Sstevel@tonic-gate  * instance assignment to be tracked there.
3430Sstevel@tonic-gate  *
3440Sstevel@tonic-gate  * To auto-assign an instance number, we exhaustively search the instance
3450Sstevel@tonic-gate  * list for each possible instance number until we find one which is unused.
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate static int
pseudonex_auto_assign(dev_info_t * child)3480Sstevel@tonic-gate pseudonex_auto_assign(dev_info_t *child)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	dev_info_t	*tdip;
3510Sstevel@tonic-gate 	kmutex_t	*dmp;
3520Sstevel@tonic-gate 	const char 	*childname = ddi_driver_name(child);
3530Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
3540Sstevel@tonic-gate 	int inst = 0;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
3570Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
3580Sstevel@tonic-gate 	for (inst = 0; inst <= MAXMIN32; inst++) {
3590Sstevel@tonic-gate 		for (tdip = devnamesp[childmaj].dn_head; tdip != NULL;
3600Sstevel@tonic-gate 		    tdip = ddi_get_next(tdip)) {
3610Sstevel@tonic-gate 			/* is this the current node? */
3620Sstevel@tonic-gate 			if (tdip == child)
3630Sstevel@tonic-gate 				continue;
3640Sstevel@tonic-gate 			if (inst == ddi_get_instance(tdip)) {
3650Sstevel@tonic-gate 				break;
3660Sstevel@tonic-gate 			}
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 		if (tdip == NULL) {
3690Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
3700Sstevel@tonic-gate 			return (inst);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
3740Sstevel@tonic-gate 	return (-1);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate static int
pseudonex_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)3780Sstevel@tonic-gate pseudonex_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
3790Sstevel@tonic-gate     void *arg, void *result)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	switch (ctlop) {
3820Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
3830Sstevel@tonic-gate 		if (rdip == NULL)
3840Sstevel@tonic-gate 			return (DDI_FAILURE);
3850Sstevel@tonic-gate 		cmn_err(CE_CONT, "?pseudo-device: %s%d\n",
3860Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
3870Sstevel@tonic-gate 		return (DDI_SUCCESS);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
3900Sstevel@tonic-gate 	{
3910Sstevel@tonic-gate 		char name[12];	/* enough for a decimal integer */
3920Sstevel@tonic-gate 		int instance = -1;
3930Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
3940Sstevel@tonic-gate 		const char *childname = ddi_driver_name(child);
3950Sstevel@tonic-gate 		char **childlist;
3960Sstevel@tonic-gate 		uint_t nelems;
3970Sstevel@tonic-gate 		int auto_assign = 0;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 		/*
4000Sstevel@tonic-gate 		 * If this pseudonex node has a valid-children property,
4010Sstevel@tonic-gate 		 * then that acts as an access control list for children
4020Sstevel@tonic-gate 		 * allowed to attach beneath this node.  Honor it.
4030Sstevel@tonic-gate 		 */
4040Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
4050Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "valid-children", &childlist,
4060Sstevel@tonic-gate 		    &nelems) == DDI_PROP_SUCCESS) {
4070Sstevel@tonic-gate 			int i, ok = 0;
4080Sstevel@tonic-gate 			for (i = 0; i < nelems; i++) {
4090Sstevel@tonic-gate 				if (strcmp(childlist[i], childname) == 0) {
4100Sstevel@tonic-gate 					ok = 1;
4110Sstevel@tonic-gate 					break;
4120Sstevel@tonic-gate 				}
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			ddi_prop_free(childlist);
4150Sstevel@tonic-gate 			if (!ok)
4160Sstevel@tonic-gate 				return (DDI_FAILURE);
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		/*
4200Sstevel@tonic-gate 		 * Look up the "instance" property. If it does not exist,
4210Sstevel@tonic-gate 		 * check to see if the "auto-assign-instance" property is set.
4220Sstevel@tonic-gate 		 * If not, default to using instance 0; while not ideal, this
4230Sstevel@tonic-gate 		 * is a legacy behavior we must continue to support.
4240Sstevel@tonic-gate 		 */
4250Sstevel@tonic-gate 		instance = ddi_prop_get_int(DDI_DEV_T_ANY, child,
4260Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "instance", -1);
4270Sstevel@tonic-gate 		auto_assign = ddi_prop_exists(DDI_DEV_T_ANY, child,
4280Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "auto-assign-instance");
4290Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
4300Sstevel@tonic-gate 		    "pseudonex: DDI_CTLOPS_INITCHILD(instance=%d, "
4310Sstevel@tonic-gate 		    "auto-assign=%d)", instance, auto_assign));
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (instance != -1 && auto_assign != 0) {
4340Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "both instance and "
4350Sstevel@tonic-gate 			    "auto-assign-instance properties specified. "
4360Sstevel@tonic-gate 			    "Node rejected."));
4370Sstevel@tonic-gate 			return (DDI_FAILURE);
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		if (instance == -1 && auto_assign == 0) {
4410Sstevel@tonic-gate 			/* default to instance 0 if not specified */
4420Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "defaulting to 0"));
4430Sstevel@tonic-gate 			instance = 0;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		/*
4470Sstevel@tonic-gate 		 * If an instance has been specified, determine if this
4480Sstevel@tonic-gate 		 * instance is already in use; if we need to pick an instance,
4490Sstevel@tonic-gate 		 * we do it here.
4500Sstevel@tonic-gate 		 */
4510Sstevel@tonic-gate 		if (auto_assign) {
4520Sstevel@tonic-gate 			if ((instance = pseudonex_auto_assign(child)) == -1) {
4530Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_NOTE, "failed to "
4540Sstevel@tonic-gate 				    "auto-select instance for %s", childname));
4550Sstevel@tonic-gate 				return (DDI_FAILURE);
4560Sstevel@tonic-gate 			}
4570Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
4580Sstevel@tonic-gate 			    "auto-selected instance for %s: %d",
4590Sstevel@tonic-gate 			    childname, instance));
4600Sstevel@tonic-gate 		} else {
4610Sstevel@tonic-gate 			if (pseudonex_check_assignment(child, instance) ==
4620Sstevel@tonic-gate 			    DDI_FAILURE) {
4630Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_WARN,
4640Sstevel@tonic-gate 				    "Duplicate instance %d of node \"%s\" "
4650Sstevel@tonic-gate 				    "ignored.", instance, childname));
4660Sstevel@tonic-gate 				return (DDI_FAILURE);
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
4690Sstevel@tonic-gate 			    "using fixed-assignment instance for %s: %d",
4700Sstevel@tonic-gate 			    childname, instance));
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 		/*
4740Sstevel@tonic-gate 		 * Attach the instance number to the node. This allows
4750Sstevel@tonic-gate 		 * us to have multiple instances of the same pseudo
4760Sstevel@tonic-gate 		 * device, they will be named 'device@instance'. If this
4770Sstevel@tonic-gate 		 * breaks programs, we may need to special-case instance 0
4780Sstevel@tonic-gate 		 * into 'device'. Ick. devlinks appears to handle the
4790Sstevel@tonic-gate 		 * new names ok, so if only names in /dev are used
4800Sstevel@tonic-gate 		 * this may not be necessary.
4810Sstevel@tonic-gate 		 */
4820Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%d", instance);
4830Sstevel@tonic-gate 		DEVI(child)->devi_instance = instance;
4840Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
4850Sstevel@tonic-gate 		return (DDI_SUCCESS);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
4890Sstevel@tonic-gate 	{
4900Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
4930Sstevel@tonic-gate 		    "DDI_CTLOPS_UNINITCHILD(%s, instance=%d)",
4940Sstevel@tonic-gate 		    ddi_driver_name(child), DEVI(child)->devi_instance));
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		return (DDI_SUCCESS);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
5020Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
5030Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
5040Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
5050Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
5060Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
5070Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
5080Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
5090Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
5100Sstevel@tonic-gate 		/*
5110Sstevel@tonic-gate 		 * These ops correspond to functions that "shouldn't" be called
5120Sstevel@tonic-gate 		 * by a pseudo driver.  So we whine when we're called.
5130Sstevel@tonic-gate 		 */
5140Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
5150Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), ctlop,
5160Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
5170Sstevel@tonic-gate 		return (DDI_FAILURE);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
5200Sstevel@tonic-gate 	case DDI_CTLOPS_BTOP:
5210Sstevel@tonic-gate 	case DDI_CTLOPS_BTOPR:
5220Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
5230Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
5240Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
5250Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
5260Sstevel@tonic-gate 	case DDI_CTLOPS_PTOB:
5270Sstevel@tonic-gate 	default:
5280Sstevel@tonic-gate 		/*
5290Sstevel@tonic-gate 		 * The ops that we pass up (default).  We pass up memory
5300Sstevel@tonic-gate 		 * allocation oriented ops that we receive - these may be
5310Sstevel@tonic-gate 		 * associated with pseudo HBA drivers below us with target
5320Sstevel@tonic-gate 		 * drivers below them that use ddi memory allocation
5330Sstevel@tonic-gate 		 * interfaces like scsi_alloc_consistent_buf.
5340Sstevel@tonic-gate 		 */
5350Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate }
538