remove.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <linux/pci.h>
  2. #include <linux/module.h>
  3. #include <linux/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->global_list.next)
  19. return;
  20. if (!list_empty(&dev->global_list)) {
  21. pci_proc_detach_device(dev);
  22. pci_remove_sysfs_dev_files(dev);
  23. device_unregister(&dev->dev);
  24. down_write(&pci_bus_sem);
  25. list_del(&dev->global_list);
  26. dev->global_list.next = dev->global_list.prev = NULL;
  27. up_write(&pci_bus_sem);
  28. }
  29. if (dev->bus->self)
  30. pcie_aspm_exit_link_state(dev);
  31. }
  32. static void pci_destroy_dev(struct pci_dev *dev)
  33. {
  34. pci_stop_dev(dev);
  35. /* Remove the device from the device lists, and prevent any further
  36. * list accesses from this device */
  37. down_write(&pci_bus_sem);
  38. list_del(&dev->bus_list);
  39. dev->bus_list.next = dev->bus_list.prev = NULL;
  40. up_write(&pci_bus_sem);
  41. pci_free_resources(dev);
  42. pci_dev_put(dev);
  43. }
  44. /**
  45. * pci_remove_device_safe - remove an unused hotplug device
  46. * @dev: the device to remove
  47. *
  48. * Delete the device structure from the device lists and
  49. * notify userspace (/sbin/hotplug), but only if the device
  50. * in question is not being used by a driver.
  51. * Returns 0 on success.
  52. */
  53. #if 0
  54. int pci_remove_device_safe(struct pci_dev *dev)
  55. {
  56. if (pci_dev_driver(dev))
  57. return -EBUSY;
  58. pci_destroy_dev(dev);
  59. return 0;
  60. }
  61. #endif /* 0 */
  62. void pci_remove_bus(struct pci_bus *pci_bus)
  63. {
  64. pci_proc_detach_bus(pci_bus);
  65. down_write(&pci_bus_sem);
  66. list_del(&pci_bus->node);
  67. up_write(&pci_bus_sem);
  68. pci_remove_legacy_files(pci_bus);
  69. class_device_remove_file(&pci_bus->class_dev,
  70. &class_device_attr_cpuaffinity);
  71. sysfs_remove_link(&pci_bus->class_dev.kobj, "bridge");
  72. class_device_unregister(&pci_bus->class_dev);
  73. }
  74. EXPORT_SYMBOL(pci_remove_bus);
  75. /**
  76. * pci_remove_bus_device - remove a PCI device and any children
  77. * @dev: the device to remove
  78. *
  79. * Remove a PCI device from the device lists, informing the drivers
  80. * that the device has been removed. We also remove any subordinate
  81. * buses and children in a depth-first manner.
  82. *
  83. * For each device we remove, delete the device structure from the
  84. * device lists, remove the /proc entry, and notify userspace
  85. * (/sbin/hotplug).
  86. */
  87. void pci_remove_bus_device(struct pci_dev *dev)
  88. {
  89. if (dev->subordinate) {
  90. struct pci_bus *b = dev->subordinate;
  91. pci_remove_behind_bridge(dev);
  92. pci_remove_bus(b);
  93. dev->subordinate = NULL;
  94. }
  95. pci_destroy_dev(dev);
  96. }
  97. /**
  98. * pci_remove_behind_bridge - remove all devices behind a PCI bridge
  99. * @dev: PCI bridge device
  100. *
  101. * Remove all devices on the bus, except for the parent bridge.
  102. * This also removes any child buses, and any devices they may
  103. * contain in a depth-first manner.
  104. */
  105. void pci_remove_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. struct pci_dev *dev = pci_dev_b(l);
  111. pci_remove_bus_device(dev);
  112. }
  113. }
  114. }
  115. static void pci_stop_bus_devices(struct pci_bus *bus)
  116. {
  117. struct list_head *l, *n;
  118. list_for_each_safe(l, n, &bus->devices) {
  119. struct pci_dev *dev = pci_dev_b(l);
  120. pci_stop_bus_device(dev);
  121. }
  122. }
  123. /**
  124. * pci_stop_bus_device - stop a PCI device and any children
  125. * @dev: the device to stop
  126. *
  127. * Stop a PCI device (detach the driver, remove from the global list
  128. * and so on). This also stop any subordinate buses and children in a
  129. * depth-first manner.
  130. */
  131. void pci_stop_bus_device(struct pci_dev *dev)
  132. {
  133. if (dev->subordinate)
  134. pci_stop_bus_devices(dev->subordinate);
  135. pci_stop_dev(dev);
  136. }
  137. EXPORT_SYMBOL(pci_remove_bus_device);
  138. EXPORT_SYMBOL(pci_remove_behind_bridge);
  139. EXPORT_SYMBOL_GPL(pci_stop_bus_device);