iov.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/log2.h>
  12. #include <linux/pci.h>
  13. #include <linux/sizes.h>
  14. #include <linux/slab.h>
  15. #include <linux/export.h>
  16. #include <linux/string.h>
  17. #include <linux/delay.h>
  18. #include <asm/div64.h>
  19. #include "pci.h"
  20. #define VIRTFN_ID_LEN 17 /* "virtfn%u\0" for 2^32 - 1 */
  21. int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
  22. {
  23. if (!dev->is_physfn)
  24. return -EINVAL;
  25. return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  26. dev->sriov->stride * vf_id) >> 8);
  27. }
  28. int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
  29. {
  30. if (!dev->is_physfn)
  31. return -EINVAL;
  32. return (dev->devfn + dev->sriov->offset +
  33. dev->sriov->stride * vf_id) & 0xff;
  34. }
  35. EXPORT_SYMBOL_GPL(pci_iov_virtfn_devfn);
  36. int pci_iov_vf_id(struct pci_dev *dev)
  37. {
  38. struct pci_dev *pf;
  39. if (!dev->is_virtfn)
  40. return -EINVAL;
  41. pf = pci_physfn(dev);
  42. return (pci_dev_id(dev) - (pci_dev_id(pf) + pf->sriov->offset)) /
  43. pf->sriov->stride;
  44. }
  45. EXPORT_SYMBOL_GPL(pci_iov_vf_id);
  46. /**
  47. * pci_iov_get_pf_drvdata - Return the drvdata of a PF
  48. * @dev: VF pci_dev
  49. * @pf_driver: Device driver required to own the PF
  50. *
  51. * This must be called from a context that ensures that a VF driver is attached.
  52. * The value returned is invalid once the VF driver completes its remove()
  53. * callback.
  54. *
  55. * Locking is achieved by the driver core. A VF driver cannot be probed until
  56. * pci_enable_sriov() is called and pci_disable_sriov() does not return until
  57. * all VF drivers have completed their remove().
  58. *
  59. * The PF driver must call pci_disable_sriov() before it begins to destroy the
  60. * drvdata.
  61. */
  62. void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver)
  63. {
  64. struct pci_dev *pf_dev;
  65. if (!dev->is_virtfn)
  66. return ERR_PTR(-EINVAL);
  67. pf_dev = dev->physfn;
  68. if (pf_dev->driver != pf_driver)
  69. return ERR_PTR(-EINVAL);
  70. return pci_get_drvdata(pf_dev);
  71. }
  72. EXPORT_SYMBOL_GPL(pci_iov_get_pf_drvdata);
  73. /*
  74. * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
  75. * change when NumVFs changes.
  76. *
  77. * Update iov->offset and iov->stride when NumVFs is written.
  78. */
  79. static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
  80. {
  81. struct pci_sriov *iov = dev->sriov;
  82. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  83. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
  84. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
  85. }
  86. /*
  87. * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
  88. * determine how many additional bus numbers will be consumed by VFs.
  89. *
  90. * Iterate over all valid NumVFs, validate offset and stride, and calculate
  91. * the maximum number of bus numbers that could ever be required.
  92. */
  93. static int compute_max_vf_buses(struct pci_dev *dev)
  94. {
  95. struct pci_sriov *iov = dev->sriov;
  96. int nr_virtfn, busnr, rc = 0;
  97. for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
  98. pci_iov_set_numvfs(dev, nr_virtfn);
  99. if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
  100. rc = -EIO;
  101. goto out;
  102. }
  103. busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
  104. if (busnr > iov->max_VF_buses)
  105. iov->max_VF_buses = busnr;
  106. }
  107. out:
  108. pci_iov_set_numvfs(dev, 0);
  109. return rc;
  110. }
  111. static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  112. {
  113. struct pci_bus *child;
  114. if (bus->number == busnr)
  115. return bus;
  116. child = pci_find_bus(pci_domain_nr(bus), busnr);
  117. if (child)
  118. return child;
  119. child = pci_add_new_bus(bus, NULL, busnr);
  120. if (!child)
  121. return NULL;
  122. pci_bus_insert_busn_res(child, busnr, busnr);
  123. return child;
  124. }
  125. static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
  126. {
  127. if (physbus != virtbus && list_empty(&virtbus->devices))
  128. pci_remove_bus(virtbus);
  129. }
  130. resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
  131. {
  132. if (!dev->is_physfn)
  133. return 0;
  134. return dev->sriov->barsz[pci_resource_num_to_vf_bar(resno)];
  135. }
  136. void pci_iov_resource_set_size(struct pci_dev *dev, int resno, int size)
  137. {
  138. if (!pci_resource_is_iov(resno)) {
  139. pci_warn(dev, "%s is not an IOV resource\n",
  140. pci_resource_name(dev, resno));
  141. return;
  142. }
  143. resno = pci_resource_num_to_vf_bar(resno);
  144. dev->sriov->barsz[resno] = pci_rebar_size_to_bytes(size);
  145. }
  146. bool pci_iov_is_memory_decoding_enabled(struct pci_dev *dev)
  147. {
  148. u16 cmd;
  149. pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_CTRL, &cmd);
  150. return cmd & PCI_SRIOV_CTRL_MSE;
  151. }
  152. static void pci_read_vf_config_common(struct pci_dev *virtfn)
  153. {
  154. struct pci_dev *physfn = virtfn->physfn;
  155. /*
  156. * Some config registers are the same across all associated VFs.
  157. * Read them once from VF0 so we can skip reading them from the
  158. * other VFs.
  159. *
  160. * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
  161. * have the same Revision ID and Subsystem ID, but we assume they
  162. * do.
  163. */
  164. pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
  165. &physfn->sriov->class);
  166. pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
  167. &physfn->sriov->hdr_type);
  168. pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
  169. &physfn->sriov->subsystem_vendor);
  170. pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
  171. &physfn->sriov->subsystem_device);
  172. }
  173. int pci_iov_sysfs_link(struct pci_dev *dev,
  174. struct pci_dev *virtfn, int id)
  175. {
  176. char buf[VIRTFN_ID_LEN];
  177. int rc;
  178. sprintf(buf, "virtfn%u", id);
  179. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  180. if (rc)
  181. goto failed;
  182. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  183. if (rc)
  184. goto failed1;
  185. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  186. return 0;
  187. failed1:
  188. sysfs_remove_link(&dev->dev.kobj, buf);
  189. failed:
  190. return rc;
  191. }
  192. #ifdef CONFIG_PCI_MSI
  193. static ssize_t sriov_vf_total_msix_show(struct device *dev,
  194. struct device_attribute *attr,
  195. char *buf)
  196. {
  197. struct pci_dev *pdev = to_pci_dev(dev);
  198. u32 vf_total_msix = 0;
  199. device_lock(dev);
  200. if (!pdev->driver || !pdev->driver->sriov_get_vf_total_msix)
  201. goto unlock;
  202. vf_total_msix = pdev->driver->sriov_get_vf_total_msix(pdev);
  203. unlock:
  204. device_unlock(dev);
  205. return sysfs_emit(buf, "%u\n", vf_total_msix);
  206. }
  207. static DEVICE_ATTR_RO(sriov_vf_total_msix);
  208. static ssize_t sriov_vf_msix_count_store(struct device *dev,
  209. struct device_attribute *attr,
  210. const char *buf, size_t count)
  211. {
  212. struct pci_dev *vf_dev = to_pci_dev(dev);
  213. struct pci_dev *pdev = pci_physfn(vf_dev);
  214. int val, ret = 0;
  215. if (kstrtoint(buf, 0, &val) < 0)
  216. return -EINVAL;
  217. if (val < 0)
  218. return -EINVAL;
  219. device_lock(&pdev->dev);
  220. if (!pdev->driver || !pdev->driver->sriov_set_msix_vec_count) {
  221. ret = -EOPNOTSUPP;
  222. goto err_pdev;
  223. }
  224. device_lock(&vf_dev->dev);
  225. if (vf_dev->driver) {
  226. /*
  227. * A driver is already attached to this VF and has configured
  228. * itself based on the current MSI-X vector count. Changing
  229. * the vector size could mess up the driver, so block it.
  230. */
  231. ret = -EBUSY;
  232. goto err_dev;
  233. }
  234. ret = pdev->driver->sriov_set_msix_vec_count(vf_dev, val);
  235. err_dev:
  236. device_unlock(&vf_dev->dev);
  237. err_pdev:
  238. device_unlock(&pdev->dev);
  239. return ret ? : count;
  240. }
  241. static DEVICE_ATTR_WO(sriov_vf_msix_count);
  242. #endif
  243. static struct attribute *sriov_vf_dev_attrs[] = {
  244. #ifdef CONFIG_PCI_MSI
  245. &dev_attr_sriov_vf_msix_count.attr,
  246. #endif
  247. NULL,
  248. };
  249. static umode_t sriov_vf_attrs_are_visible(struct kobject *kobj,
  250. struct attribute *a, int n)
  251. {
  252. struct device *dev = kobj_to_dev(kobj);
  253. struct pci_dev *pdev = to_pci_dev(dev);
  254. if (!pdev->is_virtfn)
  255. return 0;
  256. return a->mode;
  257. }
  258. const struct attribute_group sriov_vf_dev_attr_group = {
  259. .attrs = sriov_vf_dev_attrs,
  260. .is_visible = sriov_vf_attrs_are_visible,
  261. };
  262. static struct pci_dev *pci_iov_scan_device(struct pci_dev *dev, int id,
  263. struct pci_bus *bus)
  264. {
  265. struct pci_sriov *iov = dev->sriov;
  266. struct pci_dev *virtfn;
  267. int rc;
  268. virtfn = pci_alloc_dev(bus);
  269. if (!virtfn)
  270. return ERR_PTR(-ENOMEM);
  271. virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
  272. virtfn->vendor = dev->vendor;
  273. virtfn->device = iov->vf_device;
  274. virtfn->is_virtfn = 1;
  275. virtfn->physfn = pci_dev_get(dev);
  276. virtfn->no_command_memory = 1;
  277. if (id == 0)
  278. pci_read_vf_config_common(virtfn);
  279. rc = pci_setup_device(virtfn);
  280. if (rc) {
  281. pci_dev_put(dev);
  282. pci_bus_put(virtfn->bus);
  283. kfree(virtfn);
  284. return ERR_PTR(rc);
  285. }
  286. return virtfn;
  287. }
  288. int pci_iov_add_virtfn(struct pci_dev *dev, int id)
  289. {
  290. struct pci_bus *bus;
  291. struct pci_dev *virtfn;
  292. struct resource *res;
  293. int rc, i;
  294. u64 size;
  295. bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
  296. if (!bus) {
  297. rc = -ENOMEM;
  298. goto failed;
  299. }
  300. virtfn = pci_iov_scan_device(dev, id, bus);
  301. if (IS_ERR(virtfn)) {
  302. rc = PTR_ERR(virtfn);
  303. goto failed0;
  304. }
  305. virtfn->dev.parent = dev->dev.parent;
  306. virtfn->multifunction = 0;
  307. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  308. int idx = pci_resource_num_from_vf_bar(i);
  309. res = &dev->resource[idx];
  310. if (!res->parent)
  311. continue;
  312. virtfn->resource[i].name = pci_name(virtfn);
  313. virtfn->resource[i].flags = res->flags;
  314. size = pci_iov_resource_size(dev, idx);
  315. resource_set_range(&virtfn->resource[i],
  316. res->start + size * id, size);
  317. rc = request_resource(res, &virtfn->resource[i]);
  318. BUG_ON(rc);
  319. }
  320. pci_device_add(virtfn, virtfn->bus);
  321. rc = pci_iov_sysfs_link(dev, virtfn, id);
  322. if (rc)
  323. goto failed1;
  324. pci_bus_add_device(virtfn);
  325. return 0;
  326. failed1:
  327. pci_stop_and_remove_bus_device(virtfn);
  328. pci_dev_put(dev);
  329. failed0:
  330. virtfn_remove_bus(dev->bus, bus);
  331. failed:
  332. return rc;
  333. }
  334. void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
  335. {
  336. char buf[VIRTFN_ID_LEN];
  337. struct pci_dev *virtfn;
  338. virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
  339. pci_iov_virtfn_bus(dev, id),
  340. pci_iov_virtfn_devfn(dev, id));
  341. if (!virtfn)
  342. return;
  343. sprintf(buf, "virtfn%u", id);
  344. sysfs_remove_link(&dev->dev.kobj, buf);
  345. /*
  346. * pci_stop_dev() could have been called for this virtfn already,
  347. * so the directory for the virtfn may have been removed before.
  348. * Double check to avoid spurious sysfs warnings.
  349. */
  350. if (virtfn->dev.kobj.sd)
  351. sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  352. pci_stop_and_remove_bus_device(virtfn);
  353. virtfn_remove_bus(dev->bus, virtfn->bus);
  354. /* balance pci_get_domain_bus_and_slot() */
  355. pci_dev_put(virtfn);
  356. pci_dev_put(dev);
  357. }
  358. static ssize_t sriov_totalvfs_show(struct device *dev,
  359. struct device_attribute *attr,
  360. char *buf)
  361. {
  362. struct pci_dev *pdev = to_pci_dev(dev);
  363. return sysfs_emit(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
  364. }
  365. static ssize_t sriov_numvfs_show(struct device *dev,
  366. struct device_attribute *attr,
  367. char *buf)
  368. {
  369. struct pci_dev *pdev = to_pci_dev(dev);
  370. u16 num_vfs;
  371. /* Serialize vs sriov_numvfs_store() so readers see valid num_VFs */
  372. device_lock(&pdev->dev);
  373. num_vfs = pdev->sriov->num_VFs;
  374. device_unlock(&pdev->dev);
  375. return sysfs_emit(buf, "%u\n", num_vfs);
  376. }
  377. /*
  378. * num_vfs > 0; number of VFs to enable
  379. * num_vfs = 0; disable all VFs
  380. *
  381. * Note: SRIOV spec does not allow partial VF
  382. * disable, so it's all or none.
  383. */
  384. static ssize_t sriov_numvfs_store(struct device *dev,
  385. struct device_attribute *attr,
  386. const char *buf, size_t count)
  387. {
  388. struct pci_dev *pdev = to_pci_dev(dev);
  389. int ret = 0;
  390. u16 num_vfs;
  391. if (kstrtou16(buf, 0, &num_vfs) < 0)
  392. return -EINVAL;
  393. if (num_vfs > pci_sriov_get_totalvfs(pdev))
  394. return -ERANGE;
  395. device_lock(&pdev->dev);
  396. if (num_vfs == pdev->sriov->num_VFs)
  397. goto exit;
  398. /* is PF driver loaded */
  399. if (!pdev->driver) {
  400. pci_info(pdev, "no driver bound to device; cannot configure SR-IOV\n");
  401. ret = -ENOENT;
  402. goto exit;
  403. }
  404. /* is PF driver loaded w/callback */
  405. if (!pdev->driver->sriov_configure) {
  406. pci_info(pdev, "driver does not support SR-IOV configuration via sysfs\n");
  407. ret = -ENOENT;
  408. goto exit;
  409. }
  410. if (num_vfs == 0) {
  411. /* disable VFs */
  412. pci_lock_rescan_remove();
  413. ret = pdev->driver->sriov_configure(pdev, 0);
  414. pci_unlock_rescan_remove();
  415. goto exit;
  416. }
  417. /* enable VFs */
  418. if (pdev->sriov->num_VFs) {
  419. pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
  420. pdev->sriov->num_VFs, num_vfs);
  421. ret = -EBUSY;
  422. goto exit;
  423. }
  424. pci_lock_rescan_remove();
  425. ret = pdev->driver->sriov_configure(pdev, num_vfs);
  426. pci_unlock_rescan_remove();
  427. if (ret < 0)
  428. goto exit;
  429. if (ret != num_vfs)
  430. pci_warn(pdev, "%d VFs requested; only %d enabled\n",
  431. num_vfs, ret);
  432. exit:
  433. device_unlock(&pdev->dev);
  434. if (ret < 0)
  435. return ret;
  436. return count;
  437. }
  438. static ssize_t sriov_offset_show(struct device *dev,
  439. struct device_attribute *attr,
  440. char *buf)
  441. {
  442. struct pci_dev *pdev = to_pci_dev(dev);
  443. return sysfs_emit(buf, "%u\n", pdev->sriov->offset);
  444. }
  445. static ssize_t sriov_stride_show(struct device *dev,
  446. struct device_attribute *attr,
  447. char *buf)
  448. {
  449. struct pci_dev *pdev = to_pci_dev(dev);
  450. return sysfs_emit(buf, "%u\n", pdev->sriov->stride);
  451. }
  452. static ssize_t sriov_vf_device_show(struct device *dev,
  453. struct device_attribute *attr,
  454. char *buf)
  455. {
  456. struct pci_dev *pdev = to_pci_dev(dev);
  457. return sysfs_emit(buf, "%x\n", pdev->sriov->vf_device);
  458. }
  459. static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
  460. struct device_attribute *attr,
  461. char *buf)
  462. {
  463. struct pci_dev *pdev = to_pci_dev(dev);
  464. return sysfs_emit(buf, "%u\n", pdev->sriov->drivers_autoprobe);
  465. }
  466. static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
  467. struct device_attribute *attr,
  468. const char *buf, size_t count)
  469. {
  470. struct pci_dev *pdev = to_pci_dev(dev);
  471. bool drivers_autoprobe;
  472. if (kstrtobool(buf, &drivers_autoprobe) < 0)
  473. return -EINVAL;
  474. pdev->sriov->drivers_autoprobe = drivers_autoprobe;
  475. return count;
  476. }
  477. static DEVICE_ATTR_RO(sriov_totalvfs);
  478. static DEVICE_ATTR_RW(sriov_numvfs);
  479. static DEVICE_ATTR_RO(sriov_offset);
  480. static DEVICE_ATTR_RO(sriov_stride);
  481. static DEVICE_ATTR_RO(sriov_vf_device);
  482. static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
  483. static struct attribute *sriov_pf_dev_attrs[] = {
  484. &dev_attr_sriov_totalvfs.attr,
  485. &dev_attr_sriov_numvfs.attr,
  486. &dev_attr_sriov_offset.attr,
  487. &dev_attr_sriov_stride.attr,
  488. &dev_attr_sriov_vf_device.attr,
  489. &dev_attr_sriov_drivers_autoprobe.attr,
  490. #ifdef CONFIG_PCI_MSI
  491. &dev_attr_sriov_vf_total_msix.attr,
  492. #endif
  493. NULL,
  494. };
  495. static umode_t sriov_pf_attrs_are_visible(struct kobject *kobj,
  496. struct attribute *a, int n)
  497. {
  498. struct device *dev = kobj_to_dev(kobj);
  499. if (!dev_is_pf(dev))
  500. return 0;
  501. return a->mode;
  502. }
  503. const struct attribute_group sriov_pf_dev_attr_group = {
  504. .attrs = sriov_pf_dev_attrs,
  505. .is_visible = sriov_pf_attrs_are_visible,
  506. };
  507. int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  508. {
  509. return 0;
  510. }
  511. int __weak pcibios_sriov_disable(struct pci_dev *pdev)
  512. {
  513. return 0;
  514. }
  515. static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
  516. {
  517. unsigned int i;
  518. int rc;
  519. if (dev->no_vf_scan)
  520. return 0;
  521. for (i = 0; i < num_vfs; i++) {
  522. rc = pci_iov_add_virtfn(dev, i);
  523. if (rc)
  524. goto failed;
  525. }
  526. return 0;
  527. failed:
  528. while (i--)
  529. pci_iov_remove_virtfn(dev, i);
  530. return rc;
  531. }
  532. static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
  533. {
  534. int rc;
  535. int i;
  536. int nres;
  537. u16 initial;
  538. struct resource *res;
  539. struct pci_dev *pdev;
  540. struct pci_sriov *iov = dev->sriov;
  541. int bars = 0;
  542. int bus;
  543. if (!nr_virtfn)
  544. return 0;
  545. if (iov->num_VFs)
  546. return -EINVAL;
  547. pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  548. if (initial > iov->total_VFs ||
  549. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
  550. return -EIO;
  551. if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
  552. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  553. return -EINVAL;
  554. nres = 0;
  555. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  556. int idx = pci_resource_num_from_vf_bar(i);
  557. resource_size_t vf_bar_sz = pci_iov_resource_size(dev, idx);
  558. bars |= (1 << idx);
  559. res = &dev->resource[idx];
  560. if (vf_bar_sz * nr_virtfn > resource_size(res))
  561. continue;
  562. if (res->parent)
  563. nres++;
  564. }
  565. if (nres != iov->nres) {
  566. pci_err(dev, "not enough MMIO resources for SR-IOV\n");
  567. return -ENOMEM;
  568. }
  569. bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
  570. if (bus > dev->bus->busn_res.end) {
  571. pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
  572. nr_virtfn, bus, &dev->bus->busn_res);
  573. return -ENOMEM;
  574. }
  575. if (pci_enable_resources(dev, bars)) {
  576. pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
  577. return -ENOMEM;
  578. }
  579. if (iov->link != dev->devfn) {
  580. pdev = pci_get_slot(dev->bus, iov->link);
  581. if (!pdev)
  582. return -ENODEV;
  583. if (!pdev->is_physfn) {
  584. pci_dev_put(pdev);
  585. return -ENOSYS;
  586. }
  587. rc = sysfs_create_link(&dev->dev.kobj,
  588. &pdev->dev.kobj, "dep_link");
  589. pci_dev_put(pdev);
  590. if (rc)
  591. return rc;
  592. }
  593. iov->initial_VFs = initial;
  594. if (nr_virtfn < initial)
  595. initial = nr_virtfn;
  596. rc = pcibios_sriov_enable(dev, initial);
  597. if (rc) {
  598. pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
  599. goto err_pcibios;
  600. }
  601. pci_iov_set_numvfs(dev, nr_virtfn);
  602. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  603. pci_cfg_access_lock(dev);
  604. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  605. msleep(100);
  606. pci_cfg_access_unlock(dev);
  607. rc = sriov_add_vfs(dev, initial);
  608. if (rc)
  609. goto err_pcibios;
  610. kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  611. iov->num_VFs = nr_virtfn;
  612. return 0;
  613. err_pcibios:
  614. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  615. pci_cfg_access_lock(dev);
  616. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  617. ssleep(1);
  618. pci_cfg_access_unlock(dev);
  619. pcibios_sriov_disable(dev);
  620. if (iov->link != dev->devfn)
  621. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  622. pci_iov_set_numvfs(dev, 0);
  623. return rc;
  624. }
  625. static void sriov_del_vfs(struct pci_dev *dev)
  626. {
  627. struct pci_sriov *iov = dev->sriov;
  628. int i;
  629. for (i = 0; i < iov->num_VFs; i++)
  630. pci_iov_remove_virtfn(dev, i);
  631. }
  632. static void sriov_disable(struct pci_dev *dev)
  633. {
  634. struct pci_sriov *iov = dev->sriov;
  635. if (!iov->num_VFs)
  636. return;
  637. sriov_del_vfs(dev);
  638. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  639. pci_cfg_access_lock(dev);
  640. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  641. ssleep(1);
  642. pci_cfg_access_unlock(dev);
  643. pcibios_sriov_disable(dev);
  644. if (iov->link != dev->devfn)
  645. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  646. iov->num_VFs = 0;
  647. pci_iov_set_numvfs(dev, 0);
  648. }
  649. static int sriov_init(struct pci_dev *dev, int pos)
  650. {
  651. int i, bar64;
  652. int rc;
  653. int nres;
  654. u32 pgsz;
  655. u16 ctrl, total;
  656. struct pci_sriov *iov;
  657. struct resource *res;
  658. const char *res_name;
  659. struct pci_dev *pdev;
  660. u32 sriovbars[PCI_SRIOV_NUM_BARS];
  661. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  662. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  663. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  664. ssleep(1);
  665. }
  666. ctrl = 0;
  667. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  668. if (pdev->is_physfn)
  669. goto found;
  670. pdev = NULL;
  671. if (pci_ari_enabled(dev->bus))
  672. ctrl |= PCI_SRIOV_CTRL_ARI;
  673. found:
  674. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  675. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  676. if (!total)
  677. return 0;
  678. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  679. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  680. pgsz &= ~((1 << i) - 1);
  681. if (!pgsz)
  682. return -EIO;
  683. pgsz &= ~(pgsz - 1);
  684. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  685. iov = kzalloc_obj(*iov);
  686. if (!iov)
  687. return -ENOMEM;
  688. /* Sizing SR-IOV BARs with VF Enable cleared - no decode */
  689. __pci_size_stdbars(dev, PCI_SRIOV_NUM_BARS,
  690. pos + PCI_SRIOV_BAR, sriovbars);
  691. nres = 0;
  692. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  693. int idx = pci_resource_num_from_vf_bar(i);
  694. res = &dev->resource[idx];
  695. res_name = pci_resource_name(dev, idx);
  696. /*
  697. * If it is already FIXED, don't change it, something
  698. * (perhaps EA or header fixups) wants it this way.
  699. */
  700. if (res->flags & IORESOURCE_PCI_FIXED)
  701. bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  702. else
  703. bar64 = __pci_read_base(dev, pci_bar_unknown, res,
  704. pos + PCI_SRIOV_BAR + i * 4,
  705. &sriovbars[i]);
  706. if (!res->flags)
  707. continue;
  708. if (resource_size(res) & (PAGE_SIZE - 1)) {
  709. rc = -EIO;
  710. goto failed;
  711. }
  712. iov->barsz[i] = resource_size(res);
  713. resource_set_size(res, resource_size(res) * total);
  714. pci_info(dev, "%s %pR: contains BAR %d for %d VFs\n",
  715. res_name, res, i, total);
  716. i += bar64;
  717. nres++;
  718. }
  719. iov->pos = pos;
  720. iov->nres = nres;
  721. iov->ctrl = ctrl;
  722. iov->total_VFs = total;
  723. iov->driver_max_VFs = total;
  724. pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
  725. iov->pgsz = pgsz;
  726. iov->self = dev;
  727. iov->drivers_autoprobe = true;
  728. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  729. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  730. if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  731. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  732. iov->vf_rebar_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VF_REBAR);
  733. if (pdev)
  734. iov->dev = pci_dev_get(pdev);
  735. else
  736. iov->dev = dev;
  737. dev->sriov = iov;
  738. dev->is_physfn = 1;
  739. rc = compute_max_vf_buses(dev);
  740. if (rc)
  741. goto fail_max_buses;
  742. return 0;
  743. fail_max_buses:
  744. dev->sriov = NULL;
  745. dev->is_physfn = 0;
  746. failed:
  747. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  748. res = &dev->resource[pci_resource_num_from_vf_bar(i)];
  749. res->flags = 0;
  750. }
  751. kfree(iov);
  752. return rc;
  753. }
  754. static void sriov_release(struct pci_dev *dev)
  755. {
  756. BUG_ON(dev->sriov->num_VFs);
  757. if (dev != dev->sriov->dev)
  758. pci_dev_put(dev->sriov->dev);
  759. kfree(dev->sriov);
  760. dev->sriov = NULL;
  761. }
  762. static void sriov_restore_vf_rebar_state(struct pci_dev *dev)
  763. {
  764. unsigned int pos, nbars, i;
  765. u32 ctrl;
  766. pos = pci_iov_vf_rebar_cap(dev);
  767. if (!pos)
  768. return;
  769. pci_read_config_dword(dev, pos + PCI_VF_REBAR_CTRL, &ctrl);
  770. nbars = FIELD_GET(PCI_VF_REBAR_CTRL_NBAR_MASK, ctrl);
  771. for (i = 0; i < nbars; i++, pos += 8) {
  772. int bar_idx, size;
  773. pci_read_config_dword(dev, pos + PCI_VF_REBAR_CTRL, &ctrl);
  774. bar_idx = FIELD_GET(PCI_VF_REBAR_CTRL_BAR_IDX, ctrl);
  775. size = pci_rebar_bytes_to_size(dev->sriov->barsz[bar_idx]);
  776. ctrl &= ~PCI_VF_REBAR_CTRL_BAR_SIZE;
  777. ctrl |= FIELD_PREP(PCI_VF_REBAR_CTRL_BAR_SIZE, size);
  778. pci_write_config_dword(dev, pos + PCI_VF_REBAR_CTRL, ctrl);
  779. }
  780. }
  781. static void sriov_restore_state(struct pci_dev *dev)
  782. {
  783. int i;
  784. u16 ctrl;
  785. struct pci_sriov *iov = dev->sriov;
  786. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  787. if (ctrl & PCI_SRIOV_CTRL_VFE)
  788. return;
  789. /*
  790. * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
  791. * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
  792. */
  793. ctrl &= ~PCI_SRIOV_CTRL_ARI;
  794. ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
  795. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
  796. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  797. pci_update_resource(dev, pci_resource_num_from_vf_bar(i));
  798. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  799. pci_iov_set_numvfs(dev, iov->num_VFs);
  800. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  801. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  802. msleep(100);
  803. }
  804. /**
  805. * pci_iov_init - initialize the IOV capability
  806. * @dev: the PCI device
  807. *
  808. * Returns 0 on success, or negative on failure.
  809. */
  810. int pci_iov_init(struct pci_dev *dev)
  811. {
  812. int pos;
  813. if (!pci_is_pcie(dev))
  814. return -ENODEV;
  815. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  816. if (pos)
  817. return sriov_init(dev, pos);
  818. return -ENODEV;
  819. }
  820. /**
  821. * pci_iov_release - release resources used by the IOV capability
  822. * @dev: the PCI device
  823. */
  824. void pci_iov_release(struct pci_dev *dev)
  825. {
  826. if (dev->is_physfn)
  827. sriov_release(dev);
  828. }
  829. /**
  830. * pci_iov_remove - clean up SR-IOV state after PF driver is detached
  831. * @dev: the PCI device
  832. */
  833. void pci_iov_remove(struct pci_dev *dev)
  834. {
  835. struct pci_sriov *iov = dev->sriov;
  836. if (!dev->is_physfn)
  837. return;
  838. iov->driver_max_VFs = iov->total_VFs;
  839. if (iov->num_VFs)
  840. pci_warn(dev, "driver left SR-IOV enabled after remove\n");
  841. }
  842. /**
  843. * pci_iov_update_resource - update a VF BAR
  844. * @dev: the PCI device
  845. * @resno: the resource number
  846. *
  847. * Update a VF BAR in the SR-IOV capability of a PF.
  848. */
  849. void pci_iov_update_resource(struct pci_dev *dev, int resno)
  850. {
  851. struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
  852. struct resource *res = pci_resource_n(dev, resno);
  853. int vf_bar = pci_resource_num_to_vf_bar(resno);
  854. struct pci_bus_region region;
  855. u16 cmd;
  856. u32 new;
  857. int reg;
  858. /*
  859. * The generic pci_restore_bars() path calls this for all devices,
  860. * including VFs and non-SR-IOV devices. If this is not a PF, we
  861. * have nothing to do.
  862. */
  863. if (!iov)
  864. return;
  865. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
  866. if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
  867. dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
  868. vf_bar, res);
  869. return;
  870. }
  871. /*
  872. * Ignore unimplemented BARs, unused resource slots for 64-bit
  873. * BARs, and non-movable resources, e.g., those described via
  874. * Enhanced Allocation.
  875. */
  876. if (!res->flags)
  877. return;
  878. if (res->flags & IORESOURCE_UNSET)
  879. return;
  880. if (res->flags & IORESOURCE_PCI_FIXED)
  881. return;
  882. pcibios_resource_to_bus(dev->bus, &region, res);
  883. new = region.start;
  884. new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
  885. reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
  886. pci_write_config_dword(dev, reg, new);
  887. if (res->flags & IORESOURCE_MEM_64) {
  888. new = region.start >> 16 >> 16;
  889. pci_write_config_dword(dev, reg + 4, new);
  890. }
  891. }
  892. resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
  893. int resno)
  894. {
  895. return pci_iov_resource_size(dev, resno);
  896. }
  897. /**
  898. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  899. * @dev: the PCI device
  900. * @resno: the resource number
  901. *
  902. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  903. * This is not the same as the resource size which is defined as
  904. * the VF BAR size multiplied by the number of VFs. The alignment
  905. * is just the VF BAR size.
  906. */
  907. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  908. {
  909. return pcibios_iov_resource_alignment(dev, resno);
  910. }
  911. /**
  912. * pci_restore_iov_state - restore the state of the IOV capability
  913. * @dev: the PCI device
  914. */
  915. void pci_restore_iov_state(struct pci_dev *dev)
  916. {
  917. if (dev->is_physfn) {
  918. sriov_restore_vf_rebar_state(dev);
  919. sriov_restore_state(dev);
  920. }
  921. }
  922. /**
  923. * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
  924. * @dev: the PCI device
  925. * @auto_probe: set VF drivers auto probe flag
  926. */
  927. void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
  928. {
  929. if (dev->is_physfn)
  930. dev->sriov->drivers_autoprobe = auto_probe;
  931. }
  932. /**
  933. * pci_iov_bus_range - find bus range used by Virtual Function
  934. * @bus: the PCI bus
  935. *
  936. * Returns max number of buses (exclude current one) used by Virtual
  937. * Functions.
  938. */
  939. int pci_iov_bus_range(struct pci_bus *bus)
  940. {
  941. int max = 0;
  942. struct pci_dev *dev;
  943. list_for_each_entry(dev, &bus->devices, bus_list) {
  944. if (!dev->is_physfn)
  945. continue;
  946. if (dev->sriov->max_VF_buses > max)
  947. max = dev->sriov->max_VF_buses;
  948. }
  949. return max ? max - bus->number : 0;
  950. }
  951. /**
  952. * pci_enable_sriov - enable the SR-IOV capability
  953. * @dev: the PCI device
  954. * @nr_virtfn: number of virtual functions to enable
  955. *
  956. * Returns 0 on success, or negative on failure.
  957. */
  958. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  959. {
  960. might_sleep();
  961. if (!dev->is_physfn)
  962. return -ENOSYS;
  963. return sriov_enable(dev, nr_virtfn);
  964. }
  965. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  966. /**
  967. * pci_disable_sriov - disable the SR-IOV capability
  968. * @dev: the PCI device
  969. */
  970. void pci_disable_sriov(struct pci_dev *dev)
  971. {
  972. might_sleep();
  973. if (!dev->is_physfn)
  974. return;
  975. sriov_disable(dev);
  976. }
  977. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  978. /**
  979. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  980. * @dev: the PCI device
  981. *
  982. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  983. */
  984. int pci_num_vf(struct pci_dev *dev)
  985. {
  986. if (!dev->is_physfn)
  987. return 0;
  988. return dev->sriov->num_VFs;
  989. }
  990. EXPORT_SYMBOL_GPL(pci_num_vf);
  991. /**
  992. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  993. * @dev: the PCI device
  994. *
  995. * Returns number of VFs belonging to this device that are assigned to a guest.
  996. * If device is not a physical function returns 0.
  997. */
  998. int pci_vfs_assigned(struct pci_dev *dev)
  999. {
  1000. struct pci_dev *vfdev;
  1001. unsigned int vfs_assigned = 0;
  1002. unsigned short dev_id;
  1003. /* only search if we are a PF */
  1004. if (!dev->is_physfn)
  1005. return 0;
  1006. /*
  1007. * determine the device ID for the VFs, the vendor ID will be the
  1008. * same as the PF so there is no need to check for that one
  1009. */
  1010. dev_id = dev->sriov->vf_device;
  1011. /* loop through all the VFs to see if we own any that are assigned */
  1012. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  1013. while (vfdev) {
  1014. /*
  1015. * It is considered assigned if it is a virtual function with
  1016. * our dev as the physical function and the assigned bit is set
  1017. */
  1018. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  1019. pci_is_dev_assigned(vfdev))
  1020. vfs_assigned++;
  1021. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  1022. }
  1023. return vfs_assigned;
  1024. }
  1025. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  1026. /**
  1027. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  1028. * @dev: the PCI PF device
  1029. * @numvfs: number that should be used for TotalVFs supported
  1030. *
  1031. * Should be called from PF driver's probe routine with
  1032. * device's mutex held.
  1033. *
  1034. * Returns 0 if PF is an SRIOV-capable device and
  1035. * value of numvfs valid. If not a PF return -ENOSYS;
  1036. * if numvfs is invalid return -EINVAL;
  1037. * if VFs already enabled, return -EBUSY.
  1038. */
  1039. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  1040. {
  1041. if (!dev->is_physfn)
  1042. return -ENOSYS;
  1043. if (numvfs > dev->sriov->total_VFs)
  1044. return -EINVAL;
  1045. /* Shouldn't change if VFs already enabled */
  1046. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  1047. return -EBUSY;
  1048. dev->sriov->driver_max_VFs = numvfs;
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  1052. /**
  1053. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  1054. * @dev: the PCI PF device
  1055. *
  1056. * For a PCIe device with SRIOV support, return the PCIe
  1057. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  1058. * if the driver reduced it. Otherwise 0.
  1059. */
  1060. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  1061. {
  1062. if (!dev->is_physfn)
  1063. return 0;
  1064. return dev->sriov->driver_max_VFs;
  1065. }
  1066. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
  1067. /**
  1068. * pci_sriov_configure_simple - helper to configure SR-IOV
  1069. * @dev: the PCI device
  1070. * @nr_virtfn: number of virtual functions to enable, 0 to disable
  1071. *
  1072. * Enable or disable SR-IOV for devices that don't require any PF setup
  1073. * before enabling SR-IOV. Return value is negative on error, or number of
  1074. * VFs allocated on success.
  1075. */
  1076. int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
  1077. {
  1078. int rc;
  1079. might_sleep();
  1080. if (!dev->is_physfn)
  1081. return -ENODEV;
  1082. if (pci_vfs_assigned(dev)) {
  1083. pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
  1084. return -EPERM;
  1085. }
  1086. if (nr_virtfn == 0) {
  1087. sriov_disable(dev);
  1088. return 0;
  1089. }
  1090. rc = sriov_enable(dev, nr_virtfn);
  1091. if (rc < 0)
  1092. return rc;
  1093. return nr_virtfn;
  1094. }
  1095. EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);
  1096. /**
  1097. * pci_iov_vf_bar_set_size - set a new size for a VF BAR
  1098. * @dev: the PCI device
  1099. * @resno: the resource number
  1100. * @size: new size as defined in the spec (0=1MB, 31=128TB)
  1101. *
  1102. * Set the new size of a VF BAR that supports VF resizable BAR capability.
  1103. * Unlike pci_resize_resource(), this does not cause the resource that
  1104. * reserves the MMIO space (originally up to total_VFs) to be resized, which
  1105. * means that following calls to pci_enable_sriov() can fail if the resources
  1106. * no longer fit.
  1107. *
  1108. * Return: 0 on success, or negative on failure.
  1109. */
  1110. int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
  1111. {
  1112. if (!pci_resource_is_iov(resno))
  1113. return -EINVAL;
  1114. if (pci_iov_is_memory_decoding_enabled(dev))
  1115. return -EBUSY;
  1116. if (!pci_rebar_size_supported(dev, resno, size))
  1117. return -EINVAL;
  1118. return pci_rebar_set_size(dev, resno, size);
  1119. }
  1120. EXPORT_SYMBOL_GPL(pci_iov_vf_bar_set_size);
  1121. /**
  1122. * pci_iov_vf_bar_get_sizes - get VF BAR sizes allowing to create up to num_vfs
  1123. * @dev: the PCI device
  1124. * @resno: the resource number
  1125. * @num_vfs: number of VFs
  1126. *
  1127. * Get the sizes of a VF resizable BAR that can accommodate @num_vfs within
  1128. * the currently assigned size of the resource @resno.
  1129. *
  1130. * Return: A bitmask of sizes in format defined in the spec (bit 0=1MB,
  1131. * bit 31=128TB).
  1132. */
  1133. u32 pci_iov_vf_bar_get_sizes(struct pci_dev *dev, int resno, int num_vfs)
  1134. {
  1135. u64 vf_len = pci_resource_len(dev, resno);
  1136. u64 sizes;
  1137. if (!num_vfs)
  1138. return 0;
  1139. do_div(vf_len, num_vfs);
  1140. sizes = (roundup_pow_of_two(vf_len + 1) - 1) >> ilog2(SZ_1M);
  1141. return sizes & pci_rebar_get_possible_sizes(dev, resno);
  1142. }
  1143. EXPORT_SYMBOL_GPL(pci_iov_vf_bar_get_sizes);