1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pci.h> 3 #include <linux/module.h> 4 #include <linux/pci-aspm.h> 5 #include "pci.h" 6 7 static void pci_free_resources(struct pci_dev *dev) 8 { 9 int i; 10 11 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 12 struct resource *res = dev->resource + i; 13 if (res->parent) 14 release_resource(res); 15 } 16 } 17 18 static void pci_stop_dev(struct pci_dev *dev) 19 { 20 pci_pme_active(dev, false); 21 22 if (pci_dev_is_added(dev)) { 23 device_release_driver(&dev->dev); 24 pci_proc_detach_device(dev); 25 pci_remove_sysfs_dev_files(dev); 26 27 pci_dev_assign_added(dev, false); 28 } 29 30 if (dev->bus->self) 31 pcie_aspm_exit_link_state(dev); 32 } 33 34 static void pci_destroy_dev(struct pci_dev *dev) 35 { 36 if (!dev->dev.kobj.parent) 37 return; 38 39 device_del(&dev->dev); 40 41 down_write(&pci_bus_sem); 42 list_del(&dev->bus_list); 43 up_write(&pci_bus_sem); 44 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 pci_remove_bus(bus); 163 host_bridge->bus = NULL; 164 165 /* remove the host bridge */ 166 device_unregister(&host_bridge->dev); 167 } 168 EXPORT_SYMBOL_GPL(pci_remove_root_bus); 169