1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Standard Hot Plug Controller Driver
4  *
5  * Copyright (C) 1995,2001 Compaq Computer Corporation
6  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (C) 2001 IBM Corp.
8  * Copyright (C) 2003-2004 Intel Corporation
9  *
10  * All rights reserved.
11  *
12  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/pci.h>
20 #include "../pci.h"
21 #include "shpchp.h"
22 
shpchp_configure_device(struct slot * p_slot)23 int shpchp_configure_device(struct slot *p_slot)
24 {
25 	struct pci_dev *dev;
26 	struct controller *ctrl = p_slot->ctrl;
27 	struct pci_dev *bridge = ctrl->pci_dev;
28 	struct pci_bus *parent = bridge->subordinate;
29 	int num, ret = 0;
30 
31 	pci_lock_rescan_remove();
32 
33 	dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, 0));
34 	if (dev) {
35 		ctrl_err(ctrl, "Device %s already exists at %04x:%02x:%02x, cannot hot-add\n",
36 			 pci_name(dev), pci_domain_nr(parent),
37 			 p_slot->bus, p_slot->device);
38 		pci_dev_put(dev);
39 		ret = -EINVAL;
40 		goto out;
41 	}
42 
43 	num = pci_scan_slot(parent, PCI_DEVFN(p_slot->device, 0));
44 	if (num == 0) {
45 		ctrl_err(ctrl, "No new device found\n");
46 		ret = -ENODEV;
47 		goto out;
48 	}
49 
50 	for_each_pci_bridge(dev, parent) {
51 		if (PCI_SLOT(dev->devfn) == p_slot->device)
52 			pci_hp_add_bridge(dev);
53 	}
54 
55 	pci_assign_unassigned_bridge_resources(bridge);
56 	pcie_bus_configure_settings(parent);
57 	pci_bus_add_devices(parent);
58 
59  out:
60 	pci_unlock_rescan_remove();
61 	return ret;
62 }
63 
shpchp_unconfigure_device(struct slot * p_slot)64 int shpchp_unconfigure_device(struct slot *p_slot)
65 {
66 	int rc = 0;
67 	struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
68 	struct pci_dev *dev, *temp;
69 	struct controller *ctrl = p_slot->ctrl;
70 
71 	ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:%02x\n",
72 		 __func__, pci_domain_nr(parent), p_slot->bus, p_slot->device);
73 
74 	pci_lock_rescan_remove();
75 
76 	list_for_each_entry_safe(dev, temp, &parent->devices, bus_list) {
77 		if (PCI_SLOT(dev->devfn) != p_slot->device)
78 			continue;
79 
80 		pci_dev_get(dev);
81 		pci_stop_and_remove_bus_device(dev);
82 		pci_dev_put(dev);
83 	}
84 
85 	pci_unlock_rescan_remove();
86 	return rc;
87 }
88 
89