remove.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_unregister(&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. /* Remove the device from the device lists, and prevent any further
  31. * list accesses from this device */
  32. down_write(&pci_bus_sem);
  33. list_del(&dev->bus_list);
  34. dev->bus_list.next = dev->bus_list.prev = NULL;
  35. up_write(&pci_bus_sem);
  36. pci_free_resources(dev);
  37. pci_dev_put(dev);
  38. }
  39. /**
  40. * pci_remove_device_safe - remove an unused hotplug device
  41. * @dev: the device to remove
  42. *
  43. * Delete the device structure from the device lists and
  44. * notify userspace (/sbin/hotplug), but only if the device
  45. * in question is not being used by a driver.
  46. * Returns 0 on success.
  47. */
  48. #if 0
  49. int pci_remove_device_safe(struct pci_dev *dev)
  50. {
  51. if (pci_dev_driver(dev))
  52. return -EBUSY;
  53. pci_destroy_dev(dev);
  54. return 0;
  55. }
  56. #endif /* 0 */
  57. void pci_remove_bus(struct pci_bus *pci_bus)
  58. {
  59. pci_proc_detach_bus(pci_bus);
  60. down_write(&pci_bus_sem);
  61. list_del(&pci_bus->node);
  62. up_write(&pci_bus_sem);
  63. if (!pci_bus->is_added)
  64. return;
  65. pci_remove_legacy_files(pci_bus);
  66. device_unregister(&pci_bus->dev);
  67. }
  68. EXPORT_SYMBOL(pci_remove_bus);
  69. static void __pci_remove_behind_bridge(struct pci_dev *dev);
  70. /**
  71. * pci_stop_and_remove_bus_device - remove a PCI device and any children
  72. * @dev: the device to remove
  73. *
  74. * Remove a PCI device from the device lists, informing the drivers
  75. * that the device has been removed. We also remove any subordinate
  76. * buses and children in a depth-first manner.
  77. *
  78. * For each device we remove, delete the device structure from the
  79. * device lists, remove the /proc entry, and notify userspace
  80. * (/sbin/hotplug).
  81. */
  82. void __pci_remove_bus_device(struct pci_dev *dev)
  83. {
  84. if (dev->subordinate) {
  85. struct pci_bus *b = dev->subordinate;
  86. __pci_remove_behind_bridge(dev);
  87. pci_remove_bus(b);
  88. dev->subordinate = NULL;
  89. }
  90. pci_destroy_dev(dev);
  91. }
  92. EXPORT_SYMBOL(__pci_remove_bus_device);
  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. static void __pci_remove_behind_bridge(struct pci_dev *dev)
  99. {
  100. struct list_head *l, *n;
  101. if (dev->subordinate)
  102. list_for_each_safe(l, n, &dev->subordinate->devices)
  103. __pci_remove_bus_device(pci_dev_b(l));
  104. }
  105. static void pci_stop_behind_bridge(struct pci_dev *dev)
  106. {
  107. struct list_head *l, *n;
  108. if (dev->subordinate)
  109. list_for_each_safe(l, n, &dev->subordinate->devices)
  110. pci_stop_bus_device(pci_dev_b(l));
  111. }
  112. /**
  113. * pci_stop_and_remove_behind_bridge - stop and remove all devices behind
  114. * a PCI bridge
  115. * @dev: PCI bridge device
  116. *
  117. * Remove all devices on the bus, except for the parent bridge.
  118. * This also removes any child buses, and any devices they may
  119. * contain in a depth-first manner.
  120. */
  121. void pci_stop_and_remove_behind_bridge(struct pci_dev *dev)
  122. {
  123. pci_stop_behind_bridge(dev);
  124. __pci_remove_behind_bridge(dev);
  125. }
  126. static void pci_stop_bus_devices(struct pci_bus *bus)
  127. {
  128. struct list_head *l, *n;
  129. /*
  130. * VFs could be removed by pci_stop_and_remove_bus_device() in the
  131. * pci_stop_bus_devices() code path for PF.
  132. * aka, bus->devices get updated in the process.
  133. * but VFs are inserted after PFs when SRIOV is enabled for PF,
  134. * We can iterate the list backwards to get prev valid PF instead
  135. * of removed VF.
  136. */
  137. list_for_each_prev_safe(l, n, &bus->devices) {
  138. struct pci_dev *dev = pci_dev_b(l);
  139. pci_stop_bus_device(dev);
  140. }
  141. }
  142. /**
  143. * pci_stop_bus_device - stop a PCI device and any children
  144. * @dev: the device to stop
  145. *
  146. * Stop a PCI device (detach the driver, remove from the global list
  147. * and so on). This also stop any subordinate buses and children in a
  148. * depth-first manner.
  149. */
  150. void pci_stop_bus_device(struct pci_dev *dev)
  151. {
  152. if (dev->subordinate)
  153. pci_stop_bus_devices(dev->subordinate);
  154. pci_stop_dev(dev);
  155. }
  156. EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
  157. EXPORT_SYMBOL(pci_stop_and_remove_behind_bridge);
  158. EXPORT_SYMBOL_GPL(pci_stop_bus_device);