xref: /titanic_54/usr/src/uts/common/io/pseudonex.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Pseudo devices are devices implemented entirely in software; pseudonex
31*7c478bd9Sstevel@tonic-gate  * (pseudo) is the traditional nexus for pseudodevices.  Instances are
32*7c478bd9Sstevel@tonic-gate  * typically specified via driver.conf files; e.g. a leaf device which
33*7c478bd9Sstevel@tonic-gate  * should be attached below pseudonex will have an entry like:
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  *	name="foo" parent="/pseudo" instance=0;
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * pseudonex also supports the devctl (see <sys/devctl.h>) interface via
38*7c478bd9Sstevel@tonic-gate  * its :devctl minor node.  This allows priveleged userland applications to
39*7c478bd9Sstevel@tonic-gate  * online/offline children of pseudo as needed.
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  * In general, we discourage widespread use of this tactic, as it may lead to a
42*7c478bd9Sstevel@tonic-gate  * proliferation of nodes in /pseudo.  It is preferred that implementors update
43*7c478bd9Sstevel@tonic-gate  * pseudo.conf, adding another 'pseudo' nexus child of /pseudo, and then use
44*7c478bd9Sstevel@tonic-gate  * that for their collection of device nodes.  To do so, add a driver alias
45*7c478bd9Sstevel@tonic-gate  * for the name of the nexus child and a line in pseudo.conf such as:
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * 	name="foo" parent="/pseudo" instance=<n> valid-children="bar","baz";
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * Setting 'valid-children' is important because we have an annoying problem;
50*7c478bd9Sstevel@tonic-gate  * we need to prevent pseudo devices with 'parent="pseudo"' set from binding
51*7c478bd9Sstevel@tonic-gate  * to our new pseudonex child node.  A better way might be to teach the
52*7c478bd9Sstevel@tonic-gate  * spec-node code to understand that parent="pseudo" really means
53*7c478bd9Sstevel@tonic-gate  * parent="/pseudo".
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  * At some point in the future, it would be desirable to extend the instance
56*7c478bd9Sstevel@tonic-gate  * database to include nexus children of pseudo.  Then we could use devctl
57*7c478bd9Sstevel@tonic-gate  * or devfs to online nexus children of pseudo, auto-selecting an instance #,
58*7c478bd9Sstevel@tonic-gate  * and the instance number selected would be preserved across reboot in
59*7c478bd9Sstevel@tonic-gate  * path_to_inst.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/devops.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/instance.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/open.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
73*7c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
74*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
75*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * Config information
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate static int pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip,
81*7c478bd9Sstevel@tonic-gate 	    ddi_intr_op_t op, ddi_intr_handle_impl_t *hdlp, void *result);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static int pseudonex_attach(dev_info_t *, ddi_attach_cmd_t);
84*7c478bd9Sstevel@tonic-gate static int pseudonex_detach(dev_info_t *, ddi_detach_cmd_t);
85*7c478bd9Sstevel@tonic-gate static int pseudonex_open(dev_t *, int, int, cred_t *);
86*7c478bd9Sstevel@tonic-gate static int pseudonex_close(dev_t, int, int, cred_t *);
87*7c478bd9Sstevel@tonic-gate static int pseudonex_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
88*7c478bd9Sstevel@tonic-gate static int pseudonex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
89*7c478bd9Sstevel@tonic-gate     void *);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate static void *pseudonex_state;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate typedef struct pseudonex_state {
94*7c478bd9Sstevel@tonic-gate 	dev_info_t *pnx_devi;
95*7c478bd9Sstevel@tonic-gate } pseudonex_state_t;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static struct bus_ops pseudonex_bus_ops = {
98*7c478bd9Sstevel@tonic-gate 	BUSO_REV,
99*7c478bd9Sstevel@tonic-gate 	nullbusmap,		/* bus_map */
100*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_get_intrspec */
101*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_add_intrspec */
102*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_remove_intrspec */
103*7c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,	/* bus_map_fault */
104*7c478bd9Sstevel@tonic-gate 	ddi_no_dma_map,		/* bus_dma_map */
105*7c478bd9Sstevel@tonic-gate 	ddi_no_dma_allochdl,	/* bus_dma_allochdl */
106*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_freehdl */
107*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_bindhdl */
108*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_unbindhdl */
109*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_flush */
110*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_win */
111*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_dma_ctl */
112*7c478bd9Sstevel@tonic-gate 	pseudonex_ctl,		/* bus_ctl */
113*7c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,	/* bus_prop_op */
114*7c478bd9Sstevel@tonic-gate 	0,			/* bus_get_eventcookie */
115*7c478bd9Sstevel@tonic-gate 	0,			/* bus_add_eventcall */
116*7c478bd9Sstevel@tonic-gate 	0,			/* bus_remove_eventcall	*/
117*7c478bd9Sstevel@tonic-gate 	0,			/* bus_post_event */
118*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_intr_ctl */
119*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_config */
120*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_unconfig */
121*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_init */
122*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
123*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
124*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
125*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus_power */
126*7c478bd9Sstevel@tonic-gate 	pseudonex_intr_op	/* bus_intr_op */
127*7c478bd9Sstevel@tonic-gate };
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate static struct cb_ops pseudonex_cb_ops = {
130*7c478bd9Sstevel@tonic-gate 	pseudonex_open,			/* open */
131*7c478bd9Sstevel@tonic-gate 	pseudonex_close,		/* close */
132*7c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
133*7c478bd9Sstevel@tonic-gate 	nodev,				/* print */
134*7c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
135*7c478bd9Sstevel@tonic-gate 	nodev,				/* read */
136*7c478bd9Sstevel@tonic-gate 	nodev,				/* write */
137*7c478bd9Sstevel@tonic-gate 	pseudonex_ioctl,		/* ioctl */
138*7c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
139*7c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
140*7c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
141*7c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
142*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
143*7c478bd9Sstevel@tonic-gate 	0,				/* streamtab  */
144*7c478bd9Sstevel@tonic-gate 	D_MP | D_NEW | D_HOTPLUG	/* Driver compatibility flag */
145*7c478bd9Sstevel@tonic-gate };
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate static struct dev_ops pseudo_ops = {
148*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
149*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
150*7c478bd9Sstevel@tonic-gate 	ddi_getinfo_1to1,	/* info */
151*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
152*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
153*7c478bd9Sstevel@tonic-gate 	pseudonex_attach,	/* attach */
154*7c478bd9Sstevel@tonic-gate 	pseudonex_detach,	/* detach */
155*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
156*7c478bd9Sstevel@tonic-gate 	&pseudonex_cb_ops,	/* driver operations */
157*7c478bd9Sstevel@tonic-gate 	&pseudonex_bus_ops,	/* bus operations */
158*7c478bd9Sstevel@tonic-gate 	nulldev			/* power */
159*7c478bd9Sstevel@tonic-gate };
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
163*7c478bd9Sstevel@tonic-gate  */
164*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
165*7c478bd9Sstevel@tonic-gate 	&mod_driverops,
166*7c478bd9Sstevel@tonic-gate 	"nexus driver for 'pseudo' %I%",
167*7c478bd9Sstevel@tonic-gate 	&pseudo_ops,
168*7c478bd9Sstevel@tonic-gate };
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
171*7c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
172*7c478bd9Sstevel@tonic-gate };
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate int
175*7c478bd9Sstevel@tonic-gate _init(void)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	int err;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&pseudonex_state,
180*7c478bd9Sstevel@tonic-gate 	    sizeof (pseudonex_state_t), 0)) != 0) {
181*7c478bd9Sstevel@tonic-gate 		return (err);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0) {
184*7c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&pseudonex_state);
185*7c478bd9Sstevel@tonic-gate 		return (err);
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	return (0);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate int
191*7c478bd9Sstevel@tonic-gate _fini(void)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	int err;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
196*7c478bd9Sstevel@tonic-gate 		return (err);
197*7c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&pseudonex_state);
198*7c478bd9Sstevel@tonic-gate 	return (0);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate int
202*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
208*7c478bd9Sstevel@tonic-gate static int
209*7c478bd9Sstevel@tonic-gate pseudonex_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate 	int instance;
212*7c478bd9Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
215*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
216*7c478bd9Sstevel@tonic-gate 		break;
217*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
218*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
219*7c478bd9Sstevel@tonic-gate 	default:
220*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	/*
224*7c478bd9Sstevel@tonic-gate 	 * Save the devi for this instance in the soft_state data.
225*7c478bd9Sstevel@tonic-gate 	 */
226*7c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
227*7c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pseudonex_state, instance) != DDI_SUCCESS)
228*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
229*7c478bd9Sstevel@tonic-gate 	pnx_state = ddi_get_soft_state(pseudonex_state, instance);
230*7c478bd9Sstevel@tonic-gate 	pnx_state->pnx_devi = devi;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "devctl", S_IFCHR, instance,
233*7c478bd9Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
234*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
235*7c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pseudonex_state, instance);
236*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
239*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
243*7c478bd9Sstevel@tonic-gate static int
244*7c478bd9Sstevel@tonic-gate pseudonex_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	if (cmd == DDI_SUSPEND)
249*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
252*7c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(pseudonex_state, instance);
253*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
257*7c478bd9Sstevel@tonic-gate static int
258*7c478bd9Sstevel@tonic-gate pseudonex_open(dev_t *devp, int flags, int otyp, cred_t *credp)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	int instance;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
263*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
266*7c478bd9Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
267*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	return (0);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
273*7c478bd9Sstevel@tonic-gate static int
274*7c478bd9Sstevel@tonic-gate pseudonex_close(dev_t dev, int flags, int otyp, cred_t *credp)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	int instance;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
279*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
282*7c478bd9Sstevel@tonic-gate 	if (ddi_get_soft_state(pseudonex_state, instance) == NULL)
283*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	return (0);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
289*7c478bd9Sstevel@tonic-gate static int
290*7c478bd9Sstevel@tonic-gate pseudonex_ioctl(dev_t dev,
291*7c478bd9Sstevel@tonic-gate     int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
292*7c478bd9Sstevel@tonic-gate {
293*7c478bd9Sstevel@tonic-gate 	int instance;
294*7c478bd9Sstevel@tonic-gate 	pseudonex_state_t *pnx_state;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
297*7c478bd9Sstevel@tonic-gate 	if ((pnx_state = ddi_get_soft_state(pseudonex_state, instance)) == NULL)
298*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
299*7c478bd9Sstevel@tonic-gate 	ASSERT(pnx_state->pnx_devi);
300*7c478bd9Sstevel@tonic-gate 	return (ndi_devctl_ioctl(pnx_state->pnx_devi, cmd, arg, mode, 0));
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * pseudonex_intr_op: pseudonex convert an interrupt number to an
305*7c478bd9Sstevel@tonic-gate  *			interrupt. NO OP for pseudo drivers.
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
308*7c478bd9Sstevel@tonic-gate static int
309*7c478bd9Sstevel@tonic-gate pseudonex_intr_op(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
310*7c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
313*7c478bd9Sstevel@tonic-gate }
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate static int
316*7c478bd9Sstevel@tonic-gate pseudonex_check_assignment(dev_info_t *child, int test_inst)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 	dev_info_t	*tdip;
319*7c478bd9Sstevel@tonic-gate 	kmutex_t	*dmp;
320*7c478bd9Sstevel@tonic-gate 	const char 	*childname = ddi_driver_name(child);
321*7c478bd9Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
324*7c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
325*7c478bd9Sstevel@tonic-gate 	for (tdip = devnamesp[childmaj].dn_head;
326*7c478bd9Sstevel@tonic-gate 	    tdip != NULL; tdip = ddi_get_next(tdip)) {
327*7c478bd9Sstevel@tonic-gate 		/* is this the current node? */
328*7c478bd9Sstevel@tonic-gate 		if (tdip == child)
329*7c478bd9Sstevel@tonic-gate 			continue;
330*7c478bd9Sstevel@tonic-gate 		/* is this a duplicate instance? */
331*7c478bd9Sstevel@tonic-gate 		if (test_inst == ddi_get_instance(tdip)) {
332*7c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
333*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
334*7c478bd9Sstevel@tonic-gate 		}
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
337*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate /*
341*7c478bd9Sstevel@tonic-gate  * This is a nasty, slow hack.  But we're stuck with it until we do some
342*7c478bd9Sstevel@tonic-gate  * major surgery on the instance assignment subsystem, to allow pseudonode
343*7c478bd9Sstevel@tonic-gate  * instance assignment to be tracked there.
344*7c478bd9Sstevel@tonic-gate  *
345*7c478bd9Sstevel@tonic-gate  * To auto-assign an instance number, we exhaustively search the instance
346*7c478bd9Sstevel@tonic-gate  * list for each possible instance number until we find one which is unused.
347*7c478bd9Sstevel@tonic-gate  */
348*7c478bd9Sstevel@tonic-gate static int
349*7c478bd9Sstevel@tonic-gate pseudonex_auto_assign(dev_info_t *child)
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate 	dev_info_t	*tdip;
352*7c478bd9Sstevel@tonic-gate 	kmutex_t	*dmp;
353*7c478bd9Sstevel@tonic-gate 	const char 	*childname = ddi_driver_name(child);
354*7c478bd9Sstevel@tonic-gate 	major_t		childmaj = ddi_name_to_major((char *)childname);
355*7c478bd9Sstevel@tonic-gate 	int inst = 0;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	dmp = &devnamesp[childmaj].dn_lock;
358*7c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(dmp);
359*7c478bd9Sstevel@tonic-gate 	for (inst = 0; inst <= MAXMIN32; inst++) {
360*7c478bd9Sstevel@tonic-gate 		for (tdip = devnamesp[childmaj].dn_head; tdip != NULL;
361*7c478bd9Sstevel@tonic-gate 		    tdip = ddi_get_next(tdip)) {
362*7c478bd9Sstevel@tonic-gate 			/* is this the current node? */
363*7c478bd9Sstevel@tonic-gate 			if (tdip == child)
364*7c478bd9Sstevel@tonic-gate 				continue;
365*7c478bd9Sstevel@tonic-gate 			if (inst == ddi_get_instance(tdip)) {
366*7c478bd9Sstevel@tonic-gate 				break;
367*7c478bd9Sstevel@tonic-gate 			}
368*7c478bd9Sstevel@tonic-gate 		}
369*7c478bd9Sstevel@tonic-gate 		if (tdip == NULL) {
370*7c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(dmp);
371*7c478bd9Sstevel@tonic-gate 			return (inst);
372*7c478bd9Sstevel@tonic-gate 		}
373*7c478bd9Sstevel@tonic-gate 	}
374*7c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(dmp);
375*7c478bd9Sstevel@tonic-gate 	return (-1);
376*7c478bd9Sstevel@tonic-gate }
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate static int
379*7c478bd9Sstevel@tonic-gate pseudonex_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
380*7c478bd9Sstevel@tonic-gate     void *arg, void *result)
381*7c478bd9Sstevel@tonic-gate {
382*7c478bd9Sstevel@tonic-gate 	switch (ctlop) {
383*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
384*7c478bd9Sstevel@tonic-gate 		if (rdip == NULL)
385*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
386*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?pseudo-device: %s%d\n",
387*7c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
388*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
391*7c478bd9Sstevel@tonic-gate 	{
392*7c478bd9Sstevel@tonic-gate 		char name[12];	/* enough for a decimal integer */
393*7c478bd9Sstevel@tonic-gate 		int instance = -1;
394*7c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
395*7c478bd9Sstevel@tonic-gate 		const char *childname = ddi_driver_name(child);
396*7c478bd9Sstevel@tonic-gate 		char **childlist;
397*7c478bd9Sstevel@tonic-gate 		uint_t nelems;
398*7c478bd9Sstevel@tonic-gate 		int auto_assign = 0;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 		/*
401*7c478bd9Sstevel@tonic-gate 		 * If this pseudonex node has a valid-children property,
402*7c478bd9Sstevel@tonic-gate 		 * then that acts as an access control list for children
403*7c478bd9Sstevel@tonic-gate 		 * allowed to attach beneath this node.  Honor it.
404*7c478bd9Sstevel@tonic-gate 		 */
405*7c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
406*7c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "valid-children", &childlist,
407*7c478bd9Sstevel@tonic-gate 		    &nelems) == DDI_PROP_SUCCESS) {
408*7c478bd9Sstevel@tonic-gate 			int i, ok = 0;
409*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < nelems; i++) {
410*7c478bd9Sstevel@tonic-gate 				if (strcmp(childlist[i], childname) == 0) {
411*7c478bd9Sstevel@tonic-gate 					ok = 1;
412*7c478bd9Sstevel@tonic-gate 					break;
413*7c478bd9Sstevel@tonic-gate 				}
414*7c478bd9Sstevel@tonic-gate 			}
415*7c478bd9Sstevel@tonic-gate 			ddi_prop_free(childlist);
416*7c478bd9Sstevel@tonic-gate 			if (!ok)
417*7c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 		/*
421*7c478bd9Sstevel@tonic-gate 		 * Look up the "instance" property. If it does not exist,
422*7c478bd9Sstevel@tonic-gate 		 * check to see if the "auto-assign-instance" property is set.
423*7c478bd9Sstevel@tonic-gate 		 * If not, default to using instance 0; while not ideal, this
424*7c478bd9Sstevel@tonic-gate 		 * is a legacy behavior we must continue to support.
425*7c478bd9Sstevel@tonic-gate 		 */
426*7c478bd9Sstevel@tonic-gate 		instance = ddi_prop_get_int(DDI_DEV_T_ANY, child,
427*7c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "instance", -1);
428*7c478bd9Sstevel@tonic-gate 		auto_assign = ddi_prop_exists(DDI_DEV_T_ANY, child,
429*7c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "auto-assign-instance");
430*7c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
431*7c478bd9Sstevel@tonic-gate 		    "pseudonex: DDI_CTLOPS_INITCHILD(instance=%d, "
432*7c478bd9Sstevel@tonic-gate 		    "auto-assign=%d)", instance, auto_assign));
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 		if (instance != -1 && auto_assign != 0) {
435*7c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "both instance and "
436*7c478bd9Sstevel@tonic-gate 			    "auto-assign-instance properties specified. "
437*7c478bd9Sstevel@tonic-gate 			    "Node rejected."));
438*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
439*7c478bd9Sstevel@tonic-gate 		}
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 		if (instance == -1 && auto_assign == 0) {
442*7c478bd9Sstevel@tonic-gate 			/* default to instance 0 if not specified */
443*7c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "defaulting to 0"));
444*7c478bd9Sstevel@tonic-gate 			instance = 0;
445*7c478bd9Sstevel@tonic-gate 		}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 		/*
448*7c478bd9Sstevel@tonic-gate 		 * If an instance has been specified, determine if this
449*7c478bd9Sstevel@tonic-gate 		 * instance is already in use; if we need to pick an instance,
450*7c478bd9Sstevel@tonic-gate 		 * we do it here.
451*7c478bd9Sstevel@tonic-gate 		 */
452*7c478bd9Sstevel@tonic-gate 		if (auto_assign) {
453*7c478bd9Sstevel@tonic-gate 			if ((instance = pseudonex_auto_assign(child)) == -1) {
454*7c478bd9Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_NOTE, "failed to "
455*7c478bd9Sstevel@tonic-gate 				    "auto-select instance for %s", childname));
456*7c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
457*7c478bd9Sstevel@tonic-gate 			}
458*7c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
459*7c478bd9Sstevel@tonic-gate 			    "auto-selected instance for %s: %d",
460*7c478bd9Sstevel@tonic-gate 			    childname, instance));
461*7c478bd9Sstevel@tonic-gate 		} else {
462*7c478bd9Sstevel@tonic-gate 			if (pseudonex_check_assignment(child, instance) ==
463*7c478bd9Sstevel@tonic-gate 			    DDI_FAILURE) {
464*7c478bd9Sstevel@tonic-gate 				NDI_CONFIG_DEBUG((CE_WARN,
465*7c478bd9Sstevel@tonic-gate 				    "Duplicate instance %d of node \"%s\" "
466*7c478bd9Sstevel@tonic-gate 				    "ignored.", instance, childname));
467*7c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
468*7c478bd9Sstevel@tonic-gate 			}
469*7c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
470*7c478bd9Sstevel@tonic-gate 			    "using fixed-assignment instance for %s: %d",
471*7c478bd9Sstevel@tonic-gate 			    childname, instance));
472*7c478bd9Sstevel@tonic-gate 		}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 		/*
475*7c478bd9Sstevel@tonic-gate 		 * Attach the instance number to the node. This allows
476*7c478bd9Sstevel@tonic-gate 		 * us to have multiple instances of the same pseudo
477*7c478bd9Sstevel@tonic-gate 		 * device, they will be named 'device@instance'. If this
478*7c478bd9Sstevel@tonic-gate 		 * breaks programs, we may need to special-case instance 0
479*7c478bd9Sstevel@tonic-gate 		 * into 'device'. Ick. devlinks appears to handle the
480*7c478bd9Sstevel@tonic-gate 		 * new names ok, so if only names in /dev are used
481*7c478bd9Sstevel@tonic-gate 		 * this may not be necessary.
482*7c478bd9Sstevel@tonic-gate 		 */
483*7c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%d", instance);
484*7c478bd9Sstevel@tonic-gate 		DEVI(child)->devi_instance = instance;
485*7c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
486*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
490*7c478bd9Sstevel@tonic-gate 	{
491*7c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
494*7c478bd9Sstevel@tonic-gate 		    "DDI_CTLOPS_UNINITCHILD(%s, instance=%d)",
495*7c478bd9Sstevel@tonic-gate 		    ddi_driver_name(child), DEVI(child)->devi_instance));
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
500*7c478bd9Sstevel@tonic-gate 	}
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
503*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
504*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
505*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
506*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NINTRS:
507*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
508*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
509*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
510*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INTR_HILEVEL:
511*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_XLATE_INTRS:
512*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
513*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
514*7c478bd9Sstevel@tonic-gate 		/*
515*7c478bd9Sstevel@tonic-gate 		 * These ops correspond to functions that "shouldn't" be called
516*7c478bd9Sstevel@tonic-gate 		 * by a pseudo driver.  So we whine when we're called.
517*7c478bd9Sstevel@tonic-gate 		 */
518*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
519*7c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), ctlop,
520*7c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
521*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
524*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOP:
525*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOPR:
526*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
527*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
528*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
529*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
530*7c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PTOB:
531*7c478bd9Sstevel@tonic-gate 	default:
532*7c478bd9Sstevel@tonic-gate 		/*
533*7c478bd9Sstevel@tonic-gate 		 * The ops that we pass up (default).  We pass up memory
534*7c478bd9Sstevel@tonic-gate 		 * allocation oriented ops that we receive - these may be
535*7c478bd9Sstevel@tonic-gate 		 * associated with pseudo HBA drivers below us with target
536*7c478bd9Sstevel@tonic-gate 		 * drivers below them that use ddi memory allocation
537*7c478bd9Sstevel@tonic-gate 		 * interfaces like scsi_alloc_consistent_buf.
538*7c478bd9Sstevel@tonic-gate 		 */
539*7c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate }
542