blob: 454ce5de2de7174437ba31b9733a19770367a075 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
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
Meny Yossefi3efd9a12016-02-18 18:15:01 +020033#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030034#include <rdma/ib_mad.h>
35#include <rdma/ib_smi.h>
Meny Yossefi3efd9a12016-02-18 18:15:01 +020036#include <rdma/ib_pma.h>
Eli Cohene126ba92013-07-07 17:25:49 +030037#include "mlx5_ib.h"
Leon Romanovsky73f5a822019-01-13 15:57:04 +020038#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030039
40enum {
41 MLX5_IB_VENDOR_CLASS1 = 0x9,
42 MLX5_IB_VENDOR_CLASS2 = 0xa
43};
44
Maor Gottliebc43f1112017-01-18 14:10:33 +020045static bool can_do_mad_ifc(struct mlx5_ib_dev *dev, u8 port_num,
46 struct ib_mad *in_mad)
47{
48 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED &&
49 in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
50 return true;
51 return dev->mdev->port_caps[port_num - 1].has_smi;
52}
53
Leon Romanovsky73f5a822019-01-13 15:57:04 +020054static int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey,
55 int ignore_bkey, u8 port, const struct ib_wc *in_wc,
56 const struct ib_grh *in_grh, const void *in_mad,
57 void *response_mad)
Eli Cohene126ba92013-07-07 17:25:49 +030058{
59 u8 op_modifier = 0;
60
Maor Gottliebc43f1112017-01-18 14:10:33 +020061 if (!can_do_mad_ifc(dev, port, (struct ib_mad *)in_mad))
62 return -EPERM;
63
Eli Cohene126ba92013-07-07 17:25:49 +030064 /* Key check traps can't be generated unless we have in_wc to
65 * tell us where to send the trap.
66 */
67 if (ignore_mkey || !in_wc)
68 op_modifier |= 0x1;
69 if (ignore_bkey || !in_wc)
70 op_modifier |= 0x2;
71
Leon Romanovsky73f5a822019-01-13 15:57:04 +020072 return mlx5_cmd_mad_ifc(dev->mdev, in_mad, response_mad, op_modifier,
73 port);
Eli Cohene126ba92013-07-07 17:25:49 +030074}
75
Meny Yossefi3efd9a12016-02-18 18:15:01 +020076static void pma_cnt_ext_assign(struct ib_pma_portcounters_ext *pma_cnt_ext,
77 void *out)
78{
79#define MLX5_SUM_CNT(p, cntr1, cntr2) \
80 (MLX5_GET64(query_vport_counter_out, p, cntr1) + \
81 MLX5_GET64(query_vport_counter_out, p, cntr2))
82
83 pma_cnt_ext->port_xmit_data =
84 cpu_to_be64(MLX5_SUM_CNT(out, transmitted_ib_unicast.octets,
85 transmitted_ib_multicast.octets) >> 2);
Talat Batheesh00bf5342016-06-22 17:27:27 +030086 pma_cnt_ext->port_rcv_data =
Meny Yossefi3efd9a12016-02-18 18:15:01 +020087 cpu_to_be64(MLX5_SUM_CNT(out, received_ib_unicast.octets,
88 received_ib_multicast.octets) >> 2);
89 pma_cnt_ext->port_xmit_packets =
90 cpu_to_be64(MLX5_SUM_CNT(out, transmitted_ib_unicast.packets,
91 transmitted_ib_multicast.packets));
92 pma_cnt_ext->port_rcv_packets =
93 cpu_to_be64(MLX5_SUM_CNT(out, received_ib_unicast.packets,
94 received_ib_multicast.packets));
95 pma_cnt_ext->port_unicast_xmit_packets =
96 MLX5_GET64_BE(query_vport_counter_out,
97 out, transmitted_ib_unicast.packets);
98 pma_cnt_ext->port_unicast_rcv_packets =
99 MLX5_GET64_BE(query_vport_counter_out,
100 out, received_ib_unicast.packets);
101 pma_cnt_ext->port_multicast_xmit_packets =
102 MLX5_GET64_BE(query_vport_counter_out,
103 out, transmitted_ib_multicast.packets);
104 pma_cnt_ext->port_multicast_rcv_packets =
105 MLX5_GET64_BE(query_vport_counter_out,
106 out, received_ib_multicast.packets);
107}
108
109static void pma_cnt_assign(struct ib_pma_portcounters *pma_cnt,
110 void *out)
111{
112 /* Traffic counters will be reported in
113 * their 64bit form via ib_pma_portcounters_ext by default.
114 */
115 void *out_pma = MLX5_ADDR_OF(ppcnt_reg, out,
116 counter_set);
117
118#define MLX5_ASSIGN_PMA_CNTR(counter_var, counter_name) { \
119 counter_var = MLX5_GET_BE(typeof(counter_var), \
120 ib_port_cntrs_grp_data_layout, \
121 out_pma, counter_name); \
122 }
123
124 MLX5_ASSIGN_PMA_CNTR(pma_cnt->symbol_error_counter,
125 symbol_error_counter);
126 MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_error_recovery_counter,
127 link_error_recovery_counter);
128 MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_downed_counter,
129 link_downed_counter);
130 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_errors,
131 port_rcv_errors);
132 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_remphys_errors,
133 port_rcv_remote_physical_errors);
134 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_switch_relay_errors,
135 port_rcv_switch_relay_errors);
136 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_discards,
137 port_xmit_discards);
138 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_constraint_errors,
139 port_xmit_constraint_errors);
Tim Wright133bea02017-05-01 17:30:08 +0100140 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_wait,
141 port_xmit_wait);
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200142 MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_rcv_constraint_errors,
143 port_rcv_constraint_errors);
144 MLX5_ASSIGN_PMA_CNTR(pma_cnt->link_overrun_errors,
145 link_overrun_errors);
146 MLX5_ASSIGN_PMA_CNTR(pma_cnt->vl15_dropped,
147 vl_15_dropped);
148}
149
Parav Pandit2f40cf32019-06-30 10:52:52 +0300150static int process_pma_cmd(struct mlx5_ib_dev *dev, u8 port_num,
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200151 const struct ib_mad *in_mad, struct ib_mad *out_mad)
152{
Parav Pandit2f40cf32019-06-30 10:52:52 +0300153 struct mlx5_core_dev *mdev;
154 bool native_port = true;
155 u8 mdev_port_num;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200156 void *out_cnt;
Parav Pandit2f40cf32019-06-30 10:52:52 +0300157 int err;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200158
Parav Pandit2f40cf32019-06-30 10:52:52 +0300159 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
160 if (!mdev) {
161 /* Fail to get the native port, likely due to 2nd port is still
162 * unaffiliated. In such case default to 1st port and attached
163 * PF device.
164 */
165 native_port = false;
166 mdev = dev->mdev;
167 mdev_port_num = 1;
168 }
Talat Batheesh4edf8d52017-08-17 15:50:44 +0300169 /* Declaring support of extended counters */
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200170 if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO) {
171 struct ib_class_port_info cpi = {};
172
173 cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
174 memcpy((out_mad->data + 40), &cpi, sizeof(cpi));
Parav Pandit2f40cf32019-06-30 10:52:52 +0300175 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
176 goto done;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200177 }
178
179 if (in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT) {
180 struct ib_pma_portcounters_ext *pma_cnt_ext =
181 (struct ib_pma_portcounters_ext *)(out_mad->data + 40);
182 int sz = MLX5_ST_SZ_BYTES(query_vport_counter_out);
183
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300184 out_cnt = kvzalloc(sz, GFP_KERNEL);
Parav Pandit2f40cf32019-06-30 10:52:52 +0300185 if (!out_cnt) {
186 err = IB_MAD_RESULT_FAILURE;
187 goto done;
188 }
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200189
Leon Romanovsky5d1c9a12020-04-07 18:59:51 +0300190 err = mlx5_core_query_vport_counter(mdev, 0, 0, mdev_port_num,
191 out_cnt);
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200192 if (!err)
193 pma_cnt_ext_assign(pma_cnt_ext, out_cnt);
194 } else {
195 struct ib_pma_portcounters *pma_cnt =
196 (struct ib_pma_portcounters *)(out_mad->data + 40);
197 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
198
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300199 out_cnt = kvzalloc(sz, GFP_KERNEL);
Parav Pandit2f40cf32019-06-30 10:52:52 +0300200 if (!out_cnt) {
201 err = IB_MAD_RESULT_FAILURE;
202 goto done;
203 }
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200204
Parav Pandit2f40cf32019-06-30 10:52:52 +0300205 err = mlx5_core_query_ib_ppcnt(mdev, mdev_port_num,
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200206 out_cnt, sz);
207 if (!err)
208 pma_cnt_assign(pma_cnt, out_cnt);
Parav Pandit2f40cf32019-06-30 10:52:52 +0300209 }
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200210 kvfree(out_cnt);
Parav Pandit2f40cf32019-06-30 10:52:52 +0300211 err = err ? IB_MAD_RESULT_FAILURE :
212 IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
213done:
214 if (native_port)
215 mlx5_ib_put_native_port_mdev(dev, port_num);
216 return err;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200217}
218
219int 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,
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200221 const struct ib_mad *in, struct ib_mad *out,
222 size_t *out_mad_size, u16 *out_mad_pkey_index)
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200223{
224 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200225 u8 mgmt_class = in->mad_hdr.mgmt_class;
226 u8 method = in->mad_hdr.method;
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200227 u16 slid;
228 int err;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200229
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200230 slid = in_wc ? ib_lid_cpu16(in_wc->slid) :
231 be16_to_cpu(IB_LID_PERMISSIVE);
232
233 if (method == IB_MGMT_METHOD_TRAP && !slid)
234 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
235
236 switch (mgmt_class) {
237 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
238 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE: {
239 if (method != IB_MGMT_METHOD_GET &&
240 method != IB_MGMT_METHOD_SET &&
241 method != IB_MGMT_METHOD_TRAP_REPRESS)
242 return IB_MAD_RESULT_SUCCESS;
243
244 /* Don't process SMInfo queries -- the SMA can't handle them.
245 */
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200246 if (in->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200247 return IB_MAD_RESULT_SUCCESS;
248 } break;
249 case IB_MGMT_CLASS_PERF_MGMT:
250 if (MLX5_CAP_GEN(dev->mdev, vport_counters) &&
251 method == IB_MGMT_METHOD_GET)
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200252 return process_pma_cmd(dev, port_num, in, out);
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200253 /* fallthrough */
254 case MLX5_IB_VENDOR_CLASS1:
255 /* fallthrough */
256 case MLX5_IB_VENDOR_CLASS2:
257 case IB_MGMT_CLASS_CONG_MGMT: {
258 if (method != IB_MGMT_METHOD_GET &&
259 method != IB_MGMT_METHOD_SET)
260 return IB_MAD_RESULT_SUCCESS;
261 } break;
262 default:
263 return IB_MAD_RESULT_SUCCESS;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200264 }
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200265
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200266 err = mlx5_MAD_IFC(to_mdev(ibdev), mad_flags & IB_MAD_IGNORE_MKEY,
267 mad_flags & IB_MAD_IGNORE_BKEY, port_num, in_wc,
268 in_grh, in, out);
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200269 if (err)
270 return IB_MAD_RESULT_FAILURE;
271
272 /* set return bit in status of directed route responses */
273 if (mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
Leon Romanovskye26e7b82019-10-29 08:27:45 +0200274 out->mad_hdr.status |= cpu_to_be16(1 << 15);
Leon Romanovskyffa2fd12019-10-29 08:27:42 +0200275
276 if (method == IB_MGMT_METHOD_TRAP_REPRESS)
277 /* no response for trap repress */
278 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
279
280 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
Meny Yossefi3efd9a12016-02-18 18:15:01 +0200281}
282
Eli Cohene126ba92013-07-07 17:25:49 +0300283int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port)
284{
285 struct ib_smp *in_mad = NULL;
286 struct ib_smp *out_mad = NULL;
287 int err = -ENOMEM;
288 u16 packet_error;
289
290 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
291 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
292 if (!in_mad || !out_mad)
293 goto out;
294
295 init_query_mad(in_mad);
296 in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
297 in_mad->attr_mod = cpu_to_be32(port);
298
299 err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
300
301 packet_error = be16_to_cpu(out_mad->status);
302
Saeed Mahameed938fe832015-05-28 22:28:41 +0300303 dev->mdev->port_caps[port - 1].ext_port_cap = (!err && !packet_error) ?
Eli Cohene126ba92013-07-07 17:25:49 +0300304 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO : 0;
305
306out:
307 kfree(in_mad);
308 kfree(out_mad);
309 return err;
310}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300311
312int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
313 struct ib_smp *out_mad)
314{
315 struct ib_smp *in_mad = NULL;
316 int err = -ENOMEM;
317
318 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
319 if (!in_mad)
320 return -ENOMEM;
321
322 init_query_mad(in_mad);
323 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
324
325 err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad,
326 out_mad);
327
328 kfree(in_mad);
329 return err;
330}
331
332int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
333 __be64 *sys_image_guid)
334{
335 struct ib_smp *out_mad = NULL;
336 int err = -ENOMEM;
337
338 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
339 if (!out_mad)
340 return -ENOMEM;
341
342 err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
343 if (err)
344 goto out;
345
346 memcpy(sys_image_guid, out_mad->data + 4, 8);
347
348out:
349 kfree(out_mad);
350
351 return err;
352}
353
354int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
355 u16 *max_pkeys)
356{
357 struct ib_smp *out_mad = NULL;
358 int err = -ENOMEM;
359
360 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
361 if (!out_mad)
362 return -ENOMEM;
363
364 err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
365 if (err)
366 goto out;
367
368 *max_pkeys = be16_to_cpup((__be16 *)(out_mad->data + 28));
369
370out:
371 kfree(out_mad);
372
373 return err;
374}
375
376int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
377 u32 *vendor_id)
378{
379 struct ib_smp *out_mad = NULL;
380 int err = -ENOMEM;
381
382 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
383 if (!out_mad)
384 return -ENOMEM;
385
386 err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
387 if (err)
388 goto out;
389
390 *vendor_id = be32_to_cpup((__be32 *)(out_mad->data + 36)) & 0xffff;
391
392out:
393 kfree(out_mad);
394
395 return err;
396}
397
398int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
399{
400 struct ib_smp *in_mad = NULL;
401 struct ib_smp *out_mad = NULL;
402 int err = -ENOMEM;
403
404 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
405 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
406 if (!in_mad || !out_mad)
407 goto out;
408
409 init_query_mad(in_mad);
410 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
411
412 err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
413 if (err)
414 goto out;
415
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700416 memcpy(node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300417out:
418 kfree(in_mad);
419 kfree(out_mad);
420 return err;
421}
422
423int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid)
424{
425 struct ib_smp *in_mad = NULL;
426 struct ib_smp *out_mad = NULL;
427 int err = -ENOMEM;
428
429 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
430 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
431 if (!in_mad || !out_mad)
432 goto out;
433
434 init_query_mad(in_mad);
435 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
436
437 err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
438 if (err)
439 goto out;
440
441 memcpy(node_guid, out_mad->data + 12, 8);
442out:
443 kfree(in_mad);
444 kfree(out_mad);
445 return err;
446}
447
448int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u8 port, u16 index,
449 u16 *pkey)
450{
451 struct ib_smp *in_mad = NULL;
452 struct ib_smp *out_mad = NULL;
453 int err = -ENOMEM;
454
455 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
456 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
457 if (!in_mad || !out_mad)
458 goto out;
459
460 init_query_mad(in_mad);
461 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
462 in_mad->attr_mod = cpu_to_be32(index / 32);
463
464 err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
465 out_mad);
466 if (err)
467 goto out;
468
469 *pkey = be16_to_cpu(((__be16 *)out_mad->data)[index % 32]);
470
471out:
472 kfree(in_mad);
473 kfree(out_mad);
474 return err;
475}
476
477int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u8 port, int index,
478 union ib_gid *gid)
479{
480 struct ib_smp *in_mad = NULL;
481 struct ib_smp *out_mad = NULL;
482 int err = -ENOMEM;
483
484 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
485 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
486 if (!in_mad || !out_mad)
487 goto out;
488
489 init_query_mad(in_mad);
490 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
491 in_mad->attr_mod = cpu_to_be32(port);
492
493 err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
494 out_mad);
495 if (err)
496 goto out;
497
498 memcpy(gid->raw, out_mad->data + 8, 8);
499
500 init_query_mad(in_mad);
501 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
502 in_mad->attr_mod = cpu_to_be32(index / 8);
503
504 err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
505 out_mad);
506 if (err)
507 goto out;
508
509 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
510
511out:
512 kfree(in_mad);
513 kfree(out_mad);
514 return err;
515}
516
517int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u8 port,
518 struct ib_port_attr *props)
519{
520 struct mlx5_ib_dev *dev = to_mdev(ibdev);
521 struct mlx5_core_dev *mdev = dev->mdev;
522 struct ib_smp *in_mad = NULL;
523 struct ib_smp *out_mad = NULL;
524 int ext_active_speed;
525 int err = -ENOMEM;
526
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300527 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
528 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
529 if (!in_mad || !out_mad)
530 goto out;
531
Or Gerlitzc4550c62017-01-24 13:02:39 +0200532 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300533
534 init_query_mad(in_mad);
535 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
536 in_mad->attr_mod = cpu_to_be32(port);
537
538 err = mlx5_MAD_IFC(dev, 1, 1, port, NULL, NULL, in_mad, out_mad);
539 if (err) {
540 mlx5_ib_warn(dev, "err %d\n", err);
541 goto out;
542 }
543
544 props->lid = be16_to_cpup((__be16 *)(out_mad->data + 16));
545 props->lmc = out_mad->data[34] & 0x7;
546 props->sm_lid = be16_to_cpup((__be16 *)(out_mad->data + 18));
547 props->sm_sl = out_mad->data[36] & 0xf;
548 props->state = out_mad->data[32] & 0xf;
549 props->phys_state = out_mad->data[33] >> 4;
550 props->port_cap_flags = be32_to_cpup((__be32 *)(out_mad->data + 20));
551 props->gid_tbl_len = out_mad->data[50];
552 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
553 props->pkey_tbl_len = mdev->port_caps[port - 1].pkey_table_len;
554 props->bad_pkey_cntr = be16_to_cpup((__be16 *)(out_mad->data + 46));
555 props->qkey_viol_cntr = be16_to_cpup((__be16 *)(out_mad->data + 48));
556 props->active_width = out_mad->data[31] & 0xf;
557 props->active_speed = out_mad->data[35] >> 4;
558 props->max_mtu = out_mad->data[41] & 0xf;
559 props->active_mtu = out_mad->data[36] >> 4;
560 props->subnet_timeout = out_mad->data[51] & 0x1f;
561 props->max_vl_num = out_mad->data[37] >> 4;
562 props->init_type_reply = out_mad->data[41] >> 4;
563
Michael Guralnikd7649702018-12-09 11:49:54 +0200564 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) {
Michael Guralnik4106a752018-12-09 11:49:51 +0200565 props->port_cap_flags2 =
566 be16_to_cpup((__be16 *)(out_mad->data + 60));
567
Michael Guralnikd7649702018-12-09 11:49:54 +0200568 if (props->port_cap_flags2 & IB_PORT_LINK_WIDTH_2X_SUP)
569 props->active_width = out_mad->data[31] & 0x1f;
570 }
571
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300572 /* Check if extended speeds (EDR/FDR/...) are supported */
573 if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
574 ext_active_speed = out_mad->data[62] >> 4;
575
576 switch (ext_active_speed) {
577 case 1:
578 props->active_speed = 16; /* FDR */
579 break;
580 case 2:
581 props->active_speed = 32; /* EDR */
582 break;
Michael Guralnikb8741552018-12-09 11:49:52 +0200583 case 4:
584 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP &&
585 props->port_cap_flags2 & IB_PORT_LINK_SPEED_HDR_SUP)
586 props->active_speed = IB_SPEED_HDR;
587 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300588 }
589 }
590
591 /* If reported active speed is QDR, check if is FDR-10 */
592 if (props->active_speed == 4) {
593 if (mdev->port_caps[port - 1].ext_port_cap &
594 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
595 init_query_mad(in_mad);
596 in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
597 in_mad->attr_mod = cpu_to_be32(port);
598
599 err = mlx5_MAD_IFC(dev, 1, 1, port,
600 NULL, NULL, in_mad, out_mad);
601 if (err)
602 goto out;
603
604 /* Checking LinkSpeedActive for FDR-10 */
605 if (out_mad->data[15] & 0x1)
606 props->active_speed = 8;
607 }
608 }
609
610out:
611 kfree(in_mad);
612 kfree(out_mad);
613
614 return err;
615}