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