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