1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
9 roc_npc_mcam_free_counter(struct roc_npc *roc_npc, uint16_t ctr_id)
11 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
13 return npc_mcam_free_counter(npc, ctr_id);
17 roc_npc_mcam_read_counter(struct roc_npc *roc_npc, uint32_t ctr_id,
20 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
22 return npc_mcam_read_counter(npc, ctr_id, count);
26 roc_npc_mcam_clear_counter(struct roc_npc *roc_npc, uint32_t ctr_id)
28 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
30 return npc_mcam_clear_counter(npc, ctr_id);
34 roc_npc_mcam_free_entry(struct roc_npc *roc_npc, uint32_t entry)
36 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
38 return npc_mcam_free_entry(npc, entry);
42 roc_npc_mcam_free_all_resources(struct roc_npc *roc_npc)
44 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
46 return npc_flow_free_all_resources(npc);
50 roc_npc_mcam_alloc_entries(struct roc_npc *roc_npc, int ref_entry,
51 int *alloc_entry, int req_count, int priority,
54 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
56 return npc_mcam_alloc_entries(npc, ref_entry, alloc_entry, req_count,
57 priority, resp_count);
61 roc_npc_mcam_alloc_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
62 struct roc_npc_flow *ref_mcam, int prio,
65 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
67 return npc_mcam_alloc_entry(npc, mcam, ref_mcam, prio, resp_count);
71 roc_npc_mcam_ena_dis_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
74 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
76 return npc_mcam_ena_dis_entry(npc, mcam, enable);
80 roc_npc_mcam_write_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam)
82 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
84 return npc_mcam_write_entry(npc, mcam);
88 roc_npc_get_low_priority_mcam(struct roc_npc *roc_npc)
90 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
92 if (roc_model_is_cn10k())
93 return (npc->mcam_entries - NPC_MCAME_RESVD_10XX - 1);
95 return (npc->mcam_entries - NPC_MCAME_RESVD_9XXX - 1);
99 npc_mcam_tot_entries(void)
101 /* FIXME: change to reading in AF from NPC_AF_CONST1/2
102 * MCAM_BANK_DEPTH(_EXT) * MCAM_BANKS
104 if (roc_model_is_cn10k())
105 return 16 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 4096 */
107 return 4 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 1024 */
111 roc_npc_profile_name_get(struct roc_npc *roc_npc)
113 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
115 return (char *)npc->profile_name;
119 roc_npc_init(struct roc_npc *roc_npc)
121 uint8_t *mem = NULL, *nix_mem = NULL, *npc_mem = NULL;
122 struct nix *nix = roc_nix_to_nix_priv(roc_npc->roc_nix);
123 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
128 PLT_STATIC_ASSERT(sizeof(struct npc) <= ROC_NPC_MEM_SZ);
130 memset(npc, 0, sizeof(*npc));
131 npc->mbox = (&nix->dev)->mbox;
132 roc_npc->channel = nix->rx_chan_base;
133 roc_npc->pf_func = (&nix->dev)->pf_func;
134 npc->channel = roc_npc->channel;
135 npc->pf_func = roc_npc->pf_func;
136 npc->flow_max_priority = roc_npc->flow_max_priority;
137 npc->switch_header_type = roc_npc->switch_header_type;
138 npc->flow_prealloc_size = roc_npc->flow_prealloc_size;
140 if (npc->mbox == NULL)
141 return NPC_ERR_PARAM;
143 rc = npc_mcam_fetch_kex_cfg(npc);
147 roc_npc->kex_capability = npc_get_kex_capability(npc);
148 roc_npc->rx_parse_nibble = npc->keyx_supp_nmask[NPC_MCAM_RX];
150 npc->mark_actions = 0;
152 npc->mcam_entries = npc_mcam_tot_entries() >> npc->keyw[NPC_MCAM_RX];
154 /* Free, free_rev, live and live_rev entries */
155 bmap_sz = plt_bitmap_get_memory_footprint(npc->mcam_entries);
156 mem = plt_zmalloc(4 * bmap_sz * npc->flow_max_priority, 0);
158 plt_err("Bmap alloc failed");
163 sz = npc->flow_max_priority * sizeof(struct npc_mcam_ents_info);
164 npc->flow_entry_info = plt_zmalloc(sz, 0);
165 if (npc->flow_entry_info == NULL) {
166 plt_err("flow_entry_info alloc failed");
171 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
172 npc->free_entries = plt_zmalloc(sz, 0);
173 if (npc->free_entries == NULL) {
174 plt_err("free_entries alloc failed");
179 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
180 npc->free_entries_rev = plt_zmalloc(sz, 0);
181 if (npc->free_entries_rev == NULL) {
182 plt_err("free_entries_rev alloc failed");
187 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
188 npc->live_entries = plt_zmalloc(sz, 0);
189 if (npc->live_entries == NULL) {
190 plt_err("live_entries alloc failed");
195 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
196 npc->live_entries_rev = plt_zmalloc(sz, 0);
197 if (npc->live_entries_rev == NULL) {
198 plt_err("live_entries_rev alloc failed");
203 sz = npc->flow_max_priority * sizeof(struct npc_flow_list);
204 npc->flow_list = plt_zmalloc(sz, 0);
205 if (npc->flow_list == NULL) {
206 plt_err("flow_list alloc failed");
212 for (idx = 0; idx < npc->flow_max_priority; idx++) {
213 TAILQ_INIT(&npc->flow_list[idx]);
215 npc->free_entries[idx] =
216 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
219 npc->free_entries_rev[idx] =
220 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
223 npc->live_entries[idx] =
224 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
227 npc->live_entries_rev[idx] =
228 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
231 npc->flow_entry_info[idx].free_ent = 0;
232 npc->flow_entry_info[idx].live_ent = 0;
233 npc->flow_entry_info[idx].max_id = 0;
234 npc->flow_entry_info[idx].min_id = ~(0);
237 npc->rss_grps = NPC_RSS_GRPS;
239 bmap_sz = plt_bitmap_get_memory_footprint(npc->rss_grps);
240 nix_mem = plt_zmalloc(bmap_sz, 0);
241 if (nix_mem == NULL) {
242 plt_err("Bmap alloc failed");
247 npc->rss_grp_entries = plt_bitmap_init(npc->rss_grps, nix_mem, bmap_sz);
249 if (!npc->rss_grp_entries) {
250 plt_err("bitmap init failed");
255 /* Group 0 will be used for RSS,
256 * 1 -7 will be used for npc_flow RSS action
258 plt_bitmap_set(npc->rss_grp_entries, 0);
264 plt_free(npc->flow_list);
265 if (npc->live_entries_rev)
266 plt_free(npc->live_entries_rev);
267 if (npc->live_entries)
268 plt_free(npc->live_entries);
269 if (npc->free_entries_rev)
270 plt_free(npc->free_entries_rev);
271 if (npc->free_entries)
272 plt_free(npc->free_entries);
273 if (npc->flow_entry_info)
274 plt_free(npc->flow_entry_info);
281 roc_npc_fini(struct roc_npc *roc_npc)
283 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
286 rc = npc_flow_free_all_resources(npc);
288 plt_err("Error when deleting NPC MCAM entries, counters");
292 if (npc->flow_list) {
293 plt_free(npc->flow_list);
294 npc->flow_list = NULL;
297 if (npc->live_entries_rev) {
298 plt_free(npc->live_entries_rev);
299 npc->live_entries_rev = NULL;
302 if (npc->live_entries) {
303 plt_free(npc->live_entries);
304 npc->live_entries = NULL;
307 if (npc->free_entries_rev) {
308 plt_free(npc->free_entries_rev);
309 npc->free_entries_rev = NULL;
312 if (npc->free_entries) {
313 plt_free(npc->free_entries);
314 npc->free_entries = NULL;
317 if (npc->flow_entry_info) {
318 plt_free(npc->flow_entry_info);
319 npc->flow_entry_info = NULL;
326 npc_parse_actions(struct npc *npc, const struct roc_npc_attr *attr,
327 const struct roc_npc_action actions[],
328 struct roc_npc_flow *flow)
330 const struct roc_npc_action_mark *act_mark;
331 const struct roc_npc_action_queue *act_q;
332 const struct roc_npc_action_vf *vf_act;
333 int sel_act, req_act = 0;
334 uint16_t pf_func, vf_id;
339 /* Initialize actions */
340 flow->ctr_id = NPC_COUNTER_NONE;
341 pf_func = npc->pf_func;
343 for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
344 switch (actions->type) {
345 case ROC_NPC_ACTION_TYPE_VOID:
347 case ROC_NPC_ACTION_TYPE_MARK:
348 act_mark = (const struct roc_npc_action_mark *)
350 if (act_mark->id > (NPC_FLOW_FLAG_VAL - 2)) {
351 plt_err("mark value must be < 0xfffe");
354 mark = act_mark->id + 1;
355 req_act |= ROC_NPC_ACTION_TYPE_MARK;
356 npc->mark_actions += 1;
359 case ROC_NPC_ACTION_TYPE_FLAG:
360 mark = NPC_FLOW_FLAG_VAL;
361 req_act |= ROC_NPC_ACTION_TYPE_FLAG;
362 npc->mark_actions += 1;
365 case ROC_NPC_ACTION_TYPE_COUNT:
366 /* Indicates, need a counter */
368 req_act |= ROC_NPC_ACTION_TYPE_COUNT;
371 case ROC_NPC_ACTION_TYPE_DROP:
372 req_act |= ROC_NPC_ACTION_TYPE_DROP;
375 case ROC_NPC_ACTION_TYPE_PF:
376 req_act |= ROC_NPC_ACTION_TYPE_PF;
380 case ROC_NPC_ACTION_TYPE_VF:
382 (const struct roc_npc_action_vf *)actions->conf;
383 req_act |= ROC_NPC_ACTION_TYPE_VF;
384 vf_id = vf_act->id & RVU_PFVF_FUNC_MASK;
386 pf_func = (pf_func | (vf_id + 1));
389 case ROC_NPC_ACTION_TYPE_QUEUE:
390 act_q = (const struct roc_npc_action_queue *)
393 req_act |= ROC_NPC_ACTION_TYPE_QUEUE;
396 case ROC_NPC_ACTION_TYPE_RSS:
397 req_act |= ROC_NPC_ACTION_TYPE_RSS;
400 case ROC_NPC_ACTION_TYPE_SEC:
401 /* Assumes user has already configured security
402 * session for this flow. Associated conf is
403 * opaque. When security is implemented,
404 * we need to verify that for specified security
407 * NPC_SECURITY_ACTION_TYPE_INLINE_PROTOCOL &&
408 * session_protocol ==
409 * NPC_SECURITY_PROTOCOL_IPSEC
411 * RSS is not supported with inline ipsec. Get the
412 * rq from associated conf, or make
413 * ROC_NPC_ACTION_TYPE_QUEUE compulsory with this
415 * Currently, rq = 0 is assumed.
417 req_act |= ROC_NPC_ACTION_TYPE_SEC;
421 errcode = NPC_ERR_ACTION_NOTSUP;
426 /* Check if actions specified are compatible */
428 /* Only DROP/COUNT is supported */
429 if (!(req_act & ROC_NPC_ACTION_TYPE_DROP)) {
430 errcode = NPC_ERR_ACTION_NOTSUP;
432 } else if (req_act & ~(ROC_NPC_ACTION_TYPE_DROP |
433 ROC_NPC_ACTION_TYPE_COUNT)) {
434 errcode = NPC_ERR_ACTION_NOTSUP;
437 flow->npc_action = NIX_TX_ACTIONOP_DROP;
441 /* We have already verified the attr, this is ingress.
442 * - Exactly one terminating action is supported
443 * - Exactly one of MARK or FLAG is supported
444 * - If terminating action is DROP, only count is valid.
446 sel_act = req_act & NPC_ACTION_TERM;
447 if ((sel_act & (sel_act - 1)) != 0) {
448 errcode = NPC_ERR_ACTION_NOTSUP;
452 if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
453 sel_act = req_act & ~ROC_NPC_ACTION_TYPE_COUNT;
454 if ((sel_act & (sel_act - 1)) != 0) {
455 errcode = NPC_ERR_ACTION_NOTSUP;
460 if ((req_act & (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) ==
461 (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
462 errcode = NPC_ERR_ACTION_NOTSUP;
466 /* Set NIX_RX_ACTIONOP */
467 if (req_act & (ROC_NPC_ACTION_TYPE_PF | ROC_NPC_ACTION_TYPE_VF)) {
468 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
469 if (req_act & ROC_NPC_ACTION_TYPE_QUEUE)
470 flow->npc_action |= (uint64_t)rq << 20;
471 } else if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
472 flow->npc_action = NIX_RX_ACTIONOP_DROP;
473 } else if (req_act & ROC_NPC_ACTION_TYPE_QUEUE) {
474 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
475 flow->npc_action |= (uint64_t)rq << 20;
476 } else if (req_act & ROC_NPC_ACTION_TYPE_RSS) {
477 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
478 } else if (req_act & ROC_NPC_ACTION_TYPE_SEC) {
479 flow->npc_action = NIX_RX_ACTIONOP_UCAST_IPSEC;
480 flow->npc_action |= (uint64_t)rq << 20;
482 (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
483 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
484 } else if (req_act & ROC_NPC_ACTION_TYPE_COUNT) {
485 /* Keep ROC_NPC_ACTION_TYPE_COUNT_ACT always at the end
486 * This is default action, when user specify only
489 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
491 /* Should never reach here */
492 errcode = NPC_ERR_ACTION_NOTSUP;
497 flow->npc_action |= (uint64_t)mark << 40;
500 /* Ideally AF must ensure that correct pf_func is set */
501 flow->npc_action |= (uint64_t)pf_func << 4;
509 typedef int (*npc_parse_stage_func_t)(struct npc_parse_state *pst);
512 npc_parse_pattern(struct npc *npc, const struct roc_npc_item_info pattern[],
513 struct roc_npc_flow *flow, struct npc_parse_state *pst)
515 npc_parse_stage_func_t parse_stage_funcs[] = {
516 npc_parse_meta_items, npc_parse_cpt_hdr, npc_parse_higig2_hdr,
517 npc_parse_la, npc_parse_lb, npc_parse_lc,
518 npc_parse_ld, npc_parse_le, npc_parse_lf,
519 npc_parse_lg, npc_parse_lh,
526 return NPC_ERR_PARAM;
528 memset(pst, 0, sizeof(*pst));
532 /* Use integral byte offset */
533 key_offset = pst->npc->keyx_len[flow->nix_intf];
534 key_offset = (key_offset + 7) / 8;
536 /* Location where LDATA would begin */
537 pst->mcam_data = (uint8_t *)flow->mcam_data;
538 pst->mcam_mask = (uint8_t *)flow->mcam_mask;
540 while (pattern->type != ROC_NPC_ITEM_TYPE_END &&
541 layer < PLT_DIM(parse_stage_funcs)) {
542 /* Skip place-holders */
543 pattern = npc_parse_skip_void_and_any_items(pattern);
545 pst->pattern = pattern;
546 rc = parse_stage_funcs[layer](pst);
553 * Parse stage function sets pst->pattern to
554 * 1 past the last item it consumed.
556 pattern = pst->pattern;
562 /* Skip trailing place-holders */
563 pattern = npc_parse_skip_void_and_any_items(pattern);
565 /* Are there more items than what we can handle? */
566 if (pattern->type != ROC_NPC_ITEM_TYPE_END)
567 return NPC_ERR_PATTERN_NOTSUP;
573 npc_parse_attr(struct npc *npc, const struct roc_npc_attr *attr,
574 struct roc_npc_flow *flow)
577 return NPC_ERR_PARAM;
578 else if (attr->priority >= npc->flow_max_priority)
579 return NPC_ERR_PARAM;
580 else if ((!attr->egress && !attr->ingress) ||
581 (attr->egress && attr->ingress))
582 return NPC_ERR_PARAM;
585 flow->nix_intf = ROC_NPC_INTF_RX;
587 flow->nix_intf = ROC_NPC_INTF_TX;
589 flow->priority = attr->priority;
594 npc_parse_rule(struct npc *npc, const struct roc_npc_attr *attr,
595 const struct roc_npc_item_info pattern[],
596 const struct roc_npc_action actions[], struct roc_npc_flow *flow,
597 struct npc_parse_state *pst)
602 err = npc_parse_attr(npc, attr, flow);
607 err = npc_parse_pattern(npc, pattern, flow, pst);
612 err = npc_parse_actions(npc, attr, actions, flow);
619 roc_npc_flow_parse(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
620 const struct roc_npc_item_info pattern[],
621 const struct roc_npc_action actions[],
622 struct roc_npc_flow *flow)
624 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
625 struct npc_parse_state parse_state = {0};
628 rc = npc_parse_rule(npc, attr, pattern, actions, flow, &parse_state);
632 parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
634 return npc_program_mcam(npc, &parse_state, 0);
637 struct roc_npc_flow *
638 roc_npc_flow_create(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
639 const struct roc_npc_item_info pattern[],
640 const struct roc_npc_action actions[], int *errcode)
642 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
643 struct roc_npc_flow *flow, *flow_iter;
644 struct npc_parse_state parse_state;
645 struct npc_flow_list *list;
648 flow = plt_zmalloc(sizeof(*flow), 0);
650 *errcode = NPC_ERR_NO_MEM;
653 memset(flow, 0, sizeof(*flow));
655 rc = npc_parse_rule(npc, attr, pattern, actions, flow, &parse_state);
661 parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
663 rc = npc_program_mcam(npc, &parse_state, 1);
669 list = &npc->flow_list[flow->priority];
670 /* List in ascending order of mcam entries */
671 TAILQ_FOREACH(flow_iter, list, next) {
672 if (flow_iter->mcam_id > flow->mcam_id) {
673 TAILQ_INSERT_BEFORE(flow_iter, flow, next);
678 TAILQ_INSERT_TAIL(list, flow, next);
687 roc_npc_flow_destroy(struct roc_npc *roc_npc, struct roc_npc_flow *flow)
689 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
690 struct plt_bitmap *bmap;
694 match_id = (flow->npc_action >> NPC_RX_ACT_MATCH_OFFSET) &
695 NPC_RX_ACT_MATCH_MASK;
697 if (match_id && match_id < NPC_ACTION_FLAG_DEFAULT) {
698 if (npc->mark_actions == 0)
699 return NPC_ERR_PARAM;
702 rc = npc_mcam_free_entry(npc, flow->mcam_id);
706 TAILQ_REMOVE(&npc->flow_list[flow->priority], flow, next);
708 bmap = npc->live_entries[flow->priority];
709 plt_bitmap_clear(bmap, flow->mcam_id);