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/idr.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/mlx5_ifc.h>
37 #include <linux/mlx5/vport.h>
38 #include <linux/mlx5/fs.h>
39 #include "mlx5_core.h"
41 #include "esw/indir_table.h"
42 #include "esw/acl/ofld.h"
46 #include "lib/devcom.h"
48 #include "lib/fs_chains.h"
50 #include "en/mapping.h"
54 #define mlx5_esw_for_each_rep(esw, i, rep) \
55 xa_for_each(&((esw)->offloads.vport_reps), i, rep)
57 #define mlx5_esw_for_each_sf_rep(esw, i, rep) \
58 xa_for_each_marked(&((esw)->offloads.vport_reps), i, rep, MLX5_ESW_VPT_SF)
60 #define mlx5_esw_for_each_vf_rep(esw, index, rep) \
61 mlx5_esw_for_each_entry_marked(&((esw)->offloads.vport_reps), index, \
62 rep, (esw)->esw_funcs.num_vfs, MLX5_ESW_VPT_VF)
64 /* There are two match-all miss flows, one for unicast dst mac and
67 #define MLX5_ESW_MISS_FLOWS (2)
68 #define UPLINK_REP_INDEX 0
70 #define MLX5_ESW_VPORT_TBL_SIZE 128
71 #define MLX5_ESW_VPORT_TBL_NUM_GROUPS 4
73 #define MLX5_ESW_FT_OFFLOADS_DROP_RULE (1)
75 static const struct esw_vport_tbl_namespace mlx5_esw_vport_tbl_mirror_ns = {
76 .max_fte = MLX5_ESW_VPORT_TBL_SIZE,
77 .max_num_groups = MLX5_ESW_VPORT_TBL_NUM_GROUPS,
81 static struct mlx5_eswitch_rep *mlx5_eswitch_get_rep(struct mlx5_eswitch *esw,
84 return xa_load(&esw->offloads.vport_reps, vport_num);
88 mlx5_eswitch_set_rule_flow_source(struct mlx5_eswitch *esw,
89 struct mlx5_flow_spec *spec,
90 struct mlx5_esw_flow_attr *attr)
92 if (!MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source) || !attr || !attr->in_rep)
96 spec->flow_context.flow_source = mlx5e_tc_int_port_get_flow_source(attr->int_port);
101 spec->flow_context.flow_source = (attr->in_rep->vport == MLX5_VPORT_UPLINK) ?
102 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK :
103 MLX5_FLOW_CONTEXT_FLOW_SOURCE_LOCAL_VPORT;
106 /* Actually only the upper 16 bits of reg c0 need to be cleared, but the lower 16 bits
107 * are not needed as well in the following process. So clear them all for simplicity.
110 mlx5_eswitch_clear_rule_source_port(struct mlx5_eswitch *esw, struct mlx5_flow_spec *spec)
112 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
115 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
116 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, 0);
118 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
119 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, 0);
121 if (!memchr_inv(misc2, 0, MLX5_ST_SZ_BYTES(fte_match_set_misc2)))
122 spec->match_criteria_enable &= ~MLX5_MATCH_MISC_PARAMETERS_2;
127 mlx5_eswitch_set_rule_source_port(struct mlx5_eswitch *esw,
128 struct mlx5_flow_spec *spec,
129 struct mlx5_flow_attr *attr,
130 struct mlx5_eswitch *src_esw,
133 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
138 /* Use metadata matching because vport is not represented by single
139 * VHCA in dual-port RoCE mode, and matching on source vport may fail.
141 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
142 if (mlx5_esw_indir_table_decap_vport(attr))
143 vport = mlx5_esw_indir_table_decap_vport(attr);
145 if (attr && !attr->chain && esw_attr->int_port)
147 mlx5e_tc_int_port_get_metadata_for_match(esw_attr->int_port);
150 mlx5_eswitch_get_vport_metadata_for_match(src_esw, vport);
152 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
153 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, metadata);
155 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
156 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0,
157 mlx5_eswitch_get_vport_metadata_mask());
159 spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
161 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
162 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
164 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
165 MLX5_SET(fte_match_set_misc, misc,
166 source_eswitch_owner_vhca_id,
167 MLX5_CAP_GEN(src_esw->dev, vhca_id));
169 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
170 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
171 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
172 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
173 source_eswitch_owner_vhca_id);
175 spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
180 esw_setup_decap_indir(struct mlx5_eswitch *esw,
181 struct mlx5_flow_attr *attr,
182 struct mlx5_flow_spec *spec)
184 struct mlx5_flow_table *ft;
186 if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
189 ft = mlx5_esw_indir_table_get(esw, attr, spec,
190 mlx5_esw_indir_table_decap_vport(attr), true);
191 return PTR_ERR_OR_ZERO(ft);
195 esw_cleanup_decap_indir(struct mlx5_eswitch *esw,
196 struct mlx5_flow_attr *attr)
198 if (mlx5_esw_indir_table_decap_vport(attr))
199 mlx5_esw_indir_table_put(esw, attr,
200 mlx5_esw_indir_table_decap_vport(attr),
205 esw_setup_sampler_dest(struct mlx5_flow_destination *dest,
206 struct mlx5_flow_act *flow_act,
210 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
211 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER;
212 dest[i].sampler_id = sampler_id;
218 esw_setup_ft_dest(struct mlx5_flow_destination *dest,
219 struct mlx5_flow_act *flow_act,
220 struct mlx5_eswitch *esw,
221 struct mlx5_flow_attr *attr,
222 struct mlx5_flow_spec *spec,
225 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
226 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
227 dest[i].ft = attr->dest_ft;
229 if (mlx5_esw_indir_table_decap_vport(attr))
230 return esw_setup_decap_indir(esw, attr, spec);
235 esw_setup_accept_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
236 struct mlx5_fs_chains *chains, int i)
238 if (mlx5_chains_ignore_flow_level_supported(chains))
239 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
240 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
241 dest[i].ft = mlx5_chains_get_tc_end_ft(chains);
245 esw_setup_slow_path_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
246 struct mlx5_eswitch *esw, int i)
248 if (MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level))
249 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
250 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
251 dest[i].ft = esw->fdb_table.offloads.slow_fdb;
255 esw_setup_chain_dest(struct mlx5_flow_destination *dest,
256 struct mlx5_flow_act *flow_act,
257 struct mlx5_fs_chains *chains,
258 u32 chain, u32 prio, u32 level,
261 struct mlx5_flow_table *ft;
263 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
264 ft = mlx5_chains_get_table(chains, chain, prio, level);
268 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
273 static void esw_put_dest_tables_loop(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr,
276 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
277 struct mlx5_fs_chains *chains = esw_chains(esw);
280 for (i = from; i < to; i++)
281 if (esw_attr->dests[i].flags & MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
282 mlx5_chains_put_table(chains, 0, 1, 0);
283 else if (mlx5_esw_indir_table_needed(esw, attr, esw_attr->dests[i].rep->vport,
284 esw_attr->dests[i].mdev))
285 mlx5_esw_indir_table_put(esw, attr, esw_attr->dests[i].rep->vport,
290 esw_is_chain_src_port_rewrite(struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr)
294 for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
295 if (esw_attr->dests[i].flags & MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
301 esw_setup_chain_src_port_rewrite(struct mlx5_flow_destination *dest,
302 struct mlx5_flow_act *flow_act,
303 struct mlx5_eswitch *esw,
304 struct mlx5_fs_chains *chains,
305 struct mlx5_flow_attr *attr,
308 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
311 if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
314 /* flow steering cannot handle more than one dest with the same ft
317 if (esw_attr->out_count - esw_attr->split_count > 1)
320 err = esw_setup_chain_dest(dest, flow_act, chains, attr->dest_chain, 1, 0, *i);
324 if (esw_attr->dests[esw_attr->split_count].pkt_reformat) {
325 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
326 flow_act->pkt_reformat = esw_attr->dests[esw_attr->split_count].pkt_reformat;
333 static void esw_cleanup_chain_src_port_rewrite(struct mlx5_eswitch *esw,
334 struct mlx5_flow_attr *attr)
336 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
338 esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, esw_attr->out_count);
342 esw_is_indir_table(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr)
344 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
348 /* Indirect table is supported only for flows with in_port uplink
349 * and the destination is vport on the same eswitch as the uplink,
350 * return false in case at least one of destinations doesn't meet
353 for (i = esw_attr->split_count; i < esw_attr->out_count; i++) {
354 if (esw_attr->dests[i].rep &&
355 mlx5_esw_indir_table_needed(esw, attr, esw_attr->dests[i].rep->vport,
356 esw_attr->dests[i].mdev)) {
367 esw_setup_indir_table(struct mlx5_flow_destination *dest,
368 struct mlx5_flow_act *flow_act,
369 struct mlx5_eswitch *esw,
370 struct mlx5_flow_attr *attr,
371 struct mlx5_flow_spec *spec,
372 bool ignore_flow_lvl,
375 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
378 if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
381 for (j = esw_attr->split_count; j < esw_attr->out_count; j++, (*i)++) {
383 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
384 dest[*i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
386 dest[*i].ft = mlx5_esw_indir_table_get(esw, attr, spec,
387 esw_attr->dests[j].rep->vport, false);
388 if (IS_ERR(dest[*i].ft)) {
389 err = PTR_ERR(dest[*i].ft);
390 goto err_indir_tbl_get;
394 if (mlx5_esw_indir_table_decap_vport(attr)) {
395 err = esw_setup_decap_indir(esw, attr, spec);
397 goto err_indir_tbl_get;
403 esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, j);
407 static void esw_cleanup_indir_table(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr)
409 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
411 esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, esw_attr->out_count);
412 esw_cleanup_decap_indir(esw, attr);
416 esw_cleanup_chain_dest(struct mlx5_fs_chains *chains, u32 chain, u32 prio, u32 level)
418 mlx5_chains_put_table(chains, chain, prio, level);
422 esw_setup_vport_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
423 struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
424 int attr_idx, int dest_idx, bool pkt_reformat)
426 dest[dest_idx].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
427 dest[dest_idx].vport.num = esw_attr->dests[attr_idx].rep->vport;
428 if (MLX5_CAP_ESW(esw->dev, merged_eswitch)) {
429 dest[dest_idx].vport.vhca_id =
430 MLX5_CAP_GEN(esw_attr->dests[attr_idx].mdev, vhca_id);
431 dest[dest_idx].vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
432 if (dest[dest_idx].vport.num == MLX5_VPORT_UPLINK &&
433 mlx5_lag_mpesw_is_activated(esw->dev))
434 dest[dest_idx].type = MLX5_FLOW_DESTINATION_TYPE_UPLINK;
436 if (esw_attr->dests[attr_idx].flags & MLX5_ESW_DEST_ENCAP_VALID) {
438 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
439 flow_act->pkt_reformat = esw_attr->dests[attr_idx].pkt_reformat;
441 dest[dest_idx].vport.flags |= MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
442 dest[dest_idx].vport.pkt_reformat = esw_attr->dests[attr_idx].pkt_reformat;
447 esw_setup_vport_dests(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
448 struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
453 for (j = esw_attr->split_count; j < esw_attr->out_count; j++, i++)
454 esw_setup_vport_dest(dest, flow_act, esw, esw_attr, j, i, true);
459 esw_src_port_rewrite_supported(struct mlx5_eswitch *esw)
461 return MLX5_CAP_GEN(esw->dev, reg_c_preserve) &&
462 mlx5_eswitch_vport_match_metadata_enabled(esw) &&
463 MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level);
467 esw_setup_dests(struct mlx5_flow_destination *dest,
468 struct mlx5_flow_act *flow_act,
469 struct mlx5_eswitch *esw,
470 struct mlx5_flow_attr *attr,
471 struct mlx5_flow_spec *spec,
474 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
475 struct mlx5_fs_chains *chains = esw_chains(esw);
478 if (!mlx5_eswitch_termtbl_required(esw, attr, flow_act, spec) &&
479 esw_src_port_rewrite_supported(esw))
480 attr->flags |= MLX5_ATTR_FLAG_SRC_REWRITE;
482 if (attr->flags & MLX5_ATTR_FLAG_SAMPLE &&
483 !(attr->flags & MLX5_ATTR_FLAG_SLOW_PATH)) {
484 esw_setup_sampler_dest(dest, flow_act, attr->sample_attr.sampler_id, *i);
486 } else if (attr->flags & MLX5_ATTR_FLAG_SLOW_PATH) {
487 esw_setup_slow_path_dest(dest, flow_act, esw, *i);
489 } else if (attr->flags & MLX5_ATTR_FLAG_ACCEPT) {
490 esw_setup_accept_dest(dest, flow_act, chains, *i);
492 } else if (esw_is_indir_table(esw, attr)) {
493 err = esw_setup_indir_table(dest, flow_act, esw, attr, spec, true, i);
494 } else if (esw_is_chain_src_port_rewrite(esw, esw_attr)) {
495 err = esw_setup_chain_src_port_rewrite(dest, flow_act, esw, chains, attr, i);
497 *i = esw_setup_vport_dests(dest, flow_act, esw, esw_attr, *i);
500 err = esw_setup_ft_dest(dest, flow_act, esw, attr, spec, *i);
502 } else if (attr->dest_chain) {
503 err = esw_setup_chain_dest(dest, flow_act, chains, attr->dest_chain,
513 esw_cleanup_dests(struct mlx5_eswitch *esw,
514 struct mlx5_flow_attr *attr)
516 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
517 struct mlx5_fs_chains *chains = esw_chains(esw);
520 esw_cleanup_decap_indir(esw, attr);
521 } else if (!mlx5e_tc_attr_flags_skip(attr->flags)) {
522 if (attr->dest_chain)
523 esw_cleanup_chain_dest(chains, attr->dest_chain, 1, 0);
524 else if (esw_is_indir_table(esw, attr))
525 esw_cleanup_indir_table(esw, attr);
526 else if (esw_is_chain_src_port_rewrite(esw, esw_attr))
527 esw_cleanup_chain_src_port_rewrite(esw, attr);
532 esw_setup_meter(struct mlx5_flow_attr *attr, struct mlx5_flow_act *flow_act)
534 struct mlx5e_flow_meter_handle *meter;
536 meter = attr->meter_attr.meter;
537 flow_act->exe_aso.type = attr->exe_aso_type;
538 flow_act->exe_aso.object_id = meter->obj_id;
539 flow_act->exe_aso.flow_meter.meter_idx = meter->idx;
540 flow_act->exe_aso.flow_meter.init_color = MLX5_FLOW_METER_COLOR_GREEN;
541 /* use metadata reg 5 for packet color */
542 flow_act->exe_aso.return_reg_id = 5;
545 struct mlx5_flow_handle *
546 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
547 struct mlx5_flow_spec *spec,
548 struct mlx5_flow_attr *attr)
550 struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
551 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
552 struct mlx5_fs_chains *chains = esw_chains(esw);
553 bool split = !!(esw_attr->split_count);
554 struct mlx5_vport_tbl_attr fwd_attr;
555 struct mlx5_flow_destination *dest;
556 struct mlx5_flow_handle *rule;
557 struct mlx5_flow_table *fdb;
560 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
561 return ERR_PTR(-EOPNOTSUPP);
563 dest = kcalloc(MLX5_MAX_FLOW_FWD_VPORTS + 1, sizeof(*dest), GFP_KERNEL);
565 return ERR_PTR(-ENOMEM);
567 flow_act.action = attr->action;
568 /* if per flow vlan pop/push is emulated, don't set that into the firmware */
569 if (!mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
570 flow_act.action &= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
571 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
572 else if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
573 flow_act.vlan[0].ethtype = ntohs(esw_attr->vlan_proto[0]);
574 flow_act.vlan[0].vid = esw_attr->vlan_vid[0];
575 flow_act.vlan[0].prio = esw_attr->vlan_prio[0];
576 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
577 flow_act.vlan[1].ethtype = ntohs(esw_attr->vlan_proto[1]);
578 flow_act.vlan[1].vid = esw_attr->vlan_vid[1];
579 flow_act.vlan[1].prio = esw_attr->vlan_prio[1];
583 mlx5_eswitch_set_rule_flow_source(esw, spec, esw_attr);
585 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
588 err = esw_setup_dests(dest, &flow_act, esw, attr, spec, &i);
591 goto err_create_goto_table;
595 if (esw_attr->decap_pkt_reformat)
596 flow_act.pkt_reformat = esw_attr->decap_pkt_reformat;
598 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
599 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
600 dest[i].counter_id = mlx5_fc_id(attr->counter);
604 if (attr->outer_match_level != MLX5_MATCH_NONE)
605 spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
606 if (attr->inner_match_level != MLX5_MATCH_NONE)
607 spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
609 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
610 flow_act.modify_hdr = attr->modify_hdr;
612 if ((flow_act.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) &&
613 attr->exe_aso_type == MLX5_EXE_ASO_FLOW_METER)
614 esw_setup_meter(attr, &flow_act);
617 fwd_attr.chain = attr->chain;
618 fwd_attr.prio = attr->prio;
619 fwd_attr.vport = esw_attr->in_rep->vport;
620 fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
622 fdb = mlx5_esw_vporttbl_get(esw, &fwd_attr);
624 if (attr->chain || attr->prio)
625 fdb = mlx5_chains_get_table(chains, attr->chain,
630 if (!(attr->flags & MLX5_ATTR_FLAG_NO_IN_PORT))
631 mlx5_eswitch_set_rule_source_port(esw, spec, attr,
632 esw_attr->in_mdev->priv.eswitch,
633 esw_attr->in_rep->vport);
636 rule = ERR_CAST(fdb);
640 if (mlx5_eswitch_termtbl_required(esw, attr, &flow_act, spec))
641 rule = mlx5_eswitch_add_termtbl_rule(esw, fdb, spec, esw_attr,
644 rule = mlx5_add_flow_rules(fdb, spec, &flow_act, dest, i);
648 atomic64_inc(&esw->offloads.num_flows);
655 mlx5_esw_vporttbl_put(esw, &fwd_attr);
656 else if (attr->chain || attr->prio)
657 mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
659 esw_cleanup_dests(esw, attr);
660 err_create_goto_table:
665 struct mlx5_flow_handle *
666 mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch *esw,
667 struct mlx5_flow_spec *spec,
668 struct mlx5_flow_attr *attr)
670 struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
671 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
672 struct mlx5_fs_chains *chains = esw_chains(esw);
673 struct mlx5_vport_tbl_attr fwd_attr;
674 struct mlx5_flow_destination *dest;
675 struct mlx5_flow_table *fast_fdb;
676 struct mlx5_flow_table *fwd_fdb;
677 struct mlx5_flow_handle *rule;
680 dest = kcalloc(MLX5_MAX_FLOW_FWD_VPORTS + 1, sizeof(*dest), GFP_KERNEL);
682 return ERR_PTR(-ENOMEM);
684 fast_fdb = mlx5_chains_get_table(chains, attr->chain, attr->prio, 0);
685 if (IS_ERR(fast_fdb)) {
686 rule = ERR_CAST(fast_fdb);
690 fwd_attr.chain = attr->chain;
691 fwd_attr.prio = attr->prio;
692 fwd_attr.vport = esw_attr->in_rep->vport;
693 fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
694 fwd_fdb = mlx5_esw_vporttbl_get(esw, &fwd_attr);
695 if (IS_ERR(fwd_fdb)) {
696 rule = ERR_CAST(fwd_fdb);
700 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
701 for (i = 0; i < esw_attr->split_count; i++) {
702 if (esw_is_indir_table(esw, attr))
703 err = esw_setup_indir_table(dest, &flow_act, esw, attr, spec, false, &i);
704 else if (esw_is_chain_src_port_rewrite(esw, esw_attr))
705 err = esw_setup_chain_src_port_rewrite(dest, &flow_act, esw, chains, attr,
708 esw_setup_vport_dest(dest, &flow_act, esw, esw_attr, i, i, false);
712 goto err_chain_src_rewrite;
715 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
716 dest[i].ft = fwd_fdb;
719 mlx5_eswitch_set_rule_source_port(esw, spec, attr,
720 esw_attr->in_mdev->priv.eswitch,
721 esw_attr->in_rep->vport);
723 if (attr->outer_match_level != MLX5_MATCH_NONE)
724 spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
726 flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
727 rule = mlx5_add_flow_rules(fast_fdb, spec, &flow_act, dest, i);
730 i = esw_attr->split_count;
731 goto err_chain_src_rewrite;
734 atomic64_inc(&esw->offloads.num_flows);
738 err_chain_src_rewrite:
739 esw_put_dest_tables_loop(esw, attr, 0, i);
740 mlx5_esw_vporttbl_put(esw, &fwd_attr);
742 mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
749 __mlx5_eswitch_del_rule(struct mlx5_eswitch *esw,
750 struct mlx5_flow_handle *rule,
751 struct mlx5_flow_attr *attr,
754 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
755 struct mlx5_fs_chains *chains = esw_chains(esw);
756 bool split = (esw_attr->split_count > 0);
757 struct mlx5_vport_tbl_attr fwd_attr;
760 mlx5_del_flow_rules(rule);
762 if (!mlx5e_tc_attr_flags_skip(attr->flags)) {
763 /* unref the term table */
764 for (i = 0; i < MLX5_MAX_FLOW_FWD_VPORTS; i++) {
765 if (esw_attr->dests[i].termtbl)
766 mlx5_eswitch_termtbl_put(esw, esw_attr->dests[i].termtbl);
770 atomic64_dec(&esw->offloads.num_flows);
772 if (fwd_rule || split) {
773 fwd_attr.chain = attr->chain;
774 fwd_attr.prio = attr->prio;
775 fwd_attr.vport = esw_attr->in_rep->vport;
776 fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
780 mlx5_esw_vporttbl_put(esw, &fwd_attr);
781 mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
782 esw_put_dest_tables_loop(esw, attr, 0, esw_attr->split_count);
785 mlx5_esw_vporttbl_put(esw, &fwd_attr);
786 else if (attr->chain || attr->prio)
787 mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
788 esw_cleanup_dests(esw, attr);
793 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
794 struct mlx5_flow_handle *rule,
795 struct mlx5_flow_attr *attr)
797 __mlx5_eswitch_del_rule(esw, rule, attr, false);
801 mlx5_eswitch_del_fwd_rule(struct mlx5_eswitch *esw,
802 struct mlx5_flow_handle *rule,
803 struct mlx5_flow_attr *attr)
805 __mlx5_eswitch_del_rule(esw, rule, attr, true);
808 static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
810 struct mlx5_eswitch_rep *rep;
814 esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
815 mlx5_esw_for_each_host_func_vport(esw, i, rep, esw->esw_funcs.num_vfs) {
816 if (atomic_read(&rep->rep_data[REP_ETH].state) != REP_LOADED)
819 err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
828 static struct mlx5_eswitch_rep *
829 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
831 struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;
833 in_rep = attr->in_rep;
834 out_rep = attr->dests[0].rep;
846 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
847 bool push, bool pop, bool fwd)
849 struct mlx5_eswitch_rep *in_rep, *out_rep;
851 if ((push || pop) && !fwd)
854 in_rep = attr->in_rep;
855 out_rep = attr->dests[0].rep;
857 if (push && in_rep->vport == MLX5_VPORT_UPLINK)
860 if (pop && out_rep->vport == MLX5_VPORT_UPLINK)
863 /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
864 if (!push && !pop && fwd)
865 if (in_rep->vlan && out_rep->vport == MLX5_VPORT_UPLINK)
868 /* protects against (1) setting rules with different vlans to push and
869 * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
871 if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan_vid[0]))
880 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
881 struct mlx5_flow_attr *attr)
883 struct offloads_fdb *offloads = &esw->fdb_table.offloads;
884 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
885 struct mlx5_eswitch_rep *vport = NULL;
889 /* nop if we're on the vlan push/pop non emulation mode */
890 if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
893 push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
894 pop = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
895 fwd = !!((attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
898 mutex_lock(&esw->state_lock);
900 err = esw_add_vlan_action_check(esw_attr, push, pop, fwd);
904 attr->flags &= ~MLX5_ATTR_FLAG_VLAN_HANDLED;
906 vport = esw_vlan_action_get_vport(esw_attr, push, pop);
908 if (!push && !pop && fwd) {
909 /* tracks VF --> wire rules without vlan push action */
910 if (esw_attr->dests[0].rep->vport == MLX5_VPORT_UPLINK) {
911 vport->vlan_refcount++;
912 attr->flags |= MLX5_ATTR_FLAG_VLAN_HANDLED;
921 if (!(offloads->vlan_push_pop_refcount)) {
922 /* it's the 1st vlan rule, apply global vlan pop policy */
923 err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
927 offloads->vlan_push_pop_refcount++;
930 if (vport->vlan_refcount)
933 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, esw_attr->vlan_vid[0],
934 0, SET_VLAN_INSERT | SET_VLAN_STRIP);
937 vport->vlan = esw_attr->vlan_vid[0];
939 vport->vlan_refcount++;
943 attr->flags |= MLX5_ATTR_FLAG_VLAN_HANDLED;
945 mutex_unlock(&esw->state_lock);
949 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
950 struct mlx5_flow_attr *attr)
952 struct offloads_fdb *offloads = &esw->fdb_table.offloads;
953 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
954 struct mlx5_eswitch_rep *vport = NULL;
958 /* nop if we're on the vlan push/pop non emulation mode */
959 if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
962 if (!(attr->flags & MLX5_ATTR_FLAG_VLAN_HANDLED))
965 push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
966 pop = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
967 fwd = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
969 mutex_lock(&esw->state_lock);
971 vport = esw_vlan_action_get_vport(esw_attr, push, pop);
973 if (!push && !pop && fwd) {
974 /* tracks VF --> wire rules without vlan push action */
975 if (esw_attr->dests[0].rep->vport == MLX5_VPORT_UPLINK)
976 vport->vlan_refcount--;
982 vport->vlan_refcount--;
983 if (vport->vlan_refcount)
984 goto skip_unset_push;
987 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
988 0, 0, SET_VLAN_STRIP);
994 offloads->vlan_push_pop_refcount--;
995 if (offloads->vlan_push_pop_refcount)
998 /* no more vlan rules, stop global vlan pop policy */
999 err = esw_set_global_vlan_pop(esw, 0);
1002 mutex_unlock(&esw->state_lock);
1006 struct mlx5_flow_handle *
1007 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *on_esw,
1008 struct mlx5_eswitch *from_esw,
1009 struct mlx5_eswitch_rep *rep,
1012 struct mlx5_flow_act flow_act = {0};
1013 struct mlx5_flow_destination dest = {};
1014 struct mlx5_flow_handle *flow_rule;
1015 struct mlx5_flow_spec *spec;
1018 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1020 flow_rule = ERR_PTR(-ENOMEM);
1024 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
1025 MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
1026 /* source vport is the esw manager */
1027 MLX5_SET(fte_match_set_misc, misc, source_port, from_esw->manager_vport);
1028 if (MLX5_CAP_ESW(on_esw->dev, merged_eswitch))
1029 MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
1030 MLX5_CAP_GEN(from_esw->dev, vhca_id));
1032 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
1033 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
1034 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
1035 if (MLX5_CAP_ESW(on_esw->dev, merged_eswitch))
1036 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
1037 source_eswitch_owner_vhca_id);
1039 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
1040 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1041 dest.vport.num = rep->vport;
1042 dest.vport.vhca_id = MLX5_CAP_GEN(rep->esw->dev, vhca_id);
1043 dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
1044 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1046 if (rep->vport == MLX5_VPORT_UPLINK)
1047 spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_LOCAL_VPORT;
1049 flow_rule = mlx5_add_flow_rules(on_esw->fdb_table.offloads.slow_fdb,
1050 spec, &flow_act, &dest, 1);
1051 if (IS_ERR(flow_rule))
1052 esw_warn(on_esw->dev, "FDB: Failed to add send to vport rule err %ld\n",
1053 PTR_ERR(flow_rule));
1058 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
1060 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
1062 mlx5_del_flow_rules(rule);
1065 void mlx5_eswitch_del_send_to_vport_meta_rule(struct mlx5_flow_handle *rule)
1068 mlx5_del_flow_rules(rule);
1071 struct mlx5_flow_handle *
1072 mlx5_eswitch_add_send_to_vport_meta_rule(struct mlx5_eswitch *esw, u16 vport_num)
1074 struct mlx5_flow_destination dest = {};
1075 struct mlx5_flow_act flow_act = {0};
1076 struct mlx5_flow_handle *flow_rule;
1077 struct mlx5_flow_spec *spec;
1079 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1081 return ERR_PTR(-ENOMEM);
1083 MLX5_SET(fte_match_param, spec->match_criteria,
1084 misc_parameters_2.metadata_reg_c_0, mlx5_eswitch_get_vport_metadata_mask());
1085 MLX5_SET(fte_match_param, spec->match_criteria,
1086 misc_parameters_2.metadata_reg_c_1, ESW_TUN_MASK);
1087 MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.metadata_reg_c_1,
1088 ESW_TUN_SLOW_TABLE_GOTO_VPORT_MARK);
1090 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1091 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1092 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1094 MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.metadata_reg_c_0,
1095 mlx5_eswitch_get_vport_metadata_for_match(esw, vport_num));
1096 dest.vport.num = vport_num;
1098 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1099 spec, &flow_act, &dest, 1);
1100 if (IS_ERR(flow_rule))
1101 esw_warn(esw->dev, "FDB: Failed to add send to vport meta rule vport %d, err %ld\n",
1102 vport_num, PTR_ERR(flow_rule));
1108 static bool mlx5_eswitch_reg_c1_loopback_supported(struct mlx5_eswitch *esw)
1110 return MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
1111 MLX5_FDB_TO_VPORT_REG_C_1;
1114 static int esw_set_passing_vport_metadata(struct mlx5_eswitch *esw, bool enable)
1116 u32 out[MLX5_ST_SZ_DW(query_esw_vport_context_out)] = {};
1117 u32 min[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
1118 u32 in[MLX5_ST_SZ_DW(query_esw_vport_context_in)] = {};
1122 if (!mlx5_eswitch_reg_c1_loopback_supported(esw) &&
1123 !mlx5_eswitch_vport_match_metadata_enabled(esw))
1126 MLX5_SET(query_esw_vport_context_in, in, opcode,
1127 MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT);
1128 err = mlx5_cmd_exec_inout(esw->dev, query_esw_vport_context, in, out);
1132 curr = MLX5_GET(query_esw_vport_context_out, out,
1133 esw_vport_context.fdb_to_vport_reg_c_id);
1134 wanted = MLX5_FDB_TO_VPORT_REG_C_0;
1135 if (mlx5_eswitch_reg_c1_loopback_supported(esw))
1136 wanted |= MLX5_FDB_TO_VPORT_REG_C_1;
1143 MLX5_SET(modify_esw_vport_context_in, min,
1144 esw_vport_context.fdb_to_vport_reg_c_id, curr);
1145 MLX5_SET(modify_esw_vport_context_in, min,
1146 field_select.fdb_to_vport_reg_c_id, 1);
1148 err = mlx5_eswitch_modify_esw_vport_context(esw->dev, 0, false, min);
1150 if (enable && (curr & MLX5_FDB_TO_VPORT_REG_C_1))
1151 esw->flags |= MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
1153 esw->flags &= ~MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
1159 static void peer_miss_rules_setup(struct mlx5_eswitch *esw,
1160 struct mlx5_core_dev *peer_dev,
1161 struct mlx5_flow_spec *spec,
1162 struct mlx5_flow_destination *dest)
1166 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1167 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1169 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1170 mlx5_eswitch_get_vport_metadata_mask());
1172 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1174 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1177 MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
1178 MLX5_CAP_GEN(peer_dev, vhca_id));
1180 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
1182 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1184 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
1185 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
1186 source_eswitch_owner_vhca_id);
1189 dest->type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1190 dest->vport.num = peer_dev->priv.eswitch->manager_vport;
1191 dest->vport.vhca_id = MLX5_CAP_GEN(peer_dev, vhca_id);
1192 dest->vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
1195 static void esw_set_peer_miss_rule_source_port(struct mlx5_eswitch *esw,
1196 struct mlx5_eswitch *peer_esw,
1197 struct mlx5_flow_spec *spec,
1202 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1203 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1205 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1206 mlx5_eswitch_get_vport_metadata_for_match(peer_esw,
1209 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1211 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
1215 static int esw_add_fdb_peer_miss_rules(struct mlx5_eswitch *esw,
1216 struct mlx5_core_dev *peer_dev)
1218 struct mlx5_flow_destination dest = {};
1219 struct mlx5_flow_act flow_act = {0};
1220 struct mlx5_flow_handle **flows;
1221 /* total vports is the same for both e-switches */
1222 int nvports = esw->total_vports;
1223 struct mlx5_flow_handle *flow;
1224 struct mlx5_flow_spec *spec;
1225 struct mlx5_vport *vport;
1230 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1234 peer_miss_rules_setup(esw, peer_dev, spec, &dest);
1236 flows = kvcalloc(nvports, sizeof(*flows), GFP_KERNEL);
1239 goto alloc_flows_err;
1242 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1243 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1246 if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1247 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1248 esw_set_peer_miss_rule_source_port(esw, peer_dev->priv.eswitch,
1249 spec, MLX5_VPORT_PF);
1251 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1252 spec, &flow_act, &dest, 1);
1254 err = PTR_ERR(flow);
1255 goto add_pf_flow_err;
1257 flows[vport->index] = flow;
1260 if (mlx5_ecpf_vport_exists(esw->dev)) {
1261 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1262 MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_ECPF);
1263 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1264 spec, &flow_act, &dest, 1);
1266 err = PTR_ERR(flow);
1267 goto add_ecpf_flow_err;
1269 flows[vport->index] = flow;
1272 mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev)) {
1273 esw_set_peer_miss_rule_source_port(esw,
1274 peer_dev->priv.eswitch,
1275 spec, vport->vport);
1277 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1278 spec, &flow_act, &dest, 1);
1280 err = PTR_ERR(flow);
1281 goto add_vf_flow_err;
1283 flows[vport->index] = flow;
1286 esw->fdb_table.offloads.peer_miss_rules = flows;
1292 mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev)) {
1293 if (!flows[vport->index])
1295 mlx5_del_flow_rules(flows[vport->index]);
1297 if (mlx5_ecpf_vport_exists(esw->dev)) {
1298 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1299 mlx5_del_flow_rules(flows[vport->index]);
1302 if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1303 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1304 mlx5_del_flow_rules(flows[vport->index]);
1307 esw_warn(esw->dev, "FDB: Failed to add peer miss flow rule err %d\n", err);
1314 static void esw_del_fdb_peer_miss_rules(struct mlx5_eswitch *esw)
1316 struct mlx5_flow_handle **flows;
1317 struct mlx5_vport *vport;
1320 flows = esw->fdb_table.offloads.peer_miss_rules;
1322 mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev))
1323 mlx5_del_flow_rules(flows[vport->index]);
1325 if (mlx5_ecpf_vport_exists(esw->dev)) {
1326 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1327 mlx5_del_flow_rules(flows[vport->index]);
1330 if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1331 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1332 mlx5_del_flow_rules(flows[vport->index]);
1337 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
1339 struct mlx5_flow_act flow_act = {0};
1340 struct mlx5_flow_destination dest = {};
1341 struct mlx5_flow_handle *flow_rule = NULL;
1342 struct mlx5_flow_spec *spec;
1349 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1355 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1356 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1358 dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
1359 outer_headers.dmac_47_16);
1362 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1363 dest.vport.num = esw->manager_vport;
1364 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1366 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1367 spec, &flow_act, &dest, 1);
1368 if (IS_ERR(flow_rule)) {
1369 err = PTR_ERR(flow_rule);
1370 esw_warn(esw->dev, "FDB: Failed to add unicast miss flow rule err %d\n", err);
1374 esw->fdb_table.offloads.miss_rule_uni = flow_rule;
1376 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1378 dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
1379 outer_headers.dmac_47_16);
1381 flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1382 spec, &flow_act, &dest, 1);
1383 if (IS_ERR(flow_rule)) {
1384 err = PTR_ERR(flow_rule);
1385 esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
1386 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1390 esw->fdb_table.offloads.miss_rule_multi = flow_rule;
1397 struct mlx5_flow_handle *
1398 esw_add_restore_rule(struct mlx5_eswitch *esw, u32 tag)
1400 struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
1401 struct mlx5_flow_table *ft = esw->offloads.ft_offloads_restore;
1402 struct mlx5_flow_context *flow_context;
1403 struct mlx5_flow_handle *flow_rule;
1404 struct mlx5_flow_destination dest;
1405 struct mlx5_flow_spec *spec;
1408 if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
1409 return ERR_PTR(-EOPNOTSUPP);
1411 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1413 return ERR_PTR(-ENOMEM);
1415 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1417 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1418 ESW_REG_C0_USER_DATA_METADATA_MASK);
1419 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1421 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0, tag);
1422 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1423 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1424 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1425 flow_act.modify_hdr = esw->offloads.restore_copy_hdr_id;
1427 flow_context = &spec->flow_context;
1428 flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
1429 flow_context->flow_tag = tag;
1430 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1431 dest.ft = esw->offloads.ft_offloads;
1433 flow_rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1);
1436 if (IS_ERR(flow_rule))
1438 "Failed to create restore rule for tag: %d, err(%d)\n",
1439 tag, (int)PTR_ERR(flow_rule));
1444 #define MAX_PF_SQ 256
1445 #define MAX_SQ_NVPORTS 32
1447 static void esw_set_flow_group_source_port(struct mlx5_eswitch *esw,
1450 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1454 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1455 MLX5_SET(create_flow_group_in, flow_group_in,
1456 match_criteria_enable,
1457 MLX5_MATCH_MISC_PARAMETERS_2);
1459 MLX5_SET(fte_match_param, match_criteria,
1460 misc_parameters_2.metadata_reg_c_0,
1461 mlx5_eswitch_get_vport_metadata_mask());
1463 MLX5_SET(create_flow_group_in, flow_group_in,
1464 match_criteria_enable,
1465 MLX5_MATCH_MISC_PARAMETERS);
1467 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1468 misc_parameters.source_port);
1472 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
1473 static void esw_vport_tbl_put(struct mlx5_eswitch *esw)
1475 struct mlx5_vport_tbl_attr attr;
1476 struct mlx5_vport *vport;
1481 mlx5_esw_for_each_vport(esw, i, vport) {
1482 attr.vport = vport->vport;
1483 attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
1484 mlx5_esw_vporttbl_put(esw, &attr);
1488 static int esw_vport_tbl_get(struct mlx5_eswitch *esw)
1490 struct mlx5_vport_tbl_attr attr;
1491 struct mlx5_flow_table *fdb;
1492 struct mlx5_vport *vport;
1497 mlx5_esw_for_each_vport(esw, i, vport) {
1498 attr.vport = vport->vport;
1499 attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
1500 fdb = mlx5_esw_vporttbl_get(esw, &attr);
1507 esw_vport_tbl_put(esw);
1508 return PTR_ERR(fdb);
1511 #define fdb_modify_header_fwd_to_table_supported(esw) \
1512 (MLX5_CAP_ESW_FLOWTABLE((esw)->dev, fdb_modify_header_fwd_to_table))
1513 static void esw_init_chains_offload_flags(struct mlx5_eswitch *esw, u32 *flags)
1515 struct mlx5_core_dev *dev = esw->dev;
1517 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ignore_flow_level))
1518 *flags |= MLX5_CHAINS_IGNORE_FLOW_LEVEL_SUPPORTED;
1520 if (!MLX5_CAP_ESW_FLOWTABLE(dev, multi_fdb_encap) &&
1521 esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE) {
1522 *flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1523 esw_warn(dev, "Tc chains and priorities offload aren't supported, update firmware if needed\n");
1524 } else if (!mlx5_eswitch_reg_c1_loopback_enabled(esw)) {
1525 *flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1526 esw_warn(dev, "Tc chains and priorities offload aren't supported\n");
1527 } else if (!fdb_modify_header_fwd_to_table_supported(esw)) {
1528 /* Disabled when ttl workaround is needed, e.g
1529 * when ESWITCH_IPV4_TTL_MODIFY_ENABLE = true in mlxconfig
1532 "Tc chains and priorities offload aren't supported, check firmware version, or mlxconfig settings\n");
1533 *flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1535 *flags |= MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1536 esw_info(dev, "Supported tc chains and prios offload\n");
1539 if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
1540 *flags |= MLX5_CHAINS_FT_TUNNEL_SUPPORTED;
1544 esw_chains_create(struct mlx5_eswitch *esw, struct mlx5_flow_table *miss_fdb)
1546 struct mlx5_core_dev *dev = esw->dev;
1547 struct mlx5_flow_table *nf_ft, *ft;
1548 struct mlx5_chains_attr attr = {};
1549 struct mlx5_fs_chains *chains;
1553 fdb_max = 1 << MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size);
1555 esw_init_chains_offload_flags(esw, &attr.flags);
1556 attr.ns = MLX5_FLOW_NAMESPACE_FDB;
1557 attr.max_ft_sz = fdb_max;
1558 attr.max_grp_num = esw->params.large_group_num;
1559 attr.default_ft = miss_fdb;
1560 attr.mapping = esw->offloads.reg_c0_obj_pool;
1562 chains = mlx5_chains_create(dev, &attr);
1563 if (IS_ERR(chains)) {
1564 err = PTR_ERR(chains);
1565 esw_warn(dev, "Failed to create fdb chains err(%d)\n", err);
1569 esw->fdb_table.offloads.esw_chains_priv = chains;
1571 /* Create tc_end_ft which is the always created ft chain */
1572 nf_ft = mlx5_chains_get_table(chains, mlx5_chains_get_nf_ft_chain(chains),
1574 if (IS_ERR(nf_ft)) {
1575 err = PTR_ERR(nf_ft);
1579 /* Always open the root for fast path */
1580 ft = mlx5_chains_get_table(chains, 0, 1, 0);
1586 /* Open level 1 for split fdb rules now if prios isn't supported */
1587 if (!mlx5_chains_prios_supported(chains)) {
1588 err = esw_vport_tbl_get(esw);
1593 mlx5_chains_set_end_ft(chains, nf_ft);
1598 mlx5_chains_put_table(chains, 0, 1, 0);
1600 mlx5_chains_put_table(chains, mlx5_chains_get_nf_ft_chain(chains), 1, 0);
1602 mlx5_chains_destroy(chains);
1603 esw->fdb_table.offloads.esw_chains_priv = NULL;
1609 esw_chains_destroy(struct mlx5_eswitch *esw, struct mlx5_fs_chains *chains)
1611 if (!mlx5_chains_prios_supported(chains))
1612 esw_vport_tbl_put(esw);
1613 mlx5_chains_put_table(chains, 0, 1, 0);
1614 mlx5_chains_put_table(chains, mlx5_chains_get_nf_ft_chain(chains), 1, 0);
1615 mlx5_chains_destroy(chains);
1618 #else /* CONFIG_MLX5_CLS_ACT */
1621 esw_chains_create(struct mlx5_eswitch *esw, struct mlx5_flow_table *miss_fdb)
1625 esw_chains_destroy(struct mlx5_eswitch *esw, struct mlx5_fs_chains *chains)
1631 esw_create_send_to_vport_group(struct mlx5_eswitch *esw,
1632 struct mlx5_flow_table *fdb,
1636 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1637 struct mlx5_flow_group *g;
1638 void *match_criteria;
1641 memset(flow_group_in, 0, inlen);
1643 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1644 MLX5_MATCH_MISC_PARAMETERS);
1646 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1648 MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
1649 MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
1650 if (MLX5_CAP_ESW(esw->dev, merged_eswitch)) {
1651 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1652 misc_parameters.source_eswitch_owner_vhca_id);
1653 MLX5_SET(create_flow_group_in, flow_group_in,
1654 source_eswitch_owner_vhca_id_valid, 1);
1657 /* See comment at table_size calculation */
1658 count = MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ);
1659 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1660 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, *ix + count - 1);
1663 g = mlx5_create_flow_group(fdb, flow_group_in);
1666 esw_warn(esw->dev, "Failed to create send-to-vport flow group err(%d)\n", err);
1669 esw->fdb_table.offloads.send_to_vport_grp = g;
1676 esw_create_meta_send_to_vport_group(struct mlx5_eswitch *esw,
1677 struct mlx5_flow_table *fdb,
1681 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1682 struct mlx5_flow_group *g;
1683 void *match_criteria;
1686 if (!esw_src_port_rewrite_supported(esw))
1689 memset(flow_group_in, 0, inlen);
1691 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1692 MLX5_MATCH_MISC_PARAMETERS_2);
1694 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1696 MLX5_SET(fte_match_param, match_criteria,
1697 misc_parameters_2.metadata_reg_c_0,
1698 mlx5_eswitch_get_vport_metadata_mask());
1699 MLX5_SET(fte_match_param, match_criteria,
1700 misc_parameters_2.metadata_reg_c_1, ESW_TUN_MASK);
1702 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1703 MLX5_SET(create_flow_group_in, flow_group_in,
1704 end_flow_index, *ix + esw->total_vports - 1);
1705 *ix += esw->total_vports;
1707 g = mlx5_create_flow_group(fdb, flow_group_in);
1711 "Failed to create send-to-vport meta flow group err(%d)\n", err);
1712 goto send_vport_meta_err;
1714 esw->fdb_table.offloads.send_to_vport_meta_grp = g;
1718 send_vport_meta_err:
1723 esw_create_peer_esw_miss_group(struct mlx5_eswitch *esw,
1724 struct mlx5_flow_table *fdb,
1728 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1729 struct mlx5_flow_group *g;
1730 void *match_criteria;
1733 if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
1736 memset(flow_group_in, 0, inlen);
1738 esw_set_flow_group_source_port(esw, flow_group_in);
1740 if (!mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1741 match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1745 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1746 misc_parameters.source_eswitch_owner_vhca_id);
1748 MLX5_SET(create_flow_group_in, flow_group_in,
1749 source_eswitch_owner_vhca_id_valid, 1);
1752 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1753 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1754 *ix + esw->total_vports - 1);
1755 *ix += esw->total_vports;
1757 g = mlx5_create_flow_group(fdb, flow_group_in);
1760 esw_warn(esw->dev, "Failed to create peer miss flow group err(%d)\n", err);
1763 esw->fdb_table.offloads.peer_miss_grp = g;
1770 esw_create_miss_group(struct mlx5_eswitch *esw,
1771 struct mlx5_flow_table *fdb,
1775 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1776 struct mlx5_flow_group *g;
1777 void *match_criteria;
1781 memset(flow_group_in, 0, inlen);
1783 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1784 MLX5_MATCH_OUTER_HEADERS);
1785 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
1787 dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
1788 outer_headers.dmac_47_16);
1791 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1792 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1793 *ix + MLX5_ESW_MISS_FLOWS);
1795 g = mlx5_create_flow_group(fdb, flow_group_in);
1798 esw_warn(esw->dev, "Failed to create miss flow group err(%d)\n", err);
1801 esw->fdb_table.offloads.miss_grp = g;
1803 err = esw_add_fdb_miss_rule(esw);
1810 mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1815 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw)
1817 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1818 struct mlx5_flow_table_attr ft_attr = {};
1819 struct mlx5_core_dev *dev = esw->dev;
1820 struct mlx5_flow_namespace *root_ns;
1821 struct mlx5_flow_table *fdb = NULL;
1822 int table_size, ix = 0, err = 0;
1823 u32 flags = 0, *flow_group_in;
1825 esw_debug(esw->dev, "Create offloads FDB Tables\n");
1827 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1831 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
1833 esw_warn(dev, "Failed to get FDB flow namespace\n");
1837 esw->fdb_table.offloads.ns = root_ns;
1838 err = mlx5_flow_namespace_set_mode(root_ns,
1839 esw->dev->priv.steering->mode);
1841 esw_warn(dev, "Failed to set FDB namespace steering mode\n");
1845 /* To be strictly correct:
1846 * MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ)
1848 * esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ +
1849 * peer_esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ
1850 * but as the peer device might not be in switchdev mode it's not
1851 * possible. We use the fact that by default FW sets max vfs and max sfs
1852 * to the same value on both devices. If it needs to be changed in the future note
1853 * the peer miss group should also be created based on the number of
1854 * total vports of the peer (currently is also uses esw->total_vports).
1856 table_size = MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ) +
1857 esw->total_vports * 2 + MLX5_ESW_MISS_FLOWS;
1859 /* create the slow path fdb with encap set, so further table instances
1860 * can be created at run time while VFs are probed if the FW allows that.
1862 if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
1863 flags |= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
1864 MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
1866 ft_attr.flags = flags;
1867 ft_attr.max_fte = table_size;
1868 ft_attr.prio = FDB_SLOW_PATH;
1870 fdb = mlx5_create_flow_table(root_ns, &ft_attr);
1873 esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
1876 esw->fdb_table.offloads.slow_fdb = fdb;
1878 /* Create empty TC-miss managed table. This allows plugging in following
1879 * priorities without directly exposing their level 0 table to
1880 * eswitch_offloads and passing it as miss_fdb to following call to
1881 * esw_chains_create().
1883 memset(&ft_attr, 0, sizeof(ft_attr));
1884 ft_attr.prio = FDB_TC_MISS;
1885 esw->fdb_table.offloads.tc_miss_table = mlx5_create_flow_table(root_ns, &ft_attr);
1886 if (IS_ERR(esw->fdb_table.offloads.tc_miss_table)) {
1887 err = PTR_ERR(esw->fdb_table.offloads.tc_miss_table);
1888 esw_warn(dev, "Failed to create TC miss FDB Table err %d\n", err);
1889 goto tc_miss_table_err;
1892 err = esw_chains_create(esw, esw->fdb_table.offloads.tc_miss_table);
1894 esw_warn(dev, "Failed to open fdb chains err(%d)\n", err);
1895 goto fdb_chains_err;
1898 err = esw_create_send_to_vport_group(esw, fdb, flow_group_in, &ix);
1900 goto send_vport_err;
1902 err = esw_create_meta_send_to_vport_group(esw, fdb, flow_group_in, &ix);
1904 goto send_vport_meta_err;
1906 err = esw_create_peer_esw_miss_group(esw, fdb, flow_group_in, &ix);
1910 err = esw_create_miss_group(esw, fdb, flow_group_in, &ix);
1914 kvfree(flow_group_in);
1918 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
1919 mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1921 if (esw->fdb_table.offloads.send_to_vport_meta_grp)
1922 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_meta_grp);
1923 send_vport_meta_err:
1924 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1926 esw_chains_destroy(esw, esw_chains(esw));
1928 mlx5_destroy_flow_table(esw->fdb_table.offloads.tc_miss_table);
1930 mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
1932 /* Holds true only as long as DMFS is the default */
1933 mlx5_flow_namespace_set_mode(root_ns, MLX5_FLOW_STEERING_MODE_DMFS);
1935 kvfree(flow_group_in);
1939 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
1941 if (!esw->fdb_table.offloads.slow_fdb)
1944 esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
1945 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
1946 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1947 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1948 if (esw->fdb_table.offloads.send_to_vport_meta_grp)
1949 mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_meta_grp);
1950 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
1951 mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1952 mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1954 esw_chains_destroy(esw, esw_chains(esw));
1956 mlx5_destroy_flow_table(esw->fdb_table.offloads.tc_miss_table);
1957 mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
1958 /* Holds true only as long as DMFS is the default */
1959 mlx5_flow_namespace_set_mode(esw->fdb_table.offloads.ns,
1960 MLX5_FLOW_STEERING_MODE_DMFS);
1961 atomic64_set(&esw->user_count, 0);
1964 static int esw_get_nr_ft_offloads_steering_src_ports(struct mlx5_eswitch *esw)
1968 nvports = esw->total_vports + MLX5_ESW_MISS_FLOWS;
1969 if (mlx5e_tc_int_port_supported(esw))
1970 nvports += MLX5E_TC_MAX_INT_PORT_NUM;
1975 static int esw_create_offloads_table(struct mlx5_eswitch *esw)
1977 struct mlx5_flow_table_attr ft_attr = {};
1978 struct mlx5_core_dev *dev = esw->dev;
1979 struct mlx5_flow_table *ft_offloads;
1980 struct mlx5_flow_namespace *ns;
1983 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
1985 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
1989 ft_attr.max_fte = esw_get_nr_ft_offloads_steering_src_ports(esw) +
1990 MLX5_ESW_FT_OFFLOADS_DROP_RULE;
1993 ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
1994 if (IS_ERR(ft_offloads)) {
1995 err = PTR_ERR(ft_offloads);
1996 esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
2000 esw->offloads.ft_offloads = ft_offloads;
2004 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
2006 struct mlx5_esw_offload *offloads = &esw->offloads;
2008 mlx5_destroy_flow_table(offloads->ft_offloads);
2011 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
2013 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2014 struct mlx5_flow_group *g;
2019 nvports = esw_get_nr_ft_offloads_steering_src_ports(esw);
2020 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2024 /* create vport rx group */
2025 esw_set_flow_group_source_port(esw, flow_group_in);
2027 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2028 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
2030 g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
2034 mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
2038 esw->offloads.vport_rx_group = g;
2040 kvfree(flow_group_in);
2044 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
2046 mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
2049 static int esw_create_vport_rx_drop_rule_index(struct mlx5_eswitch *esw)
2051 /* ft_offloads table is enlarged by MLX5_ESW_FT_OFFLOADS_DROP_RULE (1)
2052 * for the drop rule, which is placed at the end of the table.
2053 * So return the total of vport and int_port as rule index.
2055 return esw_get_nr_ft_offloads_steering_src_ports(esw);
2058 static int esw_create_vport_rx_drop_group(struct mlx5_eswitch *esw)
2060 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2061 struct mlx5_flow_group *g;
2066 flow_index = esw_create_vport_rx_drop_rule_index(esw);
2068 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2072 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, flow_index);
2073 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, flow_index);
2075 g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
2079 mlx5_core_warn(esw->dev, "Failed to create vport rx drop group err %d\n", err);
2083 esw->offloads.vport_rx_drop_group = g;
2085 kvfree(flow_group_in);
2089 static void esw_destroy_vport_rx_drop_group(struct mlx5_eswitch *esw)
2091 if (esw->offloads.vport_rx_drop_group)
2092 mlx5_destroy_flow_group(esw->offloads.vport_rx_drop_group);
2095 struct mlx5_flow_handle *
2096 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, u16 vport,
2097 struct mlx5_flow_destination *dest)
2099 struct mlx5_flow_act flow_act = {0};
2100 struct mlx5_flow_handle *flow_rule;
2101 struct mlx5_flow_spec *spec;
2104 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2106 flow_rule = ERR_PTR(-ENOMEM);
2110 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
2111 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
2112 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2113 mlx5_eswitch_get_vport_metadata_for_match(esw, vport));
2115 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
2116 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2117 mlx5_eswitch_get_vport_metadata_mask());
2119 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
2121 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
2122 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
2124 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
2125 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2127 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
2130 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2131 flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
2132 &flow_act, dest, 1);
2133 if (IS_ERR(flow_rule)) {
2134 esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
2143 static int esw_create_vport_rx_drop_rule(struct mlx5_eswitch *esw)
2145 struct mlx5_flow_act flow_act = {};
2146 struct mlx5_flow_handle *flow_rule;
2148 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2149 flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, NULL,
2150 &flow_act, NULL, 0);
2151 if (IS_ERR(flow_rule)) {
2153 "fs offloads: Failed to add vport rx drop rule err %ld\n",
2154 PTR_ERR(flow_rule));
2155 return PTR_ERR(flow_rule);
2158 esw->offloads.vport_rx_drop_rule = flow_rule;
2163 static void esw_destroy_vport_rx_drop_rule(struct mlx5_eswitch *esw)
2165 if (esw->offloads.vport_rx_drop_rule)
2166 mlx5_del_flow_rules(esw->offloads.vport_rx_drop_rule);
2169 static int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, u8 *mode)
2171 u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
2172 struct mlx5_core_dev *dev = esw->dev;
2173 struct mlx5_vport *vport;
2176 if (!MLX5_CAP_GEN(dev, vport_group_manager))
2179 if (!mlx5_esw_is_fdb_created(esw))
2182 switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
2183 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
2184 mlx5_mode = MLX5_INLINE_MODE_NONE;
2186 case MLX5_CAP_INLINE_MODE_L2:
2187 mlx5_mode = MLX5_INLINE_MODE_L2;
2189 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
2194 mlx5_query_nic_vport_min_inline(dev, esw->first_host_vport, &prev_mlx5_mode);
2195 mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
2196 mlx5_query_nic_vport_min_inline(dev, vport->vport, &mlx5_mode);
2197 if (prev_mlx5_mode != mlx5_mode)
2199 prev_mlx5_mode = mlx5_mode;
2207 static void esw_destroy_restore_table(struct mlx5_eswitch *esw)
2209 struct mlx5_esw_offload *offloads = &esw->offloads;
2211 if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
2214 mlx5_modify_header_dealloc(esw->dev, offloads->restore_copy_hdr_id);
2215 mlx5_destroy_flow_group(offloads->restore_group);
2216 mlx5_destroy_flow_table(offloads->ft_offloads_restore);
2219 static int esw_create_restore_table(struct mlx5_eswitch *esw)
2221 u8 modact[MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto)] = {};
2222 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2223 struct mlx5_flow_table_attr ft_attr = {};
2224 struct mlx5_core_dev *dev = esw->dev;
2225 struct mlx5_flow_namespace *ns;
2226 struct mlx5_modify_hdr *mod_hdr;
2227 void *match_criteria, *misc;
2228 struct mlx5_flow_table *ft;
2229 struct mlx5_flow_group *g;
2233 if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
2236 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
2238 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
2242 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2243 if (!flow_group_in) {
2248 ft_attr.max_fte = 1 << ESW_REG_C0_USER_DATA_METADATA_BITS;
2249 ft = mlx5_create_flow_table(ns, &ft_attr);
2252 esw_warn(esw->dev, "Failed to create restore table, err %d\n",
2257 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
2259 misc = MLX5_ADDR_OF(fte_match_param, match_criteria,
2262 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2263 ESW_REG_C0_USER_DATA_METADATA_MASK);
2264 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2265 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
2266 ft_attr.max_fte - 1);
2267 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
2268 MLX5_MATCH_MISC_PARAMETERS_2);
2269 g = mlx5_create_flow_group(ft, flow_group_in);
2272 esw_warn(dev, "Failed to create restore flow group, err: %d\n",
2277 MLX5_SET(copy_action_in, modact, action_type, MLX5_ACTION_TYPE_COPY);
2278 MLX5_SET(copy_action_in, modact, src_field,
2279 MLX5_ACTION_IN_FIELD_METADATA_REG_C_1);
2280 MLX5_SET(copy_action_in, modact, dst_field,
2281 MLX5_ACTION_IN_FIELD_METADATA_REG_B);
2282 mod_hdr = mlx5_modify_header_alloc(esw->dev,
2283 MLX5_FLOW_NAMESPACE_KERNEL, 1,
2285 if (IS_ERR(mod_hdr)) {
2286 err = PTR_ERR(mod_hdr);
2287 esw_warn(dev, "Failed to create restore mod header, err: %d\n",
2292 esw->offloads.ft_offloads_restore = ft;
2293 esw->offloads.restore_group = g;
2294 esw->offloads.restore_copy_hdr_id = mod_hdr;
2296 kvfree(flow_group_in);
2301 mlx5_destroy_flow_group(g);
2303 mlx5_destroy_flow_table(ft);
2305 kvfree(flow_group_in);
2310 static int esw_offloads_start(struct mlx5_eswitch *esw,
2311 struct netlink_ext_ack *extack)
2315 esw->mode = MLX5_ESWITCH_OFFLOADS;
2316 err = mlx5_eswitch_enable_locked(esw, esw->dev->priv.sriov.num_vfs);
2318 NL_SET_ERR_MSG_MOD(extack,
2319 "Failed setting eswitch to offloads");
2320 esw->mode = MLX5_ESWITCH_LEGACY;
2321 mlx5_rescan_drivers(esw->dev);
2323 if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
2324 if (mlx5_eswitch_inline_mode_get(esw,
2325 &esw->offloads.inline_mode)) {
2326 esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
2327 NL_SET_ERR_MSG_MOD(extack,
2328 "Inline mode is different between vports");
2334 static void mlx5_esw_offloads_rep_mark_set(struct mlx5_eswitch *esw,
2335 struct mlx5_eswitch_rep *rep,
2340 /* Copy the mark from vport to its rep */
2341 mark_set = xa_get_mark(&esw->vports, rep->vport, mark);
2343 xa_set_mark(&esw->offloads.vport_reps, rep->vport, mark);
2346 static int mlx5_esw_offloads_rep_init(struct mlx5_eswitch *esw, const struct mlx5_vport *vport)
2348 struct mlx5_eswitch_rep *rep;
2352 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
2356 rep->vport = vport->vport;
2357 rep->vport_index = vport->index;
2358 for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
2359 atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
2361 err = xa_insert(&esw->offloads.vport_reps, rep->vport, rep, GFP_KERNEL);
2365 mlx5_esw_offloads_rep_mark_set(esw, rep, MLX5_ESW_VPT_HOST_FN);
2366 mlx5_esw_offloads_rep_mark_set(esw, rep, MLX5_ESW_VPT_VF);
2367 mlx5_esw_offloads_rep_mark_set(esw, rep, MLX5_ESW_VPT_SF);
2375 static void mlx5_esw_offloads_rep_cleanup(struct mlx5_eswitch *esw,
2376 struct mlx5_eswitch_rep *rep)
2378 xa_erase(&esw->offloads.vport_reps, rep->vport);
2382 void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
2384 struct mlx5_eswitch_rep *rep;
2387 mlx5_esw_for_each_rep(esw, i, rep)
2388 mlx5_esw_offloads_rep_cleanup(esw, rep);
2389 xa_destroy(&esw->offloads.vport_reps);
2392 int esw_offloads_init_reps(struct mlx5_eswitch *esw)
2394 struct mlx5_vport *vport;
2398 xa_init(&esw->offloads.vport_reps);
2400 mlx5_esw_for_each_vport(esw, i, vport) {
2401 err = mlx5_esw_offloads_rep_init(esw, vport);
2408 esw_offloads_cleanup_reps(esw);
2412 static void __esw_offloads_unload_rep(struct mlx5_eswitch *esw,
2413 struct mlx5_eswitch_rep *rep, u8 rep_type)
2415 if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2416 REP_LOADED, REP_REGISTERED) == REP_LOADED)
2417 esw->offloads.rep_ops[rep_type]->unload(rep);
2420 static void __unload_reps_sf_vport(struct mlx5_eswitch *esw, u8 rep_type)
2422 struct mlx5_eswitch_rep *rep;
2425 mlx5_esw_for_each_sf_rep(esw, i, rep)
2426 __esw_offloads_unload_rep(esw, rep, rep_type);
2429 static void __unload_reps_all_vport(struct mlx5_eswitch *esw, u8 rep_type)
2431 struct mlx5_eswitch_rep *rep;
2434 __unload_reps_sf_vport(esw, rep_type);
2436 mlx5_esw_for_each_vf_rep(esw, i, rep)
2437 __esw_offloads_unload_rep(esw, rep, rep_type);
2439 if (mlx5_ecpf_vport_exists(esw->dev)) {
2440 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_ECPF);
2441 __esw_offloads_unload_rep(esw, rep, rep_type);
2444 if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
2445 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_PF);
2446 __esw_offloads_unload_rep(esw, rep, rep_type);
2449 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
2450 __esw_offloads_unload_rep(esw, rep, rep_type);
2453 int mlx5_esw_offloads_rep_load(struct mlx5_eswitch *esw, u16 vport_num)
2455 struct mlx5_eswitch_rep *rep;
2459 rep = mlx5_eswitch_get_rep(esw, vport_num);
2460 for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
2461 if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2462 REP_REGISTERED, REP_LOADED) == REP_REGISTERED) {
2463 err = esw->offloads.rep_ops[rep_type]->load(esw->dev, rep);
2471 atomic_set(&rep->rep_data[rep_type].state, REP_REGISTERED);
2472 for (--rep_type; rep_type >= 0; rep_type--)
2473 __esw_offloads_unload_rep(esw, rep, rep_type);
2477 void mlx5_esw_offloads_rep_unload(struct mlx5_eswitch *esw, u16 vport_num)
2479 struct mlx5_eswitch_rep *rep;
2482 rep = mlx5_eswitch_get_rep(esw, vport_num);
2483 for (rep_type = NUM_REP_TYPES - 1; rep_type >= 0; rep_type--)
2484 __esw_offloads_unload_rep(esw, rep, rep_type);
2487 int esw_offloads_load_rep(struct mlx5_eswitch *esw, u16 vport_num)
2491 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2494 if (vport_num != MLX5_VPORT_UPLINK) {
2495 err = mlx5_esw_offloads_devlink_port_register(esw, vport_num);
2500 err = mlx5_esw_offloads_rep_load(esw, vport_num);
2506 if (vport_num != MLX5_VPORT_UPLINK)
2507 mlx5_esw_offloads_devlink_port_unregister(esw, vport_num);
2511 void esw_offloads_unload_rep(struct mlx5_eswitch *esw, u16 vport_num)
2513 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2516 mlx5_esw_offloads_rep_unload(esw, vport_num);
2518 if (vport_num != MLX5_VPORT_UPLINK)
2519 mlx5_esw_offloads_devlink_port_unregister(esw, vport_num);
2522 static int esw_set_slave_root_fdb(struct mlx5_core_dev *master,
2523 struct mlx5_core_dev *slave)
2525 u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {};
2526 u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {};
2527 struct mlx5_flow_root_namespace *root;
2528 struct mlx5_flow_namespace *ns;
2531 MLX5_SET(set_flow_table_root_in, in, opcode,
2532 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
2533 MLX5_SET(set_flow_table_root_in, in, table_type,
2537 ns = mlx5_get_flow_namespace(master,
2538 MLX5_FLOW_NAMESPACE_FDB);
2539 root = find_root(&ns->node);
2540 mutex_lock(&root->chain_lock);
2541 MLX5_SET(set_flow_table_root_in, in,
2542 table_eswitch_owner_vhca_id_valid, 1);
2543 MLX5_SET(set_flow_table_root_in, in,
2544 table_eswitch_owner_vhca_id,
2545 MLX5_CAP_GEN(master, vhca_id));
2546 MLX5_SET(set_flow_table_root_in, in, table_id,
2549 ns = mlx5_get_flow_namespace(slave,
2550 MLX5_FLOW_NAMESPACE_FDB);
2551 root = find_root(&ns->node);
2552 mutex_lock(&root->chain_lock);
2553 MLX5_SET(set_flow_table_root_in, in, table_id,
2557 err = mlx5_cmd_exec(slave, in, sizeof(in), out, sizeof(out));
2558 mutex_unlock(&root->chain_lock);
2563 static int __esw_set_master_egress_rule(struct mlx5_core_dev *master,
2564 struct mlx5_core_dev *slave,
2565 struct mlx5_vport *vport,
2566 struct mlx5_flow_table *acl)
2568 struct mlx5_flow_handle *flow_rule = NULL;
2569 struct mlx5_flow_destination dest = {};
2570 struct mlx5_flow_act flow_act = {};
2571 struct mlx5_flow_spec *spec;
2575 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2579 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
2580 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2582 MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_UPLINK);
2583 MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
2584 MLX5_CAP_GEN(slave, vhca_id));
2586 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
2587 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2588 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
2589 source_eswitch_owner_vhca_id);
2591 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2592 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
2593 dest.vport.num = slave->priv.eswitch->manager_vport;
2594 dest.vport.vhca_id = MLX5_CAP_GEN(slave, vhca_id);
2595 dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
2597 flow_rule = mlx5_add_flow_rules(acl, spec, &flow_act,
2599 if (IS_ERR(flow_rule))
2600 err = PTR_ERR(flow_rule);
2602 vport->egress.offloads.bounce_rule = flow_rule;
2608 static int esw_set_master_egress_rule(struct mlx5_core_dev *master,
2609 struct mlx5_core_dev *slave)
2611 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2612 struct mlx5_eswitch *esw = master->priv.eswitch;
2613 struct mlx5_flow_table_attr ft_attr = {
2614 .max_fte = 1, .prio = 0, .level = 0,
2615 .flags = MLX5_FLOW_TABLE_OTHER_VPORT,
2617 struct mlx5_flow_namespace *egress_ns;
2618 struct mlx5_flow_table *acl;
2619 struct mlx5_flow_group *g;
2620 struct mlx5_vport *vport;
2621 void *match_criteria;
2625 vport = mlx5_eswitch_get_vport(esw, esw->manager_vport);
2627 return PTR_ERR(vport);
2629 egress_ns = mlx5_get_flow_vport_acl_namespace(master,
2630 MLX5_FLOW_NAMESPACE_ESW_EGRESS,
2635 if (vport->egress.acl)
2638 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2642 acl = mlx5_create_vport_flow_table(egress_ns, &ft_attr, vport->vport);
2648 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
2650 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2651 misc_parameters.source_port);
2652 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2653 misc_parameters.source_eswitch_owner_vhca_id);
2654 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
2655 MLX5_MATCH_MISC_PARAMETERS);
2657 MLX5_SET(create_flow_group_in, flow_group_in,
2658 source_eswitch_owner_vhca_id_valid, 1);
2659 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2660 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 0);
2662 g = mlx5_create_flow_group(acl, flow_group_in);
2668 err = __esw_set_master_egress_rule(master, slave, vport, acl);
2672 vport->egress.acl = acl;
2673 vport->egress.offloads.bounce_grp = g;
2675 kvfree(flow_group_in);
2680 mlx5_destroy_flow_group(g);
2682 mlx5_destroy_flow_table(acl);
2684 kvfree(flow_group_in);
2688 static void esw_unset_master_egress_rule(struct mlx5_core_dev *dev)
2690 struct mlx5_vport *vport;
2692 vport = mlx5_eswitch_get_vport(dev->priv.eswitch,
2693 dev->priv.eswitch->manager_vport);
2695 esw_acl_egress_ofld_cleanup(vport);
2698 int mlx5_eswitch_offloads_config_single_fdb(struct mlx5_eswitch *master_esw,
2699 struct mlx5_eswitch *slave_esw)
2703 err = esw_set_slave_root_fdb(master_esw->dev,
2708 err = esw_set_master_egress_rule(master_esw->dev,
2716 esw_set_slave_root_fdb(NULL, slave_esw->dev);
2721 void mlx5_eswitch_offloads_destroy_single_fdb(struct mlx5_eswitch *master_esw,
2722 struct mlx5_eswitch *slave_esw)
2724 esw_unset_master_egress_rule(master_esw->dev);
2725 esw_set_slave_root_fdb(NULL, slave_esw->dev);
2728 #define ESW_OFFLOADS_DEVCOM_PAIR (0)
2729 #define ESW_OFFLOADS_DEVCOM_UNPAIR (1)
2731 static void mlx5_esw_offloads_rep_event_unpair(struct mlx5_eswitch *esw)
2733 const struct mlx5_eswitch_rep_ops *ops;
2734 struct mlx5_eswitch_rep *rep;
2738 mlx5_esw_for_each_rep(esw, i, rep) {
2739 rep_type = NUM_REP_TYPES;
2740 while (rep_type--) {
2741 ops = esw->offloads.rep_ops[rep_type];
2742 if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2744 ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_UNPAIR, NULL);
2749 static void mlx5_esw_offloads_unpair(struct mlx5_eswitch *esw)
2751 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
2752 mlx5e_tc_clean_fdb_peer_flows(esw);
2754 mlx5_esw_offloads_rep_event_unpair(esw);
2755 esw_del_fdb_peer_miss_rules(esw);
2758 static int mlx5_esw_offloads_pair(struct mlx5_eswitch *esw,
2759 struct mlx5_eswitch *peer_esw)
2761 const struct mlx5_eswitch_rep_ops *ops;
2762 struct mlx5_eswitch_rep *rep;
2767 err = esw_add_fdb_peer_miss_rules(esw, peer_esw->dev);
2771 mlx5_esw_for_each_rep(esw, i, rep) {
2772 for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
2773 ops = esw->offloads.rep_ops[rep_type];
2774 if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2776 err = ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_PAIR, peer_esw);
2786 mlx5_esw_offloads_unpair(esw);
2790 static int mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch *esw,
2791 struct mlx5_eswitch *peer_esw,
2794 struct mlx5_flow_root_namespace *peer_ns;
2795 struct mlx5_flow_root_namespace *ns;
2798 peer_ns = peer_esw->dev->priv.steering->fdb_root_ns;
2799 ns = esw->dev->priv.steering->fdb_root_ns;
2802 err = mlx5_flow_namespace_set_peer(ns, peer_ns);
2806 err = mlx5_flow_namespace_set_peer(peer_ns, ns);
2808 mlx5_flow_namespace_set_peer(ns, NULL);
2812 mlx5_flow_namespace_set_peer(ns, NULL);
2813 mlx5_flow_namespace_set_peer(peer_ns, NULL);
2819 static int mlx5_esw_offloads_devcom_event(int event,
2823 struct mlx5_eswitch *esw = my_data;
2824 struct mlx5_devcom *devcom = esw->dev->priv.devcom;
2825 struct mlx5_eswitch *peer_esw = event_data;
2829 case ESW_OFFLOADS_DEVCOM_PAIR:
2830 if (mlx5_eswitch_vport_match_metadata_enabled(esw) !=
2831 mlx5_eswitch_vport_match_metadata_enabled(peer_esw))
2834 err = mlx5_esw_offloads_set_ns_peer(esw, peer_esw, true);
2837 err = mlx5_esw_offloads_pair(esw, peer_esw);
2841 err = mlx5_esw_offloads_pair(peer_esw, esw);
2845 mlx5_devcom_set_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS, true);
2848 case ESW_OFFLOADS_DEVCOM_UNPAIR:
2849 if (!mlx5_devcom_is_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS))
2852 mlx5_devcom_set_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS, false);
2853 mlx5_esw_offloads_unpair(peer_esw);
2854 mlx5_esw_offloads_unpair(esw);
2855 mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
2862 mlx5_esw_offloads_unpair(esw);
2864 mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
2866 mlx5_core_err(esw->dev, "esw offloads devcom event failure, event %u err %d",
2871 static void esw_offloads_devcom_init(struct mlx5_eswitch *esw)
2873 struct mlx5_devcom *devcom = esw->dev->priv.devcom;
2875 INIT_LIST_HEAD(&esw->offloads.peer_flows);
2876 mutex_init(&esw->offloads.peer_mutex);
2878 if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
2881 if (!mlx5_is_lag_supported(esw->dev))
2884 mlx5_devcom_register_component(devcom,
2885 MLX5_DEVCOM_ESW_OFFLOADS,
2886 mlx5_esw_offloads_devcom_event,
2889 mlx5_devcom_send_event(devcom,
2890 MLX5_DEVCOM_ESW_OFFLOADS,
2891 ESW_OFFLOADS_DEVCOM_PAIR, esw);
2894 static void esw_offloads_devcom_cleanup(struct mlx5_eswitch *esw)
2896 struct mlx5_devcom *devcom = esw->dev->priv.devcom;
2898 if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
2901 if (!mlx5_is_lag_supported(esw->dev))
2904 mlx5_devcom_send_event(devcom, MLX5_DEVCOM_ESW_OFFLOADS,
2905 ESW_OFFLOADS_DEVCOM_UNPAIR, esw);
2907 mlx5_devcom_unregister_component(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
2910 bool mlx5_esw_vport_match_metadata_supported(const struct mlx5_eswitch *esw)
2912 if (!MLX5_CAP_ESW(esw->dev, esw_uplink_ingress_acl))
2915 if (!(MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
2916 MLX5_FDB_TO_VPORT_REG_C_0))
2919 if (!MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
2925 #define MLX5_ESW_METADATA_RSVD_UPLINK 1
2927 /* Share the same metadata for uplink's. This is fine because:
2928 * (a) In shared FDB mode (LAG) both uplink's are treated the
2929 * same and tagged with the same metadata.
2930 * (b) In non shared FDB mode, packets from physical port0
2931 * cannot hit eswitch of PF1 and vice versa.
2933 static u32 mlx5_esw_match_metadata_reserved(struct mlx5_eswitch *esw)
2935 return MLX5_ESW_METADATA_RSVD_UPLINK;
2938 u32 mlx5_esw_match_metadata_alloc(struct mlx5_eswitch *esw)
2940 u32 vport_end_ida = (1 << ESW_VPORT_BITS) - 1;
2941 /* Reserve 0xf for internal port offload */
2942 u32 max_pf_num = (1 << ESW_PFNUM_BITS) - 2;
2946 /* Only 4 bits of pf_num */
2947 pf_num = mlx5_get_dev_index(esw->dev);
2948 if (pf_num > max_pf_num)
2951 /* Metadata is 4 bits of PFNUM and 12 bits of unique id */
2952 /* Use only non-zero vport_id (2-4095) for all PF's */
2953 id = ida_alloc_range(&esw->offloads.vport_metadata_ida,
2954 MLX5_ESW_METADATA_RSVD_UPLINK + 1,
2955 vport_end_ida, GFP_KERNEL);
2958 id = (pf_num << ESW_VPORT_BITS) | id;
2962 void mlx5_esw_match_metadata_free(struct mlx5_eswitch *esw, u32 metadata)
2964 u32 vport_bit_mask = (1 << ESW_VPORT_BITS) - 1;
2966 /* Metadata contains only 12 bits of actual ida id */
2967 ida_free(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
2970 static int esw_offloads_vport_metadata_setup(struct mlx5_eswitch *esw,
2971 struct mlx5_vport *vport)
2973 if (vport->vport == MLX5_VPORT_UPLINK)
2974 vport->default_metadata = mlx5_esw_match_metadata_reserved(esw);
2976 vport->default_metadata = mlx5_esw_match_metadata_alloc(esw);
2978 vport->metadata = vport->default_metadata;
2979 return vport->metadata ? 0 : -ENOSPC;
2982 static void esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch *esw,
2983 struct mlx5_vport *vport)
2985 if (!vport->default_metadata)
2988 if (vport->vport == MLX5_VPORT_UPLINK)
2991 WARN_ON(vport->metadata != vport->default_metadata);
2992 mlx5_esw_match_metadata_free(esw, vport->default_metadata);
2995 static void esw_offloads_metadata_uninit(struct mlx5_eswitch *esw)
2997 struct mlx5_vport *vport;
3000 if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3003 mlx5_esw_for_each_vport(esw, i, vport)
3004 esw_offloads_vport_metadata_cleanup(esw, vport);
3007 static int esw_offloads_metadata_init(struct mlx5_eswitch *esw)
3009 struct mlx5_vport *vport;
3013 if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3016 mlx5_esw_for_each_vport(esw, i, vport) {
3017 err = esw_offloads_vport_metadata_setup(esw, vport);
3025 esw_offloads_metadata_uninit(esw);
3029 int mlx5_esw_offloads_vport_metadata_set(struct mlx5_eswitch *esw, bool enable)
3033 down_write(&esw->mode_lock);
3034 if (mlx5_esw_is_fdb_created(esw)) {
3038 if (!mlx5_esw_vport_match_metadata_supported(esw)) {
3043 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
3045 esw->flags &= ~MLX5_ESWITCH_VPORT_MATCH_METADATA;
3047 up_write(&esw->mode_lock);
3052 esw_vport_create_offloads_acl_tables(struct mlx5_eswitch *esw,
3053 struct mlx5_vport *vport)
3057 err = esw_acl_ingress_ofld_setup(esw, vport);
3061 err = esw_acl_egress_ofld_setup(esw, vport);
3068 esw_acl_ingress_ofld_cleanup(esw, vport);
3073 esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch *esw,
3074 struct mlx5_vport *vport)
3076 esw_acl_egress_ofld_cleanup(vport);
3077 esw_acl_ingress_ofld_cleanup(esw, vport);
3080 static int esw_create_uplink_offloads_acl_tables(struct mlx5_eswitch *esw)
3082 struct mlx5_vport *vport;
3084 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3086 return PTR_ERR(vport);
3088 return esw_vport_create_offloads_acl_tables(esw, vport);
3091 static void esw_destroy_uplink_offloads_acl_tables(struct mlx5_eswitch *esw)
3093 struct mlx5_vport *vport;
3095 vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3099 esw_vport_destroy_offloads_acl_tables(esw, vport);
3102 int mlx5_eswitch_reload_reps(struct mlx5_eswitch *esw)
3104 struct mlx5_eswitch_rep *rep;
3108 if (!esw || esw->mode != MLX5_ESWITCH_OFFLOADS)
3111 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
3112 if (atomic_read(&rep->rep_data[REP_ETH].state) != REP_LOADED)
3115 ret = mlx5_esw_offloads_rep_load(esw, MLX5_VPORT_UPLINK);
3119 mlx5_esw_for_each_rep(esw, i, rep) {
3120 if (atomic_read(&rep->rep_data[REP_ETH].state) == REP_LOADED)
3121 mlx5_esw_offloads_rep_load(esw, rep->vport);
3127 static int esw_offloads_steering_init(struct mlx5_eswitch *esw)
3129 struct mlx5_esw_indir_table *indir;
3132 memset(&esw->fdb_table.offloads, 0, sizeof(struct offloads_fdb));
3133 mutex_init(&esw->fdb_table.offloads.vports.lock);
3134 hash_init(esw->fdb_table.offloads.vports.table);
3135 atomic64_set(&esw->user_count, 0);
3137 indir = mlx5_esw_indir_table_init();
3138 if (IS_ERR(indir)) {
3139 err = PTR_ERR(indir);
3140 goto create_indir_err;
3142 esw->fdb_table.offloads.indir = indir;
3144 err = esw_create_uplink_offloads_acl_tables(esw);
3146 goto create_acl_err;
3148 err = esw_create_offloads_table(esw);
3150 goto create_offloads_err;
3152 err = esw_create_restore_table(esw);
3154 goto create_restore_err;
3156 err = esw_create_offloads_fdb_tables(esw);
3158 goto create_fdb_err;
3160 err = esw_create_vport_rx_group(esw);
3164 err = esw_create_vport_rx_drop_group(esw);
3166 goto create_rx_drop_fg_err;
3168 err = esw_create_vport_rx_drop_rule(esw);
3170 goto create_rx_drop_rule_err;
3174 create_rx_drop_rule_err:
3175 esw_destroy_vport_rx_drop_group(esw);
3176 create_rx_drop_fg_err:
3177 esw_destroy_vport_rx_group(esw);
3179 esw_destroy_offloads_fdb_tables(esw);
3181 esw_destroy_restore_table(esw);
3183 esw_destroy_offloads_table(esw);
3184 create_offloads_err:
3185 esw_destroy_uplink_offloads_acl_tables(esw);
3187 mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3189 mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3193 static void esw_offloads_steering_cleanup(struct mlx5_eswitch *esw)
3195 esw_destroy_vport_rx_drop_rule(esw);
3196 esw_destroy_vport_rx_drop_group(esw);
3197 esw_destroy_vport_rx_group(esw);
3198 esw_destroy_offloads_fdb_tables(esw);
3199 esw_destroy_restore_table(esw);
3200 esw_destroy_offloads_table(esw);
3201 esw_destroy_uplink_offloads_acl_tables(esw);
3202 mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3203 mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3207 esw_vfs_changed_event_handler(struct mlx5_eswitch *esw, const u32 *out)
3209 struct devlink *devlink;
3210 bool host_pf_disabled;
3213 new_num_vfs = MLX5_GET(query_esw_functions_out, out,
3214 host_params_context.host_num_of_vfs);
3215 host_pf_disabled = MLX5_GET(query_esw_functions_out, out,
3216 host_params_context.host_pf_disabled);
3218 if (new_num_vfs == esw->esw_funcs.num_vfs || host_pf_disabled)
3221 devlink = priv_to_devlink(esw->dev);
3223 /* Number of VFs can only change from "0 to x" or "x to 0". */
3224 if (esw->esw_funcs.num_vfs > 0) {
3225 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
3229 err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
3230 MLX5_VPORT_UC_ADDR_CHANGE);
3232 devl_unlock(devlink);
3236 esw->esw_funcs.num_vfs = new_num_vfs;
3237 devl_unlock(devlink);
3240 static void esw_functions_changed_event_handler(struct work_struct *work)
3242 struct mlx5_host_work *host_work;
3243 struct mlx5_eswitch *esw;
3246 host_work = container_of(work, struct mlx5_host_work, work);
3247 esw = host_work->esw;
3249 out = mlx5_esw_query_functions(esw->dev);
3253 esw_vfs_changed_event_handler(esw, out);
3259 int mlx5_esw_funcs_changed_handler(struct notifier_block *nb, unsigned long type, void *data)
3261 struct mlx5_esw_functions *esw_funcs;
3262 struct mlx5_host_work *host_work;
3263 struct mlx5_eswitch *esw;
3265 host_work = kzalloc(sizeof(*host_work), GFP_ATOMIC);
3269 esw_funcs = mlx5_nb_cof(nb, struct mlx5_esw_functions, nb);
3270 esw = container_of(esw_funcs, struct mlx5_eswitch, esw_funcs);
3272 host_work->esw = esw;
3274 INIT_WORK(&host_work->work, esw_functions_changed_event_handler);
3275 queue_work(esw->work_queue, &host_work->work);
3280 static int mlx5_esw_host_number_init(struct mlx5_eswitch *esw)
3282 const u32 *query_host_out;
3284 if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3287 query_host_out = mlx5_esw_query_functions(esw->dev);
3288 if (IS_ERR(query_host_out))
3289 return PTR_ERR(query_host_out);
3291 /* Mark non local controller with non zero controller number. */
3292 esw->offloads.host_number = MLX5_GET(query_esw_functions_out, query_host_out,
3293 host_params_context.host_number);
3294 kvfree(query_host_out);
3298 bool mlx5_esw_offloads_controller_valid(const struct mlx5_eswitch *esw, u32 controller)
3300 /* Local controller is always valid */
3301 if (controller == 0)
3304 if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3307 /* External host number starts with zero in device */
3308 return (controller == esw->offloads.host_number + 1);
3311 int esw_offloads_enable(struct mlx5_eswitch *esw)
3313 struct mapping_ctx *reg_c0_obj_pool;
3314 struct mlx5_vport *vport;
3319 mutex_init(&esw->offloads.termtbl_mutex);
3320 mlx5_rdma_enable_roce(esw->dev);
3322 err = mlx5_esw_host_number_init(esw);
3326 err = esw_offloads_metadata_init(esw);
3330 err = esw_set_passing_vport_metadata(esw, true);
3332 goto err_vport_metadata;
3334 mapping_id = mlx5_query_nic_system_image_guid(esw->dev);
3336 reg_c0_obj_pool = mapping_create_for_id(mapping_id, MAPPING_TYPE_CHAIN,
3337 sizeof(struct mlx5_mapped_obj),
3338 ESW_REG_C0_USER_DATA_METADATA_MASK,
3341 if (IS_ERR(reg_c0_obj_pool)) {
3342 err = PTR_ERR(reg_c0_obj_pool);
3345 esw->offloads.reg_c0_obj_pool = reg_c0_obj_pool;
3347 err = esw_offloads_steering_init(esw);
3349 goto err_steering_init;
3351 /* Representor will control the vport link state */
3352 mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs)
3353 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3355 /* Uplink vport rep must load first. */
3356 err = esw_offloads_load_rep(esw, MLX5_VPORT_UPLINK);
3360 err = mlx5_eswitch_enable_pf_vf_vports(esw, MLX5_VPORT_UC_ADDR_CHANGE);
3364 esw_offloads_devcom_init(esw);
3369 esw_offloads_unload_rep(esw, MLX5_VPORT_UPLINK);
3371 esw_offloads_steering_cleanup(esw);
3373 mapping_destroy(reg_c0_obj_pool);
3375 esw_set_passing_vport_metadata(esw, false);
3377 esw_offloads_metadata_uninit(esw);
3379 mlx5_rdma_disable_roce(esw->dev);
3380 mutex_destroy(&esw->offloads.termtbl_mutex);
3384 static int esw_offloads_stop(struct mlx5_eswitch *esw,
3385 struct netlink_ext_ack *extack)
3389 esw->mode = MLX5_ESWITCH_LEGACY;
3390 err = mlx5_eswitch_enable_locked(esw, MLX5_ESWITCH_IGNORE_NUM_VFS);
3392 NL_SET_ERR_MSG_MOD(extack, "Failed setting eswitch to legacy");
3397 void esw_offloads_disable(struct mlx5_eswitch *esw)
3399 esw_offloads_devcom_cleanup(esw);
3400 mlx5_eswitch_disable_pf_vf_vports(esw);
3401 esw_offloads_unload_rep(esw, MLX5_VPORT_UPLINK);
3402 esw_set_passing_vport_metadata(esw, false);
3403 esw_offloads_steering_cleanup(esw);
3404 mapping_destroy(esw->offloads.reg_c0_obj_pool);
3405 esw_offloads_metadata_uninit(esw);
3406 mlx5_rdma_disable_roce(esw->dev);
3407 mutex_destroy(&esw->offloads.termtbl_mutex);
3410 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
3413 case DEVLINK_ESWITCH_MODE_LEGACY:
3414 *mlx5_mode = MLX5_ESWITCH_LEGACY;
3416 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
3417 *mlx5_mode = MLX5_ESWITCH_OFFLOADS;
3426 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
3428 switch (mlx5_mode) {
3429 case MLX5_ESWITCH_LEGACY:
3430 *mode = DEVLINK_ESWITCH_MODE_LEGACY;
3432 case MLX5_ESWITCH_OFFLOADS:
3433 *mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
3442 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
3445 case DEVLINK_ESWITCH_INLINE_MODE_NONE:
3446 *mlx5_mode = MLX5_INLINE_MODE_NONE;
3448 case DEVLINK_ESWITCH_INLINE_MODE_LINK:
3449 *mlx5_mode = MLX5_INLINE_MODE_L2;
3451 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
3452 *mlx5_mode = MLX5_INLINE_MODE_IP;
3454 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
3455 *mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
3464 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
3466 switch (mlx5_mode) {
3467 case MLX5_INLINE_MODE_NONE:
3468 *mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
3470 case MLX5_INLINE_MODE_L2:
3471 *mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
3473 case MLX5_INLINE_MODE_IP:
3474 *mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
3476 case MLX5_INLINE_MODE_TCP_UDP:
3477 *mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
3486 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
3487 struct netlink_ext_ack *extack)
3489 u16 cur_mlx5_mode, mlx5_mode = 0;
3490 struct mlx5_eswitch *esw;
3493 esw = mlx5_devlink_eswitch_get(devlink);
3495 return PTR_ERR(esw);
3497 if (esw_mode_from_devlink(mode, &mlx5_mode))
3500 mlx5_lag_disable_change(esw->dev);
3501 err = mlx5_esw_try_lock(esw);
3503 NL_SET_ERR_MSG_MOD(extack, "Can't change mode, E-Switch is busy");
3506 cur_mlx5_mode = err;
3509 if (cur_mlx5_mode == mlx5_mode)
3512 mlx5_eswitch_disable_locked(esw);
3513 if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV) {
3514 if (mlx5_devlink_trap_get_num_active(esw->dev)) {
3515 NL_SET_ERR_MSG_MOD(extack,
3516 "Can't change mode while devlink traps are active");
3520 err = esw_offloads_start(esw, extack);
3521 } else if (mode == DEVLINK_ESWITCH_MODE_LEGACY) {
3522 err = esw_offloads_stop(esw, extack);
3523 mlx5_rescan_drivers(esw->dev);
3529 mlx5_esw_unlock(esw);
3531 mlx5_lag_enable_change(esw->dev);
3535 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
3537 struct mlx5_eswitch *esw;
3540 esw = mlx5_devlink_eswitch_get(devlink);
3542 return PTR_ERR(esw);
3544 down_write(&esw->mode_lock);
3545 err = esw_mode_to_devlink(esw->mode, mode);
3546 up_write(&esw->mode_lock);
3550 static int mlx5_esw_vports_inline_set(struct mlx5_eswitch *esw, u8 mlx5_mode,
3551 struct netlink_ext_ack *extack)
3553 struct mlx5_core_dev *dev = esw->dev;
3554 struct mlx5_vport *vport;
3555 u16 err_vport_num = 0;
3559 mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3560 err = mlx5_modify_nic_vport_min_inline(dev, vport->vport, mlx5_mode);
3562 err_vport_num = vport->vport;
3563 NL_SET_ERR_MSG_MOD(extack,
3564 "Failed to set min inline on vport");
3565 goto revert_inline_mode;
3571 mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3572 if (vport->vport == err_vport_num)
3574 mlx5_modify_nic_vport_min_inline(dev,
3576 esw->offloads.inline_mode);
3581 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode,
3582 struct netlink_ext_ack *extack)
3584 struct mlx5_core_dev *dev = devlink_priv(devlink);
3585 struct mlx5_eswitch *esw;
3589 esw = mlx5_devlink_eswitch_get(devlink);
3591 return PTR_ERR(esw);
3593 down_write(&esw->mode_lock);
3595 switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
3596 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
3597 if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE) {
3603 case MLX5_CAP_INLINE_MODE_L2:
3604 NL_SET_ERR_MSG_MOD(extack, "Inline mode can't be set");
3607 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
3611 if (atomic64_read(&esw->offloads.num_flows) > 0) {
3612 NL_SET_ERR_MSG_MOD(extack,
3613 "Can't set inline mode when flows are configured");
3618 err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
3622 err = mlx5_esw_vports_inline_set(esw, mlx5_mode, extack);
3626 esw->offloads.inline_mode = mlx5_mode;
3627 up_write(&esw->mode_lock);
3631 up_write(&esw->mode_lock);
3635 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
3637 struct mlx5_eswitch *esw;
3640 esw = mlx5_devlink_eswitch_get(devlink);
3642 return PTR_ERR(esw);
3644 down_write(&esw->mode_lock);
3645 err = esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
3646 up_write(&esw->mode_lock);
3650 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
3651 enum devlink_eswitch_encap_mode encap,
3652 struct netlink_ext_ack *extack)
3654 struct mlx5_core_dev *dev = devlink_priv(devlink);
3655 struct mlx5_eswitch *esw;
3658 esw = mlx5_devlink_eswitch_get(devlink);
3660 return PTR_ERR(esw);
3662 down_write(&esw->mode_lock);
3664 if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
3665 (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) ||
3666 !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))) {
3671 if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC) {
3676 if (esw->mode == MLX5_ESWITCH_LEGACY) {
3677 esw->offloads.encap = encap;
3681 if (esw->offloads.encap == encap)
3684 if (atomic64_read(&esw->offloads.num_flows) > 0) {
3685 NL_SET_ERR_MSG_MOD(extack,
3686 "Can't set encapsulation when flows are configured");
3691 esw_destroy_offloads_fdb_tables(esw);
3693 esw->offloads.encap = encap;
3695 err = esw_create_offloads_fdb_tables(esw);
3698 NL_SET_ERR_MSG_MOD(extack,
3699 "Failed re-creating fast FDB table");
3700 esw->offloads.encap = !encap;
3701 (void)esw_create_offloads_fdb_tables(esw);
3705 up_write(&esw->mode_lock);
3709 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink,
3710 enum devlink_eswitch_encap_mode *encap)
3712 struct mlx5_eswitch *esw;
3714 esw = mlx5_devlink_eswitch_get(devlink);
3716 return PTR_ERR(esw);
3718 down_write(&esw->mode_lock);
3719 *encap = esw->offloads.encap;
3720 up_write(&esw->mode_lock);
3725 mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch *esw, u16 vport_num)
3727 /* Currently, only ECPF based device has representor for host PF. */
3728 if (vport_num == MLX5_VPORT_PF &&
3729 !mlx5_core_is_ecpf_esw_manager(esw->dev))
3732 if (vport_num == MLX5_VPORT_ECPF &&
3733 !mlx5_ecpf_vport_exists(esw->dev))
3739 void mlx5_eswitch_register_vport_reps(struct mlx5_eswitch *esw,
3740 const struct mlx5_eswitch_rep_ops *ops,
3743 struct mlx5_eswitch_rep_data *rep_data;
3744 struct mlx5_eswitch_rep *rep;
3747 esw->offloads.rep_ops[rep_type] = ops;
3748 mlx5_esw_for_each_rep(esw, i, rep) {
3749 if (likely(mlx5_eswitch_vport_has_rep(esw, rep->vport))) {
3751 rep_data = &rep->rep_data[rep_type];
3752 atomic_set(&rep_data->state, REP_REGISTERED);
3756 EXPORT_SYMBOL(mlx5_eswitch_register_vport_reps);
3758 void mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch *esw, u8 rep_type)
3760 struct mlx5_eswitch_rep *rep;
3763 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
3764 __unload_reps_all_vport(esw, rep_type);
3766 mlx5_esw_for_each_rep(esw, i, rep)
3767 atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
3769 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_reps);
3771 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
3773 struct mlx5_eswitch_rep *rep;
3775 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
3776 return rep->rep_data[rep_type].priv;
3779 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
3783 struct mlx5_eswitch_rep *rep;
3785 rep = mlx5_eswitch_get_rep(esw, vport);
3787 if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
3788 esw->offloads.rep_ops[rep_type]->get_proto_dev)
3789 return esw->offloads.rep_ops[rep_type]->get_proto_dev(rep);
3792 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
3794 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
3796 return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
3798 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
3800 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
3803 return mlx5_eswitch_get_rep(esw, vport);
3805 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);
3807 bool mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch *esw)
3809 return !!(esw->flags & MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED);
3811 EXPORT_SYMBOL(mlx5_eswitch_reg_c1_loopback_enabled);
3813 bool mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch *esw)
3815 return !!(esw->flags & MLX5_ESWITCH_VPORT_MATCH_METADATA);
3817 EXPORT_SYMBOL(mlx5_eswitch_vport_match_metadata_enabled);
3819 u32 mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch *esw,
3822 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
3824 if (WARN_ON_ONCE(IS_ERR(vport)))
3827 return vport->metadata << (32 - ESW_SOURCE_PORT_METADATA_BITS);
3829 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_match);
3831 int mlx5_esw_offloads_sf_vport_enable(struct mlx5_eswitch *esw, struct devlink_port *dl_port,
3832 u16 vport_num, u32 controller, u32 sfnum)
3836 err = mlx5_esw_vport_enable(esw, vport_num, MLX5_VPORT_UC_ADDR_CHANGE);
3840 err = mlx5_esw_devlink_sf_port_register(esw, dl_port, vport_num, controller, sfnum);
3844 mlx5_esw_vport_debugfs_create(esw, vport_num, true, sfnum);
3845 err = mlx5_esw_offloads_rep_load(esw, vport_num);
3851 mlx5_esw_vport_debugfs_destroy(esw, vport_num);
3852 mlx5_esw_devlink_sf_port_unregister(esw, vport_num);
3854 mlx5_esw_vport_disable(esw, vport_num);
3858 void mlx5_esw_offloads_sf_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
3860 mlx5_esw_offloads_rep_unload(esw, vport_num);
3861 mlx5_esw_vport_debugfs_destroy(esw, vport_num);
3862 mlx5_esw_devlink_sf_port_unregister(esw, vport_num);
3863 mlx5_esw_vport_disable(esw, vport_num);
3866 static int mlx5_esw_query_vport_vhca_id(struct mlx5_eswitch *esw, u16 vport_num, u16 *vhca_id)
3868 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
3874 if (mlx5_esw_is_manager_vport(esw, vport_num) ||
3875 !MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
3878 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
3882 err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx);
3886 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
3887 *vhca_id = MLX5_GET(cmd_hca_cap, hca_caps, vhca_id);
3894 int mlx5_esw_vport_vhca_id_set(struct mlx5_eswitch *esw, u16 vport_num)
3896 u16 *old_entry, *vhca_map_entry, vhca_id;
3899 err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
3901 esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%u,err=%d)\n",
3906 vhca_map_entry = kmalloc(sizeof(*vhca_map_entry), GFP_KERNEL);
3907 if (!vhca_map_entry)
3910 *vhca_map_entry = vport_num;
3911 old_entry = xa_store(&esw->offloads.vhca_map, vhca_id, vhca_map_entry, GFP_KERNEL);
3912 if (xa_is_err(old_entry)) {
3913 kfree(vhca_map_entry);
3914 return xa_err(old_entry);
3920 void mlx5_esw_vport_vhca_id_clear(struct mlx5_eswitch *esw, u16 vport_num)
3922 u16 *vhca_map_entry, vhca_id;
3925 err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
3927 esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%hu,err=%d)\n",
3930 vhca_map_entry = xa_erase(&esw->offloads.vhca_map, vhca_id);
3931 kfree(vhca_map_entry);
3934 int mlx5_eswitch_vhca_id_to_vport(struct mlx5_eswitch *esw, u16 vhca_id, u16 *vport_num)
3936 u16 *res = xa_load(&esw->offloads.vhca_map, vhca_id);
3945 u32 mlx5_eswitch_get_vport_metadata_for_set(struct mlx5_eswitch *esw,
3948 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
3950 if (WARN_ON_ONCE(IS_ERR(vport)))
3953 return vport->metadata;
3955 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_set);
3958 is_port_function_supported(struct mlx5_eswitch *esw, u16 vport_num)
3960 return vport_num == MLX5_VPORT_PF ||
3961 mlx5_eswitch_is_vf_vport(esw, vport_num) ||
3962 mlx5_esw_is_sf_vport(esw, vport_num);
3965 int mlx5_devlink_port_function_hw_addr_get(struct devlink_port *port,
3966 u8 *hw_addr, int *hw_addr_len,
3967 struct netlink_ext_ack *extack)
3969 struct mlx5_eswitch *esw;
3970 struct mlx5_vport *vport;
3973 esw = mlx5_devlink_eswitch_get(port->devlink);
3975 return PTR_ERR(esw);
3977 vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
3978 if (!is_port_function_supported(esw, vport_num))
3981 vport = mlx5_eswitch_get_vport(esw, vport_num);
3982 if (IS_ERR(vport)) {
3983 NL_SET_ERR_MSG_MOD(extack, "Invalid port");
3984 return PTR_ERR(vport);
3987 mutex_lock(&esw->state_lock);
3988 ether_addr_copy(hw_addr, vport->info.mac);
3989 *hw_addr_len = ETH_ALEN;
3990 mutex_unlock(&esw->state_lock);
3994 int mlx5_devlink_port_function_hw_addr_set(struct devlink_port *port,
3995 const u8 *hw_addr, int hw_addr_len,
3996 struct netlink_ext_ack *extack)
3998 struct mlx5_eswitch *esw;
4001 esw = mlx5_devlink_eswitch_get(port->devlink);
4003 NL_SET_ERR_MSG_MOD(extack, "Eswitch doesn't support set hw_addr");
4004 return PTR_ERR(esw);
4007 vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
4008 if (!is_port_function_supported(esw, vport_num)) {
4009 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support set hw_addr");
4013 return mlx5_eswitch_set_vport_mac(esw, vport_num, hw_addr);