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