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 * generic mpxio leaf driver
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/uio.h>
347c478bd9Sstevel@tonic-gate #include <sys/buf.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/open.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static int tcli_open(dev_t *, int, int, cred_t *);
477c478bd9Sstevel@tonic-gate static int tcli_close(dev_t, int, int, cred_t *);
487c478bd9Sstevel@tonic-gate static int tcli_read(dev_t, struct uio *, cred_t *);
497c478bd9Sstevel@tonic-gate static int tcli_write(dev_t, struct uio *, cred_t *);
507c478bd9Sstevel@tonic-gate static int tcli_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
517c478bd9Sstevel@tonic-gate static int tcli_attach(dev_info_t *, ddi_attach_cmd_t);
527c478bd9Sstevel@tonic-gate static int tcli_detach(dev_info_t *, ddi_detach_cmd_t);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static int tcli_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate struct dstate {
577c478bd9Sstevel@tonic-gate dev_info_t *dip;
587c478bd9Sstevel@tonic-gate int oflag;
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static void *dstates;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define INST_TO_MINOR(i) (i)
647c478bd9Sstevel@tonic-gate #define MINOR_TO_INST(mn) (mn)
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate static struct cb_ops tcli_cb_ops = {
677c478bd9Sstevel@tonic-gate tcli_open, /* open */
687c478bd9Sstevel@tonic-gate tcli_close, /* close */
697c478bd9Sstevel@tonic-gate nodev, /* strategy */
707c478bd9Sstevel@tonic-gate nodev, /* print */
717c478bd9Sstevel@tonic-gate nodev, /* dump */
727c478bd9Sstevel@tonic-gate tcli_read, /* read */
737c478bd9Sstevel@tonic-gate tcli_write, /* write */
747c478bd9Sstevel@tonic-gate tcli_ioctl, /* ioctl */
757c478bd9Sstevel@tonic-gate nodev, /* devmap */
767c478bd9Sstevel@tonic-gate nodev, /* mmap */
777c478bd9Sstevel@tonic-gate nodev, /* segmap */
787c478bd9Sstevel@tonic-gate nochpoll, /* poll */
797c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */
807c478bd9Sstevel@tonic-gate NULL, /* streamtab */
817c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* flag */
827c478bd9Sstevel@tonic-gate CB_REV, /* cb_rev */
837c478bd9Sstevel@tonic-gate nodev, /* aread */
847c478bd9Sstevel@tonic-gate nodev /* awrite */
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate static struct dev_ops tcli_ops = {
897c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */
907c478bd9Sstevel@tonic-gate 0, /* refcnt */
917c478bd9Sstevel@tonic-gate tcli_info, /* getinfo */
927c478bd9Sstevel@tonic-gate nulldev, /* identify */
937c478bd9Sstevel@tonic-gate nulldev, /* probe */
947c478bd9Sstevel@tonic-gate tcli_attach, /* attach */
957c478bd9Sstevel@tonic-gate tcli_detach, /* detach */
967c478bd9Sstevel@tonic-gate nodev, /* reset */
977c478bd9Sstevel@tonic-gate &tcli_cb_ops, /* driver ops */
987c478bd9Sstevel@tonic-gate (struct bus_ops *)0, /* bus ops */
9919397407SSherry Moore NULL, /* power */
10019397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1047c478bd9Sstevel@tonic-gate &mod_driverops,
10519397407SSherry Moore "vhci client test driver",
1067c478bd9Sstevel@tonic-gate &tcli_ops
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1107c478bd9Sstevel@tonic-gate MODREV_1, &modldrv, NULL
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate int
_init(void)1147c478bd9Sstevel@tonic-gate _init(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate int e;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if ((e = ddi_soft_state_init(&dstates,
1197c478bd9Sstevel@tonic-gate sizeof (struct dstate), 0)) != 0) {
1207c478bd9Sstevel@tonic-gate return (e);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) != 0) {
1247c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&dstates);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate return (e);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate int
_fini(void)1317c478bd9Sstevel@tonic-gate _fini(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate int e;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) != 0) {
1367c478bd9Sstevel@tonic-gate return (e);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&dstates);
1397c478bd9Sstevel@tonic-gate return (e);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1437c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1497c478bd9Sstevel@tonic-gate static int
tcli_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1507c478bd9Sstevel@tonic-gate tcli_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate int instance = ddi_get_instance(devi);
1537c478bd9Sstevel@tonic-gate struct dstate *dstatep;
1547c478bd9Sstevel@tonic-gate int rval;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH)
1577c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(dstates, instance) != DDI_SUCCESS) {
1607c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "%s%d: can't allocate state\n",
1617c478bd9Sstevel@tonic-gate ddi_get_name(devi), instance);
1627c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate dstatep = ddi_get_soft_state(dstates, instance);
1667c478bd9Sstevel@tonic-gate dstatep->dip = devi;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate rval = ddi_create_minor_node(devi, "client", S_IFCHR,
169*a76903e2SToomas Soome (INST_TO_MINOR(instance)), DDI_PSEUDO, 0);
1707c478bd9Sstevel@tonic-gate if (rval == DDI_FAILURE) {
1717c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
1727c478bd9Sstevel@tonic-gate ddi_soft_state_free(dstates, instance);
1737c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: can't create minor nodes",
1747c478bd9Sstevel@tonic-gate ddi_get_name(devi), instance);
1757c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate ddi_report_dev(devi);
1797c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1837c478bd9Sstevel@tonic-gate static int
tcli_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)1847c478bd9Sstevel@tonic-gate tcli_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate int instance;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
1897c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
1927c478bd9Sstevel@tonic-gate instance = ddi_get_instance(devi);
1937c478bd9Sstevel@tonic-gate ddi_soft_state_free(dstates, instance);
1947c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /* ARGSUSED */
1987c478bd9Sstevel@tonic-gate static int
tcli_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1997c478bd9Sstevel@tonic-gate tcli_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate dev_t dev;
2027c478bd9Sstevel@tonic-gate int instance;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (infocmd != DDI_INFO_DEVT2INSTANCE)
2057c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate dev = (dev_t)arg;
2087c478bd9Sstevel@tonic-gate instance = MINOR_TO_INST(getminor(dev));
2097c478bd9Sstevel@tonic-gate *result = (void *)(uintptr_t)instance;
2107c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2157c478bd9Sstevel@tonic-gate static int
tcli_open(dev_t * devp,int flag,int otyp,cred_t * cred)2167c478bd9Sstevel@tonic-gate tcli_open(dev_t *devp, int flag, int otyp, cred_t *cred)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate minor_t minor;
2197c478bd9Sstevel@tonic-gate struct dstate *dstatep;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate if (otyp != OTYP_BLK && otyp != OTYP_CHR)
2227c478bd9Sstevel@tonic-gate return (EINVAL);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate minor = getminor(*devp);
2257c478bd9Sstevel@tonic-gate if ((dstatep = ddi_get_soft_state(dstates,
2267c478bd9Sstevel@tonic-gate MINOR_TO_INST(minor))) == NULL)
2277c478bd9Sstevel@tonic-gate return (ENXIO);
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate dstatep->oflag = 1;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate return (0);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2357c478bd9Sstevel@tonic-gate static int
tcli_close(dev_t dev,int flag,int otyp,cred_t * cred)2367c478bd9Sstevel@tonic-gate tcli_close(dev_t dev, int flag, int otyp, cred_t *cred)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate struct dstate *dstatep;
2397c478bd9Sstevel@tonic-gate minor_t minor = getminor(dev);
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate if (otyp != OTYP_BLK && otyp != OTYP_CHR)
2427c478bd9Sstevel@tonic-gate return (EINVAL);
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (dstatep == NULL)
2477c478bd9Sstevel@tonic-gate return (ENXIO);
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate dstatep->oflag = 0;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate return (0);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2557c478bd9Sstevel@tonic-gate static int
tcli_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2567c478bd9Sstevel@tonic-gate tcli_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
2577c478bd9Sstevel@tonic-gate int *rvalp)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate struct dstate *dstatep;
2607c478bd9Sstevel@tonic-gate int instance;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate instance = MINOR_TO_INST(getminor(dev));
2637c478bd9Sstevel@tonic-gate dstatep = ddi_get_soft_state(dstates, instance);
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (dstatep == NULL)
2667c478bd9Sstevel@tonic-gate return (ENXIO);
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate return (0);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2727c478bd9Sstevel@tonic-gate static int
tcli_read(dev_t dev,struct uio * uiop,cred_t * credp)2737c478bd9Sstevel@tonic-gate tcli_read(dev_t dev, struct uio *uiop, cred_t *credp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate return (0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2797c478bd9Sstevel@tonic-gate static int
tcli_write(dev_t dev,struct uio * uiop,cred_t * credp)2807c478bd9Sstevel@tonic-gate tcli_write(dev_t dev, struct uio *uiop, cred_t *credp)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate return (0);
2837c478bd9Sstevel@tonic-gate }
284