ca6ac3876a1fcf062e47238e19d6e8a3c4da590d
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch_offloads.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/etherdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include "mlx5_core.h"
39 #include "eswitch.h"
40 #include "eswitch_offloads_chains.h"
41 #include "rdma.h"
42 #include "en.h"
43 #include "fs_core.h"
44 #include "lib/devcom.h"
45 #include "lib/eq.h"
46
47 /* There are two match-all miss flows, one for unicast dst mac and
48  * one for multicast.
49  */
50 #define MLX5_ESW_MISS_FLOWS (2)
51 #define UPLINK_REP_INDEX 0
52
53 /* Per vport tables */
54
55 #define MLX5_ESW_VPORT_TABLE_SIZE 128
56
57 /* This struct is used as a key to the hash table and we need it to be packed
58  * so hash result is consistent
59  */
60 struct mlx5_vport_key {
61         u32 chain;
62         u16 prio;
63         u16 vport;
64         u16 vhca_id;
65 } __packed;
66
67 struct mlx5_vport_table {
68         struct hlist_node hlist;
69         struct mlx5_flow_table *fdb;
70         u32 num_rules;
71         struct mlx5_vport_key key;
72 };
73
74 #define MLX5_ESW_VPORT_TBL_NUM_GROUPS  4
75
76 static struct mlx5_flow_table *
77 esw_vport_tbl_create(struct mlx5_eswitch *esw, struct mlx5_flow_namespace *ns)
78 {
79         struct mlx5_flow_table_attr ft_attr = {};
80         struct mlx5_flow_table *fdb;
81
82         ft_attr.autogroup.max_num_groups = MLX5_ESW_VPORT_TBL_NUM_GROUPS;
83         ft_attr.max_fte = MLX5_ESW_VPORT_TABLE_SIZE;
84         ft_attr.prio = FDB_PER_VPORT;
85         fdb = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
86         if (IS_ERR(fdb)) {
87                 esw_warn(esw->dev, "Failed to create per vport FDB Table err %ld\n",
88                          PTR_ERR(fdb));
89         }
90
91         return fdb;
92 }
93
94 static u32 flow_attr_to_vport_key(struct mlx5_eswitch *esw,
95                                   struct mlx5_esw_flow_attr *attr,
96                                   struct mlx5_vport_key *key)
97 {
98         key->vport = attr->in_rep->vport;
99         key->chain = attr->chain;
100         key->prio = attr->prio;
101         key->vhca_id = MLX5_CAP_GEN(esw->dev, vhca_id);
102         return jhash(key, sizeof(*key), 0);
103 }
104
105 /* caller must hold vports.lock */
106 static struct mlx5_vport_table *
107 esw_vport_tbl_lookup(struct mlx5_eswitch *esw, struct mlx5_vport_key *skey, u32 key)
108 {
109         struct mlx5_vport_table *e;
110
111         hash_for_each_possible(esw->fdb_table.offloads.vports.table, e, hlist, key)
112                 if (!memcmp(&e->key, skey, sizeof(*skey)))
113                         return e;
114
115         return NULL;
116 }
117
118 static void
119 esw_vport_tbl_put(struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *attr)
120 {
121         struct mlx5_vport_table *e;
122         struct mlx5_vport_key key;
123         u32 hkey;
124
125         mutex_lock(&esw->fdb_table.offloads.vports.lock);
126         hkey = flow_attr_to_vport_key(esw, attr, &key);
127         e = esw_vport_tbl_lookup(esw, &key, hkey);
128         if (!e || --e->num_rules)
129                 goto out;
130
131         hash_del(&e->hlist);
132         mlx5_destroy_flow_table(e->fdb);
133         kfree(e);
134 out:
135         mutex_unlock(&esw->fdb_table.offloads.vports.lock);
136 }
137
138 static struct mlx5_flow_table *
139 esw_vport_tbl_get(struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *attr)
140 {
141         struct mlx5_core_dev *dev = esw->dev;
142         struct mlx5_flow_namespace *ns;
143         struct mlx5_flow_table *fdb;
144         struct mlx5_vport_table *e;
145         struct mlx5_vport_key skey;
146         u32 hkey;
147
148         mutex_lock(&esw->fdb_table.offloads.vports.lock);
149         hkey = flow_attr_to_vport_key(esw, attr, &skey);
150         e = esw_vport_tbl_lookup(esw, &skey, hkey);
151         if (e) {
152                 e->num_rules++;
153                 goto out;
154         }
155
156         e = kzalloc(sizeof(*e), GFP_KERNEL);
157         if (!e) {
158                 fdb = ERR_PTR(-ENOMEM);
159                 goto err_alloc;
160         }
161
162         ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
163         if (!ns) {
164                 esw_warn(dev, "Failed to get FDB namespace\n");
165                 fdb = ERR_PTR(-ENOENT);
166                 goto err_ns;
167         }
168
169         fdb = esw_vport_tbl_create(esw, ns);
170         if (IS_ERR(fdb))
171                 goto err_ns;
172
173         e->fdb = fdb;
174         e->num_rules = 1;
175         e->key = skey;
176         hash_add(esw->fdb_table.offloads.vports.table, &e->hlist, hkey);
177 out:
178         mutex_unlock(&esw->fdb_table.offloads.vports.lock);
179         return e->fdb;
180
181 err_ns:
182         kfree(e);
183 err_alloc:
184         mutex_unlock(&esw->fdb_table.offloads.vports.lock);
185         return fdb;
186 }
187
188 int mlx5_esw_vport_tbl_get(struct mlx5_eswitch *esw)
189 {
190         struct mlx5_esw_flow_attr attr = {};
191         struct mlx5_eswitch_rep rep = {};
192         struct mlx5_flow_table *fdb;
193         struct mlx5_vport *vport;
194         int i;
195
196         attr.prio = 1;
197         attr.in_rep = &rep;
198         mlx5_esw_for_all_vports(esw, i, vport) {
199                 attr.in_rep->vport = vport->vport;
200                 fdb = esw_vport_tbl_get(esw, &attr);
201                 if (IS_ERR(fdb))
202                         goto out;
203         }
204         return 0;
205
206 out:
207         mlx5_esw_vport_tbl_put(esw);
208         return PTR_ERR(fdb);
209 }
210
211 void mlx5_esw_vport_tbl_put(struct mlx5_eswitch *esw)
212 {
213         struct mlx5_esw_flow_attr attr = {};
214         struct mlx5_eswitch_rep rep = {};
215         struct mlx5_vport *vport;
216         int i;
217
218         attr.prio = 1;
219         attr.in_rep = &rep;
220         mlx5_esw_for_all_vports(esw, i, vport) {
221                 attr.in_rep->vport = vport->vport;
222                 esw_vport_tbl_put(esw, &attr);
223         }
224 }
225
226 /* End: Per vport tables */
227
228 static struct mlx5_eswitch_rep *mlx5_eswitch_get_rep(struct mlx5_eswitch *esw,
229                                                      u16 vport_num)
230 {
231         int idx = mlx5_eswitch_vport_num_to_index(esw, vport_num);
232
233         WARN_ON(idx > esw->total_vports - 1);
234         return &esw->offloads.vport_reps[idx];
235 }
236
237 static bool
238 esw_check_ingress_prio_tag_enabled(const struct mlx5_eswitch *esw,
239                                    const struct mlx5_vport *vport)
240 {
241         return (MLX5_CAP_GEN(esw->dev, prio_tag_required) &&
242                 mlx5_eswitch_is_vf_vport(esw, vport->vport));
243 }
244
245 static void
246 mlx5_eswitch_set_rule_source_port(struct mlx5_eswitch *esw,
247                                   struct mlx5_flow_spec *spec,
248                                   struct mlx5_esw_flow_attr *attr)
249 {
250         void *misc2;
251         void *misc;
252
253         /* Use metadata matching because vport is not represented by single
254          * VHCA in dual-port RoCE mode, and matching on source vport may fail.
255          */
256         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
257                 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
258                 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0,
259                          mlx5_eswitch_get_vport_metadata_for_match(attr->in_mdev->priv.eswitch,
260                                                                    attr->in_rep->vport));
261
262                 misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
263                 MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0,
264                          mlx5_eswitch_get_vport_metadata_mask());
265
266                 spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
267                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
268                 if (memchr_inv(misc, 0, MLX5_ST_SZ_BYTES(fte_match_set_misc)))
269                         spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
270         } else {
271                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
272                 MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);
273
274                 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
275                         MLX5_SET(fte_match_set_misc, misc,
276                                  source_eswitch_owner_vhca_id,
277                                  MLX5_CAP_GEN(attr->in_mdev, vhca_id));
278
279                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
280                 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
281                 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
282                         MLX5_SET_TO_ONES(fte_match_set_misc, misc,
283                                          source_eswitch_owner_vhca_id);
284
285                 spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
286         }
287
288         if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source) &&
289             attr->in_rep->vport == MLX5_VPORT_UPLINK)
290                 spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
291 }
292
293 struct mlx5_flow_handle *
294 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
295                                 struct mlx5_flow_spec *spec,
296                                 struct mlx5_esw_flow_attr *attr)
297 {
298         struct mlx5_flow_destination dest[MLX5_MAX_FLOW_FWD_VPORTS + 1] = {};
299         struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
300         bool split = !!(attr->split_count);
301         struct mlx5_flow_handle *rule;
302         struct mlx5_flow_table *fdb;
303         bool hairpin = false;
304         int j, i = 0;
305
306         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
307                 return ERR_PTR(-EOPNOTSUPP);
308
309         flow_act.action = attr->action;
310         /* if per flow vlan pop/push is emulated, don't set that into the firmware */
311         if (!mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
312                 flow_act.action &= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
313                                      MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
314         else if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
315                 flow_act.vlan[0].ethtype = ntohs(attr->vlan_proto[0]);
316                 flow_act.vlan[0].vid = attr->vlan_vid[0];
317                 flow_act.vlan[0].prio = attr->vlan_prio[0];
318                 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
319                         flow_act.vlan[1].ethtype = ntohs(attr->vlan_proto[1]);
320                         flow_act.vlan[1].vid = attr->vlan_vid[1];
321                         flow_act.vlan[1].prio = attr->vlan_prio[1];
322                 }
323         }
324
325         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
326                 struct mlx5_flow_table *ft;
327
328                 if (attr->dest_ft) {
329                         flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
330                         dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
331                         dest[i].ft = attr->dest_ft;
332                         i++;
333                 } else if (attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH) {
334                         flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
335                         dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
336                         dest[i].ft = mlx5_esw_chains_get_tc_end_ft(esw);
337                         i++;
338                 } else if (attr->dest_chain) {
339                         flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
340                         ft = mlx5_esw_chains_get_table(esw, attr->dest_chain,
341                                                        1, 0);
342                         if (IS_ERR(ft)) {
343                                 rule = ERR_CAST(ft);
344                                 goto err_create_goto_table;
345                         }
346
347                         dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
348                         dest[i].ft = ft;
349                         i++;
350                 } else {
351                         for (j = attr->split_count; j < attr->out_count; j++) {
352                                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
353                                 dest[i].vport.num = attr->dests[j].rep->vport;
354                                 dest[i].vport.vhca_id =
355                                         MLX5_CAP_GEN(attr->dests[j].mdev, vhca_id);
356                                 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
357                                         dest[i].vport.flags |=
358                                                 MLX5_FLOW_DEST_VPORT_VHCA_ID;
359                                 if (attr->dests[j].flags & MLX5_ESW_DEST_ENCAP) {
360                                         flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
361                                         flow_act.pkt_reformat = attr->dests[j].pkt_reformat;
362                                         dest[i].vport.flags |= MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
363                                         dest[i].vport.pkt_reformat =
364                                                 attr->dests[j].pkt_reformat;
365                                 }
366                                 i++;
367                         }
368                 }
369         }
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);
373                 i++;
374         }
375
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;
380
381         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
382                 flow_act.modify_hdr = attr->modify_hdr;
383
384         if (split) {
385                 fdb = esw_vport_tbl_get(esw, attr);
386         } else {
387                 if (attr->chain || attr->prio)
388                         fdb = mlx5_esw_chains_get_table(esw, attr->chain,
389                                                         attr->prio, 0);
390                 else
391                         fdb = attr->fdb;
392
393                 if (!(attr->flags & MLX5_ESW_ATTR_FLAG_NO_IN_PORT))
394                         mlx5_eswitch_set_rule_source_port(esw, spec, attr);
395         }
396         if (IS_ERR(fdb)) {
397                 rule = ERR_CAST(fdb);
398                 goto err_esw_get;
399         }
400
401         if (mlx5_eswitch_termtbl_required(esw, attr, &flow_act, spec)) {
402                 rule = mlx5_eswitch_add_termtbl_rule(esw, fdb, spec, attr,
403                                                      &flow_act, dest, i);
404                 hairpin = true;
405         } else {
406                 rule = mlx5_add_flow_rules(fdb, spec, &flow_act, dest, i);
407         }
408         if (IS_ERR(rule))
409                 goto err_add_rule;
410         else
411                 atomic64_inc(&esw->offloads.num_flows);
412
413         if (hairpin)
414                 attr->flags |= MLX5_ESW_ATTR_FLAG_HAIRPIN;
415
416         return rule;
417
418 err_add_rule:
419         if (split)
420                 esw_vport_tbl_put(esw, attr);
421         else if (attr->chain || attr->prio)
422                 mlx5_esw_chains_put_table(esw, attr->chain, attr->prio, 0);
423 err_esw_get:
424         if (!(attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH) && attr->dest_chain)
425                 mlx5_esw_chains_put_table(esw, attr->dest_chain, 1, 0);
426 err_create_goto_table:
427         return rule;
428 }
429
430 struct mlx5_flow_handle *
431 mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch *esw,
432                           struct mlx5_flow_spec *spec,
433                           struct mlx5_esw_flow_attr *attr)
434 {
435         struct mlx5_flow_destination dest[MLX5_MAX_FLOW_FWD_VPORTS + 1] = {};
436         struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
437         struct mlx5_flow_table *fast_fdb;
438         struct mlx5_flow_table *fwd_fdb;
439         struct mlx5_flow_handle *rule;
440         int i;
441
442         fast_fdb = mlx5_esw_chains_get_table(esw, attr->chain, attr->prio, 0);
443         if (IS_ERR(fast_fdb)) {
444                 rule = ERR_CAST(fast_fdb);
445                 goto err_get_fast;
446         }
447
448         fwd_fdb = esw_vport_tbl_get(esw, attr);
449         if (IS_ERR(fwd_fdb)) {
450                 rule = ERR_CAST(fwd_fdb);
451                 goto err_get_fwd;
452         }
453
454         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
455         for (i = 0; i < attr->split_count; i++) {
456                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
457                 dest[i].vport.num = attr->dests[i].rep->vport;
458                 dest[i].vport.vhca_id =
459                         MLX5_CAP_GEN(attr->dests[i].mdev, vhca_id);
460                 if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
461                         dest[i].vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
462                 if (attr->dests[i].flags & MLX5_ESW_DEST_ENCAP) {
463                         dest[i].vport.flags |= MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
464                         dest[i].vport.pkt_reformat = attr->dests[i].pkt_reformat;
465                 }
466         }
467         dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
468         dest[i].ft = fwd_fdb,
469         i++;
470
471         mlx5_eswitch_set_rule_source_port(esw, spec, attr);
472
473         if (attr->outer_match_level != MLX5_MATCH_NONE)
474                 spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
475
476         flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
477         rule = mlx5_add_flow_rules(fast_fdb, spec, &flow_act, dest, i);
478
479         if (IS_ERR(rule))
480                 goto add_err;
481
482         atomic64_inc(&esw->offloads.num_flows);
483
484         return rule;
485 add_err:
486         esw_vport_tbl_put(esw, attr);
487 err_get_fwd:
488         mlx5_esw_chains_put_table(esw, attr->chain, attr->prio, 0);
489 err_get_fast:
490         return rule;
491 }
492
493 static void
494 __mlx5_eswitch_del_rule(struct mlx5_eswitch *esw,
495                         struct mlx5_flow_handle *rule,
496                         struct mlx5_esw_flow_attr *attr,
497                         bool fwd_rule)
498 {
499         bool split = (attr->split_count > 0);
500         int i;
501
502         mlx5_del_flow_rules(rule);
503
504         if (attr->flags & MLX5_ESW_ATTR_FLAG_HAIRPIN) {
505                 /* unref the term table */
506                 for (i = 0; i < MLX5_MAX_FLOW_FWD_VPORTS; i++) {
507                         if (attr->dests[i].termtbl)
508                                 mlx5_eswitch_termtbl_put(esw, attr->dests[i].termtbl);
509                 }
510         }
511
512         atomic64_dec(&esw->offloads.num_flows);
513
514         if (fwd_rule)  {
515                 esw_vport_tbl_put(esw, attr);
516                 mlx5_esw_chains_put_table(esw, attr->chain, attr->prio, 0);
517         } else {
518                 if (split)
519                         esw_vport_tbl_put(esw, attr);
520                 else if (attr->chain || attr->prio)
521                         mlx5_esw_chains_put_table(esw, attr->chain, attr->prio,
522                                                   0);
523                 if (attr->dest_chain)
524                         mlx5_esw_chains_put_table(esw, attr->dest_chain, 1, 0);
525         }
526 }
527
528 void
529 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
530                                 struct mlx5_flow_handle *rule,
531                                 struct mlx5_esw_flow_attr *attr)
532 {
533         __mlx5_eswitch_del_rule(esw, rule, attr, false);
534 }
535
536 void
537 mlx5_eswitch_del_fwd_rule(struct mlx5_eswitch *esw,
538                           struct mlx5_flow_handle *rule,
539                           struct mlx5_esw_flow_attr *attr)
540 {
541         __mlx5_eswitch_del_rule(esw, rule, attr, true);
542 }
543
544 static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
545 {
546         struct mlx5_eswitch_rep *rep;
547         int i, err = 0;
548
549         esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
550         mlx5_esw_for_each_host_func_rep(esw, i, rep, esw->esw_funcs.num_vfs) {
551                 if (atomic_read(&rep->rep_data[REP_ETH].state) != REP_LOADED)
552                         continue;
553
554                 err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
555                 if (err)
556                         goto out;
557         }
558
559 out:
560         return err;
561 }
562
563 static struct mlx5_eswitch_rep *
564 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
565 {
566         struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;
567
568         in_rep  = attr->in_rep;
569         out_rep = attr->dests[0].rep;
570
571         if (push)
572                 vport = in_rep;
573         else if (pop)
574                 vport = out_rep;
575         else
576                 vport = in_rep;
577
578         return vport;
579 }
580
581 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
582                                      bool push, bool pop, bool fwd)
583 {
584         struct mlx5_eswitch_rep *in_rep, *out_rep;
585
586         if ((push || pop) && !fwd)
587                 goto out_notsupp;
588
589         in_rep  = attr->in_rep;
590         out_rep = attr->dests[0].rep;
591
592         if (push && in_rep->vport == MLX5_VPORT_UPLINK)
593                 goto out_notsupp;
594
595         if (pop && out_rep->vport == MLX5_VPORT_UPLINK)
596                 goto out_notsupp;
597
598         /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
599         if (!push && !pop && fwd)
600                 if (in_rep->vlan && out_rep->vport == MLX5_VPORT_UPLINK)
601                         goto out_notsupp;
602
603         /* protects against (1) setting rules with different vlans to push and
604          * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
605          */
606         if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan_vid[0]))
607                 goto out_notsupp;
608
609         return 0;
610
611 out_notsupp:
612         return -EOPNOTSUPP;
613 }
614
615 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
616                                  struct mlx5_esw_flow_attr *attr)
617 {
618         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
619         struct mlx5_eswitch_rep *vport = NULL;
620         bool push, pop, fwd;
621         int err = 0;
622
623         /* nop if we're on the vlan push/pop non emulation mode */
624         if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
625                 return 0;
626
627         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
628         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
629         fwd  = !!((attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
630                    !attr->dest_chain);
631
632         mutex_lock(&esw->state_lock);
633
634         err = esw_add_vlan_action_check(attr, push, pop, fwd);
635         if (err)
636                 goto unlock;
637
638         attr->flags &= ~MLX5_ESW_ATTR_FLAG_VLAN_HANDLED;
639
640         vport = esw_vlan_action_get_vport(attr, push, pop);
641
642         if (!push && !pop && fwd) {
643                 /* tracks VF --> wire rules without vlan push action */
644                 if (attr->dests[0].rep->vport == MLX5_VPORT_UPLINK) {
645                         vport->vlan_refcount++;
646                         attr->flags |= MLX5_ESW_ATTR_FLAG_VLAN_HANDLED;
647                 }
648
649                 goto unlock;
650         }
651
652         if (!push && !pop)
653                 goto unlock;
654
655         if (!(offloads->vlan_push_pop_refcount)) {
656                 /* it's the 1st vlan rule, apply global vlan pop policy */
657                 err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
658                 if (err)
659                         goto out;
660         }
661         offloads->vlan_push_pop_refcount++;
662
663         if (push) {
664                 if (vport->vlan_refcount)
665                         goto skip_set_push;
666
667                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, attr->vlan_vid[0], 0,
668                                                     SET_VLAN_INSERT | SET_VLAN_STRIP);
669                 if (err)
670                         goto out;
671                 vport->vlan = attr->vlan_vid[0];
672 skip_set_push:
673                 vport->vlan_refcount++;
674         }
675 out:
676         if (!err)
677                 attr->flags |= MLX5_ESW_ATTR_FLAG_VLAN_HANDLED;
678 unlock:
679         mutex_unlock(&esw->state_lock);
680         return err;
681 }
682
683 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
684                                  struct mlx5_esw_flow_attr *attr)
685 {
686         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
687         struct mlx5_eswitch_rep *vport = NULL;
688         bool push, pop, fwd;
689         int err = 0;
690
691         /* nop if we're on the vlan push/pop non emulation mode */
692         if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
693                 return 0;
694
695         if (!(attr->flags & MLX5_ESW_ATTR_FLAG_VLAN_HANDLED))
696                 return 0;
697
698         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
699         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
700         fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
701
702         mutex_lock(&esw->state_lock);
703
704         vport = esw_vlan_action_get_vport(attr, push, pop);
705
706         if (!push && !pop && fwd) {
707                 /* tracks VF --> wire rules without vlan push action */
708                 if (attr->dests[0].rep->vport == MLX5_VPORT_UPLINK)
709                         vport->vlan_refcount--;
710
711                 goto out;
712         }
713
714         if (push) {
715                 vport->vlan_refcount--;
716                 if (vport->vlan_refcount)
717                         goto skip_unset_push;
718
719                 vport->vlan = 0;
720                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
721                                                     0, 0, SET_VLAN_STRIP);
722                 if (err)
723                         goto out;
724         }
725
726 skip_unset_push:
727         offloads->vlan_push_pop_refcount--;
728         if (offloads->vlan_push_pop_refcount)
729                 goto out;
730
731         /* no more vlan rules, stop global vlan pop policy */
732         err = esw_set_global_vlan_pop(esw, 0);
733
734 out:
735         mutex_unlock(&esw->state_lock);
736         return err;
737 }
738
739 struct mlx5_flow_handle *
740 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, u16 vport,
741                                     u32 sqn)
742 {
743         struct mlx5_flow_act flow_act = {0};
744         struct mlx5_flow_destination dest = {};
745         struct mlx5_flow_handle *flow_rule;
746         struct mlx5_flow_spec *spec;
747         void *misc;
748
749         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
750         if (!spec) {
751                 flow_rule = ERR_PTR(-ENOMEM);
752                 goto out;
753         }
754
755         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
756         MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
757         /* source vport is the esw manager */
758         MLX5_SET(fte_match_set_misc, misc, source_port, esw->manager_vport);
759
760         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
761         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
762         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
763
764         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
765         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
766         dest.vport.num = vport;
767         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
768
769         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
770                                         spec, &flow_act, &dest, 1);
771         if (IS_ERR(flow_rule))
772                 esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
773 out:
774         kvfree(spec);
775         return flow_rule;
776 }
777 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
778
779 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
780 {
781         mlx5_del_flow_rules(rule);
782 }
783
784 static bool mlx5_eswitch_reg_c1_loopback_supported(struct mlx5_eswitch *esw)
785 {
786         return MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
787                MLX5_FDB_TO_VPORT_REG_C_1;
788 }
789
790 static int esw_set_passing_vport_metadata(struct mlx5_eswitch *esw, bool enable)
791 {
792         u32 out[MLX5_ST_SZ_DW(query_esw_vport_context_out)] = {};
793         u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
794         u8 curr, wanted;
795         int err;
796
797         if (!mlx5_eswitch_reg_c1_loopback_supported(esw) &&
798             !mlx5_eswitch_vport_match_metadata_enabled(esw))
799                 return 0;
800
801         err = mlx5_eswitch_query_esw_vport_context(esw->dev, 0, false,
802                                                    out, sizeof(out));
803         if (err)
804                 return err;
805
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;
811
812         if (enable)
813                 curr |= wanted;
814         else
815                 curr &= ~wanted;
816
817         MLX5_SET(modify_esw_vport_context_in, in,
818                  esw_vport_context.fdb_to_vport_reg_c_id, curr);
819
820         MLX5_SET(modify_esw_vport_context_in, in,
821                  field_select.fdb_to_vport_reg_c_id, 1);
822
823         err = mlx5_eswitch_modify_esw_vport_context(esw->dev, 0, false, in,
824                                                     sizeof(in));
825         if (!err) {
826                 if (enable && (curr & MLX5_FDB_TO_VPORT_REG_C_1))
827                         esw->flags |= MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
828                 else
829                         esw->flags &= ~MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
830         }
831
832         return err;
833 }
834
835 static void peer_miss_rules_setup(struct mlx5_eswitch *esw,
836                                   struct mlx5_core_dev *peer_dev,
837                                   struct mlx5_flow_spec *spec,
838                                   struct mlx5_flow_destination *dest)
839 {
840         void *misc;
841
842         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
843                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
844                                     misc_parameters_2);
845                 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
846                          mlx5_eswitch_get_vport_metadata_mask());
847
848                 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
849         } else {
850                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
851                                     misc_parameters);
852
853                 MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
854                          MLX5_CAP_GEN(peer_dev, vhca_id));
855
856                 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
857
858                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
859                                     misc_parameters);
860                 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
861                 MLX5_SET_TO_ONES(fte_match_set_misc, misc,
862                                  source_eswitch_owner_vhca_id);
863         }
864
865         dest->type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
866         dest->vport.num = peer_dev->priv.eswitch->manager_vport;
867         dest->vport.vhca_id = MLX5_CAP_GEN(peer_dev, vhca_id);
868         dest->vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
869 }
870
871 static void esw_set_peer_miss_rule_source_port(struct mlx5_eswitch *esw,
872                                                struct mlx5_eswitch *peer_esw,
873                                                struct mlx5_flow_spec *spec,
874                                                u16 vport)
875 {
876         void *misc;
877
878         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
879                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
880                                     misc_parameters_2);
881                 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
882                          mlx5_eswitch_get_vport_metadata_for_match(peer_esw,
883                                                                    vport));
884         } else {
885                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
886                                     misc_parameters);
887                 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
888         }
889 }
890
891 static int esw_add_fdb_peer_miss_rules(struct mlx5_eswitch *esw,
892                                        struct mlx5_core_dev *peer_dev)
893 {
894         struct mlx5_flow_destination dest = {};
895         struct mlx5_flow_act flow_act = {0};
896         struct mlx5_flow_handle **flows;
897         struct mlx5_flow_handle *flow;
898         struct mlx5_flow_spec *spec;
899         /* total vports is the same for both e-switches */
900         int nvports = esw->total_vports;
901         void *misc;
902         int err, i;
903
904         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
905         if (!spec)
906                 return -ENOMEM;
907
908         peer_miss_rules_setup(esw, peer_dev, spec, &dest);
909
910         flows = kvzalloc(nvports * sizeof(*flows), GFP_KERNEL);
911         if (!flows) {
912                 err = -ENOMEM;
913                 goto alloc_flows_err;
914         }
915
916         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
917         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
918                             misc_parameters);
919
920         if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
921                 esw_set_peer_miss_rule_source_port(esw, peer_dev->priv.eswitch,
922                                                    spec, MLX5_VPORT_PF);
923
924                 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
925                                            spec, &flow_act, &dest, 1);
926                 if (IS_ERR(flow)) {
927                         err = PTR_ERR(flow);
928                         goto add_pf_flow_err;
929                 }
930                 flows[MLX5_VPORT_PF] = flow;
931         }
932
933         if (mlx5_ecpf_vport_exists(esw->dev)) {
934                 MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_ECPF);
935                 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
936                                            spec, &flow_act, &dest, 1);
937                 if (IS_ERR(flow)) {
938                         err = PTR_ERR(flow);
939                         goto add_ecpf_flow_err;
940                 }
941                 flows[mlx5_eswitch_ecpf_idx(esw)] = flow;
942         }
943
944         mlx5_esw_for_each_vf_vport_num(esw, i, mlx5_core_max_vfs(esw->dev)) {
945                 esw_set_peer_miss_rule_source_port(esw,
946                                                    peer_dev->priv.eswitch,
947                                                    spec, i);
948
949                 flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
950                                            spec, &flow_act, &dest, 1);
951                 if (IS_ERR(flow)) {
952                         err = PTR_ERR(flow);
953                         goto add_vf_flow_err;
954                 }
955                 flows[i] = flow;
956         }
957
958         esw->fdb_table.offloads.peer_miss_rules = flows;
959
960         kvfree(spec);
961         return 0;
962
963 add_vf_flow_err:
964         nvports = --i;
965         mlx5_esw_for_each_vf_vport_num_reverse(esw, i, nvports)
966                 mlx5_del_flow_rules(flows[i]);
967
968         if (mlx5_ecpf_vport_exists(esw->dev))
969                 mlx5_del_flow_rules(flows[mlx5_eswitch_ecpf_idx(esw)]);
970 add_ecpf_flow_err:
971         if (mlx5_core_is_ecpf_esw_manager(esw->dev))
972                 mlx5_del_flow_rules(flows[MLX5_VPORT_PF]);
973 add_pf_flow_err:
974         esw_warn(esw->dev, "FDB: Failed to add peer miss flow rule err %d\n", err);
975         kvfree(flows);
976 alloc_flows_err:
977         kvfree(spec);
978         return err;
979 }
980
981 static void esw_del_fdb_peer_miss_rules(struct mlx5_eswitch *esw)
982 {
983         struct mlx5_flow_handle **flows;
984         int i;
985
986         flows = esw->fdb_table.offloads.peer_miss_rules;
987
988         mlx5_esw_for_each_vf_vport_num_reverse(esw, i,
989                                                mlx5_core_max_vfs(esw->dev))
990                 mlx5_del_flow_rules(flows[i]);
991
992         if (mlx5_ecpf_vport_exists(esw->dev))
993                 mlx5_del_flow_rules(flows[mlx5_eswitch_ecpf_idx(esw)]);
994
995         if (mlx5_core_is_ecpf_esw_manager(esw->dev))
996                 mlx5_del_flow_rules(flows[MLX5_VPORT_PF]);
997
998         kvfree(flows);
999 }
1000
1001 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
1002 {
1003         struct mlx5_flow_act flow_act = {0};
1004         struct mlx5_flow_destination dest = {};
1005         struct mlx5_flow_handle *flow_rule = NULL;
1006         struct mlx5_flow_spec *spec;
1007         void *headers_c;
1008         void *headers_v;
1009         int err = 0;
1010         u8 *dmac_c;
1011         u8 *dmac_v;
1012
1013         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1014         if (!spec) {
1015                 err = -ENOMEM;
1016                 goto out;
1017         }
1018
1019         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1020         headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1021                                  outer_headers);
1022         dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
1023                               outer_headers.dmac_47_16);
1024         dmac_c[0] = 0x01;
1025
1026         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1027         dest.vport.num = esw->manager_vport;
1028         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1029
1030         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1031                                         spec, &flow_act, &dest, 1);
1032         if (IS_ERR(flow_rule)) {
1033                 err = PTR_ERR(flow_rule);
1034                 esw_warn(esw->dev,  "FDB: Failed to add unicast miss flow rule err %d\n", err);
1035                 goto out;
1036         }
1037
1038         esw->fdb_table.offloads.miss_rule_uni = flow_rule;
1039
1040         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1041                                  outer_headers);
1042         dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
1043                               outer_headers.dmac_47_16);
1044         dmac_v[0] = 0x01;
1045         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1046                                         spec, &flow_act, &dest, 1);
1047         if (IS_ERR(flow_rule)) {
1048                 err = PTR_ERR(flow_rule);
1049                 esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
1050                 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1051                 goto out;
1052         }
1053
1054         esw->fdb_table.offloads.miss_rule_multi = flow_rule;
1055
1056 out:
1057         kvfree(spec);
1058         return err;
1059 }
1060
1061 struct mlx5_flow_handle *
1062 esw_add_restore_rule(struct mlx5_eswitch *esw, u32 tag)
1063 {
1064         struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
1065         struct mlx5_flow_table *ft = esw->offloads.ft_offloads_restore;
1066         struct mlx5_flow_context *flow_context;
1067         struct mlx5_flow_handle *flow_rule;
1068         struct mlx5_flow_destination dest;
1069         struct mlx5_flow_spec *spec;
1070         void *misc;
1071
1072         if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
1073                 return ERR_PTR(-EOPNOTSUPP);
1074
1075         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1076         if (!spec)
1077                 return ERR_PTR(-ENOMEM);
1078
1079         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1080                             misc_parameters_2);
1081         MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1082                  ESW_CHAIN_TAG_METADATA_MASK);
1083         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1084                             misc_parameters_2);
1085         MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0, tag);
1086         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1087         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1088                           MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1089         flow_act.modify_hdr = esw->offloads.restore_copy_hdr_id;
1090
1091         flow_context = &spec->flow_context;
1092         flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
1093         flow_context->flow_tag = tag;
1094         dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1095         dest.ft = esw->offloads.ft_offloads;
1096
1097         flow_rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1);
1098         kfree(spec);
1099
1100         if (IS_ERR(flow_rule))
1101                 esw_warn(esw->dev,
1102                          "Failed to create restore rule for tag: %d, err(%d)\n",
1103                          tag, (int)PTR_ERR(flow_rule));
1104
1105         return flow_rule;
1106 }
1107
1108 u32
1109 esw_get_max_restore_tag(struct mlx5_eswitch *esw)
1110 {
1111         return ESW_CHAIN_TAG_METADATA_MASK;
1112 }
1113
1114 #define MAX_PF_SQ 256
1115 #define MAX_SQ_NVPORTS 32
1116
1117 static void esw_set_flow_group_source_port(struct mlx5_eswitch *esw,
1118                                            u32 *flow_group_in)
1119 {
1120         void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1121                                             flow_group_in,
1122                                             match_criteria);
1123
1124         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1125                 MLX5_SET(create_flow_group_in, flow_group_in,
1126                          match_criteria_enable,
1127                          MLX5_MATCH_MISC_PARAMETERS_2);
1128
1129                 MLX5_SET(fte_match_param, match_criteria,
1130                          misc_parameters_2.metadata_reg_c_0,
1131                          mlx5_eswitch_get_vport_metadata_mask());
1132         } else {
1133                 MLX5_SET(create_flow_group_in, flow_group_in,
1134                          match_criteria_enable,
1135                          MLX5_MATCH_MISC_PARAMETERS);
1136
1137                 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1138                                  misc_parameters.source_port);
1139         }
1140 }
1141
1142 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw, int nvports)
1143 {
1144         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1145         struct mlx5_flow_table_attr ft_attr = {};
1146         struct mlx5_core_dev *dev = esw->dev;
1147         struct mlx5_flow_namespace *root_ns;
1148         struct mlx5_flow_table *fdb = NULL;
1149         u32 flags = 0, *flow_group_in;
1150         int table_size, ix, err = 0;
1151         struct mlx5_flow_group *g;
1152         void *match_criteria;
1153         u8 *dmac;
1154
1155         esw_debug(esw->dev, "Create offloads FDB Tables\n");
1156
1157         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1158         if (!flow_group_in)
1159                 return -ENOMEM;
1160
1161         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
1162         if (!root_ns) {
1163                 esw_warn(dev, "Failed to get FDB flow namespace\n");
1164                 err = -EOPNOTSUPP;
1165                 goto ns_err;
1166         }
1167         esw->fdb_table.offloads.ns = root_ns;
1168         err = mlx5_flow_namespace_set_mode(root_ns,
1169                                            esw->dev->priv.steering->mode);
1170         if (err) {
1171                 esw_warn(dev, "Failed to set FDB namespace steering mode\n");
1172                 goto ns_err;
1173         }
1174
1175         table_size = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ +
1176                 MLX5_ESW_MISS_FLOWS + esw->total_vports;
1177
1178         /* create the slow path fdb with encap set, so further table instances
1179          * can be created at run time while VFs are probed if the FW allows that.
1180          */
1181         if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
1182                 flags |= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
1183                           MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
1184
1185         ft_attr.flags = flags;
1186         ft_attr.max_fte = table_size;
1187         ft_attr.prio = FDB_SLOW_PATH;
1188
1189         fdb = mlx5_create_flow_table(root_ns, &ft_attr);
1190         if (IS_ERR(fdb)) {
1191                 err = PTR_ERR(fdb);
1192                 esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
1193                 goto slow_fdb_err;
1194         }
1195         esw->fdb_table.offloads.slow_fdb = fdb;
1196
1197         err = mlx5_esw_chains_create(esw);
1198         if (err) {
1199                 esw_warn(dev, "Failed to create fdb chains err(%d)\n", err);
1200                 goto fdb_chains_err;
1201         }
1202
1203         /* create send-to-vport group */
1204         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1205                  MLX5_MATCH_MISC_PARAMETERS);
1206
1207         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1208
1209         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
1210         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
1211
1212         ix = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ;
1213         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1214         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix - 1);
1215
1216         g = mlx5_create_flow_group(fdb, flow_group_in);
1217         if (IS_ERR(g)) {
1218                 err = PTR_ERR(g);
1219                 esw_warn(dev, "Failed to create send-to-vport flow group err(%d)\n", err);
1220                 goto send_vport_err;
1221         }
1222         esw->fdb_table.offloads.send_to_vport_grp = g;
1223
1224         /* create peer esw miss group */
1225         memset(flow_group_in, 0, inlen);
1226
1227         esw_set_flow_group_source_port(esw, flow_group_in);
1228
1229         if (!mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1230                 match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1231                                               flow_group_in,
1232                                               match_criteria);
1233
1234                 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1235                                  misc_parameters.source_eswitch_owner_vhca_id);
1236
1237                 MLX5_SET(create_flow_group_in, flow_group_in,
1238                          source_eswitch_owner_vhca_id_valid, 1);
1239         }
1240
1241         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
1242         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1243                  ix + esw->total_vports - 1);
1244         ix += esw->total_vports;
1245
1246         g = mlx5_create_flow_group(fdb, flow_group_in);
1247         if (IS_ERR(g)) {
1248                 err = PTR_ERR(g);
1249                 esw_warn(dev, "Failed to create peer miss flow group err(%d)\n", err);
1250                 goto peer_miss_err;
1251         }
1252         esw->fdb_table.offloads.peer_miss_grp = g;
1253
1254         /* create miss group */
1255         memset(flow_group_in, 0, inlen);
1256         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1257                  MLX5_MATCH_OUTER_HEADERS);
1258         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
1259                                       match_criteria);
1260         dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
1261                             outer_headers.dmac_47_16);
1262         dmac[0] = 0x01;
1263
1264         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
1265         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1266                  ix + MLX5_ESW_MISS_FLOWS);
1267
1268         g = mlx5_create_flow_group(fdb, flow_group_in);
1269         if (IS_ERR(g)) {
1270                 err = PTR_ERR(g);
1271                 esw_warn(dev, "Failed to create miss flow group err(%d)\n", err);
1272                 goto miss_err;
1273         }
1274         esw->fdb_table.offloads.miss_grp = g;
1275
1276         err = esw_add_fdb_miss_rule(esw);
1277         if (err)
1278                 goto miss_rule_err;
1279
1280         esw->nvports = nvports;
1281         kvfree(flow_group_in);
1282         return 0;
1283
1284 miss_rule_err:
1285         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1286 miss_err:
1287         mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1288 peer_miss_err:
1289         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1290 send_vport_err:
1291         mlx5_esw_chains_destroy(esw);
1292 fdb_chains_err:
1293         mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
1294 slow_fdb_err:
1295         /* Holds true only as long as DMFS is the default */
1296         mlx5_flow_namespace_set_mode(root_ns, MLX5_FLOW_STEERING_MODE_DMFS);
1297 ns_err:
1298         kvfree(flow_group_in);
1299         return err;
1300 }
1301
1302 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
1303 {
1304         if (!esw->fdb_table.offloads.slow_fdb)
1305                 return;
1306
1307         esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
1308         mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
1309         mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1310         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1311         mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1312         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1313
1314         mlx5_esw_chains_destroy(esw);
1315         mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
1316         /* Holds true only as long as DMFS is the default */
1317         mlx5_flow_namespace_set_mode(esw->fdb_table.offloads.ns,
1318                                      MLX5_FLOW_STEERING_MODE_DMFS);
1319 }
1320
1321 static int esw_create_offloads_table(struct mlx5_eswitch *esw, int nvports)
1322 {
1323         struct mlx5_flow_table_attr ft_attr = {};
1324         struct mlx5_core_dev *dev = esw->dev;
1325         struct mlx5_flow_table *ft_offloads;
1326         struct mlx5_flow_namespace *ns;
1327         int err = 0;
1328
1329         ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
1330         if (!ns) {
1331                 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
1332                 return -EOPNOTSUPP;
1333         }
1334
1335         ft_attr.max_fte = nvports + MLX5_ESW_MISS_FLOWS;
1336         ft_attr.prio = 1;
1337
1338         ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
1339         if (IS_ERR(ft_offloads)) {
1340                 err = PTR_ERR(ft_offloads);
1341                 esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
1342                 return err;
1343         }
1344
1345         esw->offloads.ft_offloads = ft_offloads;
1346         return 0;
1347 }
1348
1349 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
1350 {
1351         struct mlx5_esw_offload *offloads = &esw->offloads;
1352
1353         mlx5_destroy_flow_table(offloads->ft_offloads);
1354 }
1355
1356 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw, int nvports)
1357 {
1358         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1359         struct mlx5_flow_group *g;
1360         u32 *flow_group_in;
1361         int err = 0;
1362
1363         nvports = nvports + MLX5_ESW_MISS_FLOWS;
1364         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1365         if (!flow_group_in)
1366                 return -ENOMEM;
1367
1368         /* create vport rx group */
1369         esw_set_flow_group_source_port(esw, flow_group_in);
1370
1371         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1372         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
1373
1374         g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
1375
1376         if (IS_ERR(g)) {
1377                 err = PTR_ERR(g);
1378                 mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
1379                 goto out;
1380         }
1381
1382         esw->offloads.vport_rx_group = g;
1383 out:
1384         kvfree(flow_group_in);
1385         return err;
1386 }
1387
1388 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
1389 {
1390         mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
1391 }
1392
1393 struct mlx5_flow_handle *
1394 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, u16 vport,
1395                                   struct mlx5_flow_destination *dest)
1396 {
1397         struct mlx5_flow_act flow_act = {0};
1398         struct mlx5_flow_handle *flow_rule;
1399         struct mlx5_flow_spec *spec;
1400         void *misc;
1401
1402         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1403         if (!spec) {
1404                 flow_rule = ERR_PTR(-ENOMEM);
1405                 goto out;
1406         }
1407
1408         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1409                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
1410                 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1411                          mlx5_eswitch_get_vport_metadata_for_match(esw, vport));
1412
1413                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
1414                 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1415                          mlx5_eswitch_get_vport_metadata_mask());
1416
1417                 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1418         } else {
1419                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
1420                 MLX5_SET(fte_match_set_misc, misc, source_port, vport);
1421
1422                 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
1423                 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
1424
1425                 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
1426         }
1427
1428         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1429         flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
1430                                         &flow_act, dest, 1);
1431         if (IS_ERR(flow_rule)) {
1432                 esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
1433                 goto out;
1434         }
1435
1436 out:
1437         kvfree(spec);
1438         return flow_rule;
1439 }
1440
1441
1442 static int mlx5_eswitch_inline_mode_get(const struct mlx5_eswitch *esw, u8 *mode)
1443 {
1444         u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
1445         struct mlx5_core_dev *dev = esw->dev;
1446         int vport;
1447
1448         if (!MLX5_CAP_GEN(dev, vport_group_manager))
1449                 return -EOPNOTSUPP;
1450
1451         if (esw->mode == MLX5_ESWITCH_NONE)
1452                 return -EOPNOTSUPP;
1453
1454         switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
1455         case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1456                 mlx5_mode = MLX5_INLINE_MODE_NONE;
1457                 goto out;
1458         case MLX5_CAP_INLINE_MODE_L2:
1459                 mlx5_mode = MLX5_INLINE_MODE_L2;
1460                 goto out;
1461         case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1462                 goto query_vports;
1463         }
1464
1465 query_vports:
1466         mlx5_query_nic_vport_min_inline(dev, esw->first_host_vport, &prev_mlx5_mode);
1467         mlx5_esw_for_each_host_func_vport(esw, vport, esw->esw_funcs.num_vfs) {
1468                 mlx5_query_nic_vport_min_inline(dev, vport, &mlx5_mode);
1469                 if (prev_mlx5_mode != mlx5_mode)
1470                         return -EINVAL;
1471                 prev_mlx5_mode = mlx5_mode;
1472         }
1473
1474 out:
1475         *mode = mlx5_mode;
1476         return 0;
1477 }       
1478
1479 static void esw_destroy_restore_table(struct mlx5_eswitch *esw)
1480 {
1481         struct mlx5_esw_offload *offloads = &esw->offloads;
1482
1483         if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
1484                 return;
1485
1486         mlx5_modify_header_dealloc(esw->dev, offloads->restore_copy_hdr_id);
1487         mlx5_destroy_flow_group(offloads->restore_group);
1488         mlx5_destroy_flow_table(offloads->ft_offloads_restore);
1489 }
1490
1491 static int esw_create_restore_table(struct mlx5_eswitch *esw)
1492 {
1493         u8 modact[MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)] = {};
1494         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1495         struct mlx5_flow_table_attr ft_attr = {};
1496         struct mlx5_core_dev *dev = esw->dev;
1497         struct mlx5_flow_namespace *ns;
1498         struct mlx5_modify_hdr *mod_hdr;
1499         void *match_criteria, *misc;
1500         struct mlx5_flow_table *ft;
1501         struct mlx5_flow_group *g;
1502         u32 *flow_group_in;
1503         int err = 0;
1504
1505         if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
1506                 return 0;
1507
1508         ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
1509         if (!ns) {
1510                 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
1511                 return -EOPNOTSUPP;
1512         }
1513
1514         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1515         if (!flow_group_in) {
1516                 err = -ENOMEM;
1517                 goto out_free;
1518         }
1519
1520         ft_attr.max_fte = 1 << ESW_CHAIN_TAG_METADATA_BITS;
1521         ft = mlx5_create_flow_table(ns, &ft_attr);
1522         if (IS_ERR(ft)) {
1523                 err = PTR_ERR(ft);
1524                 esw_warn(esw->dev, "Failed to create restore table, err %d\n",
1525                          err);
1526                 goto out_free;
1527         }
1528
1529         memset(flow_group_in, 0, inlen);
1530         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
1531                                       match_criteria);
1532         misc = MLX5_ADDR_OF(fte_match_param, match_criteria,
1533                             misc_parameters_2);
1534
1535         MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1536                  ESW_CHAIN_TAG_METADATA_MASK);
1537         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1538         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1539                  ft_attr.max_fte - 1);
1540         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1541                  MLX5_MATCH_MISC_PARAMETERS_2);
1542         g = mlx5_create_flow_group(ft, flow_group_in);
1543         if (IS_ERR(g)) {
1544                 err = PTR_ERR(g);
1545                 esw_warn(dev, "Failed to create restore flow group, err: %d\n",
1546                          err);
1547                 goto err_group;
1548         }
1549
1550         MLX5_SET(copy_action_in, modact, action_type, MLX5_ACTION_TYPE_COPY);
1551         MLX5_SET(copy_action_in, modact, src_field,
1552                  MLX5_ACTION_IN_FIELD_METADATA_REG_C_1);
1553         MLX5_SET(copy_action_in, modact, dst_field,
1554                  MLX5_ACTION_IN_FIELD_METADATA_REG_B);
1555         mod_hdr = mlx5_modify_header_alloc(esw->dev,
1556                                            MLX5_FLOW_NAMESPACE_KERNEL, 1,
1557                                            modact);
1558         if (IS_ERR(mod_hdr)) {
1559                 esw_warn(dev, "Failed to create restore mod header, err: %d\n",
1560                          err);
1561                 err = PTR_ERR(mod_hdr);
1562                 goto err_mod_hdr;
1563         }
1564
1565         esw->offloads.ft_offloads_restore = ft;
1566         esw->offloads.restore_group = g;
1567         esw->offloads.restore_copy_hdr_id = mod_hdr;
1568
1569         return 0;
1570
1571 err_mod_hdr:
1572         mlx5_destroy_flow_group(g);
1573 err_group:
1574         mlx5_destroy_flow_table(ft);
1575 out_free:
1576         kvfree(flow_group_in);
1577
1578         return err;
1579 }
1580
1581 static int esw_offloads_start(struct mlx5_eswitch *esw,
1582                               struct netlink_ext_ack *extack)
1583 {
1584         int err, err1;
1585
1586         if (esw->mode != MLX5_ESWITCH_LEGACY &&
1587             !mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1588                 NL_SET_ERR_MSG_MOD(extack,
1589                                    "Can't set offloads mode, SRIOV legacy not enabled");
1590                 return -EINVAL;
1591         }
1592
1593         mlx5_eswitch_disable(esw, false);
1594         mlx5_eswitch_update_num_of_vfs(esw, esw->dev->priv.sriov.num_vfs);
1595         err = mlx5_eswitch_enable(esw, MLX5_ESWITCH_OFFLOADS);
1596         if (err) {
1597                 NL_SET_ERR_MSG_MOD(extack,
1598                                    "Failed setting eswitch to offloads");
1599                 err1 = mlx5_eswitch_enable(esw, MLX5_ESWITCH_LEGACY);
1600                 if (err1) {
1601                         NL_SET_ERR_MSG_MOD(extack,
1602                                            "Failed setting eswitch back to legacy");
1603                 }
1604         }
1605         if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
1606                 if (mlx5_eswitch_inline_mode_get(esw,
1607                                                  &esw->offloads.inline_mode)) {
1608                         esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
1609                         NL_SET_ERR_MSG_MOD(extack,
1610                                            "Inline mode is different between vports");
1611                 }
1612         }
1613         return err;
1614 }
1615
1616 void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
1617 {
1618         kfree(esw->offloads.vport_reps);
1619 }
1620
1621 int esw_offloads_init_reps(struct mlx5_eswitch *esw)
1622 {
1623         int total_vports = esw->total_vports;
1624         struct mlx5_eswitch_rep *rep;
1625         int vport_index;
1626         u8 rep_type;
1627
1628         esw->offloads.vport_reps = kcalloc(total_vports,
1629                                            sizeof(struct mlx5_eswitch_rep),
1630                                            GFP_KERNEL);
1631         if (!esw->offloads.vport_reps)
1632                 return -ENOMEM;
1633
1634         mlx5_esw_for_all_reps(esw, vport_index, rep) {
1635                 rep->vport = mlx5_eswitch_index_to_vport_num(esw, vport_index);
1636                 rep->vport_index = vport_index;
1637
1638                 for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
1639                         atomic_set(&rep->rep_data[rep_type].state,
1640                                    REP_UNREGISTERED);
1641         }
1642
1643         return 0;
1644 }
1645
1646 static void __esw_offloads_unload_rep(struct mlx5_eswitch *esw,
1647                                       struct mlx5_eswitch_rep *rep, u8 rep_type)
1648 {
1649         if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
1650                            REP_LOADED, REP_REGISTERED) == REP_LOADED)
1651                 esw->offloads.rep_ops[rep_type]->unload(rep);
1652 }
1653
1654 static void __unload_reps_all_vport(struct mlx5_eswitch *esw, u8 rep_type)
1655 {
1656         struct mlx5_eswitch_rep *rep;
1657         int i;
1658
1659         mlx5_esw_for_each_vf_rep_reverse(esw, i, rep, esw->esw_funcs.num_vfs)
1660                 __esw_offloads_unload_rep(esw, rep, rep_type);
1661
1662         if (mlx5_ecpf_vport_exists(esw->dev)) {
1663                 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_ECPF);
1664                 __esw_offloads_unload_rep(esw, rep, rep_type);
1665         }
1666
1667         if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1668                 rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_PF);
1669                 __esw_offloads_unload_rep(esw, rep, rep_type);
1670         }
1671
1672         rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
1673         __esw_offloads_unload_rep(esw, rep, rep_type);
1674 }
1675
1676 int esw_offloads_load_rep(struct mlx5_eswitch *esw, u16 vport_num)
1677 {
1678         struct mlx5_eswitch_rep *rep;
1679         int rep_type;
1680         int err;
1681
1682         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
1683                 return 0;
1684
1685         rep = mlx5_eswitch_get_rep(esw, vport_num);
1686         for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
1687                 if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
1688                                    REP_REGISTERED, REP_LOADED) == REP_REGISTERED) {
1689                         err = esw->offloads.rep_ops[rep_type]->load(esw->dev, rep);
1690                         if (err)
1691                                 goto err_reps;
1692                 }
1693
1694         return 0;
1695
1696 err_reps:
1697         atomic_set(&rep->rep_data[rep_type].state, REP_REGISTERED);
1698         for (--rep_type; rep_type >= 0; rep_type--)
1699                 __esw_offloads_unload_rep(esw, rep, rep_type);
1700         return err;
1701 }
1702
1703 void esw_offloads_unload_rep(struct mlx5_eswitch *esw, u16 vport_num)
1704 {
1705         struct mlx5_eswitch_rep *rep;
1706         int rep_type;
1707
1708         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
1709                 return;
1710
1711         rep = mlx5_eswitch_get_rep(esw, vport_num);
1712         for (rep_type = NUM_REP_TYPES - 1; rep_type >= 0; rep_type--)
1713                 __esw_offloads_unload_rep(esw, rep, rep_type);
1714 }
1715
1716 #define ESW_OFFLOADS_DEVCOM_PAIR        (0)
1717 #define ESW_OFFLOADS_DEVCOM_UNPAIR      (1)
1718
1719 static int mlx5_esw_offloads_pair(struct mlx5_eswitch *esw,
1720                                   struct mlx5_eswitch *peer_esw)
1721 {
1722         int err;
1723
1724         err = esw_add_fdb_peer_miss_rules(esw, peer_esw->dev);
1725         if (err)
1726                 return err;
1727
1728         return 0;
1729 }
1730
1731 static void mlx5_esw_offloads_unpair(struct mlx5_eswitch *esw)
1732 {
1733         mlx5e_tc_clean_fdb_peer_flows(esw);
1734         esw_del_fdb_peer_miss_rules(esw);
1735 }
1736
1737 static int mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch *esw,
1738                                          struct mlx5_eswitch *peer_esw,
1739                                          bool pair)
1740 {
1741         struct mlx5_flow_root_namespace *peer_ns;
1742         struct mlx5_flow_root_namespace *ns;
1743         int err;
1744
1745         peer_ns = peer_esw->dev->priv.steering->fdb_root_ns;
1746         ns = esw->dev->priv.steering->fdb_root_ns;
1747
1748         if (pair) {
1749                 err = mlx5_flow_namespace_set_peer(ns, peer_ns);
1750                 if (err)
1751                         return err;
1752
1753                 err = mlx5_flow_namespace_set_peer(peer_ns, ns);
1754                 if (err) {
1755                         mlx5_flow_namespace_set_peer(ns, NULL);
1756                         return err;
1757                 }
1758         } else {
1759                 mlx5_flow_namespace_set_peer(ns, NULL);
1760                 mlx5_flow_namespace_set_peer(peer_ns, NULL);
1761         }
1762
1763         return 0;
1764 }
1765
1766 static int mlx5_esw_offloads_devcom_event(int event,
1767                                           void *my_data,
1768                                           void *event_data)
1769 {
1770         struct mlx5_eswitch *esw = my_data;
1771         struct mlx5_devcom *devcom = esw->dev->priv.devcom;
1772         struct mlx5_eswitch *peer_esw = event_data;
1773         int err;
1774
1775         switch (event) {
1776         case ESW_OFFLOADS_DEVCOM_PAIR:
1777                 if (mlx5_eswitch_vport_match_metadata_enabled(esw) !=
1778                     mlx5_eswitch_vport_match_metadata_enabled(peer_esw))
1779                         break;
1780
1781                 err = mlx5_esw_offloads_set_ns_peer(esw, peer_esw, true);
1782                 if (err)
1783                         goto err_out;
1784                 err = mlx5_esw_offloads_pair(esw, peer_esw);
1785                 if (err)
1786                         goto err_peer;
1787
1788                 err = mlx5_esw_offloads_pair(peer_esw, esw);
1789                 if (err)
1790                         goto err_pair;
1791
1792                 mlx5_devcom_set_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS, true);
1793                 break;
1794
1795         case ESW_OFFLOADS_DEVCOM_UNPAIR:
1796                 if (!mlx5_devcom_is_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS))
1797                         break;
1798
1799                 mlx5_devcom_set_paired(devcom, MLX5_DEVCOM_ESW_OFFLOADS, false);
1800                 mlx5_esw_offloads_unpair(peer_esw);
1801                 mlx5_esw_offloads_unpair(esw);
1802                 mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
1803                 break;
1804         }
1805
1806         return 0;
1807
1808 err_pair:
1809         mlx5_esw_offloads_unpair(esw);
1810 err_peer:
1811         mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
1812 err_out:
1813         mlx5_core_err(esw->dev, "esw offloads devcom event failure, event %u err %d",
1814                       event, err);
1815         return err;
1816 }
1817
1818 static void esw_offloads_devcom_init(struct mlx5_eswitch *esw)
1819 {
1820         struct mlx5_devcom *devcom = esw->dev->priv.devcom;
1821
1822         INIT_LIST_HEAD(&esw->offloads.peer_flows);
1823         mutex_init(&esw->offloads.peer_mutex);
1824
1825         if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
1826                 return;
1827
1828         mlx5_devcom_register_component(devcom,
1829                                        MLX5_DEVCOM_ESW_OFFLOADS,
1830                                        mlx5_esw_offloads_devcom_event,
1831                                        esw);
1832
1833         mlx5_devcom_send_event(devcom,
1834                                MLX5_DEVCOM_ESW_OFFLOADS,
1835                                ESW_OFFLOADS_DEVCOM_PAIR, esw);
1836 }
1837
1838 static void esw_offloads_devcom_cleanup(struct mlx5_eswitch *esw)
1839 {
1840         struct mlx5_devcom *devcom = esw->dev->priv.devcom;
1841
1842         if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
1843                 return;
1844
1845         mlx5_devcom_send_event(devcom, MLX5_DEVCOM_ESW_OFFLOADS,
1846                                ESW_OFFLOADS_DEVCOM_UNPAIR, esw);
1847
1848         mlx5_devcom_unregister_component(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
1849 }
1850
1851 static int esw_vport_ingress_prio_tag_config(struct mlx5_eswitch *esw,
1852                                              struct mlx5_vport *vport)
1853 {
1854         struct mlx5_flow_act flow_act = {0};
1855         struct mlx5_flow_spec *spec;
1856         int err = 0;
1857
1858         /* For prio tag mode, there is only 1 FTEs:
1859          * 1) Untagged packets - push prio tag VLAN and modify metadata if
1860          * required, allow
1861          * Unmatched traffic is allowed by default
1862          */
1863         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1864         if (!spec)
1865                 return -ENOMEM;
1866
1867         /* Untagged packets - push prio tag VLAN, allow */
1868         MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.cvlan_tag);
1869         MLX5_SET(fte_match_param, spec->match_value, outer_headers.cvlan_tag, 0);
1870         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1871         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1872                           MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1873         flow_act.vlan[0].ethtype = ETH_P_8021Q;
1874         flow_act.vlan[0].vid = 0;
1875         flow_act.vlan[0].prio = 0;
1876
1877         if (vport->ingress.offloads.modify_metadata_rule) {
1878                 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1879                 flow_act.modify_hdr = vport->ingress.offloads.modify_metadata;
1880         }
1881
1882         vport->ingress.allow_rule =
1883                 mlx5_add_flow_rules(vport->ingress.acl, spec,
1884                                     &flow_act, NULL, 0);
1885         if (IS_ERR(vport->ingress.allow_rule)) {
1886                 err = PTR_ERR(vport->ingress.allow_rule);
1887                 esw_warn(esw->dev,
1888                          "vport[%d] configure ingress untagged allow rule, err(%d)\n",
1889                          vport->vport, err);
1890                 vport->ingress.allow_rule = NULL;
1891         }
1892
1893         kvfree(spec);
1894         return err;
1895 }
1896
1897 static int esw_vport_add_ingress_acl_modify_metadata(struct mlx5_eswitch *esw,
1898                                                      struct mlx5_vport *vport)
1899 {
1900         u8 action[MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)] = {};
1901         struct mlx5_flow_act flow_act = {};
1902         int err = 0;
1903         u32 key;
1904
1905         key = mlx5_eswitch_get_vport_metadata_for_match(esw, vport->vport);
1906         key >>= ESW_SOURCE_PORT_METADATA_OFFSET;
1907
1908         MLX5_SET(set_action_in, action, action_type, MLX5_ACTION_TYPE_SET);
1909         MLX5_SET(set_action_in, action, field,
1910                  MLX5_ACTION_IN_FIELD_METADATA_REG_C_0);
1911         MLX5_SET(set_action_in, action, data, key);
1912         MLX5_SET(set_action_in, action, offset,
1913                  ESW_SOURCE_PORT_METADATA_OFFSET);
1914         MLX5_SET(set_action_in, action, length,
1915                  ESW_SOURCE_PORT_METADATA_BITS);
1916
1917         vport->ingress.offloads.modify_metadata =
1918                 mlx5_modify_header_alloc(esw->dev, MLX5_FLOW_NAMESPACE_ESW_INGRESS,
1919                                          1, action);
1920         if (IS_ERR(vport->ingress.offloads.modify_metadata)) {
1921                 err = PTR_ERR(vport->ingress.offloads.modify_metadata);
1922                 esw_warn(esw->dev,
1923                          "failed to alloc modify header for vport %d ingress acl (%d)\n",
1924                          vport->vport, err);
1925                 return err;
1926         }
1927
1928         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_MOD_HDR | MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1929         flow_act.modify_hdr = vport->ingress.offloads.modify_metadata;
1930         vport->ingress.offloads.modify_metadata_rule =
1931                                 mlx5_add_flow_rules(vport->ingress.acl,
1932                                                     NULL, &flow_act, NULL, 0);
1933         if (IS_ERR(vport->ingress.offloads.modify_metadata_rule)) {
1934                 err = PTR_ERR(vport->ingress.offloads.modify_metadata_rule);
1935                 esw_warn(esw->dev,
1936                          "failed to add setting metadata rule for vport %d ingress acl, err(%d)\n",
1937                          vport->vport, err);
1938                 mlx5_modify_header_dealloc(esw->dev, vport->ingress.offloads.modify_metadata);
1939                 vport->ingress.offloads.modify_metadata_rule = NULL;
1940         }
1941         return err;
1942 }
1943
1944 static void esw_vport_del_ingress_acl_modify_metadata(struct mlx5_eswitch *esw,
1945                                                       struct mlx5_vport *vport)
1946 {
1947         if (vport->ingress.offloads.modify_metadata_rule) {
1948                 mlx5_del_flow_rules(vport->ingress.offloads.modify_metadata_rule);
1949                 mlx5_modify_header_dealloc(esw->dev, vport->ingress.offloads.modify_metadata);
1950
1951                 vport->ingress.offloads.modify_metadata_rule = NULL;
1952         }
1953 }
1954
1955 static int esw_vport_create_ingress_acl_group(struct mlx5_eswitch *esw,
1956                                               struct mlx5_vport *vport)
1957 {
1958         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1959         struct mlx5_flow_group *g;
1960         void *match_criteria;
1961         u32 *flow_group_in;
1962         u32 flow_index = 0;
1963         int ret = 0;
1964
1965         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1966         if (!flow_group_in)
1967                 return -ENOMEM;
1968
1969         if (esw_check_ingress_prio_tag_enabled(esw, vport)) {
1970                 /* This group is to hold FTE to match untagged packets when prio_tag
1971                  * is enabled.
1972                  */
1973                 memset(flow_group_in, 0, inlen);
1974
1975                 match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1976                                               flow_group_in, match_criteria);
1977                 MLX5_SET(create_flow_group_in, flow_group_in,
1978                          match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
1979                 MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.cvlan_tag);
1980                 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, flow_index);
1981                 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, flow_index);
1982
1983                 g = mlx5_create_flow_group(vport->ingress.acl, flow_group_in);
1984                 if (IS_ERR(g)) {
1985                         ret = PTR_ERR(g);
1986                         esw_warn(esw->dev, "vport[%d] ingress create untagged flow group, err(%d)\n",
1987                                  vport->vport, ret);
1988                         goto prio_tag_err;
1989                 }
1990                 vport->ingress.offloads.metadata_prio_tag_grp = g;
1991                 flow_index++;
1992         }
1993
1994         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1995                 /* This group holds an FTE with no matches for add metadata for
1996                  * tagged packets, if prio-tag is enabled (as a fallthrough),
1997                  * or all traffic in case prio-tag is disabled.
1998                  */
1999                 memset(flow_group_in, 0, inlen);
2000                 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, flow_index);
2001                 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, flow_index);
2002
2003                 g = mlx5_create_flow_group(vport->ingress.acl, flow_group_in);
2004                 if (IS_ERR(g)) {
2005                         ret = PTR_ERR(g);
2006                         esw_warn(esw->dev, "vport[%d] ingress create drop flow group, err(%d)\n",
2007                                  vport->vport, ret);
2008                         goto metadata_err;
2009                 }
2010                 vport->ingress.offloads.metadata_allmatch_grp = g;
2011         }
2012
2013         kvfree(flow_group_in);
2014         return 0;
2015
2016 metadata_err:
2017         if (!IS_ERR_OR_NULL(vport->ingress.offloads.metadata_prio_tag_grp)) {
2018                 mlx5_destroy_flow_group(vport->ingress.offloads.metadata_prio_tag_grp);
2019                 vport->ingress.offloads.metadata_prio_tag_grp = NULL;
2020         }
2021 prio_tag_err:
2022         kvfree(flow_group_in);
2023         return ret;
2024 }
2025
2026 static void esw_vport_destroy_ingress_acl_group(struct mlx5_vport *vport)
2027 {
2028         if (vport->ingress.offloads.metadata_allmatch_grp) {
2029                 mlx5_destroy_flow_group(vport->ingress.offloads.metadata_allmatch_grp);
2030                 vport->ingress.offloads.metadata_allmatch_grp = NULL;
2031         }
2032
2033         if (vport->ingress.offloads.metadata_prio_tag_grp) {
2034                 mlx5_destroy_flow_group(vport->ingress.offloads.metadata_prio_tag_grp);
2035                 vport->ingress.offloads.metadata_prio_tag_grp = NULL;
2036         }
2037 }
2038
2039 static int esw_vport_ingress_config(struct mlx5_eswitch *esw,
2040                                     struct mlx5_vport *vport)
2041 {
2042         int num_ftes = 0;
2043         int err;
2044
2045         if (!mlx5_eswitch_vport_match_metadata_enabled(esw) &&
2046             !esw_check_ingress_prio_tag_enabled(esw, vport))
2047                 return 0;
2048
2049         esw_vport_cleanup_ingress_rules(esw, vport);
2050
2051         if (mlx5_eswitch_vport_match_metadata_enabled(esw))
2052                 num_ftes++;
2053         if (esw_check_ingress_prio_tag_enabled(esw, vport))
2054                 num_ftes++;
2055
2056         err = esw_vport_create_ingress_acl_table(esw, vport, num_ftes);
2057         if (err) {
2058                 esw_warn(esw->dev,
2059                          "failed to enable ingress acl (%d) on vport[%d]\n",
2060                          err, vport->vport);
2061                 return err;
2062         }
2063
2064         err = esw_vport_create_ingress_acl_group(esw, vport);
2065         if (err)
2066                 goto group_err;
2067
2068         esw_debug(esw->dev,
2069                   "vport[%d] configure ingress rules\n", vport->vport);
2070
2071         if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
2072                 err = esw_vport_add_ingress_acl_modify_metadata(esw, vport);
2073                 if (err)
2074                         goto metadata_err;
2075         }
2076
2077         if (esw_check_ingress_prio_tag_enabled(esw, vport)) {
2078                 err = esw_vport_ingress_prio_tag_config(esw, vport);
2079                 if (err)
2080                         goto prio_tag_err;
2081         }
2082         return 0;
2083
2084 prio_tag_err:
2085         esw_vport_del_ingress_acl_modify_metadata(esw, vport);
2086 metadata_err:
2087         esw_vport_destroy_ingress_acl_group(vport);
2088 group_err:
2089         esw_vport_destroy_ingress_acl_table(vport);
2090         return err;
2091 }
2092
2093 static int esw_vport_egress_config(struct mlx5_eswitch *esw,
2094                                    struct mlx5_vport *vport)
2095 {
2096         int err;
2097
2098         if (!MLX5_CAP_GEN(esw->dev, prio_tag_required))
2099                 return 0;
2100
2101         esw_vport_cleanup_egress_rules(esw, vport);
2102
2103         err = esw_vport_enable_egress_acl(esw, vport);
2104         if (err)
2105                 return err;
2106
2107         /* For prio tag mode, there is only 1 FTEs:
2108          * 1) prio tag packets - pop the prio tag VLAN, allow
2109          * Unmatched traffic is allowed by default
2110          */
2111         esw_debug(esw->dev,
2112                   "vport[%d] configure prio tag egress rules\n", vport->vport);
2113
2114         /* prio tag vlan rule - pop it so VF receives untagged packets */
2115         err = mlx5_esw_create_vport_egress_acl_vlan(esw, vport, 0,
2116                                                     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
2117                                                     MLX5_FLOW_CONTEXT_ACTION_ALLOW);
2118         if (err)
2119                 esw_vport_disable_egress_acl(esw, vport);
2120
2121         return err;
2122 }
2123
2124 static bool
2125 esw_check_vport_match_metadata_supported(const struct mlx5_eswitch *esw)
2126 {
2127         if (!MLX5_CAP_ESW(esw->dev, esw_uplink_ingress_acl))
2128                 return false;
2129
2130         if (!(MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
2131               MLX5_FDB_TO_VPORT_REG_C_0))
2132                 return false;
2133
2134         if (!MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
2135                 return false;
2136
2137         if (mlx5_core_is_ecpf_esw_manager(esw->dev) ||
2138             mlx5_ecpf_vport_exists(esw->dev))
2139                 return false;
2140
2141         return true;
2142 }
2143
2144 static bool
2145 esw_check_vport_match_metadata_mandatory(const struct mlx5_eswitch *esw)
2146 {
2147         return mlx5_core_mp_enabled(esw->dev);
2148 }
2149
2150 static bool esw_use_vport_metadata(const struct mlx5_eswitch *esw)
2151 {
2152         return esw_check_vport_match_metadata_mandatory(esw) &&
2153                esw_check_vport_match_metadata_supported(esw);
2154 }
2155
2156 int
2157 esw_vport_create_offloads_acl_tables(struct mlx5_eswitch *esw,
2158                                      struct mlx5_vport *vport)
2159 {
2160         int err;
2161
2162         err = esw_vport_ingress_config(esw, vport);
2163         if (err)
2164                 return err;
2165
2166         if (mlx5_eswitch_is_vf_vport(esw, vport->vport)) {
2167                 err = esw_vport_egress_config(esw, vport);
2168                 if (err) {
2169                         esw_vport_cleanup_ingress_rules(esw, vport);
2170                         esw_vport_del_ingress_acl_modify_metadata(esw, vport);
2171                         esw_vport_destroy_ingress_acl_group(vport);
2172                         esw_vport_destroy_ingress_acl_table(vport);
2173                 }
2174         }
2175         return err;
2176 }
2177
2178 void
2179 esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch *esw,
2180                                       struct mlx5_vport *vport)
2181 {
2182         esw_vport_disable_egress_acl(esw, vport);
2183         esw_vport_cleanup_ingress_rules(esw, vport);
2184         esw_vport_del_ingress_acl_modify_metadata(esw, vport);
2185         esw_vport_destroy_ingress_acl_group(vport);
2186         esw_vport_destroy_ingress_acl_table(vport);
2187 }
2188
2189 static int esw_create_uplink_offloads_acl_tables(struct mlx5_eswitch *esw)
2190 {
2191         struct mlx5_vport *vport;
2192         int err;
2193
2194         if (esw_use_vport_metadata(esw))
2195                 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
2196
2197         vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
2198         err = esw_vport_create_offloads_acl_tables(esw, vport);
2199         if (err)
2200                 esw->flags &= ~MLX5_ESWITCH_VPORT_MATCH_METADATA;
2201         return err;
2202 }
2203
2204 static void esw_destroy_uplink_offloads_acl_tables(struct mlx5_eswitch *esw)
2205 {
2206         struct mlx5_vport *vport;
2207
2208         vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
2209         esw_vport_destroy_offloads_acl_tables(esw, vport);
2210         esw->flags &= ~MLX5_ESWITCH_VPORT_MATCH_METADATA;
2211 }
2212
2213 static int esw_offloads_steering_init(struct mlx5_eswitch *esw)
2214 {
2215         int num_vfs = esw->esw_funcs.num_vfs;
2216         int total_vports;
2217         int err;
2218
2219         if (mlx5_core_is_ecpf_esw_manager(esw->dev))
2220                 total_vports = esw->total_vports;
2221         else
2222                 total_vports = num_vfs + MLX5_SPECIAL_VPORTS(esw->dev);
2223
2224         memset(&esw->fdb_table.offloads, 0, sizeof(struct offloads_fdb));
2225
2226         err = esw_create_uplink_offloads_acl_tables(esw);
2227         if (err)
2228                 return err;
2229
2230         err = esw_create_offloads_table(esw, total_vports);
2231         if (err)
2232                 goto create_offloads_err;
2233
2234         err = esw_create_restore_table(esw);
2235         if (err)
2236                 goto create_restore_err;
2237
2238         err = esw_create_offloads_fdb_tables(esw, total_vports);
2239         if (err)
2240                 goto create_fdb_err;
2241
2242         err = esw_create_vport_rx_group(esw, total_vports);
2243         if (err)
2244                 goto create_fg_err;
2245
2246         mutex_init(&esw->fdb_table.offloads.vports.lock);
2247         hash_init(esw->fdb_table.offloads.vports.table);
2248
2249         return 0;
2250
2251 create_fg_err:
2252         esw_destroy_offloads_fdb_tables(esw);
2253 create_fdb_err:
2254         esw_destroy_restore_table(esw);
2255 create_restore_err:
2256         esw_destroy_offloads_table(esw);
2257 create_offloads_err:
2258         esw_destroy_uplink_offloads_acl_tables(esw);
2259
2260         return err;
2261 }
2262
2263 static void esw_offloads_steering_cleanup(struct mlx5_eswitch *esw)
2264 {
2265         mutex_destroy(&esw->fdb_table.offloads.vports.lock);
2266         esw_destroy_vport_rx_group(esw);
2267         esw_destroy_offloads_fdb_tables(esw);
2268         esw_destroy_restore_table(esw);
2269         esw_destroy_offloads_table(esw);
2270         esw_destroy_uplink_offloads_acl_tables(esw);
2271 }
2272
2273 static void
2274 esw_vfs_changed_event_handler(struct mlx5_eswitch *esw, const u32 *out)
2275 {
2276         bool host_pf_disabled;
2277         u16 new_num_vfs;
2278
2279         new_num_vfs = MLX5_GET(query_esw_functions_out, out,
2280                                host_params_context.host_num_of_vfs);
2281         host_pf_disabled = MLX5_GET(query_esw_functions_out, out,
2282                                     host_params_context.host_pf_disabled);
2283
2284         if (new_num_vfs == esw->esw_funcs.num_vfs || host_pf_disabled)
2285                 return;
2286
2287         /* Number of VFs can only change from "0 to x" or "x to 0". */
2288         if (esw->esw_funcs.num_vfs > 0) {
2289                 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
2290         } else {
2291                 int err;
2292
2293                 err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
2294                                                   MLX5_VPORT_UC_ADDR_CHANGE);
2295                 if (err)
2296                         return;
2297         }
2298         esw->esw_funcs.num_vfs = new_num_vfs;
2299 }
2300
2301 static void esw_functions_changed_event_handler(struct work_struct *work)
2302 {
2303         struct mlx5_host_work *host_work;
2304         struct mlx5_eswitch *esw;
2305         const u32 *out;
2306
2307         host_work = container_of(work, struct mlx5_host_work, work);
2308         esw = host_work->esw;
2309
2310         out = mlx5_esw_query_functions(esw->dev);
2311         if (IS_ERR(out))
2312                 goto out;
2313
2314         esw_vfs_changed_event_handler(esw, out);
2315         kvfree(out);
2316 out:
2317         kfree(host_work);
2318 }
2319
2320 int mlx5_esw_funcs_changed_handler(struct notifier_block *nb, unsigned long type, void *data)
2321 {
2322         struct mlx5_esw_functions *esw_funcs;
2323         struct mlx5_host_work *host_work;
2324         struct mlx5_eswitch *esw;
2325
2326         host_work = kzalloc(sizeof(*host_work), GFP_ATOMIC);
2327         if (!host_work)
2328                 return NOTIFY_DONE;
2329
2330         esw_funcs = mlx5_nb_cof(nb, struct mlx5_esw_functions, nb);
2331         esw = container_of(esw_funcs, struct mlx5_eswitch, esw_funcs);
2332
2333         host_work->esw = esw;
2334
2335         INIT_WORK(&host_work->work, esw_functions_changed_event_handler);
2336         queue_work(esw->work_queue, &host_work->work);
2337
2338         return NOTIFY_OK;
2339 }
2340
2341 int esw_offloads_enable(struct mlx5_eswitch *esw)
2342 {
2343         struct mlx5_vport *vport;
2344         int err, i;
2345
2346         if (MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, reformat) &&
2347             MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, decap))
2348                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
2349         else
2350                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
2351
2352         mutex_init(&esw->offloads.termtbl_mutex);
2353         mlx5_rdma_enable_roce(esw->dev);
2354
2355         err = esw_set_passing_vport_metadata(esw, true);
2356         if (err)
2357                 goto err_vport_metadata;
2358
2359         err = esw_offloads_steering_init(esw);
2360         if (err)
2361                 goto err_steering_init;
2362
2363         /* Representor will control the vport link state */
2364         mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs)
2365                 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
2366
2367         /* Uplink vport rep must load first. */
2368         err = esw_offloads_load_rep(esw, MLX5_VPORT_UPLINK);
2369         if (err)
2370                 goto err_uplink;
2371
2372         err = mlx5_eswitch_enable_pf_vf_vports(esw, MLX5_VPORT_UC_ADDR_CHANGE);
2373         if (err)
2374                 goto err_vports;
2375
2376         esw_offloads_devcom_init(esw);
2377
2378         return 0;
2379
2380 err_vports:
2381         esw_offloads_unload_rep(esw, MLX5_VPORT_UPLINK);
2382 err_uplink:
2383         esw_set_passing_vport_metadata(esw, false);
2384 err_steering_init:
2385         esw_offloads_steering_cleanup(esw);
2386 err_vport_metadata:
2387         mlx5_rdma_disable_roce(esw->dev);
2388         mutex_destroy(&esw->offloads.termtbl_mutex);
2389         return err;
2390 }
2391
2392 static int esw_offloads_stop(struct mlx5_eswitch *esw,
2393                              struct netlink_ext_ack *extack)
2394 {
2395         int err, err1;
2396
2397         mlx5_eswitch_disable(esw, false);
2398         err = mlx5_eswitch_enable(esw, MLX5_ESWITCH_LEGACY);
2399         if (err) {
2400                 NL_SET_ERR_MSG_MOD(extack, "Failed setting eswitch to legacy");
2401                 err1 = mlx5_eswitch_enable(esw, MLX5_ESWITCH_OFFLOADS);
2402                 if (err1) {
2403                         NL_SET_ERR_MSG_MOD(extack,
2404                                            "Failed setting eswitch back to offloads");
2405                 }
2406         }
2407
2408         return err;
2409 }
2410
2411 void esw_offloads_disable(struct mlx5_eswitch *esw)
2412 {
2413         esw_offloads_devcom_cleanup(esw);
2414         mlx5_eswitch_disable_pf_vf_vports(esw);
2415         esw_offloads_unload_rep(esw, MLX5_VPORT_UPLINK);
2416         esw_set_passing_vport_metadata(esw, false);
2417         esw_offloads_steering_cleanup(esw);
2418         mlx5_rdma_disable_roce(esw->dev);
2419         mutex_destroy(&esw->offloads.termtbl_mutex);
2420         esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
2421 }
2422
2423 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
2424 {
2425         switch (mode) {
2426         case DEVLINK_ESWITCH_MODE_LEGACY:
2427                 *mlx5_mode = MLX5_ESWITCH_LEGACY;
2428                 break;
2429         case DEVLINK_ESWITCH_MODE_SWITCHDEV:
2430                 *mlx5_mode = MLX5_ESWITCH_OFFLOADS;
2431                 break;
2432         default:
2433                 return -EINVAL;
2434         }
2435
2436         return 0;
2437 }
2438
2439 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
2440 {
2441         switch (mlx5_mode) {
2442         case MLX5_ESWITCH_LEGACY:
2443                 *mode = DEVLINK_ESWITCH_MODE_LEGACY;
2444                 break;
2445         case MLX5_ESWITCH_OFFLOADS:
2446                 *mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
2447                 break;
2448         default:
2449                 return -EINVAL;
2450         }
2451
2452         return 0;
2453 }
2454
2455 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
2456 {
2457         switch (mode) {
2458         case DEVLINK_ESWITCH_INLINE_MODE_NONE:
2459                 *mlx5_mode = MLX5_INLINE_MODE_NONE;
2460                 break;
2461         case DEVLINK_ESWITCH_INLINE_MODE_LINK:
2462                 *mlx5_mode = MLX5_INLINE_MODE_L2;
2463                 break;
2464         case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
2465                 *mlx5_mode = MLX5_INLINE_MODE_IP;
2466                 break;
2467         case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
2468                 *mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
2469                 break;
2470         default:
2471                 return -EINVAL;
2472         }
2473
2474         return 0;
2475 }
2476
2477 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
2478 {
2479         switch (mlx5_mode) {
2480         case MLX5_INLINE_MODE_NONE:
2481                 *mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
2482                 break;
2483         case MLX5_INLINE_MODE_L2:
2484                 *mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
2485                 break;
2486         case MLX5_INLINE_MODE_IP:
2487                 *mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
2488                 break;
2489         case MLX5_INLINE_MODE_TCP_UDP:
2490                 *mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
2491                 break;
2492         default:
2493                 return -EINVAL;
2494         }
2495
2496         return 0;
2497 }
2498
2499 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
2500 {
2501         if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2502                 return -EOPNOTSUPP;
2503
2504         if(!MLX5_ESWITCH_MANAGER(dev))
2505                 return -EPERM;
2506
2507         if (dev->priv.eswitch->mode == MLX5_ESWITCH_NONE &&
2508             !mlx5_core_is_ecpf_esw_manager(dev))
2509                 return -EOPNOTSUPP;
2510
2511         return 0;
2512 }
2513
2514 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
2515                                   struct netlink_ext_ack *extack)
2516 {
2517         struct mlx5_core_dev *dev = devlink_priv(devlink);
2518         u16 cur_mlx5_mode, mlx5_mode = 0;
2519         int err;
2520
2521         err = mlx5_eswitch_check(dev);
2522         if (err)
2523                 return err;
2524
2525         cur_mlx5_mode = dev->priv.eswitch->mode;
2526
2527         if (esw_mode_from_devlink(mode, &mlx5_mode))
2528                 return -EINVAL;
2529
2530         if (cur_mlx5_mode == mlx5_mode)
2531                 return 0;
2532
2533         if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
2534                 return esw_offloads_start(dev->priv.eswitch, extack);
2535         else if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
2536                 return esw_offloads_stop(dev->priv.eswitch, extack);
2537         else
2538                 return -EINVAL;
2539 }
2540
2541 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
2542 {
2543         struct mlx5_core_dev *dev = devlink_priv(devlink);
2544         int err;
2545
2546         err = mlx5_eswitch_check(dev);
2547         if (err)
2548                 return err;
2549
2550         return esw_mode_to_devlink(dev->priv.eswitch->mode, mode);
2551 }
2552
2553 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode,
2554                                          struct netlink_ext_ack *extack)
2555 {
2556         struct mlx5_core_dev *dev = devlink_priv(devlink);
2557         struct mlx5_eswitch *esw = dev->priv.eswitch;
2558         int err, vport, num_vport;
2559         u8 mlx5_mode;
2560
2561         err = mlx5_eswitch_check(dev);
2562         if (err)
2563                 return err;
2564
2565         switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
2566         case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
2567                 if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE)
2568                         return 0;
2569                 /* fall through */
2570         case MLX5_CAP_INLINE_MODE_L2:
2571                 NL_SET_ERR_MSG_MOD(extack, "Inline mode can't be set");
2572                 return -EOPNOTSUPP;
2573         case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
2574                 break;
2575         }
2576
2577         if (atomic64_read(&esw->offloads.num_flows) > 0) {
2578                 NL_SET_ERR_MSG_MOD(extack,
2579                                    "Can't set inline mode when flows are configured");
2580                 return -EOPNOTSUPP;
2581         }
2582
2583         err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
2584         if (err)
2585                 goto out;
2586
2587         mlx5_esw_for_each_host_func_vport(esw, vport, esw->esw_funcs.num_vfs) {
2588                 err = mlx5_modify_nic_vport_min_inline(dev, vport, mlx5_mode);
2589                 if (err) {
2590                         NL_SET_ERR_MSG_MOD(extack,
2591                                            "Failed to set min inline on vport");
2592                         goto revert_inline_mode;
2593                 }
2594         }
2595
2596         esw->offloads.inline_mode = mlx5_mode;
2597         return 0;
2598
2599 revert_inline_mode:
2600         num_vport = --vport;
2601         mlx5_esw_for_each_host_func_vport_reverse(esw, vport, num_vport)
2602                 mlx5_modify_nic_vport_min_inline(dev,
2603                                                  vport,
2604                                                  esw->offloads.inline_mode);
2605 out:
2606         return err;
2607 }
2608
2609 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
2610 {
2611         struct mlx5_core_dev *dev = devlink_priv(devlink);
2612         struct mlx5_eswitch *esw = dev->priv.eswitch;
2613         int err;
2614
2615         err = mlx5_eswitch_check(dev);
2616         if (err)
2617                 return err;
2618
2619         return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
2620 }
2621
2622 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
2623                                         enum devlink_eswitch_encap_mode encap,
2624                                         struct netlink_ext_ack *extack)
2625 {
2626         struct mlx5_core_dev *dev = devlink_priv(devlink);
2627         struct mlx5_eswitch *esw = dev->priv.eswitch;
2628         int err;
2629
2630         err = mlx5_eswitch_check(dev);
2631         if (err)
2632                 return err;
2633
2634         if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
2635             (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) ||
2636              !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap)))
2637                 return -EOPNOTSUPP;
2638
2639         if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC)
2640                 return -EOPNOTSUPP;
2641
2642         if (esw->mode == MLX5_ESWITCH_LEGACY) {
2643                 esw->offloads.encap = encap;
2644                 return 0;
2645         }
2646
2647         if (esw->offloads.encap == encap)
2648                 return 0;
2649
2650         if (atomic64_read(&esw->offloads.num_flows) > 0) {
2651                 NL_SET_ERR_MSG_MOD(extack,
2652                                    "Can't set encapsulation when flows are configured");
2653                 return -EOPNOTSUPP;
2654         }
2655
2656         esw_destroy_offloads_fdb_tables(esw);
2657
2658         esw->offloads.encap = encap;
2659
2660         err = esw_create_offloads_fdb_tables(esw, esw->nvports);
2661
2662         if (err) {
2663                 NL_SET_ERR_MSG_MOD(extack,
2664                                    "Failed re-creating fast FDB table");
2665                 esw->offloads.encap = !encap;
2666                 (void)esw_create_offloads_fdb_tables(esw, esw->nvports);
2667         }
2668
2669         return err;
2670 }
2671
2672 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink,
2673                                         enum devlink_eswitch_encap_mode *encap)
2674 {
2675         struct mlx5_core_dev *dev = devlink_priv(devlink);
2676         struct mlx5_eswitch *esw = dev->priv.eswitch;
2677         int err;
2678
2679         err = mlx5_eswitch_check(dev);
2680         if (err)
2681                 return err;
2682
2683         *encap = esw->offloads.encap;
2684         return 0;
2685 }
2686
2687 static bool
2688 mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch *esw, u16 vport_num)
2689 {
2690         /* Currently, only ECPF based device has representor for host PF. */
2691         if (vport_num == MLX5_VPORT_PF &&
2692             !mlx5_core_is_ecpf_esw_manager(esw->dev))
2693                 return false;
2694
2695         if (vport_num == MLX5_VPORT_ECPF &&
2696             !mlx5_ecpf_vport_exists(esw->dev))
2697                 return false;
2698
2699         return true;
2700 }
2701
2702 void mlx5_eswitch_register_vport_reps(struct mlx5_eswitch *esw,
2703                                       const struct mlx5_eswitch_rep_ops *ops,
2704                                       u8 rep_type)
2705 {
2706         struct mlx5_eswitch_rep_data *rep_data;
2707         struct mlx5_eswitch_rep *rep;
2708         int i;
2709
2710         esw->offloads.rep_ops[rep_type] = ops;
2711         mlx5_esw_for_all_reps(esw, i, rep) {
2712                 if (likely(mlx5_eswitch_vport_has_rep(esw, i))) {
2713                         rep_data = &rep->rep_data[rep_type];
2714                         atomic_set(&rep_data->state, REP_REGISTERED);
2715                 }
2716         }
2717 }
2718 EXPORT_SYMBOL(mlx5_eswitch_register_vport_reps);
2719
2720 void mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch *esw, u8 rep_type)
2721 {
2722         struct mlx5_eswitch_rep *rep;
2723         int i;
2724
2725         if (esw->mode == MLX5_ESWITCH_OFFLOADS)
2726                 __unload_reps_all_vport(esw, rep_type);
2727
2728         mlx5_esw_for_all_reps(esw, i, rep)
2729                 atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
2730 }
2731 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_reps);
2732
2733 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
2734 {
2735         struct mlx5_eswitch_rep *rep;
2736
2737         rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
2738         return rep->rep_data[rep_type].priv;
2739 }
2740
2741 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
2742                                  u16 vport,
2743                                  u8 rep_type)
2744 {
2745         struct mlx5_eswitch_rep *rep;
2746
2747         rep = mlx5_eswitch_get_rep(esw, vport);
2748
2749         if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2750             esw->offloads.rep_ops[rep_type]->get_proto_dev)
2751                 return esw->offloads.rep_ops[rep_type]->get_proto_dev(rep);
2752         return NULL;
2753 }
2754 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
2755
2756 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
2757 {
2758         return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
2759 }
2760 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
2761
2762 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
2763                                                 u16 vport)
2764 {
2765         return mlx5_eswitch_get_rep(esw, vport);
2766 }
2767 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);
2768
2769 bool mlx5_eswitch_is_vf_vport(const struct mlx5_eswitch *esw, u16 vport_num)
2770 {
2771         return vport_num >= MLX5_VPORT_FIRST_VF &&
2772                vport_num <= esw->dev->priv.sriov.max_vfs;
2773 }
2774
2775 bool mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch *esw)
2776 {
2777         return !!(esw->flags & MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED);
2778 }
2779 EXPORT_SYMBOL(mlx5_eswitch_reg_c1_loopback_enabled);
2780
2781 bool mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch *esw)
2782 {
2783         return !!(esw->flags & MLX5_ESWITCH_VPORT_MATCH_METADATA);
2784 }
2785 EXPORT_SYMBOL(mlx5_eswitch_vport_match_metadata_enabled);
2786
2787 u32 mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch *esw,
2788                                               u16 vport_num)
2789 {
2790         u32 vport_num_mask = GENMASK(ESW_VPORT_BITS - 1, 0);
2791         u32 vhca_id_mask = GENMASK(ESW_VHCA_ID_BITS - 1, 0);
2792         u32 vhca_id = MLX5_CAP_GEN(esw->dev, vhca_id);
2793         u32 val;
2794
2795         /* Make sure the vhca_id fits the ESW_VHCA_ID_BITS */
2796         WARN_ON_ONCE(vhca_id >= BIT(ESW_VHCA_ID_BITS));
2797
2798         /* Trim vhca_id to ESW_VHCA_ID_BITS */
2799         vhca_id &= vhca_id_mask;
2800
2801         /* Make sure pf and ecpf map to end of ESW_VPORT_BITS range so they
2802          * don't overlap with VF numbers, and themselves, after trimming.
2803          */
2804         WARN_ON_ONCE((MLX5_VPORT_UPLINK & vport_num_mask) <
2805                      vport_num_mask - 1);
2806         WARN_ON_ONCE((MLX5_VPORT_ECPF & vport_num_mask) <
2807                      vport_num_mask - 1);
2808         WARN_ON_ONCE((MLX5_VPORT_UPLINK & vport_num_mask) ==
2809                      (MLX5_VPORT_ECPF & vport_num_mask));
2810
2811         /* Make sure that the VF vport_num fits ESW_VPORT_BITS and don't
2812          * overlap with pf and ecpf.
2813          */
2814         if (vport_num != MLX5_VPORT_UPLINK &&
2815             vport_num != MLX5_VPORT_ECPF)
2816                 WARN_ON_ONCE(vport_num >= vport_num_mask - 1);
2817
2818         /* We can now trim vport_num to ESW_VPORT_BITS */
2819         vport_num &= vport_num_mask;
2820
2821         val = (vhca_id << ESW_VPORT_BITS) | vport_num;
2822         return val << (32 - ESW_SOURCE_PORT_METADATA_BITS);
2823 }
2824 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_match);