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)
44 struct resource_entry *window, *n;
48 if (!zdev_enabled(zdev)) {
49 rc = zpci_enable_device(zdev);
52 rc = zpci_dma_init_device(zdev);
54 zpci_disable_device(zdev);
59 if (!zdev->has_resources) {
60 zpci_setup_bus_resources(zdev, &zdev->zbus->resources);
61 resource_list_for_each_entry_safe(window, n, &zdev->zbus->resources) {
63 pci_bus_add_resource(zdev->zbus->bus, res, 0);
70 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
71 * @zdev: the zdev to be scanned
73 * Scans the PCI function making it available to the common PCI code.
75 * Return: 0 on success, an error value otherwise
77 int zpci_bus_scan_device(struct zpci_dev *zdev)
82 rc = zpci_bus_prepare_device(zdev);
86 pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
90 pci_bus_add_device(pdev);
91 pci_lock_rescan_remove();
92 pci_bus_add_devices(zdev->zbus->bus);
93 pci_unlock_rescan_remove();
98 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
99 * @zdev: the zdev to be removed from the PCI core
100 * @set_error: if true the device's error state is set to permanent failure
102 * Sets a zPCI device to a configured but offline state; the zPCI
103 * device is still accessible through its hotplug slot and the zPCI
104 * API but is removed from the common code PCI bus, making it
105 * no longer available to drivers.
107 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
109 struct zpci_bus *zbus = zdev->zbus;
110 struct pci_dev *pdev;
112 if (!zdev->zbus->bus)
115 pdev = pci_get_slot(zbus->bus, zdev->devfn);
118 pdev->error_state = pci_channel_io_perm_failure;
119 if (pdev->is_virtfn) {
120 zpci_iov_remove_virtfn(pdev, zdev->vfn);
121 /* balance pci_get_slot */
125 pci_stop_and_remove_bus_device_locked(pdev);
126 /* balance pci_get_slot */
131 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
132 * @zbus: the zbus to be scanned
134 * Enables and scans all PCI functions on the bus making them available to the
135 * common PCI code. If there is no function 0 on the zbus nothing is scanned. If
136 * a function does not have a slot yet because it was added to the zbus before
137 * function 0 the slot is created. If a PCI function fails to be initialized
138 * an error will be returned but attempts will still be made for all other
139 * functions on the bus.
141 * Return: 0 on success, an error value otherwise
143 int zpci_bus_scan_bus(struct zpci_bus *zbus)
145 struct zpci_dev *zdev;
146 int devfn, rc, ret = 0;
148 for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
149 zdev = zbus->function[devfn];
150 if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
151 rc = zpci_bus_prepare_device(zdev);
157 pci_lock_rescan_remove();
158 pci_scan_child_bus(zbus->bus);
159 pci_bus_add_devices(zbus->bus);
160 pci_unlock_rescan_remove();
165 /* zpci_bus_scan_busses - Scan all registered busses
167 * Scan all available zbusses
170 void zpci_bus_scan_busses(void)
172 struct zpci_bus *zbus = NULL;
174 mutex_lock(&zbus_list_lock);
175 list_for_each_entry(zbus, &zbus_list, bus_next) {
176 zpci_bus_scan_bus(zbus);
179 mutex_unlock(&zbus_list_lock);
182 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
183 * @zbus: the zbus holding the zdevices
184 * @fr: PCI root function that will determine the bus's domain, and bus speeed
185 * @ops: the pci operations
187 * The PCI function @fr determines the domain (its UID), multifunction property
188 * and maximum bus speed of the entire bus.
190 * Return: 0 on success, an error code otherwise
192 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
197 domain = zpci_alloc_domain((u16)fr->uid);
201 zbus->domain_nr = domain;
202 zbus->multifunction = fr->rid_available;
203 zbus->max_bus_speed = fr->max_bus_speed;
206 * Note that the zbus->resources are taken over and zbus->resources
207 * is empty after a successful call
209 bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
211 zpci_free_domain(zbus->domain_nr);
216 pci_bus_add_devices(bus);
221 static void zpci_bus_release(struct kref *kref)
223 struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
226 pci_lock_rescan_remove();
227 pci_stop_root_bus(zbus->bus);
229 zpci_free_domain(zbus->domain_nr);
230 pci_free_resource_list(&zbus->resources);
232 pci_remove_root_bus(zbus->bus);
233 pci_unlock_rescan_remove();
236 mutex_lock(&zbus_list_lock);
237 list_del(&zbus->bus_next);
238 mutex_unlock(&zbus_list_lock);
242 static void zpci_bus_put(struct zpci_bus *zbus)
244 kref_put(&zbus->kref, zpci_bus_release);
247 static struct zpci_bus *zpci_bus_get(int pchid)
249 struct zpci_bus *zbus;
251 mutex_lock(&zbus_list_lock);
252 list_for_each_entry(zbus, &zbus_list, bus_next) {
253 if (pchid == zbus->pchid) {
254 kref_get(&zbus->kref);
260 mutex_unlock(&zbus_list_lock);
264 static struct zpci_bus *zpci_bus_alloc(int pchid)
266 struct zpci_bus *zbus;
268 zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
273 INIT_LIST_HEAD(&zbus->bus_next);
274 mutex_lock(&zbus_list_lock);
275 list_add_tail(&zbus->bus_next, &zbus_list);
276 mutex_unlock(&zbus_list_lock);
278 kref_init(&zbus->kref);
279 INIT_LIST_HEAD(&zbus->resources);
281 zbus->bus_resource.start = 0;
282 zbus->bus_resource.end = ZPCI_BUS_NR;
283 zbus->bus_resource.flags = IORESOURCE_BUS;
284 pci_add_resource(&zbus->resources, &zbus->bus_resource);
289 void pcibios_bus_add_device(struct pci_dev *pdev)
291 struct zpci_dev *zdev = to_zpci(pdev);
294 * With pdev->no_vf_scan the common PCI probing code does not
295 * perform PF/VF linking.
298 zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
299 pdev->no_command_memory = 1;
303 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
307 if (zbus->function[zdev->devfn]) {
308 pr_err("devfn %04x is already assigned\n", zdev->devfn);
313 zbus->function[zdev->devfn] = zdev;
316 if (zbus->multifunction && !zdev->rid_available) {
317 WARN_ONCE(1, "rid_available not set for multifunction\n");
320 rc = zpci_init_slot(zdev);
323 zdev->has_hp_slot = 1;
328 zbus->function[zdev->devfn] = NULL;
334 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
336 struct zpci_bus *zbus = NULL;
339 if (zpci_nb_devices == ZPCI_NR_DEVICES) {
340 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
341 zdev->fid, ZPCI_NR_DEVICES);
345 if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
348 if (!s390_pci_no_rid && zdev->rid_available)
349 zbus = zpci_bus_get(zdev->pchid);
352 zbus = zpci_bus_alloc(zdev->pchid);
358 /* The UID of the first PCI function registered with a zpci_bus
359 * is used as the domain number for that bus. Currently there
360 * is exactly one zpci_bus per domain.
362 rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
367 rc = zpci_bus_add_device(zbus, zdev);
374 pr_err("Adding PCI function %08x failed\n", zdev->fid);
379 void zpci_bus_device_unregister(struct zpci_dev *zdev)
381 struct zpci_bus *zbus = zdev->zbus;
384 zbus->function[zdev->devfn] = NULL;