Merge tag 'drm-msm-fixes-2018-04-25' of git://people.freedesktop.org/~robclark/linux...
[sfrench/cifs-2.6.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
41 enum {
42         FDB_FAST_PATH = 0,
43         FDB_SLOW_PATH
44 };
45
46 struct mlx5_flow_handle *
47 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
48                                 struct mlx5_flow_spec *spec,
49                                 struct mlx5_esw_flow_attr *attr)
50 {
51         struct mlx5_flow_destination dest[2] = {};
52         struct mlx5_flow_act flow_act = {0};
53         struct mlx5_fc *counter = NULL;
54         struct mlx5_flow_handle *rule;
55         void *misc;
56         int i = 0;
57
58         if (esw->mode != SRIOV_OFFLOADS)
59                 return ERR_PTR(-EOPNOTSUPP);
60
61         flow_act.action = attr->action;
62         /* if per flow vlan pop/push is emulated, don't set that into the firmware */
63         if (!mlx5_eswitch_vlan_actions_supported(esw->dev))
64                 flow_act.action &= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
65                                      MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
66         else if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
67                 flow_act.vlan.ethtype = ntohs(attr->vlan_proto);
68                 flow_act.vlan.vid = attr->vlan_vid;
69                 flow_act.vlan.prio = attr->vlan_prio;
70         }
71
72         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
73                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
74                 dest[i].vport_num = attr->out_rep->vport;
75                 i++;
76         }
77         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
78                 counter = mlx5_fc_create(esw->dev, true);
79                 if (IS_ERR(counter)) {
80                         rule = ERR_CAST(counter);
81                         goto err_counter_alloc;
82                 }
83                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
84                 dest[i].counter = counter;
85                 i++;
86         }
87
88         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
89         MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);
90
91         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
92         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
93
94         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS |
95                                       MLX5_MATCH_MISC_PARAMETERS;
96         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
97                 spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
98
99         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
100                 flow_act.modify_id = attr->mod_hdr_id;
101
102         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
103                 flow_act.encap_id = attr->encap_id;
104
105         rule = mlx5_add_flow_rules((struct mlx5_flow_table *)esw->fdb_table.fdb,
106                                    spec, &flow_act, dest, i);
107         if (IS_ERR(rule))
108                 goto err_add_rule;
109         else
110                 esw->offloads.num_flows++;
111
112         return rule;
113
114 err_add_rule:
115         mlx5_fc_destroy(esw->dev, counter);
116 err_counter_alloc:
117         return rule;
118 }
119
120 void
121 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
122                                 struct mlx5_flow_handle *rule,
123                                 struct mlx5_esw_flow_attr *attr)
124 {
125         struct mlx5_fc *counter = NULL;
126
127         counter = mlx5_flow_rule_counter(rule);
128         mlx5_del_flow_rules(rule);
129         mlx5_fc_destroy(esw->dev, counter);
130         esw->offloads.num_flows--;
131 }
132
133 static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
134 {
135         struct mlx5_eswitch_rep *rep;
136         int vf_vport, err = 0;
137
138         esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
139         for (vf_vport = 1; vf_vport < esw->enabled_vports; vf_vport++) {
140                 rep = &esw->offloads.vport_reps[vf_vport];
141                 if (!rep->rep_if[REP_ETH].valid)
142                         continue;
143
144                 err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
145                 if (err)
146                         goto out;
147         }
148
149 out:
150         return err;
151 }
152
153 static struct mlx5_eswitch_rep *
154 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
155 {
156         struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;
157
158         in_rep  = attr->in_rep;
159         out_rep = attr->out_rep;
160
161         if (push)
162                 vport = in_rep;
163         else if (pop)
164                 vport = out_rep;
165         else
166                 vport = in_rep;
167
168         return vport;
169 }
170
171 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
172                                      bool push, bool pop, bool fwd)
173 {
174         struct mlx5_eswitch_rep *in_rep, *out_rep;
175
176         if ((push || pop) && !fwd)
177                 goto out_notsupp;
178
179         in_rep  = attr->in_rep;
180         out_rep = attr->out_rep;
181
182         if (push && in_rep->vport == FDB_UPLINK_VPORT)
183                 goto out_notsupp;
184
185         if (pop && out_rep->vport == FDB_UPLINK_VPORT)
186                 goto out_notsupp;
187
188         /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
189         if (!push && !pop && fwd)
190                 if (in_rep->vlan && out_rep->vport == FDB_UPLINK_VPORT)
191                         goto out_notsupp;
192
193         /* protects against (1) setting rules with different vlans to push and
194          * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
195          */
196         if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan_vid))
197                 goto out_notsupp;
198
199         return 0;
200
201 out_notsupp:
202         return -EOPNOTSUPP;
203 }
204
205 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
206                                  struct mlx5_esw_flow_attr *attr)
207 {
208         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
209         struct mlx5_eswitch_rep *vport = NULL;
210         bool push, pop, fwd;
211         int err = 0;
212
213         /* nop if we're on the vlan push/pop non emulation mode */
214         if (mlx5_eswitch_vlan_actions_supported(esw->dev))
215                 return 0;
216
217         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
218         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
219         fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
220
221         err = esw_add_vlan_action_check(attr, push, pop, fwd);
222         if (err)
223                 return err;
224
225         attr->vlan_handled = false;
226
227         vport = esw_vlan_action_get_vport(attr, push, pop);
228
229         if (!push && !pop && fwd) {
230                 /* tracks VF --> wire rules without vlan push action */
231                 if (attr->out_rep->vport == FDB_UPLINK_VPORT) {
232                         vport->vlan_refcount++;
233                         attr->vlan_handled = true;
234                 }
235
236                 return 0;
237         }
238
239         if (!push && !pop)
240                 return 0;
241
242         if (!(offloads->vlan_push_pop_refcount)) {
243                 /* it's the 1st vlan rule, apply global vlan pop policy */
244                 err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
245                 if (err)
246                         goto out;
247         }
248         offloads->vlan_push_pop_refcount++;
249
250         if (push) {
251                 if (vport->vlan_refcount)
252                         goto skip_set_push;
253
254                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, attr->vlan_vid, 0,
255                                                     SET_VLAN_INSERT | SET_VLAN_STRIP);
256                 if (err)
257                         goto out;
258                 vport->vlan = attr->vlan_vid;
259 skip_set_push:
260                 vport->vlan_refcount++;
261         }
262 out:
263         if (!err)
264                 attr->vlan_handled = true;
265         return err;
266 }
267
268 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
269                                  struct mlx5_esw_flow_attr *attr)
270 {
271         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
272         struct mlx5_eswitch_rep *vport = NULL;
273         bool push, pop, fwd;
274         int err = 0;
275
276         /* nop if we're on the vlan push/pop non emulation mode */
277         if (mlx5_eswitch_vlan_actions_supported(esw->dev))
278                 return 0;
279
280         if (!attr->vlan_handled)
281                 return 0;
282
283         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
284         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
285         fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
286
287         vport = esw_vlan_action_get_vport(attr, push, pop);
288
289         if (!push && !pop && fwd) {
290                 /* tracks VF --> wire rules without vlan push action */
291                 if (attr->out_rep->vport == FDB_UPLINK_VPORT)
292                         vport->vlan_refcount--;
293
294                 return 0;
295         }
296
297         if (push) {
298                 vport->vlan_refcount--;
299                 if (vport->vlan_refcount)
300                         goto skip_unset_push;
301
302                 vport->vlan = 0;
303                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
304                                                     0, 0, SET_VLAN_STRIP);
305                 if (err)
306                         goto out;
307         }
308
309 skip_unset_push:
310         offloads->vlan_push_pop_refcount--;
311         if (offloads->vlan_push_pop_refcount)
312                 return 0;
313
314         /* no more vlan rules, stop global vlan pop policy */
315         err = esw_set_global_vlan_pop(esw, 0);
316
317 out:
318         return err;
319 }
320
321 struct mlx5_flow_handle *
322 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, int vport, u32 sqn)
323 {
324         struct mlx5_flow_act flow_act = {0};
325         struct mlx5_flow_destination dest = {};
326         struct mlx5_flow_handle *flow_rule;
327         struct mlx5_flow_spec *spec;
328         void *misc;
329
330         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
331         if (!spec) {
332                 flow_rule = ERR_PTR(-ENOMEM);
333                 goto out;
334         }
335
336         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
337         MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
338         MLX5_SET(fte_match_set_misc, misc, source_port, 0x0); /* source vport is 0 */
339
340         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
341         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
342         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
343
344         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
345         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
346         dest.vport_num = vport;
347         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
348
349         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fdb, spec,
350                                         &flow_act, &dest, 1);
351         if (IS_ERR(flow_rule))
352                 esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
353 out:
354         kvfree(spec);
355         return flow_rule;
356 }
357 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
358
359 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
360 {
361         mlx5_del_flow_rules(rule);
362 }
363
364 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
365 {
366         struct mlx5_flow_act flow_act = {0};
367         struct mlx5_flow_destination dest = {};
368         struct mlx5_flow_handle *flow_rule = NULL;
369         struct mlx5_flow_spec *spec;
370         void *headers_c;
371         void *headers_v;
372         int err = 0;
373         u8 *dmac_c;
374         u8 *dmac_v;
375
376         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
377         if (!spec) {
378                 err = -ENOMEM;
379                 goto out;
380         }
381
382         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
383         headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
384                                  outer_headers);
385         dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
386                               outer_headers.dmac_47_16);
387         dmac_c[0] = 0x01;
388
389         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
390         dest.vport_num = 0;
391         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
392
393         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fdb, spec,
394                                         &flow_act, &dest, 1);
395         if (IS_ERR(flow_rule)) {
396                 err = PTR_ERR(flow_rule);
397                 esw_warn(esw->dev,  "FDB: Failed to add unicast miss flow rule err %d\n", err);
398                 goto out;
399         }
400
401         esw->fdb_table.offloads.miss_rule_uni = flow_rule;
402
403         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
404                                  outer_headers);
405         dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
406                               outer_headers.dmac_47_16);
407         dmac_v[0] = 0x01;
408         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fdb, spec,
409                                         &flow_act, &dest, 1);
410         if (IS_ERR(flow_rule)) {
411                 err = PTR_ERR(flow_rule);
412                 esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
413                 mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
414                 goto out;
415         }
416
417         esw->fdb_table.offloads.miss_rule_multi = flow_rule;
418
419 out:
420         kvfree(spec);
421         return err;
422 }
423
424 #define ESW_OFFLOADS_NUM_GROUPS  4
425
426 static int esw_create_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
427 {
428         struct mlx5_core_dev *dev = esw->dev;
429         struct mlx5_flow_namespace *root_ns;
430         struct mlx5_flow_table *fdb = NULL;
431         int esw_size, err = 0;
432         u32 flags = 0;
433         u32 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
434                                 MLX5_CAP_GEN(dev, max_flow_counter_15_0);
435
436         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
437         if (!root_ns) {
438                 esw_warn(dev, "Failed to get FDB flow namespace\n");
439                 err = -EOPNOTSUPP;
440                 goto out;
441         }
442
443         esw_debug(dev, "Create offloads FDB table, min (max esw size(2^%d), max counters(%d)*groups(%d))\n",
444                   MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size),
445                   max_flow_counter, ESW_OFFLOADS_NUM_GROUPS);
446
447         esw_size = min_t(int, max_flow_counter * ESW_OFFLOADS_NUM_GROUPS,
448                          1 << MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
449
450         if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
451                 flags |= MLX5_FLOW_TABLE_TUNNEL_EN;
452
453         fdb = mlx5_create_auto_grouped_flow_table(root_ns, FDB_FAST_PATH,
454                                                   esw_size,
455                                                   ESW_OFFLOADS_NUM_GROUPS, 0,
456                                                   flags);
457         if (IS_ERR(fdb)) {
458                 err = PTR_ERR(fdb);
459                 esw_warn(dev, "Failed to create Fast path FDB Table err %d\n", err);
460                 goto out;
461         }
462         esw->fdb_table.fdb = fdb;
463
464 out:
465         return err;
466 }
467
468 static void esw_destroy_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
469 {
470         mlx5_destroy_flow_table(esw->fdb_table.fdb);
471 }
472
473 #define MAX_PF_SQ 256
474 #define MAX_SQ_NVPORTS 32
475
476 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw, int nvports)
477 {
478         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
479         struct mlx5_flow_table_attr ft_attr = {};
480         struct mlx5_core_dev *dev = esw->dev;
481         struct mlx5_flow_namespace *root_ns;
482         struct mlx5_flow_table *fdb = NULL;
483         int table_size, ix, err = 0;
484         struct mlx5_flow_group *g;
485         void *match_criteria;
486         u32 *flow_group_in;
487         u8 *dmac;
488
489         esw_debug(esw->dev, "Create offloads FDB Tables\n");
490         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
491         if (!flow_group_in)
492                 return -ENOMEM;
493
494         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
495         if (!root_ns) {
496                 esw_warn(dev, "Failed to get FDB flow namespace\n");
497                 err = -EOPNOTSUPP;
498                 goto ns_err;
499         }
500
501         err = esw_create_offloads_fast_fdb_table(esw);
502         if (err)
503                 goto fast_fdb_err;
504
505         table_size = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ + 2;
506
507         ft_attr.max_fte = table_size;
508         ft_attr.prio = FDB_SLOW_PATH;
509
510         fdb = mlx5_create_flow_table(root_ns, &ft_attr);
511         if (IS_ERR(fdb)) {
512                 err = PTR_ERR(fdb);
513                 esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
514                 goto slow_fdb_err;
515         }
516         esw->fdb_table.offloads.fdb = fdb;
517
518         /* create send-to-vport group */
519         memset(flow_group_in, 0, inlen);
520         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
521                  MLX5_MATCH_MISC_PARAMETERS);
522
523         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
524
525         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
526         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
527
528         ix = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ;
529         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
530         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix - 1);
531
532         g = mlx5_create_flow_group(fdb, flow_group_in);
533         if (IS_ERR(g)) {
534                 err = PTR_ERR(g);
535                 esw_warn(dev, "Failed to create send-to-vport flow group err(%d)\n", err);
536                 goto send_vport_err;
537         }
538         esw->fdb_table.offloads.send_to_vport_grp = g;
539
540         /* create miss group */
541         memset(flow_group_in, 0, inlen);
542         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
543                  MLX5_MATCH_OUTER_HEADERS);
544         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
545                                       match_criteria);
546         dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
547                             outer_headers.dmac_47_16);
548         dmac[0] = 0x01;
549
550         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
551         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix + 2);
552
553         g = mlx5_create_flow_group(fdb, flow_group_in);
554         if (IS_ERR(g)) {
555                 err = PTR_ERR(g);
556                 esw_warn(dev, "Failed to create miss flow group err(%d)\n", err);
557                 goto miss_err;
558         }
559         esw->fdb_table.offloads.miss_grp = g;
560
561         err = esw_add_fdb_miss_rule(esw);
562         if (err)
563                 goto miss_rule_err;
564
565         return 0;
566
567 miss_rule_err:
568         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
569 miss_err:
570         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
571 send_vport_err:
572         mlx5_destroy_flow_table(esw->fdb_table.offloads.fdb);
573 slow_fdb_err:
574         mlx5_destroy_flow_table(esw->fdb_table.fdb);
575 fast_fdb_err:
576 ns_err:
577         kvfree(flow_group_in);
578         return err;
579 }
580
581 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
582 {
583         if (!esw->fdb_table.fdb)
584                 return;
585
586         esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
587         mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
588         mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
589         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
590         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
591
592         mlx5_destroy_flow_table(esw->fdb_table.offloads.fdb);
593         esw_destroy_offloads_fast_fdb_table(esw);
594 }
595
596 static int esw_create_offloads_table(struct mlx5_eswitch *esw)
597 {
598         struct mlx5_flow_table_attr ft_attr = {};
599         struct mlx5_core_dev *dev = esw->dev;
600         struct mlx5_flow_table *ft_offloads;
601         struct mlx5_flow_namespace *ns;
602         int err = 0;
603
604         ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
605         if (!ns) {
606                 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
607                 return -EOPNOTSUPP;
608         }
609
610         ft_attr.max_fte = dev->priv.sriov.num_vfs + 2;
611
612         ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
613         if (IS_ERR(ft_offloads)) {
614                 err = PTR_ERR(ft_offloads);
615                 esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
616                 return err;
617         }
618
619         esw->offloads.ft_offloads = ft_offloads;
620         return 0;
621 }
622
623 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
624 {
625         struct mlx5_esw_offload *offloads = &esw->offloads;
626
627         mlx5_destroy_flow_table(offloads->ft_offloads);
628 }
629
630 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
631 {
632         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
633         struct mlx5_flow_group *g;
634         struct mlx5_priv *priv = &esw->dev->priv;
635         u32 *flow_group_in;
636         void *match_criteria, *misc;
637         int err = 0;
638         int nvports = priv->sriov.num_vfs + 2;
639
640         flow_group_in = kvzalloc(inlen, GFP_KERNEL);
641         if (!flow_group_in)
642                 return -ENOMEM;
643
644         /* create vport rx group */
645         memset(flow_group_in, 0, inlen);
646         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
647                  MLX5_MATCH_MISC_PARAMETERS);
648
649         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
650         misc = MLX5_ADDR_OF(fte_match_param, match_criteria, misc_parameters);
651         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
652
653         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
654         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
655
656         g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
657
658         if (IS_ERR(g)) {
659                 err = PTR_ERR(g);
660                 mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
661                 goto out;
662         }
663
664         esw->offloads.vport_rx_group = g;
665 out:
666         kfree(flow_group_in);
667         return err;
668 }
669
670 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
671 {
672         mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
673 }
674
675 struct mlx5_flow_handle *
676 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
677 {
678         struct mlx5_flow_act flow_act = {0};
679         struct mlx5_flow_destination dest = {};
680         struct mlx5_flow_handle *flow_rule;
681         struct mlx5_flow_spec *spec;
682         void *misc;
683
684         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
685         if (!spec) {
686                 flow_rule = ERR_PTR(-ENOMEM);
687                 goto out;
688         }
689
690         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
691         MLX5_SET(fte_match_set_misc, misc, source_port, vport);
692
693         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
694         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
695
696         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
697         dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
698         dest.tir_num = tirn;
699
700         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
701         flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
702                                         &flow_act, &dest, 1);
703         if (IS_ERR(flow_rule)) {
704                 esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
705                 goto out;
706         }
707
708 out:
709         kvfree(spec);
710         return flow_rule;
711 }
712
713 static int esw_offloads_start(struct mlx5_eswitch *esw)
714 {
715         int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
716
717         if (esw->mode != SRIOV_LEGACY) {
718                 esw_warn(esw->dev, "Can't set offloads mode, SRIOV legacy not enabled\n");
719                 return -EINVAL;
720         }
721
722         mlx5_eswitch_disable_sriov(esw);
723         err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
724         if (err) {
725                 esw_warn(esw->dev, "Failed setting eswitch to offloads, err %d\n", err);
726                 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
727                 if (err1)
728                         esw_warn(esw->dev, "Failed setting eswitch back to legacy, err %d\n", err1);
729         }
730         if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
731                 if (mlx5_eswitch_inline_mode_get(esw,
732                                                  num_vfs,
733                                                  &esw->offloads.inline_mode)) {
734                         esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
735                         esw_warn(esw->dev, "Inline mode is different between vports\n");
736                 }
737         }
738         return err;
739 }
740
741 void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
742 {
743         kfree(esw->offloads.vport_reps);
744 }
745
746 int esw_offloads_init_reps(struct mlx5_eswitch *esw)
747 {
748         int total_vfs = MLX5_TOTAL_VPORTS(esw->dev);
749         struct mlx5_core_dev *dev = esw->dev;
750         struct mlx5_esw_offload *offloads;
751         struct mlx5_eswitch_rep *rep;
752         u8 hw_id[ETH_ALEN];
753         int vport;
754
755         esw->offloads.vport_reps = kcalloc(total_vfs,
756                                            sizeof(struct mlx5_eswitch_rep),
757                                            GFP_KERNEL);
758         if (!esw->offloads.vport_reps)
759                 return -ENOMEM;
760
761         offloads = &esw->offloads;
762         mlx5_query_nic_vport_mac_address(dev, 0, hw_id);
763
764         for (vport = 0; vport < total_vfs; vport++) {
765                 rep = &offloads->vport_reps[vport];
766
767                 rep->vport = vport;
768                 ether_addr_copy(rep->hw_id, hw_id);
769         }
770
771         offloads->vport_reps[0].vport = FDB_UPLINK_VPORT;
772
773         return 0;
774 }
775
776 static void esw_offloads_unload_reps_type(struct mlx5_eswitch *esw, int nvports,
777                                           u8 rep_type)
778 {
779         struct mlx5_eswitch_rep *rep;
780         int vport;
781
782         for (vport = nvports - 1; vport >= 0; vport--) {
783                 rep = &esw->offloads.vport_reps[vport];
784                 if (!rep->rep_if[rep_type].valid)
785                         continue;
786
787                 rep->rep_if[rep_type].unload(rep);
788         }
789 }
790
791 static void esw_offloads_unload_reps(struct mlx5_eswitch *esw, int nvports)
792 {
793         u8 rep_type = NUM_REP_TYPES;
794
795         while (rep_type-- > 0)
796                 esw_offloads_unload_reps_type(esw, nvports, rep_type);
797 }
798
799 static int esw_offloads_load_reps_type(struct mlx5_eswitch *esw, int nvports,
800                                        u8 rep_type)
801 {
802         struct mlx5_eswitch_rep *rep;
803         int vport;
804         int err;
805
806         for (vport = 0; vport < nvports; vport++) {
807                 rep = &esw->offloads.vport_reps[vport];
808                 if (!rep->rep_if[rep_type].valid)
809                         continue;
810
811                 err = rep->rep_if[rep_type].load(esw->dev, rep);
812                 if (err)
813                         goto err_reps;
814         }
815
816         return 0;
817
818 err_reps:
819         esw_offloads_unload_reps_type(esw, vport, rep_type);
820         return err;
821 }
822
823 static int esw_offloads_load_reps(struct mlx5_eswitch *esw, int nvports)
824 {
825         u8 rep_type = 0;
826         int err;
827
828         for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
829                 err = esw_offloads_load_reps_type(esw, nvports, rep_type);
830                 if (err)
831                         goto err_reps;
832         }
833
834         return err;
835
836 err_reps:
837         while (rep_type-- > 0)
838                 esw_offloads_unload_reps_type(esw, nvports, rep_type);
839         return err;
840 }
841
842 int esw_offloads_init(struct mlx5_eswitch *esw, int nvports)
843 {
844         int err;
845
846         err = esw_create_offloads_fdb_tables(esw, nvports);
847         if (err)
848                 return err;
849
850         err = esw_create_offloads_table(esw);
851         if (err)
852                 goto create_ft_err;
853
854         err = esw_create_vport_rx_group(esw);
855         if (err)
856                 goto create_fg_err;
857
858         err = esw_offloads_load_reps(esw, nvports);
859         if (err)
860                 goto err_reps;
861
862         return 0;
863
864 err_reps:
865         esw_destroy_vport_rx_group(esw);
866
867 create_fg_err:
868         esw_destroy_offloads_table(esw);
869
870 create_ft_err:
871         esw_destroy_offloads_fdb_tables(esw);
872
873         return err;
874 }
875
876 static int esw_offloads_stop(struct mlx5_eswitch *esw)
877 {
878         int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
879
880         mlx5_eswitch_disable_sriov(esw);
881         err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
882         if (err) {
883                 esw_warn(esw->dev, "Failed setting eswitch to legacy, err %d\n", err);
884                 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
885                 if (err1)
886                         esw_warn(esw->dev, "Failed setting eswitch back to offloads, err %d\n", err);
887         }
888
889         /* enable back PF RoCE */
890         mlx5_reload_interface(esw->dev, MLX5_INTERFACE_PROTOCOL_IB);
891
892         return err;
893 }
894
895 void esw_offloads_cleanup(struct mlx5_eswitch *esw, int nvports)
896 {
897         esw_offloads_unload_reps(esw, nvports);
898         esw_destroy_vport_rx_group(esw);
899         esw_destroy_offloads_table(esw);
900         esw_destroy_offloads_fdb_tables(esw);
901 }
902
903 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
904 {
905         switch (mode) {
906         case DEVLINK_ESWITCH_MODE_LEGACY:
907                 *mlx5_mode = SRIOV_LEGACY;
908                 break;
909         case DEVLINK_ESWITCH_MODE_SWITCHDEV:
910                 *mlx5_mode = SRIOV_OFFLOADS;
911                 break;
912         default:
913                 return -EINVAL;
914         }
915
916         return 0;
917 }
918
919 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
920 {
921         switch (mlx5_mode) {
922         case SRIOV_LEGACY:
923                 *mode = DEVLINK_ESWITCH_MODE_LEGACY;
924                 break;
925         case SRIOV_OFFLOADS:
926                 *mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
927                 break;
928         default:
929                 return -EINVAL;
930         }
931
932         return 0;
933 }
934
935 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
936 {
937         switch (mode) {
938         case DEVLINK_ESWITCH_INLINE_MODE_NONE:
939                 *mlx5_mode = MLX5_INLINE_MODE_NONE;
940                 break;
941         case DEVLINK_ESWITCH_INLINE_MODE_LINK:
942                 *mlx5_mode = MLX5_INLINE_MODE_L2;
943                 break;
944         case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
945                 *mlx5_mode = MLX5_INLINE_MODE_IP;
946                 break;
947         case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
948                 *mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
949                 break;
950         default:
951                 return -EINVAL;
952         }
953
954         return 0;
955 }
956
957 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
958 {
959         switch (mlx5_mode) {
960         case MLX5_INLINE_MODE_NONE:
961                 *mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
962                 break;
963         case MLX5_INLINE_MODE_L2:
964                 *mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
965                 break;
966         case MLX5_INLINE_MODE_IP:
967                 *mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
968                 break;
969         case MLX5_INLINE_MODE_TCP_UDP:
970                 *mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
971                 break;
972         default:
973                 return -EINVAL;
974         }
975
976         return 0;
977 }
978
979 static int mlx5_devlink_eswitch_check(struct devlink *devlink)
980 {
981         struct mlx5_core_dev *dev = devlink_priv(devlink);
982
983         if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
984                 return -EOPNOTSUPP;
985
986         if (!MLX5_CAP_GEN(dev, vport_group_manager))
987                 return -EOPNOTSUPP;
988
989         if (dev->priv.eswitch->mode == SRIOV_NONE)
990                 return -EOPNOTSUPP;
991
992         return 0;
993 }
994
995 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
996 {
997         struct mlx5_core_dev *dev = devlink_priv(devlink);
998         u16 cur_mlx5_mode, mlx5_mode = 0;
999         int err;
1000
1001         err = mlx5_devlink_eswitch_check(devlink);
1002         if (err)
1003                 return err;
1004
1005         cur_mlx5_mode = dev->priv.eswitch->mode;
1006
1007         if (esw_mode_from_devlink(mode, &mlx5_mode))
1008                 return -EINVAL;
1009
1010         if (cur_mlx5_mode == mlx5_mode)
1011                 return 0;
1012
1013         if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
1014                 return esw_offloads_start(dev->priv.eswitch);
1015         else if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
1016                 return esw_offloads_stop(dev->priv.eswitch);
1017         else
1018                 return -EINVAL;
1019 }
1020
1021 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
1022 {
1023         struct mlx5_core_dev *dev = devlink_priv(devlink);
1024         int err;
1025
1026         err = mlx5_devlink_eswitch_check(devlink);
1027         if (err)
1028                 return err;
1029
1030         return esw_mode_to_devlink(dev->priv.eswitch->mode, mode);
1031 }
1032
1033 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode)
1034 {
1035         struct mlx5_core_dev *dev = devlink_priv(devlink);
1036         struct mlx5_eswitch *esw = dev->priv.eswitch;
1037         int err, vport;
1038         u8 mlx5_mode;
1039
1040         err = mlx5_devlink_eswitch_check(devlink);
1041         if (err)
1042                 return err;
1043
1044         switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
1045         case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1046                 if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE)
1047                         return 0;
1048                 /* fall through */
1049         case MLX5_CAP_INLINE_MODE_L2:
1050                 esw_warn(dev, "Inline mode can't be set\n");
1051                 return -EOPNOTSUPP;
1052         case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1053                 break;
1054         }
1055
1056         if (esw->offloads.num_flows > 0) {
1057                 esw_warn(dev, "Can't set inline mode when flows are configured\n");
1058                 return -EOPNOTSUPP;
1059         }
1060
1061         err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
1062         if (err)
1063                 goto out;
1064
1065         for (vport = 1; vport < esw->enabled_vports; vport++) {
1066                 err = mlx5_modify_nic_vport_min_inline(dev, vport, mlx5_mode);
1067                 if (err) {
1068                         esw_warn(dev, "Failed to set min inline on vport %d\n",
1069                                  vport);
1070                         goto revert_inline_mode;
1071                 }
1072         }
1073
1074         esw->offloads.inline_mode = mlx5_mode;
1075         return 0;
1076
1077 revert_inline_mode:
1078         while (--vport > 0)
1079                 mlx5_modify_nic_vport_min_inline(dev,
1080                                                  vport,
1081                                                  esw->offloads.inline_mode);
1082 out:
1083         return err;
1084 }
1085
1086 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
1087 {
1088         struct mlx5_core_dev *dev = devlink_priv(devlink);
1089         struct mlx5_eswitch *esw = dev->priv.eswitch;
1090         int err;
1091
1092         err = mlx5_devlink_eswitch_check(devlink);
1093         if (err)
1094                 return err;
1095
1096         return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
1097 }
1098
1099 int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, int nvfs, u8 *mode)
1100 {
1101         u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
1102         struct mlx5_core_dev *dev = esw->dev;
1103         int vport;
1104
1105         if (!MLX5_CAP_GEN(dev, vport_group_manager))
1106                 return -EOPNOTSUPP;
1107
1108         if (esw->mode == SRIOV_NONE)
1109                 return -EOPNOTSUPP;
1110
1111         switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
1112         case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1113                 mlx5_mode = MLX5_INLINE_MODE_NONE;
1114                 goto out;
1115         case MLX5_CAP_INLINE_MODE_L2:
1116                 mlx5_mode = MLX5_INLINE_MODE_L2;
1117                 goto out;
1118         case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1119                 goto query_vports;
1120         }
1121
1122 query_vports:
1123         for (vport = 1; vport <= nvfs; vport++) {
1124                 mlx5_query_nic_vport_min_inline(dev, vport, &mlx5_mode);
1125                 if (vport > 1 && prev_mlx5_mode != mlx5_mode)
1126                         return -EINVAL;
1127                 prev_mlx5_mode = mlx5_mode;
1128         }
1129
1130 out:
1131         *mode = mlx5_mode;
1132         return 0;
1133 }
1134
1135 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink, u8 encap)
1136 {
1137         struct mlx5_core_dev *dev = devlink_priv(devlink);
1138         struct mlx5_eswitch *esw = dev->priv.eswitch;
1139         int err;
1140
1141         err = mlx5_devlink_eswitch_check(devlink);
1142         if (err)
1143                 return err;
1144
1145         if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
1146             (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, encap) ||
1147              !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap)))
1148                 return -EOPNOTSUPP;
1149
1150         if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC)
1151                 return -EOPNOTSUPP;
1152
1153         if (esw->mode == SRIOV_LEGACY) {
1154                 esw->offloads.encap = encap;
1155                 return 0;
1156         }
1157
1158         if (esw->offloads.encap == encap)
1159                 return 0;
1160
1161         if (esw->offloads.num_flows > 0) {
1162                 esw_warn(dev, "Can't set encapsulation when flows are configured\n");
1163                 return -EOPNOTSUPP;
1164         }
1165
1166         esw_destroy_offloads_fast_fdb_table(esw);
1167
1168         esw->offloads.encap = encap;
1169         err = esw_create_offloads_fast_fdb_table(esw);
1170         if (err) {
1171                 esw_warn(esw->dev, "Failed re-creating fast FDB table, err %d\n", err);
1172                 esw->offloads.encap = !encap;
1173                 (void)esw_create_offloads_fast_fdb_table(esw);
1174         }
1175         return err;
1176 }
1177
1178 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink, u8 *encap)
1179 {
1180         struct mlx5_core_dev *dev = devlink_priv(devlink);
1181         struct mlx5_eswitch *esw = dev->priv.eswitch;
1182         int err;
1183
1184         err = mlx5_devlink_eswitch_check(devlink);
1185         if (err)
1186                 return err;
1187
1188         *encap = esw->offloads.encap;
1189         return 0;
1190 }
1191
1192 void mlx5_eswitch_register_vport_rep(struct mlx5_eswitch *esw,
1193                                      int vport_index,
1194                                      struct mlx5_eswitch_rep_if *__rep_if,
1195                                      u8 rep_type)
1196 {
1197         struct mlx5_esw_offload *offloads = &esw->offloads;
1198         struct mlx5_eswitch_rep_if *rep_if;
1199
1200         rep_if = &offloads->vport_reps[vport_index].rep_if[rep_type];
1201
1202         rep_if->load   = __rep_if->load;
1203         rep_if->unload = __rep_if->unload;
1204         rep_if->get_proto_dev = __rep_if->get_proto_dev;
1205         rep_if->priv = __rep_if->priv;
1206
1207         rep_if->valid = true;
1208 }
1209 EXPORT_SYMBOL(mlx5_eswitch_register_vport_rep);
1210
1211 void mlx5_eswitch_unregister_vport_rep(struct mlx5_eswitch *esw,
1212                                        int vport_index, u8 rep_type)
1213 {
1214         struct mlx5_esw_offload *offloads = &esw->offloads;
1215         struct mlx5_eswitch_rep *rep;
1216
1217         rep = &offloads->vport_reps[vport_index];
1218
1219         if (esw->mode == SRIOV_OFFLOADS && esw->vports[vport_index].enabled)
1220                 rep->rep_if[rep_type].unload(rep);
1221
1222         rep->rep_if[rep_type].valid = false;
1223 }
1224 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_rep);
1225
1226 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
1227 {
1228 #define UPLINK_REP_INDEX 0
1229         struct mlx5_esw_offload *offloads = &esw->offloads;
1230         struct mlx5_eswitch_rep *rep;
1231
1232         rep = &offloads->vport_reps[UPLINK_REP_INDEX];
1233         return rep->rep_if[rep_type].priv;
1234 }
1235
1236 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
1237                                  int vport,
1238                                  u8 rep_type)
1239 {
1240         struct mlx5_esw_offload *offloads = &esw->offloads;
1241         struct mlx5_eswitch_rep *rep;
1242
1243         if (vport == FDB_UPLINK_VPORT)
1244                 vport = UPLINK_REP_INDEX;
1245
1246         rep = &offloads->vport_reps[vport];
1247
1248         if (rep->rep_if[rep_type].valid &&
1249             rep->rep_if[rep_type].get_proto_dev)
1250                 return rep->rep_if[rep_type].get_proto_dev(rep);
1251         return NULL;
1252 }
1253 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
1254
1255 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
1256 {
1257         return mlx5_eswitch_get_proto_dev(esw, UPLINK_REP_INDEX, rep_type);
1258 }
1259 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
1260
1261 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
1262                                                 int vport)
1263 {
1264         return &esw->offloads.vport_reps[vport];
1265 }
1266 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);