remove.c 3.7 KB

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