xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlx5/core/vport.c (revision af9b2ff010f593d81e2f5fb04155e9fc25b9dfd0)
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  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/export.h>
34 #include <linux/etherdevice.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/eswitch.h>
38 #include "mlx5_core.h"
39 #include "sf/sf.h"
40 
41 /* Mutex to hold while enabling or disabling RoCE */
42 static DEFINE_MUTEX(mlx5_roce_en_lock);
43 
mlx5_query_vport_state(struct mlx5_core_dev * mdev,u8 opmod,u16 vport)44 u8 mlx5_query_vport_state(struct mlx5_core_dev *mdev, u8 opmod, u16 vport)
45 {
46 	u32 out[MLX5_ST_SZ_DW(query_vport_state_out)] = {};
47 	u32 in[MLX5_ST_SZ_DW(query_vport_state_in)] = {};
48 	int err;
49 
50 	MLX5_SET(query_vport_state_in, in, opcode,
51 		 MLX5_CMD_OP_QUERY_VPORT_STATE);
52 	MLX5_SET(query_vport_state_in, in, op_mod, opmod);
53 	MLX5_SET(query_vport_state_in, in, vport_number, vport);
54 	if (vport)
55 		MLX5_SET(query_vport_state_in, in, other_vport, 1);
56 
57 	err = mlx5_cmd_exec_inout(mdev, query_vport_state, in, out);
58 	if (err)
59 		return 0;
60 
61 	return MLX5_GET(query_vport_state_out, out, state);
62 }
63 
mlx5_modify_vport_admin_state(struct mlx5_core_dev * mdev,u8 opmod,u16 vport,u8 other_vport,u8 state)64 int mlx5_modify_vport_admin_state(struct mlx5_core_dev *mdev, u8 opmod,
65 				  u16 vport, u8 other_vport, u8 state)
66 {
67 	u32 in[MLX5_ST_SZ_DW(modify_vport_state_in)] = {};
68 
69 	MLX5_SET(modify_vport_state_in, in, opcode,
70 		 MLX5_CMD_OP_MODIFY_VPORT_STATE);
71 	MLX5_SET(modify_vport_state_in, in, op_mod, opmod);
72 	MLX5_SET(modify_vport_state_in, in, vport_number, vport);
73 	MLX5_SET(modify_vport_state_in, in, other_vport, other_vport);
74 	MLX5_SET(modify_vport_state_in, in, admin_state, state);
75 
76 	return mlx5_cmd_exec_in(mdev, modify_vport_state, in);
77 }
78 
mlx5_query_nic_vport_context(struct mlx5_core_dev * mdev,u16 vport,u32 * out)79 static int mlx5_query_nic_vport_context(struct mlx5_core_dev *mdev, u16 vport,
80 					u32 *out)
81 {
82 	u32 in[MLX5_ST_SZ_DW(query_nic_vport_context_in)] = {};
83 
84 	MLX5_SET(query_nic_vport_context_in, in, opcode,
85 		 MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT);
86 	MLX5_SET(query_nic_vport_context_in, in, vport_number, vport);
87 	if (vport)
88 		MLX5_SET(query_nic_vport_context_in, in, other_vport, 1);
89 
90 	return mlx5_cmd_exec_inout(mdev, query_nic_vport_context, in, out);
91 }
92 
mlx5_query_nic_vport_min_inline(struct mlx5_core_dev * mdev,u16 vport,u8 * min_inline)93 int mlx5_query_nic_vport_min_inline(struct mlx5_core_dev *mdev,
94 				    u16 vport, u8 *min_inline)
95 {
96 	u32 out[MLX5_ST_SZ_DW(query_nic_vport_context_out)] = {};
97 	int err;
98 
99 	err = mlx5_query_nic_vport_context(mdev, vport, out);
100 	if (!err)
101 		*min_inline = MLX5_GET(query_nic_vport_context_out, out,
102 				       nic_vport_context.min_wqe_inline_mode);
103 	return err;
104 }
105 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_min_inline);
106 
mlx5_query_min_inline(struct mlx5_core_dev * mdev,u8 * min_inline_mode)107 void mlx5_query_min_inline(struct mlx5_core_dev *mdev,
108 			   u8 *min_inline_mode)
109 {
110 	switch (MLX5_CAP_ETH(mdev, wqe_inline_mode)) {
111 	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
112 		if (!mlx5_query_nic_vport_min_inline(mdev, 0, min_inline_mode))
113 			break;
114 		fallthrough;
115 	case MLX5_CAP_INLINE_MODE_L2:
116 		*min_inline_mode = MLX5_INLINE_MODE_L2;
117 		break;
118 	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
119 		*min_inline_mode = MLX5_INLINE_MODE_NONE;
120 		break;
121 	}
122 }
123 EXPORT_SYMBOL_GPL(mlx5_query_min_inline);
124 
mlx5_modify_nic_vport_min_inline(struct mlx5_core_dev * mdev,u16 vport,u8 min_inline)125 int mlx5_modify_nic_vport_min_inline(struct mlx5_core_dev *mdev,
126 				     u16 vport, u8 min_inline)
127 {
128 	u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
129 	void *nic_vport_ctx;
130 
131 	MLX5_SET(modify_nic_vport_context_in, in,
132 		 field_select.min_inline, 1);
133 	MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
134 	MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
135 
136 	nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
137 				     in, nic_vport_context);
138 	MLX5_SET(nic_vport_context, nic_vport_ctx,
139 		 min_wqe_inline_mode, min_inline);
140 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
141 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
142 
143 	return mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
144 }
145 
mlx5_query_nic_vport_mac_address(struct mlx5_core_dev * mdev,u16 vport,bool other,u8 * addr)146 int mlx5_query_nic_vport_mac_address(struct mlx5_core_dev *mdev,
147 				     u16 vport, bool other, u8 *addr)
148 {
149 	u32 out[MLX5_ST_SZ_DW(query_nic_vport_context_out)] = {};
150 	u32 in[MLX5_ST_SZ_DW(query_nic_vport_context_in)] = {};
151 	u8 *out_addr;
152 	int err;
153 
154 	out_addr = MLX5_ADDR_OF(query_nic_vport_context_out, out,
155 				nic_vport_context.permanent_address);
156 
157 	MLX5_SET(query_nic_vport_context_in, in, opcode,
158 		 MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT);
159 	MLX5_SET(query_nic_vport_context_in, in, vport_number, vport);
160 	MLX5_SET(query_nic_vport_context_in, in, other_vport, other);
161 
162 	err = mlx5_cmd_exec_inout(mdev, query_nic_vport_context, in, out);
163 	if (!err)
164 		ether_addr_copy(addr, &out_addr[2]);
165 
166 	return err;
167 }
168 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_mac_address);
169 
mlx5_query_mac_address(struct mlx5_core_dev * mdev,u8 * addr)170 int mlx5_query_mac_address(struct mlx5_core_dev *mdev, u8 *addr)
171 {
172 	return mlx5_query_nic_vport_mac_address(mdev, 0, false, addr);
173 }
174 EXPORT_SYMBOL_GPL(mlx5_query_mac_address);
175 
mlx5_modify_nic_vport_mac_address(struct mlx5_core_dev * mdev,u16 vport,const u8 * addr)176 int mlx5_modify_nic_vport_mac_address(struct mlx5_core_dev *mdev,
177 				      u16 vport, const u8 *addr)
178 {
179 	void *in;
180 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
181 	int err;
182 	void *nic_vport_ctx;
183 	u8 *perm_mac;
184 
185 	in = kvzalloc(inlen, GFP_KERNEL);
186 	if (!in)
187 		return -ENOMEM;
188 
189 	MLX5_SET(modify_nic_vport_context_in, in,
190 		 field_select.permanent_address, 1);
191 	MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
192 	MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
193 
194 	nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
195 				     in, nic_vport_context);
196 	perm_mac = MLX5_ADDR_OF(nic_vport_context, nic_vport_ctx,
197 				permanent_address);
198 
199 	ether_addr_copy(&perm_mac[2], addr);
200 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
201 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
202 
203 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
204 
205 	kvfree(in);
206 
207 	return err;
208 }
209 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_mac_address);
210 
mlx5_query_nic_vport_mtu(struct mlx5_core_dev * mdev,u16 * mtu)211 int mlx5_query_nic_vport_mtu(struct mlx5_core_dev *mdev, u16 *mtu)
212 {
213 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
214 	u32 *out;
215 	int err;
216 
217 	out = kvzalloc(outlen, GFP_KERNEL);
218 	if (!out)
219 		return -ENOMEM;
220 
221 	err = mlx5_query_nic_vport_context(mdev, 0, out);
222 	if (!err)
223 		*mtu = MLX5_GET(query_nic_vport_context_out, out,
224 				nic_vport_context.mtu);
225 
226 	kvfree(out);
227 	return err;
228 }
229 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_mtu);
230 
mlx5_modify_nic_vport_mtu(struct mlx5_core_dev * mdev,u16 mtu)231 int mlx5_modify_nic_vport_mtu(struct mlx5_core_dev *mdev, u16 mtu)
232 {
233 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
234 	void *in;
235 	int err;
236 
237 	in = kvzalloc(inlen, GFP_KERNEL);
238 	if (!in)
239 		return -ENOMEM;
240 
241 	MLX5_SET(modify_nic_vport_context_in, in, field_select.mtu, 1);
242 	MLX5_SET(modify_nic_vport_context_in, in, nic_vport_context.mtu, mtu);
243 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
244 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
245 
246 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
247 
248 	kvfree(in);
249 	return err;
250 }
251 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_mtu);
252 
mlx5_query_nic_vport_mac_list(struct mlx5_core_dev * dev,u16 vport,enum mlx5_list_type list_type,u8 addr_list[][ETH_ALEN],int * list_size)253 int mlx5_query_nic_vport_mac_list(struct mlx5_core_dev *dev,
254 				  u16 vport,
255 				  enum mlx5_list_type list_type,
256 				  u8 addr_list[][ETH_ALEN],
257 				  int *list_size)
258 {
259 	u32 in[MLX5_ST_SZ_DW(query_nic_vport_context_in)] = {0};
260 	void *nic_vport_ctx;
261 	int max_list_size;
262 	int req_list_size;
263 	int out_sz;
264 	void *out;
265 	int err;
266 	int i;
267 
268 	req_list_size = *list_size;
269 
270 	max_list_size = list_type == MLX5_NVPRT_LIST_TYPE_UC ?
271 		1 << MLX5_CAP_GEN(dev, log_max_current_uc_list) :
272 		1 << MLX5_CAP_GEN(dev, log_max_current_mc_list);
273 
274 	if (req_list_size > max_list_size) {
275 		mlx5_core_warn(dev, "Requested list size (%d) > (%d) max_list_size\n",
276 			       req_list_size, max_list_size);
277 		req_list_size = max_list_size;
278 	}
279 
280 	out_sz = MLX5_ST_SZ_BYTES(query_nic_vport_context_out) +
281 			req_list_size * MLX5_ST_SZ_BYTES(mac_address_layout);
282 
283 	out = kvzalloc(out_sz, GFP_KERNEL);
284 	if (!out)
285 		return -ENOMEM;
286 
287 	MLX5_SET(query_nic_vport_context_in, in, opcode,
288 		 MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT);
289 	MLX5_SET(query_nic_vport_context_in, in, allowed_list_type, list_type);
290 	MLX5_SET(query_nic_vport_context_in, in, vport_number, vport);
291 	if (vport || mlx5_core_is_ecpf(dev))
292 		MLX5_SET(query_nic_vport_context_in, in, other_vport, 1);
293 
294 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
295 	if (err)
296 		goto out;
297 
298 	nic_vport_ctx = MLX5_ADDR_OF(query_nic_vport_context_out, out,
299 				     nic_vport_context);
300 	req_list_size = MLX5_GET(nic_vport_context, nic_vport_ctx,
301 				 allowed_list_size);
302 
303 	*list_size = req_list_size;
304 	for (i = 0; i < req_list_size; i++) {
305 		u8 *mac_addr = MLX5_ADDR_OF(nic_vport_context,
306 					nic_vport_ctx,
307 					current_uc_mac_address[i]) + 2;
308 		ether_addr_copy(addr_list[i], mac_addr);
309 	}
310 out:
311 	kvfree(out);
312 	return err;
313 }
314 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_mac_list);
315 
mlx5_modify_nic_vport_mac_list(struct mlx5_core_dev * dev,enum mlx5_list_type list_type,u8 addr_list[][ETH_ALEN],int list_size)316 int mlx5_modify_nic_vport_mac_list(struct mlx5_core_dev *dev,
317 				   enum mlx5_list_type list_type,
318 				   u8 addr_list[][ETH_ALEN],
319 				   int list_size)
320 {
321 	u32 out[MLX5_ST_SZ_DW(modify_nic_vport_context_out)] = {};
322 	void *nic_vport_ctx;
323 	int max_list_size;
324 	int in_sz;
325 	void *in;
326 	int err;
327 	int i;
328 
329 	max_list_size = list_type == MLX5_NVPRT_LIST_TYPE_UC ?
330 		 1 << MLX5_CAP_GEN(dev, log_max_current_uc_list) :
331 		 1 << MLX5_CAP_GEN(dev, log_max_current_mc_list);
332 
333 	if (list_size > max_list_size)
334 		return -ENOSPC;
335 
336 	in_sz = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in) +
337 		list_size * MLX5_ST_SZ_BYTES(mac_address_layout);
338 
339 	in = kvzalloc(in_sz, GFP_KERNEL);
340 	if (!in)
341 		return -ENOMEM;
342 
343 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
344 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
345 	MLX5_SET(modify_nic_vport_context_in, in,
346 		 field_select.addresses_list, 1);
347 
348 	nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in, in,
349 				     nic_vport_context);
350 
351 	MLX5_SET(nic_vport_context, nic_vport_ctx,
352 		 allowed_list_type, list_type);
353 	MLX5_SET(nic_vport_context, nic_vport_ctx,
354 		 allowed_list_size, list_size);
355 
356 	for (i = 0; i < list_size; i++) {
357 		u8 *curr_mac = MLX5_ADDR_OF(nic_vport_context,
358 					    nic_vport_ctx,
359 					    current_uc_mac_address[i]) + 2;
360 		ether_addr_copy(curr_mac, addr_list[i]);
361 	}
362 
363 	err = mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
364 	kvfree(in);
365 	return err;
366 }
367 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_mac_list);
368 
mlx5_modify_nic_vport_vlans(struct mlx5_core_dev * dev,u16 vlans[],int list_size)369 int mlx5_modify_nic_vport_vlans(struct mlx5_core_dev *dev,
370 				u16 vlans[],
371 				int list_size)
372 {
373 	u32 out[MLX5_ST_SZ_DW(modify_nic_vport_context_out)];
374 	void *nic_vport_ctx;
375 	int max_list_size;
376 	int in_sz;
377 	void *in;
378 	int err;
379 	int i;
380 
381 	max_list_size = 1 << MLX5_CAP_GEN(dev, log_max_vlan_list);
382 
383 	if (list_size > max_list_size)
384 		return -ENOSPC;
385 
386 	in_sz = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in) +
387 		list_size * MLX5_ST_SZ_BYTES(vlan_layout);
388 
389 	memset(out, 0, sizeof(out));
390 	in = kvzalloc(in_sz, GFP_KERNEL);
391 	if (!in)
392 		return -ENOMEM;
393 
394 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
395 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
396 	MLX5_SET(modify_nic_vport_context_in, in,
397 		 field_select.addresses_list, 1);
398 
399 	nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in, in,
400 				     nic_vport_context);
401 
402 	MLX5_SET(nic_vport_context, nic_vport_ctx,
403 		 allowed_list_type, MLX5_NVPRT_LIST_TYPE_VLAN);
404 	MLX5_SET(nic_vport_context, nic_vport_ctx,
405 		 allowed_list_size, list_size);
406 
407 	for (i = 0; i < list_size; i++) {
408 		void *vlan_addr = MLX5_ADDR_OF(nic_vport_context,
409 					       nic_vport_ctx,
410 					       current_uc_mac_address[i]);
411 		MLX5_SET(vlan_layout, vlan_addr, vlan, vlans[i]);
412 	}
413 
414 	err = mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
415 	kvfree(in);
416 	return err;
417 }
418 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_vlans);
419 
mlx5_query_nic_vport_system_image_guid(struct mlx5_core_dev * mdev,u64 * system_image_guid)420 int mlx5_query_nic_vport_system_image_guid(struct mlx5_core_dev *mdev,
421 					   u64 *system_image_guid)
422 {
423 	u32 *out;
424 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
425 	int err;
426 
427 	out = kvzalloc(outlen, GFP_KERNEL);
428 	if (!out)
429 		return -ENOMEM;
430 
431 	err = mlx5_query_nic_vport_context(mdev, 0, out);
432 	if (err)
433 		goto out;
434 
435 	*system_image_guid = MLX5_GET64(query_nic_vport_context_out, out,
436 					nic_vport_context.system_image_guid);
437 out:
438 	kvfree(out);
439 	return err;
440 }
441 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_system_image_guid);
442 
mlx5_query_nic_vport_node_guid(struct mlx5_core_dev * mdev,u64 * node_guid)443 int mlx5_query_nic_vport_node_guid(struct mlx5_core_dev *mdev, u64 *node_guid)
444 {
445 	u32 *out;
446 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
447 	int err;
448 
449 	out = kvzalloc(outlen, GFP_KERNEL);
450 	if (!out)
451 		return -ENOMEM;
452 
453 	err = mlx5_query_nic_vport_context(mdev, 0, out);
454 	if (err)
455 		goto out;
456 
457 	*node_guid = MLX5_GET64(query_nic_vport_context_out, out,
458 				nic_vport_context.node_guid);
459 out:
460 	kvfree(out);
461 
462 	return err;
463 }
464 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_node_guid);
465 
mlx5_modify_nic_vport_node_guid(struct mlx5_core_dev * mdev,u16 vport,u64 node_guid)466 int mlx5_modify_nic_vport_node_guid(struct mlx5_core_dev *mdev,
467 				    u16 vport, u64 node_guid)
468 {
469 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
470 	void *nic_vport_context;
471 	void *in;
472 	int err;
473 
474 	if (!MLX5_CAP_GEN(mdev, vport_group_manager))
475 		return -EACCES;
476 
477 	in = kvzalloc(inlen, GFP_KERNEL);
478 	if (!in)
479 		return -ENOMEM;
480 
481 	MLX5_SET(modify_nic_vport_context_in, in,
482 		 field_select.node_guid, 1);
483 	MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
484 	MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
485 
486 	nic_vport_context = MLX5_ADDR_OF(modify_nic_vport_context_in,
487 					 in, nic_vport_context);
488 	MLX5_SET64(nic_vport_context, nic_vport_context, node_guid, node_guid);
489 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
490 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
491 
492 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
493 
494 	kvfree(in);
495 
496 	return err;
497 }
498 
mlx5_query_nic_vport_qkey_viol_cntr(struct mlx5_core_dev * mdev,u16 * qkey_viol_cntr)499 int mlx5_query_nic_vport_qkey_viol_cntr(struct mlx5_core_dev *mdev,
500 					u16 *qkey_viol_cntr)
501 {
502 	u32 *out;
503 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
504 	int err;
505 
506 	out = kvzalloc(outlen, GFP_KERNEL);
507 	if (!out)
508 		return -ENOMEM;
509 
510 	err = mlx5_query_nic_vport_context(mdev, 0, out);
511 	if (err)
512 		goto out;
513 
514 	*qkey_viol_cntr = MLX5_GET(query_nic_vport_context_out, out,
515 				   nic_vport_context.qkey_violation_counter);
516 out:
517 	kvfree(out);
518 
519 	return err;
520 }
521 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_qkey_viol_cntr);
522 
mlx5_query_hca_vport_gid(struct mlx5_core_dev * dev,u8 other_vport,u8 port_num,u16 vf_num,u16 gid_index,union ib_gid * gid)523 int mlx5_query_hca_vport_gid(struct mlx5_core_dev *dev, u8 other_vport,
524 			     u8 port_num, u16  vf_num, u16 gid_index,
525 			     union ib_gid *gid)
526 {
527 	int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_in);
528 	int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
529 	int is_group_manager;
530 	void *out = NULL;
531 	void *in = NULL;
532 	union ib_gid *tmp;
533 	int tbsz;
534 	int nout;
535 	int err;
536 
537 	is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
538 	tbsz = mlx5_get_gid_table_len(MLX5_CAP_GEN(dev, gid_table_size));
539 	mlx5_core_dbg(dev, "vf_num %d, index %d, gid_table_size %d\n",
540 		      vf_num, gid_index, tbsz);
541 
542 	if (gid_index > tbsz && gid_index != 0xffff)
543 		return -EINVAL;
544 
545 	if (gid_index == 0xffff)
546 		nout = tbsz;
547 	else
548 		nout = 1;
549 
550 	out_sz += nout * sizeof(*gid);
551 
552 	in = kvzalloc(in_sz, GFP_KERNEL);
553 	out = kvzalloc(out_sz, GFP_KERNEL);
554 	if (!in || !out) {
555 		err = -ENOMEM;
556 		goto out;
557 	}
558 
559 	MLX5_SET(query_hca_vport_gid_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_GID);
560 	if (other_vport) {
561 		if (is_group_manager) {
562 			MLX5_SET(query_hca_vport_gid_in, in, vport_number, vf_num);
563 			MLX5_SET(query_hca_vport_gid_in, in, other_vport, 1);
564 		} else {
565 			err = -EPERM;
566 			goto out;
567 		}
568 	}
569 	MLX5_SET(query_hca_vport_gid_in, in, gid_index, gid_index);
570 
571 	if (MLX5_CAP_GEN(dev, num_ports) == 2)
572 		MLX5_SET(query_hca_vport_gid_in, in, port_num, port_num);
573 
574 	err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
575 	if (err)
576 		goto out;
577 
578 	tmp = out + MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
579 	gid->global.subnet_prefix = tmp->global.subnet_prefix;
580 	gid->global.interface_id = tmp->global.interface_id;
581 
582 out:
583 	kvfree(in);
584 	kvfree(out);
585 	return err;
586 }
587 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_gid);
588 
mlx5_query_hca_vport_pkey(struct mlx5_core_dev * dev,u8 other_vport,u8 port_num,u16 vf_num,u16 pkey_index,u16 * pkey)589 int mlx5_query_hca_vport_pkey(struct mlx5_core_dev *dev, u8 other_vport,
590 			      u8 port_num, u16 vf_num, u16 pkey_index,
591 			      u16 *pkey)
592 {
593 	int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_in);
594 	int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_out);
595 	int is_group_manager;
596 	void *out = NULL;
597 	void *in = NULL;
598 	void *pkarr;
599 	int nout;
600 	int tbsz;
601 	int err;
602 	int i;
603 
604 	is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
605 
606 	tbsz = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size));
607 	if (pkey_index > tbsz && pkey_index != 0xffff)
608 		return -EINVAL;
609 
610 	if (pkey_index == 0xffff)
611 		nout = tbsz;
612 	else
613 		nout = 1;
614 
615 	out_sz += nout * MLX5_ST_SZ_BYTES(pkey);
616 
617 	in = kvzalloc(in_sz, GFP_KERNEL);
618 	out = kvzalloc(out_sz, GFP_KERNEL);
619 	if (!in || !out) {
620 		err = -ENOMEM;
621 		goto out;
622 	}
623 
624 	MLX5_SET(query_hca_vport_pkey_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY);
625 	if (other_vport) {
626 		if (is_group_manager) {
627 			MLX5_SET(query_hca_vport_pkey_in, in, vport_number, vf_num);
628 			MLX5_SET(query_hca_vport_pkey_in, in, other_vport, 1);
629 		} else {
630 			err = -EPERM;
631 			goto out;
632 		}
633 	}
634 	MLX5_SET(query_hca_vport_pkey_in, in, pkey_index, pkey_index);
635 
636 	if (MLX5_CAP_GEN(dev, num_ports) == 2)
637 		MLX5_SET(query_hca_vport_pkey_in, in, port_num, port_num);
638 
639 	err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
640 	if (err)
641 		goto out;
642 
643 	pkarr = MLX5_ADDR_OF(query_hca_vport_pkey_out, out, pkey);
644 	for (i = 0; i < nout; i++, pkey++, pkarr += MLX5_ST_SZ_BYTES(pkey))
645 		*pkey = MLX5_GET_PR(pkey, pkarr, pkey);
646 
647 out:
648 	kvfree(in);
649 	kvfree(out);
650 	return err;
651 }
652 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_pkey);
653 
mlx5_query_hca_vport_context(struct mlx5_core_dev * dev,u8 other_vport,u8 port_num,u16 vf_num,struct mlx5_hca_vport_context * rep)654 int mlx5_query_hca_vport_context(struct mlx5_core_dev *dev,
655 				 u8 other_vport, u8 port_num,
656 				 u16 vf_num,
657 				 struct mlx5_hca_vport_context *rep)
658 {
659 	int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_context_out);
660 	int in[MLX5_ST_SZ_DW(query_hca_vport_context_in)] = {};
661 	int is_group_manager;
662 	void *out;
663 	void *ctx;
664 	int err;
665 
666 	is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
667 
668 	out = kvzalloc(out_sz, GFP_KERNEL);
669 	if (!out)
670 		return -ENOMEM;
671 
672 	MLX5_SET(query_hca_vport_context_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT);
673 
674 	if (other_vport) {
675 		if (is_group_manager) {
676 			MLX5_SET(query_hca_vport_context_in, in, other_vport, 1);
677 			MLX5_SET(query_hca_vport_context_in, in, vport_number, vf_num);
678 		} else {
679 			err = -EPERM;
680 			goto ex;
681 		}
682 	}
683 
684 	if (MLX5_CAP_GEN(dev, num_ports) == 2)
685 		MLX5_SET(query_hca_vport_context_in, in, port_num, port_num);
686 
687 	err = mlx5_cmd_exec_inout(dev, query_hca_vport_context, in, out);
688 	if (err)
689 		goto ex;
690 
691 	ctx = MLX5_ADDR_OF(query_hca_vport_context_out, out, hca_vport_context);
692 	rep->field_select = MLX5_GET_PR(hca_vport_context, ctx, field_select);
693 	rep->sm_virt_aware = MLX5_GET_PR(hca_vport_context, ctx, sm_virt_aware);
694 	rep->has_smi = MLX5_GET_PR(hca_vport_context, ctx, has_smi);
695 	rep->has_raw = MLX5_GET_PR(hca_vport_context, ctx, has_raw);
696 	rep->policy = MLX5_GET_PR(hca_vport_context, ctx, vport_state_policy);
697 	rep->phys_state = MLX5_GET_PR(hca_vport_context, ctx,
698 				      port_physical_state);
699 	rep->vport_state = MLX5_GET_PR(hca_vport_context, ctx, vport_state);
700 	rep->port_physical_state = MLX5_GET_PR(hca_vport_context, ctx,
701 					       port_physical_state);
702 	rep->port_guid = MLX5_GET64_PR(hca_vport_context, ctx, port_guid);
703 	rep->node_guid = MLX5_GET64_PR(hca_vport_context, ctx, node_guid);
704 	rep->cap_mask1 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask1);
705 	rep->cap_mask1_perm = MLX5_GET_PR(hca_vport_context, ctx,
706 					  cap_mask1_field_select);
707 	rep->cap_mask2 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask2);
708 	rep->cap_mask2_perm = MLX5_GET_PR(hca_vport_context, ctx,
709 					  cap_mask2_field_select);
710 	rep->lid = MLX5_GET_PR(hca_vport_context, ctx, lid);
711 	rep->init_type_reply = MLX5_GET_PR(hca_vport_context, ctx,
712 					   init_type_reply);
713 	rep->lmc = MLX5_GET_PR(hca_vport_context, ctx, lmc);
714 	rep->subnet_timeout = MLX5_GET_PR(hca_vport_context, ctx,
715 					  subnet_timeout);
716 	rep->sm_lid = MLX5_GET_PR(hca_vport_context, ctx, sm_lid);
717 	rep->sm_sl = MLX5_GET_PR(hca_vport_context, ctx, sm_sl);
718 	rep->qkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
719 						  qkey_violation_counter);
720 	rep->pkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
721 						  pkey_violation_counter);
722 	rep->grh_required = MLX5_GET_PR(hca_vport_context, ctx, grh_required);
723 	rep->sys_image_guid = MLX5_GET64_PR(hca_vport_context, ctx,
724 					    system_image_guid);
725 
726 ex:
727 	kvfree(out);
728 	return err;
729 }
730 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_context);
731 
mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev * dev,u64 * sys_image_guid)732 int mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev *dev,
733 					   u64 *sys_image_guid)
734 {
735 	struct mlx5_hca_vport_context *rep;
736 	int err;
737 
738 	rep = kvzalloc(sizeof(*rep), GFP_KERNEL);
739 	if (!rep)
740 		return -ENOMEM;
741 
742 	err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
743 	if (!err)
744 		*sys_image_guid = rep->sys_image_guid;
745 
746 	kvfree(rep);
747 	return err;
748 }
749 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_system_image_guid);
750 
mlx5_query_hca_vport_node_guid(struct mlx5_core_dev * dev,u64 * node_guid)751 int mlx5_query_hca_vport_node_guid(struct mlx5_core_dev *dev,
752 				   u64 *node_guid)
753 {
754 	struct mlx5_hca_vport_context *rep;
755 	int err;
756 
757 	rep = kvzalloc(sizeof(*rep), GFP_KERNEL);
758 	if (!rep)
759 		return -ENOMEM;
760 
761 	err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
762 	if (!err)
763 		*node_guid = rep->node_guid;
764 
765 	kvfree(rep);
766 	return err;
767 }
768 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_node_guid);
769 
mlx5_query_nic_vport_promisc(struct mlx5_core_dev * mdev,u16 vport,int * promisc_uc,int * promisc_mc,int * promisc_all)770 int mlx5_query_nic_vport_promisc(struct mlx5_core_dev *mdev,
771 				 u16 vport,
772 				 int *promisc_uc,
773 				 int *promisc_mc,
774 				 int *promisc_all)
775 {
776 	u32 *out;
777 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
778 	int err;
779 
780 	out = kvzalloc(outlen, GFP_KERNEL);
781 	if (!out)
782 		return -ENOMEM;
783 
784 	err = mlx5_query_nic_vport_context(mdev, vport, out);
785 	if (err)
786 		goto out;
787 
788 	*promisc_uc = MLX5_GET(query_nic_vport_context_out, out,
789 			       nic_vport_context.promisc_uc);
790 	*promisc_mc = MLX5_GET(query_nic_vport_context_out, out,
791 			       nic_vport_context.promisc_mc);
792 	*promisc_all = MLX5_GET(query_nic_vport_context_out, out,
793 				nic_vport_context.promisc_all);
794 
795 out:
796 	kvfree(out);
797 	return err;
798 }
799 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_promisc);
800 
mlx5_modify_nic_vport_promisc(struct mlx5_core_dev * mdev,int promisc_uc,int promisc_mc,int promisc_all)801 int mlx5_modify_nic_vport_promisc(struct mlx5_core_dev *mdev,
802 				  int promisc_uc,
803 				  int promisc_mc,
804 				  int promisc_all)
805 {
806 	void *in;
807 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
808 	int err;
809 
810 	in = kvzalloc(inlen, GFP_KERNEL);
811 	if (!in)
812 		return -ENOMEM;
813 
814 	MLX5_SET(modify_nic_vport_context_in, in, field_select.promisc, 1);
815 	MLX5_SET(modify_nic_vport_context_in, in,
816 		 nic_vport_context.promisc_uc, promisc_uc);
817 	MLX5_SET(modify_nic_vport_context_in, in,
818 		 nic_vport_context.promisc_mc, promisc_mc);
819 	MLX5_SET(modify_nic_vport_context_in, in,
820 		 nic_vport_context.promisc_all, promisc_all);
821 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
822 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
823 
824 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
825 
826 	kvfree(in);
827 
828 	return err;
829 }
830 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_promisc);
831 
832 enum {
833 	UC_LOCAL_LB,
834 	MC_LOCAL_LB
835 };
836 
mlx5_nic_vport_update_local_lb(struct mlx5_core_dev * mdev,bool enable)837 int mlx5_nic_vport_update_local_lb(struct mlx5_core_dev *mdev, bool enable)
838 {
839 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
840 	void *in;
841 	int err;
842 
843 	if (!MLX5_CAP_GEN(mdev, disable_local_lb_mc) &&
844 	    !MLX5_CAP_GEN(mdev, disable_local_lb_uc))
845 		return 0;
846 
847 	in = kvzalloc(inlen, GFP_KERNEL);
848 	if (!in)
849 		return -ENOMEM;
850 
851 	MLX5_SET(modify_nic_vport_context_in, in,
852 		 nic_vport_context.disable_mc_local_lb, !enable);
853 	MLX5_SET(modify_nic_vport_context_in, in,
854 		 nic_vport_context.disable_uc_local_lb, !enable);
855 
856 	if (MLX5_CAP_GEN(mdev, disable_local_lb_mc))
857 		MLX5_SET(modify_nic_vport_context_in, in,
858 			 field_select.disable_mc_local_lb, 1);
859 
860 	if (MLX5_CAP_GEN(mdev, disable_local_lb_uc))
861 		MLX5_SET(modify_nic_vport_context_in, in,
862 			 field_select.disable_uc_local_lb, 1);
863 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
864 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
865 
866 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
867 
868 	if (!err)
869 		mlx5_core_dbg(mdev, "%s local_lb\n",
870 			      enable ? "enable" : "disable");
871 
872 	kvfree(in);
873 	return err;
874 }
875 EXPORT_SYMBOL_GPL(mlx5_nic_vport_update_local_lb);
876 
mlx5_nic_vport_query_local_lb(struct mlx5_core_dev * mdev,bool * status)877 int mlx5_nic_vport_query_local_lb(struct mlx5_core_dev *mdev, bool *status)
878 {
879 	int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
880 	u32 *out;
881 	int value;
882 	int err;
883 
884 	out = kvzalloc(outlen, GFP_KERNEL);
885 	if (!out)
886 		return -ENOMEM;
887 
888 	err = mlx5_query_nic_vport_context(mdev, 0, out);
889 	if (err)
890 		goto out;
891 
892 	value = MLX5_GET(query_nic_vport_context_out, out,
893 			 nic_vport_context.disable_mc_local_lb) << MC_LOCAL_LB;
894 
895 	value |= MLX5_GET(query_nic_vport_context_out, out,
896 			  nic_vport_context.disable_uc_local_lb) << UC_LOCAL_LB;
897 
898 	*status = !value;
899 
900 out:
901 	kvfree(out);
902 	return err;
903 }
904 EXPORT_SYMBOL_GPL(mlx5_nic_vport_query_local_lb);
905 
906 enum mlx5_vport_roce_state {
907 	MLX5_VPORT_ROCE_DISABLED = 0,
908 	MLX5_VPORT_ROCE_ENABLED  = 1,
909 };
910 
mlx5_nic_vport_update_roce_state(struct mlx5_core_dev * mdev,enum mlx5_vport_roce_state state)911 static int mlx5_nic_vport_update_roce_state(struct mlx5_core_dev *mdev,
912 					    enum mlx5_vport_roce_state state)
913 {
914 	void *in;
915 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
916 	int err;
917 
918 	in = kvzalloc(inlen, GFP_KERNEL);
919 	if (!in)
920 		return -ENOMEM;
921 
922 	MLX5_SET(modify_nic_vport_context_in, in, field_select.roce_en, 1);
923 	MLX5_SET(modify_nic_vport_context_in, in, nic_vport_context.roce_en,
924 		 state);
925 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
926 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
927 
928 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
929 
930 	kvfree(in);
931 
932 	return err;
933 }
934 
mlx5_nic_vport_enable_roce(struct mlx5_core_dev * mdev)935 int mlx5_nic_vport_enable_roce(struct mlx5_core_dev *mdev)
936 {
937 	int err = 0;
938 
939 	mutex_lock(&mlx5_roce_en_lock);
940 	if (!mdev->roce.roce_en)
941 		err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_ENABLED);
942 
943 	if (!err)
944 		mdev->roce.roce_en++;
945 	mutex_unlock(&mlx5_roce_en_lock);
946 
947 	return err;
948 }
949 EXPORT_SYMBOL_GPL(mlx5_nic_vport_enable_roce);
950 
mlx5_nic_vport_disable_roce(struct mlx5_core_dev * mdev)951 int mlx5_nic_vport_disable_roce(struct mlx5_core_dev *mdev)
952 {
953 	int err = 0;
954 
955 	mutex_lock(&mlx5_roce_en_lock);
956 	if (mdev->roce.roce_en) {
957 		mdev->roce.roce_en--;
958 		if (mdev->roce.roce_en == 0)
959 			err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_DISABLED);
960 
961 		if (err)
962 			mdev->roce.roce_en++;
963 	}
964 	mutex_unlock(&mlx5_roce_en_lock);
965 	return err;
966 }
967 EXPORT_SYMBOL(mlx5_nic_vport_disable_roce);
968 
mlx5_core_query_vport_counter(struct mlx5_core_dev * dev,u8 other_vport,int vf,u8 port_num,void * out)969 int mlx5_core_query_vport_counter(struct mlx5_core_dev *dev, u8 other_vport,
970 				  int vf, u8 port_num, void *out)
971 {
972 	int in_sz = MLX5_ST_SZ_BYTES(query_vport_counter_in);
973 	int is_group_manager;
974 	void *in;
975 	int err;
976 
977 	is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
978 	in = kvzalloc(in_sz, GFP_KERNEL);
979 	if (!in) {
980 		err = -ENOMEM;
981 		return err;
982 	}
983 
984 	MLX5_SET(query_vport_counter_in, in, opcode,
985 		 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
986 	if (other_vport) {
987 		if (is_group_manager) {
988 			MLX5_SET(query_vport_counter_in, in, other_vport, 1);
989 			MLX5_SET(query_vport_counter_in, in, vport_number, vf + 1);
990 		} else {
991 			err = -EPERM;
992 			goto free;
993 		}
994 	}
995 	if (MLX5_CAP_GEN(dev, num_ports) == 2)
996 		MLX5_SET(query_vport_counter_in, in, port_num, port_num);
997 
998 	err = mlx5_cmd_exec_inout(dev, query_vport_counter, in, out);
999 free:
1000 	kvfree(in);
1001 	return err;
1002 }
1003 EXPORT_SYMBOL_GPL(mlx5_core_query_vport_counter);
1004 
mlx5_query_vport_down_stats(struct mlx5_core_dev * mdev,u16 vport,u8 other_vport,u64 * rx_discard_vport_down,u64 * tx_discard_vport_down)1005 int mlx5_query_vport_down_stats(struct mlx5_core_dev *mdev, u16 vport,
1006 				u8 other_vport, u64 *rx_discard_vport_down,
1007 				u64 *tx_discard_vport_down)
1008 {
1009 	u32 out[MLX5_ST_SZ_DW(query_vnic_env_out)] = {};
1010 	u32 in[MLX5_ST_SZ_DW(query_vnic_env_in)] = {};
1011 	int err;
1012 
1013 	MLX5_SET(query_vnic_env_in, in, opcode,
1014 		 MLX5_CMD_OP_QUERY_VNIC_ENV);
1015 	MLX5_SET(query_vnic_env_in, in, op_mod, 0);
1016 	MLX5_SET(query_vnic_env_in, in, vport_number, vport);
1017 	MLX5_SET(query_vnic_env_in, in, other_vport, other_vport);
1018 
1019 	err = mlx5_cmd_exec_inout(mdev, query_vnic_env, in, out);
1020 	if (err)
1021 		return err;
1022 
1023 	*rx_discard_vport_down = MLX5_GET64(query_vnic_env_out, out,
1024 					    vport_env.receive_discard_vport_down);
1025 	*tx_discard_vport_down = MLX5_GET64(query_vnic_env_out, out,
1026 					    vport_env.transmit_discard_vport_down);
1027 	return 0;
1028 }
1029 
mlx5_core_modify_hca_vport_context(struct mlx5_core_dev * dev,u8 other_vport,u8 port_num,int vf,struct mlx5_hca_vport_context * req)1030 int mlx5_core_modify_hca_vport_context(struct mlx5_core_dev *dev,
1031 				       u8 other_vport, u8 port_num,
1032 				       int vf,
1033 				       struct mlx5_hca_vport_context *req)
1034 {
1035 	int in_sz = MLX5_ST_SZ_BYTES(modify_hca_vport_context_in);
1036 	int is_group_manager;
1037 	void *ctx;
1038 	void *in;
1039 	int err;
1040 
1041 	mlx5_core_dbg(dev, "vf %d\n", vf);
1042 	is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
1043 	in = kvzalloc(in_sz, GFP_KERNEL);
1044 	if (!in)
1045 		return -ENOMEM;
1046 
1047 	MLX5_SET(modify_hca_vport_context_in, in, opcode, MLX5_CMD_OP_MODIFY_HCA_VPORT_CONTEXT);
1048 	if (other_vport) {
1049 		if (is_group_manager) {
1050 			MLX5_SET(modify_hca_vport_context_in, in, other_vport, 1);
1051 			MLX5_SET(modify_hca_vport_context_in, in, vport_number, vf);
1052 		} else {
1053 			err = -EPERM;
1054 			goto ex;
1055 		}
1056 	}
1057 
1058 	if (MLX5_CAP_GEN(dev, num_ports) > 1)
1059 		MLX5_SET(modify_hca_vport_context_in, in, port_num, port_num);
1060 
1061 	ctx = MLX5_ADDR_OF(modify_hca_vport_context_in, in, hca_vport_context);
1062 	MLX5_SET(hca_vport_context, ctx, field_select, req->field_select);
1063 	if (req->field_select & MLX5_HCA_VPORT_SEL_STATE_POLICY)
1064 		MLX5_SET(hca_vport_context, ctx, vport_state_policy,
1065 			 req->policy);
1066 	if (req->field_select & MLX5_HCA_VPORT_SEL_PORT_GUID)
1067 		MLX5_SET64(hca_vport_context, ctx, port_guid, req->port_guid);
1068 	if (req->field_select & MLX5_HCA_VPORT_SEL_NODE_GUID)
1069 		MLX5_SET64(hca_vport_context, ctx, node_guid, req->node_guid);
1070 	MLX5_SET(hca_vport_context, ctx, cap_mask1, req->cap_mask1);
1071 	MLX5_SET(hca_vport_context, ctx, cap_mask1_field_select,
1072 		 req->cap_mask1_perm);
1073 	err = mlx5_cmd_exec_in(dev, modify_hca_vport_context, in);
1074 ex:
1075 	kvfree(in);
1076 	return err;
1077 }
1078 EXPORT_SYMBOL_GPL(mlx5_core_modify_hca_vport_context);
1079 
mlx5_nic_vport_affiliate_multiport(struct mlx5_core_dev * master_mdev,struct mlx5_core_dev * port_mdev)1080 int mlx5_nic_vport_affiliate_multiport(struct mlx5_core_dev *master_mdev,
1081 				       struct mlx5_core_dev *port_mdev)
1082 {
1083 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
1084 	void *in;
1085 	int err;
1086 
1087 	in = kvzalloc(inlen, GFP_KERNEL);
1088 	if (!in)
1089 		return -ENOMEM;
1090 
1091 	err = mlx5_nic_vport_enable_roce(port_mdev);
1092 	if (err)
1093 		goto free;
1094 
1095 	MLX5_SET(modify_nic_vport_context_in, in, field_select.affiliation, 1);
1096 	if (MLX5_CAP_GEN_2(master_mdev, sw_vhca_id_valid)) {
1097 		MLX5_SET(modify_nic_vport_context_in, in,
1098 			 nic_vport_context.vhca_id_type, VHCA_ID_TYPE_SW);
1099 		MLX5_SET(modify_nic_vport_context_in, in,
1100 			 nic_vport_context.affiliated_vhca_id,
1101 			 MLX5_CAP_GEN_2(master_mdev, sw_vhca_id));
1102 	} else {
1103 		MLX5_SET(modify_nic_vport_context_in, in,
1104 			 nic_vport_context.affiliated_vhca_id,
1105 			 MLX5_CAP_GEN(master_mdev, vhca_id));
1106 	}
1107 	MLX5_SET(modify_nic_vport_context_in, in,
1108 		 nic_vport_context.affiliation_criteria,
1109 		 MLX5_CAP_GEN(port_mdev, affiliate_nic_vport_criteria));
1110 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
1111 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
1112 
1113 	err = mlx5_cmd_exec_in(port_mdev, modify_nic_vport_context, in);
1114 	if (err)
1115 		mlx5_nic_vport_disable_roce(port_mdev);
1116 
1117 free:
1118 	kvfree(in);
1119 	return err;
1120 }
1121 EXPORT_SYMBOL_GPL(mlx5_nic_vport_affiliate_multiport);
1122 
mlx5_nic_vport_unaffiliate_multiport(struct mlx5_core_dev * port_mdev)1123 int mlx5_nic_vport_unaffiliate_multiport(struct mlx5_core_dev *port_mdev)
1124 {
1125 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
1126 	void *in;
1127 	int err;
1128 
1129 	in = kvzalloc(inlen, GFP_KERNEL);
1130 	if (!in)
1131 		return -ENOMEM;
1132 
1133 	MLX5_SET(modify_nic_vport_context_in, in, field_select.affiliation, 1);
1134 	MLX5_SET(modify_nic_vport_context_in, in,
1135 		 nic_vport_context.affiliated_vhca_id, 0);
1136 	MLX5_SET(modify_nic_vport_context_in, in,
1137 		 nic_vport_context.affiliation_criteria, 0);
1138 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
1139 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
1140 
1141 	err = mlx5_cmd_exec_in(port_mdev, modify_nic_vport_context, in);
1142 	if (!err)
1143 		mlx5_nic_vport_disable_roce(port_mdev);
1144 
1145 	kvfree(in);
1146 	return err;
1147 }
1148 EXPORT_SYMBOL_GPL(mlx5_nic_vport_unaffiliate_multiport);
1149 
mlx5_query_nic_system_image_guid(struct mlx5_core_dev * mdev)1150 u64 mlx5_query_nic_system_image_guid(struct mlx5_core_dev *mdev)
1151 {
1152 	int port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1153 	u64 tmp;
1154 	int err;
1155 
1156 	if (mdev->sys_image_guid)
1157 		return mdev->sys_image_guid;
1158 
1159 	if (port_type_cap == MLX5_CAP_PORT_TYPE_ETH)
1160 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
1161 	else
1162 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
1163 
1164 	mdev->sys_image_guid = err ? 0 : tmp;
1165 
1166 	return mdev->sys_image_guid;
1167 }
1168 EXPORT_SYMBOL_GPL(mlx5_query_nic_system_image_guid);
1169 
mlx5_vport_get_other_func_cap(struct mlx5_core_dev * dev,u16 vport,void * out,u16 opmod)1170 int mlx5_vport_get_other_func_cap(struct mlx5_core_dev *dev, u16 vport, void *out,
1171 				  u16 opmod)
1172 {
1173 	bool ec_vf_func = mlx5_core_is_ec_vf_vport(dev, vport);
1174 	u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1175 
1176 	opmod = (opmod << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1177 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1178 	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1179 	MLX5_SET(query_hca_cap_in, in, function_id, mlx5_vport_to_func_id(dev, vport, ec_vf_func));
1180 	MLX5_SET(query_hca_cap_in, in, other_function, true);
1181 	MLX5_SET(query_hca_cap_in, in, ec_vf_function, ec_vf_func);
1182 	return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1183 }
1184 EXPORT_SYMBOL_GPL(mlx5_vport_get_other_func_cap);
1185 
mlx5_vport_set_other_func_cap(struct mlx5_core_dev * dev,const void * hca_cap,u16 vport,u16 opmod)1186 int mlx5_vport_set_other_func_cap(struct mlx5_core_dev *dev, const void *hca_cap,
1187 				  u16 vport, u16 opmod)
1188 {
1189 	bool ec_vf_func = mlx5_core_is_ec_vf_vport(dev, vport);
1190 	int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
1191 	void *set_hca_cap;
1192 	void *set_ctx;
1193 	int ret;
1194 
1195 	set_ctx = kzalloc(set_sz, GFP_KERNEL);
1196 	if (!set_ctx)
1197 		return -ENOMEM;
1198 
1199 	MLX5_SET(set_hca_cap_in, set_ctx, opcode, MLX5_CMD_OP_SET_HCA_CAP);
1200 	MLX5_SET(set_hca_cap_in, set_ctx, op_mod, opmod << 1);
1201 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
1202 	memcpy(set_hca_cap, hca_cap, MLX5_ST_SZ_BYTES(cmd_hca_cap));
1203 	MLX5_SET(set_hca_cap_in, set_ctx, function_id,
1204 		 mlx5_vport_to_func_id(dev, vport, ec_vf_func));
1205 	MLX5_SET(set_hca_cap_in, set_ctx, other_function, true);
1206 	MLX5_SET(set_hca_cap_in, set_ctx, ec_vf_function, ec_vf_func);
1207 	ret = mlx5_cmd_exec_in(dev, set_hca_cap, set_ctx);
1208 
1209 	kfree(set_ctx);
1210 	return ret;
1211 }
1212