sriov.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2014, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/pci.h>
  33. #include <linux/mlx5/driver.h>
  34. #include <linux/mlx5/vport.h>
  35. #include "mlx5_core.h"
  36. #include "eswitch.h"
  37. bool mlx5_sriov_is_enabled(struct mlx5_core_dev *dev)
  38. {
  39. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  40. return !!sriov->num_vfs;
  41. }
  42. static int sriov_restore_guids(struct mlx5_core_dev *dev, int vf)
  43. {
  44. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  45. struct mlx5_hca_vport_context *in;
  46. int err = 0;
  47. /* Restore sriov guid and policy settings */
  48. if (sriov->vfs_ctx[vf].node_guid ||
  49. sriov->vfs_ctx[vf].port_guid ||
  50. sriov->vfs_ctx[vf].policy != MLX5_POLICY_INVALID) {
  51. in = kzalloc(sizeof(*in), GFP_KERNEL);
  52. if (!in)
  53. return -ENOMEM;
  54. in->node_guid = sriov->vfs_ctx[vf].node_guid;
  55. in->port_guid = sriov->vfs_ctx[vf].port_guid;
  56. in->policy = sriov->vfs_ctx[vf].policy;
  57. in->field_select =
  58. !!(in->port_guid) * MLX5_HCA_VPORT_SEL_PORT_GUID |
  59. !!(in->node_guid) * MLX5_HCA_VPORT_SEL_NODE_GUID |
  60. !!(in->policy) * MLX5_HCA_VPORT_SEL_STATE_POLICY;
  61. err = mlx5_core_modify_hca_vport_context(dev, 1, 1, vf + 1, in);
  62. if (err)
  63. mlx5_core_warn(dev, "modify vport context failed, unable to restore VF %d settings\n", vf);
  64. kfree(in);
  65. }
  66. return err;
  67. }
  68. static int mlx5_device_enable_sriov(struct mlx5_core_dev *dev, int num_vfs)
  69. {
  70. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  71. int err;
  72. int vf;
  73. if (sriov->enabled_vfs) {
  74. mlx5_core_warn(dev,
  75. "failed to enable SRIOV on device, already enabled with %d vfs\n",
  76. sriov->enabled_vfs);
  77. return -EBUSY;
  78. }
  79. if (!MLX5_ESWITCH_MANAGER(dev))
  80. goto enable_vfs_hca;
  81. err = mlx5_eswitch_enable_sriov(dev->priv.eswitch, num_vfs, SRIOV_LEGACY);
  82. if (err) {
  83. mlx5_core_warn(dev,
  84. "failed to enable eswitch SRIOV (%d)\n", err);
  85. return err;
  86. }
  87. enable_vfs_hca:
  88. for (vf = 0; vf < num_vfs; vf++) {
  89. err = mlx5_core_enable_hca(dev, vf + 1);
  90. if (err) {
  91. mlx5_core_warn(dev, "failed to enable VF %d (%d)\n", vf, err);
  92. continue;
  93. }
  94. sriov->vfs_ctx[vf].enabled = 1;
  95. sriov->enabled_vfs++;
  96. if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) {
  97. err = sriov_restore_guids(dev, vf);
  98. if (err) {
  99. mlx5_core_warn(dev,
  100. "failed to restore VF %d settings, err %d\n",
  101. vf, err);
  102. continue;
  103. }
  104. }
  105. mlx5_core_dbg(dev, "successfully enabled VF* %d\n", vf);
  106. }
  107. return 0;
  108. }
  109. static void mlx5_device_disable_sriov(struct mlx5_core_dev *dev)
  110. {
  111. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  112. int err;
  113. int vf;
  114. if (!sriov->enabled_vfs)
  115. goto out;
  116. for (vf = 0; vf < sriov->num_vfs; vf++) {
  117. if (!sriov->vfs_ctx[vf].enabled)
  118. continue;
  119. err = mlx5_core_disable_hca(dev, vf + 1);
  120. if (err) {
  121. mlx5_core_warn(dev, "failed to disable VF %d\n", vf);
  122. continue;
  123. }
  124. sriov->vfs_ctx[vf].enabled = 0;
  125. sriov->enabled_vfs--;
  126. }
  127. out:
  128. if (MLX5_ESWITCH_MANAGER(dev))
  129. mlx5_eswitch_disable_sriov(dev->priv.eswitch);
  130. if (mlx5_wait_for_vf_pages(dev))
  131. mlx5_core_warn(dev, "timeout reclaiming VFs pages\n");
  132. }
  133. static int mlx5_pci_enable_sriov(struct pci_dev *pdev, int num_vfs)
  134. {
  135. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  136. int err = 0;
  137. if (pci_num_vf(pdev)) {
  138. mlx5_core_warn(dev, "Unable to enable pci sriov, already enabled\n");
  139. return -EBUSY;
  140. }
  141. err = pci_enable_sriov(pdev, num_vfs);
  142. if (err)
  143. mlx5_core_warn(dev, "pci_enable_sriov failed : %d\n", err);
  144. return err;
  145. }
  146. static void mlx5_pci_disable_sriov(struct pci_dev *pdev)
  147. {
  148. pci_disable_sriov(pdev);
  149. }
  150. static int mlx5_sriov_enable(struct pci_dev *pdev, int num_vfs)
  151. {
  152. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  153. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  154. int err = 0;
  155. err = mlx5_device_enable_sriov(dev, num_vfs);
  156. if (err) {
  157. mlx5_core_warn(dev, "mlx5_device_enable_sriov failed : %d\n", err);
  158. return err;
  159. }
  160. err = mlx5_pci_enable_sriov(pdev, num_vfs);
  161. if (err) {
  162. mlx5_core_warn(dev, "mlx5_pci_enable_sriov failed : %d\n", err);
  163. mlx5_device_disable_sriov(dev);
  164. return err;
  165. }
  166. sriov->num_vfs = num_vfs;
  167. return 0;
  168. }
  169. static void mlx5_sriov_disable(struct pci_dev *pdev)
  170. {
  171. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  172. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  173. mlx5_pci_disable_sriov(pdev);
  174. mlx5_device_disable_sriov(dev);
  175. sriov->num_vfs = 0;
  176. }
  177. int mlx5_core_sriov_configure(struct pci_dev *pdev, int num_vfs)
  178. {
  179. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  180. int err = 0;
  181. mlx5_core_dbg(dev, "requested num_vfs %d\n", num_vfs);
  182. if (!mlx5_core_is_pf(dev))
  183. return -EPERM;
  184. if (num_vfs) {
  185. int ret;
  186. ret = mlx5_lag_forbid(dev);
  187. if (ret && (ret != -ENODEV))
  188. return ret;
  189. }
  190. if (num_vfs) {
  191. err = mlx5_sriov_enable(pdev, num_vfs);
  192. } else {
  193. mlx5_sriov_disable(pdev);
  194. mlx5_lag_allow(dev);
  195. }
  196. return err ? err : num_vfs;
  197. }
  198. int mlx5_sriov_attach(struct mlx5_core_dev *dev)
  199. {
  200. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  201. if (!mlx5_core_is_pf(dev) || !sriov->num_vfs)
  202. return 0;
  203. /* If sriov VFs exist in PCI level, enable them in device level */
  204. return mlx5_device_enable_sriov(dev, sriov->num_vfs);
  205. }
  206. void mlx5_sriov_detach(struct mlx5_core_dev *dev)
  207. {
  208. if (!mlx5_core_is_pf(dev))
  209. return;
  210. mlx5_device_disable_sriov(dev);
  211. }
  212. int mlx5_sriov_init(struct mlx5_core_dev *dev)
  213. {
  214. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  215. struct pci_dev *pdev = dev->pdev;
  216. int total_vfs;
  217. if (!mlx5_core_is_pf(dev))
  218. return 0;
  219. total_vfs = pci_sriov_get_totalvfs(pdev);
  220. sriov->num_vfs = pci_num_vf(pdev);
  221. sriov->vfs_ctx = kcalloc(total_vfs, sizeof(*sriov->vfs_ctx), GFP_KERNEL);
  222. if (!sriov->vfs_ctx)
  223. return -ENOMEM;
  224. return 0;
  225. }
  226. void mlx5_sriov_cleanup(struct mlx5_core_dev *dev)
  227. {
  228. struct mlx5_core_sriov *sriov = &dev->priv.sriov;
  229. if (!mlx5_core_is_pf(dev))
  230. return;
  231. kfree(sriov->vfs_ctx);
  232. }