ib_virt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2016, Mellanox Technologies. 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/mlx5/vport.h>
  33. #include "mlx5_ib.h"
  34. static inline u32 mlx_to_net_policy(enum port_state_policy mlx_policy)
  35. {
  36. switch (mlx_policy) {
  37. case MLX5_POLICY_DOWN:
  38. return IFLA_VF_LINK_STATE_DISABLE;
  39. case MLX5_POLICY_UP:
  40. return IFLA_VF_LINK_STATE_ENABLE;
  41. case MLX5_POLICY_FOLLOW:
  42. return IFLA_VF_LINK_STATE_AUTO;
  43. default:
  44. return __IFLA_VF_LINK_STATE_MAX;
  45. }
  46. }
  47. int mlx5_ib_get_vf_config(struct ib_device *device, int vf, u32 port,
  48. struct ifla_vf_info *info)
  49. {
  50. struct mlx5_ib_dev *dev = to_mdev(device);
  51. struct mlx5_core_dev *mdev = dev->mdev;
  52. struct mlx5_hca_vport_context *rep;
  53. int err;
  54. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  55. if (!rep)
  56. return -ENOMEM;
  57. err = mlx5_query_hca_vport_context(mdev, 1, 1, vf + 1, rep);
  58. if (err) {
  59. mlx5_ib_warn(dev, "failed to query port policy for vf %d (%d)\n",
  60. vf, err);
  61. goto free;
  62. }
  63. memset(info, 0, sizeof(*info));
  64. info->linkstate = mlx_to_net_policy(rep->policy);
  65. if (info->linkstate == __IFLA_VF_LINK_STATE_MAX)
  66. err = -EINVAL;
  67. free:
  68. kfree(rep);
  69. return err;
  70. }
  71. static inline enum port_state_policy net_to_mlx_policy(int policy)
  72. {
  73. switch (policy) {
  74. case IFLA_VF_LINK_STATE_DISABLE:
  75. return MLX5_POLICY_DOWN;
  76. case IFLA_VF_LINK_STATE_ENABLE:
  77. return MLX5_POLICY_UP;
  78. case IFLA_VF_LINK_STATE_AUTO:
  79. return MLX5_POLICY_FOLLOW;
  80. default:
  81. return MLX5_POLICY_INVALID;
  82. }
  83. }
  84. int mlx5_ib_set_vf_link_state(struct ib_device *device, int vf,
  85. u32 port, int state)
  86. {
  87. struct mlx5_ib_dev *dev = to_mdev(device);
  88. struct mlx5_core_dev *mdev = dev->mdev;
  89. struct mlx5_hca_vport_context *in;
  90. struct mlx5_vf_context *vfs_ctx = mdev->priv.sriov.vfs_ctx;
  91. int err;
  92. in = kzalloc(sizeof(*in), GFP_KERNEL);
  93. if (!in)
  94. return -ENOMEM;
  95. in->policy = net_to_mlx_policy(state);
  96. if (in->policy == MLX5_POLICY_INVALID) {
  97. err = -EINVAL;
  98. goto out;
  99. }
  100. in->field_select = MLX5_HCA_VPORT_SEL_STATE_POLICY;
  101. err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in);
  102. if (!err)
  103. vfs_ctx[vf].policy = in->policy;
  104. out:
  105. kfree(in);
  106. return err;
  107. }
  108. int mlx5_ib_get_vf_stats(struct ib_device *device, int vf,
  109. u32 port, struct ifla_vf_stats *stats)
  110. {
  111. int out_sz = MLX5_ST_SZ_BYTES(query_vport_counter_out);
  112. struct mlx5_core_dev *mdev;
  113. struct mlx5_ib_dev *dev;
  114. void *out;
  115. int err;
  116. dev = to_mdev(device);
  117. mdev = dev->mdev;
  118. out = kzalloc(out_sz, GFP_KERNEL);
  119. if (!out)
  120. return -ENOMEM;
  121. err = mlx5_core_query_vport_counter(mdev, true, vf, port, out);
  122. if (err)
  123. goto ex;
  124. stats->rx_packets = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_unicast.packets);
  125. stats->tx_packets = MLX5_GET64_PR(query_vport_counter_out, out, transmitted_ib_unicast.packets);
  126. stats->rx_bytes = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_unicast.octets);
  127. stats->tx_bytes = MLX5_GET64_PR(query_vport_counter_out, out, transmitted_ib_unicast.octets);
  128. stats->multicast = MLX5_GET64_PR(query_vport_counter_out, out, received_ib_multicast.packets);
  129. ex:
  130. kfree(out);
  131. return err;
  132. }
  133. static int set_vf_node_guid(struct ib_device *device, int vf, u32 port,
  134. u64 guid)
  135. {
  136. struct mlx5_ib_dev *dev = to_mdev(device);
  137. struct mlx5_core_dev *mdev = dev->mdev;
  138. struct mlx5_hca_vport_context *in;
  139. struct mlx5_vf_context *vfs_ctx = mdev->priv.sriov.vfs_ctx;
  140. int err;
  141. in = kzalloc(sizeof(*in), GFP_KERNEL);
  142. if (!in)
  143. return -ENOMEM;
  144. in->field_select = MLX5_HCA_VPORT_SEL_NODE_GUID;
  145. in->node_guid = guid;
  146. err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in);
  147. if (!err) {
  148. vfs_ctx[vf].node_guid = guid;
  149. vfs_ctx[vf].node_guid_valid = 1;
  150. }
  151. kfree(in);
  152. return err;
  153. }
  154. static int set_vf_port_guid(struct ib_device *device, int vf, u32 port,
  155. u64 guid)
  156. {
  157. struct mlx5_ib_dev *dev = to_mdev(device);
  158. struct mlx5_core_dev *mdev = dev->mdev;
  159. struct mlx5_hca_vport_context *in;
  160. struct mlx5_vf_context *vfs_ctx = mdev->priv.sriov.vfs_ctx;
  161. int err;
  162. in = kzalloc(sizeof(*in), GFP_KERNEL);
  163. if (!in)
  164. return -ENOMEM;
  165. in->field_select = MLX5_HCA_VPORT_SEL_PORT_GUID;
  166. in->port_guid = guid;
  167. err = mlx5_core_modify_hca_vport_context(mdev, 1, 1, vf + 1, in);
  168. if (!err) {
  169. vfs_ctx[vf].port_guid = guid;
  170. vfs_ctx[vf].port_guid_valid = 1;
  171. }
  172. kfree(in);
  173. return err;
  174. }
  175. int mlx5_ib_set_vf_guid(struct ib_device *device, int vf, u32 port,
  176. u64 guid, int type)
  177. {
  178. if (type == IFLA_VF_IB_NODE_GUID)
  179. return set_vf_node_guid(device, vf, port, guid);
  180. else if (type == IFLA_VF_IB_PORT_GUID)
  181. return set_vf_port_guid(device, vf, port, guid);
  182. return -EINVAL;
  183. }
  184. int mlx5_ib_get_vf_guid(struct ib_device *device, int vf, u32 port,
  185. struct ifla_vf_guid *node_guid,
  186. struct ifla_vf_guid *port_guid)
  187. {
  188. struct mlx5_ib_dev *dev = to_mdev(device);
  189. struct mlx5_core_dev *mdev = dev->mdev;
  190. struct mlx5_vf_context *vfs_ctx = mdev->priv.sriov.vfs_ctx;
  191. node_guid->guid =
  192. vfs_ctx[vf].node_guid_valid ? vfs_ctx[vf].node_guid : 0;
  193. port_guid->guid =
  194. vfs_ctx[vf].port_guid_valid ? vfs_ctx[vf].port_guid : 0;
  195. return 0;
  196. }