1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2020
6 * Pierre Morel <pmorel@linux.ibm.com>
10 #define KMSG_COMPONENT "zpci"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/delay.h>
18 #include <linux/seq_file.h>
19 #include <linux/jump_label.h>
20 #include <linux/pci.h>
21 #include <linux/printk.h>
23 #include <asm/pci_clp.h>
24 #include <asm/pci_dma.h>
29 static LIST_HEAD(zbus_list);
30 static DEFINE_MUTEX(zbus_list_lock);
31 static int zpci_nb_devices;
33 /* zpci_bus_prepare_device - Prepare a zPCI function for scanning
34 * @zdev: the zPCI function to be prepared
36 * The PCI resources for the function are set up and added to its zbus and the
37 * function is enabled. The function must be added to a zbus which must have
38 * a PCI bus created. If an error occurs the zPCI function is not enabled.
40 * Return: 0 on success, an error code otherwise
42 static int zpci_bus_prepare_device(struct zpci_dev *zdev)
46 if (!zdev_enabled(zdev)) {
47 rc = zpci_enable_device(zdev);
52 if (!zdev->has_resources) {
53 zpci_setup_bus_resources(zdev);
54 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
55 if (zdev->bars[i].res)
56 pci_bus_add_resource(zdev->zbus->bus, zdev->bars[i].res, 0);
63 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
64 * @zdev: the zdev to be scanned
66 * Scans the PCI function making it available to the common PCI code.
68 * Return: 0 on success, an error value otherwise
70 int zpci_bus_scan_device(struct zpci_dev *zdev)
75 rc = zpci_bus_prepare_device(zdev);
79 pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
83 pci_lock_rescan_remove();
84 pci_bus_add_device(pdev);
85 pci_unlock_rescan_remove();
90 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
91 * @zdev: the zdev to be removed from the PCI core
92 * @set_error: if true the device's error state is set to permanent failure
94 * Sets a zPCI device to a configured but offline state; the zPCI
95 * device is still accessible through its hotplug slot and the zPCI
96 * API but is removed from the common code PCI bus, making it
97 * no longer available to drivers.
99 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
101 struct zpci_bus *zbus = zdev->zbus;
102 struct pci_dev *pdev;
104 if (!zdev->zbus->bus)
107 pdev = pci_get_slot(zbus->bus, zdev->devfn);
110 pdev->error_state = pci_channel_io_perm_failure;
111 if (pdev->is_virtfn) {
112 zpci_iov_remove_virtfn(pdev, zdev->vfn);
113 /* balance pci_get_slot */
117 pci_stop_and_remove_bus_device_locked(pdev);
118 /* balance pci_get_slot */
123 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
124 * @zbus: the zbus to be scanned
126 * Enables and scans all PCI functions on the bus making them available to the
127 * common PCI code. If a PCI function fails to be initialized an error will be
128 * returned but attempts will still be made for all other functions on the bus.
130 * Return: 0 on success, an error value otherwise
132 int zpci_bus_scan_bus(struct zpci_bus *zbus)
134 struct zpci_dev *zdev;
135 int devfn, rc, ret = 0;
137 for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
138 zdev = zbus->function[devfn];
139 if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
140 rc = zpci_bus_prepare_device(zdev);
146 pci_lock_rescan_remove();
147 pci_scan_child_bus(zbus->bus);
148 pci_bus_add_devices(zbus->bus);
149 pci_unlock_rescan_remove();
154 /* zpci_bus_scan_busses - Scan all registered busses
156 * Scan all available zbusses
159 void zpci_bus_scan_busses(void)
161 struct zpci_bus *zbus = NULL;
163 mutex_lock(&zbus_list_lock);
164 list_for_each_entry(zbus, &zbus_list, bus_next) {
165 zpci_bus_scan_bus(zbus);
168 mutex_unlock(&zbus_list_lock);
171 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
172 * @zbus: the zbus holding the zdevices
173 * @fr: PCI root function that will determine the bus's domain, and bus speeed
174 * @ops: the pci operations
176 * The PCI function @fr determines the domain (its UID), multifunction property
177 * and maximum bus speed of the entire bus.
179 * Return: 0 on success, an error code otherwise
181 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
186 domain = zpci_alloc_domain((u16)fr->uid);
190 zbus->domain_nr = domain;
191 zbus->multifunction = fr->rid_available;
192 zbus->max_bus_speed = fr->max_bus_speed;
195 * Note that the zbus->resources are taken over and zbus->resources
196 * is empty after a successful call
198 bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
200 zpci_free_domain(zbus->domain_nr);
209 static void zpci_bus_release(struct kref *kref)
211 struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
214 pci_lock_rescan_remove();
215 pci_stop_root_bus(zbus->bus);
217 zpci_free_domain(zbus->domain_nr);
218 pci_free_resource_list(&zbus->resources);
220 pci_remove_root_bus(zbus->bus);
221 pci_unlock_rescan_remove();
224 mutex_lock(&zbus_list_lock);
225 list_del(&zbus->bus_next);
226 mutex_unlock(&zbus_list_lock);
230 static void zpci_bus_put(struct zpci_bus *zbus)
232 kref_put(&zbus->kref, zpci_bus_release);
235 static struct zpci_bus *zpci_bus_get(int pchid)
237 struct zpci_bus *zbus;
239 mutex_lock(&zbus_list_lock);
240 list_for_each_entry(zbus, &zbus_list, bus_next) {
241 if (pchid == zbus->pchid) {
242 kref_get(&zbus->kref);
248 mutex_unlock(&zbus_list_lock);
252 static struct zpci_bus *zpci_bus_alloc(int pchid)
254 struct zpci_bus *zbus;
256 zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
261 INIT_LIST_HEAD(&zbus->bus_next);
262 mutex_lock(&zbus_list_lock);
263 list_add_tail(&zbus->bus_next, &zbus_list);
264 mutex_unlock(&zbus_list_lock);
266 kref_init(&zbus->kref);
267 INIT_LIST_HEAD(&zbus->resources);
269 zbus->bus_resource.start = 0;
270 zbus->bus_resource.end = ZPCI_BUS_NR;
271 zbus->bus_resource.flags = IORESOURCE_BUS;
272 pci_add_resource(&zbus->resources, &zbus->bus_resource);
277 void pcibios_bus_add_device(struct pci_dev *pdev)
279 struct zpci_dev *zdev = to_zpci(pdev);
282 * With pdev->no_vf_scan the common PCI probing code does not
283 * perform PF/VF linking.
286 zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
287 pdev->no_command_memory = 1;
291 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
295 if (zbus->function[zdev->devfn]) {
296 pr_err("devfn %04x is already assigned\n", zdev->devfn);
301 zbus->function[zdev->devfn] = zdev;
304 if (zbus->multifunction && !zdev->rid_available) {
305 WARN_ONCE(1, "rid_available not set for multifunction\n");
308 rc = zpci_init_slot(zdev);
311 zdev->has_hp_slot = 1;
316 zbus->function[zdev->devfn] = NULL;
322 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
324 struct zpci_bus *zbus = NULL;
327 if (zpci_nb_devices == ZPCI_NR_DEVICES) {
328 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
329 zdev->fid, ZPCI_NR_DEVICES);
333 if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
336 if (!s390_pci_no_rid && zdev->rid_available)
337 zbus = zpci_bus_get(zdev->pchid);
340 zbus = zpci_bus_alloc(zdev->pchid);
346 /* The UID of the first PCI function registered with a zpci_bus
347 * is used as the domain number for that bus. Currently there
348 * is exactly one zpci_bus per domain.
350 rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
355 rc = zpci_bus_add_device(zbus, zdev);
362 pr_err("Adding PCI function %08x failed\n", zdev->fid);
367 void zpci_bus_device_unregister(struct zpci_dev *zdev)
369 struct zpci_bus *zbus = zdev->zbus;
372 zbus->function[zdev->devfn] = NULL;