2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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 "mlx5_core.h"
46 struct mlx5_flow_handle *
47 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
48 struct mlx5_flow_spec *spec,
49 struct mlx5_esw_flow_attr *attr)
51 struct mlx5_flow_destination dest[2] = {};
52 struct mlx5_flow_act flow_act = {0};
53 struct mlx5_fc *counter = NULL;
54 struct mlx5_flow_handle *rule;
58 if (esw->mode != SRIOV_OFFLOADS)
59 return ERR_PTR(-EOPNOTSUPP);
61 flow_act.action = attr->action;
62 /* if per flow vlan pop/push is emulated, don't set that into the firmware */
63 if (!mlx5_eswitch_vlan_actions_supported(esw->dev))
64 flow_act.action &= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
65 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
66 else if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
67 flow_act.vlan.ethtype = ntohs(attr->vlan_proto);
68 flow_act.vlan.vid = attr->vlan_vid;
69 flow_act.vlan.prio = attr->vlan_prio;
72 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
73 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
74 dest[i].vport.num = attr->out_rep->vport;
75 if (MLX5_CAP_ESW(esw->dev, merged_eswitch)) {
76 dest[i].vport.vhca_id =
77 MLX5_CAP_GEN(attr->out_mdev, vhca_id);
78 dest[i].vport.vhca_id_valid = 1;
82 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
83 counter = mlx5_fc_create(esw->dev, true);
84 if (IS_ERR(counter)) {
85 rule = ERR_CAST(counter);
86 goto err_counter_alloc;
88 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
89 dest[i].counter = counter;
93 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
94 MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);
96 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
97 MLX5_SET(fte_match_set_misc, misc,
98 source_eswitch_owner_vhca_id,
99 MLX5_CAP_GEN(attr->in_mdev, vhca_id));
101 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
102 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
103 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
104 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
105 source_eswitch_owner_vhca_id);
107 if (attr->match_level == MLX5_MATCH_NONE)
108 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
110 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS |
111 MLX5_MATCH_MISC_PARAMETERS;
113 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
114 spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
116 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
117 flow_act.modify_id = attr->mod_hdr_id;
119 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
120 flow_act.encap_id = attr->encap_id;
122 rule = mlx5_add_flow_rules((struct mlx5_flow_table *)esw->fdb_table.offloads.fast_fdb,
123 spec, &flow_act, dest, i);
127 esw->offloads.num_flows++;
132 mlx5_fc_destroy(esw->dev, counter);
138 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
139 struct mlx5_flow_handle *rule,
140 struct mlx5_esw_flow_attr *attr)
142 struct mlx5_fc *counter = NULL;
144 counter = mlx5_flow_rule_counter(rule);
145 mlx5_del_flow_rules(rule);
146 mlx5_fc_destroy(esw->dev, counter);
147 esw->offloads.num_flows--;
150 static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
152 struct mlx5_eswitch_rep *rep;
153 int vf_vport, err = 0;
155 esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
156 for (vf_vport = 1; vf_vport < esw->enabled_vports; vf_vport++) {
157 rep = &esw->offloads.vport_reps[vf_vport];
158 if (!rep->rep_if[REP_ETH].valid)
161 err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
170 static struct mlx5_eswitch_rep *
171 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
173 struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;
175 in_rep = attr->in_rep;
176 out_rep = attr->out_rep;
188 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
189 bool push, bool pop, bool fwd)
191 struct mlx5_eswitch_rep *in_rep, *out_rep;
193 if ((push || pop) && !fwd)
196 in_rep = attr->in_rep;
197 out_rep = attr->out_rep;
199 if (push && in_rep->vport == FDB_UPLINK_VPORT)
202 if (pop && out_rep->vport == FDB_UPLINK_VPORT)
205 /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
206 if (!push && !pop && fwd)
207 if (in_rep->vlan && out_rep->vport == FDB_UPLINK_VPORT)
210 /* protects against (1) setting rules with different vlans to push and
211 * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
213 if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan_vid))
222 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
223 struct mlx5_esw_flow_attr *attr)
225 struct offloads_fdb *offloads = &esw->fdb_table.offloads;
226 struct mlx5_eswitch_rep *vport = NULL;
230 /* nop if we're on the vlan push/pop non emulation mode */
231 if (mlx5_eswitch_vlan_actions_supported(esw->dev))
234 push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
235 pop = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
236 fwd = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
238 err = esw_add_vlan_action_check(attr, push, pop, fwd);
242 attr->vlan_handled = false;
244 vport = esw_vlan_action_get_vport(attr, push, pop);
246 if (!push && !pop && fwd) {
247 /* tracks VF --> wire rules without vlan push action */
248 if (attr->out_rep->vport == FDB_UPLINK_VPORT) {
249 vport->vlan_refcount++;
250 attr->vlan_handled = true;
259 if (!(offloads->vlan_push_pop_refcount)) {
260 /* it's the 1st vlan rule, apply global vlan pop policy */
261 err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
265 offloads->vlan_push_pop_refcount++;
268 if (vport->vlan_refcount)
271 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, attr->vlan_vid, 0,
272 SET_VLAN_INSERT | SET_VLAN_STRIP);
275 vport->vlan = attr->vlan_vid;
277 vport->vlan_refcount++;
281 attr->vlan_handled = true;
285 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
286 struct mlx5_esw_flow_attr *attr)
288 struct offloads_fdb *offloads = &esw->fdb_table.offloads;
289 struct mlx5_eswitch_rep *vport = NULL;
293 /* nop if we're on the vlan push/pop non emulation mode */
294 if (mlx5_eswitch_vlan_actions_supported(esw->dev))
297 if (!attr->vlan_handled)
300 push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
301 pop = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
302 fwd = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
304 vport = esw_vlan_action_get_vport(attr, push, pop);
306 if (!push && !pop && fwd) {
307 /* tracks VF --> wire rules without vlan push action */
308 if (attr->out_rep->vport == FDB_UPLINK_VPORT)
309 vport->vlan_refcount--;
315 vport->vlan_refcount--;
316 if (vport->vlan_refcount)
317 goto skip_unset_push;
320 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
321 0, 0, SET_VLAN_STRIP);
327 offloads->vlan_push_pop_refcount--;
328 if (offloads->vlan_push_pop_refcount)
331 /* no more vlan rules, stop global vlan pop policy */
332 err = esw_set_global_vlan_pop(esw, 0);
338 struct mlx5_flow_handle *
339 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, int vport, u32 sqn)
341 struct mlx5_flow_act flow_act = {0};
342 struct mlx5_flow_destination dest = {};
343 struct mlx5_flow_handle *flow_rule;
344 struct mlx5_flow_spec *spec;
347 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
349 flow_rule = ERR_PTR(-ENOMEM);
353 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
354 MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
355 MLX5_SET(fte_match_set_misc, misc, source_port, 0x0); /* source vport is 0 */
357 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
358 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
359 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
361 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
362 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
363 dest.vport.num = vport;
364 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
366 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
367 &flow_act, &dest, 1);
368 if (IS_ERR(flow_rule))
369 esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
374 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
376 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
378 mlx5_del_flow_rules(rule);
381 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
383 struct mlx5_flow_act flow_act = {0};
384 struct mlx5_flow_destination dest = {};
385 struct mlx5_flow_handle *flow_rule = NULL;
386 struct mlx5_flow_spec *spec;
393 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
399 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
400 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
402 dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
403 outer_headers.dmac_47_16);
406 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
408 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
410 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
411 &flow_act, &dest, 1);
412 if (IS_ERR(flow_rule)) {
413 err = PTR_ERR(flow_rule);
414 esw_warn(esw->dev, "FDB: Failed to add unicast miss flow rule err %d\n", err);
418 esw->fdb_table.offloads.miss_rule_uni = flow_rule;
420 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
422 dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
423 outer_headers.dmac_47_16);
425 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
426 &flow_act, &dest, 1);
427 if (IS_ERR(flow_rule)) {
428 err = PTR_ERR(flow_rule);
429 esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
430 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
434 esw->fdb_table.offloads.miss_rule_multi = flow_rule;
441 #define ESW_OFFLOADS_NUM_GROUPS 4
443 static int esw_create_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
445 struct mlx5_core_dev *dev = esw->dev;
446 struct mlx5_flow_namespace *root_ns;
447 struct mlx5_flow_table *fdb = NULL;
448 int esw_size, err = 0;
450 u32 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
451 MLX5_CAP_GEN(dev, max_flow_counter_15_0);
453 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
455 esw_warn(dev, "Failed to get FDB flow namespace\n");
460 esw_debug(dev, "Create offloads FDB table, min (max esw size(2^%d), max counters(%d)*groups(%d))\n",
461 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size),
462 max_flow_counter, ESW_OFFLOADS_NUM_GROUPS);
464 esw_size = min_t(int, max_flow_counter * ESW_OFFLOADS_NUM_GROUPS,
465 1 << MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
467 if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
468 flags |= MLX5_FLOW_TABLE_TUNNEL_EN;
470 fdb = mlx5_create_auto_grouped_flow_table(root_ns, FDB_FAST_PATH,
472 ESW_OFFLOADS_NUM_GROUPS, 0,
476 esw_warn(dev, "Failed to create Fast path FDB Table err %d\n", err);
479 esw->fdb_table.offloads.fast_fdb = fdb;
485 static void esw_destroy_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
487 mlx5_destroy_flow_table(esw->fdb_table.offloads.fast_fdb);
490 #define MAX_PF_SQ 256
491 #define MAX_SQ_NVPORTS 32
493 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw, int nvports)
495 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
496 struct mlx5_flow_table_attr ft_attr = {};
497 struct mlx5_core_dev *dev = esw->dev;
498 struct mlx5_flow_namespace *root_ns;
499 struct mlx5_flow_table *fdb = NULL;
500 int table_size, ix, err = 0;
501 struct mlx5_flow_group *g;
502 void *match_criteria;
506 esw_debug(esw->dev, "Create offloads FDB Tables\n");
507 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
511 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
513 esw_warn(dev, "Failed to get FDB flow namespace\n");
518 err = esw_create_offloads_fast_fdb_table(esw);
522 table_size = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ + 2;
524 ft_attr.max_fte = table_size;
525 ft_attr.prio = FDB_SLOW_PATH;
527 fdb = mlx5_create_flow_table(root_ns, &ft_attr);
530 esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
533 esw->fdb_table.offloads.slow_fdb = fdb;
535 /* create send-to-vport group */
536 memset(flow_group_in, 0, inlen);
537 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
538 MLX5_MATCH_MISC_PARAMETERS);
540 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
542 MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
543 MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
545 ix = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ;
546 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
547 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix - 1);
549 g = mlx5_create_flow_group(fdb, flow_group_in);
552 esw_warn(dev, "Failed to create send-to-vport flow group err(%d)\n", err);
555 esw->fdb_table.offloads.send_to_vport_grp = g;
557 /* create miss group */
558 memset(flow_group_in, 0, inlen);
559 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
560 MLX5_MATCH_OUTER_HEADERS);
561 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
563 dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
564 outer_headers.dmac_47_16);
567 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
568 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix + 2);
570 g = mlx5_create_flow_group(fdb, flow_group_in);
573 esw_warn(dev, "Failed to create miss flow group err(%d)\n", err);
576 esw->fdb_table.offloads.miss_grp = g;
578 err = esw_add_fdb_miss_rule(esw);
585 mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
587 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
589 mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
591 mlx5_destroy_flow_table(esw->fdb_table.offloads.fast_fdb);
594 kvfree(flow_group_in);
598 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
600 if (!esw->fdb_table.offloads.fast_fdb)
603 esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
604 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
605 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
606 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
607 mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
609 mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
610 esw_destroy_offloads_fast_fdb_table(esw);
613 static int esw_create_offloads_table(struct mlx5_eswitch *esw)
615 struct mlx5_flow_table_attr ft_attr = {};
616 struct mlx5_core_dev *dev = esw->dev;
617 struct mlx5_flow_table *ft_offloads;
618 struct mlx5_flow_namespace *ns;
621 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
623 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
627 ft_attr.max_fte = dev->priv.sriov.num_vfs + 2;
629 ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
630 if (IS_ERR(ft_offloads)) {
631 err = PTR_ERR(ft_offloads);
632 esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
636 esw->offloads.ft_offloads = ft_offloads;
640 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
642 struct mlx5_esw_offload *offloads = &esw->offloads;
644 mlx5_destroy_flow_table(offloads->ft_offloads);
647 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
649 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
650 struct mlx5_flow_group *g;
651 struct mlx5_priv *priv = &esw->dev->priv;
653 void *match_criteria, *misc;
655 int nvports = priv->sriov.num_vfs + 2;
657 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
661 /* create vport rx group */
662 memset(flow_group_in, 0, inlen);
663 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
664 MLX5_MATCH_MISC_PARAMETERS);
666 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
667 misc = MLX5_ADDR_OF(fte_match_param, match_criteria, misc_parameters);
668 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
670 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
671 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
673 g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
677 mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
681 esw->offloads.vport_rx_group = g;
683 kvfree(flow_group_in);
687 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
689 mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
692 struct mlx5_flow_handle *
693 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
695 struct mlx5_flow_act flow_act = {0};
696 struct mlx5_flow_destination dest = {};
697 struct mlx5_flow_handle *flow_rule;
698 struct mlx5_flow_spec *spec;
701 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
703 flow_rule = ERR_PTR(-ENOMEM);
707 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
708 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
710 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
711 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
713 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
714 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
717 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
718 flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
719 &flow_act, &dest, 1);
720 if (IS_ERR(flow_rule)) {
721 esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
730 static int esw_offloads_start(struct mlx5_eswitch *esw)
732 int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
734 if (esw->mode != SRIOV_LEGACY) {
735 esw_warn(esw->dev, "Can't set offloads mode, SRIOV legacy not enabled\n");
739 mlx5_eswitch_disable_sriov(esw);
740 err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
742 esw_warn(esw->dev, "Failed setting eswitch to offloads, err %d\n", err);
743 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
745 esw_warn(esw->dev, "Failed setting eswitch back to legacy, err %d\n", err1);
747 if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
748 if (mlx5_eswitch_inline_mode_get(esw,
750 &esw->offloads.inline_mode)) {
751 esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
752 esw_warn(esw->dev, "Inline mode is different between vports\n");
758 void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
760 kfree(esw->offloads.vport_reps);
763 int esw_offloads_init_reps(struct mlx5_eswitch *esw)
765 int total_vfs = MLX5_TOTAL_VPORTS(esw->dev);
766 struct mlx5_core_dev *dev = esw->dev;
767 struct mlx5_esw_offload *offloads;
768 struct mlx5_eswitch_rep *rep;
772 esw->offloads.vport_reps = kcalloc(total_vfs,
773 sizeof(struct mlx5_eswitch_rep),
775 if (!esw->offloads.vport_reps)
778 offloads = &esw->offloads;
779 mlx5_query_nic_vport_mac_address(dev, 0, hw_id);
781 for (vport = 0; vport < total_vfs; vport++) {
782 rep = &offloads->vport_reps[vport];
785 ether_addr_copy(rep->hw_id, hw_id);
788 offloads->vport_reps[0].vport = FDB_UPLINK_VPORT;
793 static void esw_offloads_unload_reps_type(struct mlx5_eswitch *esw, int nvports,
796 struct mlx5_eswitch_rep *rep;
799 for (vport = nvports - 1; vport >= 0; vport--) {
800 rep = &esw->offloads.vport_reps[vport];
801 if (!rep->rep_if[rep_type].valid)
804 rep->rep_if[rep_type].unload(rep);
808 static void esw_offloads_unload_reps(struct mlx5_eswitch *esw, int nvports)
810 u8 rep_type = NUM_REP_TYPES;
812 while (rep_type-- > 0)
813 esw_offloads_unload_reps_type(esw, nvports, rep_type);
816 static int esw_offloads_load_reps_type(struct mlx5_eswitch *esw, int nvports,
819 struct mlx5_eswitch_rep *rep;
823 for (vport = 0; vport < nvports; vport++) {
824 rep = &esw->offloads.vport_reps[vport];
825 if (!rep->rep_if[rep_type].valid)
828 err = rep->rep_if[rep_type].load(esw->dev, rep);
836 esw_offloads_unload_reps_type(esw, vport, rep_type);
840 static int esw_offloads_load_reps(struct mlx5_eswitch *esw, int nvports)
845 for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
846 err = esw_offloads_load_reps_type(esw, nvports, rep_type);
854 while (rep_type-- > 0)
855 esw_offloads_unload_reps_type(esw, nvports, rep_type);
859 int esw_offloads_init(struct mlx5_eswitch *esw, int nvports)
863 err = esw_create_offloads_fdb_tables(esw, nvports);
867 err = esw_create_offloads_table(esw);
871 err = esw_create_vport_rx_group(esw);
875 err = esw_offloads_load_reps(esw, nvports);
882 esw_destroy_vport_rx_group(esw);
885 esw_destroy_offloads_table(esw);
888 esw_destroy_offloads_fdb_tables(esw);
893 static int esw_offloads_stop(struct mlx5_eswitch *esw)
895 int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
897 mlx5_eswitch_disable_sriov(esw);
898 err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
900 esw_warn(esw->dev, "Failed setting eswitch to legacy, err %d\n", err);
901 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
903 esw_warn(esw->dev, "Failed setting eswitch back to offloads, err %d\n", err);
906 /* enable back PF RoCE */
907 mlx5_reload_interface(esw->dev, MLX5_INTERFACE_PROTOCOL_IB);
912 void esw_offloads_cleanup(struct mlx5_eswitch *esw, int nvports)
914 esw_offloads_unload_reps(esw, nvports);
915 esw_destroy_vport_rx_group(esw);
916 esw_destroy_offloads_table(esw);
917 esw_destroy_offloads_fdb_tables(esw);
920 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
923 case DEVLINK_ESWITCH_MODE_LEGACY:
924 *mlx5_mode = SRIOV_LEGACY;
926 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
927 *mlx5_mode = SRIOV_OFFLOADS;
936 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
940 *mode = DEVLINK_ESWITCH_MODE_LEGACY;
943 *mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
952 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
955 case DEVLINK_ESWITCH_INLINE_MODE_NONE:
956 *mlx5_mode = MLX5_INLINE_MODE_NONE;
958 case DEVLINK_ESWITCH_INLINE_MODE_LINK:
959 *mlx5_mode = MLX5_INLINE_MODE_L2;
961 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
962 *mlx5_mode = MLX5_INLINE_MODE_IP;
964 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
965 *mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
974 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
977 case MLX5_INLINE_MODE_NONE:
978 *mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
980 case MLX5_INLINE_MODE_L2:
981 *mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
983 case MLX5_INLINE_MODE_IP:
984 *mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
986 case MLX5_INLINE_MODE_TCP_UDP:
987 *mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
996 static int mlx5_devlink_eswitch_check(struct devlink *devlink)
998 struct mlx5_core_dev *dev = devlink_priv(devlink);
1000 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1003 if (!MLX5_CAP_GEN(dev, vport_group_manager))
1006 if (dev->priv.eswitch->mode == SRIOV_NONE)
1012 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
1014 struct mlx5_core_dev *dev = devlink_priv(devlink);
1015 u16 cur_mlx5_mode, mlx5_mode = 0;
1018 err = mlx5_devlink_eswitch_check(devlink);
1022 cur_mlx5_mode = dev->priv.eswitch->mode;
1024 if (esw_mode_from_devlink(mode, &mlx5_mode))
1027 if (cur_mlx5_mode == mlx5_mode)
1030 if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
1031 return esw_offloads_start(dev->priv.eswitch);
1032 else if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
1033 return esw_offloads_stop(dev->priv.eswitch);
1038 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
1040 struct mlx5_core_dev *dev = devlink_priv(devlink);
1043 err = mlx5_devlink_eswitch_check(devlink);
1047 return esw_mode_to_devlink(dev->priv.eswitch->mode, mode);
1050 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode)
1052 struct mlx5_core_dev *dev = devlink_priv(devlink);
1053 struct mlx5_eswitch *esw = dev->priv.eswitch;
1057 err = mlx5_devlink_eswitch_check(devlink);
1061 switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
1062 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1063 if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE)
1066 case MLX5_CAP_INLINE_MODE_L2:
1067 esw_warn(dev, "Inline mode can't be set\n");
1069 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1073 if (esw->offloads.num_flows > 0) {
1074 esw_warn(dev, "Can't set inline mode when flows are configured\n");
1078 err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
1082 for (vport = 1; vport < esw->enabled_vports; vport++) {
1083 err = mlx5_modify_nic_vport_min_inline(dev, vport, mlx5_mode);
1085 esw_warn(dev, "Failed to set min inline on vport %d\n",
1087 goto revert_inline_mode;
1091 esw->offloads.inline_mode = mlx5_mode;
1096 mlx5_modify_nic_vport_min_inline(dev,
1098 esw->offloads.inline_mode);
1103 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
1105 struct mlx5_core_dev *dev = devlink_priv(devlink);
1106 struct mlx5_eswitch *esw = dev->priv.eswitch;
1109 err = mlx5_devlink_eswitch_check(devlink);
1113 return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
1116 int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, int nvfs, u8 *mode)
1118 u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
1119 struct mlx5_core_dev *dev = esw->dev;
1122 if (!MLX5_CAP_GEN(dev, vport_group_manager))
1125 if (esw->mode == SRIOV_NONE)
1128 switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
1129 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1130 mlx5_mode = MLX5_INLINE_MODE_NONE;
1132 case MLX5_CAP_INLINE_MODE_L2:
1133 mlx5_mode = MLX5_INLINE_MODE_L2;
1135 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1140 for (vport = 1; vport <= nvfs; vport++) {
1141 mlx5_query_nic_vport_min_inline(dev, vport, &mlx5_mode);
1142 if (vport > 1 && prev_mlx5_mode != mlx5_mode)
1144 prev_mlx5_mode = mlx5_mode;
1152 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink, u8 encap)
1154 struct mlx5_core_dev *dev = devlink_priv(devlink);
1155 struct mlx5_eswitch *esw = dev->priv.eswitch;
1158 err = mlx5_devlink_eswitch_check(devlink);
1162 if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
1163 (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, encap) ||
1164 !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap)))
1167 if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC)
1170 if (esw->mode == SRIOV_LEGACY) {
1171 esw->offloads.encap = encap;
1175 if (esw->offloads.encap == encap)
1178 if (esw->offloads.num_flows > 0) {
1179 esw_warn(dev, "Can't set encapsulation when flows are configured\n");
1183 esw_destroy_offloads_fast_fdb_table(esw);
1185 esw->offloads.encap = encap;
1186 err = esw_create_offloads_fast_fdb_table(esw);
1188 esw_warn(esw->dev, "Failed re-creating fast FDB table, err %d\n", err);
1189 esw->offloads.encap = !encap;
1190 (void)esw_create_offloads_fast_fdb_table(esw);
1195 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink, u8 *encap)
1197 struct mlx5_core_dev *dev = devlink_priv(devlink);
1198 struct mlx5_eswitch *esw = dev->priv.eswitch;
1201 err = mlx5_devlink_eswitch_check(devlink);
1205 *encap = esw->offloads.encap;
1209 void mlx5_eswitch_register_vport_rep(struct mlx5_eswitch *esw,
1211 struct mlx5_eswitch_rep_if *__rep_if,
1214 struct mlx5_esw_offload *offloads = &esw->offloads;
1215 struct mlx5_eswitch_rep_if *rep_if;
1217 rep_if = &offloads->vport_reps[vport_index].rep_if[rep_type];
1219 rep_if->load = __rep_if->load;
1220 rep_if->unload = __rep_if->unload;
1221 rep_if->get_proto_dev = __rep_if->get_proto_dev;
1222 rep_if->priv = __rep_if->priv;
1224 rep_if->valid = true;
1226 EXPORT_SYMBOL(mlx5_eswitch_register_vport_rep);
1228 void mlx5_eswitch_unregister_vport_rep(struct mlx5_eswitch *esw,
1229 int vport_index, u8 rep_type)
1231 struct mlx5_esw_offload *offloads = &esw->offloads;
1232 struct mlx5_eswitch_rep *rep;
1234 rep = &offloads->vport_reps[vport_index];
1236 if (esw->mode == SRIOV_OFFLOADS && esw->vports[vport_index].enabled)
1237 rep->rep_if[rep_type].unload(rep);
1239 rep->rep_if[rep_type].valid = false;
1241 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_rep);
1243 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
1245 #define UPLINK_REP_INDEX 0
1246 struct mlx5_esw_offload *offloads = &esw->offloads;
1247 struct mlx5_eswitch_rep *rep;
1249 rep = &offloads->vport_reps[UPLINK_REP_INDEX];
1250 return rep->rep_if[rep_type].priv;
1253 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
1257 struct mlx5_esw_offload *offloads = &esw->offloads;
1258 struct mlx5_eswitch_rep *rep;
1260 if (vport == FDB_UPLINK_VPORT)
1261 vport = UPLINK_REP_INDEX;
1263 rep = &offloads->vport_reps[vport];
1265 if (rep->rep_if[rep_type].valid &&
1266 rep->rep_if[rep_type].get_proto_dev)
1267 return rep->rep_if[rep_type].get_proto_dev(rep);
1270 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
1272 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
1274 return mlx5_eswitch_get_proto_dev(esw, UPLINK_REP_INDEX, rep_type);
1276 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
1278 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
1281 return &esw->offloads.vport_reps[vport];
1283 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);