s390/pci: Mark all VFs as not implementing PCI_COMMAND_MEMORY
[linux-2.6-microblaze.git] / arch / s390 / pci / pci_bus.c
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
28 static LIST_HEAD(zbus_list);
29 static DEFINE_SPINLOCK(zbus_list_lock);
30 static int zpci_nb_devices;
31
32 /* zpci_bus_scan
33  * @zbus: the zbus holding the zdevices
34  * @ops: the pci operations
35  *
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.
39  */
40 static int zpci_bus_scan(struct zpci_bus *zbus, int domain, struct pci_ops *ops)
41 {
42         struct pci_bus *bus;
43         int rc;
44
45         rc = zpci_alloc_domain(domain);
46         if (rc < 0)
47                 return rc;
48         zbus->domain_nr = rc;
49
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;
54         }
55
56         zbus->bus = bus;
57         pci_bus_add_devices(bus);
58         return 0;
59 }
60
61 static void zpci_bus_release(struct kref *kref)
62 {
63         struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
64
65         if (zbus->bus) {
66                 pci_lock_rescan_remove();
67                 pci_stop_root_bus(zbus->bus);
68
69                 zpci_free_domain(zbus->domain_nr);
70                 pci_free_resource_list(&zbus->resources);
71
72                 pci_remove_root_bus(zbus->bus);
73                 pci_unlock_rescan_remove();
74         }
75
76         spin_lock(&zbus_list_lock);
77         list_del(&zbus->bus_next);
78         spin_unlock(&zbus_list_lock);
79         kfree(zbus);
80 }
81
82 static void zpci_bus_put(struct zpci_bus *zbus)
83 {
84         kref_put(&zbus->kref, zpci_bus_release);
85 }
86
87 static struct zpci_bus *zpci_bus_get(int pchid)
88 {
89         struct zpci_bus *zbus;
90
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;
96                 }
97         }
98         zbus = NULL;
99 out_unlock:
100         spin_unlock(&zbus_list_lock);
101         return zbus;
102 }
103
104 static struct zpci_bus *zpci_bus_alloc(int pchid)
105 {
106         struct zpci_bus *zbus;
107
108         zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
109         if (!zbus)
110                 return NULL;
111
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);
117
118         kref_init(&zbus->kref);
119         INIT_LIST_HEAD(&zbus->resources);
120
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);
125
126         return zbus;
127 }
128
129 #ifdef CONFIG_PCI_IOV
130 static int zpci_bus_link_virtfn(struct pci_dev *pdev,
131                 struct pci_dev *virtfn, int vfid)
132 {
133         int rc;
134
135         rc = pci_iov_sysfs_link(pdev, virtfn, vfid);
136         if (rc)
137                 return rc;
138
139         virtfn->is_virtfn = 1;
140         virtfn->multifunction = 0;
141         virtfn->physfn = pci_dev_get(pdev);
142
143         return 0;
144 }
145
146 static int zpci_bus_setup_virtfn(struct zpci_bus *zbus,
147                 struct pci_dev *virtfn, int vfn)
148 {
149         int i, cand_devfn;
150         struct zpci_dev *zdev;
151         struct pci_dev *pdev;
152         int vfid = vfn - 1; /* Linux' vfid's start at 0 vfn at 1*/
153         int rc = 0;
154
155         if (!zbus->multifunction)
156                 return 0;
157
158         /* If the parent PF for the given VF is also configured in the
159          * instance, it must be on the same zbus.
160          * We can then identify the parent PF by checking what
161          * devfn the VF would have if it belonged to that PF using the PF's
162          * stride and offset. Only if this candidate devfn matches the
163          * actual devfn will we link both functions.
164          */
165         for (i = 0; i < ZPCI_FUNCTIONS_PER_BUS; i++) {
166                 zdev = zbus->function[i];
167                 if (zdev && zdev->is_physfn) {
168                         pdev = pci_get_slot(zbus->bus, zdev->devfn);
169                         if (!pdev)
170                                 continue;
171                         cand_devfn = pci_iov_virtfn_devfn(pdev, vfid);
172                         if (cand_devfn == virtfn->devfn) {
173                                 rc = zpci_bus_link_virtfn(pdev, virtfn, vfid);
174                                 /* balance pci_get_slot() */
175                                 pci_dev_put(pdev);
176                                 break;
177                         }
178                         /* balance pci_get_slot() */
179                         pci_dev_put(pdev);
180                 }
181         }
182         return rc;
183 }
184 #else
185 static inline int zpci_bus_setup_virtfn(struct zpci_bus *zbus,
186                 struct pci_dev *virtfn, int vfn)
187 {
188         return 0;
189 }
190 #endif
191
192 void pcibios_bus_add_device(struct pci_dev *pdev)
193 {
194         struct zpci_dev *zdev = to_zpci(pdev);
195
196         /*
197          * With pdev->no_vf_scan the common PCI probing code does not
198          * perform PF/VF linking.
199          */
200         if (zdev->vfn) {
201                 zpci_bus_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
202                 pdev->no_command_memory = 1;
203         }
204 }
205
206 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
207 {
208         struct pci_bus *bus;
209         struct resource_entry *window, *n;
210         struct resource *res;
211         struct pci_dev *pdev;
212         int rc;
213
214         bus = zbus->bus;
215         if (!bus)
216                 return -EINVAL;
217
218         pdev = pci_get_slot(bus, zdev->devfn);
219         if (pdev) {
220                 /* Device is already known. */
221                 pci_dev_put(pdev);
222                 return 0;
223         }
224
225         rc = zpci_init_slot(zdev);
226         if (rc)
227                 return rc;
228         zdev->has_hp_slot = 1;
229
230         resource_list_for_each_entry_safe(window, n, &zbus->resources) {
231                 res = window->res;
232                 pci_bus_add_resource(bus, res, 0);
233         }
234
235         pdev = pci_scan_single_device(bus, zdev->devfn);
236         if (pdev)
237                 pci_bus_add_device(pdev);
238
239         return 0;
240 }
241
242 static void zpci_bus_add_devices(struct zpci_bus *zbus)
243 {
244         int i;
245
246         for (i = 1; i < ZPCI_FUNCTIONS_PER_BUS; i++)
247                 if (zbus->function[i])
248                         zpci_bus_add_device(zbus, zbus->function[i]);
249
250         pci_lock_rescan_remove();
251         pci_bus_add_devices(zbus->bus);
252         pci_unlock_rescan_remove();
253 }
254
255 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
256 {
257         struct zpci_bus *zbus = NULL;
258         int rc = -EBADF;
259
260         if (zpci_nb_devices == ZPCI_NR_DEVICES) {
261                 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
262                         zdev->fid, ZPCI_NR_DEVICES);
263                 return -ENOSPC;
264         }
265         zpci_nb_devices++;
266
267         if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
268                 return -EINVAL;
269
270         if (!s390_pci_no_rid && zdev->rid_available)
271                 zbus = zpci_bus_get(zdev->pchid);
272
273         if (!zbus) {
274                 zbus = zpci_bus_alloc(zdev->pchid);
275                 if (!zbus)
276                         return -ENOMEM;
277         }
278
279         zdev->zbus = zbus;
280         if (zbus->function[zdev->devfn]) {
281                 pr_err("devfn %04x is already assigned\n", zdev->devfn);
282                 goto error; /* rc already set */
283         }
284         zbus->function[zdev->devfn] = zdev;
285
286         zpci_setup_bus_resources(zdev, &zbus->resources);
287
288         if (zbus->bus) {
289                 if (!zbus->multifunction) {
290                         WARN_ONCE(1, "zbus is not multifunction\n");
291                         goto error_bus;
292                 }
293                 if (!zdev->rid_available) {
294                         WARN_ONCE(1, "rid_available not set for multifunction\n");
295                         goto error_bus;
296                 }
297                 rc = zpci_bus_add_device(zbus, zdev);
298                 if (rc)
299                         goto error_bus;
300         } else if (zdev->devfn == 0) {
301                 if (zbus->multifunction && !zdev->rid_available) {
302                         WARN_ONCE(1, "rid_available not set on function 0 for multifunction\n");
303                         goto error_bus;
304                 }
305                 rc = zpci_bus_scan(zbus, (u16)zdev->uid, ops);
306                 if (rc)
307                         goto error_bus;
308                 zpci_bus_add_devices(zbus);
309                 rc = zpci_init_slot(zdev);
310                 if (rc)
311                         goto error_bus;
312                 zdev->has_hp_slot = 1;
313                 zbus->multifunction = zdev->rid_available;
314                 zbus->max_bus_speed = zdev->max_bus_speed;
315         } else {
316                 zbus->multifunction = 1;
317         }
318
319         return 0;
320
321 error_bus:
322         zpci_nb_devices--;
323         zbus->function[zdev->devfn] = NULL;
324 error:
325         pr_err("Adding PCI function %08x failed\n", zdev->fid);
326         zpci_bus_put(zbus);
327         return rc;
328 }
329
330 void zpci_bus_device_unregister(struct zpci_dev *zdev)
331 {
332         struct zpci_bus *zbus = zdev->zbus;
333
334         zpci_nb_devices--;
335         zbus->function[zdev->devfn] = NULL;
336         zpci_bus_put(zbus);
337 }