Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/iov.c |
| 3 | * |
| 4 | * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com> |
| 5 | * |
| 6 | * PCI Express I/O Virtualization (IOV) support. |
| 7 | * Single Root IOV 1.0 |
Yu Zhao | 302b421 | 2009-05-18 13:51:32 +0800 | [diff] [blame] | 8 | * Address Translation Service 1.0 |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 15 | #include <linux/string.h> |
| 16 | #include <linux/delay.h> |
Joerg Roedel | 5cdede2 | 2011-04-04 15:55:18 +0200 | [diff] [blame] | 17 | #include <linux/pci-ats.h> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 18 | #include "pci.h" |
| 19 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 20 | #define VIRTFN_ID_LEN 16 |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 21 | |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 22 | static inline u8 virtfn_bus(struct pci_dev *dev, int id) |
| 23 | { |
| 24 | return dev->bus->number + ((dev->devfn + dev->sriov->offset + |
| 25 | dev->sriov->stride * id) >> 8); |
| 26 | } |
| 27 | |
| 28 | static inline u8 virtfn_devfn(struct pci_dev *dev, int id) |
| 29 | { |
| 30 | return (dev->devfn + dev->sriov->offset + |
| 31 | dev->sriov->stride * id) & 0xff; |
| 32 | } |
| 33 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 34 | static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr) |
| 35 | { |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 36 | struct pci_bus *child; |
| 37 | |
| 38 | if (bus->number == busnr) |
| 39 | return bus; |
| 40 | |
| 41 | child = pci_find_bus(pci_domain_nr(bus), busnr); |
| 42 | if (child) |
| 43 | return child; |
| 44 | |
| 45 | child = pci_add_new_bus(bus, NULL, busnr); |
| 46 | if (!child) |
| 47 | return NULL; |
| 48 | |
Yinghai Lu | b7eac05 | 2012-05-17 18:51:13 -0700 | [diff] [blame] | 49 | pci_bus_insert_busn_res(child, busnr, busnr); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 50 | |
| 51 | return child; |
| 52 | } |
| 53 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 54 | static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 55 | { |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 56 | if (physbus != virtbus && list_empty(&virtbus->devices)) |
| 57 | pci_remove_bus(virtbus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 58 | } |
| 59 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 60 | resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno) |
| 61 | { |
| 62 | if (!dev->is_physfn) |
| 63 | return 0; |
| 64 | |
| 65 | return dev->sriov->barsz[resno - PCI_IOV_RESOURCES]; |
| 66 | } |
| 67 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 68 | static int virtfn_add(struct pci_dev *dev, int id, int reset) |
| 69 | { |
| 70 | int i; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 71 | int rc = -ENOMEM; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 72 | u64 size; |
| 73 | char buf[VIRTFN_ID_LEN]; |
| 74 | struct pci_dev *virtfn; |
| 75 | struct resource *res; |
| 76 | struct pci_sriov *iov = dev->sriov; |
Gu Zheng | 8b1fce0 | 2013-05-25 21:48:31 +0800 | [diff] [blame] | 77 | struct pci_bus *bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 78 | |
| 79 | mutex_lock(&iov->dev->sriov->lock); |
Gu Zheng | 8b1fce0 | 2013-05-25 21:48:31 +0800 | [diff] [blame] | 80 | bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id)); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 81 | if (!bus) |
| 82 | goto failed; |
| 83 | |
| 84 | virtfn = pci_alloc_dev(bus); |
| 85 | if (!virtfn) |
| 86 | goto failed0; |
| 87 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 88 | virtfn->devfn = virtfn_devfn(dev, id); |
| 89 | virtfn->vendor = dev->vendor; |
| 90 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device); |
| 91 | pci_setup_device(virtfn); |
| 92 | virtfn->dev.parent = dev->dev.parent; |
Xudong Hao | fbf33f5 | 2013-05-31 12:21:29 +0800 | [diff] [blame] | 93 | virtfn->physfn = pci_dev_get(dev); |
| 94 | virtfn->is_virtfn = 1; |
Alex Williamson | aa931977 | 2014-01-09 08:36:08 -0700 | [diff] [blame] | 95 | virtfn->multifunction = 0; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 96 | |
| 97 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame^] | 98 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 99 | if (!res->parent) |
| 100 | continue; |
| 101 | virtfn->resource[i].name = pci_name(virtfn); |
| 102 | virtfn->resource[i].flags = res->flags; |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 103 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 104 | virtfn->resource[i].start = res->start + size * id; |
| 105 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; |
| 106 | rc = request_resource(res, &virtfn->resource[i]); |
| 107 | BUG_ON(rc); |
| 108 | } |
| 109 | |
| 110 | if (reset) |
Yu Zhao | 8c1c699 | 2009-06-13 15:52:13 +0800 | [diff] [blame] | 111 | __pci_reset_function(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 112 | |
| 113 | pci_device_add(virtfn, virtfn->bus); |
| 114 | mutex_unlock(&iov->dev->sriov->lock); |
| 115 | |
Yijing Wang | c893d13 | 2014-05-30 11:01:03 +0800 | [diff] [blame] | 116 | pci_bus_add_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 117 | sprintf(buf, "virtfn%u", id); |
| 118 | rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); |
| 119 | if (rc) |
| 120 | goto failed1; |
| 121 | rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn"); |
| 122 | if (rc) |
| 123 | goto failed2; |
| 124 | |
| 125 | kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE); |
| 126 | |
| 127 | return 0; |
| 128 | |
| 129 | failed2: |
| 130 | sysfs_remove_link(&dev->dev.kobj, buf); |
| 131 | failed1: |
| 132 | pci_dev_put(dev); |
| 133 | mutex_lock(&iov->dev->sriov->lock); |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 134 | pci_stop_and_remove_bus_device(virtfn); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 135 | failed0: |
| 136 | virtfn_remove_bus(dev->bus, bus); |
| 137 | failed: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 138 | mutex_unlock(&iov->dev->sriov->lock); |
| 139 | |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | static void virtfn_remove(struct pci_dev *dev, int id, int reset) |
| 144 | { |
| 145 | char buf[VIRTFN_ID_LEN]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 146 | struct pci_dev *virtfn; |
| 147 | struct pci_sriov *iov = dev->sriov; |
| 148 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 149 | virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), |
| 150 | virtfn_bus(dev, id), |
| 151 | virtfn_devfn(dev, id)); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 152 | if (!virtfn) |
| 153 | return; |
| 154 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 155 | if (reset) { |
| 156 | device_release_driver(&virtfn->dev); |
Yu Zhao | 8c1c699 | 2009-06-13 15:52:13 +0800 | [diff] [blame] | 157 | __pci_reset_function(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | sprintf(buf, "virtfn%u", id); |
| 161 | sysfs_remove_link(&dev->dev.kobj, buf); |
Yinghai Lu | 09cedbe | 2012-02-04 22:55:01 -0800 | [diff] [blame] | 162 | /* |
| 163 | * pci_stop_dev() could have been called for this virtfn already, |
| 164 | * so the directory for the virtfn may have been removed before. |
| 165 | * Double check to avoid spurious sysfs warnings. |
| 166 | */ |
| 167 | if (virtfn->dev.kobj.sd) |
| 168 | sysfs_remove_link(&virtfn->dev.kobj, "physfn"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 169 | |
| 170 | mutex_lock(&iov->dev->sriov->lock); |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 171 | pci_stop_and_remove_bus_device(virtfn); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 172 | virtfn_remove_bus(dev->bus, virtfn->bus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 173 | mutex_unlock(&iov->dev->sriov->lock); |
| 174 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 175 | /* balance pci_get_domain_bus_and_slot() */ |
| 176 | pci_dev_put(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 177 | pci_dev_put(dev); |
| 178 | } |
| 179 | |
| 180 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) |
| 181 | { |
| 182 | int rc; |
| 183 | int i, j; |
| 184 | int nres; |
| 185 | u16 offset, stride, initial; |
| 186 | struct resource *res; |
| 187 | struct pci_dev *pdev; |
| 188 | struct pci_sriov *iov = dev->sriov; |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 189 | int bars = 0; |
Bjorn Helgaas | 68f8e9f | 2015-03-25 16:23:42 +0800 | [diff] [blame] | 190 | u8 bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 191 | |
| 192 | if (!nr_virtfn) |
| 193 | return 0; |
| 194 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 195 | if (iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | |
| 198 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 199 | if (initial > iov->total_VFs || |
| 200 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs))) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 201 | return -EIO; |
| 202 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 203 | if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs || |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 204 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) |
| 205 | return -EINVAL; |
| 206 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 207 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset); |
| 208 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride); |
| 209 | if (!offset || (nr_virtfn > 1 && !stride)) |
| 210 | return -EIO; |
| 211 | |
| 212 | nres = 0; |
| 213 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 214 | bars |= (1 << (i + PCI_IOV_RESOURCES)); |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame^] | 215 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 216 | if (res->parent) |
| 217 | nres++; |
| 218 | } |
| 219 | if (nres != iov->nres) { |
| 220 | dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n"); |
| 221 | return -ENOMEM; |
| 222 | } |
| 223 | |
| 224 | iov->offset = offset; |
| 225 | iov->stride = stride; |
| 226 | |
Bjorn Helgaas | 68f8e9f | 2015-03-25 16:23:42 +0800 | [diff] [blame] | 227 | bus = virtfn_bus(dev, nr_virtfn - 1); |
| 228 | if (bus > dev->bus->busn_res.end) { |
| 229 | dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n", |
| 230 | nr_virtfn, bus, &dev->bus->busn_res); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 231 | return -ENOMEM; |
| 232 | } |
| 233 | |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 234 | if (pci_enable_resources(dev, bars)) { |
| 235 | dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n"); |
| 236 | return -ENOMEM; |
| 237 | } |
| 238 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 239 | if (iov->link != dev->devfn) { |
| 240 | pdev = pci_get_slot(dev->bus, iov->link); |
| 241 | if (!pdev) |
| 242 | return -ENODEV; |
| 243 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 244 | if (!pdev->is_physfn) { |
| 245 | pci_dev_put(pdev); |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 246 | return -ENOSYS; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 247 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 248 | |
| 249 | rc = sysfs_create_link(&dev->dev.kobj, |
| 250 | &pdev->dev.kobj, "dep_link"); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 251 | pci_dev_put(pdev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 252 | if (rc) |
| 253 | return rc; |
| 254 | } |
| 255 | |
Yijing Wang | 19b6984 | 2013-07-24 17:26:12 +0800 | [diff] [blame] | 256 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 257 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 258 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 259 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 260 | msleep(100); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 261 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 262 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 263 | iov->initial_VFs = initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 264 | if (nr_virtfn < initial) |
| 265 | initial = nr_virtfn; |
| 266 | |
| 267 | for (i = 0; i < initial; i++) { |
| 268 | rc = virtfn_add(dev, i, 0); |
| 269 | if (rc) |
| 270 | goto failed; |
| 271 | } |
| 272 | |
| 273 | kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 274 | iov->num_VFs = nr_virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 275 | |
| 276 | return 0; |
| 277 | |
| 278 | failed: |
| 279 | for (j = 0; j < i; j++) |
| 280 | virtfn_remove(dev, j, 0); |
| 281 | |
| 282 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 283 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 284 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
Yijing Wang | 19b6984 | 2013-07-24 17:26:12 +0800 | [diff] [blame] | 285 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 286 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 287 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 288 | |
| 289 | if (iov->link != dev->devfn) |
| 290 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 291 | |
| 292 | return rc; |
| 293 | } |
| 294 | |
| 295 | static void sriov_disable(struct pci_dev *dev) |
| 296 | { |
| 297 | int i; |
| 298 | struct pci_sriov *iov = dev->sriov; |
| 299 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 300 | if (!iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 301 | return; |
| 302 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 303 | for (i = 0; i < iov->num_VFs; i++) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 304 | virtfn_remove(dev, i, 0); |
| 305 | |
| 306 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 307 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 308 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 309 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 310 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 311 | |
| 312 | if (iov->link != dev->devfn) |
| 313 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 314 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 315 | iov->num_VFs = 0; |
Yijing Wang | 19b6984 | 2013-07-24 17:26:12 +0800 | [diff] [blame] | 316 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 319 | static int sriov_init(struct pci_dev *dev, int pos) |
| 320 | { |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 321 | int i, bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 322 | int rc; |
| 323 | int nres; |
| 324 | u32 pgsz; |
| 325 | u16 ctrl, total, offset, stride; |
| 326 | struct pci_sriov *iov; |
| 327 | struct resource *res; |
| 328 | struct pci_dev *pdev; |
| 329 | |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 330 | if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END && |
| 331 | pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 332 | return -ENODEV; |
| 333 | |
| 334 | pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 335 | if (ctrl & PCI_SRIOV_CTRL_VFE) { |
| 336 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0); |
| 337 | ssleep(1); |
| 338 | } |
| 339 | |
| 340 | pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total); |
| 341 | if (!total) |
| 342 | return 0; |
| 343 | |
| 344 | ctrl = 0; |
| 345 | list_for_each_entry(pdev, &dev->bus->devices, bus_list) |
| 346 | if (pdev->is_physfn) |
| 347 | goto found; |
| 348 | |
| 349 | pdev = NULL; |
| 350 | if (pci_ari_enabled(dev->bus)) |
| 351 | ctrl |= PCI_SRIOV_CTRL_ARI; |
| 352 | |
| 353 | found: |
| 354 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl); |
ethan.zhao | 045cc22 | 2013-11-06 22:49:13 +0800 | [diff] [blame] | 355 | pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 356 | pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset); |
| 357 | pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride); |
| 358 | if (!offset || (total > 1 && !stride)) |
| 359 | return -EIO; |
| 360 | |
| 361 | pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz); |
| 362 | i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0; |
| 363 | pgsz &= ~((1 << i) - 1); |
| 364 | if (!pgsz) |
| 365 | return -EIO; |
| 366 | |
| 367 | pgsz &= ~(pgsz - 1); |
Vaidyanathan Srinivasan | 8161fe9 | 2012-02-02 23:11:20 +0530 | [diff] [blame] | 368 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 369 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 370 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); |
| 371 | if (!iov) |
| 372 | return -ENOMEM; |
| 373 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 374 | nres = 0; |
| 375 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame^] | 376 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 377 | bar64 = __pci_read_base(dev, pci_bar_unknown, res, |
| 378 | pos + PCI_SRIOV_BAR + i * 4); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 379 | if (!res->flags) |
| 380 | continue; |
| 381 | if (resource_size(res) & (PAGE_SIZE - 1)) { |
| 382 | rc = -EIO; |
| 383 | goto failed; |
| 384 | } |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 385 | iov->barsz[i] = resource_size(res); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 386 | res->end = res->start + resource_size(res) * total - 1; |
Wei Yang | e88ae01 | 2015-03-25 16:23:43 +0800 | [diff] [blame] | 387 | dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n", |
| 388 | i, res, i, total); |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 389 | i += bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 390 | nres++; |
| 391 | } |
| 392 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 393 | iov->pos = pos; |
| 394 | iov->nres = nres; |
| 395 | iov->ctrl = ctrl; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 396 | iov->total_VFs = total; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 397 | iov->offset = offset; |
| 398 | iov->stride = stride; |
| 399 | iov->pgsz = pgsz; |
| 400 | iov->self = dev; |
| 401 | pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap); |
| 402 | pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link); |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 403 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) |
Yu Zhao | 4d135db | 2009-05-20 17:11:57 +0800 | [diff] [blame] | 404 | iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 405 | |
| 406 | if (pdev) |
| 407 | iov->dev = pci_dev_get(pdev); |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 408 | else |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 409 | iov->dev = dev; |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 410 | |
| 411 | mutex_init(&iov->lock); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 412 | |
| 413 | dev->sriov = iov; |
| 414 | dev->is_physfn = 1; |
| 415 | |
| 416 | return 0; |
| 417 | |
| 418 | failed: |
| 419 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame^] | 420 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 421 | res->flags = 0; |
| 422 | } |
| 423 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 424 | kfree(iov); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | static void sriov_release(struct pci_dev *dev) |
| 429 | { |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 430 | BUG_ON(dev->sriov->num_VFs); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 431 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 432 | if (dev != dev->sriov->dev) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 433 | pci_dev_put(dev->sriov->dev); |
| 434 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 435 | mutex_destroy(&dev->sriov->lock); |
| 436 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 437 | kfree(dev->sriov); |
| 438 | dev->sriov = NULL; |
| 439 | } |
| 440 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 441 | static void sriov_restore_state(struct pci_dev *dev) |
| 442 | { |
| 443 | int i; |
| 444 | u16 ctrl; |
| 445 | struct pci_sriov *iov = dev->sriov; |
| 446 | |
| 447 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl); |
| 448 | if (ctrl & PCI_SRIOV_CTRL_VFE) |
| 449 | return; |
| 450 | |
| 451 | for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) |
| 452 | pci_update_resource(dev, i); |
| 453 | |
| 454 | pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 455 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs); |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 456 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 457 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 458 | msleep(100); |
| 459 | } |
| 460 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 461 | /** |
| 462 | * pci_iov_init - initialize the IOV capability |
| 463 | * @dev: the PCI device |
| 464 | * |
| 465 | * Returns 0 on success, or negative on failure. |
| 466 | */ |
| 467 | int pci_iov_init(struct pci_dev *dev) |
| 468 | { |
| 469 | int pos; |
| 470 | |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 471 | if (!pci_is_pcie(dev)) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 472 | return -ENODEV; |
| 473 | |
| 474 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV); |
| 475 | if (pos) |
| 476 | return sriov_init(dev, pos); |
| 477 | |
| 478 | return -ENODEV; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * pci_iov_release - release resources used by the IOV capability |
| 483 | * @dev: the PCI device |
| 484 | */ |
| 485 | void pci_iov_release(struct pci_dev *dev) |
| 486 | { |
| 487 | if (dev->is_physfn) |
| 488 | sriov_release(dev); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * pci_iov_resource_bar - get position of the SR-IOV BAR |
| 493 | * @dev: the PCI device |
| 494 | * @resno: the resource number |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 495 | * |
| 496 | * Returns position of the BAR encapsulated in the SR-IOV capability. |
| 497 | */ |
Myron Stowe | 26ff46c | 2014-11-11 08:04:50 -0700 | [diff] [blame] | 498 | int pci_iov_resource_bar(struct pci_dev *dev, int resno) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 499 | { |
| 500 | if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END) |
| 501 | return 0; |
| 502 | |
| 503 | BUG_ON(!dev->is_physfn); |
| 504 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 505 | return dev->sriov->pos + PCI_SRIOV_BAR + |
| 506 | 4 * (resno - PCI_IOV_RESOURCES); |
| 507 | } |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 508 | |
| 509 | /** |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 510 | * pci_sriov_resource_alignment - get resource alignment for VF BAR |
| 511 | * @dev: the PCI device |
| 512 | * @resno: the resource number |
| 513 | * |
| 514 | * Returns the alignment of the VF BAR found in the SR-IOV capability. |
| 515 | * This is not the same as the resource size which is defined as |
| 516 | * the VF BAR size multiplied by the number of VFs. The alignment |
| 517 | * is just the VF BAR size. |
| 518 | */ |
Cam Macdonell | 0e52247 | 2010-09-07 17:25:20 -0700 | [diff] [blame] | 519 | resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno) |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 520 | { |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 521 | return pci_iov_resource_size(dev, resno); |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 525 | * pci_restore_iov_state - restore the state of the IOV capability |
| 526 | * @dev: the PCI device |
| 527 | */ |
| 528 | void pci_restore_iov_state(struct pci_dev *dev) |
| 529 | { |
| 530 | if (dev->is_physfn) |
| 531 | sriov_restore_state(dev); |
| 532 | } |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 533 | |
| 534 | /** |
| 535 | * pci_iov_bus_range - find bus range used by Virtual Function |
| 536 | * @bus: the PCI bus |
| 537 | * |
| 538 | * Returns max number of buses (exclude current one) used by Virtual |
| 539 | * Functions. |
| 540 | */ |
| 541 | int pci_iov_bus_range(struct pci_bus *bus) |
| 542 | { |
| 543 | int max = 0; |
| 544 | u8 busnr; |
| 545 | struct pci_dev *dev; |
| 546 | |
| 547 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 548 | if (!dev->is_physfn) |
| 549 | continue; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 550 | busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1); |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 551 | if (busnr > max) |
| 552 | max = busnr; |
| 553 | } |
| 554 | |
| 555 | return max ? max - bus->number : 0; |
| 556 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * pci_enable_sriov - enable the SR-IOV capability |
| 560 | * @dev: the PCI device |
Randy Dunlap | 52a8873 | 2009-04-01 17:45:30 -0700 | [diff] [blame] | 561 | * @nr_virtfn: number of virtual functions to enable |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 562 | * |
| 563 | * Returns 0 on success, or negative on failure. |
| 564 | */ |
| 565 | int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) |
| 566 | { |
| 567 | might_sleep(); |
| 568 | |
| 569 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 570 | return -ENOSYS; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 571 | |
| 572 | return sriov_enable(dev, nr_virtfn); |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(pci_enable_sriov); |
| 575 | |
| 576 | /** |
| 577 | * pci_disable_sriov - disable the SR-IOV capability |
| 578 | * @dev: the PCI device |
| 579 | */ |
| 580 | void pci_disable_sriov(struct pci_dev *dev) |
| 581 | { |
| 582 | might_sleep(); |
| 583 | |
| 584 | if (!dev->is_physfn) |
| 585 | return; |
| 586 | |
| 587 | sriov_disable(dev); |
| 588 | } |
| 589 | EXPORT_SYMBOL_GPL(pci_disable_sriov); |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 590 | |
| 591 | /** |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 592 | * pci_num_vf - return number of VFs associated with a PF device_release_driver |
| 593 | * @dev: the PCI device |
| 594 | * |
| 595 | * Returns number of VFs, or 0 if SR-IOV is not enabled. |
| 596 | */ |
| 597 | int pci_num_vf(struct pci_dev *dev) |
| 598 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 599 | if (!dev->is_physfn) |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 600 | return 0; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 601 | |
| 602 | return dev->sriov->num_VFs; |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 603 | } |
| 604 | EXPORT_SYMBOL_GPL(pci_num_vf); |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 605 | |
| 606 | /** |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 607 | * pci_vfs_assigned - returns number of VFs are assigned to a guest |
| 608 | * @dev: the PCI device |
| 609 | * |
| 610 | * Returns number of VFs belonging to this device that are assigned to a guest. |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 611 | * If device is not a physical function returns 0. |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 612 | */ |
| 613 | int pci_vfs_assigned(struct pci_dev *dev) |
| 614 | { |
| 615 | struct pci_dev *vfdev; |
| 616 | unsigned int vfs_assigned = 0; |
| 617 | unsigned short dev_id; |
| 618 | |
| 619 | /* only search if we are a PF */ |
| 620 | if (!dev->is_physfn) |
| 621 | return 0; |
| 622 | |
| 623 | /* |
| 624 | * determine the device ID for the VFs, the vendor ID will be the |
| 625 | * same as the PF so there is no need to check for that one |
| 626 | */ |
| 627 | pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id); |
| 628 | |
| 629 | /* loop through all the VFs to see if we own any that are assigned */ |
| 630 | vfdev = pci_get_device(dev->vendor, dev_id, NULL); |
| 631 | while (vfdev) { |
| 632 | /* |
| 633 | * It is considered assigned if it is a virtual function with |
| 634 | * our dev as the physical function and the assigned bit is set |
| 635 | */ |
| 636 | if (vfdev->is_virtfn && (vfdev->physfn == dev) && |
Ethan Zhao | be63497 | 2014-09-09 10:21:28 +0800 | [diff] [blame] | 637 | pci_is_dev_assigned(vfdev)) |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 638 | vfs_assigned++; |
| 639 | |
| 640 | vfdev = pci_get_device(dev->vendor, dev_id, vfdev); |
| 641 | } |
| 642 | |
| 643 | return vfs_assigned; |
| 644 | } |
| 645 | EXPORT_SYMBOL_GPL(pci_vfs_assigned); |
| 646 | |
| 647 | /** |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 648 | * pci_sriov_set_totalvfs -- reduce the TotalVFs available |
| 649 | * @dev: the PCI PF device |
Randy Dunlap | 2094f16 | 2013-01-09 17:12:52 -0800 | [diff] [blame] | 650 | * @numvfs: number that should be used for TotalVFs supported |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 651 | * |
| 652 | * Should be called from PF driver's probe routine with |
| 653 | * device's mutex held. |
| 654 | * |
| 655 | * Returns 0 if PF is an SRIOV-capable device and |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 656 | * value of numvfs valid. If not a PF return -ENOSYS; |
| 657 | * if numvfs is invalid return -EINVAL; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 658 | * if VFs already enabled, return -EBUSY. |
| 659 | */ |
| 660 | int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs) |
| 661 | { |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 662 | if (!dev->is_physfn) |
| 663 | return -ENOSYS; |
| 664 | if (numvfs > dev->sriov->total_VFs) |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | |
| 667 | /* Shouldn't change if VFs already enabled */ |
| 668 | if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 669 | return -EBUSY; |
| 670 | else |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 671 | dev->sriov->driver_max_VFs = numvfs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs); |
| 676 | |
| 677 | /** |
Jonghwan Choi | ddc191f | 2013-07-08 14:02:43 -0600 | [diff] [blame] | 678 | * pci_sriov_get_totalvfs -- get total VFs supported on this device |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 679 | * @dev: the PCI PF device |
| 680 | * |
| 681 | * For a PCIe device with SRIOV support, return the PCIe |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 682 | * SRIOV capability value of TotalVFs or the value of driver_max_VFs |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 683 | * if the driver reduced it. Otherwise 0. |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 684 | */ |
| 685 | int pci_sriov_get_totalvfs(struct pci_dev *dev) |
| 686 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 687 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 688 | return 0; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 689 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 690 | if (dev->sriov->driver_max_VFs) |
| 691 | return dev->sriov->driver_max_VFs; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 692 | |
| 693 | return dev->sriov->total_VFs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 694 | } |
| 695 | EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs); |