1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 
15 #include "rvu_struct.h"
16 #include "rvu_reg.h"
17 #include "rvu.h"
18 #include "npc.h"
19 #include "cgx.h"
20 #include "npc_profile.h"
21 
22 #define RSVD_MCAM_ENTRIES_PER_PF	3 /* Broadcast, Promisc and AllMulticast */
23 #define RSVD_MCAM_ENTRIES_PER_NIXLF	1 /* Ucast for LFs */
24 
25 #define NPC_PARSE_RESULT_DMAC_OFFSET	8
26 #define NPC_HW_TSTAMP_OFFSET		8
27 #define NPC_KEX_CHAN_MASK		0xFFFULL
28 #define NPC_KEX_PF_FUNC_MASK		0xFFFFULL
29 
30 #define ALIGN_8B_CEIL(__a)	(((__a) + 7) & (-8))
31 
32 static const char def_pfl_name[] = "default";
33 
34 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
35 				      int blkaddr, u16 pcifunc);
36 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
37 				       u16 pcifunc);
38 
39 bool is_npc_intf_tx(u8 intf)
40 {
41 	return !!(intf & 0x1);
42 }
43 
44 bool is_npc_intf_rx(u8 intf)
45 {
46 	return !(intf & 0x1);
47 }
48 
49 bool is_npc_interface_valid(struct rvu *rvu, u8 intf)
50 {
51 	struct rvu_hwinfo *hw = rvu->hw;
52 
53 	return intf < hw->npc_intfs;
54 }
55 
56 int rvu_npc_get_tx_nibble_cfg(struct rvu *rvu, u64 nibble_ena)
57 {
58 	/* Due to a HW issue in these silicon versions, parse nibble enable
59 	 * configuration has to be identical for both Rx and Tx interfaces.
60 	 */
61 	if (is_rvu_96xx_B0(rvu))
62 		return nibble_ena;
63 	return 0;
64 }
65 
66 static int npc_mcam_verify_pf_func(struct rvu *rvu,
67 				   struct mcam_entry *entry_data, u8 intf,
68 				   u16 pcifunc)
69 {
70 	u16 pf_func, pf_func_mask;
71 
72 	if (is_npc_intf_rx(intf))
73 		return 0;
74 
75 	pf_func_mask = (entry_data->kw_mask[0] >> 32) &
76 		NPC_KEX_PF_FUNC_MASK;
77 	pf_func = (entry_data->kw[0] >> 32) & NPC_KEX_PF_FUNC_MASK;
78 
79 	pf_func = be16_to_cpu((__force __be16)pf_func);
80 	if (pf_func_mask != NPC_KEX_PF_FUNC_MASK ||
81 	    ((pf_func & ~RVU_PFVF_FUNC_MASK) !=
82 	     (pcifunc & ~RVU_PFVF_FUNC_MASK)))
83 		return -EINVAL;
84 
85 	return 0;
86 }
87 
88 void rvu_npc_set_pkind(struct rvu *rvu, int pkind, struct rvu_pfvf *pfvf)
89 {
90 	int blkaddr;
91 	u64 val = 0;
92 
93 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
94 	if (blkaddr < 0)
95 		return;
96 
97 	/* Config CPI base for the PKIND */
98 	val = pkind | 1ULL << 62;
99 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_CPI_DEFX(pkind, 0), val);
100 }
101 
102 int rvu_npc_get_pkind(struct rvu *rvu, u16 pf)
103 {
104 	struct npc_pkind *pkind = &rvu->hw->pkind;
105 	u32 map;
106 	int i;
107 
108 	for (i = 0; i < pkind->rsrc.max; i++) {
109 		map = pkind->pfchan_map[i];
110 		if (((map >> 16) & 0x3F) == pf)
111 			return i;
112 	}
113 	return -1;
114 }
115 
116 #define NPC_AF_ACTION0_PTR_ADVANCE	GENMASK_ULL(27, 20)
117 
118 int npc_config_ts_kpuaction(struct rvu *rvu, int pf, u16 pcifunc, bool enable)
119 {
120 	int pkind, blkaddr;
121 	u64 val;
122 
123 	pkind = rvu_npc_get_pkind(rvu, pf);
124 	if (pkind < 0) {
125 		dev_err(rvu->dev, "%s: pkind not mapped\n", __func__);
126 		return -EINVAL;
127 	}
128 
129 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
130 	if (blkaddr < 0) {
131 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
132 		return -EINVAL;
133 	}
134 
135 	val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
136 	val &= ~NPC_AF_ACTION0_PTR_ADVANCE;
137 	/* If timestamp is enabled then configure NPC to shift 8 bytes */
138 	if (enable)
139 		val |= FIELD_PREP(NPC_AF_ACTION0_PTR_ADVANCE,
140 				  NPC_HW_TSTAMP_OFFSET);
141 	rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
142 
143 	return 0;
144 }
145 
146 static int npc_get_ucast_mcam_index(struct npc_mcam *mcam, u16 pcifunc,
147 				    int nixlf)
148 {
149 	struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
150 	struct rvu *rvu = hw->rvu;
151 	int blkaddr = 0, max = 0;
152 	struct rvu_block *block;
153 	struct rvu_pfvf *pfvf;
154 
155 	pfvf = rvu_get_pfvf(rvu, pcifunc);
156 	/* Given a PF/VF and NIX LF number calculate the unicast mcam
157 	 * entry index based on the NIX block assigned to the PF/VF.
158 	 */
159 	blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
160 	while (blkaddr) {
161 		if (pfvf->nix_blkaddr == blkaddr)
162 			break;
163 		block = &rvu->hw->block[blkaddr];
164 		max += block->lf.max;
165 		blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
166 	}
167 
168 	return mcam->nixlf_offset + (max + nixlf) * RSVD_MCAM_ENTRIES_PER_NIXLF;
169 }
170 
171 int npc_get_nixlf_mcam_index(struct npc_mcam *mcam,
172 			     u16 pcifunc, int nixlf, int type)
173 {
174 	int pf = rvu_get_pf(pcifunc);
175 	int index;
176 
177 	/* Check if this is for a PF */
178 	if (pf && !(pcifunc & RVU_PFVF_FUNC_MASK)) {
179 		/* Reserved entries exclude PF0 */
180 		pf--;
181 		index = mcam->pf_offset + (pf * RSVD_MCAM_ENTRIES_PER_PF);
182 		/* Broadcast address matching entry should be first so
183 		 * that the packet can be replicated to all VFs.
184 		 */
185 		if (type == NIXLF_BCAST_ENTRY)
186 			return index;
187 		else if (type == NIXLF_ALLMULTI_ENTRY)
188 			return index + 1;
189 		else if (type == NIXLF_PROMISC_ENTRY)
190 			return index + 2;
191 	}
192 
193 	return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
194 }
195 
196 int npc_get_bank(struct npc_mcam *mcam, int index)
197 {
198 	int bank = index / mcam->banksize;
199 
200 	/* 0,1 & 2,3 banks are combined for this keysize */
201 	if (mcam->keysize == NPC_MCAM_KEY_X2)
202 		return bank ? 2 : 0;
203 
204 	return bank;
205 }
206 
207 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
208 			   int blkaddr, int index)
209 {
210 	int bank = npc_get_bank(mcam, index);
211 	u64 cfg;
212 
213 	index &= (mcam->banksize - 1);
214 	cfg = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_CFG(index, bank));
215 	return (cfg & 1);
216 }
217 
218 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
219 			   int blkaddr, int index, bool enable)
220 {
221 	int bank = npc_get_bank(mcam, index);
222 	int actbank = bank;
223 
224 	index &= (mcam->banksize - 1);
225 	for (; bank < (actbank + mcam->banks_per_entry); bank++) {
226 		rvu_write64(rvu, blkaddr,
227 			    NPC_AF_MCAMEX_BANKX_CFG(index, bank),
228 			    enable ? 1 : 0);
229 	}
230 }
231 
232 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
233 				 int blkaddr, int index)
234 {
235 	int bank = npc_get_bank(mcam, index);
236 	int actbank = bank;
237 
238 	index &= (mcam->banksize - 1);
239 	for (; bank < (actbank + mcam->banks_per_entry); bank++) {
240 		rvu_write64(rvu, blkaddr,
241 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
242 		rvu_write64(rvu, blkaddr,
243 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
244 
245 		rvu_write64(rvu, blkaddr,
246 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
247 		rvu_write64(rvu, blkaddr,
248 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
249 
250 		rvu_write64(rvu, blkaddr,
251 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
252 		rvu_write64(rvu, blkaddr,
253 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
254 	}
255 }
256 
257 static void npc_get_keyword(struct mcam_entry *entry, int idx,
258 			    u64 *cam0, u64 *cam1)
259 {
260 	u64 kw_mask = 0x00;
261 
262 #define CAM_MASK(n)	(BIT_ULL(n) - 1)
263 
264 	/* 0, 2, 4, 6 indices refer to BANKX_CAMX_W0 and
265 	 * 1, 3, 5, 7 indices refer to BANKX_CAMX_W1.
266 	 *
267 	 * Also, only 48 bits of BANKX_CAMX_W1 are valid.
268 	 */
269 	switch (idx) {
270 	case 0:
271 		/* BANK(X)_CAM_W0<63:0> = MCAM_KEY[KW0]<63:0> */
272 		*cam1 = entry->kw[0];
273 		kw_mask = entry->kw_mask[0];
274 		break;
275 	case 1:
276 		/* BANK(X)_CAM_W1<47:0> = MCAM_KEY[KW1]<47:0> */
277 		*cam1 = entry->kw[1] & CAM_MASK(48);
278 		kw_mask = entry->kw_mask[1] & CAM_MASK(48);
279 		break;
280 	case 2:
281 		/* BANK(X + 1)_CAM_W0<15:0> = MCAM_KEY[KW1]<63:48>
282 		 * BANK(X + 1)_CAM_W0<63:16> = MCAM_KEY[KW2]<47:0>
283 		 */
284 		*cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
285 		*cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
286 		kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
287 		kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
288 		break;
289 	case 3:
290 		/* BANK(X + 1)_CAM_W1<15:0> = MCAM_KEY[KW2]<63:48>
291 		 * BANK(X + 1)_CAM_W1<47:16> = MCAM_KEY[KW3]<31:0>
292 		 */
293 		*cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
294 		*cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
295 		kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
296 		kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
297 		break;
298 	case 4:
299 		/* BANK(X + 2)_CAM_W0<31:0> = MCAM_KEY[KW3]<63:32>
300 		 * BANK(X + 2)_CAM_W0<63:32> = MCAM_KEY[KW4]<31:0>
301 		 */
302 		*cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
303 		*cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
304 		kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
305 		kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
306 		break;
307 	case 5:
308 		/* BANK(X + 2)_CAM_W1<31:0> = MCAM_KEY[KW4]<63:32>
309 		 * BANK(X + 2)_CAM_W1<47:32> = MCAM_KEY[KW5]<15:0>
310 		 */
311 		*cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
312 		*cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
313 		kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
314 		kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
315 		break;
316 	case 6:
317 		/* BANK(X + 3)_CAM_W0<47:0> = MCAM_KEY[KW5]<63:16>
318 		 * BANK(X + 3)_CAM_W0<63:48> = MCAM_KEY[KW6]<15:0>
319 		 */
320 		*cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
321 		*cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
322 		kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
323 		kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
324 		break;
325 	case 7:
326 		/* BANK(X + 3)_CAM_W1<47:0> = MCAM_KEY[KW6]<63:16> */
327 		*cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
328 		kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
329 		break;
330 	}
331 
332 	*cam1 &= kw_mask;
333 	*cam0 = ~*cam1 & kw_mask;
334 }
335 
336 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
337 			       u64 cam0, u64 cam1)
338 {
339 	/* Similar to npc_get_keyword, but fills mcam_entry structure from
340 	 * CAM registers.
341 	 */
342 	switch (idx) {
343 	case 0:
344 		entry->kw[0] = cam1;
345 		entry->kw_mask[0] = cam1 ^ cam0;
346 		break;
347 	case 1:
348 		entry->kw[1] = cam1;
349 		entry->kw_mask[1] = cam1 ^ cam0;
350 		break;
351 	case 2:
352 		entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
353 		entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
354 		entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
355 		entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
356 		break;
357 	case 3:
358 		entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
359 		entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
360 		entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
361 		entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
362 		break;
363 	case 4:
364 		entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
365 		entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
366 		entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
367 		entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
368 		break;
369 	case 5:
370 		entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
371 		entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
372 		entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
373 		entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
374 		break;
375 	case 6:
376 		entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
377 		entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
378 		entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
379 		entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
380 		break;
381 	case 7:
382 		entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
383 		entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
384 		break;
385 	}
386 }
387 
388 static u64 npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
389 					int blkaddr, u16 pf_func)
390 {
391 	int bank, nixlf, index;
392 
393 	/* get ucast entry rule entry index */
394 	nix_get_nixlf(rvu, pf_func, &nixlf, NULL);
395 	index = npc_get_nixlf_mcam_index(mcam, pf_func, nixlf,
396 					 NIXLF_UCAST_ENTRY);
397 	bank = npc_get_bank(mcam, index);
398 	index &= (mcam->banksize - 1);
399 
400 	return rvu_read64(rvu, blkaddr,
401 			  NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
402 }
403 
404 static void npc_fixup_vf_rule(struct rvu *rvu, struct npc_mcam *mcam,
405 			      int blkaddr, int index, struct mcam_entry *entry,
406 			      bool *enable)
407 {
408 	u16 owner, target_func;
409 	struct rvu_pfvf *pfvf;
410 	u64 rx_action;
411 
412 	owner = mcam->entry2pfvf_map[index];
413 	target_func = (entry->action >> 4) & 0xffff;
414 	/* do nothing when target is LBK/PF or owner is not PF */
415 	if (is_pffunc_af(owner) || is_afvf(target_func) ||
416 	    (owner & RVU_PFVF_FUNC_MASK) ||
417 	    !(target_func & RVU_PFVF_FUNC_MASK))
418 		return;
419 
420 	/* save entry2target_pffunc */
421 	pfvf = rvu_get_pfvf(rvu, target_func);
422 	mcam->entry2target_pffunc[index] = target_func;
423 
424 	/* don't enable rule when nixlf not attached or initialized */
425 	if (!(is_nixlf_attached(rvu, target_func) &&
426 	      test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
427 		*enable = false;
428 
429 	/* copy VF default entry action to the VF mcam entry */
430 	rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
431 						 target_func);
432 	if (rx_action)
433 		entry->action = rx_action;
434 }
435 
436 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
437 				  int blkaddr, int index, u8 intf,
438 				  struct mcam_entry *entry, bool enable)
439 {
440 	int bank = npc_get_bank(mcam, index);
441 	int kw = 0, actbank, actindex;
442 	u8 tx_intf_mask = ~intf & 0x3;
443 	u8 tx_intf = intf;
444 	u64 cam0, cam1;
445 
446 	actbank = bank; /* Save bank id, to set action later on */
447 	actindex = index;
448 	index &= (mcam->banksize - 1);
449 
450 	/* Disable before mcam entry update */
451 	npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
452 
453 	/* Clear mcam entry to avoid writes being suppressed by NPC */
454 	npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
455 
456 	/* CAM1 takes the comparison value and
457 	 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
458 	 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
459 	 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
460 	 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
461 	 */
462 	for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
463 		/* Interface should be set in all banks */
464 		if (is_npc_intf_tx(intf)) {
465 			/* Last bit must be set and rest don't care
466 			 * for TX interfaces
467 			 */
468 			tx_intf_mask = 0x1;
469 			tx_intf = intf & tx_intf_mask;
470 			tx_intf_mask = ~tx_intf & tx_intf_mask;
471 		}
472 
473 		rvu_write64(rvu, blkaddr,
474 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
475 			    tx_intf);
476 		rvu_write64(rvu, blkaddr,
477 			    NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
478 			    tx_intf_mask);
479 
480 		/* Set the match key */
481 		npc_get_keyword(entry, kw, &cam0, &cam1);
482 		rvu_write64(rvu, blkaddr,
483 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
484 		rvu_write64(rvu, blkaddr,
485 			    NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
486 
487 		npc_get_keyword(entry, kw + 1, &cam0, &cam1);
488 		rvu_write64(rvu, blkaddr,
489 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
490 		rvu_write64(rvu, blkaddr,
491 			    NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
492 	}
493 
494 	/* PF installing VF rule */
495 	if (intf == NIX_INTF_RX && actindex < mcam->bmap_entries)
496 		npc_fixup_vf_rule(rvu, mcam, blkaddr, index, entry, &enable);
497 
498 	/* Set 'action' */
499 	rvu_write64(rvu, blkaddr,
500 		    NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
501 
502 	/* Set TAG 'action' */
503 	rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
504 		    entry->vtag_action);
505 
506 	/* Enable the entry */
507 	if (enable)
508 		npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
509 }
510 
511 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
512 			 int blkaddr, u16 src,
513 			 struct mcam_entry *entry, u8 *intf, u8 *ena)
514 {
515 	int sbank = npc_get_bank(mcam, src);
516 	int bank, kw = 0;
517 	u64 cam0, cam1;
518 
519 	src &= (mcam->banksize - 1);
520 	bank = sbank;
521 
522 	for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
523 		cam1 = rvu_read64(rvu, blkaddr,
524 				  NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
525 		cam0 = rvu_read64(rvu, blkaddr,
526 				  NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
527 		npc_fill_entryword(entry, kw, cam0, cam1);
528 
529 		cam1 = rvu_read64(rvu, blkaddr,
530 				  NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
531 		cam0 = rvu_read64(rvu, blkaddr,
532 				  NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
533 		npc_fill_entryword(entry, kw + 1, cam0, cam1);
534 	}
535 
536 	entry->action = rvu_read64(rvu, blkaddr,
537 				   NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
538 	entry->vtag_action =
539 		rvu_read64(rvu, blkaddr,
540 			   NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
541 	*intf = rvu_read64(rvu, blkaddr,
542 			   NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
543 	*ena = rvu_read64(rvu, blkaddr,
544 			  NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
545 }
546 
547 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
548 				int blkaddr, u16 src, u16 dest)
549 {
550 	int dbank = npc_get_bank(mcam, dest);
551 	int sbank = npc_get_bank(mcam, src);
552 	u64 cfg, sreg, dreg;
553 	int bank, i;
554 
555 	src &= (mcam->banksize - 1);
556 	dest &= (mcam->banksize - 1);
557 
558 	/* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
559 	for (bank = 0; bank < mcam->banks_per_entry; bank++) {
560 		sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
561 		dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
562 		for (i = 0; i < 6; i++) {
563 			cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
564 			rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
565 		}
566 	}
567 
568 	/* Copy action */
569 	cfg = rvu_read64(rvu, blkaddr,
570 			 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
571 	rvu_write64(rvu, blkaddr,
572 		    NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
573 
574 	/* Copy TAG action */
575 	cfg = rvu_read64(rvu, blkaddr,
576 			 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
577 	rvu_write64(rvu, blkaddr,
578 		    NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
579 
580 	/* Enable or disable */
581 	cfg = rvu_read64(rvu, blkaddr,
582 			 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
583 	rvu_write64(rvu, blkaddr,
584 		    NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
585 }
586 
587 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
588 			       int blkaddr, int index)
589 {
590 	int bank = npc_get_bank(mcam, index);
591 
592 	index &= (mcam->banksize - 1);
593 	return rvu_read64(rvu, blkaddr,
594 			  NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
595 }
596 
597 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
598 				 int nixlf, u64 chan, u8 *mac_addr)
599 {
600 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
601 	struct npc_install_flow_req req = { 0 };
602 	struct npc_install_flow_rsp rsp = { 0 };
603 	struct npc_mcam *mcam = &rvu->hw->mcam;
604 	struct nix_rx_action action;
605 	int blkaddr, index;
606 
607 	/* AF's and SDP VFs work in promiscuous mode */
608 	if (is_afvf(pcifunc) || is_sdp_vf(pcifunc))
609 		return;
610 
611 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
612 	if (blkaddr < 0)
613 		return;
614 
615 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
616 					 nixlf, NIXLF_UCAST_ENTRY);
617 
618 	/* Don't change the action if entry is already enabled
619 	 * Otherwise RSS action may get overwritten.
620 	 */
621 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
622 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
623 						      blkaddr, index);
624 	} else {
625 		*(u64 *)&action = 0x00;
626 		action.op = NIX_RX_ACTIONOP_UCAST;
627 		action.pf_func = pcifunc;
628 	}
629 
630 	req.default_rule = 1;
631 	ether_addr_copy(req.packet.dmac, mac_addr);
632 	eth_broadcast_addr((u8 *)&req.mask.dmac);
633 	req.features = BIT_ULL(NPC_DMAC);
634 	req.channel = chan;
635 	req.chan_mask = 0xFFFU;
636 	req.intf = pfvf->nix_rx_intf;
637 	req.op = action.op;
638 	req.hdr.pcifunc = 0; /* AF is requester */
639 	req.vf = action.pf_func;
640 	req.index = action.index;
641 	req.match_id = action.match_id;
642 	req.flow_key_alg = action.flow_key_alg;
643 
644 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
645 }
646 
647 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
648 				   int nixlf, u64 chan, u8 chan_cnt)
649 {
650 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
651 	struct npc_install_flow_req req = { 0 };
652 	struct npc_install_flow_rsp rsp = { 0 };
653 	struct npc_mcam *mcam = &rvu->hw->mcam;
654 	struct rvu_hwinfo *hw = rvu->hw;
655 	int blkaddr, ucast_idx, index;
656 	struct nix_rx_action action;
657 	u64 relaxed_mask;
658 
659 	if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
660 		return;
661 
662 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
663 	if (blkaddr < 0)
664 		return;
665 
666 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
667 					 nixlf, NIXLF_PROMISC_ENTRY);
668 
669 	if (is_cgx_vf(rvu, pcifunc))
670 		index = npc_get_nixlf_mcam_index(mcam,
671 						 pcifunc & ~RVU_PFVF_FUNC_MASK,
672 						 nixlf, NIXLF_PROMISC_ENTRY);
673 
674 	/* If the corresponding PF's ucast action is RSS,
675 	 * use the same action for promisc also
676 	 */
677 	ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
678 					     nixlf, NIXLF_UCAST_ENTRY);
679 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
680 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
681 						      blkaddr, ucast_idx);
682 
683 	if (action.op != NIX_RX_ACTIONOP_RSS) {
684 		*(u64 *)&action = 0x00;
685 		action.op = NIX_RX_ACTIONOP_UCAST;
686 	}
687 
688 	/* RX_ACTION set to MCAST for CGX PF's */
689 	if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
690 	    is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
691 		*(u64 *)&action = 0x00;
692 		action.op = NIX_RX_ACTIONOP_MCAST;
693 		pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
694 		action.index = pfvf->promisc_mce_idx;
695 	}
696 
697 	/* For cn10k the upper two bits of the channel number are
698 	 * cpt channel number. with masking out these bits in the
699 	 * mcam entry, same entry used for NIX will allow packets
700 	 * received from cpt for parsing.
701 	 */
702 	if (!is_rvu_otx2(rvu)) {
703 		req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
704 	} else {
705 		req.chan_mask = 0xFFFU;
706 	}
707 
708 	if (chan_cnt > 1) {
709 		if (!is_power_of_2(chan_cnt)) {
710 			dev_err(rvu->dev,
711 				"%s: channel count more than 1, must be power of 2\n", __func__);
712 			return;
713 		}
714 		relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
715 					   ilog2(chan_cnt));
716 		req.chan_mask &= relaxed_mask;
717 	}
718 
719 	req.channel = chan;
720 	req.intf = pfvf->nix_rx_intf;
721 	req.entry = index;
722 	req.op = action.op;
723 	req.hdr.pcifunc = 0; /* AF is requester */
724 	req.vf = pcifunc;
725 	req.index = action.index;
726 	req.match_id = action.match_id;
727 	req.flow_key_alg = action.flow_key_alg;
728 
729 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
730 }
731 
732 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
733 				  int nixlf, bool enable)
734 {
735 	struct npc_mcam *mcam = &rvu->hw->mcam;
736 	int blkaddr, index;
737 
738 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
739 	if (blkaddr < 0)
740 		return;
741 
742 	/* Get 'pcifunc' of PF device */
743 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
744 
745 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
746 					 nixlf, NIXLF_PROMISC_ENTRY);
747 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
748 }
749 
750 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
751 				       int nixlf, u64 chan)
752 {
753 	struct rvu_pfvf *pfvf;
754 	struct npc_install_flow_req req = { 0 };
755 	struct npc_install_flow_rsp rsp = { 0 };
756 	struct npc_mcam *mcam = &rvu->hw->mcam;
757 	struct rvu_hwinfo *hw = rvu->hw;
758 	int blkaddr, index;
759 
760 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
761 	if (blkaddr < 0)
762 		return;
763 
764 	/* Skip LBK VFs */
765 	if (is_afvf(pcifunc))
766 		return;
767 
768 	/* If pkt replication is not supported,
769 	 * then only PF is allowed to add a bcast match entry.
770 	 */
771 	if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
772 		return;
773 
774 	/* Get 'pcifunc' of PF device */
775 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
776 	pfvf = rvu_get_pfvf(rvu, pcifunc);
777 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
778 					 nixlf, NIXLF_BCAST_ENTRY);
779 
780 	if (!hw->cap.nix_rx_multicast) {
781 		/* Early silicon doesn't support pkt replication,
782 		 * so install entry with UCAST action, so that PF
783 		 * receives all broadcast packets.
784 		 */
785 		req.op = NIX_RX_ACTIONOP_UCAST;
786 	} else {
787 		req.op = NIX_RX_ACTIONOP_MCAST;
788 		req.index = pfvf->bcast_mce_idx;
789 	}
790 
791 	eth_broadcast_addr((u8 *)&req.packet.dmac);
792 	eth_broadcast_addr((u8 *)&req.mask.dmac);
793 	req.features = BIT_ULL(NPC_DMAC);
794 	req.channel = chan;
795 	req.chan_mask = 0xFFFU;
796 	req.intf = pfvf->nix_rx_intf;
797 	req.entry = index;
798 	req.hdr.pcifunc = 0; /* AF is requester */
799 	req.vf = pcifunc;
800 
801 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
802 }
803 
804 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
805 				bool enable)
806 {
807 	struct npc_mcam *mcam = &rvu->hw->mcam;
808 	int blkaddr, index;
809 
810 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
811 	if (blkaddr < 0)
812 		return;
813 
814 	/* Get 'pcifunc' of PF device */
815 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
816 
817 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
818 					 NIXLF_BCAST_ENTRY);
819 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
820 }
821 
822 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
823 				    u64 chan)
824 {
825 	struct npc_install_flow_req req = { 0 };
826 	struct npc_install_flow_rsp rsp = { 0 };
827 	struct npc_mcam *mcam = &rvu->hw->mcam;
828 	struct rvu_hwinfo *hw = rvu->hw;
829 	int blkaddr, ucast_idx, index;
830 	u8 mac_addr[ETH_ALEN] = { 0 };
831 	struct nix_rx_action action;
832 	struct rvu_pfvf *pfvf;
833 	u16 vf_func;
834 
835 	/* Only CGX PF/VF can add allmulticast entry */
836 	if (is_afvf(pcifunc) && is_sdp_vf(pcifunc))
837 		return;
838 
839 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
840 	if (blkaddr < 0)
841 		return;
842 
843 	/* Get 'pcifunc' of PF device */
844 	vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
845 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
846 	pfvf = rvu_get_pfvf(rvu, pcifunc);
847 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
848 					 nixlf, NIXLF_ALLMULTI_ENTRY);
849 
850 	/* If the corresponding PF's ucast action is RSS,
851 	 * use the same action for multicast entry also
852 	 */
853 	ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
854 					     nixlf, NIXLF_UCAST_ENTRY);
855 	if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
856 		*(u64 *)&action = npc_get_mcam_action(rvu, mcam,
857 							blkaddr, ucast_idx);
858 
859 	if (action.op != NIX_RX_ACTIONOP_RSS) {
860 		*(u64 *)&action = 0x00;
861 		action.op = NIX_RX_ACTIONOP_UCAST;
862 		action.pf_func = pcifunc;
863 	}
864 
865 	/* RX_ACTION set to MCAST for CGX PF's */
866 	if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
867 		*(u64 *)&action = 0x00;
868 		action.op = NIX_RX_ACTIONOP_MCAST;
869 		action.index = pfvf->mcast_mce_idx;
870 	}
871 
872 	mac_addr[0] = 0x01;	/* LSB bit of 1st byte in DMAC */
873 	ether_addr_copy(req.packet.dmac, mac_addr);
874 	ether_addr_copy(req.mask.dmac, mac_addr);
875 	req.features = BIT_ULL(NPC_DMAC);
876 
877 	/* For cn10k the upper two bits of the channel number are
878 	 * cpt channel number. with masking out these bits in the
879 	 * mcam entry, same entry used for NIX will allow packets
880 	 * received from cpt for parsing.
881 	 */
882 	if (!is_rvu_otx2(rvu))
883 		req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
884 	else
885 		req.chan_mask = 0xFFFU;
886 
887 	req.channel = chan;
888 	req.intf = pfvf->nix_rx_intf;
889 	req.entry = index;
890 	req.op = action.op;
891 	req.hdr.pcifunc = 0; /* AF is requester */
892 	req.vf = pcifunc | vf_func;
893 	req.index = action.index;
894 	req.match_id = action.match_id;
895 	req.flow_key_alg = action.flow_key_alg;
896 
897 	rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
898 }
899 
900 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
901 				   bool enable)
902 {
903 	struct npc_mcam *mcam = &rvu->hw->mcam;
904 	int blkaddr, index;
905 
906 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
907 	if (blkaddr < 0)
908 		return;
909 
910 	/* Get 'pcifunc' of PF device */
911 	pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
912 
913 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
914 					 NIXLF_ALLMULTI_ENTRY);
915 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
916 }
917 
918 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
919 				     int blkaddr, u16 pcifunc, u64 rx_action)
920 {
921 	int actindex, index, bank;
922 	bool enable;
923 
924 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
925 		return;
926 
927 	mutex_lock(&mcam->lock);
928 	for (index = 0; index < mcam->bmap_entries; index++) {
929 		if (mcam->entry2target_pffunc[index] == pcifunc) {
930 			bank = npc_get_bank(mcam, index);
931 			actindex = index;
932 			index &= (mcam->banksize - 1);
933 
934 			/* read vf flow entry enable status */
935 			enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
936 						       actindex);
937 			/* disable before mcam entry update */
938 			npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
939 					      false);
940 			/* update 'action' */
941 			rvu_write64(rvu, blkaddr,
942 				    NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
943 				    rx_action);
944 			if (enable)
945 				npc_enable_mcam_entry(rvu, mcam, blkaddr,
946 						      actindex, true);
947 		}
948 	}
949 	mutex_unlock(&mcam->lock);
950 }
951 
952 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
953 				    int group, int alg_idx, int mcam_index)
954 {
955 	struct npc_mcam *mcam = &rvu->hw->mcam;
956 	struct rvu_hwinfo *hw = rvu->hw;
957 	struct nix_rx_action action;
958 	int blkaddr, index, bank;
959 	struct rvu_pfvf *pfvf;
960 
961 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
962 	if (blkaddr < 0)
963 		return;
964 
965 	/* Check if this is for reserved default entry */
966 	if (mcam_index < 0) {
967 		if (group != DEFAULT_RSS_CONTEXT_GROUP)
968 			return;
969 		index = npc_get_nixlf_mcam_index(mcam, pcifunc,
970 						 nixlf, NIXLF_UCAST_ENTRY);
971 	} else {
972 		/* TODO: validate this mcam index */
973 		index = mcam_index;
974 	}
975 
976 	if (index >= mcam->total_entries)
977 		return;
978 
979 	bank = npc_get_bank(mcam, index);
980 	index &= (mcam->banksize - 1);
981 
982 	*(u64 *)&action = rvu_read64(rvu, blkaddr,
983 				     NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
984 	/* Ignore if no action was set earlier */
985 	if (!*(u64 *)&action)
986 		return;
987 
988 	action.op = NIX_RX_ACTIONOP_RSS;
989 	action.pf_func = pcifunc;
990 	action.index = group;
991 	action.flow_key_alg = alg_idx;
992 
993 	rvu_write64(rvu, blkaddr,
994 		    NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
995 
996 	/* update the VF flow rule action with the VF default entry action */
997 	if (mcam_index < 0)
998 		npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
999 					 *(u64 *)&action);
1000 
1001 	/* update the action change in default rule */
1002 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1003 	if (pfvf->def_ucast_rule)
1004 		pfvf->def_ucast_rule->rx_action = action;
1005 
1006 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1007 					 nixlf, NIXLF_PROMISC_ENTRY);
1008 
1009 	/* If PF's promiscuous entry is enabled,
1010 	 * Set RSS action for that entry as well
1011 	 */
1012 	if ((!hw->cap.nix_rx_multicast || !pfvf->use_mce_list) &&
1013 	    is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
1014 		bank = npc_get_bank(mcam, index);
1015 		index &= (mcam->banksize - 1);
1016 
1017 		rvu_write64(rvu, blkaddr,
1018 			    NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
1019 			    *(u64 *)&action);
1020 	}
1021 }
1022 
1023 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1024 				  int nixlf, int type, bool enable)
1025 {
1026 	struct npc_mcam *mcam = &rvu->hw->mcam;
1027 	struct rvu_hwinfo *hw = rvu->hw;
1028 	struct nix_mce_list *mce_list;
1029 	int index, blkaddr, mce_idx;
1030 	struct rvu_pfvf *pfvf;
1031 
1032 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1033 	if (blkaddr < 0)
1034 		return;
1035 
1036 	index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1037 					 nixlf, type);
1038 
1039 	/* disable MCAM entry when packet replication is not supported by hw */
1040 	if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1041 		npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1042 		return;
1043 	}
1044 
1045 	/* return incase mce list is not enabled */
1046 	pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1047 	if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1048 	    type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1049 		return;
1050 
1051 	nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1052 
1053 	nix_update_mce_list(rvu, pcifunc, mce_list,
1054 			    mce_idx, index, enable);
1055 	if (enable)
1056 		npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1057 }
1058 
1059 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1060 				       int nixlf, bool enable)
1061 {
1062 	struct npc_mcam *mcam = &rvu->hw->mcam;
1063 	int index, blkaddr;
1064 
1065 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1066 	if (blkaddr < 0)
1067 		return;
1068 
1069 	/* Ucast MCAM match entry of this PF/VF */
1070 	index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1071 					 nixlf, NIXLF_UCAST_ENTRY);
1072 	npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1073 
1074 	/* Nothing to do for VFs, on platforms where pkt replication
1075 	 * is not supported
1076 	 */
1077 	if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1078 		return;
1079 
1080 	/* add/delete pf_func to broadcast MCE list */
1081 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1082 				     NIXLF_BCAST_ENTRY, enable);
1083 }
1084 
1085 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1086 {
1087 	npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1088 
1089 	/* Delete multicast and promisc MCAM entries */
1090 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1091 				     NIXLF_ALLMULTI_ENTRY, false);
1092 	npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1093 				     NIXLF_PROMISC_ENTRY, false);
1094 }
1095 
1096 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1097 {
1098 	/* Enables only broadcast match entry. Promisc/Allmulti are enabled
1099 	 * in set_rx_mode mbox handler.
1100 	 */
1101 	npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1102 }
1103 
1104 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1105 {
1106 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1107 	struct npc_mcam *mcam = &rvu->hw->mcam;
1108 	struct rvu_npc_mcam_rule *rule, *tmp;
1109 	int blkaddr;
1110 
1111 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1112 	if (blkaddr < 0)
1113 		return;
1114 
1115 	mutex_lock(&mcam->lock);
1116 
1117 	/* Disable MCAM entries directing traffic to this 'pcifunc' */
1118 	list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1119 		if (is_npc_intf_rx(rule->intf) &&
1120 		    rule->rx_action.pf_func == pcifunc &&
1121 		    rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1122 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1123 					      rule->entry, false);
1124 			rule->enable = false;
1125 			/* Indicate that default rule is disabled */
1126 			if (rule->default_rule) {
1127 				pfvf->def_ucast_rule = NULL;
1128 				list_del(&rule->list);
1129 				kfree(rule);
1130 			}
1131 		}
1132 	}
1133 
1134 	mutex_unlock(&mcam->lock);
1135 
1136 	npc_mcam_disable_flows(rvu, pcifunc);
1137 
1138 	rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1139 }
1140 
1141 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1142 {
1143 	struct npc_mcam *mcam = &rvu->hw->mcam;
1144 	struct rvu_npc_mcam_rule *rule, *tmp;
1145 	int blkaddr;
1146 
1147 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1148 	if (blkaddr < 0)
1149 		return;
1150 
1151 	mutex_lock(&mcam->lock);
1152 
1153 	/* Free all MCAM entries owned by this 'pcifunc' */
1154 	npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1155 
1156 	/* Free all MCAM counters owned by this 'pcifunc' */
1157 	npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1158 
1159 	/* Delete MCAM entries owned by this 'pcifunc' */
1160 	list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1161 		if (rule->owner == pcifunc && !rule->default_rule) {
1162 			list_del(&rule->list);
1163 			kfree(rule);
1164 		}
1165 	}
1166 
1167 	mutex_unlock(&mcam->lock);
1168 
1169 	rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1170 }
1171 
1172 #define SET_KEX_LD(intf, lid, ltype, ld, cfg)	\
1173 	rvu_write64(rvu, blkaddr,			\
1174 		NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, ltype, ld), cfg)
1175 
1176 #define SET_KEX_LDFLAGS(intf, ld, flags, cfg)	\
1177 	rvu_write64(rvu, blkaddr,			\
1178 		NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, flags), cfg)
1179 
1180 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1181 				struct npc_mcam_kex *mkex, u8 intf)
1182 {
1183 	int lid, lt, ld, fl;
1184 
1185 	if (is_npc_intf_tx(intf))
1186 		return;
1187 
1188 	rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1189 		    mkex->keyx_cfg[NIX_INTF_RX]);
1190 
1191 	/* Program LDATA */
1192 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
1193 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
1194 			for (ld = 0; ld < NPC_MAX_LD; ld++)
1195 				SET_KEX_LD(intf, lid, lt, ld,
1196 					   mkex->intf_lid_lt_ld[NIX_INTF_RX]
1197 					   [lid][lt][ld]);
1198 		}
1199 	}
1200 	/* Program LFLAGS */
1201 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
1202 		for (fl = 0; fl < NPC_MAX_LFL; fl++)
1203 			SET_KEX_LDFLAGS(intf, ld, fl,
1204 					mkex->intf_ld_flags[NIX_INTF_RX]
1205 					[ld][fl]);
1206 	}
1207 }
1208 
1209 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1210 				struct npc_mcam_kex *mkex, u8 intf)
1211 {
1212 	int lid, lt, ld, fl;
1213 
1214 	if (is_npc_intf_rx(intf))
1215 		return;
1216 
1217 	rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1218 		    mkex->keyx_cfg[NIX_INTF_TX]);
1219 
1220 	/* Program LDATA */
1221 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
1222 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
1223 			for (ld = 0; ld < NPC_MAX_LD; ld++)
1224 				SET_KEX_LD(intf, lid, lt, ld,
1225 					   mkex->intf_lid_lt_ld[NIX_INTF_TX]
1226 					   [lid][lt][ld]);
1227 		}
1228 	}
1229 	/* Program LFLAGS */
1230 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
1231 		for (fl = 0; fl < NPC_MAX_LFL; fl++)
1232 			SET_KEX_LDFLAGS(intf, ld, fl,
1233 					mkex->intf_ld_flags[NIX_INTF_TX]
1234 					[ld][fl]);
1235 	}
1236 }
1237 
1238 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1239 				     struct npc_mcam_kex *mkex)
1240 {
1241 	struct rvu_hwinfo *hw = rvu->hw;
1242 	u8 intf;
1243 	int ld;
1244 
1245 	for (ld = 0; ld < NPC_MAX_LD; ld++)
1246 		rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1247 			    mkex->kex_ld_flags[ld]);
1248 
1249 	for (intf = 0; intf < hw->npc_intfs; intf++) {
1250 		npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1251 		npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1252 	}
1253 }
1254 
1255 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1256 				 u64 *size)
1257 {
1258 	u64 prfl_addr, prfl_sz;
1259 
1260 	if (!rvu->fwdata)
1261 		return -EINVAL;
1262 
1263 	prfl_addr = rvu->fwdata->mcam_addr;
1264 	prfl_sz = rvu->fwdata->mcam_sz;
1265 
1266 	if (!prfl_addr || !prfl_sz)
1267 		return -EINVAL;
1268 
1269 	*prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1270 	if (!(*prfl_img_addr))
1271 		return -ENOMEM;
1272 
1273 	*size = prfl_sz;
1274 
1275 	return 0;
1276 }
1277 
1278 /* strtoull of "mkexprof" with base:36 */
1279 #define MKEX_END_SIGN  0xdeadbeef
1280 
1281 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1282 				  const char *mkex_profile)
1283 {
1284 	struct device *dev = &rvu->pdev->dev;
1285 	struct npc_mcam_kex *mcam_kex;
1286 	void __iomem *mkex_prfl_addr = NULL;
1287 	u64 prfl_sz;
1288 	int ret;
1289 
1290 	/* If user not selected mkex profile */
1291 	if (rvu->kpu_fwdata_sz ||
1292 	    !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1293 		goto program_mkex;
1294 
1295 	/* Setting up the mapping for mkex profile image */
1296 	ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1297 	if (ret < 0)
1298 		goto program_mkex;
1299 
1300 	mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1301 
1302 	while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1303 		/* Compare with mkex mod_param name string */
1304 		if (mcam_kex->mkex_sign == MKEX_SIGN &&
1305 		    !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1306 			/* Due to an errata (35786) in A0/B0 pass silicon,
1307 			 * parse nibble enable configuration has to be
1308 			 * identical for both Rx and Tx interfaces.
1309 			 */
1310 			if (!is_rvu_96xx_B0(rvu) ||
1311 			    mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1312 				rvu->kpu.mkex = mcam_kex;
1313 			goto program_mkex;
1314 		}
1315 
1316 		mcam_kex++;
1317 		prfl_sz -= sizeof(struct npc_mcam_kex);
1318 	}
1319 	dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1320 
1321 program_mkex:
1322 	dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1323 	/* Program selected mkex profile */
1324 	npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1325 	if (mkex_prfl_addr)
1326 		iounmap(mkex_prfl_addr);
1327 }
1328 
1329 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1330 				 const struct npc_kpu_profile_action *kpuaction,
1331 				 int kpu, int entry, bool pkind)
1332 {
1333 	struct npc_kpu_action0 action0 = {0};
1334 	struct npc_kpu_action1 action1 = {0};
1335 	u64 reg;
1336 
1337 	action1.errlev = kpuaction->errlev;
1338 	action1.errcode = kpuaction->errcode;
1339 	action1.dp0_offset = kpuaction->dp0_offset;
1340 	action1.dp1_offset = kpuaction->dp1_offset;
1341 	action1.dp2_offset = kpuaction->dp2_offset;
1342 
1343 	if (pkind)
1344 		reg = NPC_AF_PKINDX_ACTION1(entry);
1345 	else
1346 		reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1347 
1348 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1349 
1350 	action0.byp_count = kpuaction->bypass_count;
1351 	action0.capture_ena = kpuaction->cap_ena;
1352 	action0.parse_done = kpuaction->parse_done;
1353 	action0.next_state = kpuaction->next_state;
1354 	action0.capture_lid = kpuaction->lid;
1355 	action0.capture_ltype = kpuaction->ltype;
1356 	action0.capture_flags = kpuaction->flags;
1357 	action0.ptr_advance = kpuaction->ptr_advance;
1358 	action0.var_len_offset = kpuaction->offset;
1359 	action0.var_len_mask = kpuaction->mask;
1360 	action0.var_len_right = kpuaction->right;
1361 	action0.var_len_shift = kpuaction->shift;
1362 
1363 	if (pkind)
1364 		reg = NPC_AF_PKINDX_ACTION0(entry);
1365 	else
1366 		reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1367 
1368 	rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1369 }
1370 
1371 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1372 			      const struct npc_kpu_profile_cam *kpucam,
1373 			      int kpu, int entry)
1374 {
1375 	struct npc_kpu_cam cam0 = {0};
1376 	struct npc_kpu_cam cam1 = {0};
1377 
1378 	cam1.state = kpucam->state & kpucam->state_mask;
1379 	cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1380 	cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1381 	cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1382 
1383 	cam0.state = ~kpucam->state & kpucam->state_mask;
1384 	cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1385 	cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1386 	cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1387 
1388 	rvu_write64(rvu, blkaddr,
1389 		    NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1390 	rvu_write64(rvu, blkaddr,
1391 		    NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1392 }
1393 
1394 static inline u64 enable_mask(int count)
1395 {
1396 	return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1397 }
1398 
1399 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1400 				    const struct npc_kpu_profile *profile)
1401 {
1402 	int entry, num_entries, max_entries;
1403 	u64 entry_mask;
1404 
1405 	if (profile->cam_entries != profile->action_entries) {
1406 		dev_err(rvu->dev,
1407 			"KPU%d: CAM and action entries [%d != %d] not equal\n",
1408 			kpu, profile->cam_entries, profile->action_entries);
1409 	}
1410 
1411 	max_entries = rvu->hw->npc_kpu_entries;
1412 
1413 	/* Program CAM match entries for previous KPU extracted data */
1414 	num_entries = min_t(int, profile->cam_entries, max_entries);
1415 	for (entry = 0; entry < num_entries; entry++)
1416 		npc_config_kpucam(rvu, blkaddr,
1417 				  &profile->cam[entry], kpu, entry);
1418 
1419 	/* Program this KPU's actions */
1420 	num_entries = min_t(int, profile->action_entries, max_entries);
1421 	for (entry = 0; entry < num_entries; entry++)
1422 		npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1423 				     kpu, entry, false);
1424 
1425 	/* Enable all programmed entries */
1426 	num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1427 	entry_mask = enable_mask(num_entries);
1428 	/* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1429 	if (!rvu->kpu.custom)
1430 		entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1431 	rvu_write64(rvu, blkaddr,
1432 		    NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1433 	if (num_entries > 64) {
1434 		rvu_write64(rvu, blkaddr,
1435 			    NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1436 			    enable_mask(num_entries - 64));
1437 	}
1438 
1439 	/* Enable this KPU */
1440 	rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1441 }
1442 
1443 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1444 {
1445 	profile->custom = 0;
1446 	profile->name = def_pfl_name;
1447 	profile->version = NPC_KPU_PROFILE_VER;
1448 	profile->ikpu = ikpu_action_entries;
1449 	profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1450 	profile->kpu = npc_kpu_profiles;
1451 	profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1452 	profile->lt_def = &npc_lt_defaults;
1453 	profile->mkex = &npc_mkex_default;
1454 
1455 	return 0;
1456 }
1457 
1458 static int npc_apply_custom_kpu(struct rvu *rvu,
1459 				struct npc_kpu_profile_adapter *profile)
1460 {
1461 	size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1462 	struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1463 	struct npc_kpu_profile_action *action;
1464 	struct npc_kpu_profile_cam *cam;
1465 	struct npc_kpu_fwdata *fw_kpu;
1466 	int entries;
1467 	u16 kpu, entry;
1468 
1469 	if (rvu->kpu_fwdata_sz < hdr_sz) {
1470 		dev_warn(rvu->dev, "Invalid KPU profile size\n");
1471 		return -EINVAL;
1472 	}
1473 	if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1474 		dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1475 			 fw->signature);
1476 		return -EINVAL;
1477 	}
1478 	/* Verify if the using known profile structure */
1479 	if (NPC_KPU_VER_MAJ(profile->version) >
1480 	    NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1481 		dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1482 			 NPC_KPU_VER_MAJ(profile->version),
1483 			 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1484 		return -EINVAL;
1485 	}
1486 	/* Verify if profile is aligned with the required kernel changes */
1487 	if (NPC_KPU_VER_MIN(profile->version) <
1488 	    NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1489 		dev_warn(rvu->dev,
1490 			 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1491 			 NPC_KPU_VER_MAJ(profile->version),
1492 			 NPC_KPU_VER_MIN(profile->version),
1493 			 NPC_KPU_VER_PATCH(profile->version),
1494 			 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1495 			 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1496 			 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1497 		return -EINVAL;
1498 	}
1499 	/* Verify if profile fits the HW */
1500 	if (fw->kpus > profile->kpus) {
1501 		dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1502 			 profile->kpus);
1503 		return -EINVAL;
1504 	}
1505 
1506 	profile->custom = 1;
1507 	profile->name = fw->name;
1508 	profile->version = le64_to_cpu(fw->version);
1509 	profile->mkex = &fw->mkex;
1510 	profile->lt_def = &fw->lt_def;
1511 
1512 	for (kpu = 0; kpu < fw->kpus; kpu++) {
1513 		fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1514 		if (fw_kpu->entries > KPU_MAX_CST_ENT)
1515 			dev_warn(rvu->dev,
1516 				 "Too many custom entries on KPU%d: %d > %d\n",
1517 				 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1518 		entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1519 		cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1520 		offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1521 		action = (struct npc_kpu_profile_action *)(fw->data + offset);
1522 		offset += fw_kpu->entries * sizeof(*action);
1523 		if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1524 			dev_warn(rvu->dev,
1525 				 "Profile size mismatch on KPU%i parsing.\n",
1526 				 kpu + 1);
1527 			return -EINVAL;
1528 		}
1529 		for (entry = 0; entry < entries; entry++) {
1530 			profile->kpu[kpu].cam[entry] = cam[entry];
1531 			profile->kpu[kpu].action[entry] = action[entry];
1532 		}
1533 	}
1534 
1535 	return 0;
1536 }
1537 
1538 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1539 				 u64 prfl_sz, const char *kpu_profile)
1540 {
1541 	struct npc_kpu_profile_fwdata *kpu_data = NULL;
1542 	int rc = -EINVAL;
1543 
1544 	kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1545 	if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1546 	    !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1547 		dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1548 			 kpu_profile);
1549 		rvu->kpu_fwdata = kpu_data;
1550 		rvu->kpu_fwdata_sz = prfl_sz;
1551 		rvu->kpu_prfl_addr = prfl_addr;
1552 		rc = 0;
1553 	}
1554 
1555 	return rc;
1556 }
1557 
1558 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1559 					 const char *kpu_profile)
1560 {
1561 	struct npc_coalesced_kpu_prfl *img_data = NULL;
1562 	int i = 0, rc = -EINVAL;
1563 	void __iomem *kpu_prfl_addr;
1564 	u16 offset;
1565 
1566 	img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1567 	if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1568 	    !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1569 		/* Loaded profile is a single KPU profile. */
1570 		rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1571 					   prfl_sz, kpu_profile);
1572 		goto done;
1573 	}
1574 
1575 	/* Loaded profile is coalesced image, offset of first KPU profile.*/
1576 	offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1577 		(img_data->num_prfl * sizeof(uint16_t));
1578 	/* Check if mapped image is coalesced image. */
1579 	while (i < img_data->num_prfl) {
1580 		/* Profile image offsets are rounded up to next 8 multiple.*/
1581 		offset = ALIGN_8B_CEIL(offset);
1582 		kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1583 					 offset);
1584 		rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1585 					   img_data->prfl_sz[i], kpu_profile);
1586 		if (!rc)
1587 			break;
1588 		/* Calculating offset of profile image based on profile size.*/
1589 		offset += img_data->prfl_sz[i];
1590 		i++;
1591 	}
1592 done:
1593 	return rc;
1594 }
1595 
1596 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1597 {
1598 	int ret = -EINVAL;
1599 	u64 prfl_sz;
1600 
1601 	/* Setting up the mapping for NPC profile image */
1602 	ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1603 	if (ret < 0)
1604 		goto done;
1605 
1606 	/* Detect if profile is coalesced or single KPU profile and load */
1607 	ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1608 	if (ret == 0)
1609 		goto done;
1610 
1611 	/* Cleaning up if KPU profile image from fwdata is not valid. */
1612 	if (rvu->kpu_prfl_addr) {
1613 		iounmap(rvu->kpu_prfl_addr);
1614 		rvu->kpu_prfl_addr = NULL;
1615 		rvu->kpu_fwdata_sz = 0;
1616 		rvu->kpu_fwdata = NULL;
1617 	}
1618 
1619 done:
1620 	return ret;
1621 }
1622 
1623 static void npc_load_kpu_profile(struct rvu *rvu)
1624 {
1625 	struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1626 	const char *kpu_profile = rvu->kpu_pfl_name;
1627 	const struct firmware *fw = NULL;
1628 	bool retry_fwdb = false;
1629 
1630 	/* If user not specified profile customization */
1631 	if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1632 		goto revert_to_default;
1633 	/* First prepare default KPU, then we'll customize top entries. */
1634 	npc_prepare_default_kpu(profile);
1635 
1636 	/* Order of preceedence for load loading NPC profile (high to low)
1637 	 * Firmware binary in filesystem.
1638 	 * Firmware database method.
1639 	 * Default KPU profile.
1640 	 */
1641 	if (!request_firmware(&fw, kpu_profile, rvu->dev)) {
1642 		dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1643 			 kpu_profile);
1644 		rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1645 		if (rvu->kpu_fwdata) {
1646 			memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1647 			rvu->kpu_fwdata_sz = fw->size;
1648 		}
1649 		release_firmware(fw);
1650 		retry_fwdb = true;
1651 		goto program_kpu;
1652 	}
1653 
1654 load_image_fwdb:
1655 	/* Loading the KPU profile using firmware database */
1656 	if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1657 		goto revert_to_default;
1658 
1659 program_kpu:
1660 	/* Apply profile customization if firmware was loaded. */
1661 	if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1662 		/* If image from firmware filesystem fails to load or invalid
1663 		 * retry with firmware database method.
1664 		 */
1665 		if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1666 			/* Loading image from firmware database failed. */
1667 			if (rvu->kpu_prfl_addr) {
1668 				iounmap(rvu->kpu_prfl_addr);
1669 				rvu->kpu_prfl_addr = NULL;
1670 			} else {
1671 				kfree(rvu->kpu_fwdata);
1672 			}
1673 			rvu->kpu_fwdata = NULL;
1674 			rvu->kpu_fwdata_sz = 0;
1675 			if (retry_fwdb) {
1676 				retry_fwdb = false;
1677 				goto load_image_fwdb;
1678 			}
1679 		}
1680 
1681 		dev_warn(rvu->dev,
1682 			 "Can't load KPU profile %s. Using default.\n",
1683 			 kpu_profile);
1684 		kfree(rvu->kpu_fwdata);
1685 		rvu->kpu_fwdata = NULL;
1686 		goto revert_to_default;
1687 	}
1688 
1689 	dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1690 		 profile->name, NPC_KPU_VER_MAJ(profile->version),
1691 		 NPC_KPU_VER_MIN(profile->version),
1692 		 NPC_KPU_VER_PATCH(profile->version));
1693 
1694 	return;
1695 
1696 revert_to_default:
1697 	npc_prepare_default_kpu(profile);
1698 }
1699 
1700 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1701 {
1702 	struct rvu_hwinfo *hw = rvu->hw;
1703 	int num_pkinds, num_kpus, idx;
1704 
1705 	/* Disable all KPUs and their entries */
1706 	for (idx = 0; idx < hw->npc_kpus; idx++) {
1707 		rvu_write64(rvu, blkaddr,
1708 			    NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1709 		rvu_write64(rvu, blkaddr,
1710 			    NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1711 		rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1712 	}
1713 
1714 	/* Load and customize KPU profile. */
1715 	npc_load_kpu_profile(rvu);
1716 
1717 	/* First program IKPU profile i.e PKIND configs.
1718 	 * Check HW max count to avoid configuring junk or
1719 	 * writing to unsupported CSR addresses.
1720 	 */
1721 	num_pkinds = rvu->kpu.pkinds;
1722 	num_pkinds = min_t(int, hw->npc_pkinds, num_pkinds);
1723 
1724 	for (idx = 0; idx < num_pkinds; idx++)
1725 		npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1726 
1727 	/* Program KPU CAM and Action profiles */
1728 	num_kpus = rvu->kpu.kpus;
1729 	num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1730 
1731 	for (idx = 0; idx < num_kpus; idx++)
1732 		npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1733 }
1734 
1735 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1736 {
1737 	int nixlf_count = rvu_get_nixlf_count(rvu);
1738 	struct npc_mcam *mcam = &rvu->hw->mcam;
1739 	int rsvd, err;
1740 	u16 index;
1741 	int cntr;
1742 	u64 cfg;
1743 
1744 	/* Actual number of MCAM entries vary by entry size */
1745 	cfg = (rvu_read64(rvu, blkaddr,
1746 			  NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1747 	mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1748 	mcam->keysize = cfg;
1749 
1750 	/* Number of banks combined per MCAM entry */
1751 	if (cfg == NPC_MCAM_KEY_X4)
1752 		mcam->banks_per_entry = 4;
1753 	else if (cfg == NPC_MCAM_KEY_X2)
1754 		mcam->banks_per_entry = 2;
1755 	else
1756 		mcam->banks_per_entry = 1;
1757 
1758 	/* Reserve one MCAM entry for each of the NIX LF to
1759 	 * guarantee space to install default matching DMAC rule.
1760 	 * Also reserve 2 MCAM entries for each PF for default
1761 	 * channel based matching or 'bcast & promisc' matching to
1762 	 * support BCAST and PROMISC modes of operation for PFs.
1763 	 * PF0 is excluded.
1764 	 */
1765 	rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1766 		((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1767 	if (mcam->total_entries <= rsvd) {
1768 		dev_warn(rvu->dev,
1769 			 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1770 			 mcam->total_entries);
1771 		return -ENOMEM;
1772 	}
1773 
1774 	mcam->bmap_entries = mcam->total_entries - rsvd;
1775 	mcam->nixlf_offset = mcam->bmap_entries;
1776 	mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1777 
1778 	/* Allocate bitmaps for managing MCAM entries */
1779 	mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1780 				  sizeof(long), GFP_KERNEL);
1781 	if (!mcam->bmap)
1782 		return -ENOMEM;
1783 
1784 	mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1785 					  BITS_TO_LONGS(mcam->bmap_entries),
1786 					  sizeof(long), GFP_KERNEL);
1787 	if (!mcam->bmap_reverse)
1788 		return -ENOMEM;
1789 
1790 	mcam->bmap_fcnt = mcam->bmap_entries;
1791 
1792 	/* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
1793 	mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1794 					    sizeof(u16), GFP_KERNEL);
1795 	if (!mcam->entry2pfvf_map)
1796 		return -ENOMEM;
1797 
1798 	/* Reserve 1/8th of MCAM entries at the bottom for low priority
1799 	 * allocations and another 1/8th at the top for high priority
1800 	 * allocations.
1801 	 */
1802 	mcam->lprio_count = mcam->bmap_entries / 8;
1803 	if (mcam->lprio_count > BITS_PER_LONG)
1804 		mcam->lprio_count = round_down(mcam->lprio_count,
1805 					       BITS_PER_LONG);
1806 	mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1807 	mcam->hprio_count = mcam->lprio_count;
1808 	mcam->hprio_end = mcam->hprio_count;
1809 
1810 
1811 	/* Allocate bitmap for managing MCAM counters and memory
1812 	 * for saving counter to RVU PFFUNC allocation mapping.
1813 	 */
1814 	err = rvu_alloc_bitmap(&mcam->counters);
1815 	if (err)
1816 		return err;
1817 
1818 	mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1819 					   sizeof(u16), GFP_KERNEL);
1820 	if (!mcam->cntr2pfvf_map)
1821 		goto free_mem;
1822 
1823 	/* Alloc memory for MCAM entry to counter mapping and for tracking
1824 	 * counter's reference count.
1825 	 */
1826 	mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1827 					    sizeof(u16), GFP_KERNEL);
1828 	if (!mcam->entry2cntr_map)
1829 		goto free_mem;
1830 
1831 	mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1832 					 sizeof(u16), GFP_KERNEL);
1833 	if (!mcam->cntr_refcnt)
1834 		goto free_mem;
1835 
1836 	/* Alloc memory for saving target device of mcam rule */
1837 	mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1838 						 sizeof(u16), GFP_KERNEL);
1839 	if (!mcam->entry2target_pffunc)
1840 		goto free_mem;
1841 
1842 	for (index = 0; index < mcam->bmap_entries; index++) {
1843 		mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
1844 		mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
1845 	}
1846 
1847 	for (cntr = 0; cntr < mcam->counters.max; cntr++)
1848 		mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
1849 
1850 	mutex_init(&mcam->lock);
1851 
1852 	return 0;
1853 
1854 free_mem:
1855 	kfree(mcam->counters.bmap);
1856 	return -ENOMEM;
1857 }
1858 
1859 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1860 {
1861 	struct npc_pkind *pkind = &rvu->hw->pkind;
1862 	struct npc_mcam *mcam = &rvu->hw->mcam;
1863 	struct rvu_hwinfo *hw = rvu->hw;
1864 	u64 npc_const, npc_const1;
1865 	u64 npc_const2 = 0;
1866 
1867 	npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1868 	npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1869 	if (npc_const1 & BIT_ULL(63))
1870 		npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1871 
1872 	pkind->rsrc.max = NPC_UNRESERVED_PKIND_COUNT;
1873 	hw->npc_pkinds = (npc_const1 >> 12) & 0xFFULL;
1874 	hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
1875 	hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
1876 	hw->npc_intfs = npc_const & 0xFULL;
1877 	hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
1878 
1879 	mcam->banks = (npc_const >> 44) & 0xFULL;
1880 	mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
1881 	hw->npc_stat_ena = BIT_ULL(9);
1882 	/* Extended set */
1883 	if (npc_const2) {
1884 		hw->npc_ext_set = true;
1885 		/* 96xx supports only match_stats and npc_counters
1886 		 * reflected in NPC_AF_CONST reg.
1887 		 * STAT_SEL and ENA are at [0:8] and 9 bit positions.
1888 		 * 98xx has both match_stat and ext and npc_counter
1889 		 * reflected in NPC_AF_CONST2
1890 		 * STAT_SEL_EXT added at [12:14] bit position.
1891 		 * cn10k supports only ext and hence npc_counters in
1892 		 * NPC_AF_CONST is 0 and npc_counters reflected in NPC_AF_CONST2.
1893 		 * STAT_SEL bitpos incremented from [0:8] to [0:11] and ENA bit moved to 63
1894 		 */
1895 		if (!hw->npc_counters)
1896 			hw->npc_stat_ena = BIT_ULL(63);
1897 		hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1898 		mcam->banksize = npc_const2 & 0xFFFFULL;
1899 	}
1900 
1901 	mcam->counters.max = hw->npc_counters;
1902 }
1903 
1904 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1905 {
1906 	struct npc_mcam *mcam = &rvu->hw->mcam;
1907 	struct rvu_hwinfo *hw = rvu->hw;
1908 	u64 nibble_ena, rx_kex, tx_kex;
1909 	u8 intf;
1910 
1911 	/* Reserve last counter for MCAM RX miss action which is set to
1912 	 * drop packet. This way we will know how many pkts didn't match
1913 	 * any MCAM entry.
1914 	 */
1915 	mcam->counters.max--;
1916 	mcam->rx_miss_act_cntr = mcam->counters.max;
1917 
1918 	rx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_RX];
1919 	tx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_TX];
1920 	nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1921 
1922 	nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1923 	if (nibble_ena) {
1924 		tx_kex &= ~NPC_PARSE_NIBBLE;
1925 		tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1926 		npc_mkex_default.keyx_cfg[NIX_INTF_TX] = tx_kex;
1927 	}
1928 
1929 	/* Configure RX interfaces */
1930 	for (intf = 0; intf < hw->npc_intfs; intf++) {
1931 		if (is_npc_intf_tx(intf))
1932 			continue;
1933 
1934 		/* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
1935 		rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1936 			    rx_kex);
1937 
1938 		/* If MCAM lookup doesn't result in a match, drop the received
1939 		 * packet. And map this action to a counter to count dropped
1940 		 * packets.
1941 		 */
1942 		rvu_write64(rvu, blkaddr,
1943 			    NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1944 
1945 		/* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
1946 		 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
1947 		 */
1948 		rvu_write64(rvu, blkaddr,
1949 			    NPC_AF_INTFX_MISS_STAT_ACT(intf),
1950 			    ((mcam->rx_miss_act_cntr >> 9) << 12) |
1951 			    hw->npc_stat_ena | mcam->rx_miss_act_cntr);
1952 	}
1953 
1954 	/* Configure TX interfaces */
1955 	for (intf = 0; intf < hw->npc_intfs; intf++) {
1956 		if (is_npc_intf_rx(intf))
1957 			continue;
1958 
1959 		/* Extract Ltypes LID_LA to LID_LE */
1960 		rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1961 			    tx_kex);
1962 
1963 		/* Set TX miss action to UCAST_DEFAULT i.e
1964 		 * transmit the packet on NIX LF SQ's default channel.
1965 		 */
1966 		rvu_write64(rvu, blkaddr,
1967 			    NPC_AF_INTFX_MISS_ACT(intf),
1968 			    NIX_TX_ACTIONOP_UCAST_DEFAULT);
1969 	}
1970 }
1971 
1972 int rvu_npc_init(struct rvu *rvu)
1973 {
1974 	struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
1975 	struct npc_pkind *pkind = &rvu->hw->pkind;
1976 	struct npc_mcam *mcam = &rvu->hw->mcam;
1977 	int blkaddr, entry, bank, err;
1978 
1979 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1980 	if (blkaddr < 0) {
1981 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1982 		return -ENODEV;
1983 	}
1984 
1985 	rvu_npc_hw_init(rvu, blkaddr);
1986 
1987 	/* First disable all MCAM entries, to stop traffic towards NIXLFs */
1988 	for (bank = 0; bank < mcam->banks; bank++) {
1989 		for (entry = 0; entry < mcam->banksize; entry++)
1990 			rvu_write64(rvu, blkaddr,
1991 				    NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
1992 	}
1993 
1994 	err = rvu_alloc_bitmap(&pkind->rsrc);
1995 	if (err)
1996 		return err;
1997 	/* Reserve PKIND#0 for LBKs. Power reset value of LBK_CH_PKIND is '0',
1998 	 * no need to configure PKIND for all LBKs separately.
1999 	 */
2000 	rvu_alloc_rsrc(&pkind->rsrc);
2001 
2002 	/* Allocate mem for pkind to PF and channel mapping info */
2003 	pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
2004 					 sizeof(u32), GFP_KERNEL);
2005 	if (!pkind->pfchan_map)
2006 		return -ENOMEM;
2007 
2008 	/* Configure KPU profile */
2009 	npc_parser_profile_init(rvu, blkaddr);
2010 
2011 	/* Config Outer L2, IPv4's NPC layer info */
2012 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
2013 		    (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
2014 		    kpu->lt_def->pck_ol2.ltype_mask);
2015 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
2016 		    (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
2017 		    kpu->lt_def->pck_oip4.ltype_mask);
2018 
2019 	/* Config Inner IPV4 NPC layer info */
2020 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2021 		    (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2022 		    kpu->lt_def->pck_iip4.ltype_mask);
2023 
2024 	/* Enable below for Rx pkts.
2025 	 * - Outer IPv4 header checksum validation.
2026 	 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2M].
2027 	 * - Inner IPv4 header checksum validation.
2028 	 * - Set non zero checksum error code value
2029 	 */
2030 	rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2031 		    rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2032 		    BIT_ULL(32) | BIT_ULL(24) | BIT_ULL(6) |
2033 		    BIT_ULL(2) | BIT_ULL(1));
2034 
2035 	rvu_npc_setup_interfaces(rvu, blkaddr);
2036 
2037 	/* Configure MKEX profile */
2038 	npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2039 
2040 	err = npc_mcam_rsrcs_init(rvu, blkaddr);
2041 	if (err)
2042 		return err;
2043 
2044 	err = npc_flow_steering_init(rvu, blkaddr);
2045 	if (err) {
2046 		dev_err(rvu->dev,
2047 			"Incorrect mkex profile loaded using default mkex\n");
2048 		npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2049 	}
2050 
2051 	return 0;
2052 }
2053 
2054 void rvu_npc_freemem(struct rvu *rvu)
2055 {
2056 	struct npc_pkind *pkind = &rvu->hw->pkind;
2057 	struct npc_mcam *mcam = &rvu->hw->mcam;
2058 
2059 	kfree(pkind->rsrc.bmap);
2060 	kfree(mcam->counters.bmap);
2061 	if (rvu->kpu_prfl_addr)
2062 		iounmap(rvu->kpu_prfl_addr);
2063 	else
2064 		kfree(rvu->kpu_fwdata);
2065 	mutex_destroy(&mcam->lock);
2066 }
2067 
2068 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2069 				       int blkaddr, int *alloc_cnt,
2070 				       int *enable_cnt)
2071 {
2072 	struct npc_mcam *mcam = &rvu->hw->mcam;
2073 	int entry;
2074 
2075 	*alloc_cnt = 0;
2076 	*enable_cnt = 0;
2077 
2078 	for (entry = 0; entry < mcam->bmap_entries; entry++) {
2079 		if (mcam->entry2pfvf_map[entry] == pcifunc) {
2080 			(*alloc_cnt)++;
2081 			if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2082 				(*enable_cnt)++;
2083 		}
2084 	}
2085 }
2086 
2087 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2088 					 int blkaddr, int *alloc_cnt,
2089 					 int *enable_cnt)
2090 {
2091 	struct npc_mcam *mcam = &rvu->hw->mcam;
2092 	int cntr;
2093 
2094 	*alloc_cnt = 0;
2095 	*enable_cnt = 0;
2096 
2097 	for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2098 		if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2099 			(*alloc_cnt)++;
2100 			if (mcam->cntr_refcnt[cntr])
2101 				(*enable_cnt)++;
2102 		}
2103 	}
2104 }
2105 
2106 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
2107 				 u16 pcifunc, int entry)
2108 {
2109 	/* verify AF installed entries */
2110 	if (is_pffunc_af(pcifunc))
2111 		return 0;
2112 	/* Verify if entry is valid and if it is indeed
2113 	 * allocated to the requesting PFFUNC.
2114 	 */
2115 	if (entry >= mcam->bmap_entries)
2116 		return NPC_MCAM_INVALID_REQ;
2117 
2118 	if (pcifunc != mcam->entry2pfvf_map[entry])
2119 		return NPC_MCAM_PERM_DENIED;
2120 
2121 	return 0;
2122 }
2123 
2124 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2125 				   u16 pcifunc, int cntr)
2126 {
2127 	/* Verify if counter is valid and if it is indeed
2128 	 * allocated to the requesting PFFUNC.
2129 	 */
2130 	if (cntr >= mcam->counters.max)
2131 		return NPC_MCAM_INVALID_REQ;
2132 
2133 	if (pcifunc != mcam->cntr2pfvf_map[cntr])
2134 		return NPC_MCAM_PERM_DENIED;
2135 
2136 	return 0;
2137 }
2138 
2139 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2140 					int blkaddr, u16 entry, u16 cntr)
2141 {
2142 	u16 index = entry & (mcam->banksize - 1);
2143 	u32 bank = npc_get_bank(mcam, entry);
2144 	struct rvu_hwinfo *hw = rvu->hw;
2145 
2146 	/* Set mapping and increment counter's refcnt */
2147 	mcam->entry2cntr_map[entry] = cntr;
2148 	mcam->cntr_refcnt[cntr]++;
2149 	/* Enable stats */
2150 	rvu_write64(rvu, blkaddr,
2151 		    NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2152 		    ((cntr >> 9) << 12) | hw->npc_stat_ena | cntr);
2153 }
2154 
2155 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2156 					  struct npc_mcam *mcam,
2157 					  int blkaddr, u16 entry, u16 cntr)
2158 {
2159 	u16 index = entry & (mcam->banksize - 1);
2160 	u16 bank = npc_get_bank(mcam, entry);
2161 
2162 	/* Remove mapping and reduce counter's refcnt */
2163 	mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2164 	mcam->cntr_refcnt[cntr]--;
2165 	/* Disable stats */
2166 	rvu_write64(rvu, blkaddr,
2167 		    NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2168 }
2169 
2170 /* Sets MCAM entry in bitmap as used. Update
2171  * reverse bitmap too. Should be called with
2172  * 'mcam->lock' held.
2173  */
2174 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2175 {
2176 	u16 entry, rentry;
2177 
2178 	entry = index;
2179 	rentry = mcam->bmap_entries - index - 1;
2180 
2181 	__set_bit(entry, mcam->bmap);
2182 	__set_bit(rentry, mcam->bmap_reverse);
2183 	mcam->bmap_fcnt--;
2184 }
2185 
2186 /* Sets MCAM entry in bitmap as free. Update
2187  * reverse bitmap too. Should be called with
2188  * 'mcam->lock' held.
2189  */
2190 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2191 {
2192 	u16 entry, rentry;
2193 
2194 	entry = index;
2195 	rentry = mcam->bmap_entries - index - 1;
2196 
2197 	__clear_bit(entry, mcam->bmap);
2198 	__clear_bit(rentry, mcam->bmap_reverse);
2199 	mcam->bmap_fcnt++;
2200 }
2201 
2202 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2203 				      int blkaddr, u16 pcifunc)
2204 {
2205 	u16 index, cntr;
2206 
2207 	/* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2208 	for (index = 0; index < mcam->bmap_entries; index++) {
2209 		if (mcam->entry2pfvf_map[index] == pcifunc) {
2210 			mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2211 			/* Free the entry in bitmap */
2212 			npc_mcam_clear_bit(mcam, index);
2213 			/* Disable the entry */
2214 			npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2215 
2216 			/* Update entry2counter mapping */
2217 			cntr = mcam->entry2cntr_map[index];
2218 			if (cntr != NPC_MCAM_INVALID_MAP)
2219 				npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2220 							      blkaddr, index,
2221 							      cntr);
2222 			mcam->entry2target_pffunc[index] = 0x0;
2223 		}
2224 	}
2225 }
2226 
2227 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2228 				       u16 pcifunc)
2229 {
2230 	u16 cntr;
2231 
2232 	/* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2233 	for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2234 		if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2235 			mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2236 			mcam->cntr_refcnt[cntr] = 0;
2237 			rvu_free_rsrc(&mcam->counters, cntr);
2238 			/* This API is expected to be called after freeing
2239 			 * MCAM entries, which inturn will remove
2240 			 * 'entry to counter' mapping.
2241 			 * No need to do it again.
2242 			 */
2243 		}
2244 	}
2245 }
2246 
2247 /* Find area of contiguous free entries of size 'nr'.
2248  * If not found return max contiguous free entries available.
2249  */
2250 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2251 				   u16 nr, u16 *max_area)
2252 {
2253 	u16 max_area_start = 0;
2254 	u16 index, next, end;
2255 
2256 	*max_area = 0;
2257 
2258 again:
2259 	index = find_next_zero_bit(map, size, start);
2260 	if (index >= size)
2261 		return max_area_start;
2262 
2263 	end = ((index + nr) >= size) ? size : index + nr;
2264 	next = find_next_bit(map, end, index);
2265 	if (*max_area < (next - index)) {
2266 		*max_area = next - index;
2267 		max_area_start = index;
2268 	}
2269 
2270 	if (next < end) {
2271 		start = next + 1;
2272 		goto again;
2273 	}
2274 
2275 	return max_area_start;
2276 }
2277 
2278 /* Find number of free MCAM entries available
2279  * within range i.e in between 'start' and 'end'.
2280  */
2281 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2282 {
2283 	u16 index, next;
2284 	u16 fcnt = 0;
2285 
2286 again:
2287 	if (start >= end)
2288 		return fcnt;
2289 
2290 	index = find_next_zero_bit(map, end, start);
2291 	if (index >= end)
2292 		return fcnt;
2293 
2294 	next = find_next_bit(map, end, index);
2295 	if (next <= end) {
2296 		fcnt += next - index;
2297 		start = next + 1;
2298 		goto again;
2299 	}
2300 
2301 	fcnt += end - index;
2302 	return fcnt;
2303 }
2304 
2305 static void
2306 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2307 				   struct npc_mcam_alloc_entry_req *req,
2308 				   u16 *start, u16 *end, bool *reverse)
2309 {
2310 	u16 fcnt;
2311 
2312 	if (req->priority == NPC_MCAM_HIGHER_PRIO)
2313 		goto hprio;
2314 
2315 	/* For a low priority entry allocation
2316 	 * - If reference entry is not in hprio zone then
2317 	 *      search range: ref_entry to end.
2318 	 * - If reference entry is in hprio zone and if
2319 	 *   request can be accomodated in non-hprio zone then
2320 	 *      search range: 'start of middle zone' to 'end'
2321 	 * - else search in reverse, so that less number of hprio
2322 	 *   zone entries are allocated.
2323 	 */
2324 
2325 	*reverse = false;
2326 	*start = req->ref_entry + 1;
2327 	*end = mcam->bmap_entries;
2328 
2329 	if (req->ref_entry >= mcam->hprio_end)
2330 		return;
2331 
2332 	fcnt = npc_mcam_get_free_count(mcam->bmap,
2333 				       mcam->hprio_end, mcam->bmap_entries);
2334 	if (fcnt > req->count)
2335 		*start = mcam->hprio_end;
2336 	else
2337 		*reverse = true;
2338 	return;
2339 
2340 hprio:
2341 	/* For a high priority entry allocation, search is always
2342 	 * in reverse to preserve hprio zone entries.
2343 	 * - If reference entry is not in lprio zone then
2344 	 *      search range: 0 to ref_entry.
2345 	 * - If reference entry is in lprio zone and if
2346 	 *   request can be accomodated in middle zone then
2347 	 *      search range: 'hprio_end' to 'lprio_start'
2348 	 */
2349 
2350 	*reverse = true;
2351 	*start = 0;
2352 	*end = req->ref_entry;
2353 
2354 	if (req->ref_entry <= mcam->lprio_start)
2355 		return;
2356 
2357 	fcnt = npc_mcam_get_free_count(mcam->bmap,
2358 				       mcam->hprio_end, mcam->lprio_start);
2359 	if (fcnt < req->count)
2360 		return;
2361 	*start = mcam->hprio_end;
2362 	*end = mcam->lprio_start;
2363 }
2364 
2365 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2366 				  struct npc_mcam_alloc_entry_req *req,
2367 				  struct npc_mcam_alloc_entry_rsp *rsp)
2368 {
2369 	u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2370 	u16 fcnt, hp_fcnt, lp_fcnt;
2371 	u16 start, end, index;
2372 	int entry, next_start;
2373 	bool reverse = false;
2374 	unsigned long *bmap;
2375 	u16 max_contig;
2376 
2377 	mutex_lock(&mcam->lock);
2378 
2379 	/* Check if there are any free entries */
2380 	if (!mcam->bmap_fcnt) {
2381 		mutex_unlock(&mcam->lock);
2382 		return NPC_MCAM_ALLOC_FAILED;
2383 	}
2384 
2385 	/* MCAM entries are divided into high priority, middle and
2386 	 * low priority zones. Idea is to not allocate top and lower
2387 	 * most entries as much as possible, this is to increase
2388 	 * probability of honouring priority allocation requests.
2389 	 *
2390 	 * Two bitmaps are used for mcam entry management,
2391 	 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
2392 	 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
2393 	 *
2394 	 * Reverse bitmap is used to allocate entries
2395 	 * - when a higher priority entry is requested
2396 	 * - when available free entries are less.
2397 	 * Lower priority ones out of avaialble free entries are always
2398 	 * chosen when 'high vs low' question arises.
2399 	 */
2400 
2401 	/* Get the search range for priority allocation request */
2402 	if (req->priority) {
2403 		npc_get_mcam_search_range_priority(mcam, req,
2404 						   &start, &end, &reverse);
2405 		goto alloc;
2406 	}
2407 
2408 	/* For a VF base MCAM match rule is set by its PF. And all the
2409 	 * further MCAM rules installed by VF on its own are
2410 	 * concatenated with the base rule set by its PF. Hence PF entries
2411 	 * should be at lower priority compared to VF entries. Otherwise
2412 	 * base rule is hit always and rules installed by VF will be of
2413 	 * no use. Hence if the request is from PF and NOT a priority
2414 	 * allocation request then allocate low priority entries.
2415 	 */
2416 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
2417 		goto lprio_alloc;
2418 
2419 	/* Find out the search range for non-priority allocation request
2420 	 *
2421 	 * Get MCAM free entry count in middle zone.
2422 	 */
2423 	lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2424 					  mcam->lprio_start,
2425 					  mcam->bmap_entries);
2426 	hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2427 	fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2428 
2429 	/* Check if request can be accomodated in the middle zone */
2430 	if (fcnt > req->count) {
2431 		start = mcam->hprio_end;
2432 		end = mcam->lprio_start;
2433 	} else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2434 		/* Expand search zone from half of hprio zone to
2435 		 * half of lprio zone.
2436 		 */
2437 		start = mcam->hprio_end / 2;
2438 		end = mcam->bmap_entries - (mcam->lprio_count / 2);
2439 		reverse = true;
2440 	} else {
2441 		/* Not enough free entries, search all entries in reverse,
2442 		 * so that low priority ones will get used up.
2443 		 */
2444 lprio_alloc:
2445 		reverse = true;
2446 		start = 0;
2447 		end = mcam->bmap_entries;
2448 	}
2449 
2450 alloc:
2451 	if (reverse) {
2452 		bmap = mcam->bmap_reverse;
2453 		start = mcam->bmap_entries - start;
2454 		end = mcam->bmap_entries - end;
2455 		index = start;
2456 		start = end;
2457 		end = index;
2458 	} else {
2459 		bmap = mcam->bmap;
2460 	}
2461 
2462 	if (req->contig) {
2463 		/* Allocate requested number of contiguous entries, if
2464 		 * unsuccessful find max contiguous entries available.
2465 		 */
2466 		index = npc_mcam_find_zero_area(bmap, end, start,
2467 						req->count, &max_contig);
2468 		rsp->count = max_contig;
2469 		if (reverse)
2470 			rsp->entry = mcam->bmap_entries - index - max_contig;
2471 		else
2472 			rsp->entry = index;
2473 	} else {
2474 		/* Allocate requested number of non-contiguous entries,
2475 		 * if unsuccessful allocate as many as possible.
2476 		 */
2477 		rsp->count = 0;
2478 		next_start = start;
2479 		for (entry = 0; entry < req->count; entry++) {
2480 			index = find_next_zero_bit(bmap, end, next_start);
2481 			if (index >= end)
2482 				break;
2483 
2484 			next_start = start + (index - start) + 1;
2485 
2486 			/* Save the entry's index */
2487 			if (reverse)
2488 				index = mcam->bmap_entries - index - 1;
2489 			entry_list[entry] = index;
2490 			rsp->count++;
2491 		}
2492 	}
2493 
2494 	/* If allocating requested no of entries is unsucessful,
2495 	 * expand the search range to full bitmap length and retry.
2496 	 */
2497 	if (!req->priority && (rsp->count < req->count) &&
2498 	    ((end - start) != mcam->bmap_entries)) {
2499 		reverse = true;
2500 		start = 0;
2501 		end = mcam->bmap_entries;
2502 		goto alloc;
2503 	}
2504 
2505 	/* For priority entry allocation requests, if allocation is
2506 	 * failed then expand search to max possible range and retry.
2507 	 */
2508 	if (req->priority && rsp->count < req->count) {
2509 		if (req->priority == NPC_MCAM_LOWER_PRIO &&
2510 		    (start != (req->ref_entry + 1))) {
2511 			start = req->ref_entry + 1;
2512 			end = mcam->bmap_entries;
2513 			reverse = false;
2514 			goto alloc;
2515 		} else if ((req->priority == NPC_MCAM_HIGHER_PRIO) &&
2516 			   ((end - start) != req->ref_entry)) {
2517 			start = 0;
2518 			end = req->ref_entry;
2519 			reverse = true;
2520 			goto alloc;
2521 		}
2522 	}
2523 
2524 	/* Copy MCAM entry indices into mbox response entry_list.
2525 	 * Requester always expects indices in ascending order, so
2526 	 * so reverse the list if reverse bitmap is used for allocation.
2527 	 */
2528 	if (!req->contig && rsp->count) {
2529 		index = 0;
2530 		for (entry = rsp->count - 1; entry >= 0; entry--) {
2531 			if (reverse)
2532 				rsp->entry_list[index++] = entry_list[entry];
2533 			else
2534 				rsp->entry_list[entry] = entry_list[entry];
2535 		}
2536 	}
2537 
2538 	/* Mark the allocated entries as used and set nixlf mapping */
2539 	for (entry = 0; entry < rsp->count; entry++) {
2540 		index = req->contig ?
2541 			(rsp->entry + entry) : rsp->entry_list[entry];
2542 		npc_mcam_set_bit(mcam, index);
2543 		mcam->entry2pfvf_map[index] = pcifunc;
2544 		mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2545 	}
2546 
2547 	/* Update available free count in mbox response */
2548 	rsp->free_count = mcam->bmap_fcnt;
2549 
2550 	mutex_unlock(&mcam->lock);
2551 	return 0;
2552 }
2553 
2554 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
2555 					  struct npc_mcam_alloc_entry_req *req,
2556 					  struct npc_mcam_alloc_entry_rsp *rsp)
2557 {
2558 	struct npc_mcam *mcam = &rvu->hw->mcam;
2559 	u16 pcifunc = req->hdr.pcifunc;
2560 	int blkaddr;
2561 
2562 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2563 	if (blkaddr < 0)
2564 		return NPC_MCAM_INVALID_REQ;
2565 
2566 	rsp->entry = NPC_MCAM_ENTRY_INVALID;
2567 	rsp->free_count = 0;
2568 
2569 	/* Check if ref_entry is within range */
2570 	if (req->priority && req->ref_entry >= mcam->bmap_entries) {
2571 		dev_err(rvu->dev, "%s: reference entry %d is out of range\n",
2572 			__func__, req->ref_entry);
2573 		return NPC_MCAM_INVALID_REQ;
2574 	}
2575 
2576 	/* ref_entry can't be '0' if requested priority is high.
2577 	 * Can't be last entry if requested priority is low.
2578 	 */
2579 	if ((!req->ref_entry && req->priority == NPC_MCAM_HIGHER_PRIO) ||
2580 	    ((req->ref_entry == (mcam->bmap_entries - 1)) &&
2581 	     req->priority == NPC_MCAM_LOWER_PRIO))
2582 		return NPC_MCAM_INVALID_REQ;
2583 
2584 	/* Since list of allocated indices needs to be sent to requester,
2585 	 * max number of non-contiguous entries per mbox msg is limited.
2586 	 */
2587 	if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES) {
2588 		dev_err(rvu->dev,
2589 			"%s: %d Non-contiguous MCAM entries requested is more than max (%d) allowed\n",
2590 			__func__, req->count, NPC_MAX_NONCONTIG_ENTRIES);
2591 		return NPC_MCAM_INVALID_REQ;
2592 	}
2593 
2594 	/* Alloc request from PFFUNC with no NIXLF attached should be denied */
2595 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2596 		return NPC_MCAM_ALLOC_DENIED;
2597 
2598 	return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
2599 }
2600 
2601 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
2602 					 struct npc_mcam_free_entry_req *req,
2603 					 struct msg_rsp *rsp)
2604 {
2605 	struct npc_mcam *mcam = &rvu->hw->mcam;
2606 	u16 pcifunc = req->hdr.pcifunc;
2607 	int blkaddr, rc = 0;
2608 	u16 cntr;
2609 
2610 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2611 	if (blkaddr < 0)
2612 		return NPC_MCAM_INVALID_REQ;
2613 
2614 	/* Free request from PFFUNC with no NIXLF attached, ignore */
2615 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2616 		return NPC_MCAM_INVALID_REQ;
2617 
2618 	mutex_lock(&mcam->lock);
2619 
2620 	if (req->all)
2621 		goto free_all;
2622 
2623 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2624 	if (rc)
2625 		goto exit;
2626 
2627 	mcam->entry2pfvf_map[req->entry] = NPC_MCAM_INVALID_MAP;
2628 	mcam->entry2target_pffunc[req->entry] = 0x0;
2629 	npc_mcam_clear_bit(mcam, req->entry);
2630 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2631 
2632 	/* Update entry2counter mapping */
2633 	cntr = mcam->entry2cntr_map[req->entry];
2634 	if (cntr != NPC_MCAM_INVALID_MAP)
2635 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2636 					      req->entry, cntr);
2637 
2638 	goto exit;
2639 
2640 free_all:
2641 	/* Free up all entries allocated to requesting PFFUNC */
2642 	npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
2643 exit:
2644 	mutex_unlock(&mcam->lock);
2645 	return rc;
2646 }
2647 
2648 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
2649 					 struct npc_mcam_read_entry_req *req,
2650 					 struct npc_mcam_read_entry_rsp *rsp)
2651 {
2652 	struct npc_mcam *mcam = &rvu->hw->mcam;
2653 	u16 pcifunc = req->hdr.pcifunc;
2654 	int blkaddr, rc;
2655 
2656 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2657 	if (blkaddr < 0)
2658 		return NPC_MCAM_INVALID_REQ;
2659 
2660 	mutex_lock(&mcam->lock);
2661 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2662 	if (!rc) {
2663 		npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
2664 				    &rsp->entry_data,
2665 				    &rsp->intf, &rsp->enable);
2666 	}
2667 
2668 	mutex_unlock(&mcam->lock);
2669 	return rc;
2670 }
2671 
2672 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
2673 					  struct npc_mcam_write_entry_req *req,
2674 					  struct msg_rsp *rsp)
2675 {
2676 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2677 	struct npc_mcam *mcam = &rvu->hw->mcam;
2678 	u16 pcifunc = req->hdr.pcifunc;
2679 	int blkaddr, rc;
2680 	u8 nix_intf;
2681 
2682 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2683 	if (blkaddr < 0)
2684 		return NPC_MCAM_INVALID_REQ;
2685 
2686 	mutex_lock(&mcam->lock);
2687 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2688 	if (rc)
2689 		goto exit;
2690 
2691 	if (req->set_cntr &&
2692 	    npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2693 		rc = NPC_MCAM_INVALID_REQ;
2694 		goto exit;
2695 	}
2696 
2697 	if (!is_npc_interface_valid(rvu, req->intf)) {
2698 		rc = NPC_MCAM_INVALID_REQ;
2699 		goto exit;
2700 	}
2701 
2702 	if (is_npc_intf_tx(req->intf))
2703 		nix_intf = pfvf->nix_tx_intf;
2704 	else
2705 		nix_intf = pfvf->nix_rx_intf;
2706 
2707 	if (!is_pffunc_af(pcifunc) &&
2708 	    npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf, pcifunc)) {
2709 		rc = NPC_MCAM_INVALID_REQ;
2710 		goto exit;
2711 	}
2712 
2713 	/* For AF installed rules, the nix_intf should be set to target NIX */
2714 	if (is_pffunc_af(req->hdr.pcifunc))
2715 		nix_intf = req->intf;
2716 
2717 	npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2718 			      &req->entry_data, req->enable_entry);
2719 
2720 	if (req->set_cntr)
2721 		npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2722 					    req->entry, req->cntr);
2723 
2724 	rc = 0;
2725 exit:
2726 	mutex_unlock(&mcam->lock);
2727 	return rc;
2728 }
2729 
2730 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2731 					struct npc_mcam_ena_dis_entry_req *req,
2732 					struct msg_rsp *rsp)
2733 {
2734 	struct npc_mcam *mcam = &rvu->hw->mcam;
2735 	u16 pcifunc = req->hdr.pcifunc;
2736 	int blkaddr, rc;
2737 
2738 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2739 	if (blkaddr < 0)
2740 		return NPC_MCAM_INVALID_REQ;
2741 
2742 	mutex_lock(&mcam->lock);
2743 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2744 	mutex_unlock(&mcam->lock);
2745 	if (rc)
2746 		return rc;
2747 
2748 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2749 
2750 	return 0;
2751 }
2752 
2753 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2754 					struct npc_mcam_ena_dis_entry_req *req,
2755 					struct msg_rsp *rsp)
2756 {
2757 	struct npc_mcam *mcam = &rvu->hw->mcam;
2758 	u16 pcifunc = req->hdr.pcifunc;
2759 	int blkaddr, rc;
2760 
2761 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2762 	if (blkaddr < 0)
2763 		return NPC_MCAM_INVALID_REQ;
2764 
2765 	mutex_lock(&mcam->lock);
2766 	rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2767 	mutex_unlock(&mcam->lock);
2768 	if (rc)
2769 		return rc;
2770 
2771 	npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2772 
2773 	return 0;
2774 }
2775 
2776 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2777 					  struct npc_mcam_shift_entry_req *req,
2778 					  struct npc_mcam_shift_entry_rsp *rsp)
2779 {
2780 	struct npc_mcam *mcam = &rvu->hw->mcam;
2781 	u16 pcifunc = req->hdr.pcifunc;
2782 	u16 old_entry, new_entry;
2783 	u16 index, cntr;
2784 	int blkaddr, rc;
2785 
2786 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2787 	if (blkaddr < 0)
2788 		return NPC_MCAM_INVALID_REQ;
2789 
2790 	if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2791 		return NPC_MCAM_INVALID_REQ;
2792 
2793 	mutex_lock(&mcam->lock);
2794 	for (index = 0; index < req->shift_count; index++) {
2795 		old_entry = req->curr_entry[index];
2796 		new_entry = req->new_entry[index];
2797 
2798 		/* Check if both old and new entries are valid and
2799 		 * does belong to this PFFUNC or not.
2800 		 */
2801 		rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2802 		if (rc)
2803 			break;
2804 
2805 		rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2806 		if (rc)
2807 			break;
2808 
2809 		/* new_entry should not have a counter mapped */
2810 		if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2811 			rc = NPC_MCAM_PERM_DENIED;
2812 			break;
2813 		}
2814 
2815 		/* Disable the new_entry */
2816 		npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2817 
2818 		/* Copy rule from old entry to new entry */
2819 		npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2820 
2821 		/* Copy counter mapping, if any */
2822 		cntr = mcam->entry2cntr_map[old_entry];
2823 		if (cntr != NPC_MCAM_INVALID_MAP) {
2824 			npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2825 						      old_entry, cntr);
2826 			npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2827 						    new_entry, cntr);
2828 		}
2829 
2830 		/* Enable new_entry and disable old_entry */
2831 		npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2832 		npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2833 	}
2834 
2835 	/* If shift has failed then report the failed index */
2836 	if (index != req->shift_count) {
2837 		rc = NPC_MCAM_PERM_DENIED;
2838 		rsp->failed_entry_idx = index;
2839 	}
2840 
2841 	mutex_unlock(&mcam->lock);
2842 	return rc;
2843 }
2844 
2845 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2846 			struct npc_mcam_alloc_counter_req *req,
2847 			struct npc_mcam_alloc_counter_rsp *rsp)
2848 {
2849 	struct npc_mcam *mcam = &rvu->hw->mcam;
2850 	u16 pcifunc = req->hdr.pcifunc;
2851 	u16 max_contig, cntr;
2852 	int blkaddr, index;
2853 
2854 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2855 	if (blkaddr < 0)
2856 		return NPC_MCAM_INVALID_REQ;
2857 
2858 	/* If the request is from a PFFUNC with no NIXLF attached, ignore */
2859 	if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2860 		return NPC_MCAM_INVALID_REQ;
2861 
2862 	/* Since list of allocated counter IDs needs to be sent to requester,
2863 	 * max number of non-contiguous counters per mbox msg is limited.
2864 	 */
2865 	if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2866 		return NPC_MCAM_INVALID_REQ;
2867 
2868 	mutex_lock(&mcam->lock);
2869 
2870 	/* Check if unused counters are available or not */
2871 	if (!rvu_rsrc_free_count(&mcam->counters)) {
2872 		mutex_unlock(&mcam->lock);
2873 		return NPC_MCAM_ALLOC_FAILED;
2874 	}
2875 
2876 	rsp->count = 0;
2877 
2878 	if (req->contig) {
2879 		/* Allocate requested number of contiguous counters, if
2880 		 * unsuccessful find max contiguous entries available.
2881 		 */
2882 		index = npc_mcam_find_zero_area(mcam->counters.bmap,
2883 						mcam->counters.max, 0,
2884 						req->count, &max_contig);
2885 		rsp->count = max_contig;
2886 		rsp->cntr = index;
2887 		for (cntr = index; cntr < (index + max_contig); cntr++) {
2888 			__set_bit(cntr, mcam->counters.bmap);
2889 			mcam->cntr2pfvf_map[cntr] = pcifunc;
2890 		}
2891 	} else {
2892 		/* Allocate requested number of non-contiguous counters,
2893 		 * if unsuccessful allocate as many as possible.
2894 		 */
2895 		for (cntr = 0; cntr < req->count; cntr++) {
2896 			index = rvu_alloc_rsrc(&mcam->counters);
2897 			if (index < 0)
2898 				break;
2899 			rsp->cntr_list[cntr] = index;
2900 			rsp->count++;
2901 			mcam->cntr2pfvf_map[index] = pcifunc;
2902 		}
2903 	}
2904 
2905 	mutex_unlock(&mcam->lock);
2906 	return 0;
2907 }
2908 
2909 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2910 		struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2911 {
2912 	struct npc_mcam *mcam = &rvu->hw->mcam;
2913 	u16 index, entry = 0;
2914 	int blkaddr, err;
2915 
2916 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2917 	if (blkaddr < 0)
2918 		return NPC_MCAM_INVALID_REQ;
2919 
2920 	mutex_lock(&mcam->lock);
2921 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2922 	if (err) {
2923 		mutex_unlock(&mcam->lock);
2924 		return err;
2925 	}
2926 
2927 	/* Mark counter as free/unused */
2928 	mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2929 	rvu_free_rsrc(&mcam->counters, req->cntr);
2930 
2931 	/* Disable all MCAM entry's stats which are using this counter */
2932 	while (entry < mcam->bmap_entries) {
2933 		if (!mcam->cntr_refcnt[req->cntr])
2934 			break;
2935 
2936 		index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2937 		if (index >= mcam->bmap_entries)
2938 			break;
2939 		entry = index + 1;
2940 		if (mcam->entry2cntr_map[index] != req->cntr)
2941 			continue;
2942 
2943 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2944 					      index, req->cntr);
2945 	}
2946 
2947 	mutex_unlock(&mcam->lock);
2948 	return 0;
2949 }
2950 
2951 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
2952 		struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
2953 {
2954 	struct npc_mcam *mcam = &rvu->hw->mcam;
2955 	u16 index, entry = 0;
2956 	int blkaddr, rc;
2957 
2958 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2959 	if (blkaddr < 0)
2960 		return NPC_MCAM_INVALID_REQ;
2961 
2962 	mutex_lock(&mcam->lock);
2963 	rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2964 	if (rc)
2965 		goto exit;
2966 
2967 	/* Unmap the MCAM entry and counter */
2968 	if (!req->all) {
2969 		rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
2970 		if (rc)
2971 			goto exit;
2972 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2973 					      req->entry, req->cntr);
2974 		goto exit;
2975 	}
2976 
2977 	/* Disable all MCAM entry's stats which are using this counter */
2978 	while (entry < mcam->bmap_entries) {
2979 		if (!mcam->cntr_refcnt[req->cntr])
2980 			break;
2981 
2982 		index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2983 		if (index >= mcam->bmap_entries)
2984 			break;
2985 		if (mcam->entry2cntr_map[index] != req->cntr)
2986 			continue;
2987 
2988 		entry = index + 1;
2989 		npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2990 					      index, req->cntr);
2991 	}
2992 exit:
2993 	mutex_unlock(&mcam->lock);
2994 	return rc;
2995 }
2996 
2997 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
2998 		struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2999 {
3000 	struct npc_mcam *mcam = &rvu->hw->mcam;
3001 	int blkaddr, err;
3002 
3003 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3004 	if (blkaddr < 0)
3005 		return NPC_MCAM_INVALID_REQ;
3006 
3007 	mutex_lock(&mcam->lock);
3008 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3009 	mutex_unlock(&mcam->lock);
3010 	if (err)
3011 		return err;
3012 
3013 	rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
3014 
3015 	return 0;
3016 }
3017 
3018 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
3019 			struct npc_mcam_oper_counter_req *req,
3020 			struct npc_mcam_oper_counter_rsp *rsp)
3021 {
3022 	struct npc_mcam *mcam = &rvu->hw->mcam;
3023 	int blkaddr, err;
3024 
3025 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3026 	if (blkaddr < 0)
3027 		return NPC_MCAM_INVALID_REQ;
3028 
3029 	mutex_lock(&mcam->lock);
3030 	err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3031 	mutex_unlock(&mcam->lock);
3032 	if (err)
3033 		return err;
3034 
3035 	rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
3036 	rsp->stat &= BIT_ULL(48) - 1;
3037 
3038 	return 0;
3039 }
3040 
3041 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
3042 			  struct npc_mcam_alloc_and_write_entry_req *req,
3043 			  struct npc_mcam_alloc_and_write_entry_rsp *rsp)
3044 {
3045 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3046 	struct npc_mcam_alloc_counter_req cntr_req;
3047 	struct npc_mcam_alloc_counter_rsp cntr_rsp;
3048 	struct npc_mcam_alloc_entry_req entry_req;
3049 	struct npc_mcam_alloc_entry_rsp entry_rsp;
3050 	struct npc_mcam *mcam = &rvu->hw->mcam;
3051 	u16 entry = NPC_MCAM_ENTRY_INVALID;
3052 	u16 cntr = NPC_MCAM_ENTRY_INVALID;
3053 	int blkaddr, rc;
3054 	u8 nix_intf;
3055 
3056 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3057 	if (blkaddr < 0)
3058 		return NPC_MCAM_INVALID_REQ;
3059 
3060 	if (!is_npc_interface_valid(rvu, req->intf))
3061 		return NPC_MCAM_INVALID_REQ;
3062 
3063 	if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
3064 				    req->hdr.pcifunc))
3065 		return NPC_MCAM_INVALID_REQ;
3066 
3067 	/* Try to allocate a MCAM entry */
3068 	entry_req.hdr.pcifunc = req->hdr.pcifunc;
3069 	entry_req.contig = true;
3070 	entry_req.priority = req->priority;
3071 	entry_req.ref_entry = req->ref_entry;
3072 	entry_req.count = 1;
3073 
3074 	rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
3075 						   &entry_req, &entry_rsp);
3076 	if (rc)
3077 		return rc;
3078 
3079 	if (!entry_rsp.count)
3080 		return NPC_MCAM_ALLOC_FAILED;
3081 
3082 	entry = entry_rsp.entry;
3083 
3084 	if (!req->alloc_cntr)
3085 		goto write_entry;
3086 
3087 	/* Now allocate counter */
3088 	cntr_req.hdr.pcifunc = req->hdr.pcifunc;
3089 	cntr_req.contig = true;
3090 	cntr_req.count = 1;
3091 
3092 	rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3093 	if (rc) {
3094 		/* Free allocated MCAM entry */
3095 		mutex_lock(&mcam->lock);
3096 		mcam->entry2pfvf_map[entry] = NPC_MCAM_INVALID_MAP;
3097 		npc_mcam_clear_bit(mcam, entry);
3098 		mutex_unlock(&mcam->lock);
3099 		return rc;
3100 	}
3101 
3102 	cntr = cntr_rsp.cntr;
3103 
3104 write_entry:
3105 	mutex_lock(&mcam->lock);
3106 
3107 	if (is_npc_intf_tx(req->intf))
3108 		nix_intf = pfvf->nix_tx_intf;
3109 	else
3110 		nix_intf = pfvf->nix_rx_intf;
3111 
3112 	npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
3113 			      &req->entry_data, req->enable_entry);
3114 
3115 	if (req->alloc_cntr)
3116 		npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
3117 	mutex_unlock(&mcam->lock);
3118 
3119 	rsp->entry = entry;
3120 	rsp->cntr = cntr;
3121 
3122 	return 0;
3123 }
3124 
3125 #define GET_KEX_CFG(intf) \
3126 	rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
3127 
3128 #define GET_KEX_FLAGS(ld) \
3129 	rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
3130 
3131 #define GET_KEX_LD(intf, lid, lt, ld)	\
3132 	rvu_read64(rvu, BLKADDR_NPC,	\
3133 		NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
3134 
3135 #define GET_KEX_LDFLAGS(intf, ld, fl)	\
3136 	rvu_read64(rvu, BLKADDR_NPC,	\
3137 		NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
3138 
3139 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
3140 				     struct npc_get_kex_cfg_rsp *rsp)
3141 {
3142 	int lid, lt, ld, fl;
3143 
3144 	rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3145 	rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3146 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
3147 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
3148 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
3149 				rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3150 					GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3151 				rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3152 					GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3153 			}
3154 		}
3155 	}
3156 	for (ld = 0; ld < NPC_MAX_LD; ld++)
3157 		rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3158 
3159 	for (ld = 0; ld < NPC_MAX_LD; ld++) {
3160 		for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3161 			rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3162 					GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3163 			rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3164 					GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3165 		}
3166 	}
3167 	memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3168 	return 0;
3169 }
3170 
3171 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3172 					      struct msg_req *req,
3173 					      struct npc_mcam_read_base_rule_rsp *rsp)
3174 {
3175 	struct npc_mcam *mcam = &rvu->hw->mcam;
3176 	int index, blkaddr, nixlf, rc = 0;
3177 	u16 pcifunc = req->hdr.pcifunc;
3178 	struct rvu_pfvf *pfvf;
3179 	u8 intf, enable;
3180 
3181 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3182 	if (blkaddr < 0)
3183 		return NPC_MCAM_INVALID_REQ;
3184 
3185 	/* Return the channel number in case of PF */
3186 	if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3187 		pfvf = rvu_get_pfvf(rvu, pcifunc);
3188 		rsp->entry.kw[0] = pfvf->rx_chan_base;
3189 		rsp->entry.kw_mask[0] = 0xFFFULL;
3190 		goto out;
3191 	}
3192 
3193 	/* Find the pkt steering rule installed by PF to this VF */
3194 	mutex_lock(&mcam->lock);
3195 	for (index = 0; index < mcam->bmap_entries; index++) {
3196 		if (mcam->entry2target_pffunc[index] == pcifunc)
3197 			goto read_entry;
3198 	}
3199 
3200 	rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3201 	if (rc < 0) {
3202 		mutex_unlock(&mcam->lock);
3203 		goto out;
3204 	}
3205 	/* Read the default ucast entry if there is no pkt steering rule */
3206 	index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3207 					 NIXLF_UCAST_ENTRY);
3208 read_entry:
3209 	/* Read the mcam entry */
3210 	npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3211 			    &enable);
3212 	mutex_unlock(&mcam->lock);
3213 out:
3214 	return rc;
3215 }
3216 
3217 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3218 					  struct npc_mcam_get_stats_req *req,
3219 					  struct npc_mcam_get_stats_rsp *rsp)
3220 {
3221 	struct npc_mcam *mcam = &rvu->hw->mcam;
3222 	u16 index, cntr;
3223 	int blkaddr;
3224 	u64 regval;
3225 	u32 bank;
3226 
3227 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3228 	if (blkaddr < 0)
3229 		return NPC_MCAM_INVALID_REQ;
3230 
3231 	mutex_lock(&mcam->lock);
3232 
3233 	index = req->entry & (mcam->banksize - 1);
3234 	bank = npc_get_bank(mcam, req->entry);
3235 
3236 	/* read MCAM entry STAT_ACT register */
3237 	regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3238 
3239 	if (!(regval & rvu->hw->npc_stat_ena)) {
3240 		rsp->stat_ena = 0;
3241 		mutex_unlock(&mcam->lock);
3242 		return 0;
3243 	}
3244 
3245 	cntr = regval & 0x1FF;
3246 
3247 	rsp->stat_ena = 1;
3248 	rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3249 	rsp->stat &= BIT_ULL(48) - 1;
3250 
3251 	mutex_unlock(&mcam->lock);
3252 
3253 	return 0;
3254 }
3255