1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. 29 * Copyright 2017 Nexenta Systems, Inc. 30 */ 31 32 /* 33 * Host to hypervisor virtual devices nexus driver 34 * 35 * TODO: 36 * - Add watchpoints on vbd/vif and enumerate/offline on watch callback 37 * - Add DR IOCTLs 38 * - Filter/restrict property lookups into xenstore 39 */ 40 41 #include <sys/conf.h> 42 #include <sys/kmem.h> 43 #include <sys/debug.h> 44 #include <sys/modctl.h> 45 #include <sys/autoconf.h> 46 #include <sys/ddi_impldefs.h> 47 #include <sys/ddi_subrdefs.h> 48 #include <sys/ddi.h> 49 #include <sys/sunddi.h> 50 #include <sys/sunndi.h> 51 #include <sys/avintr.h> 52 #include <sys/psm.h> 53 #include <sys/spl.h> 54 #include <sys/promif.h> 55 #include <sys/list.h> 56 #include <sys/bootconf.h> 57 #include <sys/bootsvcs.h> 58 #include <util/sscanf.h> 59 #include <sys/mach_intr.h> 60 #include <sys/bootinfo.h> 61 #ifdef XPV_HVM_DRIVER 62 #include <sys/xpv_support.h> 63 #include <sys/hypervisor.h> 64 #include <sys/archsystm.h> 65 #include <sys/cpu.h> 66 #include <public/xen.h> 67 #include <public/event_channel.h> 68 #include <public/io/xenbus.h> 69 #else 70 #include <sys/hypervisor.h> 71 #include <sys/evtchn_impl.h> 72 #include <sys/xen_mmu.h> 73 #endif 74 #include <xen/sys/xenbus_impl.h> 75 #include <xen/sys/xendev.h> 76 77 /* 78 * DDI dev_ops entrypoints 79 */ 80 static int xpvd_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 81 static int xpvd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd); 82 static int xpvd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd); 83 84 85 /* 86 * NDI bus_ops entrypoints 87 */ 88 static int xpvd_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, 89 void *); 90 static int xpvd_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t, 91 ddi_intr_handle_impl_t *, void *); 92 static int xpvd_prop_op(dev_t, dev_info_t *, dev_info_t *, ddi_prop_op_t, 93 int, char *, caddr_t, int *); 94 static int xpvd_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t, 95 void *, dev_info_t **); 96 static int xpvd_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t, 97 void *); 98 static int xpvd_get_eventcookie(dev_info_t *, dev_info_t *, 99 char *, ddi_eventcookie_t *); 100 static int xpvd_add_eventcall(dev_info_t *, dev_info_t *, 101 ddi_eventcookie_t, void (*)(dev_info_t *, 102 ddi_eventcookie_t, void *, void *), 103 void *, ddi_callback_id_t *); 104 static int xpvd_remove_eventcall(dev_info_t *, ddi_callback_id_t); 105 static int xpvd_post_event(dev_info_t *, dev_info_t *, 106 ddi_eventcookie_t, void *); 107 108 /* 109 * misc functions 110 */ 111 static int xpvd_enable_intr(dev_info_t *, ddi_intr_handle_impl_t *, int); 112 static void xpvd_disable_intr(dev_info_t *, ddi_intr_handle_impl_t *, int); 113 static int xpvd_removechild(dev_info_t *); 114 static int xpvd_initchild(dev_info_t *); 115 static int xpvd_name_child(dev_info_t *, char *, int); 116 static boolean_t i_xpvd_parse_devname(char *, xendev_devclass_t *, 117 domid_t *, int *); 118 119 120 /* Extern declarations */ 121 extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, 122 psm_intr_op_t, int *); 123 124 struct bus_ops xpvd_bus_ops = { 125 BUSO_REV, 126 i_ddi_bus_map, 127 NULL, 128 NULL, 129 NULL, 130 i_ddi_map_fault, 131 NULL, 132 ddi_dma_allochdl, 133 ddi_dma_freehdl, 134 ddi_dma_bindhdl, 135 ddi_dma_unbindhdl, 136 ddi_dma_flush, 137 ddi_dma_win, 138 ddi_dma_mctl, 139 xpvd_ctlops, 140 xpvd_prop_op, 141 xpvd_get_eventcookie, 142 xpvd_add_eventcall, 143 xpvd_remove_eventcall, 144 xpvd_post_event, 145 0, /* (*bus_intr_ctl)(); */ 146 xpvd_bus_config, 147 xpvd_bus_unconfig, 148 NULL, /* (*bus_fm_init)(); */ 149 NULL, /* (*bus_fm_fini)(); */ 150 NULL, /* (*bus_fm_access_enter)(); */ 151 NULL, /* (*bus_fm_access_exit)(); */ 152 NULL, /* (*bus_power)(); */ 153 xpvd_intr_ops /* (*bus_intr_op)(); */ 154 }; 155 156 struct dev_ops xpvd_ops = { 157 DEVO_REV, /* devo_rev */ 158 0, /* refcnt */ 159 xpvd_info, /* info */ 160 nulldev, /* identify */ 161 nulldev, /* probe */ 162 xpvd_attach, /* attach */ 163 xpvd_detach, /* detach */ 164 nulldev, /* reset */ 165 (struct cb_ops *)0, /* driver operations */ 166 &xpvd_bus_ops, /* bus operations */ 167 NULL, /* power */ 168 ddi_quiesce_not_needed, /* quiesce */ 169 }; 170 171 172 dev_info_t *xpvd_dip; 173 174 #define CF_DBG 0x1 175 #define ALL_DBG 0xff 176 177 static ndi_event_definition_t xpvd_ndi_event_defs[] = { 178 { 0, XS_OE_STATE, EPL_KERNEL, NDI_EVENT_POST_TO_TGT }, 179 { 1, XS_HP_STATE, EPL_KERNEL, NDI_EVENT_POST_TO_TGT }, 180 }; 181 182 #define XENDEV_N_NDI_EVENTS \ 183 (sizeof (xpvd_ndi_event_defs) / sizeof (xpvd_ndi_event_defs[0])) 184 185 static ndi_event_set_t xpvd_ndi_events = { 186 NDI_EVENTS_REV1, XENDEV_N_NDI_EVENTS, xpvd_ndi_event_defs 187 }; 188 189 static ndi_event_hdl_t xpvd_ndi_event_handle; 190 191 /* 192 * Hypervisor interrupt capabilities 193 */ 194 #define XENDEV_INTR_CAPABILITIES \ 195 (DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_MASKABLE | DDI_INTR_FLAG_PENDING) 196 197 /* 198 * Module linkage information for the kernel. 199 */ 200 201 static struct modldrv modldrv = { 202 &mod_driverops, /* Type of module */ 203 "virtual device nexus driver", 204 &xpvd_ops, /* driver ops */ 205 }; 206 207 static struct modlinkage modlinkage = { 208 MODREV_1, 209 (void *)&modldrv, 210 NULL 211 }; 212 213 int 214 _init(void) 215 { 216 return (mod_install(&modlinkage)); 217 } 218 219 int 220 _fini(void) 221 { 222 return (mod_remove(&modlinkage)); 223 } 224 225 int 226 _info(struct modinfo *modinfop) 227 { 228 return (mod_info(&modlinkage, modinfop)); 229 } 230 231 /* ARGSUSED */ 232 static int 233 xpvd_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 234 { 235 switch (cmd) { 236 default: 237 return (DDI_FAILURE); 238 239 case DDI_INFO_DEVT2INSTANCE: 240 *result = (void *)0; 241 return (DDI_SUCCESS); 242 243 case DDI_INFO_DEVT2DEVINFO: 244 *result = (void *)xpvd_dip; 245 return (DDI_SUCCESS); 246 } 247 } 248 249 /*ARGSUSED*/ 250 static int 251 xpvd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 252 { 253 extern void xvdi_watch_devices(int); 254 #ifdef XPV_HVM_DRIVER 255 extern dev_info_t *xpv_dip; 256 257 if (xpv_dip == NULL) { 258 if (ddi_hold_installed_driver(ddi_name_to_major("xpv")) == 259 NULL) { 260 cmn_err(CE_WARN, "Couldn't initialize xpv framework"); 261 return (DDI_FAILURE); 262 } 263 } 264 #endif /* XPV_HVM_DRIVER */ 265 266 if (ndi_event_alloc_hdl(devi, 0, &xpvd_ndi_event_handle, 267 NDI_SLEEP) != NDI_SUCCESS) { 268 xpvd_dip = NULL; 269 return (DDI_FAILURE); 270 } 271 if (ndi_event_bind_set(xpvd_ndi_event_handle, &xpvd_ndi_events, 272 NDI_SLEEP) != NDI_SUCCESS) { 273 (void) ndi_event_free_hdl(xpvd_ndi_event_handle); 274 xpvd_dip = NULL; 275 return (DDI_FAILURE); 276 } 277 if (ddi_create_minor_node(devi, "devctl", S_IFCHR, 278 ddi_get_instance(devi), DDI_PSEUDO, 0) != DDI_SUCCESS) { 279 (void) ndi_event_unbind_set(xpvd_ndi_event_handle, 280 &xpvd_ndi_events, NDI_SLEEP); 281 (void) ndi_event_free_hdl(xpvd_ndi_event_handle); 282 xpvd_dip = NULL; 283 return (DDI_FAILURE); 284 } 285 286 #ifdef XPV_HVM_DRIVER 287 (void) ddi_prop_update_int(DDI_DEV_T_NONE, devi, DDI_NO_AUTODETACH, 1); 288 289 /* 290 * Report our version to dom0. 291 */ 292 if (xenbus_printf(XBT_NULL, "guest/xpvd", "version", "%d", 293 HVMPV_XPVD_VERS)) 294 cmn_err(CE_WARN, "xpvd: couldn't write version\n"); 295 #endif /* XPV_HVM_DRIVER */ 296 297 /* watch both frontend and backend for new devices */ 298 if (DOMAIN_IS_INITDOMAIN(xen_info)) 299 (void) xs_register_xenbus_callback(xvdi_watch_devices); 300 else 301 xvdi_watch_devices(XENSTORE_UP); 302 303 xpvd_dip = devi; 304 ddi_report_dev(devi); 305 306 return (DDI_SUCCESS); 307 } 308 309 /*ARGSUSED*/ 310 static int 311 xpvd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 312 { 313 return (DDI_FAILURE); 314 } 315 316 /* 317 * xpvd_prop_op() 318 * 319 * Query xenstore for the value of properties if DDI_PROP_NOTPROM 320 * is not set. Xenstore property values are represented as ascii strings. 321 */ 322 static int 323 xpvd_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip, 324 ddi_prop_op_t prop_op, int mod_flags, char *name, caddr_t valuep, 325 int *lengthp) 326 { 327 caddr_t buff; 328 struct xendev_ppd *pdp; 329 void *prop_str; 330 size_t prop_len; 331 unsigned int len; 332 int rv; 333 334 pdp = (struct xendev_ppd *)ddi_get_parent_data(ch_dip); 335 336 if ((pdp == NULL) || !(mod_flags & (DDI_PROP_CANSLEEP)) || 337 (mod_flags & DDI_PROP_NOTPROM) || (pdp->xd_xsdev.nodename == NULL)) 338 goto toss_off; 339 /* 340 * First try reading the property off the the frontend. if that 341 * fails, try and read it from the backend node. If that 342 * also fails, pass the request on the DDI framework 343 */ 344 prop_str = NULL; 345 if ((xenbus_read(XBT_NULL, pdp->xd_xsdev.nodename, name, &prop_str, 346 &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0)) 347 goto got_xs_prop; 348 349 prop_str = NULL; 350 if ((pdp->xd_xsdev.otherend != NULL) && 351 (xenbus_read(XBT_NULL, pdp->xd_xsdev.otherend, name, &prop_str, 352 &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0)) 353 goto got_xs_prop; 354 355 toss_off: 356 return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op, 357 mod_flags | DDI_PROP_NOTPROM, name, valuep, lengthp)); 358 359 got_xs_prop: 360 prop_len = strlen(prop_str) + 1; 361 rv = DDI_PROP_SUCCESS; 362 363 switch (prop_op) { 364 case PROP_LEN: 365 *lengthp = prop_len; 366 break; 367 368 case PROP_LEN_AND_VAL_ALLOC: 369 buff = kmem_alloc((size_t)prop_len, KM_SLEEP); 370 *(caddr_t *)valuep = (caddr_t)buff; 371 break; 372 case PROP_LEN_AND_VAL_BUF: 373 buff = (caddr_t)valuep; 374 if (*lengthp < prop_len) 375 rv = DDI_PROP_BUF_TOO_SMALL; 376 break; 377 default: 378 rv = DDI_PROP_INVAL_ARG; 379 break; 380 } 381 382 if ((rv == DDI_PROP_SUCCESS) && (prop_len > 0)) { 383 bcopy(prop_str, buff, prop_len); 384 *lengthp = prop_len; 385 } 386 kmem_free(prop_str, len); 387 return (rv); 388 } 389 390 391 /* 392 * return address of the device's interrupt spec structure. 393 */ 394 /*ARGSUSED*/ 395 struct intrspec * 396 xpvd_get_ispec(dev_info_t *rdip, uint_t inumber) 397 { 398 struct xendev_ppd *pdp; 399 400 ASSERT(inumber == 0); 401 402 if ((pdp = ddi_get_parent_data(rdip)) == NULL) 403 return (NULL); 404 405 return (&pdp->xd_ispec); 406 } 407 408 /* 409 * return (and determine) the interrupt priority of the device. 410 */ 411 /*ARGSUSED*/ 412 static int 413 xpvd_get_priority(dev_info_t *dip, int inum, int *pri) 414 { 415 struct xendev_ppd *pdp; 416 struct intrspec *ispec; 417 int *intpriorities; 418 uint_t num_intpriorities; 419 420 DDI_INTR_NEXDBG((CE_CONT, "xpvd_get_priority: dip = 0x%p\n", 421 (void *)dip)); 422 423 ASSERT(inum == 0); 424 425 if ((pdp = ddi_get_parent_data(dip)) == NULL) 426 return (DDI_FAILURE); 427 428 ispec = &pdp->xd_ispec; 429 430 /* 431 * Set the default priority based on the device class. The 432 * "interrupt-priorities" property can be used to override 433 * the default. 434 */ 435 if (ispec->intrspec_pri == 0) { 436 ispec->intrspec_pri = xendev_devclass_ipl(pdp->xd_devclass); 437 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 438 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 439 "interrupt-priorities", &intpriorities, 440 &num_intpriorities) == DDI_PROP_SUCCESS) { 441 ispec->intrspec_pri = intpriorities[0]; 442 ddi_prop_free(intpriorities); 443 } 444 } 445 *pri = ispec->intrspec_pri; 446 return (DDI_SUCCESS); 447 } 448 449 450 /* 451 * xpvd_intr_ops: bus_intr_op() function for interrupt support 452 */ 453 /* ARGSUSED */ 454 static int 455 xpvd_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op, 456 ddi_intr_handle_impl_t *hdlp, void *result) 457 { 458 int priority = 0; 459 struct intrspec *ispec; 460 struct xendev_ppd *pdp; 461 462 DDI_INTR_NEXDBG((CE_CONT, 463 "xpvd_intr_ops: pdip 0x%p, rdip 0x%p, op %x handle 0x%p\n", 464 (void *)pdip, (void *)rdip, intr_op, (void *)hdlp)); 465 466 /* Process the request */ 467 switch (intr_op) { 468 case DDI_INTROP_SUPPORTED_TYPES: 469 /* Fixed supported by default */ 470 *(int *)result = DDI_INTR_TYPE_FIXED; 471 break; 472 473 case DDI_INTROP_NINTRS: 474 *(int *)result = 1; 475 break; 476 477 case DDI_INTROP_ALLOC: 478 /* 479 * FIXED interrupts: just return available interrupts 480 */ 481 if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) { 482 /* 483 * event channels are edge-triggered, maskable, 484 * and support int pending. 485 */ 486 hdlp->ih_cap |= XENDEV_INTR_CAPABILITIES; 487 *(int *)result = 1; /* DDI_INTR_TYPE_FIXED */ 488 } else { 489 return (DDI_FAILURE); 490 } 491 break; 492 493 case DDI_INTROP_FREE: 494 ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum); 495 if (ispec == NULL) 496 return (DDI_FAILURE); 497 ispec->intrspec_pri = 0; /* mark as un-initialized */ 498 break; 499 500 case DDI_INTROP_GETPRI: 501 if (xpvd_get_priority(rdip, hdlp->ih_inum, &priority) != 502 DDI_SUCCESS) 503 return (DDI_FAILURE); 504 DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: priority = 0x%x\n", 505 priority)); 506 *(int *)result = priority; 507 break; 508 509 case DDI_INTROP_SETPRI: 510 /* Validate the interrupt priority passed */ 511 if (*(int *)result > LOCK_LEVEL) 512 return (DDI_FAILURE); 513 514 /* Ensure that PSM is all initialized */ 515 if (psm_intr_ops == NULL) 516 return (DDI_FAILURE); 517 518 /* Change the priority */ 519 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) == 520 PSM_FAILURE) 521 return (DDI_FAILURE); 522 523 ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum); 524 if (ispec == NULL) 525 return (DDI_FAILURE); 526 ispec->intrspec_pri = *(int *)result; 527 break; 528 529 case DDI_INTROP_ADDISR: 530 /* update ispec */ 531 ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum); 532 if (ispec == NULL) 533 return (DDI_FAILURE); 534 ispec->intrspec_func = hdlp->ih_cb_func; 535 536 break; 537 538 case DDI_INTROP_REMISR: 539 ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum); 540 pdp = (struct xendev_ppd *)ddi_get_parent_data(rdip); 541 542 ASSERT(pdp != NULL); 543 ASSERT(pdp->xd_evtchn != INVALID_EVTCHN); 544 545 if (ispec) { 546 ispec->intrspec_vec = 0; 547 ispec->intrspec_func = (uint_t (*)()) 0; 548 } 549 pdp->xd_evtchn = INVALID_EVTCHN; 550 break; 551 552 case DDI_INTROP_GETCAP: 553 if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) { 554 /* 555 * event channels are edge-triggered, maskable, 556 * and support int pending. 557 */ 558 *(int *)result = XENDEV_INTR_CAPABILITIES; 559 } else { 560 *(int *)result = 0; 561 return (DDI_FAILURE); 562 } 563 DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETCAP returned = %x\n", 564 *(int *)result)); 565 break; 566 case DDI_INTROP_SETCAP: 567 DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: SETCAP cap=0x%x\n", 568 *(int *)result)); 569 if (psm_intr_ops == NULL) 570 return (DDI_FAILURE); 571 572 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result)) { 573 DDI_INTR_NEXDBG((CE_CONT, "GETCAP: psm_intr_ops" 574 " returned failure\n")); 575 return (DDI_FAILURE); 576 } 577 break; 578 579 case DDI_INTROP_ENABLE: 580 if (psm_intr_ops == NULL) 581 return (DDI_FAILURE); 582 583 if (xpvd_enable_intr(rdip, hdlp, (int)hdlp->ih_inum) != 584 DDI_SUCCESS) 585 return (DDI_FAILURE); 586 587 DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: ENABLE vec=0x%x\n", 588 hdlp->ih_vector)); 589 break; 590 591 case DDI_INTROP_DISABLE: 592 if (psm_intr_ops == NULL) 593 return (DDI_FAILURE); 594 xpvd_disable_intr(rdip, hdlp, hdlp->ih_inum); 595 DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: DISABLE vec = %x\n", 596 hdlp->ih_vector)); 597 break; 598 599 case DDI_INTROP_BLOCKENABLE: 600 case DDI_INTROP_BLOCKDISABLE: 601 return (DDI_FAILURE); 602 603 case DDI_INTROP_SETMASK: 604 case DDI_INTROP_CLRMASK: 605 #ifdef XPV_HVM_DRIVER 606 return (DDI_ENOTSUP); 607 #else 608 /* 609 * Handle this here 610 */ 611 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 612 return (DDI_FAILURE); 613 if (intr_op == DDI_INTROP_SETMASK) { 614 ec_disable_irq(hdlp->ih_vector); 615 } else { 616 ec_enable_irq(hdlp->ih_vector); 617 } 618 break; 619 #endif 620 case DDI_INTROP_GETPENDING: 621 #ifdef XPV_HVM_DRIVER 622 return (DDI_ENOTSUP); 623 #else 624 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 625 return (DDI_FAILURE); 626 *(int *)result = ec_pending_irq(hdlp->ih_vector); 627 DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETPENDING returned = %x\n", 628 *(int *)result)); 629 break; 630 #endif 631 632 case DDI_INTROP_NAVAIL: 633 *(int *)result = 1; 634 DDI_INTR_NEXDBG((CE_CONT, "xpvd: NAVAIL returned = %x\n", 635 *(int *)result)); 636 break; 637 638 default: 639 return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result)); 640 } 641 642 return (DDI_SUCCESS); 643 } 644 645 646 static int 647 xpvd_enable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum) 648 { 649 int vector; 650 ihdl_plat_t *ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private; 651 652 DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: hdlp %p inum %x\n", 653 (void *)hdlp, inum)); 654 655 ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum); 656 if (ihdl_plat_datap->ip_ispecp == NULL) 657 return (DDI_FAILURE); 658 659 /* translate the interrupt if needed */ 660 (void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector); 661 DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: priority=%x vector=%x\n", 662 hdlp->ih_pri, vector)); 663 664 /* Add the interrupt handler */ 665 if (!add_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func, 666 DEVI(rdip)->devi_name, vector, hdlp->ih_cb_arg1, 667 hdlp->ih_cb_arg2, NULL, rdip)) 668 return (DDI_FAILURE); 669 670 /* Note this really is an irq. */ 671 hdlp->ih_vector = (ushort_t)vector; 672 673 return (DDI_SUCCESS); 674 } 675 676 677 static void 678 xpvd_disable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum) 679 { 680 int vector; 681 ihdl_plat_t *ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private; 682 683 DDI_INTR_NEXDBG((CE_CONT, "xpvd_disable_intr: \n")); 684 ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum); 685 if (ihdl_plat_datap->ip_ispecp == NULL) 686 return; 687 688 /* translate the interrupt if needed */ 689 (void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector); 690 691 /* Disable the interrupt handler */ 692 rem_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func, vector); 693 ihdl_plat_datap->ip_ispecp = NULL; 694 } 695 696 /*ARGSUSED*/ 697 static int 698 xpvd_ctlops(dev_info_t *dip, dev_info_t *rdip, 699 ddi_ctl_enum_t ctlop, void *arg, void *result) 700 { 701 switch (ctlop) { 702 case DDI_CTLOPS_REPORTDEV: 703 if (rdip == (dev_info_t *)0) 704 return (DDI_FAILURE); 705 cmn_err(CE_CONT, "?%s@%s, %s%d\n", ddi_node_name(rdip), 706 ddi_get_name_addr(rdip), ddi_driver_name(rdip), 707 ddi_get_instance(rdip)); 708 return (DDI_SUCCESS); 709 710 case DDI_CTLOPS_INITCHILD: 711 return (xpvd_initchild((dev_info_t *)arg)); 712 713 case DDI_CTLOPS_UNINITCHILD: 714 return (xpvd_removechild((dev_info_t *)arg)); 715 716 case DDI_CTLOPS_SIDDEV: 717 return (DDI_SUCCESS); 718 719 case DDI_CTLOPS_REGSIZE: 720 case DDI_CTLOPS_NREGS: 721 return (DDI_FAILURE); 722 723 case DDI_CTLOPS_POWER: { 724 return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 725 } 726 727 default: 728 return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 729 } 730 731 /* NOTREACHED */ 732 733 } 734 735 /* 736 * Assign the address portion of the node name 737 */ 738 static int 739 xpvd_name_child(dev_info_t *child, char *addr, int addrlen) 740 { 741 int *domain, *vdev; 742 uint_t ndomain, nvdev; 743 char *prop_str; 744 745 /* 746 * i_xpvd_parse_devname() knows the formats used by this 747 * routine. If this code changes, so must that. 748 */ 749 750 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 751 "domain", &domain, &ndomain) != DDI_PROP_SUCCESS) 752 return (DDI_FAILURE); 753 ASSERT(ndomain == 1); 754 755 /* 756 * Use "domain" and "vdev" properties (backend drivers). 757 */ 758 if (*domain != DOMID_SELF) { 759 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, 760 DDI_PROP_DONTPASS, "vdev", &vdev, &nvdev) 761 != DDI_PROP_SUCCESS) { 762 ddi_prop_free(domain); 763 return (DDI_FAILURE); 764 } 765 ASSERT(nvdev == 1); 766 767 (void) snprintf(addr, addrlen, "%d,%d", domain[0], vdev[0]); 768 ddi_prop_free(vdev); 769 ddi_prop_free(domain); 770 return (DDI_SUCCESS); 771 } 772 ddi_prop_free(domain); 773 774 /* 775 * Use "unit-address" property (frontend/softdev drivers). 776 */ 777 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 778 "unit-address", &prop_str) != DDI_PROP_SUCCESS) 779 return (DDI_FAILURE); 780 (void) strlcpy(addr, prop_str, addrlen); 781 ddi_prop_free(prop_str); 782 return (DDI_SUCCESS); 783 } 784 785 static int 786 xpvd_initchild(dev_info_t *child) 787 { 788 char addr[80]; 789 790 /* 791 * Pseudo nodes indicate a prototype node with per-instance 792 * properties to be merged into the real h/w device node. 793 */ 794 if (ndi_dev_is_persistent_node(child) == 0) { 795 ddi_set_parent_data(child, NULL); 796 797 /* 798 * Try to merge the properties from this prototype 799 * node into real h/w nodes. 800 */ 801 if (ndi_merge_node(child, xpvd_name_child) == DDI_SUCCESS) { 802 /* 803 * Merged ok - return failure to remove the node. 804 */ 805 ddi_set_name_addr(child, NULL); 806 return (DDI_FAILURE); 807 } 808 809 /* 810 * The child was not merged into a h/w node, 811 * but there's not much we can do with it other 812 * than return failure to cause the node to be removed. 813 */ 814 cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged", 815 ddi_get_name(child), ddi_get_name_addr(child), 816 ddi_get_name(child)); 817 ddi_set_name_addr(child, NULL); 818 return (DDI_NOT_WELL_FORMED); 819 } 820 821 if (xvdi_init_dev(child) != DDI_SUCCESS) 822 return (DDI_FAILURE); 823 824 if (xpvd_name_child(child, addr, sizeof (addr)) != DDI_SUCCESS) { 825 xvdi_uninit_dev(child); 826 return (DDI_FAILURE); 827 } 828 ddi_set_name_addr(child, addr); 829 830 return (DDI_SUCCESS); 831 } 832 833 static int 834 xpvd_removechild(dev_info_t *dip) 835 { 836 xvdi_uninit_dev(dip); 837 838 ddi_set_name_addr(dip, NULL); 839 840 /* 841 * Strip the node to properly convert it back to prototype 842 * form. 843 */ 844 ddi_remove_minor_node(dip, NULL); 845 846 return (DDI_SUCCESS); 847 } 848 849 static int 850 xpvd_bus_unconfig(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op, 851 void *device_name) 852 { 853 return (ndi_busop_bus_unconfig(parent, flag, op, device_name)); 854 } 855 856 /* 857 * Given the name of a child of xpvd, determine the device class, 858 * domain and vdevnum to which it refers. 859 */ 860 static boolean_t 861 i_xpvd_parse_devname(char *name, xendev_devclass_t *devclassp, 862 domid_t *domp, int *vdevp) 863 { 864 int len = strlen(name) + 1; 865 char *device_name = i_ddi_strdup(name, KM_SLEEP); 866 char *cname = NULL, *caddr = NULL; 867 boolean_t ret; 868 869 i_ddi_parse_name(device_name, &cname, &caddr, NULL); 870 871 if ((cname == NULL) || (strlen(cname) == 0) || 872 (caddr == NULL) || (strlen(caddr) == 0)) { 873 ret = B_FALSE; 874 goto done; 875 } 876 877 *devclassp = xendev_nodename_to_devclass(cname); 878 if (*devclassp < 0) { 879 ret = B_FALSE; 880 goto done; 881 } 882 883 /* 884 * Parsing the address component requires knowledge of how 885 * xpvd_name_child() works. If that code changes, so must 886 * this. 887 */ 888 889 /* Backend format is "<domain>,<vdev>". */ 890 if (sscanf(caddr, "%hu,%d", domp, vdevp) == 2) { 891 ret = B_TRUE; 892 goto done; 893 } 894 895 /* Frontend format is "<vdev>". */ 896 *domp = DOMID_SELF; 897 if (sscanf(caddr, "%d", vdevp) == 1) 898 ret = B_TRUE; 899 done: 900 kmem_free(device_name, len); 901 return (ret); 902 } 903 904 /* 905 * xpvd_bus_config() 906 * 907 * BUS_CONFIG_ONE: 908 * Enumerate the exact instance of a driver. 909 * 910 * BUS_CONFIG_ALL: 911 * Enumerate all the instances of all the possible children (seen before 912 * and never seen before). 913 * 914 * BUS_CONFIG_DRIVER: 915 * Enumerate all the instances of a particular driver. 916 */ 917 static int 918 xpvd_bus_config(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op, 919 void *arg, dev_info_t **childp) 920 { 921 int circ; 922 char *cname = NULL; 923 924 ndi_devi_enter(parent, &circ); 925 926 switch (op) { 927 case BUS_CONFIG_ONE: { 928 xendev_devclass_t devclass; 929 domid_t dom; 930 int vdev; 931 932 if (!i_xpvd_parse_devname(arg, &devclass, &dom, &vdev)) { 933 ndi_devi_exit(parent, circ); 934 return (NDI_FAILURE); 935 } 936 937 *childp = xvdi_find_dev(parent, devclass, dom, vdev); 938 if (*childp == NULL) 939 *childp = xvdi_create_dev(parent, devclass, dom, vdev); 940 941 ndi_devi_exit(parent, circ); 942 943 if (*childp == NULL) 944 return (NDI_FAILURE); 945 else 946 return (ndi_busop_bus_config(parent, flag, 947 op, arg, childp, 0)); 948 } 949 950 case BUS_CONFIG_DRIVER: { 951 xendev_devclass_t devclass = XEN_INVAL; 952 953 cname = ddi_major_to_name((major_t)(uintptr_t)arg); 954 if (cname != NULL) 955 devclass = xendev_nodename_to_devclass(cname); 956 957 if (devclass == XEN_INVAL) { 958 ndi_devi_exit(parent, circ); 959 return (NDI_FAILURE); 960 } else { 961 xendev_enum_class(parent, devclass); 962 ndi_devi_exit(parent, circ); 963 return (ndi_busop_bus_config(parent, flag, op, 964 arg, childp, 0)); 965 } 966 /* NOTREACHED */ 967 } 968 969 case BUS_CONFIG_ALL: 970 xendev_enum_all(parent, B_FALSE); 971 ndi_devi_exit(parent, circ); 972 973 return (ndi_busop_bus_config(parent, flag, op, 974 arg, childp, 0)); 975 976 default: 977 ndi_devi_exit(parent, circ); 978 return (NDI_FAILURE); 979 } 980 } 981 982 /*ARGSUSED*/ 983 static int 984 xpvd_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, 985 char *eventname, ddi_eventcookie_t *cookie) 986 { 987 return (ndi_event_retrieve_cookie(xpvd_ndi_event_handle, 988 rdip, eventname, cookie, NDI_EVENT_NOPASS)); 989 } 990 991 /*ARGSUSED*/ 992 static int 993 xpvd_add_eventcall(dev_info_t *dip, dev_info_t *rdip, 994 ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip, 995 ddi_eventcookie_t cookie, void *arg, void *bus_impldata), 996 void *arg, ddi_callback_id_t *cb_id) 997 { 998 return (ndi_event_add_callback(xpvd_ndi_event_handle, 999 rdip, cookie, callback, arg, NDI_SLEEP, cb_id)); 1000 } 1001 1002 /*ARGSUSED*/ 1003 static int 1004 xpvd_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id) 1005 { 1006 return (ndi_event_remove_callback(xpvd_ndi_event_handle, 1007 cb_id)); 1008 } 1009 1010 /*ARGSUSED*/ 1011 static int 1012 xpvd_post_event(dev_info_t *dip, dev_info_t *rdip, 1013 ddi_eventcookie_t cookie, void *bus_impldata) 1014 { 1015 return (ndi_event_run_callbacks(xpvd_ndi_event_handle, rdip, 1016 cookie, bus_impldata)); 1017 } 1018