xref: /openbmc/linux/arch/s390/pci/pci_bus.c (revision 413d6ed3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2020
4  *
5  * Author(s):
6  *   Pierre Morel <pmorel@linux.ibm.com>
7  *
8  */
9 
10 #define KMSG_COMPONENT "zpci"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
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>
22 
23 #include <asm/pci_clp.h>
24 #include <asm/pci_dma.h>
25 
26 #include "pci_bus.h"
27 #include "pci_iov.h"
28 
29 static LIST_HEAD(zbus_list);
30 static DEFINE_MUTEX(zbus_list_lock);
31 static int zpci_nb_devices;
32 
33 /* zpci_bus_prepare_device - Prepare a zPCI function for scanning
34  * @zdev: the zPCI function to be prepared
35  *
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.
39  *
40  * Return: 0 on success, an error code otherwise
41  */
42 static int zpci_bus_prepare_device(struct zpci_dev *zdev)
43 {
44 	struct resource_entry *window, *n;
45 	struct resource *res;
46 	int rc;
47 
48 	if (!zdev_enabled(zdev)) {
49 		rc = zpci_enable_device(zdev);
50 		if (rc)
51 			return rc;
52 	}
53 
54 	if (!zdev->has_resources) {
55 		zpci_setup_bus_resources(zdev, &zdev->zbus->resources);
56 		resource_list_for_each_entry_safe(window, n, &zdev->zbus->resources) {
57 			res = window->res;
58 			pci_bus_add_resource(zdev->zbus->bus, res, 0);
59 		}
60 	}
61 
62 	return 0;
63 }
64 
65 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
66  * @zdev: the zdev to be scanned
67  *
68  * Scans the PCI function making it available to the common PCI code.
69  *
70  * Return: 0 on success, an error value otherwise
71  */
72 int zpci_bus_scan_device(struct zpci_dev *zdev)
73 {
74 	struct pci_dev *pdev;
75 	int rc;
76 
77 	rc = zpci_bus_prepare_device(zdev);
78 	if (rc)
79 		return rc;
80 
81 	pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
82 	if (!pdev)
83 		return -ENODEV;
84 
85 	pci_bus_add_device(pdev);
86 	pci_lock_rescan_remove();
87 	pci_bus_add_devices(zdev->zbus->bus);
88 	pci_unlock_rescan_remove();
89 
90 	return 0;
91 }
92 
93 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
94  * @zdev: the zdev to be removed from the PCI core
95  * @set_error: if true the device's error state is set to permanent failure
96  *
97  * Sets a zPCI device to a configured but offline state; the zPCI
98  * device is still accessible through its hotplug slot and the zPCI
99  * API but is removed from the common code PCI bus, making it
100  * no longer available to drivers.
101  */
102 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
103 {
104 	struct zpci_bus *zbus = zdev->zbus;
105 	struct pci_dev *pdev;
106 
107 	if (!zdev->zbus->bus)
108 		return;
109 
110 	pdev = pci_get_slot(zbus->bus, zdev->devfn);
111 	if (pdev) {
112 		if (set_error)
113 			pdev->error_state = pci_channel_io_perm_failure;
114 		if (pdev->is_virtfn) {
115 			zpci_iov_remove_virtfn(pdev, zdev->vfn);
116 			/* balance pci_get_slot */
117 			pci_dev_put(pdev);
118 			return;
119 		}
120 		pci_stop_and_remove_bus_device_locked(pdev);
121 		/* balance pci_get_slot */
122 		pci_dev_put(pdev);
123 	}
124 }
125 
126 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
127  * @zbus: the zbus to be scanned
128  *
129  * Enables and scans all PCI functions on the bus making them available to the
130  * common PCI code. If there is no function 0 on the zbus nothing is scanned. If
131  * a function does not have a slot yet because it was added to the zbus before
132  * function 0 the slot is created. If a PCI function fails to be initialized
133  * an error will be returned but attempts will still be made for all other
134  * functions on the bus.
135  *
136  * Return: 0 on success, an error value otherwise
137  */
138 int zpci_bus_scan_bus(struct zpci_bus *zbus)
139 {
140 	struct zpci_dev *zdev;
141 	int devfn, rc, ret = 0;
142 
143 	if (!zbus->function[0])
144 		return 0;
145 
146 	for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
147 		zdev = zbus->function[devfn];
148 		if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
149 			rc = zpci_bus_prepare_device(zdev);
150 			if (rc)
151 				ret = -EIO;
152 		}
153 	}
154 
155 	pci_lock_rescan_remove();
156 	pci_scan_child_bus(zbus->bus);
157 	pci_bus_add_devices(zbus->bus);
158 	pci_unlock_rescan_remove();
159 
160 	return ret;
161 }
162 
163 /* zpci_bus_scan_busses - Scan all registered busses
164  *
165  * Scan all available zbusses
166  *
167  */
168 void zpci_bus_scan_busses(void)
169 {
170 	struct zpci_bus *zbus = NULL;
171 
172 	mutex_lock(&zbus_list_lock);
173 	list_for_each_entry(zbus, &zbus_list, bus_next) {
174 		zpci_bus_scan_bus(zbus);
175 		cond_resched();
176 	}
177 	mutex_unlock(&zbus_list_lock);
178 }
179 
180 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
181  * @zbus: the zbus holding the zdevices
182  * @f0: function 0 of the bus
183  * @ops: the pci operations
184  *
185  * Function zero is taken as a parameter as this is used to determine the
186  * domain, multifunction property and maximum bus speed of the entire bus.
187  *
188  * Return: 0 on success, an error code otherwise
189  */
190 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *f0, struct pci_ops *ops)
191 {
192 	struct pci_bus *bus;
193 	int domain;
194 
195 	domain = zpci_alloc_domain((u16)f0->uid);
196 	if (domain < 0)
197 		return domain;
198 
199 	zbus->domain_nr = domain;
200 	zbus->multifunction = f0->rid_available;
201 	zbus->max_bus_speed = f0->max_bus_speed;
202 
203 	/*
204 	 * Note that the zbus->resources are taken over and zbus->resources
205 	 * is empty after a successful call
206 	 */
207 	bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
208 	if (!bus) {
209 		zpci_free_domain(zbus->domain_nr);
210 		return -EFAULT;
211 	}
212 
213 	zbus->bus = bus;
214 	pci_bus_add_devices(bus);
215 
216 	return 0;
217 }
218 
219 static void zpci_bus_release(struct kref *kref)
220 {
221 	struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
222 
223 	if (zbus->bus) {
224 		pci_lock_rescan_remove();
225 		pci_stop_root_bus(zbus->bus);
226 
227 		zpci_free_domain(zbus->domain_nr);
228 		pci_free_resource_list(&zbus->resources);
229 
230 		pci_remove_root_bus(zbus->bus);
231 		pci_unlock_rescan_remove();
232 	}
233 
234 	mutex_lock(&zbus_list_lock);
235 	list_del(&zbus->bus_next);
236 	mutex_unlock(&zbus_list_lock);
237 	kfree(zbus);
238 }
239 
240 static void zpci_bus_put(struct zpci_bus *zbus)
241 {
242 	kref_put(&zbus->kref, zpci_bus_release);
243 }
244 
245 static struct zpci_bus *zpci_bus_get(int pchid)
246 {
247 	struct zpci_bus *zbus;
248 
249 	mutex_lock(&zbus_list_lock);
250 	list_for_each_entry(zbus, &zbus_list, bus_next) {
251 		if (pchid == zbus->pchid) {
252 			kref_get(&zbus->kref);
253 			goto out_unlock;
254 		}
255 	}
256 	zbus = NULL;
257 out_unlock:
258 	mutex_unlock(&zbus_list_lock);
259 	return zbus;
260 }
261 
262 static struct zpci_bus *zpci_bus_alloc(int pchid)
263 {
264 	struct zpci_bus *zbus;
265 
266 	zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
267 	if (!zbus)
268 		return NULL;
269 
270 	zbus->pchid = pchid;
271 	INIT_LIST_HEAD(&zbus->bus_next);
272 	mutex_lock(&zbus_list_lock);
273 	list_add_tail(&zbus->bus_next, &zbus_list);
274 	mutex_unlock(&zbus_list_lock);
275 
276 	kref_init(&zbus->kref);
277 	INIT_LIST_HEAD(&zbus->resources);
278 
279 	zbus->bus_resource.start = 0;
280 	zbus->bus_resource.end = ZPCI_BUS_NR;
281 	zbus->bus_resource.flags = IORESOURCE_BUS;
282 	pci_add_resource(&zbus->resources, &zbus->bus_resource);
283 
284 	return zbus;
285 }
286 
287 void pcibios_bus_add_device(struct pci_dev *pdev)
288 {
289 	struct zpci_dev *zdev = to_zpci(pdev);
290 
291 	/*
292 	 * With pdev->no_vf_scan the common PCI probing code does not
293 	 * perform PF/VF linking.
294 	 */
295 	if (zdev->vfn) {
296 		zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
297 		pdev->no_command_memory = 1;
298 	}
299 }
300 
301 /* zpci_bus_create_hotplug_slots - Add hotplug slot(s) for device added to bus
302  * @zdev: the zPCI device that was newly added
303  *
304  * Add the hotplug slot(s) for the newly added PCI function. Normally this is
305  * simply the slot for the function itself. If however we are adding the
306  * function 0 on a zbus, it might be that we already registered functions on
307  * that zbus but could not create their hotplug slots yet so add those now too.
308  *
309  * Return: 0 on success, an error code otherwise
310  */
311 static int zpci_bus_create_hotplug_slots(struct zpci_dev *zdev)
312 {
313 	struct zpci_bus *zbus = zdev->zbus;
314 	int devfn, rc = 0;
315 
316 	rc = zpci_init_slot(zdev);
317 	if (rc)
318 		return rc;
319 	zdev->has_hp_slot = 1;
320 
321 	if (zdev->devfn == 0 && zbus->multifunction) {
322 		/* Now that function 0 is there we can finally create the
323 		 * hotplug slots for those functions with devfn != 0 that have
324 		 * been parked in zbus->function[] waiting for us to be able to
325 		 * create the PCI bus.
326 		 */
327 		for  (devfn = 1; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
328 			zdev = zbus->function[devfn];
329 			if (zdev && !zdev->has_hp_slot) {
330 				rc = zpci_init_slot(zdev);
331 				if (rc)
332 					return rc;
333 				zdev->has_hp_slot = 1;
334 			}
335 		}
336 
337 	}
338 
339 	return rc;
340 }
341 
342 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
343 {
344 	int rc = -EINVAL;
345 
346 	zdev->zbus = zbus;
347 	if (zbus->function[zdev->devfn]) {
348 		pr_err("devfn %04x is already assigned\n", zdev->devfn);
349 		return rc;
350 	}
351 	zbus->function[zdev->devfn] = zdev;
352 	zpci_nb_devices++;
353 
354 	if (zbus->bus) {
355 		if (zbus->multifunction && !zdev->rid_available) {
356 			WARN_ONCE(1, "rid_available not set for multifunction\n");
357 			goto error;
358 		}
359 
360 		zpci_bus_create_hotplug_slots(zdev);
361 	} else {
362 		/* Hotplug slot will be created once function 0 appears */
363 		zbus->multifunction = 1;
364 	}
365 
366 	return 0;
367 
368 error:
369 	zbus->function[zdev->devfn] = NULL;
370 	zpci_nb_devices--;
371 	return rc;
372 }
373 
374 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
375 {
376 	struct zpci_bus *zbus = NULL;
377 	int rc = -EBADF;
378 
379 	if (zpci_nb_devices == ZPCI_NR_DEVICES) {
380 		pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
381 			zdev->fid, ZPCI_NR_DEVICES);
382 		return -ENOSPC;
383 	}
384 
385 	if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
386 		return -EINVAL;
387 
388 	if (!s390_pci_no_rid && zdev->rid_available)
389 		zbus = zpci_bus_get(zdev->pchid);
390 
391 	if (!zbus) {
392 		zbus = zpci_bus_alloc(zdev->pchid);
393 		if (!zbus)
394 			return -ENOMEM;
395 	}
396 
397 	if (zdev->devfn == 0) {
398 		rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
399 		if (rc)
400 			goto error;
401 	}
402 
403 	rc = zpci_bus_add_device(zbus, zdev);
404 	if (rc)
405 		goto error;
406 
407 	return 0;
408 
409 error:
410 	pr_err("Adding PCI function %08x failed\n", zdev->fid);
411 	zpci_bus_put(zbus);
412 	return rc;
413 }
414 
415 void zpci_bus_device_unregister(struct zpci_dev *zdev)
416 {
417 	struct zpci_bus *zbus = zdev->zbus;
418 
419 	zpci_nb_devices--;
420 	zbus->function[zdev->devfn] = NULL;
421 	zpci_bus_put(zbus);
422 }
423