1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
9 roc_npc_vtag_actions_get(struct roc_npc *roc_npc)
11 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
13 return npc->vtag_strip_actions;
17 roc_npc_vtag_actions_sub_return(struct roc_npc *roc_npc, uint32_t count)
19 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
21 npc->vtag_strip_actions -= count;
22 return npc->vtag_strip_actions;
26 roc_npc_mcam_free_counter(struct roc_npc *roc_npc, uint16_t ctr_id)
28 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
30 return npc_mcam_free_counter(npc, ctr_id);
34 roc_npc_mcam_read_counter(struct roc_npc *roc_npc, uint32_t ctr_id,
37 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
39 return npc_mcam_read_counter(npc, ctr_id, count);
43 roc_npc_mcam_clear_counter(struct roc_npc *roc_npc, uint32_t ctr_id)
45 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
47 return npc_mcam_clear_counter(npc, ctr_id);
51 roc_npc_mcam_free_entry(struct roc_npc *roc_npc, uint32_t entry)
53 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
55 return npc_mcam_free_entry(npc, entry);
59 roc_npc_mcam_free_all_resources(struct roc_npc *roc_npc)
61 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
63 return npc_flow_free_all_resources(npc);
67 roc_npc_mcam_alloc_entries(struct roc_npc *roc_npc, int ref_entry,
68 int *alloc_entry, int req_count, int priority,
71 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
73 return npc_mcam_alloc_entries(npc, ref_entry, alloc_entry, req_count,
74 priority, resp_count);
78 roc_npc_mcam_alloc_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
79 struct roc_npc_flow *ref_mcam, int prio,
82 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
84 return npc_mcam_alloc_entry(npc, mcam, ref_mcam, prio, resp_count);
88 roc_npc_mcam_ena_dis_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam,
91 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
93 return npc_mcam_ena_dis_entry(npc, mcam, enable);
97 roc_npc_mcam_write_entry(struct roc_npc *roc_npc, struct roc_npc_flow *mcam)
99 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
101 return npc_mcam_write_entry(npc, mcam);
105 roc_npc_get_low_priority_mcam(struct roc_npc *roc_npc)
107 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
109 if (roc_model_is_cn10k())
110 return (npc->mcam_entries - NPC_MCAME_RESVD_10XX - 1);
111 else if (roc_model_is_cn98xx())
112 return (npc->mcam_entries - NPC_MCAME_RESVD_98XX - 1);
114 return (npc->mcam_entries - NPC_MCAME_RESVD_9XXX - 1);
118 npc_mcam_tot_entries(void)
120 /* FIXME: change to reading in AF from NPC_AF_CONST1/2
121 * MCAM_BANK_DEPTH(_EXT) * MCAM_BANKS
123 if (roc_model_is_cn10k() || roc_model_is_cn98xx())
124 return 16 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 4096 */
126 return 4 * 1024; /* MCAM_BANKS = 4, BANK_DEPTH_EXT = 1024 */
130 roc_npc_profile_name_get(struct roc_npc *roc_npc)
132 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
134 return (char *)npc->profile_name;
138 roc_npc_init(struct roc_npc *roc_npc)
140 uint8_t *mem = NULL, *nix_mem = NULL, *npc_mem = NULL;
141 struct nix *nix = roc_nix_to_nix_priv(roc_npc->roc_nix);
142 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
147 PLT_STATIC_ASSERT(sizeof(struct npc) <= ROC_NPC_MEM_SZ);
149 memset(npc, 0, sizeof(*npc));
150 npc->mbox = (&nix->dev)->mbox;
151 roc_npc->channel = nix->rx_chan_base;
152 roc_npc->pf_func = (&nix->dev)->pf_func;
153 npc->channel = roc_npc->channel;
154 npc->pf_func = roc_npc->pf_func;
155 npc->flow_max_priority = roc_npc->flow_max_priority;
156 npc->switch_header_type = roc_npc->switch_header_type;
157 npc->flow_prealloc_size = roc_npc->flow_prealloc_size;
159 if (npc->mbox == NULL)
160 return NPC_ERR_PARAM;
162 rc = npc_mcam_fetch_kex_cfg(npc);
166 roc_npc->kex_capability = npc_get_kex_capability(npc);
167 roc_npc->rx_parse_nibble = npc->keyx_supp_nmask[NPC_MCAM_RX];
169 npc->mark_actions = 0;
171 npc->mcam_entries = npc_mcam_tot_entries() >> npc->keyw[NPC_MCAM_RX];
173 /* Free, free_rev, live and live_rev entries */
174 bmap_sz = plt_bitmap_get_memory_footprint(npc->mcam_entries);
175 mem = plt_zmalloc(4 * bmap_sz * npc->flow_max_priority, 0);
177 plt_err("Bmap alloc failed");
182 sz = npc->flow_max_priority * sizeof(struct npc_mcam_ents_info);
183 npc->flow_entry_info = plt_zmalloc(sz, 0);
184 if (npc->flow_entry_info == NULL) {
185 plt_err("flow_entry_info alloc failed");
190 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
191 npc->free_entries = plt_zmalloc(sz, 0);
192 if (npc->free_entries == NULL) {
193 plt_err("free_entries alloc failed");
198 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
199 npc->free_entries_rev = plt_zmalloc(sz, 0);
200 if (npc->free_entries_rev == NULL) {
201 plt_err("free_entries_rev alloc failed");
206 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
207 npc->live_entries = plt_zmalloc(sz, 0);
208 if (npc->live_entries == NULL) {
209 plt_err("live_entries alloc failed");
214 sz = npc->flow_max_priority * sizeof(struct plt_bitmap *);
215 npc->live_entries_rev = plt_zmalloc(sz, 0);
216 if (npc->live_entries_rev == NULL) {
217 plt_err("live_entries_rev alloc failed");
222 sz = npc->flow_max_priority * sizeof(struct npc_flow_list);
223 npc->flow_list = plt_zmalloc(sz, 0);
224 if (npc->flow_list == NULL) {
225 plt_err("flow_list alloc failed");
231 for (idx = 0; idx < npc->flow_max_priority; idx++) {
232 TAILQ_INIT(&npc->flow_list[idx]);
234 npc->free_entries[idx] =
235 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
238 npc->free_entries_rev[idx] =
239 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
242 npc->live_entries[idx] =
243 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
246 npc->live_entries_rev[idx] =
247 plt_bitmap_init(npc->mcam_entries, mem, bmap_sz);
250 npc->flow_entry_info[idx].free_ent = 0;
251 npc->flow_entry_info[idx].live_ent = 0;
252 npc->flow_entry_info[idx].max_id = 0;
253 npc->flow_entry_info[idx].min_id = ~(0);
256 npc->rss_grps = NPC_RSS_GRPS;
258 bmap_sz = plt_bitmap_get_memory_footprint(npc->rss_grps);
259 nix_mem = plt_zmalloc(bmap_sz, 0);
260 if (nix_mem == NULL) {
261 plt_err("Bmap alloc failed");
266 npc->rss_grp_entries = plt_bitmap_init(npc->rss_grps, nix_mem, bmap_sz);
268 if (!npc->rss_grp_entries) {
269 plt_err("bitmap init failed");
274 /* Group 0 will be used for RSS,
275 * 1 -7 will be used for npc_flow RSS action
277 plt_bitmap_set(npc->rss_grp_entries, 0);
283 plt_free(npc->flow_list);
284 if (npc->live_entries_rev)
285 plt_free(npc->live_entries_rev);
286 if (npc->live_entries)
287 plt_free(npc->live_entries);
288 if (npc->free_entries_rev)
289 plt_free(npc->free_entries_rev);
290 if (npc->free_entries)
291 plt_free(npc->free_entries);
292 if (npc->flow_entry_info)
293 plt_free(npc->flow_entry_info);
300 roc_npc_fini(struct roc_npc *roc_npc)
302 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
305 rc = npc_flow_free_all_resources(npc);
307 plt_err("Error when deleting NPC MCAM entries, counters");
311 if (npc->flow_list) {
312 plt_free(npc->flow_list);
313 npc->flow_list = NULL;
316 if (npc->live_entries_rev) {
317 plt_free(npc->live_entries_rev);
318 npc->live_entries_rev = NULL;
321 if (npc->live_entries) {
322 plt_free(npc->live_entries);
323 npc->live_entries = NULL;
326 if (npc->free_entries_rev) {
327 plt_free(npc->free_entries_rev);
328 npc->free_entries_rev = NULL;
331 if (npc->free_entries) {
332 plt_free(npc->free_entries);
333 npc->free_entries = NULL;
336 if (npc->flow_entry_info) {
337 plt_free(npc->flow_entry_info);
338 npc->flow_entry_info = NULL;
345 npc_parse_actions(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
346 const struct roc_npc_action actions[],
347 struct roc_npc_flow *flow)
349 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
350 const struct roc_npc_action_mark *act_mark;
351 const struct roc_npc_action_meter *act_mtr;
352 const struct roc_npc_action_queue *act_q;
353 const struct roc_npc_action_vf *vf_act;
354 bool vlan_insert_action = false;
355 int sel_act, req_act = 0;
356 uint16_t pf_func, vf_id;
361 /* Initialize actions */
362 flow->ctr_id = NPC_COUNTER_NONE;
363 pf_func = npc->pf_func;
365 for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
366 switch (actions->type) {
367 case ROC_NPC_ACTION_TYPE_VOID:
369 case ROC_NPC_ACTION_TYPE_MARK:
370 act_mark = (const struct roc_npc_action_mark *)
372 if (act_mark->id > (NPC_FLOW_FLAG_VAL - 2)) {
373 plt_err("mark value must be < 0xfffe");
376 mark = act_mark->id + 1;
377 req_act |= ROC_NPC_ACTION_TYPE_MARK;
378 npc->mark_actions += 1;
381 case ROC_NPC_ACTION_TYPE_FLAG:
382 mark = NPC_FLOW_FLAG_VAL;
383 req_act |= ROC_NPC_ACTION_TYPE_FLAG;
384 npc->mark_actions += 1;
387 case ROC_NPC_ACTION_TYPE_COUNT:
388 /* Indicates, need a counter */
390 req_act |= ROC_NPC_ACTION_TYPE_COUNT;
393 case ROC_NPC_ACTION_TYPE_DROP:
394 req_act |= ROC_NPC_ACTION_TYPE_DROP;
397 case ROC_NPC_ACTION_TYPE_PF:
398 req_act |= ROC_NPC_ACTION_TYPE_PF;
402 case ROC_NPC_ACTION_TYPE_VF:
404 (const struct roc_npc_action_vf *)actions->conf;
405 req_act |= ROC_NPC_ACTION_TYPE_VF;
406 vf_id = vf_act->id & RVU_PFVF_FUNC_MASK;
408 pf_func = (pf_func | (vf_id + 1));
411 case ROC_NPC_ACTION_TYPE_QUEUE:
412 act_q = (const struct roc_npc_action_queue *)
415 req_act |= ROC_NPC_ACTION_TYPE_QUEUE;
418 case ROC_NPC_ACTION_TYPE_RSS:
419 req_act |= ROC_NPC_ACTION_TYPE_RSS;
422 case ROC_NPC_ACTION_TYPE_SEC:
423 /* Assumes user has already configured security
424 * session for this flow. Associated conf is
425 * opaque. When security is implemented,
426 * we need to verify that for specified security
429 * NPC_SECURITY_ACTION_TYPE_INLINE_PROTOCOL &&
430 * session_protocol ==
431 * NPC_SECURITY_PROTOCOL_IPSEC
433 req_act |= ROC_NPC_ACTION_TYPE_SEC;
436 /* Special processing when with inline device */
437 if (roc_nix_inb_is_with_inl_dev(roc_npc->roc_nix) &&
438 roc_nix_inl_dev_is_probed()) {
440 pf_func = nix_inl_dev_pffunc_get();
443 case ROC_NPC_ACTION_TYPE_VLAN_STRIP:
444 req_act |= ROC_NPC_ACTION_TYPE_VLAN_STRIP;
446 case ROC_NPC_ACTION_TYPE_VLAN_INSERT:
447 req_act |= ROC_NPC_ACTION_TYPE_VLAN_INSERT;
449 case ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT:
450 req_act |= ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT;
452 case ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT:
453 req_act |= ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT;
455 case ROC_NPC_ACTION_TYPE_METER:
456 act_mtr = (const struct roc_npc_action_meter *)
458 flow->mtr_id = act_mtr->mtr_id;
459 req_act |= ROC_NPC_ACTION_TYPE_METER;
462 errcode = NPC_ERR_ACTION_NOTSUP;
467 if (req_act & (ROC_NPC_ACTION_TYPE_VLAN_INSERT |
468 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
469 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT))
470 vlan_insert_action = true;
472 if ((req_act & (ROC_NPC_ACTION_TYPE_VLAN_INSERT |
473 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
474 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT)) ==
475 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
476 plt_err("PCP insert action can't be supported alone");
477 errcode = NPC_ERR_ACTION_NOTSUP;
481 /* Both STRIP and INSERT actions are not supported */
482 if (vlan_insert_action && (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP)) {
483 errcode = NPC_ERR_ACTION_NOTSUP;
487 /* Check if actions specified are compatible */
489 if (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
490 plt_err("VLAN pop action is not supported on Egress");
491 errcode = NPC_ERR_ACTION_NOTSUP;
496 ~(ROC_NPC_ACTION_TYPE_VLAN_INSERT |
497 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT |
498 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT |
499 ROC_NPC_ACTION_TYPE_DROP | ROC_NPC_ACTION_TYPE_COUNT)) {
500 plt_err("Only VLAN insert, drop, count supported on Egress");
501 errcode = NPC_ERR_ACTION_NOTSUP;
505 if (vlan_insert_action &&
506 (req_act & ROC_NPC_ACTION_TYPE_DROP)) {
507 plt_err("Both VLAN insert and drop actions cannot be supported");
508 errcode = NPC_ERR_ACTION_NOTSUP;
512 if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
513 flow->npc_action = NIX_TX_ACTIONOP_DROP;
514 } else if ((req_act & ROC_NPC_ACTION_TYPE_COUNT) ||
515 vlan_insert_action) {
516 flow->npc_action = NIX_TX_ACTIONOP_UCAST_DEFAULT;
518 plt_err("Unsupported action for egress");
519 errcode = NPC_ERR_ACTION_NOTSUP;
525 if (vlan_insert_action) {
526 errcode = NPC_ERR_ACTION_NOTSUP;
531 /* We have already verified the attr, this is ingress.
532 * - Exactly one terminating action is supported
533 * - Exactly one of MARK or FLAG is supported
534 * - If terminating action is DROP, only count is valid.
536 sel_act = req_act & NPC_ACTION_TERM;
537 if ((sel_act & (sel_act - 1)) != 0) {
538 errcode = NPC_ERR_ACTION_NOTSUP;
542 if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
543 sel_act = req_act & ~ROC_NPC_ACTION_TYPE_COUNT;
544 if ((sel_act & (sel_act - 1)) != 0) {
545 errcode = NPC_ERR_ACTION_NOTSUP;
550 if ((req_act & (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) ==
551 (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
552 errcode = NPC_ERR_ACTION_NOTSUP;
556 if (req_act & ROC_NPC_ACTION_TYPE_VLAN_STRIP)
557 npc->vtag_strip_actions++;
559 /* Set NIX_RX_ACTIONOP */
560 if (req_act == ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
561 /* Only VLAN action is provided */
562 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
564 (ROC_NPC_ACTION_TYPE_PF | ROC_NPC_ACTION_TYPE_VF)) {
565 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
566 if (req_act & ROC_NPC_ACTION_TYPE_QUEUE)
567 flow->npc_action |= (uint64_t)rq << 20;
568 } else if (req_act & ROC_NPC_ACTION_TYPE_DROP) {
569 flow->npc_action = NIX_RX_ACTIONOP_DROP;
570 } else if (req_act & ROC_NPC_ACTION_TYPE_QUEUE) {
571 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
572 flow->npc_action |= (uint64_t)rq << 20;
573 } else if (req_act & ROC_NPC_ACTION_TYPE_RSS) {
574 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
575 } else if (req_act & ROC_NPC_ACTION_TYPE_SEC) {
576 flow->npc_action = NIX_RX_ACTIONOP_UCAST_IPSEC;
577 flow->npc_action |= (uint64_t)rq << 20;
579 (ROC_NPC_ACTION_TYPE_FLAG | ROC_NPC_ACTION_TYPE_MARK)) {
580 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
581 } else if (req_act & ROC_NPC_ACTION_TYPE_COUNT) {
582 /* Keep ROC_NPC_ACTION_TYPE_COUNT_ACT always at the end
583 * This is default action, when user specify only
586 flow->npc_action = NIX_RX_ACTIONOP_UCAST;
588 /* Should never reach here */
589 errcode = NPC_ERR_ACTION_NOTSUP;
594 flow->npc_action |= (uint64_t)mark << 40;
597 /* Ideally AF must ensure that correct pf_func is set */
598 flow->npc_action |= (uint64_t)pf_func << 4;
606 typedef int (*npc_parse_stage_func_t)(struct npc_parse_state *pst);
609 npc_parse_pattern(struct npc *npc, const struct roc_npc_item_info pattern[],
610 struct roc_npc_flow *flow, struct npc_parse_state *pst)
612 npc_parse_stage_func_t parse_stage_funcs[] = {
613 npc_parse_meta_items, npc_parse_cpt_hdr, npc_parse_higig2_hdr,
614 npc_parse_la, npc_parse_lb, npc_parse_lc,
615 npc_parse_ld, npc_parse_le, npc_parse_lf,
616 npc_parse_lg, npc_parse_lh,
623 return NPC_ERR_PARAM;
625 memset(pst, 0, sizeof(*pst));
629 /* Use integral byte offset */
630 key_offset = pst->npc->keyx_len[flow->nix_intf];
631 key_offset = (key_offset + 7) / 8;
633 /* Location where LDATA would begin */
634 pst->mcam_data = (uint8_t *)flow->mcam_data;
635 pst->mcam_mask = (uint8_t *)flow->mcam_mask;
637 while (pattern->type != ROC_NPC_ITEM_TYPE_END &&
638 layer < PLT_DIM(parse_stage_funcs)) {
639 /* Skip place-holders */
640 pattern = npc_parse_skip_void_and_any_items(pattern);
642 pst->pattern = pattern;
643 rc = parse_stage_funcs[layer](pst);
650 * Parse stage function sets pst->pattern to
651 * 1 past the last item it consumed.
653 pattern = pst->pattern;
659 /* Skip trailing place-holders */
660 pattern = npc_parse_skip_void_and_any_items(pattern);
662 /* Are there more items than what we can handle? */
663 if (pattern->type != ROC_NPC_ITEM_TYPE_END)
664 return NPC_ERR_PATTERN_NOTSUP;
670 npc_parse_attr(struct npc *npc, const struct roc_npc_attr *attr,
671 struct roc_npc_flow *flow)
674 return NPC_ERR_PARAM;
675 else if (attr->priority >= npc->flow_max_priority)
676 return NPC_ERR_PARAM;
677 else if ((!attr->egress && !attr->ingress) ||
678 (attr->egress && attr->ingress))
679 return NPC_ERR_PARAM;
682 flow->nix_intf = ROC_NPC_INTF_RX;
684 flow->nix_intf = ROC_NPC_INTF_TX;
686 flow->priority = attr->priority;
691 npc_parse_rule(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
692 const struct roc_npc_item_info pattern[],
693 const struct roc_npc_action actions[], struct roc_npc_flow *flow,
694 struct npc_parse_state *pst)
696 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
700 err = npc_parse_attr(npc, attr, flow);
705 err = npc_parse_pattern(npc, pattern, flow, pst);
710 err = npc_parse_actions(roc_npc, attr, actions, flow);
717 roc_npc_flow_parse(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
718 const struct roc_npc_item_info pattern[],
719 const struct roc_npc_action actions[],
720 struct roc_npc_flow *flow)
722 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
723 struct npc_parse_state parse_state = {0};
726 rc = npc_parse_rule(roc_npc, attr, pattern, actions, flow,
731 parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
733 return npc_program_mcam(npc, &parse_state, 0);
737 npc_rss_free_grp_get(struct npc *npc, uint32_t *pos)
739 struct plt_bitmap *bmap = npc->rss_grp_entries;
741 for (*pos = 0; *pos < ROC_NIX_RSS_GRPS; ++*pos) {
742 if (!plt_bitmap_get(bmap, *pos))
745 return *pos < ROC_NIX_RSS_GRPS ? 0 : -1;
749 npc_rss_action_configure(struct roc_npc *roc_npc,
750 const struct roc_npc_action_rss *rss, uint8_t *alg_idx,
751 uint32_t *rss_grp, uint32_t mcam_id)
753 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
754 struct roc_nix *roc_nix = roc_npc->roc_nix;
755 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
756 uint32_t flowkey_cfg, rss_grp_idx, i, rem;
757 uint8_t key[ROC_NIX_RSS_KEY_LEN];
758 const uint8_t *key_ptr;
759 uint8_t flowkey_algx;
763 rc = npc_rss_free_grp_get(npc, &rss_grp_idx);
764 /* RSS group :0 is not usable for flow rss action */
765 if (rc < 0 || rss_grp_idx == 0)
768 for (i = 0; i < rss->queue_num; i++) {
769 if (rss->queue[i] >= nix->nb_rx_queues) {
770 plt_err("queue id > max number of queues");
775 *rss_grp = rss_grp_idx;
777 if (rss->key == NULL) {
778 roc_nix_rss_key_default_fill(roc_nix, key);
784 roc_nix_rss_key_set(roc_nix, key_ptr);
786 /* If queue count passed in the rss action is less than
787 * HW configured reta size, replicate rss action reta
788 * across HW reta table.
790 reta = nix->reta[rss_grp_idx];
792 if (rss->queue_num > nix->reta_sz) {
793 plt_err("too many queues for RSS context");
797 for (i = 0; i < (nix->reta_sz / rss->queue_num); i++)
798 memcpy(reta + i * rss->queue_num, rss->queue,
799 sizeof(uint16_t) * rss->queue_num);
801 rem = nix->reta_sz % rss->queue_num;
803 memcpy(&reta[i * rss->queue_num], rss->queue,
804 rem * sizeof(uint16_t));
806 rc = roc_nix_rss_reta_set(roc_nix, *rss_grp, reta);
808 plt_err("Failed to init rss table rc = %d", rc);
812 flowkey_cfg = roc_npc->flowkey_cfg_state;
814 rc = roc_nix_rss_flowkey_set(roc_nix, &flowkey_algx, flowkey_cfg,
817 plt_err("Failed to set rss hash function rc = %d", rc);
821 *alg_idx = flowkey_algx;
823 plt_bitmap_set(npc->rss_grp_entries, *rss_grp);
829 npc_rss_action_program(struct roc_npc *roc_npc,
830 const struct roc_npc_action actions[],
831 struct roc_npc_flow *flow)
833 const struct roc_npc_action_rss *rss;
838 for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
839 if (actions->type == ROC_NPC_ACTION_TYPE_RSS) {
840 rss = (const struct roc_npc_action_rss *)actions->conf;
841 rc = npc_rss_action_configure(roc_npc, rss, &alg_idx,
842 &rss_grp, flow->mcam_id);
846 flow->npc_action &= (~(0xfULL));
847 flow->npc_action |= NIX_RX_ACTIONOP_RSS;
849 ((uint64_t)(alg_idx & NPC_RSS_ACT_ALG_MASK)
850 << NPC_RSS_ACT_ALG_OFFSET) |
851 ((uint64_t)(rss_grp & NPC_RSS_ACT_GRP_MASK)
852 << NPC_RSS_ACT_GRP_OFFSET);
860 roc_npc_mark_actions_get(struct roc_npc *roc_npc)
862 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
864 return npc->mark_actions;
868 roc_npc_mark_actions_sub_return(struct roc_npc *roc_npc, uint32_t count)
870 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
872 npc->mark_actions -= count;
873 return npc->mark_actions;
877 npc_vtag_cfg_delete(struct roc_npc *roc_npc, struct roc_npc_flow *flow)
879 struct roc_nix *roc_nix = roc_npc->roc_nix;
880 struct nix_vtag_config *vtag_cfg;
881 struct nix_vtag_config_rsp *rsp;
888 struct nix_tx_vtag_action_s act;
891 nix = roc_nix_to_nix_priv(roc_nix);
892 mbox = (&nix->dev)->mbox;
894 tx_vtag_action.reg = flow->vtag_action;
895 vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
897 if (vtag_cfg == NULL)
900 vtag_cfg->cfg_type = VTAG_TX;
901 vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
902 vtag_cfg->tx.vtag0_idx = tx_vtag_action.act.vtag0_def;
903 vtag_cfg->tx.free_vtag0 = true;
905 if (flow->vtag_insert_count == 2) {
906 vtag_cfg->tx.vtag1_idx = tx_vtag_action.act.vtag1_def;
907 vtag_cfg->tx.free_vtag1 = true;
910 rc = mbox_process_msg(mbox, (void *)&rsp);
918 npc_vtag_insert_action_parse(const struct roc_npc_action actions[],
919 struct roc_npc_flow *flow,
920 struct npc_action_vtag_info *vlan_info,
923 bool vlan_id_found = false, ethtype_found = false, pcp_found = false;
928 /* This function parses parameters of one VLAN. When a parameter is
929 * found repeated, it treats it as the end of first VLAN's parameters
930 * and returns. The caller calls again to parse the parameters of the
934 for (; count < NPC_ACTION_MAX_VLAN_PARAMS; count++, actions++) {
935 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_INSERT) {
939 const struct roc_npc_action_of_set_vlan_vid *vtag =
940 (const struct roc_npc_action_of_set_vlan_vid *)
943 vlan_info->vlan_id = plt_be_to_cpu_16(vtag->vlan_vid);
945 if (vlan_info->vlan_id > 0xfff) {
946 plt_err("Invalid vlan_id for set vlan action");
950 flow->vtag_insert_enabled = true;
952 vlan_id_found = true;
953 } else if (actions->type ==
954 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT) {
958 const struct roc_npc_action_of_push_vlan *ethtype =
959 (const struct roc_npc_action_of_push_vlan *)
961 vlan_info->vlan_ethtype =
962 plt_be_to_cpu_16(ethtype->ethertype);
963 if (vlan_info->vlan_ethtype != ROC_ETHER_TYPE_VLAN &&
964 vlan_info->vlan_ethtype != ROC_ETHER_TYPE_QINQ) {
965 plt_err("Invalid ethtype specified for push"
969 flow->vtag_insert_enabled = true;
971 ethtype_found = true;
972 } else if (actions->type ==
973 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
976 const struct roc_npc_action_of_set_vlan_pcp *pcp =
977 (const struct roc_npc_action_of_set_vlan_pcp *)
979 vlan_info->vlan_pcp = pcp->vlan_pcp;
980 if (vlan_info->vlan_pcp > 0x7) {
981 plt_err("Invalid PCP value for pcp action");
984 flow->vtag_insert_enabled = true;
996 npc_vtag_insert_action_configure(struct mbox *mbox, struct roc_npc_flow *flow,
997 struct npc_action_vtag_info *vlan_info)
999 struct nix_vtag_config *vtag_cfg;
1000 struct nix_vtag_config_rsp *rsp;
1005 struct nix_tx_vtag_action_s act;
1008 vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
1010 if (vtag_cfg == NULL)
1013 vtag_cfg->cfg_type = VTAG_TX;
1014 vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
1015 vtag_cfg->tx.vtag0 =
1016 ((vlan_info[0].vlan_ethtype << 16) |
1017 (vlan_info[0].vlan_pcp << 13) | vlan_info[0].vlan_id);
1019 vtag_cfg->tx.cfg_vtag0 = 1;
1021 if (flow->vtag_insert_count == 2) {
1022 vtag_cfg->tx.vtag1 =
1023 ((vlan_info[1].vlan_ethtype << 16) |
1024 (vlan_info[1].vlan_pcp << 13) | vlan_info[1].vlan_id);
1026 vtag_cfg->tx.cfg_vtag1 = 1;
1029 rc = mbox_process_msg(mbox, (void *)&rsp);
1033 if (rsp->vtag0_idx < 0 ||
1034 ((flow->vtag_insert_count == 2) && (rsp->vtag1_idx < 0))) {
1035 plt_err("Failed to config TX VTAG action");
1039 tx_vtag_action.reg = 0;
1040 tx_vtag_action.act.vtag0_def = rsp->vtag0_idx;
1041 tx_vtag_action.act.vtag0_lid = NPC_LID_LA;
1042 tx_vtag_action.act.vtag0_op = NIX_TX_VTAGOP_INSERT;
1043 tx_vtag_action.act.vtag0_relptr = NIX_TX_VTAGACTION_VTAG0_RELPTR;
1045 if (flow->vtag_insert_count == 2) {
1046 tx_vtag_action.act.vtag1_def = rsp->vtag1_idx;
1047 tx_vtag_action.act.vtag1_lid = NPC_LID_LA;
1048 tx_vtag_action.act.vtag1_op = NIX_TX_VTAGOP_INSERT;
1049 /* NIX_TX_VTAG_ACTION_S
1050 * If Vtag 0 is inserted, hardware adjusts the Vtag 1 byte
1051 * offset accordingly. Thus, if the two offsets are equal in
1052 * the structure, hardware inserts Vtag 1 immediately after
1053 * Vtag 0 in the packet.
1055 tx_vtag_action.act.vtag1_relptr =
1056 NIX_TX_VTAGACTION_VTAG0_RELPTR;
1059 flow->vtag_action = tx_vtag_action.reg;
1065 npc_vtag_strip_action_configure(struct mbox *mbox,
1066 const struct roc_npc_action actions[],
1067 struct roc_npc_flow *flow, int *strip_cnt)
1069 struct nix_vtag_config *vtag_cfg;
1070 uint64_t rx_vtag_action = 0;
1071 int count = 0, rc = 0;
1075 for (; count < NPC_ACTION_MAX_VLANS_STRIPPED; count++, actions++) {
1076 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_STRIP)
1080 vtag_cfg = mbox_alloc_msg_nix_vtag_cfg(mbox);
1082 if (vtag_cfg == NULL)
1085 vtag_cfg->cfg_type = VTAG_RX;
1086 vtag_cfg->rx.strip_vtag = 1;
1087 /* Always capture */
1088 vtag_cfg->rx.capture_vtag = 1;
1089 vtag_cfg->vtag_size = NIX_VTAGSIZE_T4;
1090 vtag_cfg->rx.vtag_type = 0;
1092 rc = mbox_process(mbox);
1096 rx_vtag_action |= (NIX_RX_VTAGACTION_VTAG_VALID << 15);
1097 rx_vtag_action |= ((uint64_t)NPC_LID_LB << 8);
1098 rx_vtag_action |= NIX_RX_VTAGACTION_VTAG0_RELPTR;
1100 if (*strip_cnt == 2) {
1101 rx_vtag_action |= (NIX_RX_VTAGACTION_VTAG_VALID << 47);
1102 rx_vtag_action |= ((uint64_t)NPC_LID_LB << 40);
1103 rx_vtag_action |= NIX_RX_VTAGACTION_VTAG0_RELPTR << 32;
1105 flow->vtag_action = rx_vtag_action;
1111 npc_vtag_action_program(struct roc_npc *roc_npc,
1112 const struct roc_npc_action actions[],
1113 struct roc_npc_flow *flow)
1115 bool vlan_strip_parsed = false, vlan_insert_parsed = false;
1116 const struct roc_npc_action *insert_actions;
1117 struct roc_nix *roc_nix = roc_npc->roc_nix;
1118 struct npc_action_vtag_info vlan_info[2];
1119 int parsed_cnt = 0, strip_cnt = 0;
1120 int tot_vlan_params = 0;
1125 nix = roc_nix_to_nix_priv(roc_nix);
1126 mbox = (&nix->dev)->mbox;
1128 memset(vlan_info, 0, sizeof(vlan_info));
1130 flow->vtag_insert_enabled = false;
1132 for (; actions->type != ROC_NPC_ACTION_TYPE_END; actions++) {
1133 if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_STRIP) {
1134 if (vlan_strip_parsed) {
1135 plt_err("Incorrect VLAN strip actions");
1138 rc = npc_vtag_strip_action_configure(mbox, actions,
1146 vlan_strip_parsed = true;
1147 } else if (actions->type == ROC_NPC_ACTION_TYPE_VLAN_INSERT ||
1149 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT ||
1151 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT) {
1152 if (vlan_insert_parsed) {
1153 plt_err("Incorrect VLAN insert actions");
1157 insert_actions = actions;
1159 for (i = 0; i < 2; i++) {
1160 rc = npc_vtag_insert_action_parse(
1161 insert_actions, flow, &vlan_info[i],
1168 insert_actions += parsed_cnt;
1169 tot_vlan_params += parsed_cnt;
1170 flow->vtag_insert_count++;
1173 actions += tot_vlan_params - 1;
1174 vlan_insert_parsed = true;
1178 if (flow->vtag_insert_enabled) {
1179 rc = npc_vtag_insert_action_configure(mbox, flow, vlan_info);
1187 struct roc_npc_flow *
1188 roc_npc_flow_create(struct roc_npc *roc_npc, const struct roc_npc_attr *attr,
1189 const struct roc_npc_item_info pattern[],
1190 const struct roc_npc_action actions[], int *errcode)
1192 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1193 struct roc_npc_flow *flow, *flow_iter;
1194 struct npc_parse_state parse_state;
1195 struct npc_flow_list *list;
1198 npc->channel = roc_npc->channel;
1200 flow = plt_zmalloc(sizeof(*flow), 0);
1202 *errcode = NPC_ERR_NO_MEM;
1205 memset(flow, 0, sizeof(*flow));
1207 rc = npc_parse_rule(roc_npc, attr, pattern, actions, flow,
1214 rc = npc_vtag_action_program(roc_npc, actions, flow);
1220 parse_state.is_vf = !roc_nix_is_pf(roc_npc->roc_nix);
1222 rc = npc_program_mcam(npc, &parse_state, 1);
1228 rc = npc_rss_action_program(roc_npc, actions, flow);
1231 goto set_rss_failed;
1234 list = &npc->flow_list[flow->priority];
1235 /* List in ascending order of mcam entries */
1236 TAILQ_FOREACH(flow_iter, list, next) {
1237 if (flow_iter->mcam_id > flow->mcam_id) {
1238 TAILQ_INSERT_BEFORE(flow_iter, flow, next);
1243 TAILQ_INSERT_TAIL(list, flow, next);
1247 rc = npc_mcam_free_entry(npc, flow->mcam_id);
1259 npc_rss_group_free(struct npc *npc, struct roc_npc_flow *flow)
1263 if ((flow->npc_action & 0xF) == NIX_RX_ACTIONOP_RSS) {
1264 rss_grp = (flow->npc_action >> NPC_RSS_ACT_GRP_OFFSET) &
1265 NPC_RSS_ACT_GRP_MASK;
1266 if (rss_grp == 0 || rss_grp >= npc->rss_grps)
1269 plt_bitmap_clear(npc->rss_grp_entries, rss_grp);
1276 roc_npc_flow_destroy(struct roc_npc *roc_npc, struct roc_npc_flow *flow)
1278 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1279 struct plt_bitmap *bmap;
1282 rc = npc_rss_group_free(npc, flow);
1284 plt_err("Failed to free rss action rc = %d", rc);
1288 if (flow->vtag_insert_enabled) {
1289 rc = npc_vtag_cfg_delete(roc_npc, flow);
1294 rc = npc_mcam_free_entry(npc, flow->mcam_id);
1298 TAILQ_REMOVE(&npc->flow_list[flow->priority], flow, next);
1300 bmap = npc->live_entries[flow->priority];
1301 plt_bitmap_clear(bmap, flow->mcam_id);
1308 roc_npc_flow_dump(FILE *file, struct roc_npc *roc_npc)
1310 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1311 struct roc_npc_flow *flow_iter;
1312 struct npc_flow_list *list;
1313 uint32_t max_prio, i;
1315 max_prio = npc->flow_max_priority;
1317 for (i = 0; i < max_prio; i++) {
1318 list = &npc->flow_list[i];
1320 /* List in ascending order of mcam entries */
1321 TAILQ_FOREACH(flow_iter, list, next) {
1322 roc_npc_flow_mcam_dump(file, roc_npc, flow_iter);
1328 roc_npc_mcam_merge_base_steering_rule(struct roc_npc *roc_npc,
1329 struct roc_npc_flow *flow)
1331 struct npc_mcam_read_base_rule_rsp *base_rule_rsp;
1332 struct npc *npc = roc_npc_to_npc_priv(roc_npc);
1333 struct mcam_entry *base_entry;
1336 if (roc_nix_is_pf(roc_npc->roc_nix))
1339 (void)mbox_alloc_msg_npc_read_base_steer_rule(npc->mbox);
1340 rc = mbox_process_msg(npc->mbox, (void *)&base_rule_rsp);
1342 plt_err("Failed to fetch VF's base MCAM entry");
1345 base_entry = &base_rule_rsp->entry_data;
1346 for (idx = 0; idx < ROC_NPC_MAX_MCAM_WIDTH_DWORDS; idx++) {
1347 flow->mcam_data[idx] |= base_entry->kw[idx];
1348 flow->mcam_mask[idx] |= base_entry->kw_mask[idx];