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 pr_fmt(fmt) "zpci: " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/seq_file.h>
17 #include <linux/irqdomain.h>
18 #include <linux/jump_label.h>
19 #include <linux/pci.h>
20 #include <linux/printk.h>
21 #include <linux/dma-direct.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 pr_err("Enabling PCI function %08x failed\n", zdev->fid);
50 return rc;
51 }
52 }
53
54 if (!zdev->has_resources) {
55 zpci_setup_bus_resources(zdev);
56 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
57 if (zdev->bars[i].res)
58 pci_bus_add_resource(zdev->zbus->bus, zdev->bars[i].res);
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 */
zpci_bus_scan_device(struct zpci_dev * zdev)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_lock_rescan_remove();
86 pci_bus_add_device(pdev);
87 pci_unlock_rescan_remove();
88
89 return 0;
90 }
91
92 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
93 * @zdev: the zdev to be removed from the PCI core
94 * @set_error: if true the device's error state is set to permanent failure
95 *
96 * Sets a zPCI device to a configured but offline state; the zPCI
97 * device is still accessible through its hotplug slot and the zPCI
98 * API but is removed from the common code PCI bus, making it
99 * no longer available to drivers.
100 */
zpci_bus_remove_device(struct zpci_dev * zdev,bool set_error)101 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
102 {
103 struct zpci_bus *zbus = zdev->zbus;
104 struct pci_dev *pdev;
105
106 if (!zdev->zbus->bus)
107 return;
108
109 pdev = pci_get_slot(zbus->bus, zdev->devfn);
110 if (pdev) {
111 if (set_error)
112 pdev->error_state = pci_channel_io_perm_failure;
113 if (pdev->is_virtfn) {
114 zpci_iov_remove_virtfn(pdev, zdev->vfn);
115 /* balance pci_get_slot */
116 pci_dev_put(pdev);
117 return;
118 }
119 pci_stop_and_remove_bus_device_locked(pdev);
120 /* balance pci_get_slot */
121 pci_dev_put(pdev);
122 }
123 }
124
125 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
126 * @zbus: the zbus to be scanned
127 *
128 * Enables and scans all PCI functions on the bus making them available to the
129 * common PCI code. If a PCI function fails to be initialized an error will be
130 * returned but attempts will still be made for all other functions on the bus.
131 *
132 * Return: 0 on success, an error value otherwise
133 */
zpci_bus_scan_bus(struct zpci_bus * zbus)134 int zpci_bus_scan_bus(struct zpci_bus *zbus)
135 {
136 struct zpci_dev *zdev;
137 int devfn, rc, ret = 0;
138
139 for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
140 zdev = zbus->function[devfn];
141 if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
142 rc = zpci_bus_prepare_device(zdev);
143 if (rc)
144 ret = -EIO;
145 }
146 }
147
148 pci_lock_rescan_remove();
149 pci_scan_child_bus(zbus->bus);
150 pci_bus_add_devices(zbus->bus);
151 pci_unlock_rescan_remove();
152
153 return ret;
154 }
155
156 /* zpci_bus_scan_busses - Scan all registered busses
157 *
158 * Scan all available zbusses
159 *
160 */
zpci_bus_scan_busses(void)161 void zpci_bus_scan_busses(void)
162 {
163 struct zpci_bus *zbus = NULL;
164
165 mutex_lock(&zbus_list_lock);
166 list_for_each_entry(zbus, &zbus_list, bus_next) {
167 zpci_bus_scan_bus(zbus);
168 cond_resched();
169 }
170 mutex_unlock(&zbus_list_lock);
171 }
172
zpci_bus_is_multifunction_root(struct zpci_dev * zdev)173 static bool zpci_bus_is_multifunction_root(struct zpci_dev *zdev)
174 {
175 return !s390_pci_no_rid && zdev->rid_available &&
176 !zdev->vfn;
177 }
178
179 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
180 * @zbus: the zbus holding the zdevices
181 * @fr: PCI root function that will determine the bus's domain, and bus speed
182 * @ops: the pci operations
183 *
184 * The PCI function @fr determines the domain (its UID), multifunction property
185 * and maximum bus speed of the entire bus.
186 *
187 * Return: 0 on success, an error code otherwise
188 */
zpci_bus_create_pci_bus(struct zpci_bus * zbus,struct zpci_dev * fr,struct pci_ops * ops)189 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
190 {
191 struct pci_bus *bus;
192 int domain;
193
194 domain = zpci_alloc_domain((u16)fr->uid);
195 if (domain < 0)
196 return domain;
197
198 zbus->domain_nr = domain;
199 zbus->multifunction = zpci_bus_is_multifunction_root(fr);
200 zbus->max_bus_speed = fr->max_bus_speed;
201
202 if (zpci_create_parent_msi_domain(zbus))
203 goto out_free_domain;
204
205 /*
206 * Note that the zbus->resources are taken over and zbus->resources
207 * is empty after a successful call
208 */
209 bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
210 if (!bus)
211 goto out_remove_msi_domain;
212
213 zbus->bus = bus;
214 dev_set_msi_domain(&zbus->bus->dev, zbus->msi_parent_domain);
215
216 return 0;
217
218 out_remove_msi_domain:
219 zpci_remove_parent_msi_domain(zbus);
220 out_free_domain:
221 zpci_free_domain(zbus->domain_nr);
222 return -ENOMEM;
223 }
224
zpci_bus_release(struct kref * kref)225 static void zpci_bus_release(struct kref *kref)
226 {
227 struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
228
229 if (zbus->bus) {
230 pci_lock_rescan_remove();
231 pci_stop_root_bus(zbus->bus);
232
233 zpci_free_domain(zbus->domain_nr);
234 pci_free_resource_list(&zbus->resources);
235
236 pci_remove_root_bus(zbus->bus);
237 pci_unlock_rescan_remove();
238 }
239
240 mutex_lock(&zbus_list_lock);
241 list_del(&zbus->bus_next);
242 mutex_unlock(&zbus_list_lock);
243 zpci_remove_parent_msi_domain(zbus);
244 kfree(zbus);
245 }
246
zpci_bus_put(struct zpci_bus * zbus)247 static void zpci_bus_put(struct zpci_bus *zbus)
248 {
249 kref_put(&zbus->kref, zpci_bus_release);
250 }
251
zpci_bus_get(int topo,bool topo_is_tid)252 static struct zpci_bus *zpci_bus_get(int topo, bool topo_is_tid)
253 {
254 struct zpci_bus *zbus;
255
256 mutex_lock(&zbus_list_lock);
257 list_for_each_entry(zbus, &zbus_list, bus_next) {
258 if (!zbus->multifunction)
259 continue;
260 if (topo_is_tid == zbus->topo_is_tid && topo == zbus->topo) {
261 kref_get(&zbus->kref);
262 goto out_unlock;
263 }
264 }
265 zbus = NULL;
266 out_unlock:
267 mutex_unlock(&zbus_list_lock);
268 return zbus;
269 }
270
zpci_bus_alloc(int topo,bool topo_is_tid)271 static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid)
272 {
273 struct zpci_bus *zbus;
274
275 zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
276 if (!zbus)
277 return NULL;
278
279 zbus->topo = topo;
280 zbus->topo_is_tid = topo_is_tid;
281 INIT_LIST_HEAD(&zbus->bus_next);
282 mutex_lock(&zbus_list_lock);
283 list_add_tail(&zbus->bus_next, &zbus_list);
284 mutex_unlock(&zbus_list_lock);
285
286 kref_init(&zbus->kref);
287 INIT_LIST_HEAD(&zbus->resources);
288
289 zbus->bus_resource.start = 0;
290 zbus->bus_resource.end = ZPCI_BUS_NR;
291 zbus->bus_resource.flags = IORESOURCE_BUS;
292 pci_add_resource(&zbus->resources, &zbus->bus_resource);
293
294 return zbus;
295 }
296
pci_dma_range_setup(struct pci_dev * pdev)297 static void pci_dma_range_setup(struct pci_dev *pdev)
298 {
299 struct zpci_dev *zdev = to_zpci(pdev);
300 u64 aligned_end, size;
301 dma_addr_t dma_start;
302 int ret;
303
304 dma_start = PAGE_ALIGN(zdev->start_dma);
305 aligned_end = PAGE_ALIGN_DOWN(zdev->end_dma + 1);
306 if (aligned_end >= dma_start)
307 size = aligned_end - dma_start;
308 else
309 size = 0;
310 WARN_ON_ONCE(size == 0);
311
312 ret = dma_direct_set_offset(&pdev->dev, 0, dma_start, size);
313 if (ret)
314 pr_err("Failed to allocate DMA range map for %s\n", pci_name(pdev));
315 }
316
pcibios_bus_add_device(struct pci_dev * pdev)317 void pcibios_bus_add_device(struct pci_dev *pdev)
318 {
319 struct zpci_dev *zdev = to_zpci(pdev);
320
321 pci_dma_range_setup(pdev);
322
323 /*
324 * With pdev->no_vf_scan the common PCI probing code does not
325 * perform PF/VF linking.
326 */
327 if (zdev->vfn) {
328 zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
329 pdev->no_command_memory = 1;
330 }
331 }
332
zpci_bus_add_device(struct zpci_bus * zbus,struct zpci_dev * zdev)333 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
334 {
335 int rc = -EINVAL;
336
337 if (zbus->multifunction) {
338 if (!zdev->rid_available) {
339 WARN_ONCE(1, "rid_available not set for multifunction\n");
340 return rc;
341 }
342 zdev->devfn = zdev->rid & ZPCI_RID_MASK_DEVFN;
343 }
344
345 if (zbus->function[zdev->devfn]) {
346 pr_err("devfn %04x is already assigned\n", zdev->devfn);
347 return rc;
348 }
349 zdev->zbus = zbus;
350 zbus->function[zdev->devfn] = zdev;
351 zpci_nb_devices++;
352
353 rc = zpci_init_slot(zdev);
354 if (rc)
355 goto error;
356 zdev->has_hp_slot = 1;
357
358 return 0;
359
360 error:
361 zbus->function[zdev->devfn] = NULL;
362 zdev->zbus = NULL;
363 zpci_nb_devices--;
364 return rc;
365 }
366
zpci_bus_is_isolated_vf(struct zpci_bus * zbus,struct zpci_dev * zdev)367 static bool zpci_bus_is_isolated_vf(struct zpci_bus *zbus, struct zpci_dev *zdev)
368 {
369 struct pci_dev *pdev;
370
371 if (!zdev->vfn)
372 return false;
373
374 pdev = zpci_iov_find_parent_pf(zbus, zdev);
375 if (!pdev)
376 return true;
377 pci_dev_put(pdev);
378 return false;
379 }
380
zpci_bus_device_register(struct zpci_dev * zdev,struct pci_ops * ops)381 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
382 {
383 bool topo_is_tid = zdev->tid_avail;
384 struct zpci_bus *zbus = NULL;
385 int topo, rc = -EBADF;
386
387 if (zpci_nb_devices == ZPCI_NR_DEVICES) {
388 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
389 zdev->fid, ZPCI_NR_DEVICES);
390 return -ENOSPC;
391 }
392
393 topo = topo_is_tid ? zdev->tid : zdev->pchid;
394 zbus = zpci_bus_get(topo, topo_is_tid);
395 /*
396 * An isolated VF gets its own domain/bus even if there exists
397 * a matching domain/bus already
398 */
399 if (zbus && zpci_bus_is_isolated_vf(zbus, zdev)) {
400 zpci_bus_put(zbus);
401 zbus = NULL;
402 }
403
404 if (!zbus) {
405 zbus = zpci_bus_alloc(topo, topo_is_tid);
406 if (!zbus)
407 return -ENOMEM;
408 }
409
410 if (!zbus->bus) {
411 /* The UID of the first PCI function registered with a zpci_bus
412 * is used as the domain number for that bus. Currently there
413 * is exactly one zpci_bus per domain.
414 */
415 rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
416 if (rc)
417 goto error;
418 }
419
420 rc = zpci_bus_add_device(zbus, zdev);
421 if (rc)
422 goto error;
423
424 return 0;
425
426 error:
427 pr_err("Adding PCI function %08x failed\n", zdev->fid);
428 zpci_bus_put(zbus);
429 return rc;
430 }
431
zpci_bus_device_unregister(struct zpci_dev * zdev)432 void zpci_bus_device_unregister(struct zpci_dev *zdev)
433 {
434 struct zpci_bus *zbus = zdev->zbus;
435
436 zpci_nb_devices--;
437 zbus->function[zdev->devfn] = NULL;
438 zpci_bus_put(zbus);
439 }
440