xref: /openbmc/linux/drivers/infiniband/hw/mlx5/mad.c (revision 910be4f9)
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 
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 #include "cmd.h"
39 
40 enum {
41 	MLX5_IB_VENDOR_CLASS1 = 0x9,
42 	MLX5_IB_VENDOR_CLASS2 = 0xa
43 };
44 
can_do_mad_ifc(struct mlx5_ib_dev * dev,u32 port_num,struct ib_mad * in_mad)45 static bool can_do_mad_ifc(struct mlx5_ib_dev *dev, u32 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->port_caps[port_num - 1].has_smi;
52 }
53 
mlx5_MAD_IFC(struct mlx5_ib_dev * dev,int ignore_mkey,int ignore_bkey,u32 port,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const void * in_mad,void * response_mad)54 static int mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey,
55 			int ignore_bkey, u32 port, const struct ib_wc *in_wc,
56 			const struct ib_grh *in_grh, const void *in_mad,
57 			void *response_mad)
58 {
59 	u8 op_modifier = 0;
60 
61 	if (!can_do_mad_ifc(dev, port, (struct ib_mad *)in_mad))
62 		return -EPERM;
63 
64 	/* 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 
72 	return mlx5_cmd_mad_ifc(dev->mdev, in_mad, response_mad, op_modifier,
73 				port);
74 }
75 
pma_cnt_ext_assign(struct ib_pma_portcounters_ext * pma_cnt_ext,void * out)76 static 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);
86 	pma_cnt_ext->port_rcv_data =
87 		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 
pma_cnt_assign(struct ib_pma_portcounters * pma_cnt,void * out)109 static 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);
140 	MLX5_ASSIGN_PMA_CNTR(pma_cnt->port_xmit_wait,
141 			     port_xmit_wait);
142 	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 
query_ib_ppcnt(struct mlx5_core_dev * dev,u8 port_num,void * out,size_t sz)150 static int query_ib_ppcnt(struct mlx5_core_dev *dev, u8 port_num, void *out,
151 			  size_t sz)
152 {
153 	u32 *in;
154 	int err;
155 
156 	in  = kvzalloc(sz, GFP_KERNEL);
157 	if (!in) {
158 		err = -ENOMEM;
159 		return err;
160 	}
161 
162 	MLX5_SET(ppcnt_reg, in, local_port, port_num);
163 
164 	MLX5_SET(ppcnt_reg, in, grp, MLX5_INFINIBAND_PORT_COUNTERS_GROUP);
165 	err = mlx5_core_access_reg(dev, in, sz, out,
166 				   sz, MLX5_REG_PPCNT, 0, 0);
167 
168 	kvfree(in);
169 	return err;
170 }
171 
process_pma_cmd(struct mlx5_ib_dev * dev,u32 port_num,const struct ib_mad * in_mad,struct ib_mad * out_mad)172 static int process_pma_cmd(struct mlx5_ib_dev *dev, u32 port_num,
173 			   const struct ib_mad *in_mad, struct ib_mad *out_mad)
174 {
175 	struct mlx5_core_dev *mdev;
176 	bool native_port = true;
177 	u32 mdev_port_num;
178 	void *out_cnt;
179 	int err;
180 
181 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
182 	if (!mdev) {
183 		/* Fail to get the native port, likely due to 2nd port is still
184 		 * unaffiliated. In such case default to 1st port and attached
185 		 * PF device.
186 		 */
187 		native_port = false;
188 		mdev = dev->mdev;
189 		mdev_port_num = 1;
190 	}
191 	if (MLX5_CAP_GEN(dev->mdev, num_ports) == 1 &&
192 	    !mlx5_core_mp_enabled(mdev)) {
193 		/* set local port to one for Function-Per-Port HCA. */
194 		mdev = dev->mdev;
195 		mdev_port_num = 1;
196 	}
197 
198 	/* Declaring support of extended counters */
199 	if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO) {
200 		struct ib_class_port_info cpi = {};
201 
202 		cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
203 		memcpy((out_mad->data + 40), &cpi, sizeof(cpi));
204 		err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
205 		goto done;
206 	}
207 
208 	if (in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT) {
209 		struct ib_pma_portcounters_ext *pma_cnt_ext =
210 			(struct ib_pma_portcounters_ext *)(out_mad->data + 40);
211 		int sz = MLX5_ST_SZ_BYTES(query_vport_counter_out);
212 
213 		out_cnt = kvzalloc(sz, GFP_KERNEL);
214 		if (!out_cnt) {
215 			err = IB_MAD_RESULT_FAILURE;
216 			goto done;
217 		}
218 
219 		err = mlx5_core_query_vport_counter(mdev, 0, 0, mdev_port_num,
220 						    out_cnt);
221 		if (!err)
222 			pma_cnt_ext_assign(pma_cnt_ext, out_cnt);
223 	} else {
224 		struct ib_pma_portcounters *pma_cnt =
225 			(struct ib_pma_portcounters *)(out_mad->data + 40);
226 		int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
227 
228 		out_cnt = kvzalloc(sz, GFP_KERNEL);
229 		if (!out_cnt) {
230 			err = IB_MAD_RESULT_FAILURE;
231 			goto done;
232 		}
233 
234 		err = query_ib_ppcnt(mdev, mdev_port_num, out_cnt, sz);
235 		if (!err)
236 			pma_cnt_assign(pma_cnt, out_cnt);
237 	}
238 	kvfree(out_cnt);
239 	err = err ? IB_MAD_RESULT_FAILURE :
240 		    IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
241 done:
242 	if (native_port)
243 		mlx5_ib_put_native_port_mdev(dev, port_num);
244 	return err;
245 }
246 
mlx5_ib_process_mad(struct ib_device * ibdev,int mad_flags,u32 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad * in,struct ib_mad * out,size_t * out_mad_size,u16 * out_mad_pkey_index)247 int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
248 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
249 			const struct ib_mad *in, struct ib_mad *out,
250 			size_t *out_mad_size, u16 *out_mad_pkey_index)
251 {
252 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
253 	u8 mgmt_class = in->mad_hdr.mgmt_class;
254 	u8 method = in->mad_hdr.method;
255 	u16 slid;
256 	int err;
257 
258 	slid = in_wc ? ib_lid_cpu16(in_wc->slid) :
259 		       be16_to_cpu(IB_LID_PERMISSIVE);
260 
261 	if (method == IB_MGMT_METHOD_TRAP && !slid)
262 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
263 
264 	switch (mgmt_class) {
265 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
266 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE: {
267 		if (method != IB_MGMT_METHOD_GET &&
268 		    method != IB_MGMT_METHOD_SET &&
269 		    method != IB_MGMT_METHOD_TRAP_REPRESS)
270 			return IB_MAD_RESULT_SUCCESS;
271 
272 		/* Don't process SMInfo queries -- the SMA can't handle them.
273 		 */
274 		if (in->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
275 			return IB_MAD_RESULT_SUCCESS;
276 	} break;
277 	case IB_MGMT_CLASS_PERF_MGMT:
278 		if (MLX5_CAP_GEN(dev->mdev, vport_counters) &&
279 		    method == IB_MGMT_METHOD_GET)
280 			return process_pma_cmd(dev, port_num, in, out);
281 		fallthrough;
282 	case MLX5_IB_VENDOR_CLASS1:
283 	case MLX5_IB_VENDOR_CLASS2:
284 	case IB_MGMT_CLASS_CONG_MGMT: {
285 		if (method != IB_MGMT_METHOD_GET &&
286 		    method != IB_MGMT_METHOD_SET)
287 			return IB_MAD_RESULT_SUCCESS;
288 	} break;
289 	default:
290 		return IB_MAD_RESULT_SUCCESS;
291 	}
292 
293 	err = mlx5_MAD_IFC(to_mdev(ibdev), mad_flags & IB_MAD_IGNORE_MKEY,
294 			   mad_flags & IB_MAD_IGNORE_BKEY, port_num, in_wc,
295 			   in_grh, in, out);
296 	if (err)
297 		return IB_MAD_RESULT_FAILURE;
298 
299 	/* set return bit in status of directed route responses */
300 	if (mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
301 		out->mad_hdr.status |= cpu_to_be16(1 << 15);
302 
303 	if (method == IB_MGMT_METHOD_TRAP_REPRESS)
304 		/* no response for trap repress */
305 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
306 
307 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
308 }
309 
mlx5_query_ext_port_caps(struct mlx5_ib_dev * dev,unsigned int port)310 int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, unsigned int port)
311 {
312 	struct ib_smp *in_mad;
313 	struct ib_smp *out_mad;
314 	int err = -ENOMEM;
315 	u16 packet_error;
316 
317 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
318 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
319 	if (!in_mad || !out_mad)
320 		goto out;
321 
322 	ib_init_query_mad(in_mad);
323 	in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
324 	in_mad->attr_mod = cpu_to_be32(port);
325 
326 	err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
327 
328 	packet_error = be16_to_cpu(out_mad->status);
329 
330 	dev->port_caps[port - 1].ext_port_cap = (!err && !packet_error) ?
331 		MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO : 0;
332 
333 out:
334 	kfree(in_mad);
335 	kfree(out_mad);
336 	return err;
337 }
338 
mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device * ibdev,struct ib_smp * out_mad)339 static int mlx5_query_mad_ifc_smp_attr_node_info(struct ib_device *ibdev,
340 						 struct ib_smp *out_mad)
341 {
342 	struct ib_smp *in_mad;
343 	int err;
344 
345 	in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
346 	if (!in_mad)
347 		return -ENOMEM;
348 
349 	ib_init_query_mad(in_mad);
350 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
351 
352 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad,
353 			   out_mad);
354 
355 	kfree(in_mad);
356 	return err;
357 }
358 
mlx5_query_mad_ifc_system_image_guid(struct ib_device * ibdev,__be64 * sys_image_guid)359 int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
360 					 __be64 *sys_image_guid)
361 {
362 	struct ib_smp *out_mad;
363 	int err;
364 
365 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
366 	if (!out_mad)
367 		return -ENOMEM;
368 
369 	err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
370 	if (err)
371 		goto out;
372 
373 	memcpy(sys_image_guid, out_mad->data + 4, 8);
374 
375 out:
376 	kfree(out_mad);
377 
378 	return err;
379 }
380 
mlx5_query_mad_ifc_max_pkeys(struct ib_device * ibdev,u16 * max_pkeys)381 int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
382 				 u16 *max_pkeys)
383 {
384 	struct ib_smp *out_mad;
385 	int err;
386 
387 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
388 	if (!out_mad)
389 		return -ENOMEM;
390 
391 	err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
392 	if (err)
393 		goto out;
394 
395 	*max_pkeys = be16_to_cpup((__be16 *)(out_mad->data + 28));
396 
397 out:
398 	kfree(out_mad);
399 
400 	return err;
401 }
402 
mlx5_query_mad_ifc_vendor_id(struct ib_device * ibdev,u32 * vendor_id)403 int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
404 				 u32 *vendor_id)
405 {
406 	struct ib_smp *out_mad;
407 	int err;
408 
409 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
410 	if (!out_mad)
411 		return -ENOMEM;
412 
413 	err = mlx5_query_mad_ifc_smp_attr_node_info(ibdev, out_mad);
414 	if (err)
415 		goto out;
416 
417 	*vendor_id = be32_to_cpup((__be32 *)(out_mad->data + 36)) & 0xffff;
418 
419 out:
420 	kfree(out_mad);
421 
422 	return err;
423 }
424 
mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev * dev,char * node_desc)425 int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
426 {
427 	struct ib_smp *in_mad;
428 	struct ib_smp *out_mad;
429 	int err = -ENOMEM;
430 
431 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
432 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
433 	if (!in_mad || !out_mad)
434 		goto out;
435 
436 	ib_init_query_mad(in_mad);
437 	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
438 
439 	err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
440 	if (err)
441 		goto out;
442 
443 	memcpy(node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
444 out:
445 	kfree(in_mad);
446 	kfree(out_mad);
447 	return err;
448 }
449 
mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev * dev,__be64 * node_guid)450 int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid)
451 {
452 	struct ib_smp *in_mad;
453 	struct ib_smp *out_mad;
454 	int err = -ENOMEM;
455 
456 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
457 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
458 	if (!in_mad || !out_mad)
459 		goto out;
460 
461 	ib_init_query_mad(in_mad);
462 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
463 
464 	err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
465 	if (err)
466 		goto out;
467 
468 	memcpy(node_guid, out_mad->data + 12, 8);
469 out:
470 	kfree(in_mad);
471 	kfree(out_mad);
472 	return err;
473 }
474 
mlx5_query_mad_ifc_pkey(struct ib_device * ibdev,u32 port,u16 index,u16 * pkey)475 int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u32 port, u16 index,
476 			    u16 *pkey)
477 {
478 	struct ib_smp *in_mad;
479 	struct ib_smp *out_mad;
480 	int err = -ENOMEM;
481 
482 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
483 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
484 	if (!in_mad || !out_mad)
485 		goto out;
486 
487 	ib_init_query_mad(in_mad);
488 	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
489 	in_mad->attr_mod = cpu_to_be32(index / 32);
490 
491 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
492 			   out_mad);
493 	if (err)
494 		goto out;
495 
496 	*pkey = be16_to_cpu(((__be16 *)out_mad->data)[index % 32]);
497 
498 out:
499 	kfree(in_mad);
500 	kfree(out_mad);
501 	return err;
502 }
503 
mlx5_query_mad_ifc_gids(struct ib_device * ibdev,u32 port,int index,union ib_gid * gid)504 int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u32 port, int index,
505 			    union ib_gid *gid)
506 {
507 	struct ib_smp *in_mad;
508 	struct ib_smp *out_mad;
509 	int err = -ENOMEM;
510 
511 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
512 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
513 	if (!in_mad || !out_mad)
514 		goto out;
515 
516 	ib_init_query_mad(in_mad);
517 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
518 	in_mad->attr_mod = cpu_to_be32(port);
519 
520 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
521 			   out_mad);
522 	if (err)
523 		goto out;
524 
525 	memcpy(gid->raw, out_mad->data + 8, 8);
526 
527 	ib_init_query_mad(in_mad);
528 	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
529 	in_mad->attr_mod = cpu_to_be32(index / 8);
530 
531 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad,
532 			   out_mad);
533 	if (err)
534 		goto out;
535 
536 	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
537 
538 out:
539 	kfree(in_mad);
540 	kfree(out_mad);
541 	return err;
542 }
543 
mlx5_query_mad_ifc_port(struct ib_device * ibdev,u32 port,struct ib_port_attr * props)544 int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u32 port,
545 			    struct ib_port_attr *props)
546 {
547 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
548 	struct mlx5_core_dev *mdev = dev->mdev;
549 	struct ib_smp *in_mad;
550 	struct ib_smp *out_mad;
551 	int ext_active_speed;
552 	int err = -ENOMEM;
553 
554 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
555 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
556 	if (!in_mad || !out_mad)
557 		goto out;
558 
559 	/* props being zeroed by the caller, avoid zeroing it here */
560 
561 	ib_init_query_mad(in_mad);
562 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
563 	in_mad->attr_mod = cpu_to_be32(port);
564 
565 	err = mlx5_MAD_IFC(dev, 1, 1, port, NULL, NULL, in_mad, out_mad);
566 	if (err) {
567 		mlx5_ib_warn(dev, "err %d\n", err);
568 		goto out;
569 	}
570 
571 	props->lid		= be16_to_cpup((__be16 *)(out_mad->data + 16));
572 	props->lmc		= out_mad->data[34] & 0x7;
573 	props->sm_lid		= be16_to_cpup((__be16 *)(out_mad->data + 18));
574 	props->sm_sl		= out_mad->data[36] & 0xf;
575 	props->state		= out_mad->data[32] & 0xf;
576 	props->phys_state	= out_mad->data[33] >> 4;
577 	props->port_cap_flags	= be32_to_cpup((__be32 *)(out_mad->data + 20));
578 	props->gid_tbl_len	= out_mad->data[50];
579 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
580 	props->pkey_tbl_len	= dev->pkey_table_len;
581 	props->bad_pkey_cntr	= be16_to_cpup((__be16 *)(out_mad->data + 46));
582 	props->qkey_viol_cntr	= be16_to_cpup((__be16 *)(out_mad->data + 48));
583 	props->active_width	= out_mad->data[31] & 0xf;
584 	props->active_speed	= out_mad->data[35] >> 4;
585 	props->max_mtu		= out_mad->data[41] & 0xf;
586 	props->active_mtu	= out_mad->data[36] >> 4;
587 	props->subnet_timeout	= out_mad->data[51] & 0x1f;
588 	props->max_vl_num	= out_mad->data[37] >> 4;
589 	props->init_type_reply	= out_mad->data[41] >> 4;
590 
591 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) {
592 		props->port_cap_flags2 =
593 			be16_to_cpup((__be16 *)(out_mad->data + 60));
594 
595 		if (props->port_cap_flags2 & IB_PORT_LINK_WIDTH_2X_SUP)
596 			props->active_width = out_mad->data[31] & 0x1f;
597 	}
598 
599 	/* Check if extended speeds (EDR/FDR/...) are supported */
600 	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
601 		ext_active_speed = out_mad->data[62] >> 4;
602 
603 		switch (ext_active_speed) {
604 		case 1:
605 			props->active_speed = 16; /* FDR */
606 			break;
607 		case 2:
608 			props->active_speed = 32; /* EDR */
609 			break;
610 		case 4:
611 			if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP &&
612 			    props->port_cap_flags2 & IB_PORT_LINK_SPEED_HDR_SUP)
613 				props->active_speed = IB_SPEED_HDR;
614 			break;
615 		case 8:
616 			if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP &&
617 			    props->port_cap_flags2 & IB_PORT_LINK_SPEED_NDR_SUP)
618 				props->active_speed = IB_SPEED_NDR;
619 			break;
620 		}
621 	}
622 
623 	/* If reported active speed is QDR, check if is FDR-10 */
624 	if (props->active_speed == 4) {
625 		if (dev->port_caps[port - 1].ext_port_cap &
626 		    MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
627 			ib_init_query_mad(in_mad);
628 			in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
629 			in_mad->attr_mod = cpu_to_be32(port);
630 
631 			err = mlx5_MAD_IFC(dev, 1, 1, port,
632 					   NULL, NULL, in_mad, out_mad);
633 			if (err)
634 				goto out;
635 
636 			/* Checking LinkSpeedActive for FDR-10 */
637 			if (out_mad->data[15] & 0x1)
638 				props->active_speed = 8;
639 		}
640 	}
641 
642 out:
643 	kfree(in_mad);
644 	kfree(out_mad);
645 
646 	return err;
647 }
648