iov.c 18 KB

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