common/cnxk: support meter in action list
[dpdk.git] / drivers / common / cnxk / roc_npc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "roc_api.h"
6 #include "roc_priv.h"
7
8 int
9 roc_npc_vtag_actions_get(struct roc_npc *roc_npc)
10 {
11         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
12
13         return npc->vtag_strip_actions;
14 }
15
16 int
17 roc_npc_vtag_actions_sub_return(struct roc_npc *roc_npc, uint32_t count)
18 {
19         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
20
21         npc->vtag_strip_actions -= count;
22         return npc->vtag_strip_actions;
23 }
24
25 int
26 roc_npc_mcam_free_counter(struct roc_npc *roc_npc, uint16_t ctr_id)
27 {
28         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
29
30         return npc_mcam_free_counter(npc, ctr_id);
31 }
32
33 int
34 roc_npc_mcam_read_counter(struct roc_npc *roc_npc, uint32_t ctr_id,
35                           uint64_t *count)
36 {
37         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
38
39         return npc_mcam_read_counter(npc, ctr_id, count);
40 }
41
42 int
43 roc_npc_mcam_clear_counter(struct roc_npc *roc_npc, uint32_t ctr_id)
44 {
45         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
46
47         return npc_mcam_clear_counter(npc, ctr_id);
48 }
49
50 int
51 roc_npc_mcam_free_entry(struct roc_npc *roc_npc, uint32_t entry)
52 {
53         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
54
55         return npc_mcam_free_entry(npc, entry);
56 }
57
58 int
59 roc_npc_mcam_free_all_resources(struct roc_npc *roc_npc)
60 {
61         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
62
63         return npc_flow_free_all_resources(npc);
64 }
65
66 int
67 roc_npc_mcam_alloc_entries(struct roc_npc *roc_npc, int ref_entry,
68                            int *alloc_entry, int req_count, int priority,
69                            int *resp_count)
70 {
71         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
72
73         return npc_mcam_alloc_entries(npc, ref_entry, alloc_entry, req_count,
74                                       priority, resp_count);
75 }
76
77 int
78 roc_npc_mcam_alloc_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
79                          struct roc_npc_flow *ref_mcam, int prio,
80                          int *resp_count)
81 {
82         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
83
84         return npc_mcam_alloc_entry(npc, mcam, ref_mcam, prio, resp_count);
85 }
86
87 int
88 roc_npc_mcam_ena_dis_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
89                            bool enable)
90 {
91         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
92
93         return npc_mcam_ena_dis_entry(npc, mcam, enable);
94 }
95
96 int
97 roc_npc_mcam_write_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam)
98 {
99         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
100
101         return npc_mcam_write_entry(npc, mcam);
102 }
103
104 int
105 roc_npc_get_low_priority_mcam(struct roc_npc *roc_npc)
106 {
107         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
108
109         if (roc_model_is_cn10k())
110                 return (npc->mcam_entries - NPC_MCAME_RESVD_10XX - 1);
111         else if (roc_model_is_cn98xx())
112                 return (npc->mcam_entries - NPC_MCAME_RESVD_98XX - 1);
113         else
114                 return (npc->mcam_entries - NPC_MCAME_RESVD_9XXX - 1);
115 }
116
117 static int
118 npc_mcam_tot_entries(void)
119 {
120         /* FIXME: change to reading in AF from NPC_AF_CONST1/2
121          * MCAM_BANK_DEPTH(_EXT) * MCAM_BANKS
122          */
123         if (roc_model_is_cn10k() || roc_model_is_cn98xx())
124                 return 16 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 4096 */
125         else
126                 return 4 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 1024 */
127 }
128
129 const char *
130 roc_npc_profile_name_get(struct roc_npc *roc_npc)
131 {
132         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
133
134         return (char *)npc->profile_name;
135 }
136
137 int
138 roc_npc_init(struct roc_npc *roc_npc)
139 {
140         uint8_t *mem = NULL, *nix_mem = NULL, *npc_mem = NULL;
141         struct nix *nix = roc_nix_to_nix_priv(roc_npc->roc_nix);
142         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
143         uint32_t bmap_sz;
144         int rc = 0, idx;
145         size_t sz;
146
147         PLT_STATIC_ASSERT(sizeof(struct npc) <= ROC_NPC_MEM_SZ);
148
149         memset(npc, 0, sizeof(*npc));
150         npc->mbox = (&nix->dev)->mbox;
151         roc_npc->channel = nix->rx_chan_base;
152         roc_npc->pf_func = (&nix->dev)->pf_func;
153         npc->channel = roc_npc->channel;
154         npc->pf_func = roc_npc->pf_func;
155         npc->flow_max_priority = roc_npc->flow_max_priority;
156         npc->switch_header_type = roc_npc->switch_header_type;
157         npc->flow_prealloc_size = roc_npc->flow_prealloc_size;
158
159         if (npc->mbox == NULL)
160                 return NPC_ERR_PARAM;
161
162         rc = npc_mcam_fetch_kex_cfg(npc);
163         if (rc)
164                 goto done;
165
166         roc_npc->kex_capability = npc_get_kex_capability(npc);
167         roc_npc->rx_parse_nibble = npc->keyx_supp_nmask[NPC_MCAM_RX];
168
169         npc->mark_actions = 0;
170
171         npc->mcam_entries = npc_mcam_tot_entries() >> npc->keyw[NPC_MCAM_RX];
172
173         /* Free, free_rev, live and live_rev entries */
174         bmap_sz = plt_bitmap_get_memory_footprint(npc->mcam_entries);
175         mem = plt_zmalloc(4 * bmap_sz * npc->flow_max_priority, 0);
176         if (mem == NULL) {
177                 plt_err("Bmap alloc failed");
178                 rc = NPC_ERR_NO_MEM;
179                 return rc;
180         }
181
182         sz = npc->flow_max_priority * sizeof(struct npc_mcam_ents_info);
183         npc->flow_entry_info = plt_zmalloc(sz, 0);
184         if (npc->flow_entry_info == NULL) {
185                 plt_err("flow_entry_info alloc failed");
186                 rc = NPC_ERR_NO_MEM;
187                 goto done;
188         }
189
190         sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
191         npc->free_entries = plt_zmalloc(sz, 0);
192         if (npc->free_entries == NULL) {
193                 plt_err("free_entries alloc failed");
194                 rc = NPC_ERR_NO_MEM;
195                 goto done;
196         }
197
198         sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
199         npc->free_entries_rev = plt_zmalloc(sz, 0);
200         if (npc->free_entries_rev == NULL) {
201                 plt_err("free_entries_rev alloc failed");
202                 rc = NPC_ERR_NO_MEM;
203                 goto done;
204         }
205
206         sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
207         npc->live_entries = plt_zmalloc(sz, 0);
208         if (npc->live_entries == NULL) {
209                 plt_err("live_entries alloc failed");
210                 rc = NPC_ERR_NO_MEM;
211                 goto done;
212         }
213
214         sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
215         npc->live_entries_rev = plt_zmalloc(sz, 0);
216         if (npc->live_entries_rev == NULL) {
217                 plt_err("live_entries_rev alloc failed");
218                 rc = NPC_ERR_NO_MEM;
219                 goto done;
220         }
221
222         sz = npc->flow_max_priority * sizeof(struct npc_flow_list);
223         npc->flow_list = plt_zmalloc(sz, 0);
224         if (npc->flow_list == NULL) {
225                 plt_err("flow_list alloc failed");
226                 rc = NPC_ERR_NO_MEM;
227                 goto done;
228         }
229
230         npc_mem = mem;
231         for (idx = 0; idx < npc->flow_max_priority; idx++) {
232                 TAILQ_INIT(&npc->flow_list[idx]);
233
234                 npc->free_entries[idx] =
235                         plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
236                 mem += bmap_sz;
237
238                 npc->free_entries_rev[idx] =
239                         plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
240                 mem += bmap_sz;
241
242                 npc->live_entries[idx] =
243                         plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
244                 mem += bmap_sz;
245
246                 npc->live_entries_rev[idx] =
247                         plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
248                 mem += bmap_sz;
249
250                 npc->flow_entry_info[idx].free_ent = 0;
251                 npc->flow_entry_info[idx].live_ent = 0;
252                 npc->flow_entry_info[idx].max_id = 0;
253                 npc->flow_entry_info[idx].min_id = ~(0);
254         }
255
256         npc->rss_grps = NPC_RSS_GRPS;
257
258         bmap_sz = plt_bitmap_get_memory_footprint(npc->rss_grps);
259         nix_mem = plt_zmalloc(bmap_sz, 0);
260         if (nix_mem == NULL) {
261                 plt_err("Bmap alloc failed");
262                 rc = NPC_ERR_NO_MEM;
263                 goto done;
264         }
265
266         npc->rss_grp_entries = plt_bitmap_init(npc->rss_grps, nix_mem, bmap_sz);
267
268         if (!npc->rss_grp_entries) {
269                 plt_err("bitmap init failed");
270                 rc = NPC_ERR_NO_MEM;
271                 goto done;
272         }
273
274         /* Group 0 will be used for RSS,
275          * 1 -7 will be used for npc_flow RSS action
276          */
277         plt_bitmap_set(npc->rss_grp_entries, 0);
278
279         return rc;
280
281 done:
282         if (npc->flow_list)
283                 plt_free(npc->flow_list);
284         if (npc->live_entries_rev)
285                 plt_free(npc->live_entries_rev);
286         if (npc->live_entries)
287                 plt_free(npc->live_entries);
288         if (npc->free_entries_rev)
289                 plt_free(npc->free_entries_rev);
290         if (npc->free_entries)
291                 plt_free(npc->free_entries);
292         if (npc->flow_entry_info)
293                 plt_free(npc->flow_entry_info);
294         if (npc_mem)
295                 plt_free(npc_mem);
296         return rc;
297 }
298
299 int
300 roc_npc_fini(struct roc_npc *roc_npc)
301 {
302         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
303         int rc;
304
305         rc = npc_flow_free_all_resources(npc);
306         if (rc) {
307                 plt_err("Error when deleting NPC MCAM entries, counters");
308                 return rc;
309         }
310
311         if (npc->flow_list) {
312                 plt_free(npc->flow_list);
313                 npc->flow_list = NULL;
314         }
315
316         if (npc->live_entries_rev) {
317                 plt_free(npc->live_entries_rev);
318                 npc->live_entries_rev = NULL;
319         }
320
321         if (npc->live_entries) {
322                 plt_free(npc->live_entries);
323                 npc->live_entries = NULL;
324         }
325
326         if (npc->free_entries_rev) {
327                 plt_free(npc->free_entries_rev);
328                 npc->free_entries_rev = NULL;
329         }
330
331         if (npc->free_entries) {
332                 plt_free(npc->free_entries);
333                 npc->free_entries = NULL;
334         }
335
336         if (npc->flow_entry_info) {
337                 plt_free(npc->flow_entry_info);
338                 npc->flow_entry_info = NULL;
339         }
340
341         return 0;
342 }
343
344 static int
345 npc_parse_actions(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
346                   const struct roc_npc_action actions[],
347                   struct roc_npc_flow *flow)
348 {
349         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
350         const struct roc_npc_action_mark *act_mark;
351         const struct roc_npc_action_meter *act_mtr;
352         const struct roc_npc_action_queue *act_q;
353         const struct roc_npc_action_vf *vf_act;
354         bool vlan_insert_action = false;
355         int sel_act, req_act = 0;
356         uint16_t pf_func, vf_id;
357         int errcode = 0;
358         int mark = 0;
359         int rq = 0;
360
361         /* Initialize actions */
362         flow->ctr_id = NPC_COUNTER_NONE;
363         pf_func = npc->pf_func;
364
365         for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
366                 switch (actions->type) {
367                 case ROC_NPC_ACTION_TYPE_VOID:
368                         break;
369                 case ROC_NPC_ACTION_TYPE_MARK:
370                         act_mark = (const struct roc_npc_action_mark *)
371                                            actions->conf;
372                         if (act_mark->id > (NPC_FLOW_FLAG_VAL - 2)) {
373                                 plt_err("mark value must be < 0xfffe");
374                                 goto err_exit;
375                         }
376                         mark = act_mark->id + 1;
377                         req_act |= ROC_NPC_ACTION_TYPE_MARK;
378                         npc->mark_actions += 1;
379                         break;
380
381                 case ROC_NPC_ACTION_TYPE_FLAG:
382                         mark = NPC_FLOW_FLAG_VAL;
383                         req_act |= ROC_NPC_ACTION_TYPE_FLAG;
384                         npc->mark_actions += 1;
385                         break;
386
387                 case ROC_NPC_ACTION_TYPE_COUNT:
388                         /* Indicates, need a counter */
389                         flow->ctr_id = 1;
390                         req_act |= ROC_NPC_ACTION_TYPE_COUNT;
391                         break;
392
393                 case ROC_NPC_ACTION_TYPE_DROP:
394                         req_act |= ROC_NPC_ACTION_TYPE_DROP;
395                         break;
396
397                 case ROC_NPC_ACTION_TYPE_PF:
398                         req_act |= ROC_NPC_ACTION_TYPE_PF;
399                         pf_func &= (0xfc00);
400                         break;
401
402                 case ROC_NPC_ACTION_TYPE_VF:
403                         vf_act =
404                                 (const struct roc_npc_action_vf *)actions->conf;
405                         req_act |= ROC_NPC_ACTION_TYPE_VF;
406                         vf_id = vf_act->id & RVU_PFVF_FUNC_MASK;
407                         pf_func &= (0xfc00);
408                         pf_func = (pf_func | (vf_id + 1));
409                         break;
410
411                 case ROC_NPC_ACTION_TYPE_QUEUE:
412                         act_q = (const struct roc_npc_action_queue *)
413                                         actions->conf;
414                         rq = act_q->index;
415                         req_act |= ROC_NPC_ACTION_TYPE_QUEUE;
416                         break;
417
418                 case ROC_NPC_ACTION_TYPE_RSS:
419                         req_act |= ROC_NPC_ACTION_TYPE_RSS;
420                         break;
421
422                 case ROC_NPC_ACTION_TYPE_SEC:
423                         /* Assumes user has already configured security
424                          * session for this flow. Associated conf is
425                          * opaque. When security is implemented,
426                          * we need to verify that for specified security
427                          * session:
428                          *  action_type ==
429                          *    NPC_SECURITY_ACTION_TYPE_INLINE_PROTOCOL &&
430                          *  session_protocol ==
431                          *    NPC_SECURITY_PROTOCOL_IPSEC
432                          */
433                         req_act |= ROC_NPC_ACTION_TYPE_SEC;
434                         rq = 0;
435
436                         /* Special processing when with inline device */
437                         if (roc_nix_inb_is_with_inl_dev(roc_npc->roc_nix) &&
438                             roc_nix_inl_dev_is_probed()) {
439                                 rq = 0;
440                                 pf_func = nix_inl_dev_pffunc_get();
441                         }
442                         break;
443                 case ROC_NPC_ACTION_TYPE_VLAN_STRIP:
444                         req_act |= ROC_NPC_ACTION_TYPE_VLAN_STRIP;
445                         break;
446                 case ROC_NPC_ACTION_TYPE_VLAN_INSERT:
447                         req_act |= ROC_NPC_ACTION_TYPE_VLAN_INSERT;
448                         break;
449                 case ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT:
450                         req_act |= ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT;
451                         break;
452                 case ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT:
453                         req_act |= ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT;
454                         break;
455                 case ROC_NPC_ACTION_TYPE_METER:
456                         act_mtr = (const struct roc_npc_action_meter *)
457                                           actions->conf;
458                         flow->mtr_id = act_mtr->mtr_id;
459                         req_act |= ROC_NPC_ACTION_TYPE_METER;
460                         break;
461                 default:
462                         errcode = NPC_ERR_ACTION_NOTSUP;
463                         goto err_exit;
464                 }
465         }
466
467         if (req_act & (ROC_NPC_ACTION_TYPE_VLAN_INSERT |
468                        ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
469                        ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT))
470                 vlan_insert_action = true;
471
472         if ((req_act & (ROC_NPC_ACTION_TYPE_VLAN_INSERT |
473                         ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
474                         ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT)) ==
475             ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
476                 plt_err("PCP insert action can't be supported alone");
477                 errcode = NPC_ERR_ACTION_NOTSUP;
478                 goto err_exit;
479         }
480
481         /* Both STRIP and INSERT actions are not supported */
482         if (vlan_insert_action && (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP)) {
483                 errcode = NPC_ERR_ACTION_NOTSUP;
484                 goto err_exit;
485         }
486
487         /* Check if actions specified are compatible */
488         if (attr->egress) {
489                 if (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
490                         plt_err("VLAN pop action is not supported on Egress");
491                         errcode = NPC_ERR_ACTION_NOTSUP;
492                         goto err_exit;
493                 }
494
495                 if (req_act &
496                     ~(ROC_NPC_ACTION_TYPE_VLAN_INSERT |
497                       ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
498                       ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT |
499                       ROC_NPC_ACTION_TYPE_DROP | ROC_NPC_ACTION_TYPE_COUNT)) {
500                         plt_err("Only VLAN insert, drop, count supported on Egress");
501                         errcode = NPC_ERR_ACTION_NOTSUP;
502                         goto err_exit;
503                 }
504
505                 if (vlan_insert_action &&
506                     (req_act & ROC_NPC_ACTION_TYPE_DROP)) {
507                         plt_err("Both VLAN insert and drop actions cannot be supported");
508                         errcode = NPC_ERR_ACTION_NOTSUP;
509                         goto err_exit;
510                 }
511
512                 if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
513                         flow->npc_action = NIX_TX_ACTIONOP_DROP;
514                 } else if ((req_act & ROC_NPC_ACTION_TYPE_COUNT) ||
515                            vlan_insert_action) {
516                         flow->npc_action = NIX_TX_ACTIONOP_UCAST_DEFAULT;
517                 } else {
518                         plt_err("Unsupported action for egress");
519                         errcode = NPC_ERR_ACTION_NOTSUP;
520                         goto err_exit;
521                 }
522
523                 goto set_pf_func;
524         } else {
525                 if (vlan_insert_action) {
526                         errcode = NPC_ERR_ACTION_NOTSUP;
527                         goto err_exit;
528                 }
529         }
530
531         /* We have already verified the attr, this is ingress.
532          * - Exactly one terminating action is supported
533          * - Exactly one of MARK or FLAG is supported
534          * - If terminating action is DROP, only count is valid.
535          */
536         sel_act = req_act & NPC_ACTION_TERM;
537         if ((sel_act & (sel_act - 1)) != 0) {
538                 errcode = NPC_ERR_ACTION_NOTSUP;
539                 goto err_exit;
540         }
541
542         if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
543                 sel_act = req_act & ~ROC_NPC_ACTION_TYPE_COUNT;
544                 if ((sel_act & (sel_act - 1)) != 0) {
545                         errcode = NPC_ERR_ACTION_NOTSUP;
546                         goto err_exit;
547                 }
548         }
549
550         if ((req_act & (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) ==
551             (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
552                 errcode = NPC_ERR_ACTION_NOTSUP;
553                 goto err_exit;
554         }
555
556         if (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP)
557                 npc->vtag_strip_actions++;
558
559         /* Set NIX_RX_ACTIONOP */
560         if (req_act == ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
561                 /* Only VLAN action is provided */
562                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
563         } else if (req_act &
564                    (ROC_NPC_ACTION_TYPE_PF | ROC_NPC_ACTION_TYPE_VF)) {
565                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
566                 if (req_act & ROC_NPC_ACTION_TYPE_QUEUE)
567                         flow->npc_action |= (uint64_t)rq << 20;
568         } else if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
569                 flow->npc_action = NIX_RX_ACTIONOP_DROP;
570         } else if (req_act & ROC_NPC_ACTION_TYPE_QUEUE) {
571                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
572                 flow->npc_action |= (uint64_t)rq << 20;
573         } else if (req_act & ROC_NPC_ACTION_TYPE_RSS) {
574                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
575         } else if (req_act & ROC_NPC_ACTION_TYPE_SEC) {
576                 flow->npc_action = NIX_RX_ACTIONOP_UCAST_IPSEC;
577                 flow->npc_action |= (uint64_t)rq << 20;
578         } else if (req_act &
579                    (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
580                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
581         } else if (req_act & ROC_NPC_ACTION_TYPE_COUNT) {
582                 /* Keep ROC_NPC_ACTION_TYPE_COUNT_ACT always at the end
583                  * This is default action, when user specify only
584                  * COUNT ACTION
585                  */
586                 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
587         } else {
588                 /* Should never reach here */
589                 errcode = NPC_ERR_ACTION_NOTSUP;
590                 goto err_exit;
591         }
592
593         if (mark)
594                 flow->npc_action |= (uint64_t)mark << 40;
595
596 set_pf_func:
597         /* Ideally AF must ensure that correct pf_func is set */
598         flow->npc_action |= (uint64_t)pf_func << 4;
599
600         return 0;
601
602 err_exit:
603         return errcode;
604 }
605
606 typedef int (*npc_parse_stage_func_t)(struct npc_parse_state *pst);
607
608 static int
609 npc_parse_pattern(struct npc *npc, const struct roc_npc_item_info pattern[],
610                   struct roc_npc_flow *flow, struct npc_parse_state *pst)
611 {
612         npc_parse_stage_func_t parse_stage_funcs[] = {
613                 npc_parse_meta_items, npc_parse_cpt_hdr, npc_parse_higig2_hdr,
614                 npc_parse_la,         npc_parse_lb,      npc_parse_lc,
615                 npc_parse_ld,         npc_parse_le,      npc_parse_lf,
616                 npc_parse_lg,         npc_parse_lh,
617         };
618         uint8_t layer = 0;
619         int key_offset;
620         int rc;
621
622         if (pattern == NULL)
623                 return NPC_ERR_PARAM;
624
625         memset(pst, 0, sizeof(*pst));
626         pst->npc = npc;
627         pst->flow = flow;
628
629         /* Use integral byte offset */
630         key_offset = pst->npc->keyx_len[flow->nix_intf];
631         key_offset = (key_offset + 7) / 8;
632
633         /* Location where LDATA would begin */
634         pst->mcam_data = (uint8_t *)flow->mcam_data;
635         pst->mcam_mask = (uint8_t *)flow->mcam_mask;
636
637         while (pattern->type != ROC_NPC_ITEM_TYPE_END &&
638                layer < PLT_DIM(parse_stage_funcs)) {
639                 /* Skip place-holders */
640                 pattern = npc_parse_skip_void_and_any_items(pattern);
641
642                 pst->pattern = pattern;
643                 rc = parse_stage_funcs[layer](pst);
644                 if (rc != 0)
645                         return rc;
646
647                 layer++;
648
649                 /*
650                  * Parse stage function sets pst->pattern to
651                  * 1 past the last item it consumed.
652                  */
653                 pattern = pst->pattern;
654
655                 if (pst->terminate)
656                         break;
657         }
658
659         /* Skip trailing place-holders */
660         pattern = npc_parse_skip_void_and_any_items(pattern);
661
662         /* Are there more items than what we can handle? */
663         if (pattern->type != ROC_NPC_ITEM_TYPE_END)
664                 return NPC_ERR_PATTERN_NOTSUP;
665
666         return 0;
667 }
668
669 static int
670 npc_parse_attr(struct npc *npc, const struct roc_npc_attr *attr,
671                struct roc_npc_flow *flow)
672 {
673         if (attr == NULL)
674                 return NPC_ERR_PARAM;
675         else if (attr->priority >= npc->flow_max_priority)
676                 return NPC_ERR_PARAM;
677         else if ((!attr->egress && !attr->ingress) ||
678                  (attr->egress && attr->ingress))
679                 return NPC_ERR_PARAM;
680
681         if (attr->ingress)
682                 flow->nix_intf = ROC_NPC_INTF_RX;
683         else
684                 flow->nix_intf = ROC_NPC_INTF_TX;
685
686         flow->priority = attr->priority;
687         return 0;
688 }
689
690 static int
691 npc_parse_rule(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
692                const struct roc_npc_item_info pattern[],
693                const struct roc_npc_action actions[], struct roc_npc_flow *flow,
694                struct npc_parse_state *pst)
695 {
696         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
697         int err;
698
699         /* Check attr */
700         err = npc_parse_attr(npc, attr, flow);
701         if (err)
702                 return err;
703
704         /* Check pattern */
705         err = npc_parse_pattern(npc, pattern, flow, pst);
706         if (err)
707                 return err;
708
709         /* Check action */
710         err = npc_parse_actions(roc_npc, attr, actions, flow);
711         if (err)
712                 return err;
713         return 0;
714 }
715
716 int
717 roc_npc_flow_parse(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
718                    const struct roc_npc_item_info pattern[],
719                    const struct roc_npc_action actions[],
720                    struct roc_npc_flow *flow)
721 {
722         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
723         struct npc_parse_state parse_state = {0};
724         int rc;
725
726         rc = npc_parse_rule(roc_npc, attr, pattern, actions, flow,
727                             &parse_state);
728         if (rc)
729                 return rc;
730
731         parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
732
733         return npc_program_mcam(npc, &parse_state, 0);
734 }
735
736 int
737 npc_rss_free_grp_get(struct npc *npc, uint32_t *pos)
738 {
739         struct plt_bitmap *bmap = npc->rss_grp_entries;
740
741         for (*pos = 0; *pos < ROC_NIX_RSS_GRPS; ++*pos) {
742                 if (!plt_bitmap_get(bmap, *pos))
743                         break;
744         }
745         return *pos < ROC_NIX_RSS_GRPS ? 0 : -1;
746 }
747
748 int
749 npc_rss_action_configure(struct roc_npc *roc_npc,
750                          const struct roc_npc_action_rss *rss, uint8_t *alg_idx,
751                          uint32_t *rss_grp, uint32_t mcam_id)
752 {
753         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
754         struct roc_nix *roc_nix = roc_npc->roc_nix;
755         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
756         uint32_t flowkey_cfg, rss_grp_idx, i, rem;
757         uint8_t key[ROC_NIX_RSS_KEY_LEN];
758         const uint8_t *key_ptr;
759         uint8_t flowkey_algx;
760         uint16_t *reta;
761         int rc;
762
763         rc = npc_rss_free_grp_get(npc, &rss_grp_idx);
764         /* RSS group :0 is not usable for flow rss action */
765         if (rc < 0 || rss_grp_idx == 0)
766                 return -ENOSPC;
767
768         for (i = 0; i < rss->queue_num; i++) {
769                 if (rss->queue[i] >= nix->nb_rx_queues) {
770                         plt_err("queue id > max number of queues");
771                         return -EINVAL;
772                 }
773         }
774
775         *rss_grp = rss_grp_idx;
776
777         if (rss->key == NULL) {
778                 roc_nix_rss_key_default_fill(roc_nix, key);
779                 key_ptr = key;
780         } else {
781                 key_ptr = rss->key;
782         }
783
784         roc_nix_rss_key_set(roc_nix, key_ptr);
785
786         /* If queue count passed in the rss action is less than
787          * HW configured reta size, replicate rss action reta
788          * across HW reta table.
789          */
790         reta = nix->reta[rss_grp_idx];
791
792         if (rss->queue_num > nix->reta_sz) {
793                 plt_err("too many queues for RSS context");
794                 return -ENOTSUP;
795         }
796
797         for (i = 0; i < (nix->reta_sz / rss->queue_num); i++)
798                 memcpy(reta + i * rss->queue_num, rss->queue,
799                        sizeof(uint16_t) * rss->queue_num);
800
801         rem = nix->reta_sz % rss->queue_num;
802         if (rem)
803                 memcpy(&reta[i * rss->queue_num], rss->queue,
804                        rem * sizeof(uint16_t));
805
806         rc = roc_nix_rss_reta_set(roc_nix, *rss_grp, reta);
807         if (rc) {
808                 plt_err("Failed to init rss table rc = %d", rc);
809                 return rc;
810         }
811
812         flowkey_cfg = roc_npc->flowkey_cfg_state;
813
814         rc = roc_nix_rss_flowkey_set(roc_nix, &flowkey_algx, flowkey_cfg,
815                                      *rss_grp, mcam_id);
816         if (rc) {
817                 plt_err("Failed to set rss hash function rc = %d", rc);
818                 return rc;
819         }
820
821         *alg_idx = flowkey_algx;
822
823         plt_bitmap_set(npc->rss_grp_entries, *rss_grp);
824
825         return 0;
826 }
827
828 int
829 npc_rss_action_program(struct roc_npc *roc_npc,
830                        const struct roc_npc_action actions[],
831                        struct roc_npc_flow *flow)
832 {
833         const struct roc_npc_action_rss *rss;
834         uint32_t rss_grp;
835         uint8_t alg_idx;
836         int rc;
837
838         for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
839                 if (actions->type == ROC_NPC_ACTION_TYPE_RSS) {
840                         rss = (const struct roc_npc_action_rss *)actions->conf;
841                         rc = npc_rss_action_configure(roc_npc, rss, &alg_idx,
842                                                       &rss_grp, flow->mcam_id);
843                         if (rc)
844                                 return rc;
845
846                         flow->npc_action &= (~(0xfULL));
847                         flow->npc_action |= NIX_RX_ACTIONOP_RSS;
848                         flow->npc_action |=
849                                 ((uint64_t)(alg_idx & NPC_RSS_ACT_ALG_MASK)
850                                  << NPC_RSS_ACT_ALG_OFFSET) |
851                                 ((uint64_t)(rss_grp & NPC_RSS_ACT_GRP_MASK)
852                                  << NPC_RSS_ACT_GRP_OFFSET);
853                         break;
854                 }
855         }
856         return 0;
857 }
858
859 int
860 roc_npc_mark_actions_get(struct roc_npc *roc_npc)
861 {
862         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
863
864         return npc->mark_actions;
865 }
866
867 int
868 roc_npc_mark_actions_sub_return(struct roc_npc *roc_npc, uint32_t count)
869 {
870         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
871
872         npc->mark_actions -= count;
873         return npc->mark_actions;
874 }
875
876 static int
877 npc_vtag_cfg_delete(struct roc_npc *roc_npc, struct roc_npc_flow *flow)
878 {
879         struct roc_nix *roc_nix = roc_npc->roc_nix;
880         struct nix_vtag_config *vtag_cfg;
881         struct nix_vtag_config_rsp *rsp;
882         struct mbox *mbox;
883         struct nix *nix;
884         int rc = 0;
885
886         union {
887                 uint64_t reg;
888                 struct nix_tx_vtag_action_s act;
889         } tx_vtag_action;
890
891         nix = roc_nix_to_nix_priv(roc_nix);
892         mbox = (&nix->dev)->mbox;
893
894         tx_vtag_action.reg = flow->vtag_action;
895         vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
896
897         if (vtag_cfg == NULL)
898                 return -ENOSPC;
899
900         vtag_cfg->cfg_type = VTAG_TX;
901         vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
902         vtag_cfg->tx.vtag0_idx = tx_vtag_action.act.vtag0_def;
903         vtag_cfg->tx.free_vtag0 = true;
904
905         if (flow->vtag_insert_count == 2) {
906                 vtag_cfg->tx.vtag1_idx = tx_vtag_action.act.vtag1_def;
907                 vtag_cfg->tx.free_vtag1 = true;
908         }
909
910         rc = mbox_process_msg(mbox, (void *)&rsp);
911         if (rc)
912                 return rc;
913
914         return 0;
915 }
916
917 static int
918 npc_vtag_insert_action_parse(const struct roc_npc_action actions[],
919                              struct roc_npc_flow *flow,
920                              struct npc_action_vtag_info *vlan_info,
921                              int *parsed_cnt)
922 {
923         bool vlan_id_found = false, ethtype_found = false, pcp_found = false;
924         int count = 0;
925
926         *parsed_cnt = 0;
927
928         /* This function parses parameters of one VLAN. When a parameter is
929          * found repeated, it treats it as the end of first VLAN's parameters
930          * and returns. The caller calls again to parse the parameters of the
931          * second VLAN.
932          */
933
934         for (; count < NPC_ACTION_MAX_VLAN_PARAMS; count++, actions++) {
935                 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_INSERT) {
936                         if (vlan_id_found)
937                                 return 0;
938
939                         const struct roc_npc_action_of_set_vlan_vid *vtag =
940                                 (const struct roc_npc_action_of_set_vlan_vid *)
941                                         actions->conf;
942
943                         vlan_info->vlan_id = plt_be_to_cpu_16(vtag->vlan_vid);
944
945                         if (vlan_info->vlan_id > 0xfff) {
946                                 plt_err("Invalid vlan_id for set vlan action");
947                                 return -EINVAL;
948                         }
949
950                         flow->vtag_insert_enabled = true;
951                         (*parsed_cnt)++;
952                         vlan_id_found = true;
953                 } else if (actions->type ==
954                            ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT) {
955                         if (ethtype_found)
956                                 return 0;
957
958                         const struct roc_npc_action_of_push_vlan *ethtype =
959                                 (const struct roc_npc_action_of_push_vlan *)
960                                         actions->conf;
961                         vlan_info->vlan_ethtype =
962                                 plt_be_to_cpu_16(ethtype->ethertype);
963                         if (vlan_info->vlan_ethtype != ROC_ETHER_TYPE_VLAN &&
964                             vlan_info->vlan_ethtype != ROC_ETHER_TYPE_QINQ) {
965                                 plt_err("Invalid ethtype specified for push"
966                                         " vlan action");
967                                 return -EINVAL;
968                         }
969                         flow->vtag_insert_enabled = true;
970                         (*parsed_cnt)++;
971                         ethtype_found = true;
972                 } else if (actions->type ==
973                            ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
974                         if (pcp_found)
975                                 return 0;
976                         const struct roc_npc_action_of_set_vlan_pcp *pcp =
977                                 (const struct roc_npc_action_of_set_vlan_pcp *)
978                                         actions->conf;
979                         vlan_info->vlan_pcp = pcp->vlan_pcp;
980                         if (vlan_info->vlan_pcp > 0x7) {
981                                 plt_err("Invalid PCP value for pcp action");
982                                 return -EINVAL;
983                         }
984                         flow->vtag_insert_enabled = true;
985                         (*parsed_cnt)++;
986                         pcp_found = true;
987                 } else {
988                         return 0;
989                 }
990         }
991
992         return 0;
993 }
994
995 static int
996 npc_vtag_insert_action_configure(struct mbox *mbox, struct roc_npc_flow *flow,
997                                  struct npc_action_vtag_info *vlan_info)
998 {
999         struct nix_vtag_config *vtag_cfg;
1000         struct nix_vtag_config_rsp *rsp;
1001         int rc = 0;
1002
1003         union {
1004                 uint64_t reg;
1005                 struct nix_tx_vtag_action_s act;
1006         } tx_vtag_action;
1007
1008         vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
1009
1010         if (vtag_cfg == NULL)
1011                 return -ENOSPC;
1012
1013         vtag_cfg->cfg_type = VTAG_TX;
1014         vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
1015         vtag_cfg->tx.vtag0 =
1016                 ((vlan_info[0].vlan_ethtype << 16) |
1017                  (vlan_info[0].vlan_pcp << 13) | vlan_info[0].vlan_id);
1018
1019         vtag_cfg->tx.cfg_vtag0 = 1;
1020
1021         if (flow->vtag_insert_count == 2) {
1022                 vtag_cfg->tx.vtag1 =
1023                         ((vlan_info[1].vlan_ethtype << 16) |
1024                          (vlan_info[1].vlan_pcp << 13) | vlan_info[1].vlan_id);
1025
1026                 vtag_cfg->tx.cfg_vtag1 = 1;
1027         }
1028
1029         rc = mbox_process_msg(mbox, (void *)&rsp);
1030         if (rc)
1031                 return rc;
1032
1033         if (rsp->vtag0_idx < 0 ||
1034             ((flow->vtag_insert_count == 2) && (rsp->vtag1_idx < 0))) {
1035                 plt_err("Failed to config TX VTAG action");
1036                 return -EINVAL;
1037         }
1038
1039         tx_vtag_action.reg = 0;
1040         tx_vtag_action.act.vtag0_def = rsp->vtag0_idx;
1041         tx_vtag_action.act.vtag0_lid = NPC_LID_LA;
1042         tx_vtag_action.act.vtag0_op = NIX_TX_VTAGOP_INSERT;
1043         tx_vtag_action.act.vtag0_relptr = NIX_TX_VTAGACTION_VTAG0_RELPTR;
1044
1045         if (flow->vtag_insert_count == 2) {
1046                 tx_vtag_action.act.vtag1_def = rsp->vtag1_idx;
1047                 tx_vtag_action.act.vtag1_lid = NPC_LID_LA;
1048                 tx_vtag_action.act.vtag1_op = NIX_TX_VTAGOP_INSERT;
1049                 /* NIX_TX_VTAG_ACTION_S
1050                  *  If Vtag 0 is inserted, hardware adjusts the Vtag 1 byte
1051                  *  offset accordingly. Thus, if the two offsets are equal in
1052                  *  the structure, hardware inserts Vtag 1 immediately after
1053                  *  Vtag 0 in the packet.
1054                  */
1055                 tx_vtag_action.act.vtag1_relptr =
1056                         NIX_TX_VTAGACTION_VTAG0_RELPTR;
1057         }
1058
1059         flow->vtag_action = tx_vtag_action.reg;
1060
1061         return 0;
1062 }
1063
1064 static int
1065 npc_vtag_strip_action_configure(struct mbox *mbox,
1066                                 const struct roc_npc_action actions[],
1067                                 struct roc_npc_flow *flow, int *strip_cnt)
1068 {
1069         struct nix_vtag_config *vtag_cfg;
1070         uint64_t rx_vtag_action = 0;
1071         int count = 0, rc = 0;
1072
1073         *strip_cnt = 0;
1074
1075         for (; count < NPC_ACTION_MAX_VLANS_STRIPPED; count++, actions++) {
1076                 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_STRIP)
1077                         (*strip_cnt)++;
1078         }
1079
1080         vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
1081
1082         if (vtag_cfg == NULL)
1083                 return -ENOSPC;
1084
1085         vtag_cfg->cfg_type = VTAG_RX;
1086         vtag_cfg->rx.strip_vtag = 1;
1087         /* Always capture */
1088         vtag_cfg->rx.capture_vtag = 1;
1089         vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
1090         vtag_cfg->rx.vtag_type = 0;
1091
1092         rc = mbox_process(mbox);
1093         if (rc)
1094                 return rc;
1095
1096         rx_vtag_action |= (NIX_RX_VTAGACTION_VTAG_VALID << 15);
1097         rx_vtag_action |= ((uint64_t)NPC_LID_LB << 8);
1098         rx_vtag_action |= NIX_RX_VTAGACTION_VTAG0_RELPTR;
1099
1100         if (*strip_cnt == 2) {
1101                 rx_vtag_action |= (NIX_RX_VTAGACTION_VTAG_VALID << 47);
1102                 rx_vtag_action |= ((uint64_t)NPC_LID_LB << 40);
1103                 rx_vtag_action |= NIX_RX_VTAGACTION_VTAG0_RELPTR << 32;
1104         }
1105         flow->vtag_action = rx_vtag_action;
1106
1107         return 0;
1108 }
1109
1110 static int
1111 npc_vtag_action_program(struct roc_npc *roc_npc,
1112                         const struct roc_npc_action actions[],
1113                         struct roc_npc_flow *flow)
1114 {
1115         bool vlan_strip_parsed = false, vlan_insert_parsed = false;
1116         const struct roc_npc_action *insert_actions;
1117         struct roc_nix *roc_nix = roc_npc->roc_nix;
1118         struct npc_action_vtag_info vlan_info[2];
1119         int parsed_cnt = 0, strip_cnt = 0;
1120         int tot_vlan_params = 0;
1121         struct mbox *mbox;
1122         struct nix *nix;
1123         int i, rc;
1124
1125         nix = roc_nix_to_nix_priv(roc_nix);
1126         mbox = (&nix->dev)->mbox;
1127
1128         memset(vlan_info, 0, sizeof(vlan_info));
1129
1130         flow->vtag_insert_enabled = false;
1131
1132         for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
1133                 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
1134                         if (vlan_strip_parsed) {
1135                                 plt_err("Incorrect VLAN strip actions");
1136                                 return -EINVAL;
1137                         }
1138                         rc = npc_vtag_strip_action_configure(mbox, actions,
1139                                                              flow, &strip_cnt);
1140                         if (rc)
1141                                 return rc;
1142
1143                         if (strip_cnt == 2)
1144                                 actions++;
1145
1146                         vlan_strip_parsed = true;
1147                 } else if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_INSERT ||
1148                            actions->type ==
1149                                    ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT ||
1150                            actions->type ==
1151                                    ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
1152                         if (vlan_insert_parsed) {
1153                                 plt_err("Incorrect VLAN insert actions");
1154                                 return -EINVAL;
1155                         }
1156
1157                         insert_actions = actions;
1158
1159                         for (i = 0; i < 2; i++) {
1160                                 rc = npc_vtag_insert_action_parse(
1161                                         insert_actions, flow, &vlan_info[i],
1162                                         &parsed_cnt);
1163
1164                                 if (rc)
1165                                         return rc;
1166
1167                                 if (parsed_cnt) {
1168                                         insert_actions += parsed_cnt;
1169                                         tot_vlan_params += parsed_cnt;
1170                                         flow->vtag_insert_count++;
1171                                 }
1172                         }
1173                         actions += tot_vlan_params - 1;
1174                         vlan_insert_parsed = true;
1175                 }
1176         }
1177
1178         if (flow->vtag_insert_enabled) {
1179                 rc = npc_vtag_insert_action_configure(mbox, flow, vlan_info);
1180
1181                 if (rc)
1182                         return rc;
1183         }
1184         return 0;
1185 }
1186
1187 struct roc_npc_flow *
1188 roc_npc_flow_create(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
1189                     const struct roc_npc_item_info pattern[],
1190                     const struct roc_npc_action actions[], int *errcode)
1191 {
1192         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1193         struct roc_npc_flow *flow, *flow_iter;
1194         struct npc_parse_state parse_state;
1195         struct npc_flow_list *list;
1196         int rc;
1197
1198         npc->channel = roc_npc->channel;
1199
1200         flow = plt_zmalloc(sizeof(*flow), 0);
1201         if (flow == NULL) {
1202                 *errcode = NPC_ERR_NO_MEM;
1203                 return NULL;
1204         }
1205         memset(flow, 0, sizeof(*flow));
1206
1207         rc = npc_parse_rule(roc_npc, attr, pattern, actions, flow,
1208                             &parse_state);
1209         if (rc != 0) {
1210                 *errcode = rc;
1211                 goto err_exit;
1212         }
1213
1214         rc = npc_vtag_action_program(roc_npc, actions, flow);
1215         if (rc != 0) {
1216                 *errcode = rc;
1217                 goto err_exit;
1218         }
1219
1220         parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
1221
1222         rc = npc_program_mcam(npc, &parse_state, 1);
1223         if (rc != 0) {
1224                 *errcode = rc;
1225                 goto err_exit;
1226         }
1227
1228         rc = npc_rss_action_program(roc_npc, actions, flow);
1229         if (rc != 0) {
1230                 *errcode = rc;
1231                 goto set_rss_failed;
1232         }
1233
1234         list = &npc->flow_list[flow->priority];
1235         /* List in ascending order of mcam entries */
1236         TAILQ_FOREACH(flow_iter, list, next) {
1237                 if (flow_iter->mcam_id > flow->mcam_id) {
1238                         TAILQ_INSERT_BEFORE(flow_iter, flow, next);
1239                         return flow;
1240                 }
1241         }
1242
1243         TAILQ_INSERT_TAIL(list, flow, next);
1244         return flow;
1245
1246 set_rss_failed:
1247         rc = npc_mcam_free_entry(npc, flow->mcam_id);
1248         if (rc != 0) {
1249                 *errcode = rc;
1250                 plt_free(flow);
1251                 return NULL;
1252         }
1253 err_exit:
1254         plt_free(flow);
1255         return NULL;
1256 }
1257
1258 int
1259 npc_rss_group_free(struct npc *npc, struct roc_npc_flow *flow)
1260 {
1261         uint32_t rss_grp;
1262
1263         if ((flow->npc_action & 0xF) == NIX_RX_ACTIONOP_RSS) {
1264                 rss_grp = (flow->npc_action >> NPC_RSS_ACT_GRP_OFFSET) &
1265                           NPC_RSS_ACT_GRP_MASK;
1266                 if (rss_grp == 0 || rss_grp >= npc->rss_grps)
1267                         return -EINVAL;
1268
1269                 plt_bitmap_clear(npc->rss_grp_entries, rss_grp);
1270         }
1271
1272         return 0;
1273 }
1274
1275 int
1276 roc_npc_flow_destroy(struct roc_npc *roc_npc, struct roc_npc_flow *flow)
1277 {
1278         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1279         struct plt_bitmap *bmap;
1280         int rc;
1281
1282         rc = npc_rss_group_free(npc, flow);
1283         if (rc != 0) {
1284                 plt_err("Failed to free rss action rc = %d", rc);
1285                 return rc;
1286         }
1287
1288         if (flow->vtag_insert_enabled) {
1289                 rc = npc_vtag_cfg_delete(roc_npc, flow);
1290                 if (rc != 0)
1291                         return rc;
1292         }
1293
1294         rc = npc_mcam_free_entry(npc, flow->mcam_id);
1295         if (rc != 0)
1296                 return rc;
1297
1298         TAILQ_REMOVE(&npc->flow_list[flow->priority], flow, next);
1299
1300         bmap = npc->live_entries[flow->priority];
1301         plt_bitmap_clear(bmap, flow->mcam_id);
1302
1303         plt_free(flow);
1304         return 0;
1305 }
1306
1307 void
1308 roc_npc_flow_dump(FILE *file, struct roc_npc *roc_npc)
1309 {
1310         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1311         struct roc_npc_flow *flow_iter;
1312         struct npc_flow_list *list;
1313         uint32_t max_prio, i;
1314
1315         max_prio = npc->flow_max_priority;
1316
1317         for (i = 0; i < max_prio; i++) {
1318                 list = &npc->flow_list[i];
1319
1320                 /* List in ascending order of mcam entries */
1321                 TAILQ_FOREACH(flow_iter, list, next) {
1322                         roc_npc_flow_mcam_dump(file, roc_npc, flow_iter);
1323                 }
1324         }
1325 }
1326
1327 int
1328 roc_npc_mcam_merge_base_steering_rule(struct roc_npc *roc_npc,
1329                                       struct roc_npc_flow *flow)
1330 {
1331         struct npc_mcam_read_base_rule_rsp *base_rule_rsp;
1332         struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1333         struct mcam_entry *base_entry;
1334         int idx, rc;
1335
1336         if (roc_nix_is_pf(roc_npc->roc_nix))
1337                 return 0;
1338
1339         (void)mbox_alloc_msg_npc_read_base_steer_rule(npc->mbox);
1340         rc = mbox_process_msg(npc->mbox, (void *)&base_rule_rsp);
1341         if (rc) {
1342                 plt_err("Failed to fetch VF's base MCAM entry");
1343                 return rc;
1344         }
1345         base_entry = &base_rule_rsp->entry_data;
1346         for (idx = 0; idx < ROC_NPC_MAX_MCAM_WIDTH_DWORDS; idx++) {
1347                 flow->mcam_data[idx] |= base_entry->kw[idx];
1348                 flow->mcam_mask[idx] |= base_entry->kw_mask[idx];
1349         }
1350
1351         return 0;
1352 }