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