net/mlx5e: Support SRIOV TC encapsulation offloads for IPv6 tunnels
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.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 <net/flow_dissector.h>
34 #include <net/sch_generic.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_skbedit.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/device.h>
40 #include <linux/rhashtable.h>
41 #include <net/switchdev.h>
42 #include <net/tc_act/tc_mirred.h>
43 #include <net/tc_act/tc_vlan.h>
44 #include <net/tc_act/tc_tunnel_key.h>
45 #include <net/vxlan.h>
46 #include "en.h"
47 #include "en_tc.h"
48 #include "eswitch.h"
49 #include "vxlan.h"
50
51 struct mlx5e_tc_flow {
52         struct rhash_head       node;
53         u64                     cookie;
54         struct mlx5_flow_handle *rule;
55         struct list_head        encap; /* flows sharing the same encap */
56         struct mlx5_esw_flow_attr *attr;
57 };
58
59 enum {
60         MLX5_HEADER_TYPE_VXLAN = 0x0,
61         MLX5_HEADER_TYPE_NVGRE = 0x1,
62 };
63
64 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
65 #define MLX5E_TC_TABLE_NUM_GROUPS 4
66
67 static struct mlx5_flow_handle *
68 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
69                       struct mlx5_flow_spec *spec,
70                       u32 action, u32 flow_tag)
71 {
72         struct mlx5_core_dev *dev = priv->mdev;
73         struct mlx5_flow_destination dest = { 0 };
74         struct mlx5_flow_act flow_act = {
75                 .action = action,
76                 .flow_tag = flow_tag,
77                 .encap_id = 0,
78         };
79         struct mlx5_fc *counter = NULL;
80         struct mlx5_flow_handle *rule;
81         bool table_created = false;
82
83         if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
84                 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
85                 dest.ft = priv->fs.vlan.ft.t;
86         } else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
87                 counter = mlx5_fc_create(dev, true);
88                 if (IS_ERR(counter))
89                         return ERR_CAST(counter);
90
91                 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
92                 dest.counter = counter;
93         }
94
95         if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
96                 priv->fs.tc.t =
97                         mlx5_create_auto_grouped_flow_table(priv->fs.ns,
98                                                             MLX5E_TC_PRIO,
99                                                             MLX5E_TC_TABLE_NUM_ENTRIES,
100                                                             MLX5E_TC_TABLE_NUM_GROUPS,
101                                                             0, 0);
102                 if (IS_ERR(priv->fs.tc.t)) {
103                         netdev_err(priv->netdev,
104                                    "Failed to create tc offload table\n");
105                         rule = ERR_CAST(priv->fs.tc.t);
106                         goto err_create_ft;
107                 }
108
109                 table_created = true;
110         }
111
112         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
113         rule = mlx5_add_flow_rules(priv->fs.tc.t, spec, &flow_act, &dest, 1);
114
115         if (IS_ERR(rule))
116                 goto err_add_rule;
117
118         return rule;
119
120 err_add_rule:
121         if (table_created) {
122                 mlx5_destroy_flow_table(priv->fs.tc.t);
123                 priv->fs.tc.t = NULL;
124         }
125 err_create_ft:
126         mlx5_fc_destroy(dev, counter);
127
128         return rule;
129 }
130
131 static struct mlx5_flow_handle *
132 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
133                       struct mlx5_flow_spec *spec,
134                       struct mlx5_esw_flow_attr *attr)
135 {
136         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
137         int err;
138
139         err = mlx5_eswitch_add_vlan_action(esw, attr);
140         if (err)
141                 return ERR_PTR(err);
142
143         return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
144 }
145
146 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
147                                struct mlx5e_tc_flow *flow) {
148         struct list_head *next = flow->encap.next;
149
150         list_del(&flow->encap);
151         if (list_empty(next)) {
152                 struct mlx5_encap_entry *e;
153
154                 e = list_entry(next, struct mlx5_encap_entry, flows);
155                 if (e->n) {
156                         mlx5_encap_dealloc(priv->mdev, e->encap_id);
157                         neigh_release(e->n);
158                 }
159                 hlist_del_rcu(&e->encap_hlist);
160                 kfree(e);
161         }
162 }
163
164 /* we get here also when setting rule to the FW failed, etc. It means that the
165  * flow rule itself might not exist, but some offloading related to the actions
166  * should be cleaned.
167  */
168 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
169                               struct mlx5e_tc_flow *flow)
170 {
171         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
172         struct mlx5_fc *counter = NULL;
173
174         if (!IS_ERR(flow->rule)) {
175                 counter = mlx5_flow_rule_counter(flow->rule);
176                 mlx5_del_flow_rules(flow->rule);
177                 mlx5_fc_destroy(priv->mdev, counter);
178         }
179
180         if (esw && esw->mode == SRIOV_OFFLOADS) {
181                 mlx5_eswitch_del_vlan_action(esw, flow->attr);
182                 if (flow->attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
183                         mlx5e_detach_encap(priv, flow);
184         }
185
186         if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
187                 mlx5_destroy_flow_table(priv->fs.tc.t);
188                 priv->fs.tc.t = NULL;
189         }
190 }
191
192 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
193                              struct tc_cls_flower_offload *f)
194 {
195         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
196                                        outer_headers);
197         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
198                                        outer_headers);
199         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
200                                     misc_parameters);
201         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
202                                     misc_parameters);
203
204         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
205         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
206
207         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
208                 struct flow_dissector_key_keyid *key =
209                         skb_flow_dissector_target(f->dissector,
210                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
211                                                   f->key);
212                 struct flow_dissector_key_keyid *mask =
213                         skb_flow_dissector_target(f->dissector,
214                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
215                                                   f->mask);
216                 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
217                          be32_to_cpu(mask->keyid));
218                 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
219                          be32_to_cpu(key->keyid));
220         }
221 }
222
223 static int parse_tunnel_attr(struct mlx5e_priv *priv,
224                              struct mlx5_flow_spec *spec,
225                              struct tc_cls_flower_offload *f)
226 {
227         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
228                                        outer_headers);
229         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
230                                        outer_headers);
231
232         struct flow_dissector_key_control *enc_control =
233                 skb_flow_dissector_target(f->dissector,
234                                           FLOW_DISSECTOR_KEY_ENC_CONTROL,
235                                           f->key);
236
237         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
238                 struct flow_dissector_key_ports *key =
239                         skb_flow_dissector_target(f->dissector,
240                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
241                                                   f->key);
242                 struct flow_dissector_key_ports *mask =
243                         skb_flow_dissector_target(f->dissector,
244                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
245                                                   f->mask);
246
247                 /* Full udp dst port must be given */
248                 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
249                         goto vxlan_match_offload_err;
250
251                 if (mlx5e_vxlan_lookup_port(priv, be16_to_cpu(key->dst)) &&
252                     MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
253                         parse_vxlan_attr(spec, f);
254                 else {
255                         netdev_warn(priv->netdev,
256                                     "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
257                         return -EOPNOTSUPP;
258                 }
259
260                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
261                          udp_dport, ntohs(mask->dst));
262                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
263                          udp_dport, ntohs(key->dst));
264
265                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
266                          udp_sport, ntohs(mask->src));
267                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
268                          udp_sport, ntohs(key->src));
269         } else { /* udp dst port must be given */
270 vxlan_match_offload_err:
271                 netdev_warn(priv->netdev,
272                             "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
273                 return -EOPNOTSUPP;
274         }
275
276         if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
277                 struct flow_dissector_key_ipv4_addrs *key =
278                         skb_flow_dissector_target(f->dissector,
279                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
280                                                   f->key);
281                 struct flow_dissector_key_ipv4_addrs *mask =
282                         skb_flow_dissector_target(f->dissector,
283                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
284                                                   f->mask);
285                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
286                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
287                          ntohl(mask->src));
288                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
289                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
290                          ntohl(key->src));
291
292                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
293                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
294                          ntohl(mask->dst));
295                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
296                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
297                          ntohl(key->dst));
298
299                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
300                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
301         } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
302                 struct flow_dissector_key_ipv6_addrs *key =
303                         skb_flow_dissector_target(f->dissector,
304                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
305                                                   f->key);
306                 struct flow_dissector_key_ipv6_addrs *mask =
307                         skb_flow_dissector_target(f->dissector,
308                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
309                                                   f->mask);
310
311                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
312                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
313                        &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
314                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
315                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
316                        &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
317
318                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
319                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
320                        &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
321                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
322                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
323                        &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
324
325                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
326                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
327         }
328
329         /* Enforce DMAC when offloading incoming tunneled flows.
330          * Flow counters require a match on the DMAC.
331          */
332         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
333         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
334         ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
335                                      dmac_47_16), priv->netdev->dev_addr);
336
337         /* let software handle IP fragments */
338         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
339         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
340
341         return 0;
342 }
343
344 static int __parse_cls_flower(struct mlx5e_priv *priv,
345                               struct mlx5_flow_spec *spec,
346                               struct tc_cls_flower_offload *f,
347                               u8 *min_inline)
348 {
349         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
350                                        outer_headers);
351         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
352                                        outer_headers);
353         u16 addr_type = 0;
354         u8 ip_proto = 0;
355
356         *min_inline = MLX5_INLINE_MODE_L2;
357
358         if (f->dissector->used_keys &
359             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
360               BIT(FLOW_DISSECTOR_KEY_BASIC) |
361               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
362               BIT(FLOW_DISSECTOR_KEY_VLAN) |
363               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
364               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
365               BIT(FLOW_DISSECTOR_KEY_PORTS) |
366               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
367               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
368               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
369               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
370               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL))) {
371                 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
372                             f->dissector->used_keys);
373                 return -EOPNOTSUPP;
374         }
375
376         if ((dissector_uses_key(f->dissector,
377                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
378              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
379              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
380             dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
381                 struct flow_dissector_key_control *key =
382                         skb_flow_dissector_target(f->dissector,
383                                                   FLOW_DISSECTOR_KEY_ENC_CONTROL,
384                                                   f->key);
385                 switch (key->addr_type) {
386                 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
387                 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
388                         if (parse_tunnel_attr(priv, spec, f))
389                                 return -EOPNOTSUPP;
390                         break;
391                 default:
392                         return -EOPNOTSUPP;
393                 }
394
395                 /* In decap flow, header pointers should point to the inner
396                  * headers, outer header were already set by parse_tunnel_attr
397                  */
398                 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
399                                          inner_headers);
400                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
401                                          inner_headers);
402         }
403
404         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
405                 struct flow_dissector_key_control *key =
406                         skb_flow_dissector_target(f->dissector,
407                                                   FLOW_DISSECTOR_KEY_CONTROL,
408                                                   f->key);
409
410                 struct flow_dissector_key_control *mask =
411                         skb_flow_dissector_target(f->dissector,
412                                                   FLOW_DISSECTOR_KEY_CONTROL,
413                                                   f->mask);
414                 addr_type = key->addr_type;
415
416                 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
417                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
418                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
419                                  key->flags & FLOW_DIS_IS_FRAGMENT);
420
421                         /* the HW doesn't need L3 inline to match on frag=no */
422                         if (key->flags & FLOW_DIS_IS_FRAGMENT)
423                                 *min_inline = MLX5_INLINE_MODE_IP;
424                 }
425         }
426
427         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
428                 struct flow_dissector_key_basic *key =
429                         skb_flow_dissector_target(f->dissector,
430                                                   FLOW_DISSECTOR_KEY_BASIC,
431                                                   f->key);
432                 struct flow_dissector_key_basic *mask =
433                         skb_flow_dissector_target(f->dissector,
434                                                   FLOW_DISSECTOR_KEY_BASIC,
435                                                   f->mask);
436                 ip_proto = key->ip_proto;
437
438                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
439                          ntohs(mask->n_proto));
440                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
441                          ntohs(key->n_proto));
442
443                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
444                          mask->ip_proto);
445                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
446                          key->ip_proto);
447
448                 if (mask->ip_proto)
449                         *min_inline = MLX5_INLINE_MODE_IP;
450         }
451
452         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
453                 struct flow_dissector_key_eth_addrs *key =
454                         skb_flow_dissector_target(f->dissector,
455                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
456                                                   f->key);
457                 struct flow_dissector_key_eth_addrs *mask =
458                         skb_flow_dissector_target(f->dissector,
459                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
460                                                   f->mask);
461
462                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
463                                              dmac_47_16),
464                                 mask->dst);
465                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
466                                              dmac_47_16),
467                                 key->dst);
468
469                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
470                                              smac_47_16),
471                                 mask->src);
472                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
473                                              smac_47_16),
474                                 key->src);
475         }
476
477         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
478                 struct flow_dissector_key_vlan *key =
479                         skb_flow_dissector_target(f->dissector,
480                                                   FLOW_DISSECTOR_KEY_VLAN,
481                                                   f->key);
482                 struct flow_dissector_key_vlan *mask =
483                         skb_flow_dissector_target(f->dissector,
484                                                   FLOW_DISSECTOR_KEY_VLAN,
485                                                   f->mask);
486                 if (mask->vlan_id || mask->vlan_priority) {
487                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
488                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
489
490                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
491                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
492
493                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
494                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
495                 }
496         }
497
498         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
499                 struct flow_dissector_key_ipv4_addrs *key =
500                         skb_flow_dissector_target(f->dissector,
501                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
502                                                   f->key);
503                 struct flow_dissector_key_ipv4_addrs *mask =
504                         skb_flow_dissector_target(f->dissector,
505                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
506                                                   f->mask);
507
508                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
509                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
510                        &mask->src, sizeof(mask->src));
511                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
512                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
513                        &key->src, sizeof(key->src));
514                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
515                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
516                        &mask->dst, sizeof(mask->dst));
517                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
518                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
519                        &key->dst, sizeof(key->dst));
520
521                 if (mask->src || mask->dst)
522                         *min_inline = MLX5_INLINE_MODE_IP;
523         }
524
525         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
526                 struct flow_dissector_key_ipv6_addrs *key =
527                         skb_flow_dissector_target(f->dissector,
528                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
529                                                   f->key);
530                 struct flow_dissector_key_ipv6_addrs *mask =
531                         skb_flow_dissector_target(f->dissector,
532                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
533                                                   f->mask);
534
535                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
536                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
537                        &mask->src, sizeof(mask->src));
538                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
539                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
540                        &key->src, sizeof(key->src));
541
542                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
543                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
544                        &mask->dst, sizeof(mask->dst));
545                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
546                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
547                        &key->dst, sizeof(key->dst));
548
549                 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
550                     ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
551                         *min_inline = MLX5_INLINE_MODE_IP;
552         }
553
554         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
555                 struct flow_dissector_key_ports *key =
556                         skb_flow_dissector_target(f->dissector,
557                                                   FLOW_DISSECTOR_KEY_PORTS,
558                                                   f->key);
559                 struct flow_dissector_key_ports *mask =
560                         skb_flow_dissector_target(f->dissector,
561                                                   FLOW_DISSECTOR_KEY_PORTS,
562                                                   f->mask);
563                 switch (ip_proto) {
564                 case IPPROTO_TCP:
565                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
566                                  tcp_sport, ntohs(mask->src));
567                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
568                                  tcp_sport, ntohs(key->src));
569
570                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
571                                  tcp_dport, ntohs(mask->dst));
572                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
573                                  tcp_dport, ntohs(key->dst));
574                         break;
575
576                 case IPPROTO_UDP:
577                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
578                                  udp_sport, ntohs(mask->src));
579                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
580                                  udp_sport, ntohs(key->src));
581
582                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
583                                  udp_dport, ntohs(mask->dst));
584                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
585                                  udp_dport, ntohs(key->dst));
586                         break;
587                 default:
588                         netdev_err(priv->netdev,
589                                    "Only UDP and TCP transport are supported\n");
590                         return -EINVAL;
591                 }
592
593                 if (mask->src || mask->dst)
594                         *min_inline = MLX5_INLINE_MODE_TCP_UDP;
595         }
596
597         return 0;
598 }
599
600 static int parse_cls_flower(struct mlx5e_priv *priv,
601                             struct mlx5_flow_spec *spec,
602                             struct tc_cls_flower_offload *f)
603 {
604         struct mlx5_core_dev *dev = priv->mdev;
605         struct mlx5_eswitch *esw = dev->priv.eswitch;
606         struct mlx5_eswitch_rep *rep = priv->ppriv;
607         u8 min_inline;
608         int err;
609
610         err = __parse_cls_flower(priv, spec, f, &min_inline);
611
612         if (!err && esw->mode == SRIOV_OFFLOADS &&
613             rep->vport != FDB_UPLINK_VPORT) {
614                 if (min_inline > esw->offloads.inline_mode) {
615                         netdev_warn(priv->netdev,
616                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
617                                     min_inline, esw->offloads.inline_mode);
618                         return -EOPNOTSUPP;
619                 }
620         }
621
622         return err;
623 }
624
625 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
626                                 u32 *action, u32 *flow_tag)
627 {
628         const struct tc_action *a;
629         LIST_HEAD(actions);
630
631         if (tc_no_actions(exts))
632                 return -EINVAL;
633
634         *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
635         *action = 0;
636
637         tcf_exts_to_list(exts, &actions);
638         list_for_each_entry(a, &actions, list) {
639                 /* Only support a single action per rule */
640                 if (*action)
641                         return -EINVAL;
642
643                 if (is_tcf_gact_shot(a)) {
644                         *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
645                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
646                                                flow_table_properties_nic_receive.flow_counter))
647                                 *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
648                         continue;
649                 }
650
651                 if (is_tcf_skbedit_mark(a)) {
652                         u32 mark = tcf_skbedit_mark(a);
653
654                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
655                                 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
656                                             mark);
657                                 return -EINVAL;
658                         }
659
660                         *flow_tag = mark;
661                         *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
662                         continue;
663                 }
664
665                 return -EINVAL;
666         }
667
668         return 0;
669 }
670
671 static inline int cmp_encap_info(struct ip_tunnel_key *a,
672                                  struct ip_tunnel_key *b)
673 {
674         return memcmp(a, b, sizeof(*a));
675 }
676
677 static inline int hash_encap_info(struct ip_tunnel_key *key)
678 {
679         return jhash(key, sizeof(*key), 0);
680 }
681
682 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
683                                    struct net_device *mirred_dev,
684                                    struct net_device **out_dev,
685                                    struct flowi4 *fl4,
686                                    struct neighbour **out_n,
687                                    int *out_ttl)
688 {
689         struct rtable *rt;
690         struct neighbour *n = NULL;
691
692 #if IS_ENABLED(CONFIG_INET)
693         int ret;
694
695         rt = ip_route_output_key(dev_net(mirred_dev), fl4);
696         ret = PTR_ERR_OR_ZERO(rt);
697         if (ret)
698                 return ret;
699 #else
700         return -EOPNOTSUPP;
701 #endif
702
703         if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev)) {
704                 pr_warn("%s: can't offload, devices not on same HW e-switch\n", __func__);
705                 ip_rt_put(rt);
706                 return -EOPNOTSUPP;
707         }
708
709         *out_ttl = ip4_dst_hoplimit(&rt->dst);
710         n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
711         ip_rt_put(rt);
712         if (!n)
713                 return -ENOMEM;
714
715         *out_n = n;
716         *out_dev = rt->dst.dev;
717
718         return 0;
719 }
720
721 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
722                                    struct net_device *mirred_dev,
723                                    struct net_device **out_dev,
724                                    struct flowi6 *fl6,
725                                    struct neighbour **out_n,
726                                    int *out_ttl)
727 {
728         struct neighbour *n = NULL;
729         struct dst_entry *dst;
730
731 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
732         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
733         int ret;
734
735         dst = ip6_route_output(dev_net(mirred_dev), NULL, fl6);
736         if (dst->error) {
737                 ret = dst->error;
738                 dst_release(dst);
739                 return ret;
740         }
741
742         *out_ttl = ip6_dst_hoplimit(dst);
743
744         /* if the egress device isn't on the same HW e-switch, we use the uplink */
745         if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
746                 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
747         else
748                 *out_dev = dst->dev;
749 #else
750         return -EOPNOTSUPP;
751 #endif
752
753         n = dst_neigh_lookup(dst, &fl6->daddr);
754         dst_release(dst);
755         if (!n)
756                 return -ENOMEM;
757
758         *out_n = n;
759         return 0;
760 }
761
762 static int gen_vxlan_header_ipv4(struct net_device *out_dev,
763                                  char buf[],
764                                  unsigned char h_dest[ETH_ALEN],
765                                  int ttl,
766                                  __be32 daddr,
767                                  __be32 saddr,
768                                  __be16 udp_dst_port,
769                                  __be32 vx_vni)
770 {
771         int encap_size = VXLAN_HLEN + sizeof(struct iphdr) + ETH_HLEN;
772         struct ethhdr *eth = (struct ethhdr *)buf;
773         struct iphdr  *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
774         struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
775         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
776
777         memset(buf, 0, encap_size);
778
779         ether_addr_copy(eth->h_dest, h_dest);
780         ether_addr_copy(eth->h_source, out_dev->dev_addr);
781         eth->h_proto = htons(ETH_P_IP);
782
783         ip->daddr = daddr;
784         ip->saddr = saddr;
785
786         ip->ttl = ttl;
787         ip->protocol = IPPROTO_UDP;
788         ip->version = 0x4;
789         ip->ihl = 0x5;
790
791         udp->dest = udp_dst_port;
792         vxh->vx_flags = VXLAN_HF_VNI;
793         vxh->vx_vni = vxlan_vni_field(vx_vni);
794
795         return encap_size;
796 }
797
798 static int gen_vxlan_header_ipv6(struct net_device *out_dev,
799                                  char buf[],
800                                  unsigned char h_dest[ETH_ALEN],
801                                  int ttl,
802                                  struct in6_addr *daddr,
803                                  struct in6_addr *saddr,
804                                  __be16 udp_dst_port,
805                                  __be32 vx_vni)
806 {
807         int encap_size = VXLAN_HLEN + sizeof(struct ipv6hdr) + ETH_HLEN;
808         struct ethhdr *eth = (struct ethhdr *)buf;
809         struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
810         struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
811         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
812
813         memset(buf, 0, encap_size);
814
815         ether_addr_copy(eth->h_dest, h_dest);
816         ether_addr_copy(eth->h_source, out_dev->dev_addr);
817         eth->h_proto = htons(ETH_P_IPV6);
818
819         ip6_flow_hdr(ip6h, 0, 0);
820         /* the HW fills up ipv6 payload len */
821         ip6h->nexthdr     = IPPROTO_UDP;
822         ip6h->hop_limit   = ttl;
823         ip6h->daddr       = *daddr;
824         ip6h->saddr       = *saddr;
825
826         udp->dest = udp_dst_port;
827         vxh->vx_flags = VXLAN_HF_VNI;
828         vxh->vx_vni = vxlan_vni_field(vx_vni);
829
830         return encap_size;
831 }
832
833 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
834                                           struct net_device *mirred_dev,
835                                           struct mlx5_encap_entry *e,
836                                           struct net_device **out_dev)
837 {
838         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
839         struct ip_tunnel_key *tun_key = &e->tun_info.key;
840         int encap_size, ttl, err;
841         struct neighbour *n = NULL;
842         struct flowi4 fl4 = {};
843         char *encap_header;
844
845         encap_header = kzalloc(max_encap_size, GFP_KERNEL);
846         if (!encap_header)
847                 return -ENOMEM;
848
849         switch (e->tunnel_type) {
850         case MLX5_HEADER_TYPE_VXLAN:
851                 fl4.flowi4_proto = IPPROTO_UDP;
852                 fl4.fl4_dport = tun_key->tp_dst;
853                 break;
854         default:
855                 err = -EOPNOTSUPP;
856                 goto out;
857         }
858         fl4.flowi4_tos = tun_key->tos;
859         fl4.daddr = tun_key->u.ipv4.dst;
860         fl4.saddr = tun_key->u.ipv4.src;
861
862         err = mlx5e_route_lookup_ipv4(priv, mirred_dev, out_dev,
863                                       &fl4, &n, &ttl);
864         if (err)
865                 goto out;
866
867         if (!(n->nud_state & NUD_VALID)) {
868                 pr_warn("%s: can't offload, neighbour to %pI4 invalid\n", __func__, &fl4.daddr);
869                 err = -EOPNOTSUPP;
870                 goto out;
871         }
872
873         e->n = n;
874         e->out_dev = *out_dev;
875
876         neigh_ha_snapshot(e->h_dest, n, *out_dev);
877
878         switch (e->tunnel_type) {
879         case MLX5_HEADER_TYPE_VXLAN:
880                 encap_size = gen_vxlan_header_ipv4(*out_dev, encap_header,
881                                                    e->h_dest, ttl,
882                                                    fl4.daddr,
883                                                    fl4.saddr, tun_key->tp_dst,
884                                                    tunnel_id_to_key32(tun_key->tun_id));
885                 break;
886         default:
887                 err = -EOPNOTSUPP;
888                 goto out;
889         }
890
891         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
892                                encap_size, encap_header, &e->encap_id);
893 out:
894         if (err && n)
895                 neigh_release(n);
896         kfree(encap_header);
897         return err;
898 }
899
900 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
901                                           struct net_device *mirred_dev,
902                                           struct mlx5_encap_entry *e,
903                                           struct net_device **out_dev)
904
905 {
906         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
907         struct ip_tunnel_key *tun_key = &e->tun_info.key;
908         int encap_size, err, ttl = 0;
909         struct neighbour *n = NULL;
910         struct flowi6 fl6 = {};
911         char *encap_header;
912
913         encap_header = kzalloc(max_encap_size, GFP_KERNEL);
914         if (!encap_header)
915                 return -ENOMEM;
916
917         switch (e->tunnel_type) {
918         case MLX5_HEADER_TYPE_VXLAN:
919                 fl6.flowi6_proto = IPPROTO_UDP;
920                 fl6.fl6_dport = tun_key->tp_dst;
921                 break;
922         default:
923                 err = -EOPNOTSUPP;
924                 goto out;
925         }
926
927         fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
928         fl6.daddr = tun_key->u.ipv6.dst;
929         fl6.saddr = tun_key->u.ipv6.src;
930
931         err = mlx5e_route_lookup_ipv6(priv, mirred_dev, out_dev,
932                                       &fl6, &n, &ttl);
933         if (err)
934                 goto out;
935
936         if (!(n->nud_state & NUD_VALID)) {
937                 pr_warn("%s: can't offload, neighbour to %pI6 invalid\n", __func__, &fl6.daddr);
938                 err = -EOPNOTSUPP;
939                 goto out;
940         }
941
942         e->n = n;
943         e->out_dev = *out_dev;
944
945         neigh_ha_snapshot(e->h_dest, n, *out_dev);
946
947         switch (e->tunnel_type) {
948         case MLX5_HEADER_TYPE_VXLAN:
949                 encap_size = gen_vxlan_header_ipv6(*out_dev, encap_header,
950                                                    e->h_dest, ttl,
951                                                    &fl6.daddr,
952                                                    &fl6.saddr, tun_key->tp_dst,
953                                                    tunnel_id_to_key32(tun_key->tun_id));
954                 break;
955         default:
956                 err = -EOPNOTSUPP;
957                 goto out;
958         }
959
960         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
961                                encap_size, encap_header, &e->encap_id);
962 out:
963         if (err && n)
964                 neigh_release(n);
965         kfree(encap_header);
966         return err;
967 }
968
969 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
970                               struct ip_tunnel_info *tun_info,
971                               struct net_device *mirred_dev,
972                               struct mlx5_esw_flow_attr *attr)
973 {
974         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
975         unsigned short family = ip_tunnel_info_af(tun_info);
976         struct ip_tunnel_key *key = &tun_info->key;
977         struct mlx5_encap_entry *e;
978         struct net_device *out_dev;
979         int tunnel_type, err = -EOPNOTSUPP;
980         uintptr_t hash_key;
981         bool found = false;
982
983         /* udp dst port must be set */
984         if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
985                 goto vxlan_encap_offload_err;
986
987         /* setting udp src port isn't supported */
988         if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
989 vxlan_encap_offload_err:
990                 netdev_warn(priv->netdev,
991                             "must set udp dst port and not set udp src port\n");
992                 return -EOPNOTSUPP;
993         }
994
995         if (mlx5e_vxlan_lookup_port(priv, be16_to_cpu(key->tp_dst)) &&
996             MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
997                 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
998         } else {
999                 netdev_warn(priv->netdev,
1000                             "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
1001                 return -EOPNOTSUPP;
1002         }
1003
1004         hash_key = hash_encap_info(key);
1005
1006         hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
1007                                    encap_hlist, hash_key) {
1008                 if (!cmp_encap_info(&e->tun_info.key, key)) {
1009                         found = true;
1010                         break;
1011                 }
1012         }
1013
1014         if (found) {
1015                 attr->encap = e;
1016                 return 0;
1017         }
1018
1019         e = kzalloc(sizeof(*e), GFP_KERNEL);
1020         if (!e)
1021                 return -ENOMEM;
1022
1023         e->tun_info = *tun_info;
1024         e->tunnel_type = tunnel_type;
1025         INIT_LIST_HEAD(&e->flows);
1026
1027         if (family == AF_INET)
1028                 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e, &out_dev);
1029         else if (family == AF_INET6)
1030                 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e, &out_dev);
1031
1032         if (err)
1033                 goto out_err;
1034
1035         attr->encap = e;
1036         hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
1037
1038         return err;
1039
1040 out_err:
1041         kfree(e);
1042         return err;
1043 }
1044
1045 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1046                                 struct mlx5e_tc_flow *flow)
1047 {
1048         struct mlx5_esw_flow_attr *attr = flow->attr;
1049         struct ip_tunnel_info *info = NULL;
1050         const struct tc_action *a;
1051         LIST_HEAD(actions);
1052         bool encap = false;
1053         int err;
1054
1055         if (tc_no_actions(exts))
1056                 return -EINVAL;
1057
1058         memset(attr, 0, sizeof(*attr));
1059         attr->in_rep = priv->ppriv;
1060
1061         tcf_exts_to_list(exts, &actions);
1062         list_for_each_entry(a, &actions, list) {
1063                 if (is_tcf_gact_shot(a)) {
1064                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
1065                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1066                         continue;
1067                 }
1068
1069                 if (is_tcf_mirred_egress_redirect(a)) {
1070                         int ifindex = tcf_mirred_ifindex(a);
1071                         struct net_device *out_dev;
1072                         struct mlx5e_priv *out_priv;
1073
1074                         out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
1075
1076                         if (switchdev_port_same_parent_id(priv->netdev,
1077                                                           out_dev)) {
1078                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1079                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1080                                 out_priv = netdev_priv(out_dev);
1081                                 attr->out_rep = out_priv->ppriv;
1082                         } else if (encap) {
1083                                 err = mlx5e_attach_encap(priv, info,
1084                                                          out_dev, attr);
1085                                 if (err)
1086                                         return err;
1087                                 list_add(&flow->encap, &attr->encap->flows);
1088                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1089                                         MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1090                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1091                                 out_priv = netdev_priv(attr->encap->out_dev);
1092                                 attr->out_rep = out_priv->ppriv;
1093                         } else {
1094                                 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
1095                                        priv->netdev->name, out_dev->name);
1096                                 return -EINVAL;
1097                         }
1098                         continue;
1099                 }
1100
1101                 if (is_tcf_tunnel_set(a)) {
1102                         info = tcf_tunnel_info(a);
1103                         if (info)
1104                                 encap = true;
1105                         else
1106                                 return -EOPNOTSUPP;
1107                         continue;
1108                 }
1109
1110                 if (is_tcf_vlan(a)) {
1111                         if (tcf_vlan_action(a) == VLAN_F_POP) {
1112                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
1113                         } else if (tcf_vlan_action(a) == VLAN_F_PUSH) {
1114                                 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
1115                                         return -EOPNOTSUPP;
1116
1117                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
1118                                 attr->vlan = tcf_vlan_push_vid(a);
1119                         }
1120                         continue;
1121                 }
1122
1123                 if (is_tcf_tunnel_release(a)) {
1124                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
1125                         continue;
1126                 }
1127
1128                 return -EINVAL;
1129         }
1130         return 0;
1131 }
1132
1133 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
1134                            struct tc_cls_flower_offload *f)
1135 {
1136         struct mlx5e_tc_table *tc = &priv->fs.tc;
1137         int err = 0;
1138         bool fdb_flow = false;
1139         u32 flow_tag, action;
1140         struct mlx5e_tc_flow *flow;
1141         struct mlx5_flow_spec *spec;
1142         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1143
1144         if (esw && esw->mode == SRIOV_OFFLOADS)
1145                 fdb_flow = true;
1146
1147         if (fdb_flow)
1148                 flow = kzalloc(sizeof(*flow) +
1149                                sizeof(struct mlx5_esw_flow_attr),
1150                                GFP_KERNEL);
1151         else
1152                 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
1153
1154         spec = mlx5_vzalloc(sizeof(*spec));
1155         if (!spec || !flow) {
1156                 err = -ENOMEM;
1157                 goto err_free;
1158         }
1159
1160         flow->cookie = f->cookie;
1161
1162         err = parse_cls_flower(priv, spec, f);
1163         if (err < 0)
1164                 goto err_free;
1165
1166         if (fdb_flow) {
1167                 flow->attr  = (struct mlx5_esw_flow_attr *)(flow + 1);
1168                 err = parse_tc_fdb_actions(priv, f->exts, flow);
1169                 if (err < 0)
1170                         goto err_free;
1171                 flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
1172         } else {
1173                 err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
1174                 if (err < 0)
1175                         goto err_free;
1176                 flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
1177         }
1178
1179         if (IS_ERR(flow->rule)) {
1180                 err = PTR_ERR(flow->rule);
1181                 goto err_del_rule;
1182         }
1183
1184         err = rhashtable_insert_fast(&tc->ht, &flow->node,
1185                                      tc->ht_params);
1186         if (err)
1187                 goto err_del_rule;
1188
1189         goto out;
1190
1191 err_del_rule:
1192         mlx5e_tc_del_flow(priv, flow);
1193
1194 err_free:
1195         kfree(flow);
1196 out:
1197         kvfree(spec);
1198         return err;
1199 }
1200
1201 int mlx5e_delete_flower(struct mlx5e_priv *priv,
1202                         struct tc_cls_flower_offload *f)
1203 {
1204         struct mlx5e_tc_flow *flow;
1205         struct mlx5e_tc_table *tc = &priv->fs.tc;
1206
1207         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
1208                                       tc->ht_params);
1209         if (!flow)
1210                 return -EINVAL;
1211
1212         rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
1213
1214         mlx5e_tc_del_flow(priv, flow);
1215
1216
1217         kfree(flow);
1218
1219         return 0;
1220 }
1221
1222 int mlx5e_stats_flower(struct mlx5e_priv *priv,
1223                        struct tc_cls_flower_offload *f)
1224 {
1225         struct mlx5e_tc_table *tc = &priv->fs.tc;
1226         struct mlx5e_tc_flow *flow;
1227         struct tc_action *a;
1228         struct mlx5_fc *counter;
1229         LIST_HEAD(actions);
1230         u64 bytes;
1231         u64 packets;
1232         u64 lastuse;
1233
1234         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
1235                                       tc->ht_params);
1236         if (!flow)
1237                 return -EINVAL;
1238
1239         counter = mlx5_flow_rule_counter(flow->rule);
1240         if (!counter)
1241                 return 0;
1242
1243         mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
1244
1245         tcf_exts_to_list(f->exts, &actions);
1246         list_for_each_entry(a, &actions, list)
1247                 tcf_action_stats_update(a, bytes, packets, lastuse);
1248
1249         return 0;
1250 }
1251
1252 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
1253         .head_offset = offsetof(struct mlx5e_tc_flow, node),
1254         .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
1255         .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
1256         .automatic_shrinking = true,
1257 };
1258
1259 int mlx5e_tc_init(struct mlx5e_priv *priv)
1260 {
1261         struct mlx5e_tc_table *tc = &priv->fs.tc;
1262
1263         tc->ht_params = mlx5e_tc_flow_ht_params;
1264         return rhashtable_init(&tc->ht, &tc->ht_params);
1265 }
1266
1267 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
1268 {
1269         struct mlx5e_tc_flow *flow = ptr;
1270         struct mlx5e_priv *priv = arg;
1271
1272         mlx5e_tc_del_flow(priv, flow);
1273         kfree(flow);
1274 }
1275
1276 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
1277 {
1278         struct mlx5e_tc_table *tc = &priv->fs.tc;
1279
1280         rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
1281
1282         if (!IS_ERR_OR_NULL(tc->t)) {
1283                 mlx5_destroy_flow_table(tc->t);
1284                 tc->t = NULL;
1285         }
1286 }