WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_tc_flower.c
blob1b88bd1c2dbe426cecba5f9de6557983eb62580d
1 /*
2 * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
4 * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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.
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.
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>
40 #include "cxgb4.h"
41 #include "cxgb4_filter.h"
42 #include "cxgb4_tc_flower.h"
44 #define STATS_CHECK_PERIOD (HZ / 2)
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),
63 static const struct cxgb4_natmode_config cxgb4_natmode_config_array[] = {
64 /* Default supported NAT modes */
66 .chip = CHELSIO_T5,
67 .flags = CXGB4_ACTION_NATMODE_NONE,
68 .natmode = NAT_MODE_NONE,
71 .chip = CHELSIO_T5,
72 .flags = CXGB4_ACTION_NATMODE_DIP,
73 .natmode = NAT_MODE_DIP,
76 .chip = CHELSIO_T5,
77 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT,
78 .natmode = NAT_MODE_DIP_DP,
81 .chip = CHELSIO_T5,
82 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
83 CXGB4_ACTION_NATMODE_SIP,
84 .natmode = NAT_MODE_DIP_DP_SIP,
87 .chip = CHELSIO_T5,
88 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
89 CXGB4_ACTION_NATMODE_SPORT,
90 .natmode = NAT_MODE_DIP_DP_SP,
93 .chip = CHELSIO_T5,
94 .flags = CXGB4_ACTION_NATMODE_SIP | CXGB4_ACTION_NATMODE_SPORT,
95 .natmode = NAT_MODE_SIP_SP,
98 .chip = CHELSIO_T5,
99 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
100 CXGB4_ACTION_NATMODE_SPORT,
101 .natmode = NAT_MODE_DIP_SIP_SP,
104 .chip = CHELSIO_T5,
105 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
106 CXGB4_ACTION_NATMODE_DPORT |
107 CXGB4_ACTION_NATMODE_SPORT,
108 .natmode = NAT_MODE_ALL,
110 /* T6+ can ignore L4 ports when they're disabled. */
112 .chip = CHELSIO_T6,
113 .flags = CXGB4_ACTION_NATMODE_SIP,
114 .natmode = NAT_MODE_SIP_SP,
117 .chip = CHELSIO_T6,
118 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SPORT,
119 .natmode = NAT_MODE_DIP_DP_SP,
122 .chip = CHELSIO_T6,
123 .flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP,
124 .natmode = NAT_MODE_ALL,
128 static void cxgb4_action_natmode_tweak(struct ch_filter_specification *fs,
129 u8 natmode_flags)
131 u8 i = 0;
133 /* Translate the enabled NAT 4-tuple fields to one of the
134 * hardware supported NAT mode configurations. This ensures
135 * that we pick a valid combination, where the disabled fields
136 * do not get overwritten to 0.
138 for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
139 if (cxgb4_natmode_config_array[i].flags == natmode_flags) {
140 fs->nat_mode = cxgb4_natmode_config_array[i].natmode;
141 return;
146 static struct ch_tc_flower_entry *allocate_flower_entry(void)
148 struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
149 if (new)
150 spin_lock_init(&new->lock);
151 return new;
154 /* Must be called with either RTNL or rcu_read_lock */
155 static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
156 unsigned long flower_cookie)
158 return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
159 adap->flower_ht_params);
162 static void cxgb4_process_flow_match(struct net_device *dev,
163 struct flow_rule *rule,
164 struct ch_filter_specification *fs)
166 u16 addr_type = 0;
168 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
169 struct flow_match_control match;
171 flow_rule_match_control(rule, &match);
172 addr_type = match.key->addr_type;
173 } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
174 addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
175 } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
176 addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
179 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
180 struct flow_match_basic match;
181 u16 ethtype_key, ethtype_mask;
183 flow_rule_match_basic(rule, &match);
184 ethtype_key = ntohs(match.key->n_proto);
185 ethtype_mask = ntohs(match.mask->n_proto);
187 if (ethtype_key == ETH_P_ALL) {
188 ethtype_key = 0;
189 ethtype_mask = 0;
192 if (ethtype_key == ETH_P_IPV6)
193 fs->type = 1;
195 fs->val.ethtype = ethtype_key;
196 fs->mask.ethtype = ethtype_mask;
197 fs->val.proto = match.key->ip_proto;
198 fs->mask.proto = match.mask->ip_proto;
201 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
202 struct flow_match_ipv4_addrs match;
204 flow_rule_match_ipv4_addrs(rule, &match);
205 fs->type = 0;
206 memcpy(&fs->val.lip[0], &match.key->dst, sizeof(match.key->dst));
207 memcpy(&fs->val.fip[0], &match.key->src, sizeof(match.key->src));
208 memcpy(&fs->mask.lip[0], &match.mask->dst, sizeof(match.mask->dst));
209 memcpy(&fs->mask.fip[0], &match.mask->src, sizeof(match.mask->src));
211 /* also initialize nat_lip/fip to same values */
212 memcpy(&fs->nat_lip[0], &match.key->dst, sizeof(match.key->dst));
213 memcpy(&fs->nat_fip[0], &match.key->src, sizeof(match.key->src));
216 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
217 struct flow_match_ipv6_addrs match;
219 flow_rule_match_ipv6_addrs(rule, &match);
220 fs->type = 1;
221 memcpy(&fs->val.lip[0], match.key->dst.s6_addr,
222 sizeof(match.key->dst));
223 memcpy(&fs->val.fip[0], match.key->src.s6_addr,
224 sizeof(match.key->src));
225 memcpy(&fs->mask.lip[0], match.mask->dst.s6_addr,
226 sizeof(match.mask->dst));
227 memcpy(&fs->mask.fip[0], match.mask->src.s6_addr,
228 sizeof(match.mask->src));
230 /* also initialize nat_lip/fip to same values */
231 memcpy(&fs->nat_lip[0], match.key->dst.s6_addr,
232 sizeof(match.key->dst));
233 memcpy(&fs->nat_fip[0], match.key->src.s6_addr,
234 sizeof(match.key->src));
237 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
238 struct flow_match_ports match;
240 flow_rule_match_ports(rule, &match);
241 fs->val.lport = be16_to_cpu(match.key->dst);
242 fs->mask.lport = be16_to_cpu(match.mask->dst);
243 fs->val.fport = be16_to_cpu(match.key->src);
244 fs->mask.fport = be16_to_cpu(match.mask->src);
246 /* also initialize nat_lport/fport to same values */
247 fs->nat_lport = fs->val.lport;
248 fs->nat_fport = fs->val.fport;
251 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
252 struct flow_match_ip match;
254 flow_rule_match_ip(rule, &match);
255 fs->val.tos = match.key->tos;
256 fs->mask.tos = match.mask->tos;
259 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
260 struct flow_match_enc_keyid match;
262 flow_rule_match_enc_keyid(rule, &match);
263 fs->val.vni = be32_to_cpu(match.key->keyid);
264 fs->mask.vni = be32_to_cpu(match.mask->keyid);
265 if (fs->mask.vni) {
266 fs->val.encap_vld = 1;
267 fs->mask.encap_vld = 1;
271 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
272 struct flow_match_vlan match;
273 u16 vlan_tci, vlan_tci_mask;
275 flow_rule_match_vlan(rule, &match);
276 vlan_tci = match.key->vlan_id | (match.key->vlan_priority <<
277 VLAN_PRIO_SHIFT);
278 vlan_tci_mask = match.mask->vlan_id | (match.mask->vlan_priority <<
279 VLAN_PRIO_SHIFT);
280 fs->val.ivlan = vlan_tci;
281 fs->mask.ivlan = vlan_tci_mask;
283 fs->val.ivlan_vld = 1;
284 fs->mask.ivlan_vld = 1;
286 /* Chelsio adapters use ivlan_vld bit to match vlan packets
287 * as 802.1Q. Also, when vlan tag is present in packets,
288 * ethtype match is used then to match on ethtype of inner
289 * header ie. the header following the vlan header.
290 * So, set the ivlan_vld based on ethtype info supplied by
291 * TC for vlan packets if its 802.1Q. And then reset the
292 * ethtype value else, hw will try to match the supplied
293 * ethtype value with ethtype of inner header.
295 if (fs->val.ethtype == ETH_P_8021Q) {
296 fs->val.ethtype = 0;
297 fs->mask.ethtype = 0;
301 /* Match only packets coming from the ingress port where this
302 * filter will be created.
304 fs->val.iport = netdev2pinfo(dev)->port_id;
305 fs->mask.iport = ~0;
308 static int cxgb4_validate_flow_match(struct net_device *dev,
309 struct flow_rule *rule)
311 struct flow_dissector *dissector = rule->match.dissector;
312 u16 ethtype_mask = 0;
313 u16 ethtype_key = 0;
315 if (dissector->used_keys &
316 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
317 BIT(FLOW_DISSECTOR_KEY_BASIC) |
318 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
319 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
320 BIT(FLOW_DISSECTOR_KEY_PORTS) |
321 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
322 BIT(FLOW_DISSECTOR_KEY_VLAN) |
323 BIT(FLOW_DISSECTOR_KEY_IP))) {
324 netdev_warn(dev, "Unsupported key used: 0x%x\n",
325 dissector->used_keys);
326 return -EOPNOTSUPP;
329 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
330 struct flow_match_basic match;
332 flow_rule_match_basic(rule, &match);
333 ethtype_key = ntohs(match.key->n_proto);
334 ethtype_mask = ntohs(match.mask->n_proto);
337 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
338 u16 eth_ip_type = ethtype_key & ethtype_mask;
339 struct flow_match_ip match;
341 if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
342 netdev_err(dev, "IP Key supported only with IPv4/v6");
343 return -EINVAL;
346 flow_rule_match_ip(rule, &match);
347 if (match.mask->ttl) {
348 netdev_warn(dev, "ttl match unsupported for offload");
349 return -EOPNOTSUPP;
353 return 0;
356 static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
357 u8 field)
359 u32 set_val = val & ~mask;
360 u32 offset = 0;
361 u8 size = 1;
362 int i;
364 for (i = 0; i < ARRAY_SIZE(pedits); i++) {
365 if (pedits[i].field == field) {
366 offset = pedits[i].offset;
367 size = pedits[i].size;
368 break;
371 memcpy((u8 *)fs + offset, &set_val, size);
374 static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
375 u32 mask, u32 offset, u8 htype,
376 u8 *natmode_flags)
378 switch (htype) {
379 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
380 switch (offset) {
381 case PEDIT_ETH_DMAC_31_0:
382 fs->newdmac = 1;
383 offload_pedit(fs, val, mask, ETH_DMAC_31_0);
384 break;
385 case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
386 if (~mask & PEDIT_ETH_DMAC_MASK)
387 offload_pedit(fs, val, mask, ETH_DMAC_47_32);
388 else
389 offload_pedit(fs, val >> 16, mask >> 16,
390 ETH_SMAC_15_0);
391 break;
392 case PEDIT_ETH_SMAC_47_16:
393 fs->newsmac = 1;
394 offload_pedit(fs, val, mask, ETH_SMAC_47_16);
396 break;
397 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
398 switch (offset) {
399 case PEDIT_IP4_SRC:
400 offload_pedit(fs, val, mask, IP4_SRC);
401 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
402 break;
403 case PEDIT_IP4_DST:
404 offload_pedit(fs, val, mask, IP4_DST);
405 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
407 break;
408 case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
409 switch (offset) {
410 case PEDIT_IP6_SRC_31_0:
411 offload_pedit(fs, val, mask, IP6_SRC_31_0);
412 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
413 break;
414 case PEDIT_IP6_SRC_63_32:
415 offload_pedit(fs, val, mask, IP6_SRC_63_32);
416 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
417 break;
418 case PEDIT_IP6_SRC_95_64:
419 offload_pedit(fs, val, mask, IP6_SRC_95_64);
420 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
421 break;
422 case PEDIT_IP6_SRC_127_96:
423 offload_pedit(fs, val, mask, IP6_SRC_127_96);
424 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
425 break;
426 case PEDIT_IP6_DST_31_0:
427 offload_pedit(fs, val, mask, IP6_DST_31_0);
428 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
429 break;
430 case PEDIT_IP6_DST_63_32:
431 offload_pedit(fs, val, mask, IP6_DST_63_32);
432 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
433 break;
434 case PEDIT_IP6_DST_95_64:
435 offload_pedit(fs, val, mask, IP6_DST_95_64);
436 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
437 break;
438 case PEDIT_IP6_DST_127_96:
439 offload_pedit(fs, val, mask, IP6_DST_127_96);
440 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
442 break;
443 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
444 switch (offset) {
445 case PEDIT_TCP_SPORT_DPORT:
446 if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
447 fs->nat_fport = val;
448 *natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
449 } else {
450 fs->nat_lport = val >> 16;
451 *natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
454 break;
455 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
456 switch (offset) {
457 case PEDIT_UDP_SPORT_DPORT:
458 if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
459 fs->nat_fport = val;
460 *natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
461 } else {
462 fs->nat_lport = val >> 16;
463 *natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
466 break;
470 static int cxgb4_action_natmode_validate(struct adapter *adap, u8 natmode_flags,
471 struct netlink_ext_ack *extack)
473 u8 i = 0;
475 /* Extract the NAT mode to enable based on what 4-tuple fields
476 * are enabled to be overwritten. This ensures that the
477 * disabled fields don't get overwritten to 0.
479 for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
480 const struct cxgb4_natmode_config *c;
482 c = &cxgb4_natmode_config_array[i];
483 if (CHELSIO_CHIP_VERSION(adap->params.chip) >= c->chip &&
484 natmode_flags == c->flags)
485 return 0;
487 NL_SET_ERR_MSG_MOD(extack, "Unsupported NAT mode 4-tuple combination");
488 return -EOPNOTSUPP;
491 void cxgb4_process_flow_actions(struct net_device *in,
492 struct flow_action *actions,
493 struct ch_filter_specification *fs)
495 struct flow_action_entry *act;
496 u8 natmode_flags = 0;
497 int i;
499 flow_action_for_each(i, act, actions) {
500 switch (act->id) {
501 case FLOW_ACTION_ACCEPT:
502 fs->action = FILTER_PASS;
503 break;
504 case FLOW_ACTION_DROP:
505 fs->action = FILTER_DROP;
506 break;
507 case FLOW_ACTION_MIRRED:
508 case FLOW_ACTION_REDIRECT: {
509 struct net_device *out = act->dev;
510 struct port_info *pi = netdev_priv(out);
512 fs->action = FILTER_SWITCH;
513 fs->eport = pi->port_id;
515 break;
516 case FLOW_ACTION_VLAN_POP:
517 case FLOW_ACTION_VLAN_PUSH:
518 case FLOW_ACTION_VLAN_MANGLE: {
519 u8 prio = act->vlan.prio;
520 u16 vid = act->vlan.vid;
521 u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
522 switch (act->id) {
523 case FLOW_ACTION_VLAN_POP:
524 fs->newvlan |= VLAN_REMOVE;
525 break;
526 case FLOW_ACTION_VLAN_PUSH:
527 fs->newvlan |= VLAN_INSERT;
528 fs->vlan = vlan_tci;
529 break;
530 case FLOW_ACTION_VLAN_MANGLE:
531 fs->newvlan |= VLAN_REWRITE;
532 fs->vlan = vlan_tci;
533 break;
534 default:
535 break;
538 break;
539 case FLOW_ACTION_MANGLE: {
540 u32 mask, val, offset;
541 u8 htype;
543 htype = act->mangle.htype;
544 mask = act->mangle.mask;
545 val = act->mangle.val;
546 offset = act->mangle.offset;
548 process_pedit_field(fs, val, mask, offset, htype,
549 &natmode_flags);
551 break;
552 case FLOW_ACTION_QUEUE:
553 fs->action = FILTER_PASS;
554 fs->dirsteer = 1;
555 fs->iq = act->queue.index;
556 break;
557 default:
558 break;
561 if (natmode_flags)
562 cxgb4_action_natmode_tweak(fs, natmode_flags);
566 static bool valid_l4_mask(u32 mask)
568 u16 hi, lo;
570 /* Either the upper 16-bits (SPORT) OR the lower
571 * 16-bits (DPORT) can be set, but NOT BOTH.
573 hi = (mask >> 16) & 0xFFFF;
574 lo = mask & 0xFFFF;
576 return hi && lo ? false : true;
579 static bool valid_pedit_action(struct net_device *dev,
580 const struct flow_action_entry *act,
581 u8 *natmode_flags)
583 u32 mask, offset;
584 u8 htype;
586 htype = act->mangle.htype;
587 mask = act->mangle.mask;
588 offset = act->mangle.offset;
590 switch (htype) {
591 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
592 switch (offset) {
593 case PEDIT_ETH_DMAC_31_0:
594 case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
595 case PEDIT_ETH_SMAC_47_16:
596 break;
597 default:
598 netdev_err(dev, "%s: Unsupported pedit field\n",
599 __func__);
600 return false;
602 break;
603 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
604 switch (offset) {
605 case PEDIT_IP4_SRC:
606 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
607 break;
608 case PEDIT_IP4_DST:
609 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
610 break;
611 default:
612 netdev_err(dev, "%s: Unsupported pedit field\n",
613 __func__);
614 return false;
616 break;
617 case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
618 switch (offset) {
619 case PEDIT_IP6_SRC_31_0:
620 case PEDIT_IP6_SRC_63_32:
621 case PEDIT_IP6_SRC_95_64:
622 case PEDIT_IP6_SRC_127_96:
623 *natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
624 break;
625 case PEDIT_IP6_DST_31_0:
626 case PEDIT_IP6_DST_63_32:
627 case PEDIT_IP6_DST_95_64:
628 case PEDIT_IP6_DST_127_96:
629 *natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
630 break;
631 default:
632 netdev_err(dev, "%s: Unsupported pedit field\n",
633 __func__);
634 return false;
636 break;
637 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
638 switch (offset) {
639 case PEDIT_TCP_SPORT_DPORT:
640 if (!valid_l4_mask(~mask)) {
641 netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
642 __func__);
643 return false;
645 if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
646 *natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
647 else
648 *natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
649 break;
650 default:
651 netdev_err(dev, "%s: Unsupported pedit field\n",
652 __func__);
653 return false;
655 break;
656 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
657 switch (offset) {
658 case PEDIT_UDP_SPORT_DPORT:
659 if (!valid_l4_mask(~mask)) {
660 netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
661 __func__);
662 return false;
664 if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
665 *natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
666 else
667 *natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
668 break;
669 default:
670 netdev_err(dev, "%s: Unsupported pedit field\n",
671 __func__);
672 return false;
674 break;
675 default:
676 netdev_err(dev, "%s: Unsupported pedit type\n", __func__);
677 return false;
679 return true;
682 int cxgb4_validate_flow_actions(struct net_device *dev,
683 struct flow_action *actions,
684 struct netlink_ext_ack *extack,
685 u8 matchall_filter)
687 struct adapter *adap = netdev2adap(dev);
688 struct flow_action_entry *act;
689 bool act_redir = false;
690 bool act_pedit = false;
691 bool act_vlan = false;
692 u8 natmode_flags = 0;
693 int i;
695 if (!flow_action_basic_hw_stats_check(actions, extack))
696 return -EOPNOTSUPP;
698 flow_action_for_each(i, act, actions) {
699 switch (act->id) {
700 case FLOW_ACTION_ACCEPT:
701 case FLOW_ACTION_DROP:
702 /* Do nothing */
703 break;
704 case FLOW_ACTION_MIRRED:
705 case FLOW_ACTION_REDIRECT: {
706 struct net_device *n_dev, *target_dev;
707 bool found = false;
708 unsigned int i;
710 if (act->id == FLOW_ACTION_MIRRED &&
711 !matchall_filter) {
712 NL_SET_ERR_MSG_MOD(extack,
713 "Egress mirror action is only supported for tc-matchall");
714 return -EOPNOTSUPP;
717 target_dev = act->dev;
718 for_each_port(adap, i) {
719 n_dev = adap->port[i];
720 if (target_dev == n_dev) {
721 found = true;
722 break;
726 /* If interface doesn't belong to our hw, then
727 * the provided output port is not valid
729 if (!found) {
730 netdev_err(dev, "%s: Out port invalid\n",
731 __func__);
732 return -EINVAL;
734 act_redir = true;
736 break;
737 case FLOW_ACTION_VLAN_POP:
738 case FLOW_ACTION_VLAN_PUSH:
739 case FLOW_ACTION_VLAN_MANGLE: {
740 u16 proto = be16_to_cpu(act->vlan.proto);
742 switch (act->id) {
743 case FLOW_ACTION_VLAN_POP:
744 break;
745 case FLOW_ACTION_VLAN_PUSH:
746 case FLOW_ACTION_VLAN_MANGLE:
747 if (proto != ETH_P_8021Q) {
748 netdev_err(dev, "%s: Unsupported vlan proto\n",
749 __func__);
750 return -EOPNOTSUPP;
752 break;
753 default:
754 netdev_err(dev, "%s: Unsupported vlan action\n",
755 __func__);
756 return -EOPNOTSUPP;
758 act_vlan = true;
760 break;
761 case FLOW_ACTION_MANGLE: {
762 bool pedit_valid = valid_pedit_action(dev, act,
763 &natmode_flags);
765 if (!pedit_valid)
766 return -EOPNOTSUPP;
767 act_pedit = true;
769 break;
770 case FLOW_ACTION_QUEUE:
771 /* Do nothing. cxgb4_set_filter will validate */
772 break;
773 default:
774 netdev_err(dev, "%s: Unsupported action\n", __func__);
775 return -EOPNOTSUPP;
779 if ((act_pedit || act_vlan) && !act_redir) {
780 netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
781 __func__);
782 return -EINVAL;
785 if (act_pedit) {
786 int ret;
788 ret = cxgb4_action_natmode_validate(adap, natmode_flags,
789 extack);
790 if (ret)
791 return ret;
794 return 0;
797 static void cxgb4_tc_flower_hash_prio_add(struct adapter *adap, u32 tc_prio)
799 spin_lock_bh(&adap->tids.ftid_lock);
800 if (adap->tids.tc_hash_tids_max_prio < tc_prio)
801 adap->tids.tc_hash_tids_max_prio = tc_prio;
802 spin_unlock_bh(&adap->tids.ftid_lock);
805 static void cxgb4_tc_flower_hash_prio_del(struct adapter *adap, u32 tc_prio)
807 struct tid_info *t = &adap->tids;
808 struct ch_tc_flower_entry *fe;
809 struct rhashtable_iter iter;
810 u32 found = 0;
812 spin_lock_bh(&t->ftid_lock);
813 /* Bail if the current rule is not the one with the max
814 * prio.
816 if (t->tc_hash_tids_max_prio != tc_prio)
817 goto out_unlock;
819 /* Search for the next rule having the same or next lower
820 * max prio.
822 rhashtable_walk_enter(&adap->flower_tbl, &iter);
823 do {
824 rhashtable_walk_start(&iter);
826 fe = rhashtable_walk_next(&iter);
827 while (!IS_ERR_OR_NULL(fe)) {
828 if (fe->fs.hash &&
829 fe->fs.tc_prio <= t->tc_hash_tids_max_prio) {
830 t->tc_hash_tids_max_prio = fe->fs.tc_prio;
831 found++;
833 /* Bail if we found another rule
834 * having the same prio as the
835 * current max one.
837 if (fe->fs.tc_prio == tc_prio)
838 break;
841 fe = rhashtable_walk_next(&iter);
844 rhashtable_walk_stop(&iter);
845 } while (fe == ERR_PTR(-EAGAIN));
846 rhashtable_walk_exit(&iter);
848 if (!found)
849 t->tc_hash_tids_max_prio = 0;
851 out_unlock:
852 spin_unlock_bh(&t->ftid_lock);
855 int cxgb4_flow_rule_replace(struct net_device *dev, struct flow_rule *rule,
856 u32 tc_prio, struct netlink_ext_ack *extack,
857 struct ch_filter_specification *fs, u32 *tid)
859 struct adapter *adap = netdev2adap(dev);
860 struct filter_ctx ctx;
861 u8 inet_family;
862 int fidx, ret;
864 if (cxgb4_validate_flow_actions(dev, &rule->action, extack, 0))
865 return -EOPNOTSUPP;
867 if (cxgb4_validate_flow_match(dev, rule))
868 return -EOPNOTSUPP;
870 cxgb4_process_flow_match(dev, rule, fs);
871 cxgb4_process_flow_actions(dev, &rule->action, fs);
873 fs->hash = is_filter_exact_match(adap, fs);
874 inet_family = fs->type ? PF_INET6 : PF_INET;
876 /* Get a free filter entry TID, where we can insert this new
877 * rule. Only insert rule if its prio doesn't conflict with
878 * existing rules.
880 fidx = cxgb4_get_free_ftid(dev, inet_family, fs->hash,
881 tc_prio);
882 if (fidx < 0) {
883 NL_SET_ERR_MSG_MOD(extack,
884 "No free LETCAM index available");
885 return -ENOMEM;
888 if (fidx < adap->tids.nhpftids) {
889 fs->prio = 1;
890 fs->hash = 0;
893 /* If the rule can be inserted into HASH region, then ignore
894 * the index to normal FILTER region.
896 if (fs->hash)
897 fidx = 0;
899 fs->tc_prio = tc_prio;
901 init_completion(&ctx.completion);
902 ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
903 if (ret) {
904 netdev_err(dev, "%s: filter creation err %d\n",
905 __func__, ret);
906 return ret;
909 /* Wait for reply */
910 ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
911 if (!ret)
912 return -ETIMEDOUT;
914 /* Check if hw returned error for filter creation */
915 if (ctx.result)
916 return ctx.result;
918 *tid = ctx.tid;
920 if (fs->hash)
921 cxgb4_tc_flower_hash_prio_add(adap, tc_prio);
923 return 0;
926 int cxgb4_tc_flower_replace(struct net_device *dev,
927 struct flow_cls_offload *cls)
929 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
930 struct netlink_ext_ack *extack = cls->common.extack;
931 struct adapter *adap = netdev2adap(dev);
932 struct ch_tc_flower_entry *ch_flower;
933 struct ch_filter_specification *fs;
934 int ret;
936 ch_flower = allocate_flower_entry();
937 if (!ch_flower) {
938 netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
939 return -ENOMEM;
942 fs = &ch_flower->fs;
943 fs->hitcnts = 1;
944 fs->tc_cookie = cls->cookie;
946 ret = cxgb4_flow_rule_replace(dev, rule, cls->common.prio, extack, fs,
947 &ch_flower->filter_id);
948 if (ret)
949 goto free_entry;
951 ch_flower->tc_flower_cookie = cls->cookie;
952 ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
953 adap->flower_ht_params);
954 if (ret)
955 goto del_filter;
957 return 0;
959 del_filter:
960 if (fs->hash)
961 cxgb4_tc_flower_hash_prio_del(adap, cls->common.prio);
963 cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
965 free_entry:
966 kfree(ch_flower);
967 return ret;
970 int cxgb4_flow_rule_destroy(struct net_device *dev, u32 tc_prio,
971 struct ch_filter_specification *fs, int tid)
973 struct adapter *adap = netdev2adap(dev);
974 u8 hash;
975 int ret;
977 hash = fs->hash;
979 ret = cxgb4_del_filter(dev, tid, fs);
980 if (ret)
981 return ret;
983 if (hash)
984 cxgb4_tc_flower_hash_prio_del(adap, tc_prio);
986 return ret;
989 int cxgb4_tc_flower_destroy(struct net_device *dev,
990 struct flow_cls_offload *cls)
992 struct adapter *adap = netdev2adap(dev);
993 struct ch_tc_flower_entry *ch_flower;
994 int ret;
996 ch_flower = ch_flower_lookup(adap, cls->cookie);
997 if (!ch_flower)
998 return -ENOENT;
1000 ret = cxgb4_flow_rule_destroy(dev, ch_flower->fs.tc_prio,
1001 &ch_flower->fs, ch_flower->filter_id);
1002 if (ret)
1003 goto err;
1005 ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
1006 adap->flower_ht_params);
1007 if (ret) {
1008 netdev_err(dev, "Flow remove from rhashtable failed");
1009 goto err;
1011 kfree_rcu(ch_flower, rcu);
1013 err:
1014 return ret;
1017 static void ch_flower_stats_handler(struct work_struct *work)
1019 struct adapter *adap = container_of(work, struct adapter,
1020 flower_stats_work);
1021 struct ch_tc_flower_entry *flower_entry;
1022 struct ch_tc_flower_stats *ofld_stats;
1023 struct rhashtable_iter iter;
1024 u64 packets;
1025 u64 bytes;
1026 int ret;
1028 rhashtable_walk_enter(&adap->flower_tbl, &iter);
1029 do {
1030 rhashtable_walk_start(&iter);
1032 while ((flower_entry = rhashtable_walk_next(&iter)) &&
1033 !IS_ERR(flower_entry)) {
1034 ret = cxgb4_get_filter_counters(adap->port[0],
1035 flower_entry->filter_id,
1036 &packets, &bytes,
1037 flower_entry->fs.hash);
1038 if (!ret) {
1039 spin_lock(&flower_entry->lock);
1040 ofld_stats = &flower_entry->stats;
1042 if (ofld_stats->prev_packet_count != packets) {
1043 ofld_stats->prev_packet_count = packets;
1044 ofld_stats->last_used = jiffies;
1046 spin_unlock(&flower_entry->lock);
1050 rhashtable_walk_stop(&iter);
1052 } while (flower_entry == ERR_PTR(-EAGAIN));
1053 rhashtable_walk_exit(&iter);
1054 mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
1057 static void ch_flower_stats_cb(struct timer_list *t)
1059 struct adapter *adap = from_timer(adap, t, flower_stats_timer);
1061 schedule_work(&adap->flower_stats_work);
1064 int cxgb4_tc_flower_stats(struct net_device *dev,
1065 struct flow_cls_offload *cls)
1067 struct adapter *adap = netdev2adap(dev);
1068 struct ch_tc_flower_stats *ofld_stats;
1069 struct ch_tc_flower_entry *ch_flower;
1070 u64 packets;
1071 u64 bytes;
1072 int ret;
1074 ch_flower = ch_flower_lookup(adap, cls->cookie);
1075 if (!ch_flower) {
1076 ret = -ENOENT;
1077 goto err;
1080 ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
1081 &packets, &bytes,
1082 ch_flower->fs.hash);
1083 if (ret < 0)
1084 goto err;
1086 spin_lock_bh(&ch_flower->lock);
1087 ofld_stats = &ch_flower->stats;
1088 if (ofld_stats->packet_count != packets) {
1089 if (ofld_stats->prev_packet_count != packets)
1090 ofld_stats->last_used = jiffies;
1091 flow_stats_update(&cls->stats, bytes - ofld_stats->byte_count,
1092 packets - ofld_stats->packet_count, 0,
1093 ofld_stats->last_used,
1094 FLOW_ACTION_HW_STATS_IMMEDIATE);
1096 ofld_stats->packet_count = packets;
1097 ofld_stats->byte_count = bytes;
1098 ofld_stats->prev_packet_count = packets;
1100 spin_unlock_bh(&ch_flower->lock);
1101 return 0;
1103 err:
1104 return ret;
1107 static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
1108 .nelem_hint = 384,
1109 .head_offset = offsetof(struct ch_tc_flower_entry, node),
1110 .key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
1111 .key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
1112 .max_size = 524288,
1113 .min_size = 512,
1114 .automatic_shrinking = true
1117 int cxgb4_init_tc_flower(struct adapter *adap)
1119 int ret;
1121 if (adap->tc_flower_initialized)
1122 return -EEXIST;
1124 adap->flower_ht_params = cxgb4_tc_flower_ht_params;
1125 ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
1126 if (ret)
1127 return ret;
1129 INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
1130 timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
1131 mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
1132 adap->tc_flower_initialized = true;
1133 return 0;
1136 void cxgb4_cleanup_tc_flower(struct adapter *adap)
1138 if (!adap->tc_flower_initialized)
1139 return;
1141 if (adap->flower_stats_timer.function)
1142 del_timer_sync(&adap->flower_stats_timer);
1143 cancel_work_sync(&adap->flower_stats_work);
1144 rhashtable_destroy(&adap->flower_tbl);
1145 adap->tc_flower_initialized = false;