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 /*
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
26*3fe80ca4SDan Cross /*
27*3fe80ca4SDan Cross * Copyright 2023 Oxide Computer Company
28*3fe80ca4SDan Cross */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * The tphci driver can be used to exercise the mpxio framework together
327c478bd9Sstevel@tonic-gate * with tvhci/tclient.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/open.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
447c478bd9Sstevel@tonic-gate #include <sys/disp.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /* cb_ops entry points */
477c478bd9Sstevel@tonic-gate static int tphci_open(dev_t *, int, int, cred_t *);
487c478bd9Sstevel@tonic-gate static int tphci_close(dev_t, int, int, cred_t *);
497c478bd9Sstevel@tonic-gate static int tphci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
507c478bd9Sstevel@tonic-gate static int tphci_attach(dev_info_t *, ddi_attach_cmd_t);
517c478bd9Sstevel@tonic-gate static int tphci_detach(dev_info_t *, ddi_detach_cmd_t);
527c478bd9Sstevel@tonic-gate static int tphci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /* bus_ops entry points */
557c478bd9Sstevel@tonic-gate static int tphci_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
567c478bd9Sstevel@tonic-gate void *);
577c478bd9Sstevel@tonic-gate static int tphci_initchild(dev_info_t *, dev_info_t *);
587c478bd9Sstevel@tonic-gate static int tphci_uninitchild(dev_info_t *, dev_info_t *);
597c478bd9Sstevel@tonic-gate static int tphci_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t, void *,
607c478bd9Sstevel@tonic-gate dev_info_t **);
617c478bd9Sstevel@tonic-gate static int tphci_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t,
627c478bd9Sstevel@tonic-gate void *);
637c478bd9Sstevel@tonic-gate static int tphci_intr_op(dev_info_t *dip, dev_info_t *rdip,
647c478bd9Sstevel@tonic-gate ddi_intr_op_t op, ddi_intr_handle_impl_t *hdlp, void *result);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate static void *tphci_state;
687c478bd9Sstevel@tonic-gate struct tphci_state {
697c478bd9Sstevel@tonic-gate dev_info_t *dip;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static struct cb_ops tphci_cb_ops = {
737c478bd9Sstevel@tonic-gate tphci_open, /* open */
747c478bd9Sstevel@tonic-gate tphci_close, /* close */
757c478bd9Sstevel@tonic-gate nodev, /* strategy */
767c478bd9Sstevel@tonic-gate nodev, /* print */
777c478bd9Sstevel@tonic-gate nodev, /* dump */
787c478bd9Sstevel@tonic-gate nodev, /* read */
797c478bd9Sstevel@tonic-gate nodev, /* write */
807c478bd9Sstevel@tonic-gate tphci_ioctl, /* ioctl */
817c478bd9Sstevel@tonic-gate nodev, /* devmap */
827c478bd9Sstevel@tonic-gate nodev, /* mmap */
837c478bd9Sstevel@tonic-gate nodev, /* segmap */
847c478bd9Sstevel@tonic-gate nochpoll, /* chpoll */
857c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
867c478bd9Sstevel@tonic-gate 0, /* streamtab */
877c478bd9Sstevel@tonic-gate D_NEW | D_MP, /* cb_flag */
887c478bd9Sstevel@tonic-gate CB_REV, /* rev */
897c478bd9Sstevel@tonic-gate nodev, /* aread */
907c478bd9Sstevel@tonic-gate nodev /* awrite */
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate static struct bus_ops tphci_bus_ops = {
947c478bd9Sstevel@tonic-gate BUSO_REV, /* busops_rev */
957c478bd9Sstevel@tonic-gate nullbusmap, /* bus_map */
967c478bd9Sstevel@tonic-gate NULL, /* bus_get_intrspec */
977c478bd9Sstevel@tonic-gate NULL, /* bus_add_interspec */
987c478bd9Sstevel@tonic-gate NULL, /* bus_remove_interspec */
997c478bd9Sstevel@tonic-gate i_ddi_map_fault, /* bus_map_fault */
1007c478bd9Sstevel@tonic-gate ddi_no_dma_map, /* bus_dma_map */
1017c478bd9Sstevel@tonic-gate ddi_no_dma_allochdl, /* bus_dma_allochdl */
1027c478bd9Sstevel@tonic-gate NULL, /* bus_dma_freehdl */
1037c478bd9Sstevel@tonic-gate NULL, /* bus_dma_bindhdl */
1047c478bd9Sstevel@tonic-gate NULL, /* bus_dma_unbindhdl */
1057c478bd9Sstevel@tonic-gate NULL, /* bus_dma_flush */
1067c478bd9Sstevel@tonic-gate NULL, /* bus_dma_win */
1077c478bd9Sstevel@tonic-gate NULL, /* bus_dma_ctl */
1087c478bd9Sstevel@tonic-gate tphci_ctl, /* bus_ctl */
1097c478bd9Sstevel@tonic-gate ddi_bus_prop_op, /* bus_prop_op */
1107c478bd9Sstevel@tonic-gate NULL, /* bus_get_eventcookie */
1117c478bd9Sstevel@tonic-gate NULL, /* bus_add_eventcall */
1127c478bd9Sstevel@tonic-gate NULL, /* bus_remove_event */
1137c478bd9Sstevel@tonic-gate NULL, /* bus_post_event */
1147c478bd9Sstevel@tonic-gate NULL, /* bus_intr_ctl */
1157c478bd9Sstevel@tonic-gate tphci_bus_config, /* bus_config */
1167c478bd9Sstevel@tonic-gate tphci_bus_unconfig, /* bus_unconfig */
1177c478bd9Sstevel@tonic-gate NULL, /* bus_fm_init */
1187c478bd9Sstevel@tonic-gate NULL, /* bus_fm_fini */
1197c478bd9Sstevel@tonic-gate NULL, /* bus_fm_access_enter */
1207c478bd9Sstevel@tonic-gate NULL, /* bus_fm_access_exit */
1217c478bd9Sstevel@tonic-gate NULL, /* bus_power */
1227c478bd9Sstevel@tonic-gate tphci_intr_op /* bus_intr_op */
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate static struct dev_ops tphci_ops = {
1267c478bd9Sstevel@tonic-gate DEVO_REV,
1277c478bd9Sstevel@tonic-gate 0,
1287c478bd9Sstevel@tonic-gate tphci_getinfo,
1297c478bd9Sstevel@tonic-gate nulldev, /* identify */
1307c478bd9Sstevel@tonic-gate nulldev, /* probe */
1317c478bd9Sstevel@tonic-gate tphci_attach, /* attach and detach are mandatory */
1327c478bd9Sstevel@tonic-gate tphci_detach,
1337c478bd9Sstevel@tonic-gate nodev, /* reset */
1347c478bd9Sstevel@tonic-gate &tphci_cb_ops, /* cb_ops */
1357c478bd9Sstevel@tonic-gate &tphci_bus_ops, /* bus_ops */
13619397407SSherry Moore NULL, /* power */
13719397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1437c478bd9Sstevel@tonic-gate &mod_driverops,
14419397407SSherry Moore "test phci driver",
1457c478bd9Sstevel@tonic-gate &tphci_ops
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1497c478bd9Sstevel@tonic-gate MODREV_1,
1507c478bd9Sstevel@tonic-gate &modldrv,
1517c478bd9Sstevel@tonic-gate NULL
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate int
_init(void)1557c478bd9Sstevel@tonic-gate _init(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate int rval;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if ((rval = ddi_soft_state_init(&tphci_state,
1607c478bd9Sstevel@tonic-gate sizeof (struct tphci_state), 2)) != 0) {
1617c478bd9Sstevel@tonic-gate return (rval);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if ((rval = mod_install(&modlinkage)) != 0) {
1657c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&tphci_state);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate return (rval);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate int
_fini(void)1727c478bd9Sstevel@tonic-gate _fini(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate int rval;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * don't start cleaning up until we know that the module remove
1787c478bd9Sstevel@tonic-gate * has worked -- if this works, then we know that each instance
1797c478bd9Sstevel@tonic-gate * has successfully been detached
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate if ((rval = mod_remove(&modlinkage)) != 0) {
1827c478bd9Sstevel@tonic-gate return (rval);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&tphci_state);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate return (rval);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1917c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /* ARGSUSED */
1977c478bd9Sstevel@tonic-gate static int
tphci_open(dev_t * devp,int flag,int otype,cred_t * credp)1987c478bd9Sstevel@tonic-gate tphci_open(dev_t *devp, int flag, int otype, cred_t *credp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate struct tphci_state *phci;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate if (otype != OTYP_CHR) {
2037c478bd9Sstevel@tonic-gate return (EINVAL);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate phci = ddi_get_soft_state(tphci_state, getminor(*devp));
2077c478bd9Sstevel@tonic-gate if (phci == NULL) {
2087c478bd9Sstevel@tonic-gate return (ENXIO);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate return (0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /* ARGSUSED */
2167c478bd9Sstevel@tonic-gate static int
tphci_close(dev_t dev,int flag,int otype,cred_t * credp)2177c478bd9Sstevel@tonic-gate tphci_close(dev_t dev, int flag, int otype, cred_t *credp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate struct tphci_state *phci;
2207c478bd9Sstevel@tonic-gate if (otype != OTYP_CHR) {
2217c478bd9Sstevel@tonic-gate return (EINVAL);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate phci = ddi_get_soft_state(tphci_state, getminor(dev));
2257c478bd9Sstevel@tonic-gate if (phci == NULL) {
2267c478bd9Sstevel@tonic-gate return (ENXIO);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate return (0);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* ARGSUSED */
2337c478bd9Sstevel@tonic-gate static int
tphci_ioctl(dev_t dev,int cmd,intptr_t data,int mode,cred_t * credp,int * rval)2347c478bd9Sstevel@tonic-gate tphci_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
2357c478bd9Sstevel@tonic-gate cred_t *credp, int *rval)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * attach the module
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate static int
tphci_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2447c478bd9Sstevel@tonic-gate tphci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate char *vclass;
2477c478bd9Sstevel@tonic-gate int instance, phci_regis = 0;
2487c478bd9Sstevel@tonic-gate struct tphci_state *phci = NULL;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip);
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate switch (cmd) {
2537c478bd9Sstevel@tonic-gate case DDI_ATTACH:
2547c478bd9Sstevel@tonic-gate break;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate case DDI_RESUME:
2577c478bd9Sstevel@tonic-gate case DDI_PM_RESUME:
2587c478bd9Sstevel@tonic-gate return (0); /* nothing to do */
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate default:
2617c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * Allocate phci data structure.
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(tphci_state, instance) != DDI_SUCCESS) {
2687c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate phci = ddi_get_soft_state(tphci_state, instance);
2727c478bd9Sstevel@tonic-gate ASSERT(phci != NULL);
2737c478bd9Sstevel@tonic-gate phci->dip = dip;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate /* bus_addr has the form #,<vhci_class> */
2767c478bd9Sstevel@tonic-gate vclass = strchr(ddi_get_name_addr(dip), ',');
2777c478bd9Sstevel@tonic-gate if (vclass == NULL || vclass[1] == '\0') {
2787c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "tphci invalid bus_addr %s",
2797c478bd9Sstevel@tonic-gate ddi_get_name_addr(dip));
2807c478bd9Sstevel@tonic-gate goto attach_fail;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * Attach this instance with the mpxio framework
2857c478bd9Sstevel@tonic-gate */
2867c478bd9Sstevel@tonic-gate if (mdi_phci_register(vclass + 1, dip, 0) != MDI_SUCCESS) {
2877c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s mdi_phci_register failed",
2887c478bd9Sstevel@tonic-gate ddi_node_name(dip));
2897c478bd9Sstevel@tonic-gate goto attach_fail;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate phci_regis++;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
2947c478bd9Sstevel@tonic-gate instance, DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
2957c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "%s ddi_create_minor_node failed",
2967c478bd9Sstevel@tonic-gate ddi_node_name(dip));
2977c478bd9Sstevel@tonic-gate goto attach_fail;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate (void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1);
3017c478bd9Sstevel@tonic-gate ddi_report_dev(dip);
3027c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate attach_fail:
3057c478bd9Sstevel@tonic-gate if (phci_regis)
3067c478bd9Sstevel@tonic-gate (void) mdi_phci_unregister(dip, 0);
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate ddi_soft_state_free(tphci_state, instance);
3097c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3147c478bd9Sstevel@tonic-gate static int
tphci_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3157c478bd9Sstevel@tonic-gate tphci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate int instance = ddi_get_instance(dip);
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate switch (cmd) {
3207c478bd9Sstevel@tonic-gate case DDI_DETACH:
3217c478bd9Sstevel@tonic-gate break;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate case DDI_SUSPEND:
3247c478bd9Sstevel@tonic-gate case DDI_PM_SUSPEND:
3257c478bd9Sstevel@tonic-gate return (0); /* nothing to do */
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate default:
3287c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (mdi_phci_unregister(dip, 0) != MDI_SUCCESS)
3327c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
3357c478bd9Sstevel@tonic-gate ddi_soft_state_free(tphci_state, instance);
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate * tphci_getinfo()
3427c478bd9Sstevel@tonic-gate * Given the device number, return the devinfo pointer or the
3437c478bd9Sstevel@tonic-gate * instance number.
3447c478bd9Sstevel@tonic-gate * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach.
3457c478bd9Sstevel@tonic-gate */
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3487c478bd9Sstevel@tonic-gate static int
tphci_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3497c478bd9Sstevel@tonic-gate tphci_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate struct tphci_state *phci;
3527c478bd9Sstevel@tonic-gate int instance = getminor((dev_t)arg);
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate switch (cmd) {
3557c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
3567c478bd9Sstevel@tonic-gate phci = ddi_get_soft_state(tphci_state, instance);
3577c478bd9Sstevel@tonic-gate if (phci != NULL)
3587c478bd9Sstevel@tonic-gate *result = phci->dip;
3597c478bd9Sstevel@tonic-gate else {
3607c478bd9Sstevel@tonic-gate *result = NULL;
3617c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
3667c478bd9Sstevel@tonic-gate *result = (void *)(uintptr_t)instance;
3677c478bd9Sstevel@tonic-gate break;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate default:
3707c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate * Interrupt stuff. NO OP for pseudo drivers.
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3807c478bd9Sstevel@tonic-gate static int
tphci_intr_op(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t op,ddi_intr_handle_impl_t * hdlp,void * result)3817c478bd9Sstevel@tonic-gate tphci_intr_op(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
3827c478bd9Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate static int
tphci_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)3887c478bd9Sstevel@tonic-gate tphci_ctl(dev_info_t *dip, dev_info_t *rdip,
3897c478bd9Sstevel@tonic-gate ddi_ctl_enum_t ctlop, void *arg, void *result)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate switch (ctlop) {
3927c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV:
3937c478bd9Sstevel@tonic-gate if (rdip == (dev_info_t *)0)
3947c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
3957c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?tphci-device: %s%d\n",
3967c478bd9Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip));
3977c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD:
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg;
4027c478bd9Sstevel@tonic-gate return (tphci_initchild(dip, child));
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD:
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg;
4087c478bd9Sstevel@tonic-gate return (tphci_uninitchild(dip, child));
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate case DDI_CTLOPS_DMAPMAPC:
4127c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REPORTINT:
4137c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE:
4147c478bd9Sstevel@tonic-gate case DDI_CTLOPS_NREGS:
4157c478bd9Sstevel@tonic-gate case DDI_CTLOPS_SIDDEV:
4167c478bd9Sstevel@tonic-gate case DDI_CTLOPS_SLAVEONLY:
4177c478bd9Sstevel@tonic-gate case DDI_CTLOPS_AFFINITY:
4187c478bd9Sstevel@tonic-gate case DDI_CTLOPS_POKE:
4197c478bd9Sstevel@tonic-gate case DDI_CTLOPS_PEEK:
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate * These ops correspond to functions that "shouldn't" be called
4227c478bd9Sstevel@tonic-gate * by a pseudo driver. So we whine when we're called.
4237c478bd9Sstevel@tonic-gate */
4247c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
4257c478bd9Sstevel@tonic-gate ddi_get_name(dip), ddi_get_instance(dip),
4267c478bd9Sstevel@tonic-gate ctlop, ddi_get_name(rdip), ddi_get_instance(rdip));
4277c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate case DDI_CTLOPS_ATTACH:
4307c478bd9Sstevel@tonic-gate case DDI_CTLOPS_BTOP:
4317c478bd9Sstevel@tonic-gate case DDI_CTLOPS_BTOPR:
4327c478bd9Sstevel@tonic-gate case DDI_CTLOPS_DETACH:
4337c478bd9Sstevel@tonic-gate case DDI_CTLOPS_DVMAPAGESIZE:
4347c478bd9Sstevel@tonic-gate case DDI_CTLOPS_IOMIN:
4357c478bd9Sstevel@tonic-gate case DDI_CTLOPS_POWER:
4367c478bd9Sstevel@tonic-gate case DDI_CTLOPS_PTOB:
4377c478bd9Sstevel@tonic-gate default:
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * The ops that we pass up (default). We pass up memory
4407c478bd9Sstevel@tonic-gate * allocation oriented ops that we receive - these may be
4417c478bd9Sstevel@tonic-gate * associated with pseudo HBA drivers below us with target
4427c478bd9Sstevel@tonic-gate * drivers below them that use ddi memory allocation
4437c478bd9Sstevel@tonic-gate * interfaces like scsi_alloc_consistent_buf.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, ctlop, arg, result));
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate static int
tphci_initchild(dev_info_t * dip,dev_info_t * child)4507c478bd9Sstevel@tonic-gate tphci_initchild(dev_info_t *dip, dev_info_t *child)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(dip))
4537c478bd9Sstevel@tonic-gate ddi_set_name_addr(child, "0");
4547c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4587c478bd9Sstevel@tonic-gate static int
tphci_uninitchild(dev_info_t * dip,dev_info_t * child)4597c478bd9Sstevel@tonic-gate tphci_uninitchild(dev_info_t *dip, dev_info_t *child)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate ddi_set_name_addr(child, NULL);
4627c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate static int
tp_decode_name(char * devnm,char ** cname,char ** paddr,char ** guid)4667c478bd9Sstevel@tonic-gate tp_decode_name(char *devnm, char **cname, char **paddr, char **guid)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate char *tmp;
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate i_ddi_parse_name(devnm, cname, paddr, NULL);
4717c478bd9Sstevel@tonic-gate if ((strcmp(*cname, "tclient") != 0) &&
4727c478bd9Sstevel@tonic-gate (strcmp(*cname, "tphci") != 0) || *paddr == NULL)
4737c478bd9Sstevel@tonic-gate return (-1);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate tmp = strchr(*paddr, ',');
4767c478bd9Sstevel@tonic-gate if (tmp == NULL || tmp[1] == '\0')
4777c478bd9Sstevel@tonic-gate return (-1);
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate *guid = tmp + 1;
4807c478bd9Sstevel@tonic-gate return (0);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate static int
tphci_bus_config(dev_info_t * parent,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** childp)4847c478bd9Sstevel@tonic-gate tphci_bus_config(dev_info_t *parent, uint_t flags,
4857c478bd9Sstevel@tonic-gate ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags))
4887c478bd9Sstevel@tonic-gate char *cname, *paddr, *guid, *devnm;
4897c478bd9Sstevel@tonic-gate mdi_pathinfo_t *pip;
490*3fe80ca4SDan Cross int len, rval;
491*3fe80ca4SDan Cross boolean_t enteredv;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate switch (op) {
4947c478bd9Sstevel@tonic-gate case BUS_CONFIG_ONE:
4957c478bd9Sstevel@tonic-gate break;
4967c478bd9Sstevel@tonic-gate case BUS_CONFIG_DRIVER: /* no direct children to configure */
4977c478bd9Sstevel@tonic-gate case BUS_CONFIG_ALL:
4985e3986cbScth return (NDI_SUCCESS);
4997c478bd9Sstevel@tonic-gate default:
5005e3986cbScth return (NDI_FAILURE);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate /* only implement BUS_CONFIG_ONE */
5047c478bd9Sstevel@tonic-gate devnm = i_ddi_strdup((char *)arg, KM_SLEEP);
5057c478bd9Sstevel@tonic-gate len = strlen(devnm) + 1;
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /* caddr is hardcoded in the form *,<guid> */
5087c478bd9Sstevel@tonic-gate if (tp_decode_name(devnm, &cname, &paddr, &guid) != 0) {
5097c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "tphci_bus_config -- invalid device %s",
5107c478bd9Sstevel@tonic-gate (char *)arg);
5117c478bd9Sstevel@tonic-gate kmem_free(devnm, len);
5125e3986cbScth return (NDI_FAILURE);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate
515*3fe80ca4SDan Cross mdi_devi_enter(parent, &enteredv);
5165e3986cbScth rval = mdi_pi_alloc(parent, cname, guid, paddr, 0, &pip);
5175e3986cbScth kmem_free(devnm, len);
5185e3986cbScth if (rval != MDI_SUCCESS) {
5197c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "tphci_bus_config -- mdi_pi_alloc failed");
520*3fe80ca4SDan Cross mdi_devi_exit(parent, enteredv);
5215e3986cbScth return (NDI_FAILURE);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5245e3986cbScth /*
5255e3986cbScth * Hold the path and exit the pHCI while calling mdi_pi_online
5265e3986cbScth * to avoid deadlock with power management of pHCI.
5275e3986cbScth */
5285e3986cbScth mdi_hold_path(pip);
529*3fe80ca4SDan Cross mdi_devi_exit_phci(parent);
5305e3986cbScth rval = mdi_pi_online(pip, 0);
531*3fe80ca4SDan Cross mdi_devi_enter_phci(parent);
5325e3986cbScth mdi_rele_path(pip);
5335e3986cbScth
5345e3986cbScth if (rval != MDI_SUCCESS) {
5357c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "tphci_bus_config -- mdi_pi_online failed");
5367c478bd9Sstevel@tonic-gate (void) mdi_pi_free(pip, 0);
537*3fe80ca4SDan Cross mdi_devi_exit(parent, enteredv);
5385e3986cbScth return (NDI_FAILURE);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate if (childp) {
5425e3986cbScth *childp = mdi_pi_get_client(pip);
5437c478bd9Sstevel@tonic-gate ndi_hold_devi(*childp);
5447c478bd9Sstevel@tonic-gate }
545*3fe80ca4SDan Cross mdi_devi_exit(parent, enteredv);
5465e3986cbScth
5475e3986cbScth return (NDI_SUCCESS);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate static int
tphci_bus_unconfig(dev_info_t * parent,uint_t flags,ddi_bus_config_op_t op,void * arg)5517c478bd9Sstevel@tonic-gate tphci_bus_unconfig(dev_info_t *parent, uint_t flags,
5527c478bd9Sstevel@tonic-gate ddi_bus_config_op_t op, void *arg)
5537c478bd9Sstevel@tonic-gate {
5545e3986cbScth int rval = MDI_SUCCESS;
555*3fe80ca4SDan Cross boolean_t enteredv;
5565e3986cbScth mdi_pathinfo_t *pip, *next;
5577c478bd9Sstevel@tonic-gate char *devnm, *cname, *caddr;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate switch (op) {
5607c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ONE:
5617c478bd9Sstevel@tonic-gate devnm = (char *)arg;
5627c478bd9Sstevel@tonic-gate i_ddi_parse_name(devnm, &cname, &caddr, NULL);
5637c478bd9Sstevel@tonic-gate if (strcmp(cname, "tclient") != 0)
5645e3986cbScth return (NDI_SUCCESS); /* no such device */
5655e3986cbScth
566*3fe80ca4SDan Cross mdi_devi_enter(parent, &enteredv);
5677c478bd9Sstevel@tonic-gate pip = mdi_pi_find(parent, NULL, caddr);
5685e3986cbScth if (pip) {
5695e3986cbScth mdi_hold_path(pip);
570*3fe80ca4SDan Cross mdi_devi_exit_phci(parent);
5717c478bd9Sstevel@tonic-gate rval = mdi_pi_offline(pip, NDI_DEVI_REMOVE);
572*3fe80ca4SDan Cross mdi_devi_enter_phci(parent);
5735e3986cbScth mdi_rele_path(pip);
5745e3986cbScth
5755e3986cbScth if (rval == MDI_SUCCESS)
5765e3986cbScth (void) mdi_pi_free(pip, 0);
5775e3986cbScth }
578*3fe80ca4SDan Cross mdi_devi_exit(parent, enteredv);
5797c478bd9Sstevel@tonic-gate return (rval == MDI_SUCCESS ? NDI_SUCCESS : NDI_FAILURE);
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ALL:
5827c478bd9Sstevel@tonic-gate if (flags & NDI_AUTODETACH)
5835e3986cbScth return (NDI_FAILURE);
5847c478bd9Sstevel@tonic-gate
585*3fe80ca4SDan Cross mdi_devi_enter(parent, &enteredv);
5865e3986cbScth next = mdi_get_next_client_path(parent, NULL);
5875e3986cbScth while ((pip = next) != NULL) {
5885e3986cbScth next = mdi_get_next_client_path(parent, pip);
5897c478bd9Sstevel@tonic-gate
5905e3986cbScth mdi_hold_path(pip);
591*3fe80ca4SDan Cross mdi_devi_exit_phci(parent);
5927c478bd9Sstevel@tonic-gate rval = mdi_pi_offline(pip, NDI_DEVI_REMOVE);
593*3fe80ca4SDan Cross mdi_devi_enter_phci(parent);
5945e3986cbScth mdi_rele_path(pip);
5955e3986cbScth
5965e3986cbScth if (rval != MDI_SUCCESS)
5977c478bd9Sstevel@tonic-gate break;
5985e3986cbScth (void) mdi_pi_free(pip, 0);
5997c478bd9Sstevel@tonic-gate }
600*3fe80ca4SDan Cross mdi_devi_exit(parent, enteredv);
6017c478bd9Sstevel@tonic-gate return (rval == MDI_SUCCESS ? NDI_SUCCESS : NDI_FAILURE);
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_DRIVER: /* nothing to do */
6045e3986cbScth return (NDI_SUCCESS);
6055e3986cbScth
6067c478bd9Sstevel@tonic-gate default:
6075e3986cbScth return (NDI_FAILURE);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate /*NOTREACHED*/
6107c478bd9Sstevel@tonic-gate }
611