xref: /openbmc/linux/arch/s390/pci/pci_bus.c (revision 66c98360)
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 	int rc, i;
45 
46 	if (!zdev_enabled(zdev)) {
47 		rc = zpci_enable_device(zdev);
48 		if (rc)
49 			return rc;
50 		rc = zpci_dma_init_device(zdev);
51 		if (rc) {
52 			zpci_disable_device(zdev);
53 			return rc;
54 		}
55 	}
56 
57 	if (!zdev->has_resources) {
58 		zpci_setup_bus_resources(zdev);
59 		for (i = 0; i < PCI_STD_NUM_BARS; i++) {
60 			if (zdev->bars[i].res)
61 				pci_bus_add_resource(zdev->zbus->bus, zdev->bars[i].res, 0);
62 		}
63 	}
64 
65 	return 0;
66 }
67 
68 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
69  * @zdev: the zdev to be scanned
70  *
71  * Scans the PCI function making it available to the common PCI code.
72  *
73  * Return: 0 on success, an error value otherwise
74  */
75 int zpci_bus_scan_device(struct zpci_dev *zdev)
76 {
77 	struct pci_dev *pdev;
78 	int rc;
79 
80 	rc = zpci_bus_prepare_device(zdev);
81 	if (rc)
82 		return rc;
83 
84 	pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
85 	if (!pdev)
86 		return -ENODEV;
87 
88 	pci_lock_rescan_remove();
89 	pci_bus_add_device(pdev);
90 	pci_unlock_rescan_remove();
91 
92 	return 0;
93 }
94 
95 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
96  * @zdev: the zdev to be removed from the PCI core
97  * @set_error: if true the device's error state is set to permanent failure
98  *
99  * Sets a zPCI device to a configured but offline state; the zPCI
100  * device is still accessible through its hotplug slot and the zPCI
101  * API but is removed from the common code PCI bus, making it
102  * no longer available to drivers.
103  */
104 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
105 {
106 	struct zpci_bus *zbus = zdev->zbus;
107 	struct pci_dev *pdev;
108 
109 	if (!zdev->zbus->bus)
110 		return;
111 
112 	pdev = pci_get_slot(zbus->bus, zdev->devfn);
113 	if (pdev) {
114 		if (set_error)
115 			pdev->error_state = pci_channel_io_perm_failure;
116 		if (pdev->is_virtfn) {
117 			zpci_iov_remove_virtfn(pdev, zdev->vfn);
118 			/* balance pci_get_slot */
119 			pci_dev_put(pdev);
120 			return;
121 		}
122 		pci_stop_and_remove_bus_device_locked(pdev);
123 		/* balance pci_get_slot */
124 		pci_dev_put(pdev);
125 	}
126 }
127 
128 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
129  * @zbus: the zbus to be scanned
130  *
131  * Enables and scans all PCI functions on the bus making them available to the
132  * common PCI code. If a PCI function fails to be initialized an error will be
133  * returned but attempts will still be made for all other functions on the bus.
134  *
135  * Return: 0 on success, an error value otherwise
136  */
137 int zpci_bus_scan_bus(struct zpci_bus *zbus)
138 {
139 	struct zpci_dev *zdev;
140 	int devfn, rc, ret = 0;
141 
142 	for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
143 		zdev = zbus->function[devfn];
144 		if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
145 			rc = zpci_bus_prepare_device(zdev);
146 			if (rc)
147 				ret = -EIO;
148 		}
149 	}
150 
151 	pci_lock_rescan_remove();
152 	pci_scan_child_bus(zbus->bus);
153 	pci_bus_add_devices(zbus->bus);
154 	pci_unlock_rescan_remove();
155 
156 	return ret;
157 }
158 
159 /* zpci_bus_scan_busses - Scan all registered busses
160  *
161  * Scan all available zbusses
162  *
163  */
164 void zpci_bus_scan_busses(void)
165 {
166 	struct zpci_bus *zbus = NULL;
167 
168 	mutex_lock(&zbus_list_lock);
169 	list_for_each_entry(zbus, &zbus_list, bus_next) {
170 		zpci_bus_scan_bus(zbus);
171 		cond_resched();
172 	}
173 	mutex_unlock(&zbus_list_lock);
174 }
175 
176 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
177  * @zbus: the zbus holding the zdevices
178  * @fr: PCI root function that will determine the bus's domain, and bus speeed
179  * @ops: the pci operations
180  *
181  * The PCI function @fr determines the domain (its UID), multifunction property
182  * and maximum bus speed of the entire bus.
183  *
184  * Return: 0 on success, an error code otherwise
185  */
186 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
187 {
188 	struct pci_bus *bus;
189 	int domain;
190 
191 	domain = zpci_alloc_domain((u16)fr->uid);
192 	if (domain < 0)
193 		return domain;
194 
195 	zbus->domain_nr = domain;
196 	zbus->multifunction = fr->rid_available;
197 	zbus->max_bus_speed = fr->max_bus_speed;
198 
199 	/*
200 	 * Note that the zbus->resources are taken over and zbus->resources
201 	 * is empty after a successful call
202 	 */
203 	bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
204 	if (!bus) {
205 		zpci_free_domain(zbus->domain_nr);
206 		return -EFAULT;
207 	}
208 
209 	zbus->bus = bus;
210 
211 	return 0;
212 }
213 
214 static void zpci_bus_release(struct kref *kref)
215 {
216 	struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
217 
218 	if (zbus->bus) {
219 		pci_lock_rescan_remove();
220 		pci_stop_root_bus(zbus->bus);
221 
222 		zpci_free_domain(zbus->domain_nr);
223 		pci_free_resource_list(&zbus->resources);
224 
225 		pci_remove_root_bus(zbus->bus);
226 		pci_unlock_rescan_remove();
227 	}
228 
229 	mutex_lock(&zbus_list_lock);
230 	list_del(&zbus->bus_next);
231 	mutex_unlock(&zbus_list_lock);
232 	kfree(zbus);
233 }
234 
235 static void zpci_bus_put(struct zpci_bus *zbus)
236 {
237 	kref_put(&zbus->kref, zpci_bus_release);
238 }
239 
240 static struct zpci_bus *zpci_bus_get(int pchid)
241 {
242 	struct zpci_bus *zbus;
243 
244 	mutex_lock(&zbus_list_lock);
245 	list_for_each_entry(zbus, &zbus_list, bus_next) {
246 		if (pchid == zbus->pchid) {
247 			kref_get(&zbus->kref);
248 			goto out_unlock;
249 		}
250 	}
251 	zbus = NULL;
252 out_unlock:
253 	mutex_unlock(&zbus_list_lock);
254 	return zbus;
255 }
256 
257 static struct zpci_bus *zpci_bus_alloc(int pchid)
258 {
259 	struct zpci_bus *zbus;
260 
261 	zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
262 	if (!zbus)
263 		return NULL;
264 
265 	zbus->pchid = pchid;
266 	INIT_LIST_HEAD(&zbus->bus_next);
267 	mutex_lock(&zbus_list_lock);
268 	list_add_tail(&zbus->bus_next, &zbus_list);
269 	mutex_unlock(&zbus_list_lock);
270 
271 	kref_init(&zbus->kref);
272 	INIT_LIST_HEAD(&zbus->resources);
273 
274 	zbus->bus_resource.start = 0;
275 	zbus->bus_resource.end = ZPCI_BUS_NR;
276 	zbus->bus_resource.flags = IORESOURCE_BUS;
277 	pci_add_resource(&zbus->resources, &zbus->bus_resource);
278 
279 	return zbus;
280 }
281 
282 void pcibios_bus_add_device(struct pci_dev *pdev)
283 {
284 	struct zpci_dev *zdev = to_zpci(pdev);
285 
286 	/*
287 	 * With pdev->no_vf_scan the common PCI probing code does not
288 	 * perform PF/VF linking.
289 	 */
290 	if (zdev->vfn) {
291 		zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
292 		pdev->no_command_memory = 1;
293 	}
294 }
295 
296 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
297 {
298 	int rc = -EINVAL;
299 
300 	if (zbus->function[zdev->devfn]) {
301 		pr_err("devfn %04x is already assigned\n", zdev->devfn);
302 		return rc;
303 	}
304 
305 	zdev->zbus = zbus;
306 	zbus->function[zdev->devfn] = zdev;
307 	zpci_nb_devices++;
308 
309 	if (zbus->multifunction && !zdev->rid_available) {
310 		WARN_ONCE(1, "rid_available not set for multifunction\n");
311 		goto error;
312 	}
313 	rc = zpci_init_slot(zdev);
314 	if (rc)
315 		goto error;
316 	zdev->has_hp_slot = 1;
317 
318 	return 0;
319 
320 error:
321 	zbus->function[zdev->devfn] = NULL;
322 	zdev->zbus = NULL;
323 	zpci_nb_devices--;
324 	return rc;
325 }
326 
327 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
328 {
329 	struct zpci_bus *zbus = NULL;
330 	int rc = -EBADF;
331 
332 	if (zpci_nb_devices == ZPCI_NR_DEVICES) {
333 		pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
334 			zdev->fid, ZPCI_NR_DEVICES);
335 		return -ENOSPC;
336 	}
337 
338 	if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
339 		return -EINVAL;
340 
341 	if (!s390_pci_no_rid && zdev->rid_available)
342 		zbus = zpci_bus_get(zdev->pchid);
343 
344 	if (!zbus) {
345 		zbus = zpci_bus_alloc(zdev->pchid);
346 		if (!zbus)
347 			return -ENOMEM;
348 	}
349 
350 	if (!zbus->bus) {
351 		/* The UID of the first PCI function registered with a zpci_bus
352 		 * is used as the domain number for that bus. Currently there
353 		 * is exactly one zpci_bus per domain.
354 		 */
355 		rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
356 		if (rc)
357 			goto error;
358 	}
359 
360 	rc = zpci_bus_add_device(zbus, zdev);
361 	if (rc)
362 		goto error;
363 
364 	return 0;
365 
366 error:
367 	pr_err("Adding PCI function %08x failed\n", zdev->fid);
368 	zpci_bus_put(zbus);
369 	return rc;
370 }
371 
372 void zpci_bus_device_unregister(struct zpci_dev *zdev)
373 {
374 	struct zpci_bus *zbus = zdev->zbus;
375 
376 	zpci_nb_devices--;
377 	zbus->function[zdev->devfn] = NULL;
378 	zpci_bus_put(zbus);
379 }
380