xref: /illumos-gate/usr/src/uts/common/io/pseudonex.c (revision b38ce9de)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Pseudo devices are devices implemented entirely in software; pseudonex
297c478bd9Sstevel@tonic-gate  * (pseudo) is the traditional nexus for pseudodevices.  Instances are
307c478bd9Sstevel@tonic-gate  * typically specified via driver.conf files; e.g. a leaf device which
317c478bd9Sstevel@tonic-gate  * should be attached below pseudonex will have an entry like:
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *	name="foo" parent="/pseudo" instance=0;
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * pseudonex also supports the devctl (see <sys/devctl.h>) interface via
367c478bd9Sstevel@tonic-gate  * its :devctl minor node.  This allows priveleged userland applications to
377c478bd9Sstevel@tonic-gate  * online/offline children of pseudo as needed.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * In general, we discourage widespread use of this tactic, as it may lead to a
407c478bd9Sstevel@tonic-gate  * proliferation of nodes in /pseudo.  It is preferred that implementors update
417c478bd9Sstevel@tonic-gate  * pseudo.conf, adding another 'pseudo' nexus child of /pseudo, and then use
427c478bd9Sstevel@tonic-gate  * that for their collection of device nodes.  To do so, add a driver alias
437c478bd9Sstevel@tonic-gate  * for the name of the nexus child and a line in pseudo.conf such as:
447c478bd9Sstevel@tonic-gate  *
45*b38ce9deSRobert Mustacchi  *	name="foo" parent="/pseudo" instance=<n> valid-children="bar","baz";
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * Setting 'valid-children' is important because we have an annoying problem;
487c478bd9Sstevel@tonic-gate  * we need to prevent pseudo devices with 'parent="pseudo"' set from binding
497c478bd9Sstevel@tonic-gate  * to our new pseudonex child node.  A better way might be to teach the
507c478bd9Sstevel@tonic-gate  * spec-node code to understand that parent="pseudo" really means
517c478bd9Sstevel@tonic-gate  * parent="/pseudo".
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * At some point in the future, it would be desirable to extend the instance
547c478bd9Sstevel@tonic-gate  * database to include nexus children of pseudo.  Then we could use devctl
557c478bd9Sstevel@tonic-gate  * or devfs to online nexus children of pseudo, auto-selecting an instance #,
567c478bd9Sstevel@tonic-gate  * and the instance number selected would be preserved across reboot in
577c478bd9Sstevel@tonic-gate  * path_to_inst.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/types.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/conf.h>
637c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
647c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
657c478bd9Sstevel@tonic-gate #include <sys/devops.h>
667c478bd9Sstevel@tonic-gate #include <sys/instance.h>
677c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
687c478bd9Sstevel@tonic-gate #include <sys/open.h>
697c478bd9Sstevel@tonic-gate #include <sys/stat.h>
707c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
717c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
727c478bd9Sstevel@tonic-gate #include <sys/systm.h>
737c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Config information
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static int pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip,
797c478bd9Sstevel@tonic-gate 	    ddi_intr_op_t op, ddi_intr_handle_impl_t *hdlp, void *result);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int pseudonex_attach(dev_info_t *, ddi_attach_cmd_t);
827c478bd9Sstevel@tonic-gate static int pseudonex_detach(dev_info_t *, ddi_detach_cmd_t);
837c478bd9Sstevel@tonic-gate static int pseudonex_open(dev_t *, int, int, cred_t *);
847c478bd9Sstevel@tonic-gate static int pseudonex_close(dev_t, int, int, cred_t *);
857c478bd9Sstevel@tonic-gate static int pseudonex_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
86*b38ce9deSRobert Mustacchi static int pseudonex_fm_init(dev_info_t *, dev_info_t *, int,
87*b38ce9deSRobert Mustacchi     ddi_iblock_cookie_t *);
887c478bd9Sstevel@tonic-gate static int pseudonex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
897c478bd9Sstevel@tonic-gate     void *);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void *pseudonex_state;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate typedef struct pseudonex_state {
947c478bd9Sstevel@tonic-gate 	dev_info_t *pnx_devi;
95*b38ce9deSRobert Mustacchi 	int pnx_fmcap;
96*b38ce9deSRobert Mustacchi 	ddi_iblock_cookie_t pnx_fm_ibc;
977c478bd9Sstevel@tonic-gate } pseudonex_state_t;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static struct bus_ops pseudonex_bus_ops = {
1007c478bd9Sstevel@tonic-gate 	BUSO_REV,
1017c478bd9Sstevel@tonic-gate 	nullbusmap,		/* bus_map */
1027c478bd9Sstevel@tonic-gate 	NULL,			/* bus_get_intrspec */
1037c478bd9Sstevel@tonic-gate 	NULL,			/* bus_add_intrspec */
1047c478bd9Sstevel@tonic-gate 	NULL,			/* bus_remove_intrspec */
1057c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,	/* bus_map_fault */
1067c478bd9Sstevel@tonic-gate 	ddi_no_dma_map,		/* bus_dma_map */
1077c478bd9Sstevel@tonic-gate 	ddi_no_dma_allochdl,	/* bus_dma_allochdl */
1087c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_freehdl */
1097c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_bindhdl */
1107c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_unbindhdl */
1117c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_flush */
1127c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_win */
1137c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_ctl */
1147c478bd9Sstevel@tonic-gate 	pseudonex_ctl,		/* bus_ctl */
1157c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,	/* bus_prop_op */
1167c478bd9Sstevel@tonic-gate 	0,			/* bus_get_eventcookie */
1177c478bd9Sstevel@tonic-gate 	0,			/* bus_add_eventcall */
1187c478bd9Sstevel@tonic-gate 	0,			/* bus_remove_eventcall	*/
1197c478bd9Sstevel@tonic-gate 	0,			/* bus_post_event */
1207c478bd9Sstevel@tonic-gate 	NULL,			/* bus_intr_ctl */
1217c478bd9Sstevel@tonic-gate 	NULL,			/* bus_config */
1227c478bd9Sstevel@tonic-gate 	NULL,			/* bus_unconfig */
123*b38ce9deSRobert Mustacchi 	pseudonex_fm_init,	/* bus_fm_init */
1247c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
1257c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
1267c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
1277c478bd9Sstevel@tonic-gate 	NULL,			/* bus_power */
1287c478bd9Sstevel@tonic-gate 	pseudonex_intr_op	/* bus_intr_op */
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static struct cb_ops pseudonex_cb_ops = {
1327c478bd9Sstevel@tonic-gate 	pseudonex_open,			/* open */
1337c478bd9Sstevel@tonic-gate 	pseudonex_close,		/* close */
1347c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
1357c478bd9Sstevel@tonic-gate 	nodev,				/* print */
1367c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
1377c478bd9Sstevel@tonic-gate 	nodev,				/* read */
1387c478bd9Sstevel@tonic-gate 	nodev,				/* write */
1397c478bd9Sstevel@tonic-gate 	pseudonex_ioctl,		/* ioctl */
1407c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
1417c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
1427c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
1437c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
1447c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1457c478bd9Sstevel@tonic-gate 	0,				/* streamtab  */
1467c478bd9Sstevel@tonic-gate 	D_MP | D_NEW | D_HOTPLUG	/* Driver compatibility flag */
1477c478bd9Sstevel@tonic-gate };
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static struct dev_ops pseudo_ops = {
1507c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1517c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1527c478bd9Sstevel@tonic-gate 	ddi_getinfo_1to1,	/* info */
1537c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1547c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1557c478bd9Sstevel@tonic-gate 	pseudonex_attach,	/* attach */
1567c478bd9Sstevel@tonic-gate 	pseudonex_detach,	/* detach */
1577c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1587c478bd9Sstevel@tonic-gate 	&pseudonex_cb_ops,	/* driver operations */
1597c478bd9Sstevel@tonic-gate 	&pseudonex_bus_ops,	/* bus operations */
16019397407SSherry Moore 	nulldev,		/* power */
16119397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1687c478bd9Sstevel@tonic-gate 	&mod_driverops,
16919397407SSherry Moore 	"nexus driver for 'pseudo' 1.31",
1707c478bd9Sstevel@tonic-gate 	&pseudo_ops,
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1747c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1757c478bd9Sstevel@tonic-gate };
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int
_init(void)1787c478bd9Sstevel@tonic-gate _init(void)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	int err;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&pseudonex_state,
1837c478bd9Sstevel@tonic-gate 	    sizeof (pseudonex_state_t), 0)) != 0) {
1847c478bd9Sstevel@tonic-gate 		return (err);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0) {
1877c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&pseudonex_state);
1887c478bd9Sstevel@tonic-gate 		return (err);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate int
_fini(void)1947c478bd9Sstevel@tonic-gate _fini(void)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int err;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
1997c478bd9Sstevel@tonic-gate 		return (err);
2007c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&pseudonex_state);
2017c478bd9Sstevel@tonic-gate 	return (0);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2057c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2117c478bd9Sstevel@tonic-gate static int
pseudonex_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2127c478bd9Sstevel@tonic-gate pseudonex_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	int instance;
2157c478bd9Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	switch (cmd) {
2187c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2197c478bd9Sstevel@tonic-gate 		break;
2207c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
2217c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2227c478bd9Sstevel@tonic-gate 	default:
2237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Save the devi for this instance in the soft_state data.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
2307c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pseudonex_state, instance) != DDI_SUCCESS)
2317c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2327c478bd9Sstevel@tonic-gate 	pnx_state = ddi_get_soft_state(pseudonex_state, instance);
2337c478bd9Sstevel@tonic-gate 	pnx_state->pnx_devi = devi;
2347c478bd9Sstevel@tonic-gate 
235*b38ce9deSRobert Mustacchi 	pnx_state->pnx_fmcap = DDI_FM_EREPORT_CAPABLE;
236*b38ce9deSRobert Mustacchi 	ddi_fm_init(devi, &pnx_state->pnx_fmcap, &pnx_state->pnx_fm_ibc);
237*b38ce9deSRobert Mustacchi 
2387c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "devctl", S_IFCHR, instance,
2397c478bd9Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
2407c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2417c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pseudonex_state, instance);
2427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
2457c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2497c478bd9Sstevel@tonic-gate static int
pseudonex_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2507c478bd9Sstevel@tonic-gate pseudonex_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (cmd == DDI_SUSPEND)
2557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2567c478bd9Sstevel@tonic-gate 
257*b38ce9deSRobert Mustacchi 	if (cmd != DDI_DETACH)
258*b38ce9deSRobert Mustacchi 		return (DDI_FAILURE);
259*b38ce9deSRobert Mustacchi 
260*b38ce9deSRobert Mustacchi 	ddi_fm_fini(devi);
2617c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2627c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(pseudonex_state, instance);
2637c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2677c478bd9Sstevel@tonic-gate static int
pseudonex_open(dev_t * devp,int flags,int otyp,cred_t * credp)2687c478bd9Sstevel@tonic-gate pseudonex_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	int instance;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
2737c478bd9Sstevel@tonic-gate 		return (EINVAL);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
2767c478bd9Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
2777c478bd9Sstevel@tonic-gate 		return (ENXIO);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (0);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2837c478bd9Sstevel@tonic-gate static int
pseudonex_close(dev_t dev,int flags,int otyp,cred_t * credp)2847c478bd9Sstevel@tonic-gate pseudonex_close(dev_t dev, int flags, int otyp, cred_t *credp)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	int instance;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
2897c478bd9Sstevel@tonic-gate 		return (EINVAL);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
2927c478bd9Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
2937c478bd9Sstevel@tonic-gate 		return (ENXIO);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	return (0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2997c478bd9Sstevel@tonic-gate static int
pseudonex_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)3007c478bd9Sstevel@tonic-gate pseudonex_ioctl(dev_t dev,
3017c478bd9Sstevel@tonic-gate     int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	int instance;
3047c478bd9Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
3077c478bd9Sstevel@tonic-gate 	if ((pnx_state = ddi_get_soft_state(pseudonex_state, instance)) == NULL)
3087c478bd9Sstevel@tonic-gate 		return (ENXIO);
3097c478bd9Sstevel@tonic-gate 	ASSERT(pnx_state->pnx_devi);
3107c478bd9Sstevel@tonic-gate 	return (ndi_devctl_ioctl(pnx_state->pnx_devi, cmd, arg, mode, 0));
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * pseudonex_intr_op: pseudonex convert an interrupt number to an
3157c478bd9Sstevel@tonic-gate  *			interrupt. NO OP for pseudo drivers.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3187c478bd9Sstevel@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)3197c478bd9Sstevel@tonic-gate pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
3207c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static int
pseudonex_check_assignment(dev_info_t * child,int test_inst)3267c478bd9Sstevel@tonic-gate pseudonex_check_assignment(dev_info_t *child, int test_inst)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	dev_info_t	*tdip;
3297c478bd9Sstevel@tonic-gate 	kmutex_t	*dmp;
330*b38ce9deSRobert Mustacchi 	const char	*childname = ddi_driver_name(child);
3317c478bd9Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
3347c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
3357c478bd9Sstevel@tonic-gate 	for (tdip = devnamesp[childmaj].dn_head;
3367c478bd9Sstevel@tonic-gate 	    tdip != NULL; tdip = ddi_get_next(tdip)) {
3377c478bd9Sstevel@tonic-gate 		/* is this the current node? */
3387c478bd9Sstevel@tonic-gate 		if (tdip == child)
3397c478bd9Sstevel@tonic-gate 			continue;
3407c478bd9Sstevel@tonic-gate 		/* is this a duplicate instance? */
3417c478bd9Sstevel@tonic-gate 		if (test_inst == ddi_get_instance(tdip)) {
3427c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
3437c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
3477c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * This is a nasty, slow hack.  But we're stuck with it until we do some
3527c478bd9Sstevel@tonic-gate  * major surgery on the instance assignment subsystem, to allow pseudonode
3537c478bd9Sstevel@tonic-gate  * instance assignment to be tracked there.
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * To auto-assign an instance number, we exhaustively search the instance
3567c478bd9Sstevel@tonic-gate  * list for each possible instance number until we find one which is unused.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate static int
pseudonex_auto_assign(dev_info_t * child)3597c478bd9Sstevel@tonic-gate pseudonex_auto_assign(dev_info_t *child)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	dev_info_t	*tdip;
3627c478bd9Sstevel@tonic-gate 	kmutex_t	*dmp;
363*b38ce9deSRobert Mustacchi 	const char	*childname = ddi_driver_name(child);
3647c478bd9Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
3657c478bd9Sstevel@tonic-gate 	int inst = 0;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
3687c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
3697c478bd9Sstevel@tonic-gate 	for (inst = 0; inst <= MAXMIN32; inst++) {
3707c478bd9Sstevel@tonic-gate 		for (tdip = devnamesp[childmaj].dn_head; tdip != NULL;
3717c478bd9Sstevel@tonic-gate 		    tdip = ddi_get_next(tdip)) {
3727c478bd9Sstevel@tonic-gate 			/* is this the current node? */
3737c478bd9Sstevel@tonic-gate 			if (tdip == child)
3747c478bd9Sstevel@tonic-gate 				continue;
3757c478bd9Sstevel@tonic-gate 			if (inst == ddi_get_instance(tdip)) {
3767c478bd9Sstevel@tonic-gate 				break;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		if (tdip == NULL) {
3807c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
3817c478bd9Sstevel@tonic-gate 			return (inst);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
3857c478bd9Sstevel@tonic-gate 	return (-1);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
388*b38ce9deSRobert Mustacchi static int
pseudonex_fm_init(dev_info_t * dip,dev_info_t * tdip,int cap,ddi_iblock_cookie_t * ibc)389*b38ce9deSRobert Mustacchi pseudonex_fm_init(dev_info_t *dip, dev_info_t *tdip, int cap,
390*b38ce9deSRobert Mustacchi     ddi_iblock_cookie_t *ibc)
391*b38ce9deSRobert Mustacchi {
392*b38ce9deSRobert Mustacchi 	pseudonex_state_t *pnx_state;
393*b38ce9deSRobert Mustacchi 
394*b38ce9deSRobert Mustacchi 	pnx_state = ddi_get_soft_state(pseudonex_state, ddi_get_instance(dip));
395*b38ce9deSRobert Mustacchi 	ASSERT(pnx_state != NULL);
396*b38ce9deSRobert Mustacchi 	ASSERT(ibc != NULL);
397*b38ce9deSRobert Mustacchi 	*ibc = pnx_state->pnx_fm_ibc;
398*b38ce9deSRobert Mustacchi 	return (pnx_state->pnx_fmcap & cap);
399*b38ce9deSRobert Mustacchi }
400*b38ce9deSRobert Mustacchi 
4017c478bd9Sstevel@tonic-gate static int
pseudonex_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)4027c478bd9Sstevel@tonic-gate pseudonex_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
4037c478bd9Sstevel@tonic-gate     void *arg, void *result)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	switch (ctlop) {
4067c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
4077c478bd9Sstevel@tonic-gate 		if (rdip == NULL)
4087c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4097c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?pseudo-device: %s%d\n",
4107c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
4117c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
4147c478bd9Sstevel@tonic-gate 	{
4157c478bd9Sstevel@tonic-gate 		char name[12];	/* enough for a decimal integer */
4167c478bd9Sstevel@tonic-gate 		int instance = -1;
4177c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
4187c478bd9Sstevel@tonic-gate 		const char *childname = ddi_driver_name(child);
4197c478bd9Sstevel@tonic-gate 		char **childlist;
4207c478bd9Sstevel@tonic-gate 		uint_t nelems;
4217c478bd9Sstevel@tonic-gate 		int auto_assign = 0;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		/*
4247c478bd9Sstevel@tonic-gate 		 * If this pseudonex node has a valid-children property,
4257c478bd9Sstevel@tonic-gate 		 * then that acts as an access control list for children
4267c478bd9Sstevel@tonic-gate 		 * allowed to attach beneath this node.  Honor it.
4277c478bd9Sstevel@tonic-gate 		 */
4287c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
4297c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "valid-children", &childlist,
4307c478bd9Sstevel@tonic-gate 		    &nelems) == DDI_PROP_SUCCESS) {
4317c478bd9Sstevel@tonic-gate 			int i, ok = 0;
4327c478bd9Sstevel@tonic-gate 			for (i = 0; i < nelems; i++) {
4337c478bd9Sstevel@tonic-gate 				if (strcmp(childlist[i], childname) == 0) {
4347c478bd9Sstevel@tonic-gate 					ok = 1;
4357c478bd9Sstevel@tonic-gate 					break;
4367c478bd9Sstevel@tonic-gate 				}
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 			ddi_prop_free(childlist);
4397c478bd9Sstevel@tonic-gate 			if (!ok)
4407c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		/*
4447c478bd9Sstevel@tonic-gate 		 * Look up the "instance" property. If it does not exist,
4457c478bd9Sstevel@tonic-gate 		 * check to see if the "auto-assign-instance" property is set.
4467c478bd9Sstevel@tonic-gate 		 * If not, default to using instance 0; while not ideal, this
4477c478bd9Sstevel@tonic-gate 		 * is a legacy behavior we must continue to support.
4487c478bd9Sstevel@tonic-gate 		 */
4497c478bd9Sstevel@tonic-gate 		instance = ddi_prop_get_int(DDI_DEV_T_ANY, child,
4507c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "instance", -1);
4517c478bd9Sstevel@tonic-gate 		auto_assign = ddi_prop_exists(DDI_DEV_T_ANY, child,
4527c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "auto-assign-instance");
4537c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
4547c478bd9Sstevel@tonic-gate 		    "pseudonex: DDI_CTLOPS_INITCHILD(instance=%d, "
4557c478bd9Sstevel@tonic-gate 		    "auto-assign=%d)", instance, auto_assign));
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		if (instance != -1 && auto_assign != 0) {
4587c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "both instance and "
4597c478bd9Sstevel@tonic-gate 			    "auto-assign-instance properties specified. "
4607c478bd9Sstevel@tonic-gate 			    "Node rejected."));
4617c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		if (instance == -1 && auto_assign == 0) {
4657c478bd9Sstevel@tonic-gate 			/* default to instance 0 if not specified */
4667c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "defaulting to 0"));
4677c478bd9Sstevel@tonic-gate 			instance = 0;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * If an instance has been specified, determine if this
4727c478bd9Sstevel@tonic-gate 		 * instance is already in use; if we need to pick an instance,
4737c478bd9Sstevel@tonic-gate 		 * we do it here.
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (auto_assign) {
4767c478bd9Sstevel@tonic-gate 			if ((instance = pseudonex_auto_assign(child)) == -1) {
4777c478bd9Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_NOTE, "failed to "
4787c478bd9Sstevel@tonic-gate 				    "auto-select instance for %s", childname));
4797c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
4827c478bd9Sstevel@tonic-gate 			    "auto-selected instance for %s: %d",
4837c478bd9Sstevel@tonic-gate 			    childname, instance));
4847c478bd9Sstevel@tonic-gate 		} else {
4857c478bd9Sstevel@tonic-gate 			if (pseudonex_check_assignment(child, instance) ==
4867c478bd9Sstevel@tonic-gate 			    DDI_FAILURE) {
4877c478bd9Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_WARN,
4887c478bd9Sstevel@tonic-gate 				    "Duplicate instance %d of node \"%s\" "
4897c478bd9Sstevel@tonic-gate 				    "ignored.", instance, childname));
4907c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
4937c478bd9Sstevel@tonic-gate 			    "using fixed-assignment instance for %s: %d",
4947c478bd9Sstevel@tonic-gate 			    childname, instance));
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		/*
4987c478bd9Sstevel@tonic-gate 		 * Attach the instance number to the node. This allows
4997c478bd9Sstevel@tonic-gate 		 * us to have multiple instances of the same pseudo
5007c478bd9Sstevel@tonic-gate 		 * device, they will be named 'device@instance'. If this
5017c478bd9Sstevel@tonic-gate 		 * breaks programs, we may need to special-case instance 0
5027c478bd9Sstevel@tonic-gate 		 * into 'device'. Ick. devlinks appears to handle the
5037c478bd9Sstevel@tonic-gate 		 * new names ok, so if only names in /dev are used
5047c478bd9Sstevel@tonic-gate 		 * this may not be necessary.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%d", instance);
5077c478bd9Sstevel@tonic-gate 		DEVI(child)->devi_instance = instance;
5087c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
5097c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
5137c478bd9Sstevel@tonic-gate 	{
5147c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
5177c478bd9Sstevel@tonic-gate 		    "DDI_CTLOPS_UNINITCHILD(%s, instance=%d)",
5187c478bd9Sstevel@tonic-gate 		    ddi_driver_name(child), DEVI(child)->devi_instance));
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
5267c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
5277c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
5287c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
5297c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
5307c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
5317c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
5327c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
5337c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
5347c478bd9Sstevel@tonic-gate 		/*
5357c478bd9Sstevel@tonic-gate 		 * These ops correspond to functions that "shouldn't" be called
5367c478bd9Sstevel@tonic-gate 		 * by a pseudo driver.  So we whine when we're called.
5377c478bd9Sstevel@tonic-gate 		 */
5387c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
5397c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), ctlop,
5407c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
5417c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
5447c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOP:
5457c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOPR:
5467c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
5477c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
5487c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
5497c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
5507c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PTOB:
5517c478bd9Sstevel@tonic-gate 	default:
5527c478bd9Sstevel@tonic-gate 		/*
5537c478bd9Sstevel@tonic-gate 		 * The ops that we pass up (default).  We pass up memory
5547c478bd9Sstevel@tonic-gate 		 * allocation oriented ops that we receive - these may be
5557c478bd9Sstevel@tonic-gate 		 * associated with pseudo HBA drivers below us with target
5567c478bd9Sstevel@tonic-gate 		 * drivers below them that use ddi memory allocation
5577c478bd9Sstevel@tonic-gate 		 * interfaces like scsi_alloc_consistent_buf.
5587c478bd9Sstevel@tonic-gate 		 */
5597c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate }
562