remove.c 3.2 KB

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