remove.c 3.6 KB

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