Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Marvell RVU Ethernet driver
0003  *
0004  * Copyright (C) 2020 Marvell.
0005  *
0006  */
0007 
0008 #include <net/ipv6.h>
0009 #include <linux/sort.h>
0010 
0011 #include "otx2_common.h"
0012 
0013 #define OTX2_DEFAULT_ACTION 0x1
0014 
0015 static int otx2_mcam_entry_init(struct otx2_nic *pfvf);
0016 
0017 struct otx2_flow {
0018     struct ethtool_rx_flow_spec flow_spec;
0019     struct list_head list;
0020     u32 location;
0021     u32 entry;
0022     bool is_vf;
0023     u8 rss_ctx_id;
0024 #define DMAC_FILTER_RULE        BIT(0)
0025 #define PFC_FLOWCTRL_RULE       BIT(1)
0026     u16 rule_type;
0027     int vf;
0028 };
0029 
0030 enum dmac_req {
0031     DMAC_ADDR_UPDATE,
0032     DMAC_ADDR_DEL
0033 };
0034 
0035 static void otx2_clear_ntuple_flow_info(struct otx2_nic *pfvf, struct otx2_flow_config *flow_cfg)
0036 {
0037     devm_kfree(pfvf->dev, flow_cfg->flow_ent);
0038     flow_cfg->flow_ent = NULL;
0039     flow_cfg->max_flows = 0;
0040 }
0041 
0042 static int otx2_free_ntuple_mcam_entries(struct otx2_nic *pfvf)
0043 {
0044     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
0045     struct npc_mcam_free_entry_req *req;
0046     int ent, err;
0047 
0048     if (!flow_cfg->max_flows)
0049         return 0;
0050 
0051     mutex_lock(&pfvf->mbox.lock);
0052     for (ent = 0; ent < flow_cfg->max_flows; ent++) {
0053         req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
0054         if (!req)
0055             break;
0056 
0057         req->entry = flow_cfg->flow_ent[ent];
0058 
0059         /* Send message to AF to free MCAM entries */
0060         err = otx2_sync_mbox_msg(&pfvf->mbox);
0061         if (err)
0062             break;
0063     }
0064     mutex_unlock(&pfvf->mbox.lock);
0065     otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
0066     return 0;
0067 }
0068 
0069 static int mcam_entry_cmp(const void *a, const void *b)
0070 {
0071     return *(u16 *)a - *(u16 *)b;
0072 }
0073 
0074 int otx2_alloc_mcam_entries(struct otx2_nic *pfvf, u16 count)
0075 {
0076     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
0077     struct npc_mcam_alloc_entry_req *req;
0078     struct npc_mcam_alloc_entry_rsp *rsp;
0079     int ent, allocated = 0;
0080 
0081     /* Free current ones and allocate new ones with requested count */
0082     otx2_free_ntuple_mcam_entries(pfvf);
0083 
0084     if (!count)
0085         return 0;
0086 
0087     flow_cfg->flow_ent = devm_kmalloc_array(pfvf->dev, count,
0088                         sizeof(u16), GFP_KERNEL);
0089     if (!flow_cfg->flow_ent) {
0090         netdev_err(pfvf->netdev,
0091                "%s: Unable to allocate memory for flow entries\n",
0092                 __func__);
0093         return -ENOMEM;
0094     }
0095 
0096     mutex_lock(&pfvf->mbox.lock);
0097 
0098     /* In a single request a max of NPC_MAX_NONCONTIG_ENTRIES MCAM entries
0099      * can only be allocated.
0100      */
0101     while (allocated < count) {
0102         req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
0103         if (!req)
0104             goto exit;
0105 
0106         req->contig = false;
0107         req->count = (count - allocated) > NPC_MAX_NONCONTIG_ENTRIES ?
0108                 NPC_MAX_NONCONTIG_ENTRIES : count - allocated;
0109 
0110         /* Allocate higher priority entries for PFs, so that VF's entries
0111          * will be on top of PF.
0112          */
0113         if (!is_otx2_vf(pfvf->pcifunc)) {
0114             req->priority = NPC_MCAM_HIGHER_PRIO;
0115             req->ref_entry = flow_cfg->def_ent[0];
0116         }
0117 
0118         /* Send message to AF */
0119         if (otx2_sync_mbox_msg(&pfvf->mbox))
0120             goto exit;
0121 
0122         rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
0123             (&pfvf->mbox.mbox, 0, &req->hdr);
0124 
0125         for (ent = 0; ent < rsp->count; ent++)
0126             flow_cfg->flow_ent[ent + allocated] = rsp->entry_list[ent];
0127 
0128         allocated += rsp->count;
0129 
0130         /* If this request is not fulfilled, no need to send
0131          * further requests.
0132          */
0133         if (rsp->count != req->count)
0134             break;
0135     }
0136 
0137     /* Multiple MCAM entry alloc requests could result in non-sequential
0138      * MCAM entries in the flow_ent[] array. Sort them in an ascending order,
0139      * otherwise user installed ntuple filter index and MCAM entry index will
0140      * not be in sync.
0141      */
0142     if (allocated)
0143         sort(&flow_cfg->flow_ent[0], allocated,
0144              sizeof(flow_cfg->flow_ent[0]), mcam_entry_cmp, NULL);
0145 
0146 exit:
0147     mutex_unlock(&pfvf->mbox.lock);
0148 
0149     flow_cfg->max_flows = allocated;
0150 
0151     if (allocated) {
0152         pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
0153         pfvf->flags |= OTX2_FLAG_NTUPLE_SUPPORT;
0154     }
0155 
0156     if (allocated != count)
0157         netdev_info(pfvf->netdev,
0158                 "Unable to allocate %d MCAM entries, got only %d\n",
0159                 count, allocated);
0160     return allocated;
0161 }
0162 EXPORT_SYMBOL(otx2_alloc_mcam_entries);
0163 
0164 static int otx2_mcam_entry_init(struct otx2_nic *pfvf)
0165 {
0166     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
0167     struct npc_mcam_alloc_entry_req *req;
0168     struct npc_mcam_alloc_entry_rsp *rsp;
0169     int vf_vlan_max_flows;
0170     int ent, count;
0171 
0172     vf_vlan_max_flows = pfvf->total_vfs * OTX2_PER_VF_VLAN_FLOWS;
0173     count = OTX2_MAX_UNICAST_FLOWS +
0174             OTX2_MAX_VLAN_FLOWS + vf_vlan_max_flows;
0175 
0176     flow_cfg->def_ent = devm_kmalloc_array(pfvf->dev, count,
0177                            sizeof(u16), GFP_KERNEL);
0178     if (!flow_cfg->def_ent)
0179         return -ENOMEM;
0180 
0181     mutex_lock(&pfvf->mbox.lock);
0182 
0183     req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
0184     if (!req) {
0185         mutex_unlock(&pfvf->mbox.lock);
0186         return -ENOMEM;
0187     }
0188 
0189     req->contig = false;
0190     req->count = count;
0191 
0192     /* Send message to AF */
0193     if (otx2_sync_mbox_msg(&pfvf->mbox)) {
0194         mutex_unlock(&pfvf->mbox.lock);
0195         return -EINVAL;
0196     }
0197 
0198     rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
0199            (&pfvf->mbox.mbox, 0, &req->hdr);
0200 
0201     if (rsp->count != req->count) {
0202         netdev_info(pfvf->netdev,
0203                 "Unable to allocate MCAM entries for ucast, vlan and vf_vlan\n");
0204         mutex_unlock(&pfvf->mbox.lock);
0205         devm_kfree(pfvf->dev, flow_cfg->def_ent);
0206         return 0;
0207     }
0208 
0209     for (ent = 0; ent < rsp->count; ent++)
0210         flow_cfg->def_ent[ent] = rsp->entry_list[ent];
0211 
0212     flow_cfg->vf_vlan_offset = 0;
0213     flow_cfg->unicast_offset = vf_vlan_max_flows;
0214     flow_cfg->rx_vlan_offset = flow_cfg->unicast_offset +
0215                     OTX2_MAX_UNICAST_FLOWS;
0216     pfvf->flags |= OTX2_FLAG_UCAST_FLTR_SUPPORT;
0217     pfvf->flags |= OTX2_FLAG_RX_VLAN_SUPPORT;
0218     pfvf->flags |= OTX2_FLAG_VF_VLAN_SUPPORT;
0219 
0220     pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
0221     mutex_unlock(&pfvf->mbox.lock);
0222 
0223     /* Allocate entries for Ntuple filters */
0224     count = otx2_alloc_mcam_entries(pfvf, OTX2_DEFAULT_FLOWCOUNT);
0225     if (count <= 0) {
0226         otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
0227         return 0;
0228     }
0229 
0230     pfvf->flags |= OTX2_FLAG_TC_FLOWER_SUPPORT;
0231 
0232     return 0;
0233 }
0234 
0235 /* TODO : revisit on size */
0236 #define OTX2_DMAC_FLTR_BITMAP_SZ (4 * 2048 + 32)
0237 
0238 int otx2vf_mcam_flow_init(struct otx2_nic *pfvf)
0239 {
0240     struct otx2_flow_config *flow_cfg;
0241 
0242     pfvf->flow_cfg = devm_kzalloc(pfvf->dev,
0243                       sizeof(struct otx2_flow_config),
0244                       GFP_KERNEL);
0245     if (!pfvf->flow_cfg)
0246         return -ENOMEM;
0247 
0248     pfvf->flow_cfg->dmacflt_bmap = devm_kcalloc(pfvf->dev,
0249                             BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
0250                             sizeof(long), GFP_KERNEL);
0251     if (!pfvf->flow_cfg->dmacflt_bmap)
0252         return -ENOMEM;
0253 
0254     flow_cfg = pfvf->flow_cfg;
0255     INIT_LIST_HEAD(&flow_cfg->flow_list);
0256     flow_cfg->max_flows = 0;
0257 
0258     return 0;
0259 }
0260 EXPORT_SYMBOL(otx2vf_mcam_flow_init);
0261 
0262 int otx2_mcam_flow_init(struct otx2_nic *pf)
0263 {
0264     int err;
0265 
0266     pf->flow_cfg = devm_kzalloc(pf->dev, sizeof(struct otx2_flow_config),
0267                     GFP_KERNEL);
0268     if (!pf->flow_cfg)
0269         return -ENOMEM;
0270 
0271     pf->flow_cfg->dmacflt_bmap = devm_kcalloc(pf->dev,
0272                           BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
0273                           sizeof(long), GFP_KERNEL);
0274     if (!pf->flow_cfg->dmacflt_bmap)
0275         return -ENOMEM;
0276 
0277     INIT_LIST_HEAD(&pf->flow_cfg->flow_list);
0278 
0279     /* Allocate bare minimum number of MCAM entries needed for
0280      * unicast and ntuple filters.
0281      */
0282     err = otx2_mcam_entry_init(pf);
0283     if (err)
0284         return err;
0285 
0286     /* Check if MCAM entries are allocate or not */
0287     if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
0288         return 0;
0289 
0290     pf->mac_table = devm_kzalloc(pf->dev, sizeof(struct otx2_mac_table)
0291                     * OTX2_MAX_UNICAST_FLOWS, GFP_KERNEL);
0292     if (!pf->mac_table)
0293         return -ENOMEM;
0294 
0295     otx2_dmacflt_get_max_cnt(pf);
0296 
0297     /* DMAC filters are not allocated */
0298     if (!pf->flow_cfg->dmacflt_max_flows)
0299         return 0;
0300 
0301     pf->flow_cfg->bmap_to_dmacindex =
0302             devm_kzalloc(pf->dev, sizeof(u32) *
0303                      pf->flow_cfg->dmacflt_max_flows,
0304                      GFP_KERNEL);
0305 
0306     if (!pf->flow_cfg->bmap_to_dmacindex)
0307         return -ENOMEM;
0308 
0309     pf->flags |= OTX2_FLAG_DMACFLTR_SUPPORT;
0310 
0311     return 0;
0312 }
0313 
0314 void otx2_mcam_flow_del(struct otx2_nic *pf)
0315 {
0316     otx2_destroy_mcam_flows(pf);
0317 }
0318 EXPORT_SYMBOL(otx2_mcam_flow_del);
0319 
0320 /*  On success adds mcam entry
0321  *  On failure enable promisous mode
0322  */
0323 static int otx2_do_add_macfilter(struct otx2_nic *pf, const u8 *mac)
0324 {
0325     struct otx2_flow_config *flow_cfg = pf->flow_cfg;
0326     struct npc_install_flow_req *req;
0327     int err, i;
0328 
0329     if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
0330         return -ENOMEM;
0331 
0332     /* dont have free mcam entries or uc list is greater than alloted */
0333     if (netdev_uc_count(pf->netdev) > OTX2_MAX_UNICAST_FLOWS)
0334         return -ENOMEM;
0335 
0336     mutex_lock(&pf->mbox.lock);
0337     req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
0338     if (!req) {
0339         mutex_unlock(&pf->mbox.lock);
0340         return -ENOMEM;
0341     }
0342 
0343     /* unicast offset starts with 32 0..31 for ntuple */
0344     for (i = 0; i <  OTX2_MAX_UNICAST_FLOWS; i++) {
0345         if (pf->mac_table[i].inuse)
0346             continue;
0347         ether_addr_copy(pf->mac_table[i].addr, mac);
0348         pf->mac_table[i].inuse = true;
0349         pf->mac_table[i].mcam_entry =
0350             flow_cfg->def_ent[i + flow_cfg->unicast_offset];
0351         req->entry =  pf->mac_table[i].mcam_entry;
0352         break;
0353     }
0354 
0355     ether_addr_copy(req->packet.dmac, mac);
0356     eth_broadcast_addr((u8 *)&req->mask.dmac);
0357     req->features = BIT_ULL(NPC_DMAC);
0358     req->channel = pf->hw.rx_chan_base;
0359     req->intf = NIX_INTF_RX;
0360     req->op = NIX_RX_ACTION_DEFAULT;
0361     req->set_cntr = 1;
0362 
0363     err = otx2_sync_mbox_msg(&pf->mbox);
0364     mutex_unlock(&pf->mbox.lock);
0365 
0366     return err;
0367 }
0368 
0369 int otx2_add_macfilter(struct net_device *netdev, const u8 *mac)
0370 {
0371     struct otx2_nic *pf = netdev_priv(netdev);
0372 
0373     if (!bitmap_empty(pf->flow_cfg->dmacflt_bmap,
0374               pf->flow_cfg->dmacflt_max_flows))
0375         netdev_warn(netdev,
0376                 "Add %pM to CGX/RPM DMAC filters list as well\n",
0377                 mac);
0378 
0379     return otx2_do_add_macfilter(pf, mac);
0380 }
0381 
0382 static bool otx2_get_mcamentry_for_mac(struct otx2_nic *pf, const u8 *mac,
0383                        int *mcam_entry)
0384 {
0385     int i;
0386 
0387     for (i = 0; i < OTX2_MAX_UNICAST_FLOWS; i++) {
0388         if (!pf->mac_table[i].inuse)
0389             continue;
0390 
0391         if (ether_addr_equal(pf->mac_table[i].addr, mac)) {
0392             *mcam_entry = pf->mac_table[i].mcam_entry;
0393             pf->mac_table[i].inuse = false;
0394             return true;
0395         }
0396     }
0397     return false;
0398 }
0399 
0400 int otx2_del_macfilter(struct net_device *netdev, const u8 *mac)
0401 {
0402     struct otx2_nic *pf = netdev_priv(netdev);
0403     struct npc_delete_flow_req *req;
0404     int err, mcam_entry;
0405 
0406     /* check does mcam entry exists for given mac */
0407     if (!otx2_get_mcamentry_for_mac(pf, mac, &mcam_entry))
0408         return 0;
0409 
0410     mutex_lock(&pf->mbox.lock);
0411     req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
0412     if (!req) {
0413         mutex_unlock(&pf->mbox.lock);
0414         return -ENOMEM;
0415     }
0416     req->entry = mcam_entry;
0417     /* Send message to AF */
0418     err = otx2_sync_mbox_msg(&pf->mbox);
0419     mutex_unlock(&pf->mbox.lock);
0420 
0421     return err;
0422 }
0423 
0424 static struct otx2_flow *otx2_find_flow(struct otx2_nic *pfvf, u32 location)
0425 {
0426     struct otx2_flow *iter;
0427 
0428     list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
0429         if (iter->location == location)
0430             return iter;
0431     }
0432 
0433     return NULL;
0434 }
0435 
0436 static void otx2_add_flow_to_list(struct otx2_nic *pfvf, struct otx2_flow *flow)
0437 {
0438     struct list_head *head = &pfvf->flow_cfg->flow_list;
0439     struct otx2_flow *iter;
0440 
0441     list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
0442         if (iter->location > flow->location)
0443             break;
0444         head = &iter->list;
0445     }
0446 
0447     list_add(&flow->list, head);
0448 }
0449 
0450 int otx2_get_maxflows(struct otx2_flow_config *flow_cfg)
0451 {
0452     if (!flow_cfg)
0453         return 0;
0454 
0455     if (flow_cfg->nr_flows == flow_cfg->max_flows ||
0456         !bitmap_empty(flow_cfg->dmacflt_bmap,
0457               flow_cfg->dmacflt_max_flows))
0458         return flow_cfg->max_flows + flow_cfg->dmacflt_max_flows;
0459     else
0460         return flow_cfg->max_flows;
0461 }
0462 EXPORT_SYMBOL(otx2_get_maxflows);
0463 
0464 int otx2_get_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
0465           u32 location)
0466 {
0467     struct otx2_flow *iter;
0468 
0469     if (location >= otx2_get_maxflows(pfvf->flow_cfg))
0470         return -EINVAL;
0471 
0472     list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
0473         if (iter->location == location) {
0474             nfc->fs = iter->flow_spec;
0475             nfc->rss_context = iter->rss_ctx_id;
0476             return 0;
0477         }
0478     }
0479 
0480     return -ENOENT;
0481 }
0482 
0483 int otx2_get_all_flows(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
0484                u32 *rule_locs)
0485 {
0486     u32 rule_cnt = nfc->rule_cnt;
0487     u32 location = 0;
0488     int idx = 0;
0489     int err = 0;
0490 
0491     nfc->data = otx2_get_maxflows(pfvf->flow_cfg);
0492     while ((!err || err == -ENOENT) && idx < rule_cnt) {
0493         err = otx2_get_flow(pfvf, nfc, location);
0494         if (!err)
0495             rule_locs[idx++] = location;
0496         location++;
0497     }
0498     nfc->rule_cnt = rule_cnt;
0499 
0500     return err;
0501 }
0502 
0503 static int otx2_prepare_ipv4_flow(struct ethtool_rx_flow_spec *fsp,
0504                   struct npc_install_flow_req *req,
0505                   u32 flow_type)
0506 {
0507     struct ethtool_usrip4_spec *ipv4_usr_mask = &fsp->m_u.usr_ip4_spec;
0508     struct ethtool_usrip4_spec *ipv4_usr_hdr = &fsp->h_u.usr_ip4_spec;
0509     struct ethtool_tcpip4_spec *ipv4_l4_mask = &fsp->m_u.tcp_ip4_spec;
0510     struct ethtool_tcpip4_spec *ipv4_l4_hdr = &fsp->h_u.tcp_ip4_spec;
0511     struct ethtool_ah_espip4_spec *ah_esp_hdr = &fsp->h_u.ah_ip4_spec;
0512     struct ethtool_ah_espip4_spec *ah_esp_mask = &fsp->m_u.ah_ip4_spec;
0513     struct flow_msg *pmask = &req->mask;
0514     struct flow_msg *pkt = &req->packet;
0515 
0516     switch (flow_type) {
0517     case IP_USER_FLOW:
0518         if (ipv4_usr_mask->ip4src) {
0519             memcpy(&pkt->ip4src, &ipv4_usr_hdr->ip4src,
0520                    sizeof(pkt->ip4src));
0521             memcpy(&pmask->ip4src, &ipv4_usr_mask->ip4src,
0522                    sizeof(pmask->ip4src));
0523             req->features |= BIT_ULL(NPC_SIP_IPV4);
0524         }
0525         if (ipv4_usr_mask->ip4dst) {
0526             memcpy(&pkt->ip4dst, &ipv4_usr_hdr->ip4dst,
0527                    sizeof(pkt->ip4dst));
0528             memcpy(&pmask->ip4dst, &ipv4_usr_mask->ip4dst,
0529                    sizeof(pmask->ip4dst));
0530             req->features |= BIT_ULL(NPC_DIP_IPV4);
0531         }
0532         if (ipv4_usr_mask->tos) {
0533             pkt->tos = ipv4_usr_hdr->tos;
0534             pmask->tos = ipv4_usr_mask->tos;
0535             req->features |= BIT_ULL(NPC_TOS);
0536         }
0537         if (ipv4_usr_mask->proto) {
0538             switch (ipv4_usr_hdr->proto) {
0539             case IPPROTO_ICMP:
0540                 req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
0541                 break;
0542             case IPPROTO_TCP:
0543                 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
0544                 break;
0545             case IPPROTO_UDP:
0546                 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
0547                 break;
0548             case IPPROTO_SCTP:
0549                 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
0550                 break;
0551             case IPPROTO_AH:
0552                 req->features |= BIT_ULL(NPC_IPPROTO_AH);
0553                 break;
0554             case IPPROTO_ESP:
0555                 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
0556                 break;
0557             default:
0558                 return -EOPNOTSUPP;
0559             }
0560         }
0561         pkt->etype = cpu_to_be16(ETH_P_IP);
0562         pmask->etype = cpu_to_be16(0xFFFF);
0563         req->features |= BIT_ULL(NPC_ETYPE);
0564         break;
0565     case TCP_V4_FLOW:
0566     case UDP_V4_FLOW:
0567     case SCTP_V4_FLOW:
0568         pkt->etype = cpu_to_be16(ETH_P_IP);
0569         pmask->etype = cpu_to_be16(0xFFFF);
0570         req->features |= BIT_ULL(NPC_ETYPE);
0571         if (ipv4_l4_mask->ip4src) {
0572             memcpy(&pkt->ip4src, &ipv4_l4_hdr->ip4src,
0573                    sizeof(pkt->ip4src));
0574             memcpy(&pmask->ip4src, &ipv4_l4_mask->ip4src,
0575                    sizeof(pmask->ip4src));
0576             req->features |= BIT_ULL(NPC_SIP_IPV4);
0577         }
0578         if (ipv4_l4_mask->ip4dst) {
0579             memcpy(&pkt->ip4dst, &ipv4_l4_hdr->ip4dst,
0580                    sizeof(pkt->ip4dst));
0581             memcpy(&pmask->ip4dst, &ipv4_l4_mask->ip4dst,
0582                    sizeof(pmask->ip4dst));
0583             req->features |= BIT_ULL(NPC_DIP_IPV4);
0584         }
0585         if (ipv4_l4_mask->tos) {
0586             pkt->tos = ipv4_l4_hdr->tos;
0587             pmask->tos = ipv4_l4_mask->tos;
0588             req->features |= BIT_ULL(NPC_TOS);
0589         }
0590         if (ipv4_l4_mask->psrc) {
0591             memcpy(&pkt->sport, &ipv4_l4_hdr->psrc,
0592                    sizeof(pkt->sport));
0593             memcpy(&pmask->sport, &ipv4_l4_mask->psrc,
0594                    sizeof(pmask->sport));
0595             if (flow_type == UDP_V4_FLOW)
0596                 req->features |= BIT_ULL(NPC_SPORT_UDP);
0597             else if (flow_type == TCP_V4_FLOW)
0598                 req->features |= BIT_ULL(NPC_SPORT_TCP);
0599             else
0600                 req->features |= BIT_ULL(NPC_SPORT_SCTP);
0601         }
0602         if (ipv4_l4_mask->pdst) {
0603             memcpy(&pkt->dport, &ipv4_l4_hdr->pdst,
0604                    sizeof(pkt->dport));
0605             memcpy(&pmask->dport, &ipv4_l4_mask->pdst,
0606                    sizeof(pmask->dport));
0607             if (flow_type == UDP_V4_FLOW)
0608                 req->features |= BIT_ULL(NPC_DPORT_UDP);
0609             else if (flow_type == TCP_V4_FLOW)
0610                 req->features |= BIT_ULL(NPC_DPORT_TCP);
0611             else
0612                 req->features |= BIT_ULL(NPC_DPORT_SCTP);
0613         }
0614         if (flow_type == UDP_V4_FLOW)
0615             req->features |= BIT_ULL(NPC_IPPROTO_UDP);
0616         else if (flow_type == TCP_V4_FLOW)
0617             req->features |= BIT_ULL(NPC_IPPROTO_TCP);
0618         else
0619             req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
0620         break;
0621     case AH_V4_FLOW:
0622     case ESP_V4_FLOW:
0623         pkt->etype = cpu_to_be16(ETH_P_IP);
0624         pmask->etype = cpu_to_be16(0xFFFF);
0625         req->features |= BIT_ULL(NPC_ETYPE);
0626         if (ah_esp_mask->ip4src) {
0627             memcpy(&pkt->ip4src, &ah_esp_hdr->ip4src,
0628                    sizeof(pkt->ip4src));
0629             memcpy(&pmask->ip4src, &ah_esp_mask->ip4src,
0630                    sizeof(pmask->ip4src));
0631             req->features |= BIT_ULL(NPC_SIP_IPV4);
0632         }
0633         if (ah_esp_mask->ip4dst) {
0634             memcpy(&pkt->ip4dst, &ah_esp_hdr->ip4dst,
0635                    sizeof(pkt->ip4dst));
0636             memcpy(&pmask->ip4dst, &ah_esp_mask->ip4dst,
0637                    sizeof(pmask->ip4dst));
0638             req->features |= BIT_ULL(NPC_DIP_IPV4);
0639         }
0640         if (ah_esp_mask->tos) {
0641             pkt->tos = ah_esp_hdr->tos;
0642             pmask->tos = ah_esp_mask->tos;
0643             req->features |= BIT_ULL(NPC_TOS);
0644         }
0645 
0646         /* NPC profile doesn't extract AH/ESP header fields */
0647         if (ah_esp_mask->spi & ah_esp_hdr->spi)
0648             return -EOPNOTSUPP;
0649 
0650         if (flow_type == AH_V4_FLOW)
0651             req->features |= BIT_ULL(NPC_IPPROTO_AH);
0652         else
0653             req->features |= BIT_ULL(NPC_IPPROTO_ESP);
0654         break;
0655     default:
0656         break;
0657     }
0658 
0659     return 0;
0660 }
0661 
0662 static int otx2_prepare_ipv6_flow(struct ethtool_rx_flow_spec *fsp,
0663                   struct npc_install_flow_req *req,
0664                   u32 flow_type)
0665 {
0666     struct ethtool_usrip6_spec *ipv6_usr_mask = &fsp->m_u.usr_ip6_spec;
0667     struct ethtool_usrip6_spec *ipv6_usr_hdr = &fsp->h_u.usr_ip6_spec;
0668     struct ethtool_tcpip6_spec *ipv6_l4_mask = &fsp->m_u.tcp_ip6_spec;
0669     struct ethtool_tcpip6_spec *ipv6_l4_hdr = &fsp->h_u.tcp_ip6_spec;
0670     struct ethtool_ah_espip6_spec *ah_esp_hdr = &fsp->h_u.ah_ip6_spec;
0671     struct ethtool_ah_espip6_spec *ah_esp_mask = &fsp->m_u.ah_ip6_spec;
0672     struct flow_msg *pmask = &req->mask;
0673     struct flow_msg *pkt = &req->packet;
0674 
0675     switch (flow_type) {
0676     case IPV6_USER_FLOW:
0677         if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6src)) {
0678             memcpy(&pkt->ip6src, &ipv6_usr_hdr->ip6src,
0679                    sizeof(pkt->ip6src));
0680             memcpy(&pmask->ip6src, &ipv6_usr_mask->ip6src,
0681                    sizeof(pmask->ip6src));
0682             req->features |= BIT_ULL(NPC_SIP_IPV6);
0683         }
0684         if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6dst)) {
0685             memcpy(&pkt->ip6dst, &ipv6_usr_hdr->ip6dst,
0686                    sizeof(pkt->ip6dst));
0687             memcpy(&pmask->ip6dst, &ipv6_usr_mask->ip6dst,
0688                    sizeof(pmask->ip6dst));
0689             req->features |= BIT_ULL(NPC_DIP_IPV6);
0690         }
0691         pkt->etype = cpu_to_be16(ETH_P_IPV6);
0692         pmask->etype = cpu_to_be16(0xFFFF);
0693         req->features |= BIT_ULL(NPC_ETYPE);
0694         break;
0695     case TCP_V6_FLOW:
0696     case UDP_V6_FLOW:
0697     case SCTP_V6_FLOW:
0698         pkt->etype = cpu_to_be16(ETH_P_IPV6);
0699         pmask->etype = cpu_to_be16(0xFFFF);
0700         req->features |= BIT_ULL(NPC_ETYPE);
0701         if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6src)) {
0702             memcpy(&pkt->ip6src, &ipv6_l4_hdr->ip6src,
0703                    sizeof(pkt->ip6src));
0704             memcpy(&pmask->ip6src, &ipv6_l4_mask->ip6src,
0705                    sizeof(pmask->ip6src));
0706             req->features |= BIT_ULL(NPC_SIP_IPV6);
0707         }
0708         if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6dst)) {
0709             memcpy(&pkt->ip6dst, &ipv6_l4_hdr->ip6dst,
0710                    sizeof(pkt->ip6dst));
0711             memcpy(&pmask->ip6dst, &ipv6_l4_mask->ip6dst,
0712                    sizeof(pmask->ip6dst));
0713             req->features |= BIT_ULL(NPC_DIP_IPV6);
0714         }
0715         if (ipv6_l4_mask->psrc) {
0716             memcpy(&pkt->sport, &ipv6_l4_hdr->psrc,
0717                    sizeof(pkt->sport));
0718             memcpy(&pmask->sport, &ipv6_l4_mask->psrc,
0719                    sizeof(pmask->sport));
0720             if (flow_type == UDP_V6_FLOW)
0721                 req->features |= BIT_ULL(NPC_SPORT_UDP);
0722             else if (flow_type == TCP_V6_FLOW)
0723                 req->features |= BIT_ULL(NPC_SPORT_TCP);
0724             else
0725                 req->features |= BIT_ULL(NPC_SPORT_SCTP);
0726         }
0727         if (ipv6_l4_mask->pdst) {
0728             memcpy(&pkt->dport, &ipv6_l4_hdr->pdst,
0729                    sizeof(pkt->dport));
0730             memcpy(&pmask->dport, &ipv6_l4_mask->pdst,
0731                    sizeof(pmask->dport));
0732             if (flow_type == UDP_V6_FLOW)
0733                 req->features |= BIT_ULL(NPC_DPORT_UDP);
0734             else if (flow_type == TCP_V6_FLOW)
0735                 req->features |= BIT_ULL(NPC_DPORT_TCP);
0736             else
0737                 req->features |= BIT_ULL(NPC_DPORT_SCTP);
0738         }
0739         if (flow_type == UDP_V6_FLOW)
0740             req->features |= BIT_ULL(NPC_IPPROTO_UDP);
0741         else if (flow_type == TCP_V6_FLOW)
0742             req->features |= BIT_ULL(NPC_IPPROTO_TCP);
0743         else
0744             req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
0745         break;
0746     case AH_V6_FLOW:
0747     case ESP_V6_FLOW:
0748         pkt->etype = cpu_to_be16(ETH_P_IPV6);
0749         pmask->etype = cpu_to_be16(0xFFFF);
0750         req->features |= BIT_ULL(NPC_ETYPE);
0751         if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6src)) {
0752             memcpy(&pkt->ip6src, &ah_esp_hdr->ip6src,
0753                    sizeof(pkt->ip6src));
0754             memcpy(&pmask->ip6src, &ah_esp_mask->ip6src,
0755                    sizeof(pmask->ip6src));
0756             req->features |= BIT_ULL(NPC_SIP_IPV6);
0757         }
0758         if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6dst)) {
0759             memcpy(&pkt->ip6dst, &ah_esp_hdr->ip6dst,
0760                    sizeof(pkt->ip6dst));
0761             memcpy(&pmask->ip6dst, &ah_esp_mask->ip6dst,
0762                    sizeof(pmask->ip6dst));
0763             req->features |= BIT_ULL(NPC_DIP_IPV6);
0764         }
0765 
0766         /* NPC profile doesn't extract AH/ESP header fields */
0767         if ((ah_esp_mask->spi & ah_esp_hdr->spi) ||
0768             (ah_esp_mask->tclass & ah_esp_mask->tclass))
0769             return -EOPNOTSUPP;
0770 
0771         if (flow_type == AH_V6_FLOW)
0772             req->features |= BIT_ULL(NPC_IPPROTO_AH);
0773         else
0774             req->features |= BIT_ULL(NPC_IPPROTO_ESP);
0775         break;
0776     default:
0777         break;
0778     }
0779 
0780     return 0;
0781 }
0782 
0783 static int otx2_prepare_flow_request(struct ethtool_rx_flow_spec *fsp,
0784                   struct npc_install_flow_req *req)
0785 {
0786     struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
0787     struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
0788     struct flow_msg *pmask = &req->mask;
0789     struct flow_msg *pkt = &req->packet;
0790     u32 flow_type;
0791     int ret;
0792 
0793     flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
0794     switch (flow_type) {
0795     /* bits not set in mask are don't care */
0796     case ETHER_FLOW:
0797         if (!is_zero_ether_addr(eth_mask->h_source)) {
0798             ether_addr_copy(pkt->smac, eth_hdr->h_source);
0799             ether_addr_copy(pmask->smac, eth_mask->h_source);
0800             req->features |= BIT_ULL(NPC_SMAC);
0801         }
0802         if (!is_zero_ether_addr(eth_mask->h_dest)) {
0803             ether_addr_copy(pkt->dmac, eth_hdr->h_dest);
0804             ether_addr_copy(pmask->dmac, eth_mask->h_dest);
0805             req->features |= BIT_ULL(NPC_DMAC);
0806         }
0807         if (eth_hdr->h_proto) {
0808             memcpy(&pkt->etype, &eth_hdr->h_proto,
0809                    sizeof(pkt->etype));
0810             memcpy(&pmask->etype, &eth_mask->h_proto,
0811                    sizeof(pmask->etype));
0812             req->features |= BIT_ULL(NPC_ETYPE);
0813         }
0814         break;
0815     case IP_USER_FLOW:
0816     case TCP_V4_FLOW:
0817     case UDP_V4_FLOW:
0818     case SCTP_V4_FLOW:
0819     case AH_V4_FLOW:
0820     case ESP_V4_FLOW:
0821         ret = otx2_prepare_ipv4_flow(fsp, req, flow_type);
0822         if (ret)
0823             return ret;
0824         break;
0825     case IPV6_USER_FLOW:
0826     case TCP_V6_FLOW:
0827     case UDP_V6_FLOW:
0828     case SCTP_V6_FLOW:
0829     case AH_V6_FLOW:
0830     case ESP_V6_FLOW:
0831         ret = otx2_prepare_ipv6_flow(fsp, req, flow_type);
0832         if (ret)
0833             return ret;
0834         break;
0835     default:
0836         return -EOPNOTSUPP;
0837     }
0838     if (fsp->flow_type & FLOW_EXT) {
0839         u16 vlan_etype;
0840 
0841         if (fsp->m_ext.vlan_etype) {
0842             /* Partial masks not supported */
0843             if (be16_to_cpu(fsp->m_ext.vlan_etype) != 0xFFFF)
0844                 return -EINVAL;
0845 
0846             vlan_etype = be16_to_cpu(fsp->h_ext.vlan_etype);
0847             /* Only ETH_P_8021Q and ETH_P_802AD types supported */
0848             if (vlan_etype != ETH_P_8021Q &&
0849                 vlan_etype != ETH_P_8021AD)
0850                 return -EINVAL;
0851 
0852             memcpy(&pkt->vlan_etype, &fsp->h_ext.vlan_etype,
0853                    sizeof(pkt->vlan_etype));
0854             memcpy(&pmask->vlan_etype, &fsp->m_ext.vlan_etype,
0855                    sizeof(pmask->vlan_etype));
0856 
0857             if (vlan_etype == ETH_P_8021Q)
0858                 req->features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG);
0859             else
0860                 req->features |= BIT_ULL(NPC_VLAN_ETYPE_STAG);
0861         }
0862 
0863         if (fsp->m_ext.vlan_tci) {
0864             memcpy(&pkt->vlan_tci, &fsp->h_ext.vlan_tci,
0865                    sizeof(pkt->vlan_tci));
0866             memcpy(&pmask->vlan_tci, &fsp->m_ext.vlan_tci,
0867                    sizeof(pmask->vlan_tci));
0868             req->features |= BIT_ULL(NPC_OUTER_VID);
0869         }
0870 
0871         /* Not Drop/Direct to queue but use action in default entry */
0872         if (fsp->m_ext.data[1] &&
0873             fsp->h_ext.data[1] == cpu_to_be32(OTX2_DEFAULT_ACTION))
0874             req->op = NIX_RX_ACTION_DEFAULT;
0875     }
0876 
0877     if (fsp->flow_type & FLOW_MAC_EXT &&
0878         !is_zero_ether_addr(fsp->m_ext.h_dest)) {
0879         ether_addr_copy(pkt->dmac, fsp->h_ext.h_dest);
0880         ether_addr_copy(pmask->dmac, fsp->m_ext.h_dest);
0881         req->features |= BIT_ULL(NPC_DMAC);
0882     }
0883 
0884     if (!req->features)
0885         return -EOPNOTSUPP;
0886 
0887     return 0;
0888 }
0889 
0890 static int otx2_is_flow_rule_dmacfilter(struct otx2_nic *pfvf,
0891                     struct ethtool_rx_flow_spec *fsp)
0892 {
0893     struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
0894     struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
0895     u64 ring_cookie = fsp->ring_cookie;
0896     u32 flow_type;
0897 
0898     if (!(pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT))
0899         return false;
0900 
0901     flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
0902 
0903     /* CGX/RPM block dmac filtering configured for white listing
0904      * check for action other than DROP
0905      */
0906     if (flow_type == ETHER_FLOW && ring_cookie != RX_CLS_FLOW_DISC &&
0907         !ethtool_get_flow_spec_ring_vf(ring_cookie)) {
0908         if (is_zero_ether_addr(eth_mask->h_dest) &&
0909             is_valid_ether_addr(eth_hdr->h_dest))
0910             return true;
0911     }
0912 
0913     return false;
0914 }
0915 
0916 static int otx2_add_flow_msg(struct otx2_nic *pfvf, struct otx2_flow *flow)
0917 {
0918     u64 ring_cookie = flow->flow_spec.ring_cookie;
0919 #ifdef CONFIG_DCB
0920     int vlan_prio, qidx, pfc_rule = 0;
0921 #endif
0922     struct npc_install_flow_req *req;
0923     int err, vf = 0;
0924 
0925     mutex_lock(&pfvf->mbox.lock);
0926     req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
0927     if (!req) {
0928         mutex_unlock(&pfvf->mbox.lock);
0929         return -ENOMEM;
0930     }
0931 
0932     err = otx2_prepare_flow_request(&flow->flow_spec, req);
0933     if (err) {
0934         /* free the allocated msg above */
0935         otx2_mbox_reset(&pfvf->mbox.mbox, 0);
0936         mutex_unlock(&pfvf->mbox.lock);
0937         return err;
0938     }
0939 
0940     req->entry = flow->entry;
0941     req->intf = NIX_INTF_RX;
0942     req->set_cntr = 1;
0943     req->channel = pfvf->hw.rx_chan_base;
0944     if (ring_cookie == RX_CLS_FLOW_DISC) {
0945         req->op = NIX_RX_ACTIONOP_DROP;
0946     } else {
0947         /* change to unicast only if action of default entry is not
0948          * requested by user
0949          */
0950         if (flow->flow_spec.flow_type & FLOW_RSS) {
0951             req->op = NIX_RX_ACTIONOP_RSS;
0952             req->index = flow->rss_ctx_id;
0953             req->flow_key_alg = pfvf->hw.flowkey_alg_idx;
0954         } else {
0955             req->op = NIX_RX_ACTIONOP_UCAST;
0956             req->index = ethtool_get_flow_spec_ring(ring_cookie);
0957         }
0958         vf = ethtool_get_flow_spec_ring_vf(ring_cookie);
0959         if (vf > pci_num_vf(pfvf->pdev)) {
0960             mutex_unlock(&pfvf->mbox.lock);
0961             return -EINVAL;
0962         }
0963 
0964 #ifdef CONFIG_DCB
0965         /* Identify PFC rule if PFC enabled and ntuple rule is vlan */
0966         if (!vf && (req->features & BIT_ULL(NPC_OUTER_VID)) &&
0967             pfvf->pfc_en && req->op != NIX_RX_ACTIONOP_RSS) {
0968             vlan_prio = ntohs(req->packet.vlan_tci) &
0969                     ntohs(req->mask.vlan_tci);
0970 
0971             /* Get the priority */
0972             vlan_prio >>= 13;
0973             flow->rule_type |= PFC_FLOWCTRL_RULE;
0974             /* Check if PFC enabled for this priority */
0975             if (pfvf->pfc_en & BIT(vlan_prio)) {
0976                 pfc_rule = true;
0977                 qidx = req->index;
0978             }
0979         }
0980 #endif
0981     }
0982 
0983     /* ethtool ring_cookie has (VF + 1) for VF */
0984     if (vf) {
0985         req->vf = vf;
0986         flow->is_vf = true;
0987         flow->vf = vf;
0988     }
0989 
0990     /* Send message to AF */
0991     err = otx2_sync_mbox_msg(&pfvf->mbox);
0992 
0993 #ifdef CONFIG_DCB
0994     if (!err && pfc_rule)
0995         otx2_update_bpid_in_rqctx(pfvf, vlan_prio, qidx, true);
0996 #endif
0997 
0998     mutex_unlock(&pfvf->mbox.lock);
0999     return err;
1000 }
1001 
1002 static int otx2_add_flow_with_pfmac(struct otx2_nic *pfvf,
1003                     struct otx2_flow *flow)
1004 {
1005     struct otx2_flow *pf_mac;
1006     struct ethhdr *eth_hdr;
1007 
1008     pf_mac = kzalloc(sizeof(*pf_mac), GFP_KERNEL);
1009     if (!pf_mac)
1010         return -ENOMEM;
1011 
1012     pf_mac->entry = 0;
1013     pf_mac->rule_type |= DMAC_FILTER_RULE;
1014     pf_mac->location = pfvf->flow_cfg->max_flows;
1015     memcpy(&pf_mac->flow_spec, &flow->flow_spec,
1016            sizeof(struct ethtool_rx_flow_spec));
1017     pf_mac->flow_spec.location = pf_mac->location;
1018 
1019     /* Copy PF mac address */
1020     eth_hdr = &pf_mac->flow_spec.h_u.ether_spec;
1021     ether_addr_copy(eth_hdr->h_dest, pfvf->netdev->dev_addr);
1022 
1023     /* Install DMAC filter with PF mac address */
1024     otx2_dmacflt_add(pfvf, eth_hdr->h_dest, 0);
1025 
1026     otx2_add_flow_to_list(pfvf, pf_mac);
1027     pfvf->flow_cfg->nr_flows++;
1028     set_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1029 
1030     return 0;
1031 }
1032 
1033 int otx2_add_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc)
1034 {
1035     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1036     struct ethtool_rx_flow_spec *fsp = &nfc->fs;
1037     struct otx2_flow *flow;
1038     struct ethhdr *eth_hdr;
1039     bool new = false;
1040     int err = 0;
1041     u32 ring;
1042 
1043     if (!flow_cfg->max_flows) {
1044         netdev_err(pfvf->netdev,
1045                "Ntuple rule count is 0, allocate and retry\n");
1046         return -EINVAL;
1047     }
1048 
1049     ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
1050     if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1051         return -ENOMEM;
1052 
1053     if (ring >= pfvf->hw.rx_queues && fsp->ring_cookie != RX_CLS_FLOW_DISC)
1054         return -EINVAL;
1055 
1056     if (fsp->location >= otx2_get_maxflows(flow_cfg))
1057         return -EINVAL;
1058 
1059     flow = otx2_find_flow(pfvf, fsp->location);
1060     if (!flow) {
1061         flow = kzalloc(sizeof(*flow), GFP_KERNEL);
1062         if (!flow)
1063             return -ENOMEM;
1064         flow->location = fsp->location;
1065         flow->entry = flow_cfg->flow_ent[flow->location];
1066         new = true;
1067     }
1068     /* struct copy */
1069     flow->flow_spec = *fsp;
1070 
1071     if (fsp->flow_type & FLOW_RSS)
1072         flow->rss_ctx_id = nfc->rss_context;
1073 
1074     if (otx2_is_flow_rule_dmacfilter(pfvf, &flow->flow_spec)) {
1075         eth_hdr = &flow->flow_spec.h_u.ether_spec;
1076 
1077         /* Sync dmac filter table with updated fields */
1078         if (flow->rule_type & DMAC_FILTER_RULE)
1079             return otx2_dmacflt_update(pfvf, eth_hdr->h_dest,
1080                            flow->entry);
1081 
1082         if (bitmap_full(flow_cfg->dmacflt_bmap,
1083                 flow_cfg->dmacflt_max_flows)) {
1084             netdev_warn(pfvf->netdev,
1085                     "Can't insert the rule %d as max allowed dmac filters are %d\n",
1086                     flow->location +
1087                     flow_cfg->dmacflt_max_flows,
1088                     flow_cfg->dmacflt_max_flows);
1089             err = -EINVAL;
1090             if (new)
1091                 kfree(flow);
1092             return err;
1093         }
1094 
1095         /* Install PF mac address to DMAC filter list */
1096         if (!test_bit(0, flow_cfg->dmacflt_bmap))
1097             otx2_add_flow_with_pfmac(pfvf, flow);
1098 
1099         flow->rule_type |= DMAC_FILTER_RULE;
1100         flow->entry = find_first_zero_bit(flow_cfg->dmacflt_bmap,
1101                           flow_cfg->dmacflt_max_flows);
1102         fsp->location = flow_cfg->max_flows + flow->entry;
1103         flow->flow_spec.location = fsp->location;
1104         flow->location = fsp->location;
1105 
1106         set_bit(flow->entry, flow_cfg->dmacflt_bmap);
1107         otx2_dmacflt_add(pfvf, eth_hdr->h_dest, flow->entry);
1108 
1109     } else {
1110         if (flow->location >= pfvf->flow_cfg->max_flows) {
1111             netdev_warn(pfvf->netdev,
1112                     "Can't insert non dmac ntuple rule at %d, allowed range %d-0\n",
1113                     flow->location,
1114                     flow_cfg->max_flows - 1);
1115             err = -EINVAL;
1116         } else {
1117             err = otx2_add_flow_msg(pfvf, flow);
1118         }
1119     }
1120 
1121     if (err) {
1122         if (err == MBOX_MSG_INVALID)
1123             err = -EINVAL;
1124         if (new)
1125             kfree(flow);
1126         return err;
1127     }
1128 
1129     /* add the new flow installed to list */
1130     if (new) {
1131         otx2_add_flow_to_list(pfvf, flow);
1132         flow_cfg->nr_flows++;
1133     }
1134 
1135     return 0;
1136 }
1137 
1138 static int otx2_remove_flow_msg(struct otx2_nic *pfvf, u16 entry, bool all)
1139 {
1140     struct npc_delete_flow_req *req;
1141     int err;
1142 
1143     mutex_lock(&pfvf->mbox.lock);
1144     req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1145     if (!req) {
1146         mutex_unlock(&pfvf->mbox.lock);
1147         return -ENOMEM;
1148     }
1149 
1150     req->entry = entry;
1151     if (all)
1152         req->all = 1;
1153 
1154     /* Send message to AF */
1155     err = otx2_sync_mbox_msg(&pfvf->mbox);
1156     mutex_unlock(&pfvf->mbox.lock);
1157     return err;
1158 }
1159 
1160 static void otx2_update_rem_pfmac(struct otx2_nic *pfvf, int req)
1161 {
1162     struct otx2_flow *iter;
1163     struct ethhdr *eth_hdr;
1164     bool found = false;
1165 
1166     list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
1167         if ((iter->rule_type & DMAC_FILTER_RULE) && iter->entry == 0) {
1168             eth_hdr = &iter->flow_spec.h_u.ether_spec;
1169             if (req == DMAC_ADDR_DEL) {
1170                 otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1171                             0);
1172                 clear_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1173                 found = true;
1174             } else {
1175                 ether_addr_copy(eth_hdr->h_dest,
1176                         pfvf->netdev->dev_addr);
1177 
1178                 otx2_dmacflt_update(pfvf, eth_hdr->h_dest, 0);
1179             }
1180             break;
1181         }
1182     }
1183 
1184     if (found) {
1185         list_del(&iter->list);
1186         kfree(iter);
1187         pfvf->flow_cfg->nr_flows--;
1188     }
1189 }
1190 
1191 int otx2_remove_flow(struct otx2_nic *pfvf, u32 location)
1192 {
1193     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1194     struct otx2_flow *flow;
1195     int err;
1196 
1197     if (location >= otx2_get_maxflows(flow_cfg))
1198         return -EINVAL;
1199 
1200     flow = otx2_find_flow(pfvf, location);
1201     if (!flow)
1202         return -ENOENT;
1203 
1204     if (flow->rule_type & DMAC_FILTER_RULE) {
1205         struct ethhdr *eth_hdr = &flow->flow_spec.h_u.ether_spec;
1206 
1207         /* user not allowed to remove dmac filter with interface mac */
1208         if (ether_addr_equal(pfvf->netdev->dev_addr, eth_hdr->h_dest))
1209             return -EPERM;
1210 
1211         err = otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1212                       flow->entry);
1213         clear_bit(flow->entry, flow_cfg->dmacflt_bmap);
1214         /* If all dmac filters are removed delete macfilter with
1215          * interface mac address and configure CGX/RPM block in
1216          * promiscuous mode
1217          */
1218         if (bitmap_weight(flow_cfg->dmacflt_bmap,
1219                   flow_cfg->dmacflt_max_flows) == 1)
1220             otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
1221     } else {
1222 #ifdef CONFIG_DCB
1223         if (flow->rule_type & PFC_FLOWCTRL_RULE)
1224             otx2_update_bpid_in_rqctx(pfvf, 0,
1225                           flow->flow_spec.ring_cookie,
1226                           false);
1227 #endif
1228 
1229         err = otx2_remove_flow_msg(pfvf, flow->entry, false);
1230     }
1231 
1232     if (err)
1233         return err;
1234 
1235     list_del(&flow->list);
1236     kfree(flow);
1237     flow_cfg->nr_flows--;
1238 
1239     return 0;
1240 }
1241 
1242 void otx2_rss_ctx_flow_del(struct otx2_nic *pfvf, int ctx_id)
1243 {
1244     struct otx2_flow *flow, *tmp;
1245     int err;
1246 
1247     list_for_each_entry_safe(flow, tmp, &pfvf->flow_cfg->flow_list, list) {
1248         if (flow->rss_ctx_id != ctx_id)
1249             continue;
1250         err = otx2_remove_flow(pfvf, flow->location);
1251         if (err)
1252             netdev_warn(pfvf->netdev,
1253                     "Can't delete the rule %d associated with this rss group err:%d",
1254                     flow->location, err);
1255     }
1256 }
1257 
1258 int otx2_destroy_ntuple_flows(struct otx2_nic *pfvf)
1259 {
1260     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1261     struct npc_delete_flow_req *req;
1262     struct otx2_flow *iter, *tmp;
1263     int err;
1264 
1265     if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1266         return 0;
1267 
1268     if (!flow_cfg->max_flows)
1269         return 0;
1270 
1271     mutex_lock(&pfvf->mbox.lock);
1272     req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1273     if (!req) {
1274         mutex_unlock(&pfvf->mbox.lock);
1275         return -ENOMEM;
1276     }
1277 
1278     req->start = flow_cfg->flow_ent[0];
1279     req->end   = flow_cfg->flow_ent[flow_cfg->max_flows - 1];
1280     err = otx2_sync_mbox_msg(&pfvf->mbox);
1281     mutex_unlock(&pfvf->mbox.lock);
1282 
1283     list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1284         list_del(&iter->list);
1285         kfree(iter);
1286         flow_cfg->nr_flows--;
1287     }
1288     return err;
1289 }
1290 
1291 int otx2_destroy_mcam_flows(struct otx2_nic *pfvf)
1292 {
1293     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1294     struct npc_mcam_free_entry_req *req;
1295     struct otx2_flow *iter, *tmp;
1296     int err;
1297 
1298     if (!(pfvf->flags & OTX2_FLAG_MCAM_ENTRIES_ALLOC))
1299         return 0;
1300 
1301     /* remove all flows */
1302     err = otx2_remove_flow_msg(pfvf, 0, true);
1303     if (err)
1304         return err;
1305 
1306     list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1307         list_del(&iter->list);
1308         kfree(iter);
1309         flow_cfg->nr_flows--;
1310     }
1311 
1312     mutex_lock(&pfvf->mbox.lock);
1313     req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
1314     if (!req) {
1315         mutex_unlock(&pfvf->mbox.lock);
1316         return -ENOMEM;
1317     }
1318 
1319     req->all = 1;
1320     /* Send message to AF to free MCAM entries */
1321     err = otx2_sync_mbox_msg(&pfvf->mbox);
1322     if (err) {
1323         mutex_unlock(&pfvf->mbox.lock);
1324         return err;
1325     }
1326 
1327     pfvf->flags &= ~OTX2_FLAG_MCAM_ENTRIES_ALLOC;
1328     mutex_unlock(&pfvf->mbox.lock);
1329 
1330     return 0;
1331 }
1332 
1333 int otx2_install_rxvlan_offload_flow(struct otx2_nic *pfvf)
1334 {
1335     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1336     struct npc_install_flow_req *req;
1337     int err;
1338 
1339     mutex_lock(&pfvf->mbox.lock);
1340     req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
1341     if (!req) {
1342         mutex_unlock(&pfvf->mbox.lock);
1343         return -ENOMEM;
1344     }
1345 
1346     req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1347     req->intf = NIX_INTF_RX;
1348     ether_addr_copy(req->packet.dmac, pfvf->netdev->dev_addr);
1349     eth_broadcast_addr((u8 *)&req->mask.dmac);
1350     req->channel = pfvf->hw.rx_chan_base;
1351     req->op = NIX_RX_ACTION_DEFAULT;
1352     req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
1353     req->vtag0_valid = true;
1354     req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1355 
1356     /* Send message to AF */
1357     err = otx2_sync_mbox_msg(&pfvf->mbox);
1358     mutex_unlock(&pfvf->mbox.lock);
1359     return err;
1360 }
1361 
1362 static int otx2_delete_rxvlan_offload_flow(struct otx2_nic *pfvf)
1363 {
1364     struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1365     struct npc_delete_flow_req *req;
1366     int err;
1367 
1368     mutex_lock(&pfvf->mbox.lock);
1369     req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1370     if (!req) {
1371         mutex_unlock(&pfvf->mbox.lock);
1372         return -ENOMEM;
1373     }
1374 
1375     req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1376     /* Send message to AF */
1377     err = otx2_sync_mbox_msg(&pfvf->mbox);
1378     mutex_unlock(&pfvf->mbox.lock);
1379     return err;
1380 }
1381 
1382 int otx2_enable_rxvlan(struct otx2_nic *pf, bool enable)
1383 {
1384     struct nix_vtag_config *req;
1385     struct mbox_msghdr *rsp_hdr;
1386     int err;
1387 
1388     /* Dont have enough mcam entries */
1389     if (!(pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT))
1390         return -ENOMEM;
1391 
1392     if (enable) {
1393         err = otx2_install_rxvlan_offload_flow(pf);
1394         if (err)
1395             return err;
1396     } else {
1397         err = otx2_delete_rxvlan_offload_flow(pf);
1398         if (err)
1399             return err;
1400     }
1401 
1402     mutex_lock(&pf->mbox.lock);
1403     req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
1404     if (!req) {
1405         mutex_unlock(&pf->mbox.lock);
1406         return -ENOMEM;
1407     }
1408 
1409     /* config strip, capture and size */
1410     req->vtag_size = VTAGSIZE_T4;
1411     req->cfg_type = 1; /* rx vlan cfg */
1412     req->rx.vtag_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1413     req->rx.strip_vtag = enable;
1414     req->rx.capture_vtag = enable;
1415 
1416     err = otx2_sync_mbox_msg(&pf->mbox);
1417     if (err) {
1418         mutex_unlock(&pf->mbox.lock);
1419         return err;
1420     }
1421 
1422     rsp_hdr = otx2_mbox_get_rsp(&pf->mbox.mbox, 0, &req->hdr);
1423     if (IS_ERR(rsp_hdr)) {
1424         mutex_unlock(&pf->mbox.lock);
1425         return PTR_ERR(rsp_hdr);
1426     }
1427 
1428     mutex_unlock(&pf->mbox.lock);
1429     return rsp_hdr->rc;
1430 }
1431 
1432 void otx2_dmacflt_reinstall_flows(struct otx2_nic *pf)
1433 {
1434     struct otx2_flow *iter;
1435     struct ethhdr *eth_hdr;
1436 
1437     list_for_each_entry(iter, &pf->flow_cfg->flow_list, list) {
1438         if (iter->rule_type & DMAC_FILTER_RULE) {
1439             eth_hdr = &iter->flow_spec.h_u.ether_spec;
1440             otx2_dmacflt_add(pf, eth_hdr->h_dest,
1441                      iter->entry);
1442         }
1443     }
1444 }
1445 
1446 void otx2_dmacflt_update_pfmac_flow(struct otx2_nic *pfvf)
1447 {
1448     otx2_update_rem_pfmac(pfvf, DMAC_ADDR_UPDATE);
1449 }