ib_virt.c 5.4 KB

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