1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
4 * Copyright (C) 2021 Marvell.
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/inetdevice.h>
11 #include <linux/rhashtable.h>
12 #include <linux/bitfield.h>
13 #include <net/flow_dissector.h>
14 #include <net/pkt_cls.h>
15 #include <net/tc_act/tc_gact.h>
16 #include <net/tc_act/tc_mirred.h>
17 #include <net/tc_act/tc_vlan.h>
21 #include "otx2_common.h"
24 #define CN10K_MAX_BURST_MANTISSA 0x7FFFULL
25 #define CN10K_MAX_BURST_SIZE 8453888ULL
27 #define CN10K_TLX_BURST_MANTISSA GENMASK_ULL(43, 29)
28 #define CN10K_TLX_BURST_EXPONENT GENMASK_ULL(47, 44)
30 struct otx2_tc_flow_stats {
37 struct rhash_head node;
41 struct otx2_tc_flow_stats stats;
42 spinlock_t lock; /* lock for stats */
49 int otx2_tc_alloc_ent_bitmap(struct otx2_nic *nic)
51 struct otx2_tc_info *tc = &nic->tc_info;
53 if (!nic->flow_cfg->max_flows)
56 /* Max flows changed, free the existing bitmap */
57 kfree(tc->tc_entries_bitmap);
59 tc->tc_entries_bitmap =
60 kcalloc(BITS_TO_LONGS(nic->flow_cfg->max_flows),
61 sizeof(long), GFP_KERNEL);
62 if (!tc->tc_entries_bitmap) {
63 netdev_err(nic->netdev,
64 "Unable to alloc TC flow entries bitmap\n");
70 EXPORT_SYMBOL(otx2_tc_alloc_ent_bitmap);
72 static void otx2_get_egress_burst_cfg(struct otx2_nic *nic, u32 burst,
73 u32 *burst_exp, u32 *burst_mantissa)
75 int max_burst, max_mantissa;
78 if (is_dev_otx2(nic->pdev)) {
79 max_burst = MAX_BURST_SIZE;
80 max_mantissa = MAX_BURST_MANTISSA;
82 max_burst = CN10K_MAX_BURST_SIZE;
83 max_mantissa = CN10K_MAX_BURST_MANTISSA;
86 /* Burst is calculated as
87 * ((256 + BURST_MANTISSA) << (1 + BURST_EXPONENT)) / 256
88 * Max supported burst size is 130,816 bytes.
90 burst = min_t(u32, burst, max_burst);
92 *burst_exp = ilog2(burst) ? ilog2(burst) - 1 : 0;
93 tmp = burst - rounddown_pow_of_two(burst);
94 if (burst < max_mantissa)
95 *burst_mantissa = tmp * 2;
97 *burst_mantissa = tmp / (1ULL << (*burst_exp - 7));
99 *burst_exp = MAX_BURST_EXPONENT;
100 *burst_mantissa = max_mantissa;
104 static void otx2_get_egress_rate_cfg(u64 maxrate, u32 *exp,
105 u32 *mantissa, u32 *div_exp)
109 /* Rate calculation by hardware
111 * PIR_ADD = ((256 + mantissa) << exp) / 256
112 * rate = (2 * PIR_ADD) / ( 1 << div_exp)
113 * The resultant rate is in Mbps.
116 /* 2Mbps to 100Gbps can be expressed with div_exp = 0.
117 * Setting this to '0' will ease the calculation of
118 * exponent and mantissa.
123 *exp = ilog2(maxrate) ? ilog2(maxrate) - 1 : 0;
124 tmp = maxrate - rounddown_pow_of_two(maxrate);
125 if (maxrate < MAX_RATE_MANTISSA)
128 *mantissa = tmp / (1ULL << (*exp - 7));
130 /* Instead of disabling rate limiting, set all values to max */
131 *exp = MAX_RATE_EXPONENT;
132 *mantissa = MAX_RATE_MANTISSA;
136 u64 otx2_get_txschq_rate_regval(struct otx2_nic *nic,
137 u64 maxrate, u32 burst)
139 u32 burst_exp, burst_mantissa;
140 u32 exp, mantissa, div_exp;
143 /* Get exponent and mantissa values from the desired rate */
144 otx2_get_egress_burst_cfg(nic, burst, &burst_exp, &burst_mantissa);
145 otx2_get_egress_rate_cfg(maxrate, &exp, &mantissa, &div_exp);
147 if (is_dev_otx2(nic->pdev)) {
148 regval = FIELD_PREP(TLX_BURST_EXPONENT, (u64)burst_exp) |
149 FIELD_PREP(TLX_BURST_MANTISSA, (u64)burst_mantissa) |
150 FIELD_PREP(TLX_RATE_DIVIDER_EXPONENT, div_exp) |
151 FIELD_PREP(TLX_RATE_EXPONENT, exp) |
152 FIELD_PREP(TLX_RATE_MANTISSA, mantissa) | BIT_ULL(0);
154 regval = FIELD_PREP(CN10K_TLX_BURST_EXPONENT, (u64)burst_exp) |
155 FIELD_PREP(CN10K_TLX_BURST_MANTISSA, (u64)burst_mantissa) |
156 FIELD_PREP(TLX_RATE_DIVIDER_EXPONENT, div_exp) |
157 FIELD_PREP(TLX_RATE_EXPONENT, exp) |
158 FIELD_PREP(TLX_RATE_MANTISSA, mantissa) | BIT_ULL(0);
164 static int otx2_set_matchall_egress_rate(struct otx2_nic *nic,
165 u32 burst, u64 maxrate)
167 struct otx2_hw *hw = &nic->hw;
168 struct nix_txschq_config *req;
171 /* All SQs share the same TL4, so pick the first scheduler */
172 txschq = hw->txschq_list[NIX_TXSCH_LVL_TL4][0];
174 mutex_lock(&nic->mbox.lock);
175 req = otx2_mbox_alloc_msg_nix_txschq_cfg(&nic->mbox);
177 mutex_unlock(&nic->mbox.lock);
181 req->lvl = NIX_TXSCH_LVL_TL4;
183 req->reg[0] = NIX_AF_TL4X_PIR(txschq);
184 req->regval[0] = otx2_get_txschq_rate_regval(nic, maxrate, burst);
186 err = otx2_sync_mbox_msg(&nic->mbox);
187 mutex_unlock(&nic->mbox.lock);
191 static int otx2_tc_validate_flow(struct otx2_nic *nic,
192 struct flow_action *actions,
193 struct netlink_ext_ack *extack)
195 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
196 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
200 if (!flow_action_has_entries(actions)) {
201 NL_SET_ERR_MSG_MOD(extack, "MATCHALL offload called with no action");
205 if (!flow_offload_has_one_action(actions)) {
206 NL_SET_ERR_MSG_MOD(extack,
207 "Egress MATCHALL offload supports only 1 policing action");
213 static int otx2_policer_validate(const struct flow_action *action,
214 const struct flow_action_entry *act,
215 struct netlink_ext_ack *extack)
217 if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
218 NL_SET_ERR_MSG_MOD(extack,
219 "Offload not supported when exceed action is not drop");
223 if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
224 act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
225 NL_SET_ERR_MSG_MOD(extack,
226 "Offload not supported when conform action is not pipe or ok");
230 if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
231 !flow_action_is_last_entry(action, act)) {
232 NL_SET_ERR_MSG_MOD(extack,
233 "Offload not supported when conform action is ok, but action is not last");
237 if (act->police.peakrate_bytes_ps ||
238 act->police.avrate || act->police.overhead) {
239 NL_SET_ERR_MSG_MOD(extack,
240 "Offload not supported when peakrate/avrate/overhead is configured");
247 static int otx2_tc_egress_matchall_install(struct otx2_nic *nic,
248 struct tc_cls_matchall_offload *cls)
250 struct netlink_ext_ack *extack = cls->common.extack;
251 struct flow_action *actions = &cls->rule->action;
252 struct flow_action_entry *entry;
255 err = otx2_tc_validate_flow(nic, actions, extack);
259 if (nic->flags & OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED) {
260 NL_SET_ERR_MSG_MOD(extack,
261 "Only one Egress MATCHALL ratelimiter can be offloaded");
265 entry = &cls->rule->action.entries[0];
267 case FLOW_ACTION_POLICE:
268 err = otx2_policer_validate(&cls->rule->action, entry, extack);
272 if (entry->police.rate_pkt_ps) {
273 NL_SET_ERR_MSG_MOD(extack, "QoS offload not support packets per second");
276 err = otx2_set_matchall_egress_rate(nic, entry->police.burst,
277 otx2_convert_rate(entry->police.rate_bytes_ps));
280 nic->flags |= OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED;
283 NL_SET_ERR_MSG_MOD(extack,
284 "Only police action is supported with Egress MATCHALL offload");
291 static int otx2_tc_egress_matchall_delete(struct otx2_nic *nic,
292 struct tc_cls_matchall_offload *cls)
294 struct netlink_ext_ack *extack = cls->common.extack;
297 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
298 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
302 err = otx2_set_matchall_egress_rate(nic, 0, 0);
303 nic->flags &= ~OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED;
307 static int otx2_tc_act_set_police(struct otx2_nic *nic,
308 struct otx2_tc_flow *node,
309 struct flow_cls_offload *f,
310 u64 rate, u32 burst, u32 mark,
311 struct npc_install_flow_req *req, bool pps)
313 struct netlink_ext_ack *extack = f->common.extack;
314 struct otx2_hw *hw = &nic->hw;
317 rq_idx = find_first_zero_bit(&nic->rq_bmap, hw->rx_queues);
318 if (rq_idx >= hw->rx_queues) {
319 NL_SET_ERR_MSG_MOD(extack, "Police action rules exceeded");
323 mutex_lock(&nic->mbox.lock);
325 rc = cn10k_alloc_leaf_profile(nic, &node->leaf_profile);
327 mutex_unlock(&nic->mbox.lock);
331 rc = cn10k_set_ipolicer_rate(nic, node->leaf_profile, burst, rate, pps);
335 rc = cn10k_map_unmap_rq_policer(nic, rq_idx, node->leaf_profile, true);
339 mutex_unlock(&nic->mbox.lock);
341 req->match_id = mark & 0xFFFFULL;
343 req->op = NIX_RX_ACTIONOP_UCAST;
344 set_bit(rq_idx, &nic->rq_bmap);
345 node->is_act_police = true;
351 if (cn10k_free_leaf_profile(nic, node->leaf_profile))
352 netdev_err(nic->netdev,
353 "Unable to free leaf bandwidth profile(%d)\n",
355 mutex_unlock(&nic->mbox.lock);
359 static int otx2_tc_parse_actions(struct otx2_nic *nic,
360 struct flow_action *flow_action,
361 struct npc_install_flow_req *req,
362 struct flow_cls_offload *f,
363 struct otx2_tc_flow *node)
365 struct netlink_ext_ack *extack = f->common.extack;
366 struct flow_action_entry *act;
367 struct net_device *target;
368 struct otx2_nic *priv;
376 if (!flow_action_has_entries(flow_action)) {
377 NL_SET_ERR_MSG_MOD(extack, "no tc actions specified");
381 flow_action_for_each(i, act, flow_action) {
383 case FLOW_ACTION_DROP:
384 req->op = NIX_RX_ACTIONOP_DROP;
386 case FLOW_ACTION_ACCEPT:
387 req->op = NIX_RX_ACTION_DEFAULT;
389 case FLOW_ACTION_REDIRECT_INGRESS:
391 priv = netdev_priv(target);
392 /* npc_install_flow_req doesn't support passing a target pcifunc */
393 if (rvu_get_pf(nic->pcifunc) != rvu_get_pf(priv->pcifunc)) {
394 NL_SET_ERR_MSG_MOD(extack,
395 "can't redirect to other pf/vf");
398 req->vf = priv->pcifunc & RVU_PFVF_FUNC_MASK;
400 /* if op is already set; avoid overwriting the same */
402 req->op = NIX_RX_ACTION_DEFAULT;
405 case FLOW_ACTION_VLAN_POP:
406 req->vtag0_valid = true;
407 /* use RX_VTAG_TYPE7 which is initialized to strip vlan tag */
408 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE7;
410 case FLOW_ACTION_POLICE:
411 /* Ingress ratelimiting is not supported on OcteonTx2 */
412 if (is_dev_otx2(nic->pdev)) {
413 NL_SET_ERR_MSG_MOD(extack,
414 "Ingress policing not supported on this platform");
418 err = otx2_policer_validate(flow_action, act, extack);
422 if (act->police.rate_bytes_ps > 0) {
423 rate = act->police.rate_bytes_ps * 8;
424 burst = act->police.burst;
425 } else if (act->police.rate_pkt_ps > 0) {
426 /* The algorithm used to calculate rate
427 * mantissa, exponent values for a given token
428 * rate (token can be byte or packet) requires
429 * token rate to be mutiplied by 8.
431 rate = act->police.rate_pkt_ps * 8;
432 burst = act->police.burst_pkt;
437 case FLOW_ACTION_MARK:
441 case FLOW_ACTION_RX_QUEUE_MAPPING:
442 req->op = NIX_RX_ACTIONOP_UCAST;
443 req->index = act->rx_queue;
452 NL_SET_ERR_MSG_MOD(extack,
453 "rate limit police offload requires a single action");
458 return otx2_tc_act_set_police(nic, node, f, rate, burst,
464 static int otx2_tc_prepare_flow(struct otx2_nic *nic, struct otx2_tc_flow *node,
465 struct flow_cls_offload *f,
466 struct npc_install_flow_req *req)
468 struct netlink_ext_ack *extack = f->common.extack;
469 struct flow_msg *flow_spec = &req->packet;
470 struct flow_msg *flow_mask = &req->mask;
471 struct flow_dissector *dissector;
472 struct flow_rule *rule;
475 rule = flow_cls_offload_flow_rule(f);
476 dissector = rule->match.dissector;
478 if ((dissector->used_keys &
479 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
480 BIT(FLOW_DISSECTOR_KEY_BASIC) |
481 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
482 BIT(FLOW_DISSECTOR_KEY_VLAN) |
483 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
484 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
485 BIT(FLOW_DISSECTOR_KEY_PORTS) |
486 BIT(FLOW_DISSECTOR_KEY_IP)))) {
487 netdev_info(nic->netdev, "unsupported flow used key 0x%x",
488 dissector->used_keys);
492 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
493 struct flow_match_basic match;
495 flow_rule_match_basic(rule, &match);
497 /* All EtherTypes can be matched, no hw limitation */
498 flow_spec->etype = match.key->n_proto;
499 flow_mask->etype = match.mask->n_proto;
500 req->features |= BIT_ULL(NPC_ETYPE);
502 if (match.mask->ip_proto &&
503 (match.key->ip_proto != IPPROTO_TCP &&
504 match.key->ip_proto != IPPROTO_UDP &&
505 match.key->ip_proto != IPPROTO_SCTP &&
506 match.key->ip_proto != IPPROTO_ICMP &&
507 match.key->ip_proto != IPPROTO_ICMPV6)) {
508 netdev_info(nic->netdev,
509 "ip_proto=0x%x not supported\n",
510 match.key->ip_proto);
513 if (match.mask->ip_proto)
514 ip_proto = match.key->ip_proto;
516 if (ip_proto == IPPROTO_UDP)
517 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
518 else if (ip_proto == IPPROTO_TCP)
519 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
520 else if (ip_proto == IPPROTO_SCTP)
521 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
522 else if (ip_proto == IPPROTO_ICMP)
523 req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
524 else if (ip_proto == IPPROTO_ICMPV6)
525 req->features |= BIT_ULL(NPC_IPPROTO_ICMP6);
528 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
529 struct flow_match_control match;
531 flow_rule_match_control(rule, &match);
532 if (match.mask->flags & FLOW_DIS_FIRST_FRAG) {
533 NL_SET_ERR_MSG_MOD(extack, "HW doesn't support frag first/later");
537 if (match.mask->flags & FLOW_DIS_IS_FRAGMENT) {
538 if (ntohs(flow_spec->etype) == ETH_P_IP) {
539 flow_spec->ip_flag = IPV4_FLAG_MORE;
540 flow_mask->ip_flag = IPV4_FLAG_MORE;
541 req->features |= BIT_ULL(NPC_IPFRAG_IPV4);
542 } else if (ntohs(flow_spec->etype) == ETH_P_IPV6) {
543 flow_spec->next_header = IPPROTO_FRAGMENT;
544 flow_mask->next_header = 0xff;
545 req->features |= BIT_ULL(NPC_IPFRAG_IPV6);
547 NL_SET_ERR_MSG_MOD(extack, "flow-type should be either IPv4 and IPv6");
553 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
554 struct flow_match_eth_addrs match;
556 flow_rule_match_eth_addrs(rule, &match);
557 if (!is_zero_ether_addr(match.mask->src)) {
558 NL_SET_ERR_MSG_MOD(extack, "src mac match not supported");
562 if (!is_zero_ether_addr(match.mask->dst)) {
563 ether_addr_copy(flow_spec->dmac, (u8 *)&match.key->dst);
564 ether_addr_copy(flow_mask->dmac,
565 (u8 *)&match.mask->dst);
566 req->features |= BIT_ULL(NPC_DMAC);
570 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
571 struct flow_match_ip match;
573 flow_rule_match_ip(rule, &match);
574 if ((ntohs(flow_spec->etype) != ETH_P_IP) &&
576 NL_SET_ERR_MSG_MOD(extack, "tos not supported");
579 if (match.mask->ttl) {
580 NL_SET_ERR_MSG_MOD(extack, "ttl not supported");
583 flow_spec->tos = match.key->tos;
584 flow_mask->tos = match.mask->tos;
585 req->features |= BIT_ULL(NPC_TOS);
588 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
589 struct flow_match_vlan match;
590 u16 vlan_tci, vlan_tci_mask;
592 flow_rule_match_vlan(rule, &match);
594 if (ntohs(match.key->vlan_tpid) != ETH_P_8021Q) {
595 netdev_err(nic->netdev, "vlan tpid 0x%x not supported\n",
596 ntohs(match.key->vlan_tpid));
600 if (match.mask->vlan_id ||
601 match.mask->vlan_dei ||
602 match.mask->vlan_priority) {
603 vlan_tci = match.key->vlan_id |
604 match.key->vlan_dei << 12 |
605 match.key->vlan_priority << 13;
607 vlan_tci_mask = match.mask->vlan_id |
608 match.mask->vlan_dei << 12 |
609 match.mask->vlan_priority << 13;
611 flow_spec->vlan_tci = htons(vlan_tci);
612 flow_mask->vlan_tci = htons(vlan_tci_mask);
613 req->features |= BIT_ULL(NPC_OUTER_VID);
617 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
618 struct flow_match_ipv4_addrs match;
620 flow_rule_match_ipv4_addrs(rule, &match);
622 flow_spec->ip4dst = match.key->dst;
623 flow_mask->ip4dst = match.mask->dst;
624 req->features |= BIT_ULL(NPC_DIP_IPV4);
626 flow_spec->ip4src = match.key->src;
627 flow_mask->ip4src = match.mask->src;
628 req->features |= BIT_ULL(NPC_SIP_IPV4);
629 } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
630 struct flow_match_ipv6_addrs match;
632 flow_rule_match_ipv6_addrs(rule, &match);
634 if (ipv6_addr_loopback(&match.key->dst) ||
635 ipv6_addr_loopback(&match.key->src)) {
636 NL_SET_ERR_MSG_MOD(extack,
637 "Flow matching IPv6 loopback addr not supported");
641 if (!ipv6_addr_any(&match.mask->dst)) {
642 memcpy(&flow_spec->ip6dst,
643 (struct in6_addr *)&match.key->dst,
644 sizeof(flow_spec->ip6dst));
645 memcpy(&flow_mask->ip6dst,
646 (struct in6_addr *)&match.mask->dst,
647 sizeof(flow_spec->ip6dst));
648 req->features |= BIT_ULL(NPC_DIP_IPV6);
651 if (!ipv6_addr_any(&match.mask->src)) {
652 memcpy(&flow_spec->ip6src,
653 (struct in6_addr *)&match.key->src,
654 sizeof(flow_spec->ip6src));
655 memcpy(&flow_mask->ip6src,
656 (struct in6_addr *)&match.mask->src,
657 sizeof(flow_spec->ip6src));
658 req->features |= BIT_ULL(NPC_SIP_IPV6);
662 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
663 struct flow_match_ports match;
665 flow_rule_match_ports(rule, &match);
667 flow_spec->dport = match.key->dst;
668 flow_mask->dport = match.mask->dst;
670 if (flow_mask->dport) {
671 if (ip_proto == IPPROTO_UDP)
672 req->features |= BIT_ULL(NPC_DPORT_UDP);
673 else if (ip_proto == IPPROTO_TCP)
674 req->features |= BIT_ULL(NPC_DPORT_TCP);
675 else if (ip_proto == IPPROTO_SCTP)
676 req->features |= BIT_ULL(NPC_DPORT_SCTP);
679 flow_spec->sport = match.key->src;
680 flow_mask->sport = match.mask->src;
682 if (flow_mask->sport) {
683 if (ip_proto == IPPROTO_UDP)
684 req->features |= BIT_ULL(NPC_SPORT_UDP);
685 else if (ip_proto == IPPROTO_TCP)
686 req->features |= BIT_ULL(NPC_SPORT_TCP);
687 else if (ip_proto == IPPROTO_SCTP)
688 req->features |= BIT_ULL(NPC_SPORT_SCTP);
692 return otx2_tc_parse_actions(nic, &rule->action, req, f, node);
695 static int otx2_del_mcam_flow_entry(struct otx2_nic *nic, u16 entry)
697 struct npc_delete_flow_req *req;
700 mutex_lock(&nic->mbox.lock);
701 req = otx2_mbox_alloc_msg_npc_delete_flow(&nic->mbox);
703 mutex_unlock(&nic->mbox.lock);
709 /* Send message to AF */
710 err = otx2_sync_mbox_msg(&nic->mbox);
712 netdev_err(nic->netdev, "Failed to delete MCAM flow entry %d\n",
714 mutex_unlock(&nic->mbox.lock);
717 mutex_unlock(&nic->mbox.lock);
722 static int otx2_tc_del_flow(struct otx2_nic *nic,
723 struct flow_cls_offload *tc_flow_cmd)
725 struct otx2_flow_config *flow_cfg = nic->flow_cfg;
726 struct otx2_tc_info *tc_info = &nic->tc_info;
727 struct otx2_tc_flow *flow_node;
730 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
731 &tc_flow_cmd->cookie,
732 tc_info->flow_ht_params);
734 netdev_err(nic->netdev, "tc flow not found for cookie 0x%lx\n",
735 tc_flow_cmd->cookie);
739 if (flow_node->is_act_police) {
740 mutex_lock(&nic->mbox.lock);
742 err = cn10k_map_unmap_rq_policer(nic, flow_node->rq,
743 flow_node->leaf_profile, false);
745 netdev_err(nic->netdev,
746 "Unmapping RQ %d & profile %d failed\n",
747 flow_node->rq, flow_node->leaf_profile);
749 err = cn10k_free_leaf_profile(nic, flow_node->leaf_profile);
751 netdev_err(nic->netdev,
752 "Unable to free leaf bandwidth profile(%d)\n",
753 flow_node->leaf_profile);
755 __clear_bit(flow_node->rq, &nic->rq_bmap);
757 mutex_unlock(&nic->mbox.lock);
760 otx2_del_mcam_flow_entry(nic, flow_node->entry);
762 WARN_ON(rhashtable_remove_fast(&nic->tc_info.flow_table,
764 nic->tc_info.flow_ht_params));
765 kfree_rcu(flow_node, rcu);
767 clear_bit(flow_node->bitpos, tc_info->tc_entries_bitmap);
768 flow_cfg->nr_flows--;
773 static int otx2_tc_add_flow(struct otx2_nic *nic,
774 struct flow_cls_offload *tc_flow_cmd)
776 struct netlink_ext_ack *extack = tc_flow_cmd->common.extack;
777 struct otx2_flow_config *flow_cfg = nic->flow_cfg;
778 struct otx2_tc_info *tc_info = &nic->tc_info;
779 struct otx2_tc_flow *new_node, *old_node;
780 struct npc_install_flow_req *req, dummy;
783 if (!(nic->flags & OTX2_FLAG_TC_FLOWER_SUPPORT))
786 if (bitmap_full(tc_info->tc_entries_bitmap, flow_cfg->max_flows)) {
787 NL_SET_ERR_MSG_MOD(extack,
788 "Free MCAM entry not available to add the flow");
792 /* allocate memory for the new flow and it's node */
793 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
796 spin_lock_init(&new_node->lock);
797 new_node->cookie = tc_flow_cmd->cookie;
799 memset(&dummy, 0, sizeof(struct npc_install_flow_req));
801 rc = otx2_tc_prepare_flow(nic, new_node, tc_flow_cmd, &dummy);
803 kfree_rcu(new_node, rcu);
807 /* If a flow exists with the same cookie, delete it */
808 old_node = rhashtable_lookup_fast(&tc_info->flow_table,
809 &tc_flow_cmd->cookie,
810 tc_info->flow_ht_params);
812 otx2_tc_del_flow(nic, tc_flow_cmd);
814 mutex_lock(&nic->mbox.lock);
815 req = otx2_mbox_alloc_msg_npc_install_flow(&nic->mbox);
817 mutex_unlock(&nic->mbox.lock);
822 memcpy(&dummy.hdr, &req->hdr, sizeof(struct mbox_msghdr));
823 memcpy(req, &dummy, sizeof(struct npc_install_flow_req));
825 new_node->bitpos = find_first_zero_bit(tc_info->tc_entries_bitmap,
826 flow_cfg->max_flows);
827 req->channel = nic->hw.rx_chan_base;
828 req->entry = flow_cfg->flow_ent[flow_cfg->max_flows - new_node->bitpos - 1];
829 req->intf = NIX_INTF_RX;
831 new_node->entry = req->entry;
833 /* Send message to AF */
834 rc = otx2_sync_mbox_msg(&nic->mbox);
836 NL_SET_ERR_MSG_MOD(extack, "Failed to install MCAM flow entry");
837 mutex_unlock(&nic->mbox.lock);
838 kfree_rcu(new_node, rcu);
841 mutex_unlock(&nic->mbox.lock);
843 /* add new flow to flow-table */
844 rc = rhashtable_insert_fast(&nic->tc_info.flow_table, &new_node->node,
845 nic->tc_info.flow_ht_params);
847 otx2_del_mcam_flow_entry(nic, req->entry);
848 kfree_rcu(new_node, rcu);
852 set_bit(new_node->bitpos, tc_info->tc_entries_bitmap);
853 flow_cfg->nr_flows++;
858 if (new_node->is_act_police) {
859 mutex_lock(&nic->mbox.lock);
861 err = cn10k_map_unmap_rq_policer(nic, new_node->rq,
862 new_node->leaf_profile, false);
864 netdev_err(nic->netdev,
865 "Unmapping RQ %d & profile %d failed\n",
866 new_node->rq, new_node->leaf_profile);
867 err = cn10k_free_leaf_profile(nic, new_node->leaf_profile);
869 netdev_err(nic->netdev,
870 "Unable to free leaf bandwidth profile(%d)\n",
871 new_node->leaf_profile);
873 __clear_bit(new_node->rq, &nic->rq_bmap);
875 mutex_unlock(&nic->mbox.lock);
881 static int otx2_tc_get_flow_stats(struct otx2_nic *nic,
882 struct flow_cls_offload *tc_flow_cmd)
884 struct otx2_tc_info *tc_info = &nic->tc_info;
885 struct npc_mcam_get_stats_req *req;
886 struct npc_mcam_get_stats_rsp *rsp;
887 struct otx2_tc_flow_stats *stats;
888 struct otx2_tc_flow *flow_node;
891 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
892 &tc_flow_cmd->cookie,
893 tc_info->flow_ht_params);
895 netdev_info(nic->netdev, "tc flow not found for cookie %lx",
896 tc_flow_cmd->cookie);
900 mutex_lock(&nic->mbox.lock);
902 req = otx2_mbox_alloc_msg_npc_mcam_entry_stats(&nic->mbox);
904 mutex_unlock(&nic->mbox.lock);
908 req->entry = flow_node->entry;
910 err = otx2_sync_mbox_msg(&nic->mbox);
912 netdev_err(nic->netdev, "Failed to get stats for MCAM flow entry %d\n",
914 mutex_unlock(&nic->mbox.lock);
918 rsp = (struct npc_mcam_get_stats_rsp *)otx2_mbox_get_rsp
919 (&nic->mbox.mbox, 0, &req->hdr);
921 mutex_unlock(&nic->mbox.lock);
925 mutex_unlock(&nic->mbox.lock);
930 stats = &flow_node->stats;
932 spin_lock(&flow_node->lock);
933 flow_stats_update(&tc_flow_cmd->stats, 0x0, rsp->stat - stats->pkts, 0x0, 0x0,
934 FLOW_ACTION_HW_STATS_IMMEDIATE);
935 stats->pkts = rsp->stat;
936 spin_unlock(&flow_node->lock);
941 static int otx2_setup_tc_cls_flower(struct otx2_nic *nic,
942 struct flow_cls_offload *cls_flower)
944 switch (cls_flower->command) {
945 case FLOW_CLS_REPLACE:
946 return otx2_tc_add_flow(nic, cls_flower);
947 case FLOW_CLS_DESTROY:
948 return otx2_tc_del_flow(nic, cls_flower);
950 return otx2_tc_get_flow_stats(nic, cls_flower);
956 static int otx2_tc_ingress_matchall_install(struct otx2_nic *nic,
957 struct tc_cls_matchall_offload *cls)
959 struct netlink_ext_ack *extack = cls->common.extack;
960 struct flow_action *actions = &cls->rule->action;
961 struct flow_action_entry *entry;
965 err = otx2_tc_validate_flow(nic, actions, extack);
969 if (nic->flags & OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED) {
970 NL_SET_ERR_MSG_MOD(extack,
971 "Only one ingress MATCHALL ratelimitter can be offloaded");
975 entry = &cls->rule->action.entries[0];
977 case FLOW_ACTION_POLICE:
978 /* Ingress ratelimiting is not supported on OcteonTx2 */
979 if (is_dev_otx2(nic->pdev)) {
980 NL_SET_ERR_MSG_MOD(extack,
981 "Ingress policing not supported on this platform");
985 err = cn10k_alloc_matchall_ipolicer(nic);
989 /* Convert to bits per second */
990 rate = entry->police.rate_bytes_ps * 8;
991 err = cn10k_set_matchall_ipolicer_rate(nic, entry->police.burst, rate);
994 nic->flags |= OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED;
997 NL_SET_ERR_MSG_MOD(extack,
998 "Only police action supported with Ingress MATCHALL offload");
1005 static int otx2_tc_ingress_matchall_delete(struct otx2_nic *nic,
1006 struct tc_cls_matchall_offload *cls)
1008 struct netlink_ext_ack *extack = cls->common.extack;
1011 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
1012 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
1016 err = cn10k_free_matchall_ipolicer(nic);
1017 nic->flags &= ~OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED;
1021 static int otx2_setup_tc_ingress_matchall(struct otx2_nic *nic,
1022 struct tc_cls_matchall_offload *cls_matchall)
1024 switch (cls_matchall->command) {
1025 case TC_CLSMATCHALL_REPLACE:
1026 return otx2_tc_ingress_matchall_install(nic, cls_matchall);
1027 case TC_CLSMATCHALL_DESTROY:
1028 return otx2_tc_ingress_matchall_delete(nic, cls_matchall);
1029 case TC_CLSMATCHALL_STATS:
1037 static int otx2_setup_tc_block_ingress_cb(enum tc_setup_type type,
1038 void *type_data, void *cb_priv)
1040 struct otx2_nic *nic = cb_priv;
1042 if (!tc_cls_can_offload_and_chain0(nic->netdev, type_data))
1046 case TC_SETUP_CLSFLOWER:
1047 return otx2_setup_tc_cls_flower(nic, type_data);
1048 case TC_SETUP_CLSMATCHALL:
1049 return otx2_setup_tc_ingress_matchall(nic, type_data);
1057 static int otx2_setup_tc_egress_matchall(struct otx2_nic *nic,
1058 struct tc_cls_matchall_offload *cls_matchall)
1060 switch (cls_matchall->command) {
1061 case TC_CLSMATCHALL_REPLACE:
1062 return otx2_tc_egress_matchall_install(nic, cls_matchall);
1063 case TC_CLSMATCHALL_DESTROY:
1064 return otx2_tc_egress_matchall_delete(nic, cls_matchall);
1065 case TC_CLSMATCHALL_STATS:
1073 static int otx2_setup_tc_block_egress_cb(enum tc_setup_type type,
1074 void *type_data, void *cb_priv)
1076 struct otx2_nic *nic = cb_priv;
1078 if (!tc_cls_can_offload_and_chain0(nic->netdev, type_data))
1082 case TC_SETUP_CLSMATCHALL:
1083 return otx2_setup_tc_egress_matchall(nic, type_data);
1091 static LIST_HEAD(otx2_block_cb_list);
1093 static int otx2_setup_tc_block(struct net_device *netdev,
1094 struct flow_block_offload *f)
1096 struct otx2_nic *nic = netdev_priv(netdev);
1097 flow_setup_cb_t *cb;
1100 if (f->block_shared)
1103 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1104 cb = otx2_setup_tc_block_ingress_cb;
1106 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1107 cb = otx2_setup_tc_block_egress_cb;
1113 return flow_block_cb_setup_simple(f, &otx2_block_cb_list, cb,
1117 int otx2_setup_tc(struct net_device *netdev, enum tc_setup_type type,
1121 case TC_SETUP_BLOCK:
1122 return otx2_setup_tc_block(netdev, type_data);
1123 case TC_SETUP_QDISC_HTB:
1124 return otx2_setup_tc_htb(netdev, type_data);
1129 EXPORT_SYMBOL(otx2_setup_tc);
1131 static const struct rhashtable_params tc_flow_ht_params = {
1132 .head_offset = offsetof(struct otx2_tc_flow, node),
1133 .key_offset = offsetof(struct otx2_tc_flow, cookie),
1134 .key_len = sizeof(((struct otx2_tc_flow *)0)->cookie),
1135 .automatic_shrinking = true,
1138 int otx2_init_tc(struct otx2_nic *nic)
1140 struct otx2_tc_info *tc = &nic->tc_info;
1143 /* Exclude receive queue 0 being used for police action */
1144 set_bit(0, &nic->rq_bmap);
1146 if (!nic->flow_cfg) {
1147 netdev_err(nic->netdev,
1148 "Can't init TC, nic->flow_cfg is not setup\n");
1152 err = otx2_tc_alloc_ent_bitmap(nic);
1156 tc->flow_ht_params = tc_flow_ht_params;
1157 err = rhashtable_init(&tc->flow_table, &tc->flow_ht_params);
1159 kfree(tc->tc_entries_bitmap);
1160 tc->tc_entries_bitmap = NULL;
1164 EXPORT_SYMBOL(otx2_init_tc);
1166 void otx2_shutdown_tc(struct otx2_nic *nic)
1168 struct otx2_tc_info *tc = &nic->tc_info;
1170 kfree(tc->tc_entries_bitmap);
1171 rhashtable_destroy(&tc->flow_table);
1173 EXPORT_SYMBOL(otx2_shutdown_tc);