remove.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <linux/pci.h>
  2. #include <linux/module.h>
  3. #include <linux/pci-aspm.h>
  4. #include "pci.h"
  5. static void pci_free_resources(struct pci_dev *dev)
  6. {
  7. int i;
  8. msi_remove_pci_irq_vectors(dev);
  9. pci_cleanup_rom(dev);
  10. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  11. struct resource *res = dev->resource + i;
  12. if (res->parent)
  13. release_resource(res);
  14. }
  15. }
  16. static void pci_stop_dev(struct pci_dev *dev)
  17. {
  18. if (dev->is_added) {
  19. pci_proc_detach_device(dev);
  20. pci_remove_sysfs_dev_files(dev);
  21. device_unregister(&dev->dev);
  22. dev->is_added = 0;
  23. }
  24. if (dev->bus->self)
  25. pcie_aspm_exit_link_state(dev);
  26. }
  27. static void pci_destroy_dev(struct pci_dev *dev)
  28. {
  29. /* Remove the device from the device lists, and prevent any further
  30. * list accesses from this device */
  31. down_write(&pci_bus_sem);
  32. list_del(&dev->bus_list);
  33. dev->bus_list.next = dev->bus_list.prev = NULL;
  34. up_write(&pci_bus_sem);
  35. pci_free_resources(dev);
  36. pci_dev_put(dev);
  37. }
  38. /**
  39. * pci_remove_device_safe - remove an unused hotplug device
  40. * @dev: the device to remove
  41. *
  42. * Delete the device structure from the device lists and
  43. * notify userspace (/sbin/hotplug), but only if the device
  44. * in question is not being used by a driver.
  45. * Returns 0 on success.
  46. */
  47. #if 0
  48. int pci_remove_device_safe(struct pci_dev *dev)
  49. {
  50. if (pci_dev_driver(dev))
  51. return -EBUSY;
  52. pci_destroy_dev(dev);
  53. return 0;
  54. }
  55. #endif /* 0 */
  56. void pci_remove_bus(struct pci_bus *pci_bus)
  57. {
  58. pci_proc_detach_bus(pci_bus);
  59. down_write(&pci_bus_sem);
  60. list_del(&pci_bus->node);
  61. pci_bus_release_busn_res(pci_bus);
  62. up_write(&pci_bus_sem);
  63. if (!pci_bus->is_added)
  64. return;
  65. pci_remove_legacy_files(pci_bus);
  66. device_unregister(&pci_bus->dev);
  67. }
  68. EXPORT_SYMBOL(pci_remove_bus);
  69. /**
  70. * pci_stop_and_remove_bus_device - remove a PCI device and any children
  71. * @dev: the device to remove
  72. *
  73. * Remove a PCI device from the device lists, informing the drivers
  74. * that the device has been removed. We also remove any subordinate
  75. * buses and children in a depth-first manner.
  76. *
  77. * For each device we remove, delete the device structure from the
  78. * device lists, remove the /proc entry, and notify userspace
  79. * (/sbin/hotplug).
  80. */
  81. void pci_stop_and_remove_bus_device(struct pci_dev *dev)
  82. {
  83. struct pci_bus *bus = dev->subordinate;
  84. struct pci_dev *child, *tmp;
  85. /*
  86. * Removing an SR-IOV PF device removes all the associated VFs,
  87. * which will update the bus->devices list and confuse the
  88. * iterator. Therefore, iterate in reverse so we remove the VFs
  89. * first, then the PF.
  90. */
  91. if (bus) {
  92. list_for_each_entry_safe_reverse(child, tmp,
  93. &bus->devices, bus_list)
  94. pci_stop_and_remove_bus_device(child);
  95. pci_remove_bus(bus);
  96. dev->subordinate = NULL;
  97. }
  98. pci_stop_dev(dev);
  99. pci_destroy_dev(dev);
  100. }
  101. EXPORT_SYMBOL(pci_stop_and_remove_bus_device);