1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
4 * Copyright (C) 2018 Marvell.
8 #include <linux/bitfield.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
12 #include "rvu_struct.h"
17 #include "npc_profile.h"
19 #define RSVD_MCAM_ENTRIES_PER_PF 3 /* Broadcast, Promisc and AllMulticast */
20 #define RSVD_MCAM_ENTRIES_PER_NIXLF 1 /* Ucast for LFs */
22 #define NPC_PARSE_RESULT_DMAC_OFFSET 8
23 #define NPC_HW_TSTAMP_OFFSET 8ULL
24 #define NPC_KEX_CHAN_MASK 0xFFFULL
25 #define NPC_KEX_PF_FUNC_MASK 0xFFFFULL
27 #define ALIGN_8B_CEIL(__a) (((__a) + 7) & (-8))
29 static const char def_pfl_name[] = "default";
31 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
32 int blkaddr, u16 pcifunc);
33 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
36 bool is_npc_intf_tx(u8 intf)
38 return !!(intf & 0x1);
41 bool is_npc_intf_rx(u8 intf)
46 bool is_npc_interface_valid(struct rvu *rvu, u8 intf)
48 struct rvu_hwinfo *hw = rvu->hw;
50 return intf < hw->npc_intfs;
53 int rvu_npc_get_tx_nibble_cfg(struct rvu *rvu, u64 nibble_ena)
55 /* Due to a HW issue in these silicon versions, parse nibble enable
56 * configuration has to be identical for both Rx and Tx interfaces.
58 if (is_rvu_96xx_B0(rvu))
63 static int npc_mcam_verify_pf_func(struct rvu *rvu,
64 struct mcam_entry *entry_data, u8 intf,
67 u16 pf_func, pf_func_mask;
69 if (is_npc_intf_rx(intf))
72 pf_func_mask = (entry_data->kw_mask[0] >> 32) &
74 pf_func = (entry_data->kw[0] >> 32) & NPC_KEX_PF_FUNC_MASK;
76 pf_func = be16_to_cpu((__force __be16)pf_func);
77 if (pf_func_mask != NPC_KEX_PF_FUNC_MASK ||
78 ((pf_func & ~RVU_PFVF_FUNC_MASK) !=
79 (pcifunc & ~RVU_PFVF_FUNC_MASK)))
85 void rvu_npc_set_pkind(struct rvu *rvu, int pkind, struct rvu_pfvf *pfvf)
90 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
94 /* Config CPI base for the PKIND */
95 val = pkind | 1ULL << 62;
96 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_CPI_DEFX(pkind, 0), val);
99 int rvu_npc_get_pkind(struct rvu *rvu, u16 pf)
101 struct npc_pkind *pkind = &rvu->hw->pkind;
105 for (i = 0; i < pkind->rsrc.max; i++) {
106 map = pkind->pfchan_map[i];
107 if (((map >> 16) & 0x3F) == pf)
113 #define NPC_AF_ACTION0_PTR_ADVANCE GENMASK_ULL(27, 20)
115 int npc_config_ts_kpuaction(struct rvu *rvu, int pf, u16 pcifunc, bool enable)
120 pkind = rvu_npc_get_pkind(rvu, pf);
122 dev_err(rvu->dev, "%s: pkind not mapped\n", __func__);
126 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
128 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
132 val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
133 val &= ~NPC_AF_ACTION0_PTR_ADVANCE;
134 /* If timestamp is enabled then configure NPC to shift 8 bytes */
136 val |= FIELD_PREP(NPC_AF_ACTION0_PTR_ADVANCE,
137 NPC_HW_TSTAMP_OFFSET);
138 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
143 static int npc_get_ucast_mcam_index(struct npc_mcam *mcam, u16 pcifunc,
146 struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
147 struct rvu *rvu = hw->rvu;
148 int blkaddr = 0, max = 0;
149 struct rvu_block *block;
150 struct rvu_pfvf *pfvf;
152 pfvf = rvu_get_pfvf(rvu, pcifunc);
153 /* Given a PF/VF and NIX LF number calculate the unicast mcam
154 * entry index based on the NIX block assigned to the PF/VF.
156 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
158 if (pfvf->nix_blkaddr == blkaddr)
160 block = &rvu->hw->block[blkaddr];
161 max += block->lf.max;
162 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
165 return mcam->nixlf_offset + (max + nixlf) * RSVD_MCAM_ENTRIES_PER_NIXLF;
168 int npc_get_nixlf_mcam_index(struct npc_mcam *mcam,
169 u16 pcifunc, int nixlf, int type)
171 int pf = rvu_get_pf(pcifunc);
174 /* Check if this is for a PF */
175 if (pf && !(pcifunc & RVU_PFVF_FUNC_MASK)) {
176 /* Reserved entries exclude PF0 */
178 index = mcam->pf_offset + (pf * RSVD_MCAM_ENTRIES_PER_PF);
179 /* Broadcast address matching entry should be first so
180 * that the packet can be replicated to all VFs.
182 if (type == NIXLF_BCAST_ENTRY)
184 else if (type == NIXLF_ALLMULTI_ENTRY)
186 else if (type == NIXLF_PROMISC_ENTRY)
190 return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
193 int npc_get_bank(struct npc_mcam *mcam, int index)
195 int bank = index / mcam->banksize;
197 /* 0,1 & 2,3 banks are combined for this keysize */
198 if (mcam->keysize == NPC_MCAM_KEY_X2)
204 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
205 int blkaddr, int index)
207 int bank = npc_get_bank(mcam, index);
210 index &= (mcam->banksize - 1);
211 cfg = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_CFG(index, bank));
215 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
216 int blkaddr, int index, bool enable)
218 int bank = npc_get_bank(mcam, index);
221 index &= (mcam->banksize - 1);
222 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
223 rvu_write64(rvu, blkaddr,
224 NPC_AF_MCAMEX_BANKX_CFG(index, bank),
229 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
230 int blkaddr, int index)
232 int bank = npc_get_bank(mcam, index);
235 index &= (mcam->banksize - 1);
236 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
237 rvu_write64(rvu, blkaddr,
238 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
239 rvu_write64(rvu, blkaddr,
240 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
242 rvu_write64(rvu, blkaddr,
243 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
244 rvu_write64(rvu, blkaddr,
245 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
247 rvu_write64(rvu, blkaddr,
248 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
249 rvu_write64(rvu, blkaddr,
250 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
254 static void npc_get_keyword(struct mcam_entry *entry, int idx,
255 u64 *cam0, u64 *cam1)
259 #define CAM_MASK(n) (BIT_ULL(n) - 1)
261 /* 0, 2, 4, 6 indices refer to BANKX_CAMX_W0 and
262 * 1, 3, 5, 7 indices refer to BANKX_CAMX_W1.
264 * Also, only 48 bits of BANKX_CAMX_W1 are valid.
268 /* BANK(X)_CAM_W0<63:0> = MCAM_KEY[KW0]<63:0> */
269 *cam1 = entry->kw[0];
270 kw_mask = entry->kw_mask[0];
273 /* BANK(X)_CAM_W1<47:0> = MCAM_KEY[KW1]<47:0> */
274 *cam1 = entry->kw[1] & CAM_MASK(48);
275 kw_mask = entry->kw_mask[1] & CAM_MASK(48);
278 /* BANK(X + 1)_CAM_W0<15:0> = MCAM_KEY[KW1]<63:48>
279 * BANK(X + 1)_CAM_W0<63:16> = MCAM_KEY[KW2]<47:0>
281 *cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
282 *cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
283 kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
284 kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
287 /* BANK(X + 1)_CAM_W1<15:0> = MCAM_KEY[KW2]<63:48>
288 * BANK(X + 1)_CAM_W1<47:16> = MCAM_KEY[KW3]<31:0>
290 *cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
291 *cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
292 kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
293 kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
296 /* BANK(X + 2)_CAM_W0<31:0> = MCAM_KEY[KW3]<63:32>
297 * BANK(X + 2)_CAM_W0<63:32> = MCAM_KEY[KW4]<31:0>
299 *cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
300 *cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
301 kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
302 kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
305 /* BANK(X + 2)_CAM_W1<31:0> = MCAM_KEY[KW4]<63:32>
306 * BANK(X + 2)_CAM_W1<47:32> = MCAM_KEY[KW5]<15:0>
308 *cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
309 *cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
310 kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
311 kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
314 /* BANK(X + 3)_CAM_W0<47:0> = MCAM_KEY[KW5]<63:16>
315 * BANK(X + 3)_CAM_W0<63:48> = MCAM_KEY[KW6]<15:0>
317 *cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
318 *cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
319 kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
320 kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
323 /* BANK(X + 3)_CAM_W1<47:0> = MCAM_KEY[KW6]<63:16> */
324 *cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
325 kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
330 *cam0 = ~*cam1 & kw_mask;
333 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
336 /* Similar to npc_get_keyword, but fills mcam_entry structure from
342 entry->kw_mask[0] = cam1 ^ cam0;
346 entry->kw_mask[1] = cam1 ^ cam0;
349 entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
350 entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
351 entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
352 entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
355 entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
356 entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
357 entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
358 entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
361 entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
362 entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
363 entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
364 entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
367 entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
368 entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
369 entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
370 entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
373 entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
374 entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
375 entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
376 entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
379 entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
380 entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
385 static u64 npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
386 int blkaddr, u16 pf_func)
388 int bank, nixlf, index;
390 /* get ucast entry rule entry index */
391 nix_get_nixlf(rvu, pf_func, &nixlf, NULL);
392 index = npc_get_nixlf_mcam_index(mcam, pf_func, nixlf,
394 bank = npc_get_bank(mcam, index);
395 index &= (mcam->banksize - 1);
397 return rvu_read64(rvu, blkaddr,
398 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
401 static void npc_fixup_vf_rule(struct rvu *rvu, struct npc_mcam *mcam,
402 int blkaddr, int index, struct mcam_entry *entry,
405 u16 owner, target_func;
406 struct rvu_pfvf *pfvf;
409 owner = mcam->entry2pfvf_map[index];
410 target_func = (entry->action >> 4) & 0xffff;
411 /* do nothing when target is LBK/PF or owner is not PF */
412 if (is_pffunc_af(owner) || is_afvf(target_func) ||
413 (owner & RVU_PFVF_FUNC_MASK) ||
414 !(target_func & RVU_PFVF_FUNC_MASK))
417 /* save entry2target_pffunc */
418 pfvf = rvu_get_pfvf(rvu, target_func);
419 mcam->entry2target_pffunc[index] = target_func;
421 /* don't enable rule when nixlf not attached or initialized */
422 if (!(is_nixlf_attached(rvu, target_func) &&
423 test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
426 /* copy VF default entry action to the VF mcam entry */
427 rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
430 entry->action = rx_action;
433 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
434 int blkaddr, int index, u8 intf,
435 struct mcam_entry *entry, bool enable)
437 int bank = npc_get_bank(mcam, index);
438 int kw = 0, actbank, actindex;
439 u8 tx_intf_mask = ~intf & 0x3;
443 actbank = bank; /* Save bank id, to set action later on */
445 index &= (mcam->banksize - 1);
447 /* Disable before mcam entry update */
448 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
450 /* Clear mcam entry to avoid writes being suppressed by NPC */
451 npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
453 /* CAM1 takes the comparison value and
454 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
455 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
456 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
457 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
459 for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
460 /* Interface should be set in all banks */
461 if (is_npc_intf_tx(intf)) {
462 /* Last bit must be set and rest don't care
466 tx_intf = intf & tx_intf_mask;
467 tx_intf_mask = ~tx_intf & tx_intf_mask;
470 rvu_write64(rvu, blkaddr,
471 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
473 rvu_write64(rvu, blkaddr,
474 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
477 /* Set the match key */
478 npc_get_keyword(entry, kw, &cam0, &cam1);
479 rvu_write64(rvu, blkaddr,
480 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
481 rvu_write64(rvu, blkaddr,
482 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
484 npc_get_keyword(entry, kw + 1, &cam0, &cam1);
485 rvu_write64(rvu, blkaddr,
486 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
487 rvu_write64(rvu, blkaddr,
488 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
491 /* PF installing VF rule */
492 if (intf == NIX_INTF_RX && actindex < mcam->bmap_entries)
493 npc_fixup_vf_rule(rvu, mcam, blkaddr, index, entry, &enable);
496 rvu_write64(rvu, blkaddr,
497 NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
499 /* Set TAG 'action' */
500 rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
503 /* Enable the entry */
505 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
508 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
509 int blkaddr, u16 src,
510 struct mcam_entry *entry, u8 *intf, u8 *ena)
512 int sbank = npc_get_bank(mcam, src);
516 src &= (mcam->banksize - 1);
519 for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
520 cam1 = rvu_read64(rvu, blkaddr,
521 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
522 cam0 = rvu_read64(rvu, blkaddr,
523 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
524 npc_fill_entryword(entry, kw, cam0, cam1);
526 cam1 = rvu_read64(rvu, blkaddr,
527 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
528 cam0 = rvu_read64(rvu, blkaddr,
529 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
530 npc_fill_entryword(entry, kw + 1, cam0, cam1);
533 entry->action = rvu_read64(rvu, blkaddr,
534 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
536 rvu_read64(rvu, blkaddr,
537 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
538 *intf = rvu_read64(rvu, blkaddr,
539 NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
540 *ena = rvu_read64(rvu, blkaddr,
541 NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
544 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
545 int blkaddr, u16 src, u16 dest)
547 int dbank = npc_get_bank(mcam, dest);
548 int sbank = npc_get_bank(mcam, src);
552 src &= (mcam->banksize - 1);
553 dest &= (mcam->banksize - 1);
555 /* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
556 for (bank = 0; bank < mcam->banks_per_entry; bank++) {
557 sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
558 dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
559 for (i = 0; i < 6; i++) {
560 cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
561 rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
566 cfg = rvu_read64(rvu, blkaddr,
567 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
568 rvu_write64(rvu, blkaddr,
569 NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
571 /* Copy TAG action */
572 cfg = rvu_read64(rvu, blkaddr,
573 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
574 rvu_write64(rvu, blkaddr,
575 NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
577 /* Enable or disable */
578 cfg = rvu_read64(rvu, blkaddr,
579 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
580 rvu_write64(rvu, blkaddr,
581 NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
584 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
585 int blkaddr, int index)
587 int bank = npc_get_bank(mcam, index);
589 index &= (mcam->banksize - 1);
590 return rvu_read64(rvu, blkaddr,
591 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
594 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
595 int nixlf, u64 chan, u8 *mac_addr)
597 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
598 struct npc_install_flow_req req = { 0 };
599 struct npc_install_flow_rsp rsp = { 0 };
600 struct npc_mcam *mcam = &rvu->hw->mcam;
601 struct nix_rx_action action;
604 /* AF's and SDP VFs work in promiscuous mode */
605 if (is_afvf(pcifunc) || is_sdp_vf(pcifunc))
608 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
612 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
613 nixlf, NIXLF_UCAST_ENTRY);
615 /* Don't change the action if entry is already enabled
616 * Otherwise RSS action may get overwritten.
618 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
619 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
622 *(u64 *)&action = 0x00;
623 action.op = NIX_RX_ACTIONOP_UCAST;
624 action.pf_func = pcifunc;
627 req.default_rule = 1;
628 ether_addr_copy(req.packet.dmac, mac_addr);
629 eth_broadcast_addr((u8 *)&req.mask.dmac);
630 req.features = BIT_ULL(NPC_DMAC);
632 req.chan_mask = 0xFFFU;
633 req.intf = pfvf->nix_rx_intf;
635 req.hdr.pcifunc = 0; /* AF is requester */
636 req.vf = action.pf_func;
637 req.index = action.index;
638 req.match_id = action.match_id;
639 req.flow_key_alg = action.flow_key_alg;
641 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
644 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
645 int nixlf, u64 chan, u8 chan_cnt)
647 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
648 struct npc_install_flow_req req = { 0 };
649 struct npc_install_flow_rsp rsp = { 0 };
650 struct npc_mcam *mcam = &rvu->hw->mcam;
651 struct rvu_hwinfo *hw = rvu->hw;
652 int blkaddr, ucast_idx, index;
653 struct nix_rx_action action;
656 if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
659 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
663 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
664 nixlf, NIXLF_PROMISC_ENTRY);
666 if (is_cgx_vf(rvu, pcifunc))
667 index = npc_get_nixlf_mcam_index(mcam,
668 pcifunc & ~RVU_PFVF_FUNC_MASK,
669 nixlf, NIXLF_PROMISC_ENTRY);
671 /* If the corresponding PF's ucast action is RSS,
672 * use the same action for promisc also
674 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
675 nixlf, NIXLF_UCAST_ENTRY);
676 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
677 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
680 if (action.op != NIX_RX_ACTIONOP_RSS) {
681 *(u64 *)&action = 0x00;
682 action.op = NIX_RX_ACTIONOP_UCAST;
685 /* RX_ACTION set to MCAST for CGX PF's */
686 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
687 is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
688 *(u64 *)&action = 0x00;
689 action.op = NIX_RX_ACTIONOP_MCAST;
690 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
691 action.index = pfvf->promisc_mce_idx;
694 /* For cn10k the upper two bits of the channel number are
695 * cpt channel number. with masking out these bits in the
696 * mcam entry, same entry used for NIX will allow packets
697 * received from cpt for parsing.
699 if (!is_rvu_otx2(rvu)) {
700 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
702 req.chan_mask = 0xFFFU;
706 if (!is_power_of_2(chan_cnt)) {
708 "%s: channel count more than 1, must be power of 2\n", __func__);
711 relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
713 req.chan_mask &= relaxed_mask;
717 req.intf = pfvf->nix_rx_intf;
720 req.hdr.pcifunc = 0; /* AF is requester */
722 req.index = action.index;
723 req.match_id = action.match_id;
724 req.flow_key_alg = action.flow_key_alg;
726 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
729 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
730 int nixlf, bool enable)
732 struct npc_mcam *mcam = &rvu->hw->mcam;
735 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
739 /* Get 'pcifunc' of PF device */
740 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
742 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
743 nixlf, NIXLF_PROMISC_ENTRY);
744 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
747 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
750 struct rvu_pfvf *pfvf;
751 struct npc_install_flow_req req = { 0 };
752 struct npc_install_flow_rsp rsp = { 0 };
753 struct npc_mcam *mcam = &rvu->hw->mcam;
754 struct rvu_hwinfo *hw = rvu->hw;
757 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
762 if (is_afvf(pcifunc))
765 /* If pkt replication is not supported,
766 * then only PF is allowed to add a bcast match entry.
768 if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
771 /* Get 'pcifunc' of PF device */
772 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
773 pfvf = rvu_get_pfvf(rvu, pcifunc);
774 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
775 nixlf, NIXLF_BCAST_ENTRY);
777 if (!hw->cap.nix_rx_multicast) {
778 /* Early silicon doesn't support pkt replication,
779 * so install entry with UCAST action, so that PF
780 * receives all broadcast packets.
782 req.op = NIX_RX_ACTIONOP_UCAST;
784 req.op = NIX_RX_ACTIONOP_MCAST;
785 req.index = pfvf->bcast_mce_idx;
788 eth_broadcast_addr((u8 *)&req.packet.dmac);
789 eth_broadcast_addr((u8 *)&req.mask.dmac);
790 req.features = BIT_ULL(NPC_DMAC);
792 req.chan_mask = 0xFFFU;
793 req.intf = pfvf->nix_rx_intf;
795 req.hdr.pcifunc = 0; /* AF is requester */
798 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
801 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
804 struct npc_mcam *mcam = &rvu->hw->mcam;
807 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
811 /* Get 'pcifunc' of PF device */
812 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
814 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
816 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
819 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
822 struct npc_install_flow_req req = { 0 };
823 struct npc_install_flow_rsp rsp = { 0 };
824 struct npc_mcam *mcam = &rvu->hw->mcam;
825 struct rvu_hwinfo *hw = rvu->hw;
826 int blkaddr, ucast_idx, index;
827 u8 mac_addr[ETH_ALEN] = { 0 };
828 struct nix_rx_action action;
829 struct rvu_pfvf *pfvf;
832 /* Only CGX PF/VF can add allmulticast entry */
833 if (is_afvf(pcifunc) && is_sdp_vf(pcifunc))
836 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
840 /* Get 'pcifunc' of PF device */
841 vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
842 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
843 pfvf = rvu_get_pfvf(rvu, pcifunc);
844 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
845 nixlf, NIXLF_ALLMULTI_ENTRY);
847 /* If the corresponding PF's ucast action is RSS,
848 * use the same action for multicast entry also
850 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
851 nixlf, NIXLF_UCAST_ENTRY);
852 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
853 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
856 if (action.op != NIX_RX_ACTIONOP_RSS) {
857 *(u64 *)&action = 0x00;
858 action.op = NIX_RX_ACTIONOP_UCAST;
859 action.pf_func = pcifunc;
862 /* RX_ACTION set to MCAST for CGX PF's */
863 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
864 *(u64 *)&action = 0x00;
865 action.op = NIX_RX_ACTIONOP_MCAST;
866 action.index = pfvf->mcast_mce_idx;
869 mac_addr[0] = 0x01; /* LSB bit of 1st byte in DMAC */
870 ether_addr_copy(req.packet.dmac, mac_addr);
871 ether_addr_copy(req.mask.dmac, mac_addr);
872 req.features = BIT_ULL(NPC_DMAC);
874 /* For cn10k the upper two bits of the channel number are
875 * cpt channel number. with masking out these bits in the
876 * mcam entry, same entry used for NIX will allow packets
877 * received from cpt for parsing.
879 if (!is_rvu_otx2(rvu))
880 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
882 req.chan_mask = 0xFFFU;
885 req.intf = pfvf->nix_rx_intf;
888 req.hdr.pcifunc = 0; /* AF is requester */
889 req.vf = pcifunc | vf_func;
890 req.index = action.index;
891 req.match_id = action.match_id;
892 req.flow_key_alg = action.flow_key_alg;
894 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
897 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
900 struct npc_mcam *mcam = &rvu->hw->mcam;
903 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
907 /* Get 'pcifunc' of PF device */
908 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
910 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
911 NIXLF_ALLMULTI_ENTRY);
912 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
915 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
916 int blkaddr, u16 pcifunc, u64 rx_action)
918 int actindex, index, bank, entry;
921 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
924 mutex_lock(&mcam->lock);
925 for (index = 0; index < mcam->bmap_entries; index++) {
926 if (mcam->entry2target_pffunc[index] == pcifunc) {
927 bank = npc_get_bank(mcam, index);
929 entry = index & (mcam->banksize - 1);
931 /* read vf flow entry enable status */
932 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
934 /* disable before mcam entry update */
935 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
937 /* update 'action' */
938 rvu_write64(rvu, blkaddr,
939 NPC_AF_MCAMEX_BANKX_ACTION(entry, bank),
942 npc_enable_mcam_entry(rvu, mcam, blkaddr,
946 mutex_unlock(&mcam->lock);
949 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
950 int group, int alg_idx, int mcam_index)
952 struct npc_mcam *mcam = &rvu->hw->mcam;
953 struct rvu_hwinfo *hw = rvu->hw;
954 struct nix_rx_action action;
955 int blkaddr, index, bank;
956 struct rvu_pfvf *pfvf;
958 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
962 /* Check if this is for reserved default entry */
963 if (mcam_index < 0) {
964 if (group != DEFAULT_RSS_CONTEXT_GROUP)
966 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
967 nixlf, NIXLF_UCAST_ENTRY);
969 /* TODO: validate this mcam index */
973 if (index >= mcam->total_entries)
976 bank = npc_get_bank(mcam, index);
977 index &= (mcam->banksize - 1);
979 *(u64 *)&action = rvu_read64(rvu, blkaddr,
980 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
981 /* Ignore if no action was set earlier */
982 if (!*(u64 *)&action)
985 action.op = NIX_RX_ACTIONOP_RSS;
986 action.pf_func = pcifunc;
987 action.index = group;
988 action.flow_key_alg = alg_idx;
990 rvu_write64(rvu, blkaddr,
991 NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
993 /* update the VF flow rule action with the VF default entry action */
995 npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
998 /* update the action change in default rule */
999 pfvf = rvu_get_pfvf(rvu, pcifunc);
1000 if (pfvf->def_ucast_rule)
1001 pfvf->def_ucast_rule->rx_action = action;
1003 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1004 nixlf, NIXLF_PROMISC_ENTRY);
1006 /* If PF's promiscuous entry is enabled,
1007 * Set RSS action for that entry as well
1009 if ((!hw->cap.nix_rx_multicast || !pfvf->use_mce_list) &&
1010 is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
1011 bank = npc_get_bank(mcam, index);
1012 index &= (mcam->banksize - 1);
1014 rvu_write64(rvu, blkaddr,
1015 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
1020 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1021 int nixlf, int type, bool enable)
1023 struct npc_mcam *mcam = &rvu->hw->mcam;
1024 struct rvu_hwinfo *hw = rvu->hw;
1025 struct nix_mce_list *mce_list;
1026 int index, blkaddr, mce_idx;
1027 struct rvu_pfvf *pfvf;
1029 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1033 index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1036 /* disable MCAM entry when packet replication is not supported by hw */
1037 if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1038 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1042 /* return incase mce list is not enabled */
1043 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1044 if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1045 type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1048 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1050 nix_update_mce_list(rvu, pcifunc, mce_list,
1051 mce_idx, index, enable);
1053 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1056 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1057 int nixlf, bool enable)
1059 struct npc_mcam *mcam = &rvu->hw->mcam;
1062 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1066 /* Ucast MCAM match entry of this PF/VF */
1067 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1068 nixlf, NIXLF_UCAST_ENTRY);
1069 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1071 /* Nothing to do for VFs, on platforms where pkt replication
1074 if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1077 /* add/delete pf_func to broadcast MCE list */
1078 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1079 NIXLF_BCAST_ENTRY, enable);
1082 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1084 npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1086 /* Delete multicast and promisc MCAM entries */
1087 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1088 NIXLF_ALLMULTI_ENTRY, false);
1089 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1090 NIXLF_PROMISC_ENTRY, false);
1093 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1095 /* Enables only broadcast match entry. Promisc/Allmulti are enabled
1096 * in set_rx_mode mbox handler.
1098 npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1101 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1103 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1104 struct npc_mcam *mcam = &rvu->hw->mcam;
1105 struct rvu_npc_mcam_rule *rule, *tmp;
1108 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1112 mutex_lock(&mcam->lock);
1114 /* Disable MCAM entries directing traffic to this 'pcifunc' */
1115 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1116 if (is_npc_intf_rx(rule->intf) &&
1117 rule->rx_action.pf_func == pcifunc &&
1118 rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1119 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1120 rule->entry, false);
1121 rule->enable = false;
1122 /* Indicate that default rule is disabled */
1123 if (rule->default_rule) {
1124 pfvf->def_ucast_rule = NULL;
1125 list_del(&rule->list);
1131 mutex_unlock(&mcam->lock);
1133 npc_mcam_disable_flows(rvu, pcifunc);
1135 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1138 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1140 struct npc_mcam *mcam = &rvu->hw->mcam;
1141 struct rvu_npc_mcam_rule *rule, *tmp;
1144 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1148 mutex_lock(&mcam->lock);
1150 /* Free all MCAM entries owned by this 'pcifunc' */
1151 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1153 /* Free all MCAM counters owned by this 'pcifunc' */
1154 npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1156 /* Delete MCAM entries owned by this 'pcifunc' */
1157 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1158 if (rule->owner == pcifunc && !rule->default_rule) {
1159 list_del(&rule->list);
1164 mutex_unlock(&mcam->lock);
1166 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1169 #define SET_KEX_LD(intf, lid, ltype, ld, cfg) \
1170 rvu_write64(rvu, blkaddr, \
1171 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, ltype, ld), cfg)
1173 #define SET_KEX_LDFLAGS(intf, ld, flags, cfg) \
1174 rvu_write64(rvu, blkaddr, \
1175 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, flags), cfg)
1177 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1178 struct npc_mcam_kex *mkex, u8 intf)
1180 int lid, lt, ld, fl;
1182 if (is_npc_intf_tx(intf))
1185 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1186 mkex->keyx_cfg[NIX_INTF_RX]);
1189 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1190 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1191 for (ld = 0; ld < NPC_MAX_LD; ld++)
1192 SET_KEX_LD(intf, lid, lt, ld,
1193 mkex->intf_lid_lt_ld[NIX_INTF_RX]
1197 /* Program LFLAGS */
1198 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1199 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1200 SET_KEX_LDFLAGS(intf, ld, fl,
1201 mkex->intf_ld_flags[NIX_INTF_RX]
1206 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1207 struct npc_mcam_kex *mkex, u8 intf)
1209 int lid, lt, ld, fl;
1211 if (is_npc_intf_rx(intf))
1214 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1215 mkex->keyx_cfg[NIX_INTF_TX]);
1218 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1219 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1220 for (ld = 0; ld < NPC_MAX_LD; ld++)
1221 SET_KEX_LD(intf, lid, lt, ld,
1222 mkex->intf_lid_lt_ld[NIX_INTF_TX]
1226 /* Program LFLAGS */
1227 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1228 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1229 SET_KEX_LDFLAGS(intf, ld, fl,
1230 mkex->intf_ld_flags[NIX_INTF_TX]
1235 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1236 struct npc_mcam_kex *mkex)
1238 struct rvu_hwinfo *hw = rvu->hw;
1242 for (ld = 0; ld < NPC_MAX_LD; ld++)
1243 rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1244 mkex->kex_ld_flags[ld]);
1246 for (intf = 0; intf < hw->npc_intfs; intf++) {
1247 npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1248 npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1252 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1255 u64 prfl_addr, prfl_sz;
1260 prfl_addr = rvu->fwdata->mcam_addr;
1261 prfl_sz = rvu->fwdata->mcam_sz;
1263 if (!prfl_addr || !prfl_sz)
1266 *prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1267 if (!(*prfl_img_addr))
1275 /* strtoull of "mkexprof" with base:36 */
1276 #define MKEX_END_SIGN 0xdeadbeef
1278 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1279 const char *mkex_profile)
1281 struct device *dev = &rvu->pdev->dev;
1282 struct npc_mcam_kex *mcam_kex;
1283 void __iomem *mkex_prfl_addr = NULL;
1287 /* If user not selected mkex profile */
1288 if (rvu->kpu_fwdata_sz ||
1289 !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1292 /* Setting up the mapping for mkex profile image */
1293 ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1297 mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1299 while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1300 /* Compare with mkex mod_param name string */
1301 if (mcam_kex->mkex_sign == MKEX_SIGN &&
1302 !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1303 /* Due to an errata (35786) in A0/B0 pass silicon,
1304 * parse nibble enable configuration has to be
1305 * identical for both Rx and Tx interfaces.
1307 if (!is_rvu_96xx_B0(rvu) ||
1308 mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1309 rvu->kpu.mkex = mcam_kex;
1314 prfl_sz -= sizeof(struct npc_mcam_kex);
1316 dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1319 dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1320 /* Program selected mkex profile */
1321 npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1323 iounmap(mkex_prfl_addr);
1326 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1327 const struct npc_kpu_profile_action *kpuaction,
1328 int kpu, int entry, bool pkind)
1330 struct npc_kpu_action0 action0 = {0};
1331 struct npc_kpu_action1 action1 = {0};
1334 action1.errlev = kpuaction->errlev;
1335 action1.errcode = kpuaction->errcode;
1336 action1.dp0_offset = kpuaction->dp0_offset;
1337 action1.dp1_offset = kpuaction->dp1_offset;
1338 action1.dp2_offset = kpuaction->dp2_offset;
1341 reg = NPC_AF_PKINDX_ACTION1(entry);
1343 reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1345 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1347 action0.byp_count = kpuaction->bypass_count;
1348 action0.capture_ena = kpuaction->cap_ena;
1349 action0.parse_done = kpuaction->parse_done;
1350 action0.next_state = kpuaction->next_state;
1351 action0.capture_lid = kpuaction->lid;
1352 action0.capture_ltype = kpuaction->ltype;
1353 action0.capture_flags = kpuaction->flags;
1354 action0.ptr_advance = kpuaction->ptr_advance;
1355 action0.var_len_offset = kpuaction->offset;
1356 action0.var_len_mask = kpuaction->mask;
1357 action0.var_len_right = kpuaction->right;
1358 action0.var_len_shift = kpuaction->shift;
1361 reg = NPC_AF_PKINDX_ACTION0(entry);
1363 reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1365 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1368 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1369 const struct npc_kpu_profile_cam *kpucam,
1372 struct npc_kpu_cam cam0 = {0};
1373 struct npc_kpu_cam cam1 = {0};
1375 cam1.state = kpucam->state & kpucam->state_mask;
1376 cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1377 cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1378 cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1380 cam0.state = ~kpucam->state & kpucam->state_mask;
1381 cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1382 cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1383 cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1385 rvu_write64(rvu, blkaddr,
1386 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1387 rvu_write64(rvu, blkaddr,
1388 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1391 static inline u64 enable_mask(int count)
1393 return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1396 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1397 const struct npc_kpu_profile *profile)
1399 int entry, num_entries, max_entries;
1402 if (profile->cam_entries != profile->action_entries) {
1404 "KPU%d: CAM and action entries [%d != %d] not equal\n",
1405 kpu, profile->cam_entries, profile->action_entries);
1408 max_entries = rvu->hw->npc_kpu_entries;
1410 /* Program CAM match entries for previous KPU extracted data */
1411 num_entries = min_t(int, profile->cam_entries, max_entries);
1412 for (entry = 0; entry < num_entries; entry++)
1413 npc_config_kpucam(rvu, blkaddr,
1414 &profile->cam[entry], kpu, entry);
1416 /* Program this KPU's actions */
1417 num_entries = min_t(int, profile->action_entries, max_entries);
1418 for (entry = 0; entry < num_entries; entry++)
1419 npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1422 /* Enable all programmed entries */
1423 num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1424 entry_mask = enable_mask(num_entries);
1425 /* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1426 if (!rvu->kpu.custom)
1427 entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1428 rvu_write64(rvu, blkaddr,
1429 NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1430 if (num_entries > 64) {
1431 rvu_write64(rvu, blkaddr,
1432 NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1433 enable_mask(num_entries - 64));
1436 /* Enable this KPU */
1437 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1440 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1442 profile->custom = 0;
1443 profile->name = def_pfl_name;
1444 profile->version = NPC_KPU_PROFILE_VER;
1445 profile->ikpu = ikpu_action_entries;
1446 profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1447 profile->kpu = npc_kpu_profiles;
1448 profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1449 profile->lt_def = &npc_lt_defaults;
1450 profile->mkex = &npc_mkex_default;
1455 static int npc_apply_custom_kpu(struct rvu *rvu,
1456 struct npc_kpu_profile_adapter *profile)
1458 size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1459 struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1460 struct npc_kpu_profile_action *action;
1461 struct npc_kpu_profile_cam *cam;
1462 struct npc_kpu_fwdata *fw_kpu;
1466 if (rvu->kpu_fwdata_sz < hdr_sz) {
1467 dev_warn(rvu->dev, "Invalid KPU profile size\n");
1470 if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1471 dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1475 /* Verify if the using known profile structure */
1476 if (NPC_KPU_VER_MAJ(profile->version) >
1477 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1478 dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1479 NPC_KPU_VER_MAJ(profile->version),
1480 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1483 /* Verify if profile is aligned with the required kernel changes */
1484 if (NPC_KPU_VER_MIN(profile->version) <
1485 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1487 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1488 NPC_KPU_VER_MAJ(profile->version),
1489 NPC_KPU_VER_MIN(profile->version),
1490 NPC_KPU_VER_PATCH(profile->version),
1491 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1492 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1493 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1496 /* Verify if profile fits the HW */
1497 if (fw->kpus > profile->kpus) {
1498 dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1503 profile->custom = 1;
1504 profile->name = fw->name;
1505 profile->version = le64_to_cpu(fw->version);
1506 profile->mkex = &fw->mkex;
1507 profile->lt_def = &fw->lt_def;
1509 for (kpu = 0; kpu < fw->kpus; kpu++) {
1510 fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1511 if (fw_kpu->entries > KPU_MAX_CST_ENT)
1513 "Too many custom entries on KPU%d: %d > %d\n",
1514 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1515 entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1516 cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1517 offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1518 action = (struct npc_kpu_profile_action *)(fw->data + offset);
1519 offset += fw_kpu->entries * sizeof(*action);
1520 if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1522 "Profile size mismatch on KPU%i parsing.\n",
1526 for (entry = 0; entry < entries; entry++) {
1527 profile->kpu[kpu].cam[entry] = cam[entry];
1528 profile->kpu[kpu].action[entry] = action[entry];
1535 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1536 u64 prfl_sz, const char *kpu_profile)
1538 struct npc_kpu_profile_fwdata *kpu_data = NULL;
1541 kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1542 if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1543 !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1544 dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1546 rvu->kpu_fwdata = kpu_data;
1547 rvu->kpu_fwdata_sz = prfl_sz;
1548 rvu->kpu_prfl_addr = prfl_addr;
1555 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1556 const char *kpu_profile)
1558 struct npc_coalesced_kpu_prfl *img_data = NULL;
1559 int i = 0, rc = -EINVAL;
1560 void __iomem *kpu_prfl_addr;
1563 img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1564 if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1565 !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1566 /* Loaded profile is a single KPU profile. */
1567 rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1568 prfl_sz, kpu_profile);
1572 /* Loaded profile is coalesced image, offset of first KPU profile.*/
1573 offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1574 (img_data->num_prfl * sizeof(uint16_t));
1575 /* Check if mapped image is coalesced image. */
1576 while (i < img_data->num_prfl) {
1577 /* Profile image offsets are rounded up to next 8 multiple.*/
1578 offset = ALIGN_8B_CEIL(offset);
1579 kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1581 rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1582 img_data->prfl_sz[i], kpu_profile);
1585 /* Calculating offset of profile image based on profile size.*/
1586 offset += img_data->prfl_sz[i];
1593 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1598 /* Setting up the mapping for NPC profile image */
1599 ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1603 /* Detect if profile is coalesced or single KPU profile and load */
1604 ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1608 /* Cleaning up if KPU profile image from fwdata is not valid. */
1609 if (rvu->kpu_prfl_addr) {
1610 iounmap(rvu->kpu_prfl_addr);
1611 rvu->kpu_prfl_addr = NULL;
1612 rvu->kpu_fwdata_sz = 0;
1613 rvu->kpu_fwdata = NULL;
1620 static void npc_load_kpu_profile(struct rvu *rvu)
1622 struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1623 const char *kpu_profile = rvu->kpu_pfl_name;
1624 const struct firmware *fw = NULL;
1625 bool retry_fwdb = false;
1627 /* If user not specified profile customization */
1628 if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1629 goto revert_to_default;
1630 /* First prepare default KPU, then we'll customize top entries. */
1631 npc_prepare_default_kpu(profile);
1633 /* Order of preceedence for load loading NPC profile (high to low)
1634 * Firmware binary in filesystem.
1635 * Firmware database method.
1636 * Default KPU profile.
1638 if (!request_firmware(&fw, kpu_profile, rvu->dev)) {
1639 dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1641 rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1642 if (rvu->kpu_fwdata) {
1643 memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1644 rvu->kpu_fwdata_sz = fw->size;
1646 release_firmware(fw);
1652 /* Loading the KPU profile using firmware database */
1653 if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1654 goto revert_to_default;
1657 /* Apply profile customization if firmware was loaded. */
1658 if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1659 /* If image from firmware filesystem fails to load or invalid
1660 * retry with firmware database method.
1662 if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1663 /* Loading image from firmware database failed. */
1664 if (rvu->kpu_prfl_addr) {
1665 iounmap(rvu->kpu_prfl_addr);
1666 rvu->kpu_prfl_addr = NULL;
1668 kfree(rvu->kpu_fwdata);
1670 rvu->kpu_fwdata = NULL;
1671 rvu->kpu_fwdata_sz = 0;
1674 goto load_image_fwdb;
1679 "Can't load KPU profile %s. Using default.\n",
1681 kfree(rvu->kpu_fwdata);
1682 rvu->kpu_fwdata = NULL;
1683 goto revert_to_default;
1686 dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1687 profile->name, NPC_KPU_VER_MAJ(profile->version),
1688 NPC_KPU_VER_MIN(profile->version),
1689 NPC_KPU_VER_PATCH(profile->version));
1694 npc_prepare_default_kpu(profile);
1697 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1699 struct rvu_hwinfo *hw = rvu->hw;
1700 int num_pkinds, num_kpus, idx;
1702 /* Disable all KPUs and their entries */
1703 for (idx = 0; idx < hw->npc_kpus; idx++) {
1704 rvu_write64(rvu, blkaddr,
1705 NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1706 rvu_write64(rvu, blkaddr,
1707 NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1708 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1711 /* Load and customize KPU profile. */
1712 npc_load_kpu_profile(rvu);
1714 /* First program IKPU profile i.e PKIND configs.
1715 * Check HW max count to avoid configuring junk or
1716 * writing to unsupported CSR addresses.
1718 num_pkinds = rvu->kpu.pkinds;
1719 num_pkinds = min_t(int, hw->npc_pkinds, num_pkinds);
1721 for (idx = 0; idx < num_pkinds; idx++)
1722 npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1724 /* Program KPU CAM and Action profiles */
1725 num_kpus = rvu->kpu.kpus;
1726 num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1728 for (idx = 0; idx < num_kpus; idx++)
1729 npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1732 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1734 int nixlf_count = rvu_get_nixlf_count(rvu);
1735 struct npc_mcam *mcam = &rvu->hw->mcam;
1741 /* Actual number of MCAM entries vary by entry size */
1742 cfg = (rvu_read64(rvu, blkaddr,
1743 NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1744 mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1745 mcam->keysize = cfg;
1747 /* Number of banks combined per MCAM entry */
1748 if (cfg == NPC_MCAM_KEY_X4)
1749 mcam->banks_per_entry = 4;
1750 else if (cfg == NPC_MCAM_KEY_X2)
1751 mcam->banks_per_entry = 2;
1753 mcam->banks_per_entry = 1;
1755 /* Reserve one MCAM entry for each of the NIX LF to
1756 * guarantee space to install default matching DMAC rule.
1757 * Also reserve 2 MCAM entries for each PF for default
1758 * channel based matching or 'bcast & promisc' matching to
1759 * support BCAST and PROMISC modes of operation for PFs.
1762 rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1763 ((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1764 if (mcam->total_entries <= rsvd) {
1766 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1767 mcam->total_entries);
1771 mcam->bmap_entries = mcam->total_entries - rsvd;
1772 mcam->nixlf_offset = mcam->bmap_entries;
1773 mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1775 /* Allocate bitmaps for managing MCAM entries */
1776 mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1777 sizeof(long), GFP_KERNEL);
1781 mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1782 BITS_TO_LONGS(mcam->bmap_entries),
1783 sizeof(long), GFP_KERNEL);
1784 if (!mcam->bmap_reverse)
1787 mcam->bmap_fcnt = mcam->bmap_entries;
1789 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
1790 mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1791 sizeof(u16), GFP_KERNEL);
1792 if (!mcam->entry2pfvf_map)
1795 /* Reserve 1/8th of MCAM entries at the bottom for low priority
1796 * allocations and another 1/8th at the top for high priority
1799 mcam->lprio_count = mcam->bmap_entries / 8;
1800 if (mcam->lprio_count > BITS_PER_LONG)
1801 mcam->lprio_count = round_down(mcam->lprio_count,
1803 mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1804 mcam->hprio_count = mcam->lprio_count;
1805 mcam->hprio_end = mcam->hprio_count;
1808 /* Allocate bitmap for managing MCAM counters and memory
1809 * for saving counter to RVU PFFUNC allocation mapping.
1811 err = rvu_alloc_bitmap(&mcam->counters);
1815 mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1816 sizeof(u16), GFP_KERNEL);
1817 if (!mcam->cntr2pfvf_map)
1820 /* Alloc memory for MCAM entry to counter mapping and for tracking
1821 * counter's reference count.
1823 mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1824 sizeof(u16), GFP_KERNEL);
1825 if (!mcam->entry2cntr_map)
1828 mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1829 sizeof(u16), GFP_KERNEL);
1830 if (!mcam->cntr_refcnt)
1833 /* Alloc memory for saving target device of mcam rule */
1834 mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1835 sizeof(u16), GFP_KERNEL);
1836 if (!mcam->entry2target_pffunc)
1839 for (index = 0; index < mcam->bmap_entries; index++) {
1840 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
1841 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
1844 for (cntr = 0; cntr < mcam->counters.max; cntr++)
1845 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
1847 mutex_init(&mcam->lock);
1852 kfree(mcam->counters.bmap);
1856 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1858 struct npc_pkind *pkind = &rvu->hw->pkind;
1859 struct npc_mcam *mcam = &rvu->hw->mcam;
1860 struct rvu_hwinfo *hw = rvu->hw;
1861 u64 npc_const, npc_const1;
1864 npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1865 npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1866 if (npc_const1 & BIT_ULL(63))
1867 npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1869 pkind->rsrc.max = NPC_UNRESERVED_PKIND_COUNT;
1870 hw->npc_pkinds = (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;
1878 hw->npc_stat_ena = BIT_ULL(9);
1881 hw->npc_ext_set = true;
1882 /* 96xx supports only match_stats and npc_counters
1883 * reflected in NPC_AF_CONST reg.
1884 * STAT_SEL and ENA are at [0:8] and 9 bit positions.
1885 * 98xx has both match_stat and ext and npc_counter
1886 * reflected in NPC_AF_CONST2
1887 * STAT_SEL_EXT added at [12:14] bit position.
1888 * cn10k supports only ext and hence npc_counters in
1889 * NPC_AF_CONST is 0 and npc_counters reflected in NPC_AF_CONST2.
1890 * STAT_SEL bitpos incremented from [0:8] to [0:11] and ENA bit moved to 63
1892 if (!hw->npc_counters)
1893 hw->npc_stat_ena = BIT_ULL(63);
1894 hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1895 mcam->banksize = npc_const2 & 0xFFFFULL;
1898 mcam->counters.max = hw->npc_counters;
1901 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1903 struct npc_mcam *mcam = &rvu->hw->mcam;
1904 struct rvu_hwinfo *hw = rvu->hw;
1905 u64 nibble_ena, rx_kex, tx_kex;
1908 /* Reserve last counter for MCAM RX miss action which is set to
1909 * drop packet. This way we will know how many pkts didn't match
1912 mcam->counters.max--;
1913 mcam->rx_miss_act_cntr = mcam->counters.max;
1915 rx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_RX];
1916 tx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_TX];
1917 nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1919 nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1921 tx_kex &= ~NPC_PARSE_NIBBLE;
1922 tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1923 npc_mkex_default.keyx_cfg[NIX_INTF_TX] = tx_kex;
1926 /* Configure RX interfaces */
1927 for (intf = 0; intf < hw->npc_intfs; intf++) {
1928 if (is_npc_intf_tx(intf))
1931 /* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
1932 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1935 /* If MCAM lookup doesn't result in a match, drop the received
1936 * packet. And map this action to a counter to count dropped
1939 rvu_write64(rvu, blkaddr,
1940 NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1942 /* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
1943 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
1945 rvu_write64(rvu, blkaddr,
1946 NPC_AF_INTFX_MISS_STAT_ACT(intf),
1947 ((mcam->rx_miss_act_cntr >> 9) << 12) |
1948 hw->npc_stat_ena | mcam->rx_miss_act_cntr);
1951 /* Configure TX interfaces */
1952 for (intf = 0; intf < hw->npc_intfs; intf++) {
1953 if (is_npc_intf_rx(intf))
1956 /* Extract Ltypes LID_LA to LID_LE */
1957 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1960 /* Set TX miss action to UCAST_DEFAULT i.e
1961 * transmit the packet on NIX LF SQ's default channel.
1963 rvu_write64(rvu, blkaddr,
1964 NPC_AF_INTFX_MISS_ACT(intf),
1965 NIX_TX_ACTIONOP_UCAST_DEFAULT);
1969 int rvu_npc_init(struct rvu *rvu)
1971 struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
1972 struct npc_pkind *pkind = &rvu->hw->pkind;
1973 struct npc_mcam *mcam = &rvu->hw->mcam;
1974 int blkaddr, entry, bank, err;
1976 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1978 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1982 rvu_npc_hw_init(rvu, blkaddr);
1984 /* First disable all MCAM entries, to stop traffic towards NIXLFs */
1985 for (bank = 0; bank < mcam->banks; bank++) {
1986 for (entry = 0; entry < mcam->banksize; entry++)
1987 rvu_write64(rvu, blkaddr,
1988 NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
1991 err = rvu_alloc_bitmap(&pkind->rsrc);
1994 /* Reserve PKIND#0 for LBKs. Power reset value of LBK_CH_PKIND is '0',
1995 * no need to configure PKIND for all LBKs separately.
1997 rvu_alloc_rsrc(&pkind->rsrc);
1999 /* Allocate mem for pkind to PF and channel mapping info */
2000 pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
2001 sizeof(u32), GFP_KERNEL);
2002 if (!pkind->pfchan_map)
2005 /* Configure KPU profile */
2006 npc_parser_profile_init(rvu, blkaddr);
2008 /* Config Outer L2, IPv4's NPC layer info */
2009 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
2010 (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
2011 kpu->lt_def->pck_ol2.ltype_mask);
2012 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
2013 (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
2014 kpu->lt_def->pck_oip4.ltype_mask);
2016 /* Config Inner IPV4 NPC layer info */
2017 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2018 (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2019 kpu->lt_def->pck_iip4.ltype_mask);
2021 /* Enable below for Rx pkts.
2022 * - Outer IPv4 header checksum validation.
2023 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2M].
2024 * - Inner IPv4 header checksum validation.
2025 * - Set non zero checksum error code value
2027 rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2028 rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2029 BIT_ULL(32) | BIT_ULL(24) | BIT_ULL(6) |
2030 BIT_ULL(2) | BIT_ULL(1));
2032 rvu_npc_setup_interfaces(rvu, blkaddr);
2034 /* Configure MKEX profile */
2035 npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2037 err = npc_mcam_rsrcs_init(rvu, blkaddr);
2041 err = npc_flow_steering_init(rvu, blkaddr);
2044 "Incorrect mkex profile loaded using default mkex\n");
2045 npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2051 void rvu_npc_freemem(struct rvu *rvu)
2053 struct npc_pkind *pkind = &rvu->hw->pkind;
2054 struct npc_mcam *mcam = &rvu->hw->mcam;
2056 kfree(pkind->rsrc.bmap);
2057 kfree(mcam->counters.bmap);
2058 if (rvu->kpu_prfl_addr)
2059 iounmap(rvu->kpu_prfl_addr);
2061 kfree(rvu->kpu_fwdata);
2062 mutex_destroy(&mcam->lock);
2065 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2066 int blkaddr, int *alloc_cnt,
2069 struct npc_mcam *mcam = &rvu->hw->mcam;
2075 for (entry = 0; entry < mcam->bmap_entries; entry++) {
2076 if (mcam->entry2pfvf_map[entry] == pcifunc) {
2078 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2084 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2085 int blkaddr, int *alloc_cnt,
2088 struct npc_mcam *mcam = &rvu->hw->mcam;
2094 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2095 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2097 if (mcam->cntr_refcnt[cntr])
2103 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
2104 u16 pcifunc, int entry)
2106 /* verify AF installed entries */
2107 if (is_pffunc_af(pcifunc))
2109 /* Verify if entry is valid and if it is indeed
2110 * allocated to the requesting PFFUNC.
2112 if (entry >= mcam->bmap_entries)
2113 return NPC_MCAM_INVALID_REQ;
2115 if (pcifunc != mcam->entry2pfvf_map[entry])
2116 return NPC_MCAM_PERM_DENIED;
2121 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2122 u16 pcifunc, int cntr)
2124 /* Verify if counter is valid and if it is indeed
2125 * allocated to the requesting PFFUNC.
2127 if (cntr >= mcam->counters.max)
2128 return NPC_MCAM_INVALID_REQ;
2130 if (pcifunc != mcam->cntr2pfvf_map[cntr])
2131 return NPC_MCAM_PERM_DENIED;
2136 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2137 int blkaddr, u16 entry, u16 cntr)
2139 u16 index = entry & (mcam->banksize - 1);
2140 u32 bank = npc_get_bank(mcam, entry);
2141 struct rvu_hwinfo *hw = rvu->hw;
2143 /* Set mapping and increment counter's refcnt */
2144 mcam->entry2cntr_map[entry] = cntr;
2145 mcam->cntr_refcnt[cntr]++;
2147 rvu_write64(rvu, blkaddr,
2148 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2149 ((cntr >> 9) << 12) | hw->npc_stat_ena | cntr);
2152 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2153 struct npc_mcam *mcam,
2154 int blkaddr, u16 entry, u16 cntr)
2156 u16 index = entry & (mcam->banksize - 1);
2157 u32 bank = npc_get_bank(mcam, entry);
2159 /* Remove mapping and reduce counter's refcnt */
2160 mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2161 mcam->cntr_refcnt[cntr]--;
2163 rvu_write64(rvu, blkaddr,
2164 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2167 /* Sets MCAM entry in bitmap as used. Update
2168 * reverse bitmap too. Should be called with
2169 * 'mcam->lock' held.
2171 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2176 rentry = mcam->bmap_entries - index - 1;
2178 __set_bit(entry, mcam->bmap);
2179 __set_bit(rentry, mcam->bmap_reverse);
2183 /* Sets MCAM entry in bitmap as free. Update
2184 * reverse bitmap too. Should be called with
2185 * 'mcam->lock' held.
2187 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2192 rentry = mcam->bmap_entries - index - 1;
2194 __clear_bit(entry, mcam->bmap);
2195 __clear_bit(rentry, mcam->bmap_reverse);
2199 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2200 int blkaddr, u16 pcifunc)
2204 /* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2205 for (index = 0; index < mcam->bmap_entries; index++) {
2206 if (mcam->entry2pfvf_map[index] == pcifunc) {
2207 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2208 /* Free the entry in bitmap */
2209 npc_mcam_clear_bit(mcam, index);
2210 /* Disable the entry */
2211 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2213 /* Update entry2counter mapping */
2214 cntr = mcam->entry2cntr_map[index];
2215 if (cntr != NPC_MCAM_INVALID_MAP)
2216 npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2219 mcam->entry2target_pffunc[index] = 0x0;
2224 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2229 /* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2230 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2231 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2232 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2233 mcam->cntr_refcnt[cntr] = 0;
2234 rvu_free_rsrc(&mcam->counters, cntr);
2235 /* This API is expected to be called after freeing
2236 * MCAM entries, which inturn will remove
2237 * 'entry to counter' mapping.
2238 * No need to do it again.
2244 /* Find area of contiguous free entries of size 'nr'.
2245 * If not found return max contiguous free entries available.
2247 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2248 u16 nr, u16 *max_area)
2250 u16 max_area_start = 0;
2251 u16 index, next, end;
2256 index = find_next_zero_bit(map, size, start);
2258 return max_area_start;
2260 end = ((index + nr) >= size) ? size : index + nr;
2261 next = find_next_bit(map, end, index);
2262 if (*max_area < (next - index)) {
2263 *max_area = next - index;
2264 max_area_start = index;
2272 return max_area_start;
2275 /* Find number of free MCAM entries available
2276 * within range i.e in between 'start' and 'end'.
2278 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2287 index = find_next_zero_bit(map, end, start);
2291 next = find_next_bit(map, end, index);
2293 fcnt += next - index;
2298 fcnt += end - index;
2303 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2304 struct npc_mcam_alloc_entry_req *req,
2305 u16 *start, u16 *end, bool *reverse)
2309 if (req->priority == NPC_MCAM_HIGHER_PRIO)
2312 /* For a low priority entry allocation
2313 * - If reference entry is not in hprio zone then
2314 * search range: ref_entry to end.
2315 * - If reference entry is in hprio zone and if
2316 * request can be accomodated in non-hprio zone then
2317 * search range: 'start of middle zone' to 'end'
2318 * - else search in reverse, so that less number of hprio
2319 * zone entries are allocated.
2323 *start = req->ref_entry + 1;
2324 *end = mcam->bmap_entries;
2326 if (req->ref_entry >= mcam->hprio_end)
2329 fcnt = npc_mcam_get_free_count(mcam->bmap,
2330 mcam->hprio_end, mcam->bmap_entries);
2331 if (fcnt > req->count)
2332 *start = mcam->hprio_end;
2338 /* For a high priority entry allocation, search is always
2339 * in reverse to preserve hprio zone entries.
2340 * - If reference entry is not in lprio zone then
2341 * search range: 0 to ref_entry.
2342 * - If reference entry is in lprio zone and if
2343 * request can be accomodated in middle zone then
2344 * search range: 'hprio_end' to 'lprio_start'
2349 *end = req->ref_entry;
2351 if (req->ref_entry <= mcam->lprio_start)
2354 fcnt = npc_mcam_get_free_count(mcam->bmap,
2355 mcam->hprio_end, mcam->lprio_start);
2356 if (fcnt < req->count)
2358 *start = mcam->hprio_end;
2359 *end = mcam->lprio_start;
2362 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2363 struct npc_mcam_alloc_entry_req *req,
2364 struct npc_mcam_alloc_entry_rsp *rsp)
2366 u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2367 u16 fcnt, hp_fcnt, lp_fcnt;
2368 u16 start, end, index;
2369 int entry, next_start;
2370 bool reverse = false;
2371 unsigned long *bmap;
2374 mutex_lock(&mcam->lock);
2376 /* Check if there are any free entries */
2377 if (!mcam->bmap_fcnt) {
2378 mutex_unlock(&mcam->lock);
2379 return NPC_MCAM_ALLOC_FAILED;
2382 /* MCAM entries are divided into high priority, middle and
2383 * low priority zones. Idea is to not allocate top and lower
2384 * most entries as much as possible, this is to increase
2385 * probability of honouring priority allocation requests.
2387 * Two bitmaps are used for mcam entry management,
2388 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
2389 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
2391 * Reverse bitmap is used to allocate entries
2392 * - when a higher priority entry is requested
2393 * - when available free entries are less.
2394 * Lower priority ones out of avaialble free entries are always
2395 * chosen when 'high vs low' question arises.
2398 /* Get the search range for priority allocation request */
2399 if (req->priority) {
2400 npc_get_mcam_search_range_priority(mcam, req,
2401 &start, &end, &reverse);
2405 /* For a VF base MCAM match rule is set by its PF. And all the
2406 * further MCAM rules installed by VF on its own are
2407 * concatenated with the base rule set by its PF. Hence PF entries
2408 * should be at lower priority compared to VF entries. Otherwise
2409 * base rule is hit always and rules installed by VF will be of
2410 * no use. Hence if the request is from PF and NOT a priority
2411 * allocation request then allocate low priority entries.
2413 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
2416 /* Find out the search range for non-priority allocation request
2418 * Get MCAM free entry count in middle zone.
2420 lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2422 mcam->bmap_entries);
2423 hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2424 fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2426 /* Check if request can be accomodated in the middle zone */
2427 if (fcnt > req->count) {
2428 start = mcam->hprio_end;
2429 end = mcam->lprio_start;
2430 } else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2431 /* Expand search zone from half of hprio zone to
2432 * half of lprio zone.
2434 start = mcam->hprio_end / 2;
2435 end = mcam->bmap_entries - (mcam->lprio_count / 2);
2438 /* Not enough free entries, search all entries in reverse,
2439 * so that low priority ones will get used up.
2444 end = mcam->bmap_entries;
2449 bmap = mcam->bmap_reverse;
2450 start = mcam->bmap_entries - start;
2451 end = mcam->bmap_entries - end;
2460 /* Allocate requested number of contiguous entries, if
2461 * unsuccessful find max contiguous entries available.
2463 index = npc_mcam_find_zero_area(bmap, end, start,
2464 req->count, &max_contig);
2465 rsp->count = max_contig;
2467 rsp->entry = mcam->bmap_entries - index - max_contig;
2471 /* Allocate requested number of non-contiguous entries,
2472 * if unsuccessful allocate as many as possible.
2476 for (entry = 0; entry < req->count; entry++) {
2477 index = find_next_zero_bit(bmap, end, next_start);
2481 next_start = start + (index - start) + 1;
2483 /* Save the entry's index */
2485 index = mcam->bmap_entries - index - 1;
2486 entry_list[entry] = index;
2491 /* If allocating requested no of entries is unsucessful,
2492 * expand the search range to full bitmap length and retry.
2494 if (!req->priority && (rsp->count < req->count) &&
2495 ((end - start) != mcam->bmap_entries)) {
2498 end = mcam->bmap_entries;
2502 /* For priority entry allocation requests, if allocation is
2503 * failed then expand search to max possible range and retry.
2505 if (req->priority && rsp->count < req->count) {
2506 if (req->priority == NPC_MCAM_LOWER_PRIO &&
2507 (start != (req->ref_entry + 1))) {
2508 start = req->ref_entry + 1;
2509 end = mcam->bmap_entries;
2512 } else if ((req->priority == NPC_MCAM_HIGHER_PRIO) &&
2513 ((end - start) != req->ref_entry)) {
2515 end = req->ref_entry;
2521 /* Copy MCAM entry indices into mbox response entry_list.
2522 * Requester always expects indices in ascending order, so
2523 * so reverse the list if reverse bitmap is used for allocation.
2525 if (!req->contig && rsp->count) {
2527 for (entry = rsp->count - 1; entry >= 0; entry--) {
2529 rsp->entry_list[index++] = entry_list[entry];
2531 rsp->entry_list[entry] = entry_list[entry];
2535 /* Mark the allocated entries as used and set nixlf mapping */
2536 for (entry = 0; entry < rsp->count; entry++) {
2537 index = req->contig ?
2538 (rsp->entry + entry) : rsp->entry_list[entry];
2539 npc_mcam_set_bit(mcam, index);
2540 mcam->entry2pfvf_map[index] = pcifunc;
2541 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2544 /* Update available free count in mbox response */
2545 rsp->free_count = mcam->bmap_fcnt;
2547 mutex_unlock(&mcam->lock);
2551 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
2552 struct npc_mcam_alloc_entry_req *req,
2553 struct npc_mcam_alloc_entry_rsp *rsp)
2555 struct npc_mcam *mcam = &rvu->hw->mcam;
2556 u16 pcifunc = req->hdr.pcifunc;
2559 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2561 return NPC_MCAM_INVALID_REQ;
2563 rsp->entry = NPC_MCAM_ENTRY_INVALID;
2564 rsp->free_count = 0;
2566 /* Check if ref_entry is within range */
2567 if (req->priority && req->ref_entry >= mcam->bmap_entries) {
2568 dev_err(rvu->dev, "%s: reference entry %d is out of range\n",
2569 __func__, req->ref_entry);
2570 return NPC_MCAM_INVALID_REQ;
2573 /* ref_entry can't be '0' if requested priority is high.
2574 * Can't be last entry if requested priority is low.
2576 if ((!req->ref_entry && req->priority == NPC_MCAM_HIGHER_PRIO) ||
2577 ((req->ref_entry == (mcam->bmap_entries - 1)) &&
2578 req->priority == NPC_MCAM_LOWER_PRIO))
2579 return NPC_MCAM_INVALID_REQ;
2581 /* Since list of allocated indices needs to be sent to requester,
2582 * max number of non-contiguous entries per mbox msg is limited.
2584 if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES) {
2586 "%s: %d Non-contiguous MCAM entries requested is more than max (%d) allowed\n",
2587 __func__, req->count, NPC_MAX_NONCONTIG_ENTRIES);
2588 return NPC_MCAM_INVALID_REQ;
2591 /* Alloc request from PFFUNC with no NIXLF attached should be denied */
2592 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2593 return NPC_MCAM_ALLOC_DENIED;
2595 return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
2598 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
2599 struct npc_mcam_free_entry_req *req,
2600 struct msg_rsp *rsp)
2602 struct npc_mcam *mcam = &rvu->hw->mcam;
2603 u16 pcifunc = req->hdr.pcifunc;
2604 int blkaddr, rc = 0;
2607 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2609 return NPC_MCAM_INVALID_REQ;
2611 /* Free request from PFFUNC with no NIXLF attached, ignore */
2612 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2613 return NPC_MCAM_INVALID_REQ;
2615 mutex_lock(&mcam->lock);
2620 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2624 mcam->entry2pfvf_map[req->entry] = NPC_MCAM_INVALID_MAP;
2625 mcam->entry2target_pffunc[req->entry] = 0x0;
2626 npc_mcam_clear_bit(mcam, req->entry);
2627 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2629 /* Update entry2counter mapping */
2630 cntr = mcam->entry2cntr_map[req->entry];
2631 if (cntr != NPC_MCAM_INVALID_MAP)
2632 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2638 /* Free up all entries allocated to requesting PFFUNC */
2639 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
2641 mutex_unlock(&mcam->lock);
2645 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
2646 struct npc_mcam_read_entry_req *req,
2647 struct npc_mcam_read_entry_rsp *rsp)
2649 struct npc_mcam *mcam = &rvu->hw->mcam;
2650 u16 pcifunc = req->hdr.pcifunc;
2653 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2655 return NPC_MCAM_INVALID_REQ;
2657 mutex_lock(&mcam->lock);
2658 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2660 npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
2662 &rsp->intf, &rsp->enable);
2665 mutex_unlock(&mcam->lock);
2669 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
2670 struct npc_mcam_write_entry_req *req,
2671 struct msg_rsp *rsp)
2673 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2674 struct npc_mcam *mcam = &rvu->hw->mcam;
2675 u16 pcifunc = req->hdr.pcifunc;
2679 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2681 return NPC_MCAM_INVALID_REQ;
2683 mutex_lock(&mcam->lock);
2684 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2688 if (req->set_cntr &&
2689 npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2690 rc = NPC_MCAM_INVALID_REQ;
2694 if (!is_npc_interface_valid(rvu, req->intf)) {
2695 rc = NPC_MCAM_INVALID_REQ;
2699 if (is_npc_intf_tx(req->intf))
2700 nix_intf = pfvf->nix_tx_intf;
2702 nix_intf = pfvf->nix_rx_intf;
2704 if (!is_pffunc_af(pcifunc) &&
2705 npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf, pcifunc)) {
2706 rc = NPC_MCAM_INVALID_REQ;
2710 /* For AF installed rules, the nix_intf should be set to target NIX */
2711 if (is_pffunc_af(req->hdr.pcifunc))
2712 nix_intf = req->intf;
2714 npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2715 &req->entry_data, req->enable_entry);
2718 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2719 req->entry, req->cntr);
2723 mutex_unlock(&mcam->lock);
2727 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2728 struct npc_mcam_ena_dis_entry_req *req,
2729 struct msg_rsp *rsp)
2731 struct npc_mcam *mcam = &rvu->hw->mcam;
2732 u16 pcifunc = req->hdr.pcifunc;
2735 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2737 return NPC_MCAM_INVALID_REQ;
2739 mutex_lock(&mcam->lock);
2740 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2741 mutex_unlock(&mcam->lock);
2745 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2750 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2751 struct npc_mcam_ena_dis_entry_req *req,
2752 struct msg_rsp *rsp)
2754 struct npc_mcam *mcam = &rvu->hw->mcam;
2755 u16 pcifunc = req->hdr.pcifunc;
2758 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2760 return NPC_MCAM_INVALID_REQ;
2762 mutex_lock(&mcam->lock);
2763 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2764 mutex_unlock(&mcam->lock);
2768 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2773 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2774 struct npc_mcam_shift_entry_req *req,
2775 struct npc_mcam_shift_entry_rsp *rsp)
2777 struct npc_mcam *mcam = &rvu->hw->mcam;
2778 u16 pcifunc = req->hdr.pcifunc;
2779 u16 old_entry, new_entry;
2780 int blkaddr, rc = 0;
2783 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2785 return NPC_MCAM_INVALID_REQ;
2787 if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2788 return NPC_MCAM_INVALID_REQ;
2790 mutex_lock(&mcam->lock);
2791 for (index = 0; index < req->shift_count; index++) {
2792 old_entry = req->curr_entry[index];
2793 new_entry = req->new_entry[index];
2795 /* Check if both old and new entries are valid and
2796 * does belong to this PFFUNC or not.
2798 rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2802 rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2806 /* new_entry should not have a counter mapped */
2807 if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2808 rc = NPC_MCAM_PERM_DENIED;
2812 /* Disable the new_entry */
2813 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2815 /* Copy rule from old entry to new entry */
2816 npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2818 /* Copy counter mapping, if any */
2819 cntr = mcam->entry2cntr_map[old_entry];
2820 if (cntr != NPC_MCAM_INVALID_MAP) {
2821 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2823 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2827 /* Enable new_entry and disable old_entry */
2828 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2829 npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2832 /* If shift has failed then report the failed index */
2833 if (index != req->shift_count) {
2834 rc = NPC_MCAM_PERM_DENIED;
2835 rsp->failed_entry_idx = index;
2838 mutex_unlock(&mcam->lock);
2842 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2843 struct npc_mcam_alloc_counter_req *req,
2844 struct npc_mcam_alloc_counter_rsp *rsp)
2846 struct npc_mcam *mcam = &rvu->hw->mcam;
2847 u16 pcifunc = req->hdr.pcifunc;
2848 u16 max_contig, cntr;
2851 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2853 return NPC_MCAM_INVALID_REQ;
2855 /* If the request is from a PFFUNC with no NIXLF attached, ignore */
2856 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2857 return NPC_MCAM_INVALID_REQ;
2859 /* Since list of allocated counter IDs needs to be sent to requester,
2860 * max number of non-contiguous counters per mbox msg is limited.
2862 if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2863 return NPC_MCAM_INVALID_REQ;
2865 mutex_lock(&mcam->lock);
2867 /* Check if unused counters are available or not */
2868 if (!rvu_rsrc_free_count(&mcam->counters)) {
2869 mutex_unlock(&mcam->lock);
2870 return NPC_MCAM_ALLOC_FAILED;
2876 /* Allocate requested number of contiguous counters, if
2877 * unsuccessful find max contiguous entries available.
2879 index = npc_mcam_find_zero_area(mcam->counters.bmap,
2880 mcam->counters.max, 0,
2881 req->count, &max_contig);
2882 rsp->count = max_contig;
2884 for (cntr = index; cntr < (index + max_contig); cntr++) {
2885 __set_bit(cntr, mcam->counters.bmap);
2886 mcam->cntr2pfvf_map[cntr] = pcifunc;
2889 /* Allocate requested number of non-contiguous counters,
2890 * if unsuccessful allocate as many as possible.
2892 for (cntr = 0; cntr < req->count; cntr++) {
2893 index = rvu_alloc_rsrc(&mcam->counters);
2896 rsp->cntr_list[cntr] = index;
2898 mcam->cntr2pfvf_map[index] = pcifunc;
2902 mutex_unlock(&mcam->lock);
2906 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2907 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2909 struct npc_mcam *mcam = &rvu->hw->mcam;
2910 u16 index, entry = 0;
2913 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2915 return NPC_MCAM_INVALID_REQ;
2917 mutex_lock(&mcam->lock);
2918 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2920 mutex_unlock(&mcam->lock);
2924 /* Mark counter as free/unused */
2925 mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2926 rvu_free_rsrc(&mcam->counters, req->cntr);
2928 /* Disable all MCAM entry's stats which are using this counter */
2929 while (entry < mcam->bmap_entries) {
2930 if (!mcam->cntr_refcnt[req->cntr])
2933 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2934 if (index >= mcam->bmap_entries)
2937 if (mcam->entry2cntr_map[index] != req->cntr)
2940 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2944 mutex_unlock(&mcam->lock);
2948 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
2949 struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
2951 struct npc_mcam *mcam = &rvu->hw->mcam;
2952 u16 index, entry = 0;
2955 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2957 return NPC_MCAM_INVALID_REQ;
2959 mutex_lock(&mcam->lock);
2960 rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2964 /* Unmap the MCAM entry and counter */
2966 rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
2969 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2970 req->entry, req->cntr);
2974 /* Disable all MCAM entry's stats which are using this counter */
2975 while (entry < mcam->bmap_entries) {
2976 if (!mcam->cntr_refcnt[req->cntr])
2979 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2980 if (index >= mcam->bmap_entries)
2984 if (mcam->entry2cntr_map[index] != req->cntr)
2987 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2991 mutex_unlock(&mcam->lock);
2995 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
2996 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2998 struct npc_mcam *mcam = &rvu->hw->mcam;
3001 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3003 return NPC_MCAM_INVALID_REQ;
3005 mutex_lock(&mcam->lock);
3006 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3007 mutex_unlock(&mcam->lock);
3011 rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
3016 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
3017 struct npc_mcam_oper_counter_req *req,
3018 struct npc_mcam_oper_counter_rsp *rsp)
3020 struct npc_mcam *mcam = &rvu->hw->mcam;
3023 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3025 return NPC_MCAM_INVALID_REQ;
3027 mutex_lock(&mcam->lock);
3028 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3029 mutex_unlock(&mcam->lock);
3033 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
3034 rsp->stat &= BIT_ULL(48) - 1;
3039 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
3040 struct npc_mcam_alloc_and_write_entry_req *req,
3041 struct npc_mcam_alloc_and_write_entry_rsp *rsp)
3043 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3044 struct npc_mcam_alloc_counter_req cntr_req;
3045 struct npc_mcam_alloc_counter_rsp cntr_rsp;
3046 struct npc_mcam_alloc_entry_req entry_req;
3047 struct npc_mcam_alloc_entry_rsp entry_rsp;
3048 struct npc_mcam *mcam = &rvu->hw->mcam;
3049 u16 entry = NPC_MCAM_ENTRY_INVALID;
3050 u16 cntr = NPC_MCAM_ENTRY_INVALID;
3054 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3056 return NPC_MCAM_INVALID_REQ;
3058 if (!is_npc_interface_valid(rvu, req->intf))
3059 return NPC_MCAM_INVALID_REQ;
3061 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
3063 return NPC_MCAM_INVALID_REQ;
3065 /* Try to allocate a MCAM entry */
3066 entry_req.hdr.pcifunc = req->hdr.pcifunc;
3067 entry_req.contig = true;
3068 entry_req.priority = req->priority;
3069 entry_req.ref_entry = req->ref_entry;
3070 entry_req.count = 1;
3072 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
3073 &entry_req, &entry_rsp);
3077 if (!entry_rsp.count)
3078 return NPC_MCAM_ALLOC_FAILED;
3080 entry = entry_rsp.entry;
3082 if (!req->alloc_cntr)
3085 /* Now allocate counter */
3086 cntr_req.hdr.pcifunc = req->hdr.pcifunc;
3087 cntr_req.contig = true;
3090 rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3092 /* Free allocated MCAM entry */
3093 mutex_lock(&mcam->lock);
3094 mcam->entry2pfvf_map[entry] = NPC_MCAM_INVALID_MAP;
3095 npc_mcam_clear_bit(mcam, entry);
3096 mutex_unlock(&mcam->lock);
3100 cntr = cntr_rsp.cntr;
3103 mutex_lock(&mcam->lock);
3105 if (is_npc_intf_tx(req->intf))
3106 nix_intf = pfvf->nix_tx_intf;
3108 nix_intf = pfvf->nix_rx_intf;
3110 npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
3111 &req->entry_data, req->enable_entry);
3113 if (req->alloc_cntr)
3114 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
3115 mutex_unlock(&mcam->lock);
3123 #define GET_KEX_CFG(intf) \
3124 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
3126 #define GET_KEX_FLAGS(ld) \
3127 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
3129 #define GET_KEX_LD(intf, lid, lt, ld) \
3130 rvu_read64(rvu, BLKADDR_NPC, \
3131 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
3133 #define GET_KEX_LDFLAGS(intf, ld, fl) \
3134 rvu_read64(rvu, BLKADDR_NPC, \
3135 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
3137 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
3138 struct npc_get_kex_cfg_rsp *rsp)
3140 int lid, lt, ld, fl;
3142 rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3143 rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3144 for (lid = 0; lid < NPC_MAX_LID; lid++) {
3145 for (lt = 0; lt < NPC_MAX_LT; lt++) {
3146 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3147 rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3148 GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3149 rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3150 GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3154 for (ld = 0; ld < NPC_MAX_LD; ld++)
3155 rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3157 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3158 for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3159 rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3160 GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3161 rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3162 GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3165 memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3169 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3170 struct msg_req *req,
3171 struct npc_mcam_read_base_rule_rsp *rsp)
3173 struct npc_mcam *mcam = &rvu->hw->mcam;
3174 int index, blkaddr, nixlf, rc = 0;
3175 u16 pcifunc = req->hdr.pcifunc;
3176 struct rvu_pfvf *pfvf;
3179 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3181 return NPC_MCAM_INVALID_REQ;
3183 /* Return the channel number in case of PF */
3184 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3185 pfvf = rvu_get_pfvf(rvu, pcifunc);
3186 rsp->entry.kw[0] = pfvf->rx_chan_base;
3187 rsp->entry.kw_mask[0] = 0xFFFULL;
3191 /* Find the pkt steering rule installed by PF to this VF */
3192 mutex_lock(&mcam->lock);
3193 for (index = 0; index < mcam->bmap_entries; index++) {
3194 if (mcam->entry2target_pffunc[index] == pcifunc)
3198 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3200 mutex_unlock(&mcam->lock);
3203 /* Read the default ucast entry if there is no pkt steering rule */
3204 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3207 /* Read the mcam entry */
3208 npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3210 mutex_unlock(&mcam->lock);
3215 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3216 struct npc_mcam_get_stats_req *req,
3217 struct npc_mcam_get_stats_rsp *rsp)
3219 struct npc_mcam *mcam = &rvu->hw->mcam;
3225 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3227 return NPC_MCAM_INVALID_REQ;
3229 mutex_lock(&mcam->lock);
3231 index = req->entry & (mcam->banksize - 1);
3232 bank = npc_get_bank(mcam, req->entry);
3234 /* read MCAM entry STAT_ACT register */
3235 regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3237 if (!(regval & rvu->hw->npc_stat_ena)) {
3239 mutex_unlock(&mcam->lock);
3243 cntr = regval & 0x1FF;
3246 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3247 rsp->stat &= BIT_ULL(48) - 1;
3249 mutex_unlock(&mcam->lock);