xref: /onnv-gate/usr/src/uts/common/io/tclient.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * generic mpxio leaf driver
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/errno.h>
35*0Sstevel@tonic-gate #include <sys/uio.h>
36*0Sstevel@tonic-gate #include <sys/buf.h>
37*0Sstevel@tonic-gate #include <sys/modctl.h>
38*0Sstevel@tonic-gate #include <sys/open.h>
39*0Sstevel@tonic-gate #include <sys/kmem.h>
40*0Sstevel@tonic-gate #include <sys/conf.h>
41*0Sstevel@tonic-gate #include <sys/cmn_err.h>
42*0Sstevel@tonic-gate #include <sys/stat.h>
43*0Sstevel@tonic-gate #include <sys/ddi.h>
44*0Sstevel@tonic-gate #include <sys/sunddi.h>
45*0Sstevel@tonic-gate #include <sys/sunndi.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static int tcli_open(dev_t *, int, int, cred_t *);
49*0Sstevel@tonic-gate static int tcli_close(dev_t, int, int, cred_t *);
50*0Sstevel@tonic-gate static int tcli_read(dev_t, struct uio *, cred_t *);
51*0Sstevel@tonic-gate static int tcli_write(dev_t, struct uio *, cred_t *);
52*0Sstevel@tonic-gate static int tcli_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
53*0Sstevel@tonic-gate static int tcli_attach(dev_info_t *, ddi_attach_cmd_t);
54*0Sstevel@tonic-gate static int tcli_detach(dev_info_t *, ddi_detach_cmd_t);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static int tcli_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate struct dstate {
59*0Sstevel@tonic-gate 	dev_info_t *dip;
60*0Sstevel@tonic-gate 	int oflag;
61*0Sstevel@tonic-gate };
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static void *dstates;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	INST_TO_MINOR(i)	(i)
66*0Sstevel@tonic-gate #define	MINOR_TO_INST(mn)	(mn)
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static struct cb_ops tcli_cb_ops = {
69*0Sstevel@tonic-gate 	tcli_open,			/* open */
70*0Sstevel@tonic-gate 	tcli_close,			/* close */
71*0Sstevel@tonic-gate 	nodev,				/* strategy */
72*0Sstevel@tonic-gate 	nodev,				/* print */
73*0Sstevel@tonic-gate 	nodev,				/* dump */
74*0Sstevel@tonic-gate 	tcli_read,			/* read */
75*0Sstevel@tonic-gate 	tcli_write,			/* write */
76*0Sstevel@tonic-gate 	tcli_ioctl,			/* ioctl */
77*0Sstevel@tonic-gate 	nodev,				/* devmap */
78*0Sstevel@tonic-gate 	nodev,				/* mmap */
79*0Sstevel@tonic-gate 	nodev,				/* segmap */
80*0Sstevel@tonic-gate 	nochpoll,			/* poll */
81*0Sstevel@tonic-gate 	ddi_prop_op,			/* prop_op */
82*0Sstevel@tonic-gate 	NULL,				/* streamtab */
83*0Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* flag */
84*0Sstevel@tonic-gate 	CB_REV,				/* cb_rev */
85*0Sstevel@tonic-gate 	nodev,				/* aread */
86*0Sstevel@tonic-gate 	nodev				/* awrite */
87*0Sstevel@tonic-gate };
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static struct dev_ops tcli_ops = {
91*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
92*0Sstevel@tonic-gate 	0,			/* refcnt */
93*0Sstevel@tonic-gate 	tcli_info,		/* getinfo */
94*0Sstevel@tonic-gate 	nulldev,		/* identify */
95*0Sstevel@tonic-gate 	nulldev,		/* probe */
96*0Sstevel@tonic-gate 	tcli_attach,		/* attach */
97*0Sstevel@tonic-gate 	tcli_detach,		/* detach */
98*0Sstevel@tonic-gate 	nodev,			/* reset */
99*0Sstevel@tonic-gate 	&tcli_cb_ops,		/* driver ops */
100*0Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus ops */
101*0Sstevel@tonic-gate 	NULL			/* power */
102*0Sstevel@tonic-gate };
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate static struct modldrv modldrv = {
105*0Sstevel@tonic-gate 	&mod_driverops,
106*0Sstevel@tonic-gate 	"vhci client test driver %I%",
107*0Sstevel@tonic-gate 	&tcli_ops
108*0Sstevel@tonic-gate };
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
111*0Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
112*0Sstevel@tonic-gate };
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate int
115*0Sstevel@tonic-gate _init(void)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	int e;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&dstates,
120*0Sstevel@tonic-gate 	    sizeof (struct dstate), 0)) != 0) {
121*0Sstevel@tonic-gate 		return (e);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
125*0Sstevel@tonic-gate 		ddi_soft_state_fini(&dstates);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	return (e);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate int
132*0Sstevel@tonic-gate _fini(void)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	int e;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
137*0Sstevel@tonic-gate 		return (e);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	ddi_soft_state_fini(&dstates);
140*0Sstevel@tonic-gate 	return (e);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate int
144*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*ARGSUSED*/
150*0Sstevel@tonic-gate static int
151*0Sstevel@tonic-gate tcli_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
154*0Sstevel@tonic-gate 	struct dstate *dstatep;
155*0Sstevel@tonic-gate 	int rval;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
158*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(dstates, instance) != DDI_SUCCESS) {
161*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: can't allocate state\n",
162*0Sstevel@tonic-gate 		    ddi_get_name(devi), instance);
163*0Sstevel@tonic-gate 		return (DDI_FAILURE);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
167*0Sstevel@tonic-gate 	dstatep->dip = devi;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	rval = ddi_create_minor_node(devi, "client", S_IFCHR,
170*0Sstevel@tonic-gate 	    (INST_TO_MINOR(instance)), DDI_PSEUDO, NULL);
171*0Sstevel@tonic-gate 	if (rval == DDI_FAILURE) {
172*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
173*0Sstevel@tonic-gate 		ddi_soft_state_free(dstates, instance);
174*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can't create minor nodes",
175*0Sstevel@tonic-gate 		    ddi_get_name(devi), instance);
176*0Sstevel@tonic-gate 		return (DDI_FAILURE);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	ddi_report_dev(devi);
180*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /*ARGSUSED*/
184*0Sstevel@tonic-gate static int
185*0Sstevel@tonic-gate tcli_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	int instance;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
190*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
193*0Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
194*0Sstevel@tonic-gate 	ddi_soft_state_free(dstates, instance);
195*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /* ARGSUSED */
199*0Sstevel@tonic-gate static int
200*0Sstevel@tonic-gate tcli_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	dev_t	dev;
203*0Sstevel@tonic-gate 	int	instance;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
206*0Sstevel@tonic-gate 		return (DDI_FAILURE);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	dev = (dev_t)arg;
209*0Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
210*0Sstevel@tonic-gate 	*result = (void *)(uintptr_t)instance;
211*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*ARGSUSED*/
216*0Sstevel@tonic-gate static int
217*0Sstevel@tonic-gate tcli_open(dev_t *devp, int flag, int otyp, cred_t *cred)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	minor_t minor;
220*0Sstevel@tonic-gate 	struct dstate *dstatep;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
223*0Sstevel@tonic-gate 		return (EINVAL);
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	minor = getminor(*devp);
226*0Sstevel@tonic-gate 	if ((dstatep = ddi_get_soft_state(dstates,
227*0Sstevel@tonic-gate 	    MINOR_TO_INST(minor))) == NULL)
228*0Sstevel@tonic-gate 		return (ENXIO);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	dstatep->oflag = 1;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return (0);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate /*ARGSUSED*/
236*0Sstevel@tonic-gate static int
237*0Sstevel@tonic-gate tcli_close(dev_t dev, int flag, int otyp, cred_t *cred)
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate 	struct dstate *dstatep;
240*0Sstevel@tonic-gate 	minor_t minor = getminor(dev);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
243*0Sstevel@tonic-gate 		return (EINVAL);
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (dstatep == NULL)
248*0Sstevel@tonic-gate 		return (ENXIO);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	dstatep->oflag = 0;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	return (0);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /*ARGSUSED*/
256*0Sstevel@tonic-gate static int
257*0Sstevel@tonic-gate tcli_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
258*0Sstevel@tonic-gate     int *rvalp)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	struct dstate *dstatep;
261*0Sstevel@tonic-gate 	int instance;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
264*0Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	if (dstatep == NULL)
267*0Sstevel@tonic-gate 		return (ENXIO);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	return (0);
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*ARGSUSED*/
273*0Sstevel@tonic-gate static int
274*0Sstevel@tonic-gate tcli_read(dev_t dev, struct uio *uiop, cred_t *credp)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	return (0);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /*ARGSUSED*/
280*0Sstevel@tonic-gate static int
281*0Sstevel@tonic-gate tcli_write(dev_t dev, struct uio *uiop, cred_t *credp)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	return (0);
284*0Sstevel@tonic-gate }
285