15084Sjohnlev /* 25084Sjohnlev * CDDL HEADER START 35084Sjohnlev * 45084Sjohnlev * The contents of this file are subject to the terms of the 55084Sjohnlev * Common Development and Distribution License (the "License"). 65084Sjohnlev * You may not use this file except in compliance with the License. 75084Sjohnlev * 85084Sjohnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95084Sjohnlev * or http://www.opensolaris.org/os/licensing. 105084Sjohnlev * See the License for the specific language governing permissions 115084Sjohnlev * and limitations under the License. 125084Sjohnlev * 135084Sjohnlev * When distributing Covered Code, include this CDDL HEADER in each 145084Sjohnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155084Sjohnlev * If applicable, add the following below this CDDL HEADER, with the 165084Sjohnlev * fields enclosed by brackets "[]" replaced with your own identifying 175084Sjohnlev * information: Portions Copyright [yyyy] [name of copyright owner] 185084Sjohnlev * 195084Sjohnlev * CDDL HEADER END 205084Sjohnlev */ 215084Sjohnlev 225084Sjohnlev /* 235910Smrj * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 245084Sjohnlev * Use is subject to license terms. 255084Sjohnlev */ 265084Sjohnlev 275084Sjohnlev #pragma ident "%Z%%M% %I% %E% SMI" 285084Sjohnlev 295084Sjohnlev /* 305084Sjohnlev * Xen virtual device driver interfaces 315084Sjohnlev */ 325084Sjohnlev 335084Sjohnlev /* 345084Sjohnlev * todo: 355084Sjohnlev * + name space clean up: 365084Sjohnlev * xvdi_* - public xen interfaces, for use by all leaf drivers 375084Sjohnlev * xd_* - public xen data structures 385084Sjohnlev * i_xvdi_* - implementation private functions 395084Sjohnlev * xendev_* - xendev driver interfaces, both internal and in cb_ops/bus_ops 405084Sjohnlev * + add mdb dcmds to dump ring status 415084Sjohnlev * + implement xvdi_xxx to wrap xenbus_xxx read/write function 425084Sjohnlev * + convert (xendev_ring_t *) into xvdi_ring_handle_t 435084Sjohnlev */ 445084Sjohnlev #include <sys/conf.h> 455084Sjohnlev #include <sys/param.h> 465084Sjohnlev #include <sys/kmem.h> 475084Sjohnlev #include <vm/seg_kmem.h> 485084Sjohnlev #include <sys/debug.h> 495084Sjohnlev #include <sys/modctl.h> 505084Sjohnlev #include <sys/autoconf.h> 515084Sjohnlev #include <sys/ddi_impldefs.h> 525084Sjohnlev #include <sys/ddi_subrdefs.h> 535084Sjohnlev #include <sys/ddi.h> 545084Sjohnlev #include <sys/sunddi.h> 555084Sjohnlev #include <sys/sunndi.h> 565084Sjohnlev #include <sys/sunldi.h> 575084Sjohnlev #include <sys/fs/dv_node.h> 585084Sjohnlev #include <sys/avintr.h> 595084Sjohnlev #include <sys/psm.h> 605084Sjohnlev #include <sys/spl.h> 615084Sjohnlev #include <sys/promif.h> 625084Sjohnlev #include <sys/list.h> 635084Sjohnlev #include <sys/bootconf.h> 645084Sjohnlev #include <sys/bootsvcs.h> 655084Sjohnlev #include <sys/bootinfo.h> 665084Sjohnlev #include <sys/note.h> 675741Smrj #ifdef XPV_HVM_DRIVER 685741Smrj #include <sys/xpv_support.h> 695741Smrj #include <sys/hypervisor.h> 705741Smrj #include <public/grant_table.h> 715741Smrj #include <public/xen.h> 725741Smrj #include <public/io/xenbus.h> 735741Smrj #include <public/io/xs_wire.h> 745741Smrj #include <public/event_channel.h> 755741Smrj #include <public/io/xenbus.h> 765741Smrj #else /* XPV_HVM_DRIVER */ 775741Smrj #include <sys/hypervisor.h> 785084Sjohnlev #include <sys/xen_mmu.h> 795084Sjohnlev #include <xen/sys/xenbus_impl.h> 805741Smrj #include <sys/evtchn_impl.h> 815741Smrj #endif /* XPV_HVM_DRIVER */ 825741Smrj #include <sys/gnttab.h> 835084Sjohnlev #include <xen/sys/xendev.h> 845084Sjohnlev #include <vm/hat_i86.h> 855084Sjohnlev #include <sys/scsi/generic/inquiry.h> 865084Sjohnlev #include <util/sscanf.h> 875084Sjohnlev #include <xen/public/io/xs_wire.h> 885084Sjohnlev 895084Sjohnlev 905084Sjohnlev static void xvdi_ring_init_sring(xendev_ring_t *); 915084Sjohnlev static void xvdi_ring_init_front_ring(xendev_ring_t *, size_t, size_t); 925741Smrj #ifndef XPV_HVM_DRIVER 935084Sjohnlev static void xvdi_ring_init_back_ring(xendev_ring_t *, size_t, size_t); 945741Smrj #endif 955084Sjohnlev static void xvdi_reinit_ring(dev_info_t *, grant_ref_t *, xendev_ring_t *); 965084Sjohnlev 975084Sjohnlev static int i_xvdi_add_watches(dev_info_t *); 985084Sjohnlev static void i_xvdi_rem_watches(dev_info_t *); 995084Sjohnlev 1005084Sjohnlev static int i_xvdi_add_watch_oestate(dev_info_t *); 1015084Sjohnlev static void i_xvdi_rem_watch_oestate(dev_info_t *); 1025084Sjohnlev static void i_xvdi_oestate_cb(struct xenbus_device *, XenbusState); 1035084Sjohnlev static void i_xvdi_oestate_handler(void *); 1045084Sjohnlev 1055084Sjohnlev static int i_xvdi_add_watch_hpstate(dev_info_t *); 1065084Sjohnlev static void i_xvdi_rem_watch_hpstate(dev_info_t *); 1075084Sjohnlev static void i_xvdi_hpstate_cb(struct xenbus_watch *, const char **, 1085084Sjohnlev unsigned int); 1095084Sjohnlev static void i_xvdi_hpstate_handler(void *); 1105084Sjohnlev 1115084Sjohnlev static int i_xvdi_add_watch_bepath(dev_info_t *); 1125084Sjohnlev static void i_xvdi_rem_watch_bepath(dev_info_t *); 1135084Sjohnlev static void i_xvdi_bepath_cb(struct xenbus_watch *, const char **, 1145084Sjohnlev unsigned in); 1155084Sjohnlev 1165084Sjohnlev static void xendev_offline_device(void *); 1175084Sjohnlev 1185084Sjohnlev static void i_xvdi_probe_path_cb(struct xenbus_watch *, const char **, 1195084Sjohnlev unsigned int); 1205084Sjohnlev static void i_xvdi_probe_path_handler(void *); 1215084Sjohnlev 1225084Sjohnlev typedef struct xd_cfg { 1235084Sjohnlev xendev_devclass_t devclass; 1245084Sjohnlev char *xsdev; 1255084Sjohnlev char *xs_path_fe; 1265084Sjohnlev char *xs_path_be; 1275084Sjohnlev char *node_fe; 1285084Sjohnlev char *node_be; 1295084Sjohnlev char *device_type; 1305084Sjohnlev int xd_ipl; 1315084Sjohnlev int flags; 1325084Sjohnlev } i_xd_cfg_t; 1335084Sjohnlev 1345084Sjohnlev #define XD_DOM_ZERO 0x01 /* dom0 only. */ 1355084Sjohnlev #define XD_DOM_GUEST 0x02 /* Guest domains (i.e. non-dom0). */ 1365084Sjohnlev #define XD_DOM_IO 0x04 /* IO domains. */ 1375084Sjohnlev 1385084Sjohnlev #define XD_DOM_ALL (XD_DOM_ZERO | XD_DOM_GUEST) 1395084Sjohnlev 1405084Sjohnlev static i_xd_cfg_t xdci[] = { 1415084Sjohnlev { XEN_CONSOLE, NULL, NULL, NULL, "xencons", NULL, 1425084Sjohnlev "console", IPL_CONS, XD_DOM_ALL, }, 1435084Sjohnlev 1445084Sjohnlev { XEN_VNET, "vif", "device/vif", "backend/vif", "xnf", "xnb", 1455084Sjohnlev "network", IPL_VIF, XD_DOM_ALL, }, 1465084Sjohnlev 1475084Sjohnlev { XEN_VBLK, "vbd", "device/vbd", "backend/vbd", "xdf", "xdb", 1485084Sjohnlev "block", IPL_VBD, XD_DOM_ALL, }, 1495084Sjohnlev 1505084Sjohnlev { XEN_XENBUS, NULL, NULL, NULL, "xenbus", NULL, 1515084Sjohnlev NULL, 0, XD_DOM_ALL, }, 1525084Sjohnlev 1535084Sjohnlev { XEN_DOMCAPS, NULL, NULL, NULL, "domcaps", NULL, 1545084Sjohnlev NULL, 0, XD_DOM_ALL, }, 1555084Sjohnlev 1565084Sjohnlev { XEN_BALLOON, NULL, NULL, NULL, "balloon", NULL, 1575084Sjohnlev NULL, 0, XD_DOM_ALL, }, 1585084Sjohnlev 1595084Sjohnlev { XEN_EVTCHN, NULL, NULL, NULL, "evtchn", NULL, 1605084Sjohnlev NULL, 0, XD_DOM_ZERO, }, 1615084Sjohnlev 1625084Sjohnlev { XEN_PRIVCMD, NULL, NULL, NULL, "privcmd", NULL, 1635084Sjohnlev NULL, 0, XD_DOM_ZERO, }, 1645084Sjohnlev }; 1655084Sjohnlev #define NXDC (sizeof (xdci) / sizeof (xdci[0])) 1665084Sjohnlev 1675084Sjohnlev static void i_xvdi_enum_fe(dev_info_t *, i_xd_cfg_t *); 1685084Sjohnlev static void i_xvdi_enum_be(dev_info_t *, i_xd_cfg_t *); 1695084Sjohnlev static void i_xvdi_enum_worker(dev_info_t *, i_xd_cfg_t *, char *); 1705084Sjohnlev 1715084Sjohnlev /* 1725084Sjohnlev * Xen device channel device access and DMA attributes 1735084Sjohnlev */ 1745084Sjohnlev static ddi_device_acc_attr_t xendev_dc_accattr = { 1755084Sjohnlev DDI_DEVICE_ATTR_V0, DDI_NEVERSWAP_ACC, DDI_STRICTORDER_ACC 1765084Sjohnlev }; 1775084Sjohnlev 1785084Sjohnlev static ddi_dma_attr_t xendev_dc_dmaattr = { 1795084Sjohnlev DMA_ATTR_V0, /* version of this structure */ 1805084Sjohnlev 0, /* lowest usable address */ 1815084Sjohnlev 0xffffffffffffffffULL, /* highest usable address */ 1825084Sjohnlev 0x7fffffff, /* maximum DMAable byte count */ 1835084Sjohnlev MMU_PAGESIZE, /* alignment in bytes */ 1845084Sjohnlev 0x7ff, /* bitmap of burst sizes */ 1855084Sjohnlev 1, /* minimum transfer */ 1865084Sjohnlev 0xffffffffU, /* maximum transfer */ 1875084Sjohnlev 0xffffffffffffffffULL, /* maximum segment length */ 1885084Sjohnlev 1, /* maximum number of segments */ 1895084Sjohnlev 1, /* granularity */ 1905084Sjohnlev 0, /* flags (reserved) */ 1915084Sjohnlev }; 1925084Sjohnlev 1935084Sjohnlev static dev_info_t *xendev_dip = NULL; 1945084Sjohnlev 1955084Sjohnlev #define XVDI_DBG_STATE 0x01 1965084Sjohnlev #define XVDI_DBG_PROBE 0x02 1975084Sjohnlev 1985084Sjohnlev #ifdef DEBUG 1995316Sjohnlev int i_xvdi_debug = 0; 2005084Sjohnlev 2015084Sjohnlev #define XVDI_DPRINTF(flag, format, ...) \ 2025084Sjohnlev { \ 2035084Sjohnlev if (i_xvdi_debug & (flag)) \ 2045084Sjohnlev prom_printf((format), __VA_ARGS__); \ 2055084Sjohnlev } 2065084Sjohnlev #else 2075084Sjohnlev #define XVDI_DPRINTF(flag, format, ...) 2085084Sjohnlev #endif /* DEBUG */ 2095084Sjohnlev 2105084Sjohnlev static i_xd_cfg_t * 2115084Sjohnlev i_xvdi_devclass2cfg(xendev_devclass_t devclass) 2125084Sjohnlev { 2135084Sjohnlev i_xd_cfg_t *xdcp; 2145084Sjohnlev int i; 2155084Sjohnlev 2165084Sjohnlev for (i = 0, xdcp = xdci; i < NXDC; i++, xdcp++) 2175084Sjohnlev if (xdcp->devclass == devclass) 2185084Sjohnlev return (xdcp); 2195084Sjohnlev 2205084Sjohnlev return (NULL); 2215084Sjohnlev } 2225084Sjohnlev 2235084Sjohnlev int 2245084Sjohnlev xvdi_init_dev(dev_info_t *dip) 2255084Sjohnlev { 2265084Sjohnlev xendev_devclass_t devcls; 2275084Sjohnlev int vdevnum; 2285084Sjohnlev domid_t domid; 2295084Sjohnlev struct xendev_ppd *pdp; 2305084Sjohnlev i_xd_cfg_t *xdcp; 2315084Sjohnlev boolean_t backend; 2325084Sjohnlev char xsnamebuf[TYPICALMAXPATHLEN]; 2335084Sjohnlev char *xsname; 2345084Sjohnlev 2355084Sjohnlev devcls = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 2365084Sjohnlev DDI_PROP_DONTPASS, "devclass", XEN_INVAL); 2375084Sjohnlev vdevnum = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 238*6175Sjohnlev DDI_PROP_DONTPASS, "vdev", VDEV_NOXS); 2395084Sjohnlev domid = (domid_t)ddi_prop_get_int(DDI_DEV_T_ANY, dip, 2405084Sjohnlev DDI_PROP_DONTPASS, "domain", DOMID_SELF); 2415084Sjohnlev 2425084Sjohnlev backend = (domid != DOMID_SELF); 2435084Sjohnlev xdcp = i_xvdi_devclass2cfg(devcls); 2445084Sjohnlev if (xdcp->device_type != NULL) 2455084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2465084Sjohnlev "device_type", xdcp->device_type); 2475084Sjohnlev 2485084Sjohnlev pdp = kmem_zalloc(sizeof (*pdp), KM_SLEEP); 2495084Sjohnlev pdp->xd_domain = domid; 2505084Sjohnlev pdp->xd_vdevnum = vdevnum; 2515084Sjohnlev pdp->xd_devclass = devcls; 2525084Sjohnlev pdp->xd_evtchn = INVALID_EVTCHN; 2535084Sjohnlev mutex_init(&pdp->xd_lk, NULL, MUTEX_DRIVER, NULL); 2545084Sjohnlev ddi_set_parent_data(dip, pdp); 2555084Sjohnlev 2565084Sjohnlev /* 2575084Sjohnlev * devices that do not need to interact with xenstore 2585084Sjohnlev */ 259*6175Sjohnlev if (vdevnum == VDEV_NOXS) { 2605084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2615084Sjohnlev "unit-address", "0"); 2625084Sjohnlev if (devcls == XEN_CONSOLE) 2635084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2645084Sjohnlev "pm-hardware-state", "needs-suspend-resume"); 2655084Sjohnlev return (DDI_SUCCESS); 2665084Sjohnlev } 2675084Sjohnlev 2685084Sjohnlev /* 2695084Sjohnlev * PV devices that need to probe xenstore 2705084Sjohnlev */ 2715084Sjohnlev 2725084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2735084Sjohnlev "pm-hardware-state", "needs-suspend-resume"); 2745084Sjohnlev 2755084Sjohnlev xsname = xsnamebuf; 2765084Sjohnlev if (!backend) 2775084Sjohnlev (void) snprintf(xsnamebuf, sizeof (xsnamebuf), 2785084Sjohnlev "%s/%d", xdcp->xs_path_fe, vdevnum); 2795084Sjohnlev else 2805084Sjohnlev (void) snprintf(xsnamebuf, sizeof (xsnamebuf), 2815084Sjohnlev "%s/%d/%d", xdcp->xs_path_be, domid, vdevnum); 2825159Sjohnlev if ((xenbus_read_driver_state(xsname) >= XenbusStateClosing)) { 2835159Sjohnlev /* Don't try to init a dev that may be closing */ 2845159Sjohnlev mutex_destroy(&pdp->xd_lk); 2855159Sjohnlev kmem_free(pdp, sizeof (*pdp)); 2865159Sjohnlev ddi_set_parent_data(dip, NULL); 2875159Sjohnlev return (DDI_FAILURE); 2885159Sjohnlev } 2895084Sjohnlev 2905084Sjohnlev pdp->xd_xsdev.nodename = i_ddi_strdup(xsname, KM_SLEEP); 2915084Sjohnlev pdp->xd_xsdev.devicetype = xdcp->xsdev; 2925084Sjohnlev pdp->xd_xsdev.frontend = (backend ? 0 : 1); 2935084Sjohnlev pdp->xd_xsdev.data = dip; 2945084Sjohnlev pdp->xd_xsdev.otherend_id = (backend ? domid : -1); 2955084Sjohnlev if (i_xvdi_add_watches(dip) != DDI_SUCCESS) { 2965084Sjohnlev cmn_err(CE_WARN, "xvdi_init_dev: " 2975084Sjohnlev "cannot add watches for %s", xsname); 2985084Sjohnlev xvdi_uninit_dev(dip); 2995084Sjohnlev return (DDI_FAILURE); 3005084Sjohnlev } 3015084Sjohnlev 3025084Sjohnlev /* 3035084Sjohnlev * frontend device will use "unit-addr" as 3045084Sjohnlev * the bus address, which will be set here 3055084Sjohnlev */ 3065084Sjohnlev if (!backend) { 3075084Sjohnlev void *prop_str; 3085084Sjohnlev unsigned int prop_len, addr; 3095084Sjohnlev 3105084Sjohnlev switch (devcls) { 3115084Sjohnlev case XEN_VNET: 3125084Sjohnlev if (xenbus_read(XBT_NULL, xsname, "mac", &prop_str, 3135084Sjohnlev &prop_len) == 0) { 3145084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, 3155084Sjohnlev dip, "mac", prop_str); 3165084Sjohnlev kmem_free(prop_str, prop_len); 3175084Sjohnlev } 3185084Sjohnlev prop_str = NULL; 3195084Sjohnlev if (xenbus_scanf(XBT_NULL, xsname, "handle", "%u", 3205084Sjohnlev &addr) == 0) { 3215084Sjohnlev char unitaddr[9]; /* hold 32-bit hex */ 3225084Sjohnlev 3235084Sjohnlev (void) snprintf(unitaddr, 9, "%x", addr); 3245084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, 3255084Sjohnlev dip, "unit-address", unitaddr); 3265084Sjohnlev } 3275084Sjohnlev break; 3285084Sjohnlev case XEN_VBLK: 3295084Sjohnlev if (xenbus_read(XBT_NULL, pdp->xd_xsdev.otherend, 3305084Sjohnlev "dev", &prop_str, &prop_len) == 0) { 3315084Sjohnlev (void) ndi_prop_update_string(DDI_DEV_T_NONE, 3325084Sjohnlev dip, "unit-address", prop_str); 3335084Sjohnlev kmem_free(prop_str, prop_len); 3345084Sjohnlev } 3355741Smrj #ifdef XPV_HVM_DRIVER 3365741Smrj /* 3375741Smrj * The mapping between the 'dev' name and the 3385741Smrj * device ID maintained by Xenstore has to be 3395741Smrj * tracked explicitly in HVM domains. 3405741Smrj */ 3415741Smrj prop_str = strrchr(pdp->xd_xsdev.otherend, '/'); 3425741Smrj if (prop_str != NULL) { 3435741Smrj prop_str = ((caddr_t)prop_str) + 1; 3445741Smrj (void) ndi_prop_update_string(DDI_DEV_T_NONE, 3455741Smrj dip, "xenstore-id", prop_str); 3465741Smrj } 3475741Smrj #endif /* XPV_HVM_DRIVER */ 3485084Sjohnlev break; 3495084Sjohnlev default: 3505084Sjohnlev break; 3515084Sjohnlev } 3525084Sjohnlev } 3535084Sjohnlev 3545084Sjohnlev return (DDI_SUCCESS); 3555084Sjohnlev } 3565084Sjohnlev 3575084Sjohnlev void 3585084Sjohnlev xvdi_uninit_dev(dev_info_t *dip) 3595084Sjohnlev { 3605084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 3615084Sjohnlev 3625084Sjohnlev if (pdp != NULL) { 3635084Sjohnlev /* Remove any registered callbacks. */ 3645084Sjohnlev xvdi_remove_event_handler(dip, NULL); 3655084Sjohnlev 3665084Sjohnlev /* Remove any registered watches. */ 3675084Sjohnlev i_xvdi_rem_watches(dip); 3685084Sjohnlev 3695159Sjohnlev /* tell other end to close */ 3705741Smrj if (pdp->xd_xsdev.otherend_id != (domid_t)-1) 3715741Smrj (void) xvdi_switch_state(dip, XBT_NULL, 3725741Smrj XenbusStateClosed); 3735159Sjohnlev 3745084Sjohnlev if (pdp->xd_xsdev.nodename != NULL) 3755084Sjohnlev kmem_free((char *)(pdp->xd_xsdev.nodename), 3765084Sjohnlev strlen(pdp->xd_xsdev.nodename) + 1); 3775084Sjohnlev 3785084Sjohnlev ddi_set_parent_data(dip, NULL); 3795084Sjohnlev 3805084Sjohnlev mutex_destroy(&pdp->xd_lk); 3815084Sjohnlev kmem_free(pdp, sizeof (*pdp)); 3825084Sjohnlev } 3835084Sjohnlev } 3845084Sjohnlev 3855084Sjohnlev /* 3865084Sjohnlev * Bind the event channel for this device instance. 3875084Sjohnlev * Currently we only support one evtchn per device instance. 3885084Sjohnlev */ 3895084Sjohnlev int 3905084Sjohnlev xvdi_bind_evtchn(dev_info_t *dip, evtchn_port_t evtchn) 3915084Sjohnlev { 3925084Sjohnlev struct xendev_ppd *pdp; 3935084Sjohnlev domid_t oeid; 3945084Sjohnlev int r; 3955084Sjohnlev 3965084Sjohnlev pdp = ddi_get_parent_data(dip); 3975084Sjohnlev ASSERT(pdp != NULL); 3985084Sjohnlev ASSERT(pdp->xd_evtchn == INVALID_EVTCHN); 3995084Sjohnlev 4005084Sjohnlev mutex_enter(&pdp->xd_lk); 4015084Sjohnlev if (pdp->xd_devclass == XEN_CONSOLE) { 4025084Sjohnlev if (!DOMAIN_IS_INITDOMAIN(xen_info)) { 4035084Sjohnlev pdp->xd_evtchn = xen_info->console.domU.evtchn; 4045084Sjohnlev } else { 4055084Sjohnlev pdp->xd_evtchn = INVALID_EVTCHN; 4065084Sjohnlev mutex_exit(&pdp->xd_lk); 4075084Sjohnlev return (DDI_SUCCESS); 4085084Sjohnlev } 4095084Sjohnlev } else { 4105084Sjohnlev oeid = pdp->xd_xsdev.otherend_id; 4115084Sjohnlev if (oeid == (domid_t)-1) { 4125084Sjohnlev mutex_exit(&pdp->xd_lk); 4135084Sjohnlev return (DDI_FAILURE); 4145084Sjohnlev } 4155084Sjohnlev 4165084Sjohnlev if ((r = xen_bind_interdomain(oeid, evtchn, &pdp->xd_evtchn))) { 4175084Sjohnlev xvdi_dev_error(dip, r, "bind event channel"); 4185084Sjohnlev mutex_exit(&pdp->xd_lk); 4195084Sjohnlev return (DDI_FAILURE); 4205084Sjohnlev } 4215084Sjohnlev } 4225741Smrj #ifndef XPV_HVM_DRIVER 4235084Sjohnlev pdp->xd_ispec.intrspec_vec = ec_bind_evtchn_to_irq(pdp->xd_evtchn); 4245741Smrj #endif 4255084Sjohnlev mutex_exit(&pdp->xd_lk); 4265084Sjohnlev 4275084Sjohnlev return (DDI_SUCCESS); 4285084Sjohnlev } 4295084Sjohnlev 4305084Sjohnlev /* 4315084Sjohnlev * Allocate an event channel for this device instance. 4325084Sjohnlev * Currently we only support one evtchn per device instance. 4335084Sjohnlev */ 4345084Sjohnlev int 4355084Sjohnlev xvdi_alloc_evtchn(dev_info_t *dip) 4365084Sjohnlev { 4375084Sjohnlev struct xendev_ppd *pdp; 4385084Sjohnlev domid_t oeid; 4395084Sjohnlev int rv; 4405084Sjohnlev 4415084Sjohnlev pdp = ddi_get_parent_data(dip); 4425084Sjohnlev ASSERT(pdp != NULL); 4435084Sjohnlev ASSERT(pdp->xd_evtchn == INVALID_EVTCHN); 4445084Sjohnlev 4455084Sjohnlev mutex_enter(&pdp->xd_lk); 4465084Sjohnlev if (pdp->xd_devclass == XEN_CONSOLE) { 4475084Sjohnlev if (!DOMAIN_IS_INITDOMAIN(xen_info)) { 4485084Sjohnlev pdp->xd_evtchn = xen_info->console.domU.evtchn; 4495084Sjohnlev } else { 4505084Sjohnlev pdp->xd_evtchn = INVALID_EVTCHN; 4515084Sjohnlev mutex_exit(&pdp->xd_lk); 4525084Sjohnlev return (DDI_SUCCESS); 4535084Sjohnlev } 4545084Sjohnlev } else { 4555084Sjohnlev oeid = pdp->xd_xsdev.otherend_id; 4565084Sjohnlev if (oeid == (domid_t)-1) { 4575084Sjohnlev mutex_exit(&pdp->xd_lk); 4585084Sjohnlev return (DDI_FAILURE); 4595084Sjohnlev } 4605084Sjohnlev 4615084Sjohnlev if ((rv = xen_alloc_unbound_evtchn(oeid, &pdp->xd_evtchn))) { 4625084Sjohnlev xvdi_dev_error(dip, rv, "bind event channel"); 4635084Sjohnlev mutex_exit(&pdp->xd_lk); 4645084Sjohnlev return (DDI_FAILURE); 4655084Sjohnlev } 4665084Sjohnlev } 4675741Smrj #ifndef XPV_HVM_DRIVER 4685084Sjohnlev pdp->xd_ispec.intrspec_vec = ec_bind_evtchn_to_irq(pdp->xd_evtchn); 4695741Smrj #endif 4705084Sjohnlev mutex_exit(&pdp->xd_lk); 4715084Sjohnlev 4725084Sjohnlev return (DDI_SUCCESS); 4735084Sjohnlev } 4745084Sjohnlev 4755084Sjohnlev /* 4765084Sjohnlev * Unbind the event channel for this device instance. 4775084Sjohnlev * Currently we only support one evtchn per device instance. 4785084Sjohnlev */ 4795084Sjohnlev void 4805084Sjohnlev xvdi_free_evtchn(dev_info_t *dip) 4815084Sjohnlev { 4825084Sjohnlev struct xendev_ppd *pdp; 4835084Sjohnlev 4845084Sjohnlev pdp = ddi_get_parent_data(dip); 4855084Sjohnlev ASSERT(pdp != NULL); 4865084Sjohnlev 4875084Sjohnlev mutex_enter(&pdp->xd_lk); 4885084Sjohnlev if (pdp->xd_evtchn != INVALID_EVTCHN) { 4895741Smrj #ifndef XPV_HVM_DRIVER 4905084Sjohnlev ec_unbind_irq(pdp->xd_ispec.intrspec_vec); 4915741Smrj pdp->xd_ispec.intrspec_vec = 0; 4925741Smrj #endif 4935084Sjohnlev pdp->xd_evtchn = INVALID_EVTCHN; 4945084Sjohnlev } 4955084Sjohnlev mutex_exit(&pdp->xd_lk); 4965084Sjohnlev } 4975084Sjohnlev 4985741Smrj #ifndef XPV_HVM_DRIVER 4995084Sjohnlev /* 5005084Sjohnlev * Map an inter-domain communication ring for a virtual device. 5015084Sjohnlev * This is used by backend drivers. 5025084Sjohnlev */ 5035084Sjohnlev int 5045084Sjohnlev xvdi_map_ring(dev_info_t *dip, size_t nentry, size_t entrysize, 5055084Sjohnlev grant_ref_t gref, xendev_ring_t **ringpp) 5065084Sjohnlev { 5075084Sjohnlev domid_t oeid; 5085084Sjohnlev gnttab_map_grant_ref_t mapop; 5095084Sjohnlev gnttab_unmap_grant_ref_t unmapop; 5105084Sjohnlev caddr_t ringva; 5115084Sjohnlev ddi_acc_hdl_t *ap; 5125084Sjohnlev ddi_acc_impl_t *iap; 5135084Sjohnlev xendev_ring_t *ring; 5145084Sjohnlev int err; 5155084Sjohnlev char errstr[] = "mapping in ring buffer"; 5165084Sjohnlev 5175084Sjohnlev ring = kmem_zalloc(sizeof (xendev_ring_t), KM_SLEEP); 5185084Sjohnlev oeid = xvdi_get_oeid(dip); 5195084Sjohnlev 5205084Sjohnlev /* alloc va in backend dom for ring buffer */ 5215084Sjohnlev ringva = vmem_xalloc(heap_arena, PAGESIZE, PAGESIZE, 5225084Sjohnlev 0, 0, 0, 0, VM_SLEEP); 5235084Sjohnlev 5245084Sjohnlev /* map in ring page */ 5255084Sjohnlev hat_prepare_mapping(kas.a_hat, ringva); 5265084Sjohnlev mapop.host_addr = (uint64_t)(uintptr_t)ringva; 5275084Sjohnlev mapop.flags = GNTMAP_host_map; 5285084Sjohnlev mapop.ref = gref; 5295084Sjohnlev mapop.dom = oeid; 5305084Sjohnlev err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &mapop, 1); 5315084Sjohnlev if (err) { 5325084Sjohnlev xvdi_fatal_error(dip, err, errstr); 5335084Sjohnlev goto errout1; 5345084Sjohnlev } 5355084Sjohnlev 5365084Sjohnlev if (mapop.status != 0) { 5375084Sjohnlev xvdi_fatal_error(dip, err, errstr); 5385084Sjohnlev goto errout2; 5395084Sjohnlev } 5405084Sjohnlev ring->xr_vaddr = ringva; 5415084Sjohnlev ring->xr_grant_hdl = mapop.handle; 5425084Sjohnlev ring->xr_gref = gref; 5435084Sjohnlev 5445084Sjohnlev /* 5455084Sjohnlev * init an acc handle and associate it w/ this ring 5465084Sjohnlev * this is only for backend drivers. we get the memory by calling 5475084Sjohnlev * vmem_xalloc(), instead of calling any ddi function, so we have 5485084Sjohnlev * to init an acc handle by ourselves 5495084Sjohnlev */ 5505084Sjohnlev ring->xr_acc_hdl = impl_acc_hdl_alloc(KM_SLEEP, NULL); 5515084Sjohnlev ap = impl_acc_hdl_get(ring->xr_acc_hdl); 5525084Sjohnlev ap->ah_vers = VERS_ACCHDL; 5535084Sjohnlev ap->ah_dip = dip; 5545084Sjohnlev ap->ah_xfermodes = DDI_DMA_CONSISTENT; 5555084Sjohnlev ap->ah_acc = xendev_dc_accattr; 5565084Sjohnlev iap = (ddi_acc_impl_t *)ap->ah_platform_private; 5575084Sjohnlev iap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR; 5585084Sjohnlev impl_acc_hdl_init(ap); 5595084Sjohnlev ap->ah_offset = 0; 5605084Sjohnlev ap->ah_len = (off_t)PAGESIZE; 5615084Sjohnlev ap->ah_addr = ring->xr_vaddr; 5625084Sjohnlev 5635084Sjohnlev /* init backend ring */ 5645084Sjohnlev xvdi_ring_init_back_ring(ring, nentry, entrysize); 5655084Sjohnlev 5665084Sjohnlev *ringpp = ring; 5675084Sjohnlev 5685084Sjohnlev return (DDI_SUCCESS); 5695084Sjohnlev 5705084Sjohnlev errout2: 5715084Sjohnlev /* unmap ring page */ 5725084Sjohnlev unmapop.host_addr = (uint64_t)(uintptr_t)ringva; 5735084Sjohnlev unmapop.handle = ring->xr_grant_hdl; 5745084Sjohnlev unmapop.dev_bus_addr = NULL; 5755084Sjohnlev (void) HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &unmapop, 1); 5765084Sjohnlev hat_release_mapping(kas.a_hat, ringva); 5775084Sjohnlev errout1: 5785084Sjohnlev vmem_xfree(heap_arena, ringva, PAGESIZE); 5795084Sjohnlev kmem_free(ring, sizeof (xendev_ring_t)); 5805084Sjohnlev return (DDI_FAILURE); 5815084Sjohnlev } 5825084Sjohnlev 5835084Sjohnlev /* 5845084Sjohnlev * Unmap a ring for a virtual device. 5855084Sjohnlev * This is used by backend drivers. 5865084Sjohnlev */ 5875084Sjohnlev void 5885084Sjohnlev xvdi_unmap_ring(xendev_ring_t *ring) 5895084Sjohnlev { 5905084Sjohnlev gnttab_unmap_grant_ref_t unmapop; 5915084Sjohnlev 5925084Sjohnlev ASSERT((ring != NULL) && (ring->xr_vaddr != NULL)); 5935084Sjohnlev 5945084Sjohnlev impl_acc_hdl_free(ring->xr_acc_hdl); 5955084Sjohnlev unmapop.host_addr = (uint64_t)(uintptr_t)ring->xr_vaddr; 5965084Sjohnlev unmapop.handle = ring->xr_grant_hdl; 5975084Sjohnlev unmapop.dev_bus_addr = NULL; 5985084Sjohnlev (void) HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &unmapop, 1); 5995084Sjohnlev hat_release_mapping(kas.a_hat, ring->xr_vaddr); 6005084Sjohnlev vmem_xfree(heap_arena, ring->xr_vaddr, PAGESIZE); 6015084Sjohnlev kmem_free(ring, sizeof (xendev_ring_t)); 6025084Sjohnlev } 6035741Smrj #endif /* XPV_HVM_DRIVER */ 6045084Sjohnlev 6055084Sjohnlev /* 6065084Sjohnlev * Re-initialise an inter-domain communications ring for the backend domain. 6075084Sjohnlev * ring will be re-initialized after re-grant succeed 6085084Sjohnlev * ring will be freed if fails to re-grant access to backend domain 6095084Sjohnlev * so, don't keep useful data in the ring 6105084Sjohnlev * used only in frontend driver 6115084Sjohnlev */ 6125084Sjohnlev static void 6135084Sjohnlev xvdi_reinit_ring(dev_info_t *dip, grant_ref_t *gref, xendev_ring_t *ringp) 6145084Sjohnlev { 6155084Sjohnlev paddr_t rpaddr; 6165084Sjohnlev maddr_t rmaddr; 6175084Sjohnlev 6185084Sjohnlev ASSERT((ringp != NULL) && (ringp->xr_paddr != 0)); 6195084Sjohnlev rpaddr = ringp->xr_paddr; 6205084Sjohnlev 6215084Sjohnlev rmaddr = DOMAIN_IS_INITDOMAIN(xen_info) ? rpaddr : pa_to_ma(rpaddr); 6225084Sjohnlev gnttab_grant_foreign_access_ref(ringp->xr_gref, xvdi_get_oeid(dip), 6235084Sjohnlev rmaddr >> PAGESHIFT, 0); 6245084Sjohnlev *gref = ringp->xr_gref; 6255084Sjohnlev 6265084Sjohnlev /* init frontend ring */ 6275084Sjohnlev xvdi_ring_init_sring(ringp); 6285084Sjohnlev xvdi_ring_init_front_ring(ringp, ringp->xr_sring.fr.nr_ents, 6295084Sjohnlev ringp->xr_entry_size); 6305084Sjohnlev } 6315084Sjohnlev 6325084Sjohnlev /* 6335084Sjohnlev * allocate Xen inter-domain communications ring for Xen virtual devices 6345084Sjohnlev * used only in frontend driver 6355084Sjohnlev * if *ringpp is not NULL, we'll simply re-init it 6365084Sjohnlev */ 6375084Sjohnlev int 6385084Sjohnlev xvdi_alloc_ring(dev_info_t *dip, size_t nentry, size_t entrysize, 6395084Sjohnlev grant_ref_t *gref, xendev_ring_t **ringpp) 6405084Sjohnlev { 6415084Sjohnlev size_t len; 6425084Sjohnlev xendev_ring_t *ring; 6435084Sjohnlev ddi_dma_cookie_t dma_cookie; 6445084Sjohnlev uint_t ncookies; 6455084Sjohnlev grant_ref_t ring_gref; 6465084Sjohnlev domid_t oeid; 6475084Sjohnlev maddr_t rmaddr; 6485084Sjohnlev 6495084Sjohnlev if (*ringpp) { 6505084Sjohnlev xvdi_reinit_ring(dip, gref, *ringpp); 6515084Sjohnlev return (DDI_SUCCESS); 6525084Sjohnlev } 6535084Sjohnlev 6545084Sjohnlev *ringpp = ring = kmem_zalloc(sizeof (xendev_ring_t), KM_SLEEP); 6555084Sjohnlev oeid = xvdi_get_oeid(dip); 6565084Sjohnlev 6575084Sjohnlev /* 6585084Sjohnlev * Allocate page for this ring buffer 6595084Sjohnlev */ 6605084Sjohnlev if (ddi_dma_alloc_handle(dip, &xendev_dc_dmaattr, DDI_DMA_SLEEP, 6615084Sjohnlev 0, &ring->xr_dma_hdl) != DDI_SUCCESS) 6625084Sjohnlev goto err; 6635084Sjohnlev 6645084Sjohnlev if (ddi_dma_mem_alloc(ring->xr_dma_hdl, PAGESIZE, 6655084Sjohnlev &xendev_dc_accattr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, 0, 6665084Sjohnlev &ring->xr_vaddr, &len, &ring->xr_acc_hdl) != DDI_SUCCESS) { 6675084Sjohnlev ddi_dma_free_handle(&ring->xr_dma_hdl); 6685084Sjohnlev goto err; 6695084Sjohnlev } 6705084Sjohnlev 6715084Sjohnlev if (ddi_dma_addr_bind_handle(ring->xr_dma_hdl, NULL, 6725084Sjohnlev ring->xr_vaddr, len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 6735084Sjohnlev DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies) != DDI_DMA_MAPPED) { 6745084Sjohnlev ddi_dma_mem_free(&ring->xr_acc_hdl); 6755084Sjohnlev ring->xr_vaddr = NULL; 6765084Sjohnlev ddi_dma_free_handle(&ring->xr_dma_hdl); 6775084Sjohnlev goto err; 6785084Sjohnlev } 6795084Sjohnlev ASSERT(ncookies == 1); 6805084Sjohnlev ring->xr_paddr = dma_cookie.dmac_laddress; 6815084Sjohnlev rmaddr = DOMAIN_IS_INITDOMAIN(xen_info) ? ring->xr_paddr : 6825084Sjohnlev pa_to_ma(ring->xr_paddr); 6835084Sjohnlev 6845084Sjohnlev if ((ring_gref = gnttab_grant_foreign_access(oeid, 6855084Sjohnlev rmaddr >> PAGESHIFT, 0)) == (grant_ref_t)-1) { 6865084Sjohnlev (void) ddi_dma_unbind_handle(ring->xr_dma_hdl); 6875084Sjohnlev ddi_dma_mem_free(&ring->xr_acc_hdl); 6885084Sjohnlev ring->xr_vaddr = NULL; 6895084Sjohnlev ddi_dma_free_handle(&ring->xr_dma_hdl); 6905084Sjohnlev goto err; 6915084Sjohnlev } 6925084Sjohnlev *gref = ring->xr_gref = ring_gref; 6935084Sjohnlev 6945084Sjohnlev /* init frontend ring */ 6955084Sjohnlev xvdi_ring_init_sring(ring); 6965084Sjohnlev xvdi_ring_init_front_ring(ring, nentry, entrysize); 6975084Sjohnlev 6985084Sjohnlev return (DDI_SUCCESS); 6995084Sjohnlev 7005084Sjohnlev err: 7015084Sjohnlev kmem_free(ring, sizeof (xendev_ring_t)); 7025084Sjohnlev return (DDI_FAILURE); 7035084Sjohnlev } 7045084Sjohnlev 7055084Sjohnlev /* 7065084Sjohnlev * Release ring buffers allocated for Xen devices 7075084Sjohnlev * used for frontend driver 7085084Sjohnlev */ 7095084Sjohnlev void 7105084Sjohnlev xvdi_free_ring(xendev_ring_t *ring) 7115084Sjohnlev { 7125084Sjohnlev ASSERT((ring != NULL) && (ring->xr_vaddr != NULL)); 7135084Sjohnlev 7145084Sjohnlev (void) gnttab_end_foreign_access_ref(ring->xr_gref, 0); 7155084Sjohnlev (void) ddi_dma_unbind_handle(ring->xr_dma_hdl); 7165084Sjohnlev ddi_dma_mem_free(&ring->xr_acc_hdl); 7175084Sjohnlev ddi_dma_free_handle(&ring->xr_dma_hdl); 7185084Sjohnlev kmem_free(ring, sizeof (xendev_ring_t)); 7195084Sjohnlev } 7205084Sjohnlev 7215084Sjohnlev dev_info_t * 7225084Sjohnlev xvdi_create_dev(dev_info_t *parent, xendev_devclass_t devclass, 7235084Sjohnlev domid_t dom, int vdev) 7245084Sjohnlev { 7255084Sjohnlev dev_info_t *dip; 7265084Sjohnlev boolean_t backend; 7275084Sjohnlev i_xd_cfg_t *xdcp; 7285084Sjohnlev char xsnamebuf[TYPICALMAXPATHLEN]; 7295084Sjohnlev char *type, *node = NULL, *xsname = NULL; 7305084Sjohnlev unsigned int tlen; 7315159Sjohnlev int ret; 7325084Sjohnlev 7335084Sjohnlev ASSERT(DEVI_BUSY_OWNED(parent)); 7345084Sjohnlev 7355084Sjohnlev backend = (dom != DOMID_SELF); 7365084Sjohnlev xdcp = i_xvdi_devclass2cfg(devclass); 7375084Sjohnlev ASSERT(xdcp != NULL); 7385084Sjohnlev 739*6175Sjohnlev if (vdev != VDEV_NOXS) { 7405084Sjohnlev if (!backend) { 7415084Sjohnlev (void) snprintf(xsnamebuf, sizeof (xsnamebuf), 7425084Sjohnlev "%s/%d", xdcp->xs_path_fe, vdev); 7435084Sjohnlev xsname = xsnamebuf; 7445084Sjohnlev node = xdcp->node_fe; 7455084Sjohnlev } else { 7465084Sjohnlev (void) snprintf(xsnamebuf, sizeof (xsnamebuf), 7475084Sjohnlev "%s/%d/%d", xdcp->xs_path_be, dom, vdev); 7485084Sjohnlev xsname = xsnamebuf; 7495084Sjohnlev node = xdcp->node_be; 7505084Sjohnlev } 7515084Sjohnlev } else { 7525084Sjohnlev node = xdcp->node_fe; 7535084Sjohnlev } 7545084Sjohnlev 7555084Sjohnlev /* Must have a driver to use. */ 7565084Sjohnlev if (node == NULL) 7575084Sjohnlev return (NULL); 7585084Sjohnlev 7595084Sjohnlev /* 7605084Sjohnlev * We need to check the state of this device before we go 7615084Sjohnlev * further, otherwise we'll end up with a dead loop if 7625084Sjohnlev * anything goes wrong. 7635084Sjohnlev */ 7645084Sjohnlev if ((xsname != NULL) && 7655084Sjohnlev (xenbus_read_driver_state(xsname) >= XenbusStateClosing)) 7665084Sjohnlev return (NULL); 7675084Sjohnlev 7685084Sjohnlev ndi_devi_alloc_sleep(parent, node, DEVI_SID_NODEID, &dip); 7695084Sjohnlev 7705084Sjohnlev /* 7715084Sjohnlev * Driver binding uses the compatible property _before_ the 7725084Sjohnlev * node name, so we set the node name to the 'model' of the 7735084Sjohnlev * device (i.e. 'xnb' or 'xdb') and, if 'type' is present, 7745084Sjohnlev * encode both the model and the type in a compatible property 7755084Sjohnlev * (i.e. 'xnb,netfront' or 'xnb,SUNW_mac'). This allows a 7765084Sjohnlev * driver binding based on the <model,type> pair _before_ a 7775084Sjohnlev * binding based on the node name. 7785084Sjohnlev */ 7795084Sjohnlev if ((xsname != NULL) && 7805084Sjohnlev (xenbus_read(XBT_NULL, xsname, "type", (void *)&type, &tlen) 7815084Sjohnlev == 0)) { 7825084Sjohnlev size_t clen; 7835084Sjohnlev char *c[1]; 7845084Sjohnlev 7855084Sjohnlev clen = strlen(node) + strlen(type) + 2; 7865084Sjohnlev c[0] = kmem_alloc(clen, KM_SLEEP); 7875084Sjohnlev (void) snprintf(c[0], clen, "%s,%s", node, type); 7885084Sjohnlev 7895084Sjohnlev (void) ndi_prop_update_string_array(DDI_DEV_T_NONE, 7905084Sjohnlev dip, "compatible", (char **)c, 1); 7915084Sjohnlev 7925084Sjohnlev kmem_free(c[0], clen); 7935084Sjohnlev kmem_free(type, tlen); 7945084Sjohnlev } 7955084Sjohnlev 7965084Sjohnlev (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "devclass", devclass); 7975084Sjohnlev (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "domain", dom); 7985084Sjohnlev (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vdev", vdev); 7995084Sjohnlev 8005084Sjohnlev if (i_ddi_devi_attached(parent)) 8015159Sjohnlev ret = ndi_devi_online(dip, 0); 8025084Sjohnlev else 8035159Sjohnlev ret = ndi_devi_bind_driver(dip, 0); 8045159Sjohnlev if (ret != NDI_SUCCESS) 8055159Sjohnlev (void) ndi_devi_offline(dip, NDI_DEVI_REMOVE); 8065084Sjohnlev 8075084Sjohnlev return (dip); 8085084Sjohnlev } 8095084Sjohnlev 8105084Sjohnlev /* 8115084Sjohnlev * xendev_enum_class() 8125084Sjohnlev */ 8135084Sjohnlev void 8145084Sjohnlev xendev_enum_class(dev_info_t *parent, xendev_devclass_t devclass) 8155084Sjohnlev { 8165910Smrj boolean_t dom0 = DOMAIN_IS_INITDOMAIN(xen_info); 8175910Smrj boolean_t domU = !dom0; 8185084Sjohnlev i_xd_cfg_t *xdcp; 8195084Sjohnlev 8205084Sjohnlev xdcp = i_xvdi_devclass2cfg(devclass); 8215084Sjohnlev ASSERT(xdcp != NULL); 8225084Sjohnlev 8235910Smrj if (dom0 && !(xdcp->flags & XD_DOM_ZERO)) 8245910Smrj return; 8255910Smrj 8265910Smrj if (domU && !(xdcp->flags & XD_DOM_GUEST)) 8275910Smrj return; 8285910Smrj 8295084Sjohnlev if (xdcp->xsdev == NULL) { 8305084Sjohnlev int circ; 8315084Sjohnlev 8325084Sjohnlev /* 8335084Sjohnlev * Don't need to probe this kind of device from the 8345084Sjohnlev * store, just create one if it doesn't exist. 8355084Sjohnlev */ 8365084Sjohnlev 8375084Sjohnlev ndi_devi_enter(parent, &circ); 838*6175Sjohnlev if (xvdi_find_dev(parent, devclass, DOMID_SELF, VDEV_NOXS) 8395084Sjohnlev == NULL) 8405084Sjohnlev (void) xvdi_create_dev(parent, devclass, 841*6175Sjohnlev DOMID_SELF, VDEV_NOXS); 8425084Sjohnlev ndi_devi_exit(parent, circ); 8435084Sjohnlev } else { 8445084Sjohnlev /* 8455084Sjohnlev * Probe this kind of device from the store, both 8465084Sjohnlev * frontend and backend. 8475084Sjohnlev */ 8485084Sjohnlev 8495084Sjohnlev i_xvdi_enum_fe(parent, xdcp); 8505084Sjohnlev i_xvdi_enum_be(parent, xdcp); 8515084Sjohnlev } 8525084Sjohnlev } 8535084Sjohnlev 8545084Sjohnlev /* 8555084Sjohnlev * xendev_enum_all() 8565084Sjohnlev */ 8575084Sjohnlev void 8585084Sjohnlev xendev_enum_all(dev_info_t *parent, boolean_t store_unavailable) 8595084Sjohnlev { 8605084Sjohnlev int i; 8615084Sjohnlev i_xd_cfg_t *xdcp; 8625084Sjohnlev boolean_t dom0 = DOMAIN_IS_INITDOMAIN(xen_info); 8635084Sjohnlev 8645084Sjohnlev for (i = 0, xdcp = xdci; i < NXDC; i++, xdcp++) { 8655084Sjohnlev /* 8665084Sjohnlev * Dom0 relies on watchpoints to create non-soft 8675084Sjohnlev * devices - don't attempt to iterate over the store. 8685084Sjohnlev */ 8695084Sjohnlev if (dom0 && (xdcp->xsdev != NULL)) 8705084Sjohnlev continue; 8715084Sjohnlev 8725084Sjohnlev /* 8735084Sjohnlev * If the store is not yet available, don't attempt to 8745084Sjohnlev * iterate. 8755084Sjohnlev */ 8765084Sjohnlev if (store_unavailable && (xdcp->xsdev != NULL)) 8775084Sjohnlev continue; 8785084Sjohnlev 8795084Sjohnlev xendev_enum_class(parent, xdcp->devclass); 8805084Sjohnlev } 8815084Sjohnlev } 8825084Sjohnlev 8835084Sjohnlev xendev_devclass_t 8845084Sjohnlev xendev_nodename_to_devclass(char *nodename) 8855084Sjohnlev { 8865084Sjohnlev int i; 8875084Sjohnlev i_xd_cfg_t *xdcp; 8885084Sjohnlev 8895084Sjohnlev /* 8905084Sjohnlev * This relies on the convention that variants of a base 8915084Sjohnlev * driver share the same prefix and that there are no drivers 8925084Sjohnlev * which share a common prefix with the name of any other base 8935084Sjohnlev * drivers. 8945084Sjohnlev * 8955084Sjohnlev * So for a base driver 'xnb' (which is the name listed in 8965084Sjohnlev * xdci) the variants all begin with the string 'xnb' (in fact 8975084Sjohnlev * they are 'xnbe', 'xnbo' and 'xnbu') and there are no other 8985084Sjohnlev * base drivers which have the prefix 'xnb'. 8995084Sjohnlev */ 9005084Sjohnlev ASSERT(nodename != NULL); 9015084Sjohnlev for (i = 0, xdcp = xdci; i < NXDC; i++, xdcp++) { 9025084Sjohnlev if (((xdcp->node_fe != NULL) && 9035084Sjohnlev (strncmp(nodename, xdcp->node_fe, 9045084Sjohnlev strlen(xdcp->node_fe)) == 0)) || 9055084Sjohnlev ((xdcp->node_be != NULL) && 9065084Sjohnlev (strncmp(nodename, xdcp->node_be, 9075084Sjohnlev strlen(xdcp->node_be)) == 0))) 9085084Sjohnlev 9095084Sjohnlev return (xdcp->devclass); 9105084Sjohnlev } 9115084Sjohnlev return (XEN_INVAL); 9125084Sjohnlev } 9135084Sjohnlev 9145084Sjohnlev int 9155084Sjohnlev xendev_devclass_ipl(xendev_devclass_t devclass) 9165084Sjohnlev { 9175084Sjohnlev i_xd_cfg_t *xdcp; 9185084Sjohnlev 9195084Sjohnlev xdcp = i_xvdi_devclass2cfg(devclass); 9205084Sjohnlev ASSERT(xdcp != NULL); 9215084Sjohnlev 9225084Sjohnlev return (xdcp->xd_ipl); 9235084Sjohnlev } 9245084Sjohnlev 9255084Sjohnlev /* 9265084Sjohnlev * Determine if a devinfo instance exists of a particular device 9275084Sjohnlev * class, domain and xenstore virtual device number. 9285084Sjohnlev */ 9295084Sjohnlev dev_info_t * 9305084Sjohnlev xvdi_find_dev(dev_info_t *parent, xendev_devclass_t devclass, 9315084Sjohnlev domid_t dom, int vdev) 9325084Sjohnlev { 9335084Sjohnlev dev_info_t *dip; 9345084Sjohnlev 9355084Sjohnlev ASSERT(DEVI_BUSY_OWNED(parent)); 9365084Sjohnlev 9375084Sjohnlev switch (devclass) { 9385084Sjohnlev case XEN_CONSOLE: 9395084Sjohnlev case XEN_XENBUS: 9405084Sjohnlev case XEN_DOMCAPS: 9415084Sjohnlev case XEN_BALLOON: 9425084Sjohnlev case XEN_EVTCHN: 9435084Sjohnlev case XEN_PRIVCMD: 9445084Sjohnlev /* Console and soft devices have no vdev. */ 945*6175Sjohnlev vdev = VDEV_NOXS; 9465084Sjohnlev break; 9475084Sjohnlev default: 9485084Sjohnlev break; 9495084Sjohnlev } 9505084Sjohnlev 9515084Sjohnlev for (dip = ddi_get_child(parent); dip != NULL; 9525084Sjohnlev dip = ddi_get_next_sibling(dip)) { 9535084Sjohnlev int *vdevnump, *domidp, *devclsp, vdevnum; 9545084Sjohnlev uint_t ndomid, nvdevnum, ndevcls; 9555084Sjohnlev xendev_devclass_t devcls; 9565084Sjohnlev domid_t domid; 9575084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 9585084Sjohnlev 9595084Sjohnlev if (pdp == NULL) { 9605084Sjohnlev if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 9615084Sjohnlev DDI_PROP_DONTPASS, "domain", &domidp, &ndomid) != 9625084Sjohnlev DDI_PROP_SUCCESS) 9635084Sjohnlev continue; 9645084Sjohnlev ASSERT(ndomid == 1); 9655084Sjohnlev domid = (domid_t)*domidp; 9665084Sjohnlev ddi_prop_free(domidp); 9675084Sjohnlev 9685084Sjohnlev if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 9695084Sjohnlev DDI_PROP_DONTPASS, "vdev", &vdevnump, &nvdevnum) != 9705084Sjohnlev DDI_PROP_SUCCESS) 9715084Sjohnlev continue; 9725084Sjohnlev ASSERT(nvdevnum == 1); 9735084Sjohnlev vdevnum = *vdevnump; 9745084Sjohnlev ddi_prop_free(vdevnump); 9755084Sjohnlev 9765084Sjohnlev if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 9775084Sjohnlev DDI_PROP_DONTPASS, "devclass", &devclsp, 9785084Sjohnlev &ndevcls) != DDI_PROP_SUCCESS) 9795084Sjohnlev continue; 9805084Sjohnlev ASSERT(ndevcls == 1); 9815084Sjohnlev devcls = (xendev_devclass_t)*devclsp; 9825084Sjohnlev ddi_prop_free(devclsp); 9835084Sjohnlev } else { 9845084Sjohnlev domid = pdp->xd_domain; 9855084Sjohnlev vdevnum = pdp->xd_vdevnum; 9865084Sjohnlev devcls = pdp->xd_devclass; 9875084Sjohnlev } 9885084Sjohnlev 9895084Sjohnlev if ((domid == dom) && (vdevnum == vdev) && (devcls == devclass)) 9905084Sjohnlev return (dip); 9915084Sjohnlev } 9925084Sjohnlev return (NULL); 9935084Sjohnlev } 9945084Sjohnlev 9955084Sjohnlev int 9965084Sjohnlev xvdi_get_evtchn(dev_info_t *xdip) 9975084Sjohnlev { 9985084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 9995084Sjohnlev 10005084Sjohnlev ASSERT(pdp != NULL); 10015084Sjohnlev return (pdp->xd_evtchn); 10025084Sjohnlev } 10035084Sjohnlev 10045084Sjohnlev int 10055084Sjohnlev xvdi_get_vdevnum(dev_info_t *xdip) 10065084Sjohnlev { 10075084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 10085084Sjohnlev 10095084Sjohnlev ASSERT(pdp != NULL); 10105084Sjohnlev return (pdp->xd_vdevnum); 10115084Sjohnlev } 10125084Sjohnlev 10135084Sjohnlev char * 10145084Sjohnlev xvdi_get_xsname(dev_info_t *xdip) 10155084Sjohnlev { 10165084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 10175084Sjohnlev 10185084Sjohnlev ASSERT(pdp != NULL); 10195084Sjohnlev return ((char *)(pdp->xd_xsdev.nodename)); 10205084Sjohnlev } 10215084Sjohnlev 10225084Sjohnlev char * 10235084Sjohnlev xvdi_get_oename(dev_info_t *xdip) 10245084Sjohnlev { 10255084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 10265084Sjohnlev 10275084Sjohnlev ASSERT(pdp != NULL); 10285084Sjohnlev if (pdp->xd_devclass == XEN_CONSOLE) 10295084Sjohnlev return (NULL); 10305084Sjohnlev return ((char *)(pdp->xd_xsdev.otherend)); 10315084Sjohnlev } 10325084Sjohnlev 10335084Sjohnlev struct xenbus_device * 10345084Sjohnlev xvdi_get_xsd(dev_info_t *xdip) 10355084Sjohnlev { 10365084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 10375084Sjohnlev 10385084Sjohnlev ASSERT(pdp != NULL); 10395084Sjohnlev return (&pdp->xd_xsdev); 10405084Sjohnlev } 10415084Sjohnlev 10425084Sjohnlev domid_t 10435084Sjohnlev xvdi_get_oeid(dev_info_t *xdip) 10445084Sjohnlev { 10455084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(xdip); 10465084Sjohnlev 10475084Sjohnlev ASSERT(pdp != NULL); 10485084Sjohnlev if (pdp->xd_devclass == XEN_CONSOLE) 10495084Sjohnlev return ((domid_t)-1); 10505084Sjohnlev return ((domid_t)(pdp->xd_xsdev.otherend_id)); 10515084Sjohnlev } 10525084Sjohnlev 10535084Sjohnlev void 10545084Sjohnlev xvdi_dev_error(dev_info_t *dip, int errno, char *errstr) 10555084Sjohnlev { 10565084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 10575084Sjohnlev 10585084Sjohnlev ASSERT(pdp != NULL); 10595084Sjohnlev xenbus_dev_error(&pdp->xd_xsdev, errno, errstr); 10605084Sjohnlev } 10615084Sjohnlev 10625084Sjohnlev void 10635084Sjohnlev xvdi_fatal_error(dev_info_t *dip, int errno, char *errstr) 10645084Sjohnlev { 10655084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 10665084Sjohnlev 10675084Sjohnlev ASSERT(pdp != NULL); 10685084Sjohnlev xenbus_dev_fatal(&pdp->xd_xsdev, errno, errstr); 10695084Sjohnlev } 10705084Sjohnlev 10715084Sjohnlev static void 10725084Sjohnlev i_xvdi_oestate_handler(void *arg) 10735084Sjohnlev { 10745084Sjohnlev dev_info_t *dip = arg; 10755084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 10765084Sjohnlev XenbusState oestate = pdp->xd_xsdev.otherend_state; 10775084Sjohnlev ddi_eventcookie_t evc; 10785084Sjohnlev 10795084Sjohnlev mutex_enter(&pdp->xd_lk); 10805084Sjohnlev 10815084Sjohnlev if (pdp->xd_oe_ehid != NULL) { 10825084Sjohnlev /* send notification to driver */ 10835084Sjohnlev if (ddi_get_eventcookie(dip, XS_OE_STATE, 10845084Sjohnlev &evc) == DDI_SUCCESS) { 10855084Sjohnlev mutex_exit(&pdp->xd_lk); 10865084Sjohnlev (void) ndi_post_event(dip, dip, evc, &oestate); 10875084Sjohnlev mutex_enter(&pdp->xd_lk); 10885084Sjohnlev } 10895084Sjohnlev } else { 10905084Sjohnlev /* 10915084Sjohnlev * take default action, if driver hasn't registered its 10925084Sjohnlev * event handler yet 10935084Sjohnlev */ 10945084Sjohnlev if (oestate == XenbusStateClosing) { 10955084Sjohnlev (void) xvdi_switch_state(dip, XBT_NULL, 10965084Sjohnlev XenbusStateClosed); 10975084Sjohnlev } else if (oestate == XenbusStateClosed) { 10985084Sjohnlev (void) xvdi_switch_state(dip, XBT_NULL, 10995084Sjohnlev XenbusStateClosed); 11005084Sjohnlev (void) xvdi_post_event(dip, XEN_HP_REMOVE); 11015084Sjohnlev } 11025084Sjohnlev } 11035084Sjohnlev 11045084Sjohnlev mutex_exit(&pdp->xd_lk); 11055084Sjohnlev 11065084Sjohnlev /* 11075084Sjohnlev * We'll try to remove the devinfo node of this device if the 11085084Sjohnlev * other end has closed. 11095084Sjohnlev */ 11105084Sjohnlev if (oestate == XenbusStateClosed) 11115084Sjohnlev (void) ddi_taskq_dispatch(DEVI(ddi_get_parent(dip))->devi_taskq, 11125084Sjohnlev xendev_offline_device, dip, DDI_SLEEP); 11135084Sjohnlev } 11145084Sjohnlev 11155084Sjohnlev static void 11165084Sjohnlev i_xvdi_hpstate_handler(void *arg) 11175084Sjohnlev { 11185084Sjohnlev dev_info_t *dip = (dev_info_t *)arg; 11195084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 11205084Sjohnlev ddi_eventcookie_t evc; 11215084Sjohnlev char *hp_status; 11225084Sjohnlev unsigned int hpl; 11235084Sjohnlev 11245084Sjohnlev mutex_enter(&pdp->xd_lk); 11255084Sjohnlev if ((ddi_get_eventcookie(dip, XS_HP_STATE, &evc) == DDI_SUCCESS) && 11265084Sjohnlev (xenbus_read(XBT_NULL, pdp->xd_hp_watch.node, "", 11275084Sjohnlev (void *)&hp_status, &hpl) == 0)) { 11285084Sjohnlev 11295084Sjohnlev xendev_hotplug_state_t new_state = Unrecognized; 11305084Sjohnlev 11315084Sjohnlev if (strcmp(hp_status, "connected") == 0) 11325084Sjohnlev new_state = Connected; 11335084Sjohnlev 11345084Sjohnlev mutex_exit(&pdp->xd_lk); 11355084Sjohnlev 11365084Sjohnlev (void) ndi_post_event(dip, dip, evc, &new_state); 11375084Sjohnlev kmem_free(hp_status, hpl); 11385084Sjohnlev return; 11395084Sjohnlev } 11405084Sjohnlev mutex_exit(&pdp->xd_lk); 11415084Sjohnlev } 11425084Sjohnlev 11435084Sjohnlev void 11445084Sjohnlev xvdi_notify_oe(dev_info_t *dip) 11455084Sjohnlev { 11465084Sjohnlev struct xendev_ppd *pdp; 11475084Sjohnlev 11485084Sjohnlev pdp = ddi_get_parent_data(dip); 11495084Sjohnlev ASSERT(pdp->xd_evtchn != INVALID_EVTCHN); 11505084Sjohnlev ec_notify_via_evtchn(pdp->xd_evtchn); 11515084Sjohnlev } 11525084Sjohnlev 11535084Sjohnlev static void 11545084Sjohnlev i_xvdi_bepath_cb(struct xenbus_watch *w, const char **vec, unsigned int len) 11555084Sjohnlev { 11565084Sjohnlev dev_info_t *dip = (dev_info_t *)w->dev; 11575084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 11585084Sjohnlev char *be = NULL; 11595084Sjohnlev unsigned int bel; 11605084Sjohnlev 11615084Sjohnlev ASSERT(len > XS_WATCH_PATH); 11625084Sjohnlev ASSERT(vec[XS_WATCH_PATH] != NULL); 11635084Sjohnlev 11645084Sjohnlev /* 11655084Sjohnlev * If the backend is not the same as that we already stored, 11665084Sjohnlev * re-set our watch for its' state. 11675084Sjohnlev */ 11685084Sjohnlev if ((xenbus_read(XBT_NULL, "", vec[XS_WATCH_PATH], (void *)be, &bel) 11695084Sjohnlev == 0) && (strcmp(be, pdp->xd_xsdev.otherend) != 0)) 11705084Sjohnlev (void) i_xvdi_add_watch_oestate(dip); 11715084Sjohnlev 11725084Sjohnlev if (be != NULL) { 11735084Sjohnlev ASSERT(bel > 0); 11745084Sjohnlev kmem_free(be, bel); 11755084Sjohnlev } 11765084Sjohnlev } 11775084Sjohnlev 11785084Sjohnlev static int 11795084Sjohnlev i_xvdi_add_watch_oestate(dev_info_t *dip) 11805084Sjohnlev { 11815084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 11825084Sjohnlev 11835084Sjohnlev ASSERT(pdp != NULL); 11845084Sjohnlev ASSERT(pdp->xd_xsdev.nodename != NULL); 11855084Sjohnlev ASSERT(mutex_owned(&pdp->xd_lk)); 11865084Sjohnlev 11875084Sjohnlev /* 11885084Sjohnlev * Create taskq for delivering other end state change event to 11895084Sjohnlev * this device later. 11905084Sjohnlev * 11915084Sjohnlev * Set nthreads to 1 to make sure that events can be delivered 11925084Sjohnlev * in order. 11935084Sjohnlev * 11945084Sjohnlev * Note: It is _not_ guaranteed that driver can see every 11955084Sjohnlev * xenstore change under the path that it is watching. If two 11965084Sjohnlev * changes happen consecutively in a very short amount of 11975084Sjohnlev * time, it is likely that the driver will see only the last 11985084Sjohnlev * one. 11995084Sjohnlev */ 12005084Sjohnlev if (pdp->xd_oe_taskq == NULL) 12015084Sjohnlev if ((pdp->xd_oe_taskq = ddi_taskq_create(dip, 12025084Sjohnlev "xendev_oe_taskq", 1, TASKQ_DEFAULTPRI, 0)) == NULL) 12035084Sjohnlev return (DDI_FAILURE); 12045084Sjohnlev 12055084Sjohnlev /* 12065084Sjohnlev * Watch for changes to the XenbusState of otherend. 12075084Sjohnlev */ 12085084Sjohnlev pdp->xd_xsdev.otherend_state = XenbusStateUnknown; 12095084Sjohnlev pdp->xd_xsdev.otherend_changed = i_xvdi_oestate_cb; 12105084Sjohnlev 12115084Sjohnlev if (talk_to_otherend(&pdp->xd_xsdev) != 0) { 12125084Sjohnlev i_xvdi_rem_watch_oestate(dip); 12135084Sjohnlev return (DDI_FAILURE); 12145084Sjohnlev } 12155084Sjohnlev 12165084Sjohnlev return (DDI_SUCCESS); 12175084Sjohnlev } 12185084Sjohnlev 12195084Sjohnlev static void 12205084Sjohnlev i_xvdi_rem_watch_oestate(dev_info_t *dip) 12215084Sjohnlev { 12225084Sjohnlev struct xendev_ppd *pdp; 12235084Sjohnlev struct xenbus_device *dev; 12245084Sjohnlev 12255084Sjohnlev pdp = ddi_get_parent_data(dip); 12265084Sjohnlev ASSERT(pdp != NULL); 12275084Sjohnlev ASSERT(mutex_owned(&pdp->xd_lk)); 12285084Sjohnlev 12295084Sjohnlev dev = &pdp->xd_xsdev; 12305084Sjohnlev 12315084Sjohnlev /* Unwatch for changes to XenbusState of otherend */ 12325084Sjohnlev if (dev->otherend_watch.node != NULL) { 12335084Sjohnlev mutex_exit(&pdp->xd_lk); 12345084Sjohnlev unregister_xenbus_watch(&dev->otherend_watch); 12355084Sjohnlev mutex_enter(&pdp->xd_lk); 12365084Sjohnlev } 12375084Sjohnlev 12385084Sjohnlev /* make sure no event handler is running */ 12395084Sjohnlev if (pdp->xd_oe_taskq != NULL) { 12405084Sjohnlev mutex_exit(&pdp->xd_lk); 12415084Sjohnlev ddi_taskq_destroy(pdp->xd_oe_taskq); 12425084Sjohnlev mutex_enter(&pdp->xd_lk); 12435084Sjohnlev pdp->xd_oe_taskq = NULL; 12445084Sjohnlev } 12455084Sjohnlev 12465084Sjohnlev /* clean up */ 12475084Sjohnlev dev->otherend_state = XenbusStateUnknown; 12485084Sjohnlev dev->otherend_id = (domid_t)-1; 12495084Sjohnlev if (dev->otherend_watch.node != NULL) 12505084Sjohnlev kmem_free((void *)dev->otherend_watch.node, 12515084Sjohnlev strlen(dev->otherend_watch.node) + 1); 12525084Sjohnlev dev->otherend_watch.node = NULL; 12535084Sjohnlev if (dev->otherend != NULL) 12545084Sjohnlev kmem_free((void *)dev->otherend, strlen(dev->otherend) + 1); 12555084Sjohnlev dev->otherend = NULL; 12565084Sjohnlev } 12575084Sjohnlev 12585084Sjohnlev static int 12595084Sjohnlev i_xvdi_add_watch_hpstate(dev_info_t *dip) 12605084Sjohnlev { 12615084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 12625084Sjohnlev 12635084Sjohnlev ASSERT(pdp != NULL); 12645084Sjohnlev ASSERT(pdp->xd_xsdev.frontend == 0); 12655084Sjohnlev ASSERT(mutex_owned(&pdp->xd_lk)); 12665084Sjohnlev 12675084Sjohnlev /* 12685084Sjohnlev * Create taskq for delivering hotplug status change event to 12695084Sjohnlev * this device later. 12705084Sjohnlev * 12715084Sjohnlev * Set nthreads to 1 to make sure that events can be delivered 12725084Sjohnlev * in order. 12735084Sjohnlev * 12745084Sjohnlev * Note: It is _not_ guaranteed that driver can see every 12755084Sjohnlev * hotplug status change under the path that it is 12765084Sjohnlev * watching. If two changes happen consecutively in a very 12775084Sjohnlev * short amount of time, it is likely that the driver only 12785084Sjohnlev * sees the last one. 12795084Sjohnlev */ 12805084Sjohnlev if (pdp->xd_hp_taskq == NULL) 12815084Sjohnlev if ((pdp->xd_hp_taskq = ddi_taskq_create(dip, 12825084Sjohnlev "xendev_hp_taskq", 1, TASKQ_DEFAULTPRI, 0)) == NULL) 12835084Sjohnlev return (DDI_FAILURE); 12845084Sjohnlev 12855084Sjohnlev if (pdp->xd_hp_watch.node == NULL) { 12865084Sjohnlev size_t len; 12875084Sjohnlev char *path; 12885084Sjohnlev 12895084Sjohnlev ASSERT(pdp->xd_xsdev.nodename != NULL); 12905084Sjohnlev 12915084Sjohnlev len = strlen(pdp->xd_xsdev.nodename) + 12925084Sjohnlev strlen("/hotplug-status") + 1; 12935084Sjohnlev path = kmem_alloc(len, KM_SLEEP); 12945084Sjohnlev (void) snprintf(path, len, "%s/hotplug-status", 12955084Sjohnlev pdp->xd_xsdev.nodename); 12965084Sjohnlev 12975084Sjohnlev pdp->xd_hp_watch.node = path; 12985084Sjohnlev pdp->xd_hp_watch.callback = i_xvdi_hpstate_cb; 12995084Sjohnlev pdp->xd_hp_watch.dev = (struct xenbus_device *)dip; /* yuck! */ 13005084Sjohnlev if (register_xenbus_watch(&pdp->xd_hp_watch) != 0) { 13015084Sjohnlev i_xvdi_rem_watch_hpstate(dip); 13025084Sjohnlev return (DDI_FAILURE); 13035084Sjohnlev } 13045084Sjohnlev } 13055084Sjohnlev 13065084Sjohnlev return (DDI_SUCCESS); 13075084Sjohnlev } 13085084Sjohnlev 13095084Sjohnlev static void 13105084Sjohnlev i_xvdi_rem_watch_hpstate(dev_info_t *dip) 13115084Sjohnlev { 13125084Sjohnlev struct xendev_ppd *pdp; 13135084Sjohnlev pdp = ddi_get_parent_data(dip); 13145084Sjohnlev 13155084Sjohnlev ASSERT(pdp != NULL); 13165084Sjohnlev ASSERT(pdp->xd_xsdev.frontend == 0); 13175084Sjohnlev ASSERT(mutex_owned(&pdp->xd_lk)); 13185084Sjohnlev 13195084Sjohnlev /* Unwatch for changes to "hotplug-status" node for backend device. */ 13205084Sjohnlev if (pdp->xd_hp_watch.node != NULL) { 13215084Sjohnlev mutex_exit(&pdp->xd_lk); 13225084Sjohnlev unregister_xenbus_watch(&pdp->xd_hp_watch); 13235084Sjohnlev mutex_enter(&pdp->xd_lk); 13245084Sjohnlev } 13255084Sjohnlev 13265084Sjohnlev /* Make sure no event handler is running. */ 13275084Sjohnlev if (pdp->xd_hp_taskq != NULL) { 13285084Sjohnlev mutex_exit(&pdp->xd_lk); 13295084Sjohnlev ddi_taskq_destroy(pdp->xd_hp_taskq); 13305084Sjohnlev mutex_enter(&pdp->xd_lk); 13315084Sjohnlev pdp->xd_hp_taskq = NULL; 13325084Sjohnlev } 13335084Sjohnlev 13345084Sjohnlev /* Clean up. */ 13355084Sjohnlev if (pdp->xd_hp_watch.node != NULL) { 13365084Sjohnlev kmem_free((void *)pdp->xd_hp_watch.node, 13375084Sjohnlev strlen(pdp->xd_hp_watch.node) + 1); 13385084Sjohnlev pdp->xd_hp_watch.node = NULL; 13395084Sjohnlev } 13405084Sjohnlev } 13415084Sjohnlev 13425084Sjohnlev static int 13435084Sjohnlev i_xvdi_add_watches(dev_info_t *dip) 13445084Sjohnlev { 13455084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 13465084Sjohnlev 13475084Sjohnlev ASSERT(pdp != NULL); 13485084Sjohnlev 13495084Sjohnlev mutex_enter(&pdp->xd_lk); 13505084Sjohnlev 13515084Sjohnlev if (i_xvdi_add_watch_oestate(dip) != DDI_SUCCESS) { 13525084Sjohnlev mutex_exit(&pdp->xd_lk); 13535084Sjohnlev return (DDI_FAILURE); 13545084Sjohnlev } 13555084Sjohnlev 13565084Sjohnlev if (pdp->xd_xsdev.frontend == 1) { 13575084Sjohnlev /* 13585084Sjohnlev * Frontend devices must watch for the backend path 13595084Sjohnlev * changing. 13605084Sjohnlev */ 13615084Sjohnlev if (i_xvdi_add_watch_bepath(dip) != DDI_SUCCESS) 13625084Sjohnlev goto unwatch_and_fail; 13635084Sjohnlev } else { 13645084Sjohnlev /* 13655084Sjohnlev * Backend devices must watch for hotplug events. 13665084Sjohnlev */ 13675084Sjohnlev if (i_xvdi_add_watch_hpstate(dip) != DDI_SUCCESS) 13685084Sjohnlev goto unwatch_and_fail; 13695084Sjohnlev } 13705084Sjohnlev 13715084Sjohnlev mutex_exit(&pdp->xd_lk); 13725084Sjohnlev 13735084Sjohnlev return (DDI_SUCCESS); 13745084Sjohnlev 13755084Sjohnlev unwatch_and_fail: 13765084Sjohnlev i_xvdi_rem_watch_oestate(dip); 13775084Sjohnlev mutex_exit(&pdp->xd_lk); 13785084Sjohnlev 13795084Sjohnlev return (DDI_FAILURE); 13805084Sjohnlev } 13815084Sjohnlev 13825084Sjohnlev static void 13835084Sjohnlev i_xvdi_rem_watches(dev_info_t *dip) 13845084Sjohnlev { 13855084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 13865084Sjohnlev 13875084Sjohnlev ASSERT(pdp != NULL); 13885084Sjohnlev 13895084Sjohnlev mutex_enter(&pdp->xd_lk); 13905084Sjohnlev 13915084Sjohnlev i_xvdi_rem_watch_oestate(dip); 13925084Sjohnlev 13935084Sjohnlev if (pdp->xd_xsdev.frontend == 1) 13945084Sjohnlev i_xvdi_rem_watch_bepath(dip); 13955084Sjohnlev else 13965084Sjohnlev i_xvdi_rem_watch_hpstate(dip); 13975084Sjohnlev 13985084Sjohnlev mutex_exit(&pdp->xd_lk); 13995084Sjohnlev } 14005084Sjohnlev 14015084Sjohnlev static int 14025084Sjohnlev i_xvdi_add_watch_bepath(dev_info_t *dip) 14035084Sjohnlev { 14045084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 14055084Sjohnlev 14065084Sjohnlev ASSERT(pdp != NULL); 14075084Sjohnlev ASSERT(pdp->xd_xsdev.frontend == 1); 14085084Sjohnlev 14095084Sjohnlev /* 14105084Sjohnlev * Frontend devices need to watch for the backend path changing. 14115084Sjohnlev */ 14125084Sjohnlev if (pdp->xd_bepath_watch.node == NULL) { 14135084Sjohnlev size_t len; 14145084Sjohnlev char *path; 14155084Sjohnlev 14165084Sjohnlev ASSERT(pdp->xd_xsdev.nodename != NULL); 14175084Sjohnlev 14185084Sjohnlev len = strlen(pdp->xd_xsdev.nodename) + strlen("/backend") + 1; 14195084Sjohnlev path = kmem_alloc(len, KM_SLEEP); 14205084Sjohnlev (void) snprintf(path, len, "%s/backend", 14215084Sjohnlev pdp->xd_xsdev.nodename); 14225084Sjohnlev 14235084Sjohnlev pdp->xd_bepath_watch.node = path; 14245084Sjohnlev pdp->xd_bepath_watch.callback = i_xvdi_bepath_cb; 14255084Sjohnlev pdp->xd_bepath_watch.dev = (struct xenbus_device *)dip; 14265084Sjohnlev if (register_xenbus_watch(&pdp->xd_bepath_watch) != 0) { 14275084Sjohnlev kmem_free(path, len); 14285084Sjohnlev pdp->xd_bepath_watch.node = NULL; 14295084Sjohnlev return (DDI_FAILURE); 14305084Sjohnlev } 14315084Sjohnlev } 14325084Sjohnlev 14335084Sjohnlev return (DDI_SUCCESS); 14345084Sjohnlev } 14355084Sjohnlev 14365084Sjohnlev static void 14375084Sjohnlev i_xvdi_rem_watch_bepath(dev_info_t *dip) 14385084Sjohnlev { 14395084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 14405084Sjohnlev 14415084Sjohnlev ASSERT(pdp != NULL); 14425084Sjohnlev ASSERT(pdp->xd_xsdev.frontend == 1); 14435084Sjohnlev ASSERT(mutex_owned(&pdp->xd_lk)); 14445084Sjohnlev 14455084Sjohnlev if (pdp->xd_bepath_watch.node != NULL) { 14465084Sjohnlev mutex_exit(&pdp->xd_lk); 14475084Sjohnlev unregister_xenbus_watch(&pdp->xd_bepath_watch); 14485084Sjohnlev mutex_enter(&pdp->xd_lk); 14495084Sjohnlev 14505084Sjohnlev kmem_free((void *)(pdp->xd_bepath_watch.node), 14515084Sjohnlev strlen(pdp->xd_bepath_watch.node) + 1); 14525084Sjohnlev pdp->xd_bepath_watch.node = NULL; 14535084Sjohnlev } 14545084Sjohnlev } 14555084Sjohnlev 14565084Sjohnlev int 14575084Sjohnlev xvdi_switch_state(dev_info_t *dip, xenbus_transaction_t xbt, 14585084Sjohnlev XenbusState newState) 14595084Sjohnlev { 14605084Sjohnlev int rv; 14615084Sjohnlev struct xendev_ppd *pdp; 14625084Sjohnlev 14635084Sjohnlev pdp = ddi_get_parent_data(dip); 14645084Sjohnlev ASSERT(pdp != NULL); 14655084Sjohnlev 14665084Sjohnlev XVDI_DPRINTF(XVDI_DBG_STATE, 14675084Sjohnlev "xvdi_switch_state: dip 0x%p moves to %d", 14685084Sjohnlev (void *)dip, newState); 14695084Sjohnlev 14705084Sjohnlev rv = xenbus_switch_state(&pdp->xd_xsdev, xbt, newState); 14715084Sjohnlev if (rv > 0) 14725084Sjohnlev cmn_err(CE_WARN, "xvdi_switch_state: change state failed"); 14735084Sjohnlev 14745084Sjohnlev return (rv); 14755084Sjohnlev } 14765084Sjohnlev 14775084Sjohnlev /* 14785084Sjohnlev * Notify hotplug script running in userland 14795084Sjohnlev */ 14805084Sjohnlev int 14815084Sjohnlev xvdi_post_event(dev_info_t *dip, xendev_hotplug_cmd_t hpc) 14825084Sjohnlev { 14835084Sjohnlev struct xendev_ppd *pdp; 14845084Sjohnlev nvlist_t *attr_list = NULL; 14855084Sjohnlev i_xd_cfg_t *xdcp; 14865084Sjohnlev sysevent_id_t eid; 14875084Sjohnlev int err; 14885084Sjohnlev char devname[256]; /* XXPV dme: ? */ 14895084Sjohnlev 14905084Sjohnlev pdp = ddi_get_parent_data(dip); 14915084Sjohnlev ASSERT(pdp != NULL); 14925084Sjohnlev 14935084Sjohnlev xdcp = i_xvdi_devclass2cfg(pdp->xd_devclass); 14945084Sjohnlev ASSERT(xdcp != NULL); 14955084Sjohnlev 14965084Sjohnlev (void) snprintf(devname, sizeof (devname) - 1, "%s%d", 14975084Sjohnlev ddi_driver_name(dip), ddi_get_instance(dip)); 14985084Sjohnlev 14995084Sjohnlev err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME, KM_NOSLEEP); 15005084Sjohnlev if (err != DDI_SUCCESS) 15015084Sjohnlev goto failure; 15025084Sjohnlev 15035084Sjohnlev err = nvlist_add_int32(attr_list, "domain", pdp->xd_domain); 15045084Sjohnlev if (err != DDI_SUCCESS) 15055084Sjohnlev goto failure; 15065084Sjohnlev err = nvlist_add_int32(attr_list, "vdev", pdp->xd_vdevnum); 15075084Sjohnlev if (err != DDI_SUCCESS) 15085084Sjohnlev goto failure; 15095084Sjohnlev err = nvlist_add_string(attr_list, "devclass", xdcp->xsdev); 15105084Sjohnlev if (err != DDI_SUCCESS) 15115084Sjohnlev goto failure; 15125084Sjohnlev err = nvlist_add_string(attr_list, "device", devname); 15135084Sjohnlev if (err != DDI_SUCCESS) 15145084Sjohnlev goto failure; 15155084Sjohnlev err = nvlist_add_string(attr_list, "fob", 15165084Sjohnlev ((pdp->xd_xsdev.frontend == 1) ? "frontend" : "backend")); 15175084Sjohnlev if (err != DDI_SUCCESS) 15185084Sjohnlev goto failure; 15195084Sjohnlev 15205084Sjohnlev switch (hpc) { 15215084Sjohnlev case XEN_HP_ADD: 15225084Sjohnlev err = ddi_log_sysevent(dip, DDI_VENDOR_SUNW, "EC_xendev", 15235084Sjohnlev "add", attr_list, &eid, DDI_NOSLEEP); 15245084Sjohnlev break; 15255084Sjohnlev case XEN_HP_REMOVE: 15265084Sjohnlev err = ddi_log_sysevent(dip, DDI_VENDOR_SUNW, "EC_xendev", 15275084Sjohnlev "remove", attr_list, &eid, DDI_NOSLEEP); 15285084Sjohnlev break; 15295084Sjohnlev default: 15305084Sjohnlev err = DDI_FAILURE; 15315084Sjohnlev goto failure; 15325084Sjohnlev } 15335084Sjohnlev 15345084Sjohnlev failure: 15355084Sjohnlev if (attr_list != NULL) 15365084Sjohnlev nvlist_free(attr_list); 15375084Sjohnlev 15385084Sjohnlev return (err); 15395084Sjohnlev } 15405084Sjohnlev 15415084Sjohnlev /* ARGSUSED */ 15425084Sjohnlev static void 15435084Sjohnlev i_xvdi_probe_path_cb(struct xenbus_watch *w, const char **vec, 15445084Sjohnlev unsigned int len) 15455084Sjohnlev { 15465084Sjohnlev char *path; 15475084Sjohnlev 15485084Sjohnlev if (xendev_dip == NULL) 15495084Sjohnlev xendev_dip = ddi_find_devinfo("xpvd", -1, 0); 15505084Sjohnlev 15515084Sjohnlev path = i_ddi_strdup((char *)vec[XS_WATCH_PATH], KM_SLEEP); 15525084Sjohnlev 15535084Sjohnlev (void) ddi_taskq_dispatch(DEVI(xendev_dip)->devi_taskq, 15545084Sjohnlev i_xvdi_probe_path_handler, (void *)path, DDI_SLEEP); 15555084Sjohnlev } 15565084Sjohnlev 15575084Sjohnlev static void 15585084Sjohnlev i_xvdi_watch_device(char *path) 15595084Sjohnlev { 15605084Sjohnlev struct xenbus_watch *w; 15615084Sjohnlev 15625084Sjohnlev ASSERT(path != NULL); 15635084Sjohnlev 15645084Sjohnlev w = kmem_zalloc(sizeof (*w), KM_SLEEP); 15655084Sjohnlev w->node = path; 15665084Sjohnlev w->callback = &i_xvdi_probe_path_cb; 15675084Sjohnlev w->dev = NULL; 15685084Sjohnlev 15695084Sjohnlev if (register_xenbus_watch(w) != 0) { 15705084Sjohnlev cmn_err(CE_WARN, "i_xvdi_watch_device: " 15715084Sjohnlev "cannot set watch on %s", path); 15725084Sjohnlev kmem_free(w, sizeof (*w)); 15735084Sjohnlev return; 15745084Sjohnlev } 15755084Sjohnlev } 15765084Sjohnlev 15775084Sjohnlev void 15785084Sjohnlev xvdi_watch_devices(int newstate) 15795084Sjohnlev { 15805084Sjohnlev int devclass; 15815084Sjohnlev 15825084Sjohnlev /* 15835084Sjohnlev * Watch for devices being created in the store. 15845084Sjohnlev */ 15855084Sjohnlev if (newstate == XENSTORE_DOWN) 15865084Sjohnlev return; 15875084Sjohnlev for (devclass = 0; devclass < NXDC; devclass++) { 15885084Sjohnlev if (xdci[devclass].xs_path_fe != NULL) 15895084Sjohnlev i_xvdi_watch_device(xdci[devclass].xs_path_fe); 15905084Sjohnlev if (xdci[devclass].xs_path_be != NULL) 15915084Sjohnlev i_xvdi_watch_device(xdci[devclass].xs_path_be); 15925084Sjohnlev } 15935084Sjohnlev } 15945084Sjohnlev 15955084Sjohnlev /* 15965084Sjohnlev * Iterate over the store looking for backend devices to create. 15975084Sjohnlev */ 15985084Sjohnlev static void 15995084Sjohnlev i_xvdi_enum_be(dev_info_t *parent, i_xd_cfg_t *xdcp) 16005084Sjohnlev { 16015084Sjohnlev char **domains; 16025084Sjohnlev unsigned int ndomains; 16035084Sjohnlev int ldomains, i; 16045084Sjohnlev 16055084Sjohnlev if ((domains = xenbus_directory(XBT_NULL, xdcp->xs_path_be, "", 16065084Sjohnlev &ndomains)) == NULL) 16075084Sjohnlev return; 16085084Sjohnlev 16095084Sjohnlev for (i = 0, ldomains = 0; i < ndomains; i++) { 16105084Sjohnlev ldomains += strlen(domains[i]) + 1 + sizeof (char *); 16115084Sjohnlev 16125084Sjohnlev i_xvdi_enum_worker(parent, xdcp, domains[i]); 16135084Sjohnlev } 16145084Sjohnlev kmem_free(domains, ldomains); 16155084Sjohnlev } 16165084Sjohnlev 16175084Sjohnlev /* 16185084Sjohnlev * Iterate over the store looking for frontend devices to create. 16195084Sjohnlev */ 16205084Sjohnlev static void 16215084Sjohnlev i_xvdi_enum_fe(dev_info_t *parent, i_xd_cfg_t *xdcp) 16225084Sjohnlev { 16235084Sjohnlev i_xvdi_enum_worker(parent, xdcp, NULL); 16245084Sjohnlev } 16255084Sjohnlev 16265084Sjohnlev static void 16275084Sjohnlev i_xvdi_enum_worker(dev_info_t *parent, i_xd_cfg_t *xdcp, 16285084Sjohnlev char *domain) 16295084Sjohnlev { 16305084Sjohnlev char *path, *domain_path, *ep; 16315084Sjohnlev char **devices; 16325084Sjohnlev unsigned int ndevices; 16335084Sjohnlev int ldevices, j, circ; 16345084Sjohnlev domid_t dom; 16355084Sjohnlev 16365084Sjohnlev if (domain == NULL) { 16375084Sjohnlev dom = DOMID_SELF; 16385084Sjohnlev path = xdcp->xs_path_fe; 16395084Sjohnlev domain_path = ""; 16405084Sjohnlev } else { 16415084Sjohnlev (void) ddi_strtol(domain, &ep, 0, (long *)&dom); 16425084Sjohnlev path = xdcp->xs_path_be; 16435084Sjohnlev domain_path = domain; 16445084Sjohnlev } 16455084Sjohnlev 16465084Sjohnlev if ((devices = xenbus_directory(XBT_NULL, path, domain_path, 16475084Sjohnlev &ndevices)) == NULL) 16485084Sjohnlev return; 16495084Sjohnlev 16505084Sjohnlev for (j = 0, ldevices = 0; j < ndevices; j++) { 16515084Sjohnlev int vdev; 16525084Sjohnlev 16535084Sjohnlev ldevices += strlen(devices[j]) + 1 + sizeof (char *); 16545084Sjohnlev (void) ddi_strtol(devices[j], &ep, 0, (long *)&vdev); 16555084Sjohnlev 16565084Sjohnlev ndi_devi_enter(parent, &circ); 16575084Sjohnlev 16585084Sjohnlev if (xvdi_find_dev(parent, xdcp->devclass, dom, vdev) 16595084Sjohnlev == NULL) 16605084Sjohnlev (void) xvdi_create_dev(parent, xdcp->devclass, 16615084Sjohnlev dom, vdev); 16625084Sjohnlev 16635084Sjohnlev ndi_devi_exit(parent, circ); 16645084Sjohnlev } 16655084Sjohnlev kmem_free(devices, ldevices); 16665084Sjohnlev } 16675084Sjohnlev 16685084Sjohnlev /* 16695084Sjohnlev * Leaf drivers should call this in their detach() routine during suspend. 16705084Sjohnlev */ 16715084Sjohnlev void 16725084Sjohnlev xvdi_suspend(dev_info_t *dip) 16735084Sjohnlev { 16745084Sjohnlev i_xvdi_rem_watches(dip); 16755084Sjohnlev } 16765084Sjohnlev 16775084Sjohnlev /* 16785084Sjohnlev * Leaf drivers should call this in their attach() routine during resume. 16795084Sjohnlev */ 16805084Sjohnlev int 16815084Sjohnlev xvdi_resume(dev_info_t *dip) 16825084Sjohnlev { 16835084Sjohnlev return (i_xvdi_add_watches(dip)); 16845084Sjohnlev } 16855084Sjohnlev 16865084Sjohnlev /* 16875084Sjohnlev * Add event handler for the leaf driver 16885084Sjohnlev * to handle event triggered by the change in xenstore 16895084Sjohnlev */ 16905084Sjohnlev int 16915084Sjohnlev xvdi_add_event_handler(dev_info_t *dip, char *name, 16925084Sjohnlev void (*evthandler)(dev_info_t *, ddi_eventcookie_t, void *, void *)) 16935084Sjohnlev { 16945084Sjohnlev ddi_eventcookie_t ecv; 16955084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 16965084Sjohnlev ddi_callback_id_t *cbid; 16975084Sjohnlev 16985084Sjohnlev ASSERT(pdp != NULL); 16995084Sjohnlev 17005084Sjohnlev mutex_enter(&pdp->xd_lk); 17015084Sjohnlev 17025084Sjohnlev if (strcmp(name, XS_OE_STATE) == 0) { 17035084Sjohnlev ASSERT(pdp->xd_xsdev.otherend != NULL); 17045084Sjohnlev 17055084Sjohnlev cbid = &pdp->xd_oe_ehid; 17065084Sjohnlev } else if (strcmp(name, XS_HP_STATE) == 0) { 17075084Sjohnlev if (pdp->xd_xsdev.frontend == 1) { 17085084Sjohnlev mutex_exit(&pdp->xd_lk); 17095084Sjohnlev return (DDI_FAILURE); 17105084Sjohnlev } 17115084Sjohnlev 17125084Sjohnlev ASSERT(pdp->xd_hp_watch.node != NULL); 17135084Sjohnlev 17145084Sjohnlev cbid = &pdp->xd_hp_ehid; 17155084Sjohnlev } else { 17165084Sjohnlev /* Unsupported watch. */ 17175084Sjohnlev mutex_exit(&pdp->xd_lk); 17185084Sjohnlev return (DDI_FAILURE); 17195084Sjohnlev } 17205084Sjohnlev 17215084Sjohnlev /* 17225084Sjohnlev * No event handler provided, take default action to handle 17235084Sjohnlev * event. 17245084Sjohnlev */ 17255084Sjohnlev if (evthandler == NULL) { 17265084Sjohnlev mutex_exit(&pdp->xd_lk); 17275084Sjohnlev return (DDI_SUCCESS); 17285084Sjohnlev } 17295084Sjohnlev 17305084Sjohnlev ASSERT(*cbid == NULL); 17315084Sjohnlev 17325084Sjohnlev if (ddi_get_eventcookie(dip, name, &ecv) != DDI_SUCCESS) { 17335084Sjohnlev cmn_err(CE_WARN, "failed to find %s cookie for %s@%s", 17345084Sjohnlev name, ddi_get_name(dip), ddi_get_name_addr(dip)); 17355084Sjohnlev mutex_exit(&pdp->xd_lk); 17365084Sjohnlev return (DDI_FAILURE); 17375084Sjohnlev } 17385084Sjohnlev if (ddi_add_event_handler(dip, ecv, evthandler, NULL, cbid) 17395084Sjohnlev != DDI_SUCCESS) { 17405084Sjohnlev cmn_err(CE_WARN, "failed to add %s event handler for %s@%s", 17415084Sjohnlev name, ddi_get_name(dip), ddi_get_name_addr(dip)); 17425084Sjohnlev *cbid = NULL; 17435084Sjohnlev mutex_exit(&pdp->xd_lk); 17445084Sjohnlev return (DDI_FAILURE); 17455084Sjohnlev } 17465084Sjohnlev 17475084Sjohnlev mutex_exit(&pdp->xd_lk); 17485084Sjohnlev 17495084Sjohnlev return (DDI_SUCCESS); 17505084Sjohnlev } 17515084Sjohnlev 17525084Sjohnlev /* 17535084Sjohnlev * Remove event handler for the leaf driver and unwatch xenstore 17545084Sjohnlev * so, driver will not be notified when xenstore entry changed later 17555084Sjohnlev */ 17565084Sjohnlev void 17575084Sjohnlev xvdi_remove_event_handler(dev_info_t *dip, char *name) 17585084Sjohnlev { 17595084Sjohnlev struct xendev_ppd *pdp; 17605084Sjohnlev boolean_t rem_oe = B_FALSE, rem_hp = B_FALSE; 17615084Sjohnlev ddi_callback_id_t oeid = NULL, hpid = NULL; 17625084Sjohnlev 17635084Sjohnlev pdp = ddi_get_parent_data(dip); 17645084Sjohnlev ASSERT(pdp != NULL); 17655084Sjohnlev 17665084Sjohnlev if (name == NULL) { 17675084Sjohnlev rem_oe = B_TRUE; 17685084Sjohnlev rem_hp = B_TRUE; 17695084Sjohnlev } else if (strcmp(name, XS_OE_STATE) == 0) { 17705084Sjohnlev rem_oe = B_TRUE; 17715084Sjohnlev } else if (strcmp(name, XS_HP_STATE) == 0) { 17725084Sjohnlev rem_hp = B_TRUE; 17735084Sjohnlev } else { 17745084Sjohnlev cmn_err(CE_WARN, "event %s not supported, cannot remove", name); 17755084Sjohnlev return; 17765084Sjohnlev } 17775084Sjohnlev 17785084Sjohnlev mutex_enter(&pdp->xd_lk); 17795084Sjohnlev 17805084Sjohnlev if (rem_oe && (pdp->xd_oe_ehid != NULL)) { 17815084Sjohnlev oeid = pdp->xd_oe_ehid; 17825084Sjohnlev pdp->xd_oe_ehid = NULL; 17835084Sjohnlev } 17845084Sjohnlev 17855084Sjohnlev if (rem_hp && (pdp->xd_hp_ehid != NULL)) { 17865084Sjohnlev hpid = pdp->xd_hp_ehid; 17875084Sjohnlev pdp->xd_hp_ehid = NULL; 17885084Sjohnlev } 17895084Sjohnlev 17905084Sjohnlev mutex_exit(&pdp->xd_lk); 17915084Sjohnlev 17925084Sjohnlev if (oeid != NULL) 17935084Sjohnlev (void) ddi_remove_event_handler(oeid); 17945084Sjohnlev if (hpid != NULL) 17955084Sjohnlev (void) ddi_remove_event_handler(hpid); 17965084Sjohnlev } 17975084Sjohnlev 17985084Sjohnlev 17995084Sjohnlev /* 18005084Sjohnlev * common ring interfaces 18015084Sjohnlev */ 18025084Sjohnlev 18035084Sjohnlev #define FRONT_RING(_ringp) (&(_ringp)->xr_sring.fr) 18045084Sjohnlev #define BACK_RING(_ringp) (&(_ringp)->xr_sring.br) 18055084Sjohnlev #define GET_RING_SIZE(_ringp) RING_SIZE(FRONT_RING(ringp)) 18065084Sjohnlev #define GET_RING_ENTRY_FE(_ringp, _idx) \ 18075084Sjohnlev (FRONT_RING(_ringp)->sring->ring + \ 18085084Sjohnlev (_ringp)->xr_entry_size * ((_idx) & (GET_RING_SIZE(_ringp) - 1))) 18095084Sjohnlev #define GET_RING_ENTRY_BE(_ringp, _idx) \ 18105084Sjohnlev (BACK_RING(_ringp)->sring->ring + \ 18115084Sjohnlev (_ringp)->xr_entry_size * ((_idx) & (GET_RING_SIZE(_ringp) - 1))) 18125084Sjohnlev 18135084Sjohnlev unsigned int 18145084Sjohnlev xvdi_ring_avail_slots(xendev_ring_t *ringp) 18155084Sjohnlev { 18165084Sjohnlev comif_ring_fe_t *frp; 18175084Sjohnlev comif_ring_be_t *brp; 18185084Sjohnlev 18195084Sjohnlev if (ringp->xr_frontend) { 18205084Sjohnlev frp = FRONT_RING(ringp); 18215084Sjohnlev return (GET_RING_SIZE(ringp) - 18225084Sjohnlev (frp->req_prod_pvt - frp->rsp_cons)); 18235084Sjohnlev } else { 18245084Sjohnlev brp = BACK_RING(ringp); 18255084Sjohnlev return (GET_RING_SIZE(ringp) - 18265084Sjohnlev (brp->rsp_prod_pvt - brp->req_cons)); 18275084Sjohnlev } 18285084Sjohnlev } 18295084Sjohnlev 18305084Sjohnlev int 18315084Sjohnlev xvdi_ring_has_unconsumed_requests(xendev_ring_t *ringp) 18325084Sjohnlev { 18335084Sjohnlev comif_ring_be_t *brp; 18345084Sjohnlev 18355084Sjohnlev ASSERT(!ringp->xr_frontend); 18365084Sjohnlev brp = BACK_RING(ringp); 18375084Sjohnlev return ((brp->req_cons != 18385084Sjohnlev ddi_get32(ringp->xr_acc_hdl, &brp->sring->req_prod)) && 18395084Sjohnlev ((brp->req_cons - brp->rsp_prod_pvt) != RING_SIZE(brp))); 18405084Sjohnlev } 18415084Sjohnlev 18425084Sjohnlev int 18435084Sjohnlev xvdi_ring_has_incomp_request(xendev_ring_t *ringp) 18445084Sjohnlev { 18455084Sjohnlev comif_ring_fe_t *frp; 18465084Sjohnlev 18475084Sjohnlev ASSERT(ringp->xr_frontend); 18485084Sjohnlev frp = FRONT_RING(ringp); 18495084Sjohnlev return (frp->req_prod_pvt != 18505084Sjohnlev ddi_get32(ringp->xr_acc_hdl, &frp->sring->rsp_prod)); 18515084Sjohnlev } 18525084Sjohnlev 18535084Sjohnlev int 18545084Sjohnlev xvdi_ring_has_unconsumed_responses(xendev_ring_t *ringp) 18555084Sjohnlev { 18565084Sjohnlev comif_ring_fe_t *frp; 18575084Sjohnlev 18585084Sjohnlev ASSERT(ringp->xr_frontend); 18595084Sjohnlev frp = FRONT_RING(ringp); 18605084Sjohnlev return (frp->rsp_cons != 18615084Sjohnlev ddi_get32(ringp->xr_acc_hdl, &frp->sring->rsp_prod)); 18625084Sjohnlev } 18635084Sjohnlev 18645084Sjohnlev /* NOTE: req_event will be increased as needed */ 18655084Sjohnlev void * 18665084Sjohnlev xvdi_ring_get_request(xendev_ring_t *ringp) 18675084Sjohnlev { 18685084Sjohnlev comif_ring_fe_t *frp; 18695084Sjohnlev comif_ring_be_t *brp; 18705084Sjohnlev 18715084Sjohnlev if (ringp->xr_frontend) { 18725084Sjohnlev /* for frontend ring */ 18735084Sjohnlev frp = FRONT_RING(ringp); 18745084Sjohnlev if (!RING_FULL(frp)) 18755084Sjohnlev return (GET_RING_ENTRY_FE(ringp, frp->req_prod_pvt++)); 18765084Sjohnlev else 18775084Sjohnlev return (NULL); 18785084Sjohnlev } else { 18795084Sjohnlev /* for backend ring */ 18805084Sjohnlev brp = BACK_RING(ringp); 18815084Sjohnlev /* RING_FINAL_CHECK_FOR_REQUESTS() */ 18825084Sjohnlev if (xvdi_ring_has_unconsumed_requests(ringp)) 18835084Sjohnlev return (GET_RING_ENTRY_BE(ringp, brp->req_cons++)); 18845084Sjohnlev else { 18855084Sjohnlev ddi_put32(ringp->xr_acc_hdl, &brp->sring->req_event, 18865084Sjohnlev brp->req_cons + 1); 18875084Sjohnlev membar_enter(); 18885084Sjohnlev if (xvdi_ring_has_unconsumed_requests(ringp)) 18895084Sjohnlev return (GET_RING_ENTRY_BE(ringp, 18905084Sjohnlev brp->req_cons++)); 18915084Sjohnlev else 18925084Sjohnlev return (NULL); 18935084Sjohnlev } 18945084Sjohnlev } 18955084Sjohnlev } 18965084Sjohnlev 18975084Sjohnlev int 18985084Sjohnlev xvdi_ring_push_request(xendev_ring_t *ringp) 18995084Sjohnlev { 19005084Sjohnlev RING_IDX old, new, reqevt; 19015084Sjohnlev comif_ring_fe_t *frp; 19025084Sjohnlev 19035084Sjohnlev /* only frontend should be able to push request */ 19045084Sjohnlev ASSERT(ringp->xr_frontend); 19055084Sjohnlev 19065084Sjohnlev /* RING_PUSH_REQUEST_AND_CHECK_NOTIFY() */ 19075084Sjohnlev frp = FRONT_RING(ringp); 19085084Sjohnlev old = ddi_get32(ringp->xr_acc_hdl, &frp->sring->req_prod); 19095084Sjohnlev new = frp->req_prod_pvt; 19105084Sjohnlev ddi_put32(ringp->xr_acc_hdl, &frp->sring->req_prod, new); 19115084Sjohnlev membar_enter(); 19125084Sjohnlev reqevt = ddi_get32(ringp->xr_acc_hdl, &frp->sring->req_event); 19135084Sjohnlev return ((RING_IDX)(new - reqevt) < (RING_IDX)(new - old)); 19145084Sjohnlev } 19155084Sjohnlev 19165084Sjohnlev /* NOTE: rsp_event will be increased as needed */ 19175084Sjohnlev void * 19185084Sjohnlev xvdi_ring_get_response(xendev_ring_t *ringp) 19195084Sjohnlev { 19205084Sjohnlev comif_ring_fe_t *frp; 19215084Sjohnlev comif_ring_be_t *brp; 19225084Sjohnlev 19235084Sjohnlev if (!ringp->xr_frontend) { 19245084Sjohnlev /* for backend ring */ 19255084Sjohnlev brp = BACK_RING(ringp); 19265084Sjohnlev return (GET_RING_ENTRY_BE(ringp, brp->rsp_prod_pvt++)); 19275084Sjohnlev } else { 19285084Sjohnlev /* for frontend ring */ 19295084Sjohnlev frp = FRONT_RING(ringp); 19305084Sjohnlev /* RING_FINAL_CHECK_FOR_RESPONSES() */ 19315084Sjohnlev if (xvdi_ring_has_unconsumed_responses(ringp)) 19325084Sjohnlev return (GET_RING_ENTRY_FE(ringp, frp->rsp_cons++)); 19335084Sjohnlev else { 19345084Sjohnlev ddi_put32(ringp->xr_acc_hdl, &frp->sring->rsp_event, 19355084Sjohnlev frp->rsp_cons + 1); 19365084Sjohnlev membar_enter(); 19375084Sjohnlev if (xvdi_ring_has_unconsumed_responses(ringp)) 19385084Sjohnlev return (GET_RING_ENTRY_FE(ringp, 19395084Sjohnlev frp->rsp_cons++)); 19405084Sjohnlev else 19415084Sjohnlev return (NULL); 19425084Sjohnlev } 19435084Sjohnlev } 19445084Sjohnlev } 19455084Sjohnlev 19465084Sjohnlev int 19475084Sjohnlev xvdi_ring_push_response(xendev_ring_t *ringp) 19485084Sjohnlev { 19495084Sjohnlev RING_IDX old, new, rspevt; 19505084Sjohnlev comif_ring_be_t *brp; 19515084Sjohnlev 19525084Sjohnlev /* only backend should be able to push response */ 19535084Sjohnlev ASSERT(!ringp->xr_frontend); 19545084Sjohnlev 19555084Sjohnlev /* RING_PUSH_RESPONSE_AND_CHECK_NOTIFY() */ 19565084Sjohnlev brp = BACK_RING(ringp); 19575084Sjohnlev old = ddi_get32(ringp->xr_acc_hdl, &brp->sring->rsp_prod); 19585084Sjohnlev new = brp->rsp_prod_pvt; 19595084Sjohnlev ddi_put32(ringp->xr_acc_hdl, &brp->sring->rsp_prod, new); 19605084Sjohnlev membar_enter(); 19615084Sjohnlev rspevt = ddi_get32(ringp->xr_acc_hdl, &brp->sring->rsp_event); 19625084Sjohnlev return ((RING_IDX)(new - rspevt) < (RING_IDX)(new - old)); 19635084Sjohnlev } 19645084Sjohnlev 19655084Sjohnlev static void 19665084Sjohnlev xvdi_ring_init_sring(xendev_ring_t *ringp) 19675084Sjohnlev { 19685084Sjohnlev ddi_acc_handle_t acchdl; 19695084Sjohnlev comif_sring_t *xsrp; 19705084Sjohnlev int i; 19715084Sjohnlev 19725084Sjohnlev xsrp = (comif_sring_t *)ringp->xr_vaddr; 19735084Sjohnlev acchdl = ringp->xr_acc_hdl; 19745084Sjohnlev 19755084Sjohnlev /* shared ring initialization */ 19765084Sjohnlev ddi_put32(acchdl, &xsrp->req_prod, 0); 19775084Sjohnlev ddi_put32(acchdl, &xsrp->rsp_prod, 0); 19785084Sjohnlev ddi_put32(acchdl, &xsrp->req_event, 1); 19795084Sjohnlev ddi_put32(acchdl, &xsrp->rsp_event, 1); 19805084Sjohnlev for (i = 0; i < sizeof (xsrp->pad); i++) 19815084Sjohnlev ddi_put8(acchdl, xsrp->pad + i, 0); 19825084Sjohnlev } 19835084Sjohnlev 19845084Sjohnlev static void 19855084Sjohnlev xvdi_ring_init_front_ring(xendev_ring_t *ringp, size_t nentry, size_t entrysize) 19865084Sjohnlev { 19875084Sjohnlev comif_ring_fe_t *xfrp; 19885084Sjohnlev 19895084Sjohnlev xfrp = &ringp->xr_sring.fr; 19905084Sjohnlev xfrp->req_prod_pvt = 0; 19915084Sjohnlev xfrp->rsp_cons = 0; 19925084Sjohnlev xfrp->nr_ents = nentry; 19935084Sjohnlev xfrp->sring = (comif_sring_t *)ringp->xr_vaddr; 19945084Sjohnlev 19955084Sjohnlev ringp->xr_frontend = 1; 19965084Sjohnlev ringp->xr_entry_size = entrysize; 19975084Sjohnlev } 19985084Sjohnlev 19995741Smrj #ifndef XPV_HVM_DRIVER 20005084Sjohnlev static void 20015084Sjohnlev xvdi_ring_init_back_ring(xendev_ring_t *ringp, size_t nentry, size_t entrysize) 20025084Sjohnlev { 20035084Sjohnlev comif_ring_be_t *xbrp; 20045084Sjohnlev 20055084Sjohnlev xbrp = &ringp->xr_sring.br; 20065084Sjohnlev xbrp->rsp_prod_pvt = 0; 20075084Sjohnlev xbrp->req_cons = 0; 20085084Sjohnlev xbrp->nr_ents = nentry; 20095084Sjohnlev xbrp->sring = (comif_sring_t *)ringp->xr_vaddr; 20105084Sjohnlev 20115084Sjohnlev ringp->xr_frontend = 0; 20125084Sjohnlev ringp->xr_entry_size = entrysize; 20135084Sjohnlev } 20145741Smrj #endif /* XPV_HVM_DRIVER */ 20155084Sjohnlev 20165084Sjohnlev static void 20175084Sjohnlev xendev_offline_device(void *arg) 20185084Sjohnlev { 20195084Sjohnlev dev_info_t *dip = (dev_info_t *)arg; 20205084Sjohnlev char devname[MAXNAMELEN] = {0}; 20215084Sjohnlev 20225084Sjohnlev /* 20235084Sjohnlev * This is currently the only chance to delete a devinfo node, which 20245084Sjohnlev * is _not_ always successful. 20255084Sjohnlev */ 20265084Sjohnlev (void) ddi_deviname(dip, devname); 20275084Sjohnlev (void) devfs_clean(ddi_get_parent(dip), devname + 1, DV_CLEAN_FORCE); 20285084Sjohnlev (void) ndi_devi_offline(dip, NDI_DEVI_REMOVE); 20295084Sjohnlev } 20305084Sjohnlev 20315084Sjohnlev static void 20325084Sjohnlev i_xvdi_oestate_cb(struct xenbus_device *dev, XenbusState oestate) 20335084Sjohnlev { 20345084Sjohnlev dev_info_t *dip = (dev_info_t *)dev->data; 20355084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 20365084Sjohnlev 20375084Sjohnlev /* 20385084Sjohnlev * Don't trigger two consecutive ndi_devi_offline on the same 20395084Sjohnlev * dip. 20405084Sjohnlev */ 20415084Sjohnlev if ((oestate == XenbusStateClosed) && 20425084Sjohnlev (dev->otherend_state == XenbusStateClosed)) 20435084Sjohnlev return; 20445084Sjohnlev 20455084Sjohnlev dev->otherend_state = oestate; 20465084Sjohnlev (void) ddi_taskq_dispatch(pdp->xd_oe_taskq, 20475084Sjohnlev i_xvdi_oestate_handler, (void *)dip, DDI_SLEEP); 20485084Sjohnlev } 20495084Sjohnlev 20505084Sjohnlev /*ARGSUSED*/ 20515084Sjohnlev static void 20525084Sjohnlev i_xvdi_hpstate_cb(struct xenbus_watch *w, const char **vec, 20535084Sjohnlev unsigned int len) 20545084Sjohnlev { 20555084Sjohnlev dev_info_t *dip = (dev_info_t *)w->dev; 20565084Sjohnlev struct xendev_ppd *pdp = ddi_get_parent_data(dip); 20575084Sjohnlev 20585084Sjohnlev (void) ddi_taskq_dispatch(pdp->xd_hp_taskq, 20595084Sjohnlev i_xvdi_hpstate_handler, (void *)dip, DDI_SLEEP); 20605084Sjohnlev } 20615084Sjohnlev 20625084Sjohnlev static void 20635084Sjohnlev i_xvdi_probe_path_handler(void *arg) 20645084Sjohnlev { 20655084Sjohnlev dev_info_t *parent; 20665084Sjohnlev char *path = arg, *p = NULL; 20675084Sjohnlev int i, vdev, circ; 20685084Sjohnlev i_xd_cfg_t *xdcp; 20695084Sjohnlev boolean_t frontend; 20705084Sjohnlev domid_t dom; 20715084Sjohnlev 20725084Sjohnlev for (i = 0, xdcp = &xdci[0]; i < NXDC; i++, xdcp++) { 20735084Sjohnlev 20745084Sjohnlev if ((xdcp->xs_path_fe != NULL) && 20755084Sjohnlev (strncmp(path, xdcp->xs_path_fe, strlen(xdcp->xs_path_fe)) 20765084Sjohnlev == 0)) { 20775084Sjohnlev 20785084Sjohnlev frontend = B_TRUE; 20795084Sjohnlev p = path + strlen(xdcp->xs_path_fe); 20805084Sjohnlev break; 20815084Sjohnlev } 20825084Sjohnlev 20835084Sjohnlev if ((xdcp->xs_path_be != NULL) && 20845084Sjohnlev (strncmp(path, xdcp->xs_path_be, strlen(xdcp->xs_path_be)) 20855084Sjohnlev == 0)) { 20865084Sjohnlev 20875084Sjohnlev frontend = B_FALSE; 20885084Sjohnlev p = path + strlen(xdcp->xs_path_be); 20895084Sjohnlev break; 20905084Sjohnlev } 20915084Sjohnlev 20925084Sjohnlev } 20935084Sjohnlev 20945084Sjohnlev if (p == NULL) { 20955084Sjohnlev cmn_err(CE_WARN, "i_xvdi_probe_path_handler: " 20965084Sjohnlev "unexpected path prefix in %s", path); 20975084Sjohnlev goto done; 20985084Sjohnlev } 20995084Sjohnlev 21005084Sjohnlev if (frontend) { 21015084Sjohnlev dom = DOMID_SELF; 21025084Sjohnlev if (sscanf(p, "/%d/", &vdev) != 1) { 21035084Sjohnlev XVDI_DPRINTF(XVDI_DBG_PROBE, 21045084Sjohnlev "i_xvdi_probe_path_handler: " 21055084Sjohnlev "cannot parse frontend path %s", 21065084Sjohnlev path); 21075084Sjohnlev goto done; 21085084Sjohnlev } 21095084Sjohnlev } else { 21105084Sjohnlev if (sscanf(p, "/%d/%d/", &dom, &vdev) != 2) { 21115084Sjohnlev XVDI_DPRINTF(XVDI_DBG_PROBE, 21125084Sjohnlev "i_xvdi_probe_path_handler: " 21135084Sjohnlev "cannot parse backend path %s", 21145084Sjohnlev path); 21155084Sjohnlev goto done; 21165084Sjohnlev } 21175084Sjohnlev } 21185084Sjohnlev 2119*6175Sjohnlev /* 2120*6175Sjohnlev * This is an oxymoron, so indicates a bogus configuration we 2121*6175Sjohnlev * must check for. 2122*6175Sjohnlev */ 2123*6175Sjohnlev if (vdev == VDEV_NOXS) { 2124*6175Sjohnlev cmn_err(CE_WARN, "i_xvdi_probe_path_handler: " 2125*6175Sjohnlev "invalid path %s", path); 2126*6175Sjohnlev goto done; 2127*6175Sjohnlev } 2128*6175Sjohnlev 21295084Sjohnlev parent = xendev_dip; 21305084Sjohnlev ASSERT(parent != NULL); 21315084Sjohnlev 21325084Sjohnlev ndi_devi_enter(parent, &circ); 21335084Sjohnlev 21345084Sjohnlev if (xvdi_find_dev(parent, xdcp->devclass, dom, vdev) == NULL) { 21355084Sjohnlev XVDI_DPRINTF(XVDI_DBG_PROBE, 21365084Sjohnlev "i_xvdi_probe_path_handler: create for %s", path); 21375084Sjohnlev (void) xvdi_create_dev(parent, xdcp->devclass, dom, vdev); 21385084Sjohnlev } else { 21395084Sjohnlev XVDI_DPRINTF(XVDI_DBG_PROBE, 21405084Sjohnlev "i_xvdi_probe_path_handler: %s already exists", path); 21415084Sjohnlev } 21425084Sjohnlev 21435084Sjohnlev ndi_devi_exit(parent, circ); 21445084Sjohnlev 21455084Sjohnlev done: 21465084Sjohnlev kmem_free(path, strlen(path) + 1); 21475084Sjohnlev } 2148