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