Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright IBM Corp. 2020
0004  *
0005  * Author(s):
0006  *   Pierre Morel <pmorel@linux.ibm.com>
0007  *
0008  */
0009 
0010 #define KMSG_COMPONENT "zpci"
0011 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/slab.h>
0015 #include <linux/err.h>
0016 #include <linux/export.h>
0017 #include <linux/delay.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/jump_label.h>
0020 #include <linux/pci.h>
0021 #include <linux/printk.h>
0022 
0023 #include <asm/pci_clp.h>
0024 #include <asm/pci_dma.h>
0025 
0026 #include "pci_bus.h"
0027 #include "pci_iov.h"
0028 
0029 static LIST_HEAD(zbus_list);
0030 static DEFINE_MUTEX(zbus_list_lock);
0031 static int zpci_nb_devices;
0032 
0033 /* zpci_bus_prepare_device - Prepare a zPCI function for scanning
0034  * @zdev: the zPCI function to be prepared
0035  *
0036  * The PCI resources for the function are set up and added to its zbus and the
0037  * function is enabled. The function must be added to a zbus which must have
0038  * a PCI bus created. If an error occurs the zPCI function is not enabled.
0039  *
0040  * Return: 0 on success, an error code otherwise
0041  */
0042 static int zpci_bus_prepare_device(struct zpci_dev *zdev)
0043 {
0044     struct resource_entry *window, *n;
0045     struct resource *res;
0046     int rc;
0047 
0048     if (!zdev_enabled(zdev)) {
0049         rc = zpci_enable_device(zdev);
0050         if (rc)
0051             return rc;
0052         rc = zpci_dma_init_device(zdev);
0053         if (rc) {
0054             zpci_disable_device(zdev);
0055             return rc;
0056         }
0057     }
0058 
0059     if (!zdev->has_resources) {
0060         zpci_setup_bus_resources(zdev, &zdev->zbus->resources);
0061         resource_list_for_each_entry_safe(window, n, &zdev->zbus->resources) {
0062             res = window->res;
0063             pci_bus_add_resource(zdev->zbus->bus, res, 0);
0064         }
0065     }
0066 
0067     return 0;
0068 }
0069 
0070 /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
0071  * @zdev: the zdev to be scanned
0072  *
0073  * Scans the PCI function making it available to the common PCI code.
0074  *
0075  * Return: 0 on success, an error value otherwise
0076  */
0077 int zpci_bus_scan_device(struct zpci_dev *zdev)
0078 {
0079     struct pci_dev *pdev;
0080     int rc;
0081 
0082     rc = zpci_bus_prepare_device(zdev);
0083     if (rc)
0084         return rc;
0085 
0086     pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
0087     if (!pdev)
0088         return -ENODEV;
0089 
0090     pci_bus_add_device(pdev);
0091     pci_lock_rescan_remove();
0092     pci_bus_add_devices(zdev->zbus->bus);
0093     pci_unlock_rescan_remove();
0094 
0095     return 0;
0096 }
0097 
0098 /* zpci_bus_remove_device - Removes the given zdev from the PCI core
0099  * @zdev: the zdev to be removed from the PCI core
0100  * @set_error: if true the device's error state is set to permanent failure
0101  *
0102  * Sets a zPCI device to a configured but offline state; the zPCI
0103  * device is still accessible through its hotplug slot and the zPCI
0104  * API but is removed from the common code PCI bus, making it
0105  * no longer available to drivers.
0106  */
0107 void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
0108 {
0109     struct zpci_bus *zbus = zdev->zbus;
0110     struct pci_dev *pdev;
0111 
0112     if (!zdev->zbus->bus)
0113         return;
0114 
0115     pdev = pci_get_slot(zbus->bus, zdev->devfn);
0116     if (pdev) {
0117         if (set_error)
0118             pdev->error_state = pci_channel_io_perm_failure;
0119         if (pdev->is_virtfn) {
0120             zpci_iov_remove_virtfn(pdev, zdev->vfn);
0121             /* balance pci_get_slot */
0122             pci_dev_put(pdev);
0123             return;
0124         }
0125         pci_stop_and_remove_bus_device_locked(pdev);
0126         /* balance pci_get_slot */
0127         pci_dev_put(pdev);
0128     }
0129 }
0130 
0131 /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
0132  * @zbus: the zbus to be scanned
0133  *
0134  * Enables and scans all PCI functions on the bus making them available to the
0135  * common PCI code. If there is no function 0 on the zbus nothing is scanned. If
0136  * a function does not have a slot yet because it was added to the zbus before
0137  * function 0 the slot is created. If a PCI function fails to be initialized
0138  * an error will be returned but attempts will still be made for all other
0139  * functions on the bus.
0140  *
0141  * Return: 0 on success, an error value otherwise
0142  */
0143 int zpci_bus_scan_bus(struct zpci_bus *zbus)
0144 {
0145     struct zpci_dev *zdev;
0146     int devfn, rc, ret = 0;
0147 
0148     for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
0149         zdev = zbus->function[devfn];
0150         if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
0151             rc = zpci_bus_prepare_device(zdev);
0152             if (rc)
0153                 ret = -EIO;
0154         }
0155     }
0156 
0157     pci_lock_rescan_remove();
0158     pci_scan_child_bus(zbus->bus);
0159     pci_bus_add_devices(zbus->bus);
0160     pci_unlock_rescan_remove();
0161 
0162     return ret;
0163 }
0164 
0165 /* zpci_bus_scan_busses - Scan all registered busses
0166  *
0167  * Scan all available zbusses
0168  *
0169  */
0170 void zpci_bus_scan_busses(void)
0171 {
0172     struct zpci_bus *zbus = NULL;
0173 
0174     mutex_lock(&zbus_list_lock);
0175     list_for_each_entry(zbus, &zbus_list, bus_next) {
0176         zpci_bus_scan_bus(zbus);
0177         cond_resched();
0178     }
0179     mutex_unlock(&zbus_list_lock);
0180 }
0181 
0182 /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
0183  * @zbus: the zbus holding the zdevices
0184  * @fr: PCI root function that will determine the bus's domain, and bus speeed
0185  * @ops: the pci operations
0186  *
0187  * The PCI function @fr determines the domain (its UID), multifunction property
0188  * and maximum bus speed of the entire bus.
0189  *
0190  * Return: 0 on success, an error code otherwise
0191  */
0192 static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
0193 {
0194     struct pci_bus *bus;
0195     int domain;
0196 
0197     domain = zpci_alloc_domain((u16)fr->uid);
0198     if (domain < 0)
0199         return domain;
0200 
0201     zbus->domain_nr = domain;
0202     zbus->multifunction = fr->rid_available;
0203     zbus->max_bus_speed = fr->max_bus_speed;
0204 
0205     /*
0206      * Note that the zbus->resources are taken over and zbus->resources
0207      * is empty after a successful call
0208      */
0209     bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
0210     if (!bus) {
0211         zpci_free_domain(zbus->domain_nr);
0212         return -EFAULT;
0213     }
0214 
0215     zbus->bus = bus;
0216     pci_bus_add_devices(bus);
0217 
0218     return 0;
0219 }
0220 
0221 static void zpci_bus_release(struct kref *kref)
0222 {
0223     struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
0224 
0225     if (zbus->bus) {
0226         pci_lock_rescan_remove();
0227         pci_stop_root_bus(zbus->bus);
0228 
0229         zpci_free_domain(zbus->domain_nr);
0230         pci_free_resource_list(&zbus->resources);
0231 
0232         pci_remove_root_bus(zbus->bus);
0233         pci_unlock_rescan_remove();
0234     }
0235 
0236     mutex_lock(&zbus_list_lock);
0237     list_del(&zbus->bus_next);
0238     mutex_unlock(&zbus_list_lock);
0239     kfree(zbus);
0240 }
0241 
0242 static void zpci_bus_put(struct zpci_bus *zbus)
0243 {
0244     kref_put(&zbus->kref, zpci_bus_release);
0245 }
0246 
0247 static struct zpci_bus *zpci_bus_get(int pchid)
0248 {
0249     struct zpci_bus *zbus;
0250 
0251     mutex_lock(&zbus_list_lock);
0252     list_for_each_entry(zbus, &zbus_list, bus_next) {
0253         if (pchid == zbus->pchid) {
0254             kref_get(&zbus->kref);
0255             goto out_unlock;
0256         }
0257     }
0258     zbus = NULL;
0259 out_unlock:
0260     mutex_unlock(&zbus_list_lock);
0261     return zbus;
0262 }
0263 
0264 static struct zpci_bus *zpci_bus_alloc(int pchid)
0265 {
0266     struct zpci_bus *zbus;
0267 
0268     zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
0269     if (!zbus)
0270         return NULL;
0271 
0272     zbus->pchid = pchid;
0273     INIT_LIST_HEAD(&zbus->bus_next);
0274     mutex_lock(&zbus_list_lock);
0275     list_add_tail(&zbus->bus_next, &zbus_list);
0276     mutex_unlock(&zbus_list_lock);
0277 
0278     kref_init(&zbus->kref);
0279     INIT_LIST_HEAD(&zbus->resources);
0280 
0281     zbus->bus_resource.start = 0;
0282     zbus->bus_resource.end = ZPCI_BUS_NR;
0283     zbus->bus_resource.flags = IORESOURCE_BUS;
0284     pci_add_resource(&zbus->resources, &zbus->bus_resource);
0285 
0286     return zbus;
0287 }
0288 
0289 void pcibios_bus_add_device(struct pci_dev *pdev)
0290 {
0291     struct zpci_dev *zdev = to_zpci(pdev);
0292 
0293     /*
0294      * With pdev->no_vf_scan the common PCI probing code does not
0295      * perform PF/VF linking.
0296      */
0297     if (zdev->vfn) {
0298         zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
0299         pdev->no_command_memory = 1;
0300     }
0301 }
0302 
0303 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
0304 {
0305     int rc = -EINVAL;
0306 
0307     if (zbus->function[zdev->devfn]) {
0308         pr_err("devfn %04x is already assigned\n", zdev->devfn);
0309         return rc;
0310     }
0311 
0312     zdev->zbus = zbus;
0313     zbus->function[zdev->devfn] = zdev;
0314     zpci_nb_devices++;
0315 
0316     if (zbus->multifunction && !zdev->rid_available) {
0317         WARN_ONCE(1, "rid_available not set for multifunction\n");
0318         goto error;
0319     }
0320     rc = zpci_init_slot(zdev);
0321     if (rc)
0322         goto error;
0323     zdev->has_hp_slot = 1;
0324 
0325     return 0;
0326 
0327 error:
0328     zbus->function[zdev->devfn] = NULL;
0329     zdev->zbus = NULL;
0330     zpci_nb_devices--;
0331     return rc;
0332 }
0333 
0334 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
0335 {
0336     struct zpci_bus *zbus = NULL;
0337     int rc = -EBADF;
0338 
0339     if (zpci_nb_devices == ZPCI_NR_DEVICES) {
0340         pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
0341             zdev->fid, ZPCI_NR_DEVICES);
0342         return -ENOSPC;
0343     }
0344 
0345     if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
0346         return -EINVAL;
0347 
0348     if (!s390_pci_no_rid && zdev->rid_available)
0349         zbus = zpci_bus_get(zdev->pchid);
0350 
0351     if (!zbus) {
0352         zbus = zpci_bus_alloc(zdev->pchid);
0353         if (!zbus)
0354             return -ENOMEM;
0355     }
0356 
0357     if (!zbus->bus) {
0358         /* The UID of the first PCI function registered with a zpci_bus
0359          * is used as the domain number for that bus. Currently there
0360          * is exactly one zpci_bus per domain.
0361          */
0362         rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
0363         if (rc)
0364             goto error;
0365     }
0366 
0367     rc = zpci_bus_add_device(zbus, zdev);
0368     if (rc)
0369         goto error;
0370 
0371     return 0;
0372 
0373 error:
0374     pr_err("Adding PCI function %08x failed\n", zdev->fid);
0375     zpci_bus_put(zbus);
0376     return rc;
0377 }
0378 
0379 void zpci_bus_device_unregister(struct zpci_dev *zdev)
0380 {
0381     struct zpci_bus *zbus = zdev->zbus;
0382 
0383     zpci_nb_devices--;
0384     zbus->function[zdev->devfn] = NULL;
0385     zpci_bus_put(zbus);
0386 }