GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_tc_flower.c
1 /*
2  * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_pedit.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_vlan.h>
39
40 #include "cxgb4.h"
41 #include "cxgb4_filter.h"
42 #include "cxgb4_tc_flower.h"
43
44 #define STATS_CHECK_PERIOD (HZ / 2)
45
46 static struct ch_tc_pedit_fields pedits[] = {
47         PEDIT_FIELDS(ETH_, DMAC_31_0, 4, dmac, 0),
48         PEDIT_FIELDS(ETH_, DMAC_47_32, 2, dmac, 4),
49         PEDIT_FIELDS(ETH_, SMAC_15_0, 2, smac, 0),
50         PEDIT_FIELDS(ETH_, SMAC_47_16, 4, smac, 2),
51         PEDIT_FIELDS(IP4_, SRC, 4, nat_fip, 0),
52         PEDIT_FIELDS(IP4_, DST, 4, nat_lip, 0),
53         PEDIT_FIELDS(IP6_, SRC_31_0, 4, nat_fip, 0),
54         PEDIT_FIELDS(IP6_, SRC_63_32, 4, nat_fip, 4),
55         PEDIT_FIELDS(IP6_, SRC_95_64, 4, nat_fip, 8),
56         PEDIT_FIELDS(IP6_, SRC_127_96, 4, nat_fip, 12),
57         PEDIT_FIELDS(IP6_, DST_31_0, 4, nat_lip, 0),
58         PEDIT_FIELDS(IP6_, DST_63_32, 4, nat_lip, 4),
59         PEDIT_FIELDS(IP6_, DST_95_64, 4, nat_lip, 8),
60         PEDIT_FIELDS(IP6_, DST_127_96, 4, nat_lip, 12),
61         PEDIT_FIELDS(TCP_, SPORT, 2, nat_fport, 0),
62         PEDIT_FIELDS(TCP_, DPORT, 2, nat_lport, 0),
63         PEDIT_FIELDS(UDP_, SPORT, 2, nat_fport, 0),
64         PEDIT_FIELDS(UDP_, DPORT, 2, nat_lport, 0),
65 };
66
67 static struct ch_tc_flower_entry *allocate_flower_entry(void)
68 {
69         struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
70         if (new)
71                 spin_lock_init(&new->lock);
72         return new;
73 }
74
75 /* Must be called with either RTNL or rcu_read_lock */
76 static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
77                                                    unsigned long flower_cookie)
78 {
79         return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
80                                       adap->flower_ht_params);
81 }
82
83 static void cxgb4_process_flow_match(struct net_device *dev,
84                                      struct tc_cls_flower_offload *cls,
85                                      struct ch_filter_specification *fs)
86 {
87         u16 addr_type = 0;
88
89         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
90                 struct flow_dissector_key_control *key =
91                         skb_flow_dissector_target(cls->dissector,
92                                                   FLOW_DISSECTOR_KEY_CONTROL,
93                                                   cls->key);
94
95                 addr_type = key->addr_type;
96         }
97
98         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
99                 struct flow_dissector_key_basic *key =
100                         skb_flow_dissector_target(cls->dissector,
101                                                   FLOW_DISSECTOR_KEY_BASIC,
102                                                   cls->key);
103                 struct flow_dissector_key_basic *mask =
104                         skb_flow_dissector_target(cls->dissector,
105                                                   FLOW_DISSECTOR_KEY_BASIC,
106                                                   cls->mask);
107                 u16 ethtype_key = ntohs(key->n_proto);
108                 u16 ethtype_mask = ntohs(mask->n_proto);
109
110                 if (ethtype_key == ETH_P_ALL) {
111                         ethtype_key = 0;
112                         ethtype_mask = 0;
113                 }
114
115                 if (ethtype_key == ETH_P_IPV6)
116                         fs->type = 1;
117
118                 fs->val.ethtype = ethtype_key;
119                 fs->mask.ethtype = ethtype_mask;
120                 fs->val.proto = key->ip_proto;
121                 fs->mask.proto = mask->ip_proto;
122         }
123
124         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
125                 struct flow_dissector_key_ipv4_addrs *key =
126                         skb_flow_dissector_target(cls->dissector,
127                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
128                                                   cls->key);
129                 struct flow_dissector_key_ipv4_addrs *mask =
130                         skb_flow_dissector_target(cls->dissector,
131                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
132                                                   cls->mask);
133                 fs->type = 0;
134                 memcpy(&fs->val.lip[0], &key->dst, sizeof(key->dst));
135                 memcpy(&fs->val.fip[0], &key->src, sizeof(key->src));
136                 memcpy(&fs->mask.lip[0], &mask->dst, sizeof(mask->dst));
137                 memcpy(&fs->mask.fip[0], &mask->src, sizeof(mask->src));
138
139                 /* also initialize nat_lip/fip to same values */
140                 memcpy(&fs->nat_lip[0], &key->dst, sizeof(key->dst));
141                 memcpy(&fs->nat_fip[0], &key->src, sizeof(key->src));
142
143         }
144
145         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
146                 struct flow_dissector_key_ipv6_addrs *key =
147                         skb_flow_dissector_target(cls->dissector,
148                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
149                                                   cls->key);
150                 struct flow_dissector_key_ipv6_addrs *mask =
151                         skb_flow_dissector_target(cls->dissector,
152                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
153                                                   cls->mask);
154
155                 fs->type = 1;
156                 memcpy(&fs->val.lip[0], key->dst.s6_addr, sizeof(key->dst));
157                 memcpy(&fs->val.fip[0], key->src.s6_addr, sizeof(key->src));
158                 memcpy(&fs->mask.lip[0], mask->dst.s6_addr, sizeof(mask->dst));
159                 memcpy(&fs->mask.fip[0], mask->src.s6_addr, sizeof(mask->src));
160
161                 /* also initialize nat_lip/fip to same values */
162                 memcpy(&fs->nat_lip[0], key->dst.s6_addr, sizeof(key->dst));
163                 memcpy(&fs->nat_fip[0], key->src.s6_addr, sizeof(key->src));
164         }
165
166         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
167                 struct flow_dissector_key_ports *key, *mask;
168
169                 key = skb_flow_dissector_target(cls->dissector,
170                                                 FLOW_DISSECTOR_KEY_PORTS,
171                                                 cls->key);
172                 mask = skb_flow_dissector_target(cls->dissector,
173                                                  FLOW_DISSECTOR_KEY_PORTS,
174                                                  cls->mask);
175                 fs->val.lport = cpu_to_be16(key->dst);
176                 fs->mask.lport = cpu_to_be16(mask->dst);
177                 fs->val.fport = cpu_to_be16(key->src);
178                 fs->mask.fport = cpu_to_be16(mask->src);
179
180                 /* also initialize nat_lport/fport to same values */
181                 fs->nat_lport = cpu_to_be16(key->dst);
182                 fs->nat_fport = cpu_to_be16(key->src);
183         }
184
185         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
186                 struct flow_dissector_key_ip *key, *mask;
187
188                 key = skb_flow_dissector_target(cls->dissector,
189                                                 FLOW_DISSECTOR_KEY_IP,
190                                                 cls->key);
191                 mask = skb_flow_dissector_target(cls->dissector,
192                                                  FLOW_DISSECTOR_KEY_IP,
193                                                  cls->mask);
194                 fs->val.tos = key->tos;
195                 fs->mask.tos = mask->tos;
196         }
197
198         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
199                 struct flow_dissector_key_keyid *key, *mask;
200
201                 key = skb_flow_dissector_target(cls->dissector,
202                                                 FLOW_DISSECTOR_KEY_ENC_KEYID,
203                                                 cls->key);
204                 mask = skb_flow_dissector_target(cls->dissector,
205                                                  FLOW_DISSECTOR_KEY_ENC_KEYID,
206                                                  cls->mask);
207                 fs->val.vni = be32_to_cpu(key->keyid);
208                 fs->mask.vni = be32_to_cpu(mask->keyid);
209                 if (fs->mask.vni) {
210                         fs->val.encap_vld = 1;
211                         fs->mask.encap_vld = 1;
212                 }
213         }
214
215         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
216                 struct flow_dissector_key_vlan *key, *mask;
217                 u16 vlan_tci, vlan_tci_mask;
218
219                 key = skb_flow_dissector_target(cls->dissector,
220                                                 FLOW_DISSECTOR_KEY_VLAN,
221                                                 cls->key);
222                 mask = skb_flow_dissector_target(cls->dissector,
223                                                  FLOW_DISSECTOR_KEY_VLAN,
224                                                  cls->mask);
225                 vlan_tci = key->vlan_id | (key->vlan_priority <<
226                                            VLAN_PRIO_SHIFT);
227                 vlan_tci_mask = mask->vlan_id | (mask->vlan_priority <<
228                                                  VLAN_PRIO_SHIFT);
229                 fs->val.ivlan = vlan_tci;
230                 fs->mask.ivlan = vlan_tci_mask;
231
232                 fs->val.ivlan_vld = 1;
233                 fs->mask.ivlan_vld = 1;
234
235                 /* Chelsio adapters use ivlan_vld bit to match vlan packets
236                  * as 802.1Q. Also, when vlan tag is present in packets,
237                  * ethtype match is used then to match on ethtype of inner
238                  * header ie. the header following the vlan header.
239                  * So, set the ivlan_vld based on ethtype info supplied by
240                  * TC for vlan packets if its 802.1Q. And then reset the
241                  * ethtype value else, hw will try to match the supplied
242                  * ethtype value with ethtype of inner header.
243                  */
244                 if (fs->val.ethtype == ETH_P_8021Q) {
245                         fs->val.ethtype = 0;
246                         fs->mask.ethtype = 0;
247                 }
248         }
249
250         /* Match only packets coming from the ingress port where this
251          * filter will be created.
252          */
253         fs->val.iport = netdev2pinfo(dev)->port_id;
254         fs->mask.iport = ~0;
255 }
256
257 static int cxgb4_validate_flow_match(struct net_device *dev,
258                                      struct tc_cls_flower_offload *cls)
259 {
260         u16 ethtype_mask = 0;
261         u16 ethtype_key = 0;
262
263         if (cls->dissector->used_keys &
264             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
265               BIT(FLOW_DISSECTOR_KEY_BASIC) |
266               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
267               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
268               BIT(FLOW_DISSECTOR_KEY_PORTS) |
269               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
270               BIT(FLOW_DISSECTOR_KEY_VLAN) |
271               BIT(FLOW_DISSECTOR_KEY_IP))) {
272                 netdev_warn(dev, "Unsupported key used: 0x%x\n",
273                             cls->dissector->used_keys);
274                 return -EOPNOTSUPP;
275         }
276
277         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
278                 struct flow_dissector_key_basic *key =
279                         skb_flow_dissector_target(cls->dissector,
280                                                   FLOW_DISSECTOR_KEY_BASIC,
281                                                   cls->key);
282                 struct flow_dissector_key_basic *mask =
283                         skb_flow_dissector_target(cls->dissector,
284                                                   FLOW_DISSECTOR_KEY_BASIC,
285                                                   cls->mask);
286                 ethtype_key = ntohs(key->n_proto);
287                 ethtype_mask = ntohs(mask->n_proto);
288         }
289
290         if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
291                 u16 eth_ip_type = ethtype_key & ethtype_mask;
292                 struct flow_dissector_key_ip *mask;
293
294                 if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
295                         netdev_err(dev, "IP Key supported only with IPv4/v6");
296                         return -EINVAL;
297                 }
298
299                 mask = skb_flow_dissector_target(cls->dissector,
300                                                  FLOW_DISSECTOR_KEY_IP,
301                                                  cls->mask);
302                 if (mask->ttl) {
303                         netdev_warn(dev, "ttl match unsupported for offload");
304                         return -EOPNOTSUPP;
305                 }
306         }
307
308         return 0;
309 }
310
311 static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
312                           u8 field)
313 {
314         u32 set_val = val & ~mask;
315         u32 offset = 0;
316         u8 size = 1;
317         int i;
318
319         for (i = 0; i < ARRAY_SIZE(pedits); i++) {
320                 if (pedits[i].field == field) {
321                         offset = pedits[i].offset;
322                         size = pedits[i].size;
323                         break;
324                 }
325         }
326         memcpy((u8 *)fs + offset, &set_val, size);
327 }
328
329 static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
330                                 u32 mask, u32 offset, u8 htype)
331 {
332         switch (htype) {
333         case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
334                 switch (offset) {
335                 case PEDIT_ETH_DMAC_31_0:
336                         fs->newdmac = 1;
337                         offload_pedit(fs, val, mask, ETH_DMAC_31_0);
338                         break;
339                 case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
340                         if (~mask & PEDIT_ETH_DMAC_MASK)
341                                 offload_pedit(fs, val, mask, ETH_DMAC_47_32);
342                         else
343                                 offload_pedit(fs, val >> 16, mask >> 16,
344                                               ETH_SMAC_15_0);
345                         break;
346                 case PEDIT_ETH_SMAC_47_16:
347                         fs->newsmac = 1;
348                         offload_pedit(fs, val, mask, ETH_SMAC_47_16);
349                 }
350                 break;
351         case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
352                 switch (offset) {
353                 case PEDIT_IP4_SRC:
354                         offload_pedit(fs, val, mask, IP4_SRC);
355                         break;
356                 case PEDIT_IP4_DST:
357                         offload_pedit(fs, val, mask, IP4_DST);
358                 }
359                 fs->nat_mode = NAT_MODE_ALL;
360                 break;
361         case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
362                 switch (offset) {
363                 case PEDIT_IP6_SRC_31_0:
364                         offload_pedit(fs, val, mask, IP6_SRC_31_0);
365                         break;
366                 case PEDIT_IP6_SRC_63_32:
367                         offload_pedit(fs, val, mask, IP6_SRC_63_32);
368                         break;
369                 case PEDIT_IP6_SRC_95_64:
370                         offload_pedit(fs, val, mask, IP6_SRC_95_64);
371                         break;
372                 case PEDIT_IP6_SRC_127_96:
373                         offload_pedit(fs, val, mask, IP6_SRC_127_96);
374                         break;
375                 case PEDIT_IP6_DST_31_0:
376                         offload_pedit(fs, val, mask, IP6_DST_31_0);
377                         break;
378                 case PEDIT_IP6_DST_63_32:
379                         offload_pedit(fs, val, mask, IP6_DST_63_32);
380                         break;
381                 case PEDIT_IP6_DST_95_64:
382                         offload_pedit(fs, val, mask, IP6_DST_95_64);
383                         break;
384                 case PEDIT_IP6_DST_127_96:
385                         offload_pedit(fs, val, mask, IP6_DST_127_96);
386                 }
387                 fs->nat_mode = NAT_MODE_ALL;
388                 break;
389         case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
390                 switch (offset) {
391                 case PEDIT_TCP_SPORT_DPORT:
392                         if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
393                                 offload_pedit(fs, cpu_to_be32(val) >> 16,
394                                               cpu_to_be32(mask) >> 16,
395                                               TCP_SPORT);
396                         else
397                                 offload_pedit(fs, cpu_to_be32(val),
398                                               cpu_to_be32(mask), TCP_DPORT);
399                 }
400                 fs->nat_mode = NAT_MODE_ALL;
401                 break;
402         case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
403                 switch (offset) {
404                 case PEDIT_UDP_SPORT_DPORT:
405                         if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
406                                 offload_pedit(fs, cpu_to_be32(val) >> 16,
407                                               cpu_to_be32(mask) >> 16,
408                                               UDP_SPORT);
409                         else
410                                 offload_pedit(fs, cpu_to_be32(val),
411                                               cpu_to_be32(mask), UDP_DPORT);
412                 }
413                 fs->nat_mode = NAT_MODE_ALL;
414         }
415 }
416
417 static void cxgb4_process_flow_actions(struct net_device *in,
418                                        struct tc_cls_flower_offload *cls,
419                                        struct ch_filter_specification *fs)
420 {
421         const struct tc_action *a;
422         int i;
423
424         tcf_exts_for_each_action(i, a, cls->exts) {
425                 if (is_tcf_gact_ok(a)) {
426                         fs->action = FILTER_PASS;
427                 } else if (is_tcf_gact_shot(a)) {
428                         fs->action = FILTER_DROP;
429                 } else if (is_tcf_mirred_egress_redirect(a)) {
430                         struct net_device *out = tcf_mirred_dev(a);
431                         struct port_info *pi = netdev_priv(out);
432
433                         fs->action = FILTER_SWITCH;
434                         fs->eport = pi->port_id;
435                 } else if (is_tcf_vlan(a)) {
436                         u32 vlan_action = tcf_vlan_action(a);
437                         u8 prio = tcf_vlan_push_prio(a);
438                         u16 vid = tcf_vlan_push_vid(a);
439                         u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
440
441                         switch (vlan_action) {
442                         case TCA_VLAN_ACT_POP:
443                                 fs->newvlan |= VLAN_REMOVE;
444                                 break;
445                         case TCA_VLAN_ACT_PUSH:
446                                 fs->newvlan |= VLAN_INSERT;
447                                 fs->vlan = vlan_tci;
448                                 break;
449                         case TCA_VLAN_ACT_MODIFY:
450                                 fs->newvlan |= VLAN_REWRITE;
451                                 fs->vlan = vlan_tci;
452                                 break;
453                         default:
454                                 break;
455                         }
456                 } else if (is_tcf_pedit(a)) {
457                         u32 mask, val, offset;
458                         int nkeys, i;
459                         u8 htype;
460
461                         nkeys = tcf_pedit_nkeys(a);
462                         for (i = 0; i < nkeys; i++) {
463                                 htype = tcf_pedit_htype(a, i);
464                                 mask = tcf_pedit_mask(a, i);
465                                 val = tcf_pedit_val(a, i);
466                                 offset = tcf_pedit_offset(a, i);
467
468                                 process_pedit_field(fs, val, mask, offset,
469                                                     htype);
470                         }
471                 }
472         }
473 }
474
475 static bool valid_l4_mask(u32 mask)
476 {
477         u16 hi, lo;
478
479         /* Either the upper 16-bits (SPORT) OR the lower
480          * 16-bits (DPORT) can be set, but NOT BOTH.
481          */
482         hi = (mask >> 16) & 0xFFFF;
483         lo = mask & 0xFFFF;
484
485         return hi && lo ? false : true;
486 }
487
488 static bool valid_pedit_action(struct net_device *dev,
489                                const struct tc_action *a)
490 {
491         u32 mask, offset;
492         u8 cmd, htype;
493         int nkeys, i;
494
495         nkeys = tcf_pedit_nkeys(a);
496         for (i = 0; i < nkeys; i++) {
497                 htype = tcf_pedit_htype(a, i);
498                 cmd = tcf_pedit_cmd(a, i);
499                 mask = tcf_pedit_mask(a, i);
500                 offset = tcf_pedit_offset(a, i);
501
502                 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET) {
503                         netdev_err(dev, "%s: Unsupported pedit cmd\n",
504                                    __func__);
505                         return false;
506                 }
507
508                 switch (htype) {
509                 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
510                         switch (offset) {
511                         case PEDIT_ETH_DMAC_31_0:
512                         case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
513                         case PEDIT_ETH_SMAC_47_16:
514                                 break;
515                         default:
516                                 netdev_err(dev, "%s: Unsupported pedit field\n",
517                                            __func__);
518                                 return false;
519                         }
520                         break;
521                 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
522                         switch (offset) {
523                         case PEDIT_IP4_SRC:
524                         case PEDIT_IP4_DST:
525                                 break;
526                         default:
527                                 netdev_err(dev, "%s: Unsupported pedit field\n",
528                                            __func__);
529                                 return false;
530                         }
531                         break;
532                 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
533                         switch (offset) {
534                         case PEDIT_IP6_SRC_31_0:
535                         case PEDIT_IP6_SRC_63_32:
536                         case PEDIT_IP6_SRC_95_64:
537                         case PEDIT_IP6_SRC_127_96:
538                         case PEDIT_IP6_DST_31_0:
539                         case PEDIT_IP6_DST_63_32:
540                         case PEDIT_IP6_DST_95_64:
541                         case PEDIT_IP6_DST_127_96:
542                                 break;
543                         default:
544                                 netdev_err(dev, "%s: Unsupported pedit field\n",
545                                            __func__);
546                                 return false;
547                         }
548                         break;
549                 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
550                         switch (offset) {
551                         case PEDIT_TCP_SPORT_DPORT:
552                                 if (!valid_l4_mask(~mask)) {
553                                         netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
554                                                    __func__);
555                                         return false;
556                                 }
557                                 break;
558                         default:
559                                 netdev_err(dev, "%s: Unsupported pedit field\n",
560                                            __func__);
561                                 return false;
562                         }
563                         break;
564                 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
565                         switch (offset) {
566                         case PEDIT_UDP_SPORT_DPORT:
567                                 if (!valid_l4_mask(~mask)) {
568                                         netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
569                                                    __func__);
570                                         return false;
571                                 }
572                                 break;
573                         default:
574                                 netdev_err(dev, "%s: Unsupported pedit field\n",
575                                            __func__);
576                                 return false;
577                         }
578                         break;
579                 default:
580                         netdev_err(dev, "%s: Unsupported pedit type\n",
581                                    __func__);
582                         return false;
583                 }
584         }
585         return true;
586 }
587
588 static int cxgb4_validate_flow_actions(struct net_device *dev,
589                                        struct tc_cls_flower_offload *cls)
590 {
591         const struct tc_action *a;
592         bool act_redir = false;
593         bool act_pedit = false;
594         bool act_vlan = false;
595         int i;
596
597         tcf_exts_for_each_action(i, a, cls->exts) {
598                 if (is_tcf_gact_ok(a)) {
599                         /* Do nothing */
600                 } else if (is_tcf_gact_shot(a)) {
601                         /* Do nothing */
602                 } else if (is_tcf_mirred_egress_redirect(a)) {
603                         struct adapter *adap = netdev2adap(dev);
604                         struct net_device *n_dev, *target_dev;
605                         unsigned int i;
606                         bool found = false;
607
608                         target_dev = tcf_mirred_dev(a);
609                         for_each_port(adap, i) {
610                                 n_dev = adap->port[i];
611                                 if (target_dev == n_dev) {
612                                         found = true;
613                                         break;
614                                 }
615                         }
616
617                         /* If interface doesn't belong to our hw, then
618                          * the provided output port is not valid
619                          */
620                         if (!found) {
621                                 netdev_err(dev, "%s: Out port invalid\n",
622                                            __func__);
623                                 return -EINVAL;
624                         }
625                         act_redir = true;
626                 } else if (is_tcf_vlan(a)) {
627                         u16 proto = be16_to_cpu(tcf_vlan_push_proto(a));
628                         u32 vlan_action = tcf_vlan_action(a);
629
630                         switch (vlan_action) {
631                         case TCA_VLAN_ACT_POP:
632                                 break;
633                         case TCA_VLAN_ACT_PUSH:
634                         case TCA_VLAN_ACT_MODIFY:
635                                 if (proto != ETH_P_8021Q) {
636                                         netdev_err(dev, "%s: Unsupported vlan proto\n",
637                                                    __func__);
638                                         return -EOPNOTSUPP;
639                                 }
640                                 break;
641                         default:
642                                 netdev_err(dev, "%s: Unsupported vlan action\n",
643                                            __func__);
644                                 return -EOPNOTSUPP;
645                         }
646                         act_vlan = true;
647                 } else if (is_tcf_pedit(a)) {
648                         bool pedit_valid = valid_pedit_action(dev, a);
649
650                         if (!pedit_valid)
651                                 return -EOPNOTSUPP;
652                         act_pedit = true;
653                 } else {
654                         netdev_err(dev, "%s: Unsupported action\n", __func__);
655                         return -EOPNOTSUPP;
656                 }
657         }
658
659         if ((act_pedit || act_vlan) && !act_redir) {
660                 netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
661                            __func__);
662                 return -EINVAL;
663         }
664
665         return 0;
666 }
667
668 int cxgb4_tc_flower_replace(struct net_device *dev,
669                             struct tc_cls_flower_offload *cls)
670 {
671         struct adapter *adap = netdev2adap(dev);
672         struct ch_tc_flower_entry *ch_flower;
673         struct ch_filter_specification *fs;
674         struct filter_ctx ctx;
675         int fidx;
676         int ret;
677
678         if (cxgb4_validate_flow_actions(dev, cls))
679                 return -EOPNOTSUPP;
680
681         if (cxgb4_validate_flow_match(dev, cls))
682                 return -EOPNOTSUPP;
683
684         ch_flower = allocate_flower_entry();
685         if (!ch_flower) {
686                 netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
687                 return -ENOMEM;
688         }
689
690         fs = &ch_flower->fs;
691         fs->hitcnts = 1;
692         cxgb4_process_flow_match(dev, cls, fs);
693         cxgb4_process_flow_actions(dev, cls, fs);
694
695         fs->hash = is_filter_exact_match(adap, fs);
696         if (fs->hash) {
697                 fidx = 0;
698         } else {
699                 fidx = cxgb4_get_free_ftid(dev, fs->type ? PF_INET6 : PF_INET);
700                 if (fidx < 0) {
701                         netdev_err(dev, "%s: No fidx for offload.\n", __func__);
702                         ret = -ENOMEM;
703                         goto free_entry;
704                 }
705         }
706
707         init_completion(&ctx.completion);
708         ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
709         if (ret) {
710                 netdev_err(dev, "%s: filter creation err %d\n",
711                            __func__, ret);
712                 goto free_entry;
713         }
714
715         /* Wait for reply */
716         ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
717         if (!ret) {
718                 ret = -ETIMEDOUT;
719                 goto free_entry;
720         }
721
722         ret = ctx.result;
723         /* Check if hw returned error for filter creation */
724         if (ret) {
725                 netdev_err(dev, "%s: filter creation err %d\n",
726                            __func__, ret);
727                 goto free_entry;
728         }
729
730         ch_flower->tc_flower_cookie = cls->cookie;
731         ch_flower->filter_id = ctx.tid;
732         ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
733                                      adap->flower_ht_params);
734         if (ret)
735                 goto del_filter;
736
737         return 0;
738
739 del_filter:
740         cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
741
742 free_entry:
743         kfree(ch_flower);
744         return ret;
745 }
746
747 int cxgb4_tc_flower_destroy(struct net_device *dev,
748                             struct tc_cls_flower_offload *cls)
749 {
750         struct adapter *adap = netdev2adap(dev);
751         struct ch_tc_flower_entry *ch_flower;
752         int ret;
753
754         ch_flower = ch_flower_lookup(adap, cls->cookie);
755         if (!ch_flower)
756                 return -ENOENT;
757
758         ret = cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
759         if (ret)
760                 goto err;
761
762         ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
763                                      adap->flower_ht_params);
764         if (ret) {
765                 netdev_err(dev, "Flow remove from rhashtable failed");
766                 goto err;
767         }
768         kfree_rcu(ch_flower, rcu);
769
770 err:
771         return ret;
772 }
773
774 static void ch_flower_stats_handler(struct work_struct *work)
775 {
776         struct adapter *adap = container_of(work, struct adapter,
777                                             flower_stats_work);
778         struct ch_tc_flower_entry *flower_entry;
779         struct ch_tc_flower_stats *ofld_stats;
780         struct rhashtable_iter iter;
781         u64 packets;
782         u64 bytes;
783         int ret;
784
785         rhashtable_walk_enter(&adap->flower_tbl, &iter);
786         do {
787                 rhashtable_walk_start(&iter);
788
789                 while ((flower_entry = rhashtable_walk_next(&iter)) &&
790                        !IS_ERR(flower_entry)) {
791                         ret = cxgb4_get_filter_counters(adap->port[0],
792                                                         flower_entry->filter_id,
793                                                         &packets, &bytes,
794                                                         flower_entry->fs.hash);
795                         if (!ret) {
796                                 spin_lock(&flower_entry->lock);
797                                 ofld_stats = &flower_entry->stats;
798
799                                 if (ofld_stats->prev_packet_count != packets) {
800                                         ofld_stats->prev_packet_count = packets;
801                                         ofld_stats->last_used = jiffies;
802                                 }
803                                 spin_unlock(&flower_entry->lock);
804                         }
805                 }
806
807                 rhashtable_walk_stop(&iter);
808
809         } while (flower_entry == ERR_PTR(-EAGAIN));
810         rhashtable_walk_exit(&iter);
811         mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
812 }
813
814 static void ch_flower_stats_cb(struct timer_list *t)
815 {
816         struct adapter *adap = from_timer(adap, t, flower_stats_timer);
817
818         schedule_work(&adap->flower_stats_work);
819 }
820
821 int cxgb4_tc_flower_stats(struct net_device *dev,
822                           struct tc_cls_flower_offload *cls)
823 {
824         struct adapter *adap = netdev2adap(dev);
825         struct ch_tc_flower_stats *ofld_stats;
826         struct ch_tc_flower_entry *ch_flower;
827         u64 packets;
828         u64 bytes;
829         int ret;
830
831         ch_flower = ch_flower_lookup(adap, cls->cookie);
832         if (!ch_flower) {
833                 ret = -ENOENT;
834                 goto err;
835         }
836
837         ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
838                                         &packets, &bytes,
839                                         ch_flower->fs.hash);
840         if (ret < 0)
841                 goto err;
842
843         spin_lock_bh(&ch_flower->lock);
844         ofld_stats = &ch_flower->stats;
845         if (ofld_stats->packet_count != packets) {
846                 if (ofld_stats->prev_packet_count != packets)
847                         ofld_stats->last_used = jiffies;
848                 tcf_exts_stats_update(cls->exts, bytes - ofld_stats->byte_count,
849                                       packets - ofld_stats->packet_count,
850                                       ofld_stats->last_used);
851
852                 ofld_stats->packet_count = packets;
853                 ofld_stats->byte_count = bytes;
854                 ofld_stats->prev_packet_count = packets;
855         }
856         spin_unlock_bh(&ch_flower->lock);
857         return 0;
858
859 err:
860         return ret;
861 }
862
863 static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
864         .nelem_hint = 384,
865         .head_offset = offsetof(struct ch_tc_flower_entry, node),
866         .key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
867         .key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
868         .max_size = 524288,
869         .min_size = 512,
870         .automatic_shrinking = true
871 };
872
873 int cxgb4_init_tc_flower(struct adapter *adap)
874 {
875         int ret;
876
877         if (adap->tc_flower_initialized)
878                 return -EEXIST;
879
880         adap->flower_ht_params = cxgb4_tc_flower_ht_params;
881         ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
882         if (ret)
883                 return ret;
884
885         INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
886         timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
887         mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
888         adap->tc_flower_initialized = true;
889         return 0;
890 }
891
892 void cxgb4_cleanup_tc_flower(struct adapter *adap)
893 {
894         if (!adap->tc_flower_initialized)
895                 return;
896
897         if (adap->flower_stats_timer.function)
898                 del_timer_sync(&adap->flower_stats_timer);
899         cancel_work_sync(&adap->flower_stats_work);
900         rhashtable_destroy(&adap->flower_tbl);
901         adap->tc_flower_initialized = false;
902 }