Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 
0003 #include <linux/kernel.h>
0004 #include <linux/ioport.h>
0005 #include <linux/bitmap.h>
0006 #include <linux/pci.h>
0007 
0008 #include <asm/opal.h>
0009 
0010 #include "pci.h"
0011 
0012 /*
0013  * The majority of the complexity in supporting SR-IOV on PowerNV comes from
0014  * the need to put the MMIO space for each VF into a separate PE. Internally
0015  * the PHB maps MMIO addresses to a specific PE using the "Memory BAR Table".
0016  * The MBT historically only applied to the 64bit MMIO window of the PHB
0017  * so it's common to see it referred to as the "M64BT".
0018  *
0019  * An MBT entry stores the mapped range as an <base>,<mask> pair. This forces
0020  * the address range that we want to map to be power-of-two sized and aligned.
0021  * For conventional PCI devices this isn't really an issue since PCI device BARs
0022  * have the same requirement.
0023  *
0024  * For a SR-IOV BAR things are a little more awkward since size and alignment
0025  * are not coupled. The alignment is set based on the per-VF BAR size, but
0026  * the total BAR area is: number-of-vfs * per-vf-size. The number of VFs
0027  * isn't necessarily a power of two, so neither is the total size. To fix that
0028  * we need to finesse (read: hack) the Linux BAR allocator so that it will
0029  * allocate the SR-IOV BARs in a way that lets us map them using the MBT.
0030  *
0031  * The changes to size and alignment that we need to do depend on the "mode"
0032  * of MBT entry that we use. We only support SR-IOV on PHB3 (IODA2) and above,
0033  * so as a baseline we can assume that we have the following BAR modes
0034  * available:
0035  *
0036  *   NB: $PE_COUNT is the number of PEs that the PHB supports.
0037  *
0038  * a) A segmented BAR that splits the mapped range into $PE_COUNT equally sized
0039  *    segments. The n'th segment is mapped to the n'th PE.
0040  * b) An un-segmented BAR that maps the whole address range to a specific PE.
0041  *
0042  *
0043  * We prefer to use mode a) since it only requires one MBT entry per SR-IOV BAR
0044  * For comparison b) requires one entry per-VF per-BAR, or:
0045  * (num-vfs * num-sriov-bars) in total. To use a) we need the size of each segment
0046  * to equal the size of the per-VF BAR area. So:
0047  *
0048  *  new_size = per-vf-size * number-of-PEs
0049  *
0050  * The alignment for the SR-IOV BAR also needs to be changed from per-vf-size
0051  * to "new_size", calculated above. Implementing this is a convoluted process
0052  * which requires several hooks in the PCI core:
0053  *
0054  * 1. In pcibios_device_add() we call pnv_pci_ioda_fixup_iov().
0055  *
0056  *    At this point the device has been probed and the device's BARs are sized,
0057  *    but no resource allocations have been done. The SR-IOV BARs are sized
0058  *    based on the maximum number of VFs supported by the device and we need
0059  *    to increase that to new_size.
0060  *
0061  * 2. Later, when Linux actually assigns resources it tries to make the resource
0062  *    allocations for each PCI bus as compact as possible. As a part of that it
0063  *    sorts the BARs on a bus by their required alignment, which is calculated
0064  *    using pci_resource_alignment().
0065  *
0066  *    For IOV resources this goes:
0067  *    pci_resource_alignment()
0068  *        pci_sriov_resource_alignment()
0069  *            pcibios_sriov_resource_alignment()
0070  *                pnv_pci_iov_resource_alignment()
0071  *
0072  *    Our hook overrides the default alignment, equal to the per-vf-size, with
0073  *    new_size computed above.
0074  *
0075  * 3. When userspace enables VFs for a device:
0076  *
0077  *    sriov_enable()
0078  *       pcibios_sriov_enable()
0079  *           pnv_pcibios_sriov_enable()
0080  *
0081  *    This is where we actually allocate PE numbers for each VF and setup the
0082  *    MBT mapping for each SR-IOV BAR. In steps 1) and 2) we setup an "arena"
0083  *    where each MBT segment is equal in size to the VF BAR so we can shift
0084  *    around the actual SR-IOV BAR location within this arena. We need this
0085  *    ability because the PE space is shared by all devices on the same PHB.
0086  *    When using mode a) described above segment 0 in maps to PE#0 which might
0087  *    be already being used by another device on the PHB.
0088  *
0089  *    As a result we need allocate a contigious range of PE numbers, then shift
0090  *    the address programmed into the SR-IOV BAR of the PF so that the address
0091  *    of VF0 matches up with the segment corresponding to the first allocated
0092  *    PE number. This is handled in pnv_pci_vf_resource_shift().
0093  *
0094  *    Once all that is done we return to the PCI core which then enables VFs,
0095  *    scans them and creates pci_devs for each. The init process for a VF is
0096  *    largely the same as a normal device, but the VF is inserted into the IODA
0097  *    PE that we allocated for it rather than the PE associated with the bus.
0098  *
0099  * 4. When userspace disables VFs we unwind the above in
0100  *    pnv_pcibios_sriov_disable(). Fortunately this is relatively simple since
0101  *    we don't need to validate anything, just tear down the mappings and
0102  *    move SR-IOV resource back to its "proper" location.
0103  *
0104  * That's how mode a) works. In theory mode b) (single PE mapping) is less work
0105  * since we can map each individual VF with a separate BAR. However, there's a
0106  * few limitations:
0107  *
0108  * 1) For IODA2 mode b) has a minimum alignment requirement of 32MB. This makes
0109  *    it only usable for devices with very large per-VF BARs. Such devices are
0110  *    similar to Big Foot. They definitely exist, but I've never seen one.
0111  *
0112  * 2) The number of MBT entries that we have is limited. PHB3 and PHB4 only
0113  *    16 total and some are needed for. Most SR-IOV capable network cards can support
0114  *    more than 16 VFs on each port.
0115  *
0116  * We use b) when using a) would use more than 1/4 of the entire 64 bit MMIO
0117  * window of the PHB.
0118  *
0119  *
0120  *
0121  * PHB4 (IODA3) added a few new features that would be useful for SR-IOV. It
0122  * allowed the MBT to map 32bit MMIO space in addition to 64bit which allows
0123  * us to support SR-IOV BARs in the 32bit MMIO window. This is useful since
0124  * the Linux BAR allocation will place any BAR marked as non-prefetchable into
0125  * the non-prefetchable bridge window, which is 32bit only. It also added two
0126  * new modes:
0127  *
0128  * c) A segmented BAR similar to a), but each segment can be individually
0129  *    mapped to any PE. This is matches how the 32bit MMIO window worked on
0130  *    IODA1&2.
0131  *
0132  * d) A segmented BAR with 8, 64, or 128 segments. This works similarly to a),
0133  *    but with fewer segments and configurable base PE.
0134  *
0135  *    i.e. The n'th segment maps to the (n + base)'th PE.
0136  *
0137  *    The base PE is also required to be a multiple of the window size.
0138  *
0139  * Unfortunately, the OPAL API doesn't currently (as of skiboot v6.6) allow us
0140  * to exploit any of the IODA3 features.
0141  */
0142 
0143 static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
0144 {
0145     struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
0146     struct resource *res;
0147     int i;
0148     resource_size_t vf_bar_sz;
0149     struct pnv_iov_data *iov;
0150     int mul;
0151 
0152     iov = kzalloc(sizeof(*iov), GFP_KERNEL);
0153     if (!iov)
0154         goto disable_iov;
0155     pdev->dev.archdata.iov_data = iov;
0156     mul = phb->ioda.total_pe_num;
0157 
0158     for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0159         res = &pdev->resource[i + PCI_IOV_RESOURCES];
0160         if (!res->flags || res->parent)
0161             continue;
0162         if (!pnv_pci_is_m64_flags(res->flags)) {
0163             dev_warn(&pdev->dev, "Don't support SR-IOV with non M64 VF BAR%d: %pR. \n",
0164                  i, res);
0165             goto disable_iov;
0166         }
0167 
0168         vf_bar_sz = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
0169 
0170         /*
0171          * Generally, one segmented M64 BAR maps one IOV BAR. However,
0172          * if a VF BAR is too large we end up wasting a lot of space.
0173          * If each VF needs more than 1/4 of the default m64 segment
0174          * then each VF BAR should be mapped in single-PE mode to reduce
0175          * the amount of space required. This does however limit the
0176          * number of VFs we can support.
0177          *
0178          * The 1/4 limit is arbitrary and can be tweaked.
0179          */
0180         if (vf_bar_sz > (phb->ioda.m64_segsize >> 2)) {
0181             /*
0182              * On PHB3, the minimum size alignment of M64 BAR in
0183              * single mode is 32MB. If this VF BAR is smaller than
0184              * 32MB, but still too large for a segmented window
0185              * then we can't map it and need to disable SR-IOV for
0186              * this device.
0187              */
0188             if (vf_bar_sz < SZ_32M) {
0189                 pci_err(pdev, "VF BAR%d: %pR can't be mapped in single PE mode\n",
0190                     i, res);
0191                 goto disable_iov;
0192             }
0193 
0194             iov->m64_single_mode[i] = true;
0195             continue;
0196         }
0197 
0198         /*
0199          * This BAR can be mapped with one segmented window, so adjust
0200          * te resource size to accommodate.
0201          */
0202         pci_dbg(pdev, " Fixing VF BAR%d: %pR to\n", i, res);
0203         res->end = res->start + vf_bar_sz * mul - 1;
0204         pci_dbg(pdev, "                       %pR\n", res);
0205 
0206         pci_info(pdev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
0207              i, res, mul);
0208 
0209         iov->need_shift = true;
0210     }
0211 
0212     return;
0213 
0214 disable_iov:
0215     /* Save ourselves some MMIO space by disabling the unusable BARs */
0216     for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0217         res = &pdev->resource[i + PCI_IOV_RESOURCES];
0218         res->flags = 0;
0219         res->end = res->start - 1;
0220     }
0221 
0222     pdev->dev.archdata.iov_data = NULL;
0223     kfree(iov);
0224 }
0225 
0226 void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev)
0227 {
0228     if (pdev->is_virtfn) {
0229         struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev);
0230 
0231         /*
0232          * VF PEs are single-device PEs so their pdev pointer needs to
0233          * be set. The pdev doesn't exist when the PE is allocated (in
0234          * (pcibios_sriov_enable()) so we fix it up here.
0235          */
0236         pe->pdev = pdev;
0237         WARN_ON(!(pe->flags & PNV_IODA_PE_VF));
0238     } else if (pdev->is_physfn) {
0239         /*
0240          * For PFs adjust their allocated IOV resources to match what
0241          * the PHB can support using it's M64 BAR table.
0242          */
0243         pnv_pci_ioda_fixup_iov_resources(pdev);
0244     }
0245 }
0246 
0247 resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
0248                               int resno)
0249 {
0250     resource_size_t align = pci_iov_resource_size(pdev, resno);
0251     struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
0252     struct pnv_iov_data *iov = pnv_iov_get(pdev);
0253 
0254     /*
0255      * iov can be null if we have an SR-IOV device with IOV BAR that can't
0256      * be placed in the m64 space (i.e. The BAR is 32bit or non-prefetch).
0257      * In that case we don't allow VFs to be enabled since one of their
0258      * BARs would not be placed in the correct PE.
0259      */
0260     if (!iov)
0261         return align;
0262 
0263     /*
0264      * If we're using single mode then we can just use the native VF BAR
0265      * alignment. We validated that it's possible to use a single PE
0266      * window above when we did the fixup.
0267      */
0268     if (iov->m64_single_mode[resno - PCI_IOV_RESOURCES])
0269         return align;
0270 
0271     /*
0272      * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
0273      * SR-IOV. While from hardware perspective, the range mapped by M64
0274      * BAR should be size aligned.
0275      *
0276      * This function returns the total IOV BAR size if M64 BAR is in
0277      * Shared PE mode or just VF BAR size if not.
0278      * If the M64 BAR is in Single PE mode, return the VF BAR size or
0279      * M64 segment size if IOV BAR size is less.
0280      */
0281     return phb->ioda.total_pe_num * align;
0282 }
0283 
0284 static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
0285 {
0286     struct pnv_iov_data   *iov;
0287     struct pnv_phb        *phb;
0288     int window_id;
0289 
0290     phb = pci_bus_to_pnvhb(pdev->bus);
0291     iov = pnv_iov_get(pdev);
0292 
0293     for_each_set_bit(window_id, iov->used_m64_bar_mask, MAX_M64_BARS) {
0294         opal_pci_phb_mmio_enable(phb->opal_id,
0295                      OPAL_M64_WINDOW_TYPE,
0296                      window_id,
0297                      0);
0298 
0299         clear_bit(window_id, &phb->ioda.m64_bar_alloc);
0300     }
0301 
0302     return 0;
0303 }
0304 
0305 
0306 /*
0307  * PHB3 and beyond support segmented windows. The window's address range
0308  * is subdivided into phb->ioda.total_pe_num segments and there's a 1-1
0309  * mapping between PEs and segments.
0310  */
0311 static int64_t pnv_ioda_map_m64_segmented(struct pnv_phb *phb,
0312                       int window_id,
0313                       resource_size_t start,
0314                       resource_size_t size)
0315 {
0316     int64_t rc;
0317 
0318     rc = opal_pci_set_phb_mem_window(phb->opal_id,
0319                      OPAL_M64_WINDOW_TYPE,
0320                      window_id,
0321                      start,
0322                      0, /* unused */
0323                      size);
0324     if (rc)
0325         goto out;
0326 
0327     rc = opal_pci_phb_mmio_enable(phb->opal_id,
0328                       OPAL_M64_WINDOW_TYPE,
0329                       window_id,
0330                       OPAL_ENABLE_M64_SPLIT);
0331 out:
0332     if (rc)
0333         pr_err("Failed to map M64 window #%d: %lld\n", window_id, rc);
0334 
0335     return rc;
0336 }
0337 
0338 static int64_t pnv_ioda_map_m64_single(struct pnv_phb *phb,
0339                        int pe_num,
0340                        int window_id,
0341                        resource_size_t start,
0342                        resource_size_t size)
0343 {
0344     int64_t rc;
0345 
0346     /*
0347      * The API for setting up m64 mmio windows seems to have been designed
0348      * with P7-IOC in mind. For that chip each M64 BAR (window) had a fixed
0349      * split of 8 equally sized segments each of which could individually
0350      * assigned to a PE.
0351      *
0352      * The problem with this is that the API doesn't have any way to
0353      * communicate the number of segments we want on a BAR. This wasn't
0354      * a problem for p7-ioc since you didn't have a choice, but the
0355      * single PE windows added in PHB3 don't map cleanly to this API.
0356      *
0357      * As a result we've got this slightly awkward process where we
0358      * call opal_pci_map_pe_mmio_window() to put the single in single
0359      * PE mode, and set the PE for the window before setting the address
0360      * bounds. We need to do it this way because the single PE windows
0361      * for PHB3 have different alignment requirements on PHB3.
0362      */
0363     rc = opal_pci_map_pe_mmio_window(phb->opal_id,
0364                      pe_num,
0365                      OPAL_M64_WINDOW_TYPE,
0366                      window_id,
0367                      0);
0368     if (rc)
0369         goto out;
0370 
0371     /*
0372      * NB: In single PE mode the window needs to be aligned to 32MB
0373      */
0374     rc = opal_pci_set_phb_mem_window(phb->opal_id,
0375                      OPAL_M64_WINDOW_TYPE,
0376                      window_id,
0377                      start,
0378                      0, /* ignored by FW, m64 is 1-1 */
0379                      size);
0380     if (rc)
0381         goto out;
0382 
0383     /*
0384      * Now actually enable it. We specified the BAR should be in "non-split"
0385      * mode so FW will validate that the BAR is in single PE mode.
0386      */
0387     rc = opal_pci_phb_mmio_enable(phb->opal_id,
0388                       OPAL_M64_WINDOW_TYPE,
0389                       window_id,
0390                       OPAL_ENABLE_M64_NON_SPLIT);
0391 out:
0392     if (rc)
0393         pr_err("Error mapping single PE BAR\n");
0394 
0395     return rc;
0396 }
0397 
0398 static int pnv_pci_alloc_m64_bar(struct pnv_phb *phb, struct pnv_iov_data *iov)
0399 {
0400     int win;
0401 
0402     do {
0403         win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
0404                 phb->ioda.m64_bar_idx + 1, 0);
0405 
0406         if (win >= phb->ioda.m64_bar_idx + 1)
0407             return -1;
0408     } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
0409 
0410     set_bit(win, iov->used_m64_bar_mask);
0411 
0412     return win;
0413 }
0414 
0415 static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
0416 {
0417     struct pnv_iov_data   *iov;
0418     struct pnv_phb        *phb;
0419     int                    win;
0420     struct resource       *res;
0421     int                    i, j;
0422     int64_t                rc;
0423     resource_size_t        size, start;
0424     int                    base_pe_num;
0425 
0426     phb = pci_bus_to_pnvhb(pdev->bus);
0427     iov = pnv_iov_get(pdev);
0428 
0429     for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0430         res = &pdev->resource[i + PCI_IOV_RESOURCES];
0431         if (!res->flags || !res->parent)
0432             continue;
0433 
0434         /* don't need single mode? map everything in one go! */
0435         if (!iov->m64_single_mode[i]) {
0436             win = pnv_pci_alloc_m64_bar(phb, iov);
0437             if (win < 0)
0438                 goto m64_failed;
0439 
0440             size = resource_size(res);
0441             start = res->start;
0442 
0443             rc = pnv_ioda_map_m64_segmented(phb, win, start, size);
0444             if (rc)
0445                 goto m64_failed;
0446 
0447             continue;
0448         }
0449 
0450         /* otherwise map each VF with single PE BARs */
0451         size = pci_iov_resource_size(pdev, PCI_IOV_RESOURCES + i);
0452         base_pe_num = iov->vf_pe_arr[0].pe_number;
0453 
0454         for (j = 0; j < num_vfs; j++) {
0455             win = pnv_pci_alloc_m64_bar(phb, iov);
0456             if (win < 0)
0457                 goto m64_failed;
0458 
0459             start = res->start + size * j;
0460             rc = pnv_ioda_map_m64_single(phb, win,
0461                              base_pe_num + j,
0462                              start,
0463                              size);
0464             if (rc)
0465                 goto m64_failed;
0466         }
0467     }
0468     return 0;
0469 
0470 m64_failed:
0471     pnv_pci_vf_release_m64(pdev, num_vfs);
0472     return -EBUSY;
0473 }
0474 
0475 static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
0476 {
0477     struct pnv_phb        *phb;
0478     struct pnv_ioda_pe    *pe, *pe_n;
0479 
0480     phb = pci_bus_to_pnvhb(pdev->bus);
0481 
0482     if (!pdev->is_physfn)
0483         return;
0484 
0485     /* FIXME: Use pnv_ioda_release_pe()? */
0486     list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
0487         if (pe->parent_dev != pdev)
0488             continue;
0489 
0490         pnv_pci_ioda2_release_pe_dma(pe);
0491 
0492         /* Remove from list */
0493         mutex_lock(&phb->ioda.pe_list_mutex);
0494         list_del(&pe->list);
0495         mutex_unlock(&phb->ioda.pe_list_mutex);
0496 
0497         pnv_ioda_deconfigure_pe(phb, pe);
0498 
0499         pnv_ioda_free_pe(pe);
0500     }
0501 }
0502 
0503 static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
0504 {
0505     struct resource *res, res2;
0506     struct pnv_iov_data *iov;
0507     resource_size_t size;
0508     u16 num_vfs;
0509     int i;
0510 
0511     if (!dev->is_physfn)
0512         return -EINVAL;
0513     iov = pnv_iov_get(dev);
0514 
0515     /*
0516      * "offset" is in VFs.  The M64 windows are sized so that when they
0517      * are segmented, each segment is the same size as the IOV BAR.
0518      * Each segment is in a separate PE, and the high order bits of the
0519      * address are the PE number.  Therefore, each VF's BAR is in a
0520      * separate PE, and changing the IOV BAR start address changes the
0521      * range of PEs the VFs are in.
0522      */
0523     num_vfs = iov->num_vfs;
0524     for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0525         res = &dev->resource[i + PCI_IOV_RESOURCES];
0526         if (!res->flags || !res->parent)
0527             continue;
0528         if (iov->m64_single_mode[i])
0529             continue;
0530 
0531         /*
0532          * The actual IOV BAR range is determined by the start address
0533          * and the actual size for num_vfs VFs BAR.  This check is to
0534          * make sure that after shifting, the range will not overlap
0535          * with another device.
0536          */
0537         size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
0538         res2.flags = res->flags;
0539         res2.start = res->start + (size * offset);
0540         res2.end = res2.start + (size * num_vfs) - 1;
0541 
0542         if (res2.end > res->end) {
0543             dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
0544                 i, &res2, res, num_vfs, offset);
0545             return -EBUSY;
0546         }
0547     }
0548 
0549     /*
0550      * Since M64 BAR shares segments among all possible 256 PEs,
0551      * we have to shift the beginning of PF IOV BAR to make it start from
0552      * the segment which belongs to the PE number assigned to the first VF.
0553      * This creates a "hole" in the /proc/iomem which could be used for
0554      * allocating other resources so we reserve this area below and
0555      * release when IOV is released.
0556      */
0557     for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0558         res = &dev->resource[i + PCI_IOV_RESOURCES];
0559         if (!res->flags || !res->parent)
0560             continue;
0561         if (iov->m64_single_mode[i])
0562             continue;
0563 
0564         size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
0565         res2 = *res;
0566         res->start += size * offset;
0567 
0568         dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
0569              i, &res2, res, (offset > 0) ? "En" : "Dis",
0570              num_vfs, offset);
0571 
0572         if (offset < 0) {
0573             devm_release_resource(&dev->dev, &iov->holes[i]);
0574             memset(&iov->holes[i], 0, sizeof(iov->holes[i]));
0575         }
0576 
0577         pci_update_resource(dev, i + PCI_IOV_RESOURCES);
0578 
0579         if (offset > 0) {
0580             iov->holes[i].start = res2.start;
0581             iov->holes[i].end = res2.start + size * offset - 1;
0582             iov->holes[i].flags = IORESOURCE_BUS;
0583             iov->holes[i].name = "pnv_iov_reserved";
0584             devm_request_resource(&dev->dev, res->parent,
0585                     &iov->holes[i]);
0586         }
0587     }
0588     return 0;
0589 }
0590 
0591 static void pnv_pci_sriov_disable(struct pci_dev *pdev)
0592 {
0593     u16                    num_vfs, base_pe;
0594     struct pnv_iov_data   *iov;
0595 
0596     iov = pnv_iov_get(pdev);
0597     num_vfs = iov->num_vfs;
0598     base_pe = iov->vf_pe_arr[0].pe_number;
0599 
0600     if (WARN_ON(!iov))
0601         return;
0602 
0603     /* Release VF PEs */
0604     pnv_ioda_release_vf_PE(pdev);
0605 
0606     /* Un-shift the IOV BARs if we need to */
0607     if (iov->need_shift)
0608         pnv_pci_vf_resource_shift(pdev, -base_pe);
0609 
0610     /* Release M64 windows */
0611     pnv_pci_vf_release_m64(pdev, num_vfs);
0612 }
0613 
0614 static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
0615 {
0616     struct pnv_phb        *phb;
0617     struct pnv_ioda_pe    *pe;
0618     int                    pe_num;
0619     u16                    vf_index;
0620     struct pnv_iov_data   *iov;
0621     struct pci_dn         *pdn;
0622 
0623     if (!pdev->is_physfn)
0624         return;
0625 
0626     phb = pci_bus_to_pnvhb(pdev->bus);
0627     pdn = pci_get_pdn(pdev);
0628     iov = pnv_iov_get(pdev);
0629 
0630     /* Reserve PE for each VF */
0631     for (vf_index = 0; vf_index < num_vfs; vf_index++) {
0632         int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index);
0633         int vf_bus = pci_iov_virtfn_bus(pdev, vf_index);
0634         struct pci_dn *vf_pdn;
0635 
0636         pe = &iov->vf_pe_arr[vf_index];
0637         pe->phb = phb;
0638         pe->flags = PNV_IODA_PE_VF;
0639         pe->pbus = NULL;
0640         pe->parent_dev = pdev;
0641         pe->mve_number = -1;
0642         pe->rid = (vf_bus << 8) | vf_devfn;
0643 
0644         pe_num = pe->pe_number;
0645         pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
0646             pci_domain_nr(pdev->bus), pdev->bus->number,
0647             PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num);
0648 
0649         if (pnv_ioda_configure_pe(phb, pe)) {
0650             /* XXX What do we do here ? */
0651             pnv_ioda_free_pe(pe);
0652             pe->pdev = NULL;
0653             continue;
0654         }
0655 
0656         /* Put PE to the list */
0657         mutex_lock(&phb->ioda.pe_list_mutex);
0658         list_add_tail(&pe->list, &phb->ioda.pe_list);
0659         mutex_unlock(&phb->ioda.pe_list_mutex);
0660 
0661         /* associate this pe to it's pdn */
0662         list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) {
0663             if (vf_pdn->busno == vf_bus &&
0664                 vf_pdn->devfn == vf_devfn) {
0665                 vf_pdn->pe_number = pe_num;
0666                 break;
0667             }
0668         }
0669 
0670         pnv_pci_ioda2_setup_dma_pe(phb, pe);
0671     }
0672 }
0673 
0674 static int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
0675 {
0676     struct pnv_ioda_pe    *base_pe;
0677     struct pnv_iov_data   *iov;
0678     struct pnv_phb        *phb;
0679     int                    ret;
0680     u16                    i;
0681 
0682     phb = pci_bus_to_pnvhb(pdev->bus);
0683     iov = pnv_iov_get(pdev);
0684 
0685     /*
0686      * There's a calls to IODA2 PE setup code littered throughout. We could
0687      * probably fix that, but we'd still have problems due to the
0688      * restriction inherent on IODA1 PHBs.
0689      *
0690      * NB: We class IODA3 as IODA2 since they're very similar.
0691      */
0692     if (phb->type != PNV_PHB_IODA2) {
0693         pci_err(pdev, "SR-IOV is not supported on this PHB\n");
0694         return -ENXIO;
0695     }
0696 
0697     if (!iov) {
0698         dev_info(&pdev->dev, "don't support this SRIOV device with non 64bit-prefetchable IOV BAR\n");
0699         return -ENOSPC;
0700     }
0701 
0702     /* allocate a contiguous block of PEs for our VFs */
0703     base_pe = pnv_ioda_alloc_pe(phb, num_vfs);
0704     if (!base_pe) {
0705         pci_err(pdev, "Unable to allocate PEs for %d VFs\n", num_vfs);
0706         return -EBUSY;
0707     }
0708 
0709     iov->vf_pe_arr = base_pe;
0710     iov->num_vfs = num_vfs;
0711 
0712     /* Assign M64 window accordingly */
0713     ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
0714     if (ret) {
0715         dev_info(&pdev->dev, "Not enough M64 window resources\n");
0716         goto m64_failed;
0717     }
0718 
0719     /*
0720      * When using one M64 BAR to map one IOV BAR, we need to shift
0721      * the IOV BAR according to the PE# allocated to the VFs.
0722      * Otherwise, the PE# for the VF will conflict with others.
0723      */
0724     if (iov->need_shift) {
0725         ret = pnv_pci_vf_resource_shift(pdev, base_pe->pe_number);
0726         if (ret)
0727             goto shift_failed;
0728     }
0729 
0730     /* Setup VF PEs */
0731     pnv_ioda_setup_vf_PE(pdev, num_vfs);
0732 
0733     return 0;
0734 
0735 shift_failed:
0736     pnv_pci_vf_release_m64(pdev, num_vfs);
0737 
0738 m64_failed:
0739     for (i = 0; i < num_vfs; i++)
0740         pnv_ioda_free_pe(&iov->vf_pe_arr[i]);
0741 
0742     return ret;
0743 }
0744 
0745 int pnv_pcibios_sriov_disable(struct pci_dev *pdev)
0746 {
0747     pnv_pci_sriov_disable(pdev);
0748 
0749     /* Release PCI data */
0750     remove_sriov_vf_pdns(pdev);
0751     return 0;
0752 }
0753 
0754 int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
0755 {
0756     /* Allocate PCI data */
0757     add_sriov_vf_pdns(pdev);
0758 
0759     return pnv_pci_sriov_enable(pdev, num_vfs);
0760 }