iov.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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
  8. * Address Translation Service 1.0
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/export.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/pci-ats.h>
  17. #include "pci.h"
  18. #define VIRTFN_ID_LEN 16
  19. static inline u8 virtfn_bus(struct pci_dev *dev, int id)
  20. {
  21. return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  22. dev->sriov->stride * id) >> 8);
  23. }
  24. static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
  25. {
  26. return (dev->devfn + dev->sriov->offset +
  27. dev->sriov->stride * id) & 0xff;
  28. }
  29. static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  30. {
  31. struct pci_bus *child;
  32. if (bus->number == busnr)
  33. return bus;
  34. child = pci_find_bus(pci_domain_nr(bus), busnr);
  35. if (child)
  36. return child;
  37. child = pci_add_new_bus(bus, NULL, busnr);
  38. if (!child)
  39. return NULL;
  40. pci_bus_insert_busn_res(child, busnr, busnr);
  41. return child;
  42. }
  43. static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
  44. {
  45. if (physbus != virtbus && list_empty(&virtbus->devices))
  46. pci_remove_bus(virtbus);
  47. }
  48. static int virtfn_add(struct pci_dev *dev, int id, int reset)
  49. {
  50. int i;
  51. int rc = -ENOMEM;
  52. u64 size;
  53. char buf[VIRTFN_ID_LEN];
  54. struct pci_dev *virtfn;
  55. struct resource *res;
  56. struct pci_sriov *iov = dev->sriov;
  57. struct pci_bus *bus;
  58. mutex_lock(&iov->dev->sriov->lock);
  59. bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
  60. if (!bus)
  61. goto failed;
  62. virtfn = pci_alloc_dev(bus);
  63. if (!virtfn)
  64. goto failed0;
  65. virtfn->devfn = virtfn_devfn(dev, id);
  66. virtfn->vendor = dev->vendor;
  67. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
  68. pci_setup_device(virtfn);
  69. virtfn->dev.parent = dev->dev.parent;
  70. virtfn->physfn = pci_dev_get(dev);
  71. virtfn->is_virtfn = 1;
  72. virtfn->multifunction = 0;
  73. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  74. res = dev->resource + PCI_IOV_RESOURCES + i;
  75. if (!res->parent)
  76. continue;
  77. virtfn->resource[i].name = pci_name(virtfn);
  78. virtfn->resource[i].flags = res->flags;
  79. size = resource_size(res);
  80. do_div(size, iov->total_VFs);
  81. virtfn->resource[i].start = res->start + size * id;
  82. virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
  83. rc = request_resource(res, &virtfn->resource[i]);
  84. BUG_ON(rc);
  85. }
  86. if (reset)
  87. __pci_reset_function(virtfn);
  88. pci_device_add(virtfn, virtfn->bus);
  89. mutex_unlock(&iov->dev->sriov->lock);
  90. pci_bus_add_device(virtfn);
  91. sprintf(buf, "virtfn%u", id);
  92. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  93. if (rc)
  94. goto failed1;
  95. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  96. if (rc)
  97. goto failed2;
  98. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  99. return 0;
  100. failed2:
  101. sysfs_remove_link(&dev->dev.kobj, buf);
  102. failed1:
  103. pci_dev_put(dev);
  104. mutex_lock(&iov->dev->sriov->lock);
  105. pci_stop_and_remove_bus_device(virtfn);
  106. failed0:
  107. virtfn_remove_bus(dev->bus, bus);
  108. failed:
  109. mutex_unlock(&iov->dev->sriov->lock);
  110. return rc;
  111. }
  112. static void virtfn_remove(struct pci_dev *dev, int id, int reset)
  113. {
  114. char buf[VIRTFN_ID_LEN];
  115. struct pci_dev *virtfn;
  116. struct pci_sriov *iov = dev->sriov;
  117. virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
  118. virtfn_bus(dev, id),
  119. virtfn_devfn(dev, id));
  120. if (!virtfn)
  121. return;
  122. if (reset) {
  123. device_release_driver(&virtfn->dev);
  124. __pci_reset_function(virtfn);
  125. }
  126. sprintf(buf, "virtfn%u", id);
  127. sysfs_remove_link(&dev->dev.kobj, buf);
  128. /*
  129. * pci_stop_dev() could have been called for this virtfn already,
  130. * so the directory for the virtfn may have been removed before.
  131. * Double check to avoid spurious sysfs warnings.
  132. */
  133. if (virtfn->dev.kobj.sd)
  134. sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  135. mutex_lock(&iov->dev->sriov->lock);
  136. pci_stop_and_remove_bus_device(virtfn);
  137. virtfn_remove_bus(dev->bus, virtfn->bus);
  138. mutex_unlock(&iov->dev->sriov->lock);
  139. /* balance pci_get_domain_bus_and_slot() */
  140. pci_dev_put(virtfn);
  141. pci_dev_put(dev);
  142. }
  143. static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
  144. {
  145. int rc;
  146. int i, j;
  147. int nres;
  148. u16 offset, stride, initial;
  149. struct resource *res;
  150. struct pci_dev *pdev;
  151. struct pci_sriov *iov = dev->sriov;
  152. int bars = 0;
  153. if (!nr_virtfn)
  154. return 0;
  155. if (iov->num_VFs)
  156. return -EINVAL;
  157. pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  158. if (initial > iov->total_VFs ||
  159. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
  160. return -EIO;
  161. if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
  162. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  163. return -EINVAL;
  164. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
  165. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
  166. if (!offset || (nr_virtfn > 1 && !stride))
  167. return -EIO;
  168. nres = 0;
  169. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  170. bars |= (1 << (i + PCI_IOV_RESOURCES));
  171. res = dev->resource + PCI_IOV_RESOURCES + i;
  172. if (res->parent)
  173. nres++;
  174. }
  175. if (nres != iov->nres) {
  176. dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
  177. return -ENOMEM;
  178. }
  179. iov->offset = offset;
  180. iov->stride = stride;
  181. if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
  182. dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
  183. return -ENOMEM;
  184. }
  185. if (pci_enable_resources(dev, bars)) {
  186. dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
  187. return -ENOMEM;
  188. }
  189. if (iov->link != dev->devfn) {
  190. pdev = pci_get_slot(dev->bus, iov->link);
  191. if (!pdev)
  192. return -ENODEV;
  193. if (!pdev->is_physfn) {
  194. pci_dev_put(pdev);
  195. return -ENOSYS;
  196. }
  197. rc = sysfs_create_link(&dev->dev.kobj,
  198. &pdev->dev.kobj, "dep_link");
  199. pci_dev_put(pdev);
  200. if (rc)
  201. return rc;
  202. }
  203. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  204. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  205. pci_cfg_access_lock(dev);
  206. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  207. msleep(100);
  208. pci_cfg_access_unlock(dev);
  209. iov->initial_VFs = initial;
  210. if (nr_virtfn < initial)
  211. initial = nr_virtfn;
  212. for (i = 0; i < initial; i++) {
  213. rc = virtfn_add(dev, i, 0);
  214. if (rc)
  215. goto failed;
  216. }
  217. kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  218. iov->num_VFs = nr_virtfn;
  219. return 0;
  220. failed:
  221. for (j = 0; j < i; j++)
  222. virtfn_remove(dev, j, 0);
  223. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  224. pci_cfg_access_lock(dev);
  225. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  226. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
  227. ssleep(1);
  228. pci_cfg_access_unlock(dev);
  229. if (iov->link != dev->devfn)
  230. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  231. return rc;
  232. }
  233. static void sriov_disable(struct pci_dev *dev)
  234. {
  235. int i;
  236. struct pci_sriov *iov = dev->sriov;
  237. if (!iov->num_VFs)
  238. return;
  239. for (i = 0; i < iov->num_VFs; i++)
  240. virtfn_remove(dev, i, 0);
  241. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  242. pci_cfg_access_lock(dev);
  243. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  244. ssleep(1);
  245. pci_cfg_access_unlock(dev);
  246. if (iov->link != dev->devfn)
  247. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  248. iov->num_VFs = 0;
  249. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
  250. }
  251. static int sriov_init(struct pci_dev *dev, int pos)
  252. {
  253. int i;
  254. int rc;
  255. int nres;
  256. u32 pgsz;
  257. u16 ctrl, total, offset, stride;
  258. struct pci_sriov *iov;
  259. struct resource *res;
  260. struct pci_dev *pdev;
  261. if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
  262. pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
  263. return -ENODEV;
  264. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  265. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  266. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  267. ssleep(1);
  268. }
  269. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  270. if (!total)
  271. return 0;
  272. ctrl = 0;
  273. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  274. if (pdev->is_physfn)
  275. goto found;
  276. pdev = NULL;
  277. if (pci_ari_enabled(dev->bus))
  278. ctrl |= PCI_SRIOV_CTRL_ARI;
  279. found:
  280. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  281. pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
  282. pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
  283. pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
  284. if (!offset || (total > 1 && !stride))
  285. return -EIO;
  286. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  287. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  288. pgsz &= ~((1 << i) - 1);
  289. if (!pgsz)
  290. return -EIO;
  291. pgsz &= ~(pgsz - 1);
  292. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  293. nres = 0;
  294. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  295. res = dev->resource + PCI_IOV_RESOURCES + i;
  296. i += __pci_read_base(dev, pci_bar_unknown, res,
  297. pos + PCI_SRIOV_BAR + i * 4);
  298. if (!res->flags)
  299. continue;
  300. if (resource_size(res) & (PAGE_SIZE - 1)) {
  301. rc = -EIO;
  302. goto failed;
  303. }
  304. res->end = res->start + resource_size(res) * total - 1;
  305. nres++;
  306. }
  307. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  308. if (!iov) {
  309. rc = -ENOMEM;
  310. goto failed;
  311. }
  312. iov->pos = pos;
  313. iov->nres = nres;
  314. iov->ctrl = ctrl;
  315. iov->total_VFs = total;
  316. iov->offset = offset;
  317. iov->stride = stride;
  318. iov->pgsz = pgsz;
  319. iov->self = dev;
  320. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  321. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  322. if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  323. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  324. if (pdev)
  325. iov->dev = pci_dev_get(pdev);
  326. else
  327. iov->dev = dev;
  328. mutex_init(&iov->lock);
  329. dev->sriov = iov;
  330. dev->is_physfn = 1;
  331. return 0;
  332. failed:
  333. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  334. res = dev->resource + PCI_IOV_RESOURCES + i;
  335. res->flags = 0;
  336. }
  337. return rc;
  338. }
  339. static void sriov_release(struct pci_dev *dev)
  340. {
  341. BUG_ON(dev->sriov->num_VFs);
  342. if (dev != dev->sriov->dev)
  343. pci_dev_put(dev->sriov->dev);
  344. mutex_destroy(&dev->sriov->lock);
  345. kfree(dev->sriov);
  346. dev->sriov = NULL;
  347. }
  348. static void sriov_restore_state(struct pci_dev *dev)
  349. {
  350. int i;
  351. u16 ctrl;
  352. struct pci_sriov *iov = dev->sriov;
  353. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  354. if (ctrl & PCI_SRIOV_CTRL_VFE)
  355. return;
  356. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  357. pci_update_resource(dev, i);
  358. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  359. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
  360. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  361. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  362. msleep(100);
  363. }
  364. /**
  365. * pci_iov_init - initialize the IOV capability
  366. * @dev: the PCI device
  367. *
  368. * Returns 0 on success, or negative on failure.
  369. */
  370. int pci_iov_init(struct pci_dev *dev)
  371. {
  372. int pos;
  373. if (!pci_is_pcie(dev))
  374. return -ENODEV;
  375. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  376. if (pos)
  377. return sriov_init(dev, pos);
  378. return -ENODEV;
  379. }
  380. /**
  381. * pci_iov_release - release resources used by the IOV capability
  382. * @dev: the PCI device
  383. */
  384. void pci_iov_release(struct pci_dev *dev)
  385. {
  386. if (dev->is_physfn)
  387. sriov_release(dev);
  388. }
  389. /**
  390. * pci_iov_resource_bar - get position of the SR-IOV BAR
  391. * @dev: the PCI device
  392. * @resno: the resource number
  393. * @type: the BAR type to be filled in
  394. *
  395. * Returns position of the BAR encapsulated in the SR-IOV capability.
  396. */
  397. int pci_iov_resource_bar(struct pci_dev *dev, int resno,
  398. enum pci_bar_type *type)
  399. {
  400. if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
  401. return 0;
  402. BUG_ON(!dev->is_physfn);
  403. *type = pci_bar_unknown;
  404. return dev->sriov->pos + PCI_SRIOV_BAR +
  405. 4 * (resno - PCI_IOV_RESOURCES);
  406. }
  407. /**
  408. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  409. * @dev: the PCI device
  410. * @resno: the resource number
  411. *
  412. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  413. * This is not the same as the resource size which is defined as
  414. * the VF BAR size multiplied by the number of VFs. The alignment
  415. * is just the VF BAR size.
  416. */
  417. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  418. {
  419. struct resource tmp;
  420. enum pci_bar_type type;
  421. int reg = pci_iov_resource_bar(dev, resno, &type);
  422. if (!reg)
  423. return 0;
  424. __pci_read_base(dev, type, &tmp, reg);
  425. return resource_alignment(&tmp);
  426. }
  427. /**
  428. * pci_restore_iov_state - restore the state of the IOV capability
  429. * @dev: the PCI device
  430. */
  431. void pci_restore_iov_state(struct pci_dev *dev)
  432. {
  433. if (dev->is_physfn)
  434. sriov_restore_state(dev);
  435. }
  436. /**
  437. * pci_iov_bus_range - find bus range used by Virtual Function
  438. * @bus: the PCI bus
  439. *
  440. * Returns max number of buses (exclude current one) used by Virtual
  441. * Functions.
  442. */
  443. int pci_iov_bus_range(struct pci_bus *bus)
  444. {
  445. int max = 0;
  446. u8 busnr;
  447. struct pci_dev *dev;
  448. list_for_each_entry(dev, &bus->devices, bus_list) {
  449. if (!dev->is_physfn)
  450. continue;
  451. busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
  452. if (busnr > max)
  453. max = busnr;
  454. }
  455. return max ? max - bus->number : 0;
  456. }
  457. /**
  458. * pci_enable_sriov - enable the SR-IOV capability
  459. * @dev: the PCI device
  460. * @nr_virtfn: number of virtual functions to enable
  461. *
  462. * Returns 0 on success, or negative on failure.
  463. */
  464. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  465. {
  466. might_sleep();
  467. if (!dev->is_physfn)
  468. return -ENOSYS;
  469. return sriov_enable(dev, nr_virtfn);
  470. }
  471. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  472. /**
  473. * pci_disable_sriov - disable the SR-IOV capability
  474. * @dev: the PCI device
  475. */
  476. void pci_disable_sriov(struct pci_dev *dev)
  477. {
  478. might_sleep();
  479. if (!dev->is_physfn)
  480. return;
  481. sriov_disable(dev);
  482. }
  483. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  484. /**
  485. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  486. * @dev: the PCI device
  487. *
  488. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  489. */
  490. int pci_num_vf(struct pci_dev *dev)
  491. {
  492. if (!dev->is_physfn)
  493. return 0;
  494. return dev->sriov->num_VFs;
  495. }
  496. EXPORT_SYMBOL_GPL(pci_num_vf);
  497. /**
  498. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  499. * @dev: the PCI device
  500. *
  501. * Returns number of VFs belonging to this device that are assigned to a guest.
  502. * If device is not a physical function returns 0.
  503. */
  504. int pci_vfs_assigned(struct pci_dev *dev)
  505. {
  506. struct pci_dev *vfdev;
  507. unsigned int vfs_assigned = 0;
  508. unsigned short dev_id;
  509. /* only search if we are a PF */
  510. if (!dev->is_physfn)
  511. return 0;
  512. /*
  513. * determine the device ID for the VFs, the vendor ID will be the
  514. * same as the PF so there is no need to check for that one
  515. */
  516. pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
  517. /* loop through all the VFs to see if we own any that are assigned */
  518. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  519. while (vfdev) {
  520. /*
  521. * It is considered assigned if it is a virtual function with
  522. * our dev as the physical function and the assigned bit is set
  523. */
  524. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  525. pci_is_dev_assigned(vfdev))
  526. vfs_assigned++;
  527. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  528. }
  529. return vfs_assigned;
  530. }
  531. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  532. /**
  533. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  534. * @dev: the PCI PF device
  535. * @numvfs: number that should be used for TotalVFs supported
  536. *
  537. * Should be called from PF driver's probe routine with
  538. * device's mutex held.
  539. *
  540. * Returns 0 if PF is an SRIOV-capable device and
  541. * value of numvfs valid. If not a PF return -ENOSYS;
  542. * if numvfs is invalid return -EINVAL;
  543. * if VFs already enabled, return -EBUSY.
  544. */
  545. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  546. {
  547. if (!dev->is_physfn)
  548. return -ENOSYS;
  549. if (numvfs > dev->sriov->total_VFs)
  550. return -EINVAL;
  551. /* Shouldn't change if VFs already enabled */
  552. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  553. return -EBUSY;
  554. else
  555. dev->sriov->driver_max_VFs = numvfs;
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  559. /**
  560. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  561. * @dev: the PCI PF device
  562. *
  563. * For a PCIe device with SRIOV support, return the PCIe
  564. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  565. * if the driver reduced it. Otherwise 0.
  566. */
  567. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  568. {
  569. if (!dev->is_physfn)
  570. return 0;
  571. if (dev->sriov->driver_max_VFs)
  572. return dev->sriov->driver_max_VFs;
  573. return dev->sriov->total_VFs;
  574. }
  575. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);