1 /* 2 * PCI Stub Driver - Grabs devices in backend to be exported later 3 * 4 * Ryan Wilson <hap9@epoch.ncsc.mil> 5 * Chris Bookholt <hap10@epoch.ncsc.mil> 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 #define dev_fmt pr_fmt 10 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/rwsem.h> 14 #include <linux/list.h> 15 #include <linux/spinlock.h> 16 #include <linux/kref.h> 17 #include <linux/pci.h> 18 #include <linux/wait.h> 19 #include <linux/sched.h> 20 #include <linux/atomic.h> 21 #include <xen/events.h> 22 #include <asm/xen/pci.h> 23 #include <asm/xen/hypervisor.h> 24 #include <xen/interface/physdev.h> 25 #include "pciback.h" 26 #include "conf_space.h" 27 #include "conf_space_quirks.h" 28 29 #define PCISTUB_DRIVER_NAME "pciback" 30 31 static char *pci_devs_to_hide; 32 wait_queue_head_t xen_pcibk_aer_wait_queue; 33 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops, 34 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed 35 */ 36 static DECLARE_RWSEM(pcistub_sem); 37 module_param_named(hide, pci_devs_to_hide, charp, 0444); 38 39 struct pcistub_device_id { 40 struct list_head slot_list; 41 int domain; 42 unsigned char bus; 43 unsigned int devfn; 44 }; 45 static LIST_HEAD(pcistub_device_ids); 46 static DEFINE_SPINLOCK(device_ids_lock); 47 48 struct pcistub_device { 49 struct kref kref; 50 struct list_head dev_list; 51 spinlock_t lock; 52 53 struct pci_dev *dev; 54 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */ 55 }; 56 57 /* Access to pcistub_devices & seized_devices lists and the initialize_devices 58 * flag must be locked with pcistub_devices_lock 59 */ 60 static DEFINE_SPINLOCK(pcistub_devices_lock); 61 static LIST_HEAD(pcistub_devices); 62 63 /* wait for device_initcall before initializing our devices 64 * (see pcistub_init_devices_late) 65 */ 66 static int initialize_devices; 67 static LIST_HEAD(seized_devices); 68 69 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev) 70 { 71 struct pcistub_device *psdev; 72 73 dev_dbg(&dev->dev, "pcistub_device_alloc\n"); 74 75 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL); 76 if (!psdev) 77 return NULL; 78 79 psdev->dev = pci_dev_get(dev); 80 if (!psdev->dev) { 81 kfree(psdev); 82 return NULL; 83 } 84 85 kref_init(&psdev->kref); 86 spin_lock_init(&psdev->lock); 87 88 return psdev; 89 } 90 91 /* Don't call this directly as it's called by pcistub_device_put */ 92 static void pcistub_device_release(struct kref *kref) 93 { 94 struct pcistub_device *psdev; 95 struct pci_dev *dev; 96 struct xen_pcibk_dev_data *dev_data; 97 98 psdev = container_of(kref, struct pcistub_device, kref); 99 dev = psdev->dev; 100 dev_data = pci_get_drvdata(dev); 101 102 dev_dbg(&dev->dev, "pcistub_device_release\n"); 103 104 xen_unregister_device_domain_owner(dev); 105 106 /* Call the reset function which does not take lock as this 107 * is called from "unbind" which takes a device_lock mutex. 108 */ 109 __pci_reset_function_locked(dev); 110 if (dev_data && 111 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) 112 dev_info(&dev->dev, "Could not reload PCI state\n"); 113 else 114 pci_restore_state(dev); 115 116 if (dev->msix_cap) { 117 struct physdev_pci_device ppdev = { 118 .seg = pci_domain_nr(dev->bus), 119 .bus = dev->bus->number, 120 .devfn = dev->devfn 121 }; 122 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix, 123 &ppdev); 124 125 if (err && err != -ENOSYS) 126 dev_warn(&dev->dev, "MSI-X release failed (%d)\n", 127 err); 128 } 129 130 /* Disable the device */ 131 xen_pcibk_reset_device(dev); 132 133 kfree(dev_data); 134 pci_set_drvdata(dev, NULL); 135 136 /* Clean-up the device */ 137 xen_pcibk_config_free_dyn_fields(dev); 138 xen_pcibk_config_free_dev(dev); 139 140 pci_clear_dev_assigned(dev); 141 pci_dev_put(dev); 142 143 kfree(psdev); 144 } 145 146 static inline void pcistub_device_get(struct pcistub_device *psdev) 147 { 148 kref_get(&psdev->kref); 149 } 150 151 static inline void pcistub_device_put(struct pcistub_device *psdev) 152 { 153 kref_put(&psdev->kref, pcistub_device_release); 154 } 155 156 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus, 157 int slot, int func) 158 { 159 struct pcistub_device *psdev; 160 161 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 162 if (psdev->dev != NULL 163 && domain == pci_domain_nr(psdev->dev->bus) 164 && bus == psdev->dev->bus->number 165 && slot == PCI_SLOT(psdev->dev->devfn) 166 && func == PCI_FUNC(psdev->dev->devfn)) { 167 return psdev; 168 } 169 } 170 171 return NULL; 172 } 173 174 static struct pcistub_device *pcistub_device_find(int domain, int bus, 175 int slot, int func) 176 { 177 struct pcistub_device *psdev; 178 unsigned long flags; 179 180 spin_lock_irqsave(&pcistub_devices_lock, flags); 181 182 psdev = pcistub_device_find_locked(domain, bus, slot, func); 183 if (psdev) 184 pcistub_device_get(psdev); 185 186 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 187 return psdev; 188 } 189 190 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev, 191 struct pcistub_device *psdev) 192 { 193 struct pci_dev *pci_dev = NULL; 194 unsigned long flags; 195 196 pcistub_device_get(psdev); 197 198 spin_lock_irqsave(&psdev->lock, flags); 199 if (!psdev->pdev) { 200 psdev->pdev = pdev; 201 pci_dev = psdev->dev; 202 } 203 spin_unlock_irqrestore(&psdev->lock, flags); 204 205 if (!pci_dev) 206 pcistub_device_put(psdev); 207 208 return pci_dev; 209 } 210 211 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev, 212 int domain, int bus, 213 int slot, int func) 214 { 215 struct pcistub_device *psdev; 216 struct pci_dev *found_dev = NULL; 217 unsigned long flags; 218 219 spin_lock_irqsave(&pcistub_devices_lock, flags); 220 221 psdev = pcistub_device_find_locked(domain, bus, slot, func); 222 if (psdev) 223 found_dev = pcistub_device_get_pci_dev(pdev, psdev); 224 225 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 226 return found_dev; 227 } 228 229 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev, 230 struct pci_dev *dev) 231 { 232 struct pcistub_device *psdev; 233 struct pci_dev *found_dev = NULL; 234 unsigned long flags; 235 236 spin_lock_irqsave(&pcistub_devices_lock, flags); 237 238 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 239 if (psdev->dev == dev) { 240 found_dev = pcistub_device_get_pci_dev(pdev, psdev); 241 break; 242 } 243 } 244 245 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 246 return found_dev; 247 } 248 249 /* 250 * Called when: 251 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device 252 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove 253 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove 254 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove 255 * 256 * As such we have to be careful. 257 * 258 * To make this easier, the caller has to hold the device lock. 259 */ 260 void pcistub_put_pci_dev(struct pci_dev *dev) 261 { 262 struct pcistub_device *psdev, *found_psdev = NULL; 263 unsigned long flags; 264 struct xen_pcibk_dev_data *dev_data; 265 int ret; 266 267 spin_lock_irqsave(&pcistub_devices_lock, flags); 268 269 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 270 if (psdev->dev == dev) { 271 found_psdev = psdev; 272 break; 273 } 274 } 275 276 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 277 if (WARN_ON(!found_psdev)) 278 return; 279 280 /*hold this lock for avoiding breaking link between 281 * pcistub and xen_pcibk when AER is in processing 282 */ 283 down_write(&pcistub_sem); 284 /* Cleanup our device 285 * (so it's ready for the next domain) 286 */ 287 device_lock_assert(&dev->dev); 288 __pci_reset_function_locked(dev); 289 290 dev_data = pci_get_drvdata(dev); 291 ret = pci_load_saved_state(dev, dev_data->pci_saved_state); 292 if (!ret) { 293 /* 294 * The usual sequence is pci_save_state & pci_restore_state 295 * but the guest might have messed the configuration space up. 296 * Use the initial version (when device was bound to us). 297 */ 298 pci_restore_state(dev); 299 } else 300 dev_info(&dev->dev, "Could not reload PCI state\n"); 301 /* This disables the device. */ 302 xen_pcibk_reset_device(dev); 303 304 /* And cleanup up our emulated fields. */ 305 xen_pcibk_config_reset_dev(dev); 306 xen_pcibk_config_free_dyn_fields(dev); 307 308 dev_data->allow_interrupt_control = 0; 309 310 xen_unregister_device_domain_owner(dev); 311 312 spin_lock_irqsave(&found_psdev->lock, flags); 313 found_psdev->pdev = NULL; 314 spin_unlock_irqrestore(&found_psdev->lock, flags); 315 316 pcistub_device_put(found_psdev); 317 up_write(&pcistub_sem); 318 } 319 320 static int pcistub_match_one(struct pci_dev *dev, 321 struct pcistub_device_id *pdev_id) 322 { 323 /* Match the specified device by domain, bus, slot, func and also if 324 * any of the device's parent bridges match. 325 */ 326 for (; dev != NULL; dev = dev->bus->self) { 327 if (pci_domain_nr(dev->bus) == pdev_id->domain 328 && dev->bus->number == pdev_id->bus 329 && dev->devfn == pdev_id->devfn) 330 return 1; 331 332 /* Sometimes topmost bridge links to itself. */ 333 if (dev == dev->bus->self) 334 break; 335 } 336 337 return 0; 338 } 339 340 static int pcistub_match(struct pci_dev *dev) 341 { 342 struct pcistub_device_id *pdev_id; 343 unsigned long flags; 344 int found = 0; 345 346 spin_lock_irqsave(&device_ids_lock, flags); 347 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) { 348 if (pcistub_match_one(dev, pdev_id)) { 349 found = 1; 350 break; 351 } 352 } 353 spin_unlock_irqrestore(&device_ids_lock, flags); 354 355 return found; 356 } 357 358 static int pcistub_init_device(struct pci_dev *dev) 359 { 360 struct xen_pcibk_dev_data *dev_data; 361 int err = 0; 362 363 dev_dbg(&dev->dev, "initializing...\n"); 364 365 /* The PCI backend is not intended to be a module (or to work with 366 * removable PCI devices (yet). If it were, xen_pcibk_config_free() 367 * would need to be called somewhere to free the memory allocated 368 * here and then to call kfree(pci_get_drvdata(psdev->dev)). 369 */ 370 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]") 371 + strlen(pci_name(dev)) + 1, GFP_KERNEL); 372 if (!dev_data) { 373 err = -ENOMEM; 374 goto out; 375 } 376 pci_set_drvdata(dev, dev_data); 377 378 /* 379 * Setup name for fake IRQ handler. It will only be enabled 380 * once the device is turned on by the guest. 381 */ 382 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev)); 383 384 dev_dbg(&dev->dev, "initializing config\n"); 385 386 init_waitqueue_head(&xen_pcibk_aer_wait_queue); 387 err = xen_pcibk_config_init_dev(dev); 388 if (err) 389 goto out; 390 391 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we 392 * must do this here because pcibios_enable_device may specify 393 * the pci device's true irq (and possibly its other resources) 394 * if they differ from what's in the configuration space. 395 * This makes the assumption that the device's resources won't 396 * change after this point (otherwise this code may break!) 397 */ 398 dev_dbg(&dev->dev, "enabling device\n"); 399 err = pci_enable_device(dev); 400 if (err) 401 goto config_release; 402 403 if (dev->msix_cap) { 404 struct physdev_pci_device ppdev = { 405 .seg = pci_domain_nr(dev->bus), 406 .bus = dev->bus->number, 407 .devfn = dev->devfn 408 }; 409 410 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev); 411 if (err && err != -ENOSYS) 412 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n", 413 err); 414 } 415 416 /* We need the device active to save the state. */ 417 dev_dbg(&dev->dev, "save state of device\n"); 418 pci_save_state(dev); 419 dev_data->pci_saved_state = pci_store_saved_state(dev); 420 if (!dev_data->pci_saved_state) 421 dev_err(&dev->dev, "Could not store PCI conf saved state!\n"); 422 else { 423 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n"); 424 __pci_reset_function_locked(dev); 425 pci_restore_state(dev); 426 } 427 /* Now disable the device (this also ensures some private device 428 * data is setup before we export) 429 */ 430 dev_dbg(&dev->dev, "reset device\n"); 431 xen_pcibk_reset_device(dev); 432 433 pci_set_dev_assigned(dev); 434 return 0; 435 436 config_release: 437 xen_pcibk_config_free_dev(dev); 438 439 out: 440 pci_set_drvdata(dev, NULL); 441 kfree(dev_data); 442 return err; 443 } 444 445 /* 446 * Because some initialization still happens on 447 * devices during fs_initcall, we need to defer 448 * full initialization of our devices until 449 * device_initcall. 450 */ 451 static int __init pcistub_init_devices_late(void) 452 { 453 struct pcistub_device *psdev; 454 unsigned long flags; 455 int err = 0; 456 457 spin_lock_irqsave(&pcistub_devices_lock, flags); 458 459 while (!list_empty(&seized_devices)) { 460 psdev = container_of(seized_devices.next, 461 struct pcistub_device, dev_list); 462 list_del(&psdev->dev_list); 463 464 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 465 466 err = pcistub_init_device(psdev->dev); 467 if (err) { 468 dev_err(&psdev->dev->dev, 469 "error %d initializing device\n", err); 470 kfree(psdev); 471 psdev = NULL; 472 } 473 474 spin_lock_irqsave(&pcistub_devices_lock, flags); 475 476 if (psdev) 477 list_add_tail(&psdev->dev_list, &pcistub_devices); 478 } 479 480 initialize_devices = 1; 481 482 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 483 484 return 0; 485 } 486 487 static void pcistub_device_id_add_list(struct pcistub_device_id *new, 488 int domain, int bus, unsigned int devfn) 489 { 490 struct pcistub_device_id *pci_dev_id; 491 unsigned long flags; 492 int found = 0; 493 494 spin_lock_irqsave(&device_ids_lock, flags); 495 496 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { 497 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus && 498 pci_dev_id->devfn == devfn) { 499 found = 1; 500 break; 501 } 502 } 503 504 if (!found) { 505 new->domain = domain; 506 new->bus = bus; 507 new->devfn = devfn; 508 list_add_tail(&new->slot_list, &pcistub_device_ids); 509 } 510 511 spin_unlock_irqrestore(&device_ids_lock, flags); 512 513 if (found) 514 kfree(new); 515 } 516 517 static int pcistub_seize(struct pci_dev *dev, 518 struct pcistub_device_id *pci_dev_id) 519 { 520 struct pcistub_device *psdev; 521 unsigned long flags; 522 int err = 0; 523 524 psdev = pcistub_device_alloc(dev); 525 if (!psdev) { 526 kfree(pci_dev_id); 527 return -ENOMEM; 528 } 529 530 spin_lock_irqsave(&pcistub_devices_lock, flags); 531 532 if (initialize_devices) { 533 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 534 535 /* don't want irqs disabled when calling pcistub_init_device */ 536 err = pcistub_init_device(psdev->dev); 537 538 spin_lock_irqsave(&pcistub_devices_lock, flags); 539 540 if (!err) 541 list_add(&psdev->dev_list, &pcistub_devices); 542 } else { 543 dev_dbg(&dev->dev, "deferring initialization\n"); 544 list_add(&psdev->dev_list, &seized_devices); 545 } 546 547 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 548 549 if (err) { 550 kfree(pci_dev_id); 551 pcistub_device_put(psdev); 552 } else if (pci_dev_id) 553 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus), 554 dev->bus->number, dev->devfn); 555 556 return err; 557 } 558 559 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or 560 * other functions that take the sysfs lock. */ 561 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id) 562 { 563 int err = 0, match; 564 struct pcistub_device_id *pci_dev_id = NULL; 565 566 dev_dbg(&dev->dev, "probing...\n"); 567 568 match = pcistub_match(dev); 569 570 if ((dev->driver_override && 571 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) || 572 match) { 573 574 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL 575 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { 576 dev_err(&dev->dev, "can't export pci devices that " 577 "don't have a normal (0) or bridge (1) " 578 "header type!\n"); 579 err = -ENODEV; 580 goto out; 581 } 582 583 if (!match) { 584 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL); 585 if (!pci_dev_id) { 586 err = -ENOMEM; 587 goto out; 588 } 589 } 590 591 dev_info(&dev->dev, "seizing device\n"); 592 err = pcistub_seize(dev, pci_dev_id); 593 } else 594 /* Didn't find the device */ 595 err = -ENODEV; 596 597 out: 598 return err; 599 } 600 601 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or 602 * other functions that take the sysfs lock. */ 603 static void pcistub_remove(struct pci_dev *dev) 604 { 605 struct pcistub_device *psdev, *found_psdev = NULL; 606 unsigned long flags; 607 608 dev_dbg(&dev->dev, "removing\n"); 609 610 spin_lock_irqsave(&pcistub_devices_lock, flags); 611 612 xen_pcibk_config_quirk_release(dev); 613 614 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 615 if (psdev->dev == dev) { 616 found_psdev = psdev; 617 break; 618 } 619 } 620 621 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 622 623 if (found_psdev) { 624 dev_dbg(&dev->dev, "found device to remove %s\n", 625 found_psdev->pdev ? "- in-use" : ""); 626 627 if (found_psdev->pdev) { 628 int domid = xen_find_device_domain_owner(dev); 629 630 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n", 631 pci_name(found_psdev->dev), domid); 632 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n"); 633 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n"); 634 dev_warn(&dev->dev, "****** to other drivers or domains\n"); 635 636 /* N.B. This ends up calling pcistub_put_pci_dev which ends up 637 * doing the FLR. */ 638 xen_pcibk_release_pci_dev(found_psdev->pdev, 639 found_psdev->dev, 640 false /* caller holds the lock. */); 641 } 642 643 spin_lock_irqsave(&pcistub_devices_lock, flags); 644 list_del(&found_psdev->dev_list); 645 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 646 647 /* the final put for releasing from the list */ 648 pcistub_device_put(found_psdev); 649 } 650 } 651 652 static const struct pci_device_id pcistub_ids[] = { 653 { 654 .vendor = PCI_ANY_ID, 655 .device = PCI_ANY_ID, 656 .subvendor = PCI_ANY_ID, 657 .subdevice = PCI_ANY_ID, 658 }, 659 {0,}, 660 }; 661 662 #define PCI_NODENAME_MAX 40 663 static void kill_domain_by_device(struct pcistub_device *psdev) 664 { 665 struct xenbus_transaction xbt; 666 int err; 667 char nodename[PCI_NODENAME_MAX]; 668 669 BUG_ON(!psdev); 670 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0", 671 psdev->pdev->xdev->otherend_id); 672 673 again: 674 err = xenbus_transaction_start(&xbt); 675 if (err) { 676 dev_err(&psdev->dev->dev, 677 "error %d when start xenbus transaction\n", err); 678 return; 679 } 680 /*PV AER handlers will set this flag*/ 681 xenbus_printf(xbt, nodename, "aerState" , "aerfail"); 682 err = xenbus_transaction_end(xbt, 0); 683 if (err) { 684 if (err == -EAGAIN) 685 goto again; 686 dev_err(&psdev->dev->dev, 687 "error %d when end xenbus transaction\n", err); 688 return; 689 } 690 } 691 692 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and 693 * backend need to have cooperation. In xen_pcibk, those steps will do similar 694 * jobs: send service request and waiting for front_end response. 695 */ 696 static pci_ers_result_t common_process(struct pcistub_device *psdev, 697 pci_channel_state_t state, int aer_cmd, 698 pci_ers_result_t result) 699 { 700 pci_ers_result_t res = result; 701 struct xen_pcie_aer_op *aer_op; 702 struct xen_pcibk_device *pdev = psdev->pdev; 703 struct xen_pci_sharedinfo *sh_info = pdev->sh_info; 704 int ret; 705 706 /*with PV AER drivers*/ 707 aer_op = &(sh_info->aer_op); 708 aer_op->cmd = aer_cmd ; 709 /*useful for error_detected callback*/ 710 aer_op->err = state; 711 /*pcifront_end BDF*/ 712 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev, 713 &aer_op->domain, &aer_op->bus, &aer_op->devfn); 714 if (!ret) { 715 dev_err(&psdev->dev->dev, "failed to get pcifront device\n"); 716 return PCI_ERS_RESULT_NONE; 717 } 718 wmb(); 719 720 dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n", 721 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn); 722 /*local flag to mark there's aer request, xen_pcibk callback will use 723 * this flag to judge whether we need to check pci-front give aer 724 * service ack signal 725 */ 726 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags); 727 728 /*It is possible that a pcifront conf_read_write ops request invokes 729 * the callback which cause the spurious execution of wake_up. 730 * Yet it is harmless and better than a spinlock here 731 */ 732 set_bit(_XEN_PCIB_active, 733 (unsigned long *)&sh_info->flags); 734 wmb(); 735 notify_remote_via_irq(pdev->evtchn_irq); 736 737 ret = wait_event_timeout(xen_pcibk_aer_wait_queue, 738 !(test_bit(_XEN_PCIB_active, (unsigned long *) 739 &sh_info->flags)), 300*HZ); 740 741 if (!ret) { 742 if (test_bit(_XEN_PCIB_active, 743 (unsigned long *)&sh_info->flags)) { 744 dev_err(&psdev->dev->dev, 745 "pcifront aer process not responding!\n"); 746 clear_bit(_XEN_PCIB_active, 747 (unsigned long *)&sh_info->flags); 748 aer_op->err = PCI_ERS_RESULT_NONE; 749 return res; 750 } 751 } 752 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags); 753 754 if (test_bit(_XEN_PCIF_active, 755 (unsigned long *)&sh_info->flags)) { 756 dev_dbg(&psdev->dev->dev, "schedule pci_conf service\n"); 757 xen_pcibk_test_and_schedule_op(psdev->pdev); 758 } 759 760 res = (pci_ers_result_t)aer_op->err; 761 return res; 762 } 763 764 /* 765 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case 766 * of the device driver could provide this service, and then wait for pcifront 767 * ack. 768 * @dev: pointer to PCI devices 769 * return value is used by aer_core do_recovery policy 770 */ 771 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev) 772 { 773 struct pcistub_device *psdev; 774 pci_ers_result_t result; 775 776 result = PCI_ERS_RESULT_RECOVERED; 777 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n", 778 dev->bus->number, dev->devfn); 779 780 down_write(&pcistub_sem); 781 psdev = pcistub_device_find(pci_domain_nr(dev->bus), 782 dev->bus->number, 783 PCI_SLOT(dev->devfn), 784 PCI_FUNC(dev->devfn)); 785 786 if (!psdev || !psdev->pdev) { 787 dev_err(&dev->dev, "device is not found/assigned\n"); 788 goto end; 789 } 790 791 if (!psdev->pdev->sh_info) { 792 dev_err(&dev->dev, "device is not connected or owned" 793 " by HVM, kill it\n"); 794 kill_domain_by_device(psdev); 795 goto end; 796 } 797 798 if (!test_bit(_XEN_PCIB_AERHANDLER, 799 (unsigned long *)&psdev->pdev->sh_info->flags)) { 800 dev_err(&dev->dev, 801 "guest with no AER driver should have been killed\n"); 802 goto end; 803 } 804 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result); 805 806 if (result == PCI_ERS_RESULT_NONE || 807 result == PCI_ERS_RESULT_DISCONNECT) { 808 dev_dbg(&dev->dev, 809 "No AER slot_reset service or disconnected!\n"); 810 kill_domain_by_device(psdev); 811 } 812 end: 813 if (psdev) 814 pcistub_device_put(psdev); 815 up_write(&pcistub_sem); 816 return result; 817 818 } 819 820 821 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront 822 * in case of the device driver could provide this service, and then wait 823 * for pcifront ack 824 * @dev: pointer to PCI devices 825 * return value is used by aer_core do_recovery policy 826 */ 827 828 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev) 829 { 830 struct pcistub_device *psdev; 831 pci_ers_result_t result; 832 833 result = PCI_ERS_RESULT_RECOVERED; 834 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n", 835 dev->bus->number, dev->devfn); 836 837 down_write(&pcistub_sem); 838 psdev = pcistub_device_find(pci_domain_nr(dev->bus), 839 dev->bus->number, 840 PCI_SLOT(dev->devfn), 841 PCI_FUNC(dev->devfn)); 842 843 if (!psdev || !psdev->pdev) { 844 dev_err(&dev->dev, "device is not found/assigned\n"); 845 goto end; 846 } 847 848 if (!psdev->pdev->sh_info) { 849 dev_err(&dev->dev, "device is not connected or owned" 850 " by HVM, kill it\n"); 851 kill_domain_by_device(psdev); 852 goto end; 853 } 854 855 if (!test_bit(_XEN_PCIB_AERHANDLER, 856 (unsigned long *)&psdev->pdev->sh_info->flags)) { 857 dev_err(&dev->dev, 858 "guest with no AER driver should have been killed\n"); 859 goto end; 860 } 861 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result); 862 863 if (result == PCI_ERS_RESULT_NONE || 864 result == PCI_ERS_RESULT_DISCONNECT) { 865 dev_dbg(&dev->dev, 866 "No AER mmio_enabled service or disconnected!\n"); 867 kill_domain_by_device(psdev); 868 } 869 end: 870 if (psdev) 871 pcistub_device_put(psdev); 872 up_write(&pcistub_sem); 873 return result; 874 } 875 876 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront 877 * in case of the device driver could provide this service, and then wait 878 * for pcifront ack. 879 * @dev: pointer to PCI devices 880 * @error: the current PCI connection state 881 * return value is used by aer_core do_recovery policy 882 */ 883 884 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev, 885 pci_channel_state_t error) 886 { 887 struct pcistub_device *psdev; 888 pci_ers_result_t result; 889 890 result = PCI_ERS_RESULT_CAN_RECOVER; 891 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n", 892 dev->bus->number, dev->devfn); 893 894 down_write(&pcistub_sem); 895 psdev = pcistub_device_find(pci_domain_nr(dev->bus), 896 dev->bus->number, 897 PCI_SLOT(dev->devfn), 898 PCI_FUNC(dev->devfn)); 899 900 if (!psdev || !psdev->pdev) { 901 dev_err(&dev->dev, "device is not found/assigned\n"); 902 goto end; 903 } 904 905 if (!psdev->pdev->sh_info) { 906 dev_err(&dev->dev, "device is not connected or owned" 907 " by HVM, kill it\n"); 908 kill_domain_by_device(psdev); 909 goto end; 910 } 911 912 /*Guest owns the device yet no aer handler regiested, kill guest*/ 913 if (!test_bit(_XEN_PCIB_AERHANDLER, 914 (unsigned long *)&psdev->pdev->sh_info->flags)) { 915 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n"); 916 kill_domain_by_device(psdev); 917 goto end; 918 } 919 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result); 920 921 if (result == PCI_ERS_RESULT_NONE || 922 result == PCI_ERS_RESULT_DISCONNECT) { 923 dev_dbg(&dev->dev, 924 "No AER error_detected service or disconnected!\n"); 925 kill_domain_by_device(psdev); 926 } 927 end: 928 if (psdev) 929 pcistub_device_put(psdev); 930 up_write(&pcistub_sem); 931 return result; 932 } 933 934 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront 935 * in case of the device driver could provide this service, and then wait 936 * for pcifront ack. 937 * @dev: pointer to PCI devices 938 */ 939 940 static void xen_pcibk_error_resume(struct pci_dev *dev) 941 { 942 struct pcistub_device *psdev; 943 944 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n", 945 dev->bus->number, dev->devfn); 946 947 down_write(&pcistub_sem); 948 psdev = pcistub_device_find(pci_domain_nr(dev->bus), 949 dev->bus->number, 950 PCI_SLOT(dev->devfn), 951 PCI_FUNC(dev->devfn)); 952 953 if (!psdev || !psdev->pdev) { 954 dev_err(&dev->dev, "device is not found/assigned\n"); 955 goto end; 956 } 957 958 if (!psdev->pdev->sh_info) { 959 dev_err(&dev->dev, "device is not connected or owned" 960 " by HVM, kill it\n"); 961 kill_domain_by_device(psdev); 962 goto end; 963 } 964 965 if (!test_bit(_XEN_PCIB_AERHANDLER, 966 (unsigned long *)&psdev->pdev->sh_info->flags)) { 967 dev_err(&dev->dev, 968 "guest with no AER driver should have been killed\n"); 969 kill_domain_by_device(psdev); 970 goto end; 971 } 972 common_process(psdev, 1, XEN_PCI_OP_aer_resume, 973 PCI_ERS_RESULT_RECOVERED); 974 end: 975 if (psdev) 976 pcistub_device_put(psdev); 977 up_write(&pcistub_sem); 978 return; 979 } 980 981 /*add xen_pcibk AER handling*/ 982 static const struct pci_error_handlers xen_pcibk_error_handler = { 983 .error_detected = xen_pcibk_error_detected, 984 .mmio_enabled = xen_pcibk_mmio_enabled, 985 .slot_reset = xen_pcibk_slot_reset, 986 .resume = xen_pcibk_error_resume, 987 }; 988 989 /* 990 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't 991 * for a normal device. I don't want it to be loaded automatically. 992 */ 993 994 static struct pci_driver xen_pcibk_pci_driver = { 995 /* The name should be xen_pciback, but until the tools are updated 996 * we will keep it as pciback. */ 997 .name = PCISTUB_DRIVER_NAME, 998 .id_table = pcistub_ids, 999 .probe = pcistub_probe, 1000 .remove = pcistub_remove, 1001 .err_handler = &xen_pcibk_error_handler, 1002 }; 1003 1004 static inline int str_to_slot(const char *buf, int *domain, int *bus, 1005 int *slot, int *func) 1006 { 1007 int parsed = 0; 1008 1009 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func, 1010 &parsed)) { 1011 case 3: 1012 *func = -1; 1013 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed); 1014 break; 1015 case 2: 1016 *slot = *func = -1; 1017 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed); 1018 break; 1019 } 1020 if (parsed && !buf[parsed]) 1021 return 0; 1022 1023 /* try again without domain */ 1024 *domain = 0; 1025 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) { 1026 case 2: 1027 *func = -1; 1028 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed); 1029 break; 1030 case 1: 1031 *slot = *func = -1; 1032 sscanf(buf, " %x:*.* %n", bus, &parsed); 1033 break; 1034 } 1035 if (parsed && !buf[parsed]) 1036 return 0; 1037 1038 return -EINVAL; 1039 } 1040 1041 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int 1042 *slot, int *func, int *reg, int *size, int *mask) 1043 { 1044 int parsed = 0; 1045 1046 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func, 1047 reg, size, mask, &parsed); 1048 if (parsed && !buf[parsed]) 1049 return 0; 1050 1051 /* try again without domain */ 1052 *domain = 0; 1053 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size, 1054 mask, &parsed); 1055 if (parsed && !buf[parsed]) 1056 return 0; 1057 1058 return -EINVAL; 1059 } 1060 1061 static int pcistub_device_id_add(int domain, int bus, int slot, int func) 1062 { 1063 struct pcistub_device_id *pci_dev_id; 1064 int rc = 0, devfn = PCI_DEVFN(slot, func); 1065 1066 if (slot < 0) { 1067 for (slot = 0; !rc && slot < 32; ++slot) 1068 rc = pcistub_device_id_add(domain, bus, slot, func); 1069 return rc; 1070 } 1071 1072 if (func < 0) { 1073 for (func = 0; !rc && func < 8; ++func) 1074 rc = pcistub_device_id_add(domain, bus, slot, func); 1075 return rc; 1076 } 1077 1078 if (( 1079 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \ 1080 || !defined(CONFIG_PCI_DOMAINS) 1081 !pci_domains_supported ? domain : 1082 #endif 1083 domain < 0 || domain > 0xffff) 1084 || bus < 0 || bus > 0xff 1085 || PCI_SLOT(devfn) != slot 1086 || PCI_FUNC(devfn) != func) 1087 return -EINVAL; 1088 1089 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL); 1090 if (!pci_dev_id) 1091 return -ENOMEM; 1092 1093 pr_debug("wants to seize %04x:%02x:%02x.%d\n", 1094 domain, bus, slot, func); 1095 1096 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn); 1097 1098 return 0; 1099 } 1100 1101 static int pcistub_device_id_remove(int domain, int bus, int slot, int func) 1102 { 1103 struct pcistub_device_id *pci_dev_id, *t; 1104 int err = -ENOENT; 1105 unsigned long flags; 1106 1107 spin_lock_irqsave(&device_ids_lock, flags); 1108 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, 1109 slot_list) { 1110 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus 1111 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot) 1112 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) { 1113 /* Don't break; here because it's possible the same 1114 * slot could be in the list more than once 1115 */ 1116 list_del(&pci_dev_id->slot_list); 1117 kfree(pci_dev_id); 1118 1119 err = 0; 1120 1121 pr_debug("removed %04x:%02x:%02x.%d from seize list\n", 1122 domain, bus, slot, func); 1123 } 1124 } 1125 spin_unlock_irqrestore(&device_ids_lock, flags); 1126 1127 return err; 1128 } 1129 1130 static int pcistub_reg_add(int domain, int bus, int slot, int func, 1131 unsigned int reg, unsigned int size, 1132 unsigned int mask) 1133 { 1134 int err = 0; 1135 struct pcistub_device *psdev; 1136 struct pci_dev *dev; 1137 struct config_field *field; 1138 1139 if (reg > 0xfff || (size < 4 && (mask >> (size * 8)))) 1140 return -EINVAL; 1141 1142 psdev = pcistub_device_find(domain, bus, slot, func); 1143 if (!psdev) { 1144 err = -ENODEV; 1145 goto out; 1146 } 1147 dev = psdev->dev; 1148 1149 field = kzalloc(sizeof(*field), GFP_KERNEL); 1150 if (!field) { 1151 err = -ENOMEM; 1152 goto out; 1153 } 1154 1155 field->offset = reg; 1156 field->size = size; 1157 field->mask = mask; 1158 field->init = NULL; 1159 field->reset = NULL; 1160 field->release = NULL; 1161 field->clean = xen_pcibk_config_field_free; 1162 1163 err = xen_pcibk_config_quirks_add_field(dev, field); 1164 if (err) 1165 kfree(field); 1166 out: 1167 if (psdev) 1168 pcistub_device_put(psdev); 1169 return err; 1170 } 1171 1172 static ssize_t new_slot_store(struct device_driver *drv, const char *buf, 1173 size_t count) 1174 { 1175 int domain, bus, slot, func; 1176 int err; 1177 1178 err = str_to_slot(buf, &domain, &bus, &slot, &func); 1179 if (err) 1180 goto out; 1181 1182 err = pcistub_device_id_add(domain, bus, slot, func); 1183 1184 out: 1185 if (!err) 1186 err = count; 1187 return err; 1188 } 1189 static DRIVER_ATTR_WO(new_slot); 1190 1191 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf, 1192 size_t count) 1193 { 1194 int domain, bus, slot, func; 1195 int err; 1196 1197 err = str_to_slot(buf, &domain, &bus, &slot, &func); 1198 if (err) 1199 goto out; 1200 1201 err = pcistub_device_id_remove(domain, bus, slot, func); 1202 1203 out: 1204 if (!err) 1205 err = count; 1206 return err; 1207 } 1208 static DRIVER_ATTR_WO(remove_slot); 1209 1210 static ssize_t slots_show(struct device_driver *drv, char *buf) 1211 { 1212 struct pcistub_device_id *pci_dev_id; 1213 size_t count = 0; 1214 unsigned long flags; 1215 1216 spin_lock_irqsave(&device_ids_lock, flags); 1217 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { 1218 if (count >= PAGE_SIZE) 1219 break; 1220 1221 count += scnprintf(buf + count, PAGE_SIZE - count, 1222 "%04x:%02x:%02x.%d\n", 1223 pci_dev_id->domain, pci_dev_id->bus, 1224 PCI_SLOT(pci_dev_id->devfn), 1225 PCI_FUNC(pci_dev_id->devfn)); 1226 } 1227 spin_unlock_irqrestore(&device_ids_lock, flags); 1228 1229 return count; 1230 } 1231 static DRIVER_ATTR_RO(slots); 1232 1233 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf) 1234 { 1235 struct pcistub_device *psdev; 1236 struct xen_pcibk_dev_data *dev_data; 1237 size_t count = 0; 1238 unsigned long flags; 1239 1240 spin_lock_irqsave(&pcistub_devices_lock, flags); 1241 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 1242 if (count >= PAGE_SIZE) 1243 break; 1244 if (!psdev->dev) 1245 continue; 1246 dev_data = pci_get_drvdata(psdev->dev); 1247 if (!dev_data) 1248 continue; 1249 count += 1250 scnprintf(buf + count, PAGE_SIZE - count, 1251 "%s:%s:%sing:%ld\n", 1252 pci_name(psdev->dev), 1253 dev_data->isr_on ? "on" : "off", 1254 dev_data->ack_intr ? "ack" : "not ack", 1255 dev_data->handled); 1256 } 1257 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1258 return count; 1259 } 1260 static DRIVER_ATTR_RO(irq_handlers); 1261 1262 static ssize_t irq_handler_state_store(struct device_driver *drv, 1263 const char *buf, size_t count) 1264 { 1265 struct pcistub_device *psdev; 1266 struct xen_pcibk_dev_data *dev_data; 1267 int domain, bus, slot, func; 1268 int err; 1269 1270 err = str_to_slot(buf, &domain, &bus, &slot, &func); 1271 if (err) 1272 return err; 1273 1274 psdev = pcistub_device_find(domain, bus, slot, func); 1275 if (!psdev) { 1276 err = -ENOENT; 1277 goto out; 1278 } 1279 1280 dev_data = pci_get_drvdata(psdev->dev); 1281 if (!dev_data) { 1282 err = -ENOENT; 1283 goto out; 1284 } 1285 1286 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n", 1287 dev_data->irq_name, dev_data->isr_on, 1288 !dev_data->isr_on); 1289 1290 dev_data->isr_on = !(dev_data->isr_on); 1291 if (dev_data->isr_on) 1292 dev_data->ack_intr = 1; 1293 out: 1294 if (psdev) 1295 pcistub_device_put(psdev); 1296 if (!err) 1297 err = count; 1298 return err; 1299 } 1300 static DRIVER_ATTR_WO(irq_handler_state); 1301 1302 static ssize_t quirks_store(struct device_driver *drv, const char *buf, 1303 size_t count) 1304 { 1305 int domain, bus, slot, func, reg, size, mask; 1306 int err; 1307 1308 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size, 1309 &mask); 1310 if (err) 1311 goto out; 1312 1313 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask); 1314 1315 out: 1316 if (!err) 1317 err = count; 1318 return err; 1319 } 1320 1321 static ssize_t quirks_show(struct device_driver *drv, char *buf) 1322 { 1323 int count = 0; 1324 unsigned long flags; 1325 struct xen_pcibk_config_quirk *quirk; 1326 struct xen_pcibk_dev_data *dev_data; 1327 const struct config_field *field; 1328 const struct config_field_entry *cfg_entry; 1329 1330 spin_lock_irqsave(&device_ids_lock, flags); 1331 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) { 1332 if (count >= PAGE_SIZE) 1333 goto out; 1334 1335 count += scnprintf(buf + count, PAGE_SIZE - count, 1336 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n", 1337 quirk->pdev->bus->number, 1338 PCI_SLOT(quirk->pdev->devfn), 1339 PCI_FUNC(quirk->pdev->devfn), 1340 quirk->devid.vendor, quirk->devid.device, 1341 quirk->devid.subvendor, 1342 quirk->devid.subdevice); 1343 1344 dev_data = pci_get_drvdata(quirk->pdev); 1345 1346 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) { 1347 field = cfg_entry->field; 1348 if (count >= PAGE_SIZE) 1349 goto out; 1350 1351 count += scnprintf(buf + count, PAGE_SIZE - count, 1352 "\t\t%08x:%01x:%08x\n", 1353 cfg_entry->base_offset + 1354 field->offset, field->size, 1355 field->mask); 1356 } 1357 } 1358 1359 out: 1360 spin_unlock_irqrestore(&device_ids_lock, flags); 1361 1362 return count; 1363 } 1364 static DRIVER_ATTR_RW(quirks); 1365 1366 static ssize_t permissive_store(struct device_driver *drv, const char *buf, 1367 size_t count) 1368 { 1369 int domain, bus, slot, func; 1370 int err; 1371 struct pcistub_device *psdev; 1372 struct xen_pcibk_dev_data *dev_data; 1373 1374 err = str_to_slot(buf, &domain, &bus, &slot, &func); 1375 if (err) 1376 goto out; 1377 1378 psdev = pcistub_device_find(domain, bus, slot, func); 1379 if (!psdev) { 1380 err = -ENODEV; 1381 goto out; 1382 } 1383 1384 dev_data = pci_get_drvdata(psdev->dev); 1385 /* the driver data for a device should never be null at this point */ 1386 if (!dev_data) { 1387 err = -ENXIO; 1388 goto release; 1389 } 1390 if (!dev_data->permissive) { 1391 dev_data->permissive = 1; 1392 /* Let user know that what they're doing could be unsafe */ 1393 dev_warn(&psdev->dev->dev, "enabling permissive mode " 1394 "configuration space accesses!\n"); 1395 dev_warn(&psdev->dev->dev, 1396 "permissive mode is potentially unsafe!\n"); 1397 } 1398 release: 1399 pcistub_device_put(psdev); 1400 out: 1401 if (!err) 1402 err = count; 1403 return err; 1404 } 1405 1406 static ssize_t permissive_show(struct device_driver *drv, char *buf) 1407 { 1408 struct pcistub_device *psdev; 1409 struct xen_pcibk_dev_data *dev_data; 1410 size_t count = 0; 1411 unsigned long flags; 1412 spin_lock_irqsave(&pcistub_devices_lock, flags); 1413 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 1414 if (count >= PAGE_SIZE) 1415 break; 1416 if (!psdev->dev) 1417 continue; 1418 dev_data = pci_get_drvdata(psdev->dev); 1419 if (!dev_data || !dev_data->permissive) 1420 continue; 1421 count += 1422 scnprintf(buf + count, PAGE_SIZE - count, "%s\n", 1423 pci_name(psdev->dev)); 1424 } 1425 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1426 return count; 1427 } 1428 static DRIVER_ATTR_RW(permissive); 1429 1430 static ssize_t allow_interrupt_control_store(struct device_driver *drv, 1431 const char *buf, size_t count) 1432 { 1433 int domain, bus, slot, func; 1434 int err; 1435 struct pcistub_device *psdev; 1436 struct xen_pcibk_dev_data *dev_data; 1437 1438 err = str_to_slot(buf, &domain, &bus, &slot, &func); 1439 if (err) 1440 goto out; 1441 1442 psdev = pcistub_device_find(domain, bus, slot, func); 1443 if (!psdev) { 1444 err = -ENODEV; 1445 goto out; 1446 } 1447 1448 dev_data = pci_get_drvdata(psdev->dev); 1449 /* the driver data for a device should never be null at this point */ 1450 if (!dev_data) { 1451 err = -ENXIO; 1452 goto release; 1453 } 1454 dev_data->allow_interrupt_control = 1; 1455 release: 1456 pcistub_device_put(psdev); 1457 out: 1458 if (!err) 1459 err = count; 1460 return err; 1461 } 1462 1463 static ssize_t allow_interrupt_control_show(struct device_driver *drv, 1464 char *buf) 1465 { 1466 struct pcistub_device *psdev; 1467 struct xen_pcibk_dev_data *dev_data; 1468 size_t count = 0; 1469 unsigned long flags; 1470 1471 spin_lock_irqsave(&pcistub_devices_lock, flags); 1472 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 1473 if (count >= PAGE_SIZE) 1474 break; 1475 if (!psdev->dev) 1476 continue; 1477 dev_data = pci_get_drvdata(psdev->dev); 1478 if (!dev_data || !dev_data->allow_interrupt_control) 1479 continue; 1480 count += 1481 scnprintf(buf + count, PAGE_SIZE - count, "%s\n", 1482 pci_name(psdev->dev)); 1483 } 1484 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1485 return count; 1486 } 1487 static DRIVER_ATTR_RW(allow_interrupt_control); 1488 1489 static void pcistub_exit(void) 1490 { 1491 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot); 1492 driver_remove_file(&xen_pcibk_pci_driver.driver, 1493 &driver_attr_remove_slot); 1494 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots); 1495 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks); 1496 driver_remove_file(&xen_pcibk_pci_driver.driver, 1497 &driver_attr_permissive); 1498 driver_remove_file(&xen_pcibk_pci_driver.driver, 1499 &driver_attr_allow_interrupt_control); 1500 driver_remove_file(&xen_pcibk_pci_driver.driver, 1501 &driver_attr_irq_handlers); 1502 driver_remove_file(&xen_pcibk_pci_driver.driver, 1503 &driver_attr_irq_handler_state); 1504 pci_unregister_driver(&xen_pcibk_pci_driver); 1505 } 1506 1507 static int __init pcistub_init(void) 1508 { 1509 int pos = 0; 1510 int err = 0; 1511 int domain, bus, slot, func; 1512 int parsed; 1513 1514 if (pci_devs_to_hide && *pci_devs_to_hide) { 1515 do { 1516 parsed = 0; 1517 1518 err = sscanf(pci_devs_to_hide + pos, 1519 " (%x:%x:%x.%x) %n", 1520 &domain, &bus, &slot, &func, &parsed); 1521 switch (err) { 1522 case 3: 1523 func = -1; 1524 sscanf(pci_devs_to_hide + pos, 1525 " (%x:%x:%x.*) %n", 1526 &domain, &bus, &slot, &parsed); 1527 break; 1528 case 2: 1529 slot = func = -1; 1530 sscanf(pci_devs_to_hide + pos, 1531 " (%x:%x:*.*) %n", 1532 &domain, &bus, &parsed); 1533 break; 1534 } 1535 1536 if (!parsed) { 1537 domain = 0; 1538 err = sscanf(pci_devs_to_hide + pos, 1539 " (%x:%x.%x) %n", 1540 &bus, &slot, &func, &parsed); 1541 switch (err) { 1542 case 2: 1543 func = -1; 1544 sscanf(pci_devs_to_hide + pos, 1545 " (%x:%x.*) %n", 1546 &bus, &slot, &parsed); 1547 break; 1548 case 1: 1549 slot = func = -1; 1550 sscanf(pci_devs_to_hide + pos, 1551 " (%x:*.*) %n", 1552 &bus, &parsed); 1553 break; 1554 } 1555 } 1556 1557 if (parsed <= 0) 1558 goto parse_error; 1559 1560 err = pcistub_device_id_add(domain, bus, slot, func); 1561 if (err) 1562 goto out; 1563 1564 pos += parsed; 1565 } while (pci_devs_to_hide[pos]); 1566 } 1567 1568 /* If we're the first PCI Device Driver to register, we're the 1569 * first one to get offered PCI devices as they become 1570 * available (and thus we can be the first to grab them) 1571 */ 1572 err = pci_register_driver(&xen_pcibk_pci_driver); 1573 if (err < 0) 1574 goto out; 1575 1576 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1577 &driver_attr_new_slot); 1578 if (!err) 1579 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1580 &driver_attr_remove_slot); 1581 if (!err) 1582 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1583 &driver_attr_slots); 1584 if (!err) 1585 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1586 &driver_attr_quirks); 1587 if (!err) 1588 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1589 &driver_attr_permissive); 1590 if (!err) 1591 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1592 &driver_attr_allow_interrupt_control); 1593 1594 if (!err) 1595 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1596 &driver_attr_irq_handlers); 1597 if (!err) 1598 err = driver_create_file(&xen_pcibk_pci_driver.driver, 1599 &driver_attr_irq_handler_state); 1600 if (err) 1601 pcistub_exit(); 1602 1603 out: 1604 return err; 1605 1606 parse_error: 1607 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n", 1608 pci_devs_to_hide + pos); 1609 return -EINVAL; 1610 } 1611 1612 #ifndef MODULE 1613 /* 1614 * fs_initcall happens before device_initcall 1615 * so xen_pcibk *should* get called first (b/c we 1616 * want to suck up any device before other drivers 1617 * get a chance by being the first pci device 1618 * driver to register) 1619 */ 1620 fs_initcall(pcistub_init); 1621 #endif 1622 1623 #ifdef CONFIG_PCI_IOV 1624 static struct pcistub_device *find_vfs(const struct pci_dev *pdev) 1625 { 1626 struct pcistub_device *psdev = NULL; 1627 unsigned long flags; 1628 bool found = false; 1629 1630 spin_lock_irqsave(&pcistub_devices_lock, flags); 1631 list_for_each_entry(psdev, &pcistub_devices, dev_list) { 1632 if (!psdev->pdev && psdev->dev != pdev 1633 && pci_physfn(psdev->dev) == pdev) { 1634 found = true; 1635 break; 1636 } 1637 } 1638 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1639 if (found) 1640 return psdev; 1641 return NULL; 1642 } 1643 1644 static int pci_stub_notifier(struct notifier_block *nb, 1645 unsigned long action, void *data) 1646 { 1647 struct device *dev = data; 1648 const struct pci_dev *pdev = to_pci_dev(dev); 1649 1650 if (action != BUS_NOTIFY_UNBIND_DRIVER) 1651 return NOTIFY_DONE; 1652 1653 if (!pdev->is_physfn) 1654 return NOTIFY_DONE; 1655 1656 for (;;) { 1657 struct pcistub_device *psdev = find_vfs(pdev); 1658 if (!psdev) 1659 break; 1660 device_release_driver(&psdev->dev->dev); 1661 } 1662 return NOTIFY_DONE; 1663 } 1664 1665 static struct notifier_block pci_stub_nb = { 1666 .notifier_call = pci_stub_notifier, 1667 }; 1668 #endif 1669 1670 static int __init xen_pcibk_init(void) 1671 { 1672 int err; 1673 1674 if (!xen_initial_domain()) 1675 return -ENODEV; 1676 1677 err = xen_pcibk_config_init(); 1678 if (err) 1679 return err; 1680 1681 #ifdef MODULE 1682 err = pcistub_init(); 1683 if (err < 0) 1684 return err; 1685 #endif 1686 1687 pcistub_init_devices_late(); 1688 err = xen_pcibk_xenbus_register(); 1689 if (err) 1690 pcistub_exit(); 1691 #ifdef CONFIG_PCI_IOV 1692 else 1693 bus_register_notifier(&pci_bus_type, &pci_stub_nb); 1694 #endif 1695 1696 return err; 1697 } 1698 1699 static void __exit xen_pcibk_cleanup(void) 1700 { 1701 #ifdef CONFIG_PCI_IOV 1702 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb); 1703 #endif 1704 xen_pcibk_xenbus_unregister(); 1705 pcistub_exit(); 1706 } 1707 1708 module_init(xen_pcibk_init); 1709 module_exit(xen_pcibk_cleanup); 1710 1711 MODULE_LICENSE("Dual BSD/GPL"); 1712 MODULE_ALIAS("xen-backend:pci"); 1713