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