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