1 /*
2 * Copyright (c) 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/etherdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include <linux/mlx5/mpfs.h>
39 #include <linux/debugfs.h>
40 #include "esw/acl/lgcy.h"
41 #include "esw/legacy.h"
42 #include "esw/qos.h"
43 #include "mlx5_core.h"
44 #include "lib/eq.h"
45 #include "eswitch.h"
46 #include "fs_core.h"
47 #include "devlink.h"
48 #include "ecpf.h"
49 #include "en/mod_hdr.h"
50
51 enum {
52 MLX5_ACTION_NONE = 0,
53 MLX5_ACTION_ADD = 1,
54 MLX5_ACTION_DEL = 2,
55 };
56
57 /* Vport UC/MC hash node */
58 struct vport_addr {
59 struct l2addr_node node;
60 u8 action;
61 u16 vport;
62 struct mlx5_flow_handle *flow_rule;
63 bool mpfs; /* UC MAC was added to MPFs */
64 /* A flag indicating that mac was added due to mc promiscuous vport */
65 bool mc_promisc;
66 };
67
mlx5_eswitch_check(const struct mlx5_core_dev * dev)68 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
69 {
70 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
71 return -EOPNOTSUPP;
72
73 if (!MLX5_ESWITCH_MANAGER(dev))
74 return -EOPNOTSUPP;
75
76 return 0;
77 }
78
mlx5_devlink_eswitch_get(struct devlink * devlink)79 struct mlx5_eswitch *mlx5_devlink_eswitch_get(struct devlink *devlink)
80 {
81 struct mlx5_core_dev *dev = devlink_priv(devlink);
82 int err;
83
84 err = mlx5_eswitch_check(dev);
85 if (err)
86 return ERR_PTR(err);
87
88 return dev->priv.eswitch;
89 }
90
91 struct mlx5_vport *__must_check
mlx5_eswitch_get_vport(struct mlx5_eswitch * esw,u16 vport_num)92 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
93 {
94 struct mlx5_vport *vport;
95
96 if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager))
97 return ERR_PTR(-EPERM);
98
99 vport = xa_load(&esw->vports, vport_num);
100 if (!vport) {
101 esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
102 return ERR_PTR(-EINVAL);
103 }
104 return vport;
105 }
106
arm_vport_context_events_cmd(struct mlx5_core_dev * dev,u16 vport,u32 events_mask)107 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
108 u32 events_mask)
109 {
110 u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
111 void *nic_vport_ctx;
112
113 MLX5_SET(modify_nic_vport_context_in, in,
114 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
115 MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
116 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
117 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
118 nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
119 in, nic_vport_context);
120
121 MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
122
123 if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
124 MLX5_SET(nic_vport_context, nic_vport_ctx,
125 event_on_uc_address_change, 1);
126 if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
127 MLX5_SET(nic_vport_context, nic_vport_ctx,
128 event_on_mc_address_change, 1);
129 if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
130 MLX5_SET(nic_vport_context, nic_vport_ctx,
131 event_on_promisc_change, 1);
132
133 return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
134 }
135
136 /* E-Switch vport context HW commands */
mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev * dev,u16 vport,bool other_vport,void * in)137 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
138 bool other_vport, void *in)
139 {
140 MLX5_SET(modify_esw_vport_context_in, in, opcode,
141 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
142 MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
143 MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
144 return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
145 }
146
modify_esw_vport_cvlan(struct mlx5_core_dev * dev,u16 vport,u16 vlan,u8 qos,u8 set_flags)147 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
148 u16 vlan, u8 qos, u8 set_flags)
149 {
150 u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
151
152 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
153 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
154 return -EOPNOTSUPP;
155
156 esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
157 vport, vlan, qos, set_flags);
158
159 if (set_flags & SET_VLAN_STRIP)
160 MLX5_SET(modify_esw_vport_context_in, in,
161 esw_vport_context.vport_cvlan_strip, 1);
162
163 if (set_flags & SET_VLAN_INSERT) {
164 if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
165 /* insert either if vlan exist in packet or not */
166 MLX5_SET(modify_esw_vport_context_in, in,
167 esw_vport_context.vport_cvlan_insert,
168 MLX5_VPORT_CVLAN_INSERT_ALWAYS);
169 } else {
170 /* insert only if no vlan in packet */
171 MLX5_SET(modify_esw_vport_context_in, in,
172 esw_vport_context.vport_cvlan_insert,
173 MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
174 }
175 MLX5_SET(modify_esw_vport_context_in, in,
176 esw_vport_context.cvlan_pcp, qos);
177 MLX5_SET(modify_esw_vport_context_in, in,
178 esw_vport_context.cvlan_id, vlan);
179 }
180
181 MLX5_SET(modify_esw_vport_context_in, in,
182 field_select.vport_cvlan_strip, 1);
183 MLX5_SET(modify_esw_vport_context_in, in,
184 field_select.vport_cvlan_insert, 1);
185
186 return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
187 }
188
189 /* E-Switch FDB */
190 static struct mlx5_flow_handle *
__esw_fdb_set_vport_rule(struct mlx5_eswitch * esw,u16 vport,bool rx_rule,u8 mac_c[ETH_ALEN],u8 mac_v[ETH_ALEN])191 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
192 u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
193 {
194 int match_header = (is_zero_ether_addr(mac_c) ? 0 :
195 MLX5_MATCH_OUTER_HEADERS);
196 struct mlx5_flow_handle *flow_rule = NULL;
197 struct mlx5_flow_act flow_act = {0};
198 struct mlx5_flow_destination dest = {};
199 struct mlx5_flow_spec *spec;
200 void *mv_misc = NULL;
201 void *mc_misc = NULL;
202 u8 *dmac_v = NULL;
203 u8 *dmac_c = NULL;
204
205 if (rx_rule)
206 match_header |= MLX5_MATCH_MISC_PARAMETERS;
207
208 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
209 if (!spec)
210 return NULL;
211
212 dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
213 outer_headers.dmac_47_16);
214 dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
215 outer_headers.dmac_47_16);
216
217 if (match_header & MLX5_MATCH_OUTER_HEADERS) {
218 ether_addr_copy(dmac_v, mac_v);
219 ether_addr_copy(dmac_c, mac_c);
220 }
221
222 if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
223 mv_misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
224 misc_parameters);
225 mc_misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
226 misc_parameters);
227 MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
228 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
229 }
230
231 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
232 dest.vport.num = vport;
233
234 esw_debug(esw->dev,
235 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
236 dmac_v, dmac_c, vport);
237 spec->match_criteria_enable = match_header;
238 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
239 flow_rule =
240 mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
241 &flow_act, &dest, 1);
242 if (IS_ERR(flow_rule)) {
243 esw_warn(esw->dev,
244 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
245 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
246 flow_rule = NULL;
247 }
248
249 kvfree(spec);
250 return flow_rule;
251 }
252
253 static struct mlx5_flow_handle *
esw_fdb_set_vport_rule(struct mlx5_eswitch * esw,u8 mac[ETH_ALEN],u16 vport)254 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
255 {
256 u8 mac_c[ETH_ALEN];
257
258 eth_broadcast_addr(mac_c);
259 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
260 }
261
262 static struct mlx5_flow_handle *
esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch * esw,u16 vport)263 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
264 {
265 u8 mac_c[ETH_ALEN];
266 u8 mac_v[ETH_ALEN];
267
268 eth_zero_addr(mac_c);
269 eth_zero_addr(mac_v);
270 mac_c[0] = 0x01;
271 mac_v[0] = 0x01;
272 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
273 }
274
275 static struct mlx5_flow_handle *
esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch * esw,u16 vport)276 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
277 {
278 u8 mac_c[ETH_ALEN];
279 u8 mac_v[ETH_ALEN];
280
281 eth_zero_addr(mac_c);
282 eth_zero_addr(mac_v);
283 return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
284 }
285
286 /* E-Switch vport UC/MC lists management */
287 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
288 struct vport_addr *vaddr);
289
esw_add_uc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)290 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
291 {
292 u8 *mac = vaddr->node.addr;
293 u16 vport = vaddr->vport;
294 int err;
295
296 /* Skip mlx5_mpfs_add_mac for eswitch_managers,
297 * it is already done by its netdev in mlx5e_execute_l2_action
298 */
299 if (mlx5_esw_is_manager_vport(esw, vport))
300 goto fdb_add;
301
302 err = mlx5_mpfs_add_mac(esw->dev, mac);
303 if (err) {
304 esw_warn(esw->dev,
305 "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
306 mac, vport, err);
307 return err;
308 }
309 vaddr->mpfs = true;
310
311 fdb_add:
312 /* SRIOV is enabled: Forward UC MAC to vport */
313 if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY)
314 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
315
316 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
317 vport, mac, vaddr->flow_rule);
318
319 return 0;
320 }
321
esw_del_uc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)322 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
323 {
324 u8 *mac = vaddr->node.addr;
325 u16 vport = vaddr->vport;
326 int err = 0;
327
328 /* Skip mlx5_mpfs_del_mac for eswitch managers,
329 * it is already done by its netdev in mlx5e_execute_l2_action
330 */
331 if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
332 goto fdb_del;
333
334 err = mlx5_mpfs_del_mac(esw->dev, mac);
335 if (err)
336 esw_warn(esw->dev,
337 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
338 mac, vport, err);
339 vaddr->mpfs = false;
340
341 fdb_del:
342 if (vaddr->flow_rule)
343 mlx5_del_flow_rules(vaddr->flow_rule);
344 vaddr->flow_rule = NULL;
345
346 return 0;
347 }
348
update_allmulti_vports(struct mlx5_eswitch * esw,struct vport_addr * vaddr,struct esw_mc_addr * esw_mc)349 static void update_allmulti_vports(struct mlx5_eswitch *esw,
350 struct vport_addr *vaddr,
351 struct esw_mc_addr *esw_mc)
352 {
353 u8 *mac = vaddr->node.addr;
354 struct mlx5_vport *vport;
355 unsigned long i;
356 u16 vport_num;
357
358 mlx5_esw_for_each_vport(esw, i, vport) {
359 struct hlist_head *vport_hash = vport->mc_list;
360 struct vport_addr *iter_vaddr =
361 l2addr_hash_find(vport_hash,
362 mac,
363 struct vport_addr);
364 vport_num = vport->vport;
365 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
366 vaddr->vport == vport_num)
367 continue;
368 switch (vaddr->action) {
369 case MLX5_ACTION_ADD:
370 if (iter_vaddr)
371 continue;
372 iter_vaddr = l2addr_hash_add(vport_hash, mac,
373 struct vport_addr,
374 GFP_KERNEL);
375 if (!iter_vaddr) {
376 esw_warn(esw->dev,
377 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
378 mac, vport_num);
379 continue;
380 }
381 iter_vaddr->vport = vport_num;
382 iter_vaddr->flow_rule =
383 esw_fdb_set_vport_rule(esw,
384 mac,
385 vport_num);
386 iter_vaddr->mc_promisc = true;
387 break;
388 case MLX5_ACTION_DEL:
389 if (!iter_vaddr)
390 continue;
391 mlx5_del_flow_rules(iter_vaddr->flow_rule);
392 l2addr_hash_del(iter_vaddr);
393 break;
394 }
395 }
396 }
397
esw_add_mc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)398 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
399 {
400 struct hlist_head *hash = esw->mc_table;
401 struct esw_mc_addr *esw_mc;
402 u8 *mac = vaddr->node.addr;
403 u16 vport = vaddr->vport;
404
405 if (!esw->fdb_table.legacy.fdb)
406 return 0;
407
408 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
409 if (esw_mc)
410 goto add;
411
412 esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
413 if (!esw_mc)
414 return -ENOMEM;
415
416 esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
417 esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
418
419 /* Add this multicast mac to all the mc promiscuous vports */
420 update_allmulti_vports(esw, vaddr, esw_mc);
421
422 add:
423 /* If the multicast mac is added as a result of mc promiscuous vport,
424 * don't increment the multicast ref count
425 */
426 if (!vaddr->mc_promisc)
427 esw_mc->refcnt++;
428
429 /* Forward MC MAC to vport */
430 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
431 esw_debug(esw->dev,
432 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
433 vport, mac, vaddr->flow_rule,
434 esw_mc->refcnt, esw_mc->uplink_rule);
435 return 0;
436 }
437
esw_del_mc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)438 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
439 {
440 struct hlist_head *hash = esw->mc_table;
441 struct esw_mc_addr *esw_mc;
442 u8 *mac = vaddr->node.addr;
443 u16 vport = vaddr->vport;
444
445 if (!esw->fdb_table.legacy.fdb)
446 return 0;
447
448 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
449 if (!esw_mc) {
450 esw_warn(esw->dev,
451 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
452 mac, vport);
453 return -EINVAL;
454 }
455 esw_debug(esw->dev,
456 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
457 vport, mac, vaddr->flow_rule, esw_mc->refcnt,
458 esw_mc->uplink_rule);
459
460 if (vaddr->flow_rule)
461 mlx5_del_flow_rules(vaddr->flow_rule);
462 vaddr->flow_rule = NULL;
463
464 /* If the multicast mac is added as a result of mc promiscuous vport,
465 * don't decrement the multicast ref count.
466 */
467 if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
468 return 0;
469
470 /* Remove this multicast mac from all the mc promiscuous vports */
471 update_allmulti_vports(esw, vaddr, esw_mc);
472
473 if (esw_mc->uplink_rule)
474 mlx5_del_flow_rules(esw_mc->uplink_rule);
475
476 l2addr_hash_del(esw_mc);
477 return 0;
478 }
479
480 /* Apply vport UC/MC list to HW l2 table and FDB table */
esw_apply_vport_addr_list(struct mlx5_eswitch * esw,struct mlx5_vport * vport,int list_type)481 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
482 struct mlx5_vport *vport, int list_type)
483 {
484 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
485 vport_addr_action vport_addr_add;
486 vport_addr_action vport_addr_del;
487 struct vport_addr *addr;
488 struct l2addr_node *node;
489 struct hlist_head *hash;
490 struct hlist_node *tmp;
491 int hi;
492
493 vport_addr_add = is_uc ? esw_add_uc_addr :
494 esw_add_mc_addr;
495 vport_addr_del = is_uc ? esw_del_uc_addr :
496 esw_del_mc_addr;
497
498 hash = is_uc ? vport->uc_list : vport->mc_list;
499 for_each_l2hash_node(node, tmp, hash, hi) {
500 addr = container_of(node, struct vport_addr, node);
501 switch (addr->action) {
502 case MLX5_ACTION_ADD:
503 vport_addr_add(esw, addr);
504 addr->action = MLX5_ACTION_NONE;
505 break;
506 case MLX5_ACTION_DEL:
507 vport_addr_del(esw, addr);
508 l2addr_hash_del(addr);
509 break;
510 }
511 }
512 }
513
514 /* Sync vport UC/MC list from vport context */
esw_update_vport_addr_list(struct mlx5_eswitch * esw,struct mlx5_vport * vport,int list_type)515 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
516 struct mlx5_vport *vport, int list_type)
517 {
518 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
519 u8 (*mac_list)[ETH_ALEN];
520 struct l2addr_node *node;
521 struct vport_addr *addr;
522 struct hlist_head *hash;
523 struct hlist_node *tmp;
524 int size;
525 int err;
526 int hi;
527 int i;
528
529 size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
530 MLX5_MAX_MC_PER_VPORT(esw->dev);
531
532 mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
533 if (!mac_list)
534 return;
535
536 hash = is_uc ? vport->uc_list : vport->mc_list;
537
538 for_each_l2hash_node(node, tmp, hash, hi) {
539 addr = container_of(node, struct vport_addr, node);
540 addr->action = MLX5_ACTION_DEL;
541 }
542
543 if (!vport->enabled)
544 goto out;
545
546 err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
547 mac_list, &size);
548 if (err)
549 goto out;
550 esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
551 vport->vport, is_uc ? "UC" : "MC", size);
552
553 for (i = 0; i < size; i++) {
554 if (is_uc && !is_valid_ether_addr(mac_list[i]))
555 continue;
556
557 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
558 continue;
559
560 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
561 if (addr) {
562 addr->action = MLX5_ACTION_NONE;
563 /* If this mac was previously added because of allmulti
564 * promiscuous rx mode, its now converted to be original
565 * vport mac.
566 */
567 if (addr->mc_promisc) {
568 struct esw_mc_addr *esw_mc =
569 l2addr_hash_find(esw->mc_table,
570 mac_list[i],
571 struct esw_mc_addr);
572 if (!esw_mc) {
573 esw_warn(esw->dev,
574 "Failed to MAC(%pM) in mcast DB\n",
575 mac_list[i]);
576 continue;
577 }
578 esw_mc->refcnt++;
579 addr->mc_promisc = false;
580 }
581 continue;
582 }
583
584 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
585 GFP_KERNEL);
586 if (!addr) {
587 esw_warn(esw->dev,
588 "Failed to add MAC(%pM) to vport[%d] DB\n",
589 mac_list[i], vport->vport);
590 continue;
591 }
592 addr->vport = vport->vport;
593 addr->action = MLX5_ACTION_ADD;
594 }
595 out:
596 kfree(mac_list);
597 }
598
599 /* Sync vport UC/MC list from vport context
600 * Must be called after esw_update_vport_addr_list
601 */
esw_update_vport_mc_promisc(struct mlx5_eswitch * esw,struct mlx5_vport * vport)602 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
603 struct mlx5_vport *vport)
604 {
605 struct l2addr_node *node;
606 struct vport_addr *addr;
607 struct hlist_head *hash;
608 struct hlist_node *tmp;
609 int hi;
610
611 hash = vport->mc_list;
612
613 for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
614 u8 *mac = node->addr;
615
616 addr = l2addr_hash_find(hash, mac, struct vport_addr);
617 if (addr) {
618 if (addr->action == MLX5_ACTION_DEL)
619 addr->action = MLX5_ACTION_NONE;
620 continue;
621 }
622 addr = l2addr_hash_add(hash, mac, struct vport_addr,
623 GFP_KERNEL);
624 if (!addr) {
625 esw_warn(esw->dev,
626 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
627 mac, vport->vport);
628 continue;
629 }
630 addr->vport = vport->vport;
631 addr->action = MLX5_ACTION_ADD;
632 addr->mc_promisc = true;
633 }
634 }
635
636 /* Apply vport rx mode to HW FDB table */
esw_apply_vport_rx_mode(struct mlx5_eswitch * esw,struct mlx5_vport * vport,bool promisc,bool mc_promisc)637 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
638 struct mlx5_vport *vport,
639 bool promisc, bool mc_promisc)
640 {
641 struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
642
643 if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
644 goto promisc;
645
646 if (mc_promisc) {
647 vport->allmulti_rule =
648 esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
649 if (!allmulti_addr->uplink_rule)
650 allmulti_addr->uplink_rule =
651 esw_fdb_set_vport_allmulti_rule(esw,
652 MLX5_VPORT_UPLINK);
653 allmulti_addr->refcnt++;
654 } else if (vport->allmulti_rule) {
655 mlx5_del_flow_rules(vport->allmulti_rule);
656 vport->allmulti_rule = NULL;
657
658 if (--allmulti_addr->refcnt > 0)
659 goto promisc;
660
661 if (allmulti_addr->uplink_rule)
662 mlx5_del_flow_rules(allmulti_addr->uplink_rule);
663 allmulti_addr->uplink_rule = NULL;
664 }
665
666 promisc:
667 if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
668 return;
669
670 if (promisc) {
671 vport->promisc_rule =
672 esw_fdb_set_vport_promisc_rule(esw, vport->vport);
673 } else if (vport->promisc_rule) {
674 mlx5_del_flow_rules(vport->promisc_rule);
675 vport->promisc_rule = NULL;
676 }
677 }
678
679 /* Sync vport rx mode from vport context */
esw_update_vport_rx_mode(struct mlx5_eswitch * esw,struct mlx5_vport * vport)680 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
681 struct mlx5_vport *vport)
682 {
683 int promisc_all = 0;
684 int promisc_uc = 0;
685 int promisc_mc = 0;
686 int err;
687
688 err = mlx5_query_nic_vport_promisc(esw->dev,
689 vport->vport,
690 &promisc_uc,
691 &promisc_mc,
692 &promisc_all);
693 if (err)
694 return;
695 esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
696 vport->vport, promisc_all, promisc_mc);
697
698 if (!vport->info.trusted || !vport->enabled) {
699 promisc_uc = 0;
700 promisc_mc = 0;
701 promisc_all = 0;
702 }
703
704 esw_apply_vport_rx_mode(esw, vport, promisc_all,
705 (promisc_all || promisc_mc));
706 }
707
esw_vport_change_handle_locked(struct mlx5_vport * vport)708 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
709 {
710 struct mlx5_core_dev *dev = vport->dev;
711 struct mlx5_eswitch *esw = dev->priv.eswitch;
712 u8 mac[ETH_ALEN];
713
714 mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
715 esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
716 vport->vport, mac);
717
718 if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
719 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
720 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
721 }
722
723 if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
724 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
725
726 if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
727 esw_update_vport_rx_mode(esw, vport);
728 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
729 esw_update_vport_mc_promisc(esw, vport);
730 }
731
732 if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
733 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
734
735 esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
736 if (vport->enabled)
737 arm_vport_context_events_cmd(dev, vport->vport,
738 vport->enabled_events);
739 }
740
esw_vport_change_handler(struct work_struct * work)741 static void esw_vport_change_handler(struct work_struct *work)
742 {
743 struct mlx5_vport *vport =
744 container_of(work, struct mlx5_vport, vport_change_handler);
745 struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
746
747 mutex_lock(&esw->state_lock);
748 esw_vport_change_handle_locked(vport);
749 mutex_unlock(&esw->state_lock);
750 }
751
node_guid_gen_from_mac(u64 * node_guid,const u8 * mac)752 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
753 {
754 ((u8 *)node_guid)[7] = mac[0];
755 ((u8 *)node_guid)[6] = mac[1];
756 ((u8 *)node_guid)[5] = mac[2];
757 ((u8 *)node_guid)[4] = 0xff;
758 ((u8 *)node_guid)[3] = 0xfe;
759 ((u8 *)node_guid)[2] = mac[3];
760 ((u8 *)node_guid)[1] = mac[4];
761 ((u8 *)node_guid)[0] = mac[5];
762 }
763
esw_vport_setup_acl(struct mlx5_eswitch * esw,struct mlx5_vport * vport)764 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
765 struct mlx5_vport *vport)
766 {
767 if (esw->mode == MLX5_ESWITCH_LEGACY)
768 return esw_legacy_vport_acl_setup(esw, vport);
769 else
770 return esw_vport_create_offloads_acl_tables(esw, vport);
771 }
772
esw_vport_cleanup_acl(struct mlx5_eswitch * esw,struct mlx5_vport * vport)773 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
774 struct mlx5_vport *vport)
775 {
776 if (esw->mode == MLX5_ESWITCH_LEGACY)
777 esw_legacy_vport_acl_cleanup(esw, vport);
778 else
779 esw_vport_destroy_offloads_acl_tables(esw, vport);
780 }
781
esw_vport_setup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)782 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
783 {
784 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
785 u16 vport_num = vport->vport;
786 int flags;
787 int err;
788
789 err = esw_vport_setup_acl(esw, vport);
790 if (err)
791 return err;
792
793 if (mlx5_esw_is_manager_vport(esw, vport_num))
794 return 0;
795
796 mlx5_modify_vport_admin_state(esw->dev,
797 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
798 vport_num, 1,
799 vport->info.link_state);
800
801 /* Host PF has its own mac/guid. */
802 if (vport_num) {
803 mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
804 vport->info.mac);
805 mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
806 vport->info.node_guid);
807 }
808
809 flags = (vport->info.vlan || vport->info.qos) ?
810 SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
811 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
812 modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
813 vport->info.qos, flags);
814
815 return 0;
816 }
817
818 /* Don't cleanup vport->info, it's needed to restore vport configuration */
esw_vport_cleanup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)819 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
820 {
821 u16 vport_num = vport->vport;
822
823 if (!mlx5_esw_is_manager_vport(esw, vport_num))
824 mlx5_modify_vport_admin_state(esw->dev,
825 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
826 vport_num, 1,
827 MLX5_VPORT_ADMIN_STATE_DOWN);
828
829 mlx5_esw_qos_vport_disable(esw, vport);
830 esw_vport_cleanup_acl(esw, vport);
831 }
832
mlx5_esw_vport_enable(struct mlx5_eswitch * esw,u16 vport_num,enum mlx5_eswitch_vport_event enabled_events)833 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, u16 vport_num,
834 enum mlx5_eswitch_vport_event enabled_events)
835 {
836 struct mlx5_vport *vport;
837 int ret;
838
839 vport = mlx5_eswitch_get_vport(esw, vport_num);
840 if (IS_ERR(vport))
841 return PTR_ERR(vport);
842
843 mutex_lock(&esw->state_lock);
844 WARN_ON(vport->enabled);
845
846 esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
847
848 ret = esw_vport_setup(esw, vport);
849 if (ret)
850 goto done;
851
852 /* Sync with current vport context */
853 vport->enabled_events = enabled_events;
854 vport->enabled = true;
855
856 /* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
857 * in smartNIC as it's a vport group manager.
858 */
859 if (mlx5_esw_is_manager_vport(esw, vport_num) ||
860 (!vport_num && mlx5_core_is_ecpf(esw->dev)))
861 vport->info.trusted = true;
862
863 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
864 MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
865 ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
866 if (ret)
867 goto err_vhca_mapping;
868 }
869
870 /* External controller host PF has factory programmed MAC.
871 * Read it from the device.
872 */
873 if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
874 mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
875
876 esw_vport_change_handle_locked(vport);
877
878 esw->enabled_vports++;
879 esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
880 done:
881 mutex_unlock(&esw->state_lock);
882 return ret;
883
884 err_vhca_mapping:
885 esw_vport_cleanup(esw, vport);
886 mutex_unlock(&esw->state_lock);
887 return ret;
888 }
889
mlx5_esw_vport_disable(struct mlx5_eswitch * esw,u16 vport_num)890 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
891 {
892 struct mlx5_vport *vport;
893
894 vport = mlx5_eswitch_get_vport(esw, vport_num);
895 if (IS_ERR(vport))
896 return;
897
898 mutex_lock(&esw->state_lock);
899 if (!vport->enabled)
900 goto done;
901
902 esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
903 /* Mark this vport as disabled to discard new events */
904 vport->enabled = false;
905
906 /* Disable events from this vport */
907 arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
908
909 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
910 MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
911 mlx5_esw_vport_vhca_id_clear(esw, vport_num);
912
913 /* We don't assume VFs will cleanup after themselves.
914 * Calling vport change handler while vport is disabled will cleanup
915 * the vport resources.
916 */
917 esw_vport_change_handle_locked(vport);
918 vport->enabled_events = 0;
919 esw_vport_cleanup(esw, vport);
920 esw->enabled_vports--;
921
922 done:
923 mutex_unlock(&esw->state_lock);
924 }
925
eswitch_vport_event(struct notifier_block * nb,unsigned long type,void * data)926 static int eswitch_vport_event(struct notifier_block *nb,
927 unsigned long type, void *data)
928 {
929 struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
930 struct mlx5_eqe *eqe = data;
931 struct mlx5_vport *vport;
932 u16 vport_num;
933
934 vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
935 vport = mlx5_eswitch_get_vport(esw, vport_num);
936 if (!IS_ERR(vport))
937 queue_work(esw->work_queue, &vport->vport_change_handler);
938 return NOTIFY_OK;
939 }
940
941 /**
942 * mlx5_esw_query_functions - Returns raw output about functions state
943 * @dev: Pointer to device to query
944 *
945 * mlx5_esw_query_functions() allocates and returns functions changed
946 * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
947 * Caller must free the memory using kvfree() when valid pointer is returned.
948 */
mlx5_esw_query_functions(struct mlx5_core_dev * dev)949 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
950 {
951 int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
952 u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
953 u32 *out;
954 int err;
955
956 out = kvzalloc(outlen, GFP_KERNEL);
957 if (!out)
958 return ERR_PTR(-ENOMEM);
959
960 MLX5_SET(query_esw_functions_in, in, opcode,
961 MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
962
963 err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
964 if (!err)
965 return out;
966
967 kvfree(out);
968 return ERR_PTR(err);
969 }
970
mlx5_eswitch_event_handlers_register(struct mlx5_eswitch * esw)971 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
972 {
973 MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
974 mlx5_eq_notifier_register(esw->dev, &esw->nb);
975
976 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
977 MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
978 ESW_FUNCTIONS_CHANGED);
979 mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
980 }
981 }
982
mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch * esw)983 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
984 {
985 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
986 mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
987
988 mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
989
990 flush_workqueue(esw->work_queue);
991 }
992
mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch * esw)993 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
994 {
995 struct mlx5_vport *vport;
996 unsigned long i;
997
998 mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
999 memset(&vport->qos, 0, sizeof(vport->qos));
1000 memset(&vport->info, 0, sizeof(vport->info));
1001 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1002 }
1003 }
1004
1005 /* Public E-Switch API */
mlx5_eswitch_load_vport(struct mlx5_eswitch * esw,u16 vport_num,enum mlx5_eswitch_vport_event enabled_events)1006 int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, u16 vport_num,
1007 enum mlx5_eswitch_vport_event enabled_events)
1008 {
1009 int err;
1010
1011 err = mlx5_esw_vport_enable(esw, vport_num, enabled_events);
1012 if (err)
1013 return err;
1014
1015 mlx5_esw_vport_debugfs_create(esw, vport_num, false, 0);
1016 err = esw_offloads_load_rep(esw, vport_num);
1017 if (err)
1018 goto err_rep;
1019
1020 return err;
1021
1022 err_rep:
1023 mlx5_esw_vport_debugfs_destroy(esw, vport_num);
1024 mlx5_esw_vport_disable(esw, vport_num);
1025 return err;
1026 }
1027
mlx5_eswitch_unload_vport(struct mlx5_eswitch * esw,u16 vport_num)1028 void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, u16 vport_num)
1029 {
1030 esw_offloads_unload_rep(esw, vport_num);
1031 mlx5_esw_vport_debugfs_destroy(esw, vport_num);
1032 mlx5_esw_vport_disable(esw, vport_num);
1033 }
1034
mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch * esw,u16 num_vfs)1035 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1036 {
1037 struct mlx5_vport *vport;
1038 unsigned long i;
1039
1040 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1041 if (!vport->enabled)
1042 continue;
1043 mlx5_eswitch_unload_vport(esw, vport->vport);
1044 }
1045 }
1046
mlx5_eswitch_load_vf_vports(struct mlx5_eswitch * esw,u16 num_vfs,enum mlx5_eswitch_vport_event enabled_events)1047 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1048 enum mlx5_eswitch_vport_event enabled_events)
1049 {
1050 struct mlx5_vport *vport;
1051 unsigned long i;
1052 int err;
1053
1054 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1055 err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1056 if (err)
1057 goto vf_err;
1058 }
1059
1060 return 0;
1061
1062 vf_err:
1063 mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1064 return err;
1065 }
1066
host_pf_enable_hca(struct mlx5_core_dev * dev)1067 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1068 {
1069 if (!mlx5_core_is_ecpf(dev))
1070 return 0;
1071
1072 /* Once vport and representor are ready, take out the external host PF
1073 * out of initializing state. Enabling HCA clears the iser->initializing
1074 * bit and host PF driver loading can progress.
1075 */
1076 return mlx5_cmd_host_pf_enable_hca(dev);
1077 }
1078
host_pf_disable_hca(struct mlx5_core_dev * dev)1079 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1080 {
1081 if (!mlx5_core_is_ecpf(dev))
1082 return;
1083
1084 mlx5_cmd_host_pf_disable_hca(dev);
1085 }
1086
1087 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1088 * whichever are present on the eswitch.
1089 */
1090 int
mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch * esw,enum mlx5_eswitch_vport_event enabled_events)1091 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1092 enum mlx5_eswitch_vport_event enabled_events)
1093 {
1094 int ret;
1095
1096 /* Enable PF vport */
1097 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_PF, enabled_events);
1098 if (ret)
1099 return ret;
1100
1101 /* Enable external host PF HCA */
1102 ret = host_pf_enable_hca(esw->dev);
1103 if (ret)
1104 goto pf_hca_err;
1105
1106 /* Enable ECPF vport */
1107 if (mlx5_ecpf_vport_exists(esw->dev)) {
1108 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1109 if (ret)
1110 goto ecpf_err;
1111 }
1112
1113 /* Enable VF vports */
1114 ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1115 enabled_events);
1116 if (ret)
1117 goto vf_err;
1118 return 0;
1119
1120 vf_err:
1121 if (mlx5_ecpf_vport_exists(esw->dev))
1122 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1123 ecpf_err:
1124 host_pf_disable_hca(esw->dev);
1125 pf_hca_err:
1126 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1127 return ret;
1128 }
1129
1130 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1131 * whichever are previously enabled on the eswitch.
1132 */
mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch * esw)1133 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1134 {
1135 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1136
1137 if (mlx5_ecpf_vport_exists(esw->dev))
1138 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1139
1140 host_pf_disable_hca(esw->dev);
1141 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1142 }
1143
mlx5_eswitch_get_devlink_param(struct mlx5_eswitch * esw)1144 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1145 {
1146 struct devlink *devlink = priv_to_devlink(esw->dev);
1147 union devlink_param_value val;
1148 int err;
1149
1150 err = devlink_param_driverinit_value_get(devlink,
1151 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1152 &val);
1153 if (!err) {
1154 esw->params.large_group_num = val.vu32;
1155 } else {
1156 esw_warn(esw->dev,
1157 "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1158 ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1159 esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1160 }
1161 }
1162
1163 static void
mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch * esw,int num_vfs)1164 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1165 {
1166 const u32 *out;
1167
1168 if (num_vfs < 0)
1169 return;
1170
1171 if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1172 esw->esw_funcs.num_vfs = num_vfs;
1173 return;
1174 }
1175
1176 out = mlx5_esw_query_functions(esw->dev);
1177 if (IS_ERR(out))
1178 return;
1179
1180 esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1181 host_params_context.host_num_of_vfs);
1182 kvfree(out);
1183 }
1184
mlx5_esw_mode_change_notify(struct mlx5_eswitch * esw,u16 mode)1185 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1186 {
1187 struct mlx5_esw_event_info info = {};
1188
1189 info.new_mode = mode;
1190
1191 blocking_notifier_call_chain(&esw->n_head, 0, &info);
1192 }
1193
mlx5_esw_acls_ns_init(struct mlx5_eswitch * esw)1194 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1195 {
1196 struct mlx5_core_dev *dev = esw->dev;
1197 int total_vports;
1198 int err;
1199
1200 if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1201 return 0;
1202
1203 total_vports = mlx5_eswitch_get_total_vports(dev);
1204
1205 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1206 err = mlx5_fs_egress_acls_init(dev, total_vports);
1207 if (err)
1208 return err;
1209 } else {
1210 esw_warn(dev, "engress ACL is not supported by FW\n");
1211 }
1212
1213 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1214 err = mlx5_fs_ingress_acls_init(dev, total_vports);
1215 if (err)
1216 goto err;
1217 } else {
1218 esw_warn(dev, "ingress ACL is not supported by FW\n");
1219 }
1220 esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1221 return 0;
1222
1223 err:
1224 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1225 mlx5_fs_egress_acls_cleanup(dev);
1226 return err;
1227 }
1228
mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch * esw)1229 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1230 {
1231 struct mlx5_core_dev *dev = esw->dev;
1232
1233 esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1234 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1235 mlx5_fs_ingress_acls_cleanup(dev);
1236 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1237 mlx5_fs_egress_acls_cleanup(dev);
1238 }
1239
1240 /**
1241 * mlx5_eswitch_enable_locked - Enable eswitch
1242 * @esw: Pointer to eswitch
1243 * @num_vfs: Enable eswitch for given number of VFs. This is optional.
1244 * Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1245 * Caller should pass num_vfs > 0 when enabling eswitch for
1246 * vf vports. Caller should pass num_vfs = 0, when eswitch
1247 * is enabled without sriov VFs or when caller
1248 * is unaware of the sriov state of the host PF on ECPF based
1249 * eswitch. Caller should pass < 0 when num_vfs should be
1250 * completely ignored. This is typically the case when eswitch
1251 * is enabled without sriov regardless of PF/ECPF system.
1252 * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1253 * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1254 * It returns 0 on success or error code on failure.
1255 */
mlx5_eswitch_enable_locked(struct mlx5_eswitch * esw,int num_vfs)1256 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1257 {
1258 int err;
1259
1260 lockdep_assert_held(&esw->mode_lock);
1261
1262 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1263 esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1264 return -EOPNOTSUPP;
1265 }
1266
1267 mlx5_eswitch_get_devlink_param(esw);
1268
1269 err = mlx5_esw_acls_ns_init(esw);
1270 if (err)
1271 return err;
1272
1273 mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1274
1275 if (esw->mode == MLX5_ESWITCH_LEGACY) {
1276 err = esw_legacy_enable(esw);
1277 } else {
1278 mlx5_rescan_drivers(esw->dev);
1279 err = esw_offloads_enable(esw);
1280 }
1281
1282 if (err)
1283 goto abort;
1284
1285 esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1286
1287 mlx5_eswitch_event_handlers_register(esw);
1288
1289 esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), active vports(%d)\n",
1290 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1291 esw->esw_funcs.num_vfs, esw->enabled_vports);
1292
1293 mlx5_esw_mode_change_notify(esw, esw->mode);
1294
1295 return 0;
1296
1297 abort:
1298 mlx5_esw_acls_ns_cleanup(esw);
1299 return err;
1300 }
1301
1302 /**
1303 * mlx5_eswitch_enable - Enable eswitch
1304 * @esw: Pointer to eswitch
1305 * @num_vfs: Enable eswitch switch for given number of VFs.
1306 * Caller must pass num_vfs > 0 when enabling eswitch for
1307 * vf vports.
1308 * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1309 */
mlx5_eswitch_enable(struct mlx5_eswitch * esw,int num_vfs)1310 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1311 {
1312 bool toggle_lag;
1313 int ret;
1314
1315 if (!mlx5_esw_allowed(esw))
1316 return 0;
1317
1318 devl_assert_locked(priv_to_devlink(esw->dev));
1319
1320 toggle_lag = !mlx5_esw_is_fdb_created(esw);
1321
1322 if (toggle_lag)
1323 mlx5_lag_disable_change(esw->dev);
1324
1325 down_write(&esw->mode_lock);
1326 if (!mlx5_esw_is_fdb_created(esw)) {
1327 ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1328 } else {
1329 enum mlx5_eswitch_vport_event vport_events;
1330
1331 vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1332 MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1333 ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1334 if (!ret)
1335 esw->esw_funcs.num_vfs = num_vfs;
1336 }
1337 up_write(&esw->mode_lock);
1338
1339 if (toggle_lag)
1340 mlx5_lag_enable_change(esw->dev);
1341
1342 return ret;
1343 }
1344
1345 /* When disabling sriov, free driver level resources. */
mlx5_eswitch_disable_sriov(struct mlx5_eswitch * esw,bool clear_vf)1346 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1347 {
1348 if (!mlx5_esw_allowed(esw))
1349 return;
1350
1351 devl_assert_locked(priv_to_devlink(esw->dev));
1352 down_write(&esw->mode_lock);
1353 /* If driver is unloaded, this function is called twice by remove_one()
1354 * and mlx5_unload(). Prevent the second call.
1355 */
1356 if (!esw->esw_funcs.num_vfs && !clear_vf)
1357 goto unlock;
1358
1359 esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), active vports(%d)\n",
1360 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1361 esw->esw_funcs.num_vfs, esw->enabled_vports);
1362
1363 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1364 if (clear_vf)
1365 mlx5_eswitch_clear_vf_vports_info(esw);
1366 /* If disabling sriov in switchdev mode, free meta rules here
1367 * because it depends on num_vfs.
1368 */
1369 if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1370 struct devlink *devlink = priv_to_devlink(esw->dev);
1371
1372 devl_rate_nodes_destroy(devlink);
1373 }
1374 /* Destroy legacy fdb when disabling sriov in legacy mode. */
1375 if (esw->mode == MLX5_ESWITCH_LEGACY)
1376 mlx5_eswitch_disable_locked(esw);
1377
1378 esw->esw_funcs.num_vfs = 0;
1379
1380 unlock:
1381 up_write(&esw->mode_lock);
1382 }
1383
1384 /* Free resources for corresponding eswitch mode. It is called by devlink
1385 * when changing eswitch mode or modprobe when unloading driver.
1386 */
mlx5_eswitch_disable_locked(struct mlx5_eswitch * esw)1387 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1388 {
1389 struct devlink *devlink = priv_to_devlink(esw->dev);
1390
1391 /* Notify eswitch users that it is exiting from current mode.
1392 * So that it can do necessary cleanup before the eswitch is disabled.
1393 */
1394 mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1395
1396 mlx5_eswitch_event_handlers_unregister(esw);
1397
1398 esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), active vports(%d)\n",
1399 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1400 esw->esw_funcs.num_vfs, esw->enabled_vports);
1401
1402 if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1403 esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1404 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1405 esw_offloads_disable(esw);
1406 else if (esw->mode == MLX5_ESWITCH_LEGACY)
1407 esw_legacy_disable(esw);
1408 mlx5_esw_acls_ns_cleanup(esw);
1409 }
1410
1411 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1412 devl_rate_nodes_destroy(devlink);
1413 }
1414
mlx5_eswitch_disable(struct mlx5_eswitch * esw)1415 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1416 {
1417 if (!mlx5_esw_allowed(esw))
1418 return;
1419
1420 devl_assert_locked(priv_to_devlink(esw->dev));
1421 mlx5_lag_disable_change(esw->dev);
1422 down_write(&esw->mode_lock);
1423 mlx5_eswitch_disable_locked(esw);
1424 esw->mode = MLX5_ESWITCH_LEGACY;
1425 up_write(&esw->mode_lock);
1426 mlx5_lag_enable_change(esw->dev);
1427 }
1428
mlx5_query_hca_cap_host_pf(struct mlx5_core_dev * dev,void * out)1429 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1430 {
1431 u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1432 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1433
1434 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1435 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1436 MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1437 MLX5_SET(query_hca_cap_in, in, other_function, true);
1438 return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1439 }
1440
mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev * dev,u16 * max_sfs,u16 * sf_base_id)1441 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1442
1443 {
1444 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1445 void *query_ctx;
1446 void *hca_caps;
1447 int err;
1448
1449 if (!mlx5_core_is_ecpf(dev)) {
1450 *max_sfs = 0;
1451 return 0;
1452 }
1453
1454 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1455 if (!query_ctx)
1456 return -ENOMEM;
1457
1458 err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1459 if (err)
1460 goto out_free;
1461
1462 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1463 *max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1464 *sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1465
1466 out_free:
1467 kfree(query_ctx);
1468 return err;
1469 }
1470
mlx5_esw_vport_alloc(struct mlx5_eswitch * esw,struct mlx5_core_dev * dev,int index,u16 vport_num)1471 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw, struct mlx5_core_dev *dev,
1472 int index, u16 vport_num)
1473 {
1474 struct mlx5_vport *vport;
1475 int err;
1476
1477 vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1478 if (!vport)
1479 return -ENOMEM;
1480
1481 vport->dev = esw->dev;
1482 vport->vport = vport_num;
1483 vport->index = index;
1484 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1485 INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1486 err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1487 if (err)
1488 goto insert_err;
1489
1490 esw->total_vports++;
1491 return 0;
1492
1493 insert_err:
1494 kfree(vport);
1495 return err;
1496 }
1497
mlx5_esw_vport_free(struct mlx5_eswitch * esw,struct mlx5_vport * vport)1498 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1499 {
1500 xa_erase(&esw->vports, vport->vport);
1501 kfree(vport);
1502 }
1503
mlx5_esw_vports_cleanup(struct mlx5_eswitch * esw)1504 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1505 {
1506 struct mlx5_vport *vport;
1507 unsigned long i;
1508
1509 mlx5_esw_for_each_vport(esw, i, vport)
1510 mlx5_esw_vport_free(esw, vport);
1511 xa_destroy(&esw->vports);
1512 }
1513
mlx5_esw_vports_init(struct mlx5_eswitch * esw)1514 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1515 {
1516 struct mlx5_core_dev *dev = esw->dev;
1517 u16 max_host_pf_sfs;
1518 u16 base_sf_num;
1519 int idx = 0;
1520 int err;
1521 int i;
1522
1523 xa_init(&esw->vports);
1524
1525 err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_PF);
1526 if (err)
1527 goto err;
1528 if (esw->first_host_vport == MLX5_VPORT_PF)
1529 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1530 idx++;
1531
1532 for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1533 err = mlx5_esw_vport_alloc(esw, dev, idx, idx);
1534 if (err)
1535 goto err;
1536 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1537 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1538 idx++;
1539 }
1540 base_sf_num = mlx5_sf_start_function_id(dev);
1541 for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1542 err = mlx5_esw_vport_alloc(esw, dev, idx, base_sf_num + i);
1543 if (err)
1544 goto err;
1545 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1546 idx++;
1547 }
1548
1549 err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1550 if (err)
1551 goto err;
1552 for (i = 0; i < max_host_pf_sfs; i++) {
1553 err = mlx5_esw_vport_alloc(esw, dev, idx, base_sf_num + i);
1554 if (err)
1555 goto err;
1556 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1557 idx++;
1558 }
1559
1560 if (mlx5_ecpf_vport_exists(dev)) {
1561 err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_ECPF);
1562 if (err)
1563 goto err;
1564 idx++;
1565 }
1566 err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_UPLINK);
1567 if (err)
1568 goto err;
1569 return 0;
1570
1571 err:
1572 mlx5_esw_vports_cleanup(esw);
1573 return err;
1574 }
1575
mlx5_eswitch_init(struct mlx5_core_dev * dev)1576 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1577 {
1578 struct mlx5_eswitch *esw;
1579 int err;
1580
1581 if (!MLX5_VPORT_MANAGER(dev))
1582 return 0;
1583
1584 esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1585 if (!esw)
1586 return -ENOMEM;
1587
1588 esw->dev = dev;
1589 esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1590 esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1591
1592 esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1593 if (!esw->work_queue) {
1594 err = -ENOMEM;
1595 goto abort;
1596 }
1597
1598 err = mlx5_esw_vports_init(esw);
1599 if (err)
1600 goto abort;
1601
1602 err = esw_offloads_init_reps(esw);
1603 if (err)
1604 goto reps_err;
1605
1606 mutex_init(&esw->offloads.encap_tbl_lock);
1607 hash_init(esw->offloads.encap_tbl);
1608 mutex_init(&esw->offloads.decap_tbl_lock);
1609 hash_init(esw->offloads.decap_tbl);
1610 mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1611 atomic64_set(&esw->offloads.num_flows, 0);
1612 ida_init(&esw->offloads.vport_metadata_ida);
1613 xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1614 mutex_init(&esw->state_lock);
1615 init_rwsem(&esw->mode_lock);
1616 refcount_set(&esw->qos.refcnt, 0);
1617
1618 esw->enabled_vports = 0;
1619 esw->mode = MLX5_ESWITCH_LEGACY;
1620 esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1621 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1622 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1623 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1624 else
1625 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1626 if (MLX5_ESWITCH_MANAGER(dev) &&
1627 mlx5_esw_vport_match_metadata_supported(esw))
1628 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1629
1630 dev->priv.eswitch = esw;
1631 BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1632
1633 esw->dbgfs = debugfs_create_dir("esw", mlx5_debugfs_get_dev_root(esw->dev));
1634 esw_info(dev,
1635 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1636 esw->total_vports,
1637 MLX5_MAX_UC_PER_VPORT(dev),
1638 MLX5_MAX_MC_PER_VPORT(dev));
1639 return 0;
1640
1641 reps_err:
1642 mlx5_esw_vports_cleanup(esw);
1643 abort:
1644 if (esw->work_queue)
1645 destroy_workqueue(esw->work_queue);
1646 kfree(esw);
1647 return err;
1648 }
1649
mlx5_eswitch_cleanup(struct mlx5_eswitch * esw)1650 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1651 {
1652 if (!esw || !MLX5_VPORT_MANAGER(esw->dev))
1653 return;
1654
1655 esw_info(esw->dev, "cleanup\n");
1656
1657 debugfs_remove_recursive(esw->dbgfs);
1658 esw->dev->priv.eswitch = NULL;
1659 destroy_workqueue(esw->work_queue);
1660 WARN_ON(refcount_read(&esw->qos.refcnt));
1661 mutex_destroy(&esw->state_lock);
1662 WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1663 xa_destroy(&esw->offloads.vhca_map);
1664 ida_destroy(&esw->offloads.vport_metadata_ida);
1665 mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1666 mutex_destroy(&esw->offloads.encap_tbl_lock);
1667 mutex_destroy(&esw->offloads.decap_tbl_lock);
1668 esw_offloads_cleanup_reps(esw);
1669 mlx5_esw_vports_cleanup(esw);
1670 kfree(esw);
1671 }
1672
1673 /* Vport Administration */
1674 static int
mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch * esw,struct mlx5_vport * evport,const u8 * mac)1675 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1676 struct mlx5_vport *evport, const u8 *mac)
1677 {
1678 u16 vport_num = evport->vport;
1679 u64 node_guid;
1680 int err = 0;
1681
1682 if (is_multicast_ether_addr(mac))
1683 return -EINVAL;
1684
1685 if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1686 mlx5_core_warn(esw->dev,
1687 "Set invalid MAC while spoofchk is on, vport(%d)\n",
1688 vport_num);
1689
1690 err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1691 if (err) {
1692 mlx5_core_warn(esw->dev,
1693 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1694 vport_num, err);
1695 return err;
1696 }
1697
1698 node_guid_gen_from_mac(&node_guid, mac);
1699 err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1700 if (err)
1701 mlx5_core_warn(esw->dev,
1702 "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1703 vport_num, err);
1704
1705 ether_addr_copy(evport->info.mac, mac);
1706 evport->info.node_guid = node_guid;
1707 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1708 err = esw_acl_ingress_lgcy_setup(esw, evport);
1709
1710 return err;
1711 }
1712
mlx5_eswitch_set_vport_mac(struct mlx5_eswitch * esw,u16 vport,const u8 * mac)1713 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1714 u16 vport, const u8 *mac)
1715 {
1716 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1717 int err = 0;
1718
1719 if (IS_ERR(evport))
1720 return PTR_ERR(evport);
1721
1722 mutex_lock(&esw->state_lock);
1723 err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
1724 mutex_unlock(&esw->state_lock);
1725 return err;
1726 }
1727
mlx5_esw_check_port_type(struct mlx5_eswitch * esw,u16 vport_num,xa_mark_t mark)1728 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
1729 {
1730 struct mlx5_vport *vport;
1731
1732 vport = mlx5_eswitch_get_vport(esw, vport_num);
1733 if (IS_ERR(vport))
1734 return false;
1735
1736 return xa_get_mark(&esw->vports, vport_num, mark);
1737 }
1738
mlx5_eswitch_is_vf_vport(struct mlx5_eswitch * esw,u16 vport_num)1739 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1740 {
1741 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
1742 }
1743
mlx5_esw_is_sf_vport(struct mlx5_eswitch * esw,u16 vport_num)1744 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1745 {
1746 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
1747 }
1748
mlx5_eswitch_set_vport_state(struct mlx5_eswitch * esw,u16 vport,int link_state)1749 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1750 u16 vport, int link_state)
1751 {
1752 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1753 int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
1754 int other_vport = 1;
1755 int err = 0;
1756
1757 if (!mlx5_esw_allowed(esw))
1758 return -EPERM;
1759 if (IS_ERR(evport))
1760 return PTR_ERR(evport);
1761
1762 if (vport == MLX5_VPORT_UPLINK) {
1763 opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
1764 other_vport = 0;
1765 vport = 0;
1766 }
1767 mutex_lock(&esw->state_lock);
1768 if (esw->mode != MLX5_ESWITCH_LEGACY) {
1769 err = -EOPNOTSUPP;
1770 goto unlock;
1771 }
1772
1773 err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
1774 if (err) {
1775 mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
1776 vport, opmod, err);
1777 goto unlock;
1778 }
1779
1780 evport->info.link_state = link_state;
1781
1782 unlock:
1783 mutex_unlock(&esw->state_lock);
1784 return err;
1785 }
1786
mlx5_eswitch_get_vport_config(struct mlx5_eswitch * esw,u16 vport,struct ifla_vf_info * ivi)1787 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1788 u16 vport, struct ifla_vf_info *ivi)
1789 {
1790 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1791
1792 if (IS_ERR(evport))
1793 return PTR_ERR(evport);
1794
1795 memset(ivi, 0, sizeof(*ivi));
1796 ivi->vf = vport - 1;
1797
1798 mutex_lock(&esw->state_lock);
1799 ether_addr_copy(ivi->mac, evport->info.mac);
1800 ivi->linkstate = evport->info.link_state;
1801 ivi->vlan = evport->info.vlan;
1802 ivi->qos = evport->info.qos;
1803 ivi->spoofchk = evport->info.spoofchk;
1804 ivi->trusted = evport->info.trusted;
1805 if (evport->qos.enabled) {
1806 ivi->min_tx_rate = evport->qos.min_rate;
1807 ivi->max_tx_rate = evport->qos.max_rate;
1808 }
1809 mutex_unlock(&esw->state_lock);
1810
1811 return 0;
1812 }
1813
__mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch * esw,u16 vport,u16 vlan,u8 qos,u8 set_flags)1814 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1815 u16 vport, u16 vlan, u8 qos, u8 set_flags)
1816 {
1817 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1818 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
1819 int err = 0;
1820
1821 if (IS_ERR(evport))
1822 return PTR_ERR(evport);
1823 if (vlan > 4095 || qos > 7)
1824 return -EINVAL;
1825
1826 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
1827 err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
1828 if (err)
1829 return err;
1830 }
1831
1832 evport->info.vlan = vlan;
1833 evport->info.qos = qos;
1834 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
1835 err = esw_acl_ingress_lgcy_setup(esw, evport);
1836 if (err)
1837 return err;
1838 err = esw_acl_egress_lgcy_setup(esw, evport);
1839 }
1840
1841 return err;
1842 }
1843
mlx5_eswitch_get_vport_stats(struct mlx5_eswitch * esw,u16 vport_num,struct ifla_vf_stats * vf_stats)1844 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
1845 u16 vport_num,
1846 struct ifla_vf_stats *vf_stats)
1847 {
1848 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
1849 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
1850 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
1851 struct mlx5_vport_drop_stats stats = {};
1852 int err = 0;
1853 u32 *out;
1854
1855 if (IS_ERR(vport))
1856 return PTR_ERR(vport);
1857
1858 out = kvzalloc(outlen, GFP_KERNEL);
1859 if (!out)
1860 return -ENOMEM;
1861
1862 MLX5_SET(query_vport_counter_in, in, opcode,
1863 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1864 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
1865 MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
1866 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1867
1868 err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
1869 if (err)
1870 goto free_out;
1871
1872 #define MLX5_GET_CTR(p, x) \
1873 MLX5_GET64(query_vport_counter_out, p, x)
1874
1875 memset(vf_stats, 0, sizeof(*vf_stats));
1876 vf_stats->rx_packets =
1877 MLX5_GET_CTR(out, received_eth_unicast.packets) +
1878 MLX5_GET_CTR(out, received_ib_unicast.packets) +
1879 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1880 MLX5_GET_CTR(out, received_ib_multicast.packets) +
1881 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1882
1883 vf_stats->rx_bytes =
1884 MLX5_GET_CTR(out, received_eth_unicast.octets) +
1885 MLX5_GET_CTR(out, received_ib_unicast.octets) +
1886 MLX5_GET_CTR(out, received_eth_multicast.octets) +
1887 MLX5_GET_CTR(out, received_ib_multicast.octets) +
1888 MLX5_GET_CTR(out, received_eth_broadcast.octets);
1889
1890 vf_stats->tx_packets =
1891 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
1892 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
1893 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
1894 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
1895 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
1896
1897 vf_stats->tx_bytes =
1898 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
1899 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
1900 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
1901 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
1902 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
1903
1904 vf_stats->multicast =
1905 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1906 MLX5_GET_CTR(out, received_ib_multicast.packets);
1907
1908 vf_stats->broadcast =
1909 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1910
1911 err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
1912 if (err)
1913 goto free_out;
1914 vf_stats->rx_dropped = stats.rx_dropped;
1915 vf_stats->tx_dropped = stats.tx_dropped;
1916
1917 free_out:
1918 kvfree(out);
1919 return err;
1920 }
1921
mlx5_eswitch_mode(const struct mlx5_core_dev * dev)1922 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
1923 {
1924 struct mlx5_eswitch *esw = dev->priv.eswitch;
1925
1926 return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
1927 }
1928 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
1929
1930 enum devlink_eswitch_encap_mode
mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev * dev)1931 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
1932 {
1933 struct mlx5_eswitch *esw;
1934
1935 esw = dev->priv.eswitch;
1936 return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS) ? esw->offloads.encap :
1937 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1938 }
1939 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
1940
mlx5_esw_multipath_prereq(struct mlx5_core_dev * dev0,struct mlx5_core_dev * dev1)1941 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
1942 struct mlx5_core_dev *dev1)
1943 {
1944 return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
1945 dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
1946 }
1947
mlx5_esw_event_notifier_register(struct mlx5_eswitch * esw,struct notifier_block * nb)1948 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
1949 {
1950 return blocking_notifier_chain_register(&esw->n_head, nb);
1951 }
1952
mlx5_esw_event_notifier_unregister(struct mlx5_eswitch * esw,struct notifier_block * nb)1953 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
1954 {
1955 blocking_notifier_chain_unregister(&esw->n_head, nb);
1956 }
1957
1958 /**
1959 * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
1960 * @mdev: mlx5 core device.
1961 *
1962 * Should be called by esw resources callers.
1963 *
1964 * Return: true on success or false.
1965 */
mlx5_esw_hold(struct mlx5_core_dev * mdev)1966 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
1967 {
1968 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1969
1970 /* e.g. VF doesn't have eswitch so nothing to do */
1971 if (!mlx5_esw_allowed(esw))
1972 return true;
1973
1974 if (down_read_trylock(&esw->mode_lock) != 0)
1975 return true;
1976
1977 return false;
1978 }
1979
1980 /**
1981 * mlx5_esw_release() - Release a read lock on esw mode lock.
1982 * @mdev: mlx5 core device.
1983 */
mlx5_esw_release(struct mlx5_core_dev * mdev)1984 void mlx5_esw_release(struct mlx5_core_dev *mdev)
1985 {
1986 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1987
1988 if (mlx5_esw_allowed(esw))
1989 up_read(&esw->mode_lock);
1990 }
1991
1992 /**
1993 * mlx5_esw_get() - Increase esw user count.
1994 * @mdev: mlx5 core device.
1995 */
mlx5_esw_get(struct mlx5_core_dev * mdev)1996 void mlx5_esw_get(struct mlx5_core_dev *mdev)
1997 {
1998 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1999
2000 if (mlx5_esw_allowed(esw))
2001 atomic64_inc(&esw->user_count);
2002 }
2003
2004 /**
2005 * mlx5_esw_put() - Decrease esw user count.
2006 * @mdev: mlx5 core device.
2007 */
mlx5_esw_put(struct mlx5_core_dev * mdev)2008 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2009 {
2010 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2011
2012 if (mlx5_esw_allowed(esw))
2013 atomic64_dec_if_positive(&esw->user_count);
2014 }
2015
2016 /**
2017 * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2018 * @esw: eswitch device.
2019 *
2020 * Should be called by esw mode change routine.
2021 *
2022 * Return:
2023 * * 0 - esw mode if successfully locked and refcount is 0.
2024 * * -EBUSY - refcount is not 0.
2025 * * -EINVAL - In the middle of switching mode or lock is already held.
2026 */
mlx5_esw_try_lock(struct mlx5_eswitch * esw)2027 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2028 {
2029 if (down_write_trylock(&esw->mode_lock) == 0)
2030 return -EINVAL;
2031
2032 if (atomic64_read(&esw->user_count) > 0) {
2033 up_write(&esw->mode_lock);
2034 return -EBUSY;
2035 }
2036
2037 return esw->mode;
2038 }
2039
2040 /**
2041 * mlx5_esw_unlock() - Release write lock on esw mode lock
2042 * @esw: eswitch device.
2043 */
mlx5_esw_unlock(struct mlx5_eswitch * esw)2044 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2045 {
2046 up_write(&esw->mode_lock);
2047 }
2048
2049 /**
2050 * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2051 *
2052 * @dev: Pointer to core device
2053 *
2054 * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2055 */
mlx5_eswitch_get_total_vports(const struct mlx5_core_dev * dev)2056 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2057 {
2058 struct mlx5_eswitch *esw;
2059
2060 esw = dev->priv.eswitch;
2061 return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2062 }
2063 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2064
2065 /**
2066 * mlx5_eswitch_get_core_dev - Get the mdev device
2067 * @esw : eswitch device.
2068 *
2069 * Return the mellanox core device which manages the eswitch.
2070 */
mlx5_eswitch_get_core_dev(struct mlx5_eswitch * esw)2071 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2072 {
2073 return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2074 }
2075 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);
2076