xref: /linux/drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell.
5  *
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 
12 #include "rvu_struct.h"
13 #include "rvu_reg.h"
14 #include "rvu.h"
15 #include "npc.h"
16 #include "cgx.h"
17 #include "npc_profile.h"
18 #include "rvu_npc_hash.h"
19 #include "cn20k/npc.h"
20 #include "rvu_npc.h"
21 #include "cn20k/reg.h"
22 #include "lmac_common.h"
23 
24 #define RSVD_MCAM_ENTRIES_PER_PF	3 /* Broadcast, Promisc and AllMulticast */
25 #define RSVD_MCAM_ENTRIES_PER_NIXLF	1 /* Ucast for LFs */
26 
27 #define NPC_PARSE_RESULT_DMAC_OFFSET	8
28 #define NPC_HW_TSTAMP_OFFSET		8ULL
29 #define NPC_KEX_CHAN_MASK		0xFFFULL
30 #define NPC_KEX_PF_FUNC_MASK		0xFFFFULL
31 
32 #define ALIGN_8B_CEIL(__a)	(((__a) + 7) & (-8))
33 
34 static const char def_pfl_name[] = "default";
35 
36 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
37 				      int blkaddr, u16 pcifunc);
38 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
39 				       u16 pcifunc);
40 
41 bool is_npc_intf_tx(u8 intf)
42 {
43 	return !!(intf & 0x1);
44 }
45 
46 bool is_npc_intf_rx(u8 intf)
47 {
48 	return !(intf & 0x1);
49 }
50 
51 bool is_npc_interface_valid(struct rvu *rvu, u8 intf)
52 {
53 	struct rvu_hwinfo *hw = rvu->hw;
54 
55 	return intf < hw->npc_intfs;
56 }
57 
58 int rvu_npc_get_tx_nibble_cfg(struct rvu *rvu, u64 nibble_ena)
59 {
60 	/* Due to a HW issue in these silicon versions, parse nibble enable
61 	 * configuration has to be identical for both Rx and Tx interfaces.
62 	 */
63 	if (is_rvu_96xx_B0(rvu))
64 		return nibble_ena;
65 	return 0;
66 }
67 
68 void rvu_npc_set_pkind(struct rvu *rvu, int pkind, struct rvu_pfvf *pfvf)
69 {
70 	int blkaddr;
71 	u64 val = 0;
72 
73 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
74 	if (blkaddr < 0)
75 		return;
76 
77 	/* Config CPI base for the PKIND */
78 	val = pkind | 1ULL << 62;
79 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_CPI_DEFX(pkind, 0), val);
80 }
81 
82 int rvu_npc_get_pkind(struct rvu *rvu, u16 pf)
83 {
84 	struct npc_pkind *pkind = &rvu->hw->pkind;
85 	u32 map;
86 	int i;
87 
88 	for (i = 0; i < pkind->rsrc.max; i++) {
89 		map = pkind->pfchan_map[i];
90 		if (((map >> 16) & 0x3F) == pf)
91 			return i;
92 	}
93 	return -1;
94 }
95 
96 #define NPC_AF_ACTION0_PTR_ADVANCE	GENMASK_ULL(27, 20)
97 
98 int npc_config_ts_kpuaction(struct rvu *rvu, int pf, u16 pcifunc, bool enable)
99 {
100 	int pkind, blkaddr;
101 	u64 val;
102 
103 	pkind = rvu_npc_get_pkind(rvu, pf);
104 	if (pkind < 0) {
105 		dev_err(rvu->dev, "%s: pkind not mapped\n", __func__);
106 		return -EINVAL;
107 	}
108 
109 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
110 	if (blkaddr < 0) {
111 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
112 		return -EINVAL;
113 	}
114 
115 	val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
116 	val &= ~NPC_AF_ACTION0_PTR_ADVANCE;
117 	/* If timestamp is enabled then configure NPC to shift 8 bytes */
118 	if (enable)
119 		val |= FIELD_PREP(NPC_AF_ACTION0_PTR_ADVANCE,
120 				  NPC_HW_TSTAMP_OFFSET);
121 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
122 
123 	return 0;
124 }
125 
126 static int npc_get_ucast_mcam_index(struct npc_mcam *mcam, u16 pcifunc,
127 				    int nixlf)
128 {
129 	struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
130 	struct rvu *rvu = hw->rvu;
131 	int blkaddr = 0, max = 0;
132 	struct rvu_block *block;
133 	struct rvu_pfvf *pfvf;
134 
135 	pfvf = rvu_get_pfvf(rvu, pcifunc);
136 	/* Given a PF/VF and NIX LF number calculate the unicast mcam
137 	 * entry index based on the NIX block assigned to the PF/VF.
138 	 */
139 	blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
140 	while (blkaddr) {
141 		if (pfvf->nix_blkaddr == blkaddr)
142 			break;
143 		block = &rvu->hw->block[blkaddr];
144 		max += block->lf.max;
145 		blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
146 	}
147 
148 	return mcam->nixlf_offset + (max + nixlf) * RSVD_MCAM_ENTRIES_PER_NIXLF;
149 }
150 
151 int npc_get_nixlf_mcam_index(struct npc_mcam *mcam,
152 			     u16 pcifunc, int nixlf, int type)
153 {
154 	struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
155 	struct rvu *rvu = hw->rvu;
156 	u16 bcast, mcast, promisc, ucast;
157 	int index;
158 	int rc;
159 	int pf;
160 
161 	if (is_cn20k(rvu->pdev)) {
162 		rc = npc_cn20k_dft_rules_idx_get(rvu, pcifunc, &bcast, &mcast,
163 						 &promisc, &ucast);
164 		if (rc)
165 			return -EFAULT;
166 
167 		if (is_lbk_vf(rvu, pcifunc)) {
168 			if (promisc == USHRT_MAX)
169 				return -EINVAL;
170 			return promisc;
171 		}
172 
173 		if (is_cgx_vf(rvu, pcifunc)) {
174 			if (ucast == USHRT_MAX)
175 				return -EINVAL;
176 
177 			return ucast;
178 		}
179 
180 		switch (type) {
181 		case NIXLF_BCAST_ENTRY:
182 			if (bcast == USHRT_MAX)
183 				return -EINVAL;
184 			return bcast;
185 		case NIXLF_ALLMULTI_ENTRY:
186 			if (mcast == USHRT_MAX)
187 				return -EINVAL;
188 			return mcast;
189 		case NIXLF_PROMISC_ENTRY:
190 			if (promisc == USHRT_MAX)
191 				return -EINVAL;
192 			return promisc;
193 		case NIXLF_UCAST_ENTRY:
194 			if (ucast == USHRT_MAX)
195 				return -EINVAL;
196 			return ucast;
197 		default:
198 			return -EINVAL;
199 		}
200 	}
201 
202 	/* Check if this is for a PF */
203 	pf = rvu_get_pf(rvu->pdev, pcifunc);
204 	if (pf && !(pcifunc & RVU_PFVF_FUNC_MASK)) {
205 		/* Reserved entries exclude PF0 */
206 		pf--;
207 		index = mcam->pf_offset + (pf * RSVD_MCAM_ENTRIES_PER_PF);
208 		/* Broadcast address matching entry should be first so
209 		 * that the packet can be replicated to all VFs.
210 		 */
211 		if (type == NIXLF_BCAST_ENTRY)
212 			return index;
213 		else if (type == NIXLF_ALLMULTI_ENTRY)
214 			return index + 1;
215 		else if (type == NIXLF_PROMISC_ENTRY)
216 			return index + 2;
217 	}
218 
219 	return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
220 }
221 
222 int npc_get_bank(struct npc_mcam *mcam, int index)
223 {
224 	struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
225 	int bank = index / mcam->banksize;
226 	struct rvu *rvu = hw->rvu;
227 
228 	if (is_cn20k(rvu->pdev))
229 		return bank;
230 
231 	/* 0,1 & 2,3 banks are combined for this keysize */
232 	if (mcam->keysize == NPC_MCAM_KEY_X2)
233 		return bank ? 2 : 0;
234 
235 	return bank;
236 }
237 
238 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
239 			   int blkaddr, int index)
240 {
241 	int bank = npc_get_bank(mcam, index);
242 	u64 cfg;
243 
244 	index &= (mcam->banksize - 1);
245 	if (is_cn20k(rvu->pdev))
246 		cfg = rvu_read64(rvu, blkaddr,
247 				 NPC_AF_CN20K_MCAMEX_BANKX_CFG_EXT(index,
248 								   bank));
249 	else
250 		cfg = rvu_read64(rvu, blkaddr,
251 				 NPC_AF_MCAMEX_BANKX_CFG(index, bank));
252 
253 	return (cfg & 1);
254 }
255 
256 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
257 			   int blkaddr, int index, bool enable)
258 {
259 	int bank = npc_get_bank(mcam, index);
260 	int actbank = bank;
261 
262 	if (is_cn20k(rvu->pdev)) {
263 		if (npc_cn20k_enable_mcam_entry(rvu, blkaddr, index, enable))
264 			dev_err(rvu->dev, "Error to %s mcam %u entry\n",
265 				enable ? "enable" : "disable", index);
266 		return;
267 	}
268 
269 	index &= (mcam->banksize - 1);
270 	for (; bank < (actbank + mcam->banks_per_entry); bank++) {
271 		rvu_write64(rvu, blkaddr,
272 			    NPC_AF_MCAMEX_BANKX_CFG(index, bank),
273 			    enable ? 1 : 0);
274 	}
275 }
276 
277 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
278 				 int blkaddr, int index)
279 {
280 	int bank = npc_get_bank(mcam, index);
281 	int actbank = bank;
282 
283 	if (is_cn20k(rvu->pdev)) {
284 		if (npc_cn20k_clear_mcam_entry(rvu, blkaddr, index))
285 			dev_err(rvu->dev, "%s Failed to clear mcam %u\n",
286 				__func__, index);
287 		return;
288 	}
289 
290 	index &= (mcam->banksize - 1);
291 	for (; bank < (actbank + mcam->banks_per_entry); bank++) {
292 		rvu_write64(rvu, blkaddr,
293 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
294 		rvu_write64(rvu, blkaddr,
295 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
296 
297 		rvu_write64(rvu, blkaddr,
298 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
299 		rvu_write64(rvu, blkaddr,
300 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
301 
302 		rvu_write64(rvu, blkaddr,
303 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
304 		rvu_write64(rvu, blkaddr,
305 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
306 	}
307 }
308 
309 static void npc_get_keyword(struct mcam_entry *entry, int idx,
310 			    u64 *cam0, u64 *cam1)
311 {
312 	u64 kw_mask = 0x00;
313 
314 #define CAM_MASK(n)	(BIT_ULL(n) - 1)
315 
316 	/* 0, 2, 4, 6 indices refer to BANKX_CAMX_W0 and
317 	 * 1, 3, 5, 7 indices refer to BANKX_CAMX_W1.
318 	 *
319 	 * Also, only 48 bits of BANKX_CAMX_W1 are valid.
320 	 */
321 	switch (idx) {
322 	case 0:
323 		/* BANK(X)_CAM_W0<63:0> = MCAM_KEY[KW0]<63:0> */
324 		*cam1 = entry->kw[0];
325 		kw_mask = entry->kw_mask[0];
326 		break;
327 	case 1:
328 		/* BANK(X)_CAM_W1<47:0> = MCAM_KEY[KW1]<47:0> */
329 		*cam1 = entry->kw[1] & CAM_MASK(48);
330 		kw_mask = entry->kw_mask[1] & CAM_MASK(48);
331 		break;
332 	case 2:
333 		/* BANK(X + 1)_CAM_W0<15:0> = MCAM_KEY[KW1]<63:48>
334 		 * BANK(X + 1)_CAM_W0<63:16> = MCAM_KEY[KW2]<47:0>
335 		 */
336 		*cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
337 		*cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
338 		kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
339 		kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
340 		break;
341 	case 3:
342 		/* BANK(X + 1)_CAM_W1<15:0> = MCAM_KEY[KW2]<63:48>
343 		 * BANK(X + 1)_CAM_W1<47:16> = MCAM_KEY[KW3]<31:0>
344 		 */
345 		*cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
346 		*cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
347 		kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
348 		kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
349 		break;
350 	case 4:
351 		/* BANK(X + 2)_CAM_W0<31:0> = MCAM_KEY[KW3]<63:32>
352 		 * BANK(X + 2)_CAM_W0<63:32> = MCAM_KEY[KW4]<31:0>
353 		 */
354 		*cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
355 		*cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
356 		kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
357 		kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
358 		break;
359 	case 5:
360 		/* BANK(X + 2)_CAM_W1<31:0> = MCAM_KEY[KW4]<63:32>
361 		 * BANK(X + 2)_CAM_W1<47:32> = MCAM_KEY[KW5]<15:0>
362 		 */
363 		*cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
364 		*cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
365 		kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
366 		kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
367 		break;
368 	case 6:
369 		/* BANK(X + 3)_CAM_W0<47:0> = MCAM_KEY[KW5]<63:16>
370 		 * BANK(X + 3)_CAM_W0<63:48> = MCAM_KEY[KW6]<15:0>
371 		 */
372 		*cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
373 		*cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
374 		kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
375 		kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
376 		break;
377 	case 7:
378 		/* BANK(X + 3)_CAM_W1<47:0> = MCAM_KEY[KW6]<63:16> */
379 		*cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
380 		kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
381 		break;
382 	}
383 
384 	*cam1 &= kw_mask;
385 	*cam0 = ~*cam1 & kw_mask;
386 }
387 
388 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
389 			       u64 cam0, u64 cam1)
390 {
391 	/* Similar to npc_get_keyword, but fills mcam_entry structure from
392 	 * CAM registers.
393 	 */
394 	switch (idx) {
395 	case 0:
396 		entry->kw[0] = cam1;
397 		entry->kw_mask[0] = cam1 ^ cam0;
398 		break;
399 	case 1:
400 		entry->kw[1] = cam1;
401 		entry->kw_mask[1] = cam1 ^ cam0;
402 		break;
403 	case 2:
404 		entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
405 		entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
406 		entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
407 		entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
408 		break;
409 	case 3:
410 		entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
411 		entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
412 		entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
413 		entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
414 		break;
415 	case 4:
416 		entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
417 		entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
418 		entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
419 		entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
420 		break;
421 	case 5:
422 		entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
423 		entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
424 		entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
425 		entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
426 		break;
427 	case 6:
428 		entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
429 		entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
430 		entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
431 		entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
432 		break;
433 	case 7:
434 		entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
435 		entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
436 		break;
437 	}
438 }
439 
440 static u64 npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
441 					int blkaddr, u16 pf_func)
442 {
443 	int bank, nixlf, index;
444 	u64 reg;
445 
446 	/* get ucast entry rule entry index */
447 	if (nix_get_nixlf(rvu, pf_func, &nixlf, NULL)) {
448 		dev_err(rvu->dev, "%s: nixlf not attached to pcifunc:0x%x\n",
449 			__func__, pf_func);
450 		/* Action 0 is drop */
451 		return 0;
452 	}
453 
454 	index = npc_get_nixlf_mcam_index(mcam, pf_func, nixlf,
455 					 NIXLF_UCAST_ENTRY);
456 
457 	if (index < 0) {
458 		dev_err(rvu->dev,
459 			"%s: failed to get ucast entry pcifunc:0x%x\n",
460 			__func__, pf_func);
461 		/* Action 0 is drop */
462 		return 0;
463 	}
464 
465 	bank = npc_get_bank(mcam, index);
466 	index &= (mcam->banksize - 1);
467 
468 	if (is_cn20k(rvu->pdev))
469 		reg = NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(index, bank, 0);
470 	else
471 		reg = NPC_AF_MCAMEX_BANKX_ACTION(index, bank);
472 
473 	return rvu_read64(rvu, blkaddr, reg);
474 }
475 
476 static void npc_fixup_vf_rule(struct rvu *rvu, struct npc_mcam *mcam,
477 			      int blkaddr, int index, struct mcam_entry *entry,
478 			      bool *enable)
479 {
480 	struct rvu_npc_mcam_rule *rule;
481 	u16 owner, target_func;
482 	struct rvu_pfvf *pfvf;
483 	u64 rx_action;
484 
485 	owner = mcam->entry2pfvf_map[index];
486 	target_func = (entry->action >> 4) & 0xffff;
487 	/* do nothing when target is LBK/PF or owner is not PF */
488 	if (is_pffunc_af(owner) || is_lbk_vf(rvu, target_func) ||
489 	    (owner & RVU_PFVF_FUNC_MASK) ||
490 	    !(target_func & RVU_PFVF_FUNC_MASK))
491 		return;
492 
493 	/* save entry2target_pffunc */
494 	pfvf = rvu_get_pfvf(rvu, target_func);
495 	mcam->entry2target_pffunc[index] = target_func;
496 
497 	/* don't enable rule when nixlf not attached or initialized */
498 	if (!(is_nixlf_attached(rvu, target_func) &&
499 	      test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
500 		*enable = false;
501 
502 	/* fix up not needed for the rules added by user(ntuple filters) */
503 	list_for_each_entry(rule, &mcam->mcam_rules, list) {
504 		if (rule->entry == index)
505 			return;
506 	}
507 
508 	/* AF modifies given action iff PF/VF has requested for it */
509 	if ((entry->action & 0xFULL) != NIX_RX_ACTION_DEFAULT)
510 		return;
511 
512 	/* copy VF default entry action to the VF mcam entry */
513 	rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
514 						 target_func);
515 	if (rx_action)
516 		entry->action = rx_action;
517 }
518 
519 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
520 				  int blkaddr, int index, u8 intf,
521 				  struct mcam_entry *entry, bool enable)
522 {
523 	int bank = npc_get_bank(mcam, index);
524 	int kw = 0, actbank, actindex;
525 	u8 tx_intf_mask = ~intf & 0x3;
526 	u8 tx_intf = intf;
527 	u64 cam0, cam1;
528 
529 	actbank = bank; /* Save bank id, to set action later on */
530 	actindex = index;
531 	index &= (mcam->banksize - 1);
532 
533 	/* Disable before mcam entry update */
534 	npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
535 
536 	/* Clear mcam entry to avoid writes being suppressed by NPC */
537 	npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
538 
539 	/* CAM1 takes the comparison value and
540 	 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
541 	 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
542 	 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
543 	 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
544 	 */
545 	for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
546 		/* Interface should be set in all banks */
547 		if (is_npc_intf_tx(intf)) {
548 			/* Last bit must be set and rest don't care
549 			 * for TX interfaces
550 			 */
551 			tx_intf_mask = 0x1;
552 			tx_intf = intf & tx_intf_mask;
553 			tx_intf_mask = ~tx_intf & tx_intf_mask;
554 		}
555 
556 		rvu_write64(rvu, blkaddr,
557 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
558 			    tx_intf);
559 		rvu_write64(rvu, blkaddr,
560 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
561 			    tx_intf_mask);
562 
563 		/* Set the match key */
564 		npc_get_keyword(entry, kw, &cam0, &cam1);
565 		rvu_write64(rvu, blkaddr,
566 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
567 		rvu_write64(rvu, blkaddr,
568 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
569 
570 		npc_get_keyword(entry, kw + 1, &cam0, &cam1);
571 		rvu_write64(rvu, blkaddr,
572 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
573 		rvu_write64(rvu, blkaddr,
574 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
575 	}
576 
577 	/* PF installing VF rule */
578 	if (is_npc_intf_rx(intf) && actindex < mcam->bmap_entries)
579 		npc_fixup_vf_rule(rvu, mcam, blkaddr, actindex, entry, &enable);
580 
581 	/* Set 'action' */
582 	rvu_write64(rvu, blkaddr,
583 		    NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
584 
585 	/* Set TAG 'action' */
586 	rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
587 		    entry->vtag_action);
588 
589 	/* Enable the entry */
590 	if (enable)
591 		npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
592 }
593 
594 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
595 			 int blkaddr, u16 src,
596 			 struct mcam_entry *entry, u8 *intf, u8 *ena)
597 {
598 	int sbank = npc_get_bank(mcam, src);
599 	int bank, kw = 0;
600 	u64 cam0, cam1;
601 
602 	src &= (mcam->banksize - 1);
603 	bank = sbank;
604 
605 	for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
606 		cam1 = rvu_read64(rvu, blkaddr,
607 				  NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
608 		cam0 = rvu_read64(rvu, blkaddr,
609 				  NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
610 		npc_fill_entryword(entry, kw, cam0, cam1);
611 
612 		cam1 = rvu_read64(rvu, blkaddr,
613 				  NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
614 		cam0 = rvu_read64(rvu, blkaddr,
615 				  NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
616 		npc_fill_entryword(entry, kw + 1, cam0, cam1);
617 	}
618 
619 	entry->action = rvu_read64(rvu, blkaddr,
620 				   NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
621 	entry->vtag_action =
622 		rvu_read64(rvu, blkaddr,
623 			   NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
624 	*intf = rvu_read64(rvu, blkaddr,
625 			   NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
626 	*ena = rvu_read64(rvu, blkaddr,
627 			  NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
628 }
629 
630 static int npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
631 			       int blkaddr, u16 src, u16 dest)
632 {
633 	int dbank = npc_get_bank(mcam, dest);
634 	int sbank = npc_get_bank(mcam, src);
635 	u64 cfg, sreg, dreg;
636 	int bank, i;
637 
638 	if (is_cn20k(rvu->pdev))
639 		return npc_cn20k_copy_mcam_entry(rvu, blkaddr, src, dest);
640 
641 	src &= (mcam->banksize - 1);
642 	dest &= (mcam->banksize - 1);
643 
644 	/* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
645 	for (bank = 0; bank < mcam->banks_per_entry; bank++) {
646 		sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
647 		dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
648 		for (i = 0; i < 6; i++) {
649 			cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
650 			rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
651 		}
652 	}
653 
654 	/* Copy action */
655 	cfg = rvu_read64(rvu, blkaddr,
656 			 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
657 	rvu_write64(rvu, blkaddr,
658 		    NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
659 
660 	/* Copy TAG action */
661 	cfg = rvu_read64(rvu, blkaddr,
662 			 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
663 	rvu_write64(rvu, blkaddr,
664 		    NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
665 
666 	/* Enable or disable */
667 	cfg = rvu_read64(rvu, blkaddr,
668 			 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
669 	rvu_write64(rvu, blkaddr,
670 		    NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
671 	return 0;
672 }
673 
674 u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
675 			int blkaddr, int index)
676 {
677 	int bank = npc_get_bank(mcam, index);
678 	u64 reg;
679 
680 	index &= (mcam->banksize - 1);
681 
682 	if (is_cn20k(rvu->pdev))
683 		reg = NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(index, bank, 0);
684 	else
685 		reg = NPC_AF_MCAMEX_BANKX_ACTION(index, bank);
686 	return rvu_read64(rvu, blkaddr, reg);
687 }
688 
689 void npc_set_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
690 			 int blkaddr, int index, u64 cfg)
691 {
692 	int bank = npc_get_bank(mcam, index);
693 	u64 reg;
694 
695 	index &= (mcam->banksize - 1);
696 
697 	if (is_cn20k(rvu->pdev))
698 		reg = NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(index, bank, 0);
699 	else
700 		reg = NPC_AF_MCAMEX_BANKX_ACTION(index, bank);
701 
702 	return rvu_write64(rvu, blkaddr, reg, cfg);
703 }
704 
705 u32 rvu_get_cpt_chan_mask(struct rvu *rvu)
706 {
707 	/* For cn10k the upper two bits of the channel number are
708 	 * cpt channel number. with masking out these bits in the
709 	 * mcam entry, same entry used for NIX will allow packets
710 	 * received from cpt for parsing.
711 	 */
712 	if (!is_rvu_otx2(rvu))
713 		return NIX_CHAN_CPT_X2P_MASK;
714 	else
715 		return 0xFFFu;
716 }
717 
718 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
719 				 int nixlf, u64 chan, u8 *mac_addr)
720 {
721 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
722 	struct npc_install_flow_req req = { 0 };
723 	struct npc_install_flow_rsp rsp = { 0 };
724 	struct npc_mcam *mcam = &rvu->hw->mcam;
725 	struct nix_rx_action action = { 0 };
726 	int blkaddr, index;
727 
728 	/* AF's and SDP VFs work in promiscuous mode */
729 	if (is_lbk_vf(rvu, pcifunc) || is_sdp_vf(rvu, pcifunc))
730 		return;
731 
732 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
733 	if (blkaddr < 0)
734 		return;
735 
736 	/* Ucast rule should not be installed if DMAC
737 	 * extraction is not supported by the profile.
738 	 */
739 	if (!npc_is_feature_supported(rvu, BIT_ULL(NPC_DMAC), pfvf->nix_rx_intf))
740 		return;
741 
742 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
743 					 nixlf, NIXLF_UCAST_ENTRY);
744 	if (index < 0) {
745 		dev_err(rvu->dev,
746 			"%s: Error to get ucast entry for pcifunc=%#x\n",
747 			__func__, pcifunc);
748 		return;
749 	}
750 
751 	/* Don't change the action if entry is already enabled
752 	 * Otherwise RSS action may get overwritten.
753 	 */
754 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
755 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
756 						      blkaddr, index);
757 	} else {
758 		action.op = NIX_RX_ACTIONOP_UCAST;
759 		action.pf_func = pcifunc;
760 	}
761 
762 	req.default_rule = 1;
763 	ether_addr_copy(req.packet.dmac, mac_addr);
764 	eth_broadcast_addr((u8 *)&req.mask.dmac);
765 	req.features = BIT_ULL(NPC_DMAC);
766 	req.channel = chan;
767 	req.chan_mask = rvu_get_cpt_chan_mask(rvu);
768 	req.intf = pfvf->nix_rx_intf;
769 	req.op = action.op;
770 	req.hdr.pcifunc = 0; /* AF is requester */
771 	req.vf = action.pf_func;
772 	req.index = action.index;
773 	req.match_id = action.match_id;
774 	req.flow_key_alg = action.flow_key_alg;
775 
776 	if (is_cn20k(rvu->pdev))
777 		req.hw_prio = pfvf->hw_prio;
778 
779 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
780 }
781 
782 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
783 				   int nixlf, u64 chan, u8 chan_cnt)
784 {
785 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
786 	struct npc_install_flow_req req = { 0 };
787 	struct npc_install_flow_rsp rsp = { 0 };
788 	struct npc_mcam *mcam = &rvu->hw->mcam;
789 	struct rvu_hwinfo *hw = rvu->hw;
790 	int blkaddr, ucast_idx, index;
791 	struct nix_rx_action action = { 0 };
792 	u64 relaxed_mask;
793 	u8 flow_key_alg;
794 
795 	if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
796 		return;
797 
798 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
799 	if (blkaddr < 0)
800 		return;
801 
802 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
803 					 nixlf, NIXLF_PROMISC_ENTRY);
804 
805 	/* In cn20k, default indexes are installed only for CGX mapped
806 	 * and lbk interfaces
807 	 */
808 	if (is_cgx_vf(rvu, pcifunc))
809 		index = npc_get_nixlf_mcam_index(mcam,
810 						 pcifunc & ~RVU_PFVF_FUNC_MASK,
811 						 nixlf, NIXLF_PROMISC_ENTRY);
812 
813 	if (index < 0) {
814 		dev_err(rvu->dev,
815 			"%s: Error to get promisc entry for pcifunc=%#x\n",
816 			__func__, pcifunc);
817 		return;
818 	}
819 
820 	/* If the corresponding PF's ucast action is RSS,
821 	 * use the same action for promisc also
822 	 * Please note that for lbk(s) "index" and "ucast_idx"
823 	 * will be same.
824 	 */
825 	if (is_lbk_vf(rvu, pcifunc))
826 		ucast_idx = index;
827 	else
828 		ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
829 						     nixlf, NIXLF_UCAST_ENTRY);
830 	if (ucast_idx < 0) {
831 		dev_err(rvu->dev,
832 			"%s: Error to get ucast/promisc entry for pcifunc=%#x\n",
833 			__func__, pcifunc);
834 		return;
835 	}
836 
837 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
838 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
839 						      blkaddr, ucast_idx);
840 
841 	if (action.op != NIX_RX_ACTIONOP_RSS) {
842 		*(u64 *)&action = 0;
843 		action.op = NIX_RX_ACTIONOP_UCAST;
844 	}
845 
846 	flow_key_alg = action.flow_key_alg;
847 
848 	/* RX_ACTION set to MCAST for CGX PF's */
849 	if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
850 	    is_pf_cgxmapped(rvu, rvu_get_pf(rvu->pdev, pcifunc))) {
851 		*(u64 *)&action = 0;
852 		action.op = NIX_RX_ACTIONOP_MCAST;
853 		pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
854 		action.index = pfvf->promisc_mce_idx;
855 	}
856 
857 	/* For cn10k the upper two bits of the channel number are
858 	 * cpt channel number. with masking out these bits in the
859 	 * mcam entry, same entry used for NIX will allow packets
860 	 * received from cpt for parsing.
861 	 */
862 	req.chan_mask = rvu_get_cpt_chan_mask(rvu);
863 
864 	if (chan_cnt > 1) {
865 		if (!is_power_of_2(chan_cnt)) {
866 			dev_err(rvu->dev,
867 				"%s: channel count more than 1, must be power of 2\n", __func__);
868 			return;
869 		}
870 		relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
871 					   ilog2(chan_cnt));
872 		req.chan_mask &= relaxed_mask;
873 	}
874 
875 	req.channel = chan;
876 	req.intf = pfvf->nix_rx_intf;
877 	req.entry = index;
878 	req.op = action.op;
879 	req.hdr.pcifunc = 0; /* AF is requester */
880 	req.vf = pcifunc;
881 	req.index = action.index;
882 	req.match_id = action.match_id;
883 	req.flow_key_alg = flow_key_alg;
884 
885 	if (is_cn20k(rvu->pdev))
886 		req.hw_prio = pfvf->hw_prio;
887 
888 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
889 }
890 
891 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
892 				  int nixlf, bool enable)
893 {
894 	struct npc_mcam *mcam = &rvu->hw->mcam;
895 	int blkaddr, index;
896 
897 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
898 	if (blkaddr < 0)
899 		return;
900 
901 	/* Get 'pcifunc' of PF device */
902 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
903 
904 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
905 					 nixlf, NIXLF_PROMISC_ENTRY);
906 
907 	if (index < 0) {
908 		dev_err(rvu->dev,
909 			"%s: Error to get promisc entry for pcifunc=%#x\n",
910 			__func__, pcifunc);
911 		return;
912 	}
913 
914 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
915 }
916 
917 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
918 				       int nixlf, u64 chan)
919 {
920 	struct rvu_pfvf *pfvf;
921 	struct npc_install_flow_req req = { 0 };
922 	struct npc_install_flow_rsp rsp = { 0 };
923 	struct npc_mcam *mcam = &rvu->hw->mcam;
924 	struct rvu_hwinfo *hw = rvu->hw;
925 	int blkaddr, index;
926 
927 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
928 	if (blkaddr < 0)
929 		return;
930 
931 	/* Skip LBK VFs */
932 	if (is_lbk_vf(rvu, pcifunc))
933 		return;
934 
935 	/* If pkt replication is not supported,
936 	 * then only PF is allowed to add a bcast match entry.
937 	 */
938 	if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
939 		return;
940 
941 	/* Get 'pcifunc' of PF device */
942 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
943 	pfvf = rvu_get_pfvf(rvu, pcifunc);
944 
945 	/* Bcast rule should not be installed if both DMAC
946 	 * and LXMB extraction is not supported by the profile.
947 	 */
948 	if (!npc_is_feature_supported(rvu, BIT_ULL(NPC_DMAC), pfvf->nix_rx_intf) &&
949 	    !npc_is_feature_supported(rvu, BIT_ULL(NPC_LXMB), pfvf->nix_rx_intf))
950 		return;
951 
952 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
953 					 nixlf, NIXLF_BCAST_ENTRY);
954 	if (index < 0) {
955 		dev_err(rvu->dev,
956 			"%s: Error to get bcast entry for pcifunc=%#x\n",
957 			__func__, pcifunc);
958 		return;
959 	}
960 
961 	if (!hw->cap.nix_rx_multicast) {
962 		/* Early silicon doesn't support pkt replication,
963 		 * so install entry with UCAST action, so that PF
964 		 * receives all broadcast packets.
965 		 */
966 		req.op = NIX_RX_ACTIONOP_UCAST;
967 	} else {
968 		req.op = NIX_RX_ACTIONOP_MCAST;
969 		req.index = pfvf->bcast_mce_idx;
970 	}
971 
972 	eth_broadcast_addr((u8 *)&req.packet.dmac);
973 	eth_broadcast_addr((u8 *)&req.mask.dmac);
974 	req.features = BIT_ULL(NPC_DMAC);
975 	req.channel = chan;
976 	req.chan_mask = 0xFFFU;
977 	req.intf = pfvf->nix_rx_intf;
978 	req.entry = index;
979 	req.hdr.pcifunc = 0; /* AF is requester */
980 	req.vf = pcifunc;
981 
982 	if (is_cn20k(rvu->pdev))
983 		req.hw_prio = pfvf->hw_prio;
984 
985 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
986 }
987 
988 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
989 				    u64 chan)
990 {
991 	struct npc_install_flow_req req = { 0 };
992 	struct npc_install_flow_rsp rsp = { 0 };
993 	struct npc_mcam *mcam = &rvu->hw->mcam;
994 	struct rvu_hwinfo *hw = rvu->hw;
995 	int blkaddr, ucast_idx, index;
996 	u8 mac_addr[ETH_ALEN] = { 0 };
997 	struct nix_rx_action action = { 0 };
998 	struct rvu_pfvf *pfvf;
999 	u8 flow_key_alg;
1000 	u16 vf_func;
1001 
1002 	/* Only CGX PF/VF can add allmulticast entry */
1003 	if (is_lbk_vf(rvu, pcifunc) || is_sdp_vf(rvu, pcifunc))
1004 		return;
1005 
1006 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1007 	if (blkaddr < 0)
1008 		return;
1009 
1010 	/* Get 'pcifunc' of PF device */
1011 	vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
1012 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
1013 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1014 
1015 	/* Mcast rule should not be installed if both DMAC
1016 	 * and LXMB extraction is not supported by the profile.
1017 	 */
1018 	if (!npc_is_feature_supported(rvu, BIT_ULL(NPC_DMAC), pfvf->nix_rx_intf) &&
1019 	    !npc_is_feature_supported(rvu, BIT_ULL(NPC_LXMB), pfvf->nix_rx_intf))
1020 		return;
1021 
1022 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1023 					 nixlf, NIXLF_ALLMULTI_ENTRY);
1024 	if (index < 0) {
1025 		dev_err(rvu->dev,
1026 			"%s: Error to get mcast entry for pcifunc=%#x\n",
1027 			__func__, pcifunc);
1028 		return;
1029 	}
1030 
1031 	/* If the corresponding PF's ucast action is RSS,
1032 	 * use the same action for multicast entry also
1033 	 */
1034 	ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
1035 					     nixlf, NIXLF_UCAST_ENTRY);
1036 	if (ucast_idx < 0) {
1037 		dev_err(rvu->dev,
1038 			"%s: Error to get ucast entry for pcifunc=%#x\n",
1039 			__func__, pcifunc);
1040 		return;
1041 	}
1042 
1043 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
1044 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
1045 							blkaddr, ucast_idx);
1046 
1047 	flow_key_alg = action.flow_key_alg;
1048 	if (action.op != NIX_RX_ACTIONOP_RSS) {
1049 		*(u64 *)&action = 0;
1050 		action.op = NIX_RX_ACTIONOP_UCAST;
1051 		action.pf_func = pcifunc;
1052 	}
1053 
1054 	/* RX_ACTION set to MCAST for CGX PF's */
1055 	if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
1056 		*(u64 *)&action = 0;
1057 		action.op = NIX_RX_ACTIONOP_MCAST;
1058 		action.index = pfvf->mcast_mce_idx;
1059 	}
1060 
1061 	mac_addr[0] = 0x01;	/* LSB bit of 1st byte in DMAC */
1062 	ether_addr_copy(req.packet.dmac, mac_addr);
1063 	ether_addr_copy(req.mask.dmac, mac_addr);
1064 	req.features = BIT_ULL(NPC_DMAC);
1065 
1066 	req.chan_mask = rvu_get_cpt_chan_mask(rvu);
1067 	req.channel = chan;
1068 	req.intf = pfvf->nix_rx_intf;
1069 	req.entry = index;
1070 	req.op = action.op;
1071 	req.hdr.pcifunc = 0; /* AF is requester */
1072 	req.vf = pcifunc | vf_func;
1073 	req.index = action.index;
1074 	req.match_id = action.match_id;
1075 	req.flow_key_alg = flow_key_alg;
1076 
1077 	if (is_cn20k(rvu->pdev))
1078 		req.hw_prio = pfvf->hw_prio;
1079 
1080 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
1081 }
1082 
1083 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
1084 				   bool enable)
1085 {
1086 	struct npc_mcam *mcam = &rvu->hw->mcam;
1087 	int blkaddr, index;
1088 
1089 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1090 	if (blkaddr < 0)
1091 		return;
1092 
1093 	/* Get 'pcifunc' of PF device */
1094 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
1095 
1096 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
1097 					 NIXLF_ALLMULTI_ENTRY);
1098 	if (index < 0) {
1099 		dev_err(rvu->dev,
1100 			"%s: Error to get mcast entry for pcifunc=%#x\n",
1101 			__func__, pcifunc);
1102 		return;
1103 	}
1104 
1105 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1106 }
1107 
1108 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
1109 				     int blkaddr, u16 pcifunc, u64 rx_action)
1110 {
1111 	int actindex, index, bank, entry;
1112 	struct rvu_npc_mcam_rule *rule;
1113 	bool enable, update;
1114 
1115 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
1116 		return;
1117 
1118 	mutex_lock(&mcam->lock);
1119 	for (index = 0; index < mcam->bmap_entries; index++) {
1120 		if (mcam->entry2target_pffunc[index] != pcifunc)
1121 			continue;
1122 		update = true;
1123 		/* update not needed for the rules added via ntuple filters */
1124 		list_for_each_entry(rule, &mcam->mcam_rules, list) {
1125 			if (rule->entry == index)
1126 				update = false;
1127 		}
1128 		if (!update)
1129 			continue;
1130 		bank = npc_get_bank(mcam, index);
1131 		actindex = index;
1132 		entry = index & (mcam->banksize - 1);
1133 
1134 		/* read vf flow entry enable status */
1135 		enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
1136 					       actindex);
1137 		/* disable before mcam entry update */
1138 		npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
1139 				      false);
1140 
1141 		/* update 'action' */
1142 		if (is_cn20k(rvu->pdev))
1143 			rvu_write64(rvu, blkaddr,
1144 				    NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(entry,
1145 									  bank,
1146 									  0),
1147 				    rx_action);
1148 		else
1149 			rvu_write64(rvu, blkaddr,
1150 				    NPC_AF_MCAMEX_BANKX_ACTION(entry, bank),
1151 				    rx_action);
1152 
1153 		if (enable)
1154 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1155 					      actindex, true);
1156 	}
1157 	mutex_unlock(&mcam->lock);
1158 }
1159 
1160 static void npc_update_rx_action_with_alg_idx(struct rvu *rvu, struct nix_rx_action action,
1161 					      struct rvu_pfvf *pfvf, int mcam_index, int blkaddr,
1162 					      int alg_idx)
1163 
1164 {
1165 	struct npc_mcam *mcam = &rvu->hw->mcam;
1166 	struct rvu_hwinfo *hw = rvu->hw;
1167 	int bank, op_rss;
1168 	u64 reg;
1169 
1170 	if (!is_mcam_entry_enabled(rvu, mcam, blkaddr, mcam_index))
1171 		return;
1172 
1173 	op_rss = (!hw->cap.nix_rx_multicast || !pfvf->use_mce_list);
1174 
1175 	bank = npc_get_bank(mcam, mcam_index);
1176 	mcam_index &= (mcam->banksize - 1);
1177 
1178 	if (is_cn20k(rvu->pdev))
1179 		reg = NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(mcam_index,
1180 							    bank, 0);
1181 	else
1182 		reg = NPC_AF_MCAMEX_BANKX_ACTION(mcam_index, bank);
1183 
1184 	/* If Rx action is MCAST update only RSS algorithm index */
1185 	if (!op_rss) {
1186 		*(u64 *)&action = rvu_read64(rvu, blkaddr, reg);
1187 
1188 		action.flow_key_alg = alg_idx;
1189 	}
1190 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action);
1191 }
1192 
1193 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
1194 				    int group, int alg_idx, int mcam_index)
1195 {
1196 	struct npc_mcam *mcam = &rvu->hw->mcam;
1197 	struct nix_rx_action action;
1198 	int blkaddr, index, bank;
1199 	struct rvu_pfvf *pfvf;
1200 	u64 reg;
1201 
1202 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1203 	if (blkaddr < 0)
1204 		return;
1205 
1206 	/* Check if this is for reserved default entry */
1207 	if (mcam_index < 0) {
1208 		if (group != DEFAULT_RSS_CONTEXT_GROUP)
1209 			return;
1210 		index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1211 						 nixlf, NIXLF_UCAST_ENTRY);
1212 	} else {
1213 		/* TODO: validate this mcam index */
1214 		index = mcam_index;
1215 	}
1216 
1217 	if (index < 0 || index >= mcam->total_entries) {
1218 		dev_err(rvu->dev,
1219 			"%s: Invalid mcam index, pcifunc=%#x\n",
1220 			__func__, pcifunc);
1221 		return;
1222 	}
1223 
1224 	bank = npc_get_bank(mcam, index);
1225 	index &= (mcam->banksize - 1);
1226 
1227 	if (is_cn20k(rvu->pdev))
1228 		reg = NPC_AF_CN20K_MCAMEX_BANKX_ACTIONX_EXT(index, bank, 0);
1229 	else
1230 		reg = NPC_AF_MCAMEX_BANKX_ACTION(index, bank);
1231 
1232 	*(u64 *)&action = rvu_read64(rvu, blkaddr, reg);
1233 	/* Ignore if no action was set earlier */
1234 	if (!*(u64 *)&action)
1235 		return;
1236 
1237 	action.op = NIX_RX_ACTIONOP_RSS;
1238 	action.pf_func = pcifunc;
1239 	action.index = group;
1240 	action.flow_key_alg = alg_idx;
1241 
1242 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action);
1243 	/* update the VF flow rule action with the VF default entry action */
1244 	if (mcam_index < 0)
1245 		npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
1246 					 *(u64 *)&action);
1247 
1248 	/* update the action change in default rule */
1249 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1250 	if (pfvf->def_ucast_rule)
1251 		pfvf->def_ucast_rule->rx_action = action;
1252 
1253 	if (mcam_index < 0) {
1254 		/* update the VF flow rule action with the VF default
1255 		 * entry action
1256 		 */
1257 		npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
1258 					 *(u64 *)&action);
1259 
1260 		index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1261 						 nixlf, NIXLF_PROMISC_ENTRY);
1262 
1263 		/* If PF's promiscuous  entry is enabled,
1264 		 * Set RSS action for that entry as well
1265 		 */
1266 		if (index >= 0)
1267 			npc_update_rx_action_with_alg_idx(rvu, action, pfvf, index,
1268 							  blkaddr, alg_idx);
1269 
1270 		index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1271 						 nixlf, NIXLF_ALLMULTI_ENTRY);
1272 		/* If PF's allmulti  entry is enabled,
1273 		 * Set RSS action for that entry as well
1274 		 */
1275 		if (index >= 0)
1276 			npc_update_rx_action_with_alg_idx(rvu, action, pfvf, index,
1277 							  blkaddr, alg_idx);
1278 	}
1279 }
1280 
1281 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1282 				  int nixlf, int type, bool enable)
1283 {
1284 	struct npc_mcam *mcam = &rvu->hw->mcam;
1285 	struct rvu_hwinfo *hw = rvu->hw;
1286 	struct nix_mce_list *mce_list;
1287 	int index, blkaddr, mce_idx;
1288 	struct rvu_pfvf *pfvf;
1289 	u16 ptr[4];
1290 
1291 	/* multicast pkt replication is not enabled for AF's VFs & SDP links */
1292 	if (is_lbk_vf(rvu, pcifunc) || is_sdp_pfvf(rvu, pcifunc))
1293 		return;
1294 
1295 	/* In cn20k, only CGX mapped devices have default MCAST entry */
1296 	if (is_cn20k(rvu->pdev) &&
1297 	    npc_cn20k_dft_rules_idx_get(rvu, pcifunc, &ptr[0], &ptr[1],
1298 					&ptr[2], &ptr[3]))
1299 		return;
1300 
1301 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1302 	if (blkaddr < 0)
1303 		return;
1304 
1305 	index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1306 					 nixlf, type);
1307 	if (index < 0) {
1308 		dev_err(rvu->dev,
1309 			"%s: Error to get entry for pcifunc=%#x, type=%u\n",
1310 			__func__, pcifunc, type);
1311 		return;
1312 	}
1313 
1314 	/* disable MCAM entry when packet replication is not supported by hw */
1315 	if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1316 		npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1317 		return;
1318 	}
1319 
1320 	/* return incase mce list is not enabled */
1321 	pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1322 	if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1323 	    type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1324 		return;
1325 
1326 	nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1327 
1328 	nix_update_mce_list(rvu, pcifunc, mce_list,
1329 			    mce_idx, index, enable);
1330 	if (enable)
1331 		npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1332 }
1333 
1334 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1335 				       int nixlf, bool enable)
1336 {
1337 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1338 	struct npc_mcam *mcam = &rvu->hw->mcam;
1339 	int index, blkaddr;
1340 	u16 ptr[4];
1341 
1342 	/* only CGX or LBK interfaces have default entries */
1343 	if (is_cn20k(rvu->pdev) &&
1344 	    npc_cn20k_dft_rules_idx_get(rvu, pcifunc, &ptr[0], &ptr[1],
1345 					&ptr[2], &ptr[3]))
1346 		return;
1347 
1348 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1349 	if (blkaddr < 0)
1350 		return;
1351 
1352 	/* Ucast MCAM match entry of this PF/VF */
1353 	if (npc_is_feature_supported(rvu, BIT_ULL(NPC_DMAC),
1354 				     pfvf->nix_rx_intf)) {
1355 		index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1356 						 nixlf, NIXLF_UCAST_ENTRY);
1357 		if (index < 0) {
1358 			dev_err(rvu->dev,
1359 				"%s: Error to get ucast entry for pcifunc=%#x\n",
1360 				__func__, pcifunc);
1361 			return;
1362 		}
1363 		npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1364 	}
1365 
1366 	/* Nothing to do for VFs, on platforms where pkt replication
1367 	 * is not supported
1368 	 */
1369 	if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1370 		return;
1371 
1372 	/* add/delete pf_func to broadcast MCE list */
1373 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1374 				     NIXLF_BCAST_ENTRY, enable);
1375 }
1376 
1377 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1378 {
1379 	if (nixlf < 0)
1380 		return;
1381 
1382 	npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1383 
1384 	/* Delete multicast and promisc MCAM entries */
1385 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1386 				     NIXLF_ALLMULTI_ENTRY, false);
1387 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1388 				     NIXLF_PROMISC_ENTRY, false);
1389 }
1390 
1391 bool rvu_npc_enable_mcam_by_entry_index(struct rvu *rvu, int entry, int intf, bool enable)
1392 {
1393 	int blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1394 	struct npc_mcam *mcam = &rvu->hw->mcam;
1395 	struct rvu_npc_mcam_rule *rule, *tmp;
1396 
1397 	mutex_lock(&mcam->lock);
1398 
1399 	list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1400 		if (rule->intf != intf)
1401 			continue;
1402 
1403 		if (rule->entry != entry)
1404 			continue;
1405 
1406 		rule->enable = enable;
1407 		mutex_unlock(&mcam->lock);
1408 
1409 		npc_enable_mcam_entry(rvu, mcam, blkaddr,
1410 				      entry, enable);
1411 
1412 		return true;
1413 	}
1414 
1415 	mutex_unlock(&mcam->lock);
1416 	return false;
1417 }
1418 
1419 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1420 {
1421 	if (nixlf < 0)
1422 		return;
1423 
1424 	/* Enables only broadcast match entry. Promisc/Allmulti are enabled
1425 	 * in set_rx_mode mbox handler.
1426 	 */
1427 	npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1428 }
1429 
1430 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1431 {
1432 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1433 	struct npc_mcam *mcam = &rvu->hw->mcam;
1434 	struct rvu_npc_mcam_rule *rule, *tmp;
1435 	int blkaddr;
1436 
1437 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1438 	if (blkaddr < 0)
1439 		return;
1440 
1441 	mutex_lock(&mcam->lock);
1442 
1443 	/* Disable MCAM entries directing traffic to this 'pcifunc' */
1444 	list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1445 		if (is_npc_intf_rx(rule->intf) &&
1446 		    rule->rx_action.pf_func == pcifunc &&
1447 		    rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1448 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1449 					      rule->entry, false);
1450 			rule->enable = false;
1451 			/* Indicate that default rule is disabled */
1452 			if (rule->default_rule) {
1453 				pfvf->def_ucast_rule = NULL;
1454 				list_del(&rule->list);
1455 				kfree(rule);
1456 			}
1457 		}
1458 	}
1459 
1460 	mutex_unlock(&mcam->lock);
1461 
1462 	npc_mcam_disable_flows(rvu, pcifunc);
1463 
1464 	rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1465 }
1466 
1467 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1468 {
1469 	struct npc_mcam *mcam = &rvu->hw->mcam;
1470 	struct rvu_npc_mcam_rule *rule, *tmp;
1471 	int blkaddr;
1472 
1473 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1474 	if (blkaddr < 0)
1475 		return;
1476 
1477 	mutex_lock(&mcam->lock);
1478 
1479 	/* Free all MCAM entries owned by this 'pcifunc' */
1480 	npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1481 
1482 	/* Free all MCAM counters owned by this 'pcifunc' */
1483 	npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1484 
1485 	/* Delete MCAM entries owned by this 'pcifunc' */
1486 	list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1487 		if (rule->owner == pcifunc && !rule->default_rule) {
1488 			list_del(&rule->list);
1489 			kfree(rule);
1490 		}
1491 	}
1492 
1493 	mutex_unlock(&mcam->lock);
1494 
1495 	rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1496 }
1497 
1498 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1499 				const struct npc_mcam_kex *mkex,
1500 				u8 intf)
1501 {
1502 	int lid, lt, ld, fl;
1503 
1504 	if (is_npc_intf_tx(intf))
1505 		return;
1506 
1507 	rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1508 		    mkex->keyx_cfg[NIX_INTF_RX]);
1509 
1510 	/* Program LDATA */
1511 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
1512 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
1513 			for (ld = 0; ld < NPC_MAX_LD; ld++)
1514 				SET_KEX_LD(intf, lid, lt, ld,
1515 					   mkex->intf_lid_lt_ld[NIX_INTF_RX]
1516 					   [lid][lt][ld]);
1517 		}
1518 	}
1519 	/* Program LFLAGS */
1520 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
1521 		for (fl = 0; fl < NPC_MAX_LFL; fl++)
1522 			SET_KEX_LDFLAGS(intf, ld, fl,
1523 					mkex->intf_ld_flags[NIX_INTF_RX]
1524 					[ld][fl]);
1525 	}
1526 }
1527 
1528 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1529 				const struct npc_mcam_kex *mkex,
1530 				u8 intf)
1531 {
1532 	int lid, lt, ld, fl;
1533 
1534 	if (is_npc_intf_rx(intf))
1535 		return;
1536 
1537 	rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1538 		    mkex->keyx_cfg[NIX_INTF_TX]);
1539 
1540 	/* Program LDATA */
1541 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
1542 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
1543 			for (ld = 0; ld < NPC_MAX_LD; ld++)
1544 				SET_KEX_LD(intf, lid, lt, ld,
1545 					   mkex->intf_lid_lt_ld[NIX_INTF_TX]
1546 					   [lid][lt][ld]);
1547 		}
1548 	}
1549 	/* Program LFLAGS */
1550 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
1551 		for (fl = 0; fl < NPC_MAX_LFL; fl++)
1552 			SET_KEX_LDFLAGS(intf, ld, fl,
1553 					mkex->intf_ld_flags[NIX_INTF_TX]
1554 					[ld][fl]);
1555 	}
1556 }
1557 
1558 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1559 				     const struct npc_mcam_kex *mkex)
1560 {
1561 	struct rvu_hwinfo *hw = rvu->hw;
1562 	u8 intf;
1563 	int ld;
1564 
1565 	for (ld = 0; ld < NPC_MAX_LD; ld++)
1566 		rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1567 			    mkex->kex_ld_flags[ld]);
1568 
1569 	for (intf = 0; intf < hw->npc_intfs; intf++) {
1570 		npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1571 		npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1572 	}
1573 
1574 	/* Programme mkex hash profile */
1575 	npc_program_mkex_hash(rvu, blkaddr);
1576 }
1577 
1578 int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1579 			  u64 *size)
1580 {
1581 	u64 prfl_addr, prfl_sz;
1582 
1583 	if (!rvu->fwdata)
1584 		return -EINVAL;
1585 
1586 	prfl_addr = rvu->fwdata->mcam_addr;
1587 	prfl_sz = rvu->fwdata->mcam_sz;
1588 
1589 	if (!prfl_addr || !prfl_sz)
1590 		return -EINVAL;
1591 
1592 	*prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1593 	if (!(*prfl_img_addr))
1594 		return -ENOMEM;
1595 
1596 	*size = prfl_sz;
1597 
1598 	return 0;
1599 }
1600 
1601 /* strtoull of "mkexprof" with base:36 */
1602 #define MKEX_END_SIGN  0xdeadbeef
1603 
1604 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1605 				  const char *mkex_profile)
1606 {
1607 	struct device *dev = &rvu->pdev->dev;
1608 	struct npc_mcam_kex *mcam_kex;
1609 	void __iomem *mkex_prfl_addr = NULL;
1610 	u64 prfl_sz;
1611 	int ret;
1612 
1613 	/* If user not selected mkex profile */
1614 	if (rvu->kpu_fwdata_sz ||
1615 	    !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1616 		goto program_mkex;
1617 
1618 	/* Setting up the mapping for mkex profile image */
1619 	ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1620 	if (ret < 0)
1621 		goto program_mkex;
1622 
1623 	mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1624 
1625 	while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1626 		/* Compare with mkex mod_param name string */
1627 		if (mcam_kex->mkex_sign == MKEX_SIGN &&
1628 		    !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1629 			/* Due to an errata (35786) in A0/B0 pass silicon,
1630 			 * parse nibble enable configuration has to be
1631 			 * identical for both Rx and Tx interfaces.
1632 			 */
1633 			if (!is_rvu_96xx_B0(rvu) ||
1634 			    mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1635 				rvu->kpu.mcam_kex_prfl.mkex = mcam_kex;
1636 			goto program_mkex;
1637 		}
1638 
1639 		mcam_kex++;
1640 		prfl_sz -= sizeof(struct npc_mcam_kex);
1641 	}
1642 	dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1643 
1644 program_mkex:
1645 	dev_info(rvu->dev, "Using %s mkex profile\n",
1646 		 rvu->kpu.mcam_kex_prfl.mkex->name);
1647 	/* Program selected mkex profile */
1648 	npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mcam_kex_prfl.mkex);
1649 	if (mkex_prfl_addr)
1650 		iounmap(mkex_prfl_addr);
1651 }
1652 
1653 void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1654 			  const struct npc_kpu_profile_action *kpuaction,
1655 			  int kpu, int entry, bool pkind)
1656 {
1657 	struct npc_kpu_action0 action0 = {0};
1658 	struct npc_kpu_action1 action1 = {0};
1659 	u64 reg;
1660 
1661 	action1.errlev = kpuaction->errlev;
1662 	action1.errcode = kpuaction->errcode;
1663 	action1.dp0_offset = kpuaction->dp0_offset;
1664 	action1.dp1_offset = kpuaction->dp1_offset;
1665 	action1.dp2_offset = kpuaction->dp2_offset;
1666 
1667 	if (pkind)
1668 		reg = NPC_AF_PKINDX_ACTION1(entry);
1669 	else
1670 		reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1671 
1672 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1673 
1674 	action0.byp_count = kpuaction->bypass_count;
1675 	action0.capture_ena = kpuaction->cap_ena;
1676 	action0.parse_done = kpuaction->parse_done;
1677 	action0.next_state = kpuaction->next_state;
1678 	action0.capture_lid = kpuaction->lid;
1679 	action0.capture_ltype = kpuaction->ltype;
1680 	action0.capture_flags = kpuaction->flags;
1681 	action0.ptr_advance = kpuaction->ptr_advance;
1682 	action0.var_len_offset = kpuaction->offset;
1683 	action0.var_len_mask = kpuaction->mask;
1684 	action0.var_len_right = kpuaction->right;
1685 	action0.var_len_shift = kpuaction->shift;
1686 
1687 	if (pkind)
1688 		reg = NPC_AF_PKINDX_ACTION0(entry);
1689 	else
1690 		reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1691 
1692 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1693 }
1694 
1695 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1696 			      const struct npc_kpu_profile_cam *kpucam,
1697 			      int kpu, int entry)
1698 {
1699 	const struct npc_kpu_profile_cam2 *kpucam2 = (void *)kpucam;
1700 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1701 	struct npc_kpu_cam cam0 = {0};
1702 	struct npc_kpu_cam cam1 = {0};
1703 	u64 *val = (u64 *)&cam1;
1704 	u64 *mask = (u64 *)&cam0;
1705 
1706 	cam1.state = kpucam->state & kpucam->state_mask;
1707 	cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1708 	cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1709 	cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1710 
1711 	cam0.state = ~kpucam->state & kpucam->state_mask;
1712 	cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1713 	cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1714 	cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1715 
1716 	if (profile->from_fs) {
1717 		u8 ptype = kpucam2->ptype;
1718 		u8 pmask = kpucam2->ptype_mask;
1719 
1720 		*val |= FIELD_PREP(GENMASK_ULL(57, 56), ptype & pmask);
1721 		*mask |= FIELD_PREP(GENMASK_ULL(57, 56), ~ptype & pmask);
1722 	}
1723 
1724 	rvu_write64(rvu, blkaddr,
1725 		    NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1726 	rvu_write64(rvu, blkaddr,
1727 		    NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1728 }
1729 
1730 u64 npc_enable_mask(int count)
1731 {
1732 	return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1733 }
1734 
1735 struct npc_kpu_profile_action *
1736 npc_get_ikpu_nth_entry(struct rvu *rvu, int n)
1737 {
1738 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1739 
1740 	if (profile->from_fs)
1741 		return &profile->ikpu2[n];
1742 
1743 	return &profile->ikpu[n];
1744 }
1745 
1746 int
1747 npc_get_num_kpu_cam_entries(struct rvu *rvu,
1748 			    const struct npc_kpu_profile *kpu_pfl)
1749 {
1750 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1751 
1752 	if (profile->from_fs)
1753 		return kpu_pfl->cam_entries2;
1754 
1755 	return kpu_pfl->cam_entries;
1756 }
1757 
1758 struct npc_kpu_profile_cam *
1759 npc_get_kpu_cam_nth_entry(struct rvu *rvu,
1760 			  const struct npc_kpu_profile *kpu_pfl, int n)
1761 {
1762 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1763 
1764 	if (profile->from_fs)
1765 		return (void *)&kpu_pfl->cam2[n];
1766 
1767 	return (void *)&kpu_pfl->cam[n];
1768 }
1769 
1770 int
1771 npc_get_num_kpu_action_entries(struct rvu *rvu,
1772 			       const struct npc_kpu_profile *kpu_pfl)
1773 {
1774 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1775 
1776 	if (profile->from_fs)
1777 		return kpu_pfl->action_entries2;
1778 
1779 	return kpu_pfl->action_entries;
1780 }
1781 
1782 struct npc_kpu_profile_action *
1783 npc_get_kpu_action_nth_entry(struct rvu *rvu,
1784 			     const struct npc_kpu_profile *kpu_pfl,
1785 			     int n)
1786 {
1787 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1788 
1789 	if (profile->from_fs)
1790 		return (void *)&kpu_pfl->action2[n];
1791 
1792 	return (void *)&kpu_pfl->action[n];
1793 }
1794 
1795 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1796 				    const struct npc_kpu_profile *profile)
1797 {
1798 	int num_cam_entries, num_action_entries;
1799 	int entry, num_entries, max_entries;
1800 	u64 entry_mask;
1801 
1802 	num_cam_entries = npc_get_num_kpu_cam_entries(rvu, profile);
1803 	num_action_entries = npc_get_num_kpu_action_entries(rvu, profile);
1804 
1805 	if (num_cam_entries != num_action_entries) {
1806 		dev_err(rvu->dev,
1807 			"KPU%d: CAM and action entries [%d != %d] not equal\n",
1808 			kpu, num_cam_entries, num_action_entries);
1809 	}
1810 
1811 	max_entries = rvu->hw->npc_kpu_entries;
1812 
1813 	WARN(num_cam_entries > max_entries,
1814 	     "KPU%u: err: hw max entries=%u, input entries=%u\n",
1815 	     kpu,  rvu->hw->npc_kpu_entries, num_cam_entries);
1816 
1817 	/* Program CAM match entries for previous KPU extracted data */
1818 	num_entries = min_t(int, num_cam_entries, max_entries);
1819 	for (entry = 0; entry < num_entries; entry++)
1820 		npc_config_kpucam(rvu, blkaddr,
1821 				  (void *)npc_get_kpu_cam_nth_entry(rvu, profile, entry),
1822 				  kpu, entry);
1823 
1824 	/* Program this KPU's actions */
1825 	num_entries = min_t(int, num_action_entries, max_entries);
1826 	for (entry = 0; entry < num_entries; entry++)
1827 		npc_config_kpuaction(rvu, blkaddr,
1828 				     (void *)npc_get_kpu_action_nth_entry(rvu, profile, entry),
1829 				     kpu, entry, false);
1830 
1831 	/* Enable all programmed entries */
1832 	num_entries = min_t(int, num_action_entries, num_cam_entries);
1833 	entry_mask = npc_enable_mask(num_entries);
1834 	/* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1835 	if (!rvu->kpu.custom)
1836 		entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1837 	rvu_write64(rvu, blkaddr,
1838 		    NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1839 	if (num_entries > 64) {
1840 		rvu_write64(rvu, blkaddr,
1841 			    NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1842 			    npc_enable_mask(num_entries - 64));
1843 	}
1844 
1845 	/* Enable this KPU */
1846 	rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1847 }
1848 
1849 static void npc_prepare_default_kpu(struct rvu *rvu,
1850 				    struct npc_kpu_profile_adapter *profile)
1851 {
1852 	profile->custom = 0;
1853 	profile->name = def_pfl_name;
1854 	profile->version = NPC_KPU_PROFILE_VER;
1855 	profile->ikpu = ikpu_action_entries;
1856 	profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1857 	profile->kpu = npc_kpu_profiles;
1858 	profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1859 	profile->lt_def = &npc_lt_defaults;
1860 	profile->mkex_hash = &npc_mkex_hash_default;
1861 
1862 	if (!is_cn20k(rvu->pdev)) {
1863 		profile->mcam_kex_prfl.mkex = &npc_mkex_default;
1864 		return;
1865 	}
1866 
1867 	profile->mcam_kex_prfl.mkex_extr = npc_mkex_extr_default_get();
1868 	ikpu_action_entries[NPC_RX_CPT_HDR_PKIND].offset = 6;
1869 	ikpu_action_entries[NPC_RX_CPT_HDR_PKIND].mask = 0xe0;
1870 	ikpu_action_entries[NPC_RX_CPT_HDR_PKIND].shift = 0x5;
1871 	ikpu_action_entries[NPC_RX_CPT_HDR_PKIND].right = 0x1;
1872 
1873 	npc_cn20k_update_action_entries_n_flags(rvu, profile);
1874 }
1875 
1876 static int npc_alloc_kpu_cam2_n_action2(struct rvu *rvu, int kpu_num,
1877 					int num_entries)
1878 {
1879 	struct npc_kpu_profile_adapter *adapter = &rvu->kpu;
1880 	struct npc_kpu_profile *kpu;
1881 
1882 	kpu = &adapter->kpu[kpu_num];
1883 
1884 	kpu->cam2 = devm_kzalloc_objs(rvu->dev, *kpu->cam2, num_entries);
1885 	if (!kpu->cam2)
1886 		return -ENOMEM;
1887 
1888 	kpu->action2 = devm_kzalloc_objs(rvu->dev, *kpu->action2, num_entries);
1889 	if (!kpu->action2)
1890 		return -ENOMEM;
1891 
1892 	return 0;
1893 }
1894 
1895 static int npc_apply_custom_kpu_from_fw(struct rvu *rvu,
1896 					struct npc_kpu_profile_adapter *profile)
1897 {
1898 	size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1899 	const struct npc_kpu_profile_fwdata *fw;
1900 	struct npc_kpu_profile_action *action;
1901 	struct npc_kpu_profile_cam *cam;
1902 	struct npc_kpu_fwdata *fw_kpu;
1903 	int entries, entry, kpu;
1904 
1905 	fw = rvu->kpu_fwdata;
1906 
1907 	for (kpu = 0; kpu < fw->kpus; kpu++) {
1908 		if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1909 			dev_warn(rvu->dev,
1910 				 "Profile size mismatch on KPU%i parsing\n",
1911 				 kpu + 1);
1912 			return -EINVAL;
1913 		}
1914 
1915 		fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1916 		if (fw_kpu->entries < 0) {
1917 			dev_warn(rvu->dev,
1918 				 "Profile entries is negative on KPU%i parsing\n",
1919 				 kpu + 1);
1920 			return -EINVAL;
1921 		}
1922 
1923 		if (fw_kpu->entries > KPU_MAX_CST_ENT)
1924 			dev_warn(rvu->dev,
1925 				 "Too many custom entries on KPU%d: %d > %d\n",
1926 				 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1927 		entries = min_t(int, fw_kpu->entries, KPU_MAX_CST_ENT);
1928 		cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1929 		offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1930 		action = (struct npc_kpu_profile_action *)(fw->data + offset);
1931 		offset += fw_kpu->entries * sizeof(*action);
1932 		if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1933 			dev_warn(rvu->dev,
1934 				 "Profile size mismatch on KPU%i parsing.\n",
1935 				 kpu + 1);
1936 			return -EINVAL;
1937 		}
1938 		for (entry = 0; entry < entries; entry++) {
1939 			profile->kpu[kpu].cam[entry] = cam[entry];
1940 			profile->kpu[kpu].action[entry] = action[entry];
1941 		}
1942 	}
1943 
1944 	return 0;
1945 }
1946 
1947 static int npc_apply_custom_kpu_from_fs(struct rvu *rvu,
1948 					struct npc_kpu_profile_adapter *profile)
1949 {
1950 	size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1951 	const struct npc_kpu_profile_fwdata *fw;
1952 	struct npc_kpu_profile_action *action;
1953 	struct npc_kpu_profile_cam2 *cam2;
1954 	struct npc_kpu_fwdata *fw_kpu;
1955 	int entries, ret, entry, kpu;
1956 
1957 	fw = rvu->kpu_fwdata;
1958 
1959 	/* Binary blob contains ikpu actions entries at start of data[0] */
1960 	profile->ikpu2 = devm_kzalloc_objs(rvu->dev, *profile->ikpu2,
1961 					   ARRAY_SIZE(ikpu_action_entries));
1962 	if (!profile->ikpu2)
1963 		return -ENOMEM;
1964 
1965 	action = (struct npc_kpu_profile_action *)(fw->data + offset);
1966 
1967 	if (rvu->kpu_fwdata_sz < hdr_sz + sizeof(ikpu_action_entries))
1968 		return -EINVAL;
1969 
1970 	/* The firmware layout does dependent on the internal size of
1971 	 * ikpu_action_entries.
1972 	 */
1973 	memcpy((void *)profile->ikpu2, action, sizeof(ikpu_action_entries));
1974 	offset += sizeof(ikpu_action_entries);
1975 
1976 	for (kpu = 0; kpu < fw->kpus; kpu++) {
1977 		if (rvu->kpu_fwdata_sz < hdr_sz + offset + sizeof(*fw_kpu)) {
1978 			dev_warn(rvu->dev,
1979 				 "profile size mismatch on kpu%i parsing\n",
1980 				 kpu + 1);
1981 			return -EINVAL;
1982 		}
1983 
1984 		fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1985 		if (fw_kpu->entries <= 0) {
1986 			dev_warn(rvu->dev,
1987 				 "Invalid kpu entries on KPU%d\n", kpu);
1988 			return -EINVAL;
1989 		}
1990 
1991 		entries = min_t(int, fw_kpu->entries, rvu->hw->npc_kpu_entries);
1992 		dev_info(rvu->dev,
1993 			 "Loading %u entries on KPU%d\n", entries, kpu);
1994 
1995 		cam2 = (struct npc_kpu_profile_cam2 *)fw_kpu->data;
1996 		offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam2);
1997 		action = (struct npc_kpu_profile_action *)(fw->data + offset);
1998 		offset += fw_kpu->entries * sizeof(*action);
1999 		if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
2000 			dev_warn(rvu->dev,
2001 				 "profile size mismatch on kpu%i parsing.\n",
2002 				 kpu + 1);
2003 			return -EINVAL;
2004 		}
2005 
2006 		profile->kpu[kpu].cam_entries2 = entries;
2007 		profile->kpu[kpu].action_entries2 = entries;
2008 		ret = npc_alloc_kpu_cam2_n_action2(rvu, kpu, entries);
2009 		if (ret) {
2010 			dev_warn(rvu->dev,
2011 				 "profile entry allocation failed for kpu=%d for %d entries\n",
2012 				 kpu, entries);
2013 			return -EINVAL;
2014 		}
2015 
2016 		for (entry = 0; entry < entries; entry++) {
2017 			profile->kpu[kpu].cam2[entry] = cam2[entry];
2018 			profile->kpu[kpu].action2[entry] = action[entry];
2019 		}
2020 	}
2021 
2022 	return 0;
2023 }
2024 
2025 static int npc_apply_custom_kpu(struct rvu *rvu,
2026 				struct npc_kpu_profile_adapter *profile,
2027 				bool from_fs, int *fw_kpus)
2028 {
2029 	size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata);
2030 	const struct npc_kpu_profile_fwdata *fw;
2031 	struct npc_kpu_profile_fwdata *sfw;
2032 
2033 	if (is_cn20k(rvu->pdev))
2034 		return npc_cn20k_apply_custom_kpu(rvu, profile);
2035 
2036 	if (rvu->kpu_fwdata_sz < hdr_sz) {
2037 		dev_warn(rvu->dev, "Invalid KPU profile size\n");
2038 		return -EINVAL;
2039 	}
2040 
2041 	fw = rvu->kpu_fwdata;
2042 	if (le64_to_cpu(fw->signature) != KPU_SIGN) {
2043 		dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
2044 			 fw->signature);
2045 		return -EINVAL;
2046 	}
2047 	/* Verify if the using known profile structure */
2048 	if (NPC_KPU_VER_MAJ(profile->version) >
2049 	    NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
2050 		dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
2051 			 NPC_KPU_VER_MAJ(profile->version),
2052 			 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
2053 		return -EINVAL;
2054 	}
2055 	/* Verify if profile is aligned with the required kernel changes */
2056 	if (NPC_KPU_VER_MIN(profile->version) <
2057 	    NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
2058 		dev_warn(rvu->dev,
2059 			 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
2060 			 NPC_KPU_VER_MAJ(profile->version),
2061 			 NPC_KPU_VER_MIN(profile->version),
2062 			 NPC_KPU_VER_PATCH(profile->version),
2063 			 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
2064 			 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
2065 			 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
2066 		return -EINVAL;
2067 	}
2068 	/* Verify if profile fits the HW */
2069 	if (fw->kpus > rvu->hw->npc_kpus) {
2070 		dev_warn(rvu->dev, "Not enough KPUs: %d > %d\n", fw->kpus,
2071 			 rvu->hw->npc_kpus);
2072 		return -EINVAL;
2073 	}
2074 
2075 	/* Check if there is enough memory for fw loading.
2076 	 * Check if there is enough entries for profile->kpu[] to
2077 	 * set cam_entries2 and action_entries2
2078 	 */
2079 	if (fw->kpus > profile->kpus) {
2080 		dev_warn(rvu->dev, "Not enough KPUs: %d > %zu\n", fw->kpus,
2081 			 profile->kpus);
2082 		return -EINVAL;
2083 	}
2084 
2085 	*fw_kpus = fw->kpus;
2086 
2087 	sfw = devm_kzalloc_objs(rvu->dev, *sfw, 1);
2088 	if (!sfw)
2089 		return -ENOMEM;
2090 
2091 	memcpy(sfw, fw, sizeof(*sfw));
2092 
2093 	profile->custom = 1;
2094 	profile->name = sfw->name;
2095 	profile->version = le64_to_cpu(fw->version);
2096 	profile->mcam_kex_prfl.mkex = &sfw->mkex;
2097 	profile->lt_def = &sfw->lt_def;
2098 
2099 	return from_fs ? npc_apply_custom_kpu_from_fs(rvu, profile) :
2100 		npc_apply_custom_kpu_from_fw(rvu, profile);
2101 }
2102 
2103 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
2104 				 u64 prfl_sz, const char *kpu_profile)
2105 {
2106 	struct npc_kpu_profile_fwdata *kpu_data = NULL;
2107 	int rc = -EINVAL;
2108 
2109 	kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
2110 	if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
2111 	    !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
2112 		dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
2113 			 kpu_profile);
2114 		rvu->kpu_fwdata = kpu_data;
2115 		rvu->kpu_fwdata_sz = prfl_sz;
2116 		rvu->kpu_prfl_addr = prfl_addr;
2117 		rc = 0;
2118 	}
2119 
2120 	return rc;
2121 }
2122 
2123 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
2124 					 const char *kpu_profile)
2125 {
2126 	struct npc_coalesced_kpu_prfl *img_data = NULL;
2127 	int i = 0, rc = -EINVAL;
2128 	void __iomem *kpu_prfl_addr;
2129 	u32 offset;
2130 
2131 	img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
2132 	if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
2133 	    !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
2134 		/* Loaded profile is a single KPU profile. */
2135 		rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
2136 					   prfl_sz, kpu_profile);
2137 		goto done;
2138 	}
2139 
2140 	/* Loaded profile is coalesced image, offset of first KPU profile.*/
2141 	offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
2142 		(img_data->num_prfl * sizeof(uint16_t));
2143 	/* Check if mapped image is coalesced image. */
2144 	while (i < img_data->num_prfl) {
2145 		/* Profile image offsets are rounded up to next 8 multiple.*/
2146 		offset = ALIGN_8B_CEIL(offset);
2147 		kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
2148 					 offset);
2149 		rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
2150 					   img_data->prfl_sz[i], kpu_profile);
2151 		if (!rc)
2152 			break;
2153 		/* Calculating offset of profile image based on profile size.*/
2154 		offset += img_data->prfl_sz[i];
2155 		i++;
2156 	}
2157 done:
2158 	return rc;
2159 }
2160 
2161 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
2162 {
2163 	int ret = -EINVAL;
2164 	u64 prfl_sz;
2165 
2166 	/* Setting up the mapping for NPC profile image */
2167 	ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
2168 	if (ret < 0)
2169 		goto done;
2170 
2171 	/* Detect if profile is coalesced or single KPU profile and load */
2172 	ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
2173 	if (ret == 0)
2174 		goto done;
2175 
2176 	/* Cleaning up if KPU profile image from fwdata is not valid. */
2177 	if (rvu->kpu_prfl_addr) {
2178 		iounmap(rvu->kpu_prfl_addr);
2179 		rvu->kpu_prfl_addr = NULL;
2180 		rvu->kpu_fwdata_sz = 0;
2181 		rvu->kpu_fwdata = NULL;
2182 	}
2183 
2184 done:
2185 	return ret;
2186 }
2187 
2188 static int npc_load_kpu_profile_from_fw(struct rvu *rvu)
2189 {
2190 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
2191 	const char *kpu_profile = rvu->kpu_pfl_name;
2192 	int fw_kpus = 0;
2193 
2194 	/* Loading the KPU profile using firmware database */
2195 	if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
2196 		return -EFAULT;
2197 
2198 	/* Apply profile customization if firmware was loaded. */
2199 	if (!rvu->kpu_fwdata_sz ||
2200 	    npc_apply_custom_kpu(rvu, profile, false, &fw_kpus)) {
2201 		/* If image from firmware filesystem fails to load or invalid
2202 		 * retry with firmware database method.
2203 		 */
2204 		if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
2205 			/* Loading image from firmware database failed. */
2206 			if (rvu->kpu_prfl_addr) {
2207 				iounmap(rvu->kpu_prfl_addr);
2208 				rvu->kpu_prfl_addr = NULL;
2209 			} else {
2210 				kfree(rvu->kpu_fwdata);
2211 			}
2212 			rvu->kpu_fwdata = NULL;
2213 			rvu->kpu_fwdata_sz = 0;
2214 		}
2215 
2216 		dev_warn(rvu->dev,
2217 			 "Can't load KPU profile %s. Using default.\n",
2218 			 kpu_profile);
2219 		kfree(rvu->kpu_fwdata);
2220 		rvu->kpu_fwdata = NULL;
2221 		return -EFAULT;
2222 	}
2223 
2224 	dev_info(rvu->dev, "Using custom profile '%.32s', version %d.%d.%d\n",
2225 		 profile->name, NPC_KPU_VER_MAJ(profile->version),
2226 		 NPC_KPU_VER_MIN(profile->version),
2227 		 NPC_KPU_VER_PATCH(profile->version));
2228 
2229 	return 0;
2230 }
2231 
2232 static int npc_load_kpu_profile_from_fs(struct rvu *rvu)
2233 {
2234 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
2235 	const char *kpu_profile = rvu->kpu_pfl_name;
2236 	const struct firmware *fw = NULL;
2237 	int ret, fw_kpus = 0;
2238 	char path[512] = "kpu/";
2239 
2240 	if (strlen(kpu_profile) > sizeof(path) - strlen("kpu/") - 1) {
2241 		dev_err(rvu->dev, "kpu profile name is too big\n");
2242 		return -ENOSPC;
2243 	}
2244 
2245 	strcat(path, kpu_profile);
2246 
2247 	if (request_firmware_direct(&fw, path, rvu->dev))
2248 		return -ENOENT;
2249 
2250 	dev_info(rvu->dev, "Loading KPU profile from filesystem: %s\n",
2251 		 path);
2252 
2253 	rvu->kpu_fwdata = fw->data;
2254 	rvu->kpu_fwdata_sz = fw->size;
2255 
2256 	ret = npc_apply_custom_kpu(rvu, profile, true, &fw_kpus);
2257 	release_firmware(fw);
2258 	rvu->kpu_fwdata = NULL;
2259 
2260 	if (ret) {
2261 		rvu->kpu_fwdata_sz = 0;
2262 		dev_err(rvu->dev,
2263 			"Loading KPU profile from filesystem failed\n");
2264 		return ret;
2265 	}
2266 
2267 	/* In firmware loading from filesystem method, all entries are from
2268 	 * same binary blob.
2269 	 */
2270 	rvu->kpu.kpus = fw_kpus;
2271 	profile->kpus = fw_kpus;
2272 	profile->from_fs = true;
2273 	return 0;
2274 }
2275 
2276 void npc_load_kpu_profile(struct rvu *rvu)
2277 {
2278 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
2279 	const char *kpu_profile = rvu->kpu_pfl_name;
2280 
2281 	profile->from_fs = false;
2282 
2283 	npc_prepare_default_kpu(rvu, profile);
2284 
2285 	/* If user not specified profile customization */
2286 	if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
2287 		return;
2288 
2289 	/* Order of preceedence for load loading NPC profile (high to low)
2290 	 * Firmware binary in filesystem.
2291 	 * Firmware database method.
2292 	 * Default KPU profile.
2293 	 */
2294 
2295 	/* Filesystem-based KPU loading is not supported on cn20k.
2296 	 * npc_prepare_default_kpu() was invoked earlier, but control
2297 	 * reached this point because the default profile was not selected.
2298 	 * No need to call it again.
2299 	 */
2300 	if (!is_cn20k(rvu->pdev)) {
2301 		if (!npc_load_kpu_profile_from_fs(rvu))
2302 			return;
2303 	}
2304 
2305 	/* First prepare default KPU, then we'll customize top entries. */
2306 	npc_prepare_default_kpu(rvu, profile);
2307 	if (!npc_load_kpu_profile_from_fw(rvu))
2308 		return;
2309 
2310 	npc_prepare_default_kpu(rvu, profile);
2311 }
2312 
2313 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
2314 {
2315 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
2316 	struct rvu_hwinfo *hw = rvu->hw;
2317 	int num_pkinds, num_kpus, idx;
2318 
2319 	/* Disable all KPUs and their entries */
2320 	for (idx = 0; idx < hw->npc_kpus; idx++) {
2321 		rvu_write64(rvu, blkaddr,
2322 			    NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
2323 		rvu_write64(rvu, blkaddr,
2324 			    NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
2325 		rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
2326 	}
2327 
2328 	/* Load and customize KPU profile. */
2329 	npc_load_kpu_profile(rvu);
2330 
2331 	/* First program IKPU profile i.e PKIND configs.
2332 	 * Check HW max count to avoid configuring junk or
2333 	 * writing to unsupported CSR addresses.
2334 	 */
2335 	num_pkinds = rvu->kpu.pkinds;
2336 	num_pkinds = min_t(int, hw->npc_pkinds, num_pkinds);
2337 
2338 	for (idx = 0; idx < num_pkinds; idx++)
2339 		npc_config_kpuaction(rvu, blkaddr,
2340 				     npc_get_ikpu_nth_entry(rvu, idx),
2341 				     0, idx, true);
2342 
2343 	/* Program KPU CAM and Action profiles */
2344 	num_kpus = rvu->kpu.kpus;
2345 	num_kpus = min_t(int, hw->npc_kpus, num_kpus);
2346 
2347 	for (idx = 0; idx < num_kpus; idx++)
2348 		npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
2349 
2350 	if (profile->from_fs) {
2351 		rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_TYPE(54), 0x03);
2352 		rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_TYPE(58), 0x03);
2353 	}
2354 }
2355 
2356 void npc_mcam_rsrcs_deinit(struct rvu *rvu)
2357 {
2358 	struct npc_mcam *mcam = &rvu->hw->mcam;
2359 
2360 	bitmap_free(mcam->bmap);
2361 	bitmap_free(mcam->bmap_reverse);
2362 	kfree(mcam->entry2pfvf_map);
2363 	kfree(mcam->cntr2pfvf_map);
2364 	kfree(mcam->entry2cntr_map);
2365 	kfree(mcam->cntr_refcnt);
2366 	kfree(mcam->entry2target_pffunc);
2367 	kfree(mcam->counters.bmap);
2368 }
2369 
2370 int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
2371 {
2372 	int nixlf_count = rvu_get_nixlf_count(rvu);
2373 	struct npc_mcam *mcam = &rvu->hw->mcam;
2374 	int rsvd, err;
2375 	u16 index;
2376 	int cntr;
2377 	u64 cfg;
2378 
2379 	cfg = (rvu_read64(rvu, blkaddr,
2380 			  NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
2381 	mcam->keysize = cfg;
2382 
2383 	/* Number of banks combined per MCAM entry */
2384 	if (is_cn20k(rvu->pdev)) {
2385 		/* In cn20k, x2 entries is allowed for x4 profile.
2386 		 * set total_entries as 8192 * 2 and key size as x2.
2387 		 */
2388 		mcam->total_entries = mcam->banks * mcam->banksize;
2389 		if (cfg == NPC_MCAM_KEY_X2)
2390 			mcam->banks_per_entry = 1;
2391 		else
2392 			mcam->banks_per_entry = 2;
2393 
2394 		rsvd = 0;
2395 	} else {
2396 		mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) *
2397 						mcam->banksize;
2398 
2399 		if (cfg == NPC_MCAM_KEY_X4)
2400 			mcam->banks_per_entry = 4;
2401 		else if (cfg == NPC_MCAM_KEY_X2)
2402 			mcam->banks_per_entry = 2;
2403 		else
2404 			mcam->banks_per_entry = 1;
2405 
2406 		/* Reserve one MCAM entry for each of the NIX LF to
2407 		 * guarantee space to install default matching DMAC rule.
2408 		 * Also reserve 2 MCAM entries for each PF for default
2409 		 * channel based matching or 'bcast & promisc' matching to
2410 		 * support BCAST and PROMISC modes of operation for PFs.
2411 		 * PF0 is excluded.
2412 		 */
2413 		rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
2414 			((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
2415 		if (mcam->total_entries <= rsvd) {
2416 			dev_warn(rvu->dev,
2417 				 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
2418 				 mcam->total_entries);
2419 			return -ENOMEM;
2420 		}
2421 	}
2422 
2423 	mcam->bmap_entries = mcam->total_entries - rsvd;
2424 	/* cn20k does not need offsets to alloc mcam entries */
2425 	if (!is_cn20k(rvu->pdev)) {
2426 		mcam->nixlf_offset = mcam->bmap_entries;
2427 		mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
2428 	}
2429 
2430 	/* Allocate bitmaps for managing MCAM entries */
2431 	mcam->bmap = bitmap_zalloc(mcam->bmap_entries, GFP_KERNEL);
2432 	if (!mcam->bmap)
2433 		return -ENOMEM;
2434 
2435 	mcam->bmap_reverse = bitmap_zalloc(mcam->bmap_entries, GFP_KERNEL);
2436 	if (!mcam->bmap_reverse)
2437 		goto free_bmap;
2438 
2439 	mcam->bmap_fcnt = mcam->bmap_entries;
2440 
2441 	/* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
2442 	mcam->entry2pfvf_map = kzalloc_objs(u16, mcam->bmap_entries);
2443 
2444 	if (!mcam->entry2pfvf_map)
2445 		goto free_bmap_reverse;
2446 
2447 	/* Reserve 1/8th of MCAM entries at the bottom for low priority
2448 	 * allocations and another 1/8th at the top for high priority
2449 	 * allocations.
2450 	 */
2451 	if (!is_cn20k(rvu->pdev)) {
2452 		mcam->lprio_count = mcam->bmap_entries / 8;
2453 		if (mcam->lprio_count > BITS_PER_LONG)
2454 			mcam->lprio_count = round_down(mcam->lprio_count,
2455 						       BITS_PER_LONG);
2456 		mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
2457 		mcam->hprio_count = mcam->lprio_count;
2458 		mcam->hprio_end = mcam->hprio_count;
2459 	}
2460 
2461 	/* Allocate bitmap for managing MCAM counters and memory
2462 	 * for saving counter to RVU PFFUNC allocation mapping.
2463 	 */
2464 	err = rvu_alloc_bitmap(&mcam->counters);
2465 	if (err)
2466 		goto free_entry_map;
2467 
2468 	mcam->cntr2pfvf_map = kzalloc_objs(u16, mcam->counters.max);
2469 	if (!mcam->cntr2pfvf_map)
2470 		goto free_cntr_bmap;
2471 
2472 	/* Alloc memory for MCAM entry to counter mapping and for tracking
2473 	 * counter's reference count.
2474 	 */
2475 	mcam->entry2cntr_map = kzalloc_objs(u16, mcam->total_entries);
2476 	if (!mcam->entry2cntr_map)
2477 		goto free_cntr_map;
2478 
2479 	mcam->cntr_refcnt = kzalloc_objs(u16, mcam->counters.max);
2480 	if (!mcam->cntr_refcnt)
2481 		goto free_entry_cntr_map;
2482 
2483 	/* Alloc memory for saving target device of mcam rule */
2484 	mcam->entry2target_pffunc = kzalloc_objs(u16, mcam->total_entries);
2485 	if (!mcam->entry2target_pffunc)
2486 		goto free_cntr_refcnt;
2487 
2488 	for (index = 0; index < mcam->bmap_entries; index++)
2489 		mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2490 
2491 	for (index = 0; index < mcam->total_entries; index++)
2492 		mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2493 
2494 	for (cntr = 0; cntr < mcam->counters.max; cntr++)
2495 		mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2496 
2497 	mutex_init(&mcam->lock);
2498 
2499 	return 0;
2500 
2501 free_cntr_refcnt:
2502 	kfree(mcam->cntr_refcnt);
2503 free_entry_cntr_map:
2504 	kfree(mcam->entry2cntr_map);
2505 free_cntr_map:
2506 	kfree(mcam->cntr2pfvf_map);
2507 free_cntr_bmap:
2508 	kfree(mcam->counters.bmap);
2509 free_entry_map:
2510 	kfree(mcam->entry2pfvf_map);
2511 free_bmap_reverse:
2512 	bitmap_free(mcam->bmap_reverse);
2513 free_bmap:
2514 	bitmap_free(mcam->bmap);
2515 
2516 	return -ENOMEM;
2517 }
2518 
2519 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
2520 {
2521 	struct npc_pkind *pkind = &rvu->hw->pkind;
2522 	struct npc_mcam *mcam = &rvu->hw->mcam;
2523 	struct rvu_hwinfo *hw = rvu->hw;
2524 	u64 npc_const, npc_const1;
2525 	u64 npc_const2 = 0;
2526 
2527 	npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
2528 	npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
2529 	if (npc_const1 & BIT_ULL(63))
2530 		npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
2531 
2532 	pkind->rsrc.max = NPC_UNRESERVED_PKIND_COUNT;
2533 	hw->npc_pkinds = (npc_const1 >> 12) & 0xFFULL;
2534 	hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
2535 	hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
2536 	/* For Cn20k silicon, check if enhanced parser
2537 	 * is present, then set the NUM_KPMS = NUM_KPUS / 2 and
2538 	 * number of LDATA extractors per KEX.
2539 	 */
2540 	if (is_cn20k(rvu->pdev)) {
2541 		if (!(npc_const1 & BIT_ULL(62))) {
2542 			WARN(1, "Enhanced parser is not supported\n");
2543 			return;
2544 		}
2545 		hw->npc_kpms = hw->npc_kpus / 2;
2546 		hw->npc_kex_extr = (npc_const1 >> 36) & 0x3FULL;
2547 	}
2548 
2549 	hw->npc_intfs = npc_const & 0xFULL;
2550 	hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
2551 
2552 	mcam->banks = (npc_const >> 44) & 0xFULL;
2553 	mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
2554 	hw->npc_stat_ena = BIT_ULL(9);
2555 	/* Extended set */
2556 	if (npc_const2) {
2557 		hw->npc_ext_set = true;
2558 		/* 96xx supports only match_stats and npc_counters
2559 		 * reflected in NPC_AF_CONST reg.
2560 		 * STAT_SEL and ENA are at [0:8] and 9 bit positions.
2561 		 * 98xx has both match_stat and ext and npc_counter
2562 		 * reflected in NPC_AF_CONST2
2563 		 * STAT_SEL_EXT added at [12:14] bit position.
2564 		 * cn10k supports only ext and hence npc_counters in
2565 		 * NPC_AF_CONST is 0 and npc_counters reflected in NPC_AF_CONST2.
2566 		 * STAT_SEL bitpos incremented from [0:8] to [0:11] and ENA bit moved to 63
2567 		 */
2568 		if (!hw->npc_counters)
2569 			hw->npc_stat_ena = BIT_ULL(63);
2570 		hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
2571 		mcam->banksize = npc_const2 & 0xFFFFULL;
2572 	}
2573 
2574 	mcam->counters.max = hw->npc_counters;
2575 }
2576 
2577 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
2578 {
2579 	const struct npc_mcam_kex_extr *mkex_extr;
2580 	struct npc_mcam *mcam = &rvu->hw->mcam;
2581 	struct rvu_hwinfo *hw = rvu->hw;
2582 	const struct npc_mcam_kex *mkex;
2583 	u64 nibble_ena, rx_kex, tx_kex;
2584 	u64 *keyx_cfg, reg;
2585 	u8 intf;
2586 
2587 	mkex_extr = rvu->kpu.mcam_kex_prfl.mkex_extr;
2588 	mkex = rvu->kpu.mcam_kex_prfl.mkex;
2589 
2590 	if (is_cn20k(rvu->pdev)) {
2591 		keyx_cfg = (u64 *)mkex_extr->keyx_cfg;
2592 	} else {
2593 		keyx_cfg = (u64 *)mkex->keyx_cfg;
2594 		/* Reserve last counter for MCAM RX miss action which is set to
2595 		 * drop packet. This way we will know how many pkts didn't
2596 		 * match any MCAM entry.
2597 		 */
2598 		mcam->counters.max--;
2599 		mcam->rx_miss_act_cntr = mcam->counters.max;
2600 	}
2601 
2602 	rx_kex = keyx_cfg[NIX_INTF_RX];
2603 	tx_kex = keyx_cfg[NIX_INTF_TX];
2604 
2605 	nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
2606 
2607 	nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
2608 	if (nibble_ena) {
2609 		tx_kex &= ~NPC_PARSE_NIBBLE;
2610 		tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
2611 		keyx_cfg[NIX_INTF_TX] = tx_kex;
2612 	}
2613 
2614 	/* Configure RX interfaces */
2615 	for (intf = 0; intf < hw->npc_intfs; intf++) {
2616 		if (is_npc_intf_tx(intf))
2617 			continue;
2618 
2619 		/* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
2620 		rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
2621 			    rx_kex);
2622 
2623 		if (is_cn20k(rvu->pdev))
2624 			reg = NPC_AF_INTFX_MISS_ACTX(intf, 0);
2625 		else
2626 			reg = NPC_AF_INTFX_MISS_ACT(intf);
2627 
2628 		/* If MCAM lookup doesn't result in a match, drop the received
2629 		 * packet. And map this action to a counter to count dropped
2630 		 * packets.
2631 		 */
2632 		rvu_write64(rvu, blkaddr, reg, NIX_RX_ACTIONOP_DROP);
2633 
2634 		if (is_cn20k(rvu->pdev))
2635 			continue;
2636 
2637 		/* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
2638 		 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
2639 		 */
2640 		rvu_write64(rvu, blkaddr,
2641 			    NPC_AF_INTFX_MISS_STAT_ACT(intf),
2642 			    ((mcam->rx_miss_act_cntr >> 9) << 12) |
2643 			    hw->npc_stat_ena | mcam->rx_miss_act_cntr);
2644 	}
2645 
2646 	/* Configure TX interfaces */
2647 	for (intf = 0; intf < hw->npc_intfs; intf++) {
2648 		if (is_npc_intf_rx(intf))
2649 			continue;
2650 
2651 		/* Extract Ltypes LID_LA to LID_LE */
2652 		rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
2653 			    tx_kex);
2654 
2655 		if (is_cn20k(rvu->pdev))
2656 			reg = NPC_AF_INTFX_MISS_ACTX(intf, 0);
2657 		else
2658 			reg = NPC_AF_INTFX_MISS_ACT(intf);
2659 
2660 		/* Set TX miss action to UCAST_DEFAULT i.e
2661 		 * transmit the packet on NIX LF SQ's default channel.
2662 		 */
2663 		rvu_write64(rvu, blkaddr, reg, NIX_TX_ACTIONOP_UCAST_DEFAULT);
2664 	}
2665 }
2666 
2667 int rvu_npc_init(struct rvu *rvu)
2668 {
2669 	struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
2670 	struct npc_pkind *pkind = &rvu->hw->pkind;
2671 	struct npc_mcam *mcam = &rvu->hw->mcam;
2672 	int blkaddr, entry, bank, err;
2673 
2674 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2675 	if (blkaddr < 0) {
2676 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
2677 		return -ENODEV;
2678 	}
2679 
2680 	rvu_npc_hw_init(rvu, blkaddr);
2681 
2682 	/* First disable all MCAM entries, to stop traffic towards NIXLFs */
2683 	for (bank = 0; bank < mcam->banks; bank++) {
2684 		for (entry = 0; entry < mcam->banksize; entry++)
2685 			rvu_write64(rvu, blkaddr,
2686 				    NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
2687 	}
2688 
2689 	err = rvu_alloc_bitmap(&pkind->rsrc);
2690 	if (err)
2691 		return err;
2692 	/* Reserve PKIND#0 for LBKs. Power reset value of LBK_CH_PKIND is '0',
2693 	 * no need to configure PKIND for all LBKs separately.
2694 	 */
2695 	rvu_alloc_rsrc(&pkind->rsrc);
2696 
2697 	/* Allocate mem for pkind to PF and channel mapping info */
2698 	pkind->pfchan_map = devm_kzalloc_objs(rvu->dev, u32, pkind->rsrc.max);
2699 	if (!pkind->pfchan_map)
2700 		return -ENOMEM;
2701 
2702 	/* Configure KPU profile */
2703 	if (is_cn20k(rvu->pdev))
2704 		npc_cn20k_parser_profile_init(rvu, blkaddr);
2705 	else
2706 		npc_parser_profile_init(rvu, blkaddr);
2707 
2708 	/* Config Outer L2, IPv4's NPC layer info */
2709 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
2710 		    (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
2711 		    kpu->lt_def->pck_ol2.ltype_mask);
2712 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
2713 		    (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
2714 		    kpu->lt_def->pck_oip4.ltype_mask);
2715 
2716 	/* Config Inner IPV4 NPC layer info */
2717 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2718 		    (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2719 		    kpu->lt_def->pck_iip4.ltype_mask);
2720 
2721 	/* Enable below for Rx pkts.
2722 	 * - Outer IPv4 header checksum validation.
2723 	 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2B].
2724 	 * - Detect outer L2 multicast address and set NPC_RESULT_S[L2M].
2725 	 * - Inner IPv4 header checksum validation.
2726 	 * - Set non zero checksum error code value
2727 	 */
2728 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2729 		    rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2730 		    ((u64)NPC_EC_OIP4_CSUM << 32) | (NPC_EC_IIP4_CSUM << 24) |
2731 		    BIT_ULL(7) | BIT_ULL(6) | BIT_ULL(2) | BIT_ULL(1));
2732 
2733 	rvu_npc_setup_interfaces(rvu, blkaddr);
2734 
2735 	npc_config_secret_key(rvu, blkaddr);
2736 	/* Configure MKEX profile */
2737 	if (is_cn20k(rvu->pdev))
2738 		npc_cn20k_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2739 	else
2740 		npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2741 
2742 	err = npc_mcam_rsrcs_init(rvu, blkaddr);
2743 	if (err)
2744 		return err;
2745 
2746 	err = npc_flow_steering_init(rvu, blkaddr);
2747 	if (err) {
2748 		dev_err(rvu->dev,
2749 			"Incorrect mkex profile loaded using default mkex\n");
2750 		if (is_cn20k(rvu->pdev))
2751 			npc_cn20k_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2752 		else
2753 			npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2754 	}
2755 
2756 	if (is_cn20k(rvu->pdev))
2757 		return npc_cn20k_init(rvu);
2758 
2759 	return 0;
2760 }
2761 
2762 void rvu_npc_freemem(struct rvu *rvu)
2763 {
2764 	struct npc_pkind *pkind = &rvu->hw->pkind;
2765 	struct npc_mcam *mcam = &rvu->hw->mcam;
2766 
2767 	kfree(pkind->rsrc.bmap);
2768 	npc_mcam_rsrcs_deinit(rvu);
2769 	if (rvu->kpu_prfl_addr)
2770 		iounmap(rvu->kpu_prfl_addr);
2771 	else
2772 		kfree(rvu->kpu_fwdata);
2773 	mutex_destroy(&mcam->lock);
2774 
2775 	if (is_cn20k(rvu->pdev))
2776 		npc_cn20k_deinit(rvu);
2777 }
2778 
2779 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2780 				       int blkaddr, int *alloc_cnt,
2781 				       int *enable_cnt)
2782 {
2783 	struct npc_mcam *mcam = &rvu->hw->mcam;
2784 	int entry;
2785 
2786 	*alloc_cnt = 0;
2787 	*enable_cnt = 0;
2788 
2789 	for (entry = 0; entry < mcam->bmap_entries; entry++) {
2790 		if (mcam->entry2pfvf_map[entry] == pcifunc) {
2791 			(*alloc_cnt)++;
2792 			if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2793 				(*enable_cnt)++;
2794 		}
2795 	}
2796 }
2797 
2798 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2799 					 int blkaddr, int *alloc_cnt,
2800 					 int *enable_cnt)
2801 {
2802 	struct npc_mcam *mcam = &rvu->hw->mcam;
2803 	int cntr;
2804 
2805 	*alloc_cnt = 0;
2806 	*enable_cnt = 0;
2807 
2808 	for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2809 		if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2810 			(*alloc_cnt)++;
2811 			if (mcam->cntr_refcnt[cntr])
2812 				(*enable_cnt)++;
2813 		}
2814 	}
2815 }
2816 
2817 int npc_mcam_verify_entry(struct npc_mcam *mcam,
2818 			  u16 pcifunc, int entry)
2819 {
2820 	/* verify AF installed entries */
2821 	if (is_pffunc_af(pcifunc))
2822 		return 0;
2823 	/* Verify if entry is valid and if it is indeed
2824 	 * allocated to the requesting PFFUNC.
2825 	 */
2826 	if (entry >= mcam->bmap_entries)
2827 		return NPC_MCAM_INVALID_REQ;
2828 
2829 	if (pcifunc != mcam->entry2pfvf_map[entry])
2830 		return NPC_MCAM_PERM_DENIED;
2831 
2832 	return 0;
2833 }
2834 
2835 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2836 				   u16 pcifunc, int cntr)
2837 {
2838 	/* Verify if counter is valid and if it is indeed
2839 	 * allocated to the requesting PFFUNC.
2840 	 */
2841 	if (cntr >= mcam->counters.max)
2842 		return NPC_MCAM_INVALID_REQ;
2843 
2844 	if (pcifunc != mcam->cntr2pfvf_map[cntr])
2845 		return NPC_MCAM_PERM_DENIED;
2846 
2847 	return 0;
2848 }
2849 
2850 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2851 					int blkaddr, u16 entry, u16 cntr)
2852 {
2853 	u16 index = entry & (mcam->banksize - 1);
2854 	u32 bank = npc_get_bank(mcam, entry);
2855 	struct rvu_hwinfo *hw = rvu->hw;
2856 
2857 	/* Set mapping and increment counter's refcnt */
2858 	mcam->entry2cntr_map[entry] = cntr;
2859 	mcam->cntr_refcnt[cntr]++;
2860 
2861 	if (is_cn20k(rvu->pdev))
2862 		return;
2863 
2864 	/* Enable stats */
2865 	rvu_write64(rvu, blkaddr,
2866 		    NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2867 		    ((cntr >> 9) << 12) | hw->npc_stat_ena | cntr);
2868 }
2869 
2870 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2871 					  struct npc_mcam *mcam,
2872 					  int blkaddr, u16 entry, u16 cntr)
2873 {
2874 	u16 index = entry & (mcam->banksize - 1);
2875 	u32 bank = npc_get_bank(mcam, entry);
2876 
2877 	/* Remove mapping and reduce counter's refcnt */
2878 	mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2879 	mcam->cntr_refcnt[cntr]--;
2880 
2881 	if (is_cn20k(rvu->pdev))
2882 		return;
2883 
2884 	/* Disable stats */
2885 	rvu_write64(rvu, blkaddr,
2886 		    NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2887 }
2888 
2889 /* Sets MCAM entry in bitmap as used. Update
2890  * reverse bitmap too. Should be called with
2891  * 'mcam->lock' held.
2892  */
2893 void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2894 {
2895 	u16 entry, rentry;
2896 
2897 	entry = index;
2898 	rentry = mcam->bmap_entries - index - 1;
2899 
2900 	__set_bit(entry, mcam->bmap);
2901 	__set_bit(rentry, mcam->bmap_reverse);
2902 	mcam->bmap_fcnt--;
2903 }
2904 
2905 /* Sets MCAM entry in bitmap as free. Update
2906  * reverse bitmap too. Should be called with
2907  * 'mcam->lock' held.
2908  */
2909 void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2910 {
2911 	u16 entry, rentry;
2912 
2913 	entry = index;
2914 	rentry = mcam->bmap_entries - index - 1;
2915 
2916 	__clear_bit(entry, mcam->bmap);
2917 	__clear_bit(rentry, mcam->bmap_reverse);
2918 	mcam->bmap_fcnt++;
2919 }
2920 
2921 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2922 				      int blkaddr, u16 pcifunc)
2923 {
2924 	u16 dft_idxs[NPC_DFT_RULE_MAX_ID] = {[0 ... NPC_DFT_RULE_MAX_ID - 1] = USHRT_MAX};
2925 	bool cn20k_dft_rl;
2926 	u16 index, cntr;
2927 	int rc;
2928 
2929 	npc_cn20k_dft_rules_idx_get(rvu, pcifunc,
2930 				    &dft_idxs[NPC_DFT_RULE_BCAST_ID],
2931 				    &dft_idxs[NPC_DFT_RULE_MCAST_ID],
2932 				    &dft_idxs[NPC_DFT_RULE_PROMISC_ID],
2933 				    &dft_idxs[NPC_DFT_RULE_UCAST_ID]);
2934 
2935 	/* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2936 	for (index = 0; index < mcam->bmap_entries; index++) {
2937 		if (mcam->entry2pfvf_map[index] != pcifunc)
2938 			continue;
2939 
2940 		cn20k_dft_rl = false;
2941 
2942 		if (is_cn20k(rvu->pdev)) {
2943 			if (dft_idxs[NPC_DFT_RULE_BCAST_ID] == index ||
2944 			    dft_idxs[NPC_DFT_RULE_MCAST_ID] == index ||
2945 			    dft_idxs[NPC_DFT_RULE_PROMISC_ID] == index ||
2946 			    dft_idxs[NPC_DFT_RULE_UCAST_ID] == index) {
2947 				cn20k_dft_rl = true;
2948 			}
2949 		}
2950 
2951 		if (!cn20k_dft_rl) {
2952 			/* Disable the entry */
2953 			npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2954 			mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2955 			/* Free the entry in bitmap */
2956 			npc_mcam_clear_bit(mcam, index);
2957 			mcam->entry2target_pffunc[index] = 0x0;
2958 		}
2959 
2960 		/* Update entry2counter mapping */
2961 		cntr = mcam->entry2cntr_map[index];
2962 		if (cntr != NPC_MCAM_INVALID_MAP)
2963 			npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2964 						      blkaddr, index,
2965 						      cntr);
2966 
2967 		if (!is_cn20k(rvu->pdev) || cn20k_dft_rl)
2968 			continue;
2969 
2970 		rc = npc_cn20k_idx_free(rvu, &index, 1);
2971 		if (rc)
2972 			dev_err(rvu->dev,
2973 				"Failed to free mcam idx=%u pcifunc=%#x\n",
2974 				index, pcifunc);
2975 	}
2976 }
2977 
2978 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2979 				       u16 pcifunc)
2980 {
2981 	u16 cntr;
2982 
2983 	/* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2984 	for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2985 		if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2986 			mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2987 			mcam->cntr_refcnt[cntr] = 0;
2988 			rvu_free_rsrc(&mcam->counters, cntr);
2989 			/* This API is expected to be called after freeing
2990 			 * MCAM entries, which inturn will remove
2991 			 * 'entry to counter' mapping.
2992 			 * No need to do it again.
2993 			 */
2994 		}
2995 	}
2996 }
2997 
2998 /* Find area of contiguous free entries of size 'nr'.
2999  * If not found return max contiguous free entries available.
3000  */
3001 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
3002 				   u16 nr, u16 *max_area)
3003 {
3004 	u16 max_area_start = 0;
3005 	u16 index, next, end;
3006 
3007 	*max_area = 0;
3008 
3009 again:
3010 	index = find_next_zero_bit(map, size, start);
3011 	if (index >= size)
3012 		return max_area_start;
3013 
3014 	end = ((index + nr) >= size) ? size : index + nr;
3015 	next = find_next_bit(map, end, index);
3016 	if (*max_area < (next - index)) {
3017 		*max_area = next - index;
3018 		max_area_start = index;
3019 	}
3020 
3021 	if (next < end) {
3022 		start = next + 1;
3023 		goto again;
3024 	}
3025 
3026 	return max_area_start;
3027 }
3028 
3029 /* Find number of free MCAM entries available
3030  * within range i.e in between 'start' and 'end'.
3031  */
3032 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
3033 {
3034 	u16 index, next;
3035 	u16 fcnt = 0;
3036 
3037 again:
3038 	if (start >= end)
3039 		return fcnt;
3040 
3041 	index = find_next_zero_bit(map, end, start);
3042 	if (index >= end)
3043 		return fcnt;
3044 
3045 	next = find_next_bit(map, end, index);
3046 	if (next <= end) {
3047 		fcnt += next - index;
3048 		start = next + 1;
3049 		goto again;
3050 	}
3051 
3052 	fcnt += end - index;
3053 	return fcnt;
3054 }
3055 
3056 static void
3057 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
3058 				   struct npc_mcam_alloc_entry_req *req,
3059 				   u16 *start, u16 *end, bool *reverse)
3060 {
3061 	u16 fcnt;
3062 
3063 	if (req->ref_prio == NPC_MCAM_HIGHER_PRIO)
3064 		goto hprio;
3065 
3066 	/* For a low priority entry allocation
3067 	 * - If reference entry is not in hprio zone then
3068 	 *      search range: ref_entry to end.
3069 	 * - If reference entry is in hprio zone and if
3070 	 *   request can be accomodated in non-hprio zone then
3071 	 *      search range: 'start of middle zone' to 'end'
3072 	 * - else search in reverse, so that less number of hprio
3073 	 *   zone entries are allocated.
3074 	 */
3075 
3076 	*reverse = false;
3077 	*start = req->ref_entry + 1;
3078 	*end = mcam->bmap_entries;
3079 
3080 	if (req->ref_entry >= mcam->hprio_end)
3081 		return;
3082 
3083 	fcnt = npc_mcam_get_free_count(mcam->bmap,
3084 				       mcam->hprio_end, mcam->bmap_entries);
3085 	if (fcnt > req->count)
3086 		*start = mcam->hprio_end;
3087 	else
3088 		*reverse = true;
3089 	return;
3090 
3091 hprio:
3092 	/* For a high priority entry allocation, search is always
3093 	 * in reverse to preserve hprio zone entries.
3094 	 * - If reference entry is not in lprio zone then
3095 	 *      search range: 0 to ref_entry.
3096 	 * - If reference entry is in lprio zone and if
3097 	 *   request can be accomodated in middle zone then
3098 	 *      search range: 'hprio_end' to 'lprio_start'
3099 	 */
3100 
3101 	*reverse = true;
3102 	*start = 0;
3103 	*end = req->ref_entry;
3104 
3105 	if (req->ref_entry <= mcam->lprio_start)
3106 		return;
3107 
3108 	fcnt = npc_mcam_get_free_count(mcam->bmap,
3109 				       mcam->hprio_end, mcam->lprio_start);
3110 	if (fcnt < req->count)
3111 		return;
3112 	*start = mcam->hprio_end;
3113 	*end = mcam->lprio_start;
3114 }
3115 
3116 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
3117 				  struct npc_mcam_alloc_entry_req *req,
3118 				  struct npc_mcam_alloc_entry_rsp *rsp)
3119 {
3120 	struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
3121 	u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
3122 	u16 fcnt, hp_fcnt, lp_fcnt;
3123 	struct rvu *rvu = hw->rvu;
3124 	u16 start, end, index;
3125 	int entry, next_start;
3126 	bool reverse = false;
3127 	unsigned long *bmap;
3128 	int ret, limit = 0;
3129 	u16 max_contig;
3130 
3131 	if (!is_cn20k(rvu->pdev))
3132 		goto not_cn20k;
3133 
3134 	/* Only x2 or x4 key types are accepted */
3135 	if (req->kw_type != NPC_MCAM_KEY_X2 && req->kw_type != NPC_MCAM_KEY_X4)
3136 		return NPC_MCAM_INVALID_REQ;
3137 
3138 	/* The below table is being followed during allocation,
3139 	 *
3140 	 * 1. ref_entry == 0 && prio == HIGH && count == 1 : user wants to
3141 	 *						    allocate 0th index
3142 	 * 2. ref_entry == 0 && prio == HIGH && count > 1  : Invalid request
3143 	 * 3. ref_entry == 0 && prio == LOW && count >= 1  : limit = 0
3144 	 * 4. ref_entry != 0 && prio == HIGH && count >= 1 : limit = 0
3145 	 * 5. ref_entry != 0 && prio == LOW && count >=1   : limit = Max
3146 	 *						(X2 2*8192, X4 8192)
3147 	 */
3148 	if (req->ref_entry && req->ref_prio == NPC_MCAM_LOWER_PRIO) {
3149 		if (req->kw_type == NPC_MCAM_KEY_X2)
3150 			limit = 2 * mcam->bmap_entries;
3151 		else
3152 			limit = mcam->bmap_entries;
3153 	}
3154 
3155 	ret = npc_cn20k_ref_idx_alloc(rvu, pcifunc, req->kw_type,
3156 				      req->ref_prio, rsp->entry_list,
3157 				      req->ref_entry, limit,
3158 				      req->contig, req->count, !!req->virt);
3159 
3160 	if (ret) {
3161 		rsp->count = 0;
3162 		return NPC_MCAM_ALLOC_FAILED;
3163 	}
3164 
3165 	rsp->count = req->count;
3166 	if (req->contig)
3167 		rsp->entry = rsp->entry_list[0];
3168 
3169 	/* cn20k, entries allocation algorithm is different.
3170 	 * This common API updates some bitmap on usage etc, which
3171 	 * will be used by other functions. So update those for
3172 	 * cn20k as well.
3173 	 */
3174 
3175 	mutex_lock(&mcam->lock);
3176 	/* Mark the allocated entries as used and set nixlf mapping */
3177 	for (entry = 0; entry < rsp->count; entry++) {
3178 		index = npc_cn20k_vidx2idx(rsp->entry_list[entry]);
3179 		npc_mcam_set_bit(mcam, index);
3180 		mcam->entry2pfvf_map[index] = pcifunc;
3181 		mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
3182 	}
3183 
3184 	/* cn20k, free count is provided thru different mbox message.
3185 	 * one counter to indicate free x2 slots and free x4 slots
3186 	 * does not provide any useful information to the user.
3187 	 */
3188 	rsp->free_count = -1;
3189 	mutex_unlock(&mcam->lock);
3190 
3191 	return 0;
3192 
3193 not_cn20k:
3194 	mutex_lock(&mcam->lock);
3195 
3196 	/* Check if there are any free entries */
3197 	if (!mcam->bmap_fcnt) {
3198 		mutex_unlock(&mcam->lock);
3199 		return NPC_MCAM_ALLOC_FAILED;
3200 	}
3201 
3202 	/* MCAM entries are divided into high priority, middle and
3203 	 * low priority zones. Idea is to not allocate top and lower
3204 	 * most entries as much as possible, this is to increase
3205 	 * probability of honouring priority allocation requests.
3206 	 *
3207 	 * Two bitmaps are used for mcam entry management,
3208 	 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
3209 	 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
3210 	 *
3211 	 * Reverse bitmap is used to allocate entries
3212 	 * - when a higher priority entry is requested
3213 	 * - when available free entries are less.
3214 	 * Lower priority ones out of avaialble free entries are always
3215 	 * chosen when 'high vs low' question arises.
3216 	 *
3217 	 * For a VF base MCAM match rule is set by its PF. And all the
3218 	 * further MCAM rules installed by VF on its own are
3219 	 * concatenated with the base rule set by its PF. Hence PF entries
3220 	 * should be at lower priority compared to VF entries. Otherwise
3221 	 * base rule is hit always and rules installed by VF will be of
3222 	 * no use. Hence if the request is from PF then allocate low
3223 	 * priority entries.
3224 	 */
3225 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
3226 		goto lprio_alloc;
3227 
3228 	/* Get the search range for priority allocation request */
3229 	if (req->ref_prio) {
3230 		npc_get_mcam_search_range_priority(mcam, req,
3231 						   &start, &end, &reverse);
3232 		goto alloc;
3233 	}
3234 
3235 	/* Find out the search range for non-priority allocation request
3236 	 *
3237 	 * Get MCAM free entry count in middle zone.
3238 	 */
3239 	lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
3240 					  mcam->lprio_start,
3241 					  mcam->bmap_entries);
3242 	hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
3243 	fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
3244 
3245 	/* Check if request can be accomodated in the middle zone */
3246 	if (fcnt > req->count) {
3247 		start = mcam->hprio_end;
3248 		end = mcam->lprio_start;
3249 	} else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
3250 		/* Expand search zone from half of hprio zone to
3251 		 * half of lprio zone.
3252 		 */
3253 		start = mcam->hprio_end / 2;
3254 		end = mcam->bmap_entries - (mcam->lprio_count / 2);
3255 		reverse = true;
3256 	} else {
3257 		/* Not enough free entries, search all entries in reverse,
3258 		 * so that low priority ones will get used up.
3259 		 */
3260 lprio_alloc:
3261 		reverse = true;
3262 		start = 0;
3263 		end = mcam->bmap_entries;
3264 		/* Ensure PF requests are always at bottom and if PF requests
3265 		 * for higher/lower priority entry wrt reference entry then
3266 		 * honour that criteria and start search for entries from bottom
3267 		 * and not in mid zone.
3268 		 */
3269 		if (!(pcifunc & RVU_PFVF_FUNC_MASK) &&
3270 		    req->ref_prio == NPC_MCAM_HIGHER_PRIO)
3271 			end = req->ref_entry;
3272 
3273 		if (!(pcifunc & RVU_PFVF_FUNC_MASK) &&
3274 		    req->ref_prio == NPC_MCAM_LOWER_PRIO)
3275 			start = req->ref_entry;
3276 	}
3277 
3278 alloc:
3279 	if (reverse) {
3280 		bmap = mcam->bmap_reverse;
3281 		start = mcam->bmap_entries - start;
3282 		end = mcam->bmap_entries - end;
3283 		swap(start, end);
3284 	} else {
3285 		bmap = mcam->bmap;
3286 	}
3287 
3288 	if (req->contig) {
3289 		/* Allocate requested number of contiguous entries, if
3290 		 * unsuccessful find max contiguous entries available.
3291 		 */
3292 		index = npc_mcam_find_zero_area(bmap, end, start,
3293 						req->count, &max_contig);
3294 		rsp->count = max_contig;
3295 		if (reverse)
3296 			rsp->entry = mcam->bmap_entries - index - max_contig;
3297 		else
3298 			rsp->entry = index;
3299 	} else {
3300 		/* Allocate requested number of non-contiguous entries,
3301 		 * if unsuccessful allocate as many as possible.
3302 		 */
3303 		rsp->count = 0;
3304 		next_start = start;
3305 		for (entry = 0; entry < req->count; entry++) {
3306 			index = find_next_zero_bit(bmap, end, next_start);
3307 			if (index >= end)
3308 				break;
3309 
3310 			next_start = start + (index - start) + 1;
3311 
3312 			/* Save the entry's index */
3313 			if (reverse)
3314 				index = mcam->bmap_entries - index - 1;
3315 			entry_list[entry] = index;
3316 			rsp->count++;
3317 		}
3318 	}
3319 
3320 	/* If allocating requested no of entries is unsucessful,
3321 	 * expand the search range to full bitmap length and retry.
3322 	 */
3323 	if (!req->ref_prio && rsp->count < req->count &&
3324 	    ((end - start) != mcam->bmap_entries)) {
3325 		reverse = true;
3326 		start = 0;
3327 		end = mcam->bmap_entries;
3328 		goto alloc;
3329 	}
3330 
3331 	/* For priority entry allocation requests, if allocation is
3332 	 * failed then expand search to max possible range and retry.
3333 	 */
3334 	if (req->ref_prio && rsp->count < req->count) {
3335 		if (req->ref_prio == NPC_MCAM_LOWER_PRIO &&
3336 		    (start != (req->ref_entry + 1))) {
3337 			start = req->ref_entry + 1;
3338 			end = mcam->bmap_entries;
3339 			reverse = false;
3340 			goto alloc;
3341 		} else if ((req->ref_prio == NPC_MCAM_HIGHER_PRIO) &&
3342 			   ((end - start) != req->ref_entry)) {
3343 			start = 0;
3344 			end = req->ref_entry;
3345 			reverse = true;
3346 			goto alloc;
3347 		}
3348 	}
3349 
3350 	/* Copy MCAM entry indices into mbox response entry_list.
3351 	 * Requester always expects indices in ascending order, so
3352 	 * reverse the list if reverse bitmap is used for allocation.
3353 	 */
3354 	if (!req->contig && rsp->count) {
3355 		index = 0;
3356 		for (entry = rsp->count - 1; entry >= 0; entry--) {
3357 			if (reverse)
3358 				rsp->entry_list[index++] = entry_list[entry];
3359 			else
3360 				rsp->entry_list[entry] = entry_list[entry];
3361 		}
3362 	}
3363 
3364 	/* Mark the allocated entries as used and set nixlf mapping */
3365 	for (entry = 0; entry < rsp->count; entry++) {
3366 		index = req->contig ?
3367 			(rsp->entry + entry) : rsp->entry_list[entry];
3368 		npc_mcam_set_bit(mcam, index);
3369 		mcam->entry2pfvf_map[index] = pcifunc;
3370 		mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
3371 	}
3372 
3373 	/* Update available free count in mbox response */
3374 	rsp->free_count = mcam->bmap_fcnt;
3375 
3376 	mutex_unlock(&mcam->lock);
3377 	return 0;
3378 }
3379 
3380 /* Marks bitmaps to reserved the mcam slot */
3381 void npc_mcam_rsrcs_reserve(struct rvu *rvu, int blkaddr, int entry_idx)
3382 {
3383 	struct npc_mcam *mcam = &rvu->hw->mcam;
3384 
3385 	npc_mcam_set_bit(mcam, entry_idx);
3386 }
3387 
3388 int npc_config_cntr_default_entries(struct rvu *rvu, bool enable)
3389 {
3390 	struct npc_mcam *mcam = &rvu->hw->mcam;
3391 	struct npc_install_flow_rsp rsp;
3392 	struct rvu_npc_mcam_rule *rule;
3393 	int blkaddr;
3394 
3395 	/* Counter is set for each rule by default */
3396 	if (is_cn20k(rvu->pdev))
3397 		return -EINVAL;
3398 
3399 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3400 	if (blkaddr < 0)
3401 		return -EINVAL;
3402 
3403 	mutex_lock(&mcam->lock);
3404 	list_for_each_entry(rule, &mcam->mcam_rules, list) {
3405 		if (!is_mcam_entry_enabled(rvu, mcam, blkaddr, rule->entry))
3406 			continue;
3407 		if (!rule->default_rule)
3408 			continue;
3409 		if (enable && !rule->has_cntr) { /* Alloc and map new counter */
3410 			__rvu_mcam_add_counter_to_rule(rvu, rule->owner,
3411 						       rule, &rsp);
3412 			if (rsp.counter < 0) {
3413 				dev_err(rvu->dev,
3414 					"%s: Failed to allocate cntr for default rule (err=%d)\n",
3415 					__func__, rsp.counter);
3416 				break;
3417 			}
3418 			npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3419 						    rule->entry, rsp.counter);
3420 			/* Reset counter before use */
3421 			rvu_write64(rvu, blkaddr,
3422 				    NPC_AF_MATCH_STATX(rule->cntr), 0x0);
3423 		}
3424 
3425 		/* Free and unmap counter */
3426 		if (!enable && rule->has_cntr)
3427 			__rvu_mcam_remove_counter_from_rule(rvu, rule->owner,
3428 							    rule);
3429 	}
3430 	mutex_unlock(&mcam->lock);
3431 
3432 	return 0;
3433 }
3434 
3435 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
3436 					  struct npc_mcam_alloc_entry_req *req,
3437 					  struct npc_mcam_alloc_entry_rsp *rsp)
3438 {
3439 	struct npc_mcam *mcam = &rvu->hw->mcam;
3440 	u16 pcifunc = req->hdr.pcifunc;
3441 	int blkaddr;
3442 
3443 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3444 	if (blkaddr < 0)
3445 		return NPC_MCAM_INVALID_REQ;
3446 
3447 	rsp->entry = NPC_MCAM_ENTRY_INVALID;
3448 	rsp->free_count = 0;
3449 
3450 	/* Check if ref_entry is greater that the range
3451 	 * then set it to max value.
3452 	 */
3453 	if (req->ref_entry > mcam->bmap_entries)
3454 		req->ref_entry = mcam->bmap_entries;
3455 
3456 	/* ref_entry can't be '0' if requested priority is high.
3457 	 * Can't be last entry if requested priority is low.
3458 	 */
3459 	if ((!req->ref_entry && req->ref_prio == NPC_MCAM_HIGHER_PRIO) ||
3460 	    (req->ref_entry == mcam->bmap_entries &&
3461 	     req->ref_prio == NPC_MCAM_LOWER_PRIO))
3462 		return NPC_MCAM_INVALID_REQ;
3463 
3464 	/* Since list of allocated indices needs to be sent to requester,
3465 	 * max number of non-contiguous entries per mbox msg is limited.
3466 	 */
3467 	if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES) {
3468 		dev_err(rvu->dev,
3469 			"%s: %d Non-contiguous MCAM entries requested is more than max (%d) allowed\n",
3470 			__func__, req->count, NPC_MAX_NONCONTIG_ENTRIES);
3471 		return NPC_MCAM_INVALID_REQ;
3472 	}
3473 
3474 	/* Alloc request from PFFUNC with no NIXLF attached should be denied */
3475 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
3476 		return NPC_MCAM_ALLOC_DENIED;
3477 
3478 	return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
3479 }
3480 
3481 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
3482 					 struct npc_mcam_free_entry_req *req,
3483 					 struct msg_rsp *rsp)
3484 {
3485 	struct npc_mcam *mcam = &rvu->hw->mcam;
3486 	u16 pcifunc = req->hdr.pcifunc;
3487 	int blkaddr, rc = 0;
3488 	u16 cntr;
3489 
3490 	req->entry = npc_cn20k_vidx2idx(req->entry);
3491 
3492 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3493 	if (blkaddr < 0)
3494 		return NPC_MCAM_INVALID_REQ;
3495 
3496 	/* Free request from PFFUNC with no NIXLF attached, ignore */
3497 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
3498 		return NPC_MCAM_INVALID_REQ;
3499 
3500 	mutex_lock(&mcam->lock);
3501 
3502 	if (req->all)
3503 		goto free_all;
3504 
3505 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
3506 	if (rc)
3507 		goto exit;
3508 
3509 	mcam->entry2pfvf_map[req->entry] = NPC_MCAM_INVALID_MAP;
3510 	mcam->entry2target_pffunc[req->entry] = 0x0;
3511 	npc_mcam_clear_bit(mcam, req->entry);
3512 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
3513 
3514 	/* Update entry2counter mapping */
3515 	cntr = mcam->entry2cntr_map[req->entry];
3516 	if (cntr != NPC_MCAM_INVALID_MAP)
3517 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3518 					      req->entry, cntr);
3519 
3520 	if (is_cn20k(rvu->pdev)) {
3521 		rc = npc_cn20k_idx_free(rvu, &req->entry, 1);
3522 		if (rc)
3523 			dev_err(rvu->dev,
3524 				"Failed to free index=%u\n",
3525 				req->entry);
3526 	}
3527 
3528 	goto exit;
3529 
3530 free_all:
3531 	/* Free up all entries allocated to requesting PFFUNC */
3532 	npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
3533 exit:
3534 	mutex_unlock(&mcam->lock);
3535 	return rc;
3536 }
3537 
3538 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
3539 					 struct npc_mcam_read_entry_req *req,
3540 					 struct npc_mcam_read_entry_rsp *rsp)
3541 {
3542 	struct npc_mcam *mcam = &rvu->hw->mcam;
3543 	u16 pcifunc = req->hdr.pcifunc;
3544 	int blkaddr, rc;
3545 
3546 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3547 	if (blkaddr < 0)
3548 		return NPC_MCAM_INVALID_REQ;
3549 
3550 	mutex_lock(&mcam->lock);
3551 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
3552 	if (!rc) {
3553 		npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
3554 				    &rsp->entry_data,
3555 				    &rsp->intf, &rsp->enable);
3556 	}
3557 
3558 	mutex_unlock(&mcam->lock);
3559 	return rc;
3560 }
3561 
3562 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
3563 					  struct npc_mcam_write_entry_req *req,
3564 					  struct msg_rsp *rsp)
3565 {
3566 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3567 	struct npc_mcam *mcam = &rvu->hw->mcam;
3568 	u16 pcifunc = req->hdr.pcifunc;
3569 	int blkaddr, rc;
3570 	u8 nix_intf;
3571 
3572 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3573 	if (blkaddr < 0)
3574 		return NPC_MCAM_INVALID_REQ;
3575 
3576 	mutex_lock(&mcam->lock);
3577 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
3578 	if (rc)
3579 		goto exit;
3580 
3581 	if (!is_cn20k(rvu->pdev)) {
3582 		/* Verify counter in SoCs other than cn20k */
3583 		if (req->set_cntr &&
3584 		    npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
3585 			rc = NPC_MCAM_INVALID_REQ;
3586 			goto exit;
3587 		}
3588 	}
3589 
3590 	if (!is_npc_interface_valid(rvu, req->intf)) {
3591 		rc = NPC_MCAM_INVALID_REQ;
3592 		goto exit;
3593 	}
3594 
3595 	if (is_npc_intf_tx(req->intf))
3596 		nix_intf = pfvf->nix_tx_intf;
3597 	else
3598 		nix_intf = pfvf->nix_rx_intf;
3599 
3600 	/* For AF installed rules, the nix_intf should be set to target NIX */
3601 	if (is_pffunc_af(req->hdr.pcifunc))
3602 		nix_intf = req->intf;
3603 
3604 	npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
3605 			      &req->entry_data, req->enable_entry);
3606 
3607 	if (req->set_cntr)
3608 		npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3609 					    req->entry, req->cntr);
3610 
3611 	rc = 0;
3612 exit:
3613 	mutex_unlock(&mcam->lock);
3614 	return rc;
3615 }
3616 
3617 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
3618 					struct npc_mcam_ena_dis_entry_req *req,
3619 					struct msg_rsp *rsp)
3620 {
3621 	struct npc_mcam *mcam = &rvu->hw->mcam;
3622 	u16 pcifunc = req->hdr.pcifunc;
3623 	int blkaddr, rc;
3624 
3625 	req->entry = npc_cn20k_vidx2idx(req->entry);
3626 
3627 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3628 	if (blkaddr < 0)
3629 		return NPC_MCAM_INVALID_REQ;
3630 
3631 	mutex_lock(&mcam->lock);
3632 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
3633 	mutex_unlock(&mcam->lock);
3634 	if (rc)
3635 		return rc;
3636 
3637 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
3638 
3639 	return 0;
3640 }
3641 
3642 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
3643 					struct npc_mcam_ena_dis_entry_req *req,
3644 					struct msg_rsp *rsp)
3645 {
3646 	struct npc_mcam *mcam = &rvu->hw->mcam;
3647 	u16 pcifunc = req->hdr.pcifunc;
3648 	int blkaddr, rc;
3649 
3650 	req->entry = npc_cn20k_vidx2idx(req->entry);
3651 
3652 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3653 	if (blkaddr < 0)
3654 		return NPC_MCAM_INVALID_REQ;
3655 
3656 	mutex_lock(&mcam->lock);
3657 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
3658 	mutex_unlock(&mcam->lock);
3659 	if (rc)
3660 		return rc;
3661 
3662 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
3663 
3664 	return 0;
3665 }
3666 
3667 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
3668 					  struct npc_mcam_shift_entry_req *req,
3669 					  struct npc_mcam_shift_entry_rsp *rsp)
3670 {
3671 	struct npc_mcam *mcam = &rvu->hw->mcam;
3672 	u16 pcifunc = req->hdr.pcifunc;
3673 	u16 old_entry, new_entry;
3674 	int blkaddr, rc = 0;
3675 	u16 index, cntr;
3676 
3677 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3678 	if (blkaddr < 0)
3679 		return NPC_MCAM_INVALID_REQ;
3680 
3681 	if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
3682 		return NPC_MCAM_INVALID_REQ;
3683 
3684 	mutex_lock(&mcam->lock);
3685 	for (index = 0; index < req->shift_count; index++) {
3686 		old_entry = npc_cn20k_vidx2idx(req->curr_entry[index]);
3687 		new_entry = npc_cn20k_vidx2idx(req->new_entry[index]);
3688 
3689 		/* Check if both old and new entries are valid and
3690 		 * does belong to this PFFUNC or not.
3691 		 */
3692 		rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
3693 		if (rc)
3694 			break;
3695 
3696 		rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
3697 		if (rc)
3698 			break;
3699 
3700 		/* new_entry should not have a counter mapped */
3701 		if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
3702 			rc = NPC_MCAM_PERM_DENIED;
3703 			break;
3704 		}
3705 
3706 		/* Disable the new_entry */
3707 		npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
3708 
3709 		/* Copy rule from old entry to new entry */
3710 		if (npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry)) {
3711 			rc = NPC_MCAM_INVALID_REQ;
3712 			break;
3713 		}
3714 
3715 		/* Copy counter mapping, if any */
3716 		cntr = mcam->entry2cntr_map[old_entry];
3717 		if (cntr != NPC_MCAM_INVALID_MAP) {
3718 			npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3719 						      old_entry, cntr);
3720 			npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3721 						    new_entry, cntr);
3722 		}
3723 
3724 		/* Enable new_entry and disable old_entry */
3725 		npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
3726 		npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
3727 	}
3728 
3729 	/* If shift has failed then report the failed index */
3730 	if (index != req->shift_count) {
3731 		if (!rc)
3732 			rc = NPC_MCAM_PERM_DENIED;
3733 		rsp->failed_entry_idx = index;
3734 	}
3735 
3736 	mutex_unlock(&mcam->lock);
3737 	return rc;
3738 }
3739 
3740 static int __npc_mcam_alloc_counter(struct rvu *rvu,
3741 				    struct npc_mcam_alloc_counter_req *req,
3742 				    struct npc_mcam_alloc_counter_rsp *rsp)
3743 {
3744 	struct npc_mcam *mcam = &rvu->hw->mcam;
3745 	u16 pcifunc = req->hdr.pcifunc;
3746 	u16 max_contig, cntr;
3747 	int blkaddr, index;
3748 
3749 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3750 	if (blkaddr < 0)
3751 		return NPC_MCAM_INVALID_REQ;
3752 
3753 	/* If the request is from a PFFUNC with no NIXLF attached, ignore */
3754 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
3755 		return NPC_MCAM_INVALID_REQ;
3756 
3757 	/* Since list of allocated counter IDs needs to be sent to requester,
3758 	 * max number of non-contiguous counters per mbox msg is limited.
3759 	 */
3760 	if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
3761 		return NPC_MCAM_INVALID_REQ;
3762 
3763 	/* Check if unused counters are available or not */
3764 	if (!rvu_rsrc_free_count(&mcam->counters)) {
3765 		return NPC_MCAM_ALLOC_FAILED;
3766 	}
3767 
3768 	rsp->count = 0;
3769 
3770 	if (req->contig) {
3771 		/* Allocate requested number of contiguous counters, if
3772 		 * unsuccessful find max contiguous entries available.
3773 		 */
3774 		index = npc_mcam_find_zero_area(mcam->counters.bmap,
3775 						mcam->counters.max, 0,
3776 						req->count, &max_contig);
3777 		rsp->count = max_contig;
3778 		rsp->cntr = index;
3779 		for (cntr = index; cntr < (index + max_contig); cntr++) {
3780 			__set_bit(cntr, mcam->counters.bmap);
3781 			mcam->cntr2pfvf_map[cntr] = pcifunc;
3782 		}
3783 	} else {
3784 		/* Allocate requested number of non-contiguous counters,
3785 		 * if unsuccessful allocate as many as possible.
3786 		 */
3787 		for (cntr = 0; cntr < req->count; cntr++) {
3788 			index = rvu_alloc_rsrc(&mcam->counters);
3789 			if (index < 0)
3790 				break;
3791 			rsp->cntr_list[cntr] = index;
3792 			rsp->count++;
3793 			mcam->cntr2pfvf_map[index] = pcifunc;
3794 		}
3795 	}
3796 
3797 	return 0;
3798 }
3799 
3800 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
3801 			struct npc_mcam_alloc_counter_req *req,
3802 			struct npc_mcam_alloc_counter_rsp *rsp)
3803 {
3804 	struct npc_mcam *mcam = &rvu->hw->mcam;
3805 	int err;
3806 
3807 	/* Counter is not supported for CN20K */
3808 	if (is_cn20k(rvu->pdev))
3809 		return NPC_MCAM_INVALID_REQ;
3810 
3811 	mutex_lock(&mcam->lock);
3812 
3813 	err = __npc_mcam_alloc_counter(rvu, req, rsp);
3814 
3815 	mutex_unlock(&mcam->lock);
3816 	return err;
3817 }
3818 
3819 static int __npc_mcam_free_counter(struct rvu *rvu,
3820 				   struct npc_mcam_oper_counter_req *req,
3821 				   struct msg_rsp *rsp)
3822 {
3823 	struct npc_mcam *mcam = &rvu->hw->mcam;
3824 	u16 index;
3825 	int blkaddr, err;
3826 
3827 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3828 	if (blkaddr < 0)
3829 		return NPC_MCAM_INVALID_REQ;
3830 
3831 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3832 	if (err) {
3833 		return err;
3834 	}
3835 
3836 	/* Mark counter as free/unused */
3837 	mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
3838 	rvu_free_rsrc(&mcam->counters, req->cntr);
3839 
3840 	/* Disable all MCAM entry's stats which are using this counter.
3841 	 * Scan the full MCAM index range: AF-reserved rules (e.g. CPT pass-2)
3842 	 */
3843 	for (index = 0; index < mcam->total_entries; index++) {
3844 		if (!mcam->cntr_refcnt[req->cntr])
3845 			break;
3846 		if (mcam->entry2cntr_map[index] != req->cntr)
3847 			continue;
3848 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr, index,
3849 					      req->cntr);
3850 	}
3851 
3852 	return 0;
3853 }
3854 
3855 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
3856 		struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
3857 {
3858 	struct npc_mcam *mcam = &rvu->hw->mcam;
3859 	int err;
3860 
3861 	/* Counter is not supported for CN20K */
3862 	if (is_cn20k(rvu->pdev))
3863 		return NPC_MCAM_INVALID_REQ;
3864 
3865 	mutex_lock(&mcam->lock);
3866 
3867 	err = __npc_mcam_free_counter(rvu, req, rsp);
3868 
3869 	mutex_unlock(&mcam->lock);
3870 
3871 	return err;
3872 }
3873 
3874 void __rvu_mcam_remove_counter_from_rule(struct rvu *rvu, u16 pcifunc,
3875 					 struct rvu_npc_mcam_rule *rule)
3876 {
3877 	struct npc_mcam_oper_counter_req free_req = { 0 };
3878 	struct msg_rsp free_rsp;
3879 
3880 	if (!rule->has_cntr)
3881 		return;
3882 
3883 	free_req.hdr.pcifunc = pcifunc;
3884 	free_req.cntr = rule->cntr;
3885 
3886 	__npc_mcam_free_counter(rvu, &free_req, &free_rsp);
3887 	rule->has_cntr = false;
3888 }
3889 
3890 void __rvu_mcam_add_counter_to_rule(struct rvu *rvu, u16 pcifunc,
3891 				    struct rvu_npc_mcam_rule *rule,
3892 				    struct npc_install_flow_rsp *rsp)
3893 {
3894 	struct npc_mcam_alloc_counter_req cntr_req = { 0 };
3895 	struct npc_mcam_alloc_counter_rsp cntr_rsp = { 0 };
3896 	int err;
3897 
3898 	cntr_req.hdr.pcifunc = pcifunc;
3899 	cntr_req.contig = true;
3900 	cntr_req.count = 1;
3901 
3902 	/* we try to allocate a counter to track the stats of this
3903 	 * rule. If counter could not be allocated then proceed
3904 	 * without counter because counters are limited than entries.
3905 	 */
3906 	err = __npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3907 	if (!err && cntr_rsp.count) {
3908 		rule->cntr = cntr_rsp.cntr;
3909 		rule->has_cntr = true;
3910 		rsp->counter = rule->cntr;
3911 	} else {
3912 		rsp->counter = err;
3913 	}
3914 }
3915 
3916 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
3917 		struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
3918 {
3919 	struct npc_mcam *mcam = &rvu->hw->mcam;
3920 	u16 index;
3921 	int blkaddr, rc;
3922 
3923 	/* Counter is not supported for CN20K */
3924 	if (is_cn20k(rvu->pdev))
3925 		return NPC_MCAM_INVALID_REQ;
3926 
3927 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3928 	if (blkaddr < 0)
3929 		return NPC_MCAM_INVALID_REQ;
3930 
3931 	mutex_lock(&mcam->lock);
3932 	rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3933 	if (rc)
3934 		goto exit;
3935 
3936 	/* Unmap the MCAM entry and counter */
3937 	if (!req->all) {
3938 		rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
3939 		if (rc)
3940 			goto exit;
3941 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3942 					      req->entry, req->cntr);
3943 		goto exit;
3944 	}
3945 
3946 	/* Disable all MCAM entry's stats which are using this counter */
3947 	for (index = 0; index < mcam->total_entries; index++) {
3948 		if (!mcam->cntr_refcnt[req->cntr])
3949 			break;
3950 		if (mcam->entry2cntr_map[index] != req->cntr)
3951 			continue;
3952 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr, index,
3953 					      req->cntr);
3954 	}
3955 exit:
3956 	mutex_unlock(&mcam->lock);
3957 	return rc;
3958 }
3959 
3960 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
3961 		struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
3962 {
3963 	struct npc_mcam *mcam = &rvu->hw->mcam;
3964 	int blkaddr, err, index, bank;
3965 
3966 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3967 	if (blkaddr < 0)
3968 		return NPC_MCAM_INVALID_REQ;
3969 
3970 	/* For cn20k, npc mcam index is passed as cntr, as each
3971 	 * mcam entry has corresponding counter.
3972 	 */
3973 	if (is_cn20k(rvu->pdev)) {
3974 		index = req->cntr & (mcam->banksize - 1);
3975 		bank = npc_get_bank(mcam, req->cntr);
3976 		rvu_write64(rvu, blkaddr,
3977 			    NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT(index, bank), 0);
3978 		return 0;
3979 	}
3980 
3981 	mutex_lock(&mcam->lock);
3982 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3983 	mutex_unlock(&mcam->lock);
3984 	if (err)
3985 		return err;
3986 
3987 	rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
3988 
3989 	return 0;
3990 }
3991 
3992 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
3993 			struct npc_mcam_oper_counter_req *req,
3994 			struct npc_mcam_oper_counter_rsp *rsp)
3995 {
3996 	struct npc_mcam *mcam = &rvu->hw->mcam;
3997 	int blkaddr, err, index, bank;
3998 	u64 regval;
3999 
4000 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4001 	if (blkaddr < 0)
4002 		return NPC_MCAM_INVALID_REQ;
4003 
4004 	/* In CN20K, mcam index is passed cntr. Each cn20k mcam entry
4005 	 * has its own counter. No need to verify the counter index.
4006 	 */
4007 	if (is_cn20k(rvu->pdev)) {
4008 		index = req->cntr & (mcam->banksize - 1);
4009 		bank = npc_get_bank(mcam, req->cntr);
4010 		regval = rvu_read64(rvu, blkaddr,
4011 				    NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT(index,
4012 								       bank));
4013 		rsp->stat = regval;
4014 		return 0;
4015 	}
4016 
4017 	mutex_lock(&mcam->lock);
4018 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
4019 	mutex_unlock(&mcam->lock);
4020 	if (err)
4021 		return err;
4022 
4023 	rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
4024 	rsp->stat &= BIT_ULL(48) - 1;
4025 
4026 	return 0;
4027 }
4028 
4029 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
4030 			  struct npc_mcam_alloc_and_write_entry_req *req,
4031 			  struct npc_mcam_alloc_and_write_entry_rsp *rsp)
4032 {
4033 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
4034 	struct npc_mcam_alloc_counter_req cntr_req;
4035 	struct npc_mcam_alloc_counter_rsp cntr_rsp;
4036 	struct npc_mcam_alloc_entry_req entry_req;
4037 	struct npc_mcam_alloc_entry_rsp entry_rsp;
4038 	struct npc_mcam *mcam = &rvu->hw->mcam;
4039 	u16 entry = NPC_MCAM_ENTRY_INVALID;
4040 	u16 cntr = NPC_MCAM_ENTRY_INVALID;
4041 	int blkaddr, rc;
4042 	u8 nix_intf;
4043 
4044 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4045 	if (blkaddr < 0)
4046 		return NPC_MCAM_INVALID_REQ;
4047 
4048 	if (!is_npc_interface_valid(rvu, req->intf))
4049 		return NPC_MCAM_INVALID_REQ;
4050 
4051 	/* Try to allocate a MCAM entry */
4052 	entry_req.hdr.pcifunc = req->hdr.pcifunc;
4053 	entry_req.contig = true;
4054 	entry_req.ref_prio = req->ref_prio;
4055 	entry_req.ref_entry = req->ref_entry;
4056 	entry_req.count = 1;
4057 
4058 	rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
4059 						   &entry_req, &entry_rsp);
4060 	if (rc)
4061 		return rc;
4062 
4063 	if (!entry_rsp.count)
4064 		return NPC_MCAM_ALLOC_FAILED;
4065 
4066 	entry = entry_rsp.entry;
4067 
4068 	if (!req->alloc_cntr)
4069 		goto write_entry;
4070 
4071 	/* Now allocate counter */
4072 	cntr_req.hdr.pcifunc = req->hdr.pcifunc;
4073 	cntr_req.contig = true;
4074 	cntr_req.count = 1;
4075 
4076 	rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
4077 	if (rc) {
4078 		/* Free allocated MCAM entry */
4079 		mutex_lock(&mcam->lock);
4080 		mcam->entry2pfvf_map[entry] = NPC_MCAM_INVALID_MAP;
4081 		npc_mcam_clear_bit(mcam, entry);
4082 		mutex_unlock(&mcam->lock);
4083 		return rc;
4084 	}
4085 
4086 	cntr = cntr_rsp.cntr;
4087 
4088 write_entry:
4089 	mutex_lock(&mcam->lock);
4090 
4091 	if (is_npc_intf_tx(req->intf))
4092 		nix_intf = pfvf->nix_tx_intf;
4093 	else
4094 		nix_intf = pfvf->nix_rx_intf;
4095 
4096 	npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
4097 			      &req->entry_data, req->enable_entry);
4098 
4099 	if (req->alloc_cntr)
4100 		npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
4101 	mutex_unlock(&mcam->lock);
4102 
4103 	rsp->entry = entry;
4104 	rsp->cntr = cntr;
4105 
4106 	return 0;
4107 }
4108 
4109 #define GET_KEX_CFG(intf) \
4110 	rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
4111 
4112 #define GET_KEX_FLAGS(ld) \
4113 	rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
4114 
4115 #define GET_KEX_LD(intf, lid, lt, ld)	\
4116 	rvu_read64(rvu, BLKADDR_NPC,	\
4117 		NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
4118 
4119 #define GET_KEX_LDFLAGS(intf, ld, fl)	\
4120 	rvu_read64(rvu, BLKADDR_NPC,	\
4121 		NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
4122 
4123 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
4124 				     struct npc_get_kex_cfg_rsp *rsp)
4125 {
4126 	int lid, lt, ld, fl;
4127 
4128 	rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
4129 	rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
4130 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
4131 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
4132 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
4133 				rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
4134 					GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
4135 				rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
4136 					GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
4137 			}
4138 		}
4139 	}
4140 	for (ld = 0; ld < NPC_MAX_LD; ld++)
4141 		rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
4142 
4143 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
4144 		for (fl = 0; fl < NPC_MAX_LFL; fl++) {
4145 			rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
4146 					GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
4147 			rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
4148 					GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
4149 		}
4150 	}
4151 	memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
4152 	return 0;
4153 }
4154 
4155 static int
4156 npc_set_var_len_offset_pkind(struct rvu *rvu, u16 pcifunc, u64 pkind,
4157 			     u8 var_len_off, u8 var_len_off_mask, u8 shift_dir)
4158 {
4159 	struct npc_kpu_action0 *act0;
4160 	u8 shift_count = 0;
4161 	int blkaddr;
4162 	u64 val;
4163 
4164 	if (!var_len_off_mask)
4165 		return -EINVAL;
4166 
4167 	if (var_len_off_mask != 0xff) {
4168 		if (shift_dir)
4169 			shift_count = __ffs(var_len_off_mask);
4170 		else
4171 			shift_count = (8 - __fls(var_len_off_mask));
4172 	}
4173 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
4174 	if (blkaddr < 0) {
4175 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
4176 		return -EINVAL;
4177 	}
4178 	val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
4179 	act0 = (struct npc_kpu_action0 *)&val;
4180 	act0->var_len_shift = shift_count;
4181 	act0->var_len_right = shift_dir;
4182 	act0->var_len_mask = var_len_off_mask;
4183 	act0->var_len_offset = var_len_off;
4184 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
4185 	return 0;
4186 }
4187 
4188 static int npc_set_skip_size_pkind(struct rvu *rvu, u16 pcifunc, u64 pkind,
4189 				   u8 skip_size)
4190 {
4191 	struct npc_kpu_action0 *act0;
4192 	int blkaddr;
4193 	u64 val;
4194 
4195 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
4196 	if (blkaddr < 0) {
4197 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
4198 		return -EINVAL;
4199 	}
4200 
4201 	val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
4202 	act0 = (struct npc_kpu_action0 *)&val;
4203 	act0->ptr_advance = skip_size;
4204 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
4205 
4206 	/* Update CPT_HR new PKIND */
4207 	val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind + 4));
4208 	act0 = (struct npc_kpu_action0 *)&val;
4209 	act0->ptr_advance = (skip_size + 40);
4210 	act0->next_state = NPC_S_KPU1_CPT_HDR;
4211 	act0->var_len_offset = (skip_size + 6);
4212 	act0->var_len_mask = 0xe0;
4213 	act0->var_len_shift = 0x5;
4214 	act0->var_len_right = 0x1;
4215 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind + 4), val);
4216 	return 0;
4217 }
4218 
4219 int rvu_npc_set_parse_mode(struct rvu *rvu, u16 pcifunc, u64 mode, u8 dir,
4220 			   u64 pkind, u8 var_len_off, u8 var_len_off_mask,
4221 			   u8 shift_dir, u8 skip_size)
4222 {
4223 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
4224 	int pf = rvu_get_pf(rvu->pdev, pcifunc);
4225 	int blkaddr, nixlf, rc, intf_mode;
4226 	u8 cgx_id = 0, lmac_id = 0;
4227 	u64 rxpkind, txpkind;
4228 	struct cgx *cgxd;
4229 
4230 	/* use default pkind to disable edsa/higig */
4231 	rxpkind = rvu_npc_get_pkind(rvu, pf);
4232 	txpkind = NPC_TX_DEF_PKIND;
4233 	intf_mode = NPC_INTF_MODE_DEF;
4234 
4235 	if (mode & OTX2_PRIV_FLAGS_CUSTOM) {
4236 		if (pkind == NPC_RX_CUSTOM_PRE_L2_PKIND) {
4237 			rc = npc_set_var_len_offset_pkind(rvu, pcifunc, pkind,
4238 							  var_len_off,
4239 							  var_len_off_mask,
4240 							  shift_dir);
4241 			if (rc)
4242 				return rc;
4243 		} else if (pkind >= NPC_RX_SKIP_SIZE_PKIND &&
4244 			   pkind <= NPC_RX_SKIP_SIZE_PKIND + 3) {
4245 			rc = npc_set_skip_size_pkind(rvu, pcifunc, pkind,
4246 						     skip_size);
4247 			if (rc)
4248 				return rc;
4249 		}
4250 		rxpkind = pkind;
4251 		txpkind = pkind;
4252 	}
4253 
4254 	if (dir & PKIND_RX) {
4255 		/* rx pkind set req valid only for cgx mapped PFs */
4256 		if (!is_cgx_config_permitted(rvu, pcifunc))
4257 			return 0;
4258 		if (!rvu_cgx_check_permission_and_set_pkind(rvu, pcifunc, rxpkind))
4259 			return -EINVAL;
4260 	}
4261 
4262 	if (dir & PKIND_TX) {
4263 		/* Tx pkind set request valid if PCIFUNC has NIXLF attached */
4264 		rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
4265 		if (rc)
4266 			return rc;
4267 
4268 		if (is_pf_cgxmapped(rvu, pf) && is_vf(pcifunc)) {
4269 			rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id,
4270 					    &lmac_id);
4271 			cgxd = rvu_cgx_pdata(cgx_id, rvu);
4272 			mutex_lock(&cgxd->lock);
4273 			if (rvu_cgx_is_pkind_config_permitted(rvu, pcifunc))
4274 				rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf),
4275 					    txpkind);
4276 			mutex_unlock(&cgxd->lock);
4277 		} else {
4278 			rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf),
4279 				    txpkind);
4280 		}
4281 	}
4282 
4283 	pfvf->intf_mode = intf_mode;
4284 	return 0;
4285 }
4286 
4287 int rvu_mbox_handler_npc_set_pkind(struct rvu *rvu, struct npc_set_pkind *req,
4288 				   struct msg_rsp *rsp)
4289 {
4290 	return rvu_npc_set_parse_mode(rvu, req->hdr.pcifunc, req->mode,
4291 				      req->dir, req->pkind, req->var_len_off,
4292 				      req->var_len_off_mask, req->shift_dir,
4293 				      req->skip_size);
4294 }
4295 
4296 int rvu_mbox_handler_npc_read_default_rule(struct rvu *rvu,
4297 					   struct msg_req *req,
4298 					   struct npc_mcam_read_base_rule_rsp *rsp)
4299 {
4300 	struct npc_mcam *mcam = &rvu->hw->mcam;
4301 	int index, blkaddr, nixlf, rc;
4302 	u16 pcifunc = req->hdr.pcifunc;
4303 	u8 intf, enable;
4304 
4305 	if (is_cn20k(rvu->pdev))
4306 		return NPC_MCAM_INVALID_REQ;
4307 
4308 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4309 	if (blkaddr < 0)
4310 		return NPC_MCAM_INVALID_REQ;
4311 
4312 	rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4313 	if (rc < 0)
4314 		return rc;
4315 
4316 	/* Read the default ucast entry */
4317 	mutex_lock(&mcam->lock);
4318 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
4319 					 NIXLF_UCAST_ENTRY);
4320 	if (index < 0) {
4321 		mutex_unlock(&mcam->lock);
4322 		return NIX_AF_ERR_AF_LF_INVALID;
4323 	}
4324 
4325 	/* Read the mcam entry */
4326 	npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
4327 			    &enable);
4328 	mutex_unlock(&mcam->lock);
4329 
4330 	return 0;
4331 }
4332 
4333 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
4334 					      struct msg_req *req,
4335 					      struct npc_mcam_read_base_rule_rsp *rsp)
4336 {
4337 	struct npc_mcam *mcam = &rvu->hw->mcam;
4338 	int index, blkaddr, nixlf, rc = 0;
4339 	u16 pcifunc = req->hdr.pcifunc;
4340 	struct rvu_pfvf *pfvf;
4341 	u8 intf, enable;
4342 
4343 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4344 	if (blkaddr < 0)
4345 		return NPC_MCAM_INVALID_REQ;
4346 
4347 	/* Return the channel number in case of PF */
4348 	if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
4349 		pfvf = rvu_get_pfvf(rvu, pcifunc);
4350 		rsp->entry.kw[0] = pfvf->rx_chan_base;
4351 		rsp->entry.kw_mask[0] = 0xFFFULL;
4352 		goto out;
4353 	}
4354 
4355 	/* Find the pkt steering rule installed by PF to this VF */
4356 	mutex_lock(&mcam->lock);
4357 	for (index = 0; index < mcam->bmap_entries; index++) {
4358 		if (mcam->entry2target_pffunc[index] == pcifunc)
4359 			goto read_entry;
4360 	}
4361 
4362 	rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4363 	if (rc < 0) {
4364 		mutex_unlock(&mcam->lock);
4365 		goto out;
4366 	}
4367 	/* Read the default ucast entry if there is no pkt steering rule */
4368 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
4369 					 NIXLF_UCAST_ENTRY);
4370 	if (index < 0) {
4371 		mutex_unlock(&mcam->lock);
4372 		rc = NIX_AF_ERR_AF_LF_INVALID;
4373 		goto out;
4374 	}
4375 
4376 read_entry:
4377 	/* Read the mcam entry */
4378 	npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
4379 			    &enable);
4380 	mutex_unlock(&mcam->lock);
4381 out:
4382 	return rc;
4383 }
4384 
4385 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
4386 					  struct npc_mcam_get_stats_req *req,
4387 					  struct npc_mcam_get_stats_rsp *rsp)
4388 {
4389 	struct npc_mcam *mcam = &rvu->hw->mcam;
4390 	u16 index, cntr;
4391 	int blkaddr;
4392 	u64 regval;
4393 	u32 bank;
4394 
4395 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4396 	if (blkaddr < 0)
4397 		return NPC_MCAM_INVALID_REQ;
4398 
4399 	req->entry = npc_cn20k_vidx2idx(req->entry);
4400 
4401 	index = req->entry & (mcam->banksize - 1);
4402 	bank = npc_get_bank(mcam, req->entry);
4403 
4404 	mutex_lock(&mcam->lock);
4405 
4406 	if (is_cn20k(rvu->pdev)) {
4407 		regval = rvu_read64(rvu, blkaddr,
4408 				    NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT(index,
4409 								       bank));
4410 		rsp->stat_ena = 1;
4411 		rsp->stat = regval;
4412 		mutex_unlock(&mcam->lock);
4413 		return 0;
4414 	}
4415 
4416 	/* read MCAM entry STAT_ACT register */
4417 	regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index,
4418 								       bank));
4419 
4420 	if (!(regval & rvu->hw->npc_stat_ena)) {
4421 		rsp->stat_ena = 0;
4422 		mutex_unlock(&mcam->lock);
4423 		return 0;
4424 	}
4425 
4426 	cntr = regval & 0x1FF;
4427 
4428 	rsp->stat_ena = 1;
4429 	rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
4430 	rsp->stat &= BIT_ULL(48) - 1;
4431 
4432 	mutex_unlock(&mcam->lock);
4433 
4434 	return 0;
4435 }
4436 
4437 void rvu_npc_clear_ucast_entry(struct rvu *rvu, int pcifunc, int nixlf)
4438 {
4439 	struct npc_mcam *mcam = &rvu->hw->mcam;
4440 	struct rvu_npc_mcam_rule *rule;
4441 	int ucast_idx, blkaddr;
4442 
4443 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
4444 	if (blkaddr < 0)
4445 		return;
4446 
4447 	ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
4448 					     nixlf, NIXLF_UCAST_ENTRY);
4449 
4450 	/* In cn20k, default rules are freed before detach rsrc */
4451 	if (ucast_idx < 0)
4452 		return;
4453 
4454 	npc_enable_mcam_entry(rvu, mcam, blkaddr, ucast_idx, false);
4455 
4456 	npc_set_mcam_action(rvu, mcam, blkaddr, ucast_idx, 0);
4457 
4458 	npc_clear_mcam_entry(rvu, mcam, blkaddr, ucast_idx);
4459 
4460 	mutex_lock(&mcam->lock);
4461 	list_for_each_entry(rule, &mcam->mcam_rules, list) {
4462 		if (rule->entry == ucast_idx) {
4463 			list_del(&rule->list);
4464 			kfree(rule);
4465 			break;
4466 		}
4467 	}
4468 	mutex_unlock(&mcam->lock);
4469 }
4470