xref: /linux/arch/s390/pci/pci_bus.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 	}
51 
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);
57 		}
58 	}
59 
60 	return 0;
61 }
62 
63 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
64  * @zdev: the zdev to be scanned
65  *
66  * Scans the PCI function making it available to the common PCI code.
67  *
68  * Return: 0 on success, an error value otherwise
69  */
70 int zpci_bus_scan_device(struct zpci_dev *zdev)
71 {
72 	struct pci_dev *pdev;
73 	int rc;
74 
75 	rc = zpci_bus_prepare_device(zdev);
76 	if (rc)
77 		return rc;
78 
79 	pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
80 	if (!pdev)
81 		return -ENODEV;
82 
83 	pci_lock_rescan_remove();
84 	pci_bus_add_device(pdev);
85 	pci_unlock_rescan_remove();
86 
87 	return 0;
88 }
89 
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
93  *
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.
98  */
99 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
100 {
101 	struct zpci_bus *zbus = zdev->zbus;
102 	struct pci_dev *pdev;
103 
104 	if (!zdev->zbus->bus)
105 		return;
106 
107 	pdev = pci_get_slot(zbus->bus, zdev->devfn);
108 	if (pdev) {
109 		if (set_error)
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 */
114 			pci_dev_put(pdev);
115 			return;
116 		}
117 		pci_stop_and_remove_bus_device_locked(pdev);
118 		/* balance pci_get_slot */
119 		pci_dev_put(pdev);
120 	}
121 }
122 
123 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
124  * @zbus: the zbus to be scanned
125  *
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.
129  *
130  * Return: 0 on success, an error value otherwise
131  */
132 int zpci_bus_scan_bus(struct zpci_bus *zbus)
133 {
134 	struct zpci_dev *zdev;
135 	int devfn, rc, ret = 0;
136 
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);
141 			if (rc)
142 				ret = -EIO;
143 		}
144 	}
145 
146 	pci_lock_rescan_remove();
147 	pci_scan_child_bus(zbus->bus);
148 	pci_bus_add_devices(zbus->bus);
149 	pci_unlock_rescan_remove();
150 
151 	return ret;
152 }
153 
154 /* zpci_bus_scan_busses - Scan all registered busses
155  *
156  * Scan all available zbusses
157  *
158  */
159 void zpci_bus_scan_busses(void)
160 {
161 	struct zpci_bus *zbus = NULL;
162 
163 	mutex_lock(&zbus_list_lock);
164 	list_for_each_entry(zbus, &zbus_list, bus_next) {
165 		zpci_bus_scan_bus(zbus);
166 		cond_resched();
167 	}
168 	mutex_unlock(&zbus_list_lock);
169 }
170 
171 static bool zpci_bus_is_multifunction_root(struct zpci_dev *zdev)
172 {
173 	return !s390_pci_no_rid && zdev->rid_available &&
174 		zpci_is_device_configured(zdev) &&
175 		!zdev->vfn;
176 }
177 
178 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
179  * @zbus: the zbus holding the zdevices
180  * @fr: PCI root function that will determine the bus's domain, and bus speed
181  * @ops: the pci operations
182  *
183  * The PCI function @fr determines the domain (its UID), multifunction property
184  * and maximum bus speed of the entire bus.
185  *
186  * Return: 0 on success, an error code otherwise
187  */
188 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
189 {
190 	struct pci_bus *bus;
191 	int domain;
192 
193 	domain = zpci_alloc_domain((u16)fr->uid);
194 	if (domain < 0)
195 		return domain;
196 
197 	zbus->domain_nr = domain;
198 	zbus->multifunction = zpci_bus_is_multifunction_root(fr);
199 	zbus->max_bus_speed = fr->max_bus_speed;
200 
201 	/*
202 	 * Note that the zbus->resources are taken over and zbus->resources
203 	 * is empty after a successful call
204 	 */
205 	bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
206 	if (!bus) {
207 		zpci_free_domain(zbus->domain_nr);
208 		return -EFAULT;
209 	}
210 
211 	zbus->bus = bus;
212 
213 	return 0;
214 }
215 
216 static void zpci_bus_release(struct kref *kref)
217 {
218 	struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
219 
220 	if (zbus->bus) {
221 		pci_lock_rescan_remove();
222 		pci_stop_root_bus(zbus->bus);
223 
224 		zpci_free_domain(zbus->domain_nr);
225 		pci_free_resource_list(&zbus->resources);
226 
227 		pci_remove_root_bus(zbus->bus);
228 		pci_unlock_rescan_remove();
229 	}
230 
231 	mutex_lock(&zbus_list_lock);
232 	list_del(&zbus->bus_next);
233 	mutex_unlock(&zbus_list_lock);
234 	kfree(zbus);
235 }
236 
237 static void zpci_bus_put(struct zpci_bus *zbus)
238 {
239 	kref_put(&zbus->kref, zpci_bus_release);
240 }
241 
242 static struct zpci_bus *zpci_bus_get(int topo, bool topo_is_tid)
243 {
244 	struct zpci_bus *zbus;
245 
246 	mutex_lock(&zbus_list_lock);
247 	list_for_each_entry(zbus, &zbus_list, bus_next) {
248 		if (!zbus->multifunction)
249 			continue;
250 		if (topo_is_tid == zbus->topo_is_tid && topo == zbus->topo) {
251 			kref_get(&zbus->kref);
252 			goto out_unlock;
253 		}
254 	}
255 	zbus = NULL;
256 out_unlock:
257 	mutex_unlock(&zbus_list_lock);
258 	return zbus;
259 }
260 
261 static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid)
262 {
263 	struct zpci_bus *zbus;
264 
265 	zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
266 	if (!zbus)
267 		return NULL;
268 
269 	zbus->topo = topo;
270 	zbus->topo_is_tid = topo_is_tid;
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 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
302 {
303 	int rc = -EINVAL;
304 
305 	if (zbus->multifunction) {
306 		if (!zdev->rid_available) {
307 			WARN_ONCE(1, "rid_available not set for multifunction\n");
308 			return rc;
309 		}
310 		zdev->devfn = zdev->rid & ZPCI_RID_MASK_DEVFN;
311 	}
312 
313 	if (zbus->function[zdev->devfn]) {
314 		pr_err("devfn %04x is already assigned\n", zdev->devfn);
315 		return rc;
316 	}
317 	zdev->zbus = zbus;
318 	zbus->function[zdev->devfn] = zdev;
319 	zpci_nb_devices++;
320 
321 	rc = zpci_init_slot(zdev);
322 	if (rc)
323 		goto error;
324 	zdev->has_hp_slot = 1;
325 
326 	return 0;
327 
328 error:
329 	zbus->function[zdev->devfn] = NULL;
330 	zdev->zbus = NULL;
331 	zpci_nb_devices--;
332 	return rc;
333 }
334 
335 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
336 {
337 	bool topo_is_tid = zdev->tid_avail;
338 	struct zpci_bus *zbus = NULL;
339 	int topo, rc = -EBADF;
340 
341 	if (zpci_nb_devices == ZPCI_NR_DEVICES) {
342 		pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
343 			zdev->fid, ZPCI_NR_DEVICES);
344 		return -ENOSPC;
345 	}
346 
347 	topo = topo_is_tid ? zdev->tid : zdev->pchid;
348 	zbus = zpci_bus_get(topo, topo_is_tid);
349 	if (!zbus) {
350 		zbus = zpci_bus_alloc(topo, topo_is_tid);
351 		if (!zbus)
352 			return -ENOMEM;
353 	}
354 
355 	if (!zbus->bus) {
356 		/* The UID of the first PCI function registered with a zpci_bus
357 		 * is used as the domain number for that bus. Currently there
358 		 * is exactly one zpci_bus per domain.
359 		 */
360 		rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
361 		if (rc)
362 			goto error;
363 	}
364 
365 	rc = zpci_bus_add_device(zbus, zdev);
366 	if (rc)
367 		goto error;
368 
369 	return 0;
370 
371 error:
372 	pr_err("Adding PCI function %08x failed\n", zdev->fid);
373 	zpci_bus_put(zbus);
374 	return rc;
375 }
376 
377 void zpci_bus_device_unregister(struct zpci_dev *zdev)
378 {
379 	struct zpci_bus *zbus = zdev->zbus;
380 
381 	zpci_nb_devices--;
382 	zbus->function[zdev->devfn] = NULL;
383 	zpci_bus_put(zbus);
384 }
385