remove.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  9. struct resource *res = dev->resource + i;
  10. if (res->parent)
  11. release_resource(res);
  12. }
  13. }
  14. static void pci_stop_dev(struct pci_dev *dev)
  15. {
  16. pci_pme_active(dev, false);
  17. if (dev->is_added) {
  18. device_release_driver(&dev->dev);
  19. pci_proc_detach_device(dev);
  20. pci_remove_sysfs_dev_files(dev);
  21. dev->is_added = 0;
  22. }
  23. if (dev->bus->self)
  24. pcie_aspm_exit_link_state(dev);
  25. }
  26. static void pci_destroy_dev(struct pci_dev *dev)
  27. {
  28. if (!dev->dev.kobj.parent)
  29. return;
  30. device_del(&dev->dev);
  31. down_write(&pci_bus_sem);
  32. list_del(&dev->bus_list);
  33. up_write(&pci_bus_sem);
  34. pci_bridge_d3_device_removed(dev);
  35. pci_free_resources(dev);
  36. put_device(&dev->dev);
  37. }
  38. void pci_remove_bus(struct pci_bus *bus)
  39. {
  40. pci_proc_detach_bus(bus);
  41. down_write(&pci_bus_sem);
  42. list_del(&bus->node);
  43. pci_bus_release_busn_res(bus);
  44. up_write(&pci_bus_sem);
  45. pci_remove_legacy_files(bus);
  46. if (bus->ops->remove_bus)
  47. bus->ops->remove_bus(bus);
  48. pcibios_remove_bus(bus);
  49. device_unregister(&bus->dev);
  50. }
  51. EXPORT_SYMBOL(pci_remove_bus);
  52. static void pci_stop_bus_device(struct pci_dev *dev)
  53. {
  54. struct pci_bus *bus = dev->subordinate;
  55. struct pci_dev *child, *tmp;
  56. /*
  57. * Stopping an SR-IOV PF device removes all the associated VFs,
  58. * which will update the bus->devices list and confuse the
  59. * iterator. Therefore, iterate in reverse so we remove the VFs
  60. * first, then the PF.
  61. */
  62. if (bus) {
  63. list_for_each_entry_safe_reverse(child, tmp,
  64. &bus->devices, bus_list)
  65. pci_stop_bus_device(child);
  66. }
  67. pci_stop_dev(dev);
  68. }
  69. static void pci_remove_bus_device(struct pci_dev *dev)
  70. {
  71. struct pci_bus *bus = dev->subordinate;
  72. struct pci_dev *child, *tmp;
  73. if (bus) {
  74. list_for_each_entry_safe(child, tmp,
  75. &bus->devices, bus_list)
  76. pci_remove_bus_device(child);
  77. pci_remove_bus(bus);
  78. dev->subordinate = NULL;
  79. }
  80. pci_destroy_dev(dev);
  81. }
  82. /**
  83. * pci_stop_and_remove_bus_device - remove a PCI device and any children
  84. * @dev: the device to remove
  85. *
  86. * Remove a PCI device from the device lists, informing the drivers
  87. * that the device has been removed. We also remove any subordinate
  88. * buses and children in a depth-first manner.
  89. *
  90. * For each device we remove, delete the device structure from the
  91. * device lists, remove the /proc entry, and notify userspace
  92. * (/sbin/hotplug).
  93. */
  94. void pci_stop_and_remove_bus_device(struct pci_dev *dev)
  95. {
  96. pci_stop_bus_device(dev);
  97. pci_remove_bus_device(dev);
  98. }
  99. EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
  100. void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
  101. {
  102. pci_lock_rescan_remove();
  103. pci_stop_and_remove_bus_device(dev);
  104. pci_unlock_rescan_remove();
  105. }
  106. EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
  107. void pci_stop_root_bus(struct pci_bus *bus)
  108. {
  109. struct pci_dev *child, *tmp;
  110. struct pci_host_bridge *host_bridge;
  111. if (!pci_is_root_bus(bus))
  112. return;
  113. host_bridge = to_pci_host_bridge(bus->bridge);
  114. list_for_each_entry_safe_reverse(child, tmp,
  115. &bus->devices, bus_list)
  116. pci_stop_bus_device(child);
  117. /* stop the host bridge */
  118. device_release_driver(&host_bridge->dev);
  119. }
  120. EXPORT_SYMBOL_GPL(pci_stop_root_bus);
  121. void pci_remove_root_bus(struct pci_bus *bus)
  122. {
  123. struct pci_dev *child, *tmp;
  124. struct pci_host_bridge *host_bridge;
  125. if (!pci_is_root_bus(bus))
  126. return;
  127. host_bridge = to_pci_host_bridge(bus->bridge);
  128. list_for_each_entry_safe(child, tmp,
  129. &bus->devices, bus_list)
  130. pci_remove_bus_device(child);
  131. pci_remove_bus(bus);
  132. host_bridge->bus = NULL;
  133. /* remove the host bridge */
  134. device_unregister(&host_bridge->dev);
  135. }
  136. EXPORT_SYMBOL_GPL(pci_remove_root_bus);