18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/pci.h>
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include "pci.h"
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_cistatic void pci_free_resources(struct pci_dev *dev)
78c2ecf20Sopenharmony_ci{
88c2ecf20Sopenharmony_ci	int i;
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
118c2ecf20Sopenharmony_ci		struct resource *res = dev->resource + i;
128c2ecf20Sopenharmony_ci		if (res->parent)
138c2ecf20Sopenharmony_ci			release_resource(res);
148c2ecf20Sopenharmony_ci	}
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic void pci_stop_dev(struct pci_dev *dev)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	pci_pme_active(dev, false);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	if (pci_dev_is_added(dev)) {
228c2ecf20Sopenharmony_ci		device_release_driver(&dev->dev);
238c2ecf20Sopenharmony_ci		pci_proc_detach_device(dev);
248c2ecf20Sopenharmony_ci		pci_remove_sysfs_dev_files(dev);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci		pci_dev_assign_added(dev, false);
278c2ecf20Sopenharmony_ci	}
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void pci_destroy_dev(struct pci_dev *dev)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	if (!dev->dev.kobj.parent)
338c2ecf20Sopenharmony_ci		return;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	device_del(&dev->dev);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	down_write(&pci_bus_sem);
388c2ecf20Sopenharmony_ci	list_del(&dev->bus_list);
398c2ecf20Sopenharmony_ci	up_write(&pci_bus_sem);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	pcie_aspm_exit_link_state(dev);
428c2ecf20Sopenharmony_ci	pci_bridge_d3_update(dev);
438c2ecf20Sopenharmony_ci	pci_free_resources(dev);
448c2ecf20Sopenharmony_ci	put_device(&dev->dev);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_civoid pci_remove_bus(struct pci_bus *bus)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	pci_proc_detach_bus(bus);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	down_write(&pci_bus_sem);
528c2ecf20Sopenharmony_ci	list_del(&bus->node);
538c2ecf20Sopenharmony_ci	pci_bus_release_busn_res(bus);
548c2ecf20Sopenharmony_ci	up_write(&pci_bus_sem);
558c2ecf20Sopenharmony_ci	pci_remove_legacy_files(bus);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (bus->ops->remove_bus)
588c2ecf20Sopenharmony_ci		bus->ops->remove_bus(bus);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	pcibios_remove_bus(bus);
618c2ecf20Sopenharmony_ci	device_unregister(&bus->dev);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pci_remove_bus);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void pci_stop_bus_device(struct pci_dev *dev)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct pci_bus *bus = dev->subordinate;
688c2ecf20Sopenharmony_ci	struct pci_dev *child, *tmp;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * Stopping an SR-IOV PF device removes all the associated VFs,
728c2ecf20Sopenharmony_ci	 * which will update the bus->devices list and confuse the
738c2ecf20Sopenharmony_ci	 * iterator.  Therefore, iterate in reverse so we remove the VFs
748c2ecf20Sopenharmony_ci	 * first, then the PF.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	if (bus) {
778c2ecf20Sopenharmony_ci		list_for_each_entry_safe_reverse(child, tmp,
788c2ecf20Sopenharmony_ci						 &bus->devices, bus_list)
798c2ecf20Sopenharmony_ci			pci_stop_bus_device(child);
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	pci_stop_dev(dev);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void pci_remove_bus_device(struct pci_dev *dev)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct pci_bus *bus = dev->subordinate;
888c2ecf20Sopenharmony_ci	struct pci_dev *child, *tmp;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (bus) {
918c2ecf20Sopenharmony_ci		list_for_each_entry_safe(child, tmp,
928c2ecf20Sopenharmony_ci					 &bus->devices, bus_list)
938c2ecf20Sopenharmony_ci			pci_remove_bus_device(child);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		pci_remove_bus(bus);
968c2ecf20Sopenharmony_ci		dev->subordinate = NULL;
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	pci_destroy_dev(dev);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/**
1038c2ecf20Sopenharmony_ci * pci_stop_and_remove_bus_device - remove a PCI device and any children
1048c2ecf20Sopenharmony_ci * @dev: the device to remove
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * Remove a PCI device from the device lists, informing the drivers
1078c2ecf20Sopenharmony_ci * that the device has been removed.  We also remove any subordinate
1088c2ecf20Sopenharmony_ci * buses and children in a depth-first manner.
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * For each device we remove, delete the device structure from the
1118c2ecf20Sopenharmony_ci * device lists, remove the /proc entry, and notify userspace
1128c2ecf20Sopenharmony_ci * (/sbin/hotplug).
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_civoid pci_stop_and_remove_bus_device(struct pci_dev *dev)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	pci_stop_bus_device(dev);
1178c2ecf20Sopenharmony_ci	pci_remove_bus_device(dev);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pci_stop_and_remove_bus_device);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_civoid pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	pci_lock_rescan_remove();
1248c2ecf20Sopenharmony_ci	pci_stop_and_remove_bus_device(dev);
1258c2ecf20Sopenharmony_ci	pci_unlock_rescan_remove();
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_civoid pci_stop_root_bus(struct pci_bus *bus)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct pci_dev *child, *tmp;
1328c2ecf20Sopenharmony_ci	struct pci_host_bridge *host_bridge;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (!pci_is_root_bus(bus))
1358c2ecf20Sopenharmony_ci		return;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	host_bridge = to_pci_host_bridge(bus->bridge);
1388c2ecf20Sopenharmony_ci	list_for_each_entry_safe_reverse(child, tmp,
1398c2ecf20Sopenharmony_ci					 &bus->devices, bus_list)
1408c2ecf20Sopenharmony_ci		pci_stop_bus_device(child);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* stop the host bridge */
1438c2ecf20Sopenharmony_ci	device_release_driver(&host_bridge->dev);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_stop_root_bus);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_civoid pci_remove_root_bus(struct pci_bus *bus)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct pci_dev *child, *tmp;
1508c2ecf20Sopenharmony_ci	struct pci_host_bridge *host_bridge;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (!pci_is_root_bus(bus))
1538c2ecf20Sopenharmony_ci		return;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	host_bridge = to_pci_host_bridge(bus->bridge);
1568c2ecf20Sopenharmony_ci	list_for_each_entry_safe(child, tmp,
1578c2ecf20Sopenharmony_ci				 &bus->devices, bus_list)
1588c2ecf20Sopenharmony_ci		pci_remove_bus_device(child);
1598c2ecf20Sopenharmony_ci	pci_remove_bus(bus);
1608c2ecf20Sopenharmony_ci	host_bridge->bus = NULL;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/* remove the host bridge */
1638c2ecf20Sopenharmony_ci	device_del(&host_bridge->dev);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_remove_root_bus);
166