remove.c 3.7 KB

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