1abb95b75SNiklas Schnelle // SPDX-License-Identifier: GPL-2.0
2abb95b75SNiklas Schnelle /*
3abb95b75SNiklas Schnelle * Copyright IBM Corp. 2020
4abb95b75SNiklas Schnelle *
5abb95b75SNiklas Schnelle * Author(s):
6abb95b75SNiklas Schnelle * Niklas Schnelle <schnelle@linux.ibm.com>
7abb95b75SNiklas Schnelle *
8abb95b75SNiklas Schnelle */
9abb95b75SNiklas Schnelle
10abb95b75SNiklas Schnelle #define KMSG_COMPONENT "zpci"
11abb95b75SNiklas Schnelle #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12abb95b75SNiklas Schnelle
13abb95b75SNiklas Schnelle #include <linux/kernel.h>
14abb95b75SNiklas Schnelle #include <linux/pci.h>
15abb95b75SNiklas Schnelle
164904e194SNiklas Schnelle #include "pci_iov.h"
174904e194SNiklas Schnelle
18abb95b75SNiklas Schnelle static struct resource iov_res = {
19abb95b75SNiklas Schnelle .name = "PCI IOV res",
20abb95b75SNiklas Schnelle .start = 0,
21abb95b75SNiklas Schnelle .end = -1,
22abb95b75SNiklas Schnelle .flags = IORESOURCE_MEM,
23abb95b75SNiklas Schnelle };
24abb95b75SNiklas Schnelle
zpci_iov_map_resources(struct pci_dev * pdev)25abb95b75SNiklas Schnelle void zpci_iov_map_resources(struct pci_dev *pdev)
26abb95b75SNiklas Schnelle {
27abb95b75SNiklas Schnelle resource_size_t len;
28abb95b75SNiklas Schnelle int i;
29abb95b75SNiklas Schnelle
30abb95b75SNiklas Schnelle for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
31abb95b75SNiklas Schnelle int bar = i + PCI_IOV_RESOURCES;
32abb95b75SNiklas Schnelle
33abb95b75SNiklas Schnelle len = pci_resource_len(pdev, bar);
34abb95b75SNiklas Schnelle if (!len)
35abb95b75SNiklas Schnelle continue;
36abb95b75SNiklas Schnelle pdev->resource[bar].parent = &iov_res;
37abb95b75SNiklas Schnelle }
38abb95b75SNiklas Schnelle }
39abb95b75SNiklas Schnelle
zpci_iov_remove_virtfn(struct pci_dev * pdev,int vfn)40abb95b75SNiklas Schnelle void zpci_iov_remove_virtfn(struct pci_dev *pdev, int vfn)
41abb95b75SNiklas Schnelle {
42abb95b75SNiklas Schnelle pci_lock_rescan_remove();
43abb95b75SNiklas Schnelle /* Linux' vfid's start at 0 vfn at 1 */
44abb95b75SNiklas Schnelle pci_iov_remove_virtfn(pdev->physfn, vfn - 1);
45abb95b75SNiklas Schnelle pci_unlock_rescan_remove();
46abb95b75SNiklas Schnelle }
47abb95b75SNiklas Schnelle
zpci_iov_link_virtfn(struct pci_dev * pdev,struct pci_dev * virtfn,int vfid)48abb95b75SNiklas Schnelle static int zpci_iov_link_virtfn(struct pci_dev *pdev, struct pci_dev *virtfn, int vfid)
49abb95b75SNiklas Schnelle {
50abb95b75SNiklas Schnelle int rc;
51abb95b75SNiklas Schnelle
52abb95b75SNiklas Schnelle rc = pci_iov_sysfs_link(pdev, virtfn, vfid);
53abb95b75SNiklas Schnelle if (rc)
54abb95b75SNiklas Schnelle return rc;
55abb95b75SNiklas Schnelle
56abb95b75SNiklas Schnelle virtfn->is_virtfn = 1;
57abb95b75SNiklas Schnelle virtfn->multifunction = 0;
58abb95b75SNiklas Schnelle virtfn->physfn = pci_dev_get(pdev);
59abb95b75SNiklas Schnelle
60abb95b75SNiklas Schnelle return 0;
61abb95b75SNiklas Schnelle }
62abb95b75SNiklas Schnelle
6305793884SNiklas Schnelle /**
6405793884SNiklas Schnelle * zpci_iov_find_parent_pf - Find the parent PF, if any, of the given function
6505793884SNiklas Schnelle * @zbus: The bus that the PCI function is on, or would be added on
6605793884SNiklas Schnelle * @zdev: The PCI function
6705793884SNiklas Schnelle *
6805793884SNiklas Schnelle * Finds the parent PF, if it exists and is configured, of the given PCI function
6905793884SNiklas Schnelle * and increments its refcount. Th PF is searched for on the provided bus so the
7005793884SNiklas Schnelle * caller has to ensure that this is the correct bus to search. This function may
7105793884SNiklas Schnelle * be used before adding the PCI function to a zbus.
7205793884SNiklas Schnelle *
7305793884SNiklas Schnelle * Return: Pointer to the struct pci_dev of the parent PF or NULL if it not
7405793884SNiklas Schnelle * found. If the function is not a VF or has no RequesterID information,
7505793884SNiklas Schnelle * NULL is returned as well.
7605793884SNiklas Schnelle */
zpci_iov_find_parent_pf(struct zpci_bus * zbus,struct zpci_dev * zdev)77*2844ddbdSNiklas Schnelle struct pci_dev *zpci_iov_find_parent_pf(struct zpci_bus *zbus, struct zpci_dev *zdev)
78abb95b75SNiklas Schnelle {
7905793884SNiklas Schnelle int i, vfid, devfn, cand_devfn;
80abb95b75SNiklas Schnelle struct pci_dev *pdev;
81abb95b75SNiklas Schnelle
82abb95b75SNiklas Schnelle if (!zbus->multifunction)
8305793884SNiklas Schnelle return NULL;
8405793884SNiklas Schnelle /* Non-VFs and VFs without RID available don't have a parent */
8505793884SNiklas Schnelle if (!zdev->vfn || !zdev->rid_available)
8605793884SNiklas Schnelle return NULL;
8705793884SNiklas Schnelle /* Linux vfid starts at 0 vfn at 1 */
8805793884SNiklas Schnelle vfid = zdev->vfn - 1;
8905793884SNiklas Schnelle devfn = zdev->rid & ZPCI_RID_MASK_DEVFN;
9005793884SNiklas Schnelle /*
9105793884SNiklas Schnelle * If the parent PF for the given VF is also configured in the
92abb95b75SNiklas Schnelle * instance, it must be on the same zbus.
93abb95b75SNiklas Schnelle * We can then identify the parent PF by checking what
94abb95b75SNiklas Schnelle * devfn the VF would have if it belonged to that PF using the PF's
95abb95b75SNiklas Schnelle * stride and offset. Only if this candidate devfn matches the
96abb95b75SNiklas Schnelle * actual devfn will we link both functions.
97abb95b75SNiklas Schnelle */
98abb95b75SNiklas Schnelle for (i = 0; i < ZPCI_FUNCTIONS_PER_BUS; i++) {
99abb95b75SNiklas Schnelle zdev = zbus->function[i];
100abb95b75SNiklas Schnelle if (zdev && zdev->is_physfn) {
101abb95b75SNiklas Schnelle pdev = pci_get_slot(zbus->bus, zdev->devfn);
102abb95b75SNiklas Schnelle if (!pdev)
103abb95b75SNiklas Schnelle continue;
104abb95b75SNiklas Schnelle cand_devfn = pci_iov_virtfn_devfn(pdev, vfid);
10505793884SNiklas Schnelle if (cand_devfn == devfn)
10605793884SNiklas Schnelle return pdev;
107abb95b75SNiklas Schnelle /* balance pci_get_slot() */
108abb95b75SNiklas Schnelle pci_dev_put(pdev);
109abb95b75SNiklas Schnelle }
110abb95b75SNiklas Schnelle }
11105793884SNiklas Schnelle return NULL;
11205793884SNiklas Schnelle }
11305793884SNiklas Schnelle
zpci_iov_setup_virtfn(struct zpci_bus * zbus,struct pci_dev * virtfn,int vfn)11405793884SNiklas Schnelle int zpci_iov_setup_virtfn(struct zpci_bus *zbus, struct pci_dev *virtfn, int vfn)
11505793884SNiklas Schnelle {
11605793884SNiklas Schnelle struct zpci_dev *zdev = to_zpci(virtfn);
11705793884SNiklas Schnelle struct pci_dev *pdev_pf;
11805793884SNiklas Schnelle int rc = 0;
11905793884SNiklas Schnelle
12005793884SNiklas Schnelle pdev_pf = zpci_iov_find_parent_pf(zbus, zdev);
12105793884SNiklas Schnelle if (pdev_pf) {
12205793884SNiklas Schnelle /* Linux' vfids start at 0 while zdev->vfn starts at 1 */
12305793884SNiklas Schnelle rc = zpci_iov_link_virtfn(pdev_pf, virtfn, zdev->vfn - 1);
12405793884SNiklas Schnelle pci_dev_put(pdev_pf);
12505793884SNiklas Schnelle }
126abb95b75SNiklas Schnelle return rc;
127abb95b75SNiklas Schnelle }
128