iov.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. *
  394. * Returns position of the BAR encapsulated in the SR-IOV capability.
  395. */
  396. int pci_iov_resource_bar(struct pci_dev *dev, int resno)
  397. {
  398. if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
  399. return 0;
  400. BUG_ON(!dev->is_physfn);
  401. return dev->sriov->pos + PCI_SRIOV_BAR +
  402. 4 * (resno - PCI_IOV_RESOURCES);
  403. }
  404. /**
  405. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  406. * @dev: the PCI device
  407. * @resno: the resource number
  408. *
  409. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  410. * This is not the same as the resource size which is defined as
  411. * the VF BAR size multiplied by the number of VFs. The alignment
  412. * is just the VF BAR size.
  413. */
  414. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  415. {
  416. struct resource tmp;
  417. int reg = pci_iov_resource_bar(dev, resno);
  418. if (!reg)
  419. return 0;
  420. __pci_read_base(dev, pci_bar_unknown, &tmp, reg);
  421. return resource_alignment(&tmp);
  422. }
  423. /**
  424. * pci_restore_iov_state - restore the state of the IOV capability
  425. * @dev: the PCI device
  426. */
  427. void pci_restore_iov_state(struct pci_dev *dev)
  428. {
  429. if (dev->is_physfn)
  430. sriov_restore_state(dev);
  431. }
  432. /**
  433. * pci_iov_bus_range - find bus range used by Virtual Function
  434. * @bus: the PCI bus
  435. *
  436. * Returns max number of buses (exclude current one) used by Virtual
  437. * Functions.
  438. */
  439. int pci_iov_bus_range(struct pci_bus *bus)
  440. {
  441. int max = 0;
  442. u8 busnr;
  443. struct pci_dev *dev;
  444. list_for_each_entry(dev, &bus->devices, bus_list) {
  445. if (!dev->is_physfn)
  446. continue;
  447. busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
  448. if (busnr > max)
  449. max = busnr;
  450. }
  451. return max ? max - bus->number : 0;
  452. }
  453. /**
  454. * pci_enable_sriov - enable the SR-IOV capability
  455. * @dev: the PCI device
  456. * @nr_virtfn: number of virtual functions to enable
  457. *
  458. * Returns 0 on success, or negative on failure.
  459. */
  460. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  461. {
  462. might_sleep();
  463. if (!dev->is_physfn)
  464. return -ENOSYS;
  465. return sriov_enable(dev, nr_virtfn);
  466. }
  467. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  468. /**
  469. * pci_disable_sriov - disable the SR-IOV capability
  470. * @dev: the PCI device
  471. */
  472. void pci_disable_sriov(struct pci_dev *dev)
  473. {
  474. might_sleep();
  475. if (!dev->is_physfn)
  476. return;
  477. sriov_disable(dev);
  478. }
  479. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  480. /**
  481. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  482. * @dev: the PCI device
  483. *
  484. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  485. */
  486. int pci_num_vf(struct pci_dev *dev)
  487. {
  488. if (!dev->is_physfn)
  489. return 0;
  490. return dev->sriov->num_VFs;
  491. }
  492. EXPORT_SYMBOL_GPL(pci_num_vf);
  493. /**
  494. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  495. * @dev: the PCI device
  496. *
  497. * Returns number of VFs belonging to this device that are assigned to a guest.
  498. * If device is not a physical function returns 0.
  499. */
  500. int pci_vfs_assigned(struct pci_dev *dev)
  501. {
  502. struct pci_dev *vfdev;
  503. unsigned int vfs_assigned = 0;
  504. unsigned short dev_id;
  505. /* only search if we are a PF */
  506. if (!dev->is_physfn)
  507. return 0;
  508. /*
  509. * determine the device ID for the VFs, the vendor ID will be the
  510. * same as the PF so there is no need to check for that one
  511. */
  512. pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
  513. /* loop through all the VFs to see if we own any that are assigned */
  514. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  515. while (vfdev) {
  516. /*
  517. * It is considered assigned if it is a virtual function with
  518. * our dev as the physical function and the assigned bit is set
  519. */
  520. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  521. pci_is_dev_assigned(vfdev))
  522. vfs_assigned++;
  523. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  524. }
  525. return vfs_assigned;
  526. }
  527. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  528. /**
  529. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  530. * @dev: the PCI PF device
  531. * @numvfs: number that should be used for TotalVFs supported
  532. *
  533. * Should be called from PF driver's probe routine with
  534. * device's mutex held.
  535. *
  536. * Returns 0 if PF is an SRIOV-capable device and
  537. * value of numvfs valid. If not a PF return -ENOSYS;
  538. * if numvfs is invalid return -EINVAL;
  539. * if VFs already enabled, return -EBUSY.
  540. */
  541. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  542. {
  543. if (!dev->is_physfn)
  544. return -ENOSYS;
  545. if (numvfs > dev->sriov->total_VFs)
  546. return -EINVAL;
  547. /* Shouldn't change if VFs already enabled */
  548. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  549. return -EBUSY;
  550. else
  551. dev->sriov->driver_max_VFs = numvfs;
  552. return 0;
  553. }
  554. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  555. /**
  556. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  557. * @dev: the PCI PF device
  558. *
  559. * For a PCIe device with SRIOV support, return the PCIe
  560. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  561. * if the driver reduced it. Otherwise 0.
  562. */
  563. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  564. {
  565. if (!dev->is_physfn)
  566. return 0;
  567. if (dev->sriov->driver_max_VFs)
  568. return dev->sriov->driver_max_VFs;
  569. return dev->sriov->total_VFs;
  570. }
  571. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);