net/mlx5e: Add TC tunnel release action for SRIOV offloads
[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
63         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
64                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
65                 dest[i].vport_num = attr->out_rep->vport;
66                 i++;
67         }
68         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
69                 counter = mlx5_fc_create(esw->dev, true);
70                 if (IS_ERR(counter))
71                         return ERR_CAST(counter);
72                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
73                 dest[i].counter = counter;
74                 i++;
75         }
76
77         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
78         MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);
79
80         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
81         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
82
83         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS |
84                                       MLX5_MATCH_MISC_PARAMETERS;
85         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
86                 spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
87
88         rule = mlx5_add_flow_rules((struct mlx5_flow_table *)esw->fdb_table.fdb,
89                                    spec, &flow_act, dest, i);
90         if (IS_ERR(rule))
91                 mlx5_fc_destroy(esw->dev, counter);
92
93         return rule;
94 }
95
96 static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
97 {
98         struct mlx5_eswitch_rep *rep;
99         int vf_vport, err = 0;
100
101         esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
102         for (vf_vport = 1; vf_vport < esw->enabled_vports; vf_vport++) {
103                 rep = &esw->offloads.vport_reps[vf_vport];
104                 if (!rep->valid)
105                         continue;
106
107                 err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
108                 if (err)
109                         goto out;
110         }
111
112 out:
113         return err;
114 }
115
116 static struct mlx5_eswitch_rep *
117 esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
118 {
119         struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;
120
121         in_rep  = attr->in_rep;
122         out_rep = attr->out_rep;
123
124         if (push)
125                 vport = in_rep;
126         else if (pop)
127                 vport = out_rep;
128         else
129                 vport = in_rep;
130
131         return vport;
132 }
133
134 static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
135                                      bool push, bool pop, bool fwd)
136 {
137         struct mlx5_eswitch_rep *in_rep, *out_rep;
138
139         if ((push || pop) && !fwd)
140                 goto out_notsupp;
141
142         in_rep  = attr->in_rep;
143         out_rep = attr->out_rep;
144
145         if (push && in_rep->vport == FDB_UPLINK_VPORT)
146                 goto out_notsupp;
147
148         if (pop && out_rep->vport == FDB_UPLINK_VPORT)
149                 goto out_notsupp;
150
151         /* vport has vlan push configured, can't offload VF --> wire rules w.o it */
152         if (!push && !pop && fwd)
153                 if (in_rep->vlan && out_rep->vport == FDB_UPLINK_VPORT)
154                         goto out_notsupp;
155
156         /* protects against (1) setting rules with different vlans to push and
157          * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
158          */
159         if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan))
160                 goto out_notsupp;
161
162         return 0;
163
164 out_notsupp:
165         return -ENOTSUPP;
166 }
167
168 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
169                                  struct mlx5_esw_flow_attr *attr)
170 {
171         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
172         struct mlx5_eswitch_rep *vport = NULL;
173         bool push, pop, fwd;
174         int err = 0;
175
176         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
177         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
178         fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
179
180         err = esw_add_vlan_action_check(attr, push, pop, fwd);
181         if (err)
182                 return err;
183
184         attr->vlan_handled = false;
185
186         vport = esw_vlan_action_get_vport(attr, push, pop);
187
188         if (!push && !pop && fwd) {
189                 /* tracks VF --> wire rules without vlan push action */
190                 if (attr->out_rep->vport == FDB_UPLINK_VPORT) {
191                         vport->vlan_refcount++;
192                         attr->vlan_handled = true;
193                 }
194
195                 return 0;
196         }
197
198         if (!push && !pop)
199                 return 0;
200
201         if (!(offloads->vlan_push_pop_refcount)) {
202                 /* it's the 1st vlan rule, apply global vlan pop policy */
203                 err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
204                 if (err)
205                         goto out;
206         }
207         offloads->vlan_push_pop_refcount++;
208
209         if (push) {
210                 if (vport->vlan_refcount)
211                         goto skip_set_push;
212
213                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, attr->vlan, 0,
214                                                     SET_VLAN_INSERT | SET_VLAN_STRIP);
215                 if (err)
216                         goto out;
217                 vport->vlan = attr->vlan;
218 skip_set_push:
219                 vport->vlan_refcount++;
220         }
221 out:
222         if (!err)
223                 attr->vlan_handled = true;
224         return err;
225 }
226
227 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
228                                  struct mlx5_esw_flow_attr *attr)
229 {
230         struct offloads_fdb *offloads = &esw->fdb_table.offloads;
231         struct mlx5_eswitch_rep *vport = NULL;
232         bool push, pop, fwd;
233         int err = 0;
234
235         if (!attr->vlan_handled)
236                 return 0;
237
238         push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
239         pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
240         fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);
241
242         vport = esw_vlan_action_get_vport(attr, push, pop);
243
244         if (!push && !pop && fwd) {
245                 /* tracks VF --> wire rules without vlan push action */
246                 if (attr->out_rep->vport == FDB_UPLINK_VPORT)
247                         vport->vlan_refcount--;
248
249                 return 0;
250         }
251
252         if (push) {
253                 vport->vlan_refcount--;
254                 if (vport->vlan_refcount)
255                         goto skip_unset_push;
256
257                 vport->vlan = 0;
258                 err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
259                                                     0, 0, SET_VLAN_STRIP);
260                 if (err)
261                         goto out;
262         }
263
264 skip_unset_push:
265         offloads->vlan_push_pop_refcount--;
266         if (offloads->vlan_push_pop_refcount)
267                 return 0;
268
269         /* no more vlan rules, stop global vlan pop policy */
270         err = esw_set_global_vlan_pop(esw, 0);
271
272 out:
273         return err;
274 }
275
276 static struct mlx5_flow_handle *
277 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, int vport, u32 sqn)
278 {
279         struct mlx5_flow_act flow_act = {0};
280         struct mlx5_flow_destination dest;
281         struct mlx5_flow_handle *flow_rule;
282         struct mlx5_flow_spec *spec;
283         void *misc;
284
285         spec = mlx5_vzalloc(sizeof(*spec));
286         if (!spec) {
287                 esw_warn(esw->dev, "FDB: Failed to alloc match parameters\n");
288                 flow_rule = ERR_PTR(-ENOMEM);
289                 goto out;
290         }
291
292         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
293         MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
294         MLX5_SET(fte_match_set_misc, misc, source_port, 0x0); /* source vport is 0 */
295
296         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
297         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
298         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
299
300         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
301         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
302         dest.vport_num = vport;
303         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
304
305         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fdb, spec,
306                                         &flow_act, &dest, 1);
307         if (IS_ERR(flow_rule))
308                 esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
309 out:
310         kvfree(spec);
311         return flow_rule;
312 }
313
314 void mlx5_eswitch_sqs2vport_stop(struct mlx5_eswitch *esw,
315                                  struct mlx5_eswitch_rep *rep)
316 {
317         struct mlx5_esw_sq *esw_sq, *tmp;
318
319         if (esw->mode != SRIOV_OFFLOADS)
320                 return;
321
322         list_for_each_entry_safe(esw_sq, tmp, &rep->vport_sqs_list, list) {
323                 mlx5_del_flow_rules(esw_sq->send_to_vport_rule);
324                 list_del(&esw_sq->list);
325                 kfree(esw_sq);
326         }
327 }
328
329 int mlx5_eswitch_sqs2vport_start(struct mlx5_eswitch *esw,
330                                  struct mlx5_eswitch_rep *rep,
331                                  u16 *sqns_array, int sqns_num)
332 {
333         struct mlx5_flow_handle *flow_rule;
334         struct mlx5_esw_sq *esw_sq;
335         int err;
336         int i;
337
338         if (esw->mode != SRIOV_OFFLOADS)
339                 return 0;
340
341         for (i = 0; i < sqns_num; i++) {
342                 esw_sq = kzalloc(sizeof(*esw_sq), GFP_KERNEL);
343                 if (!esw_sq) {
344                         err = -ENOMEM;
345                         goto out_err;
346                 }
347
348                 /* Add re-inject rule to the PF/representor sqs */
349                 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
350                                                                 rep->vport,
351                                                                 sqns_array[i]);
352                 if (IS_ERR(flow_rule)) {
353                         err = PTR_ERR(flow_rule);
354                         kfree(esw_sq);
355                         goto out_err;
356                 }
357                 esw_sq->send_to_vport_rule = flow_rule;
358                 list_add(&esw_sq->list, &rep->vport_sqs_list);
359         }
360         return 0;
361
362 out_err:
363         mlx5_eswitch_sqs2vport_stop(esw, rep);
364         return err;
365 }
366
367 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
368 {
369         struct mlx5_flow_act flow_act = {0};
370         struct mlx5_flow_destination dest;
371         struct mlx5_flow_handle *flow_rule = NULL;
372         struct mlx5_flow_spec *spec;
373         int err = 0;
374
375         spec = mlx5_vzalloc(sizeof(*spec));
376         if (!spec) {
377                 esw_warn(esw->dev, "FDB: Failed to alloc match parameters\n");
378                 err = -ENOMEM;
379                 goto out;
380         }
381
382         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
383         dest.vport_num = 0;
384         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
385
386         flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fdb, spec,
387                                         &flow_act, &dest, 1);
388         if (IS_ERR(flow_rule)) {
389                 err = PTR_ERR(flow_rule);
390                 esw_warn(esw->dev,  "FDB: Failed to add miss flow rule err %d\n", err);
391                 goto out;
392         }
393
394         esw->fdb_table.offloads.miss_rule = flow_rule;
395 out:
396         kvfree(spec);
397         return err;
398 }
399
400 #define MAX_PF_SQ 256
401 #define ESW_OFFLOADS_NUM_ENTRIES (1 << 13) /* 8K */
402 #define ESW_OFFLOADS_NUM_GROUPS  4
403
404 static int esw_create_offloads_fdb_table(struct mlx5_eswitch *esw, int nvports)
405 {
406         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
407         struct mlx5_core_dev *dev = esw->dev;
408         struct mlx5_flow_namespace *root_ns;
409         struct mlx5_flow_table *fdb = NULL;
410         struct mlx5_flow_group *g;
411         u32 *flow_group_in;
412         void *match_criteria;
413         int table_size, ix, err = 0;
414         u32 flags = 0;
415
416         flow_group_in = mlx5_vzalloc(inlen);
417         if (!flow_group_in)
418                 return -ENOMEM;
419
420         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
421         if (!root_ns) {
422                 esw_warn(dev, "Failed to get FDB flow namespace\n");
423                 goto ns_err;
424         }
425
426         esw_debug(dev, "Create offloads FDB table, log_max_size(%d)\n",
427                   MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
428
429         if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, encap) &&
430             MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
431                 flags |= MLX5_FLOW_TABLE_TUNNEL_EN;
432
433         fdb = mlx5_create_auto_grouped_flow_table(root_ns, FDB_FAST_PATH,
434                                                   ESW_OFFLOADS_NUM_ENTRIES,
435                                                   ESW_OFFLOADS_NUM_GROUPS, 0,
436                                                   flags);
437         if (IS_ERR(fdb)) {
438                 err = PTR_ERR(fdb);
439                 esw_warn(dev, "Failed to create Fast path FDB Table err %d\n", err);
440                 goto fast_fdb_err;
441         }
442         esw->fdb_table.fdb = fdb;
443
444         table_size = nvports + MAX_PF_SQ + 1;
445         fdb = mlx5_create_flow_table(root_ns, FDB_SLOW_PATH, table_size, 0, 0);
446         if (IS_ERR(fdb)) {
447                 err = PTR_ERR(fdb);
448                 esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
449                 goto slow_fdb_err;
450         }
451         esw->fdb_table.offloads.fdb = fdb;
452
453         /* create send-to-vport group */
454         memset(flow_group_in, 0, inlen);
455         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
456                  MLX5_MATCH_MISC_PARAMETERS);
457
458         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
459
460         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
461         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
462
463         ix = nvports + MAX_PF_SQ;
464         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
465         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix - 1);
466
467         g = mlx5_create_flow_group(fdb, flow_group_in);
468         if (IS_ERR(g)) {
469                 err = PTR_ERR(g);
470                 esw_warn(dev, "Failed to create send-to-vport flow group err(%d)\n", err);
471                 goto send_vport_err;
472         }
473         esw->fdb_table.offloads.send_to_vport_grp = g;
474
475         /* create miss group */
476         memset(flow_group_in, 0, inlen);
477         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable, 0);
478
479         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
480         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix + 1);
481
482         g = mlx5_create_flow_group(fdb, flow_group_in);
483         if (IS_ERR(g)) {
484                 err = PTR_ERR(g);
485                 esw_warn(dev, "Failed to create miss flow group err(%d)\n", err);
486                 goto miss_err;
487         }
488         esw->fdb_table.offloads.miss_grp = g;
489
490         err = esw_add_fdb_miss_rule(esw);
491         if (err)
492                 goto miss_rule_err;
493
494         return 0;
495
496 miss_rule_err:
497         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
498 miss_err:
499         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
500 send_vport_err:
501         mlx5_destroy_flow_table(esw->fdb_table.offloads.fdb);
502 slow_fdb_err:
503         mlx5_destroy_flow_table(esw->fdb_table.fdb);
504 fast_fdb_err:
505 ns_err:
506         kvfree(flow_group_in);
507         return err;
508 }
509
510 static void esw_destroy_offloads_fdb_table(struct mlx5_eswitch *esw)
511 {
512         if (!esw->fdb_table.fdb)
513                 return;
514
515         esw_debug(esw->dev, "Destroy offloads FDB Table\n");
516         mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule);
517         mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
518         mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
519
520         mlx5_destroy_flow_table(esw->fdb_table.offloads.fdb);
521         mlx5_destroy_flow_table(esw->fdb_table.fdb);
522 }
523
524 static int esw_create_offloads_table(struct mlx5_eswitch *esw)
525 {
526         struct mlx5_flow_namespace *ns;
527         struct mlx5_flow_table *ft_offloads;
528         struct mlx5_core_dev *dev = esw->dev;
529         int err = 0;
530
531         ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
532         if (!ns) {
533                 esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
534                 return -ENOMEM;
535         }
536
537         ft_offloads = mlx5_create_flow_table(ns, 0, dev->priv.sriov.num_vfs + 2, 0, 0);
538         if (IS_ERR(ft_offloads)) {
539                 err = PTR_ERR(ft_offloads);
540                 esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
541                 return err;
542         }
543
544         esw->offloads.ft_offloads = ft_offloads;
545         return 0;
546 }
547
548 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
549 {
550         struct mlx5_esw_offload *offloads = &esw->offloads;
551
552         mlx5_destroy_flow_table(offloads->ft_offloads);
553 }
554
555 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
556 {
557         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
558         struct mlx5_flow_group *g;
559         struct mlx5_priv *priv = &esw->dev->priv;
560         u32 *flow_group_in;
561         void *match_criteria, *misc;
562         int err = 0;
563         int nvports = priv->sriov.num_vfs + 2;
564
565         flow_group_in = mlx5_vzalloc(inlen);
566         if (!flow_group_in)
567                 return -ENOMEM;
568
569         /* create vport rx group */
570         memset(flow_group_in, 0, inlen);
571         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
572                  MLX5_MATCH_MISC_PARAMETERS);
573
574         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
575         misc = MLX5_ADDR_OF(fte_match_param, match_criteria, misc_parameters);
576         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
577
578         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
579         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
580
581         g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
582
583         if (IS_ERR(g)) {
584                 err = PTR_ERR(g);
585                 mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
586                 goto out;
587         }
588
589         esw->offloads.vport_rx_group = g;
590 out:
591         kfree(flow_group_in);
592         return err;
593 }
594
595 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
596 {
597         mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
598 }
599
600 struct mlx5_flow_handle *
601 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
602 {
603         struct mlx5_flow_act flow_act = {0};
604         struct mlx5_flow_destination dest;
605         struct mlx5_flow_handle *flow_rule;
606         struct mlx5_flow_spec *spec;
607         void *misc;
608
609         spec = mlx5_vzalloc(sizeof(*spec));
610         if (!spec) {
611                 esw_warn(esw->dev, "Failed to alloc match parameters\n");
612                 flow_rule = ERR_PTR(-ENOMEM);
613                 goto out;
614         }
615
616         misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
617         MLX5_SET(fte_match_set_misc, misc, source_port, vport);
618
619         misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
620         MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
621
622         spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
623         dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
624         dest.tir_num = tirn;
625
626         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
627         flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
628                                        &flow_act, &dest, 1);
629         if (IS_ERR(flow_rule)) {
630                 esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
631                 goto out;
632         }
633
634 out:
635         kvfree(spec);
636         return flow_rule;
637 }
638
639 static int esw_offloads_start(struct mlx5_eswitch *esw)
640 {
641         int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
642
643         if (esw->mode != SRIOV_LEGACY) {
644                 esw_warn(esw->dev, "Can't set offloads mode, SRIOV legacy not enabled\n");
645                 return -EINVAL;
646         }
647
648         mlx5_eswitch_disable_sriov(esw);
649         err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
650         if (err) {
651                 esw_warn(esw->dev, "Failed setting eswitch to offloads, err %d\n", err);
652                 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
653                 if (err1)
654                         esw_warn(esw->dev, "Failed setting eswitch back to legacy, err %d\n", err);
655         }
656         return err;
657 }
658
659 int esw_offloads_init(struct mlx5_eswitch *esw, int nvports)
660 {
661         struct mlx5_eswitch_rep *rep;
662         int vport;
663         int err;
664
665         err = esw_create_offloads_fdb_table(esw, nvports);
666         if (err)
667                 return err;
668
669         err = esw_create_offloads_table(esw);
670         if (err)
671                 goto create_ft_err;
672
673         err = esw_create_vport_rx_group(esw);
674         if (err)
675                 goto create_fg_err;
676
677         for (vport = 0; vport < nvports; vport++) {
678                 rep = &esw->offloads.vport_reps[vport];
679                 if (!rep->valid)
680                         continue;
681
682                 err = rep->load(esw, rep);
683                 if (err)
684                         goto err_reps;
685         }
686         return 0;
687
688 err_reps:
689         for (vport--; vport >= 0; vport--) {
690                 rep = &esw->offloads.vport_reps[vport];
691                 if (!rep->valid)
692                         continue;
693                 rep->unload(esw, rep);
694         }
695         esw_destroy_vport_rx_group(esw);
696
697 create_fg_err:
698         esw_destroy_offloads_table(esw);
699
700 create_ft_err:
701         esw_destroy_offloads_fdb_table(esw);
702         return err;
703 }
704
705 static int esw_offloads_stop(struct mlx5_eswitch *esw)
706 {
707         int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
708
709         mlx5_eswitch_disable_sriov(esw);
710         err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
711         if (err) {
712                 esw_warn(esw->dev, "Failed setting eswitch to legacy, err %d\n", err);
713                 err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
714                 if (err1)
715                         esw_warn(esw->dev, "Failed setting eswitch back to offloads, err %d\n", err);
716         }
717
718         return err;
719 }
720
721 void esw_offloads_cleanup(struct mlx5_eswitch *esw, int nvports)
722 {
723         struct mlx5_eswitch_rep *rep;
724         int vport;
725
726         for (vport = 0; vport < nvports; vport++) {
727                 rep = &esw->offloads.vport_reps[vport];
728                 if (!rep->valid)
729                         continue;
730                 rep->unload(esw, rep);
731         }
732
733         esw_destroy_vport_rx_group(esw);
734         esw_destroy_offloads_table(esw);
735         esw_destroy_offloads_fdb_table(esw);
736 }
737
738 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
739 {
740         switch (mode) {
741         case DEVLINK_ESWITCH_MODE_LEGACY:
742                 *mlx5_mode = SRIOV_LEGACY;
743                 break;
744         case DEVLINK_ESWITCH_MODE_SWITCHDEV:
745                 *mlx5_mode = SRIOV_OFFLOADS;
746                 break;
747         default:
748                 return -EINVAL;
749         }
750
751         return 0;
752 }
753
754 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
755 {
756         switch (mlx5_mode) {
757         case SRIOV_LEGACY:
758                 *mode = DEVLINK_ESWITCH_MODE_LEGACY;
759                 break;
760         case SRIOV_OFFLOADS:
761                 *mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
762                 break;
763         default:
764                 return -EINVAL;
765         }
766
767         return 0;
768 }
769
770 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
771 {
772         struct mlx5_core_dev *dev;
773         u16 cur_mlx5_mode, mlx5_mode = 0;
774
775         dev = devlink_priv(devlink);
776
777         if (!MLX5_CAP_GEN(dev, vport_group_manager))
778                 return -EOPNOTSUPP;
779
780         cur_mlx5_mode = dev->priv.eswitch->mode;
781
782         if (cur_mlx5_mode == SRIOV_NONE)
783                 return -EOPNOTSUPP;
784
785         if (esw_mode_from_devlink(mode, &mlx5_mode))
786                 return -EINVAL;
787
788         if (cur_mlx5_mode == mlx5_mode)
789                 return 0;
790
791         if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
792                 return esw_offloads_start(dev->priv.eswitch);
793         else if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
794                 return esw_offloads_stop(dev->priv.eswitch);
795         else
796                 return -EINVAL;
797 }
798
799 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
800 {
801         struct mlx5_core_dev *dev;
802
803         dev = devlink_priv(devlink);
804
805         if (!MLX5_CAP_GEN(dev, vport_group_manager))
806                 return -EOPNOTSUPP;
807
808         if (dev->priv.eswitch->mode == SRIOV_NONE)
809                 return -EOPNOTSUPP;
810
811         return esw_mode_to_devlink(dev->priv.eswitch->mode, mode);
812 }
813
814 void mlx5_eswitch_register_vport_rep(struct mlx5_eswitch *esw,
815                                      int vport_index,
816                                      struct mlx5_eswitch_rep *__rep)
817 {
818         struct mlx5_esw_offload *offloads = &esw->offloads;
819         struct mlx5_eswitch_rep *rep;
820
821         rep = &offloads->vport_reps[vport_index];
822
823         memset(rep, 0, sizeof(*rep));
824
825         rep->load   = __rep->load;
826         rep->unload = __rep->unload;
827         rep->vport  = __rep->vport;
828         rep->priv_data = __rep->priv_data;
829         ether_addr_copy(rep->hw_id, __rep->hw_id);
830
831         INIT_LIST_HEAD(&rep->vport_sqs_list);
832         rep->valid = true;
833 }
834
835 void mlx5_eswitch_unregister_vport_rep(struct mlx5_eswitch *esw,
836                                        int vport_index)
837 {
838         struct mlx5_esw_offload *offloads = &esw->offloads;
839         struct mlx5_eswitch_rep *rep;
840
841         rep = &offloads->vport_reps[vport_index];
842
843         if (esw->mode == SRIOV_OFFLOADS && esw->vports[vport_index].enabled)
844                 rep->unload(esw, rep);
845
846         rep->valid = false;
847 }