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/acl/ofld.h"
42 #include "esw/chains.h"
46 #include "lib/devcom.h"
49 /* There are two match-all miss flows, one for unicast dst mac and
52 #define MLX5_ESW_MISS_FLOWS (2)
53 #define UPLINK_REP_INDEX 0
55 /* Per vport tables */
57 #define MLX5_ESW_VPORT_TABLE_SIZE 128
59 /* This struct is used as a key to the hash table and we need it to be packed
60 * so hash result is consistent
62 struct mlx5_vport_key
{
69 struct mlx5_vport_table
{
70 struct hlist_node hlist
;
71 struct mlx5_flow_table
*fdb
;
73 struct mlx5_vport_key key
;
76 #define MLX5_ESW_VPORT_TBL_NUM_GROUPS 4
78 static struct mlx5_flow_table
*
79 esw_vport_tbl_create(struct mlx5_eswitch
*esw
, struct mlx5_flow_namespace
*ns
)
81 struct mlx5_flow_table_attr ft_attr
= {};
82 struct mlx5_flow_table
*fdb
;
84 ft_attr
.autogroup
.max_num_groups
= MLX5_ESW_VPORT_TBL_NUM_GROUPS
;
85 ft_attr
.max_fte
= MLX5_ESW_VPORT_TABLE_SIZE
;
86 ft_attr
.prio
= FDB_PER_VPORT
;
87 fdb
= mlx5_create_auto_grouped_flow_table(ns
, &ft_attr
);
89 esw_warn(esw
->dev
, "Failed to create per vport FDB Table err %ld\n",
96 static u32
flow_attr_to_vport_key(struct mlx5_eswitch
*esw
,
97 struct mlx5_esw_flow_attr
*attr
,
98 struct mlx5_vport_key
*key
)
100 key
->vport
= attr
->in_rep
->vport
;
101 key
->chain
= attr
->chain
;
102 key
->prio
= attr
->prio
;
103 key
->vhca_id
= MLX5_CAP_GEN(esw
->dev
, vhca_id
);
104 return jhash(key
, sizeof(*key
), 0);
107 /* caller must hold vports.lock */
108 static struct mlx5_vport_table
*
109 esw_vport_tbl_lookup(struct mlx5_eswitch
*esw
, struct mlx5_vport_key
*skey
, u32 key
)
111 struct mlx5_vport_table
*e
;
113 hash_for_each_possible(esw
->fdb_table
.offloads
.vports
.table
, e
, hlist
, key
)
114 if (!memcmp(&e
->key
, skey
, sizeof(*skey
)))
121 esw_vport_tbl_put(struct mlx5_eswitch
*esw
, struct mlx5_esw_flow_attr
*attr
)
123 struct mlx5_vport_table
*e
;
124 struct mlx5_vport_key key
;
127 mutex_lock(&esw
->fdb_table
.offloads
.vports
.lock
);
128 hkey
= flow_attr_to_vport_key(esw
, attr
, &key
);
129 e
= esw_vport_tbl_lookup(esw
, &key
, hkey
);
130 if (!e
|| --e
->num_rules
)
134 mlx5_destroy_flow_table(e
->fdb
);
137 mutex_unlock(&esw
->fdb_table
.offloads
.vports
.lock
);
140 static struct mlx5_flow_table
*
141 esw_vport_tbl_get(struct mlx5_eswitch
*esw
, struct mlx5_esw_flow_attr
*attr
)
143 struct mlx5_core_dev
*dev
= esw
->dev
;
144 struct mlx5_flow_namespace
*ns
;
145 struct mlx5_flow_table
*fdb
;
146 struct mlx5_vport_table
*e
;
147 struct mlx5_vport_key skey
;
150 mutex_lock(&esw
->fdb_table
.offloads
.vports
.lock
);
151 hkey
= flow_attr_to_vport_key(esw
, attr
, &skey
);
152 e
= esw_vport_tbl_lookup(esw
, &skey
, hkey
);
158 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
160 fdb
= ERR_PTR(-ENOMEM
);
164 ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_FDB
);
166 esw_warn(dev
, "Failed to get FDB namespace\n");
167 fdb
= ERR_PTR(-ENOENT
);
171 fdb
= esw_vport_tbl_create(esw
, ns
);
178 hash_add(esw
->fdb_table
.offloads
.vports
.table
, &e
->hlist
, hkey
);
180 mutex_unlock(&esw
->fdb_table
.offloads
.vports
.lock
);
186 mutex_unlock(&esw
->fdb_table
.offloads
.vports
.lock
);
190 int mlx5_esw_vport_tbl_get(struct mlx5_eswitch
*esw
)
192 struct mlx5_esw_flow_attr attr
= {};
193 struct mlx5_eswitch_rep rep
= {};
194 struct mlx5_flow_table
*fdb
;
195 struct mlx5_vport
*vport
;
200 mlx5_esw_for_all_vports(esw
, i
, vport
) {
201 attr
.in_rep
->vport
= vport
->vport
;
202 fdb
= esw_vport_tbl_get(esw
, &attr
);
209 mlx5_esw_vport_tbl_put(esw
);
213 void mlx5_esw_vport_tbl_put(struct mlx5_eswitch
*esw
)
215 struct mlx5_esw_flow_attr attr
= {};
216 struct mlx5_eswitch_rep rep
= {};
217 struct mlx5_vport
*vport
;
222 mlx5_esw_for_all_vports(esw
, i
, vport
) {
223 attr
.in_rep
->vport
= vport
->vport
;
224 esw_vport_tbl_put(esw
, &attr
);
228 /* End: Per vport tables */
230 static struct mlx5_eswitch_rep
*mlx5_eswitch_get_rep(struct mlx5_eswitch
*esw
,
233 int idx
= mlx5_eswitch_vport_num_to_index(esw
, vport_num
);
235 WARN_ON(idx
> esw
->total_vports
- 1);
236 return &esw
->offloads
.vport_reps
[idx
];
240 mlx5_eswitch_set_rule_flow_source(struct mlx5_eswitch
*esw
,
241 struct mlx5_flow_spec
*spec
,
242 struct mlx5_esw_flow_attr
*attr
)
244 if (MLX5_CAP_ESW_FLOWTABLE(esw
->dev
, flow_source
) &&
245 attr
&& attr
->in_rep
&& attr
->in_rep
->vport
== MLX5_VPORT_UPLINK
)
246 spec
->flow_context
.flow_source
= MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK
;
250 mlx5_eswitch_set_rule_source_port(struct mlx5_eswitch
*esw
,
251 struct mlx5_flow_spec
*spec
,
252 struct mlx5_esw_flow_attr
*attr
)
257 /* Use metadata matching because vport is not represented by single
258 * VHCA in dual-port RoCE mode, and matching on source vport may fail.
260 if (mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
261 misc2
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
, misc_parameters_2
);
262 MLX5_SET(fte_match_set_misc2
, misc2
, metadata_reg_c_0
,
263 mlx5_eswitch_get_vport_metadata_for_match(attr
->in_mdev
->priv
.eswitch
,
264 attr
->in_rep
->vport
));
266 misc2
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
, misc_parameters_2
);
267 MLX5_SET(fte_match_set_misc2
, misc2
, metadata_reg_c_0
,
268 mlx5_eswitch_get_vport_metadata_mask());
270 spec
->match_criteria_enable
|= MLX5_MATCH_MISC_PARAMETERS_2
;
272 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
, misc_parameters
);
273 MLX5_SET(fte_match_set_misc
, misc
, source_port
, attr
->in_rep
->vport
);
275 if (MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
276 MLX5_SET(fte_match_set_misc
, misc
,
277 source_eswitch_owner_vhca_id
,
278 MLX5_CAP_GEN(attr
->in_mdev
, vhca_id
));
280 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
, misc_parameters
);
281 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_port
);
282 if (MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
283 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
,
284 source_eswitch_owner_vhca_id
);
286 spec
->match_criteria_enable
|= MLX5_MATCH_MISC_PARAMETERS
;
290 struct mlx5_flow_handle
*
291 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch
*esw
,
292 struct mlx5_flow_spec
*spec
,
293 struct mlx5_esw_flow_attr
*attr
)
295 struct mlx5_flow_destination dest
[MLX5_MAX_FLOW_FWD_VPORTS
+ 1] = {};
296 struct mlx5_flow_act flow_act
= { .flags
= FLOW_ACT_NO_APPEND
, };
297 bool split
= !!(attr
->split_count
);
298 struct mlx5_flow_handle
*rule
;
299 struct mlx5_flow_table
*fdb
;
302 if (esw
->mode
!= MLX5_ESWITCH_OFFLOADS
)
303 return ERR_PTR(-EOPNOTSUPP
);
305 flow_act
.action
= attr
->action
;
306 /* if per flow vlan pop/push is emulated, don't set that into the firmware */
307 if (!mlx5_eswitch_vlan_actions_supported(esw
->dev
, 1))
308 flow_act
.action
&= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
|
309 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP
);
310 else if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
) {
311 flow_act
.vlan
[0].ethtype
= ntohs(attr
->vlan_proto
[0]);
312 flow_act
.vlan
[0].vid
= attr
->vlan_vid
[0];
313 flow_act
.vlan
[0].prio
= attr
->vlan_prio
[0];
314 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2
) {
315 flow_act
.vlan
[1].ethtype
= ntohs(attr
->vlan_proto
[1]);
316 flow_act
.vlan
[1].vid
= attr
->vlan_vid
[1];
317 flow_act
.vlan
[1].prio
= attr
->vlan_prio
[1];
321 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
) {
322 struct mlx5_flow_table
*ft
;
325 flow_act
.flags
|= FLOW_ACT_IGNORE_FLOW_LEVEL
;
326 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
327 dest
[i
].ft
= attr
->dest_ft
;
329 } else if (attr
->flags
& MLX5_ESW_ATTR_FLAG_SLOW_PATH
) {
330 flow_act
.flags
|= FLOW_ACT_IGNORE_FLOW_LEVEL
;
331 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
332 dest
[i
].ft
= mlx5_esw_chains_get_tc_end_ft(esw
);
334 } else if (attr
->dest_chain
) {
335 flow_act
.flags
|= FLOW_ACT_IGNORE_FLOW_LEVEL
;
336 ft
= mlx5_esw_chains_get_table(esw
, attr
->dest_chain
,
340 goto err_create_goto_table
;
343 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
347 for (j
= attr
->split_count
; j
< attr
->out_count
; j
++) {
348 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
349 dest
[i
].vport
.num
= attr
->dests
[j
].rep
->vport
;
350 dest
[i
].vport
.vhca_id
=
351 MLX5_CAP_GEN(attr
->dests
[j
].mdev
, vhca_id
);
352 if (MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
353 dest
[i
].vport
.flags
|=
354 MLX5_FLOW_DEST_VPORT_VHCA_ID
;
355 if (attr
->dests
[j
].flags
& MLX5_ESW_DEST_ENCAP
) {
356 flow_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT
;
357 flow_act
.pkt_reformat
= attr
->dests
[j
].pkt_reformat
;
358 dest
[i
].vport
.flags
|= MLX5_FLOW_DEST_VPORT_REFORMAT_ID
;
359 dest
[i
].vport
.pkt_reformat
=
360 attr
->dests
[j
].pkt_reformat
;
367 if (attr
->decap_pkt_reformat
)
368 flow_act
.pkt_reformat
= attr
->decap_pkt_reformat
;
370 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_COUNT
) {
371 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_COUNTER
;
372 dest
[i
].counter_id
= mlx5_fc_id(attr
->counter
);
376 if (attr
->outer_match_level
!= MLX5_MATCH_NONE
)
377 spec
->match_criteria_enable
|= MLX5_MATCH_OUTER_HEADERS
;
378 if (attr
->inner_match_level
!= MLX5_MATCH_NONE
)
379 spec
->match_criteria_enable
|= MLX5_MATCH_INNER_HEADERS
;
381 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_MOD_HDR
)
382 flow_act
.modify_hdr
= attr
->modify_hdr
;
385 fdb
= esw_vport_tbl_get(esw
, attr
);
387 if (attr
->chain
|| attr
->prio
)
388 fdb
= mlx5_esw_chains_get_table(esw
, attr
->chain
,
393 if (!(attr
->flags
& MLX5_ESW_ATTR_FLAG_NO_IN_PORT
))
394 mlx5_eswitch_set_rule_source_port(esw
, spec
, attr
);
397 rule
= ERR_CAST(fdb
);
401 mlx5_eswitch_set_rule_flow_source(esw
, spec
, attr
);
403 if (mlx5_eswitch_termtbl_required(esw
, attr
, &flow_act
, spec
))
404 rule
= mlx5_eswitch_add_termtbl_rule(esw
, fdb
, spec
, attr
,
407 rule
= mlx5_add_flow_rules(fdb
, spec
, &flow_act
, dest
, i
);
411 atomic64_inc(&esw
->offloads
.num_flows
);
417 esw_vport_tbl_put(esw
, attr
);
418 else if (attr
->chain
|| attr
->prio
)
419 mlx5_esw_chains_put_table(esw
, attr
->chain
, attr
->prio
, 0);
421 if (!(attr
->flags
& MLX5_ESW_ATTR_FLAG_SLOW_PATH
) && attr
->dest_chain
)
422 mlx5_esw_chains_put_table(esw
, attr
->dest_chain
, 1, 0);
423 err_create_goto_table
:
427 struct mlx5_flow_handle
*
428 mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch
*esw
,
429 struct mlx5_flow_spec
*spec
,
430 struct mlx5_esw_flow_attr
*attr
)
432 struct mlx5_flow_destination dest
[MLX5_MAX_FLOW_FWD_VPORTS
+ 1] = {};
433 struct mlx5_flow_act flow_act
= { .flags
= FLOW_ACT_NO_APPEND
, };
434 struct mlx5_flow_table
*fast_fdb
;
435 struct mlx5_flow_table
*fwd_fdb
;
436 struct mlx5_flow_handle
*rule
;
439 fast_fdb
= mlx5_esw_chains_get_table(esw
, attr
->chain
, attr
->prio
, 0);
440 if (IS_ERR(fast_fdb
)) {
441 rule
= ERR_CAST(fast_fdb
);
445 fwd_fdb
= esw_vport_tbl_get(esw
, attr
);
446 if (IS_ERR(fwd_fdb
)) {
447 rule
= ERR_CAST(fwd_fdb
);
451 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
452 for (i
= 0; i
< attr
->split_count
; i
++) {
453 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
454 dest
[i
].vport
.num
= attr
->dests
[i
].rep
->vport
;
455 dest
[i
].vport
.vhca_id
=
456 MLX5_CAP_GEN(attr
->dests
[i
].mdev
, vhca_id
);
457 if (MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
458 dest
[i
].vport
.flags
|= MLX5_FLOW_DEST_VPORT_VHCA_ID
;
459 if (attr
->dests
[i
].flags
& MLX5_ESW_DEST_ENCAP
) {
460 dest
[i
].vport
.flags
|= MLX5_FLOW_DEST_VPORT_REFORMAT_ID
;
461 dest
[i
].vport
.pkt_reformat
= attr
->dests
[i
].pkt_reformat
;
464 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
465 dest
[i
].ft
= fwd_fdb
,
468 mlx5_eswitch_set_rule_source_port(esw
, spec
, attr
);
469 mlx5_eswitch_set_rule_flow_source(esw
, spec
, attr
);
471 if (attr
->outer_match_level
!= MLX5_MATCH_NONE
)
472 spec
->match_criteria_enable
|= MLX5_MATCH_OUTER_HEADERS
;
474 flow_act
.flags
|= FLOW_ACT_IGNORE_FLOW_LEVEL
;
475 rule
= mlx5_add_flow_rules(fast_fdb
, spec
, &flow_act
, dest
, i
);
480 atomic64_inc(&esw
->offloads
.num_flows
);
484 esw_vport_tbl_put(esw
, attr
);
486 mlx5_esw_chains_put_table(esw
, attr
->chain
, attr
->prio
, 0);
492 __mlx5_eswitch_del_rule(struct mlx5_eswitch
*esw
,
493 struct mlx5_flow_handle
*rule
,
494 struct mlx5_esw_flow_attr
*attr
,
497 bool split
= (attr
->split_count
> 0);
500 mlx5_del_flow_rules(rule
);
502 if (!(attr
->flags
& MLX5_ESW_ATTR_FLAG_SLOW_PATH
)) {
503 /* unref the term table */
504 for (i
= 0; i
< MLX5_MAX_FLOW_FWD_VPORTS
; i
++) {
505 if (attr
->dests
[i
].termtbl
)
506 mlx5_eswitch_termtbl_put(esw
, attr
->dests
[i
].termtbl
);
510 atomic64_dec(&esw
->offloads
.num_flows
);
513 esw_vport_tbl_put(esw
, attr
);
514 mlx5_esw_chains_put_table(esw
, attr
->chain
, attr
->prio
, 0);
517 esw_vport_tbl_put(esw
, attr
);
518 else if (attr
->chain
|| attr
->prio
)
519 mlx5_esw_chains_put_table(esw
, attr
->chain
, attr
->prio
,
521 if (attr
->dest_chain
)
522 mlx5_esw_chains_put_table(esw
, attr
->dest_chain
, 1, 0);
527 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch
*esw
,
528 struct mlx5_flow_handle
*rule
,
529 struct mlx5_esw_flow_attr
*attr
)
531 __mlx5_eswitch_del_rule(esw
, rule
, attr
, false);
535 mlx5_eswitch_del_fwd_rule(struct mlx5_eswitch
*esw
,
536 struct mlx5_flow_handle
*rule
,
537 struct mlx5_esw_flow_attr
*attr
)
539 __mlx5_eswitch_del_rule(esw
, rule
, attr
, true);
542 static int esw_set_global_vlan_pop(struct mlx5_eswitch
*esw
, u8 val
)
544 struct mlx5_eswitch_rep
*rep
;
547 esw_debug(esw
->dev
, "%s applying global %s policy\n", __func__
, val
? "pop" : "none");
548 mlx5_esw_for_each_host_func_rep(esw
, i
, rep
, esw
->esw_funcs
.num_vfs
) {
549 if (atomic_read(&rep
->rep_data
[REP_ETH
].state
) != REP_LOADED
)
552 err
= __mlx5_eswitch_set_vport_vlan(esw
, rep
->vport
, 0, 0, val
);
561 static struct mlx5_eswitch_rep
*
562 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr
*attr
, bool push
, bool pop
)
564 struct mlx5_eswitch_rep
*in_rep
, *out_rep
, *vport
= NULL
;
566 in_rep
= attr
->in_rep
;
567 out_rep
= attr
->dests
[0].rep
;
579 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr
*attr
,
580 bool push
, bool pop
, bool fwd
)
582 struct mlx5_eswitch_rep
*in_rep
, *out_rep
;
584 if ((push
|| pop
) && !fwd
)
587 in_rep
= attr
->in_rep
;
588 out_rep
= attr
->dests
[0].rep
;
590 if (push
&& in_rep
->vport
== MLX5_VPORT_UPLINK
)
593 if (pop
&& out_rep
->vport
== MLX5_VPORT_UPLINK
)
596 /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
597 if (!push
&& !pop
&& fwd
)
598 if (in_rep
->vlan
&& out_rep
->vport
== MLX5_VPORT_UPLINK
)
601 /* protects against (1) setting rules with different vlans to push and
602 * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
604 if (push
&& in_rep
->vlan_refcount
&& (in_rep
->vlan
!= attr
->vlan_vid
[0]))
613 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch
*esw
,
614 struct mlx5_esw_flow_attr
*attr
)
616 struct offloads_fdb
*offloads
= &esw
->fdb_table
.offloads
;
617 struct mlx5_eswitch_rep
*vport
= NULL
;
621 /* nop if we're on the vlan push/pop non emulation mode */
622 if (mlx5_eswitch_vlan_actions_supported(esw
->dev
, 1))
625 push
= !!(attr
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
);
626 pop
= !!(attr
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_POP
);
627 fwd
= !!((attr
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
) &&
630 mutex_lock(&esw
->state_lock
);
632 err
= esw_add_vlan_action_check(attr
, push
, pop
, fwd
);
636 attr
->flags
&= ~MLX5_ESW_ATTR_FLAG_VLAN_HANDLED
;
638 vport
= esw_vlan_action_get_vport(attr
, push
, pop
);
640 if (!push
&& !pop
&& fwd
) {
641 /* tracks VF --> wire rules without vlan push action */
642 if (attr
->dests
[0].rep
->vport
== MLX5_VPORT_UPLINK
) {
643 vport
->vlan_refcount
++;
644 attr
->flags
|= MLX5_ESW_ATTR_FLAG_VLAN_HANDLED
;
653 if (!(offloads
->vlan_push_pop_refcount
)) {
654 /* it's the 1st vlan rule, apply global vlan pop policy */
655 err
= esw_set_global_vlan_pop(esw
, SET_VLAN_STRIP
);
659 offloads
->vlan_push_pop_refcount
++;
662 if (vport
->vlan_refcount
)
665 err
= __mlx5_eswitch_set_vport_vlan(esw
, vport
->vport
, attr
->vlan_vid
[0], 0,
666 SET_VLAN_INSERT
| SET_VLAN_STRIP
);
669 vport
->vlan
= attr
->vlan_vid
[0];
671 vport
->vlan_refcount
++;
675 attr
->flags
|= MLX5_ESW_ATTR_FLAG_VLAN_HANDLED
;
677 mutex_unlock(&esw
->state_lock
);
681 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch
*esw
,
682 struct mlx5_esw_flow_attr
*attr
)
684 struct offloads_fdb
*offloads
= &esw
->fdb_table
.offloads
;
685 struct mlx5_eswitch_rep
*vport
= NULL
;
689 /* nop if we're on the vlan push/pop non emulation mode */
690 if (mlx5_eswitch_vlan_actions_supported(esw
->dev
, 1))
693 if (!(attr
->flags
& MLX5_ESW_ATTR_FLAG_VLAN_HANDLED
))
696 push
= !!(attr
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
);
697 pop
= !!(attr
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_POP
);
698 fwd
= !!(attr
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
);
700 mutex_lock(&esw
->state_lock
);
702 vport
= esw_vlan_action_get_vport(attr
, push
, pop
);
704 if (!push
&& !pop
&& fwd
) {
705 /* tracks VF --> wire rules without vlan push action */
706 if (attr
->dests
[0].rep
->vport
== MLX5_VPORT_UPLINK
)
707 vport
->vlan_refcount
--;
713 vport
->vlan_refcount
--;
714 if (vport
->vlan_refcount
)
715 goto skip_unset_push
;
718 err
= __mlx5_eswitch_set_vport_vlan(esw
, vport
->vport
,
719 0, 0, SET_VLAN_STRIP
);
725 offloads
->vlan_push_pop_refcount
--;
726 if (offloads
->vlan_push_pop_refcount
)
729 /* no more vlan rules, stop global vlan pop policy */
730 err
= esw_set_global_vlan_pop(esw
, 0);
733 mutex_unlock(&esw
->state_lock
);
737 struct mlx5_flow_handle
*
738 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch
*esw
, u16 vport
,
741 struct mlx5_flow_act flow_act
= {0};
742 struct mlx5_flow_destination dest
= {};
743 struct mlx5_flow_handle
*flow_rule
;
744 struct mlx5_flow_spec
*spec
;
747 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
749 flow_rule
= ERR_PTR(-ENOMEM
);
753 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
, misc_parameters
);
754 MLX5_SET(fte_match_set_misc
, misc
, source_sqn
, sqn
);
755 /* source vport is the esw manager */
756 MLX5_SET(fte_match_set_misc
, misc
, source_port
, esw
->manager_vport
);
758 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
, misc_parameters
);
759 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_sqn
);
760 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_port
);
762 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS
;
763 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
764 dest
.vport
.num
= vport
;
765 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
767 flow_rule
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
768 spec
, &flow_act
, &dest
, 1);
769 if (IS_ERR(flow_rule
))
770 esw_warn(esw
->dev
, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule
));
775 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule
);
777 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle
*rule
)
779 mlx5_del_flow_rules(rule
);
782 static bool mlx5_eswitch_reg_c1_loopback_supported(struct mlx5_eswitch
*esw
)
784 return MLX5_CAP_ESW_FLOWTABLE(esw
->dev
, fdb_to_vport_reg_c_id
) &
785 MLX5_FDB_TO_VPORT_REG_C_1
;
788 static int esw_set_passing_vport_metadata(struct mlx5_eswitch
*esw
, bool enable
)
790 u32 out
[MLX5_ST_SZ_DW(query_esw_vport_context_out
)] = {};
791 u32 min
[MLX5_ST_SZ_DW(modify_esw_vport_context_in
)] = {};
792 u32 in
[MLX5_ST_SZ_DW(query_esw_vport_context_in
)] = {};
796 if (!mlx5_eswitch_reg_c1_loopback_supported(esw
) &&
797 !mlx5_eswitch_vport_match_metadata_enabled(esw
))
800 MLX5_SET(query_esw_vport_context_in
, in
, opcode
,
801 MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT
);
802 err
= mlx5_cmd_exec_inout(esw
->dev
, query_esw_vport_context
, in
, out
);
806 curr
= MLX5_GET(query_esw_vport_context_out
, out
,
807 esw_vport_context
.fdb_to_vport_reg_c_id
);
808 wanted
= MLX5_FDB_TO_VPORT_REG_C_0
;
809 if (mlx5_eswitch_reg_c1_loopback_supported(esw
))
810 wanted
|= MLX5_FDB_TO_VPORT_REG_C_1
;
817 MLX5_SET(modify_esw_vport_context_in
, min
,
818 esw_vport_context
.fdb_to_vport_reg_c_id
, curr
);
819 MLX5_SET(modify_esw_vport_context_in
, min
,
820 field_select
.fdb_to_vport_reg_c_id
, 1);
822 err
= mlx5_eswitch_modify_esw_vport_context(esw
->dev
, 0, false, min
);
824 if (enable
&& (curr
& MLX5_FDB_TO_VPORT_REG_C_1
))
825 esw
->flags
|= MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED
;
827 esw
->flags
&= ~MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED
;
833 static void peer_miss_rules_setup(struct mlx5_eswitch
*esw
,
834 struct mlx5_core_dev
*peer_dev
,
835 struct mlx5_flow_spec
*spec
,
836 struct mlx5_flow_destination
*dest
)
840 if (mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
841 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
843 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
844 mlx5_eswitch_get_vport_metadata_mask());
846 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS_2
;
848 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
851 MLX5_SET(fte_match_set_misc
, misc
, source_eswitch_owner_vhca_id
,
852 MLX5_CAP_GEN(peer_dev
, vhca_id
));
854 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS
;
856 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
858 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_port
);
859 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
,
860 source_eswitch_owner_vhca_id
);
863 dest
->type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
864 dest
->vport
.num
= peer_dev
->priv
.eswitch
->manager_vport
;
865 dest
->vport
.vhca_id
= MLX5_CAP_GEN(peer_dev
, vhca_id
);
866 dest
->vport
.flags
|= MLX5_FLOW_DEST_VPORT_VHCA_ID
;
869 static void esw_set_peer_miss_rule_source_port(struct mlx5_eswitch
*esw
,
870 struct mlx5_eswitch
*peer_esw
,
871 struct mlx5_flow_spec
*spec
,
876 if (mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
877 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
879 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
880 mlx5_eswitch_get_vport_metadata_for_match(peer_esw
,
883 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
885 MLX5_SET(fte_match_set_misc
, misc
, source_port
, vport
);
889 static int esw_add_fdb_peer_miss_rules(struct mlx5_eswitch
*esw
,
890 struct mlx5_core_dev
*peer_dev
)
892 struct mlx5_flow_destination dest
= {};
893 struct mlx5_flow_act flow_act
= {0};
894 struct mlx5_flow_handle
**flows
;
895 struct mlx5_flow_handle
*flow
;
896 struct mlx5_flow_spec
*spec
;
897 /* total vports is the same for both e-switches */
898 int nvports
= esw
->total_vports
;
902 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
906 peer_miss_rules_setup(esw
, peer_dev
, spec
, &dest
);
908 flows
= kvzalloc(nvports
* sizeof(*flows
), GFP_KERNEL
);
911 goto alloc_flows_err
;
914 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
915 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
918 if (mlx5_core_is_ecpf_esw_manager(esw
->dev
)) {
919 esw_set_peer_miss_rule_source_port(esw
, peer_dev
->priv
.eswitch
,
920 spec
, MLX5_VPORT_PF
);
922 flow
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
923 spec
, &flow_act
, &dest
, 1);
926 goto add_pf_flow_err
;
928 flows
[MLX5_VPORT_PF
] = flow
;
931 if (mlx5_ecpf_vport_exists(esw
->dev
)) {
932 MLX5_SET(fte_match_set_misc
, misc
, source_port
, MLX5_VPORT_ECPF
);
933 flow
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
934 spec
, &flow_act
, &dest
, 1);
937 goto add_ecpf_flow_err
;
939 flows
[mlx5_eswitch_ecpf_idx(esw
)] = flow
;
942 mlx5_esw_for_each_vf_vport_num(esw
, i
, mlx5_core_max_vfs(esw
->dev
)) {
943 esw_set_peer_miss_rule_source_port(esw
,
944 peer_dev
->priv
.eswitch
,
947 flow
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
948 spec
, &flow_act
, &dest
, 1);
951 goto add_vf_flow_err
;
956 esw
->fdb_table
.offloads
.peer_miss_rules
= flows
;
963 mlx5_esw_for_each_vf_vport_num_reverse(esw
, i
, nvports
)
964 mlx5_del_flow_rules(flows
[i
]);
966 if (mlx5_ecpf_vport_exists(esw
->dev
))
967 mlx5_del_flow_rules(flows
[mlx5_eswitch_ecpf_idx(esw
)]);
969 if (mlx5_core_is_ecpf_esw_manager(esw
->dev
))
970 mlx5_del_flow_rules(flows
[MLX5_VPORT_PF
]);
972 esw_warn(esw
->dev
, "FDB: Failed to add peer miss flow rule err %d\n", err
);
979 static void esw_del_fdb_peer_miss_rules(struct mlx5_eswitch
*esw
)
981 struct mlx5_flow_handle
**flows
;
984 flows
= esw
->fdb_table
.offloads
.peer_miss_rules
;
986 mlx5_esw_for_each_vf_vport_num_reverse(esw
, i
,
987 mlx5_core_max_vfs(esw
->dev
))
988 mlx5_del_flow_rules(flows
[i
]);
990 if (mlx5_ecpf_vport_exists(esw
->dev
))
991 mlx5_del_flow_rules(flows
[mlx5_eswitch_ecpf_idx(esw
)]);
993 if (mlx5_core_is_ecpf_esw_manager(esw
->dev
))
994 mlx5_del_flow_rules(flows
[MLX5_VPORT_PF
]);
999 static int esw_add_fdb_miss_rule(struct mlx5_eswitch
*esw
)
1001 struct mlx5_flow_act flow_act
= {0};
1002 struct mlx5_flow_destination dest
= {};
1003 struct mlx5_flow_handle
*flow_rule
= NULL
;
1004 struct mlx5_flow_spec
*spec
;
1011 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
1017 spec
->match_criteria_enable
= MLX5_MATCH_OUTER_HEADERS
;
1018 headers_c
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
1020 dmac_c
= MLX5_ADDR_OF(fte_match_param
, headers_c
,
1021 outer_headers
.dmac_47_16
);
1024 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
1025 dest
.vport
.num
= esw
->manager_vport
;
1026 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
1028 flow_rule
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
1029 spec
, &flow_act
, &dest
, 1);
1030 if (IS_ERR(flow_rule
)) {
1031 err
= PTR_ERR(flow_rule
);
1032 esw_warn(esw
->dev
, "FDB: Failed to add unicast miss flow rule err %d\n", err
);
1036 esw
->fdb_table
.offloads
.miss_rule_uni
= flow_rule
;
1038 headers_v
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
1040 dmac_v
= MLX5_ADDR_OF(fte_match_param
, headers_v
,
1041 outer_headers
.dmac_47_16
);
1043 flow_rule
= mlx5_add_flow_rules(esw
->fdb_table
.offloads
.slow_fdb
,
1044 spec
, &flow_act
, &dest
, 1);
1045 if (IS_ERR(flow_rule
)) {
1046 err
= PTR_ERR(flow_rule
);
1047 esw_warn(esw
->dev
, "FDB: Failed to add multicast miss flow rule err %d\n", err
);
1048 mlx5_del_flow_rules(esw
->fdb_table
.offloads
.miss_rule_uni
);
1052 esw
->fdb_table
.offloads
.miss_rule_multi
= flow_rule
;
1059 struct mlx5_flow_handle
*
1060 esw_add_restore_rule(struct mlx5_eswitch
*esw
, u32 tag
)
1062 struct mlx5_flow_act flow_act
= { .flags
= FLOW_ACT_NO_APPEND
, };
1063 struct mlx5_flow_table
*ft
= esw
->offloads
.ft_offloads_restore
;
1064 struct mlx5_flow_context
*flow_context
;
1065 struct mlx5_flow_handle
*flow_rule
;
1066 struct mlx5_flow_destination dest
;
1067 struct mlx5_flow_spec
*spec
;
1070 if (!mlx5_eswitch_reg_c1_loopback_supported(esw
))
1071 return ERR_PTR(-EOPNOTSUPP
);
1073 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
1075 return ERR_PTR(-ENOMEM
);
1077 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
1079 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
1080 ESW_CHAIN_TAG_METADATA_MASK
);
1081 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
1083 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
, tag
);
1084 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS_2
;
1085 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
|
1086 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR
;
1087 flow_act
.modify_hdr
= esw
->offloads
.restore_copy_hdr_id
;
1089 flow_context
= &spec
->flow_context
;
1090 flow_context
->flags
|= FLOW_CONTEXT_HAS_TAG
;
1091 flow_context
->flow_tag
= tag
;
1092 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
1093 dest
.ft
= esw
->offloads
.ft_offloads
;
1095 flow_rule
= mlx5_add_flow_rules(ft
, spec
, &flow_act
, &dest
, 1);
1098 if (IS_ERR(flow_rule
))
1100 "Failed to create restore rule for tag: %d, err(%d)\n",
1101 tag
, (int)PTR_ERR(flow_rule
));
1107 esw_get_max_restore_tag(struct mlx5_eswitch
*esw
)
1109 return ESW_CHAIN_TAG_METADATA_MASK
;
1112 #define MAX_PF_SQ 256
1113 #define MAX_SQ_NVPORTS 32
1115 static void esw_set_flow_group_source_port(struct mlx5_eswitch
*esw
,
1118 void *match_criteria
= MLX5_ADDR_OF(create_flow_group_in
,
1122 if (mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
1123 MLX5_SET(create_flow_group_in
, flow_group_in
,
1124 match_criteria_enable
,
1125 MLX5_MATCH_MISC_PARAMETERS_2
);
1127 MLX5_SET(fte_match_param
, match_criteria
,
1128 misc_parameters_2
.metadata_reg_c_0
,
1129 mlx5_eswitch_get_vport_metadata_mask());
1131 MLX5_SET(create_flow_group_in
, flow_group_in
,
1132 match_criteria_enable
,
1133 MLX5_MATCH_MISC_PARAMETERS
);
1135 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
,
1136 misc_parameters
.source_port
);
1140 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch
*esw
)
1142 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
1143 struct mlx5_flow_table_attr ft_attr
= {};
1144 struct mlx5_core_dev
*dev
= esw
->dev
;
1145 struct mlx5_flow_namespace
*root_ns
;
1146 struct mlx5_flow_table
*fdb
= NULL
;
1147 u32 flags
= 0, *flow_group_in
;
1148 int table_size
, ix
, err
= 0;
1149 struct mlx5_flow_group
*g
;
1150 void *match_criteria
;
1153 esw_debug(esw
->dev
, "Create offloads FDB Tables\n");
1155 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
1159 root_ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_FDB
);
1161 esw_warn(dev
, "Failed to get FDB flow namespace\n");
1165 esw
->fdb_table
.offloads
.ns
= root_ns
;
1166 err
= mlx5_flow_namespace_set_mode(root_ns
,
1167 esw
->dev
->priv
.steering
->mode
);
1169 esw_warn(dev
, "Failed to set FDB namespace steering mode\n");
1173 table_size
= esw
->total_vports
* MAX_SQ_NVPORTS
+ MAX_PF_SQ
+
1174 MLX5_ESW_MISS_FLOWS
+ esw
->total_vports
;
1176 /* create the slow path fdb with encap set, so further table instances
1177 * can be created at run time while VFs are probed if the FW allows that.
1179 if (esw
->offloads
.encap
!= DEVLINK_ESWITCH_ENCAP_MODE_NONE
)
1180 flags
|= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT
|
1181 MLX5_FLOW_TABLE_TUNNEL_EN_DECAP
);
1183 ft_attr
.flags
= flags
;
1184 ft_attr
.max_fte
= table_size
;
1185 ft_attr
.prio
= FDB_SLOW_PATH
;
1187 fdb
= mlx5_create_flow_table(root_ns
, &ft_attr
);
1190 esw_warn(dev
, "Failed to create slow path FDB Table err %d\n", err
);
1193 esw
->fdb_table
.offloads
.slow_fdb
= fdb
;
1195 err
= mlx5_esw_chains_create(esw
);
1197 esw_warn(dev
, "Failed to create fdb chains err(%d)\n", err
);
1198 goto fdb_chains_err
;
1201 /* create send-to-vport group */
1202 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
1203 MLX5_MATCH_MISC_PARAMETERS
);
1205 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
, match_criteria
);
1207 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, misc_parameters
.source_sqn
);
1208 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, misc_parameters
.source_port
);
1210 ix
= esw
->total_vports
* MAX_SQ_NVPORTS
+ MAX_PF_SQ
;
1211 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
1212 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, ix
- 1);
1214 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
1217 esw_warn(dev
, "Failed to create send-to-vport flow group err(%d)\n", err
);
1218 goto send_vport_err
;
1220 esw
->fdb_table
.offloads
.send_to_vport_grp
= g
;
1222 /* create peer esw miss group */
1223 memset(flow_group_in
, 0, inlen
);
1225 esw_set_flow_group_source_port(esw
, flow_group_in
);
1227 if (!mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
1228 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
,
1232 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
,
1233 misc_parameters
.source_eswitch_owner_vhca_id
);
1235 MLX5_SET(create_flow_group_in
, flow_group_in
,
1236 source_eswitch_owner_vhca_id_valid
, 1);
1239 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, ix
);
1240 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
,
1241 ix
+ esw
->total_vports
- 1);
1242 ix
+= esw
->total_vports
;
1244 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
1247 esw_warn(dev
, "Failed to create peer miss flow group err(%d)\n", err
);
1250 esw
->fdb_table
.offloads
.peer_miss_grp
= g
;
1252 /* create miss group */
1253 memset(flow_group_in
, 0, inlen
);
1254 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
1255 MLX5_MATCH_OUTER_HEADERS
);
1256 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
,
1258 dmac
= MLX5_ADDR_OF(fte_match_param
, match_criteria
,
1259 outer_headers
.dmac_47_16
);
1262 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, ix
);
1263 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
,
1264 ix
+ MLX5_ESW_MISS_FLOWS
);
1266 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
1269 esw_warn(dev
, "Failed to create miss flow group err(%d)\n", err
);
1272 esw
->fdb_table
.offloads
.miss_grp
= g
;
1274 err
= esw_add_fdb_miss_rule(esw
);
1278 kvfree(flow_group_in
);
1282 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.miss_grp
);
1284 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.peer_miss_grp
);
1286 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.send_to_vport_grp
);
1288 mlx5_esw_chains_destroy(esw
);
1290 mlx5_destroy_flow_table(esw
->fdb_table
.offloads
.slow_fdb
);
1292 /* Holds true only as long as DMFS is the default */
1293 mlx5_flow_namespace_set_mode(root_ns
, MLX5_FLOW_STEERING_MODE_DMFS
);
1295 kvfree(flow_group_in
);
1299 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch
*esw
)
1301 if (!esw
->fdb_table
.offloads
.slow_fdb
)
1304 esw_debug(esw
->dev
, "Destroy offloads FDB Tables\n");
1305 mlx5_del_flow_rules(esw
->fdb_table
.offloads
.miss_rule_multi
);
1306 mlx5_del_flow_rules(esw
->fdb_table
.offloads
.miss_rule_uni
);
1307 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.send_to_vport_grp
);
1308 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.peer_miss_grp
);
1309 mlx5_destroy_flow_group(esw
->fdb_table
.offloads
.miss_grp
);
1311 mlx5_esw_chains_destroy(esw
);
1312 mlx5_destroy_flow_table(esw
->fdb_table
.offloads
.slow_fdb
);
1313 /* Holds true only as long as DMFS is the default */
1314 mlx5_flow_namespace_set_mode(esw
->fdb_table
.offloads
.ns
,
1315 MLX5_FLOW_STEERING_MODE_DMFS
);
1318 static int esw_create_offloads_table(struct mlx5_eswitch
*esw
)
1320 struct mlx5_flow_table_attr ft_attr
= {};
1321 struct mlx5_core_dev
*dev
= esw
->dev
;
1322 struct mlx5_flow_table
*ft_offloads
;
1323 struct mlx5_flow_namespace
*ns
;
1326 ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_OFFLOADS
);
1328 esw_warn(esw
->dev
, "Failed to get offloads flow namespace\n");
1332 ft_attr
.max_fte
= esw
->total_vports
+ MLX5_ESW_MISS_FLOWS
;
1335 ft_offloads
= mlx5_create_flow_table(ns
, &ft_attr
);
1336 if (IS_ERR(ft_offloads
)) {
1337 err
= PTR_ERR(ft_offloads
);
1338 esw_warn(esw
->dev
, "Failed to create offloads table, err %d\n", err
);
1342 esw
->offloads
.ft_offloads
= ft_offloads
;
1346 static void esw_destroy_offloads_table(struct mlx5_eswitch
*esw
)
1348 struct mlx5_esw_offload
*offloads
= &esw
->offloads
;
1350 mlx5_destroy_flow_table(offloads
->ft_offloads
);
1353 static int esw_create_vport_rx_group(struct mlx5_eswitch
*esw
)
1355 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
1356 struct mlx5_flow_group
*g
;
1361 nvports
= esw
->total_vports
+ MLX5_ESW_MISS_FLOWS
;
1362 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
1366 /* create vport rx group */
1367 esw_set_flow_group_source_port(esw
, flow_group_in
);
1369 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
1370 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, nvports
- 1);
1372 g
= mlx5_create_flow_group(esw
->offloads
.ft_offloads
, flow_group_in
);
1376 mlx5_core_warn(esw
->dev
, "Failed to create vport rx group err %d\n", err
);
1380 esw
->offloads
.vport_rx_group
= g
;
1382 kvfree(flow_group_in
);
1386 static void esw_destroy_vport_rx_group(struct mlx5_eswitch
*esw
)
1388 mlx5_destroy_flow_group(esw
->offloads
.vport_rx_group
);
1391 struct mlx5_flow_handle
*
1392 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch
*esw
, u16 vport
,
1393 struct mlx5_flow_destination
*dest
)
1395 struct mlx5_flow_act flow_act
= {0};
1396 struct mlx5_flow_handle
*flow_rule
;
1397 struct mlx5_flow_spec
*spec
;
1400 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
1402 flow_rule
= ERR_PTR(-ENOMEM
);
1406 if (mlx5_eswitch_vport_match_metadata_enabled(esw
)) {
1407 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
, misc_parameters_2
);
1408 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
1409 mlx5_eswitch_get_vport_metadata_for_match(esw
, vport
));
1411 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
, misc_parameters_2
);
1412 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
1413 mlx5_eswitch_get_vport_metadata_mask());
1415 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS_2
;
1417 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
, misc_parameters
);
1418 MLX5_SET(fte_match_set_misc
, misc
, source_port
, vport
);
1420 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
, misc_parameters
);
1421 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_port
);
1423 spec
->match_criteria_enable
= MLX5_MATCH_MISC_PARAMETERS
;
1426 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
1427 flow_rule
= mlx5_add_flow_rules(esw
->offloads
.ft_offloads
, spec
,
1428 &flow_act
, dest
, 1);
1429 if (IS_ERR(flow_rule
)) {
1430 esw_warn(esw
->dev
, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule
));
1440 static int mlx5_eswitch_inline_mode_get(const struct mlx5_eswitch
*esw
, u8
*mode
)
1442 u8 prev_mlx5_mode
, mlx5_mode
= MLX5_INLINE_MODE_L2
;
1443 struct mlx5_core_dev
*dev
= esw
->dev
;
1446 if (!MLX5_CAP_GEN(dev
, vport_group_manager
))
1449 if (esw
->mode
== MLX5_ESWITCH_NONE
)
1452 switch (MLX5_CAP_ETH(dev
, wqe_inline_mode
)) {
1453 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED
:
1454 mlx5_mode
= MLX5_INLINE_MODE_NONE
;
1456 case MLX5_CAP_INLINE_MODE_L2
:
1457 mlx5_mode
= MLX5_INLINE_MODE_L2
;
1459 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT
:
1464 mlx5_query_nic_vport_min_inline(dev
, esw
->first_host_vport
, &prev_mlx5_mode
);
1465 mlx5_esw_for_each_host_func_vport(esw
, vport
, esw
->esw_funcs
.num_vfs
) {
1466 mlx5_query_nic_vport_min_inline(dev
, vport
, &mlx5_mode
);
1467 if (prev_mlx5_mode
!= mlx5_mode
)
1469 prev_mlx5_mode
= mlx5_mode
;
1477 static void esw_destroy_restore_table(struct mlx5_eswitch
*esw
)
1479 struct mlx5_esw_offload
*offloads
= &esw
->offloads
;
1481 if (!mlx5_eswitch_reg_c1_loopback_supported(esw
))
1484 mlx5_modify_header_dealloc(esw
->dev
, offloads
->restore_copy_hdr_id
);
1485 mlx5_destroy_flow_group(offloads
->restore_group
);
1486 mlx5_destroy_flow_table(offloads
->ft_offloads_restore
);
1489 static int esw_create_restore_table(struct mlx5_eswitch
*esw
)
1491 u8 modact
[MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto
)] = {};
1492 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
1493 struct mlx5_flow_table_attr ft_attr
= {};
1494 struct mlx5_core_dev
*dev
= esw
->dev
;
1495 struct mlx5_flow_namespace
*ns
;
1496 struct mlx5_modify_hdr
*mod_hdr
;
1497 void *match_criteria
, *misc
;
1498 struct mlx5_flow_table
*ft
;
1499 struct mlx5_flow_group
*g
;
1503 if (!mlx5_eswitch_reg_c1_loopback_supported(esw
))
1506 ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_OFFLOADS
);
1508 esw_warn(esw
->dev
, "Failed to get offloads flow namespace\n");
1512 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
1513 if (!flow_group_in
) {
1518 ft_attr
.max_fte
= 1 << ESW_CHAIN_TAG_METADATA_BITS
;
1519 ft
= mlx5_create_flow_table(ns
, &ft_attr
);
1522 esw_warn(esw
->dev
, "Failed to create restore table, err %d\n",
1527 memset(flow_group_in
, 0, inlen
);
1528 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
,
1530 misc
= MLX5_ADDR_OF(fte_match_param
, match_criteria
,
1533 MLX5_SET(fte_match_set_misc2
, misc
, metadata_reg_c_0
,
1534 ESW_CHAIN_TAG_METADATA_MASK
);
1535 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
1536 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
,
1537 ft_attr
.max_fte
- 1);
1538 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
1539 MLX5_MATCH_MISC_PARAMETERS_2
);
1540 g
= mlx5_create_flow_group(ft
, flow_group_in
);
1543 esw_warn(dev
, "Failed to create restore flow group, err: %d\n",
1548 MLX5_SET(copy_action_in
, modact
, action_type
, MLX5_ACTION_TYPE_COPY
);
1549 MLX5_SET(copy_action_in
, modact
, src_field
,
1550 MLX5_ACTION_IN_FIELD_METADATA_REG_C_1
);
1551 MLX5_SET(copy_action_in
, modact
, dst_field
,
1552 MLX5_ACTION_IN_FIELD_METADATA_REG_B
);
1553 mod_hdr
= mlx5_modify_header_alloc(esw
->dev
,
1554 MLX5_FLOW_NAMESPACE_KERNEL
, 1,
1556 if (IS_ERR(mod_hdr
)) {
1557 err
= PTR_ERR(mod_hdr
);
1558 esw_warn(dev
, "Failed to create restore mod header, err: %d\n",
1563 esw
->offloads
.ft_offloads_restore
= ft
;
1564 esw
->offloads
.restore_group
= g
;
1565 esw
->offloads
.restore_copy_hdr_id
= mod_hdr
;
1567 kvfree(flow_group_in
);
1572 mlx5_destroy_flow_group(g
);
1574 mlx5_destroy_flow_table(ft
);
1576 kvfree(flow_group_in
);
1581 static int esw_offloads_start(struct mlx5_eswitch
*esw
,
1582 struct netlink_ext_ack
*extack
)
1586 mlx5_eswitch_disable_locked(esw
, false);
1587 err
= mlx5_eswitch_enable_locked(esw
, MLX5_ESWITCH_OFFLOADS
,
1588 esw
->dev
->priv
.sriov
.num_vfs
);
1590 NL_SET_ERR_MSG_MOD(extack
,
1591 "Failed setting eswitch to offloads");
1592 err1
= mlx5_eswitch_enable_locked(esw
, MLX5_ESWITCH_LEGACY
,
1593 MLX5_ESWITCH_IGNORE_NUM_VFS
);
1595 NL_SET_ERR_MSG_MOD(extack
,
1596 "Failed setting eswitch back to legacy");
1599 if (esw
->offloads
.inline_mode
== MLX5_INLINE_MODE_NONE
) {
1600 if (mlx5_eswitch_inline_mode_get(esw
,
1601 &esw
->offloads
.inline_mode
)) {
1602 esw
->offloads
.inline_mode
= MLX5_INLINE_MODE_L2
;
1603 NL_SET_ERR_MSG_MOD(extack
,
1604 "Inline mode is different between vports");
1610 void esw_offloads_cleanup_reps(struct mlx5_eswitch
*esw
)
1612 kfree(esw
->offloads
.vport_reps
);
1615 int esw_offloads_init_reps(struct mlx5_eswitch
*esw
)
1617 int total_vports
= esw
->total_vports
;
1618 struct mlx5_eswitch_rep
*rep
;
1622 esw
->offloads
.vport_reps
= kcalloc(total_vports
,
1623 sizeof(struct mlx5_eswitch_rep
),
1625 if (!esw
->offloads
.vport_reps
)
1628 mlx5_esw_for_all_reps(esw
, vport_index
, rep
) {
1629 rep
->vport
= mlx5_eswitch_index_to_vport_num(esw
, vport_index
);
1630 rep
->vport_index
= vport_index
;
1632 for (rep_type
= 0; rep_type
< NUM_REP_TYPES
; rep_type
++)
1633 atomic_set(&rep
->rep_data
[rep_type
].state
,
1640 static void __esw_offloads_unload_rep(struct mlx5_eswitch
*esw
,
1641 struct mlx5_eswitch_rep
*rep
, u8 rep_type
)
1643 if (atomic_cmpxchg(&rep
->rep_data
[rep_type
].state
,
1644 REP_LOADED
, REP_REGISTERED
) == REP_LOADED
)
1645 esw
->offloads
.rep_ops
[rep_type
]->unload(rep
);
1648 static void __unload_reps_all_vport(struct mlx5_eswitch
*esw
, u8 rep_type
)
1650 struct mlx5_eswitch_rep
*rep
;
1653 mlx5_esw_for_each_vf_rep_reverse(esw
, i
, rep
, esw
->esw_funcs
.num_vfs
)
1654 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1656 if (mlx5_ecpf_vport_exists(esw
->dev
)) {
1657 rep
= mlx5_eswitch_get_rep(esw
, MLX5_VPORT_ECPF
);
1658 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1661 if (mlx5_core_is_ecpf_esw_manager(esw
->dev
)) {
1662 rep
= mlx5_eswitch_get_rep(esw
, MLX5_VPORT_PF
);
1663 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1666 rep
= mlx5_eswitch_get_rep(esw
, MLX5_VPORT_UPLINK
);
1667 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1670 int esw_offloads_load_rep(struct mlx5_eswitch
*esw
, u16 vport_num
)
1672 struct mlx5_eswitch_rep
*rep
;
1676 if (esw
->mode
!= MLX5_ESWITCH_OFFLOADS
)
1679 rep
= mlx5_eswitch_get_rep(esw
, vport_num
);
1680 for (rep_type
= 0; rep_type
< NUM_REP_TYPES
; rep_type
++)
1681 if (atomic_cmpxchg(&rep
->rep_data
[rep_type
].state
,
1682 REP_REGISTERED
, REP_LOADED
) == REP_REGISTERED
) {
1683 err
= esw
->offloads
.rep_ops
[rep_type
]->load(esw
->dev
, rep
);
1691 atomic_set(&rep
->rep_data
[rep_type
].state
, REP_REGISTERED
);
1692 for (--rep_type
; rep_type
>= 0; rep_type
--)
1693 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1697 void esw_offloads_unload_rep(struct mlx5_eswitch
*esw
, u16 vport_num
)
1699 struct mlx5_eswitch_rep
*rep
;
1702 if (esw
->mode
!= MLX5_ESWITCH_OFFLOADS
)
1705 rep
= mlx5_eswitch_get_rep(esw
, vport_num
);
1706 for (rep_type
= NUM_REP_TYPES
- 1; rep_type
>= 0; rep_type
--)
1707 __esw_offloads_unload_rep(esw
, rep
, rep_type
);
1710 #define ESW_OFFLOADS_DEVCOM_PAIR (0)
1711 #define ESW_OFFLOADS_DEVCOM_UNPAIR (1)
1713 static int mlx5_esw_offloads_pair(struct mlx5_eswitch
*esw
,
1714 struct mlx5_eswitch
*peer_esw
)
1718 err
= esw_add_fdb_peer_miss_rules(esw
, peer_esw
->dev
);
1725 static void mlx5_esw_offloads_unpair(struct mlx5_eswitch
*esw
)
1727 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
1728 mlx5e_tc_clean_fdb_peer_flows(esw
);
1730 esw_del_fdb_peer_miss_rules(esw
);
1733 static int mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch
*esw
,
1734 struct mlx5_eswitch
*peer_esw
,
1737 struct mlx5_flow_root_namespace
*peer_ns
;
1738 struct mlx5_flow_root_namespace
*ns
;
1741 peer_ns
= peer_esw
->dev
->priv
.steering
->fdb_root_ns
;
1742 ns
= esw
->dev
->priv
.steering
->fdb_root_ns
;
1745 err
= mlx5_flow_namespace_set_peer(ns
, peer_ns
);
1749 err
= mlx5_flow_namespace_set_peer(peer_ns
, ns
);
1751 mlx5_flow_namespace_set_peer(ns
, NULL
);
1755 mlx5_flow_namespace_set_peer(ns
, NULL
);
1756 mlx5_flow_namespace_set_peer(peer_ns
, NULL
);
1762 static int mlx5_esw_offloads_devcom_event(int event
,
1766 struct mlx5_eswitch
*esw
= my_data
;
1767 struct mlx5_devcom
*devcom
= esw
->dev
->priv
.devcom
;
1768 struct mlx5_eswitch
*peer_esw
= event_data
;
1772 case ESW_OFFLOADS_DEVCOM_PAIR
:
1773 if (mlx5_eswitch_vport_match_metadata_enabled(esw
) !=
1774 mlx5_eswitch_vport_match_metadata_enabled(peer_esw
))
1777 err
= mlx5_esw_offloads_set_ns_peer(esw
, peer_esw
, true);
1780 err
= mlx5_esw_offloads_pair(esw
, peer_esw
);
1784 err
= mlx5_esw_offloads_pair(peer_esw
, esw
);
1788 mlx5_devcom_set_paired(devcom
, MLX5_DEVCOM_ESW_OFFLOADS
, true);
1791 case ESW_OFFLOADS_DEVCOM_UNPAIR
:
1792 if (!mlx5_devcom_is_paired(devcom
, MLX5_DEVCOM_ESW_OFFLOADS
))
1795 mlx5_devcom_set_paired(devcom
, MLX5_DEVCOM_ESW_OFFLOADS
, false);
1796 mlx5_esw_offloads_unpair(peer_esw
);
1797 mlx5_esw_offloads_unpair(esw
);
1798 mlx5_esw_offloads_set_ns_peer(esw
, peer_esw
, false);
1805 mlx5_esw_offloads_unpair(esw
);
1807 mlx5_esw_offloads_set_ns_peer(esw
, peer_esw
, false);
1809 mlx5_core_err(esw
->dev
, "esw offloads devcom event failure, event %u err %d",
1814 static void esw_offloads_devcom_init(struct mlx5_eswitch
*esw
)
1816 struct mlx5_devcom
*devcom
= esw
->dev
->priv
.devcom
;
1818 INIT_LIST_HEAD(&esw
->offloads
.peer_flows
);
1819 mutex_init(&esw
->offloads
.peer_mutex
);
1821 if (!MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
1824 mlx5_devcom_register_component(devcom
,
1825 MLX5_DEVCOM_ESW_OFFLOADS
,
1826 mlx5_esw_offloads_devcom_event
,
1829 mlx5_devcom_send_event(devcom
,
1830 MLX5_DEVCOM_ESW_OFFLOADS
,
1831 ESW_OFFLOADS_DEVCOM_PAIR
, esw
);
1834 static void esw_offloads_devcom_cleanup(struct mlx5_eswitch
*esw
)
1836 struct mlx5_devcom
*devcom
= esw
->dev
->priv
.devcom
;
1838 if (!MLX5_CAP_ESW(esw
->dev
, merged_eswitch
))
1841 mlx5_devcom_send_event(devcom
, MLX5_DEVCOM_ESW_OFFLOADS
,
1842 ESW_OFFLOADS_DEVCOM_UNPAIR
, esw
);
1844 mlx5_devcom_unregister_component(devcom
, MLX5_DEVCOM_ESW_OFFLOADS
);
1848 esw_check_vport_match_metadata_supported(const struct mlx5_eswitch
*esw
)
1850 if (!MLX5_CAP_ESW(esw
->dev
, esw_uplink_ingress_acl
))
1853 if (!(MLX5_CAP_ESW_FLOWTABLE(esw
->dev
, fdb_to_vport_reg_c_id
) &
1854 MLX5_FDB_TO_VPORT_REG_C_0
))
1857 if (!MLX5_CAP_ESW_FLOWTABLE(esw
->dev
, flow_source
))
1860 if (mlx5_core_is_ecpf_esw_manager(esw
->dev
) ||
1861 mlx5_ecpf_vport_exists(esw
->dev
))
1868 esw_check_vport_match_metadata_mandatory(const struct mlx5_eswitch
*esw
)
1870 return mlx5_core_mp_enabled(esw
->dev
);
1873 static bool esw_use_vport_metadata(const struct mlx5_eswitch
*esw
)
1875 return esw_check_vport_match_metadata_mandatory(esw
) &&
1876 esw_check_vport_match_metadata_supported(esw
);
1879 u32
mlx5_esw_match_metadata_alloc(struct mlx5_eswitch
*esw
)
1881 u32 num_vports
= GENMASK(ESW_VPORT_BITS
- 1, 0) - 1;
1882 u32 vhca_id_mask
= GENMASK(ESW_VHCA_ID_BITS
- 1, 0);
1883 u32 vhca_id
= MLX5_CAP_GEN(esw
->dev
, vhca_id
);
1888 /* Make sure the vhca_id fits the ESW_VHCA_ID_BITS */
1889 WARN_ON_ONCE(vhca_id
>= BIT(ESW_VHCA_ID_BITS
));
1891 /* Trim vhca_id to ESW_VHCA_ID_BITS */
1892 vhca_id
&= vhca_id_mask
;
1894 start
= (vhca_id
<< ESW_VPORT_BITS
);
1895 end
= start
+ num_vports
;
1897 start
+= 1; /* zero is reserved/invalid metadata */
1898 id
= ida_alloc_range(&esw
->offloads
.vport_metadata_ida
, start
, end
, GFP_KERNEL
);
1900 return (id
< 0) ? 0 : id
;
1903 void mlx5_esw_match_metadata_free(struct mlx5_eswitch
*esw
, u32 metadata
)
1905 ida_free(&esw
->offloads
.vport_metadata_ida
, metadata
);
1908 static int esw_offloads_vport_metadata_setup(struct mlx5_eswitch
*esw
,
1909 struct mlx5_vport
*vport
)
1911 if (vport
->vport
== MLX5_VPORT_UPLINK
)
1914 vport
->default_metadata
= mlx5_esw_match_metadata_alloc(esw
);
1915 vport
->metadata
= vport
->default_metadata
;
1916 return vport
->metadata
? 0 : -ENOSPC
;
1919 static void esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch
*esw
,
1920 struct mlx5_vport
*vport
)
1922 if (vport
->vport
== MLX5_VPORT_UPLINK
|| !vport
->default_metadata
)
1925 WARN_ON(vport
->metadata
!= vport
->default_metadata
);
1926 mlx5_esw_match_metadata_free(esw
, vport
->default_metadata
);
1930 esw_vport_create_offloads_acl_tables(struct mlx5_eswitch
*esw
,
1931 struct mlx5_vport
*vport
)
1935 err
= esw_offloads_vport_metadata_setup(esw
, vport
);
1939 err
= esw_acl_ingress_ofld_setup(esw
, vport
);
1943 if (mlx5_eswitch_is_vf_vport(esw
, vport
->vport
)) {
1944 err
= esw_acl_egress_ofld_setup(esw
, vport
);
1952 esw_acl_ingress_ofld_cleanup(esw
, vport
);
1954 esw_offloads_vport_metadata_cleanup(esw
, vport
);
1960 esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch
*esw
,
1961 struct mlx5_vport
*vport
)
1963 esw_acl_egress_ofld_cleanup(vport
);
1964 esw_acl_ingress_ofld_cleanup(esw
, vport
);
1965 esw_offloads_vport_metadata_cleanup(esw
, vport
);
1968 static int esw_create_uplink_offloads_acl_tables(struct mlx5_eswitch
*esw
)
1970 struct mlx5_vport
*vport
;
1973 if (esw_use_vport_metadata(esw
))
1974 esw
->flags
|= MLX5_ESWITCH_VPORT_MATCH_METADATA
;
1976 vport
= mlx5_eswitch_get_vport(esw
, MLX5_VPORT_UPLINK
);
1977 err
= esw_vport_create_offloads_acl_tables(esw
, vport
);
1979 esw
->flags
&= ~MLX5_ESWITCH_VPORT_MATCH_METADATA
;
1983 static void esw_destroy_uplink_offloads_acl_tables(struct mlx5_eswitch
*esw
)
1985 struct mlx5_vport
*vport
;
1987 vport
= mlx5_eswitch_get_vport(esw
, MLX5_VPORT_UPLINK
);
1988 esw_vport_destroy_offloads_acl_tables(esw
, vport
);
1989 esw
->flags
&= ~MLX5_ESWITCH_VPORT_MATCH_METADATA
;
1992 static int esw_offloads_steering_init(struct mlx5_eswitch
*esw
)
1996 memset(&esw
->fdb_table
.offloads
, 0, sizeof(struct offloads_fdb
));
1997 mutex_init(&esw
->fdb_table
.offloads
.vports
.lock
);
1998 hash_init(esw
->fdb_table
.offloads
.vports
.table
);
2000 err
= esw_create_uplink_offloads_acl_tables(esw
);
2002 goto create_acl_err
;
2004 err
= esw_create_offloads_table(esw
);
2006 goto create_offloads_err
;
2008 err
= esw_create_restore_table(esw
);
2010 goto create_restore_err
;
2012 err
= esw_create_offloads_fdb_tables(esw
);
2014 goto create_fdb_err
;
2016 err
= esw_create_vport_rx_group(esw
);
2023 esw_destroy_offloads_fdb_tables(esw
);
2025 esw_destroy_restore_table(esw
);
2027 esw_destroy_offloads_table(esw
);
2028 create_offloads_err
:
2029 esw_destroy_uplink_offloads_acl_tables(esw
);
2031 mutex_destroy(&esw
->fdb_table
.offloads
.vports
.lock
);
2035 static void esw_offloads_steering_cleanup(struct mlx5_eswitch
*esw
)
2037 esw_destroy_vport_rx_group(esw
);
2038 esw_destroy_offloads_fdb_tables(esw
);
2039 esw_destroy_restore_table(esw
);
2040 esw_destroy_offloads_table(esw
);
2041 esw_destroy_uplink_offloads_acl_tables(esw
);
2042 mutex_destroy(&esw
->fdb_table
.offloads
.vports
.lock
);
2046 esw_vfs_changed_event_handler(struct mlx5_eswitch
*esw
, const u32
*out
)
2048 bool host_pf_disabled
;
2051 new_num_vfs
= MLX5_GET(query_esw_functions_out
, out
,
2052 host_params_context
.host_num_of_vfs
);
2053 host_pf_disabled
= MLX5_GET(query_esw_functions_out
, out
,
2054 host_params_context
.host_pf_disabled
);
2056 if (new_num_vfs
== esw
->esw_funcs
.num_vfs
|| host_pf_disabled
)
2059 /* Number of VFs can only change from "0 to x" or "x to 0". */
2060 if (esw
->esw_funcs
.num_vfs
> 0) {
2061 mlx5_eswitch_unload_vf_vports(esw
, esw
->esw_funcs
.num_vfs
);
2065 err
= mlx5_eswitch_load_vf_vports(esw
, new_num_vfs
,
2066 MLX5_VPORT_UC_ADDR_CHANGE
);
2070 esw
->esw_funcs
.num_vfs
= new_num_vfs
;
2073 static void esw_functions_changed_event_handler(struct work_struct
*work
)
2075 struct mlx5_host_work
*host_work
;
2076 struct mlx5_eswitch
*esw
;
2079 host_work
= container_of(work
, struct mlx5_host_work
, work
);
2080 esw
= host_work
->esw
;
2082 out
= mlx5_esw_query_functions(esw
->dev
);
2086 esw_vfs_changed_event_handler(esw
, out
);
2092 int mlx5_esw_funcs_changed_handler(struct notifier_block
*nb
, unsigned long type
, void *data
)
2094 struct mlx5_esw_functions
*esw_funcs
;
2095 struct mlx5_host_work
*host_work
;
2096 struct mlx5_eswitch
*esw
;
2098 host_work
= kzalloc(sizeof(*host_work
), GFP_ATOMIC
);
2102 esw_funcs
= mlx5_nb_cof(nb
, struct mlx5_esw_functions
, nb
);
2103 esw
= container_of(esw_funcs
, struct mlx5_eswitch
, esw_funcs
);
2105 host_work
->esw
= esw
;
2107 INIT_WORK(&host_work
->work
, esw_functions_changed_event_handler
);
2108 queue_work(esw
->work_queue
, &host_work
->work
);
2113 int esw_offloads_enable(struct mlx5_eswitch
*esw
)
2115 struct mlx5_vport
*vport
;
2118 if (MLX5_CAP_ESW_FLOWTABLE_FDB(esw
->dev
, reformat
) &&
2119 MLX5_CAP_ESW_FLOWTABLE_FDB(esw
->dev
, decap
))
2120 esw
->offloads
.encap
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
2122 esw
->offloads
.encap
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
2124 mutex_init(&esw
->offloads
.termtbl_mutex
);
2125 mlx5_rdma_enable_roce(esw
->dev
);
2127 err
= esw_set_passing_vport_metadata(esw
, true);
2129 goto err_vport_metadata
;
2131 err
= esw_offloads_steering_init(esw
);
2133 goto err_steering_init
;
2135 /* Representor will control the vport link state */
2136 mlx5_esw_for_each_vf_vport(esw
, i
, vport
, esw
->esw_funcs
.num_vfs
)
2137 vport
->info
.link_state
= MLX5_VPORT_ADMIN_STATE_DOWN
;
2139 /* Uplink vport rep must load first. */
2140 err
= esw_offloads_load_rep(esw
, MLX5_VPORT_UPLINK
);
2144 err
= mlx5_eswitch_enable_pf_vf_vports(esw
, MLX5_VPORT_UC_ADDR_CHANGE
);
2148 esw_offloads_devcom_init(esw
);
2153 esw_offloads_unload_rep(esw
, MLX5_VPORT_UPLINK
);
2155 esw_offloads_steering_cleanup(esw
);
2157 esw_set_passing_vport_metadata(esw
, false);
2159 mlx5_rdma_disable_roce(esw
->dev
);
2160 mutex_destroy(&esw
->offloads
.termtbl_mutex
);
2164 static int esw_offloads_stop(struct mlx5_eswitch
*esw
,
2165 struct netlink_ext_ack
*extack
)
2169 mlx5_eswitch_disable_locked(esw
, false);
2170 err
= mlx5_eswitch_enable_locked(esw
, MLX5_ESWITCH_LEGACY
,
2171 MLX5_ESWITCH_IGNORE_NUM_VFS
);
2173 NL_SET_ERR_MSG_MOD(extack
, "Failed setting eswitch to legacy");
2174 err1
= mlx5_eswitch_enable_locked(esw
, MLX5_ESWITCH_OFFLOADS
,
2175 MLX5_ESWITCH_IGNORE_NUM_VFS
);
2177 NL_SET_ERR_MSG_MOD(extack
,
2178 "Failed setting eswitch back to offloads");
2185 void esw_offloads_disable(struct mlx5_eswitch
*esw
)
2187 esw_offloads_devcom_cleanup(esw
);
2188 mlx5_eswitch_disable_pf_vf_vports(esw
);
2189 esw_offloads_unload_rep(esw
, MLX5_VPORT_UPLINK
);
2190 esw_set_passing_vport_metadata(esw
, false);
2191 esw_offloads_steering_cleanup(esw
);
2192 mlx5_rdma_disable_roce(esw
->dev
);
2193 mutex_destroy(&esw
->offloads
.termtbl_mutex
);
2194 esw
->offloads
.encap
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
2197 static int esw_mode_from_devlink(u16 mode
, u16
*mlx5_mode
)
2200 case DEVLINK_ESWITCH_MODE_LEGACY
:
2201 *mlx5_mode
= MLX5_ESWITCH_LEGACY
;
2203 case DEVLINK_ESWITCH_MODE_SWITCHDEV
:
2204 *mlx5_mode
= MLX5_ESWITCH_OFFLOADS
;
2213 static int esw_mode_to_devlink(u16 mlx5_mode
, u16
*mode
)
2215 switch (mlx5_mode
) {
2216 case MLX5_ESWITCH_LEGACY
:
2217 *mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
2219 case MLX5_ESWITCH_OFFLOADS
:
2220 *mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
2229 static int esw_inline_mode_from_devlink(u8 mode
, u8
*mlx5_mode
)
2232 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2233 *mlx5_mode
= MLX5_INLINE_MODE_NONE
;
2235 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2236 *mlx5_mode
= MLX5_INLINE_MODE_L2
;
2238 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2239 *mlx5_mode
= MLX5_INLINE_MODE_IP
;
2241 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2242 *mlx5_mode
= MLX5_INLINE_MODE_TCP_UDP
;
2251 static int esw_inline_mode_to_devlink(u8 mlx5_mode
, u8
*mode
)
2253 switch (mlx5_mode
) {
2254 case MLX5_INLINE_MODE_NONE
:
2255 *mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
2257 case MLX5_INLINE_MODE_L2
:
2258 *mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
2260 case MLX5_INLINE_MODE_IP
:
2261 *mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
2263 case MLX5_INLINE_MODE_TCP_UDP
:
2264 *mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
2273 static int eswitch_devlink_esw_mode_check(const struct mlx5_eswitch
*esw
)
2275 /* devlink commands in NONE eswitch mode are currently supported only
2278 return (esw
->mode
== MLX5_ESWITCH_NONE
&&
2279 !mlx5_core_is_ecpf_esw_manager(esw
->dev
)) ? -EOPNOTSUPP
: 0;
2282 int mlx5_devlink_eswitch_mode_set(struct devlink
*devlink
, u16 mode
,
2283 struct netlink_ext_ack
*extack
)
2285 u16 cur_mlx5_mode
, mlx5_mode
= 0;
2286 struct mlx5_eswitch
*esw
;
2289 esw
= mlx5_devlink_eswitch_get(devlink
);
2291 return PTR_ERR(esw
);
2293 if (esw_mode_from_devlink(mode
, &mlx5_mode
))
2296 mutex_lock(&esw
->mode_lock
);
2297 cur_mlx5_mode
= esw
->mode
;
2298 if (cur_mlx5_mode
== mlx5_mode
)
2301 if (mode
== DEVLINK_ESWITCH_MODE_SWITCHDEV
)
2302 err
= esw_offloads_start(esw
, extack
);
2303 else if (mode
== DEVLINK_ESWITCH_MODE_LEGACY
)
2304 err
= esw_offloads_stop(esw
, extack
);
2309 mutex_unlock(&esw
->mode_lock
);
2313 int mlx5_devlink_eswitch_mode_get(struct devlink
*devlink
, u16
*mode
)
2315 struct mlx5_eswitch
*esw
;
2318 esw
= mlx5_devlink_eswitch_get(devlink
);
2320 return PTR_ERR(esw
);
2322 mutex_lock(&esw
->mode_lock
);
2323 err
= eswitch_devlink_esw_mode_check(esw
);
2327 err
= esw_mode_to_devlink(esw
->mode
, mode
);
2329 mutex_unlock(&esw
->mode_lock
);
2333 int mlx5_devlink_eswitch_inline_mode_set(struct devlink
*devlink
, u8 mode
,
2334 struct netlink_ext_ack
*extack
)
2336 struct mlx5_core_dev
*dev
= devlink_priv(devlink
);
2337 int err
, vport
, num_vport
;
2338 struct mlx5_eswitch
*esw
;
2341 esw
= mlx5_devlink_eswitch_get(devlink
);
2343 return PTR_ERR(esw
);
2345 mutex_lock(&esw
->mode_lock
);
2346 err
= eswitch_devlink_esw_mode_check(esw
);
2350 switch (MLX5_CAP_ETH(dev
, wqe_inline_mode
)) {
2351 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED
:
2352 if (mode
== DEVLINK_ESWITCH_INLINE_MODE_NONE
)
2355 case MLX5_CAP_INLINE_MODE_L2
:
2356 NL_SET_ERR_MSG_MOD(extack
, "Inline mode can't be set");
2359 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT
:
2363 if (atomic64_read(&esw
->offloads
.num_flows
) > 0) {
2364 NL_SET_ERR_MSG_MOD(extack
,
2365 "Can't set inline mode when flows are configured");
2370 err
= esw_inline_mode_from_devlink(mode
, &mlx5_mode
);
2374 mlx5_esw_for_each_host_func_vport(esw
, vport
, esw
->esw_funcs
.num_vfs
) {
2375 err
= mlx5_modify_nic_vport_min_inline(dev
, vport
, mlx5_mode
);
2377 NL_SET_ERR_MSG_MOD(extack
,
2378 "Failed to set min inline on vport");
2379 goto revert_inline_mode
;
2383 esw
->offloads
.inline_mode
= mlx5_mode
;
2384 mutex_unlock(&esw
->mode_lock
);
2388 num_vport
= --vport
;
2389 mlx5_esw_for_each_host_func_vport_reverse(esw
, vport
, num_vport
)
2390 mlx5_modify_nic_vport_min_inline(dev
,
2392 esw
->offloads
.inline_mode
);
2394 mutex_unlock(&esw
->mode_lock
);
2398 int mlx5_devlink_eswitch_inline_mode_get(struct devlink
*devlink
, u8
*mode
)
2400 struct mlx5_eswitch
*esw
;
2403 esw
= mlx5_devlink_eswitch_get(devlink
);
2405 return PTR_ERR(esw
);
2407 mutex_lock(&esw
->mode_lock
);
2408 err
= eswitch_devlink_esw_mode_check(esw
);
2412 err
= esw_inline_mode_to_devlink(esw
->offloads
.inline_mode
, mode
);
2414 mutex_unlock(&esw
->mode_lock
);
2418 int mlx5_devlink_eswitch_encap_mode_set(struct devlink
*devlink
,
2419 enum devlink_eswitch_encap_mode encap
,
2420 struct netlink_ext_ack
*extack
)
2422 struct mlx5_core_dev
*dev
= devlink_priv(devlink
);
2423 struct mlx5_eswitch
*esw
;
2426 esw
= mlx5_devlink_eswitch_get(devlink
);
2428 return PTR_ERR(esw
);
2430 mutex_lock(&esw
->mode_lock
);
2431 err
= eswitch_devlink_esw_mode_check(esw
);
2435 if (encap
!= DEVLINK_ESWITCH_ENCAP_MODE_NONE
&&
2436 (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, reformat
) ||
2437 !MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, decap
))) {
2442 if (encap
&& encap
!= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
) {
2447 if (esw
->mode
== MLX5_ESWITCH_LEGACY
) {
2448 esw
->offloads
.encap
= encap
;
2452 if (esw
->offloads
.encap
== encap
)
2455 if (atomic64_read(&esw
->offloads
.num_flows
) > 0) {
2456 NL_SET_ERR_MSG_MOD(extack
,
2457 "Can't set encapsulation when flows are configured");
2462 esw_destroy_offloads_fdb_tables(esw
);
2464 esw
->offloads
.encap
= encap
;
2466 err
= esw_create_offloads_fdb_tables(esw
);
2469 NL_SET_ERR_MSG_MOD(extack
,
2470 "Failed re-creating fast FDB table");
2471 esw
->offloads
.encap
= !encap
;
2472 (void)esw_create_offloads_fdb_tables(esw
);
2476 mutex_unlock(&esw
->mode_lock
);
2480 int mlx5_devlink_eswitch_encap_mode_get(struct devlink
*devlink
,
2481 enum devlink_eswitch_encap_mode
*encap
)
2483 struct mlx5_eswitch
*esw
;
2486 esw
= mlx5_devlink_eswitch_get(devlink
);
2488 return PTR_ERR(esw
);
2491 mutex_lock(&esw
->mode_lock
);
2492 err
= eswitch_devlink_esw_mode_check(esw
);
2496 *encap
= esw
->offloads
.encap
;
2498 mutex_unlock(&esw
->mode_lock
);
2503 mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch
*esw
, u16 vport_num
)
2505 /* Currently, only ECPF based device has representor for host PF. */
2506 if (vport_num
== MLX5_VPORT_PF
&&
2507 !mlx5_core_is_ecpf_esw_manager(esw
->dev
))
2510 if (vport_num
== MLX5_VPORT_ECPF
&&
2511 !mlx5_ecpf_vport_exists(esw
->dev
))
2517 void mlx5_eswitch_register_vport_reps(struct mlx5_eswitch
*esw
,
2518 const struct mlx5_eswitch_rep_ops
*ops
,
2521 struct mlx5_eswitch_rep_data
*rep_data
;
2522 struct mlx5_eswitch_rep
*rep
;
2525 esw
->offloads
.rep_ops
[rep_type
] = ops
;
2526 mlx5_esw_for_all_reps(esw
, i
, rep
) {
2527 if (likely(mlx5_eswitch_vport_has_rep(esw
, i
))) {
2528 rep_data
= &rep
->rep_data
[rep_type
];
2529 atomic_set(&rep_data
->state
, REP_REGISTERED
);
2533 EXPORT_SYMBOL(mlx5_eswitch_register_vport_reps
);
2535 void mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch
*esw
, u8 rep_type
)
2537 struct mlx5_eswitch_rep
*rep
;
2540 if (esw
->mode
== MLX5_ESWITCH_OFFLOADS
)
2541 __unload_reps_all_vport(esw
, rep_type
);
2543 mlx5_esw_for_all_reps(esw
, i
, rep
)
2544 atomic_set(&rep
->rep_data
[rep_type
].state
, REP_UNREGISTERED
);
2546 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_reps
);
2548 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch
*esw
, u8 rep_type
)
2550 struct mlx5_eswitch_rep
*rep
;
2552 rep
= mlx5_eswitch_get_rep(esw
, MLX5_VPORT_UPLINK
);
2553 return rep
->rep_data
[rep_type
].priv
;
2556 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch
*esw
,
2560 struct mlx5_eswitch_rep
*rep
;
2562 rep
= mlx5_eswitch_get_rep(esw
, vport
);
2564 if (atomic_read(&rep
->rep_data
[rep_type
].state
) == REP_LOADED
&&
2565 esw
->offloads
.rep_ops
[rep_type
]->get_proto_dev
)
2566 return esw
->offloads
.rep_ops
[rep_type
]->get_proto_dev(rep
);
2569 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev
);
2571 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch
*esw
, u8 rep_type
)
2573 return mlx5_eswitch_get_proto_dev(esw
, MLX5_VPORT_UPLINK
, rep_type
);
2575 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev
);
2577 struct mlx5_eswitch_rep
*mlx5_eswitch_vport_rep(struct mlx5_eswitch
*esw
,
2580 return mlx5_eswitch_get_rep(esw
, vport
);
2582 EXPORT_SYMBOL(mlx5_eswitch_vport_rep
);
2584 bool mlx5_eswitch_is_vf_vport(const struct mlx5_eswitch
*esw
, u16 vport_num
)
2586 return vport_num
>= MLX5_VPORT_FIRST_VF
&&
2587 vport_num
<= esw
->dev
->priv
.sriov
.max_vfs
;
2590 bool mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch
*esw
)
2592 return !!(esw
->flags
& MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED
);
2594 EXPORT_SYMBOL(mlx5_eswitch_reg_c1_loopback_enabled
);
2596 bool mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch
*esw
)
2598 return !!(esw
->flags
& MLX5_ESWITCH_VPORT_MATCH_METADATA
);
2600 EXPORT_SYMBOL(mlx5_eswitch_vport_match_metadata_enabled
);
2602 u32
mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch
*esw
,
2605 struct mlx5_vport
*vport
= mlx5_eswitch_get_vport(esw
, vport_num
);
2607 if (WARN_ON_ONCE(IS_ERR(vport
)))
2610 return vport
->metadata
<< (32 - ESW_SOURCE_PORT_METADATA_BITS
);
2612 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_match
);