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 #define OTX2_UNSUPP_LSE_DEPTH GENMASK(6, 4)
32 #define MCAST_INVALID_GRP (-1U)
34 struct otx2_tc_flow_stats {
41 struct list_head list;
44 struct otx2_tc_flow_stats stats;
45 spinlock_t lock; /* lock for stats */
51 struct npc_install_flow_req req;
58 static void otx2_get_egress_burst_cfg(struct otx2_nic *nic, u32 burst,
59 u32 *burst_exp, u32 *burst_mantissa)
61 int max_burst, max_mantissa;
64 if (is_dev_otx2(nic->pdev)) {
65 max_burst = MAX_BURST_SIZE;
66 max_mantissa = MAX_BURST_MANTISSA;
68 max_burst = CN10K_MAX_BURST_SIZE;
69 max_mantissa = CN10K_MAX_BURST_MANTISSA;
72 /* Burst is calculated as
73 * ((256 + BURST_MANTISSA) << (1 + BURST_EXPONENT)) / 256
74 * Max supported burst size is 130,816 bytes.
76 burst = min_t(u32, burst, max_burst);
78 *burst_exp = ilog2(burst) ? ilog2(burst) - 1 : 0;
79 tmp = burst - rounddown_pow_of_two(burst);
80 if (burst < max_mantissa)
81 *burst_mantissa = tmp * 2;
83 *burst_mantissa = tmp / (1ULL << (*burst_exp - 7));
85 *burst_exp = MAX_BURST_EXPONENT;
86 *burst_mantissa = max_mantissa;
90 static void otx2_get_egress_rate_cfg(u64 maxrate, u32 *exp,
91 u32 *mantissa, u32 *div_exp)
95 /* Rate calculation by hardware
97 * PIR_ADD = ((256 + mantissa) << exp) / 256
98 * rate = (2 * PIR_ADD) / ( 1 << div_exp)
99 * The resultant rate is in Mbps.
102 /* 2Mbps to 100Gbps can be expressed with div_exp = 0.
103 * Setting this to '0' will ease the calculation of
104 * exponent and mantissa.
109 *exp = ilog2(maxrate) ? ilog2(maxrate) - 1 : 0;
110 tmp = maxrate - rounddown_pow_of_two(maxrate);
111 if (maxrate < MAX_RATE_MANTISSA)
114 *mantissa = tmp / (1ULL << (*exp - 7));
116 /* Instead of disabling rate limiting, set all values to max */
117 *exp = MAX_RATE_EXPONENT;
118 *mantissa = MAX_RATE_MANTISSA;
122 u64 otx2_get_txschq_rate_regval(struct otx2_nic *nic,
123 u64 maxrate, u32 burst)
125 u32 burst_exp, burst_mantissa;
126 u32 exp, mantissa, div_exp;
129 /* Get exponent and mantissa values from the desired rate */
130 otx2_get_egress_burst_cfg(nic, burst, &burst_exp, &burst_mantissa);
131 otx2_get_egress_rate_cfg(maxrate, &exp, &mantissa, &div_exp);
133 if (is_dev_otx2(nic->pdev)) {
134 regval = FIELD_PREP(TLX_BURST_EXPONENT, (u64)burst_exp) |
135 FIELD_PREP(TLX_BURST_MANTISSA, (u64)burst_mantissa) |
136 FIELD_PREP(TLX_RATE_DIVIDER_EXPONENT, div_exp) |
137 FIELD_PREP(TLX_RATE_EXPONENT, exp) |
138 FIELD_PREP(TLX_RATE_MANTISSA, mantissa) | BIT_ULL(0);
140 regval = FIELD_PREP(CN10K_TLX_BURST_EXPONENT, (u64)burst_exp) |
141 FIELD_PREP(CN10K_TLX_BURST_MANTISSA, (u64)burst_mantissa) |
142 FIELD_PREP(TLX_RATE_DIVIDER_EXPONENT, div_exp) |
143 FIELD_PREP(TLX_RATE_EXPONENT, exp) |
144 FIELD_PREP(TLX_RATE_MANTISSA, mantissa) | BIT_ULL(0);
150 static int otx2_set_matchall_egress_rate(struct otx2_nic *nic,
151 u32 burst, u64 maxrate)
153 struct otx2_hw *hw = &nic->hw;
154 struct nix_txschq_config *req;
157 /* All SQs share the same TL4, so pick the first scheduler */
158 txschq = hw->txschq_list[NIX_TXSCH_LVL_TL4][0];
160 mutex_lock(&nic->mbox.lock);
161 req = otx2_mbox_alloc_msg_nix_txschq_cfg(&nic->mbox);
163 mutex_unlock(&nic->mbox.lock);
167 req->lvl = NIX_TXSCH_LVL_TL4;
169 req->reg[0] = NIX_AF_TL4X_PIR(txschq);
170 req->regval[0] = otx2_get_txschq_rate_regval(nic, maxrate, burst);
172 err = otx2_sync_mbox_msg(&nic->mbox);
173 mutex_unlock(&nic->mbox.lock);
177 static int otx2_tc_validate_flow(struct otx2_nic *nic,
178 struct flow_action *actions,
179 struct netlink_ext_ack *extack)
181 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
182 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
186 if (!flow_action_has_entries(actions)) {
187 NL_SET_ERR_MSG_MOD(extack, "MATCHALL offload called with no action");
191 if (!flow_offload_has_one_action(actions)) {
192 NL_SET_ERR_MSG_MOD(extack,
193 "Egress MATCHALL offload supports only 1 policing action");
199 static int otx2_policer_validate(const struct flow_action *action,
200 const struct flow_action_entry *act,
201 struct netlink_ext_ack *extack)
203 if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
204 NL_SET_ERR_MSG_MOD(extack,
205 "Offload not supported when exceed action is not drop");
209 if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
210 act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
211 NL_SET_ERR_MSG_MOD(extack,
212 "Offload not supported when conform action is not pipe or ok");
216 if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
217 !flow_action_is_last_entry(action, act)) {
218 NL_SET_ERR_MSG_MOD(extack,
219 "Offload not supported when conform action is ok, but action is not last");
223 if (act->police.peakrate_bytes_ps ||
224 act->police.avrate || act->police.overhead) {
225 NL_SET_ERR_MSG_MOD(extack,
226 "Offload not supported when peakrate/avrate/overhead is configured");
233 static int otx2_tc_egress_matchall_install(struct otx2_nic *nic,
234 struct tc_cls_matchall_offload *cls)
236 struct netlink_ext_ack *extack = cls->common.extack;
237 struct flow_action *actions = &cls->rule->action;
238 struct flow_action_entry *entry;
241 err = otx2_tc_validate_flow(nic, actions, extack);
245 if (nic->flags & OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED) {
246 NL_SET_ERR_MSG_MOD(extack,
247 "Only one Egress MATCHALL ratelimiter can be offloaded");
251 entry = &cls->rule->action.entries[0];
253 case FLOW_ACTION_POLICE:
254 err = otx2_policer_validate(&cls->rule->action, entry, extack);
258 if (entry->police.rate_pkt_ps) {
259 NL_SET_ERR_MSG_MOD(extack, "QoS offload not support packets per second");
262 err = otx2_set_matchall_egress_rate(nic, entry->police.burst,
263 otx2_convert_rate(entry->police.rate_bytes_ps));
266 nic->flags |= OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED;
269 NL_SET_ERR_MSG_MOD(extack,
270 "Only police action is supported with Egress MATCHALL offload");
277 static int otx2_tc_egress_matchall_delete(struct otx2_nic *nic,
278 struct tc_cls_matchall_offload *cls)
280 struct netlink_ext_ack *extack = cls->common.extack;
283 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
284 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
288 err = otx2_set_matchall_egress_rate(nic, 0, 0);
289 nic->flags &= ~OTX2_FLAG_TC_MATCHALL_EGRESS_ENABLED;
293 static int otx2_tc_act_set_hw_police(struct otx2_nic *nic,
294 struct otx2_tc_flow *node)
298 mutex_lock(&nic->mbox.lock);
300 rc = cn10k_alloc_leaf_profile(nic, &node->leaf_profile);
302 mutex_unlock(&nic->mbox.lock);
306 rc = cn10k_set_ipolicer_rate(nic, node->leaf_profile,
307 node->burst, node->rate, node->is_pps);
311 rc = cn10k_map_unmap_rq_policer(nic, node->rq, node->leaf_profile, true);
315 mutex_unlock(&nic->mbox.lock);
320 if (cn10k_free_leaf_profile(nic, node->leaf_profile))
321 netdev_err(nic->netdev,
322 "Unable to free leaf bandwidth profile(%d)\n",
324 mutex_unlock(&nic->mbox.lock);
328 static int otx2_tc_act_set_police(struct otx2_nic *nic,
329 struct otx2_tc_flow *node,
330 struct flow_cls_offload *f,
331 u64 rate, u32 burst, u32 mark,
332 struct npc_install_flow_req *req, bool pps)
334 struct netlink_ext_ack *extack = f->common.extack;
335 struct otx2_hw *hw = &nic->hw;
338 rq_idx = find_first_zero_bit(&nic->rq_bmap, hw->rx_queues);
339 if (rq_idx >= hw->rx_queues) {
340 NL_SET_ERR_MSG_MOD(extack, "Police action rules exceeded");
344 req->match_id = mark & 0xFFFFULL;
346 req->op = NIX_RX_ACTIONOP_UCAST;
348 node->is_act_police = true;
354 rc = otx2_tc_act_set_hw_police(nic, node);
356 set_bit(rq_idx, &nic->rq_bmap);
361 static int otx2_tc_update_mcast(struct otx2_nic *nic,
362 struct npc_install_flow_req *req,
363 struct netlink_ext_ack *extack,
364 struct otx2_tc_flow *node,
365 struct nix_mcast_grp_update_req *ureq,
368 struct nix_mcast_grp_update_req *grp_update_req;
369 struct nix_mcast_grp_create_req *creq;
370 struct nix_mcast_grp_create_rsp *crsp;
374 mutex_lock(&nic->mbox.lock);
375 creq = otx2_mbox_alloc_msg_nix_mcast_grp_create(&nic->mbox);
381 creq->dir = NIX_MCAST_INGRESS;
382 /* Send message to AF */
383 rc = otx2_sync_mbox_msg(&nic->mbox);
385 NL_SET_ERR_MSG_MOD(extack, "Failed to create multicast group");
389 crsp = (struct nix_mcast_grp_create_rsp *)otx2_mbox_get_rsp(&nic->mbox.mbox,
397 grp_index = crsp->mcast_grp_idx;
398 grp_update_req = otx2_mbox_alloc_msg_nix_mcast_grp_update(&nic->mbox);
399 if (!grp_update_req) {
400 NL_SET_ERR_MSG_MOD(extack, "Failed to update multicast group");
405 ureq->op = NIX_MCAST_OP_ADD_ENTRY;
406 ureq->mcast_grp_idx = grp_index;
407 ureq->num_mce_entry = num_intf;
408 ureq->pcifunc[0] = nic->pcifunc;
409 ureq->channel[0] = nic->hw.tx_chan_base;
411 ureq->dest_type[0] = NIX_RX_RSS;
412 ureq->rq_rss_index[0] = 0;
413 memcpy(&ureq->hdr, &grp_update_req->hdr, sizeof(struct mbox_msghdr));
414 memcpy(grp_update_req, ureq, sizeof(struct nix_mcast_grp_update_req));
416 /* Send message to AF */
417 rc = otx2_sync_mbox_msg(&nic->mbox);
419 NL_SET_ERR_MSG_MOD(extack, "Failed to update multicast group");
423 mutex_unlock(&nic->mbox.lock);
424 req->op = NIX_RX_ACTIONOP_MCAST;
425 req->index = grp_index;
426 node->mcast_grp_idx = grp_index;
430 mutex_unlock(&nic->mbox.lock);
434 static int otx2_tc_parse_actions(struct otx2_nic *nic,
435 struct flow_action *flow_action,
436 struct npc_install_flow_req *req,
437 struct flow_cls_offload *f,
438 struct otx2_tc_flow *node)
440 struct nix_mcast_grp_update_req dummy_grp_update_req = { 0 };
441 struct netlink_ext_ack *extack = f->common.extack;
442 bool pps = false, mcast = false;
443 struct flow_action_entry *act;
444 struct net_device *target;
445 struct otx2_nic *priv;
452 if (!flow_action_has_entries(flow_action)) {
453 NL_SET_ERR_MSG_MOD(extack, "no tc actions specified");
457 flow_action_for_each(i, act, flow_action) {
459 case FLOW_ACTION_DROP:
460 req->op = NIX_RX_ACTIONOP_DROP;
462 case FLOW_ACTION_ACCEPT:
463 req->op = NIX_RX_ACTION_DEFAULT;
465 case FLOW_ACTION_REDIRECT_INGRESS:
467 priv = netdev_priv(target);
468 /* npc_install_flow_req doesn't support passing a target pcifunc */
469 if (rvu_get_pf(nic->pcifunc) != rvu_get_pf(priv->pcifunc)) {
470 NL_SET_ERR_MSG_MOD(extack,
471 "can't redirect to other pf/vf");
474 req->vf = priv->pcifunc & RVU_PFVF_FUNC_MASK;
476 /* if op is already set; avoid overwriting the same */
478 req->op = NIX_RX_ACTION_DEFAULT;
481 case FLOW_ACTION_VLAN_POP:
482 req->vtag0_valid = true;
483 /* use RX_VTAG_TYPE7 which is initialized to strip vlan tag */
484 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE7;
486 case FLOW_ACTION_POLICE:
487 /* Ingress ratelimiting is not supported on OcteonTx2 */
488 if (is_dev_otx2(nic->pdev)) {
489 NL_SET_ERR_MSG_MOD(extack,
490 "Ingress policing not supported on this platform");
494 err = otx2_policer_validate(flow_action, act, extack);
498 if (act->police.rate_bytes_ps > 0) {
499 rate = act->police.rate_bytes_ps * 8;
500 burst = act->police.burst;
501 } else if (act->police.rate_pkt_ps > 0) {
502 /* The algorithm used to calculate rate
503 * mantissa, exponent values for a given token
504 * rate (token can be byte or packet) requires
505 * token rate to be mutiplied by 8.
507 rate = act->police.rate_pkt_ps * 8;
508 burst = act->police.burst_pkt;
513 case FLOW_ACTION_MARK:
517 case FLOW_ACTION_RX_QUEUE_MAPPING:
518 req->op = NIX_RX_ACTIONOP_UCAST;
519 req->index = act->rx_queue;
522 case FLOW_ACTION_MIRRED_INGRESS:
524 priv = netdev_priv(target);
525 dummy_grp_update_req.pcifunc[num_intf] = priv->pcifunc;
526 dummy_grp_update_req.channel[num_intf] = priv->hw.tx_chan_base;
527 dummy_grp_update_req.dest_type[num_intf] = NIX_RX_RSS;
528 dummy_grp_update_req.rq_rss_index[num_intf] = 0;
539 err = otx2_tc_update_mcast(nic, req, extack, node,
540 &dummy_grp_update_req,
547 NL_SET_ERR_MSG_MOD(extack,
548 "rate limit police offload requires a single action");
553 return otx2_tc_act_set_police(nic, node, f, rate, burst,
559 static int otx2_tc_process_vlan(struct otx2_nic *nic, struct flow_msg *flow_spec,
560 struct flow_msg *flow_mask, struct flow_rule *rule,
561 struct npc_install_flow_req *req, bool is_inner)
563 struct flow_match_vlan match;
564 u16 vlan_tci, vlan_tci_mask;
567 flow_rule_match_cvlan(rule, &match);
569 flow_rule_match_vlan(rule, &match);
571 if (!eth_type_vlan(match.key->vlan_tpid)) {
572 netdev_err(nic->netdev, "vlan tpid 0x%x not supported\n",
573 ntohs(match.key->vlan_tpid));
577 if (!match.mask->vlan_id) {
578 struct flow_action_entry *act;
581 flow_action_for_each(i, act, &rule->action) {
582 if (act->id == FLOW_ACTION_DROP) {
583 netdev_err(nic->netdev,
584 "vlan tpid 0x%x with vlan_id %d is not supported for DROP rule.\n",
585 ntohs(match.key->vlan_tpid), match.key->vlan_id);
591 if (match.mask->vlan_id ||
592 match.mask->vlan_dei ||
593 match.mask->vlan_priority) {
594 vlan_tci = match.key->vlan_id |
595 match.key->vlan_dei << 12 |
596 match.key->vlan_priority << 13;
598 vlan_tci_mask = match.mask->vlan_id |
599 match.mask->vlan_dei << 12 |
600 match.mask->vlan_priority << 13;
602 flow_spec->vlan_itci = htons(vlan_tci);
603 flow_mask->vlan_itci = htons(vlan_tci_mask);
604 req->features |= BIT_ULL(NPC_INNER_VID);
606 flow_spec->vlan_tci = htons(vlan_tci);
607 flow_mask->vlan_tci = htons(vlan_tci_mask);
608 req->features |= BIT_ULL(NPC_OUTER_VID);
615 static int otx2_tc_prepare_flow(struct otx2_nic *nic, struct otx2_tc_flow *node,
616 struct flow_cls_offload *f,
617 struct npc_install_flow_req *req)
619 struct netlink_ext_ack *extack = f->common.extack;
620 struct flow_msg *flow_spec = &req->packet;
621 struct flow_msg *flow_mask = &req->mask;
622 struct flow_dissector *dissector;
623 struct flow_rule *rule;
626 rule = flow_cls_offload_flow_rule(f);
627 dissector = rule->match.dissector;
629 if ((dissector->used_keys &
630 ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
631 BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
632 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
633 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
634 BIT(FLOW_DISSECTOR_KEY_CVLAN) |
635 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
636 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
637 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
638 BIT(FLOW_DISSECTOR_KEY_IPSEC) |
639 BIT_ULL(FLOW_DISSECTOR_KEY_MPLS) |
640 BIT_ULL(FLOW_DISSECTOR_KEY_ICMP) |
641 BIT_ULL(FLOW_DISSECTOR_KEY_IP)))) {
642 netdev_info(nic->netdev, "unsupported flow used key 0x%llx",
643 dissector->used_keys);
647 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
648 struct flow_match_basic match;
650 flow_rule_match_basic(rule, &match);
652 /* All EtherTypes can be matched, no hw limitation */
653 flow_spec->etype = match.key->n_proto;
654 flow_mask->etype = match.mask->n_proto;
655 req->features |= BIT_ULL(NPC_ETYPE);
657 if (match.mask->ip_proto &&
658 (match.key->ip_proto != IPPROTO_TCP &&
659 match.key->ip_proto != IPPROTO_UDP &&
660 match.key->ip_proto != IPPROTO_SCTP &&
661 match.key->ip_proto != IPPROTO_ICMP &&
662 match.key->ip_proto != IPPROTO_ESP &&
663 match.key->ip_proto != IPPROTO_AH &&
664 match.key->ip_proto != IPPROTO_ICMPV6)) {
665 netdev_info(nic->netdev,
666 "ip_proto=0x%x not supported\n",
667 match.key->ip_proto);
670 if (match.mask->ip_proto)
671 ip_proto = match.key->ip_proto;
673 if (ip_proto == IPPROTO_UDP)
674 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
675 else if (ip_proto == IPPROTO_TCP)
676 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
677 else if (ip_proto == IPPROTO_SCTP)
678 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
679 else if (ip_proto == IPPROTO_ICMP)
680 req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
681 else if (ip_proto == IPPROTO_ICMPV6)
682 req->features |= BIT_ULL(NPC_IPPROTO_ICMP6);
683 else if (ip_proto == IPPROTO_ESP)
684 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
685 else if (ip_proto == IPPROTO_AH)
686 req->features |= BIT_ULL(NPC_IPPROTO_AH);
689 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
690 struct flow_match_control match;
693 flow_rule_match_control(rule, &match);
694 if (match.mask->flags & FLOW_DIS_FIRST_FRAG) {
695 NL_SET_ERR_MSG_MOD(extack, "HW doesn't support frag first/later");
699 if (match.mask->flags & FLOW_DIS_IS_FRAGMENT) {
700 val = match.key->flags & FLOW_DIS_IS_FRAGMENT;
701 if (ntohs(flow_spec->etype) == ETH_P_IP) {
702 flow_spec->ip_flag = val ? IPV4_FLAG_MORE : 0;
703 flow_mask->ip_flag = IPV4_FLAG_MORE;
704 req->features |= BIT_ULL(NPC_IPFRAG_IPV4);
705 } else if (ntohs(flow_spec->etype) == ETH_P_IPV6) {
706 flow_spec->next_header = val ?
707 IPPROTO_FRAGMENT : 0;
708 flow_mask->next_header = 0xff;
709 req->features |= BIT_ULL(NPC_IPFRAG_IPV6);
711 NL_SET_ERR_MSG_MOD(extack, "flow-type should be either IPv4 and IPv6");
717 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
718 struct flow_match_eth_addrs match;
720 flow_rule_match_eth_addrs(rule, &match);
721 if (!is_zero_ether_addr(match.mask->src)) {
722 NL_SET_ERR_MSG_MOD(extack, "src mac match not supported");
726 if (!is_zero_ether_addr(match.mask->dst)) {
727 ether_addr_copy(flow_spec->dmac, (u8 *)&match.key->dst);
728 ether_addr_copy(flow_mask->dmac,
729 (u8 *)&match.mask->dst);
730 req->features |= BIT_ULL(NPC_DMAC);
734 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPSEC)) {
735 struct flow_match_ipsec match;
737 flow_rule_match_ipsec(rule, &match);
738 if (!match.mask->spi) {
739 NL_SET_ERR_MSG_MOD(extack, "spi index not specified");
742 if (ip_proto != IPPROTO_ESP &&
743 ip_proto != IPPROTO_AH) {
744 NL_SET_ERR_MSG_MOD(extack,
745 "SPI index is valid only for ESP/AH proto");
749 flow_spec->spi = match.key->spi;
750 flow_mask->spi = match.mask->spi;
751 req->features |= BIT_ULL(NPC_IPSEC_SPI);
754 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
755 struct flow_match_ip match;
757 flow_rule_match_ip(rule, &match);
758 if ((ntohs(flow_spec->etype) != ETH_P_IP) &&
760 NL_SET_ERR_MSG_MOD(extack, "tos not supported");
763 if (match.mask->ttl) {
764 NL_SET_ERR_MSG_MOD(extack, "ttl not supported");
767 flow_spec->tos = match.key->tos;
768 flow_mask->tos = match.mask->tos;
769 req->features |= BIT_ULL(NPC_TOS);
772 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
775 ret = otx2_tc_process_vlan(nic, flow_spec, flow_mask, rule, req, false);
780 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
783 ret = otx2_tc_process_vlan(nic, flow_spec, flow_mask, rule, req, true);
788 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
789 struct flow_match_ipv4_addrs match;
791 flow_rule_match_ipv4_addrs(rule, &match);
793 flow_spec->ip4dst = match.key->dst;
794 flow_mask->ip4dst = match.mask->dst;
795 req->features |= BIT_ULL(NPC_DIP_IPV4);
797 flow_spec->ip4src = match.key->src;
798 flow_mask->ip4src = match.mask->src;
799 req->features |= BIT_ULL(NPC_SIP_IPV4);
800 } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
801 struct flow_match_ipv6_addrs match;
803 flow_rule_match_ipv6_addrs(rule, &match);
805 if (ipv6_addr_loopback(&match.key->dst) ||
806 ipv6_addr_loopback(&match.key->src)) {
807 NL_SET_ERR_MSG_MOD(extack,
808 "Flow matching IPv6 loopback addr not supported");
812 if (!ipv6_addr_any(&match.mask->dst)) {
813 memcpy(&flow_spec->ip6dst,
814 (struct in6_addr *)&match.key->dst,
815 sizeof(flow_spec->ip6dst));
816 memcpy(&flow_mask->ip6dst,
817 (struct in6_addr *)&match.mask->dst,
818 sizeof(flow_spec->ip6dst));
819 req->features |= BIT_ULL(NPC_DIP_IPV6);
822 if (!ipv6_addr_any(&match.mask->src)) {
823 memcpy(&flow_spec->ip6src,
824 (struct in6_addr *)&match.key->src,
825 sizeof(flow_spec->ip6src));
826 memcpy(&flow_mask->ip6src,
827 (struct in6_addr *)&match.mask->src,
828 sizeof(flow_spec->ip6src));
829 req->features |= BIT_ULL(NPC_SIP_IPV6);
833 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
834 struct flow_match_ports match;
836 flow_rule_match_ports(rule, &match);
838 flow_spec->dport = match.key->dst;
839 flow_mask->dport = match.mask->dst;
841 if (flow_mask->dport) {
842 if (ip_proto == IPPROTO_UDP)
843 req->features |= BIT_ULL(NPC_DPORT_UDP);
844 else if (ip_proto == IPPROTO_TCP)
845 req->features |= BIT_ULL(NPC_DPORT_TCP);
846 else if (ip_proto == IPPROTO_SCTP)
847 req->features |= BIT_ULL(NPC_DPORT_SCTP);
850 flow_spec->sport = match.key->src;
851 flow_mask->sport = match.mask->src;
853 if (flow_mask->sport) {
854 if (ip_proto == IPPROTO_UDP)
855 req->features |= BIT_ULL(NPC_SPORT_UDP);
856 else if (ip_proto == IPPROTO_TCP)
857 req->features |= BIT_ULL(NPC_SPORT_TCP);
858 else if (ip_proto == IPPROTO_SCTP)
859 req->features |= BIT_ULL(NPC_SPORT_SCTP);
863 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_MPLS)) {
864 struct flow_match_mpls match;
867 flow_rule_match_mpls(rule, &match);
869 if (match.mask->used_lses & OTX2_UNSUPP_LSE_DEPTH) {
870 NL_SET_ERR_MSG_MOD(extack,
871 "unsupported LSE depth for MPLS match offload");
875 for_each_set_bit(bit, (unsigned long *)&match.mask->used_lses,
877 /* check if any of the fields LABEL,TC,BOS are set */
878 if (*((u32 *)&match.mask->ls[bit]) &
879 OTX2_FLOWER_MASK_MPLS_NON_TTL) {
880 /* Hardware will capture 4 byte MPLS header into
881 * two fields NPC_MPLSX_LBTCBOS and NPC_MPLSX_TTL.
882 * Derive the associated NPC key based on header
886 req->features |= BIT_ULL(NPC_MPLS1_LBTCBOS +
888 flow_spec->mpls_lse[bit] =
889 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_LB,
890 match.key->ls[bit].mpls_label) |
891 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_TC,
892 match.key->ls[bit].mpls_tc) |
893 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_BOS,
894 match.key->ls[bit].mpls_bos);
896 flow_mask->mpls_lse[bit] =
897 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_LB,
898 match.mask->ls[bit].mpls_label) |
899 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_TC,
900 match.mask->ls[bit].mpls_tc) |
901 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_BOS,
902 match.mask->ls[bit].mpls_bos);
905 if (match.mask->ls[bit].mpls_ttl) {
906 req->features |= BIT_ULL(NPC_MPLS1_TTL +
908 flow_spec->mpls_lse[bit] |=
909 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_TTL,
910 match.key->ls[bit].mpls_ttl);
911 flow_mask->mpls_lse[bit] |=
912 FIELD_PREP(OTX2_FLOWER_MASK_MPLS_TTL,
913 match.mask->ls[bit].mpls_ttl);
918 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP)) {
919 struct flow_match_icmp match;
921 flow_rule_match_icmp(rule, &match);
923 flow_spec->icmp_type = match.key->type;
924 flow_mask->icmp_type = match.mask->type;
925 req->features |= BIT_ULL(NPC_TYPE_ICMP);
927 flow_spec->icmp_code = match.key->code;
928 flow_mask->icmp_code = match.mask->code;
929 req->features |= BIT_ULL(NPC_CODE_ICMP);
931 return otx2_tc_parse_actions(nic, &rule->action, req, f, node);
934 static void otx2_destroy_tc_flow_list(struct otx2_nic *pfvf)
936 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
937 struct otx2_tc_flow *iter, *tmp;
939 if (!(pfvf->flags & OTX2_FLAG_MCAM_ENTRIES_ALLOC))
942 list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list_tc, list) {
943 list_del(&iter->list);
945 flow_cfg->nr_flows--;
949 static struct otx2_tc_flow *otx2_tc_get_entry_by_cookie(struct otx2_flow_config *flow_cfg,
950 unsigned long cookie)
952 struct otx2_tc_flow *tmp;
954 list_for_each_entry(tmp, &flow_cfg->flow_list_tc, list) {
955 if (tmp->cookie == cookie)
962 static struct otx2_tc_flow *otx2_tc_get_entry_by_index(struct otx2_flow_config *flow_cfg,
965 struct otx2_tc_flow *tmp;
968 list_for_each_entry(tmp, &flow_cfg->flow_list_tc, list) {
977 static void otx2_tc_del_from_flow_list(struct otx2_flow_config *flow_cfg,
978 struct otx2_tc_flow *node)
980 struct list_head *pos, *n;
981 struct otx2_tc_flow *tmp;
983 list_for_each_safe(pos, n, &flow_cfg->flow_list_tc) {
984 tmp = list_entry(pos, struct otx2_tc_flow, list);
986 list_del(&node->list);
992 static int otx2_tc_add_to_flow_list(struct otx2_flow_config *flow_cfg,
993 struct otx2_tc_flow *node)
995 struct list_head *pos, *n;
996 struct otx2_tc_flow *tmp;
999 /* If the flow list is empty then add the new node */
1000 if (list_empty(&flow_cfg->flow_list_tc)) {
1001 list_add(&node->list, &flow_cfg->flow_list_tc);
1005 list_for_each_safe(pos, n, &flow_cfg->flow_list_tc) {
1006 tmp = list_entry(pos, struct otx2_tc_flow, list);
1007 if (node->prio < tmp->prio)
1012 list_add(&node->list, pos->prev);
1016 static int otx2_add_mcam_flow_entry(struct otx2_nic *nic, struct npc_install_flow_req *req)
1018 struct npc_install_flow_req *tmp_req;
1021 mutex_lock(&nic->mbox.lock);
1022 tmp_req = otx2_mbox_alloc_msg_npc_install_flow(&nic->mbox);
1024 mutex_unlock(&nic->mbox.lock);
1028 memcpy(tmp_req, req, sizeof(struct npc_install_flow_req));
1029 /* Send message to AF */
1030 err = otx2_sync_mbox_msg(&nic->mbox);
1032 netdev_err(nic->netdev, "Failed to install MCAM flow entry %d\n",
1034 mutex_unlock(&nic->mbox.lock);
1038 mutex_unlock(&nic->mbox.lock);
1042 static int otx2_del_mcam_flow_entry(struct otx2_nic *nic, u16 entry, u16 *cntr_val)
1044 struct npc_delete_flow_rsp *rsp;
1045 struct npc_delete_flow_req *req;
1048 mutex_lock(&nic->mbox.lock);
1049 req = otx2_mbox_alloc_msg_npc_delete_flow(&nic->mbox);
1051 mutex_unlock(&nic->mbox.lock);
1057 /* Send message to AF */
1058 err = otx2_sync_mbox_msg(&nic->mbox);
1060 netdev_err(nic->netdev, "Failed to delete MCAM flow entry %d\n",
1062 mutex_unlock(&nic->mbox.lock);
1067 rsp = (struct npc_delete_flow_rsp *)otx2_mbox_get_rsp(&nic->mbox.mbox,
1070 netdev_err(nic->netdev, "Failed to get MCAM delete response for entry %d\n",
1072 mutex_unlock(&nic->mbox.lock);
1076 *cntr_val = rsp->cntr_val;
1079 mutex_unlock(&nic->mbox.lock);
1083 static int otx2_tc_update_mcam_table_del_req(struct otx2_nic *nic,
1084 struct otx2_flow_config *flow_cfg,
1085 struct otx2_tc_flow *node)
1087 struct list_head *pos, *n;
1088 struct otx2_tc_flow *tmp;
1089 int i = 0, index = 0;
1092 /* Find and delete the entry from the list and re-install
1093 * all the entries from beginning to the index of the
1094 * deleted entry to higher mcam indexes.
1096 list_for_each_safe(pos, n, &flow_cfg->flow_list_tc) {
1097 tmp = list_entry(pos, struct otx2_tc_flow, list);
1099 list_del(&tmp->list);
1103 otx2_del_mcam_flow_entry(nic, tmp->entry, &cntr_val);
1105 tmp->req.entry = tmp->entry;
1106 tmp->req.cntr_val = cntr_val;
1110 list_for_each_safe(pos, n, &flow_cfg->flow_list_tc) {
1114 tmp = list_entry(pos, struct otx2_tc_flow, list);
1115 otx2_add_mcam_flow_entry(nic, &tmp->req);
1122 static int otx2_tc_update_mcam_table_add_req(struct otx2_nic *nic,
1123 struct otx2_flow_config *flow_cfg,
1124 struct otx2_tc_flow *node)
1126 int mcam_idx = flow_cfg->max_flows - flow_cfg->nr_flows - 1;
1127 struct otx2_tc_flow *tmp;
1131 /* Find the index of the entry(list_idx) whose priority
1132 * is greater than the new entry and re-install all
1133 * the entries from beginning to list_idx to higher
1136 list_idx = otx2_tc_add_to_flow_list(flow_cfg, node);
1137 for (i = 0; i < list_idx; i++) {
1138 tmp = otx2_tc_get_entry_by_index(flow_cfg, i);
1142 otx2_del_mcam_flow_entry(nic, tmp->entry, &cntr_val);
1143 tmp->entry = flow_cfg->flow_ent[mcam_idx];
1144 tmp->req.entry = tmp->entry;
1145 tmp->req.cntr_val = cntr_val;
1146 otx2_add_mcam_flow_entry(nic, &tmp->req);
1153 static int otx2_tc_update_mcam_table(struct otx2_nic *nic,
1154 struct otx2_flow_config *flow_cfg,
1155 struct otx2_tc_flow *node,
1159 return otx2_tc_update_mcam_table_add_req(nic, flow_cfg, node);
1161 return otx2_tc_update_mcam_table_del_req(nic, flow_cfg, node);
1164 static int otx2_tc_del_flow(struct otx2_nic *nic,
1165 struct flow_cls_offload *tc_flow_cmd)
1167 struct otx2_flow_config *flow_cfg = nic->flow_cfg;
1168 struct nix_mcast_grp_destroy_req *grp_destroy_req;
1169 struct otx2_tc_flow *flow_node;
1172 flow_node = otx2_tc_get_entry_by_cookie(flow_cfg, tc_flow_cmd->cookie);
1174 netdev_err(nic->netdev, "tc flow not found for cookie 0x%lx\n",
1175 tc_flow_cmd->cookie);
1179 if (flow_node->is_act_police) {
1180 __clear_bit(flow_node->rq, &nic->rq_bmap);
1182 if (nic->flags & OTX2_FLAG_INTF_DOWN)
1183 goto free_mcam_flow;
1185 mutex_lock(&nic->mbox.lock);
1187 err = cn10k_map_unmap_rq_policer(nic, flow_node->rq,
1188 flow_node->leaf_profile, false);
1190 netdev_err(nic->netdev,
1191 "Unmapping RQ %d & profile %d failed\n",
1192 flow_node->rq, flow_node->leaf_profile);
1194 err = cn10k_free_leaf_profile(nic, flow_node->leaf_profile);
1196 netdev_err(nic->netdev,
1197 "Unable to free leaf bandwidth profile(%d)\n",
1198 flow_node->leaf_profile);
1200 mutex_unlock(&nic->mbox.lock);
1202 /* Remove the multicast/mirror related nodes */
1203 if (flow_node->mcast_grp_idx != MCAST_INVALID_GRP) {
1204 mutex_lock(&nic->mbox.lock);
1205 grp_destroy_req = otx2_mbox_alloc_msg_nix_mcast_grp_destroy(&nic->mbox);
1206 grp_destroy_req->mcast_grp_idx = flow_node->mcast_grp_idx;
1207 otx2_sync_mbox_msg(&nic->mbox);
1208 mutex_unlock(&nic->mbox.lock);
1213 otx2_del_mcam_flow_entry(nic, flow_node->entry, NULL);
1214 otx2_tc_update_mcam_table(nic, flow_cfg, flow_node, false);
1215 kfree_rcu(flow_node, rcu);
1216 flow_cfg->nr_flows--;
1220 static int otx2_tc_add_flow(struct otx2_nic *nic,
1221 struct flow_cls_offload *tc_flow_cmd)
1223 struct netlink_ext_ack *extack = tc_flow_cmd->common.extack;
1224 struct otx2_flow_config *flow_cfg = nic->flow_cfg;
1225 struct otx2_tc_flow *new_node, *old_node;
1226 struct npc_install_flow_req *req, dummy;
1227 int rc, err, mcam_idx;
1229 if (!(nic->flags & OTX2_FLAG_TC_FLOWER_SUPPORT))
1232 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
1233 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
1237 if (flow_cfg->nr_flows == flow_cfg->max_flows) {
1238 NL_SET_ERR_MSG_MOD(extack,
1239 "Free MCAM entry not available to add the flow");
1243 /* allocate memory for the new flow and it's node */
1244 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
1247 spin_lock_init(&new_node->lock);
1248 new_node->cookie = tc_flow_cmd->cookie;
1249 new_node->prio = tc_flow_cmd->common.prio;
1250 new_node->mcast_grp_idx = MCAST_INVALID_GRP;
1252 memset(&dummy, 0, sizeof(struct npc_install_flow_req));
1254 rc = otx2_tc_prepare_flow(nic, new_node, tc_flow_cmd, &dummy);
1256 kfree_rcu(new_node, rcu);
1260 /* If a flow exists with the same cookie, delete it */
1261 old_node = otx2_tc_get_entry_by_cookie(flow_cfg, tc_flow_cmd->cookie);
1263 otx2_tc_del_flow(nic, tc_flow_cmd);
1265 mcam_idx = otx2_tc_update_mcam_table(nic, flow_cfg, new_node, true);
1266 mutex_lock(&nic->mbox.lock);
1267 req = otx2_mbox_alloc_msg_npc_install_flow(&nic->mbox);
1269 mutex_unlock(&nic->mbox.lock);
1274 memcpy(&dummy.hdr, &req->hdr, sizeof(struct mbox_msghdr));
1275 memcpy(req, &dummy, sizeof(struct npc_install_flow_req));
1276 req->channel = nic->hw.rx_chan_base;
1277 req->entry = flow_cfg->flow_ent[mcam_idx];
1278 req->intf = NIX_INTF_RX;
1280 new_node->entry = req->entry;
1282 /* Send message to AF */
1283 rc = otx2_sync_mbox_msg(&nic->mbox);
1285 NL_SET_ERR_MSG_MOD(extack, "Failed to install MCAM flow entry");
1286 mutex_unlock(&nic->mbox.lock);
1290 mutex_unlock(&nic->mbox.lock);
1291 memcpy(&new_node->req, req, sizeof(struct npc_install_flow_req));
1293 flow_cfg->nr_flows++;
1297 otx2_tc_del_from_flow_list(flow_cfg, new_node);
1298 kfree_rcu(new_node, rcu);
1299 if (new_node->is_act_police) {
1300 mutex_lock(&nic->mbox.lock);
1302 err = cn10k_map_unmap_rq_policer(nic, new_node->rq,
1303 new_node->leaf_profile, false);
1305 netdev_err(nic->netdev,
1306 "Unmapping RQ %d & profile %d failed\n",
1307 new_node->rq, new_node->leaf_profile);
1308 err = cn10k_free_leaf_profile(nic, new_node->leaf_profile);
1310 netdev_err(nic->netdev,
1311 "Unable to free leaf bandwidth profile(%d)\n",
1312 new_node->leaf_profile);
1314 __clear_bit(new_node->rq, &nic->rq_bmap);
1316 mutex_unlock(&nic->mbox.lock);
1322 static int otx2_tc_get_flow_stats(struct otx2_nic *nic,
1323 struct flow_cls_offload *tc_flow_cmd)
1325 struct npc_mcam_get_stats_req *req;
1326 struct npc_mcam_get_stats_rsp *rsp;
1327 struct otx2_tc_flow_stats *stats;
1328 struct otx2_tc_flow *flow_node;
1331 flow_node = otx2_tc_get_entry_by_cookie(nic->flow_cfg, tc_flow_cmd->cookie);
1333 netdev_info(nic->netdev, "tc flow not found for cookie %lx",
1334 tc_flow_cmd->cookie);
1338 mutex_lock(&nic->mbox.lock);
1340 req = otx2_mbox_alloc_msg_npc_mcam_entry_stats(&nic->mbox);
1342 mutex_unlock(&nic->mbox.lock);
1346 req->entry = flow_node->entry;
1348 err = otx2_sync_mbox_msg(&nic->mbox);
1350 netdev_err(nic->netdev, "Failed to get stats for MCAM flow entry %d\n",
1352 mutex_unlock(&nic->mbox.lock);
1356 rsp = (struct npc_mcam_get_stats_rsp *)otx2_mbox_get_rsp
1357 (&nic->mbox.mbox, 0, &req->hdr);
1359 mutex_unlock(&nic->mbox.lock);
1360 return PTR_ERR(rsp);
1363 mutex_unlock(&nic->mbox.lock);
1368 stats = &flow_node->stats;
1370 spin_lock(&flow_node->lock);
1371 flow_stats_update(&tc_flow_cmd->stats, 0x0, rsp->stat - stats->pkts, 0x0, 0x0,
1372 FLOW_ACTION_HW_STATS_IMMEDIATE);
1373 stats->pkts = rsp->stat;
1374 spin_unlock(&flow_node->lock);
1379 static int otx2_setup_tc_cls_flower(struct otx2_nic *nic,
1380 struct flow_cls_offload *cls_flower)
1382 switch (cls_flower->command) {
1383 case FLOW_CLS_REPLACE:
1384 return otx2_tc_add_flow(nic, cls_flower);
1385 case FLOW_CLS_DESTROY:
1386 return otx2_tc_del_flow(nic, cls_flower);
1387 case FLOW_CLS_STATS:
1388 return otx2_tc_get_flow_stats(nic, cls_flower);
1394 static int otx2_tc_ingress_matchall_install(struct otx2_nic *nic,
1395 struct tc_cls_matchall_offload *cls)
1397 struct netlink_ext_ack *extack = cls->common.extack;
1398 struct flow_action *actions = &cls->rule->action;
1399 struct flow_action_entry *entry;
1403 err = otx2_tc_validate_flow(nic, actions, extack);
1407 if (nic->flags & OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED) {
1408 NL_SET_ERR_MSG_MOD(extack,
1409 "Only one ingress MATCHALL ratelimitter can be offloaded");
1413 entry = &cls->rule->action.entries[0];
1414 switch (entry->id) {
1415 case FLOW_ACTION_POLICE:
1416 /* Ingress ratelimiting is not supported on OcteonTx2 */
1417 if (is_dev_otx2(nic->pdev)) {
1418 NL_SET_ERR_MSG_MOD(extack,
1419 "Ingress policing not supported on this platform");
1423 err = cn10k_alloc_matchall_ipolicer(nic);
1427 /* Convert to bits per second */
1428 rate = entry->police.rate_bytes_ps * 8;
1429 err = cn10k_set_matchall_ipolicer_rate(nic, entry->police.burst, rate);
1432 nic->flags |= OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED;
1435 NL_SET_ERR_MSG_MOD(extack,
1436 "Only police action supported with Ingress MATCHALL offload");
1443 static int otx2_tc_ingress_matchall_delete(struct otx2_nic *nic,
1444 struct tc_cls_matchall_offload *cls)
1446 struct netlink_ext_ack *extack = cls->common.extack;
1449 if (nic->flags & OTX2_FLAG_INTF_DOWN) {
1450 NL_SET_ERR_MSG_MOD(extack, "Interface not initialized");
1454 err = cn10k_free_matchall_ipolicer(nic);
1455 nic->flags &= ~OTX2_FLAG_TC_MATCHALL_INGRESS_ENABLED;
1459 static int otx2_setup_tc_ingress_matchall(struct otx2_nic *nic,
1460 struct tc_cls_matchall_offload *cls_matchall)
1462 switch (cls_matchall->command) {
1463 case TC_CLSMATCHALL_REPLACE:
1464 return otx2_tc_ingress_matchall_install(nic, cls_matchall);
1465 case TC_CLSMATCHALL_DESTROY:
1466 return otx2_tc_ingress_matchall_delete(nic, cls_matchall);
1467 case TC_CLSMATCHALL_STATS:
1475 static int otx2_setup_tc_block_ingress_cb(enum tc_setup_type type,
1476 void *type_data, void *cb_priv)
1478 struct otx2_nic *nic = cb_priv;
1481 if (!tc_cls_can_offload_and_chain0(nic->netdev, type_data))
1484 ntuple = nic->netdev->features & NETIF_F_NTUPLE;
1486 case TC_SETUP_CLSFLOWER:
1488 netdev_warn(nic->netdev,
1489 "Can't install TC flower offload rule when NTUPLE is active");
1493 return otx2_setup_tc_cls_flower(nic, type_data);
1494 case TC_SETUP_CLSMATCHALL:
1495 return otx2_setup_tc_ingress_matchall(nic, type_data);
1503 static int otx2_setup_tc_egress_matchall(struct otx2_nic *nic,
1504 struct tc_cls_matchall_offload *cls_matchall)
1506 switch (cls_matchall->command) {
1507 case TC_CLSMATCHALL_REPLACE:
1508 return otx2_tc_egress_matchall_install(nic, cls_matchall);
1509 case TC_CLSMATCHALL_DESTROY:
1510 return otx2_tc_egress_matchall_delete(nic, cls_matchall);
1511 case TC_CLSMATCHALL_STATS:
1519 static int otx2_setup_tc_block_egress_cb(enum tc_setup_type type,
1520 void *type_data, void *cb_priv)
1522 struct otx2_nic *nic = cb_priv;
1524 if (!tc_cls_can_offload_and_chain0(nic->netdev, type_data))
1528 case TC_SETUP_CLSMATCHALL:
1529 return otx2_setup_tc_egress_matchall(nic, type_data);
1537 static LIST_HEAD(otx2_block_cb_list);
1539 static int otx2_setup_tc_block(struct net_device *netdev,
1540 struct flow_block_offload *f)
1542 struct otx2_nic *nic = netdev_priv(netdev);
1543 flow_setup_cb_t *cb;
1546 if (f->block_shared)
1549 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1550 cb = otx2_setup_tc_block_ingress_cb;
1552 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1553 cb = otx2_setup_tc_block_egress_cb;
1559 return flow_block_cb_setup_simple(f, &otx2_block_cb_list, cb,
1563 int otx2_setup_tc(struct net_device *netdev, enum tc_setup_type type,
1567 case TC_SETUP_BLOCK:
1568 return otx2_setup_tc_block(netdev, type_data);
1569 case TC_SETUP_QDISC_HTB:
1570 return otx2_setup_tc_htb(netdev, type_data);
1575 EXPORT_SYMBOL(otx2_setup_tc);
1577 int otx2_init_tc(struct otx2_nic *nic)
1579 /* Exclude receive queue 0 being used for police action */
1580 set_bit(0, &nic->rq_bmap);
1582 if (!nic->flow_cfg) {
1583 netdev_err(nic->netdev,
1584 "Can't init TC, nic->flow_cfg is not setup\n");
1590 EXPORT_SYMBOL(otx2_init_tc);
1592 void otx2_shutdown_tc(struct otx2_nic *nic)
1594 otx2_destroy_tc_flow_list(nic);
1596 EXPORT_SYMBOL(otx2_shutdown_tc);
1598 static void otx2_tc_config_ingress_rule(struct otx2_nic *nic,
1599 struct otx2_tc_flow *node)
1601 struct npc_install_flow_req *req;
1603 if (otx2_tc_act_set_hw_police(nic, node))
1606 mutex_lock(&nic->mbox.lock);
1608 req = otx2_mbox_alloc_msg_npc_install_flow(&nic->mbox);
1612 memcpy(req, &node->req, sizeof(struct npc_install_flow_req));
1614 if (otx2_sync_mbox_msg(&nic->mbox))
1615 netdev_err(nic->netdev,
1616 "Failed to install MCAM flow entry for ingress rule");
1618 mutex_unlock(&nic->mbox.lock);
1621 void otx2_tc_apply_ingress_police_rules(struct otx2_nic *nic)
1623 struct otx2_flow_config *flow_cfg = nic->flow_cfg;
1624 struct otx2_tc_flow *node;
1626 /* If any ingress policer rules exist for the interface then
1627 * apply those rules. Ingress policer rules depend on bandwidth
1628 * profiles linked to the receive queues. Since no receive queues
1629 * exist when interface is down, ingress policer rules are stored
1630 * and configured in hardware after all receive queues are allocated
1633 list_for_each_entry(node, &flow_cfg->flow_list_tc, list) {
1634 if (node->is_act_police)
1635 otx2_tc_config_ingress_rule(nic, node);
1638 EXPORT_SYMBOL(otx2_tc_apply_ingress_police_rules);