iov.c 19 KB

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