10e11262d48a0b3ae7a9ba4c74e5b8bfd4e5eae6
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / marvell / octeontx2 / nic / otx2_flows.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7
8 #include <net/ipv6.h>
9 #include <linux/sort.h>
10
11 #include "otx2_common.h"
12
13 #define OTX2_DEFAULT_ACTION     0x1
14
15 static int otx2_mcam_entry_init(struct otx2_nic *pfvf);
16
17 struct otx2_flow {
18         struct ethtool_rx_flow_spec flow_spec;
19         struct list_head list;
20         u32 location;
21         u32 entry;
22         bool is_vf;
23         u8 rss_ctx_id;
24 #define DMAC_FILTER_RULE                BIT(0)
25 #define PFC_FLOWCTRL_RULE               BIT(1)
26         u16 rule_type;
27         int vf;
28 };
29
30 enum dmac_req {
31         DMAC_ADDR_UPDATE,
32         DMAC_ADDR_DEL
33 };
34
35 static void otx2_clear_ntuple_flow_info(struct otx2_nic *pfvf, struct otx2_flow_config *flow_cfg)
36 {
37         devm_kfree(pfvf->dev, flow_cfg->flow_ent);
38         flow_cfg->flow_ent = NULL;
39         flow_cfg->max_flows = 0;
40 }
41
42 static int otx2_free_ntuple_mcam_entries(struct otx2_nic *pfvf)
43 {
44         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
45         struct npc_mcam_free_entry_req *req;
46         int ent, err;
47
48         if (!flow_cfg->max_flows)
49                 return 0;
50
51         mutex_lock(&pfvf->mbox.lock);
52         for (ent = 0; ent < flow_cfg->max_flows; ent++) {
53                 req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
54                 if (!req)
55                         break;
56
57                 req->entry = flow_cfg->flow_ent[ent];
58
59                 /* Send message to AF to free MCAM entries */
60                 err = otx2_sync_mbox_msg(&pfvf->mbox);
61                 if (err)
62                         break;
63         }
64         mutex_unlock(&pfvf->mbox.lock);
65         otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
66         return 0;
67 }
68
69 static int mcam_entry_cmp(const void *a, const void *b)
70 {
71         return *(u16 *)a - *(u16 *)b;
72 }
73
74 int otx2_alloc_mcam_entries(struct otx2_nic *pfvf, u16 count)
75 {
76         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
77         struct npc_mcam_alloc_entry_req *req;
78         struct npc_mcam_alloc_entry_rsp *rsp;
79         int ent, allocated = 0;
80
81         /* Free current ones and allocate new ones with requested count */
82         otx2_free_ntuple_mcam_entries(pfvf);
83
84         if (!count)
85                 return 0;
86
87         flow_cfg->flow_ent = devm_kmalloc_array(pfvf->dev, count,
88                                                 sizeof(u16), GFP_KERNEL);
89         if (!flow_cfg->flow_ent) {
90                 netdev_err(pfvf->netdev,
91                            "%s: Unable to allocate memory for flow entries\n",
92                             __func__);
93                 return -ENOMEM;
94         }
95
96         mutex_lock(&pfvf->mbox.lock);
97
98         /* In a single request a max of NPC_MAX_NONCONTIG_ENTRIES MCAM entries
99          * can only be allocated.
100          */
101         while (allocated < count) {
102                 req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
103                 if (!req)
104                         goto exit;
105
106                 req->contig = false;
107                 req->count = (count - allocated) > NPC_MAX_NONCONTIG_ENTRIES ?
108                                 NPC_MAX_NONCONTIG_ENTRIES : count - allocated;
109
110                 /* Allocate higher priority entries for PFs, so that VF's entries
111                  * will be on top of PF.
112                  */
113                 if (!is_otx2_vf(pfvf->pcifunc)) {
114                         req->priority = NPC_MCAM_HIGHER_PRIO;
115                         req->ref_entry = flow_cfg->def_ent[0];
116                 }
117
118                 /* Send message to AF */
119                 if (otx2_sync_mbox_msg(&pfvf->mbox))
120                         goto exit;
121
122                 rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
123                         (&pfvf->mbox.mbox, 0, &req->hdr);
124
125                 for (ent = 0; ent < rsp->count; ent++)
126                         flow_cfg->flow_ent[ent + allocated] = rsp->entry_list[ent];
127
128                 allocated += rsp->count;
129
130                 /* If this request is not fulfilled, no need to send
131                  * further requests.
132                  */
133                 if (rsp->count != req->count)
134                         break;
135         }
136
137         /* Multiple MCAM entry alloc requests could result in non-sequential
138          * MCAM entries in the flow_ent[] array. Sort them in an ascending order,
139          * otherwise user installed ntuple filter index and MCAM entry index will
140          * not be in sync.
141          */
142         if (allocated)
143                 sort(&flow_cfg->flow_ent[0], allocated,
144                      sizeof(flow_cfg->flow_ent[0]), mcam_entry_cmp, NULL);
145
146 exit:
147         mutex_unlock(&pfvf->mbox.lock);
148
149         flow_cfg->max_flows = allocated;
150
151         if (allocated) {
152                 pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
153                 pfvf->flags |= OTX2_FLAG_NTUPLE_SUPPORT;
154         }
155
156         if (allocated != count)
157                 netdev_info(pfvf->netdev,
158                             "Unable to allocate %d MCAM entries, got only %d\n",
159                             count, allocated);
160         return allocated;
161 }
162 EXPORT_SYMBOL(otx2_alloc_mcam_entries);
163
164 static int otx2_mcam_entry_init(struct otx2_nic *pfvf)
165 {
166         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
167         struct npc_get_field_status_req *freq;
168         struct npc_get_field_status_rsp *frsp;
169         struct npc_mcam_alloc_entry_req *req;
170         struct npc_mcam_alloc_entry_rsp *rsp;
171         int vf_vlan_max_flows;
172         int ent, count;
173
174         vf_vlan_max_flows = pfvf->total_vfs * OTX2_PER_VF_VLAN_FLOWS;
175         count = OTX2_MAX_UNICAST_FLOWS +
176                         OTX2_MAX_VLAN_FLOWS + vf_vlan_max_flows;
177
178         flow_cfg->def_ent = devm_kmalloc_array(pfvf->dev, count,
179                                                sizeof(u16), GFP_KERNEL);
180         if (!flow_cfg->def_ent)
181                 return -ENOMEM;
182
183         mutex_lock(&pfvf->mbox.lock);
184
185         req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
186         if (!req) {
187                 mutex_unlock(&pfvf->mbox.lock);
188                 return -ENOMEM;
189         }
190
191         req->contig = false;
192         req->count = count;
193
194         /* Send message to AF */
195         if (otx2_sync_mbox_msg(&pfvf->mbox)) {
196                 mutex_unlock(&pfvf->mbox.lock);
197                 return -EINVAL;
198         }
199
200         rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
201                (&pfvf->mbox.mbox, 0, &req->hdr);
202
203         if (rsp->count != req->count) {
204                 netdev_info(pfvf->netdev,
205                             "Unable to allocate MCAM entries for ucast, vlan and vf_vlan\n");
206                 mutex_unlock(&pfvf->mbox.lock);
207                 devm_kfree(pfvf->dev, flow_cfg->def_ent);
208                 return 0;
209         }
210
211         for (ent = 0; ent < rsp->count; ent++)
212                 flow_cfg->def_ent[ent] = rsp->entry_list[ent];
213
214         flow_cfg->vf_vlan_offset = 0;
215         flow_cfg->unicast_offset = vf_vlan_max_flows;
216         flow_cfg->rx_vlan_offset = flow_cfg->unicast_offset +
217                                         OTX2_MAX_UNICAST_FLOWS;
218         pfvf->flags |= OTX2_FLAG_UCAST_FLTR_SUPPORT;
219
220         /* Check if NPC_DMAC field is supported
221          * by the mkex profile before setting VLAN support flag.
222          */
223         freq = otx2_mbox_alloc_msg_npc_get_field_status(&pfvf->mbox);
224         if (!freq) {
225                 mutex_unlock(&pfvf->mbox.lock);
226                 return -ENOMEM;
227         }
228
229         freq->field = NPC_DMAC;
230         if (otx2_sync_mbox_msg(&pfvf->mbox)) {
231                 mutex_unlock(&pfvf->mbox.lock);
232                 return -EINVAL;
233         }
234
235         frsp = (struct npc_get_field_status_rsp *)otx2_mbox_get_rsp
236                (&pfvf->mbox.mbox, 0, &freq->hdr);
237
238         if (frsp->enable) {
239                 pfvf->flags |= OTX2_FLAG_RX_VLAN_SUPPORT;
240                 pfvf->flags |= OTX2_FLAG_VF_VLAN_SUPPORT;
241         }
242
243         pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
244         mutex_unlock(&pfvf->mbox.lock);
245
246         /* Allocate entries for Ntuple filters */
247         count = otx2_alloc_mcam_entries(pfvf, OTX2_DEFAULT_FLOWCOUNT);
248         if (count <= 0) {
249                 otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
250                 return 0;
251         }
252
253         pfvf->flags |= OTX2_FLAG_TC_FLOWER_SUPPORT;
254
255         return 0;
256 }
257
258 /* TODO : revisit on size */
259 #define OTX2_DMAC_FLTR_BITMAP_SZ (4 * 2048 + 32)
260
261 int otx2vf_mcam_flow_init(struct otx2_nic *pfvf)
262 {
263         struct otx2_flow_config *flow_cfg;
264
265         pfvf->flow_cfg = devm_kzalloc(pfvf->dev,
266                                       sizeof(struct otx2_flow_config),
267                                       GFP_KERNEL);
268         if (!pfvf->flow_cfg)
269                 return -ENOMEM;
270
271         pfvf->flow_cfg->dmacflt_bmap = devm_kcalloc(pfvf->dev,
272                                                     BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
273                                                     sizeof(long), GFP_KERNEL);
274         if (!pfvf->flow_cfg->dmacflt_bmap)
275                 return -ENOMEM;
276
277         flow_cfg = pfvf->flow_cfg;
278         INIT_LIST_HEAD(&flow_cfg->flow_list);
279         flow_cfg->max_flows = 0;
280
281         return 0;
282 }
283 EXPORT_SYMBOL(otx2vf_mcam_flow_init);
284
285 int otx2_mcam_flow_init(struct otx2_nic *pf)
286 {
287         int err;
288
289         pf->flow_cfg = devm_kzalloc(pf->dev, sizeof(struct otx2_flow_config),
290                                     GFP_KERNEL);
291         if (!pf->flow_cfg)
292                 return -ENOMEM;
293
294         pf->flow_cfg->dmacflt_bmap = devm_kcalloc(pf->dev,
295                                                   BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
296                                                   sizeof(long), GFP_KERNEL);
297         if (!pf->flow_cfg->dmacflt_bmap)
298                 return -ENOMEM;
299
300         INIT_LIST_HEAD(&pf->flow_cfg->flow_list);
301
302         /* Allocate bare minimum number of MCAM entries needed for
303          * unicast and ntuple filters.
304          */
305         err = otx2_mcam_entry_init(pf);
306         if (err)
307                 return err;
308
309         /* Check if MCAM entries are allocate or not */
310         if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
311                 return 0;
312
313         pf->mac_table = devm_kzalloc(pf->dev, sizeof(struct otx2_mac_table)
314                                         * OTX2_MAX_UNICAST_FLOWS, GFP_KERNEL);
315         if (!pf->mac_table)
316                 return -ENOMEM;
317
318         otx2_dmacflt_get_max_cnt(pf);
319
320         /* DMAC filters are not allocated */
321         if (!pf->flow_cfg->dmacflt_max_flows)
322                 return 0;
323
324         pf->flow_cfg->bmap_to_dmacindex =
325                         devm_kzalloc(pf->dev, sizeof(u32) *
326                                      pf->flow_cfg->dmacflt_max_flows,
327                                      GFP_KERNEL);
328
329         if (!pf->flow_cfg->bmap_to_dmacindex)
330                 return -ENOMEM;
331
332         pf->flags |= OTX2_FLAG_DMACFLTR_SUPPORT;
333
334         return 0;
335 }
336
337 void otx2_mcam_flow_del(struct otx2_nic *pf)
338 {
339         otx2_destroy_mcam_flows(pf);
340 }
341 EXPORT_SYMBOL(otx2_mcam_flow_del);
342
343 /*  On success adds mcam entry
344  *  On failure enable promisous mode
345  */
346 static int otx2_do_add_macfilter(struct otx2_nic *pf, const u8 *mac)
347 {
348         struct otx2_flow_config *flow_cfg = pf->flow_cfg;
349         struct npc_install_flow_req *req;
350         int err, i;
351
352         if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
353                 return -ENOMEM;
354
355         /* dont have free mcam entries or uc list is greater than alloted */
356         if (netdev_uc_count(pf->netdev) > OTX2_MAX_UNICAST_FLOWS)
357                 return -ENOMEM;
358
359         mutex_lock(&pf->mbox.lock);
360         req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
361         if (!req) {
362                 mutex_unlock(&pf->mbox.lock);
363                 return -ENOMEM;
364         }
365
366         /* unicast offset starts with 32 0..31 for ntuple */
367         for (i = 0; i <  OTX2_MAX_UNICAST_FLOWS; i++) {
368                 if (pf->mac_table[i].inuse)
369                         continue;
370                 ether_addr_copy(pf->mac_table[i].addr, mac);
371                 pf->mac_table[i].inuse = true;
372                 pf->mac_table[i].mcam_entry =
373                         flow_cfg->def_ent[i + flow_cfg->unicast_offset];
374                 req->entry =  pf->mac_table[i].mcam_entry;
375                 break;
376         }
377
378         ether_addr_copy(req->packet.dmac, mac);
379         eth_broadcast_addr((u8 *)&req->mask.dmac);
380         req->features = BIT_ULL(NPC_DMAC);
381         req->channel = pf->hw.rx_chan_base;
382         req->intf = NIX_INTF_RX;
383         req->op = NIX_RX_ACTION_DEFAULT;
384         req->set_cntr = 1;
385
386         err = otx2_sync_mbox_msg(&pf->mbox);
387         mutex_unlock(&pf->mbox.lock);
388
389         return err;
390 }
391
392 int otx2_add_macfilter(struct net_device *netdev, const u8 *mac)
393 {
394         struct otx2_nic *pf = netdev_priv(netdev);
395
396         if (!bitmap_empty(pf->flow_cfg->dmacflt_bmap,
397                           pf->flow_cfg->dmacflt_max_flows))
398                 netdev_warn(netdev,
399                             "Add %pM to CGX/RPM DMAC filters list as well\n",
400                             mac);
401
402         return otx2_do_add_macfilter(pf, mac);
403 }
404
405 static bool otx2_get_mcamentry_for_mac(struct otx2_nic *pf, const u8 *mac,
406                                        int *mcam_entry)
407 {
408         int i;
409
410         for (i = 0; i < OTX2_MAX_UNICAST_FLOWS; i++) {
411                 if (!pf->mac_table[i].inuse)
412                         continue;
413
414                 if (ether_addr_equal(pf->mac_table[i].addr, mac)) {
415                         *mcam_entry = pf->mac_table[i].mcam_entry;
416                         pf->mac_table[i].inuse = false;
417                         return true;
418                 }
419         }
420         return false;
421 }
422
423 int otx2_del_macfilter(struct net_device *netdev, const u8 *mac)
424 {
425         struct otx2_nic *pf = netdev_priv(netdev);
426         struct npc_delete_flow_req *req;
427         int err, mcam_entry;
428
429         /* check does mcam entry exists for given mac */
430         if (!otx2_get_mcamentry_for_mac(pf, mac, &mcam_entry))
431                 return 0;
432
433         mutex_lock(&pf->mbox.lock);
434         req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
435         if (!req) {
436                 mutex_unlock(&pf->mbox.lock);
437                 return -ENOMEM;
438         }
439         req->entry = mcam_entry;
440         /* Send message to AF */
441         err = otx2_sync_mbox_msg(&pf->mbox);
442         mutex_unlock(&pf->mbox.lock);
443
444         return err;
445 }
446
447 static struct otx2_flow *otx2_find_flow(struct otx2_nic *pfvf, u32 location)
448 {
449         struct otx2_flow *iter;
450
451         list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
452                 if (iter->location == location)
453                         return iter;
454         }
455
456         return NULL;
457 }
458
459 static void otx2_add_flow_to_list(struct otx2_nic *pfvf, struct otx2_flow *flow)
460 {
461         struct list_head *head = &pfvf->flow_cfg->flow_list;
462         struct otx2_flow *iter;
463
464         list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
465                 if (iter->location > flow->location)
466                         break;
467                 head = &iter->list;
468         }
469
470         list_add(&flow->list, head);
471 }
472
473 int otx2_get_maxflows(struct otx2_flow_config *flow_cfg)
474 {
475         if (!flow_cfg)
476                 return 0;
477
478         if (flow_cfg->nr_flows == flow_cfg->max_flows ||
479             !bitmap_empty(flow_cfg->dmacflt_bmap,
480                           flow_cfg->dmacflt_max_flows))
481                 return flow_cfg->max_flows + flow_cfg->dmacflt_max_flows;
482         else
483                 return flow_cfg->max_flows;
484 }
485 EXPORT_SYMBOL(otx2_get_maxflows);
486
487 int otx2_get_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
488                   u32 location)
489 {
490         struct otx2_flow *iter;
491
492         if (location >= otx2_get_maxflows(pfvf->flow_cfg))
493                 return -EINVAL;
494
495         list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
496                 if (iter->location == location) {
497                         nfc->fs = iter->flow_spec;
498                         nfc->rss_context = iter->rss_ctx_id;
499                         return 0;
500                 }
501         }
502
503         return -ENOENT;
504 }
505
506 int otx2_get_all_flows(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
507                        u32 *rule_locs)
508 {
509         u32 rule_cnt = nfc->rule_cnt;
510         u32 location = 0;
511         int idx = 0;
512         int err = 0;
513
514         nfc->data = otx2_get_maxflows(pfvf->flow_cfg);
515         while ((!err || err == -ENOENT) && idx < rule_cnt) {
516                 err = otx2_get_flow(pfvf, nfc, location);
517                 if (!err)
518                         rule_locs[idx++] = location;
519                 location++;
520         }
521         nfc->rule_cnt = rule_cnt;
522
523         return err;
524 }
525
526 static int otx2_prepare_ipv4_flow(struct ethtool_rx_flow_spec *fsp,
527                                   struct npc_install_flow_req *req,
528                                   u32 flow_type)
529 {
530         struct ethtool_usrip4_spec *ipv4_usr_mask = &fsp->m_u.usr_ip4_spec;
531         struct ethtool_usrip4_spec *ipv4_usr_hdr = &fsp->h_u.usr_ip4_spec;
532         struct ethtool_tcpip4_spec *ipv4_l4_mask = &fsp->m_u.tcp_ip4_spec;
533         struct ethtool_tcpip4_spec *ipv4_l4_hdr = &fsp->h_u.tcp_ip4_spec;
534         struct ethtool_ah_espip4_spec *ah_esp_hdr = &fsp->h_u.ah_ip4_spec;
535         struct ethtool_ah_espip4_spec *ah_esp_mask = &fsp->m_u.ah_ip4_spec;
536         struct flow_msg *pmask = &req->mask;
537         struct flow_msg *pkt = &req->packet;
538
539         switch (flow_type) {
540         case IP_USER_FLOW:
541                 if (ipv4_usr_mask->ip4src) {
542                         memcpy(&pkt->ip4src, &ipv4_usr_hdr->ip4src,
543                                sizeof(pkt->ip4src));
544                         memcpy(&pmask->ip4src, &ipv4_usr_mask->ip4src,
545                                sizeof(pmask->ip4src));
546                         req->features |= BIT_ULL(NPC_SIP_IPV4);
547                 }
548                 if (ipv4_usr_mask->ip4dst) {
549                         memcpy(&pkt->ip4dst, &ipv4_usr_hdr->ip4dst,
550                                sizeof(pkt->ip4dst));
551                         memcpy(&pmask->ip4dst, &ipv4_usr_mask->ip4dst,
552                                sizeof(pmask->ip4dst));
553                         req->features |= BIT_ULL(NPC_DIP_IPV4);
554                 }
555                 if (ipv4_usr_mask->tos) {
556                         pkt->tos = ipv4_usr_hdr->tos;
557                         pmask->tos = ipv4_usr_mask->tos;
558                         req->features |= BIT_ULL(NPC_TOS);
559                 }
560                 if (ipv4_usr_mask->proto) {
561                         switch (ipv4_usr_hdr->proto) {
562                         case IPPROTO_ICMP:
563                                 req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
564                                 break;
565                         case IPPROTO_TCP:
566                                 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
567                                 break;
568                         case IPPROTO_UDP:
569                                 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
570                                 break;
571                         case IPPROTO_SCTP:
572                                 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
573                                 break;
574                         case IPPROTO_AH:
575                                 req->features |= BIT_ULL(NPC_IPPROTO_AH);
576                                 break;
577                         case IPPROTO_ESP:
578                                 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
579                                 break;
580                         default:
581                                 return -EOPNOTSUPP;
582                         }
583                 }
584                 pkt->etype = cpu_to_be16(ETH_P_IP);
585                 pmask->etype = cpu_to_be16(0xFFFF);
586                 req->features |= BIT_ULL(NPC_ETYPE);
587                 break;
588         case TCP_V4_FLOW:
589         case UDP_V4_FLOW:
590         case SCTP_V4_FLOW:
591                 pkt->etype = cpu_to_be16(ETH_P_IP);
592                 pmask->etype = cpu_to_be16(0xFFFF);
593                 req->features |= BIT_ULL(NPC_ETYPE);
594                 if (ipv4_l4_mask->ip4src) {
595                         memcpy(&pkt->ip4src, &ipv4_l4_hdr->ip4src,
596                                sizeof(pkt->ip4src));
597                         memcpy(&pmask->ip4src, &ipv4_l4_mask->ip4src,
598                                sizeof(pmask->ip4src));
599                         req->features |= BIT_ULL(NPC_SIP_IPV4);
600                 }
601                 if (ipv4_l4_mask->ip4dst) {
602                         memcpy(&pkt->ip4dst, &ipv4_l4_hdr->ip4dst,
603                                sizeof(pkt->ip4dst));
604                         memcpy(&pmask->ip4dst, &ipv4_l4_mask->ip4dst,
605                                sizeof(pmask->ip4dst));
606                         req->features |= BIT_ULL(NPC_DIP_IPV4);
607                 }
608                 if (ipv4_l4_mask->tos) {
609                         pkt->tos = ipv4_l4_hdr->tos;
610                         pmask->tos = ipv4_l4_mask->tos;
611                         req->features |= BIT_ULL(NPC_TOS);
612                 }
613                 if (ipv4_l4_mask->psrc) {
614                         memcpy(&pkt->sport, &ipv4_l4_hdr->psrc,
615                                sizeof(pkt->sport));
616                         memcpy(&pmask->sport, &ipv4_l4_mask->psrc,
617                                sizeof(pmask->sport));
618                         if (flow_type == UDP_V4_FLOW)
619                                 req->features |= BIT_ULL(NPC_SPORT_UDP);
620                         else if (flow_type == TCP_V4_FLOW)
621                                 req->features |= BIT_ULL(NPC_SPORT_TCP);
622                         else
623                                 req->features |= BIT_ULL(NPC_SPORT_SCTP);
624                 }
625                 if (ipv4_l4_mask->pdst) {
626                         memcpy(&pkt->dport, &ipv4_l4_hdr->pdst,
627                                sizeof(pkt->dport));
628                         memcpy(&pmask->dport, &ipv4_l4_mask->pdst,
629                                sizeof(pmask->dport));
630                         if (flow_type == UDP_V4_FLOW)
631                                 req->features |= BIT_ULL(NPC_DPORT_UDP);
632                         else if (flow_type == TCP_V4_FLOW)
633                                 req->features |= BIT_ULL(NPC_DPORT_TCP);
634                         else
635                                 req->features |= BIT_ULL(NPC_DPORT_SCTP);
636                 }
637                 if (flow_type == UDP_V4_FLOW)
638                         req->features |= BIT_ULL(NPC_IPPROTO_UDP);
639                 else if (flow_type == TCP_V4_FLOW)
640                         req->features |= BIT_ULL(NPC_IPPROTO_TCP);
641                 else
642                         req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
643                 break;
644         case AH_V4_FLOW:
645         case ESP_V4_FLOW:
646                 pkt->etype = cpu_to_be16(ETH_P_IP);
647                 pmask->etype = cpu_to_be16(0xFFFF);
648                 req->features |= BIT_ULL(NPC_ETYPE);
649                 if (ah_esp_mask->ip4src) {
650                         memcpy(&pkt->ip4src, &ah_esp_hdr->ip4src,
651                                sizeof(pkt->ip4src));
652                         memcpy(&pmask->ip4src, &ah_esp_mask->ip4src,
653                                sizeof(pmask->ip4src));
654                         req->features |= BIT_ULL(NPC_SIP_IPV4);
655                 }
656                 if (ah_esp_mask->ip4dst) {
657                         memcpy(&pkt->ip4dst, &ah_esp_hdr->ip4dst,
658                                sizeof(pkt->ip4dst));
659                         memcpy(&pmask->ip4dst, &ah_esp_mask->ip4dst,
660                                sizeof(pmask->ip4dst));
661                         req->features |= BIT_ULL(NPC_DIP_IPV4);
662                 }
663                 if (ah_esp_mask->tos) {
664                         pkt->tos = ah_esp_hdr->tos;
665                         pmask->tos = ah_esp_mask->tos;
666                         req->features |= BIT_ULL(NPC_TOS);
667                 }
668
669                 /* NPC profile doesn't extract AH/ESP header fields */
670                 if (ah_esp_mask->spi & ah_esp_hdr->spi)
671                         return -EOPNOTSUPP;
672
673                 if (flow_type == AH_V4_FLOW)
674                         req->features |= BIT_ULL(NPC_IPPROTO_AH);
675                 else
676                         req->features |= BIT_ULL(NPC_IPPROTO_ESP);
677                 break;
678         default:
679                 break;
680         }
681
682         return 0;
683 }
684
685 static int otx2_prepare_ipv6_flow(struct ethtool_rx_flow_spec *fsp,
686                                   struct npc_install_flow_req *req,
687                                   u32 flow_type)
688 {
689         struct ethtool_usrip6_spec *ipv6_usr_mask = &fsp->m_u.usr_ip6_spec;
690         struct ethtool_usrip6_spec *ipv6_usr_hdr = &fsp->h_u.usr_ip6_spec;
691         struct ethtool_tcpip6_spec *ipv6_l4_mask = &fsp->m_u.tcp_ip6_spec;
692         struct ethtool_tcpip6_spec *ipv6_l4_hdr = &fsp->h_u.tcp_ip6_spec;
693         struct ethtool_ah_espip6_spec *ah_esp_hdr = &fsp->h_u.ah_ip6_spec;
694         struct ethtool_ah_espip6_spec *ah_esp_mask = &fsp->m_u.ah_ip6_spec;
695         struct flow_msg *pmask = &req->mask;
696         struct flow_msg *pkt = &req->packet;
697
698         switch (flow_type) {
699         case IPV6_USER_FLOW:
700                 if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6src)) {
701                         memcpy(&pkt->ip6src, &ipv6_usr_hdr->ip6src,
702                                sizeof(pkt->ip6src));
703                         memcpy(&pmask->ip6src, &ipv6_usr_mask->ip6src,
704                                sizeof(pmask->ip6src));
705                         req->features |= BIT_ULL(NPC_SIP_IPV6);
706                 }
707                 if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6dst)) {
708                         memcpy(&pkt->ip6dst, &ipv6_usr_hdr->ip6dst,
709                                sizeof(pkt->ip6dst));
710                         memcpy(&pmask->ip6dst, &ipv6_usr_mask->ip6dst,
711                                sizeof(pmask->ip6dst));
712                         req->features |= BIT_ULL(NPC_DIP_IPV6);
713                 }
714                 if (ipv6_usr_hdr->l4_proto == IPPROTO_FRAGMENT) {
715                         pkt->next_header = ipv6_usr_hdr->l4_proto;
716                         pmask->next_header = ipv6_usr_mask->l4_proto;
717                         req->features |= BIT_ULL(NPC_IPFRAG_IPV6);
718                 }
719                 pkt->etype = cpu_to_be16(ETH_P_IPV6);
720                 pmask->etype = cpu_to_be16(0xFFFF);
721                 req->features |= BIT_ULL(NPC_ETYPE);
722                 break;
723         case TCP_V6_FLOW:
724         case UDP_V6_FLOW:
725         case SCTP_V6_FLOW:
726                 pkt->etype = cpu_to_be16(ETH_P_IPV6);
727                 pmask->etype = cpu_to_be16(0xFFFF);
728                 req->features |= BIT_ULL(NPC_ETYPE);
729                 if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6src)) {
730                         memcpy(&pkt->ip6src, &ipv6_l4_hdr->ip6src,
731                                sizeof(pkt->ip6src));
732                         memcpy(&pmask->ip6src, &ipv6_l4_mask->ip6src,
733                                sizeof(pmask->ip6src));
734                         req->features |= BIT_ULL(NPC_SIP_IPV6);
735                 }
736                 if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6dst)) {
737                         memcpy(&pkt->ip6dst, &ipv6_l4_hdr->ip6dst,
738                                sizeof(pkt->ip6dst));
739                         memcpy(&pmask->ip6dst, &ipv6_l4_mask->ip6dst,
740                                sizeof(pmask->ip6dst));
741                         req->features |= BIT_ULL(NPC_DIP_IPV6);
742                 }
743                 if (ipv6_l4_mask->psrc) {
744                         memcpy(&pkt->sport, &ipv6_l4_hdr->psrc,
745                                sizeof(pkt->sport));
746                         memcpy(&pmask->sport, &ipv6_l4_mask->psrc,
747                                sizeof(pmask->sport));
748                         if (flow_type == UDP_V6_FLOW)
749                                 req->features |= BIT_ULL(NPC_SPORT_UDP);
750                         else if (flow_type == TCP_V6_FLOW)
751                                 req->features |= BIT_ULL(NPC_SPORT_TCP);
752                         else
753                                 req->features |= BIT_ULL(NPC_SPORT_SCTP);
754                 }
755                 if (ipv6_l4_mask->pdst) {
756                         memcpy(&pkt->dport, &ipv6_l4_hdr->pdst,
757                                sizeof(pkt->dport));
758                         memcpy(&pmask->dport, &ipv6_l4_mask->pdst,
759                                sizeof(pmask->dport));
760                         if (flow_type == UDP_V6_FLOW)
761                                 req->features |= BIT_ULL(NPC_DPORT_UDP);
762                         else if (flow_type == TCP_V6_FLOW)
763                                 req->features |= BIT_ULL(NPC_DPORT_TCP);
764                         else
765                                 req->features |= BIT_ULL(NPC_DPORT_SCTP);
766                 }
767                 if (flow_type == UDP_V6_FLOW)
768                         req->features |= BIT_ULL(NPC_IPPROTO_UDP);
769                 else if (flow_type == TCP_V6_FLOW)
770                         req->features |= BIT_ULL(NPC_IPPROTO_TCP);
771                 else
772                         req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
773                 break;
774         case AH_V6_FLOW:
775         case ESP_V6_FLOW:
776                 pkt->etype = cpu_to_be16(ETH_P_IPV6);
777                 pmask->etype = cpu_to_be16(0xFFFF);
778                 req->features |= BIT_ULL(NPC_ETYPE);
779                 if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6src)) {
780                         memcpy(&pkt->ip6src, &ah_esp_hdr->ip6src,
781                                sizeof(pkt->ip6src));
782                         memcpy(&pmask->ip6src, &ah_esp_mask->ip6src,
783                                sizeof(pmask->ip6src));
784                         req->features |= BIT_ULL(NPC_SIP_IPV6);
785                 }
786                 if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6dst)) {
787                         memcpy(&pkt->ip6dst, &ah_esp_hdr->ip6dst,
788                                sizeof(pkt->ip6dst));
789                         memcpy(&pmask->ip6dst, &ah_esp_mask->ip6dst,
790                                sizeof(pmask->ip6dst));
791                         req->features |= BIT_ULL(NPC_DIP_IPV6);
792                 }
793
794                 /* NPC profile doesn't extract AH/ESP header fields */
795                 if ((ah_esp_mask->spi & ah_esp_hdr->spi) ||
796                     (ah_esp_mask->tclass & ah_esp_hdr->tclass))
797                         return -EOPNOTSUPP;
798
799                 if (flow_type == AH_V6_FLOW)
800                         req->features |= BIT_ULL(NPC_IPPROTO_AH);
801                 else
802                         req->features |= BIT_ULL(NPC_IPPROTO_ESP);
803                 break;
804         default:
805                 break;
806         }
807
808         return 0;
809 }
810
811 static int otx2_prepare_flow_request(struct ethtool_rx_flow_spec *fsp,
812                               struct npc_install_flow_req *req)
813 {
814         struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
815         struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
816         struct flow_msg *pmask = &req->mask;
817         struct flow_msg *pkt = &req->packet;
818         u32 flow_type;
819         int ret;
820
821         flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
822         switch (flow_type) {
823         /* bits not set in mask are don't care */
824         case ETHER_FLOW:
825                 if (!is_zero_ether_addr(eth_mask->h_source)) {
826                         ether_addr_copy(pkt->smac, eth_hdr->h_source);
827                         ether_addr_copy(pmask->smac, eth_mask->h_source);
828                         req->features |= BIT_ULL(NPC_SMAC);
829                 }
830                 if (!is_zero_ether_addr(eth_mask->h_dest)) {
831                         ether_addr_copy(pkt->dmac, eth_hdr->h_dest);
832                         ether_addr_copy(pmask->dmac, eth_mask->h_dest);
833                         req->features |= BIT_ULL(NPC_DMAC);
834                 }
835                 if (eth_hdr->h_proto) {
836                         memcpy(&pkt->etype, &eth_hdr->h_proto,
837                                sizeof(pkt->etype));
838                         memcpy(&pmask->etype, &eth_mask->h_proto,
839                                sizeof(pmask->etype));
840                         req->features |= BIT_ULL(NPC_ETYPE);
841                 }
842                 break;
843         case IP_USER_FLOW:
844         case TCP_V4_FLOW:
845         case UDP_V4_FLOW:
846         case SCTP_V4_FLOW:
847         case AH_V4_FLOW:
848         case ESP_V4_FLOW:
849                 ret = otx2_prepare_ipv4_flow(fsp, req, flow_type);
850                 if (ret)
851                         return ret;
852                 break;
853         case IPV6_USER_FLOW:
854         case TCP_V6_FLOW:
855         case UDP_V6_FLOW:
856         case SCTP_V6_FLOW:
857         case AH_V6_FLOW:
858         case ESP_V6_FLOW:
859                 ret = otx2_prepare_ipv6_flow(fsp, req, flow_type);
860                 if (ret)
861                         return ret;
862                 break;
863         default:
864                 return -EOPNOTSUPP;
865         }
866         if (fsp->flow_type & FLOW_EXT) {
867                 u16 vlan_etype;
868
869                 if (fsp->m_ext.vlan_etype) {
870                         /* Partial masks not supported */
871                         if (be16_to_cpu(fsp->m_ext.vlan_etype) != 0xFFFF)
872                                 return -EINVAL;
873
874                         vlan_etype = be16_to_cpu(fsp->h_ext.vlan_etype);
875                         /* Only ETH_P_8021Q and ETH_P_802AD types supported */
876                         if (vlan_etype != ETH_P_8021Q &&
877                             vlan_etype != ETH_P_8021AD)
878                                 return -EINVAL;
879
880                         memcpy(&pkt->vlan_etype, &fsp->h_ext.vlan_etype,
881                                sizeof(pkt->vlan_etype));
882                         memcpy(&pmask->vlan_etype, &fsp->m_ext.vlan_etype,
883                                sizeof(pmask->vlan_etype));
884
885                         if (vlan_etype == ETH_P_8021Q)
886                                 req->features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG);
887                         else
888                                 req->features |= BIT_ULL(NPC_VLAN_ETYPE_STAG);
889                 }
890
891                 if (fsp->m_ext.vlan_tci) {
892                         memcpy(&pkt->vlan_tci, &fsp->h_ext.vlan_tci,
893                                sizeof(pkt->vlan_tci));
894                         memcpy(&pmask->vlan_tci, &fsp->m_ext.vlan_tci,
895                                sizeof(pmask->vlan_tci));
896                         req->features |= BIT_ULL(NPC_OUTER_VID);
897                 }
898
899                 if (fsp->m_ext.data[1]) {
900                         if (flow_type == IP_USER_FLOW) {
901                                 if (be32_to_cpu(fsp->h_ext.data[1]) != IPV4_FLAG_MORE)
902                                         return -EINVAL;
903
904                                 pkt->ip_flag = be32_to_cpu(fsp->h_ext.data[1]);
905                                 pmask->ip_flag = be32_to_cpu(fsp->m_ext.data[1]);
906                                 req->features |= BIT_ULL(NPC_IPFRAG_IPV4);
907                         } else if (fsp->h_ext.data[1] ==
908                                         cpu_to_be32(OTX2_DEFAULT_ACTION)) {
909                                 /* Not Drop/Direct to queue but use action
910                                  * in default entry
911                                  */
912                                 req->op = NIX_RX_ACTION_DEFAULT;
913                         }
914                 }
915         }
916
917         if (fsp->flow_type & FLOW_MAC_EXT &&
918             !is_zero_ether_addr(fsp->m_ext.h_dest)) {
919                 ether_addr_copy(pkt->dmac, fsp->h_ext.h_dest);
920                 ether_addr_copy(pmask->dmac, fsp->m_ext.h_dest);
921                 req->features |= BIT_ULL(NPC_DMAC);
922         }
923
924         if (!req->features)
925                 return -EOPNOTSUPP;
926
927         return 0;
928 }
929
930 static int otx2_is_flow_rule_dmacfilter(struct otx2_nic *pfvf,
931                                         struct ethtool_rx_flow_spec *fsp)
932 {
933         struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
934         struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
935         u64 ring_cookie = fsp->ring_cookie;
936         u32 flow_type;
937
938         if (!(pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT))
939                 return false;
940
941         flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
942
943         /* CGX/RPM block dmac filtering configured for white listing
944          * check for action other than DROP
945          */
946         if (flow_type == ETHER_FLOW && ring_cookie != RX_CLS_FLOW_DISC &&
947             !ethtool_get_flow_spec_ring_vf(ring_cookie)) {
948                 if (is_zero_ether_addr(eth_mask->h_dest) &&
949                     is_valid_ether_addr(eth_hdr->h_dest))
950                         return true;
951         }
952
953         return false;
954 }
955
956 static int otx2_add_flow_msg(struct otx2_nic *pfvf, struct otx2_flow *flow)
957 {
958         u64 ring_cookie = flow->flow_spec.ring_cookie;
959 #ifdef CONFIG_DCB
960         int vlan_prio, qidx, pfc_rule = 0;
961 #endif
962         struct npc_install_flow_req *req;
963         int err, vf = 0;
964
965         mutex_lock(&pfvf->mbox.lock);
966         req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
967         if (!req) {
968                 mutex_unlock(&pfvf->mbox.lock);
969                 return -ENOMEM;
970         }
971
972         err = otx2_prepare_flow_request(&flow->flow_spec, req);
973         if (err) {
974                 /* free the allocated msg above */
975                 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
976                 mutex_unlock(&pfvf->mbox.lock);
977                 return err;
978         }
979
980         req->entry = flow->entry;
981         req->intf = NIX_INTF_RX;
982         req->set_cntr = 1;
983         req->channel = pfvf->hw.rx_chan_base;
984         if (ring_cookie == RX_CLS_FLOW_DISC) {
985                 req->op = NIX_RX_ACTIONOP_DROP;
986         } else {
987                 /* change to unicast only if action of default entry is not
988                  * requested by user
989                  */
990                 if (flow->flow_spec.flow_type & FLOW_RSS) {
991                         req->op = NIX_RX_ACTIONOP_RSS;
992                         req->index = flow->rss_ctx_id;
993                         req->flow_key_alg = pfvf->hw.flowkey_alg_idx;
994                 } else {
995                         req->op = NIX_RX_ACTIONOP_UCAST;
996                         req->index = ethtool_get_flow_spec_ring(ring_cookie);
997                 }
998                 vf = ethtool_get_flow_spec_ring_vf(ring_cookie);
999                 if (vf > pci_num_vf(pfvf->pdev)) {
1000                         mutex_unlock(&pfvf->mbox.lock);
1001                         return -EINVAL;
1002                 }
1003
1004 #ifdef CONFIG_DCB
1005                 /* Identify PFC rule if PFC enabled and ntuple rule is vlan */
1006                 if (!vf && (req->features & BIT_ULL(NPC_OUTER_VID)) &&
1007                     pfvf->pfc_en && req->op != NIX_RX_ACTIONOP_RSS) {
1008                         vlan_prio = ntohs(req->packet.vlan_tci) &
1009                                     ntohs(req->mask.vlan_tci);
1010
1011                         /* Get the priority */
1012                         vlan_prio >>= 13;
1013                         flow->rule_type |= PFC_FLOWCTRL_RULE;
1014                         /* Check if PFC enabled for this priority */
1015                         if (pfvf->pfc_en & BIT(vlan_prio)) {
1016                                 pfc_rule = true;
1017                                 qidx = req->index;
1018                         }
1019                 }
1020 #endif
1021         }
1022
1023         /* ethtool ring_cookie has (VF + 1) for VF */
1024         if (vf) {
1025                 req->vf = vf;
1026                 flow->is_vf = true;
1027                 flow->vf = vf;
1028         }
1029
1030         /* Send message to AF */
1031         err = otx2_sync_mbox_msg(&pfvf->mbox);
1032
1033 #ifdef CONFIG_DCB
1034         if (!err && pfc_rule)
1035                 otx2_update_bpid_in_rqctx(pfvf, vlan_prio, qidx, true);
1036 #endif
1037
1038         mutex_unlock(&pfvf->mbox.lock);
1039         return err;
1040 }
1041
1042 static int otx2_add_flow_with_pfmac(struct otx2_nic *pfvf,
1043                                     struct otx2_flow *flow)
1044 {
1045         struct otx2_flow *pf_mac;
1046         struct ethhdr *eth_hdr;
1047
1048         pf_mac = kzalloc(sizeof(*pf_mac), GFP_KERNEL);
1049         if (!pf_mac)
1050                 return -ENOMEM;
1051
1052         pf_mac->entry = 0;
1053         pf_mac->rule_type |= DMAC_FILTER_RULE;
1054         pf_mac->location = pfvf->flow_cfg->max_flows;
1055         memcpy(&pf_mac->flow_spec, &flow->flow_spec,
1056                sizeof(struct ethtool_rx_flow_spec));
1057         pf_mac->flow_spec.location = pf_mac->location;
1058
1059         /* Copy PF mac address */
1060         eth_hdr = &pf_mac->flow_spec.h_u.ether_spec;
1061         ether_addr_copy(eth_hdr->h_dest, pfvf->netdev->dev_addr);
1062
1063         /* Install DMAC filter with PF mac address */
1064         otx2_dmacflt_add(pfvf, eth_hdr->h_dest, 0);
1065
1066         otx2_add_flow_to_list(pfvf, pf_mac);
1067         pfvf->flow_cfg->nr_flows++;
1068         set_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1069
1070         return 0;
1071 }
1072
1073 int otx2_add_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc)
1074 {
1075         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1076         struct ethtool_rx_flow_spec *fsp = &nfc->fs;
1077         struct otx2_flow *flow;
1078         struct ethhdr *eth_hdr;
1079         bool new = false;
1080         int err = 0;
1081         u32 ring;
1082
1083         if (!flow_cfg->max_flows) {
1084                 netdev_err(pfvf->netdev,
1085                            "Ntuple rule count is 0, allocate and retry\n");
1086                 return -EINVAL;
1087         }
1088
1089         ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
1090         if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1091                 return -ENOMEM;
1092
1093         if (ring >= pfvf->hw.rx_queues && fsp->ring_cookie != RX_CLS_FLOW_DISC)
1094                 return -EINVAL;
1095
1096         if (fsp->location >= otx2_get_maxflows(flow_cfg))
1097                 return -EINVAL;
1098
1099         flow = otx2_find_flow(pfvf, fsp->location);
1100         if (!flow) {
1101                 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
1102                 if (!flow)
1103                         return -ENOMEM;
1104                 flow->location = fsp->location;
1105                 flow->entry = flow_cfg->flow_ent[flow->location];
1106                 new = true;
1107         }
1108         /* struct copy */
1109         flow->flow_spec = *fsp;
1110
1111         if (fsp->flow_type & FLOW_RSS)
1112                 flow->rss_ctx_id = nfc->rss_context;
1113
1114         if (otx2_is_flow_rule_dmacfilter(pfvf, &flow->flow_spec)) {
1115                 eth_hdr = &flow->flow_spec.h_u.ether_spec;
1116
1117                 /* Sync dmac filter table with updated fields */
1118                 if (flow->rule_type & DMAC_FILTER_RULE)
1119                         return otx2_dmacflt_update(pfvf, eth_hdr->h_dest,
1120                                                    flow->entry);
1121
1122                 if (bitmap_full(flow_cfg->dmacflt_bmap,
1123                                 flow_cfg->dmacflt_max_flows)) {
1124                         netdev_warn(pfvf->netdev,
1125                                     "Can't insert the rule %d as max allowed dmac filters are %d\n",
1126                                     flow->location +
1127                                     flow_cfg->dmacflt_max_flows,
1128                                     flow_cfg->dmacflt_max_flows);
1129                         err = -EINVAL;
1130                         if (new)
1131                                 kfree(flow);
1132                         return err;
1133                 }
1134
1135                 /* Install PF mac address to DMAC filter list */
1136                 if (!test_bit(0, flow_cfg->dmacflt_bmap))
1137                         otx2_add_flow_with_pfmac(pfvf, flow);
1138
1139                 flow->rule_type |= DMAC_FILTER_RULE;
1140                 flow->entry = find_first_zero_bit(flow_cfg->dmacflt_bmap,
1141                                                   flow_cfg->dmacflt_max_flows);
1142                 fsp->location = flow_cfg->max_flows + flow->entry;
1143                 flow->flow_spec.location = fsp->location;
1144                 flow->location = fsp->location;
1145
1146                 set_bit(flow->entry, flow_cfg->dmacflt_bmap);
1147                 otx2_dmacflt_add(pfvf, eth_hdr->h_dest, flow->entry);
1148
1149         } else {
1150                 if (flow->location >= pfvf->flow_cfg->max_flows) {
1151                         netdev_warn(pfvf->netdev,
1152                                     "Can't insert non dmac ntuple rule at %d, allowed range %d-0\n",
1153                                     flow->location,
1154                                     flow_cfg->max_flows - 1);
1155                         err = -EINVAL;
1156                 } else {
1157                         err = otx2_add_flow_msg(pfvf, flow);
1158                 }
1159         }
1160
1161         if (err) {
1162                 if (err == MBOX_MSG_INVALID)
1163                         err = -EINVAL;
1164                 if (new)
1165                         kfree(flow);
1166                 return err;
1167         }
1168
1169         /* add the new flow installed to list */
1170         if (new) {
1171                 otx2_add_flow_to_list(pfvf, flow);
1172                 flow_cfg->nr_flows++;
1173         }
1174
1175         return 0;
1176 }
1177
1178 static int otx2_remove_flow_msg(struct otx2_nic *pfvf, u16 entry, bool all)
1179 {
1180         struct npc_delete_flow_req *req;
1181         int err;
1182
1183         mutex_lock(&pfvf->mbox.lock);
1184         req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1185         if (!req) {
1186                 mutex_unlock(&pfvf->mbox.lock);
1187                 return -ENOMEM;
1188         }
1189
1190         req->entry = entry;
1191         if (all)
1192                 req->all = 1;
1193
1194         /* Send message to AF */
1195         err = otx2_sync_mbox_msg(&pfvf->mbox);
1196         mutex_unlock(&pfvf->mbox.lock);
1197         return err;
1198 }
1199
1200 static void otx2_update_rem_pfmac(struct otx2_nic *pfvf, int req)
1201 {
1202         struct otx2_flow *iter;
1203         struct ethhdr *eth_hdr;
1204         bool found = false;
1205
1206         list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
1207                 if ((iter->rule_type & DMAC_FILTER_RULE) && iter->entry == 0) {
1208                         eth_hdr = &iter->flow_spec.h_u.ether_spec;
1209                         if (req == DMAC_ADDR_DEL) {
1210                                 otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1211                                                     0);
1212                                 clear_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1213                                 found = true;
1214                         } else {
1215                                 ether_addr_copy(eth_hdr->h_dest,
1216                                                 pfvf->netdev->dev_addr);
1217
1218                                 otx2_dmacflt_update(pfvf, eth_hdr->h_dest, 0);
1219                         }
1220                         break;
1221                 }
1222         }
1223
1224         if (found) {
1225                 list_del(&iter->list);
1226                 kfree(iter);
1227                 pfvf->flow_cfg->nr_flows--;
1228         }
1229 }
1230
1231 int otx2_remove_flow(struct otx2_nic *pfvf, u32 location)
1232 {
1233         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1234         struct otx2_flow *flow;
1235         int err;
1236
1237         if (location >= otx2_get_maxflows(flow_cfg))
1238                 return -EINVAL;
1239
1240         flow = otx2_find_flow(pfvf, location);
1241         if (!flow)
1242                 return -ENOENT;
1243
1244         if (flow->rule_type & DMAC_FILTER_RULE) {
1245                 struct ethhdr *eth_hdr = &flow->flow_spec.h_u.ether_spec;
1246
1247                 /* user not allowed to remove dmac filter with interface mac */
1248                 if (ether_addr_equal(pfvf->netdev->dev_addr, eth_hdr->h_dest))
1249                         return -EPERM;
1250
1251                 err = otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1252                                           flow->entry);
1253                 clear_bit(flow->entry, flow_cfg->dmacflt_bmap);
1254                 /* If all dmac filters are removed delete macfilter with
1255                  * interface mac address and configure CGX/RPM block in
1256                  * promiscuous mode
1257                  */
1258                 if (bitmap_weight(flow_cfg->dmacflt_bmap,
1259                                   flow_cfg->dmacflt_max_flows) == 1)
1260                         otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
1261         } else {
1262 #ifdef CONFIG_DCB
1263                 if (flow->rule_type & PFC_FLOWCTRL_RULE)
1264                         otx2_update_bpid_in_rqctx(pfvf, 0,
1265                                                   flow->flow_spec.ring_cookie,
1266                                                   false);
1267 #endif
1268
1269                 err = otx2_remove_flow_msg(pfvf, flow->entry, false);
1270         }
1271
1272         if (err)
1273                 return err;
1274
1275         list_del(&flow->list);
1276         kfree(flow);
1277         flow_cfg->nr_flows--;
1278
1279         return 0;
1280 }
1281
1282 void otx2_rss_ctx_flow_del(struct otx2_nic *pfvf, int ctx_id)
1283 {
1284         struct otx2_flow *flow, *tmp;
1285         int err;
1286
1287         list_for_each_entry_safe(flow, tmp, &pfvf->flow_cfg->flow_list, list) {
1288                 if (flow->rss_ctx_id != ctx_id)
1289                         continue;
1290                 err = otx2_remove_flow(pfvf, flow->location);
1291                 if (err)
1292                         netdev_warn(pfvf->netdev,
1293                                     "Can't delete the rule %d associated with this rss group err:%d",
1294                                     flow->location, err);
1295         }
1296 }
1297
1298 int otx2_destroy_ntuple_flows(struct otx2_nic *pfvf)
1299 {
1300         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1301         struct npc_delete_flow_req *req;
1302         struct otx2_flow *iter, *tmp;
1303         int err;
1304
1305         if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1306                 return 0;
1307
1308         if (!flow_cfg->max_flows)
1309                 return 0;
1310
1311         mutex_lock(&pfvf->mbox.lock);
1312         req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1313         if (!req) {
1314                 mutex_unlock(&pfvf->mbox.lock);
1315                 return -ENOMEM;
1316         }
1317
1318         req->start = flow_cfg->flow_ent[0];
1319         req->end   = flow_cfg->flow_ent[flow_cfg->max_flows - 1];
1320         err = otx2_sync_mbox_msg(&pfvf->mbox);
1321         mutex_unlock(&pfvf->mbox.lock);
1322
1323         list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1324                 list_del(&iter->list);
1325                 kfree(iter);
1326                 flow_cfg->nr_flows--;
1327         }
1328         return err;
1329 }
1330
1331 int otx2_destroy_mcam_flows(struct otx2_nic *pfvf)
1332 {
1333         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1334         struct npc_mcam_free_entry_req *req;
1335         struct otx2_flow *iter, *tmp;
1336         int err;
1337
1338         if (!(pfvf->flags & OTX2_FLAG_MCAM_ENTRIES_ALLOC))
1339                 return 0;
1340
1341         /* remove all flows */
1342         err = otx2_remove_flow_msg(pfvf, 0, true);
1343         if (err)
1344                 return err;
1345
1346         list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1347                 list_del(&iter->list);
1348                 kfree(iter);
1349                 flow_cfg->nr_flows--;
1350         }
1351
1352         mutex_lock(&pfvf->mbox.lock);
1353         req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
1354         if (!req) {
1355                 mutex_unlock(&pfvf->mbox.lock);
1356                 return -ENOMEM;
1357         }
1358
1359         req->all = 1;
1360         /* Send message to AF to free MCAM entries */
1361         err = otx2_sync_mbox_msg(&pfvf->mbox);
1362         if (err) {
1363                 mutex_unlock(&pfvf->mbox.lock);
1364                 return err;
1365         }
1366
1367         pfvf->flags &= ~OTX2_FLAG_MCAM_ENTRIES_ALLOC;
1368         mutex_unlock(&pfvf->mbox.lock);
1369
1370         return 0;
1371 }
1372
1373 int otx2_install_rxvlan_offload_flow(struct otx2_nic *pfvf)
1374 {
1375         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1376         struct npc_install_flow_req *req;
1377         int err;
1378
1379         mutex_lock(&pfvf->mbox.lock);
1380         req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
1381         if (!req) {
1382                 mutex_unlock(&pfvf->mbox.lock);
1383                 return -ENOMEM;
1384         }
1385
1386         req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1387         req->intf = NIX_INTF_RX;
1388         ether_addr_copy(req->packet.dmac, pfvf->netdev->dev_addr);
1389         eth_broadcast_addr((u8 *)&req->mask.dmac);
1390         req->channel = pfvf->hw.rx_chan_base;
1391         req->op = NIX_RX_ACTION_DEFAULT;
1392         req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
1393         req->vtag0_valid = true;
1394         req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1395
1396         /* Send message to AF */
1397         err = otx2_sync_mbox_msg(&pfvf->mbox);
1398         mutex_unlock(&pfvf->mbox.lock);
1399         return err;
1400 }
1401
1402 static int otx2_delete_rxvlan_offload_flow(struct otx2_nic *pfvf)
1403 {
1404         struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1405         struct npc_delete_flow_req *req;
1406         int err;
1407
1408         mutex_lock(&pfvf->mbox.lock);
1409         req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1410         if (!req) {
1411                 mutex_unlock(&pfvf->mbox.lock);
1412                 return -ENOMEM;
1413         }
1414
1415         req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1416         /* Send message to AF */
1417         err = otx2_sync_mbox_msg(&pfvf->mbox);
1418         mutex_unlock(&pfvf->mbox.lock);
1419         return err;
1420 }
1421
1422 int otx2_enable_rxvlan(struct otx2_nic *pf, bool enable)
1423 {
1424         struct nix_vtag_config *req;
1425         struct mbox_msghdr *rsp_hdr;
1426         int err;
1427
1428         /* Dont have enough mcam entries */
1429         if (!(pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT))
1430                 return -ENOMEM;
1431
1432         if (enable) {
1433                 err = otx2_install_rxvlan_offload_flow(pf);
1434                 if (err)
1435                         return err;
1436         } else {
1437                 err = otx2_delete_rxvlan_offload_flow(pf);
1438                 if (err)
1439                         return err;
1440         }
1441
1442         mutex_lock(&pf->mbox.lock);
1443         req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
1444         if (!req) {
1445                 mutex_unlock(&pf->mbox.lock);
1446                 return -ENOMEM;
1447         }
1448
1449         /* config strip, capture and size */
1450         req->vtag_size = VTAGSIZE_T4;
1451         req->cfg_type = 1; /* rx vlan cfg */
1452         req->rx.vtag_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1453         req->rx.strip_vtag = enable;
1454         req->rx.capture_vtag = enable;
1455
1456         err = otx2_sync_mbox_msg(&pf->mbox);
1457         if (err) {
1458                 mutex_unlock(&pf->mbox.lock);
1459                 return err;
1460         }
1461
1462         rsp_hdr = otx2_mbox_get_rsp(&pf->mbox.mbox, 0, &req->hdr);
1463         if (IS_ERR(rsp_hdr)) {
1464                 mutex_unlock(&pf->mbox.lock);
1465                 return PTR_ERR(rsp_hdr);
1466         }
1467
1468         mutex_unlock(&pf->mbox.lock);
1469         return rsp_hdr->rc;
1470 }
1471
1472 void otx2_dmacflt_reinstall_flows(struct otx2_nic *pf)
1473 {
1474         struct otx2_flow *iter;
1475         struct ethhdr *eth_hdr;
1476
1477         list_for_each_entry(iter, &pf->flow_cfg->flow_list, list) {
1478                 if (iter->rule_type & DMAC_FILTER_RULE) {
1479                         eth_hdr = &iter->flow_spec.h_u.ether_spec;
1480                         otx2_dmacflt_add(pf, eth_hdr->h_dest,
1481                                          iter->entry);
1482                 }
1483         }
1484 }
1485
1486 void otx2_dmacflt_update_pfmac_flow(struct otx2_nic *pfvf)
1487 {
1488         otx2_update_rem_pfmac(pfvf, DMAC_ADDR_UPDATE);
1489 }