xref: /onnv-gate/usr/src/uts/common/io/tclient.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  * generic mpxio leaf driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/uio.h>
340Sstevel@tonic-gate #include <sys/buf.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/open.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/conf.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/sunndi.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static int tcli_open(dev_t *, int, int, cred_t *);
470Sstevel@tonic-gate static int tcli_close(dev_t, int, int, cred_t *);
480Sstevel@tonic-gate static int tcli_read(dev_t, struct uio *, cred_t *);
490Sstevel@tonic-gate static int tcli_write(dev_t, struct uio *, cred_t *);
500Sstevel@tonic-gate static int tcli_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
510Sstevel@tonic-gate static int tcli_attach(dev_info_t *, ddi_attach_cmd_t);
520Sstevel@tonic-gate static int tcli_detach(dev_info_t *, ddi_detach_cmd_t);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static int tcli_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate struct dstate {
570Sstevel@tonic-gate 	dev_info_t *dip;
580Sstevel@tonic-gate 	int oflag;
590Sstevel@tonic-gate };
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static void *dstates;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	INST_TO_MINOR(i)	(i)
640Sstevel@tonic-gate #define	MINOR_TO_INST(mn)	(mn)
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static struct cb_ops tcli_cb_ops = {
670Sstevel@tonic-gate 	tcli_open,			/* open */
680Sstevel@tonic-gate 	tcli_close,			/* close */
690Sstevel@tonic-gate 	nodev,				/* strategy */
700Sstevel@tonic-gate 	nodev,				/* print */
710Sstevel@tonic-gate 	nodev,				/* dump */
720Sstevel@tonic-gate 	tcli_read,			/* read */
730Sstevel@tonic-gate 	tcli_write,			/* write */
740Sstevel@tonic-gate 	tcli_ioctl,			/* ioctl */
750Sstevel@tonic-gate 	nodev,				/* devmap */
760Sstevel@tonic-gate 	nodev,				/* mmap */
770Sstevel@tonic-gate 	nodev,				/* segmap */
780Sstevel@tonic-gate 	nochpoll,			/* poll */
790Sstevel@tonic-gate 	ddi_prop_op,			/* prop_op */
800Sstevel@tonic-gate 	NULL,				/* streamtab */
810Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* flag */
820Sstevel@tonic-gate 	CB_REV,				/* cb_rev */
830Sstevel@tonic-gate 	nodev,				/* aread */
840Sstevel@tonic-gate 	nodev				/* awrite */
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static struct dev_ops tcli_ops = {
890Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
900Sstevel@tonic-gate 	0,			/* refcnt */
910Sstevel@tonic-gate 	tcli_info,		/* getinfo */
920Sstevel@tonic-gate 	nulldev,		/* identify */
930Sstevel@tonic-gate 	nulldev,		/* probe */
940Sstevel@tonic-gate 	tcli_attach,		/* attach */
950Sstevel@tonic-gate 	tcli_detach,		/* detach */
960Sstevel@tonic-gate 	nodev,			/* reset */
970Sstevel@tonic-gate 	&tcli_cb_ops,		/* driver ops */
980Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus ops */
99*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
100*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static struct modldrv modldrv = {
1040Sstevel@tonic-gate 	&mod_driverops,
105*7656SSherry.Moore@Sun.COM 	"vhci client test driver",
1060Sstevel@tonic-gate 	&tcli_ops
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static struct modlinkage modlinkage = {
1100Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
_init(void)1140Sstevel@tonic-gate _init(void)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	int e;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&dstates,
1190Sstevel@tonic-gate 	    sizeof (struct dstate), 0)) != 0) {
1200Sstevel@tonic-gate 		return (e);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)  {
1240Sstevel@tonic-gate 		ddi_soft_state_fini(&dstates);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (e);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate int
_fini(void)1310Sstevel@tonic-gate _fini(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int e;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)  {
1360Sstevel@tonic-gate 		return (e);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	ddi_soft_state_fini(&dstates);
1390Sstevel@tonic-gate 	return (e);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1430Sstevel@tonic-gate _info(struct modinfo *modinfop)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*ARGSUSED*/
1490Sstevel@tonic-gate static int
tcli_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1500Sstevel@tonic-gate tcli_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
1530Sstevel@tonic-gate 	struct dstate *dstatep;
1540Sstevel@tonic-gate 	int rval;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
1570Sstevel@tonic-gate 		return (DDI_SUCCESS);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(dstates, instance) != DDI_SUCCESS) {
1600Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: can't allocate state\n",
1610Sstevel@tonic-gate 		    ddi_get_name(devi), instance);
1620Sstevel@tonic-gate 		return (DDI_FAILURE);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
1660Sstevel@tonic-gate 	dstatep->dip = devi;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	rval = ddi_create_minor_node(devi, "client", S_IFCHR,
1690Sstevel@tonic-gate 	    (INST_TO_MINOR(instance)), DDI_PSEUDO, NULL);
1700Sstevel@tonic-gate 	if (rval == DDI_FAILURE) {
1710Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
1720Sstevel@tonic-gate 		ddi_soft_state_free(dstates, instance);
1730Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can't create minor nodes",
1740Sstevel@tonic-gate 		    ddi_get_name(devi), instance);
1750Sstevel@tonic-gate 		return (DDI_FAILURE);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	ddi_report_dev(devi);
1790Sstevel@tonic-gate 	return (DDI_SUCCESS);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*ARGSUSED*/
1830Sstevel@tonic-gate static int
tcli_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)1840Sstevel@tonic-gate tcli_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	int instance;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
1890Sstevel@tonic-gate 		return (DDI_SUCCESS);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
1920Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
1930Sstevel@tonic-gate 	ddi_soft_state_free(dstates, instance);
1940Sstevel@tonic-gate 	return (DDI_SUCCESS);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /* ARGSUSED */
1980Sstevel@tonic-gate static int
tcli_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1990Sstevel@tonic-gate tcli_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	dev_t	dev;
2020Sstevel@tonic-gate 	int	instance;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
2050Sstevel@tonic-gate 		return (DDI_FAILURE);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	dev = (dev_t)arg;
2080Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
2090Sstevel@tonic-gate 	*result = (void *)(uintptr_t)instance;
2100Sstevel@tonic-gate 	return (DDI_SUCCESS);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*ARGSUSED*/
2150Sstevel@tonic-gate static int
tcli_open(dev_t * devp,int flag,int otyp,cred_t * cred)2160Sstevel@tonic-gate tcli_open(dev_t *devp, int flag, int otyp, cred_t *cred)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	minor_t minor;
2190Sstevel@tonic-gate 	struct dstate *dstatep;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
2220Sstevel@tonic-gate 		return (EINVAL);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	minor = getminor(*devp);
2250Sstevel@tonic-gate 	if ((dstatep = ddi_get_soft_state(dstates,
2260Sstevel@tonic-gate 	    MINOR_TO_INST(minor))) == NULL)
2270Sstevel@tonic-gate 		return (ENXIO);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	dstatep->oflag = 1;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	return (0);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*ARGSUSED*/
2350Sstevel@tonic-gate static int
tcli_close(dev_t dev,int flag,int otyp,cred_t * cred)2360Sstevel@tonic-gate tcli_close(dev_t dev, int flag, int otyp, cred_t *cred)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	struct dstate *dstatep;
2390Sstevel@tonic-gate 	minor_t minor = getminor(dev);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
2420Sstevel@tonic-gate 		return (EINVAL);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (dstatep == NULL)
2470Sstevel@tonic-gate 		return (ENXIO);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	dstatep->oflag = 0;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	return (0);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*ARGSUSED*/
2550Sstevel@tonic-gate static int
tcli_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2560Sstevel@tonic-gate tcli_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
2570Sstevel@tonic-gate     int *rvalp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	struct dstate *dstatep;
2600Sstevel@tonic-gate 	int instance;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
2630Sstevel@tonic-gate 	dstatep = ddi_get_soft_state(dstates, instance);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	if (dstatep == 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
tcli_read(dev_t dev,struct uio * uiop,cred_t * credp)2730Sstevel@tonic-gate tcli_read(dev_t dev, struct uio *uiop, cred_t *credp)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	return (0);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*ARGSUSED*/
2790Sstevel@tonic-gate static int
tcli_write(dev_t dev,struct uio * uiop,cred_t * credp)2800Sstevel@tonic-gate tcli_write(dev_t dev, struct uio *uiop, cred_t *credp)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	return (0);
2830Sstevel@tonic-gate }
284