mad.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 <linux/mlx5/vport.h>
  34. #include <rdma/ib_mad.h>
  35. #include <rdma/ib_smi.h>
  36. #include <rdma/ib_pma.h>
  37. #include "mlx5_ib.h"
  38. enum {
  39. MLX5_IB_VENDOR_CLASS1 = 0x9,
  40. MLX5_IB_VENDOR_CLASS2 = 0xa
  41. };
  42. static bool can_do_mad_ifc(struct mlx5_ib_dev *dev, u8 port_num,
  43. struct ib_mad *in_mad)
  44. {
  45. if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  46. in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  47. return true;
  48. return dev->mdev->port_caps[port_num - 1].has_smi;
  49. }
  50. int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  51. u8 port, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  52. const void *in_mad, void *response_mad)
  53. {
  54. u8 op_modifier = 0;
  55. if (!can_do_mad_ifc(dev, port, (struct ib_mad *)in_mad))
  56. return -EPERM;
  57. /* Key check traps can't be generated unless we have in_wc to
  58. * tell us where to send the trap.
  59. */
  60. if (ignore_mkey || !in_wc)
  61. op_modifier |= 0x1;
  62. if (ignore_bkey || !in_wc)
  63. op_modifier |= 0x2;
  64. return mlx5_core_mad_ifc(dev->mdev, in_mad, response_mad, op_modifier, port);
  65. }
  66. static int process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  67. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  68. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  69. {
  70. u16 slid;
  71. int err;
  72. slid = in_wc ? ib_lid_cpu16(in_wc->slid) : be16_to_cpu(IB_LID_PERMISSIVE);
  73. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0)
  74. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  75. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  76. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  77. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  78. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  79. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  80. return IB_MAD_RESULT_SUCCESS;
  81. /* Don't process SMInfo queries -- the SMA can't handle them.
  82. */
  83. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
  84. return IB_MAD_RESULT_SUCCESS;
  85. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  86. in_mad->mad_hdr.mgmt_class == MLX5_IB_VENDOR_CLASS1 ||
  87. in_mad->mad_hdr.mgmt_class == MLX5_IB_VENDOR_CLASS2 ||
  88. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  89. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  90. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  91. return IB_MAD_RESULT_SUCCESS;
  92. } else {
  93. return IB_MAD_RESULT_SUCCESS;
  94. }
  95. err = mlx5_MAD_IFC(to_mdev(ibdev),
  96. mad_flags & IB_MAD_IGNORE_MKEY,
  97. mad_flags & IB_MAD_IGNORE_BKEY,
  98. port_num, in_wc, in_grh, in_mad, out_mad);
  99. if (err)
  100. return IB_MAD_RESULT_FAILURE;
  101. /* set return bit in status of directed route responses */
  102. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  103. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  104. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  105. /* no response for trap repress */
  106. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  107. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  108. }
  109. static void pma_cnt_ext_assign(struct ib_pma_portcounters_ext *pma_cnt_ext,
  110. void *out)
  111. {
  112. #define MLX5_SUM_CNT(p, cntr1, cntr2) \
  113. (MLX5_GET64(query_vport_counter_out, p, cntr1) + \
  114. MLX5_GET64(query_vport_counter_out, p, cntr2))
  115. pma_cnt_ext->port_xmit_data =
  116. cpu_to_be64(MLX5_SUM_CNT(out, transmitted_ib_unicast.octets,
  117. transmitted_ib_multicast.octets) >> 2);
  118. pma_cnt_ext->port_rcv_data =
  119. cpu_to_be64(MLX5_SUM_CNT(out, received_ib_unicast.octets,
  120. received_ib_multicast.octets) >> 2);
  121. pma_cnt_ext->port_xmit_packets =
  122. cpu_to_be64(MLX5_SUM_CNT(out, transmitted_ib_unicast.packets,
  123. transmitted_ib_multicast.packets));
  124. pma_cnt_ext->port_rcv_packets =
  125. cpu_to_be64(MLX5_SUM_CNT(out, received_ib_unicast.packets,
  126. received_ib_multicast.packets));
  127. pma_cnt_ext->port_unicast_xmit_packets =
  128. MLX5_GET64_BE(query_vport_counter_out,
  129. out, transmitted_ib_unicast.packets);
  130. pma_cnt_ext->port_unicast_rcv_packets =
  131. MLX5_GET64_BE(query_vport_counter_out,
  132. out, received_ib_unicast.packets);
  133. pma_cnt_ext->port_multicast_xmit_packets =
  134. MLX5_GET64_BE(query_vport_counter_out,
  135. out, transmitted_ib_multicast.packets);
  136. pma_cnt_ext->port_multicast_rcv_packets =
  137. MLX5_GET64_BE(query_vport_counter_out,
  138. out, received_ib_multicast.packets);
  139. }
  140. static void pma_cnt_assign(struct ib_pma_portcounters *pma_cnt,
  141. void *out)
  142. {
  143. /* Traffic counters will be reported in
  144. * their 64bit form via ib_pma_portcounters_ext by default.
  145. */
  146. void *out_pma = MLX5_ADDR_OF(ppcnt_reg, out,
  147. counter_set);
  148. #define MLX5_ASSIGN_PMA_CNTR(counter_var, counter_name) { \
  149. counter_var = MLX5_GET_BE(typeof(counter_var), \
  150. ib_port_cntrs_grp_data_layout, \
  151. out_pma, counter_name); \
  152. }
  153. MLX5_ASSIGN_PMA_CNTR(pma_cnt->symbol_error_counter,
  154. symbol_error_counter);
  155. MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_error_recovery_counter,
  156. link_error_recovery_counter);
  157. MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_downed_counter,
  158. link_downed_counter);
  159. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_errors,
  160. port_rcv_errors);
  161. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_remphys_errors,
  162. port_rcv_remote_physical_errors);
  163. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_switch_relay_errors,
  164. port_rcv_switch_relay_errors);
  165. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_discards,
  166. port_xmit_discards);
  167. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_constraint_errors,
  168. port_xmit_constraint_errors);
  169. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_wait,
  170. port_xmit_wait);
  171. MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_constraint_errors,
  172. port_rcv_constraint_errors);
  173. MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_overrun_errors,
  174. link_overrun_errors);
  175. MLX5_ASSIGN_PMA_CNTR(pma_cnt->vl15_dropped,
  176. vl_15_dropped);
  177. }
  178. static int process_pma_cmd(struct ib_device *ibdev, u8 port_num,
  179. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  180. {
  181. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  182. int err;
  183. void *out_cnt;
  184. /* Declaring support of extended counters */
  185. if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO) {
  186. struct ib_class_port_info cpi = {};
  187. cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
  188. memcpy((out_mad->data + 40), &cpi, sizeof(cpi));
  189. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  190. }
  191. if (in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT) {
  192. struct ib_pma_portcounters_ext *pma_cnt_ext =
  193. (struct ib_pma_portcounters_ext *)(out_mad->data + 40);
  194. int sz = MLX5_ST_SZ_BYTES(query_vport_counter_out);
  195. out_cnt = kvzalloc(sz, GFP_KERNEL);
  196. if (!out_cnt)
  197. return IB_MAD_RESULT_FAILURE;
  198. err = mlx5_core_query_vport_counter(dev->mdev, 0, 0,
  199. port_num, out_cnt, sz);
  200. if (!err)
  201. pma_cnt_ext_assign(pma_cnt_ext, out_cnt);
  202. } else {
  203. struct ib_pma_portcounters *pma_cnt =
  204. (struct ib_pma_portcounters *)(out_mad->data + 40);
  205. int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
  206. out_cnt = kvzalloc(sz, GFP_KERNEL);
  207. if (!out_cnt)
  208. return IB_MAD_RESULT_FAILURE;
  209. err = mlx5_core_query_ib_ppcnt(dev->mdev, port_num,
  210. out_cnt, sz);
  211. if (!err)
  212. pma_cnt_assign(pma_cnt, out_cnt);
  213. }
  214. kvfree(out_cnt);
  215. if (err)
  216. return IB_MAD_RESULT_FAILURE;
  217. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  218. }
  219. int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  220. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  221. const struct ib_mad_hdr *in, size_t in_mad_size,
  222. struct ib_mad_hdr *out, size_t *out_mad_size,
  223. u16 *out_mad_pkey_index)
  224. {
  225. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  226. struct mlx5_core_dev *mdev = dev->mdev;
  227. const struct ib_mad *in_mad = (const struct ib_mad *)in;
  228. struct ib_mad *out_mad = (struct ib_mad *)out;
  229. if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
  230. *out_mad_size != sizeof(*out_mad)))
  231. return IB_MAD_RESULT_FAILURE;
  232. memset(out_mad->data, 0, sizeof(out_mad->data));
  233. if (MLX5_CAP_GEN(mdev, vport_counters) &&
  234. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT &&
  235. in_mad->mad_hdr.method == IB_MGMT_METHOD_GET) {
  236. return process_pma_cmd(ibdev, port_num, in_mad, out_mad);
  237. } else {
  238. return process_mad(ibdev, mad_flags, port_num, in_wc, in_grh,
  239. in_mad, out_mad);
  240. }
  241. }
  242. int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port)
  243. {
  244. struct ib_smp *in_mad = NULL;
  245. struct ib_smp *out_mad = NULL;
  246. int err = -ENOMEM;
  247. u16 packet_error;
  248. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  249. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  250. if (!in_mad || !out_mad)
  251. goto out;
  252. init_query_mad(in_mad);
  253. in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
  254. in_mad->attr_mod = cpu_to_be32(port);
  255. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  256. packet_error = be16_to_cpu(out_mad->status);
  257. dev->mdev->port_caps[port - 1].ext_port_cap = (!err && !packet_error) ?
  258. MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO : 0;
  259. out:
  260. kfree(in_mad);
  261. kfree(out_mad);
  262. return err;
  263. }
  264. int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
  265. struct ib_smp *out_mad)
  266. {
  267. struct ib_smp *in_mad = NULL;
  268. int err = -ENOMEM;
  269. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  270. if (!in_mad)
  271. return -ENOMEM;
  272. init_query_mad(in_mad);
  273. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  274. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad,
  275. out_mad);
  276. kfree(in_mad);
  277. return err;
  278. }
  279. int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
  280. __be64 *sys_image_guid)
  281. {
  282. struct ib_smp *out_mad = NULL;
  283. int err = -ENOMEM;
  284. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  285. if (!out_mad)
  286. return -ENOMEM;
  287. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  288. if (err)
  289. goto out;
  290. memcpy(sys_image_guid, out_mad->data + 4, 8);
  291. out:
  292. kfree(out_mad);
  293. return err;
  294. }
  295. int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
  296. u16 *max_pkeys)
  297. {
  298. struct ib_smp *out_mad = NULL;
  299. int err = -ENOMEM;
  300. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  301. if (!out_mad)
  302. return -ENOMEM;
  303. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  304. if (err)
  305. goto out;
  306. *max_pkeys = be16_to_cpup((__be16 *)(out_mad->data + 28));
  307. out:
  308. kfree(out_mad);
  309. return err;
  310. }
  311. int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
  312. u32 *vendor_id)
  313. {
  314. struct ib_smp *out_mad = NULL;
  315. int err = -ENOMEM;
  316. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  317. if (!out_mad)
  318. return -ENOMEM;
  319. err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
  320. if (err)
  321. goto out;
  322. *vendor_id = be32_to_cpup((__be32 *)(out_mad->data + 36)) & 0xffff;
  323. out:
  324. kfree(out_mad);
  325. return err;
  326. }
  327. int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
  328. {
  329. struct ib_smp *in_mad = NULL;
  330. struct ib_smp *out_mad = NULL;
  331. int err = -ENOMEM;
  332. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  333. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  334. if (!in_mad || !out_mad)
  335. goto out;
  336. init_query_mad(in_mad);
  337. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  338. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  339. if (err)
  340. goto out;
  341. memcpy(node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
  342. out:
  343. kfree(in_mad);
  344. kfree(out_mad);
  345. return err;
  346. }
  347. int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid)
  348. {
  349. struct ib_smp *in_mad = NULL;
  350. struct ib_smp *out_mad = NULL;
  351. int err = -ENOMEM;
  352. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  353. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  354. if (!in_mad || !out_mad)
  355. goto out;
  356. init_query_mad(in_mad);
  357. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  358. err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  359. if (err)
  360. goto out;
  361. memcpy(node_guid, out_mad->data + 12, 8);
  362. out:
  363. kfree(in_mad);
  364. kfree(out_mad);
  365. return err;
  366. }
  367. int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u8 port, u16 index,
  368. u16 *pkey)
  369. {
  370. struct ib_smp *in_mad = NULL;
  371. struct ib_smp *out_mad = NULL;
  372. int err = -ENOMEM;
  373. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  374. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  375. if (!in_mad || !out_mad)
  376. goto out;
  377. init_query_mad(in_mad);
  378. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  379. in_mad->attr_mod = cpu_to_be32(index / 32);
  380. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  381. out_mad);
  382. if (err)
  383. goto out;
  384. *pkey = be16_to_cpu(((__be16 *)out_mad->data)[index % 32]);
  385. out:
  386. kfree(in_mad);
  387. kfree(out_mad);
  388. return err;
  389. }
  390. int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u8 port, int index,
  391. union ib_gid *gid)
  392. {
  393. struct ib_smp *in_mad = NULL;
  394. struct ib_smp *out_mad = NULL;
  395. int err = -ENOMEM;
  396. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  397. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  398. if (!in_mad || !out_mad)
  399. goto out;
  400. init_query_mad(in_mad);
  401. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  402. in_mad->attr_mod = cpu_to_be32(port);
  403. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  404. out_mad);
  405. if (err)
  406. goto out;
  407. memcpy(gid->raw, out_mad->data + 8, 8);
  408. init_query_mad(in_mad);
  409. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  410. in_mad->attr_mod = cpu_to_be32(index / 8);
  411. err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
  412. out_mad);
  413. if (err)
  414. goto out;
  415. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  416. out:
  417. kfree(in_mad);
  418. kfree(out_mad);
  419. return err;
  420. }
  421. int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u8 port,
  422. struct ib_port_attr *props)
  423. {
  424. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  425. struct mlx5_core_dev *mdev = dev->mdev;
  426. struct ib_smp *in_mad = NULL;
  427. struct ib_smp *out_mad = NULL;
  428. int ext_active_speed;
  429. int err = -ENOMEM;
  430. if (port < 1 || port > MLX5_CAP_GEN(mdev, num_ports)) {
  431. mlx5_ib_warn(dev, "invalid port number %d\n", port);
  432. return -EINVAL;
  433. }
  434. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  435. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  436. if (!in_mad || !out_mad)
  437. goto out;
  438. /* props being zeroed by the caller, avoid zeroing it here */
  439. init_query_mad(in_mad);
  440. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  441. in_mad->attr_mod = cpu_to_be32(port);
  442. err = mlx5_MAD_IFC(dev, 1, 1, port, NULL, NULL, in_mad, out_mad);
  443. if (err) {
  444. mlx5_ib_warn(dev, "err %d\n", err);
  445. goto out;
  446. }
  447. props->lid = be16_to_cpup((__be16 *)(out_mad->data + 16));
  448. props->lmc = out_mad->data[34] & 0x7;
  449. props->sm_lid = be16_to_cpup((__be16 *)(out_mad->data + 18));
  450. props->sm_sl = out_mad->data[36] & 0xf;
  451. props->state = out_mad->data[32] & 0xf;
  452. props->phys_state = out_mad->data[33] >> 4;
  453. props->port_cap_flags = be32_to_cpup((__be32 *)(out_mad->data + 20));
  454. props->gid_tbl_len = out_mad->data[50];
  455. props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
  456. props->pkey_tbl_len = mdev->port_caps[port - 1].pkey_table_len;
  457. props->bad_pkey_cntr = be16_to_cpup((__be16 *)(out_mad->data + 46));
  458. props->qkey_viol_cntr = be16_to_cpup((__be16 *)(out_mad->data + 48));
  459. props->active_width = out_mad->data[31] & 0xf;
  460. props->active_speed = out_mad->data[35] >> 4;
  461. props->max_mtu = out_mad->data[41] & 0xf;
  462. props->active_mtu = out_mad->data[36] >> 4;
  463. props->subnet_timeout = out_mad->data[51] & 0x1f;
  464. props->max_vl_num = out_mad->data[37] >> 4;
  465. props->init_type_reply = out_mad->data[41] >> 4;
  466. /* Check if extended speeds (EDR/FDR/...) are supported */
  467. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  468. ext_active_speed = out_mad->data[62] >> 4;
  469. switch (ext_active_speed) {
  470. case 1:
  471. props->active_speed = 16; /* FDR */
  472. break;
  473. case 2:
  474. props->active_speed = 32; /* EDR */
  475. break;
  476. }
  477. }
  478. /* If reported active speed is QDR, check if is FDR-10 */
  479. if (props->active_speed == 4) {
  480. if (mdev->port_caps[port - 1].ext_port_cap &
  481. MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
  482. init_query_mad(in_mad);
  483. in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
  484. in_mad->attr_mod = cpu_to_be32(port);
  485. err = mlx5_MAD_IFC(dev, 1, 1, port,
  486. NULL, NULL, in_mad, out_mad);
  487. if (err)
  488. goto out;
  489. /* Checking LinkSpeedActive for FDR-10 */
  490. if (out_mad->data[15] & 0x1)
  491. props->active_speed = 8;
  492. }
  493. }
  494. out:
  495. kfree(in_mad);
  496. kfree(out_mad);
  497. return err;
  498. }