xref: /linux/drivers/pci/remove.c (revision eba6d0f88ba2c4e9175aae8556125a05980ff8f5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/pci.h>
3 #include <linux/module.h>
4 #include <linux/of_platform.h>
5 #include "pci.h"
6 
7 static void pci_free_resources(struct pci_dev *dev)
8 {
9 	struct resource *res;
10 
11 	pci_dev_for_each_resource(dev, res) {
12 		if (res->parent)
13 			release_resource(res);
14 	}
15 }
16 
17 static void pci_stop_dev(struct pci_dev *dev)
18 {
19 	pci_pme_active(dev, false);
20 
21 	if (pci_dev_is_added(dev)) {
22 		of_platform_depopulate(&dev->dev);
23 		device_release_driver(&dev->dev);
24 		pci_proc_detach_device(dev);
25 		pci_remove_sysfs_dev_files(dev);
26 		of_pci_remove_node(dev);
27 
28 		pci_dev_assign_added(dev, false);
29 	}
30 }
31 
32 static void pci_destroy_dev(struct pci_dev *dev)
33 {
34 	if (!dev->dev.kobj.parent)
35 		return;
36 
37 	device_del(&dev->dev);
38 
39 	down_write(&pci_bus_sem);
40 	list_del(&dev->bus_list);
41 	up_write(&pci_bus_sem);
42 
43 	pci_doe_destroy(dev);
44 	pcie_aspm_exit_link_state(dev);
45 	pci_bridge_d3_update(dev);
46 	pci_free_resources(dev);
47 	put_device(&dev->dev);
48 }
49 
50 void pci_remove_bus(struct pci_bus *bus)
51 {
52 	pci_proc_detach_bus(bus);
53 
54 	down_write(&pci_bus_sem);
55 	list_del(&bus->node);
56 	pci_bus_release_busn_res(bus);
57 	up_write(&pci_bus_sem);
58 	pci_remove_legacy_files(bus);
59 
60 	if (bus->ops->remove_bus)
61 		bus->ops->remove_bus(bus);
62 
63 	pcibios_remove_bus(bus);
64 	device_unregister(&bus->dev);
65 }
66 EXPORT_SYMBOL(pci_remove_bus);
67 
68 static void pci_stop_bus_device(struct pci_dev *dev)
69 {
70 	struct pci_bus *bus = dev->subordinate;
71 	struct pci_dev *child, *tmp;
72 
73 	/*
74 	 * Stopping an SR-IOV PF device removes all the associated VFs,
75 	 * which will update the bus->devices list and confuse the
76 	 * iterator.  Therefore, iterate in reverse so we remove the VFs
77 	 * first, then the PF.
78 	 */
79 	if (bus) {
80 		list_for_each_entry_safe_reverse(child, tmp,
81 						 &bus->devices, bus_list)
82 			pci_stop_bus_device(child);
83 	}
84 
85 	pci_stop_dev(dev);
86 }
87 
88 static void pci_remove_bus_device(struct pci_dev *dev)
89 {
90 	struct pci_bus *bus = dev->subordinate;
91 	struct pci_dev *child, *tmp;
92 
93 	if (bus) {
94 		list_for_each_entry_safe(child, tmp,
95 					 &bus->devices, bus_list)
96 			pci_remove_bus_device(child);
97 
98 		pci_remove_bus(bus);
99 		dev->subordinate = NULL;
100 	}
101 
102 	pci_destroy_dev(dev);
103 }
104 
105 /**
106  * pci_stop_and_remove_bus_device - remove a PCI device and any children
107  * @dev: the device to remove
108  *
109  * Remove a PCI device from the device lists, informing the drivers
110  * that the device has been removed.  We also remove any subordinate
111  * buses and children in a depth-first manner.
112  *
113  * For each device we remove, delete the device structure from the
114  * device lists, remove the /proc entry, and notify userspace
115  * (/sbin/hotplug).
116  */
117 void pci_stop_and_remove_bus_device(struct pci_dev *dev)
118 {
119 	pci_stop_bus_device(dev);
120 	pci_remove_bus_device(dev);
121 }
122 EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
123 
124 void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
125 {
126 	pci_lock_rescan_remove();
127 	pci_stop_and_remove_bus_device(dev);
128 	pci_unlock_rescan_remove();
129 }
130 EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
131 
132 void pci_stop_root_bus(struct pci_bus *bus)
133 {
134 	struct pci_dev *child, *tmp;
135 	struct pci_host_bridge *host_bridge;
136 
137 	if (!pci_is_root_bus(bus))
138 		return;
139 
140 	host_bridge = to_pci_host_bridge(bus->bridge);
141 	list_for_each_entry_safe_reverse(child, tmp,
142 					 &bus->devices, bus_list)
143 		pci_stop_bus_device(child);
144 
145 	/* stop the host bridge */
146 	device_release_driver(&host_bridge->dev);
147 }
148 EXPORT_SYMBOL_GPL(pci_stop_root_bus);
149 
150 void pci_remove_root_bus(struct pci_bus *bus)
151 {
152 	struct pci_dev *child, *tmp;
153 	struct pci_host_bridge *host_bridge;
154 
155 	if (!pci_is_root_bus(bus))
156 		return;
157 
158 	host_bridge = to_pci_host_bridge(bus->bridge);
159 	list_for_each_entry_safe(child, tmp,
160 				 &bus->devices, bus_list)
161 		pci_remove_bus_device(child);
162 
163 #ifdef CONFIG_PCI_DOMAINS_GENERIC
164 	/* Release domain_nr if it was dynamically allocated */
165 	if (host_bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
166 		pci_bus_release_domain_nr(bus, host_bridge->dev.parent);
167 #endif
168 
169 	pci_remove_bus(bus);
170 	host_bridge->bus = NULL;
171 
172 	/* remove the host bridge */
173 	device_del(&host_bridge->dev);
174 }
175 EXPORT_SYMBOL_GPL(pci_remove_root_bus);
176