blob: ec4a82ba29a8d04b6739462e846395388cace68d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/pci.h>
2#include <linux/module.h>
Shaohua Li6c723d52008-01-24 10:21:57 +08003#include <linux/aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include "pci.h"
5
6static void pci_free_resources(struct pci_dev *dev)
7{
8 int i;
9
10 msi_remove_pci_irq_vectors(dev);
11
12 pci_cleanup_rom(dev);
13 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
14 struct resource *res = dev->resource + i;
15 if (res->parent)
16 release_resource(res);
17 }
18}
19
Satoru Takeuchi24f8aa92006-09-12 10:16:36 -070020static void pci_stop_dev(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Satoru Takeuchi24f8aa92006-09-12 10:16:36 -070022 if (!dev->global_list.next)
23 return;
24
Rajesh Shah091ca9f2005-04-28 00:25:49 -070025 if (!list_empty(&dev->global_list)) {
26 pci_proc_detach_device(dev);
27 pci_remove_sysfs_dev_files(dev);
28 device_unregister(&dev->dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +080029 down_write(&pci_bus_sem);
Rajesh Shah091ca9f2005-04-28 00:25:49 -070030 list_del(&dev->global_list);
31 dev->global_list.next = dev->global_list.prev = NULL;
Zhang Yanmind71374d2006-06-02 12:35:43 +080032 up_write(&pci_bus_sem);
Rajesh Shah091ca9f2005-04-28 00:25:49 -070033 }
Shaohua Li6c723d52008-01-24 10:21:57 +080034
35 if (dev->bus->self)
36 pcie_aspm_exit_link_state(dev);
Satoru Takeuchi24f8aa92006-09-12 10:16:36 -070037}
38
39static void pci_destroy_dev(struct pci_dev *dev)
40{
41 pci_stop_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 /* Remove the device from the device lists, and prevent any further
44 * list accesses from this device */
Zhang Yanmind71374d2006-06-02 12:35:43 +080045 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 list_del(&dev->bus_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 dev->bus_list.next = dev->bus_list.prev = NULL;
Zhang Yanmind71374d2006-06-02 12:35:43 +080048 up_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 pci_free_resources(dev);
51 pci_dev_put(dev);
52}
53
54/**
55 * pci_remove_device_safe - remove an unused hotplug device
56 * @dev: the device to remove
57 *
58 * Delete the device structure from the device lists and
59 * notify userspace (/sbin/hotplug), but only if the device
60 * in question is not being used by a driver.
61 * Returns 0 on success.
62 */
Adrian Bunk54c762f2005-12-22 01:08:52 +010063#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070064int pci_remove_device_safe(struct pci_dev *dev)
65{
66 if (pci_dev_driver(dev))
67 return -EBUSY;
68 pci_destroy_dev(dev);
69 return 0;
70}
Adrian Bunk54c762f2005-12-22 01:08:52 +010071#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73void pci_remove_bus(struct pci_bus *pci_bus)
74{
75 pci_proc_detach_bus(pci_bus);
76
Zhang Yanmind71374d2006-06-02 12:35:43 +080077 down_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 list_del(&pci_bus->node);
Zhang Yanmind71374d2006-06-02 12:35:43 +080079 up_write(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pci_remove_legacy_files(pci_bus);
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -040081 device_remove_file(&pci_bus->dev, &dev_attr_cpuaffinity);
82 device_unregister(&pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84EXPORT_SYMBOL(pci_remove_bus);
85
86/**
87 * pci_remove_bus_device - remove a PCI device and any children
88 * @dev: the device to remove
89 *
90 * Remove a PCI device from the device lists, informing the drivers
91 * that the device has been removed. We also remove any subordinate
92 * buses and children in a depth-first manner.
93 *
94 * For each device we remove, delete the device structure from the
95 * device lists, remove the /proc entry, and notify userspace
96 * (/sbin/hotplug).
97 */
98void pci_remove_bus_device(struct pci_dev *dev)
99{
100 if (dev->subordinate) {
101 struct pci_bus *b = dev->subordinate;
102
103 pci_remove_behind_bridge(dev);
104 pci_remove_bus(b);
105 dev->subordinate = NULL;
106 }
107
108 pci_destroy_dev(dev);
109}
110
111/**
112 * pci_remove_behind_bridge - remove all devices behind a PCI bridge
113 * @dev: PCI bridge device
114 *
115 * Remove all devices on the bus, except for the parent bridge.
116 * This also removes any child buses, and any devices they may
117 * contain in a depth-first manner.
118 */
119void pci_remove_behind_bridge(struct pci_dev *dev)
120{
121 struct list_head *l, *n;
122
123 if (dev->subordinate) {
124 list_for_each_safe(l, n, &dev->subordinate->devices) {
125 struct pci_dev *dev = pci_dev_b(l);
126
127 pci_remove_bus_device(dev);
128 }
129 }
130}
131
Satoru Takeuchi24f8aa92006-09-12 10:16:36 -0700132static void pci_stop_bus_devices(struct pci_bus *bus)
133{
134 struct list_head *l, *n;
135
136 list_for_each_safe(l, n, &bus->devices) {
137 struct pci_dev *dev = pci_dev_b(l);
138 pci_stop_bus_device(dev);
139 }
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 */
150void pci_stop_bus_device(struct pci_dev *dev)
151{
152 if (dev->subordinate)
153 pci_stop_bus_devices(dev->subordinate);
154
155 pci_stop_dev(dev);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158EXPORT_SYMBOL(pci_remove_bus_device);
159EXPORT_SYMBOL(pci_remove_behind_bridge);
Satoru Takeuchi24f8aa92006-09-12 10:16:36 -0700160EXPORT_SYMBOL_GPL(pci_stop_bus_device);