Debian Codegraph @

drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c

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