usb: gadget: udc: pch_udc: Fix a plethora of function documentation related issues
[linux/fpc-iii.git] / arch / s390 / pci / pci_bus.c
blob642a993846889935749280ad0b87d94c7b61841b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2020
5 * Author(s):
6 * Pierre Morel <pmorel@linux.ibm.com>
8 */
10 #define KMSG_COMPONENT "zpci"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
23 #include <asm/pci_clp.h>
24 #include <asm/pci_dma.h>
26 #include "pci_bus.h"
28 static LIST_HEAD(zbus_list);
29 static DEFINE_SPINLOCK(zbus_list_lock);
30 static int zpci_nb_devices;
32 /* zpci_bus_scan
33 * @zbus: the zbus holding the zdevices
34 * @ops: the pci operations
36 * The domain number must be set before pci_scan_root_bus is called.
37 * This function can be called once the domain is known, hence
38 * when the function_0 is dicovered.
40 static int zpci_bus_scan(struct zpci_bus *zbus, int domain, struct pci_ops *ops)
42 struct pci_bus *bus;
43 int rc;
45 rc = zpci_alloc_domain(domain);
46 if (rc < 0)
47 return rc;
48 zbus->domain_nr = rc;
50 bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
51 if (!bus) {
52 zpci_free_domain(zbus->domain_nr);
53 return -EFAULT;
56 zbus->bus = bus;
57 pci_bus_add_devices(bus);
58 return 0;
61 static void zpci_bus_release(struct kref *kref)
63 struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
65 if (zbus->bus) {
66 pci_lock_rescan_remove();
67 pci_stop_root_bus(zbus->bus);
69 zpci_free_domain(zbus->domain_nr);
70 pci_free_resource_list(&zbus->resources);
72 pci_remove_root_bus(zbus->bus);
73 pci_unlock_rescan_remove();
76 spin_lock(&zbus_list_lock);
77 list_del(&zbus->bus_next);
78 spin_unlock(&zbus_list_lock);
79 kfree(zbus);
82 static void zpci_bus_put(struct zpci_bus *zbus)
84 kref_put(&zbus->kref, zpci_bus_release);
87 static struct zpci_bus *zpci_bus_get(int pchid)
89 struct zpci_bus *zbus;
91 spin_lock(&zbus_list_lock);
92 list_for_each_entry(zbus, &zbus_list, bus_next) {
93 if (pchid == zbus->pchid) {
94 kref_get(&zbus->kref);
95 goto out_unlock;
98 zbus = NULL;
99 out_unlock:
100 spin_unlock(&zbus_list_lock);
101 return zbus;
104 static struct zpci_bus *zpci_bus_alloc(int pchid)
106 struct zpci_bus *zbus;
108 zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
109 if (!zbus)
110 return NULL;
112 zbus->pchid = pchid;
113 INIT_LIST_HEAD(&zbus->bus_next);
114 spin_lock(&zbus_list_lock);
115 list_add_tail(&zbus->bus_next, &zbus_list);
116 spin_unlock(&zbus_list_lock);
118 kref_init(&zbus->kref);
119 INIT_LIST_HEAD(&zbus->resources);
121 zbus->bus_resource.start = 0;
122 zbus->bus_resource.end = ZPCI_BUS_NR;
123 zbus->bus_resource.flags = IORESOURCE_BUS;
124 pci_add_resource(&zbus->resources, &zbus->bus_resource);
126 return zbus;
129 #ifdef CONFIG_PCI_IOV
130 static int zpci_bus_link_virtfn(struct pci_dev *pdev,
131 struct pci_dev *virtfn, int vfid)
133 int rc;
135 virtfn->physfn = pci_dev_get(pdev);
136 rc = pci_iov_sysfs_link(pdev, virtfn, vfid);
137 if (rc) {
138 pci_dev_put(pdev);
139 virtfn->physfn = NULL;
140 return rc;
142 return 0;
145 static int zpci_bus_setup_virtfn(struct zpci_bus *zbus,
146 struct pci_dev *virtfn, int vfn)
148 int i, cand_devfn;
149 struct zpci_dev *zdev;
150 struct pci_dev *pdev;
151 int vfid = vfn - 1; /* Linux' vfid's start at 0 vfn at 1*/
152 int rc = 0;
154 virtfn->is_virtfn = 1;
155 virtfn->multifunction = 0;
156 WARN_ON(vfid < 0);
157 /* If the parent PF for the given VF is also configured in the
158 * instance, it must be on the same zbus.
159 * We can then identify the parent PF by checking what
160 * devfn the VF would have if it belonged to that PF using the PF's
161 * stride and offset. Only if this candidate devfn matches the
162 * actual devfn will we link both functions.
164 for (i = 0; i < ZPCI_FUNCTIONS_PER_BUS; i++) {
165 zdev = zbus->function[i];
166 if (zdev && zdev->is_physfn) {
167 pdev = pci_get_slot(zbus->bus, zdev->devfn);
168 cand_devfn = pci_iov_virtfn_devfn(pdev, vfid);
169 if (cand_devfn == virtfn->devfn) {
170 rc = zpci_bus_link_virtfn(pdev, virtfn, vfid);
171 break;
175 return rc;
177 #else
178 static inline int zpci_bus_setup_virtfn(struct zpci_bus *zbus,
179 struct pci_dev *virtfn, int vfn)
181 virtfn->is_virtfn = 1;
182 virtfn->multifunction = 0;
183 return 0;
185 #endif
187 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
189 struct pci_bus *bus;
190 struct resource_entry *window, *n;
191 struct resource *res;
192 struct pci_dev *pdev;
193 int rc;
195 bus = zbus->bus;
196 if (!bus)
197 return -EINVAL;
199 pdev = pci_get_slot(bus, zdev->devfn);
200 if (pdev) {
201 /* Device is already known. */
202 pci_dev_put(pdev);
203 return 0;
206 rc = zpci_init_slot(zdev);
207 if (rc)
208 return rc;
209 zdev->has_hp_slot = 1;
211 resource_list_for_each_entry_safe(window, n, &zbus->resources) {
212 res = window->res;
213 pci_bus_add_resource(bus, res, 0);
216 pdev = pci_scan_single_device(bus, zdev->devfn);
217 if (pdev) {
218 if (!zdev->is_physfn) {
219 rc = zpci_bus_setup_virtfn(zbus, pdev, zdev->vfn);
220 if (rc)
221 goto failed_with_pdev;
223 pci_bus_add_device(pdev);
225 return 0;
227 failed_with_pdev:
228 pci_stop_and_remove_bus_device(pdev);
229 pci_dev_put(pdev);
230 return rc;
233 static void zpci_bus_add_devices(struct zpci_bus *zbus)
235 int i;
237 for (i = 1; i < ZPCI_FUNCTIONS_PER_BUS; i++)
238 if (zbus->function[i])
239 zpci_bus_add_device(zbus, zbus->function[i]);
241 pci_lock_rescan_remove();
242 pci_bus_add_devices(zbus->bus);
243 pci_unlock_rescan_remove();
246 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
248 struct zpci_bus *zbus = NULL;
249 int rc = -EBADF;
251 if (zpci_nb_devices == ZPCI_NR_DEVICES) {
252 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
253 zdev->fid, ZPCI_NR_DEVICES);
254 return -ENOSPC;
256 zpci_nb_devices++;
258 if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
259 return -EINVAL;
261 if (!s390_pci_no_rid && zdev->rid_available)
262 zbus = zpci_bus_get(zdev->pchid);
264 if (!zbus) {
265 zbus = zpci_bus_alloc(zdev->pchid);
266 if (!zbus)
267 return -ENOMEM;
270 zdev->zbus = zbus;
271 if (zbus->function[zdev->devfn]) {
272 pr_err("devfn %04x is already assigned\n", zdev->devfn);
273 goto error; /* rc already set */
275 zbus->function[zdev->devfn] = zdev;
277 zpci_setup_bus_resources(zdev, &zbus->resources);
279 if (zbus->bus) {
280 if (!zbus->multifunction) {
281 WARN_ONCE(1, "zbus is not multifunction\n");
282 goto error_bus;
284 if (!zdev->rid_available) {
285 WARN_ONCE(1, "rid_available not set for multifunction\n");
286 goto error_bus;
288 rc = zpci_bus_add_device(zbus, zdev);
289 if (rc)
290 goto error_bus;
291 } else if (zdev->devfn == 0) {
292 if (zbus->multifunction && !zdev->rid_available) {
293 WARN_ONCE(1, "rid_available not set on function 0 for multifunction\n");
294 goto error_bus;
296 rc = zpci_bus_scan(zbus, (u16)zdev->uid, ops);
297 if (rc)
298 goto error_bus;
299 zpci_bus_add_devices(zbus);
300 rc = zpci_init_slot(zdev);
301 if (rc)
302 goto error_bus;
303 zdev->has_hp_slot = 1;
304 zbus->multifunction = zdev->rid_available;
305 zbus->max_bus_speed = zdev->max_bus_speed;
306 } else {
307 zbus->multifunction = 1;
310 return 0;
312 error_bus:
313 zpci_nb_devices--;
314 zbus->function[zdev->devfn] = NULL;
315 error:
316 pr_err("Adding PCI function %08x failed\n", zdev->fid);
317 zpci_bus_put(zbus);
318 return rc;
321 void zpci_bus_device_unregister(struct zpci_dev *zdev)
323 struct zpci_bus *zbus = zdev->zbus;
325 zpci_nb_devices--;
326 zbus->function[zdev->devfn] = NULL;
327 zpci_bus_put(zbus);