pci-iov-howto.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. ====================================
  4. PCI Express I/O Virtualization Howto
  5. ====================================
  6. :Copyright: |copy| 2009 Intel Corporation
  7. :Authors: - Yu Zhao <yu.zhao@intel.com>
  8. - Donald Dutile <ddutile@redhat.com>
  9. Overview
  10. ========
  11. What is SR-IOV
  12. --------------
  13. Single Root I/O Virtualization (SR-IOV) is a PCI Express Extended
  14. capability which makes one physical device appear as multiple virtual
  15. devices. The physical device is referred to as Physical Function (PF)
  16. while the virtual devices are referred to as Virtual Functions (VF).
  17. Allocation of the VF can be dynamically controlled by the PF via
  18. registers encapsulated in the capability. By default, this feature is
  19. not enabled and the PF behaves as traditional PCIe device. Once it's
  20. turned on, each VF's PCI configuration space can be accessed by its own
  21. Bus, Device and Function Number (Routing ID). And each VF also has PCI
  22. Memory Space, which is used to map its register set. VF device driver
  23. operates on the register set so it can be functional and appear as a
  24. real existing PCI device.
  25. User Guide
  26. ==========
  27. How can I enable SR-IOV capability
  28. ----------------------------------
  29. Multiple methods are available for SR-IOV enablement.
  30. In the first method, the device driver (PF driver) will control the
  31. enabling and disabling of the capability via API provided by SR-IOV core.
  32. If the hardware has SR-IOV capability, loading its PF driver would
  33. enable it and all VFs associated with the PF. Some PF drivers require
  34. a module parameter to be set to determine the number of VFs to enable.
  35. In the second method, a write to the sysfs file sriov_numvfs will
  36. enable and disable the VFs associated with a PCIe PF. This method
  37. enables per-PF, VF enable/disable values versus the first method,
  38. which applies to all PFs of the same device. Additionally, the
  39. PCI SRIOV core support ensures that enable/disable operations are
  40. valid to reduce duplication in multiple drivers for the same
  41. checks, e.g., check numvfs == 0 if enabling VFs, ensure
  42. numvfs <= totalvfs.
  43. The second method is the recommended method for new/future VF devices.
  44. How can I use the Virtual Functions
  45. -----------------------------------
  46. The VF is treated as hot-plugged PCI devices in the kernel, so they
  47. should be able to work in the same way as real PCI devices. The VF
  48. requires device driver that is same as a normal PCI device's.
  49. Developer Guide
  50. ===============
  51. SR-IOV API
  52. ----------
  53. To enable SR-IOV capability:
  54. (a) For the first method, in the driver::
  55. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn);
  56. 'nr_virtfn' is number of VFs to be enabled.
  57. (b) For the second method, from sysfs::
  58. echo 'nr_virtfn' > \
  59. /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_numvfs
  60. To disable SR-IOV capability:
  61. (a) For the first method, in the driver::
  62. void pci_disable_sriov(struct pci_dev *dev);
  63. (b) For the second method, from sysfs::
  64. echo 0 > \
  65. /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_numvfs
  66. To enable auto probing VFs by a compatible driver on the host, run
  67. command below before enabling SR-IOV capabilities. This is the
  68. default behavior.
  69. ::
  70. echo 1 > \
  71. /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_drivers_autoprobe
  72. To disable auto probing VFs by a compatible driver on the host, run
  73. command below before enabling SR-IOV capabilities. Updating this
  74. entry will not affect VFs which are already probed.
  75. ::
  76. echo 0 > \
  77. /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_drivers_autoprobe
  78. Usage example
  79. -------------
  80. Following piece of code illustrates the usage of the SR-IOV API.
  81. ::
  82. static int dev_probe(struct pci_dev *dev, const struct pci_device_id *id)
  83. {
  84. pci_enable_sriov(dev, NR_VIRTFN);
  85. ...
  86. return 0;
  87. }
  88. static void dev_remove(struct pci_dev *dev)
  89. {
  90. pci_disable_sriov(dev);
  91. ...
  92. }
  93. static int dev_suspend(struct pci_dev *dev, pm_message_t state)
  94. {
  95. ...
  96. return 0;
  97. }
  98. static int dev_resume(struct pci_dev *dev)
  99. {
  100. ...
  101. return 0;
  102. }
  103. static void dev_shutdown(struct pci_dev *dev)
  104. {
  105. ...
  106. }
  107. static int dev_sriov_configure(struct pci_dev *dev, int numvfs)
  108. {
  109. if (numvfs > 0) {
  110. ...
  111. pci_enable_sriov(dev, numvfs);
  112. ...
  113. return numvfs;
  114. }
  115. if (numvfs == 0) {
  116. ....
  117. pci_disable_sriov(dev);
  118. ...
  119. return 0;
  120. }
  121. }
  122. static struct pci_driver dev_driver = {
  123. .name = "SR-IOV Physical Function driver",
  124. .id_table = dev_id_table,
  125. .probe = dev_probe,
  126. .remove = dev_remove,
  127. .suspend = dev_suspend,
  128. .resume = dev_resume,
  129. .shutdown = dev_shutdown,
  130. .sriov_configure = dev_sriov_configure,
  131. };