1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2020
4 *
5 * Author(s):
6 * Niklas Schnelle <schnelle@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/pci.h>
15
16 #include "pci_iov.h"
17
18 static struct resource iov_res = {
19 .name = "PCI IOV res",
20 .start = 0,
21 .end = -1,
22 .flags = IORESOURCE_MEM,
23 };
24
zpci_iov_map_resources(struct pci_dev * pdev)25 void zpci_iov_map_resources(struct pci_dev *pdev)
26 {
27 resource_size_t len;
28 int i;
29
30 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
31 int bar = i + PCI_IOV_RESOURCES;
32
33 len = pci_resource_len(pdev, bar);
34 if (!len)
35 continue;
36 pdev->resource[bar].parent = &iov_res;
37 }
38 }
39
zpci_iov_remove_virtfn(struct pci_dev * pdev,int vfn)40 void zpci_iov_remove_virtfn(struct pci_dev *pdev, int vfn)
41 {
42 pci_lock_rescan_remove();
43 /* Linux' vfid's start at 0 vfn at 1 */
44 pci_iov_remove_virtfn(pdev->physfn, vfn - 1);
45 pci_unlock_rescan_remove();
46 }
47
zpci_iov_link_virtfn(struct pci_dev * pdev,struct pci_dev * virtfn,int vfid)48 static int zpci_iov_link_virtfn(struct pci_dev *pdev, struct pci_dev *virtfn, int vfid)
49 {
50 int rc;
51
52 rc = pci_iov_sysfs_link(pdev, virtfn, vfid);
53 if (rc)
54 return rc;
55
56 virtfn->is_virtfn = 1;
57 virtfn->multifunction = 0;
58 virtfn->physfn = pci_dev_get(pdev);
59
60 return 0;
61 }
62
63 /**
64 * zpci_iov_find_parent_pf - Find the parent PF, if any, of the given function
65 * @zbus: The bus that the PCI function is on, or would be added on
66 * @zdev: The PCI function
67 *
68 * Finds the parent PF, if it exists and is configured, of the given PCI function
69 * and increments its refcount. Th PF is searched for on the provided bus so the
70 * caller has to ensure that this is the correct bus to search. This function may
71 * be used before adding the PCI function to a zbus.
72 *
73 * Return: Pointer to the struct pci_dev of the parent PF or NULL if it not
74 * found. If the function is not a VF or has no RequesterID information,
75 * NULL is returned as well.
76 */
zpci_iov_find_parent_pf(struct zpci_bus * zbus,struct zpci_dev * zdev)77 struct pci_dev *zpci_iov_find_parent_pf(struct zpci_bus *zbus, struct zpci_dev *zdev)
78 {
79 int i, vfid, devfn, cand_devfn;
80 struct pci_dev *pdev;
81
82 if (!zbus->multifunction)
83 return NULL;
84 /* Non-VFs and VFs without RID available don't have a parent */
85 if (!zdev->vfn || !zdev->rid_available)
86 return NULL;
87 /* Linux vfid starts at 0 vfn at 1 */
88 vfid = zdev->vfn - 1;
89 devfn = zdev->rid & ZPCI_RID_MASK_DEVFN;
90 /*
91 * If the parent PF for the given VF is also configured in the
92 * instance, it must be on the same zbus.
93 * We can then identify the parent PF by checking what
94 * devfn the VF would have if it belonged to that PF using the PF's
95 * stride and offset. Only if this candidate devfn matches the
96 * actual devfn will we link both functions.
97 */
98 for (i = 0; i < ZPCI_FUNCTIONS_PER_BUS; i++) {
99 zdev = zbus->function[i];
100 if (zdev && zdev->is_physfn) {
101 pdev = pci_get_slot(zbus->bus, zdev->devfn);
102 if (!pdev)
103 continue;
104 cand_devfn = pci_iov_virtfn_devfn(pdev, vfid);
105 if (cand_devfn == devfn)
106 return pdev;
107 /* balance pci_get_slot() */
108 pci_dev_put(pdev);
109 }
110 }
111 return NULL;
112 }
113
zpci_iov_setup_virtfn(struct zpci_bus * zbus,struct pci_dev * virtfn,int vfn)114 int zpci_iov_setup_virtfn(struct zpci_bus *zbus, struct pci_dev *virtfn, int vfn)
115 {
116 struct zpci_dev *zdev = to_zpci(virtfn);
117 struct pci_dev *pdev_pf;
118 int rc = 0;
119
120 pdev_pf = zpci_iov_find_parent_pf(zbus, zdev);
121 if (pdev_pf) {
122 /* Linux' vfids start at 0 while zdev->vfn starts at 1 */
123 rc = zpci_iov_link_virtfn(pdev_pf, virtfn, zdev->vfn - 1);
124 pci_dev_put(pdev_pf);
125 }
126 return rc;
127 }
128