iov.c 21 KB

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