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