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