1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Admin Function driver
4 * Copyright (C) 2018 Marvell International Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/bitfield.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
15 #include "rvu_struct.h"
20 #include "npc_profile.h"
22 #define RSVD_MCAM_ENTRIES_PER_PF 3 /* Broadcast, Promisc and AllMulticast */
23 #define RSVD_MCAM_ENTRIES_PER_NIXLF 1 /* Ucast for LFs */
25 #define NPC_PARSE_RESULT_DMAC_OFFSET 8
26 #define NPC_HW_TSTAMP_OFFSET 8
27 #define NPC_KEX_CHAN_MASK 0xFFFULL
28 #define NPC_KEX_PF_FUNC_MASK 0xFFFFULL
30 #define ALIGN_8B_CEIL(__a) (((__a) + 7) & (-8))
32 static const char def_pfl_name[] = "default";
34 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
35 int blkaddr, u16 pcifunc);
36 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
39 bool is_npc_intf_tx(u8 intf)
41 return !!(intf & 0x1);
44 bool is_npc_intf_rx(u8 intf)
49 bool is_npc_interface_valid(struct rvu *rvu, u8 intf)
51 struct rvu_hwinfo *hw = rvu->hw;
53 return intf < hw->npc_intfs;
56 int rvu_npc_get_tx_nibble_cfg(struct rvu *rvu, u64 nibble_ena)
58 /* Due to a HW issue in these silicon versions, parse nibble enable
59 * configuration has to be identical for both Rx and Tx interfaces.
61 if (is_rvu_96xx_B0(rvu))
66 static int npc_mcam_verify_pf_func(struct rvu *rvu,
67 struct mcam_entry *entry_data, u8 intf,
70 u16 pf_func, pf_func_mask;
72 if (is_npc_intf_rx(intf))
75 pf_func_mask = (entry_data->kw_mask[0] >> 32) &
77 pf_func = (entry_data->kw[0] >> 32) & NPC_KEX_PF_FUNC_MASK;
79 pf_func = be16_to_cpu((__force __be16)pf_func);
80 if (pf_func_mask != NPC_KEX_PF_FUNC_MASK ||
81 ((pf_func & ~RVU_PFVF_FUNC_MASK) !=
82 (pcifunc & ~RVU_PFVF_FUNC_MASK)))
88 int npc_mcam_verify_channel(struct rvu *rvu, u16 pcifunc, u8 intf, u16 channel)
90 int pf = rvu_get_pf(pcifunc);
94 if (is_npc_intf_tx(intf))
97 /* return in case of AF installed rules */
98 if (is_pffunc_af(pcifunc))
101 if (is_afvf(pcifunc)) {
102 end = rvu_get_num_lbk_chans();
106 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
107 base = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0x0);
108 /* CGX mapped functions has maximum of 16 channels */
109 end = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0xF);
112 if (channel < base || channel > end)
118 void rvu_npc_set_pkind(struct rvu *rvu, int pkind, struct rvu_pfvf *pfvf)
123 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
127 /* Config CPI base for the PKIND */
128 val = pkind | 1ULL << 62;
129 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_CPI_DEFX(pkind, 0), val);
132 int rvu_npc_get_pkind(struct rvu *rvu, u16 pf)
134 struct npc_pkind *pkind = &rvu->hw->pkind;
138 for (i = 0; i < pkind->rsrc.max; i++) {
139 map = pkind->pfchan_map[i];
140 if (((map >> 16) & 0x3F) == pf)
146 #define NPC_AF_ACTION0_PTR_ADVANCE GENMASK_ULL(27, 20)
148 int npc_config_ts_kpuaction(struct rvu *rvu, int pf, u16 pcifunc, bool enable)
153 pkind = rvu_npc_get_pkind(rvu, pf);
155 dev_err(rvu->dev, "%s: pkind not mapped\n", __func__);
159 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
161 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
165 val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
166 val &= ~NPC_AF_ACTION0_PTR_ADVANCE;
167 /* If timestamp is enabled then configure NPC to shift 8 bytes */
169 val |= FIELD_PREP(NPC_AF_ACTION0_PTR_ADVANCE,
170 NPC_HW_TSTAMP_OFFSET);
171 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
176 static int npc_get_ucast_mcam_index(struct npc_mcam *mcam, u16 pcifunc,
179 struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
180 struct rvu *rvu = hw->rvu;
181 int blkaddr = 0, max = 0;
182 struct rvu_block *block;
183 struct rvu_pfvf *pfvf;
185 pfvf = rvu_get_pfvf(rvu, pcifunc);
186 /* Given a PF/VF and NIX LF number calculate the unicast mcam
187 * entry index based on the NIX block assigned to the PF/VF.
189 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
191 if (pfvf->nix_blkaddr == blkaddr)
193 block = &rvu->hw->block[blkaddr];
194 max += block->lf.max;
195 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
198 return mcam->nixlf_offset + (max + nixlf) * RSVD_MCAM_ENTRIES_PER_NIXLF;
201 int npc_get_nixlf_mcam_index(struct npc_mcam *mcam,
202 u16 pcifunc, int nixlf, int type)
204 int pf = rvu_get_pf(pcifunc);
207 /* Check if this is for a PF */
208 if (pf && !(pcifunc & RVU_PFVF_FUNC_MASK)) {
209 /* Reserved entries exclude PF0 */
211 index = mcam->pf_offset + (pf * RSVD_MCAM_ENTRIES_PER_PF);
212 /* Broadcast address matching entry should be first so
213 * that the packet can be replicated to all VFs.
215 if (type == NIXLF_BCAST_ENTRY)
217 else if (type == NIXLF_ALLMULTI_ENTRY)
219 else if (type == NIXLF_PROMISC_ENTRY)
223 return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
226 int npc_get_bank(struct npc_mcam *mcam, int index)
228 int bank = index / mcam->banksize;
230 /* 0,1 & 2,3 banks are combined for this keysize */
231 if (mcam->keysize == NPC_MCAM_KEY_X2)
237 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
238 int blkaddr, int index)
240 int bank = npc_get_bank(mcam, index);
243 index &= (mcam->banksize - 1);
244 cfg = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_CFG(index, bank));
248 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
249 int blkaddr, int index, bool enable)
251 int bank = npc_get_bank(mcam, index);
254 index &= (mcam->banksize - 1);
255 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
256 rvu_write64(rvu, blkaddr,
257 NPC_AF_MCAMEX_BANKX_CFG(index, bank),
262 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
263 int blkaddr, int index)
265 int bank = npc_get_bank(mcam, index);
268 index &= (mcam->banksize - 1);
269 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
270 rvu_write64(rvu, blkaddr,
271 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
272 rvu_write64(rvu, blkaddr,
273 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
275 rvu_write64(rvu, blkaddr,
276 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
277 rvu_write64(rvu, blkaddr,
278 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
280 rvu_write64(rvu, blkaddr,
281 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
282 rvu_write64(rvu, blkaddr,
283 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
287 static void npc_get_keyword(struct mcam_entry *entry, int idx,
288 u64 *cam0, u64 *cam1)
292 #define CAM_MASK(n) (BIT_ULL(n) - 1)
294 /* 0, 2, 4, 6 indices refer to BANKX_CAMX_W0 and
295 * 1, 3, 5, 7 indices refer to BANKX_CAMX_W1.
297 * Also, only 48 bits of BANKX_CAMX_W1 are valid.
301 /* BANK(X)_CAM_W0<63:0> = MCAM_KEY[KW0]<63:0> */
302 *cam1 = entry->kw[0];
303 kw_mask = entry->kw_mask[0];
306 /* BANK(X)_CAM_W1<47:0> = MCAM_KEY[KW1]<47:0> */
307 *cam1 = entry->kw[1] & CAM_MASK(48);
308 kw_mask = entry->kw_mask[1] & CAM_MASK(48);
311 /* BANK(X + 1)_CAM_W0<15:0> = MCAM_KEY[KW1]<63:48>
312 * BANK(X + 1)_CAM_W0<63:16> = MCAM_KEY[KW2]<47:0>
314 *cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
315 *cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
316 kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
317 kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
320 /* BANK(X + 1)_CAM_W1<15:0> = MCAM_KEY[KW2]<63:48>
321 * BANK(X + 1)_CAM_W1<47:16> = MCAM_KEY[KW3]<31:0>
323 *cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
324 *cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
325 kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
326 kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
329 /* BANK(X + 2)_CAM_W0<31:0> = MCAM_KEY[KW3]<63:32>
330 * BANK(X + 2)_CAM_W0<63:32> = MCAM_KEY[KW4]<31:0>
332 *cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
333 *cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
334 kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
335 kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
338 /* BANK(X + 2)_CAM_W1<31:0> = MCAM_KEY[KW4]<63:32>
339 * BANK(X + 2)_CAM_W1<47:32> = MCAM_KEY[KW5]<15:0>
341 *cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
342 *cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
343 kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
344 kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
347 /* BANK(X + 3)_CAM_W0<47:0> = MCAM_KEY[KW5]<63:16>
348 * BANK(X + 3)_CAM_W0<63:48> = MCAM_KEY[KW6]<15:0>
350 *cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
351 *cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
352 kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
353 kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
356 /* BANK(X + 3)_CAM_W1<47:0> = MCAM_KEY[KW6]<63:16> */
357 *cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
358 kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
363 *cam0 = ~*cam1 & kw_mask;
366 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
369 /* Similar to npc_get_keyword, but fills mcam_entry structure from
375 entry->kw_mask[0] = cam1 ^ cam0;
379 entry->kw_mask[1] = cam1 ^ cam0;
382 entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
383 entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
384 entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
385 entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
388 entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
389 entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
390 entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
391 entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
394 entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
395 entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
396 entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
397 entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
400 entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
401 entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
402 entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
403 entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
406 entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
407 entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
408 entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
409 entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
412 entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
413 entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
418 static u64 npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
419 int blkaddr, u16 pf_func)
421 int bank, nixlf, index;
423 /* get ucast entry rule entry index */
424 nix_get_nixlf(rvu, pf_func, &nixlf, NULL);
425 index = npc_get_nixlf_mcam_index(mcam, pf_func, nixlf,
427 bank = npc_get_bank(mcam, index);
428 index &= (mcam->banksize - 1);
430 return rvu_read64(rvu, blkaddr,
431 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
434 static void npc_fixup_vf_rule(struct rvu *rvu, struct npc_mcam *mcam,
435 int blkaddr, int index, struct mcam_entry *entry,
438 u16 owner, target_func;
439 struct rvu_pfvf *pfvf;
442 owner = mcam->entry2pfvf_map[index];
443 target_func = (entry->action >> 4) & 0xffff;
444 /* do nothing when target is LBK/PF or owner is not PF */
445 if (is_afvf(target_func) || (owner & RVU_PFVF_FUNC_MASK) ||
446 !(target_func & RVU_PFVF_FUNC_MASK))
449 /* save entry2target_pffunc */
450 pfvf = rvu_get_pfvf(rvu, target_func);
451 mcam->entry2target_pffunc[index] = target_func;
453 /* don't enable rule when nixlf not attached or initialized */
454 if (!(is_nixlf_attached(rvu, target_func) &&
455 test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
458 /* copy VF default entry action to the VF mcam entry */
459 rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
462 entry->action = rx_action;
465 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
466 int blkaddr, int index, u8 intf,
467 struct mcam_entry *entry, bool enable)
469 int bank = npc_get_bank(mcam, index);
470 int kw = 0, actbank, actindex;
473 actbank = bank; /* Save bank id, to set action later on */
475 index &= (mcam->banksize - 1);
477 /* Disable before mcam entry update */
478 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
480 /* Clear mcam entry to avoid writes being suppressed by NPC */
481 npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
483 /* CAM1 takes the comparison value and
484 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
485 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
486 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
487 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
489 for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
490 /* Interface should be set in all banks */
491 rvu_write64(rvu, blkaddr,
492 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
494 rvu_write64(rvu, blkaddr,
495 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
498 /* Set the match key */
499 npc_get_keyword(entry, kw, &cam0, &cam1);
500 rvu_write64(rvu, blkaddr,
501 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
502 rvu_write64(rvu, blkaddr,
503 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
505 npc_get_keyword(entry, kw + 1, &cam0, &cam1);
506 rvu_write64(rvu, blkaddr,
507 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
508 rvu_write64(rvu, blkaddr,
509 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
512 /* PF installing VF rule */
513 if (intf == NIX_INTF_RX && actindex < mcam->bmap_entries)
514 npc_fixup_vf_rule(rvu, mcam, blkaddr, index, entry, &enable);
517 rvu_write64(rvu, blkaddr,
518 NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
520 /* Set TAG 'action' */
521 rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
524 /* Enable the entry */
526 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
529 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
530 int blkaddr, u16 src,
531 struct mcam_entry *entry, u8 *intf, u8 *ena)
533 int sbank = npc_get_bank(mcam, src);
537 src &= (mcam->banksize - 1);
540 for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
541 cam1 = rvu_read64(rvu, blkaddr,
542 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
543 cam0 = rvu_read64(rvu, blkaddr,
544 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
545 npc_fill_entryword(entry, kw, cam0, cam1);
547 cam1 = rvu_read64(rvu, blkaddr,
548 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
549 cam0 = rvu_read64(rvu, blkaddr,
550 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
551 npc_fill_entryword(entry, kw + 1, cam0, cam1);
554 entry->action = rvu_read64(rvu, blkaddr,
555 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
557 rvu_read64(rvu, blkaddr,
558 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
559 *intf = rvu_read64(rvu, blkaddr,
560 NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
561 *ena = rvu_read64(rvu, blkaddr,
562 NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
565 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
566 int blkaddr, u16 src, u16 dest)
568 int dbank = npc_get_bank(mcam, dest);
569 int sbank = npc_get_bank(mcam, src);
573 src &= (mcam->banksize - 1);
574 dest &= (mcam->banksize - 1);
576 /* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
577 for (bank = 0; bank < mcam->banks_per_entry; bank++) {
578 sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
579 dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
580 for (i = 0; i < 6; i++) {
581 cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
582 rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
587 cfg = rvu_read64(rvu, blkaddr,
588 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
589 rvu_write64(rvu, blkaddr,
590 NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
592 /* Copy TAG action */
593 cfg = rvu_read64(rvu, blkaddr,
594 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
595 rvu_write64(rvu, blkaddr,
596 NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
598 /* Enable or disable */
599 cfg = rvu_read64(rvu, blkaddr,
600 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
601 rvu_write64(rvu, blkaddr,
602 NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
605 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
606 int blkaddr, int index)
608 int bank = npc_get_bank(mcam, index);
610 index &= (mcam->banksize - 1);
611 return rvu_read64(rvu, blkaddr,
612 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
615 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
616 int nixlf, u64 chan, u8 *mac_addr)
618 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
619 struct npc_install_flow_req req = { 0 };
620 struct npc_install_flow_rsp rsp = { 0 };
621 struct npc_mcam *mcam = &rvu->hw->mcam;
622 struct nix_rx_action action;
625 /* AF's VFs work in promiscuous mode */
626 if (is_afvf(pcifunc))
629 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
633 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
634 nixlf, NIXLF_UCAST_ENTRY);
636 /* Don't change the action if entry is already enabled
637 * Otherwise RSS action may get overwritten.
639 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
640 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
643 *(u64 *)&action = 0x00;
644 action.op = NIX_RX_ACTIONOP_UCAST;
645 action.pf_func = pcifunc;
648 req.default_rule = 1;
649 ether_addr_copy(req.packet.dmac, mac_addr);
650 eth_broadcast_addr((u8 *)&req.mask.dmac);
651 req.features = BIT_ULL(NPC_DMAC);
653 req.intf = pfvf->nix_rx_intf;
655 req.hdr.pcifunc = 0; /* AF is requester */
656 req.vf = action.pf_func;
657 req.index = action.index;
658 req.match_id = action.match_id;
659 req.flow_key_alg = action.flow_key_alg;
661 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
664 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
665 int nixlf, u64 chan, u8 chan_cnt)
667 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
668 struct npc_install_flow_req req = { 0 };
669 struct npc_install_flow_rsp rsp = { 0 };
670 struct npc_mcam *mcam = &rvu->hw->mcam;
671 struct rvu_hwinfo *hw = rvu->hw;
672 int blkaddr, ucast_idx, index;
673 struct nix_rx_action action;
676 if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
679 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
683 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
684 nixlf, NIXLF_PROMISC_ENTRY);
686 if (is_cgx_vf(rvu, pcifunc))
687 index = npc_get_nixlf_mcam_index(mcam,
688 pcifunc & ~RVU_PFVF_FUNC_MASK,
689 nixlf, NIXLF_PROMISC_ENTRY);
691 /* If the corresponding PF's ucast action is RSS,
692 * use the same action for promisc also
694 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
695 nixlf, NIXLF_UCAST_ENTRY);
696 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
697 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
700 if (action.op != NIX_RX_ACTIONOP_RSS) {
701 *(u64 *)&action = 0x00;
702 action.op = NIX_RX_ACTIONOP_UCAST;
705 /* RX_ACTION set to MCAST for CGX PF's */
706 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
707 is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
708 *(u64 *)&action = 0x00;
709 action.op = NIX_RX_ACTIONOP_MCAST;
710 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
711 action.index = pfvf->promisc_mce_idx;
714 req.chan_mask = 0xFFFU;
716 if (!is_power_of_2(chan_cnt)) {
718 "%s: channel count more than 1, must be power of 2\n", __func__);
721 relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
723 req.chan_mask &= relaxed_mask;
727 req.intf = pfvf->nix_rx_intf;
730 req.hdr.pcifunc = 0; /* AF is requester */
732 req.index = action.index;
733 req.match_id = action.match_id;
734 req.flow_key_alg = action.flow_key_alg;
736 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
739 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
740 int nixlf, bool enable)
742 struct npc_mcam *mcam = &rvu->hw->mcam;
745 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
749 /* Get 'pcifunc' of PF device */
750 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
752 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
753 nixlf, NIXLF_PROMISC_ENTRY);
754 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
757 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
760 struct rvu_pfvf *pfvf;
761 struct npc_install_flow_req req = { 0 };
762 struct npc_install_flow_rsp rsp = { 0 };
763 struct npc_mcam *mcam = &rvu->hw->mcam;
764 struct rvu_hwinfo *hw = rvu->hw;
767 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
772 if (is_afvf(pcifunc))
775 /* If pkt replication is not supported,
776 * then only PF is allowed to add a bcast match entry.
778 if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
781 /* Get 'pcifunc' of PF device */
782 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
783 pfvf = rvu_get_pfvf(rvu, pcifunc);
784 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
785 nixlf, NIXLF_BCAST_ENTRY);
787 if (!hw->cap.nix_rx_multicast) {
788 /* Early silicon doesn't support pkt replication,
789 * so install entry with UCAST action, so that PF
790 * receives all broadcast packets.
792 req.op = NIX_RX_ACTIONOP_UCAST;
794 req.op = NIX_RX_ACTIONOP_MCAST;
795 req.index = pfvf->bcast_mce_idx;
798 eth_broadcast_addr((u8 *)&req.packet.dmac);
799 eth_broadcast_addr((u8 *)&req.mask.dmac);
800 req.features = BIT_ULL(NPC_DMAC);
802 req.intf = pfvf->nix_rx_intf;
804 req.hdr.pcifunc = 0; /* AF is requester */
807 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
810 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
813 struct npc_mcam *mcam = &rvu->hw->mcam;
816 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
820 /* Get 'pcifunc' of PF device */
821 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
823 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
825 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
828 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
831 struct npc_install_flow_req req = { 0 };
832 struct npc_install_flow_rsp rsp = { 0 };
833 struct npc_mcam *mcam = &rvu->hw->mcam;
834 struct rvu_hwinfo *hw = rvu->hw;
835 int blkaddr, ucast_idx, index;
836 u8 mac_addr[ETH_ALEN] = { 0 };
837 struct nix_rx_action action;
838 struct rvu_pfvf *pfvf;
841 /* Only CGX PF/VF can add allmulticast entry */
842 if (is_afvf(pcifunc))
845 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
849 /* Get 'pcifunc' of PF device */
850 vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
851 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
852 pfvf = rvu_get_pfvf(rvu, pcifunc);
853 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
854 nixlf, NIXLF_ALLMULTI_ENTRY);
856 /* If the corresponding PF's ucast action is RSS,
857 * use the same action for multicast entry also
859 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
860 nixlf, NIXLF_UCAST_ENTRY);
861 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
862 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
865 if (action.op != NIX_RX_ACTIONOP_RSS) {
866 *(u64 *)&action = 0x00;
867 action.op = NIX_RX_ACTIONOP_UCAST;
868 action.pf_func = pcifunc;
871 /* RX_ACTION set to MCAST for CGX PF's */
872 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
873 *(u64 *)&action = 0x00;
874 action.op = NIX_RX_ACTIONOP_MCAST;
875 action.index = pfvf->mcast_mce_idx;
878 mac_addr[0] = 0x01; /* LSB bit of 1st byte in DMAC */
879 ether_addr_copy(req.packet.dmac, mac_addr);
880 ether_addr_copy(req.mask.dmac, mac_addr);
881 req.features = BIT_ULL(NPC_DMAC);
883 /* For cn10k the upper two bits of the channel number are
884 * cpt channel number. with masking out these bits in the
885 * mcam entry, same entry used for NIX will allow packets
886 * received from cpt for parsing.
888 if (!is_rvu_otx2(rvu))
889 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
891 req.chan_mask = 0xFFFU;
894 req.intf = pfvf->nix_rx_intf;
897 req.hdr.pcifunc = 0; /* AF is requester */
898 req.vf = pcifunc | vf_func;
899 req.index = action.index;
900 req.match_id = action.match_id;
901 req.flow_key_alg = action.flow_key_alg;
903 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
906 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
909 struct npc_mcam *mcam = &rvu->hw->mcam;
912 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
916 /* Get 'pcifunc' of PF device */
917 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
919 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
920 NIXLF_ALLMULTI_ENTRY);
921 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
924 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
925 int blkaddr, u16 pcifunc, u64 rx_action)
927 int actindex, index, bank;
930 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
933 mutex_lock(&mcam->lock);
934 for (index = 0; index < mcam->bmap_entries; index++) {
935 if (mcam->entry2target_pffunc[index] == pcifunc) {
936 bank = npc_get_bank(mcam, index);
938 index &= (mcam->banksize - 1);
940 /* read vf flow entry enable status */
941 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
943 /* disable before mcam entry update */
944 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
946 /* update 'action' */
947 rvu_write64(rvu, blkaddr,
948 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
951 npc_enable_mcam_entry(rvu, mcam, blkaddr,
955 mutex_unlock(&mcam->lock);
958 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
959 int group, int alg_idx, int mcam_index)
961 struct npc_mcam *mcam = &rvu->hw->mcam;
962 struct rvu_hwinfo *hw = rvu->hw;
963 struct nix_rx_action action;
964 int blkaddr, index, bank;
965 struct rvu_pfvf *pfvf;
967 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
971 /* Check if this is for reserved default entry */
972 if (mcam_index < 0) {
973 if (group != DEFAULT_RSS_CONTEXT_GROUP)
975 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
976 nixlf, NIXLF_UCAST_ENTRY);
978 /* TODO: validate this mcam index */
982 if (index >= mcam->total_entries)
985 bank = npc_get_bank(mcam, index);
986 index &= (mcam->banksize - 1);
988 *(u64 *)&action = rvu_read64(rvu, blkaddr,
989 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
990 /* Ignore if no action was set earlier */
991 if (!*(u64 *)&action)
994 action.op = NIX_RX_ACTIONOP_RSS;
995 action.pf_func = pcifunc;
996 action.index = group;
997 action.flow_key_alg = alg_idx;
999 rvu_write64(rvu, blkaddr,
1000 NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
1002 /* update the VF flow rule action with the VF default entry action */
1004 npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
1007 /* update the action change in default rule */
1008 pfvf = rvu_get_pfvf(rvu, pcifunc);
1009 if (pfvf->def_ucast_rule)
1010 pfvf->def_ucast_rule->rx_action = action;
1012 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1013 nixlf, NIXLF_PROMISC_ENTRY);
1015 /* If PF's promiscuous entry is enabled,
1016 * Set RSS action for that entry as well
1018 if ((!hw->cap.nix_rx_multicast || !pfvf->use_mce_list) &&
1019 is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
1020 bank = npc_get_bank(mcam, index);
1021 index &= (mcam->banksize - 1);
1023 rvu_write64(rvu, blkaddr,
1024 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
1029 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1030 int nixlf, int type, bool enable)
1032 struct npc_mcam *mcam = &rvu->hw->mcam;
1033 struct rvu_hwinfo *hw = rvu->hw;
1034 struct nix_mce_list *mce_list;
1035 int index, blkaddr, mce_idx;
1036 struct rvu_pfvf *pfvf;
1038 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1042 index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1045 /* disable MCAM entry when packet replication is not supported by hw */
1046 if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1047 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1051 /* return incase mce list is not enabled */
1052 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1053 if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1054 type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1057 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1059 nix_update_mce_list(rvu, pcifunc, mce_list,
1060 mce_idx, index, enable);
1062 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1065 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1066 int nixlf, bool enable)
1068 struct npc_mcam *mcam = &rvu->hw->mcam;
1071 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1075 /* Ucast MCAM match entry of this PF/VF */
1076 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1077 nixlf, NIXLF_UCAST_ENTRY);
1078 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1080 /* Nothing to do for VFs, on platforms where pkt replication
1083 if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1086 /* add/delete pf_func to broadcast MCE list */
1087 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1088 NIXLF_BCAST_ENTRY, enable);
1091 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1093 npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1095 /* Delete multicast and promisc MCAM entries */
1096 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1097 NIXLF_ALLMULTI_ENTRY, false);
1098 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1099 NIXLF_PROMISC_ENTRY, false);
1102 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1104 /* Enables only broadcast match entry. Promisc/Allmulti are enabled
1105 * in set_rx_mode mbox handler.
1107 npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1110 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1112 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1113 struct npc_mcam *mcam = &rvu->hw->mcam;
1114 struct rvu_npc_mcam_rule *rule, *tmp;
1117 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1121 mutex_lock(&mcam->lock);
1123 /* Disable MCAM entries directing traffic to this 'pcifunc' */
1124 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1125 if (is_npc_intf_rx(rule->intf) &&
1126 rule->rx_action.pf_func == pcifunc &&
1127 rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1128 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1129 rule->entry, false);
1130 rule->enable = false;
1131 /* Indicate that default rule is disabled */
1132 if (rule->default_rule) {
1133 pfvf->def_ucast_rule = NULL;
1134 list_del(&rule->list);
1140 mutex_unlock(&mcam->lock);
1142 npc_mcam_disable_flows(rvu, pcifunc);
1144 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1147 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1149 struct npc_mcam *mcam = &rvu->hw->mcam;
1150 struct rvu_npc_mcam_rule *rule, *tmp;
1153 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1157 mutex_lock(&mcam->lock);
1159 /* Free all MCAM entries owned by this 'pcifunc' */
1160 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1162 /* Free all MCAM counters owned by this 'pcifunc' */
1163 npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1165 /* Delete MCAM entries owned by this 'pcifunc' */
1166 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1167 if (rule->owner == pcifunc && !rule->default_rule) {
1168 list_del(&rule->list);
1173 mutex_unlock(&mcam->lock);
1175 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1178 #define SET_KEX_LD(intf, lid, ltype, ld, cfg) \
1179 rvu_write64(rvu, blkaddr, \
1180 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, ltype, ld), cfg)
1182 #define SET_KEX_LDFLAGS(intf, ld, flags, cfg) \
1183 rvu_write64(rvu, blkaddr, \
1184 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, flags), cfg)
1186 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1187 struct npc_mcam_kex *mkex, u8 intf)
1189 int lid, lt, ld, fl;
1191 if (is_npc_intf_tx(intf))
1194 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1195 mkex->keyx_cfg[NIX_INTF_RX]);
1198 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1199 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1200 for (ld = 0; ld < NPC_MAX_LD; ld++)
1201 SET_KEX_LD(intf, lid, lt, ld,
1202 mkex->intf_lid_lt_ld[NIX_INTF_RX]
1206 /* Program LFLAGS */
1207 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1208 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1209 SET_KEX_LDFLAGS(intf, ld, fl,
1210 mkex->intf_ld_flags[NIX_INTF_RX]
1215 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1216 struct npc_mcam_kex *mkex, u8 intf)
1218 int lid, lt, ld, fl;
1220 if (is_npc_intf_rx(intf))
1223 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1224 mkex->keyx_cfg[NIX_INTF_TX]);
1227 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1228 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1229 for (ld = 0; ld < NPC_MAX_LD; ld++)
1230 SET_KEX_LD(intf, lid, lt, ld,
1231 mkex->intf_lid_lt_ld[NIX_INTF_TX]
1235 /* Program LFLAGS */
1236 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1237 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1238 SET_KEX_LDFLAGS(intf, ld, fl,
1239 mkex->intf_ld_flags[NIX_INTF_TX]
1244 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1245 struct npc_mcam_kex *mkex)
1247 struct rvu_hwinfo *hw = rvu->hw;
1251 for (ld = 0; ld < NPC_MAX_LD; ld++)
1252 rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1253 mkex->kex_ld_flags[ld]);
1255 for (intf = 0; intf < hw->npc_intfs; intf++) {
1256 npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1257 npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1261 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1264 u64 prfl_addr, prfl_sz;
1269 prfl_addr = rvu->fwdata->mcam_addr;
1270 prfl_sz = rvu->fwdata->mcam_sz;
1272 if (!prfl_addr || !prfl_sz)
1275 *prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1276 if (!(*prfl_img_addr))
1284 /* strtoull of "mkexprof" with base:36 */
1285 #define MKEX_END_SIGN 0xdeadbeef
1287 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1288 const char *mkex_profile)
1290 struct device *dev = &rvu->pdev->dev;
1291 struct npc_mcam_kex *mcam_kex;
1292 void __iomem *mkex_prfl_addr = NULL;
1296 /* If user not selected mkex profile */
1297 if (rvu->kpu_fwdata_sz ||
1298 !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1301 /* Setting up the mapping for mkex profile image */
1302 ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1306 mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1308 while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1309 /* Compare with mkex mod_param name string */
1310 if (mcam_kex->mkex_sign == MKEX_SIGN &&
1311 !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1312 /* Due to an errata (35786) in A0/B0 pass silicon,
1313 * parse nibble enable configuration has to be
1314 * identical for both Rx and Tx interfaces.
1316 if (!is_rvu_96xx_B0(rvu) ||
1317 mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1318 rvu->kpu.mkex = mcam_kex;
1323 prfl_sz -= sizeof(struct npc_mcam_kex);
1325 dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1328 dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1329 /* Program selected mkex profile */
1330 npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1332 iounmap(mkex_prfl_addr);
1335 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1336 const struct npc_kpu_profile_action *kpuaction,
1337 int kpu, int entry, bool pkind)
1339 struct npc_kpu_action0 action0 = {0};
1340 struct npc_kpu_action1 action1 = {0};
1343 action1.errlev = kpuaction->errlev;
1344 action1.errcode = kpuaction->errcode;
1345 action1.dp0_offset = kpuaction->dp0_offset;
1346 action1.dp1_offset = kpuaction->dp1_offset;
1347 action1.dp2_offset = kpuaction->dp2_offset;
1350 reg = NPC_AF_PKINDX_ACTION1(entry);
1352 reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1354 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1356 action0.byp_count = kpuaction->bypass_count;
1357 action0.capture_ena = kpuaction->cap_ena;
1358 action0.parse_done = kpuaction->parse_done;
1359 action0.next_state = kpuaction->next_state;
1360 action0.capture_lid = kpuaction->lid;
1361 action0.capture_ltype = kpuaction->ltype;
1362 action0.capture_flags = kpuaction->flags;
1363 action0.ptr_advance = kpuaction->ptr_advance;
1364 action0.var_len_offset = kpuaction->offset;
1365 action0.var_len_mask = kpuaction->mask;
1366 action0.var_len_right = kpuaction->right;
1367 action0.var_len_shift = kpuaction->shift;
1370 reg = NPC_AF_PKINDX_ACTION0(entry);
1372 reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1374 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1377 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1378 const struct npc_kpu_profile_cam *kpucam,
1381 struct npc_kpu_cam cam0 = {0};
1382 struct npc_kpu_cam cam1 = {0};
1384 cam1.state = kpucam->state & kpucam->state_mask;
1385 cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1386 cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1387 cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1389 cam0.state = ~kpucam->state & kpucam->state_mask;
1390 cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1391 cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1392 cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1394 rvu_write64(rvu, blkaddr,
1395 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1396 rvu_write64(rvu, blkaddr,
1397 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1400 static inline u64 enable_mask(int count)
1402 return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1405 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1406 const struct npc_kpu_profile *profile)
1408 int entry, num_entries, max_entries;
1411 if (profile->cam_entries != profile->action_entries) {
1413 "KPU%d: CAM and action entries [%d != %d] not equal\n",
1414 kpu, profile->cam_entries, profile->action_entries);
1417 max_entries = rvu->hw->npc_kpu_entries;
1419 /* Program CAM match entries for previous KPU extracted data */
1420 num_entries = min_t(int, profile->cam_entries, max_entries);
1421 for (entry = 0; entry < num_entries; entry++)
1422 npc_config_kpucam(rvu, blkaddr,
1423 &profile->cam[entry], kpu, entry);
1425 /* Program this KPU's actions */
1426 num_entries = min_t(int, profile->action_entries, max_entries);
1427 for (entry = 0; entry < num_entries; entry++)
1428 npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1431 /* Enable all programmed entries */
1432 num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1433 entry_mask = enable_mask(num_entries);
1434 /* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1435 if (!rvu->kpu.custom)
1436 entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1437 rvu_write64(rvu, blkaddr,
1438 NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1439 if (num_entries > 64) {
1440 rvu_write64(rvu, blkaddr,
1441 NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1442 enable_mask(num_entries - 64));
1445 /* Enable this KPU */
1446 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1449 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1451 profile->custom = 0;
1452 profile->name = def_pfl_name;
1453 profile->version = NPC_KPU_PROFILE_VER;
1454 profile->ikpu = ikpu_action_entries;
1455 profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1456 profile->kpu = npc_kpu_profiles;
1457 profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1458 profile->lt_def = &npc_lt_defaults;
1459 profile->mkex = &npc_mkex_default;
1464 static int npc_apply_custom_kpu(struct rvu *rvu,
1465 struct npc_kpu_profile_adapter *profile)
1467 size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1468 struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1469 struct npc_kpu_profile_action *action;
1470 struct npc_kpu_profile_cam *cam;
1471 struct npc_kpu_fwdata *fw_kpu;
1475 if (rvu->kpu_fwdata_sz < hdr_sz) {
1476 dev_warn(rvu->dev, "Invalid KPU profile size\n");
1479 if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1480 dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1484 /* Verify if the using known profile structure */
1485 if (NPC_KPU_VER_MAJ(profile->version) >
1486 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1487 dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1488 NPC_KPU_VER_MAJ(profile->version),
1489 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1492 /* Verify if profile is aligned with the required kernel changes */
1493 if (NPC_KPU_VER_MIN(profile->version) <
1494 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1496 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1497 NPC_KPU_VER_MAJ(profile->version),
1498 NPC_KPU_VER_MIN(profile->version),
1499 NPC_KPU_VER_PATCH(profile->version),
1500 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1501 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1502 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1505 /* Verify if profile fits the HW */
1506 if (fw->kpus > profile->kpus) {
1507 dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1512 profile->custom = 1;
1513 profile->name = fw->name;
1514 profile->version = le64_to_cpu(fw->version);
1515 profile->mkex = &fw->mkex;
1516 profile->lt_def = &fw->lt_def;
1518 for (kpu = 0; kpu < fw->kpus; kpu++) {
1519 fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1520 if (fw_kpu->entries > KPU_MAX_CST_ENT)
1522 "Too many custom entries on KPU%d: %d > %d\n",
1523 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1524 entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1525 cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1526 offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1527 action = (struct npc_kpu_profile_action *)(fw->data + offset);
1528 offset += fw_kpu->entries * sizeof(*action);
1529 if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1531 "Profile size mismatch on KPU%i parsing.\n",
1535 for (entry = 0; entry < entries; entry++) {
1536 profile->kpu[kpu].cam[entry] = cam[entry];
1537 profile->kpu[kpu].action[entry] = action[entry];
1544 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1545 u64 prfl_sz, const char *kpu_profile)
1547 struct npc_kpu_profile_fwdata *kpu_data = NULL;
1550 kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1551 if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1552 !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1553 dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1555 rvu->kpu_fwdata = kpu_data;
1556 rvu->kpu_fwdata_sz = prfl_sz;
1557 rvu->kpu_prfl_addr = prfl_addr;
1564 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1565 const char *kpu_profile)
1567 struct npc_coalesced_kpu_prfl *img_data = NULL;
1568 int i = 0, rc = -EINVAL;
1569 void __iomem *kpu_prfl_addr;
1572 img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1573 if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1574 !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1575 /* Loaded profile is a single KPU profile. */
1576 rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1577 prfl_sz, kpu_profile);
1581 /* Loaded profile is coalesced image, offset of first KPU profile.*/
1582 offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1583 (img_data->num_prfl * sizeof(uint16_t));
1584 /* Check if mapped image is coalesced image. */
1585 while (i < img_data->num_prfl) {
1586 /* Profile image offsets are rounded up to next 8 multiple.*/
1587 offset = ALIGN_8B_CEIL(offset);
1588 kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1590 rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1591 img_data->prfl_sz[i], kpu_profile);
1594 /* Calculating offset of profile image based on profile size.*/
1595 offset += img_data->prfl_sz[i];
1602 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1607 /* Setting up the mapping for NPC profile image */
1608 ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1612 /* Detect if profile is coalesced or single KPU profile and load */
1613 ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1617 /* Cleaning up if KPU profile image from fwdata is not valid. */
1618 if (rvu->kpu_prfl_addr) {
1619 iounmap(rvu->kpu_prfl_addr);
1620 rvu->kpu_prfl_addr = NULL;
1621 rvu->kpu_fwdata_sz = 0;
1622 rvu->kpu_fwdata = NULL;
1629 static void npc_load_kpu_profile(struct rvu *rvu)
1631 struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1632 const char *kpu_profile = rvu->kpu_pfl_name;
1633 const struct firmware *fw = NULL;
1634 bool retry_fwdb = false;
1636 /* If user not specified profile customization */
1637 if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1638 goto revert_to_default;
1639 /* First prepare default KPU, then we'll customize top entries. */
1640 npc_prepare_default_kpu(profile);
1642 /* Order of preceedence for load loading NPC profile (high to low)
1643 * Firmware binary in filesystem.
1644 * Firmware database method.
1645 * Default KPU profile.
1647 if (!request_firmware(&fw, kpu_profile, rvu->dev)) {
1648 dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1650 rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1651 if (rvu->kpu_fwdata) {
1652 memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1653 rvu->kpu_fwdata_sz = fw->size;
1655 release_firmware(fw);
1661 /* Loading the KPU profile using firmware database */
1662 if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1663 goto revert_to_default;
1666 /* Apply profile customization if firmware was loaded. */
1667 if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1668 /* If image from firmware filesystem fails to load or invalid
1669 * retry with firmware database method.
1671 if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1672 /* Loading image from firmware database failed. */
1673 if (rvu->kpu_prfl_addr) {
1674 iounmap(rvu->kpu_prfl_addr);
1675 rvu->kpu_prfl_addr = NULL;
1677 kfree(rvu->kpu_fwdata);
1679 rvu->kpu_fwdata = NULL;
1680 rvu->kpu_fwdata_sz = 0;
1683 goto load_image_fwdb;
1688 "Can't load KPU profile %s. Using default.\n",
1690 kfree(rvu->kpu_fwdata);
1691 rvu->kpu_fwdata = NULL;
1692 goto revert_to_default;
1695 dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1696 profile->name, NPC_KPU_VER_MAJ(profile->version),
1697 NPC_KPU_VER_MIN(profile->version),
1698 NPC_KPU_VER_PATCH(profile->version));
1703 npc_prepare_default_kpu(profile);
1706 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1708 struct rvu_hwinfo *hw = rvu->hw;
1709 int num_pkinds, num_kpus, idx;
1710 struct npc_pkind *pkind;
1712 /* Disable all KPUs and their entries */
1713 for (idx = 0; idx < hw->npc_kpus; idx++) {
1714 rvu_write64(rvu, blkaddr,
1715 NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1716 rvu_write64(rvu, blkaddr,
1717 NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1718 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1721 /* Load and customize KPU profile. */
1722 npc_load_kpu_profile(rvu);
1724 /* First program IKPU profile i.e PKIND configs.
1725 * Check HW max count to avoid configuring junk or
1726 * writing to unsupported CSR addresses.
1729 num_pkinds = rvu->kpu.pkinds;
1730 num_pkinds = min_t(int, pkind->rsrc.max, num_pkinds);
1732 for (idx = 0; idx < num_pkinds; idx++)
1733 npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1735 /* Program KPU CAM and Action profiles */
1736 num_kpus = rvu->kpu.kpus;
1737 num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1739 for (idx = 0; idx < num_kpus; idx++)
1740 npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1743 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1745 int nixlf_count = rvu_get_nixlf_count(rvu);
1746 struct npc_mcam *mcam = &rvu->hw->mcam;
1750 /* Actual number of MCAM entries vary by entry size */
1751 cfg = (rvu_read64(rvu, blkaddr,
1752 NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1753 mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1754 mcam->keysize = cfg;
1756 /* Number of banks combined per MCAM entry */
1757 if (cfg == NPC_MCAM_KEY_X4)
1758 mcam->banks_per_entry = 4;
1759 else if (cfg == NPC_MCAM_KEY_X2)
1760 mcam->banks_per_entry = 2;
1762 mcam->banks_per_entry = 1;
1764 /* Reserve one MCAM entry for each of the NIX LF to
1765 * guarantee space to install default matching DMAC rule.
1766 * Also reserve 2 MCAM entries for each PF for default
1767 * channel based matching or 'bcast & promisc' matching to
1768 * support BCAST and PROMISC modes of operation for PFs.
1771 rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1772 ((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1773 if (mcam->total_entries <= rsvd) {
1775 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1776 mcam->total_entries);
1780 mcam->bmap_entries = mcam->total_entries - rsvd;
1781 mcam->nixlf_offset = mcam->bmap_entries;
1782 mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1784 /* Allocate bitmaps for managing MCAM entries */
1785 mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1786 sizeof(long), GFP_KERNEL);
1790 mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1791 BITS_TO_LONGS(mcam->bmap_entries),
1792 sizeof(long), GFP_KERNEL);
1793 if (!mcam->bmap_reverse)
1796 mcam->bmap_fcnt = mcam->bmap_entries;
1798 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
1799 mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1800 sizeof(u16), GFP_KERNEL);
1801 if (!mcam->entry2pfvf_map)
1804 /* Reserve 1/8th of MCAM entries at the bottom for low priority
1805 * allocations and another 1/8th at the top for high priority
1808 mcam->lprio_count = mcam->bmap_entries / 8;
1809 if (mcam->lprio_count > BITS_PER_LONG)
1810 mcam->lprio_count = round_down(mcam->lprio_count,
1812 mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1813 mcam->hprio_count = mcam->lprio_count;
1814 mcam->hprio_end = mcam->hprio_count;
1817 /* Allocate bitmap for managing MCAM counters and memory
1818 * for saving counter to RVU PFFUNC allocation mapping.
1820 err = rvu_alloc_bitmap(&mcam->counters);
1824 mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1825 sizeof(u16), GFP_KERNEL);
1826 if (!mcam->cntr2pfvf_map)
1829 /* Alloc memory for MCAM entry to counter mapping and for tracking
1830 * counter's reference count.
1832 mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1833 sizeof(u16), GFP_KERNEL);
1834 if (!mcam->entry2cntr_map)
1837 mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1838 sizeof(u16), GFP_KERNEL);
1839 if (!mcam->cntr_refcnt)
1842 /* Alloc memory for saving target device of mcam rule */
1843 mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1844 sizeof(u16), GFP_KERNEL);
1845 if (!mcam->entry2target_pffunc)
1848 mutex_init(&mcam->lock);
1853 kfree(mcam->counters.bmap);
1857 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1859 struct npc_pkind *pkind = &rvu->hw->pkind;
1860 struct npc_mcam *mcam = &rvu->hw->mcam;
1861 struct rvu_hwinfo *hw = rvu->hw;
1862 u64 npc_const, npc_const1;
1865 npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1866 npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1867 if (npc_const1 & BIT_ULL(63))
1868 npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1870 pkind->rsrc.max = (npc_const1 >> 12) & 0xFFULL;
1871 hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
1872 hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
1873 hw->npc_intfs = npc_const & 0xFULL;
1874 hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
1876 mcam->banks = (npc_const >> 44) & 0xFULL;
1877 mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
1880 hw->npc_ext_set = true;
1881 hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1882 mcam->banksize = npc_const2 & 0xFFFFULL;
1885 mcam->counters.max = hw->npc_counters;
1888 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1890 struct npc_mcam *mcam = &rvu->hw->mcam;
1891 struct rvu_hwinfo *hw = rvu->hw;
1892 u64 nibble_ena, rx_kex, tx_kex;
1895 /* Reserve last counter for MCAM RX miss action which is set to
1896 * drop packet. This way we will know how many pkts didn't match
1899 mcam->counters.max--;
1900 mcam->rx_miss_act_cntr = mcam->counters.max;
1902 rx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_RX];
1903 tx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_TX];
1904 nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1906 nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1908 tx_kex &= ~NPC_PARSE_NIBBLE;
1909 tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1910 npc_mkex_default.keyx_cfg[NIX_INTF_TX] = tx_kex;
1913 /* Configure RX interfaces */
1914 for (intf = 0; intf < hw->npc_intfs; intf++) {
1915 if (is_npc_intf_tx(intf))
1918 /* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
1919 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1922 /* If MCAM lookup doesn't result in a match, drop the received
1923 * packet. And map this action to a counter to count dropped
1926 rvu_write64(rvu, blkaddr,
1927 NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1929 /* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
1930 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
1932 rvu_write64(rvu, blkaddr,
1933 NPC_AF_INTFX_MISS_STAT_ACT(intf),
1934 ((mcam->rx_miss_act_cntr >> 9) << 12) |
1935 BIT_ULL(9) | mcam->rx_miss_act_cntr);
1938 /* Configure TX interfaces */
1939 for (intf = 0; intf < hw->npc_intfs; intf++) {
1940 if (is_npc_intf_rx(intf))
1943 /* Extract Ltypes LID_LA to LID_LE */
1944 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1947 /* Set TX miss action to UCAST_DEFAULT i.e
1948 * transmit the packet on NIX LF SQ's default channel.
1950 rvu_write64(rvu, blkaddr,
1951 NPC_AF_INTFX_MISS_ACT(intf),
1952 NIX_TX_ACTIONOP_UCAST_DEFAULT);
1956 int rvu_npc_init(struct rvu *rvu)
1958 struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
1959 struct npc_pkind *pkind = &rvu->hw->pkind;
1960 struct npc_mcam *mcam = &rvu->hw->mcam;
1961 int blkaddr, entry, bank, err;
1963 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1965 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1969 rvu_npc_hw_init(rvu, blkaddr);
1971 /* First disable all MCAM entries, to stop traffic towards NIXLFs */
1972 for (bank = 0; bank < mcam->banks; bank++) {
1973 for (entry = 0; entry < mcam->banksize; entry++)
1974 rvu_write64(rvu, blkaddr,
1975 NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
1978 err = rvu_alloc_bitmap(&pkind->rsrc);
1982 /* Allocate mem for pkind to PF and channel mapping info */
1983 pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
1984 sizeof(u32), GFP_KERNEL);
1985 if (!pkind->pfchan_map)
1988 /* Configure KPU profile */
1989 npc_parser_profile_init(rvu, blkaddr);
1991 /* Config Outer L2, IPv4's NPC layer info */
1992 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
1993 (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
1994 kpu->lt_def->pck_ol2.ltype_mask);
1995 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
1996 (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
1997 kpu->lt_def->pck_oip4.ltype_mask);
1999 /* Config Inner IPV4 NPC layer info */
2000 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2001 (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2002 kpu->lt_def->pck_iip4.ltype_mask);
2004 /* Enable below for Rx pkts.
2005 * - Outer IPv4 header checksum validation.
2006 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2M].
2007 * - Inner IPv4 header checksum validation.
2008 * - Set non zero checksum error code value
2010 rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2011 rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2012 BIT_ULL(32) | BIT_ULL(24) | BIT_ULL(6) |
2013 BIT_ULL(2) | BIT_ULL(1));
2015 rvu_npc_setup_interfaces(rvu, blkaddr);
2017 /* Configure MKEX profile */
2018 npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2020 err = npc_mcam_rsrcs_init(rvu, blkaddr);
2024 err = npc_flow_steering_init(rvu, blkaddr);
2027 "Incorrect mkex profile loaded using default mkex\n");
2028 npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2034 void rvu_npc_freemem(struct rvu *rvu)
2036 struct npc_pkind *pkind = &rvu->hw->pkind;
2037 struct npc_mcam *mcam = &rvu->hw->mcam;
2039 kfree(pkind->rsrc.bmap);
2040 kfree(mcam->counters.bmap);
2041 if (rvu->kpu_prfl_addr)
2042 iounmap(rvu->kpu_prfl_addr);
2044 kfree(rvu->kpu_fwdata);
2045 mutex_destroy(&mcam->lock);
2048 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2049 int blkaddr, int *alloc_cnt,
2052 struct npc_mcam *mcam = &rvu->hw->mcam;
2058 for (entry = 0; entry < mcam->bmap_entries; entry++) {
2059 if (mcam->entry2pfvf_map[entry] == pcifunc) {
2061 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2067 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2068 int blkaddr, int *alloc_cnt,
2071 struct npc_mcam *mcam = &rvu->hw->mcam;
2077 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2078 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2080 if (mcam->cntr_refcnt[cntr])
2086 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
2087 u16 pcifunc, int entry)
2089 /* verify AF installed entries */
2090 if (is_pffunc_af(pcifunc))
2092 /* Verify if entry is valid and if it is indeed
2093 * allocated to the requesting PFFUNC.
2095 if (entry >= mcam->bmap_entries)
2096 return NPC_MCAM_INVALID_REQ;
2098 if (pcifunc != mcam->entry2pfvf_map[entry])
2099 return NPC_MCAM_PERM_DENIED;
2104 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2105 u16 pcifunc, int cntr)
2107 /* Verify if counter is valid and if it is indeed
2108 * allocated to the requesting PFFUNC.
2110 if (cntr >= mcam->counters.max)
2111 return NPC_MCAM_INVALID_REQ;
2113 if (pcifunc != mcam->cntr2pfvf_map[cntr])
2114 return NPC_MCAM_PERM_DENIED;
2119 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2120 int blkaddr, u16 entry, u16 cntr)
2122 u16 index = entry & (mcam->banksize - 1);
2123 u16 bank = npc_get_bank(mcam, entry);
2125 /* Set mapping and increment counter's refcnt */
2126 mcam->entry2cntr_map[entry] = cntr;
2127 mcam->cntr_refcnt[cntr]++;
2129 * NPC_AF_MCAMEX_BANKX_STAT_ACT[14:12] - counter[11:9]
2130 * NPC_AF_MCAMEX_BANKX_STAT_ACT[8:0] - counter[8:0]
2132 rvu_write64(rvu, blkaddr,
2133 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2134 ((cntr >> 9) << 12) | BIT_ULL(9) | cntr);
2137 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2138 struct npc_mcam *mcam,
2139 int blkaddr, u16 entry, u16 cntr)
2141 u16 index = entry & (mcam->banksize - 1);
2142 u16 bank = npc_get_bank(mcam, entry);
2144 /* Remove mapping and reduce counter's refcnt */
2145 mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2146 mcam->cntr_refcnt[cntr]--;
2148 rvu_write64(rvu, blkaddr,
2149 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2152 /* Sets MCAM entry in bitmap as used. Update
2153 * reverse bitmap too. Should be called with
2154 * 'mcam->lock' held.
2156 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2161 rentry = mcam->bmap_entries - index - 1;
2163 __set_bit(entry, mcam->bmap);
2164 __set_bit(rentry, mcam->bmap_reverse);
2168 /* Sets MCAM entry in bitmap as free. Update
2169 * reverse bitmap too. Should be called with
2170 * 'mcam->lock' held.
2172 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2177 rentry = mcam->bmap_entries - index - 1;
2179 __clear_bit(entry, mcam->bmap);
2180 __clear_bit(rentry, mcam->bmap_reverse);
2184 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2185 int blkaddr, u16 pcifunc)
2189 /* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2190 for (index = 0; index < mcam->bmap_entries; index++) {
2191 if (mcam->entry2pfvf_map[index] == pcifunc) {
2192 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2193 /* Free the entry in bitmap */
2194 npc_mcam_clear_bit(mcam, index);
2195 /* Disable the entry */
2196 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2198 /* Update entry2counter mapping */
2199 cntr = mcam->entry2cntr_map[index];
2200 if (cntr != NPC_MCAM_INVALID_MAP)
2201 npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2204 mcam->entry2target_pffunc[index] = 0x0;
2209 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2214 /* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2215 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2216 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2217 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2218 mcam->cntr_refcnt[cntr] = 0;
2219 rvu_free_rsrc(&mcam->counters, cntr);
2220 /* This API is expected to be called after freeing
2221 * MCAM entries, which inturn will remove
2222 * 'entry to counter' mapping.
2223 * No need to do it again.
2229 /* Find area of contiguous free entries of size 'nr'.
2230 * If not found return max contiguous free entries available.
2232 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2233 u16 nr, u16 *max_area)
2235 u16 max_area_start = 0;
2236 u16 index, next, end;
2241 index = find_next_zero_bit(map, size, start);
2243 return max_area_start;
2245 end = ((index + nr) >= size) ? size : index + nr;
2246 next = find_next_bit(map, end, index);
2247 if (*max_area < (next - index)) {
2248 *max_area = next - index;
2249 max_area_start = index;
2257 return max_area_start;
2260 /* Find number of free MCAM entries available
2261 * within range i.e in between 'start' and 'end'.
2263 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2272 index = find_next_zero_bit(map, end, start);
2276 next = find_next_bit(map, end, index);
2278 fcnt += next - index;
2283 fcnt += end - index;
2288 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2289 struct npc_mcam_alloc_entry_req *req,
2290 u16 *start, u16 *end, bool *reverse)
2294 if (req->priority == NPC_MCAM_HIGHER_PRIO)
2297 /* For a low priority entry allocation
2298 * - If reference entry is not in hprio zone then
2299 * search range: ref_entry to end.
2300 * - If reference entry is in hprio zone and if
2301 * request can be accomodated in non-hprio zone then
2302 * search range: 'start of middle zone' to 'end'
2303 * - else search in reverse, so that less number of hprio
2304 * zone entries are allocated.
2308 *start = req->ref_entry + 1;
2309 *end = mcam->bmap_entries;
2311 if (req->ref_entry >= mcam->hprio_end)
2314 fcnt = npc_mcam_get_free_count(mcam->bmap,
2315 mcam->hprio_end, mcam->bmap_entries);
2316 if (fcnt > req->count)
2317 *start = mcam->hprio_end;
2323 /* For a high priority entry allocation, search is always
2324 * in reverse to preserve hprio zone entries.
2325 * - If reference entry is not in lprio zone then
2326 * search range: 0 to ref_entry.
2327 * - If reference entry is in lprio zone and if
2328 * request can be accomodated in middle zone then
2329 * search range: 'hprio_end' to 'lprio_start'
2334 *end = req->ref_entry;
2336 if (req->ref_entry <= mcam->lprio_start)
2339 fcnt = npc_mcam_get_free_count(mcam->bmap,
2340 mcam->hprio_end, mcam->lprio_start);
2341 if (fcnt < req->count)
2343 *start = mcam->hprio_end;
2344 *end = mcam->lprio_start;
2347 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2348 struct npc_mcam_alloc_entry_req *req,
2349 struct npc_mcam_alloc_entry_rsp *rsp)
2351 u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2352 u16 fcnt, hp_fcnt, lp_fcnt;
2353 u16 start, end, index;
2354 int entry, next_start;
2355 bool reverse = false;
2356 unsigned long *bmap;
2359 mutex_lock(&mcam->lock);
2361 /* Check if there are any free entries */
2362 if (!mcam->bmap_fcnt) {
2363 mutex_unlock(&mcam->lock);
2364 return NPC_MCAM_ALLOC_FAILED;
2367 /* MCAM entries are divided into high priority, middle and
2368 * low priority zones. Idea is to not allocate top and lower
2369 * most entries as much as possible, this is to increase
2370 * probability of honouring priority allocation requests.
2372 * Two bitmaps are used for mcam entry management,
2373 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
2374 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
2376 * Reverse bitmap is used to allocate entries
2377 * - when a higher priority entry is requested
2378 * - when available free entries are less.
2379 * Lower priority ones out of avaialble free entries are always
2380 * chosen when 'high vs low' question arises.
2383 /* Get the search range for priority allocation request */
2384 if (req->priority) {
2385 npc_get_mcam_search_range_priority(mcam, req,
2386 &start, &end, &reverse);
2390 /* Find out the search range for non-priority allocation request
2392 * Get MCAM free entry count in middle zone.
2394 lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2396 mcam->bmap_entries);
2397 hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2398 fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2400 /* Check if request can be accomodated in the middle zone */
2401 if (fcnt > req->count) {
2402 start = mcam->hprio_end;
2403 end = mcam->lprio_start;
2404 } else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2405 /* Expand search zone from half of hprio zone to
2406 * half of lprio zone.
2408 start = mcam->hprio_end / 2;
2409 end = mcam->bmap_entries - (mcam->lprio_count / 2);
2412 /* Not enough free entries, search all entries in reverse,
2413 * so that low priority ones will get used up.
2417 end = mcam->bmap_entries;
2422 bmap = mcam->bmap_reverse;
2423 start = mcam->bmap_entries - start;
2424 end = mcam->bmap_entries - end;
2433 /* Allocate requested number of contiguous entries, if
2434 * unsuccessful find max contiguous entries available.
2436 index = npc_mcam_find_zero_area(bmap, end, start,
2437 req->count, &max_contig);
2438 rsp->count = max_contig;
2440 rsp->entry = mcam->bmap_entries - index - max_contig;
2444 /* Allocate requested number of non-contiguous entries,
2445 * if unsuccessful allocate as many as possible.
2449 for (entry = 0; entry < req->count; entry++) {
2450 index = find_next_zero_bit(bmap, end, next_start);
2454 next_start = start + (index - start) + 1;
2456 /* Save the entry's index */
2458 index = mcam->bmap_entries - index - 1;
2459 entry_list[entry] = index;
2464 /* If allocating requested no of entries is unsucessful,
2465 * expand the search range to full bitmap length and retry.
2467 if (!req->priority && (rsp->count < req->count) &&
2468 ((end - start) != mcam->bmap_entries)) {
2471 end = mcam->bmap_entries;
2475 /* For priority entry allocation requests, if allocation is
2476 * failed then expand search to max possible range and retry.
2478 if (req->priority && rsp->count < req->count) {
2479 if (req->priority == NPC_MCAM_LOWER_PRIO &&
2480 (start != (req->ref_entry + 1))) {
2481 start = req->ref_entry + 1;
2482 end = mcam->bmap_entries;
2485 } else if ((req->priority == NPC_MCAM_HIGHER_PRIO) &&
2486 ((end - start) != req->ref_entry)) {
2488 end = req->ref_entry;
2494 /* Copy MCAM entry indices into mbox response entry_list.
2495 * Requester always expects indices in ascending order, so
2496 * so reverse the list if reverse bitmap is used for allocation.
2498 if (!req->contig && rsp->count) {
2500 for (entry = rsp->count - 1; entry >= 0; entry--) {
2502 rsp->entry_list[index++] = entry_list[entry];
2504 rsp->entry_list[entry] = entry_list[entry];
2508 /* Mark the allocated entries as used and set nixlf mapping */
2509 for (entry = 0; entry < rsp->count; entry++) {
2510 index = req->contig ?
2511 (rsp->entry + entry) : rsp->entry_list[entry];
2512 npc_mcam_set_bit(mcam, index);
2513 mcam->entry2pfvf_map[index] = pcifunc;
2514 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2517 /* Update available free count in mbox response */
2518 rsp->free_count = mcam->bmap_fcnt;
2520 mutex_unlock(&mcam->lock);
2524 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
2525 struct npc_mcam_alloc_entry_req *req,
2526 struct npc_mcam_alloc_entry_rsp *rsp)
2528 struct npc_mcam *mcam = &rvu->hw->mcam;
2529 u16 pcifunc = req->hdr.pcifunc;
2532 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2534 return NPC_MCAM_INVALID_REQ;
2536 rsp->entry = NPC_MCAM_ENTRY_INVALID;
2537 rsp->free_count = 0;
2539 /* Check if ref_entry is within range */
2540 if (req->priority && req->ref_entry >= mcam->bmap_entries) {
2541 dev_err(rvu->dev, "%s: reference entry %d is out of range\n",
2542 __func__, req->ref_entry);
2543 return NPC_MCAM_INVALID_REQ;
2546 /* ref_entry can't be '0' if requested priority is high.
2547 * Can't be last entry if requested priority is low.
2549 if ((!req->ref_entry && req->priority == NPC_MCAM_HIGHER_PRIO) ||
2550 ((req->ref_entry == (mcam->bmap_entries - 1)) &&
2551 req->priority == NPC_MCAM_LOWER_PRIO))
2552 return NPC_MCAM_INVALID_REQ;
2554 /* Since list of allocated indices needs to be sent to requester,
2555 * max number of non-contiguous entries per mbox msg is limited.
2557 if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES) {
2559 "%s: %d Non-contiguous MCAM entries requested is more than max (%d) allowed\n",
2560 __func__, req->count, NPC_MAX_NONCONTIG_ENTRIES);
2561 return NPC_MCAM_INVALID_REQ;
2564 /* Alloc request from PFFUNC with no NIXLF attached should be denied */
2565 if (!is_nixlf_attached(rvu, pcifunc))
2566 return NPC_MCAM_ALLOC_DENIED;
2568 return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
2571 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
2572 struct npc_mcam_free_entry_req *req,
2573 struct msg_rsp *rsp)
2575 struct npc_mcam *mcam = &rvu->hw->mcam;
2576 u16 pcifunc = req->hdr.pcifunc;
2577 int blkaddr, rc = 0;
2580 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2582 return NPC_MCAM_INVALID_REQ;
2584 /* Free request from PFFUNC with no NIXLF attached, ignore */
2585 if (!is_nixlf_attached(rvu, pcifunc))
2586 return NPC_MCAM_INVALID_REQ;
2588 mutex_lock(&mcam->lock);
2593 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2597 mcam->entry2pfvf_map[req->entry] = 0;
2598 mcam->entry2target_pffunc[req->entry] = 0x0;
2599 npc_mcam_clear_bit(mcam, req->entry);
2600 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2602 /* Update entry2counter mapping */
2603 cntr = mcam->entry2cntr_map[req->entry];
2604 if (cntr != NPC_MCAM_INVALID_MAP)
2605 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2611 /* Free up all entries allocated to requesting PFFUNC */
2612 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
2614 mutex_unlock(&mcam->lock);
2618 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
2619 struct npc_mcam_read_entry_req *req,
2620 struct npc_mcam_read_entry_rsp *rsp)
2622 struct npc_mcam *mcam = &rvu->hw->mcam;
2623 u16 pcifunc = req->hdr.pcifunc;
2626 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2628 return NPC_MCAM_INVALID_REQ;
2630 mutex_lock(&mcam->lock);
2631 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2633 npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
2635 &rsp->intf, &rsp->enable);
2638 mutex_unlock(&mcam->lock);
2642 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
2643 struct npc_mcam_write_entry_req *req,
2644 struct msg_rsp *rsp)
2646 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2647 struct npc_mcam *mcam = &rvu->hw->mcam;
2648 u16 pcifunc = req->hdr.pcifunc;
2649 u16 channel, chan_mask;
2653 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2655 return NPC_MCAM_INVALID_REQ;
2657 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
2658 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
2659 channel &= chan_mask;
2661 mutex_lock(&mcam->lock);
2662 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2666 if (req->set_cntr &&
2667 npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2668 rc = NPC_MCAM_INVALID_REQ;
2672 if (!is_npc_interface_valid(rvu, req->intf)) {
2673 rc = NPC_MCAM_INVALID_REQ;
2677 if (is_npc_intf_tx(req->intf))
2678 nix_intf = pfvf->nix_tx_intf;
2680 nix_intf = pfvf->nix_rx_intf;
2682 if (npc_mcam_verify_channel(rvu, pcifunc, req->intf, channel)) {
2683 rc = NPC_MCAM_INVALID_REQ;
2687 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
2689 rc = NPC_MCAM_INVALID_REQ;
2693 /* For AF installed rules, the nix_intf should be set to target NIX */
2694 if (is_pffunc_af(req->hdr.pcifunc))
2695 nix_intf = req->intf;
2697 npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2698 &req->entry_data, req->enable_entry);
2701 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2702 req->entry, req->cntr);
2706 mutex_unlock(&mcam->lock);
2710 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2711 struct npc_mcam_ena_dis_entry_req *req,
2712 struct msg_rsp *rsp)
2714 struct npc_mcam *mcam = &rvu->hw->mcam;
2715 u16 pcifunc = req->hdr.pcifunc;
2718 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2720 return NPC_MCAM_INVALID_REQ;
2722 mutex_lock(&mcam->lock);
2723 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2724 mutex_unlock(&mcam->lock);
2728 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2733 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2734 struct npc_mcam_ena_dis_entry_req *req,
2735 struct msg_rsp *rsp)
2737 struct npc_mcam *mcam = &rvu->hw->mcam;
2738 u16 pcifunc = req->hdr.pcifunc;
2741 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2743 return NPC_MCAM_INVALID_REQ;
2745 mutex_lock(&mcam->lock);
2746 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2747 mutex_unlock(&mcam->lock);
2751 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2756 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2757 struct npc_mcam_shift_entry_req *req,
2758 struct npc_mcam_shift_entry_rsp *rsp)
2760 struct npc_mcam *mcam = &rvu->hw->mcam;
2761 u16 pcifunc = req->hdr.pcifunc;
2762 u16 old_entry, new_entry;
2766 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2768 return NPC_MCAM_INVALID_REQ;
2770 if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2771 return NPC_MCAM_INVALID_REQ;
2773 mutex_lock(&mcam->lock);
2774 for (index = 0; index < req->shift_count; index++) {
2775 old_entry = req->curr_entry[index];
2776 new_entry = req->new_entry[index];
2778 /* Check if both old and new entries are valid and
2779 * does belong to this PFFUNC or not.
2781 rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2785 rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2789 /* new_entry should not have a counter mapped */
2790 if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2791 rc = NPC_MCAM_PERM_DENIED;
2795 /* Disable the new_entry */
2796 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2798 /* Copy rule from old entry to new entry */
2799 npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2801 /* Copy counter mapping, if any */
2802 cntr = mcam->entry2cntr_map[old_entry];
2803 if (cntr != NPC_MCAM_INVALID_MAP) {
2804 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2806 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2810 /* Enable new_entry and disable old_entry */
2811 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2812 npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2815 /* If shift has failed then report the failed index */
2816 if (index != req->shift_count) {
2817 rc = NPC_MCAM_PERM_DENIED;
2818 rsp->failed_entry_idx = index;
2821 mutex_unlock(&mcam->lock);
2825 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2826 struct npc_mcam_alloc_counter_req *req,
2827 struct npc_mcam_alloc_counter_rsp *rsp)
2829 struct npc_mcam *mcam = &rvu->hw->mcam;
2830 u16 pcifunc = req->hdr.pcifunc;
2831 u16 max_contig, cntr;
2834 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2836 return NPC_MCAM_INVALID_REQ;
2838 /* If the request is from a PFFUNC with no NIXLF attached, ignore */
2839 if (!is_nixlf_attached(rvu, pcifunc))
2840 return NPC_MCAM_INVALID_REQ;
2842 /* Since list of allocated counter IDs needs to be sent to requester,
2843 * max number of non-contiguous counters per mbox msg is limited.
2845 if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2846 return NPC_MCAM_INVALID_REQ;
2848 mutex_lock(&mcam->lock);
2850 /* Check if unused counters are available or not */
2851 if (!rvu_rsrc_free_count(&mcam->counters)) {
2852 mutex_unlock(&mcam->lock);
2853 return NPC_MCAM_ALLOC_FAILED;
2859 /* Allocate requested number of contiguous counters, if
2860 * unsuccessful find max contiguous entries available.
2862 index = npc_mcam_find_zero_area(mcam->counters.bmap,
2863 mcam->counters.max, 0,
2864 req->count, &max_contig);
2865 rsp->count = max_contig;
2867 for (cntr = index; cntr < (index + max_contig); cntr++) {
2868 __set_bit(cntr, mcam->counters.bmap);
2869 mcam->cntr2pfvf_map[cntr] = pcifunc;
2872 /* Allocate requested number of non-contiguous counters,
2873 * if unsuccessful allocate as many as possible.
2875 for (cntr = 0; cntr < req->count; cntr++) {
2876 index = rvu_alloc_rsrc(&mcam->counters);
2879 rsp->cntr_list[cntr] = index;
2881 mcam->cntr2pfvf_map[index] = pcifunc;
2885 mutex_unlock(&mcam->lock);
2889 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2890 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2892 struct npc_mcam *mcam = &rvu->hw->mcam;
2893 u16 index, entry = 0;
2896 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2898 return NPC_MCAM_INVALID_REQ;
2900 mutex_lock(&mcam->lock);
2901 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2903 mutex_unlock(&mcam->lock);
2907 /* Mark counter as free/unused */
2908 mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2909 rvu_free_rsrc(&mcam->counters, req->cntr);
2911 /* Disable all MCAM entry's stats which are using this counter */
2912 while (entry < mcam->bmap_entries) {
2913 if (!mcam->cntr_refcnt[req->cntr])
2916 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2917 if (index >= mcam->bmap_entries)
2920 if (mcam->entry2cntr_map[index] != req->cntr)
2923 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2927 mutex_unlock(&mcam->lock);
2931 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
2932 struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
2934 struct npc_mcam *mcam = &rvu->hw->mcam;
2935 u16 index, entry = 0;
2938 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2940 return NPC_MCAM_INVALID_REQ;
2942 mutex_lock(&mcam->lock);
2943 rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2947 /* Unmap the MCAM entry and counter */
2949 rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
2952 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2953 req->entry, req->cntr);
2957 /* Disable all MCAM entry's stats which are using this counter */
2958 while (entry < mcam->bmap_entries) {
2959 if (!mcam->cntr_refcnt[req->cntr])
2962 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2963 if (index >= mcam->bmap_entries)
2965 if (mcam->entry2cntr_map[index] != req->cntr)
2969 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2973 mutex_unlock(&mcam->lock);
2977 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
2978 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2980 struct npc_mcam *mcam = &rvu->hw->mcam;
2983 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2985 return NPC_MCAM_INVALID_REQ;
2987 mutex_lock(&mcam->lock);
2988 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2989 mutex_unlock(&mcam->lock);
2993 rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
2998 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
2999 struct npc_mcam_oper_counter_req *req,
3000 struct npc_mcam_oper_counter_rsp *rsp)
3002 struct npc_mcam *mcam = &rvu->hw->mcam;
3005 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3007 return NPC_MCAM_INVALID_REQ;
3009 mutex_lock(&mcam->lock);
3010 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3011 mutex_unlock(&mcam->lock);
3015 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
3016 rsp->stat &= BIT_ULL(48) - 1;
3021 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
3022 struct npc_mcam_alloc_and_write_entry_req *req,
3023 struct npc_mcam_alloc_and_write_entry_rsp *rsp)
3025 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3026 struct npc_mcam_alloc_counter_req cntr_req;
3027 struct npc_mcam_alloc_counter_rsp cntr_rsp;
3028 struct npc_mcam_alloc_entry_req entry_req;
3029 struct npc_mcam_alloc_entry_rsp entry_rsp;
3030 struct npc_mcam *mcam = &rvu->hw->mcam;
3031 u16 entry = NPC_MCAM_ENTRY_INVALID;
3032 u16 cntr = NPC_MCAM_ENTRY_INVALID;
3033 u16 channel, chan_mask;
3037 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3039 return NPC_MCAM_INVALID_REQ;
3041 if (!is_npc_interface_valid(rvu, req->intf))
3042 return NPC_MCAM_INVALID_REQ;
3044 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
3045 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
3046 channel &= chan_mask;
3048 if (npc_mcam_verify_channel(rvu, req->hdr.pcifunc, req->intf, channel))
3049 return NPC_MCAM_INVALID_REQ;
3051 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
3053 return NPC_MCAM_INVALID_REQ;
3055 /* Try to allocate a MCAM entry */
3056 entry_req.hdr.pcifunc = req->hdr.pcifunc;
3057 entry_req.contig = true;
3058 entry_req.priority = req->priority;
3059 entry_req.ref_entry = req->ref_entry;
3060 entry_req.count = 1;
3062 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
3063 &entry_req, &entry_rsp);
3067 if (!entry_rsp.count)
3068 return NPC_MCAM_ALLOC_FAILED;
3070 entry = entry_rsp.entry;
3072 if (!req->alloc_cntr)
3075 /* Now allocate counter */
3076 cntr_req.hdr.pcifunc = req->hdr.pcifunc;
3077 cntr_req.contig = true;
3080 rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3082 /* Free allocated MCAM entry */
3083 mutex_lock(&mcam->lock);
3084 mcam->entry2pfvf_map[entry] = 0;
3085 npc_mcam_clear_bit(mcam, entry);
3086 mutex_unlock(&mcam->lock);
3090 cntr = cntr_rsp.cntr;
3093 mutex_lock(&mcam->lock);
3095 if (is_npc_intf_tx(req->intf))
3096 nix_intf = pfvf->nix_tx_intf;
3098 nix_intf = pfvf->nix_rx_intf;
3100 npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
3101 &req->entry_data, req->enable_entry);
3103 if (req->alloc_cntr)
3104 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
3105 mutex_unlock(&mcam->lock);
3113 #define GET_KEX_CFG(intf) \
3114 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
3116 #define GET_KEX_FLAGS(ld) \
3117 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
3119 #define GET_KEX_LD(intf, lid, lt, ld) \
3120 rvu_read64(rvu, BLKADDR_NPC, \
3121 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
3123 #define GET_KEX_LDFLAGS(intf, ld, fl) \
3124 rvu_read64(rvu, BLKADDR_NPC, \
3125 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
3127 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
3128 struct npc_get_kex_cfg_rsp *rsp)
3130 int lid, lt, ld, fl;
3132 rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3133 rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3134 for (lid = 0; lid < NPC_MAX_LID; lid++) {
3135 for (lt = 0; lt < NPC_MAX_LT; lt++) {
3136 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3137 rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3138 GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3139 rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3140 GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3144 for (ld = 0; ld < NPC_MAX_LD; ld++)
3145 rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3147 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3148 for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3149 rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3150 GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3151 rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3152 GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3155 memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3159 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3160 struct msg_req *req,
3161 struct npc_mcam_read_base_rule_rsp *rsp)
3163 struct npc_mcam *mcam = &rvu->hw->mcam;
3164 int index, blkaddr, nixlf, rc = 0;
3165 u16 pcifunc = req->hdr.pcifunc;
3166 struct rvu_pfvf *pfvf;
3169 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3171 return NPC_MCAM_INVALID_REQ;
3173 /* Return the channel number in case of PF */
3174 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3175 pfvf = rvu_get_pfvf(rvu, pcifunc);
3176 rsp->entry.kw[0] = pfvf->rx_chan_base;
3177 rsp->entry.kw_mask[0] = 0xFFFULL;
3181 /* Find the pkt steering rule installed by PF to this VF */
3182 mutex_lock(&mcam->lock);
3183 for (index = 0; index < mcam->bmap_entries; index++) {
3184 if (mcam->entry2target_pffunc[index] == pcifunc)
3188 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3190 mutex_unlock(&mcam->lock);
3193 /* Read the default ucast entry if there is no pkt steering rule */
3194 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3197 /* Read the mcam entry */
3198 npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3200 mutex_unlock(&mcam->lock);
3205 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3206 struct npc_mcam_get_stats_req *req,
3207 struct npc_mcam_get_stats_rsp *rsp)
3209 struct npc_mcam *mcam = &rvu->hw->mcam;
3215 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3217 return NPC_MCAM_INVALID_REQ;
3219 mutex_lock(&mcam->lock);
3221 index = req->entry & (mcam->banksize - 1);
3222 bank = npc_get_bank(mcam, req->entry);
3224 /* read MCAM entry STAT_ACT register */
3225 regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3227 if (!(regval & BIT_ULL(9))) {
3229 mutex_unlock(&mcam->lock);
3233 cntr = regval & 0x1FF;
3236 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3237 rsp->stat &= BIT_ULL(48) - 1;
3239 mutex_unlock(&mcam->lock);