mad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2013-2015, 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/cmd.h>
  33. #include <rdma/ib_mad.h>
  34. #include <rdma/ib_smi.h>
  35. #include "mlx5_ib.h"
  36. enum {
  37. MLX5_IB_VENDOR_CLASS1 = 0x9,
  38. MLX5_IB_VENDOR_CLASS2 = 0xa
  39. };
  40. int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  41. u8 port, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  42. const void *in_mad, void *response_mad)
  43. {
  44. u8 op_modifier = 0;
  45. /* Key check traps can't be generated unless we have in_wc to
  46. * tell us where to send the trap.
  47. */
  48. if (ignore_mkey || !in_wc)
  49. op_modifier |= 0x1;
  50. if (ignore_bkey || !in_wc)
  51. op_modifier |= 0x2;
  52. return mlx5_core_mad_ifc(dev->mdev, in_mad, response_mad, op_modifier, port);
  53. }
  54. int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  55. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  56. const struct ib_mad_hdr *in, size_t in_mad_size,
  57. struct ib_mad_hdr *out, size_t *out_mad_size,
  58. u16 *out_mad_pkey_index)
  59. {
  60. u16 slid;
  61. int err;
  62. const struct ib_mad *in_mad = (const struct ib_mad *)in;
  63. struct ib_mad *out_mad = (struct ib_mad *)out;
  64. BUG_ON(in_mad_size != sizeof(*in_mad) ||
  65. *out_mad_size != sizeof(*out_mad));
  66. slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
  67. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0)
  68. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  69. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  70. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  71. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  72. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  73. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  74. return IB_MAD_RESULT_SUCCESS;
  75. /* Don't process SMInfo queries -- the SMA can't handle them.
  76. */
  77. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
  78. return IB_MAD_RESULT_SUCCESS;
  79. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  80. in_mad->mad_hdr.mgmt_class == MLX5_IB_VENDOR_CLASS1 ||
  81. in_mad->mad_hdr.mgmt_class == MLX5_IB_VENDOR_CLASS2 ||
  82. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  83. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  84. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  85. return IB_MAD_RESULT_SUCCESS;
  86. } else {
  87. return IB_MAD_RESULT_SUCCESS;
  88. }
  89. err = mlx5_MAD_IFC(to_mdev(ibdev),
  90. mad_flags & IB_MAD_IGNORE_MKEY,
  91. mad_flags & IB_MAD_IGNORE_BKEY,
  92. port_num, in_wc, in_grh, in_mad, out_mad);
  93. if (err)
  94. return IB_MAD_RESULT_FAILURE;
  95. /* set return bit in status of directed route responses */
  96. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  97. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  98. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  99. /* no response for trap repress */
  100. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  101. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  102. }
  103. int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port)
  104. {
  105. struct ib_smp *in_mad = NULL;
  106. struct ib_smp *out_mad = NULL;
  107. int err = -ENOMEM;
  108. u16 packet_error;
  109. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  110. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  111. if (!in_mad || !out_mad)
  112. goto out;
  113. init_query_mad(in_mad);
  114. in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
  115. in_mad->attr_mod = cpu_to_be32(port);
  116. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  117. packet_error = be16_to_cpu(out_mad->status);
  118. dev->mdev->port_caps[port - 1].ext_port_cap = (!err && !packet_error) ?
  119. MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO : 0;
  120. out:
  121. kfree(in_mad);
  122. kfree(out_mad);
  123. return err;
  124. }
  125. int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
  126. struct ib_smp *out_mad)
  127. {
  128. struct ib_smp *in_mad = NULL;
  129. int err = -ENOMEM;
  130. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  131. if (!in_mad)
  132. return -ENOMEM;
  133. init_query_mad(in_mad);
  134. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  135. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad,
  136. out_mad);
  137. kfree(in_mad);
  138. return err;
  139. }
  140. int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
  141. __be64 *sys_image_guid)
  142. {
  143. struct ib_smp *out_mad = NULL;
  144. int err = -ENOMEM;
  145. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  146. if (!out_mad)
  147. return -ENOMEM;
  148. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  149. if (err)
  150. goto out;
  151. memcpy(sys_image_guid, out_mad->data + 4, 8);
  152. out:
  153. kfree(out_mad);
  154. return err;
  155. }
  156. int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
  157. u16 *max_pkeys)
  158. {
  159. struct ib_smp *out_mad = NULL;
  160. int err = -ENOMEM;
  161. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  162. if (!out_mad)
  163. return -ENOMEM;
  164. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  165. if (err)
  166. goto out;
  167. *max_pkeys = be16_to_cpup((__be16 *)(out_mad->data + 28));
  168. out:
  169. kfree(out_mad);
  170. return err;
  171. }
  172. int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
  173. u32 *vendor_id)
  174. {
  175. struct ib_smp *out_mad = NULL;
  176. int err = -ENOMEM;
  177. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  178. if (!out_mad)
  179. return -ENOMEM;
  180. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  181. if (err)
  182. goto out;
  183. *vendor_id = be32_to_cpup((__be32 *)(out_mad->data + 36)) & 0xffff;
  184. out:
  185. kfree(out_mad);
  186. return err;
  187. }
  188. int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
  189. {
  190. struct ib_smp *in_mad = NULL;
  191. struct ib_smp *out_mad = NULL;
  192. int err = -ENOMEM;
  193. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  194. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  195. if (!in_mad || !out_mad)
  196. goto out;
  197. init_query_mad(in_mad);
  198. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  199. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  200. if (err)
  201. goto out;
  202. memcpy(node_desc, out_mad->data, 64);
  203. out:
  204. kfree(in_mad);
  205. kfree(out_mad);
  206. return err;
  207. }
  208. int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid)
  209. {
  210. struct ib_smp *in_mad = NULL;
  211. struct ib_smp *out_mad = NULL;
  212. int err = -ENOMEM;
  213. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  214. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  215. if (!in_mad || !out_mad)
  216. goto out;
  217. init_query_mad(in_mad);
  218. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  219. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  220. if (err)
  221. goto out;
  222. memcpy(node_guid, out_mad->data + 12, 8);
  223. out:
  224. kfree(in_mad);
  225. kfree(out_mad);
  226. return err;
  227. }
  228. int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u8 port, u16 index,
  229. u16 *pkey)
  230. {
  231. struct ib_smp *in_mad = NULL;
  232. struct ib_smp *out_mad = NULL;
  233. int err = -ENOMEM;
  234. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  235. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  236. if (!in_mad || !out_mad)
  237. goto out;
  238. init_query_mad(in_mad);
  239. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  240. in_mad->attr_mod = cpu_to_be32(index / 32);
  241. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  242. out_mad);
  243. if (err)
  244. goto out;
  245. *pkey = be16_to_cpu(((__be16 *)out_mad->data)[index % 32]);
  246. out:
  247. kfree(in_mad);
  248. kfree(out_mad);
  249. return err;
  250. }
  251. int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u8 port, int index,
  252. union ib_gid *gid)
  253. {
  254. struct ib_smp *in_mad = NULL;
  255. struct ib_smp *out_mad = NULL;
  256. int err = -ENOMEM;
  257. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  258. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  259. if (!in_mad || !out_mad)
  260. goto out;
  261. init_query_mad(in_mad);
  262. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  263. in_mad->attr_mod = cpu_to_be32(port);
  264. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  265. out_mad);
  266. if (err)
  267. goto out;
  268. memcpy(gid->raw, out_mad->data + 8, 8);
  269. init_query_mad(in_mad);
  270. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  271. in_mad->attr_mod = cpu_to_be32(index / 8);
  272. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  273. out_mad);
  274. if (err)
  275. goto out;
  276. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  277. out:
  278. kfree(in_mad);
  279. kfree(out_mad);
  280. return err;
  281. }
  282. int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u8 port,
  283. struct ib_port_attr *props)
  284. {
  285. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  286. struct mlx5_core_dev *mdev = dev->mdev;
  287. struct ib_smp *in_mad = NULL;
  288. struct ib_smp *out_mad = NULL;
  289. int ext_active_speed;
  290. int err = -ENOMEM;
  291. if (port < 1 || port > MLX5_CAP_GEN(mdev, num_ports)) {
  292. mlx5_ib_warn(dev, "invalid port number %d\n", port);
  293. return -EINVAL;
  294. }
  295. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  296. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  297. if (!in_mad || !out_mad)
  298. goto out;
  299. memset(props, 0, sizeof(*props));
  300. init_query_mad(in_mad);
  301. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  302. in_mad->attr_mod = cpu_to_be32(port);
  303. err = mlx5_MAD_IFC(dev, 1, 1, port, NULL, NULL, in_mad, out_mad);
  304. if (err) {
  305. mlx5_ib_warn(dev, "err %d\n", err);
  306. goto out;
  307. }
  308. props->lid = be16_to_cpup((__be16 *)(out_mad->data + 16));
  309. props->lmc = out_mad->data[34] & 0x7;
  310. props->sm_lid = be16_to_cpup((__be16 *)(out_mad->data + 18));
  311. props->sm_sl = out_mad->data[36] & 0xf;
  312. props->state = out_mad->data[32] & 0xf;
  313. props->phys_state = out_mad->data[33] >> 4;
  314. props->port_cap_flags = be32_to_cpup((__be32 *)(out_mad->data + 20));
  315. props->gid_tbl_len = out_mad->data[50];
  316. props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
  317. props->pkey_tbl_len = mdev->port_caps[port - 1].pkey_table_len;
  318. props->bad_pkey_cntr = be16_to_cpup((__be16 *)(out_mad->data + 46));
  319. props->qkey_viol_cntr = be16_to_cpup((__be16 *)(out_mad->data + 48));
  320. props->active_width = out_mad->data[31] & 0xf;
  321. props->active_speed = out_mad->data[35] >> 4;
  322. props->max_mtu = out_mad->data[41] & 0xf;
  323. props->active_mtu = out_mad->data[36] >> 4;
  324. props->subnet_timeout = out_mad->data[51] & 0x1f;
  325. props->max_vl_num = out_mad->data[37] >> 4;
  326. props->init_type_reply = out_mad->data[41] >> 4;
  327. /* Check if extended speeds (EDR/FDR/...) are supported */
  328. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  329. ext_active_speed = out_mad->data[62] >> 4;
  330. switch (ext_active_speed) {
  331. case 1:
  332. props->active_speed = 16; /* FDR */
  333. break;
  334. case 2:
  335. props->active_speed = 32; /* EDR */
  336. break;
  337. }
  338. }
  339. /* If reported active speed is QDR, check if is FDR-10 */
  340. if (props->active_speed == 4) {
  341. if (mdev->port_caps[port - 1].ext_port_cap &
  342. MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
  343. init_query_mad(in_mad);
  344. in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
  345. in_mad->attr_mod = cpu_to_be32(port);
  346. err = mlx5_MAD_IFC(dev, 1, 1, port,
  347. NULL, NULL, in_mad, out_mad);
  348. if (err)
  349. goto out;
  350. /* Checking LinkSpeedActive for FDR-10 */
  351. if (out_mad->data[15] & 0x1)
  352. props->active_speed = 8;
  353. }
  354. }
  355. out:
  356. kfree(in_mad);
  357. kfree(out_mad);
  358. return err;
  359. }