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