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. static int virtfn_add(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. sprintf(buf, "virtfn%u", id);
  138. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  139. if (rc)
  140. goto failed1;
  141. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  142. if (rc)
  143. goto failed2;
  144. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  145. pci_bus_add_device(virtfn);
  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. static void virtfn_remove(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. iov->initial_VFs = initial;
  256. if (nr_virtfn < initial)
  257. initial = nr_virtfn;
  258. rc = pcibios_sriov_enable(dev, initial);
  259. if (rc) {
  260. dev_err(&dev->dev, "failure %d from pcibios_sriov_enable()\n", rc);
  261. goto err_pcibios;
  262. }
  263. pci_iov_set_numvfs(dev, nr_virtfn);
  264. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  265. pci_cfg_access_lock(dev);
  266. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  267. msleep(100);
  268. pci_cfg_access_unlock(dev);
  269. for (i = 0; i < initial; i++) {
  270. rc = virtfn_add(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. virtfn_remove(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. virtfn_remove(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. if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
  322. pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
  323. return -ENODEV;
  324. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  325. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  326. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  327. ssleep(1);
  328. }
  329. ctrl = 0;
  330. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  331. if (pdev->is_physfn)
  332. goto found;
  333. pdev = NULL;
  334. if (pci_ari_enabled(dev->bus))
  335. ctrl |= PCI_SRIOV_CTRL_ARI;
  336. found:
  337. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  338. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  339. if (!total)
  340. return 0;
  341. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  342. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  343. pgsz &= ~((1 << i) - 1);
  344. if (!pgsz)
  345. return -EIO;
  346. pgsz &= ~(pgsz - 1);
  347. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  348. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  349. if (!iov)
  350. return -ENOMEM;
  351. nres = 0;
  352. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  353. res = &dev->resource[i + PCI_IOV_RESOURCES];
  354. /*
  355. * If it is already FIXED, don't change it, something
  356. * (perhaps EA or header fixups) wants it this way.
  357. */
  358. if (res->flags & IORESOURCE_PCI_FIXED)
  359. bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  360. else
  361. bar64 = __pci_read_base(dev, pci_bar_unknown, res,
  362. pos + PCI_SRIOV_BAR + i * 4);
  363. if (!res->flags)
  364. continue;
  365. if (resource_size(res) & (PAGE_SIZE - 1)) {
  366. rc = -EIO;
  367. goto failed;
  368. }
  369. iov->barsz[i] = resource_size(res);
  370. res->end = res->start + resource_size(res) * total - 1;
  371. dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
  372. i, res, i, total);
  373. i += bar64;
  374. nres++;
  375. }
  376. iov->pos = pos;
  377. iov->nres = nres;
  378. iov->ctrl = ctrl;
  379. iov->total_VFs = total;
  380. iov->pgsz = pgsz;
  381. iov->self = dev;
  382. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  383. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  384. if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  385. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  386. if (pdev)
  387. iov->dev = pci_dev_get(pdev);
  388. else
  389. iov->dev = dev;
  390. mutex_init(&iov->lock);
  391. dev->sriov = iov;
  392. dev->is_physfn = 1;
  393. rc = compute_max_vf_buses(dev);
  394. if (rc)
  395. goto fail_max_buses;
  396. return 0;
  397. fail_max_buses:
  398. dev->sriov = NULL;
  399. dev->is_physfn = 0;
  400. failed:
  401. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  402. res = &dev->resource[i + PCI_IOV_RESOURCES];
  403. res->flags = 0;
  404. }
  405. kfree(iov);
  406. return rc;
  407. }
  408. static void sriov_release(struct pci_dev *dev)
  409. {
  410. BUG_ON(dev->sriov->num_VFs);
  411. if (dev != dev->sriov->dev)
  412. pci_dev_put(dev->sriov->dev);
  413. mutex_destroy(&dev->sriov->lock);
  414. kfree(dev->sriov);
  415. dev->sriov = NULL;
  416. }
  417. static void sriov_restore_state(struct pci_dev *dev)
  418. {
  419. int i;
  420. u16 ctrl;
  421. struct pci_sriov *iov = dev->sriov;
  422. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  423. if (ctrl & PCI_SRIOV_CTRL_VFE)
  424. return;
  425. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  426. pci_update_resource(dev, i);
  427. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  428. pci_iov_set_numvfs(dev, iov->num_VFs);
  429. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  430. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  431. msleep(100);
  432. }
  433. /**
  434. * pci_iov_init - initialize the IOV capability
  435. * @dev: the PCI device
  436. *
  437. * Returns 0 on success, or negative on failure.
  438. */
  439. int pci_iov_init(struct pci_dev *dev)
  440. {
  441. int pos;
  442. if (!pci_is_pcie(dev))
  443. return -ENODEV;
  444. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  445. if (pos)
  446. return sriov_init(dev, pos);
  447. return -ENODEV;
  448. }
  449. /**
  450. * pci_iov_release - release resources used by the IOV capability
  451. * @dev: the PCI device
  452. */
  453. void pci_iov_release(struct pci_dev *dev)
  454. {
  455. if (dev->is_physfn)
  456. sriov_release(dev);
  457. }
  458. /**
  459. * pci_iov_update_resource - update a VF BAR
  460. * @dev: the PCI device
  461. * @resno: the resource number
  462. *
  463. * Update a VF BAR in the SR-IOV capability of a PF.
  464. */
  465. void pci_iov_update_resource(struct pci_dev *dev, int resno)
  466. {
  467. struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
  468. struct resource *res = dev->resource + resno;
  469. int vf_bar = resno - PCI_IOV_RESOURCES;
  470. struct pci_bus_region region;
  471. u16 cmd;
  472. u32 new;
  473. int reg;
  474. /*
  475. * The generic pci_restore_bars() path calls this for all devices,
  476. * including VFs and non-SR-IOV devices. If this is not a PF, we
  477. * have nothing to do.
  478. */
  479. if (!iov)
  480. return;
  481. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
  482. if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
  483. dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
  484. vf_bar, res);
  485. return;
  486. }
  487. /*
  488. * Ignore unimplemented BARs, unused resource slots for 64-bit
  489. * BARs, and non-movable resources, e.g., those described via
  490. * Enhanced Allocation.
  491. */
  492. if (!res->flags)
  493. return;
  494. if (res->flags & IORESOURCE_UNSET)
  495. return;
  496. if (res->flags & IORESOURCE_PCI_FIXED)
  497. return;
  498. pcibios_resource_to_bus(dev->bus, &region, res);
  499. new = region.start;
  500. new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
  501. reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
  502. pci_write_config_dword(dev, reg, new);
  503. if (res->flags & IORESOURCE_MEM_64) {
  504. new = region.start >> 16 >> 16;
  505. pci_write_config_dword(dev, reg + 4, new);
  506. }
  507. }
  508. resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
  509. int resno)
  510. {
  511. return pci_iov_resource_size(dev, resno);
  512. }
  513. /**
  514. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  515. * @dev: the PCI device
  516. * @resno: the resource number
  517. *
  518. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  519. * This is not the same as the resource size which is defined as
  520. * the VF BAR size multiplied by the number of VFs. The alignment
  521. * is just the VF BAR size.
  522. */
  523. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  524. {
  525. return pcibios_iov_resource_alignment(dev, resno);
  526. }
  527. /**
  528. * pci_restore_iov_state - restore the state of the IOV capability
  529. * @dev: the PCI device
  530. */
  531. void pci_restore_iov_state(struct pci_dev *dev)
  532. {
  533. if (dev->is_physfn)
  534. sriov_restore_state(dev);
  535. }
  536. /**
  537. * pci_iov_bus_range - find bus range used by Virtual Function
  538. * @bus: the PCI bus
  539. *
  540. * Returns max number of buses (exclude current one) used by Virtual
  541. * Functions.
  542. */
  543. int pci_iov_bus_range(struct pci_bus *bus)
  544. {
  545. int max = 0;
  546. struct pci_dev *dev;
  547. list_for_each_entry(dev, &bus->devices, bus_list) {
  548. if (!dev->is_physfn)
  549. continue;
  550. if (dev->sriov->max_VF_buses > max)
  551. max = dev->sriov->max_VF_buses;
  552. }
  553. return max ? max - bus->number : 0;
  554. }
  555. /**
  556. * pci_enable_sriov - enable the SR-IOV capability
  557. * @dev: the PCI device
  558. * @nr_virtfn: number of virtual functions to enable
  559. *
  560. * Returns 0 on success, or negative on failure.
  561. */
  562. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  563. {
  564. might_sleep();
  565. if (!dev->is_physfn)
  566. return -ENOSYS;
  567. return sriov_enable(dev, nr_virtfn);
  568. }
  569. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  570. /**
  571. * pci_disable_sriov - disable the SR-IOV capability
  572. * @dev: the PCI device
  573. */
  574. void pci_disable_sriov(struct pci_dev *dev)
  575. {
  576. might_sleep();
  577. if (!dev->is_physfn)
  578. return;
  579. sriov_disable(dev);
  580. }
  581. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  582. /**
  583. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  584. * @dev: the PCI device
  585. *
  586. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  587. */
  588. int pci_num_vf(struct pci_dev *dev)
  589. {
  590. if (!dev->is_physfn)
  591. return 0;
  592. return dev->sriov->num_VFs;
  593. }
  594. EXPORT_SYMBOL_GPL(pci_num_vf);
  595. /**
  596. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  597. * @dev: the PCI device
  598. *
  599. * Returns number of VFs belonging to this device that are assigned to a guest.
  600. * If device is not a physical function returns 0.
  601. */
  602. int pci_vfs_assigned(struct pci_dev *dev)
  603. {
  604. struct pci_dev *vfdev;
  605. unsigned int vfs_assigned = 0;
  606. unsigned short dev_id;
  607. /* only search if we are a PF */
  608. if (!dev->is_physfn)
  609. return 0;
  610. /*
  611. * determine the device ID for the VFs, the vendor ID will be the
  612. * same as the PF so there is no need to check for that one
  613. */
  614. pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
  615. /* loop through all the VFs to see if we own any that are assigned */
  616. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  617. while (vfdev) {
  618. /*
  619. * It is considered assigned if it is a virtual function with
  620. * our dev as the physical function and the assigned bit is set
  621. */
  622. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  623. pci_is_dev_assigned(vfdev))
  624. vfs_assigned++;
  625. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  626. }
  627. return vfs_assigned;
  628. }
  629. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  630. /**
  631. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  632. * @dev: the PCI PF device
  633. * @numvfs: number that should be used for TotalVFs supported
  634. *
  635. * Should be called from PF driver's probe routine with
  636. * device's mutex held.
  637. *
  638. * Returns 0 if PF is an SRIOV-capable device and
  639. * value of numvfs valid. If not a PF return -ENOSYS;
  640. * if numvfs is invalid return -EINVAL;
  641. * if VFs already enabled, return -EBUSY.
  642. */
  643. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  644. {
  645. if (!dev->is_physfn)
  646. return -ENOSYS;
  647. if (numvfs > dev->sriov->total_VFs)
  648. return -EINVAL;
  649. /* Shouldn't change if VFs already enabled */
  650. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  651. return -EBUSY;
  652. else
  653. dev->sriov->driver_max_VFs = numvfs;
  654. return 0;
  655. }
  656. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  657. /**
  658. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  659. * @dev: the PCI PF device
  660. *
  661. * For a PCIe device with SRIOV support, return the PCIe
  662. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  663. * if the driver reduced it. Otherwise 0.
  664. */
  665. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  666. {
  667. if (!dev->is_physfn)
  668. return 0;
  669. if (dev->sriov->driver_max_VFs)
  670. return dev->sriov->driver_max_VFs;
  671. return dev->sriov->total_VFs;
  672. }
  673. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);