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