iov.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Express I/O Virtualization (IOV) support
  4. * Single Root IOV 1.0
  5. * Address Translation Service 1.0
  6. *
  7. * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/mutex.h>
  12. #include <linux/export.h>
  13. #include <linux/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/pci-ats.h>
  16. #include "pci.h"
  17. #define VIRTFN_ID_LEN 16
  18. int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
  19. {
  20. if (!dev->is_physfn)
  21. return -EINVAL;
  22. return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  23. dev->sriov->stride * vf_id) >> 8);
  24. }
  25. int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
  26. {
  27. if (!dev->is_physfn)
  28. return -EINVAL;
  29. return (dev->devfn + dev->sriov->offset +
  30. dev->sriov->stride * vf_id) & 0xff;
  31. }
  32. /*
  33. * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
  34. * change when NumVFs changes.
  35. *
  36. * Update iov->offset and iov->stride when NumVFs is written.
  37. */
  38. static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
  39. {
  40. struct pci_sriov *iov = dev->sriov;
  41. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  42. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
  43. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
  44. }
  45. /*
  46. * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
  47. * determine how many additional bus numbers will be consumed by VFs.
  48. *
  49. * Iterate over all valid NumVFs, validate offset and stride, and calculate
  50. * the maximum number of bus numbers that could ever be required.
  51. */
  52. static int compute_max_vf_buses(struct pci_dev *dev)
  53. {
  54. struct pci_sriov *iov = dev->sriov;
  55. int nr_virtfn, busnr, rc = 0;
  56. for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
  57. pci_iov_set_numvfs(dev, nr_virtfn);
  58. if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
  59. rc = -EIO;
  60. goto out;
  61. }
  62. busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
  63. if (busnr > iov->max_VF_buses)
  64. iov->max_VF_buses = busnr;
  65. }
  66. out:
  67. pci_iov_set_numvfs(dev, 0);
  68. return rc;
  69. }
  70. static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  71. {
  72. struct pci_bus *child;
  73. if (bus->number == busnr)
  74. return bus;
  75. child = pci_find_bus(pci_domain_nr(bus), busnr);
  76. if (child)
  77. return child;
  78. child = pci_add_new_bus(bus, NULL, busnr);
  79. if (!child)
  80. return NULL;
  81. pci_bus_insert_busn_res(child, busnr, busnr);
  82. return child;
  83. }
  84. static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
  85. {
  86. if (physbus != virtbus && list_empty(&virtbus->devices))
  87. pci_remove_bus(virtbus);
  88. }
  89. resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
  90. {
  91. if (!dev->is_physfn)
  92. return 0;
  93. return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
  94. }
  95. static void pci_read_vf_config_common(struct pci_dev *virtfn)
  96. {
  97. struct pci_dev *physfn = virtfn->physfn;
  98. /*
  99. * Some config registers are the same across all associated VFs.
  100. * Read them once from VF0 so we can skip reading them from the
  101. * other VFs.
  102. *
  103. * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
  104. * have the same Revision ID and Subsystem ID, but we assume they
  105. * do.
  106. */
  107. pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
  108. &physfn->sriov->class);
  109. pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
  110. &physfn->sriov->hdr_type);
  111. pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
  112. &physfn->sriov->subsystem_vendor);
  113. pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
  114. &physfn->sriov->subsystem_device);
  115. }
  116. int pci_iov_add_virtfn(struct pci_dev *dev, int id)
  117. {
  118. int i;
  119. int rc = -ENOMEM;
  120. u64 size;
  121. char buf[VIRTFN_ID_LEN];
  122. struct pci_dev *virtfn;
  123. struct resource *res;
  124. struct pci_sriov *iov = dev->sriov;
  125. struct pci_bus *bus;
  126. bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
  127. if (!bus)
  128. goto failed;
  129. virtfn = pci_alloc_dev(bus);
  130. if (!virtfn)
  131. goto failed0;
  132. virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
  133. virtfn->vendor = dev->vendor;
  134. virtfn->device = iov->vf_device;
  135. virtfn->is_virtfn = 1;
  136. virtfn->physfn = pci_dev_get(dev);
  137. if (id == 0)
  138. pci_read_vf_config_common(virtfn);
  139. rc = pci_setup_device(virtfn);
  140. if (rc)
  141. goto failed1;
  142. virtfn->dev.parent = dev->dev.parent;
  143. virtfn->multifunction = 0;
  144. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  145. res = &dev->resource[i + PCI_IOV_RESOURCES];
  146. if (!res->parent)
  147. continue;
  148. virtfn->resource[i].name = pci_name(virtfn);
  149. virtfn->resource[i].flags = res->flags;
  150. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  151. virtfn->resource[i].start = res->start + size * id;
  152. virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
  153. rc = request_resource(res, &virtfn->resource[i]);
  154. BUG_ON(rc);
  155. }
  156. pci_device_add(virtfn, virtfn->bus);
  157. sprintf(buf, "virtfn%u", id);
  158. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  159. if (rc)
  160. goto failed1;
  161. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  162. if (rc)
  163. goto failed2;
  164. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  165. pci_bus_add_device(virtfn);
  166. return 0;
  167. failed2:
  168. sysfs_remove_link(&dev->dev.kobj, buf);
  169. failed1:
  170. pci_stop_and_remove_bus_device(virtfn);
  171. pci_dev_put(dev);
  172. failed0:
  173. virtfn_remove_bus(dev->bus, bus);
  174. failed:
  175. return rc;
  176. }
  177. void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
  178. {
  179. char buf[VIRTFN_ID_LEN];
  180. struct pci_dev *virtfn;
  181. virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
  182. pci_iov_virtfn_bus(dev, id),
  183. pci_iov_virtfn_devfn(dev, id));
  184. if (!virtfn)
  185. return;
  186. sprintf(buf, "virtfn%u", id);
  187. sysfs_remove_link(&dev->dev.kobj, buf);
  188. /*
  189. * pci_stop_dev() could have been called for this virtfn already,
  190. * so the directory for the virtfn may have been removed before.
  191. * Double check to avoid spurious sysfs warnings.
  192. */
  193. if (virtfn->dev.kobj.sd)
  194. sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  195. pci_stop_and_remove_bus_device(virtfn);
  196. virtfn_remove_bus(dev->bus, virtfn->bus);
  197. /* balance pci_get_domain_bus_and_slot() */
  198. pci_dev_put(virtfn);
  199. pci_dev_put(dev);
  200. }
  201. int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  202. {
  203. return 0;
  204. }
  205. int __weak pcibios_sriov_disable(struct pci_dev *pdev)
  206. {
  207. return 0;
  208. }
  209. static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
  210. {
  211. int rc;
  212. int i;
  213. int nres;
  214. u16 initial;
  215. struct resource *res;
  216. struct pci_dev *pdev;
  217. struct pci_sriov *iov = dev->sriov;
  218. int bars = 0;
  219. int bus;
  220. if (!nr_virtfn)
  221. return 0;
  222. if (iov->num_VFs)
  223. return -EINVAL;
  224. pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  225. if (initial > iov->total_VFs ||
  226. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
  227. return -EIO;
  228. if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
  229. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  230. return -EINVAL;
  231. nres = 0;
  232. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  233. bars |= (1 << (i + PCI_IOV_RESOURCES));
  234. res = &dev->resource[i + PCI_IOV_RESOURCES];
  235. if (res->parent)
  236. nres++;
  237. }
  238. if (nres != iov->nres) {
  239. pci_err(dev, "not enough MMIO resources for SR-IOV\n");
  240. return -ENOMEM;
  241. }
  242. bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
  243. if (bus > dev->bus->busn_res.end) {
  244. pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
  245. nr_virtfn, bus, &dev->bus->busn_res);
  246. return -ENOMEM;
  247. }
  248. if (pci_enable_resources(dev, bars)) {
  249. pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
  250. return -ENOMEM;
  251. }
  252. if (iov->link != dev->devfn) {
  253. pdev = pci_get_slot(dev->bus, iov->link);
  254. if (!pdev)
  255. return -ENODEV;
  256. if (!pdev->is_physfn) {
  257. pci_dev_put(pdev);
  258. return -ENOSYS;
  259. }
  260. rc = sysfs_create_link(&dev->dev.kobj,
  261. &pdev->dev.kobj, "dep_link");
  262. pci_dev_put(pdev);
  263. if (rc)
  264. return rc;
  265. }
  266. iov->initial_VFs = initial;
  267. if (nr_virtfn < initial)
  268. initial = nr_virtfn;
  269. rc = pcibios_sriov_enable(dev, initial);
  270. if (rc) {
  271. pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
  272. goto err_pcibios;
  273. }
  274. pci_iov_set_numvfs(dev, nr_virtfn);
  275. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  276. pci_cfg_access_lock(dev);
  277. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  278. msleep(100);
  279. pci_cfg_access_unlock(dev);
  280. for (i = 0; i < initial; i++) {
  281. rc = pci_iov_add_virtfn(dev, i);
  282. if (rc)
  283. goto failed;
  284. }
  285. kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  286. iov->num_VFs = nr_virtfn;
  287. return 0;
  288. failed:
  289. while (i--)
  290. pci_iov_remove_virtfn(dev, i);
  291. err_pcibios:
  292. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  293. pci_cfg_access_lock(dev);
  294. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  295. ssleep(1);
  296. pci_cfg_access_unlock(dev);
  297. pcibios_sriov_disable(dev);
  298. if (iov->link != dev->devfn)
  299. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  300. pci_iov_set_numvfs(dev, 0);
  301. return rc;
  302. }
  303. static void sriov_disable(struct pci_dev *dev)
  304. {
  305. int i;
  306. struct pci_sriov *iov = dev->sriov;
  307. if (!iov->num_VFs)
  308. return;
  309. for (i = 0; i < iov->num_VFs; i++)
  310. pci_iov_remove_virtfn(dev, i);
  311. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  312. pci_cfg_access_lock(dev);
  313. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  314. ssleep(1);
  315. pci_cfg_access_unlock(dev);
  316. pcibios_sriov_disable(dev);
  317. if (iov->link != dev->devfn)
  318. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  319. iov->num_VFs = 0;
  320. pci_iov_set_numvfs(dev, 0);
  321. }
  322. static int sriov_init(struct pci_dev *dev, int pos)
  323. {
  324. int i, bar64;
  325. int rc;
  326. int nres;
  327. u32 pgsz;
  328. u16 ctrl, total;
  329. struct pci_sriov *iov;
  330. struct resource *res;
  331. struct pci_dev *pdev;
  332. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  333. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  334. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  335. ssleep(1);
  336. }
  337. ctrl = 0;
  338. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  339. if (pdev->is_physfn)
  340. goto found;
  341. pdev = NULL;
  342. if (pci_ari_enabled(dev->bus))
  343. ctrl |= PCI_SRIOV_CTRL_ARI;
  344. found:
  345. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  346. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  347. if (!total)
  348. return 0;
  349. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  350. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  351. pgsz &= ~((1 << i) - 1);
  352. if (!pgsz)
  353. return -EIO;
  354. pgsz &= ~(pgsz - 1);
  355. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  356. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  357. if (!iov)
  358. return -ENOMEM;
  359. nres = 0;
  360. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  361. res = &dev->resource[i + PCI_IOV_RESOURCES];
  362. /*
  363. * If it is already FIXED, don't change it, something
  364. * (perhaps EA or header fixups) wants it this way.
  365. */
  366. if (res->flags & IORESOURCE_PCI_FIXED)
  367. bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  368. else
  369. bar64 = __pci_read_base(dev, pci_bar_unknown, res,
  370. pos + PCI_SRIOV_BAR + i * 4);
  371. if (!res->flags)
  372. continue;
  373. if (resource_size(res) & (PAGE_SIZE - 1)) {
  374. rc = -EIO;
  375. goto failed;
  376. }
  377. iov->barsz[i] = resource_size(res);
  378. res->end = res->start + resource_size(res) * total - 1;
  379. pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
  380. i, res, i, total);
  381. i += bar64;
  382. nres++;
  383. }
  384. iov->pos = pos;
  385. iov->nres = nres;
  386. iov->ctrl = ctrl;
  387. iov->total_VFs = total;
  388. iov->driver_max_VFs = total;
  389. pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
  390. iov->pgsz = pgsz;
  391. iov->self = dev;
  392. iov->drivers_autoprobe = true;
  393. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  394. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  395. if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  396. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  397. if (pdev)
  398. iov->dev = pci_dev_get(pdev);
  399. else
  400. iov->dev = dev;
  401. dev->sriov = iov;
  402. dev->is_physfn = 1;
  403. rc = compute_max_vf_buses(dev);
  404. if (rc)
  405. goto fail_max_buses;
  406. return 0;
  407. fail_max_buses:
  408. dev->sriov = NULL;
  409. dev->is_physfn = 0;
  410. failed:
  411. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  412. res = &dev->resource[i + PCI_IOV_RESOURCES];
  413. res->flags = 0;
  414. }
  415. kfree(iov);
  416. return rc;
  417. }
  418. static void sriov_release(struct pci_dev *dev)
  419. {
  420. BUG_ON(dev->sriov->num_VFs);
  421. if (dev != dev->sriov->dev)
  422. pci_dev_put(dev->sriov->dev);
  423. kfree(dev->sriov);
  424. dev->sriov = NULL;
  425. }
  426. static void sriov_restore_state(struct pci_dev *dev)
  427. {
  428. int i;
  429. u16 ctrl;
  430. struct pci_sriov *iov = dev->sriov;
  431. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  432. if (ctrl & PCI_SRIOV_CTRL_VFE)
  433. return;
  434. /*
  435. * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
  436. * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
  437. */
  438. ctrl &= ~PCI_SRIOV_CTRL_ARI;
  439. ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
  440. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
  441. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  442. pci_update_resource(dev, i);
  443. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  444. pci_iov_set_numvfs(dev, iov->num_VFs);
  445. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  446. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  447. msleep(100);
  448. }
  449. /**
  450. * pci_iov_init - initialize the IOV capability
  451. * @dev: the PCI device
  452. *
  453. * Returns 0 on success, or negative on failure.
  454. */
  455. int pci_iov_init(struct pci_dev *dev)
  456. {
  457. int pos;
  458. if (!pci_is_pcie(dev))
  459. return -ENODEV;
  460. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  461. if (pos)
  462. return sriov_init(dev, pos);
  463. return -ENODEV;
  464. }
  465. /**
  466. * pci_iov_release - release resources used by the IOV capability
  467. * @dev: the PCI device
  468. */
  469. void pci_iov_release(struct pci_dev *dev)
  470. {
  471. if (dev->is_physfn)
  472. sriov_release(dev);
  473. }
  474. /**
  475. * pci_iov_remove - clean up SR-IOV state after PF driver is detached
  476. * @dev: the PCI device
  477. */
  478. void pci_iov_remove(struct pci_dev *dev)
  479. {
  480. struct pci_sriov *iov = dev->sriov;
  481. if (!dev->is_physfn)
  482. return;
  483. iov->driver_max_VFs = iov->total_VFs;
  484. if (iov->num_VFs)
  485. pci_warn(dev, "driver left SR-IOV enabled after remove\n");
  486. }
  487. /**
  488. * pci_iov_update_resource - update a VF BAR
  489. * @dev: the PCI device
  490. * @resno: the resource number
  491. *
  492. * Update a VF BAR in the SR-IOV capability of a PF.
  493. */
  494. void pci_iov_update_resource(struct pci_dev *dev, int resno)
  495. {
  496. struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
  497. struct resource *res = dev->resource + resno;
  498. int vf_bar = resno - PCI_IOV_RESOURCES;
  499. struct pci_bus_region region;
  500. u16 cmd;
  501. u32 new;
  502. int reg;
  503. /*
  504. * The generic pci_restore_bars() path calls this for all devices,
  505. * including VFs and non-SR-IOV devices. If this is not a PF, we
  506. * have nothing to do.
  507. */
  508. if (!iov)
  509. return;
  510. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
  511. if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
  512. dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
  513. vf_bar, res);
  514. return;
  515. }
  516. /*
  517. * Ignore unimplemented BARs, unused resource slots for 64-bit
  518. * BARs, and non-movable resources, e.g., those described via
  519. * Enhanced Allocation.
  520. */
  521. if (!res->flags)
  522. return;
  523. if (res->flags & IORESOURCE_UNSET)
  524. return;
  525. if (res->flags & IORESOURCE_PCI_FIXED)
  526. return;
  527. pcibios_resource_to_bus(dev->bus, &region, res);
  528. new = region.start;
  529. new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
  530. reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
  531. pci_write_config_dword(dev, reg, new);
  532. if (res->flags & IORESOURCE_MEM_64) {
  533. new = region.start >> 16 >> 16;
  534. pci_write_config_dword(dev, reg + 4, new);
  535. }
  536. }
  537. resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
  538. int resno)
  539. {
  540. return pci_iov_resource_size(dev, resno);
  541. }
  542. /**
  543. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  544. * @dev: the PCI device
  545. * @resno: the resource number
  546. *
  547. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  548. * This is not the same as the resource size which is defined as
  549. * the VF BAR size multiplied by the number of VFs. The alignment
  550. * is just the VF BAR size.
  551. */
  552. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  553. {
  554. return pcibios_iov_resource_alignment(dev, resno);
  555. }
  556. /**
  557. * pci_restore_iov_state - restore the state of the IOV capability
  558. * @dev: the PCI device
  559. */
  560. void pci_restore_iov_state(struct pci_dev *dev)
  561. {
  562. if (dev->is_physfn)
  563. sriov_restore_state(dev);
  564. }
  565. /**
  566. * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
  567. * @dev: the PCI device
  568. * @auto_probe: set VF drivers auto probe flag
  569. */
  570. void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
  571. {
  572. if (dev->is_physfn)
  573. dev->sriov->drivers_autoprobe = auto_probe;
  574. }
  575. /**
  576. * pci_iov_bus_range - find bus range used by Virtual Function
  577. * @bus: the PCI bus
  578. *
  579. * Returns max number of buses (exclude current one) used by Virtual
  580. * Functions.
  581. */
  582. int pci_iov_bus_range(struct pci_bus *bus)
  583. {
  584. int max = 0;
  585. struct pci_dev *dev;
  586. list_for_each_entry(dev, &bus->devices, bus_list) {
  587. if (!dev->is_physfn)
  588. continue;
  589. if (dev->sriov->max_VF_buses > max)
  590. max = dev->sriov->max_VF_buses;
  591. }
  592. return max ? max - bus->number : 0;
  593. }
  594. /**
  595. * pci_enable_sriov - enable the SR-IOV capability
  596. * @dev: the PCI device
  597. * @nr_virtfn: number of virtual functions to enable
  598. *
  599. * Returns 0 on success, or negative on failure.
  600. */
  601. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  602. {
  603. might_sleep();
  604. if (!dev->is_physfn)
  605. return -ENOSYS;
  606. return sriov_enable(dev, nr_virtfn);
  607. }
  608. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  609. /**
  610. * pci_disable_sriov - disable the SR-IOV capability
  611. * @dev: the PCI device
  612. */
  613. void pci_disable_sriov(struct pci_dev *dev)
  614. {
  615. might_sleep();
  616. if (!dev->is_physfn)
  617. return;
  618. sriov_disable(dev);
  619. }
  620. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  621. /**
  622. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  623. * @dev: the PCI device
  624. *
  625. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  626. */
  627. int pci_num_vf(struct pci_dev *dev)
  628. {
  629. if (!dev->is_physfn)
  630. return 0;
  631. return dev->sriov->num_VFs;
  632. }
  633. EXPORT_SYMBOL_GPL(pci_num_vf);
  634. /**
  635. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  636. * @dev: the PCI device
  637. *
  638. * Returns number of VFs belonging to this device that are assigned to a guest.
  639. * If device is not a physical function returns 0.
  640. */
  641. int pci_vfs_assigned(struct pci_dev *dev)
  642. {
  643. struct pci_dev *vfdev;
  644. unsigned int vfs_assigned = 0;
  645. unsigned short dev_id;
  646. /* only search if we are a PF */
  647. if (!dev->is_physfn)
  648. return 0;
  649. /*
  650. * determine the device ID for the VFs, the vendor ID will be the
  651. * same as the PF so there is no need to check for that one
  652. */
  653. dev_id = dev->sriov->vf_device;
  654. /* loop through all the VFs to see if we own any that are assigned */
  655. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  656. while (vfdev) {
  657. /*
  658. * It is considered assigned if it is a virtual function with
  659. * our dev as the physical function and the assigned bit is set
  660. */
  661. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  662. pci_is_dev_assigned(vfdev))
  663. vfs_assigned++;
  664. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  665. }
  666. return vfs_assigned;
  667. }
  668. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  669. /**
  670. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  671. * @dev: the PCI PF device
  672. * @numvfs: number that should be used for TotalVFs supported
  673. *
  674. * Should be called from PF driver's probe routine with
  675. * device's mutex held.
  676. *
  677. * Returns 0 if PF is an SRIOV-capable device and
  678. * value of numvfs valid. If not a PF return -ENOSYS;
  679. * if numvfs is invalid return -EINVAL;
  680. * if VFs already enabled, return -EBUSY.
  681. */
  682. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  683. {
  684. if (!dev->is_physfn)
  685. return -ENOSYS;
  686. if (numvfs > dev->sriov->total_VFs)
  687. return -EINVAL;
  688. /* Shouldn't change if VFs already enabled */
  689. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  690. return -EBUSY;
  691. dev->sriov->driver_max_VFs = numvfs;
  692. return 0;
  693. }
  694. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  695. /**
  696. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  697. * @dev: the PCI PF device
  698. *
  699. * For a PCIe device with SRIOV support, return the PCIe
  700. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  701. * if the driver reduced it. Otherwise 0.
  702. */
  703. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  704. {
  705. if (!dev->is_physfn)
  706. return 0;
  707. return dev->sriov->driver_max_VFs;
  708. }
  709. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
  710. /**
  711. * pci_sriov_configure_simple - helper to configure SR-IOV
  712. * @dev: the PCI device
  713. * @nr_virtfn: number of virtual functions to enable, 0 to disable
  714. *
  715. * Enable or disable SR-IOV for devices that don't require any PF setup
  716. * before enabling SR-IOV. Return value is negative on error, or number of
  717. * VFs allocated on success.
  718. */
  719. int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
  720. {
  721. int rc;
  722. might_sleep();
  723. if (!dev->is_physfn)
  724. return -ENODEV;
  725. if (pci_vfs_assigned(dev)) {
  726. pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
  727. return -EPERM;
  728. }
  729. if (nr_virtfn == 0) {
  730. sriov_disable(dev);
  731. return 0;
  732. }
  733. rc = sriov_enable(dev, nr_virtfn);
  734. if (rc < 0)
  735. return rc;
  736. return nr_virtfn;
  737. }
  738. EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);