2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mlx5/cmd.h>
34 #include <linux/mlx5/vport.h>
35 #include <rdma/ib_mad.h>
36 #include <rdma/ib_smi.h>
37 #include <rdma/ib_pma.h>
42 MLX5_IB_VENDOR_CLASS1
= 0x9,
43 MLX5_IB_VENDOR_CLASS2
= 0xa
46 static bool can_do_mad_ifc(struct mlx5_ib_dev
*dev
, u8 port_num
,
47 struct ib_mad
*in_mad
)
49 if (in_mad
->mad_hdr
.mgmt_class
!= IB_MGMT_CLASS_SUBN_LID_ROUTED
&&
50 in_mad
->mad_hdr
.mgmt_class
!= IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
52 return dev
->mdev
->port_caps
[port_num
- 1].has_smi
;
55 static int mlx5_MAD_IFC(struct mlx5_ib_dev
*dev
, int ignore_mkey
,
56 int ignore_bkey
, u8 port
, const struct ib_wc
*in_wc
,
57 const struct ib_grh
*in_grh
, const void *in_mad
,
62 if (!can_do_mad_ifc(dev
, port
, (struct ib_mad
*)in_mad
))
65 /* Key check traps can't be generated unless we have in_wc to
66 * tell us where to send the trap.
68 if (ignore_mkey
|| !in_wc
)
70 if (ignore_bkey
|| !in_wc
)
73 return mlx5_cmd_mad_ifc(dev
->mdev
, in_mad
, response_mad
, op_modifier
,
77 static void pma_cnt_ext_assign(struct ib_pma_portcounters_ext
*pma_cnt_ext
,
80 #define MLX5_SUM_CNT(p, cntr1, cntr2) \
81 (MLX5_GET64(query_vport_counter_out, p, cntr1) + \
82 MLX5_GET64(query_vport_counter_out, p, cntr2))
84 pma_cnt_ext
->port_xmit_data
=
85 cpu_to_be64(MLX5_SUM_CNT(out
, transmitted_ib_unicast
.octets
,
86 transmitted_ib_multicast
.octets
) >> 2);
87 pma_cnt_ext
->port_rcv_data
=
88 cpu_to_be64(MLX5_SUM_CNT(out
, received_ib_unicast
.octets
,
89 received_ib_multicast
.octets
) >> 2);
90 pma_cnt_ext
->port_xmit_packets
=
91 cpu_to_be64(MLX5_SUM_CNT(out
, transmitted_ib_unicast
.packets
,
92 transmitted_ib_multicast
.packets
));
93 pma_cnt_ext
->port_rcv_packets
=
94 cpu_to_be64(MLX5_SUM_CNT(out
, received_ib_unicast
.packets
,
95 received_ib_multicast
.packets
));
96 pma_cnt_ext
->port_unicast_xmit_packets
=
97 MLX5_GET64_BE(query_vport_counter_out
,
98 out
, transmitted_ib_unicast
.packets
);
99 pma_cnt_ext
->port_unicast_rcv_packets
=
100 MLX5_GET64_BE(query_vport_counter_out
,
101 out
, received_ib_unicast
.packets
);
102 pma_cnt_ext
->port_multicast_xmit_packets
=
103 MLX5_GET64_BE(query_vport_counter_out
,
104 out
, transmitted_ib_multicast
.packets
);
105 pma_cnt_ext
->port_multicast_rcv_packets
=
106 MLX5_GET64_BE(query_vport_counter_out
,
107 out
, received_ib_multicast
.packets
);
110 static void pma_cnt_assign(struct ib_pma_portcounters
*pma_cnt
,
113 /* Traffic counters will be reported in
114 * their 64bit form via ib_pma_portcounters_ext by default.
116 void *out_pma
= MLX5_ADDR_OF(ppcnt_reg
, out
,
119 #define MLX5_ASSIGN_PMA_CNTR(counter_var, counter_name) { \
120 counter_var = MLX5_GET_BE(typeof(counter_var), \
121 ib_port_cntrs_grp_data_layout, \
122 out_pma, counter_name); \
125 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->symbol_error_counter
,
126 symbol_error_counter
);
127 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->link_error_recovery_counter
,
128 link_error_recovery_counter
);
129 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->link_downed_counter
,
130 link_downed_counter
);
131 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_rcv_errors
,
133 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_rcv_remphys_errors
,
134 port_rcv_remote_physical_errors
);
135 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_rcv_switch_relay_errors
,
136 port_rcv_switch_relay_errors
);
137 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_xmit_discards
,
139 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_xmit_constraint_errors
,
140 port_xmit_constraint_errors
);
141 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_xmit_wait
,
143 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->port_rcv_constraint_errors
,
144 port_rcv_constraint_errors
);
145 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->link_overrun_errors
,
146 link_overrun_errors
);
147 MLX5_ASSIGN_PMA_CNTR(pma_cnt
->vl15_dropped
,
151 static int process_pma_cmd(struct mlx5_ib_dev
*dev
, u8 port_num
,
152 const struct ib_mad
*in_mad
, struct ib_mad
*out_mad
)
154 struct mlx5_core_dev
*mdev
;
155 bool native_port
= true;
160 mdev
= mlx5_ib_get_native_port_mdev(dev
, port_num
, &mdev_port_num
);
162 /* Fail to get the native port, likely due to 2nd port is still
163 * unaffiliated. In such case default to 1st port and attached
170 /* Declaring support of extended counters */
171 if (in_mad
->mad_hdr
.attr_id
== IB_PMA_CLASS_PORT_INFO
) {
172 struct ib_class_port_info cpi
= {};
174 cpi
.capability_mask
= IB_PMA_CLASS_CAP_EXT_WIDTH
;
175 memcpy((out_mad
->data
+ 40), &cpi
, sizeof(cpi
));
176 err
= IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
;
180 if (in_mad
->mad_hdr
.attr_id
== IB_PMA_PORT_COUNTERS_EXT
) {
181 struct ib_pma_portcounters_ext
*pma_cnt_ext
=
182 (struct ib_pma_portcounters_ext
*)(out_mad
->data
+ 40);
183 int sz
= MLX5_ST_SZ_BYTES(query_vport_counter_out
);
185 out_cnt
= kvzalloc(sz
, GFP_KERNEL
);
187 err
= IB_MAD_RESULT_FAILURE
;
191 err
= mlx5_core_query_vport_counter(mdev
, 0, 0,
192 mdev_port_num
, out_cnt
, sz
);
194 pma_cnt_ext_assign(pma_cnt_ext
, out_cnt
);
196 struct ib_pma_portcounters
*pma_cnt
=
197 (struct ib_pma_portcounters
*)(out_mad
->data
+ 40);
198 int sz
= MLX5_ST_SZ_BYTES(ppcnt_reg
);
200 out_cnt
= kvzalloc(sz
, GFP_KERNEL
);
202 err
= IB_MAD_RESULT_FAILURE
;
206 err
= mlx5_core_query_ib_ppcnt(mdev
, mdev_port_num
,
209 pma_cnt_assign(pma_cnt
, out_cnt
);
212 err
= err
? IB_MAD_RESULT_FAILURE
:
213 IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
;
216 mlx5_ib_put_native_port_mdev(dev
, port_num
);
220 int mlx5_ib_process_mad(struct ib_device
*ibdev
, int mad_flags
, u8 port_num
,
221 const struct ib_wc
*in_wc
, const struct ib_grh
*in_grh
,
222 const struct ib_mad
*in
, struct ib_mad
*out
,
223 size_t *out_mad_size
, u16
*out_mad_pkey_index
)
225 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
226 u8 mgmt_class
= in
->mad_hdr
.mgmt_class
;
227 u8 method
= in
->mad_hdr
.method
;
231 slid
= in_wc
? ib_lid_cpu16(in_wc
->slid
) :
232 be16_to_cpu(IB_LID_PERMISSIVE
);
234 if (method
== IB_MGMT_METHOD_TRAP
&& !slid
)
235 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
;
237 switch (mgmt_class
) {
238 case IB_MGMT_CLASS_SUBN_LID_ROUTED
:
239 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
: {
240 if (method
!= IB_MGMT_METHOD_GET
&&
241 method
!= IB_MGMT_METHOD_SET
&&
242 method
!= IB_MGMT_METHOD_TRAP_REPRESS
)
243 return IB_MAD_RESULT_SUCCESS
;
245 /* Don't process SMInfo queries -- the SMA can't handle them.
247 if (in
->mad_hdr
.attr_id
== IB_SMP_ATTR_SM_INFO
)
248 return IB_MAD_RESULT_SUCCESS
;
250 case IB_MGMT_CLASS_PERF_MGMT
:
251 if (MLX5_CAP_GEN(dev
->mdev
, vport_counters
) &&
252 method
== IB_MGMT_METHOD_GET
)
253 return process_pma_cmd(dev
, port_num
, in
, out
);
255 case MLX5_IB_VENDOR_CLASS1
:
257 case MLX5_IB_VENDOR_CLASS2
:
258 case IB_MGMT_CLASS_CONG_MGMT
: {
259 if (method
!= IB_MGMT_METHOD_GET
&&
260 method
!= IB_MGMT_METHOD_SET
)
261 return IB_MAD_RESULT_SUCCESS
;
264 return IB_MAD_RESULT_SUCCESS
;
267 err
= mlx5_MAD_IFC(to_mdev(ibdev
), mad_flags
& IB_MAD_IGNORE_MKEY
,
268 mad_flags
& IB_MAD_IGNORE_BKEY
, port_num
, in_wc
,
271 return IB_MAD_RESULT_FAILURE
;
273 /* set return bit in status of directed route responses */
274 if (mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
275 out
->mad_hdr
.status
|= cpu_to_be16(1 << 15);
277 if (method
== IB_MGMT_METHOD_TRAP_REPRESS
)
278 /* no response for trap repress */
279 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
;
281 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
;
284 int mlx5_query_ext_port_caps(struct mlx5_ib_dev
*dev
, u8 port
)
286 struct ib_smp
*in_mad
= NULL
;
287 struct ib_smp
*out_mad
= NULL
;
291 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
292 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
293 if (!in_mad
|| !out_mad
)
296 init_query_mad(in_mad
);
297 in_mad
->attr_id
= MLX5_ATTR_EXTENDED_PORT_INFO
;
298 in_mad
->attr_mod
= cpu_to_be32(port
);
300 err
= mlx5_MAD_IFC(dev
, 1, 1, 1, NULL
, NULL
, in_mad
, out_mad
);
302 packet_error
= be16_to_cpu(out_mad
->status
);
304 dev
->mdev
->port_caps
[port
- 1].ext_port_cap
= (!err
&& !packet_error
) ?
305 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
: 0;
313 int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device
*ibdev
,
314 struct ib_smp
*out_mad
)
316 struct ib_smp
*in_mad
= NULL
;
319 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
323 init_query_mad(in_mad
);
324 in_mad
->attr_id
= IB_SMP_ATTR_NODE_INFO
;
326 err
= mlx5_MAD_IFC(to_mdev(ibdev
), 1, 1, 1, NULL
, NULL
, in_mad
,
333 int mlx5_query_mad_ifc_system_image_guid(struct ib_device
*ibdev
,
334 __be64
*sys_image_guid
)
336 struct ib_smp
*out_mad
= NULL
;
339 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
343 err
= mlx5_query_mad_ifc_smp_attr_node_info(ibdev
, out_mad
);
347 memcpy(sys_image_guid
, out_mad
->data
+ 4, 8);
355 int mlx5_query_mad_ifc_max_pkeys(struct ib_device
*ibdev
,
358 struct ib_smp
*out_mad
= NULL
;
361 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
365 err
= mlx5_query_mad_ifc_smp_attr_node_info(ibdev
, out_mad
);
369 *max_pkeys
= be16_to_cpup((__be16
*)(out_mad
->data
+ 28));
377 int mlx5_query_mad_ifc_vendor_id(struct ib_device
*ibdev
,
380 struct ib_smp
*out_mad
= NULL
;
383 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
387 err
= mlx5_query_mad_ifc_smp_attr_node_info(ibdev
, out_mad
);
391 *vendor_id
= be32_to_cpup((__be32
*)(out_mad
->data
+ 36)) & 0xffff;
399 int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev
*dev
, char *node_desc
)
401 struct ib_smp
*in_mad
= NULL
;
402 struct ib_smp
*out_mad
= NULL
;
405 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
406 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
407 if (!in_mad
|| !out_mad
)
410 init_query_mad(in_mad
);
411 in_mad
->attr_id
= IB_SMP_ATTR_NODE_DESC
;
413 err
= mlx5_MAD_IFC(dev
, 1, 1, 1, NULL
, NULL
, in_mad
, out_mad
);
417 memcpy(node_desc
, out_mad
->data
, IB_DEVICE_NODE_DESC_MAX
);
424 int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev
*dev
, __be64
*node_guid
)
426 struct ib_smp
*in_mad
= NULL
;
427 struct ib_smp
*out_mad
= NULL
;
430 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
431 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
432 if (!in_mad
|| !out_mad
)
435 init_query_mad(in_mad
);
436 in_mad
->attr_id
= IB_SMP_ATTR_NODE_INFO
;
438 err
= mlx5_MAD_IFC(dev
, 1, 1, 1, NULL
, NULL
, in_mad
, out_mad
);
442 memcpy(node_guid
, out_mad
->data
+ 12, 8);
449 int mlx5_query_mad_ifc_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
,
452 struct ib_smp
*in_mad
= NULL
;
453 struct ib_smp
*out_mad
= NULL
;
456 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
457 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
458 if (!in_mad
|| !out_mad
)
461 init_query_mad(in_mad
);
462 in_mad
->attr_id
= IB_SMP_ATTR_PKEY_TABLE
;
463 in_mad
->attr_mod
= cpu_to_be32(index
/ 32);
465 err
= mlx5_MAD_IFC(to_mdev(ibdev
), 1, 1, port
, NULL
, NULL
, in_mad
,
470 *pkey
= be16_to_cpu(((__be16
*)out_mad
->data
)[index
% 32]);
478 int mlx5_query_mad_ifc_gids(struct ib_device
*ibdev
, u8 port
, int index
,
481 struct ib_smp
*in_mad
= NULL
;
482 struct ib_smp
*out_mad
= NULL
;
485 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
486 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
487 if (!in_mad
|| !out_mad
)
490 init_query_mad(in_mad
);
491 in_mad
->attr_id
= IB_SMP_ATTR_PORT_INFO
;
492 in_mad
->attr_mod
= cpu_to_be32(port
);
494 err
= mlx5_MAD_IFC(to_mdev(ibdev
), 1, 1, port
, NULL
, NULL
, in_mad
,
499 memcpy(gid
->raw
, out_mad
->data
+ 8, 8);
501 init_query_mad(in_mad
);
502 in_mad
->attr_id
= IB_SMP_ATTR_GUID_INFO
;
503 in_mad
->attr_mod
= cpu_to_be32(index
/ 8);
505 err
= mlx5_MAD_IFC(to_mdev(ibdev
), 1, 1, port
, NULL
, NULL
, in_mad
,
510 memcpy(gid
->raw
+ 8, out_mad
->data
+ (index
% 8) * 8, 8);
518 int mlx5_query_mad_ifc_port(struct ib_device
*ibdev
, u8 port
,
519 struct ib_port_attr
*props
)
521 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
522 struct mlx5_core_dev
*mdev
= dev
->mdev
;
523 struct ib_smp
*in_mad
= NULL
;
524 struct ib_smp
*out_mad
= NULL
;
525 int ext_active_speed
;
528 in_mad
= kzalloc(sizeof(*in_mad
), GFP_KERNEL
);
529 out_mad
= kmalloc(sizeof(*out_mad
), GFP_KERNEL
);
530 if (!in_mad
|| !out_mad
)
533 /* props being zeroed by the caller, avoid zeroing it here */
535 init_query_mad(in_mad
);
536 in_mad
->attr_id
= IB_SMP_ATTR_PORT_INFO
;
537 in_mad
->attr_mod
= cpu_to_be32(port
);
539 err
= mlx5_MAD_IFC(dev
, 1, 1, port
, NULL
, NULL
, in_mad
, out_mad
);
541 mlx5_ib_warn(dev
, "err %d\n", err
);
545 props
->lid
= be16_to_cpup((__be16
*)(out_mad
->data
+ 16));
546 props
->lmc
= out_mad
->data
[34] & 0x7;
547 props
->sm_lid
= be16_to_cpup((__be16
*)(out_mad
->data
+ 18));
548 props
->sm_sl
= out_mad
->data
[36] & 0xf;
549 props
->state
= out_mad
->data
[32] & 0xf;
550 props
->phys_state
= out_mad
->data
[33] >> 4;
551 props
->port_cap_flags
= be32_to_cpup((__be32
*)(out_mad
->data
+ 20));
552 props
->gid_tbl_len
= out_mad
->data
[50];
553 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(mdev
, log_max_msg
);
554 props
->pkey_tbl_len
= mdev
->port_caps
[port
- 1].pkey_table_len
;
555 props
->bad_pkey_cntr
= be16_to_cpup((__be16
*)(out_mad
->data
+ 46));
556 props
->qkey_viol_cntr
= be16_to_cpup((__be16
*)(out_mad
->data
+ 48));
557 props
->active_width
= out_mad
->data
[31] & 0xf;
558 props
->active_speed
= out_mad
->data
[35] >> 4;
559 props
->max_mtu
= out_mad
->data
[41] & 0xf;
560 props
->active_mtu
= out_mad
->data
[36] >> 4;
561 props
->subnet_timeout
= out_mad
->data
[51] & 0x1f;
562 props
->max_vl_num
= out_mad
->data
[37] >> 4;
563 props
->init_type_reply
= out_mad
->data
[41] >> 4;
565 if (props
->port_cap_flags
& IB_PORT_CAP_MASK2_SUP
) {
566 props
->port_cap_flags2
=
567 be16_to_cpup((__be16
*)(out_mad
->data
+ 60));
569 if (props
->port_cap_flags2
& IB_PORT_LINK_WIDTH_2X_SUP
)
570 props
->active_width
= out_mad
->data
[31] & 0x1f;
573 /* Check if extended speeds (EDR/FDR/...) are supported */
574 if (props
->port_cap_flags
& IB_PORT_EXTENDED_SPEEDS_SUP
) {
575 ext_active_speed
= out_mad
->data
[62] >> 4;
577 switch (ext_active_speed
) {
579 props
->active_speed
= 16; /* FDR */
582 props
->active_speed
= 32; /* EDR */
585 if (props
->port_cap_flags
& IB_PORT_CAP_MASK2_SUP
&&
586 props
->port_cap_flags2
& IB_PORT_LINK_SPEED_HDR_SUP
)
587 props
->active_speed
= IB_SPEED_HDR
;
592 /* If reported active speed is QDR, check if is FDR-10 */
593 if (props
->active_speed
== 4) {
594 if (mdev
->port_caps
[port
- 1].ext_port_cap
&
595 MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
) {
596 init_query_mad(in_mad
);
597 in_mad
->attr_id
= MLX5_ATTR_EXTENDED_PORT_INFO
;
598 in_mad
->attr_mod
= cpu_to_be32(port
);
600 err
= mlx5_MAD_IFC(dev
, 1, 1, port
,
601 NULL
, NULL
, in_mad
, out_mad
);
605 /* Checking LinkSpeedActive for FDR-10 */
606 if (out_mad
->data
[15] & 0x1)
607 props
->active_speed
= 8;