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_sd_group(struct mlx5_core_dev * mdev,u8 * sd_group)443 int mlx5_query_nic_vport_sd_group(struct mlx5_core_dev *mdev, u8 *sd_group)
444 {
445 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
446 u32 *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 *sd_group = MLX5_GET(query_nic_vport_context_out, out,
458 nic_vport_context.sd_group);
459 out:
460 kvfree(out);
461 return err;
462 }
463
mlx5_query_nic_vport_node_guid(struct mlx5_core_dev * mdev,u64 * node_guid)464 int mlx5_query_nic_vport_node_guid(struct mlx5_core_dev *mdev, u64 *node_guid)
465 {
466 u32 *out;
467 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
468 int err;
469
470 out = kvzalloc(outlen, GFP_KERNEL);
471 if (!out)
472 return -ENOMEM;
473
474 err = mlx5_query_nic_vport_context(mdev, 0, out);
475 if (err)
476 goto out;
477
478 *node_guid = MLX5_GET64(query_nic_vport_context_out, out,
479 nic_vport_context.node_guid);
480 out:
481 kvfree(out);
482
483 return err;
484 }
485 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_node_guid);
486
mlx5_modify_nic_vport_node_guid(struct mlx5_core_dev * mdev,u16 vport,u64 node_guid)487 int mlx5_modify_nic_vport_node_guid(struct mlx5_core_dev *mdev,
488 u16 vport, u64 node_guid)
489 {
490 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
491 void *nic_vport_context;
492 void *in;
493 int err;
494
495 if (!MLX5_CAP_GEN(mdev, vport_group_manager))
496 return -EACCES;
497
498 in = kvzalloc(inlen, GFP_KERNEL);
499 if (!in)
500 return -ENOMEM;
501
502 MLX5_SET(modify_nic_vport_context_in, in,
503 field_select.node_guid, 1);
504 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
505 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
506
507 nic_vport_context = MLX5_ADDR_OF(modify_nic_vport_context_in,
508 in, nic_vport_context);
509 MLX5_SET64(nic_vport_context, nic_vport_context, node_guid, node_guid);
510 MLX5_SET(modify_nic_vport_context_in, in, opcode,
511 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
512
513 err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
514
515 kvfree(in);
516
517 return err;
518 }
519
mlx5_query_nic_vport_qkey_viol_cntr(struct mlx5_core_dev * mdev,u16 * qkey_viol_cntr)520 int mlx5_query_nic_vport_qkey_viol_cntr(struct mlx5_core_dev *mdev,
521 u16 *qkey_viol_cntr)
522 {
523 u32 *out;
524 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
525 int err;
526
527 out = kvzalloc(outlen, GFP_KERNEL);
528 if (!out)
529 return -ENOMEM;
530
531 err = mlx5_query_nic_vport_context(mdev, 0, out);
532 if (err)
533 goto out;
534
535 *qkey_viol_cntr = MLX5_GET(query_nic_vport_context_out, out,
536 nic_vport_context.qkey_violation_counter);
537 out:
538 kvfree(out);
539
540 return err;
541 }
542 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_qkey_viol_cntr);
543
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)544 int mlx5_query_hca_vport_gid(struct mlx5_core_dev *dev, u8 other_vport,
545 u8 port_num, u16 vf_num, u16 gid_index,
546 union ib_gid *gid)
547 {
548 int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_in);
549 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
550 int is_group_manager;
551 void *out = NULL;
552 void *in = NULL;
553 union ib_gid *tmp;
554 int tbsz;
555 int nout;
556 int err;
557
558 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
559 tbsz = mlx5_get_gid_table_len(MLX5_CAP_GEN(dev, gid_table_size));
560 mlx5_core_dbg(dev, "vf_num %d, index %d, gid_table_size %d\n",
561 vf_num, gid_index, tbsz);
562
563 if (gid_index > tbsz && gid_index != 0xffff)
564 return -EINVAL;
565
566 if (gid_index == 0xffff)
567 nout = tbsz;
568 else
569 nout = 1;
570
571 out_sz += nout * sizeof(*gid);
572
573 in = kvzalloc(in_sz, GFP_KERNEL);
574 out = kvzalloc(out_sz, GFP_KERNEL);
575 if (!in || !out) {
576 err = -ENOMEM;
577 goto out;
578 }
579
580 MLX5_SET(query_hca_vport_gid_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_GID);
581 if (other_vport) {
582 if (is_group_manager) {
583 MLX5_SET(query_hca_vport_gid_in, in, vport_number, vf_num);
584 MLX5_SET(query_hca_vport_gid_in, in, other_vport, 1);
585 } else {
586 err = -EPERM;
587 goto out;
588 }
589 }
590 MLX5_SET(query_hca_vport_gid_in, in, gid_index, gid_index);
591
592 if (MLX5_CAP_GEN(dev, num_ports) == 2)
593 MLX5_SET(query_hca_vport_gid_in, in, port_num, port_num);
594
595 err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
596 if (err)
597 goto out;
598
599 tmp = out + MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
600 gid->global.subnet_prefix = tmp->global.subnet_prefix;
601 gid->global.interface_id = tmp->global.interface_id;
602
603 out:
604 kvfree(in);
605 kvfree(out);
606 return err;
607 }
608 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_gid);
609
mlx5_query_hca_vport_pkey(struct mlx5_core_dev * dev,u8 other_vport,u8 port_num,u16 vf_num,u16 pkey_index,u16 * pkey)610 int mlx5_query_hca_vport_pkey(struct mlx5_core_dev *dev, u8 other_vport,
611 u8 port_num, u16 vf_num, u16 pkey_index,
612 u16 *pkey)
613 {
614 int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_in);
615 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_out);
616 int is_group_manager;
617 void *out = NULL;
618 void *in = NULL;
619 void *pkarr;
620 int nout;
621 int tbsz;
622 int err;
623 int i;
624
625 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
626
627 tbsz = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size));
628 if (pkey_index > tbsz && pkey_index != 0xffff)
629 return -EINVAL;
630
631 if (pkey_index == 0xffff)
632 nout = tbsz;
633 else
634 nout = 1;
635
636 out_sz += nout * MLX5_ST_SZ_BYTES(pkey);
637
638 in = kvzalloc(in_sz, GFP_KERNEL);
639 out = kvzalloc(out_sz, GFP_KERNEL);
640 if (!in || !out) {
641 err = -ENOMEM;
642 goto out;
643 }
644
645 MLX5_SET(query_hca_vport_pkey_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY);
646 if (other_vport) {
647 if (is_group_manager) {
648 MLX5_SET(query_hca_vport_pkey_in, in, vport_number, vf_num);
649 MLX5_SET(query_hca_vport_pkey_in, in, other_vport, 1);
650 } else {
651 err = -EPERM;
652 goto out;
653 }
654 }
655 MLX5_SET(query_hca_vport_pkey_in, in, pkey_index, pkey_index);
656
657 if (MLX5_CAP_GEN(dev, num_ports) == 2)
658 MLX5_SET(query_hca_vport_pkey_in, in, port_num, port_num);
659
660 err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
661 if (err)
662 goto out;
663
664 pkarr = MLX5_ADDR_OF(query_hca_vport_pkey_out, out, pkey);
665 for (i = 0; i < nout; i++, pkey++, pkarr += MLX5_ST_SZ_BYTES(pkey))
666 *pkey = MLX5_GET_PR(pkey, pkarr, pkey);
667
668 out:
669 kvfree(in);
670 kvfree(out);
671 return err;
672 }
673 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_pkey);
674
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)675 int mlx5_query_hca_vport_context(struct mlx5_core_dev *dev,
676 u8 other_vport, u8 port_num,
677 u16 vf_num,
678 struct mlx5_hca_vport_context *rep)
679 {
680 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_context_out);
681 int in[MLX5_ST_SZ_DW(query_hca_vport_context_in)] = {};
682 int is_group_manager;
683 void *out;
684 void *ctx;
685 int err;
686
687 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
688
689 out = kvzalloc(out_sz, GFP_KERNEL);
690 if (!out)
691 return -ENOMEM;
692
693 MLX5_SET(query_hca_vport_context_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT);
694
695 if (other_vport) {
696 if (is_group_manager) {
697 MLX5_SET(query_hca_vport_context_in, in, other_vport, 1);
698 MLX5_SET(query_hca_vport_context_in, in, vport_number, vf_num);
699 } else {
700 err = -EPERM;
701 goto ex;
702 }
703 }
704
705 if (MLX5_CAP_GEN(dev, num_ports) == 2)
706 MLX5_SET(query_hca_vport_context_in, in, port_num, port_num);
707
708 err = mlx5_cmd_exec_inout(dev, query_hca_vport_context, in, out);
709 if (err)
710 goto ex;
711
712 ctx = MLX5_ADDR_OF(query_hca_vport_context_out, out, hca_vport_context);
713 rep->field_select = MLX5_GET_PR(hca_vport_context, ctx, field_select);
714 rep->sm_virt_aware = MLX5_GET_PR(hca_vport_context, ctx, sm_virt_aware);
715 rep->has_smi = MLX5_GET_PR(hca_vport_context, ctx, has_smi);
716 rep->has_raw = MLX5_GET_PR(hca_vport_context, ctx, has_raw);
717 rep->policy = MLX5_GET_PR(hca_vport_context, ctx, vport_state_policy);
718 rep->phys_state = MLX5_GET_PR(hca_vport_context, ctx,
719 port_physical_state);
720 rep->vport_state = MLX5_GET_PR(hca_vport_context, ctx, vport_state);
721 rep->port_physical_state = MLX5_GET_PR(hca_vport_context, ctx,
722 port_physical_state);
723 rep->port_guid = MLX5_GET64_PR(hca_vport_context, ctx, port_guid);
724 rep->node_guid = MLX5_GET64_PR(hca_vport_context, ctx, node_guid);
725 rep->cap_mask1 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask1);
726 rep->cap_mask1_perm = MLX5_GET_PR(hca_vport_context, ctx,
727 cap_mask1_field_select);
728 rep->cap_mask2 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask2);
729 rep->cap_mask2_perm = MLX5_GET_PR(hca_vport_context, ctx,
730 cap_mask2_field_select);
731 rep->lid = MLX5_GET_PR(hca_vport_context, ctx, lid);
732 rep->init_type_reply = MLX5_GET_PR(hca_vport_context, ctx,
733 init_type_reply);
734 rep->lmc = MLX5_GET_PR(hca_vport_context, ctx, lmc);
735 rep->subnet_timeout = MLX5_GET_PR(hca_vport_context, ctx,
736 subnet_timeout);
737 rep->sm_lid = MLX5_GET_PR(hca_vport_context, ctx, sm_lid);
738 rep->sm_sl = MLX5_GET_PR(hca_vport_context, ctx, sm_sl);
739 rep->qkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
740 qkey_violation_counter);
741 rep->pkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
742 pkey_violation_counter);
743 rep->grh_required = MLX5_GET_PR(hca_vport_context, ctx, grh_required);
744 rep->sys_image_guid = MLX5_GET64_PR(hca_vport_context, ctx,
745 system_image_guid);
746 rep->num_plane = MLX5_GET_PR(hca_vport_context, ctx, num_port_plane);
747
748 ex:
749 kvfree(out);
750 return err;
751 }
752 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_context);
753
mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev * dev,u64 * sys_image_guid)754 int mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev *dev,
755 u64 *sys_image_guid)
756 {
757 struct mlx5_hca_vport_context *rep;
758 int err;
759
760 rep = kvzalloc(sizeof(*rep), GFP_KERNEL);
761 if (!rep)
762 return -ENOMEM;
763
764 err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
765 if (!err)
766 *sys_image_guid = rep->sys_image_guid;
767
768 kvfree(rep);
769 return err;
770 }
771 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_system_image_guid);
772
mlx5_query_hca_vport_node_guid(struct mlx5_core_dev * dev,u64 * node_guid)773 int mlx5_query_hca_vport_node_guid(struct mlx5_core_dev *dev,
774 u64 *node_guid)
775 {
776 struct mlx5_hca_vport_context *rep;
777 int err;
778
779 rep = kvzalloc(sizeof(*rep), GFP_KERNEL);
780 if (!rep)
781 return -ENOMEM;
782
783 err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
784 if (!err)
785 *node_guid = rep->node_guid;
786
787 kvfree(rep);
788 return err;
789 }
790 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_node_guid);
791
mlx5_query_nic_vport_promisc(struct mlx5_core_dev * mdev,u16 vport,int * promisc_uc,int * promisc_mc,int * promisc_all)792 int mlx5_query_nic_vport_promisc(struct mlx5_core_dev *mdev,
793 u16 vport,
794 int *promisc_uc,
795 int *promisc_mc,
796 int *promisc_all)
797 {
798 u32 *out;
799 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
800 int err;
801
802 out = kvzalloc(outlen, GFP_KERNEL);
803 if (!out)
804 return -ENOMEM;
805
806 err = mlx5_query_nic_vport_context(mdev, vport, out);
807 if (err)
808 goto out;
809
810 *promisc_uc = MLX5_GET(query_nic_vport_context_out, out,
811 nic_vport_context.promisc_uc);
812 *promisc_mc = MLX5_GET(query_nic_vport_context_out, out,
813 nic_vport_context.promisc_mc);
814 *promisc_all = MLX5_GET(query_nic_vport_context_out, out,
815 nic_vport_context.promisc_all);
816
817 out:
818 kvfree(out);
819 return err;
820 }
821 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_promisc);
822
mlx5_modify_nic_vport_promisc(struct mlx5_core_dev * mdev,int promisc_uc,int promisc_mc,int promisc_all)823 int mlx5_modify_nic_vport_promisc(struct mlx5_core_dev *mdev,
824 int promisc_uc,
825 int promisc_mc,
826 int promisc_all)
827 {
828 void *in;
829 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
830 int err;
831
832 in = kvzalloc(inlen, GFP_KERNEL);
833 if (!in)
834 return -ENOMEM;
835
836 MLX5_SET(modify_nic_vport_context_in, in, field_select.promisc, 1);
837 MLX5_SET(modify_nic_vport_context_in, in,
838 nic_vport_context.promisc_uc, promisc_uc);
839 MLX5_SET(modify_nic_vport_context_in, in,
840 nic_vport_context.promisc_mc, promisc_mc);
841 MLX5_SET(modify_nic_vport_context_in, in,
842 nic_vport_context.promisc_all, promisc_all);
843 MLX5_SET(modify_nic_vport_context_in, in, opcode,
844 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
845
846 err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
847
848 kvfree(in);
849
850 return err;
851 }
852 EXPORT_SYMBOL_GPL(mlx5_modify_nic_vport_promisc);
853
854 enum {
855 UC_LOCAL_LB,
856 MC_LOCAL_LB
857 };
858
mlx5_nic_vport_update_local_lb(struct mlx5_core_dev * mdev,bool enable)859 int mlx5_nic_vport_update_local_lb(struct mlx5_core_dev *mdev, bool enable)
860 {
861 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
862 void *in;
863 int err;
864
865 if (!MLX5_CAP_GEN(mdev, disable_local_lb_mc) &&
866 !MLX5_CAP_GEN(mdev, disable_local_lb_uc))
867 return 0;
868
869 in = kvzalloc(inlen, GFP_KERNEL);
870 if (!in)
871 return -ENOMEM;
872
873 MLX5_SET(modify_nic_vport_context_in, in,
874 nic_vport_context.disable_mc_local_lb, !enable);
875 MLX5_SET(modify_nic_vport_context_in, in,
876 nic_vport_context.disable_uc_local_lb, !enable);
877
878 if (MLX5_CAP_GEN(mdev, disable_local_lb_mc))
879 MLX5_SET(modify_nic_vport_context_in, in,
880 field_select.disable_mc_local_lb, 1);
881
882 if (MLX5_CAP_GEN(mdev, disable_local_lb_uc))
883 MLX5_SET(modify_nic_vport_context_in, in,
884 field_select.disable_uc_local_lb, 1);
885 MLX5_SET(modify_nic_vport_context_in, in, opcode,
886 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
887
888 err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
889
890 if (!err)
891 mlx5_core_dbg(mdev, "%s local_lb\n",
892 enable ? "enable" : "disable");
893
894 kvfree(in);
895 return err;
896 }
897 EXPORT_SYMBOL_GPL(mlx5_nic_vport_update_local_lb);
898
mlx5_nic_vport_query_local_lb(struct mlx5_core_dev * mdev,bool * status)899 int mlx5_nic_vport_query_local_lb(struct mlx5_core_dev *mdev, bool *status)
900 {
901 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
902 u32 *out;
903 int value;
904 int err;
905
906 out = kvzalloc(outlen, GFP_KERNEL);
907 if (!out)
908 return -ENOMEM;
909
910 err = mlx5_query_nic_vport_context(mdev, 0, out);
911 if (err)
912 goto out;
913
914 value = MLX5_GET(query_nic_vport_context_out, out,
915 nic_vport_context.disable_mc_local_lb) << MC_LOCAL_LB;
916
917 value |= MLX5_GET(query_nic_vport_context_out, out,
918 nic_vport_context.disable_uc_local_lb) << UC_LOCAL_LB;
919
920 *status = !value;
921
922 out:
923 kvfree(out);
924 return err;
925 }
926 EXPORT_SYMBOL_GPL(mlx5_nic_vport_query_local_lb);
927
928 enum mlx5_vport_roce_state {
929 MLX5_VPORT_ROCE_DISABLED = 0,
930 MLX5_VPORT_ROCE_ENABLED = 1,
931 };
932
mlx5_nic_vport_update_roce_state(struct mlx5_core_dev * mdev,enum mlx5_vport_roce_state state)933 static int mlx5_nic_vport_update_roce_state(struct mlx5_core_dev *mdev,
934 enum mlx5_vport_roce_state state)
935 {
936 void *in;
937 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
938 int err;
939
940 in = kvzalloc(inlen, GFP_KERNEL);
941 if (!in)
942 return -ENOMEM;
943
944 MLX5_SET(modify_nic_vport_context_in, in, field_select.roce_en, 1);
945 MLX5_SET(modify_nic_vport_context_in, in, nic_vport_context.roce_en,
946 state);
947 MLX5_SET(modify_nic_vport_context_in, in, opcode,
948 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
949
950 err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
951
952 kvfree(in);
953
954 return err;
955 }
956
mlx5_nic_vport_enable_roce(struct mlx5_core_dev * mdev)957 int mlx5_nic_vport_enable_roce(struct mlx5_core_dev *mdev)
958 {
959 int err = 0;
960
961 mutex_lock(&mlx5_roce_en_lock);
962 if (!mdev->roce.roce_en)
963 err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_ENABLED);
964
965 if (!err)
966 mdev->roce.roce_en++;
967 mutex_unlock(&mlx5_roce_en_lock);
968
969 return err;
970 }
971 EXPORT_SYMBOL_GPL(mlx5_nic_vport_enable_roce);
972
mlx5_nic_vport_disable_roce(struct mlx5_core_dev * mdev)973 int mlx5_nic_vport_disable_roce(struct mlx5_core_dev *mdev)
974 {
975 int err = 0;
976
977 mutex_lock(&mlx5_roce_en_lock);
978 if (mdev->roce.roce_en) {
979 mdev->roce.roce_en--;
980 if (mdev->roce.roce_en == 0)
981 err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_DISABLED);
982
983 if (err)
984 mdev->roce.roce_en++;
985 }
986 mutex_unlock(&mlx5_roce_en_lock);
987 return err;
988 }
989 EXPORT_SYMBOL(mlx5_nic_vport_disable_roce);
990
mlx5_core_query_vport_counter(struct mlx5_core_dev * dev,u8 other_vport,int vf,u8 port_num,void * out)991 int mlx5_core_query_vport_counter(struct mlx5_core_dev *dev, u8 other_vport,
992 int vf, u8 port_num, void *out)
993 {
994 int in_sz = MLX5_ST_SZ_BYTES(query_vport_counter_in);
995 int is_group_manager;
996 void *in;
997 int err;
998
999 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
1000 in = kvzalloc(in_sz, GFP_KERNEL);
1001 if (!in) {
1002 err = -ENOMEM;
1003 return err;
1004 }
1005
1006 MLX5_SET(query_vport_counter_in, in, opcode,
1007 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1008 if (other_vport) {
1009 if (is_group_manager) {
1010 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1011 MLX5_SET(query_vport_counter_in, in, vport_number, vf + 1);
1012 } else {
1013 err = -EPERM;
1014 goto free;
1015 }
1016 }
1017 if (MLX5_CAP_GEN(dev, num_ports) == 2)
1018 MLX5_SET(query_vport_counter_in, in, port_num, port_num);
1019
1020 err = mlx5_cmd_exec_inout(dev, query_vport_counter, in, out);
1021 free:
1022 kvfree(in);
1023 return err;
1024 }
1025 EXPORT_SYMBOL_GPL(mlx5_core_query_vport_counter);
1026
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)1027 int mlx5_query_vport_down_stats(struct mlx5_core_dev *mdev, u16 vport,
1028 u8 other_vport, u64 *rx_discard_vport_down,
1029 u64 *tx_discard_vport_down)
1030 {
1031 u32 out[MLX5_ST_SZ_DW(query_vnic_env_out)] = {};
1032 u32 in[MLX5_ST_SZ_DW(query_vnic_env_in)] = {};
1033 int err;
1034
1035 MLX5_SET(query_vnic_env_in, in, opcode,
1036 MLX5_CMD_OP_QUERY_VNIC_ENV);
1037 MLX5_SET(query_vnic_env_in, in, op_mod, 0);
1038 MLX5_SET(query_vnic_env_in, in, vport_number, vport);
1039 MLX5_SET(query_vnic_env_in, in, other_vport, other_vport);
1040
1041 err = mlx5_cmd_exec_inout(mdev, query_vnic_env, in, out);
1042 if (err)
1043 return err;
1044
1045 *rx_discard_vport_down = MLX5_GET64(query_vnic_env_out, out,
1046 vport_env.receive_discard_vport_down);
1047 *tx_discard_vport_down = MLX5_GET64(query_vnic_env_out, out,
1048 vport_env.transmit_discard_vport_down);
1049 return 0;
1050 }
1051
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)1052 int mlx5_core_modify_hca_vport_context(struct mlx5_core_dev *dev,
1053 u8 other_vport, u8 port_num,
1054 int vf,
1055 struct mlx5_hca_vport_context *req)
1056 {
1057 int in_sz = MLX5_ST_SZ_BYTES(modify_hca_vport_context_in);
1058 int is_group_manager;
1059 void *ctx;
1060 void *in;
1061 int err;
1062
1063 mlx5_core_dbg(dev, "vf %d\n", vf);
1064 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
1065 in = kvzalloc(in_sz, GFP_KERNEL);
1066 if (!in)
1067 return -ENOMEM;
1068
1069 MLX5_SET(modify_hca_vport_context_in, in, opcode, MLX5_CMD_OP_MODIFY_HCA_VPORT_CONTEXT);
1070 if (other_vport) {
1071 if (is_group_manager) {
1072 MLX5_SET(modify_hca_vport_context_in, in, other_vport, 1);
1073 MLX5_SET(modify_hca_vport_context_in, in, vport_number, vf);
1074 } else {
1075 err = -EPERM;
1076 goto ex;
1077 }
1078 }
1079
1080 if (MLX5_CAP_GEN(dev, num_ports) > 1)
1081 MLX5_SET(modify_hca_vport_context_in, in, port_num, port_num);
1082
1083 ctx = MLX5_ADDR_OF(modify_hca_vport_context_in, in, hca_vport_context);
1084 MLX5_SET(hca_vport_context, ctx, field_select, req->field_select);
1085 if (req->field_select & MLX5_HCA_VPORT_SEL_STATE_POLICY)
1086 MLX5_SET(hca_vport_context, ctx, vport_state_policy,
1087 req->policy);
1088 if (req->field_select & MLX5_HCA_VPORT_SEL_PORT_GUID)
1089 MLX5_SET64(hca_vport_context, ctx, port_guid, req->port_guid);
1090 if (req->field_select & MLX5_HCA_VPORT_SEL_NODE_GUID)
1091 MLX5_SET64(hca_vport_context, ctx, node_guid, req->node_guid);
1092 MLX5_SET(hca_vport_context, ctx, cap_mask1, req->cap_mask1);
1093 MLX5_SET(hca_vport_context, ctx, cap_mask1_field_select,
1094 req->cap_mask1_perm);
1095 err = mlx5_cmd_exec_in(dev, modify_hca_vport_context, in);
1096 ex:
1097 kvfree(in);
1098 return err;
1099 }
1100 EXPORT_SYMBOL_GPL(mlx5_core_modify_hca_vport_context);
1101
mlx5_nic_vport_affiliate_multiport(struct mlx5_core_dev * master_mdev,struct mlx5_core_dev * port_mdev)1102 int mlx5_nic_vport_affiliate_multiport(struct mlx5_core_dev *master_mdev,
1103 struct mlx5_core_dev *port_mdev)
1104 {
1105 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
1106 void *in;
1107 int err;
1108
1109 in = kvzalloc(inlen, GFP_KERNEL);
1110 if (!in)
1111 return -ENOMEM;
1112
1113 err = mlx5_nic_vport_enable_roce(port_mdev);
1114 if (err)
1115 goto free;
1116
1117 MLX5_SET(modify_nic_vport_context_in, in, field_select.affiliation, 1);
1118 if (MLX5_CAP_GEN_2(master_mdev, sw_vhca_id_valid)) {
1119 MLX5_SET(modify_nic_vport_context_in, in,
1120 nic_vport_context.vhca_id_type, VHCA_ID_TYPE_SW);
1121 MLX5_SET(modify_nic_vport_context_in, in,
1122 nic_vport_context.affiliated_vhca_id,
1123 MLX5_CAP_GEN_2(master_mdev, sw_vhca_id));
1124 } else {
1125 MLX5_SET(modify_nic_vport_context_in, in,
1126 nic_vport_context.affiliated_vhca_id,
1127 MLX5_CAP_GEN(master_mdev, vhca_id));
1128 }
1129 MLX5_SET(modify_nic_vport_context_in, in,
1130 nic_vport_context.affiliation_criteria,
1131 MLX5_CAP_GEN(port_mdev, affiliate_nic_vport_criteria));
1132 MLX5_SET(modify_nic_vport_context_in, in, opcode,
1133 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
1134
1135 err = mlx5_cmd_exec_in(port_mdev, modify_nic_vport_context, in);
1136 if (err)
1137 mlx5_nic_vport_disable_roce(port_mdev);
1138
1139 free:
1140 kvfree(in);
1141 return err;
1142 }
1143 EXPORT_SYMBOL_GPL(mlx5_nic_vport_affiliate_multiport);
1144
mlx5_nic_vport_unaffiliate_multiport(struct mlx5_core_dev * port_mdev)1145 int mlx5_nic_vport_unaffiliate_multiport(struct mlx5_core_dev *port_mdev)
1146 {
1147 int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
1148 void *in;
1149 int err;
1150
1151 in = kvzalloc(inlen, GFP_KERNEL);
1152 if (!in)
1153 return -ENOMEM;
1154
1155 MLX5_SET(modify_nic_vport_context_in, in, field_select.affiliation, 1);
1156 MLX5_SET(modify_nic_vport_context_in, in,
1157 nic_vport_context.affiliated_vhca_id, 0);
1158 MLX5_SET(modify_nic_vport_context_in, in,
1159 nic_vport_context.affiliation_criteria, 0);
1160 MLX5_SET(modify_nic_vport_context_in, in, opcode,
1161 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
1162
1163 err = mlx5_cmd_exec_in(port_mdev, modify_nic_vport_context, in);
1164 if (!err)
1165 mlx5_nic_vport_disable_roce(port_mdev);
1166
1167 kvfree(in);
1168 return err;
1169 }
1170 EXPORT_SYMBOL_GPL(mlx5_nic_vport_unaffiliate_multiport);
1171
mlx5_query_nic_system_image_guid(struct mlx5_core_dev * mdev)1172 u64 mlx5_query_nic_system_image_guid(struct mlx5_core_dev *mdev)
1173 {
1174 int port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1175 u64 tmp;
1176 int err;
1177
1178 if (mdev->sys_image_guid)
1179 return mdev->sys_image_guid;
1180
1181 if (port_type_cap == MLX5_CAP_PORT_TYPE_ETH)
1182 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
1183 else
1184 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
1185
1186 mdev->sys_image_guid = err ? 0 : tmp;
1187
1188 return mdev->sys_image_guid;
1189 }
1190 EXPORT_SYMBOL_GPL(mlx5_query_nic_system_image_guid);
1191
mlx5_vport_get_other_func_cap(struct mlx5_core_dev * dev,u16 vport,void * out,u16 opmod)1192 int mlx5_vport_get_other_func_cap(struct mlx5_core_dev *dev, u16 vport, void *out,
1193 u16 opmod)
1194 {
1195 bool ec_vf_func = mlx5_core_is_ec_vf_vport(dev, vport);
1196 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1197
1198 opmod = (opmod << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1199 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1200 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1201 MLX5_SET(query_hca_cap_in, in, function_id, mlx5_vport_to_func_id(dev, vport, ec_vf_func));
1202 MLX5_SET(query_hca_cap_in, in, other_function, true);
1203 MLX5_SET(query_hca_cap_in, in, ec_vf_function, ec_vf_func);
1204 return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1205 }
1206 EXPORT_SYMBOL_GPL(mlx5_vport_get_other_func_cap);
1207
mlx5_vport_get_vhca_id(struct mlx5_core_dev * dev,u16 vport,u16 * vhca_id)1208 int mlx5_vport_get_vhca_id(struct mlx5_core_dev *dev, u16 vport, u16 *vhca_id)
1209 {
1210 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1211 void *query_ctx;
1212 void *hca_caps;
1213 int err;
1214
1215 *vhca_id = 0;
1216
1217 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1218 if (!query_ctx)
1219 return -ENOMEM;
1220
1221 err = mlx5_vport_get_other_func_general_cap(dev, vport, query_ctx);
1222 if (err)
1223 goto out_free;
1224
1225 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1226 *vhca_id = MLX5_GET(cmd_hca_cap, hca_caps, vhca_id);
1227
1228 out_free:
1229 kfree(query_ctx);
1230 return err;
1231 }
1232
mlx5_vport_set_other_func_cap(struct mlx5_core_dev * dev,const void * hca_cap,u16 vport,u16 opmod)1233 int mlx5_vport_set_other_func_cap(struct mlx5_core_dev *dev, const void *hca_cap,
1234 u16 vport, u16 opmod)
1235 {
1236 bool ec_vf_func = mlx5_core_is_ec_vf_vport(dev, vport);
1237 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
1238 void *set_hca_cap;
1239 void *set_ctx;
1240 int ret;
1241
1242 set_ctx = kzalloc(set_sz, GFP_KERNEL);
1243 if (!set_ctx)
1244 return -ENOMEM;
1245
1246 MLX5_SET(set_hca_cap_in, set_ctx, opcode, MLX5_CMD_OP_SET_HCA_CAP);
1247 MLX5_SET(set_hca_cap_in, set_ctx, op_mod, opmod << 1);
1248 set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
1249 memcpy(set_hca_cap, hca_cap, MLX5_ST_SZ_BYTES(cmd_hca_cap));
1250 MLX5_SET(set_hca_cap_in, set_ctx, function_id,
1251 mlx5_vport_to_func_id(dev, vport, ec_vf_func));
1252 MLX5_SET(set_hca_cap_in, set_ctx, other_function, true);
1253 MLX5_SET(set_hca_cap_in, set_ctx, ec_vf_function, ec_vf_func);
1254 ret = mlx5_cmd_exec_in(dev, set_hca_cap, set_ctx);
1255
1256 kfree(set_ctx);
1257 return ret;
1258 }
1259