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