fba802a02a0392314d9d885b08008bdeaf78747d
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / netronome / nfp / flower / offload.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #include <linux/skbuff.h>
5 #include <net/devlink.h>
6 #include <net/pkt_cls.h>
7
8 #include "cmsg.h"
9 #include "main.h"
10 #include "../nfpcore/nfp_cpp.h"
11 #include "../nfpcore/nfp_nsp.h"
12 #include "../nfp_app.h"
13 #include "../nfp_main.h"
14 #include "../nfp_net.h"
15 #include "../nfp_port.h"
16
17 #define NFP_FLOWER_SUPPORTED_TCPFLAGS \
18         (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST | \
19          TCPHDR_PSH | TCPHDR_URG)
20
21 #define NFP_FLOWER_SUPPORTED_CTLFLAGS \
22         (FLOW_DIS_IS_FRAGMENT | \
23          FLOW_DIS_FIRST_FRAG)
24
25 #define NFP_FLOWER_WHITELIST_DISSECTOR \
26         (BIT(FLOW_DISSECTOR_KEY_CONTROL) | \
27          BIT(FLOW_DISSECTOR_KEY_BASIC) | \
28          BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | \
29          BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | \
30          BIT(FLOW_DISSECTOR_KEY_TCP) | \
31          BIT(FLOW_DISSECTOR_KEY_PORTS) | \
32          BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | \
33          BIT(FLOW_DISSECTOR_KEY_VLAN) | \
34          BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \
35          BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
36          BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
37          BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
38          BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \
39          BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \
40          BIT(FLOW_DISSECTOR_KEY_ENC_IP) | \
41          BIT(FLOW_DISSECTOR_KEY_MPLS) | \
42          BIT(FLOW_DISSECTOR_KEY_IP))
43
44 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR \
45         (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
46          BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \
47          BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
48          BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
49          BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \
50          BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \
51          BIT(FLOW_DISSECTOR_KEY_ENC_IP))
52
53 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R \
54         (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
55          BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS))
56
57 #define NFP_FLOWER_MERGE_FIELDS \
58         (NFP_FLOWER_LAYER_PORT | \
59          NFP_FLOWER_LAYER_MAC | \
60          NFP_FLOWER_LAYER_TP | \
61          NFP_FLOWER_LAYER_IPV4 | \
62          NFP_FLOWER_LAYER_IPV6)
63
64 struct nfp_flower_merge_check {
65         union {
66                 struct {
67                         __be16 tci;
68                         struct nfp_flower_mac_mpls l2;
69                         struct nfp_flower_tp_ports l4;
70                         union {
71                                 struct nfp_flower_ipv4 ipv4;
72                                 struct nfp_flower_ipv6 ipv6;
73                         };
74                 };
75                 unsigned long vals[8];
76         };
77 };
78
79 static int
80 nfp_flower_xmit_flow(struct nfp_app *app, struct nfp_fl_payload *nfp_flow,
81                      u8 mtype)
82 {
83         u32 meta_len, key_len, mask_len, act_len, tot_len;
84         struct sk_buff *skb;
85         unsigned char *msg;
86
87         meta_len =  sizeof(struct nfp_fl_rule_metadata);
88         key_len = nfp_flow->meta.key_len;
89         mask_len = nfp_flow->meta.mask_len;
90         act_len = nfp_flow->meta.act_len;
91
92         tot_len = meta_len + key_len + mask_len + act_len;
93
94         /* Convert to long words as firmware expects
95          * lengths in units of NFP_FL_LW_SIZ.
96          */
97         nfp_flow->meta.key_len >>= NFP_FL_LW_SIZ;
98         nfp_flow->meta.mask_len >>= NFP_FL_LW_SIZ;
99         nfp_flow->meta.act_len >>= NFP_FL_LW_SIZ;
100
101         skb = nfp_flower_cmsg_alloc(app, tot_len, mtype, GFP_KERNEL);
102         if (!skb)
103                 return -ENOMEM;
104
105         msg = nfp_flower_cmsg_get_data(skb);
106         memcpy(msg, &nfp_flow->meta, meta_len);
107         memcpy(&msg[meta_len], nfp_flow->unmasked_data, key_len);
108         memcpy(&msg[meta_len + key_len], nfp_flow->mask_data, mask_len);
109         memcpy(&msg[meta_len + key_len + mask_len],
110                nfp_flow->action_data, act_len);
111
112         /* Convert back to bytes as software expects
113          * lengths in units of bytes.
114          */
115         nfp_flow->meta.key_len <<= NFP_FL_LW_SIZ;
116         nfp_flow->meta.mask_len <<= NFP_FL_LW_SIZ;
117         nfp_flow->meta.act_len <<= NFP_FL_LW_SIZ;
118
119         nfp_ctrl_tx(app->ctrl, skb);
120
121         return 0;
122 }
123
124 static bool nfp_flower_check_higher_than_mac(struct flow_cls_offload *f)
125 {
126         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
127
128         return flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS) ||
129                flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS) ||
130                flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS) ||
131                flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP);
132 }
133
134 static bool nfp_flower_check_higher_than_l3(struct flow_cls_offload *f)
135 {
136         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
137
138         return flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS) ||
139                flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP);
140 }
141
142 static int
143 nfp_flower_calc_opt_layer(struct flow_dissector_key_enc_opts *enc_opts,
144                           u32 *key_layer_two, int *key_size,
145                           struct netlink_ext_ack *extack)
146 {
147         if (enc_opts->len > NFP_FL_MAX_GENEVE_OPT_KEY) {
148                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: geneve options exceed maximum length");
149                 return -EOPNOTSUPP;
150         }
151
152         if (enc_opts->len > 0) {
153                 *key_layer_two |= NFP_FLOWER_LAYER2_GENEVE_OP;
154                 *key_size += sizeof(struct nfp_flower_geneve_options);
155         }
156
157         return 0;
158 }
159
160 static int
161 nfp_flower_calc_udp_tun_layer(struct flow_dissector_key_ports *enc_ports,
162                               struct flow_dissector_key_enc_opts *enc_op,
163                               u32 *key_layer_two, u8 *key_layer, int *key_size,
164                               struct nfp_flower_priv *priv,
165                               enum nfp_flower_tun_type *tun_type,
166                               struct netlink_ext_ack *extack)
167 {
168         int err;
169
170         switch (enc_ports->dst) {
171         case htons(IANA_VXLAN_UDP_PORT):
172                 *tun_type = NFP_FL_TUNNEL_VXLAN;
173                 *key_layer |= NFP_FLOWER_LAYER_VXLAN;
174                 *key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
175
176                 if (enc_op) {
177                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: encap options not supported on vxlan tunnels");
178                         return -EOPNOTSUPP;
179                 }
180                 break;
181         case htons(GENEVE_UDP_PORT):
182                 if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)) {
183                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support geneve offload");
184                         return -EOPNOTSUPP;
185                 }
186                 *tun_type = NFP_FL_TUNNEL_GENEVE;
187                 *key_layer |= NFP_FLOWER_LAYER_EXT_META;
188                 *key_size += sizeof(struct nfp_flower_ext_meta);
189                 *key_layer_two |= NFP_FLOWER_LAYER2_GENEVE;
190                 *key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
191
192                 if (!enc_op)
193                         break;
194                 if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE_OPT)) {
195                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support geneve option offload");
196                         return -EOPNOTSUPP;
197                 }
198                 err = nfp_flower_calc_opt_layer(enc_op, key_layer_two,
199                                                 key_size, extack);
200                 if (err)
201                         return err;
202                 break;
203         default:
204                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: tunnel type unknown");
205                 return -EOPNOTSUPP;
206         }
207
208         return 0;
209 }
210
211 static int
212 nfp_flower_calculate_key_layers(struct nfp_app *app,
213                                 struct net_device *netdev,
214                                 struct nfp_fl_key_ls *ret_key_ls,
215                                 struct flow_cls_offload *flow,
216                                 enum nfp_flower_tun_type *tun_type,
217                                 struct netlink_ext_ack *extack)
218 {
219         struct flow_rule *rule = flow_cls_offload_flow_rule(flow);
220         struct flow_dissector *dissector = rule->match.dissector;
221         struct flow_match_basic basic = { NULL, NULL};
222         struct nfp_flower_priv *priv = app->priv;
223         u32 key_layer_two;
224         u8 key_layer;
225         int key_size;
226         int err;
227
228         if (dissector->used_keys & ~NFP_FLOWER_WHITELIST_DISSECTOR) {
229                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match not supported");
230                 return -EOPNOTSUPP;
231         }
232
233         /* If any tun dissector is used then the required set must be used. */
234         if (dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR &&
235             (dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R)
236             != NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R) {
237                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: tunnel match not supported");
238                 return -EOPNOTSUPP;
239         }
240
241         key_layer_two = 0;
242         key_layer = NFP_FLOWER_LAYER_PORT;
243         key_size = sizeof(struct nfp_flower_meta_tci) +
244                    sizeof(struct nfp_flower_in_port);
245
246         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS) ||
247             flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_MPLS)) {
248                 key_layer |= NFP_FLOWER_LAYER_MAC;
249                 key_size += sizeof(struct nfp_flower_mac_mpls);
250         }
251
252         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
253                 struct flow_match_vlan vlan;
254
255                 flow_rule_match_vlan(rule, &vlan);
256                 if (!(priv->flower_ext_feats & NFP_FL_FEATS_VLAN_PCP) &&
257                     vlan.key->vlan_priority) {
258                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support VLAN PCP offload");
259                         return -EOPNOTSUPP;
260                 }
261         }
262
263         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
264                 struct flow_match_enc_opts enc_op = { NULL, NULL };
265                 struct flow_match_ipv4_addrs ipv4_addrs;
266                 struct flow_match_control enc_ctl;
267                 struct flow_match_ports enc_ports;
268
269                 flow_rule_match_enc_control(rule, &enc_ctl);
270
271                 if (enc_ctl.mask->addr_type != 0xffff) {
272                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: wildcarded protocols on tunnels are not supported");
273                         return -EOPNOTSUPP;
274                 }
275                 if (enc_ctl.key->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
276                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only IPv4 tunnels are supported");
277                         return -EOPNOTSUPP;
278                 }
279
280                 /* These fields are already verified as used. */
281                 flow_rule_match_enc_ipv4_addrs(rule, &ipv4_addrs);
282                 if (ipv4_addrs.mask->dst != cpu_to_be32(~0)) {
283                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only an exact match IPv4 destination address is supported");
284                         return -EOPNOTSUPP;
285                 }
286
287                 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS))
288                         flow_rule_match_enc_opts(rule, &enc_op);
289
290
291                 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
292                         /* check if GRE, which has no enc_ports */
293                         if (netif_is_gretap(netdev)) {
294                                 *tun_type = NFP_FL_TUNNEL_GRE;
295                                 key_layer |= NFP_FLOWER_LAYER_EXT_META;
296                                 key_size += sizeof(struct nfp_flower_ext_meta);
297                                 key_layer_two |= NFP_FLOWER_LAYER2_GRE;
298                                 key_size +=
299                                         sizeof(struct nfp_flower_ipv4_gre_tun);
300
301                                 if (enc_op.key) {
302                                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: encap options not supported on GRE tunnels");
303                                         return -EOPNOTSUPP;
304                                 }
305                         } else {
306                                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: an exact match on L4 destination port is required for non-GRE tunnels");
307                                 return -EOPNOTSUPP;
308                         }
309                 } else {
310                         flow_rule_match_enc_ports(rule, &enc_ports);
311                         if (enc_ports.mask->dst != cpu_to_be16(~0)) {
312                                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only an exact match L4 destination port is supported");
313                                 return -EOPNOTSUPP;
314                         }
315
316                         err = nfp_flower_calc_udp_tun_layer(enc_ports.key,
317                                                             enc_op.key,
318                                                             &key_layer_two,
319                                                             &key_layer,
320                                                             &key_size, priv,
321                                                             tun_type, extack);
322                         if (err)
323                                 return err;
324
325                         /* Ensure the ingress netdev matches the expected
326                          * tun type.
327                          */
328                         if (!nfp_fl_netdev_is_tunnel_type(netdev, *tun_type)) {
329                                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: ingress netdev does not match the expected tunnel type");
330                                 return -EOPNOTSUPP;
331                         }
332                 }
333         }
334
335         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC))
336                 flow_rule_match_basic(rule, &basic);
337
338         if (basic.mask && basic.mask->n_proto) {
339                 /* Ethernet type is present in the key. */
340                 switch (basic.key->n_proto) {
341                 case cpu_to_be16(ETH_P_IP):
342                         key_layer |= NFP_FLOWER_LAYER_IPV4;
343                         key_size += sizeof(struct nfp_flower_ipv4);
344                         break;
345
346                 case cpu_to_be16(ETH_P_IPV6):
347                         key_layer |= NFP_FLOWER_LAYER_IPV6;
348                         key_size += sizeof(struct nfp_flower_ipv6);
349                         break;
350
351                 /* Currently we do not offload ARP
352                  * because we rely on it to get to the host.
353                  */
354                 case cpu_to_be16(ETH_P_ARP):
355                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: ARP not supported");
356                         return -EOPNOTSUPP;
357
358                 case cpu_to_be16(ETH_P_MPLS_UC):
359                 case cpu_to_be16(ETH_P_MPLS_MC):
360                         if (!(key_layer & NFP_FLOWER_LAYER_MAC)) {
361                                 key_layer |= NFP_FLOWER_LAYER_MAC;
362                                 key_size += sizeof(struct nfp_flower_mac_mpls);
363                         }
364                         break;
365
366                 /* Will be included in layer 2. */
367                 case cpu_to_be16(ETH_P_8021Q):
368                         break;
369
370                 default:
371                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on given EtherType is not supported");
372                         return -EOPNOTSUPP;
373                 }
374         } else if (nfp_flower_check_higher_than_mac(flow)) {
375                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: cannot match above L2 without specified EtherType");
376                 return -EOPNOTSUPP;
377         }
378
379         if (basic.mask && basic.mask->ip_proto) {
380                 switch (basic.key->ip_proto) {
381                 case IPPROTO_TCP:
382                 case IPPROTO_UDP:
383                 case IPPROTO_SCTP:
384                 case IPPROTO_ICMP:
385                 case IPPROTO_ICMPV6:
386                         key_layer |= NFP_FLOWER_LAYER_TP;
387                         key_size += sizeof(struct nfp_flower_tp_ports);
388                         break;
389                 }
390         }
391
392         if (!(key_layer & NFP_FLOWER_LAYER_TP) &&
393             nfp_flower_check_higher_than_l3(flow)) {
394                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: cannot match on L4 information without specified IP protocol type");
395                 return -EOPNOTSUPP;
396         }
397
398         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP)) {
399                 struct flow_match_tcp tcp;
400                 u32 tcp_flags;
401
402                 flow_rule_match_tcp(rule, &tcp);
403                 tcp_flags = be16_to_cpu(tcp.key->flags);
404
405                 if (tcp_flags & ~NFP_FLOWER_SUPPORTED_TCPFLAGS) {
406                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: no match support for selected TCP flags");
407                         return -EOPNOTSUPP;
408                 }
409
410                 /* We only support PSH and URG flags when either
411                  * FIN, SYN or RST is present as well.
412                  */
413                 if ((tcp_flags & (TCPHDR_PSH | TCPHDR_URG)) &&
414                     !(tcp_flags & (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST))) {
415                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: PSH and URG is only supported when used with FIN, SYN or RST");
416                         return -EOPNOTSUPP;
417                 }
418
419                 /* We need to store TCP flags in the either the IPv4 or IPv6 key
420                  * space, thus we need to ensure we include a IPv4/IPv6 key
421                  * layer if we have not done so already.
422                  */
423                 if (!basic.key) {
424                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on TCP flags requires a match on L3 protocol");
425                         return -EOPNOTSUPP;
426                 }
427
428                 if (!(key_layer & NFP_FLOWER_LAYER_IPV4) &&
429                     !(key_layer & NFP_FLOWER_LAYER_IPV6)) {
430                         switch (basic.key->n_proto) {
431                         case cpu_to_be16(ETH_P_IP):
432                                 key_layer |= NFP_FLOWER_LAYER_IPV4;
433                                 key_size += sizeof(struct nfp_flower_ipv4);
434                                 break;
435
436                         case cpu_to_be16(ETH_P_IPV6):
437                                         key_layer |= NFP_FLOWER_LAYER_IPV6;
438                                 key_size += sizeof(struct nfp_flower_ipv6);
439                                 break;
440
441                         default:
442                                 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on TCP flags requires a match on IPv4/IPv6");
443                                 return -EOPNOTSUPP;
444                         }
445                 }
446         }
447
448         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
449                 struct flow_match_control ctl;
450
451                 flow_rule_match_control(rule, &ctl);
452                 if (ctl.key->flags & ~NFP_FLOWER_SUPPORTED_CTLFLAGS) {
453                         NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on unknown control flag");
454                         return -EOPNOTSUPP;
455                 }
456         }
457
458         ret_key_ls->key_layer = key_layer;
459         ret_key_ls->key_layer_two = key_layer_two;
460         ret_key_ls->key_size = key_size;
461
462         return 0;
463 }
464
465 static struct nfp_fl_payload *
466 nfp_flower_allocate_new(struct nfp_fl_key_ls *key_layer)
467 {
468         struct nfp_fl_payload *flow_pay;
469
470         flow_pay = kmalloc(sizeof(*flow_pay), GFP_KERNEL);
471         if (!flow_pay)
472                 return NULL;
473
474         flow_pay->meta.key_len = key_layer->key_size;
475         flow_pay->unmasked_data = kmalloc(key_layer->key_size, GFP_KERNEL);
476         if (!flow_pay->unmasked_data)
477                 goto err_free_flow;
478
479         flow_pay->meta.mask_len = key_layer->key_size;
480         flow_pay->mask_data = kmalloc(key_layer->key_size, GFP_KERNEL);
481         if (!flow_pay->mask_data)
482                 goto err_free_unmasked;
483
484         flow_pay->action_data = kmalloc(NFP_FL_MAX_A_SIZ, GFP_KERNEL);
485         if (!flow_pay->action_data)
486                 goto err_free_mask;
487
488         flow_pay->nfp_tun_ipv4_addr = 0;
489         flow_pay->meta.flags = 0;
490         INIT_LIST_HEAD(&flow_pay->linked_flows);
491         flow_pay->in_hw = false;
492         flow_pay->pre_tun_rule.dev = NULL;
493
494         return flow_pay;
495
496 err_free_mask:
497         kfree(flow_pay->mask_data);
498 err_free_unmasked:
499         kfree(flow_pay->unmasked_data);
500 err_free_flow:
501         kfree(flow_pay);
502         return NULL;
503 }
504
505 static int
506 nfp_flower_update_merge_with_actions(struct nfp_fl_payload *flow,
507                                      struct nfp_flower_merge_check *merge,
508                                      u8 *last_act_id, int *act_out)
509 {
510         struct nfp_fl_set_ipv6_tc_hl_fl *ipv6_tc_hl_fl;
511         struct nfp_fl_set_ip4_ttl_tos *ipv4_ttl_tos;
512         struct nfp_fl_set_ip4_addrs *ipv4_add;
513         struct nfp_fl_set_ipv6_addr *ipv6_add;
514         struct nfp_fl_push_vlan *push_vlan;
515         struct nfp_fl_set_tport *tport;
516         struct nfp_fl_set_eth *eth;
517         struct nfp_fl_act_head *a;
518         unsigned int act_off = 0;
519         u8 act_id = 0;
520         u8 *ports;
521         int i;
522
523         while (act_off < flow->meta.act_len) {
524                 a = (struct nfp_fl_act_head *)&flow->action_data[act_off];
525                 act_id = a->jump_id;
526
527                 switch (act_id) {
528                 case NFP_FL_ACTION_OPCODE_OUTPUT:
529                         if (act_out)
530                                 (*act_out)++;
531                         break;
532                 case NFP_FL_ACTION_OPCODE_PUSH_VLAN:
533                         push_vlan = (struct nfp_fl_push_vlan *)a;
534                         if (push_vlan->vlan_tci)
535                                 merge->tci = cpu_to_be16(0xffff);
536                         break;
537                 case NFP_FL_ACTION_OPCODE_POP_VLAN:
538                         merge->tci = cpu_to_be16(0);
539                         break;
540                 case NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL:
541                         /* New tunnel header means l2 to l4 can be matched. */
542                         eth_broadcast_addr(&merge->l2.mac_dst[0]);
543                         eth_broadcast_addr(&merge->l2.mac_src[0]);
544                         memset(&merge->l4, 0xff,
545                                sizeof(struct nfp_flower_tp_ports));
546                         memset(&merge->ipv4, 0xff,
547                                sizeof(struct nfp_flower_ipv4));
548                         break;
549                 case NFP_FL_ACTION_OPCODE_SET_ETHERNET:
550                         eth = (struct nfp_fl_set_eth *)a;
551                         for (i = 0; i < ETH_ALEN; i++)
552                                 merge->l2.mac_dst[i] |= eth->eth_addr_mask[i];
553                         for (i = 0; i < ETH_ALEN; i++)
554                                 merge->l2.mac_src[i] |=
555                                         eth->eth_addr_mask[ETH_ALEN + i];
556                         break;
557                 case NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS:
558                         ipv4_add = (struct nfp_fl_set_ip4_addrs *)a;
559                         merge->ipv4.ipv4_src |= ipv4_add->ipv4_src_mask;
560                         merge->ipv4.ipv4_dst |= ipv4_add->ipv4_dst_mask;
561                         break;
562                 case NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS:
563                         ipv4_ttl_tos = (struct nfp_fl_set_ip4_ttl_tos *)a;
564                         merge->ipv4.ip_ext.ttl |= ipv4_ttl_tos->ipv4_ttl_mask;
565                         merge->ipv4.ip_ext.tos |= ipv4_ttl_tos->ipv4_tos_mask;
566                         break;
567                 case NFP_FL_ACTION_OPCODE_SET_IPV6_SRC:
568                         ipv6_add = (struct nfp_fl_set_ipv6_addr *)a;
569                         for (i = 0; i < 4; i++)
570                                 merge->ipv6.ipv6_src.in6_u.u6_addr32[i] |=
571                                         ipv6_add->ipv6[i].mask;
572                         break;
573                 case NFP_FL_ACTION_OPCODE_SET_IPV6_DST:
574                         ipv6_add = (struct nfp_fl_set_ipv6_addr *)a;
575                         for (i = 0; i < 4; i++)
576                                 merge->ipv6.ipv6_dst.in6_u.u6_addr32[i] |=
577                                         ipv6_add->ipv6[i].mask;
578                         break;
579                 case NFP_FL_ACTION_OPCODE_SET_IPV6_TC_HL_FL:
580                         ipv6_tc_hl_fl = (struct nfp_fl_set_ipv6_tc_hl_fl *)a;
581                         merge->ipv6.ip_ext.ttl |=
582                                 ipv6_tc_hl_fl->ipv6_hop_limit_mask;
583                         merge->ipv6.ip_ext.tos |= ipv6_tc_hl_fl->ipv6_tc_mask;
584                         merge->ipv6.ipv6_flow_label_exthdr |=
585                                 ipv6_tc_hl_fl->ipv6_label_mask;
586                         break;
587                 case NFP_FL_ACTION_OPCODE_SET_UDP:
588                 case NFP_FL_ACTION_OPCODE_SET_TCP:
589                         tport = (struct nfp_fl_set_tport *)a;
590                         ports = (u8 *)&merge->l4.port_src;
591                         for (i = 0; i < 4; i++)
592                                 ports[i] |= tport->tp_port_mask[i];
593                         break;
594                 case NFP_FL_ACTION_OPCODE_PRE_TUNNEL:
595                 case NFP_FL_ACTION_OPCODE_PRE_LAG:
596                 case NFP_FL_ACTION_OPCODE_PUSH_GENEVE:
597                         break;
598                 default:
599                         return -EOPNOTSUPP;
600                 }
601
602                 act_off += a->len_lw << NFP_FL_LW_SIZ;
603         }
604
605         if (last_act_id)
606                 *last_act_id = act_id;
607
608         return 0;
609 }
610
611 static int
612 nfp_flower_populate_merge_match(struct nfp_fl_payload *flow,
613                                 struct nfp_flower_merge_check *merge,
614                                 bool extra_fields)
615 {
616         struct nfp_flower_meta_tci *meta_tci;
617         u8 *mask = flow->mask_data;
618         u8 key_layer, match_size;
619
620         memset(merge, 0, sizeof(struct nfp_flower_merge_check));
621
622         meta_tci = (struct nfp_flower_meta_tci *)mask;
623         key_layer = meta_tci->nfp_flow_key_layer;
624
625         if (key_layer & ~NFP_FLOWER_MERGE_FIELDS && !extra_fields)
626                 return -EOPNOTSUPP;
627
628         merge->tci = meta_tci->tci;
629         mask += sizeof(struct nfp_flower_meta_tci);
630
631         if (key_layer & NFP_FLOWER_LAYER_EXT_META)
632                 mask += sizeof(struct nfp_flower_ext_meta);
633
634         mask += sizeof(struct nfp_flower_in_port);
635
636         if (key_layer & NFP_FLOWER_LAYER_MAC) {
637                 match_size = sizeof(struct nfp_flower_mac_mpls);
638                 memcpy(&merge->l2, mask, match_size);
639                 mask += match_size;
640         }
641
642         if (key_layer & NFP_FLOWER_LAYER_TP) {
643                 match_size = sizeof(struct nfp_flower_tp_ports);
644                 memcpy(&merge->l4, mask, match_size);
645                 mask += match_size;
646         }
647
648         if (key_layer & NFP_FLOWER_LAYER_IPV4) {
649                 match_size = sizeof(struct nfp_flower_ipv4);
650                 memcpy(&merge->ipv4, mask, match_size);
651         }
652
653         if (key_layer & NFP_FLOWER_LAYER_IPV6) {
654                 match_size = sizeof(struct nfp_flower_ipv6);
655                 memcpy(&merge->ipv6, mask, match_size);
656         }
657
658         return 0;
659 }
660
661 static int
662 nfp_flower_can_merge(struct nfp_fl_payload *sub_flow1,
663                      struct nfp_fl_payload *sub_flow2)
664 {
665         /* Two flows can be merged if sub_flow2 only matches on bits that are
666          * either matched by sub_flow1 or set by a sub_flow1 action. This
667          * ensures that every packet that hits sub_flow1 and recirculates is
668          * guaranteed to hit sub_flow2.
669          */
670         struct nfp_flower_merge_check sub_flow1_merge, sub_flow2_merge;
671         int err, act_out = 0;
672         u8 last_act_id = 0;
673
674         err = nfp_flower_populate_merge_match(sub_flow1, &sub_flow1_merge,
675                                               true);
676         if (err)
677                 return err;
678
679         err = nfp_flower_populate_merge_match(sub_flow2, &sub_flow2_merge,
680                                               false);
681         if (err)
682                 return err;
683
684         err = nfp_flower_update_merge_with_actions(sub_flow1, &sub_flow1_merge,
685                                                    &last_act_id, &act_out);
686         if (err)
687                 return err;
688
689         /* Must only be 1 output action and it must be the last in sequence. */
690         if (act_out != 1 || last_act_id != NFP_FL_ACTION_OPCODE_OUTPUT)
691                 return -EOPNOTSUPP;
692
693         /* Reject merge if sub_flow2 matches on something that is not matched
694          * on or set in an action by sub_flow1.
695          */
696         err = bitmap_andnot(sub_flow2_merge.vals, sub_flow2_merge.vals,
697                             sub_flow1_merge.vals,
698                             sizeof(struct nfp_flower_merge_check) * 8);
699         if (err)
700                 return -EINVAL;
701
702         return 0;
703 }
704
705 static unsigned int
706 nfp_flower_copy_pre_actions(char *act_dst, char *act_src, int len,
707                             bool *tunnel_act)
708 {
709         unsigned int act_off = 0, act_len;
710         struct nfp_fl_act_head *a;
711         u8 act_id = 0;
712
713         while (act_off < len) {
714                 a = (struct nfp_fl_act_head *)&act_src[act_off];
715                 act_len = a->len_lw << NFP_FL_LW_SIZ;
716                 act_id = a->jump_id;
717
718                 switch (act_id) {
719                 case NFP_FL_ACTION_OPCODE_PRE_TUNNEL:
720                         if (tunnel_act)
721                                 *tunnel_act = true;
722                         /* fall through */
723                 case NFP_FL_ACTION_OPCODE_PRE_LAG:
724                         memcpy(act_dst + act_off, act_src + act_off, act_len);
725                         break;
726                 default:
727                         return act_off;
728                 }
729
730                 act_off += act_len;
731         }
732
733         return act_off;
734 }
735
736 static int
737 nfp_fl_verify_post_tun_acts(char *acts, int len, struct nfp_fl_push_vlan **vlan)
738 {
739         struct nfp_fl_act_head *a;
740         unsigned int act_off = 0;
741
742         while (act_off < len) {
743                 a = (struct nfp_fl_act_head *)&acts[act_off];
744
745                 if (a->jump_id == NFP_FL_ACTION_OPCODE_PUSH_VLAN && !act_off)
746                         *vlan = (struct nfp_fl_push_vlan *)a;
747                 else if (a->jump_id != NFP_FL_ACTION_OPCODE_OUTPUT)
748                         return -EOPNOTSUPP;
749
750                 act_off += a->len_lw << NFP_FL_LW_SIZ;
751         }
752
753         /* Ensure any VLAN push also has an egress action. */
754         if (*vlan && act_off <= sizeof(struct nfp_fl_push_vlan))
755                 return -EOPNOTSUPP;
756
757         return 0;
758 }
759
760 static int
761 nfp_fl_push_vlan_after_tun(char *acts, int len, struct nfp_fl_push_vlan *vlan)
762 {
763         struct nfp_fl_set_ipv4_tun *tun;
764         struct nfp_fl_act_head *a;
765         unsigned int act_off = 0;
766
767         while (act_off < len) {
768                 a = (struct nfp_fl_act_head *)&acts[act_off];
769
770                 if (a->jump_id == NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL) {
771                         tun = (struct nfp_fl_set_ipv4_tun *)a;
772                         tun->outer_vlan_tpid = vlan->vlan_tpid;
773                         tun->outer_vlan_tci = vlan->vlan_tci;
774
775                         return 0;
776                 }
777
778                 act_off += a->len_lw << NFP_FL_LW_SIZ;
779         }
780
781         /* Return error if no tunnel action is found. */
782         return -EOPNOTSUPP;
783 }
784
785 static int
786 nfp_flower_merge_action(struct nfp_fl_payload *sub_flow1,
787                         struct nfp_fl_payload *sub_flow2,
788                         struct nfp_fl_payload *merge_flow)
789 {
790         unsigned int sub1_act_len, sub2_act_len, pre_off1, pre_off2;
791         struct nfp_fl_push_vlan *post_tun_push_vlan = NULL;
792         bool tunnel_act = false;
793         char *merge_act;
794         int err;
795
796         /* The last action of sub_flow1 must be output - do not merge this. */
797         sub1_act_len = sub_flow1->meta.act_len - sizeof(struct nfp_fl_output);
798         sub2_act_len = sub_flow2->meta.act_len;
799
800         if (!sub2_act_len)
801                 return -EINVAL;
802
803         if (sub1_act_len + sub2_act_len > NFP_FL_MAX_A_SIZ)
804                 return -EINVAL;
805
806         /* A shortcut can only be applied if there is a single action. */
807         if (sub1_act_len)
808                 merge_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
809         else
810                 merge_flow->meta.shortcut = sub_flow2->meta.shortcut;
811
812         merge_flow->meta.act_len = sub1_act_len + sub2_act_len;
813         merge_act = merge_flow->action_data;
814
815         /* Copy any pre-actions to the start of merge flow action list. */
816         pre_off1 = nfp_flower_copy_pre_actions(merge_act,
817                                                sub_flow1->action_data,
818                                                sub1_act_len, &tunnel_act);
819         merge_act += pre_off1;
820         sub1_act_len -= pre_off1;
821         pre_off2 = nfp_flower_copy_pre_actions(merge_act,
822                                                sub_flow2->action_data,
823                                                sub2_act_len, NULL);
824         merge_act += pre_off2;
825         sub2_act_len -= pre_off2;
826
827         /* FW does a tunnel push when egressing, therefore, if sub_flow 1 pushes
828          * a tunnel, there are restrictions on what sub_flow 2 actions lead to a
829          * valid merge.
830          */
831         if (tunnel_act) {
832                 char *post_tun_acts = &sub_flow2->action_data[pre_off2];
833
834                 err = nfp_fl_verify_post_tun_acts(post_tun_acts, sub2_act_len,
835                                                   &post_tun_push_vlan);
836                 if (err)
837                         return err;
838
839                 if (post_tun_push_vlan) {
840                         pre_off2 += sizeof(*post_tun_push_vlan);
841                         sub2_act_len -= sizeof(*post_tun_push_vlan);
842                 }
843         }
844
845         /* Copy remaining actions from sub_flows 1 and 2. */
846         memcpy(merge_act, sub_flow1->action_data + pre_off1, sub1_act_len);
847
848         if (post_tun_push_vlan) {
849                 /* Update tunnel action in merge to include VLAN push. */
850                 err = nfp_fl_push_vlan_after_tun(merge_act, sub1_act_len,
851                                                  post_tun_push_vlan);
852                 if (err)
853                         return err;
854
855                 merge_flow->meta.act_len -= sizeof(*post_tun_push_vlan);
856         }
857
858         merge_act += sub1_act_len;
859         memcpy(merge_act, sub_flow2->action_data + pre_off2, sub2_act_len);
860
861         return 0;
862 }
863
864 /* Flow link code should only be accessed under RTNL. */
865 static void nfp_flower_unlink_flow(struct nfp_fl_payload_link *link)
866 {
867         list_del(&link->merge_flow.list);
868         list_del(&link->sub_flow.list);
869         kfree(link);
870 }
871
872 static void nfp_flower_unlink_flows(struct nfp_fl_payload *merge_flow,
873                                     struct nfp_fl_payload *sub_flow)
874 {
875         struct nfp_fl_payload_link *link;
876
877         list_for_each_entry(link, &merge_flow->linked_flows, merge_flow.list)
878                 if (link->sub_flow.flow == sub_flow) {
879                         nfp_flower_unlink_flow(link);
880                         return;
881                 }
882 }
883
884 static int nfp_flower_link_flows(struct nfp_fl_payload *merge_flow,
885                                  struct nfp_fl_payload *sub_flow)
886 {
887         struct nfp_fl_payload_link *link;
888
889         link = kmalloc(sizeof(*link), GFP_KERNEL);
890         if (!link)
891                 return -ENOMEM;
892
893         link->merge_flow.flow = merge_flow;
894         list_add_tail(&link->merge_flow.list, &merge_flow->linked_flows);
895         link->sub_flow.flow = sub_flow;
896         list_add_tail(&link->sub_flow.list, &sub_flow->linked_flows);
897
898         return 0;
899 }
900
901 /**
902  * nfp_flower_merge_offloaded_flows() - Merge 2 existing flows to single flow.
903  * @app:        Pointer to the APP handle
904  * @sub_flow1:  Initial flow matched to produce merge hint
905  * @sub_flow2:  Post recirculation flow matched in merge hint
906  *
907  * Combines 2 flows (if valid) to a single flow, removing the initial from hw
908  * and offloading the new, merged flow.
909  *
910  * Return: negative value on error, 0 in success.
911  */
912 int nfp_flower_merge_offloaded_flows(struct nfp_app *app,
913                                      struct nfp_fl_payload *sub_flow1,
914                                      struct nfp_fl_payload *sub_flow2)
915 {
916         struct flow_cls_offload merge_tc_off;
917         struct nfp_flower_priv *priv = app->priv;
918         struct netlink_ext_ack *extack = NULL;
919         struct nfp_fl_payload *merge_flow;
920         struct nfp_fl_key_ls merge_key_ls;
921         int err;
922
923         ASSERT_RTNL();
924
925         extack = merge_tc_off.common.extack;
926         if (sub_flow1 == sub_flow2 ||
927             nfp_flower_is_merge_flow(sub_flow1) ||
928             nfp_flower_is_merge_flow(sub_flow2))
929                 return -EINVAL;
930
931         err = nfp_flower_can_merge(sub_flow1, sub_flow2);
932         if (err)
933                 return err;
934
935         merge_key_ls.key_size = sub_flow1->meta.key_len;
936
937         merge_flow = nfp_flower_allocate_new(&merge_key_ls);
938         if (!merge_flow)
939                 return -ENOMEM;
940
941         merge_flow->tc_flower_cookie = (unsigned long)merge_flow;
942         merge_flow->ingress_dev = sub_flow1->ingress_dev;
943
944         memcpy(merge_flow->unmasked_data, sub_flow1->unmasked_data,
945                sub_flow1->meta.key_len);
946         memcpy(merge_flow->mask_data, sub_flow1->mask_data,
947                sub_flow1->meta.mask_len);
948
949         err = nfp_flower_merge_action(sub_flow1, sub_flow2, merge_flow);
950         if (err)
951                 goto err_destroy_merge_flow;
952
953         err = nfp_flower_link_flows(merge_flow, sub_flow1);
954         if (err)
955                 goto err_destroy_merge_flow;
956
957         err = nfp_flower_link_flows(merge_flow, sub_flow2);
958         if (err)
959                 goto err_unlink_sub_flow1;
960
961         merge_tc_off.cookie = merge_flow->tc_flower_cookie;
962         err = nfp_compile_flow_metadata(app, &merge_tc_off, merge_flow,
963                                         merge_flow->ingress_dev, extack);
964         if (err)
965                 goto err_unlink_sub_flow2;
966
967         err = rhashtable_insert_fast(&priv->flow_table, &merge_flow->fl_node,
968                                      nfp_flower_table_params);
969         if (err)
970                 goto err_release_metadata;
971
972         err = nfp_flower_xmit_flow(app, merge_flow,
973                                    NFP_FLOWER_CMSG_TYPE_FLOW_MOD);
974         if (err)
975                 goto err_remove_rhash;
976
977         merge_flow->in_hw = true;
978         sub_flow1->in_hw = false;
979
980         return 0;
981
982 err_remove_rhash:
983         WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table,
984                                             &merge_flow->fl_node,
985                                             nfp_flower_table_params));
986 err_release_metadata:
987         nfp_modify_flow_metadata(app, merge_flow);
988 err_unlink_sub_flow2:
989         nfp_flower_unlink_flows(merge_flow, sub_flow2);
990 err_unlink_sub_flow1:
991         nfp_flower_unlink_flows(merge_flow, sub_flow1);
992 err_destroy_merge_flow:
993         kfree(merge_flow->action_data);
994         kfree(merge_flow->mask_data);
995         kfree(merge_flow->unmasked_data);
996         kfree(merge_flow);
997         return err;
998 }
999
1000 /**
1001  * nfp_flower_validate_pre_tun_rule()
1002  * @app:        Pointer to the APP handle
1003  * @flow:       Pointer to NFP flow representation of rule
1004  * @extack:     Netlink extended ACK report
1005  *
1006  * Verifies the flow as a pre-tunnel rule.
1007  *
1008  * Return: negative value on error, 0 if verified.
1009  */
1010 static int
1011 nfp_flower_validate_pre_tun_rule(struct nfp_app *app,
1012                                  struct nfp_fl_payload *flow,
1013                                  struct netlink_ext_ack *extack)
1014 {
1015         return -EOPNOTSUPP;
1016 }
1017
1018 /**
1019  * nfp_flower_add_offload() - Adds a new flow to hardware.
1020  * @app:        Pointer to the APP handle
1021  * @netdev:     netdev structure.
1022  * @flow:       TC flower classifier offload structure.
1023  *
1024  * Adds a new flow to the repeated hash structure and action payload.
1025  *
1026  * Return: negative value on error, 0 if configured successfully.
1027  */
1028 static int
1029 nfp_flower_add_offload(struct nfp_app *app, struct net_device *netdev,
1030                        struct flow_cls_offload *flow)
1031 {
1032         enum nfp_flower_tun_type tun_type = NFP_FL_TUNNEL_NONE;
1033         struct nfp_flower_priv *priv = app->priv;
1034         struct netlink_ext_ack *extack = NULL;
1035         struct nfp_fl_payload *flow_pay;
1036         struct nfp_fl_key_ls *key_layer;
1037         struct nfp_port *port = NULL;
1038         int err;
1039
1040         extack = flow->common.extack;
1041         if (nfp_netdev_is_nfp_repr(netdev))
1042                 port = nfp_port_from_netdev(netdev);
1043
1044         key_layer = kmalloc(sizeof(*key_layer), GFP_KERNEL);
1045         if (!key_layer)
1046                 return -ENOMEM;
1047
1048         err = nfp_flower_calculate_key_layers(app, netdev, key_layer, flow,
1049                                               &tun_type, extack);
1050         if (err)
1051                 goto err_free_key_ls;
1052
1053         flow_pay = nfp_flower_allocate_new(key_layer);
1054         if (!flow_pay) {
1055                 err = -ENOMEM;
1056                 goto err_free_key_ls;
1057         }
1058
1059         err = nfp_flower_compile_flow_match(app, flow, key_layer, netdev,
1060                                             flow_pay, tun_type, extack);
1061         if (err)
1062                 goto err_destroy_flow;
1063
1064         err = nfp_flower_compile_action(app, flow, netdev, flow_pay, extack);
1065         if (err)
1066                 goto err_destroy_flow;
1067
1068         if (flow_pay->pre_tun_rule.dev) {
1069                 err = nfp_flower_validate_pre_tun_rule(app, flow_pay, extack);
1070                 if (err)
1071                         goto err_destroy_flow;
1072         }
1073
1074         err = nfp_compile_flow_metadata(app, flow, flow_pay, netdev, extack);
1075         if (err)
1076                 goto err_destroy_flow;
1077
1078         flow_pay->tc_flower_cookie = flow->cookie;
1079         err = rhashtable_insert_fast(&priv->flow_table, &flow_pay->fl_node,
1080                                      nfp_flower_table_params);
1081         if (err) {
1082                 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot insert flow into tables for offloads");
1083                 goto err_release_metadata;
1084         }
1085
1086         err = nfp_flower_xmit_flow(app, flow_pay,
1087                                    NFP_FLOWER_CMSG_TYPE_FLOW_ADD);
1088         if (err)
1089                 goto err_remove_rhash;
1090
1091         if (port)
1092                 port->tc_offload_cnt++;
1093
1094         flow_pay->in_hw = true;
1095
1096         /* Deallocate flow payload when flower rule has been destroyed. */
1097         kfree(key_layer);
1098
1099         return 0;
1100
1101 err_remove_rhash:
1102         WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table,
1103                                             &flow_pay->fl_node,
1104                                             nfp_flower_table_params));
1105 err_release_metadata:
1106         nfp_modify_flow_metadata(app, flow_pay);
1107 err_destroy_flow:
1108         kfree(flow_pay->action_data);
1109         kfree(flow_pay->mask_data);
1110         kfree(flow_pay->unmasked_data);
1111         kfree(flow_pay);
1112 err_free_key_ls:
1113         kfree(key_layer);
1114         return err;
1115 }
1116
1117 static void
1118 nfp_flower_remove_merge_flow(struct nfp_app *app,
1119                              struct nfp_fl_payload *del_sub_flow,
1120                              struct nfp_fl_payload *merge_flow)
1121 {
1122         struct nfp_flower_priv *priv = app->priv;
1123         struct nfp_fl_payload_link *link, *temp;
1124         struct nfp_fl_payload *origin;
1125         bool mod = false;
1126         int err;
1127
1128         link = list_first_entry(&merge_flow->linked_flows,
1129                                 struct nfp_fl_payload_link, merge_flow.list);
1130         origin = link->sub_flow.flow;
1131
1132         /* Re-add rule the merge had overwritten if it has not been deleted. */
1133         if (origin != del_sub_flow)
1134                 mod = true;
1135
1136         err = nfp_modify_flow_metadata(app, merge_flow);
1137         if (err) {
1138                 nfp_flower_cmsg_warn(app, "Metadata fail for merge flow delete.\n");
1139                 goto err_free_links;
1140         }
1141
1142         if (!mod) {
1143                 err = nfp_flower_xmit_flow(app, merge_flow,
1144                                            NFP_FLOWER_CMSG_TYPE_FLOW_DEL);
1145                 if (err) {
1146                         nfp_flower_cmsg_warn(app, "Failed to delete merged flow.\n");
1147                         goto err_free_links;
1148                 }
1149         } else {
1150                 __nfp_modify_flow_metadata(priv, origin);
1151                 err = nfp_flower_xmit_flow(app, origin,
1152                                            NFP_FLOWER_CMSG_TYPE_FLOW_MOD);
1153                 if (err)
1154                         nfp_flower_cmsg_warn(app, "Failed to revert merge flow.\n");
1155                 origin->in_hw = true;
1156         }
1157
1158 err_free_links:
1159         /* Clean any links connected with the merged flow. */
1160         list_for_each_entry_safe(link, temp, &merge_flow->linked_flows,
1161                                  merge_flow.list)
1162                 nfp_flower_unlink_flow(link);
1163
1164         kfree(merge_flow->action_data);
1165         kfree(merge_flow->mask_data);
1166         kfree(merge_flow->unmasked_data);
1167         WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table,
1168                                             &merge_flow->fl_node,
1169                                             nfp_flower_table_params));
1170         kfree_rcu(merge_flow, rcu);
1171 }
1172
1173 static void
1174 nfp_flower_del_linked_merge_flows(struct nfp_app *app,
1175                                   struct nfp_fl_payload *sub_flow)
1176 {
1177         struct nfp_fl_payload_link *link, *temp;
1178
1179         /* Remove any merge flow formed from the deleted sub_flow. */
1180         list_for_each_entry_safe(link, temp, &sub_flow->linked_flows,
1181                                  sub_flow.list)
1182                 nfp_flower_remove_merge_flow(app, sub_flow,
1183                                              link->merge_flow.flow);
1184 }
1185
1186 /**
1187  * nfp_flower_del_offload() - Removes a flow from hardware.
1188  * @app:        Pointer to the APP handle
1189  * @netdev:     netdev structure.
1190  * @flow:       TC flower classifier offload structure
1191  *
1192  * Removes a flow from the repeated hash structure and clears the
1193  * action payload. Any flows merged from this are also deleted.
1194  *
1195  * Return: negative value on error, 0 if removed successfully.
1196  */
1197 static int
1198 nfp_flower_del_offload(struct nfp_app *app, struct net_device *netdev,
1199                        struct flow_cls_offload *flow)
1200 {
1201         struct nfp_flower_priv *priv = app->priv;
1202         struct netlink_ext_ack *extack = NULL;
1203         struct nfp_fl_payload *nfp_flow;
1204         struct nfp_port *port = NULL;
1205         int err;
1206
1207         extack = flow->common.extack;
1208         if (nfp_netdev_is_nfp_repr(netdev))
1209                 port = nfp_port_from_netdev(netdev);
1210
1211         nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, netdev);
1212         if (!nfp_flow) {
1213                 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot remove flow that does not exist");
1214                 return -ENOENT;
1215         }
1216
1217         err = nfp_modify_flow_metadata(app, nfp_flow);
1218         if (err)
1219                 goto err_free_merge_flow;
1220
1221         if (nfp_flow->nfp_tun_ipv4_addr)
1222                 nfp_tunnel_del_ipv4_off(app, nfp_flow->nfp_tun_ipv4_addr);
1223
1224         if (!nfp_flow->in_hw) {
1225                 err = 0;
1226                 goto err_free_merge_flow;
1227         }
1228
1229         err = nfp_flower_xmit_flow(app, nfp_flow,
1230                                    NFP_FLOWER_CMSG_TYPE_FLOW_DEL);
1231         /* Fall through on error. */
1232
1233 err_free_merge_flow:
1234         nfp_flower_del_linked_merge_flows(app, nfp_flow);
1235         if (port)
1236                 port->tc_offload_cnt--;
1237         kfree(nfp_flow->action_data);
1238         kfree(nfp_flow->mask_data);
1239         kfree(nfp_flow->unmasked_data);
1240         WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table,
1241                                             &nfp_flow->fl_node,
1242                                             nfp_flower_table_params));
1243         kfree_rcu(nfp_flow, rcu);
1244         return err;
1245 }
1246
1247 static void
1248 __nfp_flower_update_merge_stats(struct nfp_app *app,
1249                                 struct nfp_fl_payload *merge_flow)
1250 {
1251         struct nfp_flower_priv *priv = app->priv;
1252         struct nfp_fl_payload_link *link;
1253         struct nfp_fl_payload *sub_flow;
1254         u64 pkts, bytes, used;
1255         u32 ctx_id;
1256
1257         ctx_id = be32_to_cpu(merge_flow->meta.host_ctx_id);
1258         pkts = priv->stats[ctx_id].pkts;
1259         /* Do not cycle subflows if no stats to distribute. */
1260         if (!pkts)
1261                 return;
1262         bytes = priv->stats[ctx_id].bytes;
1263         used = priv->stats[ctx_id].used;
1264
1265         /* Reset stats for the merge flow. */
1266         priv->stats[ctx_id].pkts = 0;
1267         priv->stats[ctx_id].bytes = 0;
1268
1269         /* The merge flow has received stats updates from firmware.
1270          * Distribute these stats to all subflows that form the merge.
1271          * The stats will collected from TC via the subflows.
1272          */
1273         list_for_each_entry(link, &merge_flow->linked_flows, merge_flow.list) {
1274                 sub_flow = link->sub_flow.flow;
1275                 ctx_id = be32_to_cpu(sub_flow->meta.host_ctx_id);
1276                 priv->stats[ctx_id].pkts += pkts;
1277                 priv->stats[ctx_id].bytes += bytes;
1278                 max_t(u64, priv->stats[ctx_id].used, used);
1279         }
1280 }
1281
1282 static void
1283 nfp_flower_update_merge_stats(struct nfp_app *app,
1284                               struct nfp_fl_payload *sub_flow)
1285 {
1286         struct nfp_fl_payload_link *link;
1287
1288         /* Get merge flows that the subflow forms to distribute their stats. */
1289         list_for_each_entry(link, &sub_flow->linked_flows, sub_flow.list)
1290                 __nfp_flower_update_merge_stats(app, link->merge_flow.flow);
1291 }
1292
1293 /**
1294  * nfp_flower_get_stats() - Populates flow stats obtained from hardware.
1295  * @app:        Pointer to the APP handle
1296  * @netdev:     Netdev structure.
1297  * @flow:       TC flower classifier offload structure
1298  *
1299  * Populates a flow statistics structure which which corresponds to a
1300  * specific flow.
1301  *
1302  * Return: negative value on error, 0 if stats populated successfully.
1303  */
1304 static int
1305 nfp_flower_get_stats(struct nfp_app *app, struct net_device *netdev,
1306                      struct flow_cls_offload *flow)
1307 {
1308         struct nfp_flower_priv *priv = app->priv;
1309         struct netlink_ext_ack *extack = NULL;
1310         struct nfp_fl_payload *nfp_flow;
1311         u32 ctx_id;
1312
1313         extack = flow->common.extack;
1314         nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, netdev);
1315         if (!nfp_flow) {
1316                 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot dump stats for flow that does not exist");
1317                 return -EINVAL;
1318         }
1319
1320         ctx_id = be32_to_cpu(nfp_flow->meta.host_ctx_id);
1321
1322         spin_lock_bh(&priv->stats_lock);
1323         /* If request is for a sub_flow, update stats from merged flows. */
1324         if (!list_empty(&nfp_flow->linked_flows))
1325                 nfp_flower_update_merge_stats(app, nfp_flow);
1326
1327         flow_stats_update(&flow->stats, priv->stats[ctx_id].bytes,
1328                           priv->stats[ctx_id].pkts, priv->stats[ctx_id].used);
1329
1330         priv->stats[ctx_id].pkts = 0;
1331         priv->stats[ctx_id].bytes = 0;
1332         spin_unlock_bh(&priv->stats_lock);
1333
1334         return 0;
1335 }
1336
1337 static int
1338 nfp_flower_repr_offload(struct nfp_app *app, struct net_device *netdev,
1339                         struct flow_cls_offload *flower)
1340 {
1341         if (!eth_proto_is_802_3(flower->common.protocol))
1342                 return -EOPNOTSUPP;
1343
1344         switch (flower->command) {
1345         case FLOW_CLS_REPLACE:
1346                 return nfp_flower_add_offload(app, netdev, flower);
1347         case FLOW_CLS_DESTROY:
1348                 return nfp_flower_del_offload(app, netdev, flower);
1349         case FLOW_CLS_STATS:
1350                 return nfp_flower_get_stats(app, netdev, flower);
1351         default:
1352                 return -EOPNOTSUPP;
1353         }
1354 }
1355
1356 static int nfp_flower_setup_tc_block_cb(enum tc_setup_type type,
1357                                         void *type_data, void *cb_priv)
1358 {
1359         struct nfp_repr *repr = cb_priv;
1360
1361         if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data))
1362                 return -EOPNOTSUPP;
1363
1364         switch (type) {
1365         case TC_SETUP_CLSFLOWER:
1366                 return nfp_flower_repr_offload(repr->app, repr->netdev,
1367                                                type_data);
1368         case TC_SETUP_CLSMATCHALL:
1369                 return nfp_flower_setup_qos_offload(repr->app, repr->netdev,
1370                                                     type_data);
1371         default:
1372                 return -EOPNOTSUPP;
1373         }
1374 }
1375
1376 static LIST_HEAD(nfp_block_cb_list);
1377
1378 static int nfp_flower_setup_tc_block(struct net_device *netdev,
1379                                      struct flow_block_offload *f)
1380 {
1381         struct nfp_repr *repr = netdev_priv(netdev);
1382         struct nfp_flower_repr_priv *repr_priv;
1383         struct flow_block_cb *block_cb;
1384
1385         if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1386                 return -EOPNOTSUPP;
1387
1388         repr_priv = repr->app_priv;
1389         repr_priv->block_shared = f->block_shared;
1390         f->driver_block_list = &nfp_block_cb_list;
1391
1392         switch (f->command) {
1393         case FLOW_BLOCK_BIND:
1394                 if (flow_block_cb_is_busy(nfp_flower_setup_tc_block_cb, repr,
1395                                           &nfp_block_cb_list))
1396                         return -EBUSY;
1397
1398                 block_cb = flow_block_cb_alloc(nfp_flower_setup_tc_block_cb,
1399                                                repr, repr, NULL);
1400                 if (IS_ERR(block_cb))
1401                         return PTR_ERR(block_cb);
1402
1403                 flow_block_cb_add(block_cb, f);
1404                 list_add_tail(&block_cb->driver_list, &nfp_block_cb_list);
1405                 return 0;
1406         case FLOW_BLOCK_UNBIND:
1407                 block_cb = flow_block_cb_lookup(f->block,
1408                                                 nfp_flower_setup_tc_block_cb,
1409                                                 repr);
1410                 if (!block_cb)
1411                         return -ENOENT;
1412
1413                 flow_block_cb_remove(block_cb, f);
1414                 list_del(&block_cb->driver_list);
1415                 return 0;
1416         default:
1417                 return -EOPNOTSUPP;
1418         }
1419 }
1420
1421 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
1422                         enum tc_setup_type type, void *type_data)
1423 {
1424         switch (type) {
1425         case TC_SETUP_BLOCK:
1426                 return nfp_flower_setup_tc_block(netdev, type_data);
1427         default:
1428                 return -EOPNOTSUPP;
1429         }
1430 }
1431
1432 struct nfp_flower_indr_block_cb_priv {
1433         struct net_device *netdev;
1434         struct nfp_app *app;
1435         struct list_head list;
1436 };
1437
1438 static struct nfp_flower_indr_block_cb_priv *
1439 nfp_flower_indr_block_cb_priv_lookup(struct nfp_app *app,
1440                                      struct net_device *netdev)
1441 {
1442         struct nfp_flower_indr_block_cb_priv *cb_priv;
1443         struct nfp_flower_priv *priv = app->priv;
1444
1445         /* All callback list access should be protected by RTNL. */
1446         ASSERT_RTNL();
1447
1448         list_for_each_entry(cb_priv, &priv->indr_block_cb_priv, list)
1449                 if (cb_priv->netdev == netdev)
1450                         return cb_priv;
1451
1452         return NULL;
1453 }
1454
1455 static int nfp_flower_setup_indr_block_cb(enum tc_setup_type type,
1456                                           void *type_data, void *cb_priv)
1457 {
1458         struct nfp_flower_indr_block_cb_priv *priv = cb_priv;
1459         struct flow_cls_offload *flower = type_data;
1460
1461         if (flower->common.chain_index)
1462                 return -EOPNOTSUPP;
1463
1464         switch (type) {
1465         case TC_SETUP_CLSFLOWER:
1466                 return nfp_flower_repr_offload(priv->app, priv->netdev,
1467                                                type_data);
1468         default:
1469                 return -EOPNOTSUPP;
1470         }
1471 }
1472
1473 static void nfp_flower_setup_indr_tc_release(void *cb_priv)
1474 {
1475         struct nfp_flower_indr_block_cb_priv *priv = cb_priv;
1476
1477         list_del(&priv->list);
1478         kfree(priv);
1479 }
1480
1481 static int
1482 nfp_flower_setup_indr_tc_block(struct net_device *netdev, struct nfp_app *app,
1483                                struct flow_block_offload *f)
1484 {
1485         struct nfp_flower_indr_block_cb_priv *cb_priv;
1486         struct nfp_flower_priv *priv = app->priv;
1487         struct flow_block_cb *block_cb;
1488
1489         if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1490             !(f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS &&
1491               nfp_flower_internal_port_can_offload(app, netdev)))
1492                 return -EOPNOTSUPP;
1493
1494         switch (f->command) {
1495         case FLOW_BLOCK_BIND:
1496                 cb_priv = kmalloc(sizeof(*cb_priv), GFP_KERNEL);
1497                 if (!cb_priv)
1498                         return -ENOMEM;
1499
1500                 cb_priv->netdev = netdev;
1501                 cb_priv->app = app;
1502                 list_add(&cb_priv->list, &priv->indr_block_cb_priv);
1503
1504                 block_cb = flow_block_cb_alloc(nfp_flower_setup_indr_block_cb,
1505                                                cb_priv, cb_priv,
1506                                                nfp_flower_setup_indr_tc_release);
1507                 if (IS_ERR(block_cb)) {
1508                         list_del(&cb_priv->list);
1509                         kfree(cb_priv);
1510                         return PTR_ERR(block_cb);
1511                 }
1512
1513                 flow_block_cb_add(block_cb, f);
1514                 list_add_tail(&block_cb->driver_list, &nfp_block_cb_list);
1515                 return 0;
1516         case FLOW_BLOCK_UNBIND:
1517                 cb_priv = nfp_flower_indr_block_cb_priv_lookup(app, netdev);
1518                 if (!cb_priv)
1519                         return -ENOENT;
1520
1521                 block_cb = flow_block_cb_lookup(f->block,
1522                                                 nfp_flower_setup_indr_block_cb,
1523                                                 cb_priv);
1524                 if (!block_cb)
1525                         return -ENOENT;
1526
1527                 flow_block_cb_remove(block_cb, f);
1528                 list_del(&block_cb->driver_list);
1529                 return 0;
1530         default:
1531                 return -EOPNOTSUPP;
1532         }
1533         return 0;
1534 }
1535
1536 static int
1537 nfp_flower_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
1538                             enum tc_setup_type type, void *type_data)
1539 {
1540         switch (type) {
1541         case TC_SETUP_BLOCK:
1542                 return nfp_flower_setup_indr_tc_block(netdev, cb_priv,
1543                                                       type_data);
1544         default:
1545                 return -EOPNOTSUPP;
1546         }
1547 }
1548
1549 int nfp_flower_reg_indir_block_handler(struct nfp_app *app,
1550                                        struct net_device *netdev,
1551                                        unsigned long event)
1552 {
1553         int err;
1554
1555         if (!nfp_fl_is_netdev_to_offload(netdev))
1556                 return NOTIFY_OK;
1557
1558         if (event == NETDEV_REGISTER) {
1559                 err = __tc_indr_block_cb_register(netdev, app,
1560                                                   nfp_flower_indr_setup_tc_cb,
1561                                                   app);
1562                 if (err)
1563                         nfp_flower_cmsg_warn(app,
1564                                              "Indirect block reg failed - %s\n",
1565                                              netdev->name);
1566         } else if (event == NETDEV_UNREGISTER) {
1567                 __tc_indr_block_cb_unregister(netdev,
1568                                               nfp_flower_indr_setup_tc_cb, app);
1569         }
1570
1571         return NOTIFY_OK;
1572 }