xref: /illumos-gate/usr/src/uts/common/io/tvhci.c (revision 19397407)
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
5144dfaa9Scth  * Common Development and Distribution License (the "License").
6144dfaa9Scth  * 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 /*
22*19397407SSherry 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  * The tvhci driver can be used to exercise the mpxio framework together
297c478bd9Sstevel@tonic-gate  * with tphci/tclient.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/scsi/scsi.h>
377c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/scsi_reset_notify.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
397c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/disp.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* cb_ops entry points */
437c478bd9Sstevel@tonic-gate static int tvhci_open(dev_t *, int, int, cred_t *);
447c478bd9Sstevel@tonic-gate static int tvhci_close(dev_t, int, int, cred_t *);
457c478bd9Sstevel@tonic-gate static int tvhci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
467c478bd9Sstevel@tonic-gate static int tvhci_attach(dev_info_t *, ddi_attach_cmd_t);
477c478bd9Sstevel@tonic-gate static int tvhci_detach(dev_info_t *, ddi_detach_cmd_t);
487c478bd9Sstevel@tonic-gate static int tvhci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* bus_ops entry points */
517c478bd9Sstevel@tonic-gate static int tvhci_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
527c478bd9Sstevel@tonic-gate     void *);
537c478bd9Sstevel@tonic-gate static int tvhci_initchild(dev_info_t *, dev_info_t *);
547c478bd9Sstevel@tonic-gate static int tvhci_uninitchild(dev_info_t *, dev_info_t *);
557c478bd9Sstevel@tonic-gate static int tvhci_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t, void *,
567c478bd9Sstevel@tonic-gate     dev_info_t **);
577c478bd9Sstevel@tonic-gate static int tvhci_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t,
587c478bd9Sstevel@tonic-gate     void *);
597c478bd9Sstevel@tonic-gate static int tvhci_intr_op(dev_info_t *dip, dev_info_t *rdip,
607c478bd9Sstevel@tonic-gate     ddi_intr_op_t op, ddi_intr_handle_impl_t *hdlp, void *result);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* vhci ops */
637c478bd9Sstevel@tonic-gate static int tvhci_pi_init(dev_info_t *, mdi_pathinfo_t *, int);
647c478bd9Sstevel@tonic-gate static int tvhci_pi_uninit(dev_info_t *, mdi_pathinfo_t *, int);
657c478bd9Sstevel@tonic-gate static int tvhci_pi_state_change(dev_info_t *, mdi_pathinfo_t *,
667c478bd9Sstevel@tonic-gate     mdi_pathinfo_state_t, uint32_t, int);
677c478bd9Sstevel@tonic-gate static int tvhci_failover(dev_info_t *, dev_info_t *, int);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void *tvhci_state;
707c478bd9Sstevel@tonic-gate struct tvhci_state {
717c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static mdi_vhci_ops_t tvhci_opinfo = {
757c478bd9Sstevel@tonic-gate 	MDI_VHCI_OPS_REV,
767c478bd9Sstevel@tonic-gate 	tvhci_pi_init,
777c478bd9Sstevel@tonic-gate 	tvhci_pi_uninit,
787c478bd9Sstevel@tonic-gate 	tvhci_pi_state_change,
797c478bd9Sstevel@tonic-gate 	tvhci_failover
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static struct cb_ops tvhci_cb_ops = {
837c478bd9Sstevel@tonic-gate 	tvhci_open,			/* open */
847c478bd9Sstevel@tonic-gate 	tvhci_close,			/* close */
857c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
867c478bd9Sstevel@tonic-gate 	nodev,				/* print */
877c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
887c478bd9Sstevel@tonic-gate 	nodev,				/* read */
897c478bd9Sstevel@tonic-gate 	nodev,				/* write */
907c478bd9Sstevel@tonic-gate 	tvhci_ioctl,			/* ioctl */
917c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
927c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
937c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
947c478bd9Sstevel@tonic-gate 	nochpoll,			/* chpoll */
957c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
967c478bd9Sstevel@tonic-gate 	0,				/* streamtab */
977c478bd9Sstevel@tonic-gate 	D_NEW | D_MP,			/* cb_flag */
987c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
997c478bd9Sstevel@tonic-gate 	nodev,				/* aread */
1007c478bd9Sstevel@tonic-gate 	nodev				/* awrite */
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static struct bus_ops tvhci_bus_ops = {
1047c478bd9Sstevel@tonic-gate 	BUSO_REV,			/* busops_rev */
1057c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1067c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1077c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_interspec */
1087c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_interspec */
1097c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,		/* bus_map_fault */
1107c478bd9Sstevel@tonic-gate 	ddi_no_dma_map,			/* bus_dma_map */
1117c478bd9Sstevel@tonic-gate 	ddi_no_dma_allochdl,		/* bus_dma_allochdl */
1127c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_freehdl */
1137c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_bindhdl */
1147c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_unbindhdl */
1157c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_flush */
1167c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_win */
1177c478bd9Sstevel@tonic-gate 	NULL,				/* bus_dma_ctl */
1187c478bd9Sstevel@tonic-gate 	tvhci_ctl,			/* bus_ctl */
1197c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1207c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_eventcookie */
1217c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_eventcall */
1227c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_event */
1237c478bd9Sstevel@tonic-gate 	NULL,				/* bus_post_event */
1247c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1257c478bd9Sstevel@tonic-gate 	tvhci_bus_config,		/* bus_config */
1267c478bd9Sstevel@tonic-gate 	tvhci_bus_unconfig,		/* bus_unconfig */
1277c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1287c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1297c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1307c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1317c478bd9Sstevel@tonic-gate 	NULL,				/* bus_power */
1327c478bd9Sstevel@tonic-gate 	tvhci_intr_op			/* bus_intr_op */
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static struct dev_ops tvhci_ops = {
1367c478bd9Sstevel@tonic-gate 	DEVO_REV,
1377c478bd9Sstevel@tonic-gate 	0,
1387c478bd9Sstevel@tonic-gate 	tvhci_getinfo,
1397c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1407c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1417c478bd9Sstevel@tonic-gate 	tvhci_attach,		/* attach and detach are mandatory */
1427c478bd9Sstevel@tonic-gate 	tvhci_detach,
1437c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1447c478bd9Sstevel@tonic-gate 	&tvhci_cb_ops,		/* cb_ops */
1457c478bd9Sstevel@tonic-gate 	&tvhci_bus_ops,		/* bus_ops */
1467c478bd9Sstevel@tonic-gate 	NULL,			/* power */
147*19397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1537c478bd9Sstevel@tonic-gate 	&mod_driverops,
154*19397407SSherry Moore 	"test vhci driver",
1557c478bd9Sstevel@tonic-gate 	&tvhci_ops
1567c478bd9Sstevel@tonic-gate };
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1597c478bd9Sstevel@tonic-gate 	MODREV_1,
1607c478bd9Sstevel@tonic-gate 	&modldrv,
1617c478bd9Sstevel@tonic-gate 	NULL
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate int
_init(void)1657c478bd9Sstevel@tonic-gate _init(void)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	int rval;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if ((rval = ddi_soft_state_init(&tvhci_state,
1707c478bd9Sstevel@tonic-gate 	    sizeof (struct tvhci_state), 2)) != 0) {
1717c478bd9Sstevel@tonic-gate 		return (rval);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
1757c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&tvhci_state);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (rval);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate int
_fini(void)1827c478bd9Sstevel@tonic-gate _fini(void)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	int rval;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * don't start cleaning up until we know that the module remove
1887c478bd9Sstevel@tonic-gate 	 * has worked  -- if this works, then we know that each instance
1897c478bd9Sstevel@tonic-gate 	 * has successfully been detached
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	if ((rval = mod_remove(&modlinkage)) != 0) {
1927c478bd9Sstevel@tonic-gate 		return (rval);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&tvhci_state);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	return (rval);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2017c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /* ARGSUSED */
2077c478bd9Sstevel@tonic-gate static int
tvhci_open(dev_t * devp,int flag,int otype,cred_t * credp)2087c478bd9Sstevel@tonic-gate tvhci_open(dev_t *devp, int flag, int otype, cred_t *credp)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	struct tvhci_state *vhci;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (otype != OTYP_CHR) {
2137c478bd9Sstevel@tonic-gate 		return (EINVAL);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	vhci = ddi_get_soft_state(tvhci_state, getminor(*devp));
2177c478bd9Sstevel@tonic-gate 	if (vhci == NULL) {
2187c478bd9Sstevel@tonic-gate 		return (ENXIO);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (0);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* ARGSUSED */
2267c478bd9Sstevel@tonic-gate static int
tvhci_close(dev_t dev,int flag,int otype,cred_t * credp)2277c478bd9Sstevel@tonic-gate tvhci_close(dev_t dev, int flag, int otype, cred_t *credp)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	struct tvhci_state *vhci;
2307c478bd9Sstevel@tonic-gate 	if (otype != OTYP_CHR) {
2317c478bd9Sstevel@tonic-gate 		return (EINVAL);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	vhci = ddi_get_soft_state(tvhci_state, getminor(dev));
2357c478bd9Sstevel@tonic-gate 	if (vhci == NULL) {
2367c478bd9Sstevel@tonic-gate 		return (ENXIO);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (0);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /* ARGSUSED */
2437c478bd9Sstevel@tonic-gate static int
tvhci_ioctl(dev_t dev,int cmd,intptr_t data,int mode,cred_t * credp,int * rval)2447c478bd9Sstevel@tonic-gate tvhci_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
2457c478bd9Sstevel@tonic-gate 	cred_t *credp, int *rval)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	return (0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * attach the module
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate static int
tvhci_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2547c478bd9Sstevel@tonic-gate tvhci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	char *vclass;
2577c478bd9Sstevel@tonic-gate 	int instance, vhci_regis = 0;
2587c478bd9Sstevel@tonic-gate 	struct tvhci_state *vhci = NULL;
2597c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	switch (cmd) {
2647c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2657c478bd9Sstevel@tonic-gate 		break;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
2687c478bd9Sstevel@tonic-gate 	case DDI_PM_RESUME:
2697c478bd9Sstevel@tonic-gate 		return (0);	/* nothing to do */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	default:
2727c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Allocate vhci data structure.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(tvhci_state, instance) != DDI_SUCCESS) {
2797c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	vhci = ddi_get_soft_state(tvhci_state, instance);
2837c478bd9Sstevel@tonic-gate 	ASSERT(vhci != NULL);
2847c478bd9Sstevel@tonic-gate 	vhci->dip = dip;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* parent must be /pshot */
2877c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
2887c478bd9Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(pdip), "pshot") != 0 ||
2897c478bd9Sstevel@tonic-gate 	    ddi_get_parent(pdip) != ddi_root_node()) {
2907c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "tvhci must be under /pshot/");
2917c478bd9Sstevel@tonic-gate 		goto attach_fail;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * XXX add mpxio-disable property. need to remove the check
2967c478bd9Sstevel@tonic-gate 	 *	from the framework
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
2997c478bd9Sstevel@tonic-gate 	    "mpxio-disable", "no");
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* bus_addr is the <vhci_class> */
3027c478bd9Sstevel@tonic-gate 	vclass = ddi_get_name_addr(dip);
3037c478bd9Sstevel@tonic-gate 	if (vclass == NULL || vclass[1] == '\0') {
3047c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "tvhci invalid vhci class");
3057c478bd9Sstevel@tonic-gate 		goto attach_fail;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Attach this instance with the mpxio framework
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (mdi_vhci_register(vclass, dip, &tvhci_opinfo, 0) != MDI_SUCCESS) {
3127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s mdi_vhci_register failed",
3137c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip));
3147c478bd9Sstevel@tonic-gate 		goto attach_fail;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	vhci_regis++;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
3197c478bd9Sstevel@tonic-gate 	    instance, DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
3207c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s ddi_create_minor_node failed",
3217c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip));
3227c478bd9Sstevel@tonic-gate 		goto attach_fail;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1);
3267c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
3277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate attach_fail:
3307c478bd9Sstevel@tonic-gate 	if (vhci_regis)
3317c478bd9Sstevel@tonic-gate 		(void) mdi_vhci_unregister(dip, 0);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(tvhci_state, instance);
3347c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3397c478bd9Sstevel@tonic-gate static int
tvhci_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3407c478bd9Sstevel@tonic-gate tvhci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	switch (cmd) {
3457c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3497c478bd9Sstevel@tonic-gate 	case DDI_PM_SUSPEND:
3507c478bd9Sstevel@tonic-gate 		return (0);	/* nothing to do */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	default:
3537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (mdi_vhci_unregister(dip, 0) != MDI_SUCCESS)
3577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3607c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(tvhci_state, instance);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * tvhci_getinfo()
3677c478bd9Sstevel@tonic-gate  * Given the device number, return the devinfo pointer or the
3687c478bd9Sstevel@tonic-gate  * instance number.
3697c478bd9Sstevel@tonic-gate  * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3737c478bd9Sstevel@tonic-gate static int
tvhci_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3747c478bd9Sstevel@tonic-gate tvhci_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	struct tvhci_state *vhci;
3777c478bd9Sstevel@tonic-gate 	int instance = getminor((dev_t)arg);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	switch (cmd) {
3807c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3817c478bd9Sstevel@tonic-gate 		vhci = ddi_get_soft_state(tvhci_state, instance);
3827c478bd9Sstevel@tonic-gate 		if (vhci != NULL)
3837c478bd9Sstevel@tonic-gate 			*result = vhci->dip;
3847c478bd9Sstevel@tonic-gate 		else {
3857c478bd9Sstevel@tonic-gate 			*result = NULL;
3867c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		break;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3917c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	default:
3957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4027c478bd9Sstevel@tonic-gate static int
tvhci_pi_init(dev_info_t * vdip,mdi_pathinfo_t * pip,int flags)4037c478bd9Sstevel@tonic-gate tvhci_pi_init(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4097c478bd9Sstevel@tonic-gate static int
tvhci_pi_uninit(dev_info_t * vdip,mdi_pathinfo_t * pip,int flags)4107c478bd9Sstevel@tonic-gate tvhci_pi_uninit(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4167c478bd9Sstevel@tonic-gate static int
tvhci_pi_state_change(dev_info_t * vdip,mdi_pathinfo_t * pip,mdi_pathinfo_state_t state,uint32_t ext_state,int flags)4177c478bd9Sstevel@tonic-gate tvhci_pi_state_change(dev_info_t *vdip, mdi_pathinfo_t *pip,
4187c478bd9Sstevel@tonic-gate     mdi_pathinfo_state_t state, uint32_t ext_state, int flags)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4247c478bd9Sstevel@tonic-gate static int
tvhci_failover(dev_info_t * vdip,dev_info_t * cdip,int flags)4257c478bd9Sstevel@tonic-gate tvhci_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * Interrupt stuff. NO OP for pseudo drivers.
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4347c478bd9Sstevel@tonic-gate static int
tvhci_intr_op(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t op,ddi_intr_handle_impl_t * hdlp,void * result)4357c478bd9Sstevel@tonic-gate tvhci_intr_op(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
4367c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4427c478bd9Sstevel@tonic-gate static int
tvhci_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)4437c478bd9Sstevel@tonic-gate tvhci_ctl(dev_info_t *dip, dev_info_t *rdip,
4447c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	switch (ctlop) {
4477c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
4487c478bd9Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
4497c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4507c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?tvhci-device: %s%d\n",
4517c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip));
4527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
4557c478bd9Sstevel@tonic-gate 	{
4567c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
4577c478bd9Sstevel@tonic-gate 		return (tvhci_initchild(dip, child));
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
4617c478bd9Sstevel@tonic-gate 	{
4627c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
4637c478bd9Sstevel@tonic-gate 		return (tvhci_uninitchild(dip, child));
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
4677c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
4687c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
4697c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
4707c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
4717c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
4727c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
4737c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
4747c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * These ops correspond to functions that "shouldn't" be called
4777c478bd9Sstevel@tonic-gate 		 * by a pseudo driver.  So we whine when we're called.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
480*19397407SSherry Moore 		    ddi_get_name(dip), ddi_get_instance(dip),
481*19397407SSherry Moore 		    ctlop, ddi_get_name(rdip), ddi_get_instance(rdip));
4827c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
4857c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOP:
4867c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_BTOPR:
4877c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
4887c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
4897c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
4907c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
4917c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PTOB:
4927c478bd9Sstevel@tonic-gate 	default:
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * The ops that we pass up (default).  We pass up memory
4957c478bd9Sstevel@tonic-gate 		 * allocation oriented ops that we receive - these may be
4967c478bd9Sstevel@tonic-gate 		 * associated with pseudo HBA drivers below us with target
4977c478bd9Sstevel@tonic-gate 		 * drivers below them that use ddi memory allocation
4987c478bd9Sstevel@tonic-gate 		 * interfaces like scsi_alloc_consistent_buf.
4997c478bd9Sstevel@tonic-gate 		 */
5007c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /* set devi_addr to "g<guid>" */
5057c478bd9Sstevel@tonic-gate static int
tvhci_initchild(dev_info_t * dip,dev_info_t * child)5067c478bd9Sstevel@tonic-gate tvhci_initchild(dev_info_t *dip, dev_info_t *child)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
5097c478bd9Sstevel@tonic-gate 	char *guid, *addr;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
5127c478bd9Sstevel@tonic-gate 	    MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
5137c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "tvhci_initchild - no guid property");
5147c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	addr = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5187c478bd9Sstevel@tonic-gate 	(void) snprintf(addr, MAXNAMELEN, "g%s", guid);
5197c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(child, addr);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	kmem_free(addr, MAXNAMELEN);
5227c478bd9Sstevel@tonic-gate 	ddi_prop_free(guid);
5237c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5277c478bd9Sstevel@tonic-gate static int
tvhci_uninitchild(dev_info_t * dip,dev_info_t * child)5287c478bd9Sstevel@tonic-gate tvhci_uninitchild(dev_info_t *dip, dev_info_t *child)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(child, NULL);
5317c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /* form paddr by cname@<phci_inst>,<guid> */
5357c478bd9Sstevel@tonic-gate static char *
tvh_get_phci_devname(char * cname,char * guid,dev_info_t * pdip,char * pname,int len)5367c478bd9Sstevel@tonic-gate tvh_get_phci_devname(char *cname, char *guid,
5377c478bd9Sstevel@tonic-gate     dev_info_t *pdip, char *pname, int len)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, len, "%s@%d,%s",
5407c478bd9Sstevel@tonic-gate 	    cname, ddi_get_instance(pdip), guid);
5417c478bd9Sstevel@tonic-gate 	return (pname);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5445e3986cbScth /*
5455e3986cbScth  * Return a pointer to the guid part of the devnm.
5465e3986cbScth  * devnm format is "nodename@busaddr", busaddr format is "gGUID".
5475e3986cbScth  */
5485e3986cbScth static char *
tvhci_devnm_to_guid(char * devnm)5495e3986cbScth tvhci_devnm_to_guid(char *devnm)
5507c478bd9Sstevel@tonic-gate {
5515e3986cbScth 	char *cp = devnm;
5527c478bd9Sstevel@tonic-gate 
5535e3986cbScth 	if (devnm == NULL)
5545e3986cbScth 		return (NULL);
5557c478bd9Sstevel@tonic-gate 
5565e3986cbScth 	while (*cp != '\0' && *cp != '@')
5575e3986cbScth 		cp++;
5585e3986cbScth 	if (*cp == '@' && *(cp + 1) == 'g')
5595e3986cbScth 		return (cp + 2);
5605e3986cbScth 	return (NULL);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static int
tvhci_bus_config(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** child)5645e3986cbScth tvhci_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
5655e3986cbScth     void *arg, dev_info_t **child)
5667c478bd9Sstevel@tonic-gate {
5675e3986cbScth 	char *guid;
5685e3986cbScth 
5695e3986cbScth 	if (op == BUS_CONFIG_ONE || op == BUS_UNCONFIG_ONE)
5705e3986cbScth 		guid = tvhci_devnm_to_guid((char *)arg);
5715e3986cbScth 	else
5725e3986cbScth 		guid = NULL;
5735e3986cbScth 
5745e3986cbScth 	if (mdi_vhci_bus_config(pdip, flags, op, arg, child, guid)
5755e3986cbScth 	    == MDI_SUCCESS)
5765e3986cbScth 		return (NDI_SUCCESS);
5775e3986cbScth 	else
5785e3986cbScth 		return (NDI_FAILURE);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate static int
tvhci_bus_unconfig(dev_info_t * parent,uint_t flags,ddi_bus_config_op_t op,void * arg)5827c478bd9Sstevel@tonic-gate tvhci_bus_unconfig(dev_info_t *parent, uint_t flags,
5837c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t op, void *arg)
5847c478bd9Sstevel@tonic-gate {
5855e3986cbScth 	return (ndi_busop_bus_unconfig(parent, flags, op, arg));
5867c478bd9Sstevel@tonic-gate }
587