1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
8 npc_prep_mcam_ldata(uint8_t *ptr, const uint8_t *data, int len)
12 for (idx = 0; idx < len; idx++)
13 ptr[idx] = data[len - 1 - idx];
17 npc_check_copysz(size_t size, size_t len)
25 npc_mem_is_zero(const void *mem, int len)
30 for (i = 0; i < len; i++) {
38 npc_set_hw_mask(struct npc_parse_item_info *info, struct npc_xtract_info *xinfo,
44 if (xinfo->enable == 0)
47 if (xinfo->hdr_off < info->hw_hdr_len)
50 max_off = xinfo->hdr_off + xinfo->len - info->hw_hdr_len;
52 if (max_off > info->len)
55 offset = xinfo->hdr_off - info->hw_hdr_len;
56 for (j = offset; j < max_off; j++)
61 npc_get_hw_supp_mask(struct npc_parse_state *pst,
62 struct npc_parse_item_info *info, int lid, int lt)
64 struct npc_xtract_info *xinfo, *lfinfo;
65 char *hw_mask = info->hw_mask;
71 xinfo = pst->npc->prx_dxcfg[intf][lid][lt].xtract;
72 memset(hw_mask, 0, info->len);
74 for (i = 0; i < NPC_MAX_LD; i++)
75 npc_set_hw_mask(info, &xinfo[i], hw_mask);
77 for (i = 0; i < NPC_MAX_LD; i++) {
78 if (xinfo[i].flags_enable == 0)
81 lf_cfg = pst->npc->prx_lfcfg[i].i;
83 for (j = 0; j < NPC_MAX_LFL; j++) {
84 lfinfo = pst->npc->prx_fxcfg[intf][i][j].xtract;
85 npc_set_hw_mask(info, &lfinfo[0], hw_mask);
92 npc_mask_is_supported(const char *mask, const char *hw_mask, int len)
95 * If no hw_mask, assume nothing is supported.
99 return npc_mem_is_zero(mask, len);
102 if ((mask[len] | hw_mask[len]) != hw_mask[len])
103 return 0; /* False */
109 npc_parse_item_basic(const struct roc_npc_item_info *item,
110 struct npc_parse_item_info *info)
112 /* Item must not be NULL */
114 return NPC_ERR_PARAM;
116 /* Don't support ranges */
117 if (item->last != NULL)
118 return NPC_ERR_INVALID_RANGE;
120 /* If spec is NULL, both mask and last must be NULL, this
121 * makes it to match ANY value (eq to mask = 0).
122 * Setting either mask or last without spec is an error
124 if (item->spec == NULL) {
125 if (item->last == NULL && item->mask == NULL) {
129 return NPC_ERR_INVALID_SPEC;
132 /* We have valid spec */
133 info->spec = item->spec;
135 /* If mask is not set, use default mask, err if default mask is
138 if (item->mask == NULL) {
139 if (info->def_mask == NULL)
140 return NPC_ERR_PARAM;
141 info->mask = info->def_mask;
143 info->mask = item->mask;
146 /* mask specified must be subset of hw supported mask
147 * mask | hw_mask == hw_mask
149 if (!npc_mask_is_supported(info->mask, info->hw_mask, info->len))
150 return NPC_ERR_INVALID_MASK;
156 npc_update_extraction_data(struct npc_parse_state *pst,
157 struct npc_parse_item_info *info,
158 struct npc_xtract_info *xinfo)
160 uint8_t int_info_mask[NPC_MAX_EXTRACT_DATA_LEN];
161 uint8_t int_info[NPC_MAX_EXTRACT_DATA_LEN];
162 struct npc_xtract_info *x;
168 hdr_off = x->hdr_off;
170 if (hdr_off < info->hw_hdr_len)
176 hdr_off -= info->hw_hdr_len;
178 if (hdr_off >= info->len)
181 if (hdr_off + len > info->len)
182 len = info->len - hdr_off;
184 len = npc_check_copysz((ROC_NPC_MAX_MCAM_WIDTH_DWORDS * 8) - x->key_off,
187 return NPC_ERR_INVALID_SIZE;
189 /* Need to reverse complete structure so that dest addr is at
190 * MSB so as to program the MCAM using mcam_data & mcam_mask
193 npc_prep_mcam_ldata(int_info, (const uint8_t *)info->spec + hdr_off,
195 npc_prep_mcam_ldata(int_info_mask,
196 (const uint8_t *)info->mask + hdr_off, x->len);
198 memcpy(pst->mcam_mask + x->key_off, int_info_mask, len);
199 memcpy(pst->mcam_data + x->key_off, int_info, len);
204 npc_update_parse_state(struct npc_parse_state *pst,
205 struct npc_parse_item_info *info, int lid, int lt,
208 struct npc_lid_lt_xtract_info *xinfo;
209 struct roc_npc_flow_dump_data *dump;
210 struct npc_xtract_info *lfinfo;
214 pst->layer_mask |= lid;
216 pst->flags[lid] = flags;
218 intf = pst->nix_intf;
219 xinfo = &pst->npc->prx_dxcfg[intf][lid][lt];
220 if (xinfo->is_terminating)
223 if (info->spec == NULL)
226 for (i = 0; i < NPC_MAX_LD; i++) {
227 rc = npc_update_extraction_data(pst, info, &xinfo->xtract[i]);
232 for (i = 0; i < NPC_MAX_LD; i++) {
233 if (xinfo->xtract[i].flags_enable == 0)
236 lf_cfg = pst->npc->prx_lfcfg[i].i;
238 for (j = 0; j < NPC_MAX_LFL; j++) {
239 lfinfo = pst->npc->prx_fxcfg[intf][i][j].xtract;
240 rc = npc_update_extraction_data(pst, info,
245 if (lfinfo[0].enable)
252 dump = &pst->flow->dump_data[pst->flow->num_patterns++];
260 npc_first_set_bit(uint64_t slab)
264 if ((slab & 0xffffffff) == 0) {
268 if ((slab & 0xffff) == 0) {
272 if ((slab & 0xff) == 0) {
276 if ((slab & 0xf) == 0) {
280 if ((slab & 0x3) == 0) {
284 if ((slab & 0x1) == 0)
291 npc_shift_lv_ent(struct mbox *mbox, struct roc_npc_flow *flow, struct npc *npc,
292 uint32_t old_ent, uint32_t new_ent)
294 struct npc_mcam_shift_entry_req *req;
295 struct npc_mcam_shift_entry_rsp *rsp;
296 struct npc_flow_list *list;
297 struct roc_npc_flow *flow_iter;
300 list = &npc->flow_list[flow->priority];
302 /* Old entry is disabled & it's contents are moved to new_entry,
303 * new entry is enabled finally.
305 req = mbox_alloc_msg_npc_mcam_shift_entry(mbox);
308 req->curr_entry[0] = old_ent;
309 req->new_entry[0] = new_ent;
310 req->shift_count = 1;
312 rc = mbox_process_msg(mbox, (void *)&rsp);
316 /* Remove old node from list */
317 TAILQ_FOREACH(flow_iter, list, next) {
318 if (flow_iter->mcam_id == old_ent)
319 TAILQ_REMOVE(list, flow_iter, next);
322 /* Insert node with new mcam id at right place */
323 TAILQ_FOREACH(flow_iter, list, next) {
324 if (flow_iter->mcam_id > new_ent)
325 TAILQ_INSERT_BEFORE(flow_iter, flow, next);
330 /* Exchange all required entries with a given priority level */
332 npc_shift_ent(struct mbox *mbox, struct roc_npc_flow *flow, struct npc *npc,
333 struct npc_mcam_alloc_entry_rsp *rsp, int dir, int prio_lvl)
335 struct plt_bitmap *fr_bmp, *fr_bmp_rev, *lv_bmp, *lv_bmp_rev, *bmp;
336 uint32_t e_fr = 0, e_lv = 0, e, e_id = 0, mcam_entries;
337 uint64_t fr_bit_pos = 0, lv_bit_pos = 0, bit_pos = 0;
338 /* Bit position within the slab */
339 uint32_t sl_fr_bit_off = 0, sl_lv_bit_off = 0;
340 /* Overall bit position of the start of slab */
341 /* free & live entry index */
342 int rc_fr = 0, rc_lv = 0, rc = 0, idx = 0;
343 struct npc_mcam_ents_info *ent_info;
344 /* free & live bitmap slab */
345 uint64_t sl_fr = 0, sl_lv = 0, *sl;
347 fr_bmp = npc->free_entries[prio_lvl];
348 fr_bmp_rev = npc->free_entries_rev[prio_lvl];
349 lv_bmp = npc->live_entries[prio_lvl];
350 lv_bmp_rev = npc->live_entries_rev[prio_lvl];
351 ent_info = &npc->flow_entry_info[prio_lvl];
352 mcam_entries = npc->mcam_entries;
354 /* New entries allocated are always contiguous, but older entries
355 * already in free/live bitmap can be non-contiguous: so return
356 * shifted entries should be in non-contiguous format.
358 while (idx <= rsp->count) {
359 if (!sl_fr && !sl_lv) {
360 /* Lower index elements to be exchanged */
362 rc_fr = plt_bitmap_scan(fr_bmp, &e_fr, &sl_fr);
363 rc_lv = plt_bitmap_scan(lv_bmp, &e_lv, &sl_lv);
365 rc_fr = plt_bitmap_scan(fr_bmp_rev,
366 &sl_fr_bit_off, &sl_fr);
367 rc_lv = plt_bitmap_scan(lv_bmp_rev,
368 &sl_lv_bit_off, &sl_lv);
373 fr_bit_pos = npc_first_set_bit(sl_fr);
374 e_fr = sl_fr_bit_off + fr_bit_pos;
380 lv_bit_pos = npc_first_set_bit(sl_lv);
381 e_lv = sl_lv_bit_off + lv_bit_pos;
386 /* First entry is from free_bmap */
391 bit_pos = fr_bit_pos;
393 e_id = mcam_entries - e - 1;
400 bit_pos = lv_bit_pos;
402 e_id = mcam_entries - e - 1;
406 if (idx < rsp->count)
407 rc = npc_shift_lv_ent(mbox, flow, npc, e_id,
411 plt_bitmap_clear(bmp, e);
412 plt_bitmap_set(bmp, rsp->entry + idx);
413 /* Update entry list, use non-contiguous
416 rsp->entry_list[idx] = e_id;
417 *sl &= ~(1UL << bit_pos);
419 /* Update min & max entry identifiers in current
423 ent_info->max_id = rsp->entry + idx;
424 ent_info->min_id = e_id;
426 ent_info->max_id = e_id;
427 ent_info->min_id = rsp->entry;
435 /* Validate if newly allocated entries lie in the correct priority zone
436 * since NPC_MCAM_LOWER_PRIO & NPC_MCAM_HIGHER_PRIO don't ensure zone accuracy.
437 * If not properly aligned, shift entries to do so
440 npc_validate_and_shift_prio_ent(struct mbox *mbox, struct roc_npc_flow *flow,
442 struct npc_mcam_alloc_entry_rsp *rsp,
445 int prio_idx = 0, rc = 0, needs_shift = 0, idx, prio = flow->priority;
446 struct npc_mcam_ents_info *info = npc->flow_entry_info;
447 int dir = (req_prio == NPC_MCAM_HIGHER_PRIO) ? 1 : -1;
448 uint32_t tot_ent = 0;
451 prio_idx = npc->flow_max_priority - 1;
453 /* Only live entries needs to be shifted, free entries can just be
454 * moved by bits manipulation.
457 /* For dir = -1(NPC_MCAM_LOWER_PRIO), when shifting,
458 * NPC_MAX_PREALLOC_ENT are exchanged with adjoining higher priority
459 * level entries(lower indexes).
461 * For dir = +1(NPC_MCAM_HIGHER_PRIO), during shift,
462 * NPC_MAX_PREALLOC_ENT are exchanged with adjoining lower priority
463 * level entries(higher indexes) with highest indexes.
466 tot_ent = info[prio_idx].free_ent + info[prio_idx].live_ent;
468 if (dir < 0 && prio_idx != prio &&
469 rsp->entry > info[prio_idx].max_id && tot_ent) {
471 } else if ((dir > 0) && (prio_idx != prio) &&
472 (rsp->entry < info[prio_idx].min_id) && tot_ent) {
478 rc = npc_shift_ent(mbox, flow, npc, rsp, dir, prio_idx);
480 for (idx = 0; idx < rsp->count; idx++)
481 rsp->entry_list[idx] = rsp->entry + idx;
483 } while ((prio_idx != prio) && (prio_idx += dir));
489 npc_find_ref_entry(struct npc *npc, int *prio, int prio_lvl)
491 struct npc_mcam_ents_info *info = npc->flow_entry_info;
494 while (step < npc->flow_max_priority) {
495 if (((prio_lvl + step) < npc->flow_max_priority) &&
496 info[prio_lvl + step].live_ent) {
497 *prio = NPC_MCAM_HIGHER_PRIO;
498 return info[prio_lvl + step].min_id;
501 if (((prio_lvl - step) >= 0) &&
502 info[prio_lvl - step].live_ent) {
503 *prio = NPC_MCAM_LOWER_PRIO;
504 return info[prio_lvl - step].max_id;
508 *prio = NPC_MCAM_ANY_PRIO;
513 npc_fill_entry_cache(struct mbox *mbox, struct roc_npc_flow *flow,
514 struct npc *npc, uint32_t *free_ent)
516 struct plt_bitmap *free_bmp, *free_bmp_rev, *live_bmp, *live_bmp_rev;
517 struct npc_mcam_alloc_entry_rsp rsp_local;
518 struct npc_mcam_alloc_entry_rsp *rsp_cmd;
519 struct npc_mcam_alloc_entry_req *req;
520 struct npc_mcam_alloc_entry_rsp *rsp;
521 struct npc_mcam_ents_info *info;
522 int rc = -ENOSPC, prio;
523 uint16_t ref_ent, idx;
525 info = &npc->flow_entry_info[flow->priority];
526 free_bmp = npc->free_entries[flow->priority];
527 free_bmp_rev = npc->free_entries_rev[flow->priority];
528 live_bmp = npc->live_entries[flow->priority];
529 live_bmp_rev = npc->live_entries_rev[flow->priority];
531 ref_ent = npc_find_ref_entry(npc, &prio, flow->priority);
533 req = mbox_alloc_msg_npc_mcam_alloc_entry(mbox);
537 req->count = npc->flow_prealloc_size;
538 req->priority = prio;
539 req->ref_entry = ref_ent;
541 rc = mbox_process_msg(mbox, (void *)&rsp_cmd);
546 memcpy(rsp, rsp_cmd, sizeof(*rsp));
548 /* Non-first ent cache fill */
549 if (prio != NPC_MCAM_ANY_PRIO) {
550 npc_validate_and_shift_prio_ent(mbox, flow, npc, rsp, prio);
552 /* Copy into response entry list */
553 for (idx = 0; idx < rsp->count; idx++)
554 rsp->entry_list[idx] = rsp->entry + idx;
557 /* Update free entries, reverse free entries list,
558 * min & max entry ids.
560 for (idx = 0; idx < rsp->count; idx++) {
561 if (unlikely(rsp->entry_list[idx] < info->min_id))
562 info->min_id = rsp->entry_list[idx];
564 if (unlikely(rsp->entry_list[idx] > info->max_id))
565 info->max_id = rsp->entry_list[idx];
567 /* Skip entry to be returned, not to be part of free
570 if (prio == NPC_MCAM_HIGHER_PRIO) {
571 if (unlikely(idx == (rsp->count - 1))) {
572 *free_ent = rsp->entry_list[idx];
576 if (unlikely(!idx)) {
577 *free_ent = rsp->entry_list[idx];
582 plt_bitmap_set(free_bmp, rsp->entry_list[idx]);
583 plt_bitmap_set(free_bmp_rev,
584 npc->mcam_entries - rsp->entry_list[idx] - 1);
588 plt_bitmap_set(live_bmp, *free_ent);
589 plt_bitmap_set(live_bmp_rev, npc->mcam_entries - *free_ent - 1);
595 npc_check_preallocated_entry_cache(struct mbox *mbox, struct roc_npc_flow *flow,
598 struct plt_bitmap *free, *free_rev, *live, *live_rev;
599 uint32_t pos = 0, free_ent = 0, mcam_entries;
600 struct npc_mcam_ents_info *info;
604 info = &npc->flow_entry_info[flow->priority];
606 free_rev = npc->free_entries_rev[flow->priority];
607 free = npc->free_entries[flow->priority];
608 live_rev = npc->live_entries_rev[flow->priority];
609 live = npc->live_entries[flow->priority];
610 mcam_entries = npc->mcam_entries;
612 if (info->free_ent) {
613 rc = plt_bitmap_scan(free, &pos, &slab);
615 /* Get free_ent from free entry bitmap */
616 free_ent = pos + __builtin_ctzll(slab);
617 /* Remove from free bitmaps and add to live ones */
618 plt_bitmap_clear(free, free_ent);
619 plt_bitmap_set(live, free_ent);
620 plt_bitmap_clear(free_rev, mcam_entries - free_ent - 1);
621 plt_bitmap_set(live_rev, mcam_entries - free_ent - 1);
627 return NPC_ERR_INTERNAL;
630 rc = npc_fill_entry_cache(mbox, flow, npc, &free_ent);