Back to home page

OSCL-LXR

 
 

    


0001 /* Broadcom NetXtreme-C/E network driver.
0002  *
0003  * Copyright (c) 2017 Broadcom Limited
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation.
0008  */
0009 
0010 #include <linux/netdevice.h>
0011 #include <linux/inetdevice.h>
0012 #include <linux/if_vlan.h>
0013 #include <net/flow_dissector.h>
0014 #include <net/pkt_cls.h>
0015 #include <net/tc_act/tc_gact.h>
0016 #include <net/tc_act/tc_skbedit.h>
0017 #include <net/tc_act/tc_mirred.h>
0018 #include <net/tc_act/tc_vlan.h>
0019 #include <net/tc_act/tc_pedit.h>
0020 #include <net/tc_act/tc_tunnel_key.h>
0021 #include <net/vxlan.h>
0022 
0023 #include "bnxt_hsi.h"
0024 #include "bnxt.h"
0025 #include "bnxt_hwrm.h"
0026 #include "bnxt_sriov.h"
0027 #include "bnxt_tc.h"
0028 #include "bnxt_vfr.h"
0029 
0030 #define BNXT_FID_INVALID            0xffff
0031 #define VLAN_TCI(vid, prio) ((vid) | ((prio) << VLAN_PRIO_SHIFT))
0032 
0033 #define is_vlan_pcp_wildcarded(vlan_tci_mask)   \
0034     ((ntohs(vlan_tci_mask) & VLAN_PRIO_MASK) == 0x0000)
0035 #define is_vlan_pcp_exactmatch(vlan_tci_mask)   \
0036     ((ntohs(vlan_tci_mask) & VLAN_PRIO_MASK) == VLAN_PRIO_MASK)
0037 #define is_vlan_pcp_zero(vlan_tci)  \
0038     ((ntohs(vlan_tci) & VLAN_PRIO_MASK) == 0x0000)
0039 #define is_vid_exactmatch(vlan_tci_mask)    \
0040     ((ntohs(vlan_tci_mask) & VLAN_VID_MASK) == VLAN_VID_MASK)
0041 
0042 static bool is_wildcard(void *mask, int len);
0043 static bool is_exactmatch(void *mask, int len);
0044 /* Return the dst fid of the func for flow forwarding
0045  * For PFs: src_fid is the fid of the PF
0046  * For VF-reps: src_fid the fid of the VF
0047  */
0048 static u16 bnxt_flow_get_dst_fid(struct bnxt *pf_bp, struct net_device *dev)
0049 {
0050     struct bnxt *bp;
0051 
0052     /* check if dev belongs to the same switch */
0053     if (!netdev_port_same_parent_id(pf_bp->dev, dev)) {
0054         netdev_info(pf_bp->dev, "dev(ifindex=%d) not on same switch\n",
0055                 dev->ifindex);
0056         return BNXT_FID_INVALID;
0057     }
0058 
0059     /* Is dev a VF-rep? */
0060     if (bnxt_dev_is_vf_rep(dev))
0061         return bnxt_vf_rep_get_fid(dev);
0062 
0063     bp = netdev_priv(dev);
0064     return bp->pf.fw_fid;
0065 }
0066 
0067 static int bnxt_tc_parse_redir(struct bnxt *bp,
0068                    struct bnxt_tc_actions *actions,
0069                    const struct flow_action_entry *act)
0070 {
0071     struct net_device *dev = act->dev;
0072 
0073     if (!dev) {
0074         netdev_info(bp->dev, "no dev in mirred action\n");
0075         return -EINVAL;
0076     }
0077 
0078     actions->flags |= BNXT_TC_ACTION_FLAG_FWD;
0079     actions->dst_dev = dev;
0080     return 0;
0081 }
0082 
0083 static int bnxt_tc_parse_vlan(struct bnxt *bp,
0084                   struct bnxt_tc_actions *actions,
0085                   const struct flow_action_entry *act)
0086 {
0087     switch (act->id) {
0088     case FLOW_ACTION_VLAN_POP:
0089         actions->flags |= BNXT_TC_ACTION_FLAG_POP_VLAN;
0090         break;
0091     case FLOW_ACTION_VLAN_PUSH:
0092         actions->flags |= BNXT_TC_ACTION_FLAG_PUSH_VLAN;
0093         actions->push_vlan_tci = htons(act->vlan.vid);
0094         actions->push_vlan_tpid = act->vlan.proto;
0095         break;
0096     default:
0097         return -EOPNOTSUPP;
0098     }
0099     return 0;
0100 }
0101 
0102 static int bnxt_tc_parse_tunnel_set(struct bnxt *bp,
0103                     struct bnxt_tc_actions *actions,
0104                     const struct flow_action_entry *act)
0105 {
0106     const struct ip_tunnel_info *tun_info = act->tunnel;
0107     const struct ip_tunnel_key *tun_key = &tun_info->key;
0108 
0109     if (ip_tunnel_info_af(tun_info) != AF_INET) {
0110         netdev_info(bp->dev, "only IPv4 tunnel-encap is supported\n");
0111         return -EOPNOTSUPP;
0112     }
0113 
0114     actions->tun_encap_key = *tun_key;
0115     actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP;
0116     return 0;
0117 }
0118 
0119 /* Key & Mask from the stack comes unaligned in multiple iterations of 4 bytes
0120  * each(u32).
0121  * This routine consolidates such multiple unaligned values into one
0122  * field each for Key & Mask (for src and dst macs separately)
0123  * For example,
0124  *          Mask/Key    Offset  Iteration
0125  *          ==========  ======  =========
0126  *  dst mac     0xffffffff  0   1
0127  *  dst mac     0x0000ffff  4   2
0128  *
0129  *  src mac     0xffff0000  4   1
0130  *  src mac     0xffffffff  8   2
0131  *
0132  * The above combination coming from the stack will be consolidated as
0133  *          Mask/Key
0134  *          ==============
0135  *  src mac:    0xffffffffffff
0136  *  dst mac:    0xffffffffffff
0137  */
0138 static void bnxt_set_l2_key_mask(u32 part_key, u32 part_mask,
0139                  u8 *actual_key, u8 *actual_mask)
0140 {
0141     u32 key = get_unaligned((u32 *)actual_key);
0142     u32 mask = get_unaligned((u32 *)actual_mask);
0143 
0144     part_key &= part_mask;
0145     part_key |= key & ~part_mask;
0146 
0147     put_unaligned(mask | part_mask, (u32 *)actual_mask);
0148     put_unaligned(part_key, (u32 *)actual_key);
0149 }
0150 
0151 static int
0152 bnxt_fill_l2_rewrite_fields(struct bnxt_tc_actions *actions,
0153                 u16 *eth_addr, u16 *eth_addr_mask)
0154 {
0155     u16 *p;
0156     int j;
0157 
0158     if (unlikely(bnxt_eth_addr_key_mask_invalid(eth_addr, eth_addr_mask)))
0159         return -EINVAL;
0160 
0161     if (!is_wildcard(&eth_addr_mask[0], ETH_ALEN)) {
0162         if (!is_exactmatch(&eth_addr_mask[0], ETH_ALEN))
0163             return -EINVAL;
0164         /* FW expects dmac to be in u16 array format */
0165         p = eth_addr;
0166         for (j = 0; j < 3; j++)
0167             actions->l2_rewrite_dmac[j] = cpu_to_be16(*(p + j));
0168     }
0169 
0170     if (!is_wildcard(&eth_addr_mask[ETH_ALEN / 2], ETH_ALEN)) {
0171         if (!is_exactmatch(&eth_addr_mask[ETH_ALEN / 2], ETH_ALEN))
0172             return -EINVAL;
0173         /* FW expects smac to be in u16 array format */
0174         p = &eth_addr[ETH_ALEN / 2];
0175         for (j = 0; j < 3; j++)
0176             actions->l2_rewrite_smac[j] = cpu_to_be16(*(p + j));
0177     }
0178 
0179     return 0;
0180 }
0181 
0182 static int
0183 bnxt_tc_parse_pedit(struct bnxt *bp, struct bnxt_tc_actions *actions,
0184             struct flow_action_entry *act, int act_idx, u8 *eth_addr,
0185             u8 *eth_addr_mask)
0186 {
0187     size_t offset_of_ip6_daddr = offsetof(struct ipv6hdr, daddr);
0188     size_t offset_of_ip6_saddr = offsetof(struct ipv6hdr, saddr);
0189     u32 mask, val, offset, idx;
0190     u8 htype;
0191 
0192     offset = act->mangle.offset;
0193     htype = act->mangle.htype;
0194     mask = ~act->mangle.mask;
0195     val = act->mangle.val;
0196 
0197     switch (htype) {
0198     case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
0199         if (offset > PEDIT_OFFSET_SMAC_LAST_4_BYTES) {
0200             netdev_err(bp->dev,
0201                    "%s: eth_hdr: Invalid pedit field\n",
0202                    __func__);
0203             return -EINVAL;
0204         }
0205         actions->flags |= BNXT_TC_ACTION_FLAG_L2_REWRITE;
0206 
0207         bnxt_set_l2_key_mask(val, mask, &eth_addr[offset],
0208                      &eth_addr_mask[offset]);
0209         break;
0210     case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
0211         actions->flags |= BNXT_TC_ACTION_FLAG_NAT_XLATE;
0212         actions->nat.l3_is_ipv4 = true;
0213         if (offset ==  offsetof(struct iphdr, saddr)) {
0214             actions->nat.src_xlate = true;
0215             actions->nat.l3.ipv4.saddr.s_addr = htonl(val);
0216         } else if (offset ==  offsetof(struct iphdr, daddr)) {
0217             actions->nat.src_xlate = false;
0218             actions->nat.l3.ipv4.daddr.s_addr = htonl(val);
0219         } else {
0220             netdev_err(bp->dev,
0221                    "%s: IPv4_hdr: Invalid pedit field\n",
0222                    __func__);
0223             return -EINVAL;
0224         }
0225 
0226         netdev_dbg(bp->dev, "nat.src_xlate = %d src IP: %pI4 dst ip : %pI4\n",
0227                actions->nat.src_xlate, &actions->nat.l3.ipv4.saddr,
0228                &actions->nat.l3.ipv4.daddr);
0229         break;
0230 
0231     case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
0232         actions->flags |= BNXT_TC_ACTION_FLAG_NAT_XLATE;
0233         actions->nat.l3_is_ipv4 = false;
0234         if (offset >= offsetof(struct ipv6hdr, saddr) &&
0235             offset < offset_of_ip6_daddr) {
0236             /* 16 byte IPv6 address comes in 4 iterations of
0237              * 4byte chunks each
0238              */
0239             actions->nat.src_xlate = true;
0240             idx = (offset - offset_of_ip6_saddr) / 4;
0241             /* First 4bytes will be copied to idx 0 and so on */
0242             actions->nat.l3.ipv6.saddr.s6_addr32[idx] = htonl(val);
0243         } else if (offset >= offset_of_ip6_daddr &&
0244                offset < offset_of_ip6_daddr + 16) {
0245             actions->nat.src_xlate = false;
0246             idx = (offset - offset_of_ip6_daddr) / 4;
0247             actions->nat.l3.ipv6.saddr.s6_addr32[idx] = htonl(val);
0248         } else {
0249             netdev_err(bp->dev,
0250                    "%s: IPv6_hdr: Invalid pedit field\n",
0251                    __func__);
0252             return -EINVAL;
0253         }
0254         break;
0255     case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
0256     case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
0257         /* HW does not support L4 rewrite alone without L3
0258          * rewrite
0259          */
0260         if (!(actions->flags & BNXT_TC_ACTION_FLAG_NAT_XLATE)) {
0261             netdev_err(bp->dev,
0262                    "Need to specify L3 rewrite as well\n");
0263             return -EINVAL;
0264         }
0265         if (actions->nat.src_xlate)
0266             actions->nat.l4.ports.sport = htons(val);
0267         else
0268             actions->nat.l4.ports.dport = htons(val);
0269         netdev_dbg(bp->dev, "actions->nat.sport = %d dport = %d\n",
0270                actions->nat.l4.ports.sport,
0271                actions->nat.l4.ports.dport);
0272         break;
0273     default:
0274         netdev_err(bp->dev, "%s: Unsupported pedit hdr type\n",
0275                __func__);
0276         return -EINVAL;
0277     }
0278     return 0;
0279 }
0280 
0281 static int bnxt_tc_parse_actions(struct bnxt *bp,
0282                  struct bnxt_tc_actions *actions,
0283                  struct flow_action *flow_action,
0284                  struct netlink_ext_ack *extack)
0285 {
0286     /* Used to store the L2 rewrite mask for dmac (6 bytes) followed by
0287      * smac (6 bytes) if rewrite of both is specified, otherwise either
0288      * dmac or smac
0289      */
0290     u16 eth_addr_mask[ETH_ALEN] = { 0 };
0291     /* Used to store the L2 rewrite key for dmac (6 bytes) followed by
0292      * smac (6 bytes) if rewrite of both is specified, otherwise either
0293      * dmac or smac
0294      */
0295     u16 eth_addr[ETH_ALEN] = { 0 };
0296     struct flow_action_entry *act;
0297     int i, rc;
0298 
0299     if (!flow_action_has_entries(flow_action)) {
0300         netdev_info(bp->dev, "no actions\n");
0301         return -EINVAL;
0302     }
0303 
0304     if (!flow_action_basic_hw_stats_check(flow_action, extack))
0305         return -EOPNOTSUPP;
0306 
0307     flow_action_for_each(i, act, flow_action) {
0308         switch (act->id) {
0309         case FLOW_ACTION_DROP:
0310             actions->flags |= BNXT_TC_ACTION_FLAG_DROP;
0311             return 0; /* don't bother with other actions */
0312         case FLOW_ACTION_REDIRECT:
0313             rc = bnxt_tc_parse_redir(bp, actions, act);
0314             if (rc)
0315                 return rc;
0316             break;
0317         case FLOW_ACTION_VLAN_POP:
0318         case FLOW_ACTION_VLAN_PUSH:
0319         case FLOW_ACTION_VLAN_MANGLE:
0320             rc = bnxt_tc_parse_vlan(bp, actions, act);
0321             if (rc)
0322                 return rc;
0323             break;
0324         case FLOW_ACTION_TUNNEL_ENCAP:
0325             rc = bnxt_tc_parse_tunnel_set(bp, actions, act);
0326             if (rc)
0327                 return rc;
0328             break;
0329         case FLOW_ACTION_TUNNEL_DECAP:
0330             actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_DECAP;
0331             break;
0332         /* Packet edit: L2 rewrite, NAT, NAPT */
0333         case FLOW_ACTION_MANGLE:
0334             rc = bnxt_tc_parse_pedit(bp, actions, act, i,
0335                          (u8 *)eth_addr,
0336                          (u8 *)eth_addr_mask);
0337             if (rc)
0338                 return rc;
0339             break;
0340         default:
0341             break;
0342         }
0343     }
0344 
0345     if (actions->flags & BNXT_TC_ACTION_FLAG_L2_REWRITE) {
0346         rc = bnxt_fill_l2_rewrite_fields(actions, eth_addr,
0347                          eth_addr_mask);
0348         if (rc)
0349             return rc;
0350     }
0351 
0352     if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
0353         if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
0354             /* dst_fid is PF's fid */
0355             actions->dst_fid = bp->pf.fw_fid;
0356         } else {
0357             /* find the FID from dst_dev */
0358             actions->dst_fid =
0359                 bnxt_flow_get_dst_fid(bp, actions->dst_dev);
0360             if (actions->dst_fid == BNXT_FID_INVALID)
0361                 return -EINVAL;
0362         }
0363     }
0364 
0365     return 0;
0366 }
0367 
0368 static int bnxt_tc_parse_flow(struct bnxt *bp,
0369                   struct flow_cls_offload *tc_flow_cmd,
0370                   struct bnxt_tc_flow *flow)
0371 {
0372     struct flow_rule *rule = flow_cls_offload_flow_rule(tc_flow_cmd);
0373     struct flow_dissector *dissector = rule->match.dissector;
0374 
0375     /* KEY_CONTROL and KEY_BASIC are needed for forming a meaningful key */
0376     if ((dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_CONTROL)) == 0 ||
0377         (dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_BASIC)) == 0) {
0378         netdev_info(bp->dev, "cannot form TC key: used_keys = 0x%x\n",
0379                 dissector->used_keys);
0380         return -EOPNOTSUPP;
0381     }
0382 
0383     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
0384         struct flow_match_basic match;
0385 
0386         flow_rule_match_basic(rule, &match);
0387         flow->l2_key.ether_type = match.key->n_proto;
0388         flow->l2_mask.ether_type = match.mask->n_proto;
0389 
0390         if (match.key->n_proto == htons(ETH_P_IP) ||
0391             match.key->n_proto == htons(ETH_P_IPV6)) {
0392             flow->l4_key.ip_proto = match.key->ip_proto;
0393             flow->l4_mask.ip_proto = match.mask->ip_proto;
0394         }
0395     }
0396 
0397     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
0398         struct flow_match_eth_addrs match;
0399 
0400         flow_rule_match_eth_addrs(rule, &match);
0401         flow->flags |= BNXT_TC_FLOW_FLAGS_ETH_ADDRS;
0402         ether_addr_copy(flow->l2_key.dmac, match.key->dst);
0403         ether_addr_copy(flow->l2_mask.dmac, match.mask->dst);
0404         ether_addr_copy(flow->l2_key.smac, match.key->src);
0405         ether_addr_copy(flow->l2_mask.smac, match.mask->src);
0406     }
0407 
0408     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
0409         struct flow_match_vlan match;
0410 
0411         flow_rule_match_vlan(rule, &match);
0412         flow->l2_key.inner_vlan_tci =
0413             cpu_to_be16(VLAN_TCI(match.key->vlan_id,
0414                          match.key->vlan_priority));
0415         flow->l2_mask.inner_vlan_tci =
0416             cpu_to_be16((VLAN_TCI(match.mask->vlan_id,
0417                           match.mask->vlan_priority)));
0418         flow->l2_key.inner_vlan_tpid = htons(ETH_P_8021Q);
0419         flow->l2_mask.inner_vlan_tpid = htons(0xffff);
0420         flow->l2_key.num_vlans = 1;
0421     }
0422 
0423     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
0424         struct flow_match_ipv4_addrs match;
0425 
0426         flow_rule_match_ipv4_addrs(rule, &match);
0427         flow->flags |= BNXT_TC_FLOW_FLAGS_IPV4_ADDRS;
0428         flow->l3_key.ipv4.daddr.s_addr = match.key->dst;
0429         flow->l3_mask.ipv4.daddr.s_addr = match.mask->dst;
0430         flow->l3_key.ipv4.saddr.s_addr = match.key->src;
0431         flow->l3_mask.ipv4.saddr.s_addr = match.mask->src;
0432     } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
0433         struct flow_match_ipv6_addrs match;
0434 
0435         flow_rule_match_ipv6_addrs(rule, &match);
0436         flow->flags |= BNXT_TC_FLOW_FLAGS_IPV6_ADDRS;
0437         flow->l3_key.ipv6.daddr = match.key->dst;
0438         flow->l3_mask.ipv6.daddr = match.mask->dst;
0439         flow->l3_key.ipv6.saddr = match.key->src;
0440         flow->l3_mask.ipv6.saddr = match.mask->src;
0441     }
0442 
0443     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
0444         struct flow_match_ports match;
0445 
0446         flow_rule_match_ports(rule, &match);
0447         flow->flags |= BNXT_TC_FLOW_FLAGS_PORTS;
0448         flow->l4_key.ports.dport = match.key->dst;
0449         flow->l4_mask.ports.dport = match.mask->dst;
0450         flow->l4_key.ports.sport = match.key->src;
0451         flow->l4_mask.ports.sport = match.mask->src;
0452     }
0453 
0454     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP)) {
0455         struct flow_match_icmp match;
0456 
0457         flow_rule_match_icmp(rule, &match);
0458         flow->flags |= BNXT_TC_FLOW_FLAGS_ICMP;
0459         flow->l4_key.icmp.type = match.key->type;
0460         flow->l4_key.icmp.code = match.key->code;
0461         flow->l4_mask.icmp.type = match.mask->type;
0462         flow->l4_mask.icmp.code = match.mask->code;
0463     }
0464 
0465     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
0466         struct flow_match_ipv4_addrs match;
0467 
0468         flow_rule_match_enc_ipv4_addrs(rule, &match);
0469         flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS;
0470         flow->tun_key.u.ipv4.dst = match.key->dst;
0471         flow->tun_mask.u.ipv4.dst = match.mask->dst;
0472         flow->tun_key.u.ipv4.src = match.key->src;
0473         flow->tun_mask.u.ipv4.src = match.mask->src;
0474     } else if (flow_rule_match_key(rule,
0475                       FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
0476         return -EOPNOTSUPP;
0477     }
0478 
0479     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
0480         struct flow_match_enc_keyid match;
0481 
0482         flow_rule_match_enc_keyid(rule, &match);
0483         flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ID;
0484         flow->tun_key.tun_id = key32_to_tunnel_id(match.key->keyid);
0485         flow->tun_mask.tun_id = key32_to_tunnel_id(match.mask->keyid);
0486     }
0487 
0488     if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
0489         struct flow_match_ports match;
0490 
0491         flow_rule_match_enc_ports(rule, &match);
0492         flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_PORTS;
0493         flow->tun_key.tp_dst = match.key->dst;
0494         flow->tun_mask.tp_dst = match.mask->dst;
0495         flow->tun_key.tp_src = match.key->src;
0496         flow->tun_mask.tp_src = match.mask->src;
0497     }
0498 
0499     return bnxt_tc_parse_actions(bp, &flow->actions, &rule->action,
0500                      tc_flow_cmd->common.extack);
0501 }
0502 
0503 static int bnxt_hwrm_cfa_flow_free(struct bnxt *bp,
0504                    struct bnxt_tc_flow_node *flow_node)
0505 {
0506     struct hwrm_cfa_flow_free_input *req;
0507     int rc;
0508 
0509     rc = hwrm_req_init(bp, req, HWRM_CFA_FLOW_FREE);
0510     if (!rc) {
0511         if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
0512             req->ext_flow_handle = flow_node->ext_flow_handle;
0513         else
0514             req->flow_handle = flow_node->flow_handle;
0515 
0516         rc = hwrm_req_send(bp, req);
0517     }
0518     if (rc)
0519         netdev_info(bp->dev, "%s: Error rc=%d\n", __func__, rc);
0520 
0521     return rc;
0522 }
0523 
0524 static int ipv6_mask_len(struct in6_addr *mask)
0525 {
0526     int mask_len = 0, i;
0527 
0528     for (i = 0; i < 4; i++)
0529         mask_len += inet_mask_len(mask->s6_addr32[i]);
0530 
0531     return mask_len;
0532 }
0533 
0534 static bool is_wildcard(void *mask, int len)
0535 {
0536     const u8 *p = mask;
0537     int i;
0538 
0539     for (i = 0; i < len; i++) {
0540         if (p[i] != 0)
0541             return false;
0542     }
0543     return true;
0544 }
0545 
0546 static bool is_exactmatch(void *mask, int len)
0547 {
0548     const u8 *p = mask;
0549     int i;
0550 
0551     for (i = 0; i < len; i++)
0552         if (p[i] != 0xff)
0553             return false;
0554 
0555     return true;
0556 }
0557 
0558 static bool is_vlan_tci_allowed(__be16  vlan_tci_mask,
0559                 __be16  vlan_tci)
0560 {
0561     /* VLAN priority must be either exactly zero or fully wildcarded and
0562      * VLAN id must be exact match.
0563      */
0564     if (is_vid_exactmatch(vlan_tci_mask) &&
0565         ((is_vlan_pcp_exactmatch(vlan_tci_mask) &&
0566           is_vlan_pcp_zero(vlan_tci)) ||
0567          is_vlan_pcp_wildcarded(vlan_tci_mask)))
0568         return true;
0569 
0570     return false;
0571 }
0572 
0573 static bool bits_set(void *key, int len)
0574 {
0575     const u8 *p = key;
0576     int i;
0577 
0578     for (i = 0; i < len; i++)
0579         if (p[i] != 0)
0580             return true;
0581 
0582     return false;
0583 }
0584 
0585 static int bnxt_hwrm_cfa_flow_alloc(struct bnxt *bp, struct bnxt_tc_flow *flow,
0586                     __le16 ref_flow_handle,
0587                     __le32 tunnel_handle,
0588                     struct bnxt_tc_flow_node *flow_node)
0589 {
0590     struct bnxt_tc_actions *actions = &flow->actions;
0591     struct bnxt_tc_l3_key *l3_mask = &flow->l3_mask;
0592     struct bnxt_tc_l3_key *l3_key = &flow->l3_key;
0593     struct hwrm_cfa_flow_alloc_output *resp;
0594     struct hwrm_cfa_flow_alloc_input *req;
0595     u16 flow_flags = 0, action_flags = 0;
0596     int rc;
0597 
0598     rc = hwrm_req_init(bp, req, HWRM_CFA_FLOW_ALLOC);
0599     if (rc)
0600         return rc;
0601 
0602     req->src_fid = cpu_to_le16(flow->src_fid);
0603     req->ref_flow_handle = ref_flow_handle;
0604 
0605     if (actions->flags & BNXT_TC_ACTION_FLAG_L2_REWRITE) {
0606         memcpy(req->l2_rewrite_dmac, actions->l2_rewrite_dmac,
0607                ETH_ALEN);
0608         memcpy(req->l2_rewrite_smac, actions->l2_rewrite_smac,
0609                ETH_ALEN);
0610         action_flags |=
0611             CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
0612     }
0613 
0614     if (actions->flags & BNXT_TC_ACTION_FLAG_NAT_XLATE) {
0615         if (actions->nat.l3_is_ipv4) {
0616             action_flags |=
0617                 CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_NAT_IPV4_ADDRESS;
0618 
0619             if (actions->nat.src_xlate) {
0620                 action_flags |=
0621                     CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_NAT_SRC;
0622                 /* L3 source rewrite */
0623                 req->nat_ip_address[0] =
0624                     actions->nat.l3.ipv4.saddr.s_addr;
0625                 /* L4 source port */
0626                 if (actions->nat.l4.ports.sport)
0627                     req->nat_port =
0628                         actions->nat.l4.ports.sport;
0629             } else {
0630                 action_flags |=
0631                     CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_NAT_DEST;
0632                 /* L3 destination rewrite */
0633                 req->nat_ip_address[0] =
0634                     actions->nat.l3.ipv4.daddr.s_addr;
0635                 /* L4 destination port */
0636                 if (actions->nat.l4.ports.dport)
0637                     req->nat_port =
0638                         actions->nat.l4.ports.dport;
0639             }
0640             netdev_dbg(bp->dev,
0641                    "req->nat_ip_address: %pI4 src_xlate: %d req->nat_port: %x\n",
0642                    req->nat_ip_address, actions->nat.src_xlate,
0643                    req->nat_port);
0644         } else {
0645             if (actions->nat.src_xlate) {
0646                 action_flags |=
0647                     CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_NAT_SRC;
0648                 /* L3 source rewrite */
0649                 memcpy(req->nat_ip_address,
0650                        actions->nat.l3.ipv6.saddr.s6_addr32,
0651                        sizeof(req->nat_ip_address));
0652                 /* L4 source port */
0653                 if (actions->nat.l4.ports.sport)
0654                     req->nat_port =
0655                         actions->nat.l4.ports.sport;
0656             } else {
0657                 action_flags |=
0658                     CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_NAT_DEST;
0659                 /* L3 destination rewrite */
0660                 memcpy(req->nat_ip_address,
0661                        actions->nat.l3.ipv6.daddr.s6_addr32,
0662                        sizeof(req->nat_ip_address));
0663                 /* L4 destination port */
0664                 if (actions->nat.l4.ports.dport)
0665                     req->nat_port =
0666                         actions->nat.l4.ports.dport;
0667             }
0668             netdev_dbg(bp->dev,
0669                    "req->nat_ip_address: %pI6 src_xlate: %d req->nat_port: %x\n",
0670                    req->nat_ip_address, actions->nat.src_xlate,
0671                    req->nat_port);
0672         }
0673     }
0674 
0675     if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP ||
0676         actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
0677         req->tunnel_handle = tunnel_handle;
0678         flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_TUNNEL;
0679         action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_TUNNEL;
0680     }
0681 
0682     req->ethertype = flow->l2_key.ether_type;
0683     req->ip_proto = flow->l4_key.ip_proto;
0684 
0685     if (flow->flags & BNXT_TC_FLOW_FLAGS_ETH_ADDRS) {
0686         memcpy(req->dmac, flow->l2_key.dmac, ETH_ALEN);
0687         memcpy(req->smac, flow->l2_key.smac, ETH_ALEN);
0688     }
0689 
0690     if (flow->l2_key.num_vlans > 0) {
0691         flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_NUM_VLAN_ONE;
0692         /* FW expects the inner_vlan_tci value to be set
0693          * in outer_vlan_tci when num_vlans is 1 (which is
0694          * always the case in TC.)
0695          */
0696         req->outer_vlan_tci = flow->l2_key.inner_vlan_tci;
0697     }
0698 
0699     /* If all IP and L4 fields are wildcarded then this is an L2 flow */
0700     if (is_wildcard(l3_mask, sizeof(*l3_mask)) &&
0701         is_wildcard(&flow->l4_mask, sizeof(flow->l4_mask))) {
0702         flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_L2;
0703     } else {
0704         flow_flags |= flow->l2_key.ether_type == htons(ETH_P_IP) ?
0705                 CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV4 :
0706                 CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV6;
0707 
0708         if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV4_ADDRS) {
0709             req->ip_dst[0] = l3_key->ipv4.daddr.s_addr;
0710             req->ip_dst_mask_len =
0711                 inet_mask_len(l3_mask->ipv4.daddr.s_addr);
0712             req->ip_src[0] = l3_key->ipv4.saddr.s_addr;
0713             req->ip_src_mask_len =
0714                 inet_mask_len(l3_mask->ipv4.saddr.s_addr);
0715         } else if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV6_ADDRS) {
0716             memcpy(req->ip_dst, l3_key->ipv6.daddr.s6_addr32,
0717                    sizeof(req->ip_dst));
0718             req->ip_dst_mask_len =
0719                     ipv6_mask_len(&l3_mask->ipv6.daddr);
0720             memcpy(req->ip_src, l3_key->ipv6.saddr.s6_addr32,
0721                    sizeof(req->ip_src));
0722             req->ip_src_mask_len =
0723                     ipv6_mask_len(&l3_mask->ipv6.saddr);
0724         }
0725     }
0726 
0727     if (flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) {
0728         req->l4_src_port = flow->l4_key.ports.sport;
0729         req->l4_src_port_mask = flow->l4_mask.ports.sport;
0730         req->l4_dst_port = flow->l4_key.ports.dport;
0731         req->l4_dst_port_mask = flow->l4_mask.ports.dport;
0732     } else if (flow->flags & BNXT_TC_FLOW_FLAGS_ICMP) {
0733         /* l4 ports serve as type/code when ip_proto is ICMP */
0734         req->l4_src_port = htons(flow->l4_key.icmp.type);
0735         req->l4_src_port_mask = htons(flow->l4_mask.icmp.type);
0736         req->l4_dst_port = htons(flow->l4_key.icmp.code);
0737         req->l4_dst_port_mask = htons(flow->l4_mask.icmp.code);
0738     }
0739     req->flags = cpu_to_le16(flow_flags);
0740 
0741     if (actions->flags & BNXT_TC_ACTION_FLAG_DROP) {
0742         action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_DROP;
0743     } else {
0744         if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
0745             action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_FWD;
0746             req->dst_fid = cpu_to_le16(actions->dst_fid);
0747         }
0748         if (actions->flags & BNXT_TC_ACTION_FLAG_PUSH_VLAN) {
0749             action_flags |=
0750                 CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
0751             req->l2_rewrite_vlan_tpid = actions->push_vlan_tpid;
0752             req->l2_rewrite_vlan_tci = actions->push_vlan_tci;
0753             memcpy(&req->l2_rewrite_dmac, &req->dmac, ETH_ALEN);
0754             memcpy(&req->l2_rewrite_smac, &req->smac, ETH_ALEN);
0755         }
0756         if (actions->flags & BNXT_TC_ACTION_FLAG_POP_VLAN) {
0757             action_flags |=
0758                 CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
0759             /* Rewrite config with tpid = 0 implies vlan pop */
0760             req->l2_rewrite_vlan_tpid = 0;
0761             memcpy(&req->l2_rewrite_dmac, &req->dmac, ETH_ALEN);
0762             memcpy(&req->l2_rewrite_smac, &req->smac, ETH_ALEN);
0763         }
0764     }
0765     req->action_flags = cpu_to_le16(action_flags);
0766 
0767     resp = hwrm_req_hold(bp, req);
0768     rc = hwrm_req_send_silent(bp, req);
0769     if (!rc) {
0770         /* CFA_FLOW_ALLOC response interpretation:
0771          *          fw with      fw with
0772          *          16-bit       64-bit
0773          *          flow handle      flow handle
0774          *          ===========      ===========
0775          * flow_handle      flow handle      flow context id
0776          * ext_flow_handle  INVALID      flow handle
0777          * flow_id      INVALID      flow counter id
0778          */
0779         flow_node->flow_handle = resp->flow_handle;
0780         if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) {
0781             flow_node->ext_flow_handle = resp->ext_flow_handle;
0782             flow_node->flow_id = resp->flow_id;
0783         }
0784     }
0785     hwrm_req_drop(bp, req);
0786     return rc;
0787 }
0788 
0789 static int hwrm_cfa_decap_filter_alloc(struct bnxt *bp,
0790                        struct bnxt_tc_flow *flow,
0791                        struct bnxt_tc_l2_key *l2_info,
0792                        __le32 ref_decap_handle,
0793                        __le32 *decap_filter_handle)
0794 {
0795     struct hwrm_cfa_decap_filter_alloc_output *resp;
0796     struct ip_tunnel_key *tun_key = &flow->tun_key;
0797     struct hwrm_cfa_decap_filter_alloc_input *req;
0798     u32 enables = 0;
0799     int rc;
0800 
0801     rc = hwrm_req_init(bp, req, HWRM_CFA_DECAP_FILTER_ALLOC);
0802     if (rc)
0803         goto exit;
0804 
0805     req->flags = cpu_to_le32(CFA_DECAP_FILTER_ALLOC_REQ_FLAGS_OVS_TUNNEL);
0806     enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE |
0807            CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL;
0808     req->tunnel_type = CFA_DECAP_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
0809     req->ip_protocol = CFA_DECAP_FILTER_ALLOC_REQ_IP_PROTOCOL_UDP;
0810 
0811     if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ID) {
0812         enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_ID;
0813         /* tunnel_id is wrongly defined in hsi defn. as __le32 */
0814         req->tunnel_id = tunnel_id_to_key32(tun_key->tun_id);
0815     }
0816 
0817     if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS) {
0818         enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_MACADDR;
0819         ether_addr_copy(req->dst_macaddr, l2_info->dmac);
0820     }
0821     if (l2_info->num_vlans) {
0822         enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_T_IVLAN_VID;
0823         req->t_ivlan_vid = l2_info->inner_vlan_tci;
0824     }
0825 
0826     enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE;
0827     req->ethertype = htons(ETH_P_IP);
0828 
0829     if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS) {
0830         enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |
0831                CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |
0832                CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE;
0833         req->ip_addr_type =
0834             CFA_DECAP_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
0835         req->dst_ipaddr[0] = tun_key->u.ipv4.dst;
0836         req->src_ipaddr[0] = tun_key->u.ipv4.src;
0837     }
0838 
0839     if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_PORTS) {
0840         enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_PORT;
0841         req->dst_port = tun_key->tp_dst;
0842     }
0843 
0844     /* Eventhough the decap_handle returned by hwrm_cfa_decap_filter_alloc
0845      * is defined as __le32, l2_ctxt_ref_id is defined in HSI as __le16.
0846      */
0847     req->l2_ctxt_ref_id = (__force __le16)ref_decap_handle;
0848     req->enables = cpu_to_le32(enables);
0849 
0850     resp = hwrm_req_hold(bp, req);
0851     rc = hwrm_req_send_silent(bp, req);
0852     if (!rc)
0853         *decap_filter_handle = resp->decap_filter_id;
0854     hwrm_req_drop(bp, req);
0855 exit:
0856     if (rc)
0857         netdev_info(bp->dev, "%s: Error rc=%d\n", __func__, rc);
0858 
0859     return rc;
0860 }
0861 
0862 static int hwrm_cfa_decap_filter_free(struct bnxt *bp,
0863                       __le32 decap_filter_handle)
0864 {
0865     struct hwrm_cfa_decap_filter_free_input *req;
0866     int rc;
0867 
0868     rc = hwrm_req_init(bp, req, HWRM_CFA_DECAP_FILTER_FREE);
0869     if (!rc) {
0870         req->decap_filter_id = decap_filter_handle;
0871         rc = hwrm_req_send(bp, req);
0872     }
0873     if (rc)
0874         netdev_info(bp->dev, "%s: Error rc=%d\n", __func__, rc);
0875 
0876     return rc;
0877 }
0878 
0879 static int hwrm_cfa_encap_record_alloc(struct bnxt *bp,
0880                        struct ip_tunnel_key *encap_key,
0881                        struct bnxt_tc_l2_key *l2_info,
0882                        __le32 *encap_record_handle)
0883 {
0884     struct hwrm_cfa_encap_record_alloc_output *resp;
0885     struct hwrm_cfa_encap_record_alloc_input *req;
0886     struct hwrm_cfa_encap_data_vxlan *encap;
0887     struct hwrm_vxlan_ipv4_hdr *encap_ipv4;
0888     int rc;
0889 
0890     rc = hwrm_req_init(bp, req, HWRM_CFA_ENCAP_RECORD_ALLOC);
0891     if (rc)
0892         goto exit;
0893 
0894     encap = (struct hwrm_cfa_encap_data_vxlan *)&req->encap_data;
0895     req->encap_type = CFA_ENCAP_RECORD_ALLOC_REQ_ENCAP_TYPE_VXLAN;
0896     ether_addr_copy(encap->dst_mac_addr, l2_info->dmac);
0897     ether_addr_copy(encap->src_mac_addr, l2_info->smac);
0898     if (l2_info->num_vlans) {
0899         encap->num_vlan_tags = l2_info->num_vlans;
0900         encap->ovlan_tci = l2_info->inner_vlan_tci;
0901         encap->ovlan_tpid = l2_info->inner_vlan_tpid;
0902     }
0903 
0904     encap_ipv4 = (struct hwrm_vxlan_ipv4_hdr *)encap->l3;
0905     encap_ipv4->ver_hlen = 4 << VXLAN_IPV4_HDR_VER_HLEN_VERSION_SFT;
0906     encap_ipv4->ver_hlen |= 5 << VXLAN_IPV4_HDR_VER_HLEN_HEADER_LENGTH_SFT;
0907     encap_ipv4->ttl = encap_key->ttl;
0908 
0909     encap_ipv4->dest_ip_addr = encap_key->u.ipv4.dst;
0910     encap_ipv4->src_ip_addr = encap_key->u.ipv4.src;
0911     encap_ipv4->protocol = IPPROTO_UDP;
0912 
0913     encap->dst_port = encap_key->tp_dst;
0914     encap->vni = tunnel_id_to_key32(encap_key->tun_id);
0915 
0916     resp = hwrm_req_hold(bp, req);
0917     rc = hwrm_req_send_silent(bp, req);
0918     if (!rc)
0919         *encap_record_handle = resp->encap_record_id;
0920     hwrm_req_drop(bp, req);
0921 exit:
0922     if (rc)
0923         netdev_info(bp->dev, "%s: Error rc=%d\n", __func__, rc);
0924 
0925     return rc;
0926 }
0927 
0928 static int hwrm_cfa_encap_record_free(struct bnxt *bp,
0929                       __le32 encap_record_handle)
0930 {
0931     struct hwrm_cfa_encap_record_free_input *req;
0932     int rc;
0933 
0934     rc = hwrm_req_init(bp, req, HWRM_CFA_ENCAP_RECORD_FREE);
0935     if (!rc) {
0936         req->encap_record_id = encap_record_handle;
0937         rc = hwrm_req_send(bp, req);
0938     }
0939     if (rc)
0940         netdev_info(bp->dev, "%s: Error rc=%d\n", __func__, rc);
0941 
0942     return rc;
0943 }
0944 
0945 static int bnxt_tc_put_l2_node(struct bnxt *bp,
0946                    struct bnxt_tc_flow_node *flow_node)
0947 {
0948     struct bnxt_tc_l2_node *l2_node = flow_node->l2_node;
0949     struct bnxt_tc_info *tc_info = bp->tc_info;
0950     int rc;
0951 
0952     /* remove flow_node from the L2 shared flow list */
0953     list_del(&flow_node->l2_list_node);
0954     if (--l2_node->refcount == 0) {
0955         rc =  rhashtable_remove_fast(&tc_info->l2_table, &l2_node->node,
0956                          tc_info->l2_ht_params);
0957         if (rc)
0958             netdev_err(bp->dev,
0959                    "Error: %s: rhashtable_remove_fast: %d\n",
0960                    __func__, rc);
0961         kfree_rcu(l2_node, rcu);
0962     }
0963     return 0;
0964 }
0965 
0966 static struct bnxt_tc_l2_node *
0967 bnxt_tc_get_l2_node(struct bnxt *bp, struct rhashtable *l2_table,
0968             struct rhashtable_params ht_params,
0969             struct bnxt_tc_l2_key *l2_key)
0970 {
0971     struct bnxt_tc_l2_node *l2_node;
0972     int rc;
0973 
0974     l2_node = rhashtable_lookup_fast(l2_table, l2_key, ht_params);
0975     if (!l2_node) {
0976         l2_node = kzalloc(sizeof(*l2_node), GFP_KERNEL);
0977         if (!l2_node) {
0978             rc = -ENOMEM;
0979             return NULL;
0980         }
0981 
0982         l2_node->key = *l2_key;
0983         rc = rhashtable_insert_fast(l2_table, &l2_node->node,
0984                         ht_params);
0985         if (rc) {
0986             kfree_rcu(l2_node, rcu);
0987             netdev_err(bp->dev,
0988                    "Error: %s: rhashtable_insert_fast: %d\n",
0989                    __func__, rc);
0990             return NULL;
0991         }
0992         INIT_LIST_HEAD(&l2_node->common_l2_flows);
0993     }
0994     return l2_node;
0995 }
0996 
0997 /* Get the ref_flow_handle for a flow by checking if there are any other
0998  * flows that share the same L2 key as this flow.
0999  */
1000 static int
1001 bnxt_tc_get_ref_flow_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1002                 struct bnxt_tc_flow_node *flow_node,
1003                 __le16 *ref_flow_handle)
1004 {
1005     struct bnxt_tc_info *tc_info = bp->tc_info;
1006     struct bnxt_tc_flow_node *ref_flow_node;
1007     struct bnxt_tc_l2_node *l2_node;
1008 
1009     l2_node = bnxt_tc_get_l2_node(bp, &tc_info->l2_table,
1010                       tc_info->l2_ht_params,
1011                       &flow->l2_key);
1012     if (!l2_node)
1013         return -1;
1014 
1015     /* If any other flow is using this l2_node, use it's flow_handle
1016      * as the ref_flow_handle
1017      */
1018     if (l2_node->refcount > 0) {
1019         ref_flow_node = list_first_entry(&l2_node->common_l2_flows,
1020                          struct bnxt_tc_flow_node,
1021                          l2_list_node);
1022         *ref_flow_handle = ref_flow_node->flow_handle;
1023     } else {
1024         *ref_flow_handle = cpu_to_le16(0xffff);
1025     }
1026 
1027     /* Insert the l2_node into the flow_node so that subsequent flows
1028      * with a matching l2 key can use the flow_handle of this flow
1029      * as their ref_flow_handle
1030      */
1031     flow_node->l2_node = l2_node;
1032     list_add(&flow_node->l2_list_node, &l2_node->common_l2_flows);
1033     l2_node->refcount++;
1034     return 0;
1035 }
1036 
1037 /* After the flow parsing is done, this routine is used for checking
1038  * if there are any aspects of the flow that prevent it from being
1039  * offloaded.
1040  */
1041 static bool bnxt_tc_can_offload(struct bnxt *bp, struct bnxt_tc_flow *flow)
1042 {
1043     /* If L4 ports are specified then ip_proto must be TCP or UDP */
1044     if ((flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) &&
1045         (flow->l4_key.ip_proto != IPPROTO_TCP &&
1046          flow->l4_key.ip_proto != IPPROTO_UDP)) {
1047         netdev_info(bp->dev, "Cannot offload non-TCP/UDP (%d) ports\n",
1048                 flow->l4_key.ip_proto);
1049         return false;
1050     }
1051 
1052     /* Currently source/dest MAC cannot be partial wildcard  */
1053     if (bits_set(&flow->l2_key.smac, sizeof(flow->l2_key.smac)) &&
1054         !is_exactmatch(flow->l2_mask.smac, sizeof(flow->l2_mask.smac))) {
1055         netdev_info(bp->dev, "Wildcard match unsupported for Source MAC\n");
1056         return false;
1057     }
1058     if (bits_set(&flow->l2_key.dmac, sizeof(flow->l2_key.dmac)) &&
1059         !is_exactmatch(&flow->l2_mask.dmac, sizeof(flow->l2_mask.dmac))) {
1060         netdev_info(bp->dev, "Wildcard match unsupported for Dest MAC\n");
1061         return false;
1062     }
1063 
1064     /* Currently VLAN fields cannot be partial wildcard */
1065     if (bits_set(&flow->l2_key.inner_vlan_tci,
1066              sizeof(flow->l2_key.inner_vlan_tci)) &&
1067         !is_vlan_tci_allowed(flow->l2_mask.inner_vlan_tci,
1068                  flow->l2_key.inner_vlan_tci)) {
1069         netdev_info(bp->dev, "Unsupported VLAN TCI\n");
1070         return false;
1071     }
1072     if (bits_set(&flow->l2_key.inner_vlan_tpid,
1073              sizeof(flow->l2_key.inner_vlan_tpid)) &&
1074         !is_exactmatch(&flow->l2_mask.inner_vlan_tpid,
1075                sizeof(flow->l2_mask.inner_vlan_tpid))) {
1076         netdev_info(bp->dev, "Wildcard match unsupported for VLAN TPID\n");
1077         return false;
1078     }
1079 
1080     /* Currently Ethertype must be set */
1081     if (!is_exactmatch(&flow->l2_mask.ether_type,
1082                sizeof(flow->l2_mask.ether_type))) {
1083         netdev_info(bp->dev, "Wildcard match unsupported for Ethertype\n");
1084         return false;
1085     }
1086 
1087     return true;
1088 }
1089 
1090 /* Returns the final refcount of the node on success
1091  * or a -ve error code on failure
1092  */
1093 static int bnxt_tc_put_tunnel_node(struct bnxt *bp,
1094                    struct rhashtable *tunnel_table,
1095                    struct rhashtable_params *ht_params,
1096                    struct bnxt_tc_tunnel_node *tunnel_node)
1097 {
1098     int rc;
1099 
1100     if (--tunnel_node->refcount == 0) {
1101         rc =  rhashtable_remove_fast(tunnel_table, &tunnel_node->node,
1102                          *ht_params);
1103         if (rc) {
1104             netdev_err(bp->dev, "rhashtable_remove_fast rc=%d\n", rc);
1105             rc = -1;
1106         }
1107         kfree_rcu(tunnel_node, rcu);
1108         return rc;
1109     } else {
1110         return tunnel_node->refcount;
1111     }
1112 }
1113 
1114 /* Get (or add) either encap or decap tunnel node from/to the supplied
1115  * hash table.
1116  */
1117 static struct bnxt_tc_tunnel_node *
1118 bnxt_tc_get_tunnel_node(struct bnxt *bp, struct rhashtable *tunnel_table,
1119             struct rhashtable_params *ht_params,
1120             struct ip_tunnel_key *tun_key)
1121 {
1122     struct bnxt_tc_tunnel_node *tunnel_node;
1123     int rc;
1124 
1125     tunnel_node = rhashtable_lookup_fast(tunnel_table, tun_key, *ht_params);
1126     if (!tunnel_node) {
1127         tunnel_node = kzalloc(sizeof(*tunnel_node), GFP_KERNEL);
1128         if (!tunnel_node) {
1129             rc = -ENOMEM;
1130             goto err;
1131         }
1132 
1133         tunnel_node->key = *tun_key;
1134         tunnel_node->tunnel_handle = INVALID_TUNNEL_HANDLE;
1135         rc = rhashtable_insert_fast(tunnel_table, &tunnel_node->node,
1136                         *ht_params);
1137         if (rc) {
1138             kfree_rcu(tunnel_node, rcu);
1139             goto err;
1140         }
1141     }
1142     tunnel_node->refcount++;
1143     return tunnel_node;
1144 err:
1145     netdev_info(bp->dev, "error rc=%d\n", rc);
1146     return NULL;
1147 }
1148 
1149 static int bnxt_tc_get_ref_decap_handle(struct bnxt *bp,
1150                     struct bnxt_tc_flow *flow,
1151                     struct bnxt_tc_l2_key *l2_key,
1152                     struct bnxt_tc_flow_node *flow_node,
1153                     __le32 *ref_decap_handle)
1154 {
1155     struct bnxt_tc_info *tc_info = bp->tc_info;
1156     struct bnxt_tc_flow_node *ref_flow_node;
1157     struct bnxt_tc_l2_node *decap_l2_node;
1158 
1159     decap_l2_node = bnxt_tc_get_l2_node(bp, &tc_info->decap_l2_table,
1160                         tc_info->decap_l2_ht_params,
1161                         l2_key);
1162     if (!decap_l2_node)
1163         return -1;
1164 
1165     /* If any other flow is using this decap_l2_node, use it's decap_handle
1166      * as the ref_decap_handle
1167      */
1168     if (decap_l2_node->refcount > 0) {
1169         ref_flow_node =
1170             list_first_entry(&decap_l2_node->common_l2_flows,
1171                      struct bnxt_tc_flow_node,
1172                      decap_l2_list_node);
1173         *ref_decap_handle = ref_flow_node->decap_node->tunnel_handle;
1174     } else {
1175         *ref_decap_handle = INVALID_TUNNEL_HANDLE;
1176     }
1177 
1178     /* Insert the l2_node into the flow_node so that subsequent flows
1179      * with a matching decap l2 key can use the decap_filter_handle of
1180      * this flow as their ref_decap_handle
1181      */
1182     flow_node->decap_l2_node = decap_l2_node;
1183     list_add(&flow_node->decap_l2_list_node,
1184          &decap_l2_node->common_l2_flows);
1185     decap_l2_node->refcount++;
1186     return 0;
1187 }
1188 
1189 static void bnxt_tc_put_decap_l2_node(struct bnxt *bp,
1190                       struct bnxt_tc_flow_node *flow_node)
1191 {
1192     struct bnxt_tc_l2_node *decap_l2_node = flow_node->decap_l2_node;
1193     struct bnxt_tc_info *tc_info = bp->tc_info;
1194     int rc;
1195 
1196     /* remove flow_node from the decap L2 sharing flow list */
1197     list_del(&flow_node->decap_l2_list_node);
1198     if (--decap_l2_node->refcount == 0) {
1199         rc =  rhashtable_remove_fast(&tc_info->decap_l2_table,
1200                          &decap_l2_node->node,
1201                          tc_info->decap_l2_ht_params);
1202         if (rc)
1203             netdev_err(bp->dev, "rhashtable_remove_fast rc=%d\n", rc);
1204         kfree_rcu(decap_l2_node, rcu);
1205     }
1206 }
1207 
1208 static void bnxt_tc_put_decap_handle(struct bnxt *bp,
1209                      struct bnxt_tc_flow_node *flow_node)
1210 {
1211     __le32 decap_handle = flow_node->decap_node->tunnel_handle;
1212     struct bnxt_tc_info *tc_info = bp->tc_info;
1213     int rc;
1214 
1215     if (flow_node->decap_l2_node)
1216         bnxt_tc_put_decap_l2_node(bp, flow_node);
1217 
1218     rc = bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
1219                      &tc_info->decap_ht_params,
1220                      flow_node->decap_node);
1221     if (!rc && decap_handle != INVALID_TUNNEL_HANDLE)
1222         hwrm_cfa_decap_filter_free(bp, decap_handle);
1223 }
1224 
1225 static int bnxt_tc_resolve_tunnel_hdrs(struct bnxt *bp,
1226                        struct ip_tunnel_key *tun_key,
1227                        struct bnxt_tc_l2_key *l2_info)
1228 {
1229 #ifdef CONFIG_INET
1230     struct net_device *real_dst_dev = bp->dev;
1231     struct flowi4 flow = { {0} };
1232     struct net_device *dst_dev;
1233     struct neighbour *nbr;
1234     struct rtable *rt;
1235     int rc;
1236 
1237     flow.flowi4_proto = IPPROTO_UDP;
1238     flow.fl4_dport = tun_key->tp_dst;
1239     flow.daddr = tun_key->u.ipv4.dst;
1240 
1241     rt = ip_route_output_key(dev_net(real_dst_dev), &flow);
1242     if (IS_ERR(rt)) {
1243         netdev_info(bp->dev, "no route to %pI4b\n", &flow.daddr);
1244         return -EOPNOTSUPP;
1245     }
1246 
1247     /* The route must either point to the real_dst_dev or a dst_dev that
1248      * uses the real_dst_dev.
1249      */
1250     dst_dev = rt->dst.dev;
1251     if (is_vlan_dev(dst_dev)) {
1252 #if IS_ENABLED(CONFIG_VLAN_8021Q)
1253         struct vlan_dev_priv *vlan = vlan_dev_priv(dst_dev);
1254 
1255         if (vlan->real_dev != real_dst_dev) {
1256             netdev_info(bp->dev,
1257                     "dst_dev(%s) doesn't use PF-if(%s)\n",
1258                     netdev_name(dst_dev),
1259                     netdev_name(real_dst_dev));
1260             rc = -EOPNOTSUPP;
1261             goto put_rt;
1262         }
1263         l2_info->inner_vlan_tci = htons(vlan->vlan_id);
1264         l2_info->inner_vlan_tpid = vlan->vlan_proto;
1265         l2_info->num_vlans = 1;
1266 #endif
1267     } else if (dst_dev != real_dst_dev) {
1268         netdev_info(bp->dev,
1269                 "dst_dev(%s) for %pI4b is not PF-if(%s)\n",
1270                 netdev_name(dst_dev), &flow.daddr,
1271                 netdev_name(real_dst_dev));
1272         rc = -EOPNOTSUPP;
1273         goto put_rt;
1274     }
1275 
1276     nbr = dst_neigh_lookup(&rt->dst, &flow.daddr);
1277     if (!nbr) {
1278         netdev_info(bp->dev, "can't lookup neighbor for %pI4b\n",
1279                 &flow.daddr);
1280         rc = -EOPNOTSUPP;
1281         goto put_rt;
1282     }
1283 
1284     tun_key->u.ipv4.src = flow.saddr;
1285     tun_key->ttl = ip4_dst_hoplimit(&rt->dst);
1286     neigh_ha_snapshot(l2_info->dmac, nbr, dst_dev);
1287     ether_addr_copy(l2_info->smac, dst_dev->dev_addr);
1288     neigh_release(nbr);
1289     ip_rt_put(rt);
1290 
1291     return 0;
1292 put_rt:
1293     ip_rt_put(rt);
1294     return rc;
1295 #else
1296     return -EOPNOTSUPP;
1297 #endif
1298 }
1299 
1300 static int bnxt_tc_get_decap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1301                     struct bnxt_tc_flow_node *flow_node,
1302                     __le32 *decap_filter_handle)
1303 {
1304     struct ip_tunnel_key *decap_key = &flow->tun_key;
1305     struct bnxt_tc_info *tc_info = bp->tc_info;
1306     struct bnxt_tc_l2_key l2_info = { {0} };
1307     struct bnxt_tc_tunnel_node *decap_node;
1308     struct ip_tunnel_key tun_key = { 0 };
1309     struct bnxt_tc_l2_key *decap_l2_info;
1310     __le32 ref_decap_handle;
1311     int rc;
1312 
1313     /* Check if there's another flow using the same tunnel decap.
1314      * If not, add this tunnel to the table and resolve the other
1315      * tunnel header fileds. Ignore src_port in the tunnel_key,
1316      * since it is not required for decap filters.
1317      */
1318     decap_key->tp_src = 0;
1319     decap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->decap_table,
1320                          &tc_info->decap_ht_params,
1321                          decap_key);
1322     if (!decap_node)
1323         return -ENOMEM;
1324 
1325     flow_node->decap_node = decap_node;
1326 
1327     if (decap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1328         goto done;
1329 
1330     /* Resolve the L2 fields for tunnel decap
1331      * Resolve the route for remote vtep (saddr) of the decap key
1332      * Find it's next-hop mac addrs
1333      */
1334     tun_key.u.ipv4.dst = flow->tun_key.u.ipv4.src;
1335     tun_key.tp_dst = flow->tun_key.tp_dst;
1336     rc = bnxt_tc_resolve_tunnel_hdrs(bp, &tun_key, &l2_info);
1337     if (rc)
1338         goto put_decap;
1339 
1340     decap_l2_info = &decap_node->l2_info;
1341     /* decap smac is wildcarded */
1342     ether_addr_copy(decap_l2_info->dmac, l2_info.smac);
1343     if (l2_info.num_vlans) {
1344         decap_l2_info->num_vlans = l2_info.num_vlans;
1345         decap_l2_info->inner_vlan_tpid = l2_info.inner_vlan_tpid;
1346         decap_l2_info->inner_vlan_tci = l2_info.inner_vlan_tci;
1347     }
1348     flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS;
1349 
1350     /* For getting a decap_filter_handle we first need to check if
1351      * there are any other decap flows that share the same tunnel L2
1352      * key and if so, pass that flow's decap_filter_handle as the
1353      * ref_decap_handle for this flow.
1354      */
1355     rc = bnxt_tc_get_ref_decap_handle(bp, flow, decap_l2_info, flow_node,
1356                       &ref_decap_handle);
1357     if (rc)
1358         goto put_decap;
1359 
1360     /* Issue the hwrm cmd to allocate a decap filter handle */
1361     rc = hwrm_cfa_decap_filter_alloc(bp, flow, decap_l2_info,
1362                      ref_decap_handle,
1363                      &decap_node->tunnel_handle);
1364     if (rc)
1365         goto put_decap_l2;
1366 
1367 done:
1368     *decap_filter_handle = decap_node->tunnel_handle;
1369     return 0;
1370 
1371 put_decap_l2:
1372     bnxt_tc_put_decap_l2_node(bp, flow_node);
1373 put_decap:
1374     bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
1375                 &tc_info->decap_ht_params,
1376                 flow_node->decap_node);
1377     return rc;
1378 }
1379 
1380 static void bnxt_tc_put_encap_handle(struct bnxt *bp,
1381                      struct bnxt_tc_tunnel_node *encap_node)
1382 {
1383     __le32 encap_handle = encap_node->tunnel_handle;
1384     struct bnxt_tc_info *tc_info = bp->tc_info;
1385     int rc;
1386 
1387     rc = bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1388                      &tc_info->encap_ht_params, encap_node);
1389     if (!rc && encap_handle != INVALID_TUNNEL_HANDLE)
1390         hwrm_cfa_encap_record_free(bp, encap_handle);
1391 }
1392 
1393 /* Lookup the tunnel encap table and check if there's an encap_handle
1394  * alloc'd already.
1395  * If not, query L2 info via a route lookup and issue an encap_record_alloc
1396  * cmd to FW.
1397  */
1398 static int bnxt_tc_get_encap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1399                     struct bnxt_tc_flow_node *flow_node,
1400                     __le32 *encap_handle)
1401 {
1402     struct ip_tunnel_key *encap_key = &flow->actions.tun_encap_key;
1403     struct bnxt_tc_info *tc_info = bp->tc_info;
1404     struct bnxt_tc_tunnel_node *encap_node;
1405     int rc;
1406 
1407     /* Check if there's another flow using the same tunnel encap.
1408      * If not, add this tunnel to the table and resolve the other
1409      * tunnel header fileds
1410      */
1411     encap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->encap_table,
1412                          &tc_info->encap_ht_params,
1413                          encap_key);
1414     if (!encap_node)
1415         return -ENOMEM;
1416 
1417     flow_node->encap_node = encap_node;
1418 
1419     if (encap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1420         goto done;
1421 
1422     rc = bnxt_tc_resolve_tunnel_hdrs(bp, encap_key, &encap_node->l2_info);
1423     if (rc)
1424         goto put_encap;
1425 
1426     /* Allocate a new tunnel encap record */
1427     rc = hwrm_cfa_encap_record_alloc(bp, encap_key, &encap_node->l2_info,
1428                      &encap_node->tunnel_handle);
1429     if (rc)
1430         goto put_encap;
1431 
1432 done:
1433     *encap_handle = encap_node->tunnel_handle;
1434     return 0;
1435 
1436 put_encap:
1437     bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1438                 &tc_info->encap_ht_params, encap_node);
1439     return rc;
1440 }
1441 
1442 static void bnxt_tc_put_tunnel_handle(struct bnxt *bp,
1443                       struct bnxt_tc_flow *flow,
1444                       struct bnxt_tc_flow_node *flow_node)
1445 {
1446     if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1447         bnxt_tc_put_decap_handle(bp, flow_node);
1448     else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1449         bnxt_tc_put_encap_handle(bp, flow_node->encap_node);
1450 }
1451 
1452 static int bnxt_tc_get_tunnel_handle(struct bnxt *bp,
1453                      struct bnxt_tc_flow *flow,
1454                      struct bnxt_tc_flow_node *flow_node,
1455                      __le32 *tunnel_handle)
1456 {
1457     if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1458         return bnxt_tc_get_decap_handle(bp, flow, flow_node,
1459                         tunnel_handle);
1460     else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1461         return bnxt_tc_get_encap_handle(bp, flow, flow_node,
1462                         tunnel_handle);
1463     else
1464         return 0;
1465 }
1466 static int __bnxt_tc_del_flow(struct bnxt *bp,
1467                   struct bnxt_tc_flow_node *flow_node)
1468 {
1469     struct bnxt_tc_info *tc_info = bp->tc_info;
1470     int rc;
1471 
1472     /* send HWRM cmd to free the flow-id */
1473     bnxt_hwrm_cfa_flow_free(bp, flow_node);
1474 
1475     mutex_lock(&tc_info->lock);
1476 
1477     /* release references to any tunnel encap/decap nodes */
1478     bnxt_tc_put_tunnel_handle(bp, &flow_node->flow, flow_node);
1479 
1480     /* release reference to l2 node */
1481     bnxt_tc_put_l2_node(bp, flow_node);
1482 
1483     mutex_unlock(&tc_info->lock);
1484 
1485     rc = rhashtable_remove_fast(&tc_info->flow_table, &flow_node->node,
1486                     tc_info->flow_ht_params);
1487     if (rc)
1488         netdev_err(bp->dev, "Error: %s: rhashtable_remove_fast rc=%d\n",
1489                __func__, rc);
1490 
1491     kfree_rcu(flow_node, rcu);
1492     return 0;
1493 }
1494 
1495 static void bnxt_tc_set_flow_dir(struct bnxt *bp, struct bnxt_tc_flow *flow,
1496                  u16 src_fid)
1497 {
1498     flow->l2_key.dir = (bp->pf.fw_fid == src_fid) ? BNXT_DIR_RX : BNXT_DIR_TX;
1499 }
1500 
1501 static void bnxt_tc_set_src_fid(struct bnxt *bp, struct bnxt_tc_flow *flow,
1502                 u16 src_fid)
1503 {
1504     if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1505         flow->src_fid = bp->pf.fw_fid;
1506     else
1507         flow->src_fid = src_fid;
1508 }
1509 
1510 /* Add a new flow or replace an existing flow.
1511  * Notes on locking:
1512  * There are essentially two critical sections here.
1513  * 1. while adding a new flow
1514  *    a) lookup l2-key
1515  *    b) issue HWRM cmd and get flow_handle
1516  *    c) link l2-key with flow
1517  * 2. while deleting a flow
1518  *    a) unlinking l2-key from flow
1519  * A lock is needed to protect these two critical sections.
1520  *
1521  * The hash-tables are already protected by the rhashtable API.
1522  */
1523 static int bnxt_tc_add_flow(struct bnxt *bp, u16 src_fid,
1524                 struct flow_cls_offload *tc_flow_cmd)
1525 {
1526     struct bnxt_tc_flow_node *new_node, *old_node;
1527     struct bnxt_tc_info *tc_info = bp->tc_info;
1528     struct bnxt_tc_flow *flow;
1529     __le32 tunnel_handle = 0;
1530     __le16 ref_flow_handle;
1531     int rc;
1532 
1533     /* allocate memory for the new flow and it's node */
1534     new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
1535     if (!new_node) {
1536         rc = -ENOMEM;
1537         goto done;
1538     }
1539     new_node->cookie = tc_flow_cmd->cookie;
1540     flow = &new_node->flow;
1541 
1542     rc = bnxt_tc_parse_flow(bp, tc_flow_cmd, flow);
1543     if (rc)
1544         goto free_node;
1545 
1546     bnxt_tc_set_src_fid(bp, flow, src_fid);
1547     bnxt_tc_set_flow_dir(bp, flow, flow->src_fid);
1548 
1549     if (!bnxt_tc_can_offload(bp, flow)) {
1550         rc = -EOPNOTSUPP;
1551         kfree_rcu(new_node, rcu);
1552         return rc;
1553     }
1554 
1555     /* If a flow exists with the same cookie, delete it */
1556     old_node = rhashtable_lookup_fast(&tc_info->flow_table,
1557                       &tc_flow_cmd->cookie,
1558                       tc_info->flow_ht_params);
1559     if (old_node)
1560         __bnxt_tc_del_flow(bp, old_node);
1561 
1562     /* Check if the L2 part of the flow has been offloaded already.
1563      * If so, bump up it's refcnt and get it's reference handle.
1564      */
1565     mutex_lock(&tc_info->lock);
1566     rc = bnxt_tc_get_ref_flow_handle(bp, flow, new_node, &ref_flow_handle);
1567     if (rc)
1568         goto unlock;
1569 
1570     /* If the flow involves tunnel encap/decap, get tunnel_handle */
1571     rc = bnxt_tc_get_tunnel_handle(bp, flow, new_node, &tunnel_handle);
1572     if (rc)
1573         goto put_l2;
1574 
1575     /* send HWRM cmd to alloc the flow */
1576     rc = bnxt_hwrm_cfa_flow_alloc(bp, flow, ref_flow_handle,
1577                       tunnel_handle, new_node);
1578     if (rc)
1579         goto put_tunnel;
1580 
1581     flow->lastused = jiffies;
1582     spin_lock_init(&flow->stats_lock);
1583     /* add new flow to flow-table */
1584     rc = rhashtable_insert_fast(&tc_info->flow_table, &new_node->node,
1585                     tc_info->flow_ht_params);
1586     if (rc)
1587         goto hwrm_flow_free;
1588 
1589     mutex_unlock(&tc_info->lock);
1590     return 0;
1591 
1592 hwrm_flow_free:
1593     bnxt_hwrm_cfa_flow_free(bp, new_node);
1594 put_tunnel:
1595     bnxt_tc_put_tunnel_handle(bp, flow, new_node);
1596 put_l2:
1597     bnxt_tc_put_l2_node(bp, new_node);
1598 unlock:
1599     mutex_unlock(&tc_info->lock);
1600 free_node:
1601     kfree_rcu(new_node, rcu);
1602 done:
1603     netdev_err(bp->dev, "Error: %s: cookie=0x%lx error=%d\n",
1604            __func__, tc_flow_cmd->cookie, rc);
1605     return rc;
1606 }
1607 
1608 static int bnxt_tc_del_flow(struct bnxt *bp,
1609                 struct flow_cls_offload *tc_flow_cmd)
1610 {
1611     struct bnxt_tc_info *tc_info = bp->tc_info;
1612     struct bnxt_tc_flow_node *flow_node;
1613 
1614     flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1615                        &tc_flow_cmd->cookie,
1616                        tc_info->flow_ht_params);
1617     if (!flow_node)
1618         return -EINVAL;
1619 
1620     return __bnxt_tc_del_flow(bp, flow_node);
1621 }
1622 
1623 static int bnxt_tc_get_flow_stats(struct bnxt *bp,
1624                   struct flow_cls_offload *tc_flow_cmd)
1625 {
1626     struct bnxt_tc_flow_stats stats, *curr_stats, *prev_stats;
1627     struct bnxt_tc_info *tc_info = bp->tc_info;
1628     struct bnxt_tc_flow_node *flow_node;
1629     struct bnxt_tc_flow *flow;
1630     unsigned long lastused;
1631 
1632     flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1633                        &tc_flow_cmd->cookie,
1634                        tc_info->flow_ht_params);
1635     if (!flow_node)
1636         return -1;
1637 
1638     flow = &flow_node->flow;
1639     curr_stats = &flow->stats;
1640     prev_stats = &flow->prev_stats;
1641 
1642     spin_lock(&flow->stats_lock);
1643     stats.packets = curr_stats->packets - prev_stats->packets;
1644     stats.bytes = curr_stats->bytes - prev_stats->bytes;
1645     *prev_stats = *curr_stats;
1646     lastused = flow->lastused;
1647     spin_unlock(&flow->stats_lock);
1648 
1649     flow_stats_update(&tc_flow_cmd->stats, stats.bytes, stats.packets, 0,
1650               lastused, FLOW_ACTION_HW_STATS_DELAYED);
1651     return 0;
1652 }
1653 
1654 static void bnxt_fill_cfa_stats_req(struct bnxt *bp,
1655                     struct bnxt_tc_flow_node *flow_node,
1656                     __le16 *flow_handle, __le32 *flow_id)
1657 {
1658     u16 handle;
1659 
1660     if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) {
1661         *flow_id = flow_node->flow_id;
1662 
1663         /* If flow_id is used to fetch flow stats then:
1664          * 1. lower 12 bits of flow_handle must be set to all 1s.
1665          * 2. 15th bit of flow_handle must specify the flow
1666          *    direction (TX/RX).
1667          */
1668         if (flow_node->flow.l2_key.dir == BNXT_DIR_RX)
1669             handle = CFA_FLOW_INFO_REQ_FLOW_HANDLE_DIR_RX |
1670                  CFA_FLOW_INFO_REQ_FLOW_HANDLE_MAX_MASK;
1671         else
1672             handle = CFA_FLOW_INFO_REQ_FLOW_HANDLE_MAX_MASK;
1673 
1674         *flow_handle = cpu_to_le16(handle);
1675     } else {
1676         *flow_handle = flow_node->flow_handle;
1677     }
1678 }
1679 
1680 static int
1681 bnxt_hwrm_cfa_flow_stats_get(struct bnxt *bp, int num_flows,
1682                  struct bnxt_tc_stats_batch stats_batch[])
1683 {
1684     struct hwrm_cfa_flow_stats_output *resp;
1685     struct hwrm_cfa_flow_stats_input *req;
1686     __le16 *req_flow_handles;
1687     __le32 *req_flow_ids;
1688     int rc, i;
1689 
1690     rc = hwrm_req_init(bp, req, HWRM_CFA_FLOW_STATS);
1691     if (rc)
1692         goto exit;
1693 
1694     req_flow_handles = &req->flow_handle_0;
1695     req_flow_ids = &req->flow_id_0;
1696 
1697     req->num_flows = cpu_to_le16(num_flows);
1698     for (i = 0; i < num_flows; i++) {
1699         struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1700 
1701         bnxt_fill_cfa_stats_req(bp, flow_node,
1702                     &req_flow_handles[i], &req_flow_ids[i]);
1703     }
1704 
1705     resp = hwrm_req_hold(bp, req);
1706     rc = hwrm_req_send(bp, req);
1707     if (!rc) {
1708         __le64 *resp_packets;
1709         __le64 *resp_bytes;
1710 
1711         resp_packets = &resp->packet_0;
1712         resp_bytes = &resp->byte_0;
1713 
1714         for (i = 0; i < num_flows; i++) {
1715             stats_batch[i].hw_stats.packets =
1716                         le64_to_cpu(resp_packets[i]);
1717             stats_batch[i].hw_stats.bytes =
1718                         le64_to_cpu(resp_bytes[i]);
1719         }
1720     }
1721     hwrm_req_drop(bp, req);
1722 exit:
1723     if (rc)
1724         netdev_info(bp->dev, "error rc=%d\n", rc);
1725 
1726     return rc;
1727 }
1728 
1729 /* Add val to accum while handling a possible wraparound
1730  * of val. Eventhough val is of type u64, its actual width
1731  * is denoted by mask and will wrap-around beyond that width.
1732  */
1733 static void accumulate_val(u64 *accum, u64 val, u64 mask)
1734 {
1735 #define low_bits(x, mask)       ((x) & (mask))
1736 #define high_bits(x, mask)      ((x) & ~(mask))
1737     bool wrapped = val < low_bits(*accum, mask);
1738 
1739     *accum = high_bits(*accum, mask) + val;
1740     if (wrapped)
1741         *accum += (mask + 1);
1742 }
1743 
1744 /* The HW counters' width is much less than 64bits.
1745  * Handle possible wrap-around while updating the stat counters
1746  */
1747 static void bnxt_flow_stats_accum(struct bnxt_tc_info *tc_info,
1748                   struct bnxt_tc_flow_stats *acc_stats,
1749                   struct bnxt_tc_flow_stats *hw_stats)
1750 {
1751     accumulate_val(&acc_stats->bytes, hw_stats->bytes, tc_info->bytes_mask);
1752     accumulate_val(&acc_stats->packets, hw_stats->packets,
1753                tc_info->packets_mask);
1754 }
1755 
1756 static int
1757 bnxt_tc_flow_stats_batch_update(struct bnxt *bp, int num_flows,
1758                 struct bnxt_tc_stats_batch stats_batch[])
1759 {
1760     struct bnxt_tc_info *tc_info = bp->tc_info;
1761     int rc, i;
1762 
1763     rc = bnxt_hwrm_cfa_flow_stats_get(bp, num_flows, stats_batch);
1764     if (rc)
1765         return rc;
1766 
1767     for (i = 0; i < num_flows; i++) {
1768         struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1769         struct bnxt_tc_flow *flow = &flow_node->flow;
1770 
1771         spin_lock(&flow->stats_lock);
1772         bnxt_flow_stats_accum(tc_info, &flow->stats,
1773                       &stats_batch[i].hw_stats);
1774         if (flow->stats.packets != flow->prev_stats.packets)
1775             flow->lastused = jiffies;
1776         spin_unlock(&flow->stats_lock);
1777     }
1778 
1779     return 0;
1780 }
1781 
1782 static int
1783 bnxt_tc_flow_stats_batch_prep(struct bnxt *bp,
1784                   struct bnxt_tc_stats_batch stats_batch[],
1785                   int *num_flows)
1786 {
1787     struct bnxt_tc_info *tc_info = bp->tc_info;
1788     struct rhashtable_iter *iter = &tc_info->iter;
1789     void *flow_node;
1790     int rc, i;
1791 
1792     rhashtable_walk_start(iter);
1793 
1794     rc = 0;
1795     for (i = 0; i < BNXT_FLOW_STATS_BATCH_MAX; i++) {
1796         flow_node = rhashtable_walk_next(iter);
1797         if (IS_ERR(flow_node)) {
1798             i = 0;
1799             if (PTR_ERR(flow_node) == -EAGAIN) {
1800                 continue;
1801             } else {
1802                 rc = PTR_ERR(flow_node);
1803                 goto done;
1804             }
1805         }
1806 
1807         /* No more flows */
1808         if (!flow_node)
1809             goto done;
1810 
1811         stats_batch[i].flow_node = flow_node;
1812     }
1813 done:
1814     rhashtable_walk_stop(iter);
1815     *num_flows = i;
1816     return rc;
1817 }
1818 
1819 void bnxt_tc_flow_stats_work(struct bnxt *bp)
1820 {
1821     struct bnxt_tc_info *tc_info = bp->tc_info;
1822     int num_flows, rc;
1823 
1824     num_flows = atomic_read(&tc_info->flow_table.nelems);
1825     if (!num_flows)
1826         return;
1827 
1828     rhashtable_walk_enter(&tc_info->flow_table, &tc_info->iter);
1829 
1830     for (;;) {
1831         rc = bnxt_tc_flow_stats_batch_prep(bp, tc_info->stats_batch,
1832                            &num_flows);
1833         if (rc) {
1834             if (rc == -EAGAIN)
1835                 continue;
1836             break;
1837         }
1838 
1839         if (!num_flows)
1840             break;
1841 
1842         bnxt_tc_flow_stats_batch_update(bp, num_flows,
1843                         tc_info->stats_batch);
1844     }
1845 
1846     rhashtable_walk_exit(&tc_info->iter);
1847 }
1848 
1849 int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
1850              struct flow_cls_offload *cls_flower)
1851 {
1852     switch (cls_flower->command) {
1853     case FLOW_CLS_REPLACE:
1854         return bnxt_tc_add_flow(bp, src_fid, cls_flower);
1855     case FLOW_CLS_DESTROY:
1856         return bnxt_tc_del_flow(bp, cls_flower);
1857     case FLOW_CLS_STATS:
1858         return bnxt_tc_get_flow_stats(bp, cls_flower);
1859     default:
1860         return -EOPNOTSUPP;
1861     }
1862 }
1863 
1864 static int bnxt_tc_setup_indr_block_cb(enum tc_setup_type type,
1865                        void *type_data, void *cb_priv)
1866 {
1867     struct bnxt_flower_indr_block_cb_priv *priv = cb_priv;
1868     struct flow_cls_offload *flower = type_data;
1869     struct bnxt *bp = priv->bp;
1870 
1871     if (!tc_cls_can_offload_and_chain0(bp->dev, type_data))
1872         return -EOPNOTSUPP;
1873 
1874     switch (type) {
1875     case TC_SETUP_CLSFLOWER:
1876         return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, flower);
1877     default:
1878         return -EOPNOTSUPP;
1879     }
1880 }
1881 
1882 static struct bnxt_flower_indr_block_cb_priv *
1883 bnxt_tc_indr_block_cb_lookup(struct bnxt *bp, struct net_device *netdev)
1884 {
1885     struct bnxt_flower_indr_block_cb_priv *cb_priv;
1886 
1887     list_for_each_entry(cb_priv, &bp->tc_indr_block_list, list)
1888         if (cb_priv->tunnel_netdev == netdev)
1889             return cb_priv;
1890 
1891     return NULL;
1892 }
1893 
1894 static void bnxt_tc_setup_indr_rel(void *cb_priv)
1895 {
1896     struct bnxt_flower_indr_block_cb_priv *priv = cb_priv;
1897 
1898     list_del(&priv->list);
1899     kfree(priv);
1900 }
1901 
1902 static int bnxt_tc_setup_indr_block(struct net_device *netdev, struct Qdisc *sch, struct bnxt *bp,
1903                     struct flow_block_offload *f, void *data,
1904                     void (*cleanup)(struct flow_block_cb *block_cb))
1905 {
1906     struct bnxt_flower_indr_block_cb_priv *cb_priv;
1907     struct flow_block_cb *block_cb;
1908 
1909     if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1910         return -EOPNOTSUPP;
1911 
1912     switch (f->command) {
1913     case FLOW_BLOCK_BIND:
1914         cb_priv = kmalloc(sizeof(*cb_priv), GFP_KERNEL);
1915         if (!cb_priv)
1916             return -ENOMEM;
1917 
1918         cb_priv->tunnel_netdev = netdev;
1919         cb_priv->bp = bp;
1920         list_add(&cb_priv->list, &bp->tc_indr_block_list);
1921 
1922         block_cb = flow_indr_block_cb_alloc(bnxt_tc_setup_indr_block_cb,
1923                             cb_priv, cb_priv,
1924                             bnxt_tc_setup_indr_rel, f,
1925                             netdev, sch, data, bp, cleanup);
1926         if (IS_ERR(block_cb)) {
1927             list_del(&cb_priv->list);
1928             kfree(cb_priv);
1929             return PTR_ERR(block_cb);
1930         }
1931 
1932         flow_block_cb_add(block_cb, f);
1933         list_add_tail(&block_cb->driver_list, &bnxt_block_cb_list);
1934         break;
1935     case FLOW_BLOCK_UNBIND:
1936         cb_priv = bnxt_tc_indr_block_cb_lookup(bp, netdev);
1937         if (!cb_priv)
1938             return -ENOENT;
1939 
1940         block_cb = flow_block_cb_lookup(f->block,
1941                         bnxt_tc_setup_indr_block_cb,
1942                         cb_priv);
1943         if (!block_cb)
1944             return -ENOENT;
1945 
1946         flow_indr_block_cb_remove(block_cb, f);
1947         list_del(&block_cb->driver_list);
1948         break;
1949     default:
1950         return -EOPNOTSUPP;
1951     }
1952     return 0;
1953 }
1954 
1955 static bool bnxt_is_netdev_indr_offload(struct net_device *netdev)
1956 {
1957     return netif_is_vxlan(netdev);
1958 }
1959 
1960 static int bnxt_tc_setup_indr_cb(struct net_device *netdev, struct Qdisc *sch, void *cb_priv,
1961                  enum tc_setup_type type, void *type_data,
1962                  void *data,
1963                  void (*cleanup)(struct flow_block_cb *block_cb))
1964 {
1965     if (!netdev || !bnxt_is_netdev_indr_offload(netdev))
1966         return -EOPNOTSUPP;
1967 
1968     switch (type) {
1969     case TC_SETUP_BLOCK:
1970         return bnxt_tc_setup_indr_block(netdev, sch, cb_priv, type_data, data, cleanup);
1971     default:
1972         break;
1973     }
1974 
1975     return -EOPNOTSUPP;
1976 }
1977 
1978 static const struct rhashtable_params bnxt_tc_flow_ht_params = {
1979     .head_offset = offsetof(struct bnxt_tc_flow_node, node),
1980     .key_offset = offsetof(struct bnxt_tc_flow_node, cookie),
1981     .key_len = sizeof(((struct bnxt_tc_flow_node *)0)->cookie),
1982     .automatic_shrinking = true
1983 };
1984 
1985 static const struct rhashtable_params bnxt_tc_l2_ht_params = {
1986     .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1987     .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1988     .key_len = BNXT_TC_L2_KEY_LEN,
1989     .automatic_shrinking = true
1990 };
1991 
1992 static const struct rhashtable_params bnxt_tc_decap_l2_ht_params = {
1993     .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1994     .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1995     .key_len = BNXT_TC_L2_KEY_LEN,
1996     .automatic_shrinking = true
1997 };
1998 
1999 static const struct rhashtable_params bnxt_tc_tunnel_ht_params = {
2000     .head_offset = offsetof(struct bnxt_tc_tunnel_node, node),
2001     .key_offset = offsetof(struct bnxt_tc_tunnel_node, key),
2002     .key_len = sizeof(struct ip_tunnel_key),
2003     .automatic_shrinking = true
2004 };
2005 
2006 /* convert counter width in bits to a mask */
2007 #define mask(width)     ((u64)~0 >> (64 - (width)))
2008 
2009 int bnxt_init_tc(struct bnxt *bp)
2010 {
2011     struct bnxt_tc_info *tc_info;
2012     int rc;
2013 
2014     if (bp->hwrm_spec_code < 0x10803)
2015         return 0;
2016 
2017     tc_info = kzalloc(sizeof(*tc_info), GFP_KERNEL);
2018     if (!tc_info)
2019         return -ENOMEM;
2020     mutex_init(&tc_info->lock);
2021 
2022     /* Counter widths are programmed by FW */
2023     tc_info->bytes_mask = mask(36);
2024     tc_info->packets_mask = mask(28);
2025 
2026     tc_info->flow_ht_params = bnxt_tc_flow_ht_params;
2027     rc = rhashtable_init(&tc_info->flow_table, &tc_info->flow_ht_params);
2028     if (rc)
2029         goto free_tc_info;
2030 
2031     tc_info->l2_ht_params = bnxt_tc_l2_ht_params;
2032     rc = rhashtable_init(&tc_info->l2_table, &tc_info->l2_ht_params);
2033     if (rc)
2034         goto destroy_flow_table;
2035 
2036     tc_info->decap_l2_ht_params = bnxt_tc_decap_l2_ht_params;
2037     rc = rhashtable_init(&tc_info->decap_l2_table,
2038                  &tc_info->decap_l2_ht_params);
2039     if (rc)
2040         goto destroy_l2_table;
2041 
2042     tc_info->decap_ht_params = bnxt_tc_tunnel_ht_params;
2043     rc = rhashtable_init(&tc_info->decap_table,
2044                  &tc_info->decap_ht_params);
2045     if (rc)
2046         goto destroy_decap_l2_table;
2047 
2048     tc_info->encap_ht_params = bnxt_tc_tunnel_ht_params;
2049     rc = rhashtable_init(&tc_info->encap_table,
2050                  &tc_info->encap_ht_params);
2051     if (rc)
2052         goto destroy_decap_table;
2053 
2054     tc_info->enabled = true;
2055     bp->dev->hw_features |= NETIF_F_HW_TC;
2056     bp->dev->features |= NETIF_F_HW_TC;
2057     bp->tc_info = tc_info;
2058 
2059     /* init indirect block notifications */
2060     INIT_LIST_HEAD(&bp->tc_indr_block_list);
2061 
2062     rc = flow_indr_dev_register(bnxt_tc_setup_indr_cb, bp);
2063     if (!rc)
2064         return 0;
2065 
2066     rhashtable_destroy(&tc_info->encap_table);
2067 
2068 destroy_decap_table:
2069     rhashtable_destroy(&tc_info->decap_table);
2070 destroy_decap_l2_table:
2071     rhashtable_destroy(&tc_info->decap_l2_table);
2072 destroy_l2_table:
2073     rhashtable_destroy(&tc_info->l2_table);
2074 destroy_flow_table:
2075     rhashtable_destroy(&tc_info->flow_table);
2076 free_tc_info:
2077     kfree(tc_info);
2078     return rc;
2079 }
2080 
2081 void bnxt_shutdown_tc(struct bnxt *bp)
2082 {
2083     struct bnxt_tc_info *tc_info = bp->tc_info;
2084 
2085     if (!bnxt_tc_flower_enabled(bp))
2086         return;
2087 
2088     flow_indr_dev_unregister(bnxt_tc_setup_indr_cb, bp,
2089                  bnxt_tc_setup_indr_rel);
2090     rhashtable_destroy(&tc_info->flow_table);
2091     rhashtable_destroy(&tc_info->l2_table);
2092     rhashtable_destroy(&tc_info->decap_l2_table);
2093     rhashtable_destroy(&tc_info->decap_table);
2094     rhashtable_destroy(&tc_info->encap_table);
2095     kfree(tc_info);
2096     bp->tc_info = NULL;
2097 }