remove.c 3.3 KB

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