net/octeontx2: add flow MCAM utility functions
[dpdk.git] / drivers / net / octeontx2 / otx2_flow_utils.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include "otx2_ethdev.h"
6 #include "otx2_flow.h"
7
8 static int
9 flow_mcam_alloc_counter(struct otx2_mbox *mbox, uint16_t *ctr)
10 {
11         struct npc_mcam_alloc_counter_req *req;
12         struct npc_mcam_alloc_counter_rsp *rsp;
13         int rc;
14
15         req = otx2_mbox_alloc_msg_npc_mcam_alloc_counter(mbox);
16         req->count = 1;
17         otx2_mbox_msg_send(mbox, 0);
18         rc = otx2_mbox_get_rsp(mbox, 0, (void *)&rsp);
19
20         *ctr = rsp->cntr_list[0];
21         return rc;
22 }
23
24 int
25 otx2_flow_mcam_free_counter(struct otx2_mbox *mbox, uint16_t ctr_id)
26 {
27         struct npc_mcam_oper_counter_req *req;
28         int rc;
29
30         req = otx2_mbox_alloc_msg_npc_mcam_free_counter(mbox);
31         req->cntr = ctr_id;
32         otx2_mbox_msg_send(mbox, 0);
33         rc = otx2_mbox_get_rsp(mbox, 0, NULL);
34
35         return rc;
36 }
37
38 int
39 otx2_flow_mcam_read_counter(struct otx2_mbox *mbox, uint32_t ctr_id,
40                             uint64_t *count)
41 {
42         struct npc_mcam_oper_counter_req *req;
43         struct npc_mcam_oper_counter_rsp *rsp;
44         int rc;
45
46         req = otx2_mbox_alloc_msg_npc_mcam_counter_stats(mbox);
47         req->cntr = ctr_id;
48         otx2_mbox_msg_send(mbox, 0);
49         rc = otx2_mbox_get_rsp(mbox, 0, (void *)&rsp);
50
51         *count = rsp->stat;
52         return rc;
53 }
54
55 int
56 otx2_flow_mcam_clear_counter(struct otx2_mbox *mbox, uint32_t ctr_id)
57 {
58         struct npc_mcam_oper_counter_req *req;
59         int rc;
60
61         req = otx2_mbox_alloc_msg_npc_mcam_clear_counter(mbox);
62         req->cntr = ctr_id;
63         otx2_mbox_msg_send(mbox, 0);
64         rc = otx2_mbox_get_rsp(mbox, 0, NULL);
65
66         return rc;
67 }
68
69 int
70 otx2_flow_mcam_free_entry(struct otx2_mbox *mbox, uint32_t entry)
71 {
72         struct npc_mcam_free_entry_req *req;
73         int rc;
74
75         req = otx2_mbox_alloc_msg_npc_mcam_free_entry(mbox);
76         req->entry = entry;
77         otx2_mbox_msg_send(mbox, 0);
78         rc = otx2_mbox_get_rsp(mbox, 0, NULL);
79
80         return rc;
81 }
82
83 int
84 otx2_flow_mcam_free_all_entries(struct otx2_mbox *mbox)
85 {
86         struct npc_mcam_free_entry_req *req;
87         int rc;
88
89         req = otx2_mbox_alloc_msg_npc_mcam_free_entry(mbox);
90         req->all = 1;
91         otx2_mbox_msg_send(mbox, 0);
92         rc = otx2_mbox_get_rsp(mbox, 0, NULL);
93
94         return rc;
95 }
96
97 static void
98 flow_prep_mcam_ldata(uint8_t *ptr, const uint8_t *data, int len)
99 {
100         int idx;
101
102         for (idx = 0; idx < len; idx++)
103                 ptr[idx] = data[len - 1 - idx];
104 }
105
106 static int
107 flow_check_copysz(size_t size, size_t len)
108 {
109         if (len <= size)
110                 return len;
111         return -1;
112 }
113
114 static inline int
115 flow_mem_is_zero(const void *mem, int len)
116 {
117         const char *m = mem;
118         int i;
119
120         for (i = 0; i < len; i++) {
121                 if (m[i] != 0)
122                         return 0;
123         }
124         return 1;
125 }
126
127 void
128 otx2_flow_get_hw_supp_mask(struct otx2_parse_state *pst,
129                            struct otx2_flow_item_info *info, int lid, int lt)
130 {
131         struct npc_xtract_info *xinfo;
132         char *hw_mask = info->hw_mask;
133         int max_off, offset;
134         int i, j;
135         int intf;
136
137         intf = pst->flow->nix_intf;
138         xinfo = pst->npc->prx_dxcfg[intf][lid][lt].xtract;
139         memset(hw_mask, 0, info->len);
140
141         for (i = 0; i < NPC_MAX_LD; i++) {
142                 if (xinfo[i].hdr_off < info->hw_hdr_len)
143                         continue;
144
145                 max_off = xinfo[i].hdr_off + xinfo[i].len - info->hw_hdr_len;
146
147                 if (xinfo[i].enable == 0)
148                         continue;
149
150                 if (max_off > info->len)
151                         max_off = info->len;
152
153                 offset = xinfo[i].hdr_off - info->hw_hdr_len;
154                 for (j = offset; j < max_off; j++)
155                         hw_mask[j] = 0xff;
156         }
157 }
158
159 int
160 otx2_flow_update_parse_state(struct otx2_parse_state *pst,
161                              struct otx2_flow_item_info *info, int lid, int lt,
162                              uint8_t flags)
163 {
164         uint8_t int_info_mask[NPC_MAX_EXTRACT_DATA_LEN];
165         uint8_t int_info[NPC_MAX_EXTRACT_DATA_LEN];
166         struct npc_lid_lt_xtract_info *xinfo;
167         int len = 0;
168         int intf;
169         int i;
170
171         otx2_npc_dbg("Parse state function info mask total %s",
172                      (const uint8_t *)info->mask);
173
174         pst->layer_mask |= lid;
175         pst->lt[lid] = lt;
176         pst->flags[lid] = flags;
177
178         intf = pst->flow->nix_intf;
179         xinfo = &pst->npc->prx_dxcfg[intf][lid][lt];
180         otx2_npc_dbg("Is_terminating = %d", xinfo->is_terminating);
181         if (xinfo->is_terminating)
182                 pst->terminate = 1;
183
184         /* Need to check if flags are supported but in latest
185          * KPU profile, flags are used as enumeration! No way,
186          * it can be validated unless MBOX is changed to return
187          * set of valid values out of 2**8 possible values.
188          */
189         if (info->spec == NULL) {       /* Nothing to match */
190                 otx2_npc_dbg("Info spec NULL");
191                 goto done;
192         }
193
194         /* Copy spec and mask into mcam match string, mask.
195          * Since both RTE FLOW and OTX2 MCAM use network-endianness
196          * for data, we are saved from nasty conversions.
197          */
198         for (i = 0; i < NPC_MAX_LD; i++) {
199                 struct npc_xtract_info *x;
200                 int k, idx, hdr_off;
201
202                 x = &xinfo->xtract[i];
203                 len = x->len;
204                 hdr_off = x->hdr_off;
205
206                 if (hdr_off < info->hw_hdr_len)
207                         continue;
208
209                 if (x->enable == 0)
210                         continue;
211
212                 otx2_npc_dbg("x->hdr_off = %d, len = %d, info->len = %d,"
213                               "x->key_off = %d", x->hdr_off, len, info->len,
214                               x->key_off);
215
216                 hdr_off -= info->hw_hdr_len;
217
218                 if (hdr_off + len > info->len)
219                         len = info->len - hdr_off;
220
221                 /* Check for over-write of previous layer */
222                 if (!flow_mem_is_zero(pst->mcam_mask + x->key_off,
223                                       len)) {
224                         /* Cannot support this data match */
225                         rte_flow_error_set(pst->error, ENOTSUP,
226                                            RTE_FLOW_ERROR_TYPE_ITEM,
227                                            pst->pattern,
228                                            "Extraction unsupported");
229                         return -rte_errno;
230                 }
231
232                 len = flow_check_copysz((OTX2_MAX_MCAM_WIDTH_DWORDS * 8)
233                                         - x->key_off,
234                                         len);
235                 if (len < 0) {
236                         rte_flow_error_set(pst->error, ENOTSUP,
237                                            RTE_FLOW_ERROR_TYPE_ITEM,
238                                            pst->pattern,
239                                            "Internal Error");
240                         return -rte_errno;
241                 }
242
243                 /* Need to reverse complete structure so that dest addr is at
244                  * MSB so as to program the MCAM using mcam_data & mcam_mask
245                  * arrays
246                  */
247                 flow_prep_mcam_ldata(int_info,
248                                      (const uint8_t *)info->spec + hdr_off,
249                                      x->len);
250                 flow_prep_mcam_ldata(int_info_mask,
251                                      (const uint8_t *)info->mask + hdr_off,
252                                      x->len);
253
254                 otx2_npc_dbg("Spec: ");
255                 for (k = 0; k < info->len; k++)
256                         otx2_npc_dbg("0x%.2x ",
257                                      ((const uint8_t *)info->spec)[k]);
258
259                 otx2_npc_dbg("Int_info: ");
260                 for (k = 0; k < info->len; k++)
261                         otx2_npc_dbg("0x%.2x ", int_info[k]);
262
263                 memcpy(pst->mcam_mask + x->key_off, int_info_mask, len);
264                 memcpy(pst->mcam_data + x->key_off, int_info, len);
265
266                 otx2_npc_dbg("Parse state mcam data & mask");
267                 for (idx = 0; idx < len ; idx++)
268                         otx2_npc_dbg("data[%d]: 0x%x, mask[%d]: 0x%x", idx,
269                                      *(pst->mcam_data + idx + x->key_off), idx,
270                                      *(pst->mcam_mask + idx + x->key_off));
271         }
272
273 done:
274         /* Next pattern to parse by subsequent layers */
275         pst->pattern++;
276         return 0;
277 }
278
279 static inline int
280 flow_range_is_valid(const char *spec, const char *last, const char *mask,
281                     int len)
282 {
283         /* Mask must be zero or equal to spec as we do not support
284          * non-contiguous ranges.
285          */
286         while (len--) {
287                 if (last[len] &&
288                     (spec[len] & mask[len]) != (last[len] & mask[len]))
289                         return 0; /* False */
290         }
291         return 1;
292 }
293
294
295 static inline int
296 flow_mask_is_supported(const char *mask, const char *hw_mask, int len)
297 {
298         /*
299          * If no hw_mask, assume nothing is supported.
300          * mask is never NULL
301          */
302         if (hw_mask == NULL)
303                 return flow_mem_is_zero(mask, len);
304
305         while (len--) {
306                 if ((mask[len] | hw_mask[len]) != hw_mask[len])
307                         return 0; /* False */
308         }
309         return 1;
310 }
311
312 int
313 otx2_flow_parse_item_basic(const struct rte_flow_item *item,
314                            struct otx2_flow_item_info *info,
315                            struct rte_flow_error *error)
316 {
317         /* Item must not be NULL */
318         if (item == NULL) {
319                 rte_flow_error_set(error, EINVAL,
320                                    RTE_FLOW_ERROR_TYPE_ITEM, NULL,
321                                    "Item is NULL");
322                 return -rte_errno;
323         }
324         /* If spec is NULL, both mask and last must be NULL, this
325          * makes it to match ANY value (eq to mask = 0).
326          * Setting either mask or last without spec is an error
327          */
328         if (item->spec == NULL) {
329                 if (item->last == NULL && item->mask == NULL) {
330                         info->spec = NULL;
331                         return 0;
332                 }
333                 rte_flow_error_set(error, EINVAL,
334                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
335                                    "mask or last set without spec");
336                 return -rte_errno;
337         }
338
339         /* We have valid spec */
340         info->spec = item->spec;
341
342         /* If mask is not set, use default mask, err if default mask is
343          * also NULL.
344          */
345         if (item->mask == NULL) {
346                 otx2_npc_dbg("Item mask null, using default mask");
347                 if (info->def_mask == NULL) {
348                         rte_flow_error_set(error, EINVAL,
349                                            RTE_FLOW_ERROR_TYPE_ITEM, item,
350                                            "No mask or default mask given");
351                         return -rte_errno;
352                 }
353                 info->mask = info->def_mask;
354         } else {
355                 info->mask = item->mask;
356         }
357
358         /* mask specified must be subset of hw supported mask
359          * mask | hw_mask == hw_mask
360          */
361         if (!flow_mask_is_supported(info->mask, info->hw_mask, info->len)) {
362                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
363                                    item, "Unsupported field in the mask");
364                 return -rte_errno;
365         }
366
367         /* Now we have spec and mask. OTX2 does not support non-contiguous
368          * range. We should have either:
369          * - spec & mask == last & mask or,
370          * - last == 0 or,
371          * - last == NULL
372          */
373         if (item->last != NULL && !flow_mem_is_zero(item->last, info->len)) {
374                 if (!flow_range_is_valid(item->spec, item->last, info->mask,
375                                          info->len)) {
376                         rte_flow_error_set(error, EINVAL,
377                                            RTE_FLOW_ERROR_TYPE_ITEM, item,
378                                            "Unsupported range for match");
379                         return -rte_errno;
380                 }
381         }
382
383         return 0;
384 }
385
386 void
387 otx2_flow_keyx_compress(uint64_t *data, uint32_t nibble_mask)
388 {
389         uint64_t cdata[2] = {0ULL, 0ULL}, nibble;
390         int i, j = 0;
391
392         for (i = 0; i < NPC_MAX_KEY_NIBBLES; i++) {
393                 if (nibble_mask & (1 << i)) {
394                         nibble = (data[i / 16] >> ((i & 0xf) * 4)) & 0xf;
395                         cdata[j / 16] |= (nibble << ((j & 0xf) * 4));
396                         j += 1;
397                 }
398         }
399
400         data[0] = cdata[0];
401         data[1] = cdata[1];
402 }
403
404 static int
405 flow_first_set_bit(uint64_t slab)
406 {
407         int num = 0;
408
409         if ((slab & 0xffffffff) == 0) {
410                 num += 32;
411                 slab >>= 32;
412         }
413         if ((slab & 0xffff) == 0) {
414                 num += 16;
415                 slab >>= 16;
416         }
417         if ((slab & 0xff) == 0) {
418                 num += 8;
419                 slab >>= 8;
420         }
421         if ((slab & 0xf) == 0) {
422                 num += 4;
423                 slab >>= 4;
424         }
425         if ((slab & 0x3) == 0) {
426                 num += 2;
427                 slab >>= 2;
428         }
429         if ((slab & 0x1) == 0)
430                 num += 1;
431
432         return num;
433 }
434
435 static int
436 flow_shift_lv_ent(struct otx2_mbox *mbox, struct rte_flow *flow,
437                   struct otx2_npc_flow_info *flow_info,
438                   uint32_t old_ent, uint32_t new_ent)
439 {
440         struct npc_mcam_shift_entry_req *req;
441         struct npc_mcam_shift_entry_rsp *rsp;
442         struct otx2_flow_list *list;
443         struct rte_flow *flow_iter;
444         int rc = 0;
445
446         otx2_npc_dbg("Old ent:%u new ent:%u priority:%u", old_ent, new_ent,
447                      flow->priority);
448
449         list = &flow_info->flow_list[flow->priority];
450
451         /* Old entry is disabled & it's contents are moved to new_entry,
452          * new entry is enabled finally.
453          */
454         req = otx2_mbox_alloc_msg_npc_mcam_shift_entry(mbox);
455         req->curr_entry[0] = old_ent;
456         req->new_entry[0] = new_ent;
457         req->shift_count = 1;
458
459         otx2_mbox_msg_send(mbox, 0);
460         rc = otx2_mbox_get_rsp(mbox, 0, (void *)&rsp);
461         if (rc)
462                 return rc;
463
464         /* Remove old node from list */
465         TAILQ_FOREACH(flow_iter, list, next) {
466                 if (flow_iter->mcam_id == old_ent)
467                         TAILQ_REMOVE(list, flow_iter, next);
468         }
469
470         /* Insert node with new mcam id at right place */
471         TAILQ_FOREACH(flow_iter, list, next) {
472                 if (flow_iter->mcam_id > new_ent)
473                         TAILQ_INSERT_BEFORE(flow_iter, flow, next);
474         }
475         return rc;
476 }
477
478 /* Exchange all required entries with a given priority level */
479 static int
480 flow_shift_ent(struct otx2_mbox *mbox, struct rte_flow *flow,
481                struct otx2_npc_flow_info *flow_info,
482                struct npc_mcam_alloc_entry_rsp *rsp, int dir, int prio_lvl)
483 {
484         struct rte_bitmap *fr_bmp, *fr_bmp_rev, *lv_bmp, *lv_bmp_rev, *bmp;
485         uint32_t e_fr = 0, e_lv = 0, e, e_id = 0, mcam_entries;
486         uint64_t fr_bit_pos = 0, lv_bit_pos = 0, bit_pos = 0;
487         /* Bit position within the slab */
488         uint32_t sl_fr_bit_off = 0, sl_lv_bit_off = 0;
489         /* Overall bit position of the start of slab */
490         /* free & live entry index */
491         int rc_fr = 0, rc_lv = 0, rc = 0, idx = 0;
492         struct otx2_mcam_ents_info *ent_info;
493         /* free & live bitmap slab */
494         uint64_t sl_fr = 0, sl_lv = 0, *sl;
495
496         fr_bmp = flow_info->free_entries[prio_lvl];
497         fr_bmp_rev = flow_info->free_entries_rev[prio_lvl];
498         lv_bmp = flow_info->live_entries[prio_lvl];
499         lv_bmp_rev = flow_info->live_entries_rev[prio_lvl];
500         ent_info = &flow_info->flow_entry_info[prio_lvl];
501         mcam_entries = flow_info->mcam_entries;
502
503
504         /* New entries allocated are always contiguous, but older entries
505          * already in free/live bitmap can be non-contiguous: so return
506          * shifted entries should be in non-contiguous format.
507          */
508         while (idx <= rsp->count) {
509                 if (!sl_fr && !sl_lv) {
510                         /* Lower index elements to be exchanged */
511                         if (dir < 0) {
512                                 rc_fr = rte_bitmap_scan(fr_bmp, &e_fr, &sl_fr);
513                                 rc_lv = rte_bitmap_scan(lv_bmp, &e_lv, &sl_lv);
514                                 otx2_npc_dbg("Fwd slab rc fr %u rc lv %u "
515                                              "e_fr %u e_lv %u", rc_fr, rc_lv,
516                                               e_fr, e_lv);
517                         } else {
518                                 rc_fr = rte_bitmap_scan(fr_bmp_rev,
519                                                         &sl_fr_bit_off,
520                                                         &sl_fr);
521                                 rc_lv = rte_bitmap_scan(lv_bmp_rev,
522                                                         &sl_lv_bit_off,
523                                                         &sl_lv);
524
525                                 otx2_npc_dbg("Rev slab rc fr %u rc lv %u "
526                                              "e_fr %u e_lv %u", rc_fr, rc_lv,
527                                               e_fr, e_lv);
528                         }
529                 }
530
531                 if (rc_fr) {
532                         fr_bit_pos = flow_first_set_bit(sl_fr);
533                         e_fr = sl_fr_bit_off + fr_bit_pos;
534                         otx2_npc_dbg("Fr_bit_pos 0x%" PRIx64, fr_bit_pos);
535                 } else {
536                         e_fr = ~(0);
537                 }
538
539                 if (rc_lv) {
540                         lv_bit_pos = flow_first_set_bit(sl_lv);
541                         e_lv = sl_lv_bit_off + lv_bit_pos;
542                         otx2_npc_dbg("Lv_bit_pos 0x%" PRIx64, lv_bit_pos);
543                 } else {
544                         e_lv = ~(0);
545                 }
546
547                 /* First entry is from free_bmap */
548                 if (e_fr < e_lv) {
549                         bmp = fr_bmp;
550                         e = e_fr;
551                         sl = &sl_fr;
552                         bit_pos = fr_bit_pos;
553                         if (dir > 0)
554                                 e_id = mcam_entries - e - 1;
555                         else
556                                 e_id = e;
557                         otx2_npc_dbg("Fr e %u e_id %u", e, e_id);
558                 } else {
559                         bmp = lv_bmp;
560                         e = e_lv;
561                         sl = &sl_lv;
562                         bit_pos = lv_bit_pos;
563                         if (dir > 0)
564                                 e_id = mcam_entries - e - 1;
565                         else
566                                 e_id = e;
567
568                         otx2_npc_dbg("Lv e %u e_id %u", e, e_id);
569                         if (idx < rsp->count)
570                                 rc =
571                                   flow_shift_lv_ent(mbox, flow,
572                                                     flow_info, e_id,
573                                                     rsp->entry + idx);
574                 }
575
576                 rte_bitmap_clear(bmp, e);
577                 rte_bitmap_set(bmp, rsp->entry + idx);
578                 /* Update entry list, use non-contiguous
579                  * list now.
580                  */
581                 rsp->entry_list[idx] = e_id;
582                 *sl &= ~(1 << bit_pos);
583
584                 /* Update min & max entry identifiers in current
585                  * priority level.
586                  */
587                 if (dir < 0) {
588                         ent_info->max_id = rsp->entry + idx;
589                         ent_info->min_id = e_id;
590                 } else {
591                         ent_info->max_id = e_id;
592                         ent_info->min_id = rsp->entry;
593                 }
594
595                 idx++;
596         }
597         return rc;
598 }
599
600 /* Validate if newly allocated entries lie in the correct priority zone
601  * since NPC_MCAM_LOWER_PRIO & NPC_MCAM_HIGHER_PRIO don't ensure zone accuracy.
602  * If not properly aligned, shift entries to do so
603  */
604 static int
605 flow_validate_and_shift_prio_ent(struct otx2_mbox *mbox, struct rte_flow *flow,
606                                  struct otx2_npc_flow_info *flow_info,
607                                  struct npc_mcam_alloc_entry_rsp *rsp,
608                                  int req_prio)
609 {
610         int prio_idx = 0, rc = 0, needs_shift = 0, idx, prio = flow->priority;
611         struct otx2_mcam_ents_info *info = flow_info->flow_entry_info;
612         int dir = (req_prio == NPC_MCAM_HIGHER_PRIO) ? 1 : -1;
613         uint32_t tot_ent = 0;
614
615         otx2_npc_dbg("Dir %d, priority = %d", dir, prio);
616
617         if (dir < 0)
618                 prio_idx = flow_info->flow_max_priority - 1;
619
620         /* Only live entries needs to be shifted, free entries can just be
621          * moved by bits manipulation.
622          */
623
624         /* For dir = -1(NPC_MCAM_LOWER_PRIO), when shifting,
625          * NPC_MAX_PREALLOC_ENT are exchanged with adjoining higher priority
626          * level entries(lower indexes).
627          *
628          * For dir = +1(NPC_MCAM_HIGHER_PRIO), during shift,
629          * NPC_MAX_PREALLOC_ENT are exchanged with adjoining lower priority
630          * level entries(higher indexes) with highest indexes.
631          */
632         do {
633                 tot_ent = info[prio_idx].free_ent + info[prio_idx].live_ent;
634
635                 if (dir < 0 && prio_idx != prio &&
636                     rsp->entry > info[prio_idx].max_id && tot_ent) {
637                         otx2_npc_dbg("Rsp entry %u prio idx %u "
638                                      "max id %u", rsp->entry, prio_idx,
639                                       info[prio_idx].max_id);
640
641                         needs_shift = 1;
642                 } else if ((dir > 0) && (prio_idx != prio) &&
643                      (rsp->entry < info[prio_idx].min_id) && tot_ent) {
644                         otx2_npc_dbg("Rsp entry %u prio idx %u "
645                                      "min id %u", rsp->entry, prio_idx,
646                                       info[prio_idx].min_id);
647                         needs_shift = 1;
648                 }
649
650                 otx2_npc_dbg("Needs_shift = %d", needs_shift);
651                 if (needs_shift) {
652                         needs_shift = 0;
653                         rc = flow_shift_ent(mbox, flow, flow_info, rsp, dir,
654                                             prio_idx);
655                 } else {
656                         for (idx = 0; idx < rsp->count; idx++)
657                                 rsp->entry_list[idx] = rsp->entry + idx;
658                 }
659         } while ((prio_idx != prio) && (prio_idx += dir));
660
661         return rc;
662 }
663
664 static int
665 flow_find_ref_entry(struct otx2_npc_flow_info *flow_info, int *prio,
666                     int prio_lvl)
667 {
668         struct otx2_mcam_ents_info *info = flow_info->flow_entry_info;
669         int step = 1;
670
671         while (step < flow_info->flow_max_priority) {
672                 if (((prio_lvl + step) < flow_info->flow_max_priority) &&
673                     info[prio_lvl + step].live_ent) {
674                         *prio = NPC_MCAM_HIGHER_PRIO;
675                         return info[prio_lvl + step].min_id;
676                 }
677
678                 if (((prio_lvl - step) >= 0) &&
679                     info[prio_lvl - step].live_ent) {
680                         otx2_npc_dbg("Prio_lvl %u live %u", prio_lvl - step,
681                                      info[prio_lvl - step].live_ent);
682                         *prio = NPC_MCAM_LOWER_PRIO;
683                         return info[prio_lvl - step].max_id;
684                 }
685                 step++;
686         }
687         *prio = NPC_MCAM_ANY_PRIO;
688         return 0;
689 }
690
691 static int
692 flow_fill_entry_cache(struct otx2_mbox *mbox, struct rte_flow *flow,
693                       struct otx2_npc_flow_info *flow_info, uint32_t *free_ent)
694 {
695         struct rte_bitmap *free_bmp, *free_bmp_rev, *live_bmp, *live_bmp_rev;
696         struct npc_mcam_alloc_entry_rsp rsp_local;
697         struct npc_mcam_alloc_entry_rsp *rsp_cmd;
698         struct npc_mcam_alloc_entry_req *req;
699         struct npc_mcam_alloc_entry_rsp *rsp;
700         struct otx2_mcam_ents_info *info;
701         uint16_t ref_ent, idx;
702         int rc, prio;
703
704         info = &flow_info->flow_entry_info[flow->priority];
705         free_bmp = flow_info->free_entries[flow->priority];
706         free_bmp_rev = flow_info->free_entries_rev[flow->priority];
707         live_bmp = flow_info->live_entries[flow->priority];
708         live_bmp_rev = flow_info->live_entries_rev[flow->priority];
709
710         ref_ent = flow_find_ref_entry(flow_info, &prio, flow->priority);
711
712         req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(mbox);
713         req->contig = 1;
714         req->count = flow_info->flow_prealloc_size;
715         req->priority = prio;
716         req->ref_entry = ref_ent;
717
718         otx2_npc_dbg("Fill cache ref entry %u prio %u", ref_ent, prio);
719
720         otx2_mbox_msg_send(mbox, 0);
721         rc = otx2_mbox_get_rsp(mbox, 0, (void *)&rsp_cmd);
722         if (rc)
723                 return rc;
724
725         rsp = &rsp_local;
726         memcpy(rsp, rsp_cmd, sizeof(*rsp));
727
728         otx2_npc_dbg("Alloc entry %u count %u , prio = %d", rsp->entry,
729                      rsp->count, prio);
730
731         /* Non-first ent cache fill */
732         if (prio != NPC_MCAM_ANY_PRIO) {
733                 flow_validate_and_shift_prio_ent(mbox, flow, flow_info, rsp,
734                                                  prio);
735         } else {
736                 /* Copy into response entry list */
737                 for (idx = 0; idx < rsp->count; idx++)
738                         rsp->entry_list[idx] = rsp->entry + idx;
739         }
740
741         otx2_npc_dbg("Fill entry cache rsp count %u", rsp->count);
742         /* Update free entries, reverse free entries list,
743          * min & max entry ids.
744          */
745         for (idx = 0; idx < rsp->count; idx++) {
746                 if (unlikely(rsp->entry_list[idx] < info->min_id))
747                         info->min_id = rsp->entry_list[idx];
748
749                 if (unlikely(rsp->entry_list[idx] > info->max_id))
750                         info->max_id = rsp->entry_list[idx];
751
752                 /* Skip entry to be returned, not to be part of free
753                  * list.
754                  */
755                 if (prio == NPC_MCAM_HIGHER_PRIO) {
756                         if (unlikely(idx == (rsp->count - 1))) {
757                                 *free_ent = rsp->entry_list[idx];
758                                 continue;
759                         }
760                 } else {
761                         if (unlikely(!idx)) {
762                                 *free_ent = rsp->entry_list[idx];
763                                 continue;
764                         }
765                 }
766                 info->free_ent++;
767                 rte_bitmap_set(free_bmp, rsp->entry_list[idx]);
768                 rte_bitmap_set(free_bmp_rev, flow_info->mcam_entries -
769                                rsp->entry_list[idx] - 1);
770
771                 otx2_npc_dbg("Final rsp entry %u rsp entry rev %u",
772                              rsp->entry_list[idx],
773                 flow_info->mcam_entries - rsp->entry_list[idx] - 1);
774         }
775
776         otx2_npc_dbg("Cache free entry %u, rev = %u", *free_ent,
777                      flow_info->mcam_entries - *free_ent - 1);
778         info->live_ent++;
779         rte_bitmap_set(live_bmp, *free_ent);
780         rte_bitmap_set(live_bmp_rev, flow_info->mcam_entries - *free_ent - 1);
781
782         return 0;
783 }
784
785 static int
786 flow_check_preallocated_entry_cache(struct otx2_mbox *mbox,
787                                     struct rte_flow *flow,
788                                     struct otx2_npc_flow_info *flow_info)
789 {
790         struct rte_bitmap *free, *free_rev, *live, *live_rev;
791         uint32_t pos = 0, free_ent = 0, mcam_entries;
792         struct otx2_mcam_ents_info *info;
793         uint64_t slab = 0;
794         int rc;
795
796         otx2_npc_dbg("Flow priority %u", flow->priority);
797
798         info = &flow_info->flow_entry_info[flow->priority];
799
800         free_rev = flow_info->free_entries_rev[flow->priority];
801         free = flow_info->free_entries[flow->priority];
802         live_rev = flow_info->live_entries_rev[flow->priority];
803         live = flow_info->live_entries[flow->priority];
804         mcam_entries = flow_info->mcam_entries;
805
806         if (info->free_ent) {
807                 rc = rte_bitmap_scan(free, &pos, &slab);
808                 if (rc) {
809                         /* Get free_ent from free entry bitmap */
810                         free_ent = pos + __builtin_ctzll(slab);
811                         otx2_npc_dbg("Allocated from cache entry %u", free_ent);
812                         /* Remove from free bitmaps and add to live ones */
813                         rte_bitmap_clear(free, free_ent);
814                         rte_bitmap_set(live, free_ent);
815                         rte_bitmap_clear(free_rev,
816                                          mcam_entries - free_ent - 1);
817                         rte_bitmap_set(live_rev,
818                                        mcam_entries - free_ent - 1);
819
820                         info->free_ent--;
821                         info->live_ent++;
822                         return free_ent;
823                 }
824
825                 otx2_npc_dbg("No free entry:its a mess");
826                 return -1;
827         }
828
829         rc = flow_fill_entry_cache(mbox, flow, flow_info, &free_ent);
830         if (rc)
831                 return rc;
832
833         return free_ent;
834 }
835
836 int
837 otx2_flow_mcam_alloc_and_write(struct rte_flow *flow, struct otx2_mbox *mbox,
838                                __rte_unused struct otx2_parse_state *pst,
839                                struct otx2_npc_flow_info *flow_info)
840 {
841         int use_ctr = (flow->ctr_id == NPC_COUNTER_NONE ? 0 : 1);
842         struct npc_mcam_write_entry_req *req;
843         struct mbox_msghdr *rsp;
844         uint16_t ctr = ~(0);
845         int rc, idx;
846         int entry;
847
848         if (use_ctr) {
849                 rc = flow_mcam_alloc_counter(mbox, &ctr);
850                 if (rc)
851                         return rc;
852         }
853
854         entry = flow_check_preallocated_entry_cache(mbox, flow, flow_info);
855         if (entry < 0) {
856                 otx2_err("Prealloc failed");
857                 otx2_flow_mcam_free_counter(mbox, ctr);
858                 return NPC_MCAM_ALLOC_FAILED;
859         }
860         req = otx2_mbox_alloc_msg_npc_mcam_write_entry(mbox);
861         req->set_cntr = use_ctr;
862         req->cntr = ctr;
863         req->entry = entry;
864         otx2_npc_dbg("Alloc & write entry %u", entry);
865
866         req->intf =
867                 (flow->nix_intf == OTX2_INTF_RX) ? NPC_MCAM_RX : NPC_MCAM_TX;
868         req->enable_entry = 1;
869         req->entry_data.action = flow->npc_action;
870
871         /*
872          * DPDK sets vtag action on per interface basis, not
873          * per flow basis. It is a matter of how we decide to support
874          * this pmd specific behavior. There are two ways:
875          *      1. Inherit the vtag action from the one configured
876          *         for this interface. This can be read from the
877          *         vtag_action configured for default mcam entry of
878          *         this pf_func.
879          *      2. Do not support vtag action with rte_flow.
880          *
881          * Second approach is used now.
882          */
883         req->entry_data.vtag_action = 0ULL;
884
885         for (idx = 0; idx < OTX2_MAX_MCAM_WIDTH_DWORDS; idx++) {
886                 req->entry_data.kw[idx] = flow->mcam_data[idx];
887                 req->entry_data.kw_mask[idx] = flow->mcam_mask[idx];
888         }
889
890         if (flow->nix_intf == OTX2_INTF_RX) {
891                 req->entry_data.kw[0] |= flow_info->channel;
892                 req->entry_data.kw_mask[0] |=  (BIT_ULL(12) - 1);
893         } else {
894                 uint16_t pf_func = (flow->npc_action >> 4) & 0xffff;
895
896                 pf_func = htons(pf_func);
897                 req->entry_data.kw[0] |= ((uint64_t)pf_func << 32);
898                 req->entry_data.kw_mask[0] |= ((uint64_t)0xffff << 32);
899         }
900
901         otx2_mbox_msg_send(mbox, 0);
902         rc = otx2_mbox_get_rsp(mbox, 0, (void *)&rsp);
903         if (rc != 0)
904                 return rc;
905
906         flow->mcam_id = entry;
907         if (use_ctr)
908                 flow->ctr_id = ctr;
909         return 0;
910 }