xref: /OK3568_Linux_fs/kernel/drivers/pci/hotplug/pciehp_pci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PCI Express Hot Plug Controller Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 1995,2001 Compaq Computer Corporation
6*4882a593Smuzhiyun  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7*4882a593Smuzhiyun  * Copyright (C) 2001 IBM Corp.
8*4882a593Smuzhiyun  * Copyright (C) 2003-2004 Intel Corporation
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * All rights reserved.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define dev_fmt(fmt) "pciehp: " fmt
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/types.h>
20*4882a593Smuzhiyun #include <linux/pci.h>
21*4882a593Smuzhiyun #include "../pci.h"
22*4882a593Smuzhiyun #include "pciehp.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun  * pciehp_configure_device() - enumerate PCI devices below a hotplug bridge
26*4882a593Smuzhiyun  * @ctrl: PCIe hotplug controller
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Enumerate PCI devices below a hotplug bridge and add them to the system.
29*4882a593Smuzhiyun  * Return 0 on success, %-EEXIST if the devices are already enumerated or
30*4882a593Smuzhiyun  * %-ENODEV if enumeration failed.
31*4882a593Smuzhiyun  */
pciehp_configure_device(struct controller * ctrl)32*4882a593Smuzhiyun int pciehp_configure_device(struct controller *ctrl)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	struct pci_dev *dev;
35*4882a593Smuzhiyun 	struct pci_dev *bridge = ctrl->pcie->port;
36*4882a593Smuzhiyun 	struct pci_bus *parent = bridge->subordinate;
37*4882a593Smuzhiyun 	int num, ret = 0;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	pci_lock_rescan_remove();
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	dev = pci_get_slot(parent, PCI_DEVFN(0, 0));
42*4882a593Smuzhiyun 	if (dev) {
43*4882a593Smuzhiyun 		/*
44*4882a593Smuzhiyun 		 * The device is already there. Either configured by the
45*4882a593Smuzhiyun 		 * boot firmware or a previous hotplug event.
46*4882a593Smuzhiyun 		 */
47*4882a593Smuzhiyun 		ctrl_dbg(ctrl, "Device %s already exists at %04x:%02x:00, skipping hot-add\n",
48*4882a593Smuzhiyun 			 pci_name(dev), pci_domain_nr(parent), parent->number);
49*4882a593Smuzhiyun 		pci_dev_put(dev);
50*4882a593Smuzhiyun 		ret = -EEXIST;
51*4882a593Smuzhiyun 		goto out;
52*4882a593Smuzhiyun 	}
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	num = pci_scan_slot(parent, PCI_DEVFN(0, 0));
55*4882a593Smuzhiyun 	if (num == 0) {
56*4882a593Smuzhiyun 		ctrl_err(ctrl, "No new device found\n");
57*4882a593Smuzhiyun 		ret = -ENODEV;
58*4882a593Smuzhiyun 		goto out;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	for_each_pci_bridge(dev, parent)
62*4882a593Smuzhiyun 		pci_hp_add_bridge(dev);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	pci_assign_unassigned_bridge_resources(bridge);
65*4882a593Smuzhiyun 	pcie_bus_configure_settings(parent);
66*4882a593Smuzhiyun 	pci_bus_add_devices(parent);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun  out:
69*4882a593Smuzhiyun 	pci_unlock_rescan_remove();
70*4882a593Smuzhiyun 	return ret;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * pciehp_unconfigure_device() - remove PCI devices below a hotplug bridge
75*4882a593Smuzhiyun  * @ctrl: PCIe hotplug controller
76*4882a593Smuzhiyun  * @presence: whether the card is still present in the slot;
77*4882a593Smuzhiyun  *	true for safe removal via sysfs or an Attention Button press,
78*4882a593Smuzhiyun  *	false for surprise removal
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * Unbind PCI devices below a hotplug bridge from their drivers and remove
81*4882a593Smuzhiyun  * them from the system.  Safely removed devices are quiesced.  Surprise
82*4882a593Smuzhiyun  * removed devices are marked as such to prevent further accesses.
83*4882a593Smuzhiyun  */
pciehp_unconfigure_device(struct controller * ctrl,bool presence)84*4882a593Smuzhiyun void pciehp_unconfigure_device(struct controller *ctrl, bool presence)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct pci_dev *dev, *temp;
87*4882a593Smuzhiyun 	struct pci_bus *parent = ctrl->pcie->port->subordinate;
88*4882a593Smuzhiyun 	u16 command;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:00\n",
91*4882a593Smuzhiyun 		 __func__, pci_domain_nr(parent), parent->number);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (!presence)
94*4882a593Smuzhiyun 		pci_walk_bus(parent, pci_dev_set_disconnected, NULL);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	pci_lock_rescan_remove();
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/*
99*4882a593Smuzhiyun 	 * Stopping an SR-IOV PF device removes all the associated VFs,
100*4882a593Smuzhiyun 	 * which will update the bus->devices list and confuse the
101*4882a593Smuzhiyun 	 * iterator.  Therefore, iterate in reverse so we remove the VFs
102*4882a593Smuzhiyun 	 * first, then the PF.  We do the same in pci_stop_bus_device().
103*4882a593Smuzhiyun 	 */
104*4882a593Smuzhiyun 	list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
105*4882a593Smuzhiyun 					 bus_list) {
106*4882a593Smuzhiyun 		pci_dev_get(dev);
107*4882a593Smuzhiyun 		pci_stop_and_remove_bus_device(dev);
108*4882a593Smuzhiyun 		/*
109*4882a593Smuzhiyun 		 * Ensure that no new Requests will be generated from
110*4882a593Smuzhiyun 		 * the device.
111*4882a593Smuzhiyun 		 */
112*4882a593Smuzhiyun 		if (presence) {
113*4882a593Smuzhiyun 			pci_read_config_word(dev, PCI_COMMAND, &command);
114*4882a593Smuzhiyun 			command &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
115*4882a593Smuzhiyun 			command |= PCI_COMMAND_INTX_DISABLE;
116*4882a593Smuzhiyun 			pci_write_config_word(dev, PCI_COMMAND, command);
117*4882a593Smuzhiyun 		}
118*4882a593Smuzhiyun 		pci_dev_put(dev);
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	pci_unlock_rescan_remove();
122*4882a593Smuzhiyun }
123