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