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