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); |
Yinghai Lu | 4f53509 | 2013-01-21 13:20:52 -0800 | [diff] [blame] | 50 | bus->is_added = 1; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 51 | |
| 52 | return child; |
| 53 | } |
| 54 | |
| 55 | static void virtfn_remove_bus(struct pci_bus *bus, int busnr) |
| 56 | { |
| 57 | struct pci_bus *child; |
| 58 | |
| 59 | if (bus->number == busnr) |
| 60 | return; |
| 61 | |
| 62 | child = pci_find_bus(pci_domain_nr(bus), busnr); |
| 63 | BUG_ON(!child); |
| 64 | |
| 65 | if (list_empty(&child->devices)) |
| 66 | pci_remove_bus(child); |
| 67 | } |
| 68 | |
| 69 | static int virtfn_add(struct pci_dev *dev, int id, int reset) |
| 70 | { |
| 71 | int i; |
| 72 | int rc; |
| 73 | u64 size; |
| 74 | char buf[VIRTFN_ID_LEN]; |
| 75 | struct pci_dev *virtfn; |
| 76 | struct resource *res; |
| 77 | struct pci_sriov *iov = dev->sriov; |
| 78 | |
| 79 | virtfn = alloc_pci_dev(); |
| 80 | if (!virtfn) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | mutex_lock(&iov->dev->sriov->lock); |
| 84 | virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id)); |
| 85 | if (!virtfn->bus) { |
| 86 | kfree(virtfn); |
| 87 | mutex_unlock(&iov->dev->sriov->lock); |
| 88 | return -ENOMEM; |
| 89 | } |
| 90 | virtfn->devfn = virtfn_devfn(dev, id); |
| 91 | virtfn->vendor = dev->vendor; |
| 92 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device); |
| 93 | pci_setup_device(virtfn); |
| 94 | virtfn->dev.parent = dev->dev.parent; |
Xudong Hao | fbf33f5 | 2013-05-31 12:21:29 +0800 | [diff] [blame^] | 95 | virtfn->physfn = pci_dev_get(dev); |
| 96 | virtfn->is_virtfn = 1; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 97 | |
| 98 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 99 | res = dev->resource + PCI_IOV_RESOURCES + i; |
| 100 | if (!res->parent) |
| 101 | continue; |
| 102 | virtfn->resource[i].name = pci_name(virtfn); |
| 103 | virtfn->resource[i].flags = res->flags; |
| 104 | size = resource_size(res); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 105 | do_div(size, iov->total_VFs); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 106 | virtfn->resource[i].start = res->start + size * id; |
| 107 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; |
| 108 | rc = request_resource(res, &virtfn->resource[i]); |
| 109 | BUG_ON(rc); |
| 110 | } |
| 111 | |
| 112 | if (reset) |
Yu Zhao | 8c1c699 | 2009-06-13 15:52:13 +0800 | [diff] [blame] | 113 | __pci_reset_function(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 114 | |
| 115 | pci_device_add(virtfn, virtfn->bus); |
| 116 | mutex_unlock(&iov->dev->sriov->lock); |
| 117 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 118 | rc = pci_bus_add_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 119 | sprintf(buf, "virtfn%u", id); |
| 120 | rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); |
| 121 | if (rc) |
| 122 | goto failed1; |
| 123 | rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn"); |
| 124 | if (rc) |
| 125 | goto failed2; |
| 126 | |
| 127 | kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE); |
| 128 | |
| 129 | return 0; |
| 130 | |
| 131 | failed2: |
| 132 | sysfs_remove_link(&dev->dev.kobj, buf); |
| 133 | failed1: |
| 134 | pci_dev_put(dev); |
| 135 | mutex_lock(&iov->dev->sriov->lock); |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 136 | pci_stop_and_remove_bus_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 137 | virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); |
| 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]; |
Bjorn Helgaas | 94bb346 | 2012-09-20 17:09:48 -0600 | [diff] [blame] | 146 | struct pci_bus *bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 147 | struct pci_dev *virtfn; |
| 148 | struct pci_sriov *iov = dev->sriov; |
| 149 | |
Bjorn Helgaas | 94bb346 | 2012-09-20 17:09:48 -0600 | [diff] [blame] | 150 | bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id)); |
| 151 | if (!bus) |
| 152 | return; |
| 153 | |
| 154 | virtfn = pci_get_slot(bus, virtfn_devfn(dev, id)); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 155 | if (!virtfn) |
| 156 | return; |
| 157 | |
| 158 | pci_dev_put(virtfn); |
| 159 | |
| 160 | if (reset) { |
| 161 | device_release_driver(&virtfn->dev); |
Yu Zhao | 8c1c699 | 2009-06-13 15:52:13 +0800 | [diff] [blame] | 162 | __pci_reset_function(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | sprintf(buf, "virtfn%u", id); |
| 166 | sysfs_remove_link(&dev->dev.kobj, buf); |
Yinghai Lu | 09cedbe | 2012-02-04 22:55:01 -0800 | [diff] [blame] | 167 | /* |
| 168 | * pci_stop_dev() could have been called for this virtfn already, |
| 169 | * so the directory for the virtfn may have been removed before. |
| 170 | * Double check to avoid spurious sysfs warnings. |
| 171 | */ |
| 172 | if (virtfn->dev.kobj.sd) |
| 173 | sysfs_remove_link(&virtfn->dev.kobj, "physfn"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 174 | |
| 175 | mutex_lock(&iov->dev->sriov->lock); |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 176 | pci_stop_and_remove_bus_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 177 | virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); |
| 178 | mutex_unlock(&iov->dev->sriov->lock); |
| 179 | |
| 180 | pci_dev_put(dev); |
| 181 | } |
| 182 | |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 183 | static int sriov_migration(struct pci_dev *dev) |
| 184 | { |
| 185 | u16 status; |
| 186 | struct pci_sriov *iov = dev->sriov; |
| 187 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 188 | if (!iov->num_VFs) |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 189 | return 0; |
| 190 | |
| 191 | if (!(iov->cap & PCI_SRIOV_CAP_VFM)) |
| 192 | return 0; |
| 193 | |
| 194 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status); |
| 195 | if (!(status & PCI_SRIOV_STATUS_VFM)) |
| 196 | return 0; |
| 197 | |
| 198 | schedule_work(&iov->mtask); |
| 199 | |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | static void sriov_migration_task(struct work_struct *work) |
| 204 | { |
| 205 | int i; |
| 206 | u8 state; |
| 207 | u16 status; |
| 208 | struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask); |
| 209 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 210 | for (i = iov->initial_VFs; i < iov->num_VFs; i++) { |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 211 | state = readb(iov->mstate + i); |
| 212 | if (state == PCI_SRIOV_VFM_MI) { |
| 213 | writeb(PCI_SRIOV_VFM_AV, iov->mstate + i); |
| 214 | state = readb(iov->mstate + i); |
| 215 | if (state == PCI_SRIOV_VFM_AV) |
| 216 | virtfn_add(iov->self, i, 1); |
| 217 | } else if (state == PCI_SRIOV_VFM_MO) { |
| 218 | virtfn_remove(iov->self, i, 1); |
| 219 | writeb(PCI_SRIOV_VFM_UA, iov->mstate + i); |
| 220 | state = readb(iov->mstate + i); |
| 221 | if (state == PCI_SRIOV_VFM_AV) |
| 222 | virtfn_add(iov->self, i, 0); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status); |
| 227 | status &= ~PCI_SRIOV_STATUS_VFM; |
| 228 | pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status); |
| 229 | } |
| 230 | |
| 231 | static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn) |
| 232 | { |
| 233 | int bir; |
| 234 | u32 table; |
| 235 | resource_size_t pa; |
| 236 | struct pci_sriov *iov = dev->sriov; |
| 237 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 238 | if (nr_virtfn <= iov->initial_VFs) |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 239 | return 0; |
| 240 | |
| 241 | pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table); |
| 242 | bir = PCI_SRIOV_VFM_BIR(table); |
| 243 | if (bir > PCI_STD_RESOURCE_END) |
| 244 | return -EIO; |
| 245 | |
| 246 | table = PCI_SRIOV_VFM_OFFSET(table); |
| 247 | if (table + nr_virtfn > pci_resource_len(dev, bir)) |
| 248 | return -EIO; |
| 249 | |
| 250 | pa = pci_resource_start(dev, bir) + table; |
| 251 | iov->mstate = ioremap(pa, nr_virtfn); |
| 252 | if (!iov->mstate) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | INIT_WORK(&iov->mtask, sriov_migration_task); |
| 256 | |
| 257 | iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR; |
| 258 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static void sriov_disable_migration(struct pci_dev *dev) |
| 264 | { |
| 265 | struct pci_sriov *iov = dev->sriov; |
| 266 | |
| 267 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR); |
| 268 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 269 | |
| 270 | cancel_work_sync(&iov->mtask); |
| 271 | iounmap(iov->mstate); |
| 272 | } |
| 273 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 274 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) |
| 275 | { |
| 276 | int rc; |
| 277 | int i, j; |
| 278 | int nres; |
| 279 | u16 offset, stride, initial; |
| 280 | struct resource *res; |
| 281 | struct pci_dev *pdev; |
| 282 | struct pci_sriov *iov = dev->sriov; |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 283 | int bars = 0; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 284 | |
| 285 | if (!nr_virtfn) |
| 286 | return 0; |
| 287 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 288 | if (iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 289 | return -EINVAL; |
| 290 | |
| 291 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 292 | if (initial > iov->total_VFs || |
| 293 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs))) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 294 | return -EIO; |
| 295 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 296 | if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs || |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 297 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); |
| 301 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset); |
| 302 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride); |
| 303 | if (!offset || (nr_virtfn > 1 && !stride)) |
| 304 | return -EIO; |
| 305 | |
| 306 | nres = 0; |
| 307 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 308 | bars |= (1 << (i + PCI_IOV_RESOURCES)); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 309 | res = dev->resource + PCI_IOV_RESOURCES + i; |
| 310 | if (res->parent) |
| 311 | nres++; |
| 312 | } |
| 313 | if (nres != iov->nres) { |
| 314 | dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n"); |
| 315 | return -ENOMEM; |
| 316 | } |
| 317 | |
| 318 | iov->offset = offset; |
| 319 | iov->stride = stride; |
| 320 | |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 321 | if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) { |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 322 | dev_err(&dev->dev, "SR-IOV: bus number out of range\n"); |
| 323 | return -ENOMEM; |
| 324 | } |
| 325 | |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 326 | if (pci_enable_resources(dev, bars)) { |
| 327 | dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n"); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 331 | if (iov->link != dev->devfn) { |
| 332 | pdev = pci_get_slot(dev->bus, iov->link); |
| 333 | if (!pdev) |
| 334 | return -ENODEV; |
| 335 | |
| 336 | pci_dev_put(pdev); |
| 337 | |
| 338 | if (!pdev->is_physfn) |
| 339 | return -ENODEV; |
| 340 | |
| 341 | rc = sysfs_create_link(&dev->dev.kobj, |
| 342 | &pdev->dev.kobj, "dep_link"); |
| 343 | if (rc) |
| 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 348 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 349 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 350 | msleep(100); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 351 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 352 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 353 | iov->initial_VFs = initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 354 | if (nr_virtfn < initial) |
| 355 | initial = nr_virtfn; |
| 356 | |
| 357 | for (i = 0; i < initial; i++) { |
| 358 | rc = virtfn_add(dev, i, 0); |
| 359 | if (rc) |
| 360 | goto failed; |
| 361 | } |
| 362 | |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 363 | if (iov->cap & PCI_SRIOV_CAP_VFM) { |
| 364 | rc = sriov_enable_migration(dev, nr_virtfn); |
| 365 | if (rc) |
| 366 | goto failed; |
| 367 | } |
| 368 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 369 | kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 370 | iov->num_VFs = nr_virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 371 | |
| 372 | return 0; |
| 373 | |
| 374 | failed: |
| 375 | for (j = 0; j < i; j++) |
| 376 | virtfn_remove(dev, j, 0); |
| 377 | |
| 378 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 379 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 380 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 381 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 382 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 383 | |
| 384 | if (iov->link != dev->devfn) |
| 385 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 386 | |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | static void sriov_disable(struct pci_dev *dev) |
| 391 | { |
| 392 | int i; |
| 393 | struct pci_sriov *iov = dev->sriov; |
| 394 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 395 | if (!iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 396 | return; |
| 397 | |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 398 | if (iov->cap & PCI_SRIOV_CAP_VFM) |
| 399 | sriov_disable_migration(dev); |
| 400 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 401 | for (i = 0; i < iov->num_VFs; i++) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 402 | virtfn_remove(dev, i, 0); |
| 403 | |
| 404 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 405 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 406 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 407 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 408 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 409 | |
| 410 | if (iov->link != dev->devfn) |
| 411 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 412 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 413 | iov->num_VFs = 0; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 416 | static int sriov_init(struct pci_dev *dev, int pos) |
| 417 | { |
| 418 | int i; |
| 419 | int rc; |
| 420 | int nres; |
| 421 | u32 pgsz; |
| 422 | u16 ctrl, total, offset, stride; |
| 423 | struct pci_sriov *iov; |
| 424 | struct resource *res; |
| 425 | struct pci_dev *pdev; |
| 426 | |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 427 | if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END && |
| 428 | pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 429 | return -ENODEV; |
| 430 | |
| 431 | pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 432 | if (ctrl & PCI_SRIOV_CTRL_VFE) { |
| 433 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0); |
| 434 | ssleep(1); |
| 435 | } |
| 436 | |
| 437 | pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total); |
| 438 | if (!total) |
| 439 | return 0; |
| 440 | |
| 441 | ctrl = 0; |
| 442 | list_for_each_entry(pdev, &dev->bus->devices, bus_list) |
| 443 | if (pdev->is_physfn) |
| 444 | goto found; |
| 445 | |
| 446 | pdev = NULL; |
| 447 | if (pci_ari_enabled(dev->bus)) |
| 448 | ctrl |= PCI_SRIOV_CTRL_ARI; |
| 449 | |
| 450 | found: |
| 451 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 452 | pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset); |
| 453 | pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride); |
| 454 | if (!offset || (total > 1 && !stride)) |
| 455 | return -EIO; |
| 456 | |
| 457 | pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz); |
| 458 | i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0; |
| 459 | pgsz &= ~((1 << i) - 1); |
| 460 | if (!pgsz) |
| 461 | return -EIO; |
| 462 | |
| 463 | pgsz &= ~(pgsz - 1); |
Vaidyanathan Srinivasan | 8161fe9 | 2012-02-02 23:11:20 +0530 | [diff] [blame] | 464 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 465 | |
| 466 | nres = 0; |
| 467 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 468 | res = dev->resource + PCI_IOV_RESOURCES + i; |
| 469 | i += __pci_read_base(dev, pci_bar_unknown, res, |
| 470 | pos + PCI_SRIOV_BAR + i * 4); |
| 471 | if (!res->flags) |
| 472 | continue; |
| 473 | if (resource_size(res) & (PAGE_SIZE - 1)) { |
| 474 | rc = -EIO; |
| 475 | goto failed; |
| 476 | } |
| 477 | res->end = res->start + resource_size(res) * total - 1; |
| 478 | nres++; |
| 479 | } |
| 480 | |
| 481 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); |
| 482 | if (!iov) { |
| 483 | rc = -ENOMEM; |
| 484 | goto failed; |
| 485 | } |
| 486 | |
| 487 | iov->pos = pos; |
| 488 | iov->nres = nres; |
| 489 | iov->ctrl = ctrl; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 490 | iov->total_VFs = total; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 491 | iov->offset = offset; |
| 492 | iov->stride = stride; |
| 493 | iov->pgsz = pgsz; |
| 494 | iov->self = dev; |
| 495 | pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap); |
| 496 | pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link); |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 497 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) |
Yu Zhao | 4d135db | 2009-05-20 17:11:57 +0800 | [diff] [blame] | 498 | iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 499 | |
| 500 | if (pdev) |
| 501 | iov->dev = pci_dev_get(pdev); |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 502 | else |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 503 | iov->dev = dev; |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 504 | |
| 505 | mutex_init(&iov->lock); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 506 | |
| 507 | dev->sriov = iov; |
| 508 | dev->is_physfn = 1; |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | failed: |
| 513 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 514 | res = dev->resource + PCI_IOV_RESOURCES + i; |
| 515 | res->flags = 0; |
| 516 | } |
| 517 | |
| 518 | return rc; |
| 519 | } |
| 520 | |
| 521 | static void sriov_release(struct pci_dev *dev) |
| 522 | { |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 523 | BUG_ON(dev->sriov->num_VFs); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 524 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 525 | if (dev != dev->sriov->dev) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 526 | pci_dev_put(dev->sriov->dev); |
| 527 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 528 | mutex_destroy(&dev->sriov->lock); |
| 529 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 530 | kfree(dev->sriov); |
| 531 | dev->sriov = NULL; |
| 532 | } |
| 533 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 534 | static void sriov_restore_state(struct pci_dev *dev) |
| 535 | { |
| 536 | int i; |
| 537 | u16 ctrl; |
| 538 | struct pci_sriov *iov = dev->sriov; |
| 539 | |
| 540 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl); |
| 541 | if (ctrl & PCI_SRIOV_CTRL_VFE) |
| 542 | return; |
| 543 | |
| 544 | for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) |
| 545 | pci_update_resource(dev, i); |
| 546 | |
| 547 | 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] | 548 | 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] | 549 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 550 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 551 | msleep(100); |
| 552 | } |
| 553 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 554 | /** |
| 555 | * pci_iov_init - initialize the IOV capability |
| 556 | * @dev: the PCI device |
| 557 | * |
| 558 | * Returns 0 on success, or negative on failure. |
| 559 | */ |
| 560 | int pci_iov_init(struct pci_dev *dev) |
| 561 | { |
| 562 | int pos; |
| 563 | |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 564 | if (!pci_is_pcie(dev)) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 565 | return -ENODEV; |
| 566 | |
| 567 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV); |
| 568 | if (pos) |
| 569 | return sriov_init(dev, pos); |
| 570 | |
| 571 | return -ENODEV; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * pci_iov_release - release resources used by the IOV capability |
| 576 | * @dev: the PCI device |
| 577 | */ |
| 578 | void pci_iov_release(struct pci_dev *dev) |
| 579 | { |
| 580 | if (dev->is_physfn) |
| 581 | sriov_release(dev); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * pci_iov_resource_bar - get position of the SR-IOV BAR |
| 586 | * @dev: the PCI device |
| 587 | * @resno: the resource number |
| 588 | * @type: the BAR type to be filled in |
| 589 | * |
| 590 | * Returns position of the BAR encapsulated in the SR-IOV capability. |
| 591 | */ |
| 592 | int pci_iov_resource_bar(struct pci_dev *dev, int resno, |
| 593 | enum pci_bar_type *type) |
| 594 | { |
| 595 | if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END) |
| 596 | return 0; |
| 597 | |
| 598 | BUG_ON(!dev->is_physfn); |
| 599 | |
| 600 | *type = pci_bar_unknown; |
| 601 | |
| 602 | return dev->sriov->pos + PCI_SRIOV_BAR + |
| 603 | 4 * (resno - PCI_IOV_RESOURCES); |
| 604 | } |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 605 | |
| 606 | /** |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 607 | * pci_sriov_resource_alignment - get resource alignment for VF BAR |
| 608 | * @dev: the PCI device |
| 609 | * @resno: the resource number |
| 610 | * |
| 611 | * Returns the alignment of the VF BAR found in the SR-IOV capability. |
| 612 | * This is not the same as the resource size which is defined as |
| 613 | * the VF BAR size multiplied by the number of VFs. The alignment |
| 614 | * is just the VF BAR size. |
| 615 | */ |
Cam Macdonell | 0e52247 | 2010-09-07 17:25:20 -0700 | [diff] [blame] | 616 | 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] | 617 | { |
| 618 | struct resource tmp; |
| 619 | enum pci_bar_type type; |
| 620 | int reg = pci_iov_resource_bar(dev, resno, &type); |
| 621 | |
| 622 | if (!reg) |
| 623 | return 0; |
| 624 | |
| 625 | __pci_read_base(dev, type, &tmp, reg); |
| 626 | return resource_alignment(&tmp); |
| 627 | } |
| 628 | |
| 629 | /** |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 630 | * pci_restore_iov_state - restore the state of the IOV capability |
| 631 | * @dev: the PCI device |
| 632 | */ |
| 633 | void pci_restore_iov_state(struct pci_dev *dev) |
| 634 | { |
| 635 | if (dev->is_physfn) |
| 636 | sriov_restore_state(dev); |
| 637 | } |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * pci_iov_bus_range - find bus range used by Virtual Function |
| 641 | * @bus: the PCI bus |
| 642 | * |
| 643 | * Returns max number of buses (exclude current one) used by Virtual |
| 644 | * Functions. |
| 645 | */ |
| 646 | int pci_iov_bus_range(struct pci_bus *bus) |
| 647 | { |
| 648 | int max = 0; |
| 649 | u8 busnr; |
| 650 | struct pci_dev *dev; |
| 651 | |
| 652 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 653 | if (!dev->is_physfn) |
| 654 | continue; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 655 | busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1); |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 656 | if (busnr > max) |
| 657 | max = busnr; |
| 658 | } |
| 659 | |
| 660 | return max ? max - bus->number : 0; |
| 661 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 662 | |
| 663 | /** |
| 664 | * pci_enable_sriov - enable the SR-IOV capability |
| 665 | * @dev: the PCI device |
Randy Dunlap | 52a8873 | 2009-04-01 17:45:30 -0700 | [diff] [blame] | 666 | * @nr_virtfn: number of virtual functions to enable |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 667 | * |
| 668 | * Returns 0 on success, or negative on failure. |
| 669 | */ |
| 670 | int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) |
| 671 | { |
| 672 | might_sleep(); |
| 673 | |
| 674 | if (!dev->is_physfn) |
| 675 | return -ENODEV; |
| 676 | |
| 677 | return sriov_enable(dev, nr_virtfn); |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(pci_enable_sriov); |
| 680 | |
| 681 | /** |
| 682 | * pci_disable_sriov - disable the SR-IOV capability |
| 683 | * @dev: the PCI device |
| 684 | */ |
| 685 | void pci_disable_sriov(struct pci_dev *dev) |
| 686 | { |
| 687 | might_sleep(); |
| 688 | |
| 689 | if (!dev->is_physfn) |
| 690 | return; |
| 691 | |
| 692 | sriov_disable(dev); |
| 693 | } |
| 694 | EXPORT_SYMBOL_GPL(pci_disable_sriov); |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 695 | |
| 696 | /** |
| 697 | * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration |
| 698 | * @dev: the PCI device |
| 699 | * |
| 700 | * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not. |
| 701 | * |
| 702 | * Physical Function driver is responsible to register IRQ handler using |
| 703 | * VF Migration Interrupt Message Number, and call this function when the |
| 704 | * interrupt is generated by the hardware. |
| 705 | */ |
| 706 | irqreturn_t pci_sriov_migration(struct pci_dev *dev) |
| 707 | { |
| 708 | if (!dev->is_physfn) |
| 709 | return IRQ_NONE; |
| 710 | |
| 711 | return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE; |
| 712 | } |
| 713 | EXPORT_SYMBOL_GPL(pci_sriov_migration); |
Yu Zhao | 302b421 | 2009-05-18 13:51:32 +0800 | [diff] [blame] | 714 | |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 715 | /** |
| 716 | * pci_num_vf - return number of VFs associated with a PF device_release_driver |
| 717 | * @dev: the PCI device |
| 718 | * |
| 719 | * Returns number of VFs, or 0 if SR-IOV is not enabled. |
| 720 | */ |
| 721 | int pci_num_vf(struct pci_dev *dev) |
| 722 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 723 | if (!dev->is_physfn) |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 724 | return 0; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 725 | |
| 726 | return dev->sriov->num_VFs; |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 727 | } |
| 728 | EXPORT_SYMBOL_GPL(pci_num_vf); |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 729 | |
| 730 | /** |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 731 | * pci_vfs_assigned - returns number of VFs are assigned to a guest |
| 732 | * @dev: the PCI device |
| 733 | * |
| 734 | * Returns number of VFs belonging to this device that are assigned to a guest. |
| 735 | * If device is not a physical function returns -ENODEV. |
| 736 | */ |
| 737 | int pci_vfs_assigned(struct pci_dev *dev) |
| 738 | { |
| 739 | struct pci_dev *vfdev; |
| 740 | unsigned int vfs_assigned = 0; |
| 741 | unsigned short dev_id; |
| 742 | |
| 743 | /* only search if we are a PF */ |
| 744 | if (!dev->is_physfn) |
| 745 | return 0; |
| 746 | |
| 747 | /* |
| 748 | * determine the device ID for the VFs, the vendor ID will be the |
| 749 | * same as the PF so there is no need to check for that one |
| 750 | */ |
| 751 | pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id); |
| 752 | |
| 753 | /* loop through all the VFs to see if we own any that are assigned */ |
| 754 | vfdev = pci_get_device(dev->vendor, dev_id, NULL); |
| 755 | while (vfdev) { |
| 756 | /* |
| 757 | * It is considered assigned if it is a virtual function with |
| 758 | * our dev as the physical function and the assigned bit is set |
| 759 | */ |
| 760 | if (vfdev->is_virtfn && (vfdev->physfn == dev) && |
| 761 | (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)) |
| 762 | vfs_assigned++; |
| 763 | |
| 764 | vfdev = pci_get_device(dev->vendor, dev_id, vfdev); |
| 765 | } |
| 766 | |
| 767 | return vfs_assigned; |
| 768 | } |
| 769 | EXPORT_SYMBOL_GPL(pci_vfs_assigned); |
| 770 | |
| 771 | /** |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 772 | * pci_sriov_set_totalvfs -- reduce the TotalVFs available |
| 773 | * @dev: the PCI PF device |
Randy Dunlap | 2094f16 | 2013-01-09 17:12:52 -0800 | [diff] [blame] | 774 | * @numvfs: number that should be used for TotalVFs supported |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 775 | * |
| 776 | * Should be called from PF driver's probe routine with |
| 777 | * device's mutex held. |
| 778 | * |
| 779 | * Returns 0 if PF is an SRIOV-capable device and |
| 780 | * value of numvfs valid. If not a PF with VFS, return -EINVAL; |
| 781 | * if VFs already enabled, return -EBUSY. |
| 782 | */ |
| 783 | int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs) |
| 784 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 785 | if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs)) |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 786 | return -EINVAL; |
| 787 | |
| 788 | /* Shouldn't change if VFs already enabled */ |
| 789 | if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 790 | return -EBUSY; |
| 791 | else |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 792 | dev->sriov->driver_max_VFs = numvfs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs); |
| 797 | |
| 798 | /** |
| 799 | * pci_sriov_get_totalvfs -- get total VFs supported on this devic3 |
| 800 | * @dev: the PCI PF device |
| 801 | * |
| 802 | * For a PCIe device with SRIOV support, return the PCIe |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 803 | * SRIOV capability value of TotalVFs or the value of driver_max_VFs |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 804 | * if the driver reduced it. Otherwise, -EINVAL. |
| 805 | */ |
| 806 | int pci_sriov_get_totalvfs(struct pci_dev *dev) |
| 807 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 808 | if (!dev->is_physfn) |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 809 | return -EINVAL; |
| 810 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 811 | if (dev->sriov->driver_max_VFs) |
| 812 | return dev->sriov->driver_max_VFs; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 813 | |
| 814 | return dev->sriov->total_VFs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 815 | } |
| 816 | EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs); |