remove.c 3.6 KB

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