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_pffunc_af(owner) || is_afvf(target_func) ||
446 (owner & RVU_PFVF_FUNC_MASK) ||
447 !(target_func & RVU_PFVF_FUNC_MASK))
450 /* save entry2target_pffunc */
451 pfvf = rvu_get_pfvf(rvu, target_func);
452 mcam->entry2target_pffunc[index] = target_func;
454 /* don't enable rule when nixlf not attached or initialized */
455 if (!(is_nixlf_attached(rvu, target_func) &&
456 test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
459 /* copy VF default entry action to the VF mcam entry */
460 rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
463 entry->action = rx_action;
466 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
467 int blkaddr, int index, u8 intf,
468 struct mcam_entry *entry, bool enable)
470 int bank = npc_get_bank(mcam, index);
471 int kw = 0, actbank, actindex;
472 u8 tx_intf_mask = ~intf & 0x3;
476 actbank = bank; /* Save bank id, to set action later on */
478 index &= (mcam->banksize - 1);
480 /* Disable before mcam entry update */
481 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
483 /* Clear mcam entry to avoid writes being suppressed by NPC */
484 npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
486 /* CAM1 takes the comparison value and
487 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
488 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
489 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
490 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
492 for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
493 /* Interface should be set in all banks */
494 if (is_npc_intf_tx(intf)) {
495 /* Last bit must be set and rest don't care
499 tx_intf = intf & tx_intf_mask;
500 tx_intf_mask = ~tx_intf & tx_intf_mask;
503 rvu_write64(rvu, blkaddr,
504 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
506 rvu_write64(rvu, blkaddr,
507 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
510 /* Set the match key */
511 npc_get_keyword(entry, kw, &cam0, &cam1);
512 rvu_write64(rvu, blkaddr,
513 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
514 rvu_write64(rvu, blkaddr,
515 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
517 npc_get_keyword(entry, kw + 1, &cam0, &cam1);
518 rvu_write64(rvu, blkaddr,
519 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
520 rvu_write64(rvu, blkaddr,
521 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
524 /* PF installing VF rule */
525 if (intf == NIX_INTF_RX && actindex < mcam->bmap_entries)
526 npc_fixup_vf_rule(rvu, mcam, blkaddr, index, entry, &enable);
529 rvu_write64(rvu, blkaddr,
530 NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
532 /* Set TAG 'action' */
533 rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
536 /* Enable the entry */
538 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
541 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
542 int blkaddr, u16 src,
543 struct mcam_entry *entry, u8 *intf, u8 *ena)
545 int sbank = npc_get_bank(mcam, src);
549 src &= (mcam->banksize - 1);
552 for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
553 cam1 = rvu_read64(rvu, blkaddr,
554 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
555 cam0 = rvu_read64(rvu, blkaddr,
556 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
557 npc_fill_entryword(entry, kw, cam0, cam1);
559 cam1 = rvu_read64(rvu, blkaddr,
560 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
561 cam0 = rvu_read64(rvu, blkaddr,
562 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
563 npc_fill_entryword(entry, kw + 1, cam0, cam1);
566 entry->action = rvu_read64(rvu, blkaddr,
567 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
569 rvu_read64(rvu, blkaddr,
570 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
571 *intf = rvu_read64(rvu, blkaddr,
572 NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
573 *ena = rvu_read64(rvu, blkaddr,
574 NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
577 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
578 int blkaddr, u16 src, u16 dest)
580 int dbank = npc_get_bank(mcam, dest);
581 int sbank = npc_get_bank(mcam, src);
585 src &= (mcam->banksize - 1);
586 dest &= (mcam->banksize - 1);
588 /* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
589 for (bank = 0; bank < mcam->banks_per_entry; bank++) {
590 sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
591 dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
592 for (i = 0; i < 6; i++) {
593 cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
594 rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
599 cfg = rvu_read64(rvu, blkaddr,
600 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
601 rvu_write64(rvu, blkaddr,
602 NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
604 /* Copy TAG action */
605 cfg = rvu_read64(rvu, blkaddr,
606 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
607 rvu_write64(rvu, blkaddr,
608 NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
610 /* Enable or disable */
611 cfg = rvu_read64(rvu, blkaddr,
612 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
613 rvu_write64(rvu, blkaddr,
614 NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
617 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
618 int blkaddr, int index)
620 int bank = npc_get_bank(mcam, index);
622 index &= (mcam->banksize - 1);
623 return rvu_read64(rvu, blkaddr,
624 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
627 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
628 int nixlf, u64 chan, u8 *mac_addr)
630 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
631 struct npc_install_flow_req req = { 0 };
632 struct npc_install_flow_rsp rsp = { 0 };
633 struct npc_mcam *mcam = &rvu->hw->mcam;
634 struct nix_rx_action action;
637 /* AF's VFs work in promiscuous mode */
638 if (is_afvf(pcifunc))
641 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
645 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
646 nixlf, NIXLF_UCAST_ENTRY);
648 /* Don't change the action if entry is already enabled
649 * Otherwise RSS action may get overwritten.
651 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
652 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
655 *(u64 *)&action = 0x00;
656 action.op = NIX_RX_ACTIONOP_UCAST;
657 action.pf_func = pcifunc;
660 req.default_rule = 1;
661 ether_addr_copy(req.packet.dmac, mac_addr);
662 eth_broadcast_addr((u8 *)&req.mask.dmac);
663 req.features = BIT_ULL(NPC_DMAC);
665 req.chan_mask = 0xFFFU;
666 req.intf = pfvf->nix_rx_intf;
668 req.hdr.pcifunc = 0; /* AF is requester */
669 req.vf = action.pf_func;
670 req.index = action.index;
671 req.match_id = action.match_id;
672 req.flow_key_alg = action.flow_key_alg;
674 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
677 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
678 int nixlf, u64 chan, u8 chan_cnt)
680 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
681 struct npc_install_flow_req req = { 0 };
682 struct npc_install_flow_rsp rsp = { 0 };
683 struct npc_mcam *mcam = &rvu->hw->mcam;
684 struct rvu_hwinfo *hw = rvu->hw;
685 int blkaddr, ucast_idx, index;
686 struct nix_rx_action action;
689 if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
692 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
696 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
697 nixlf, NIXLF_PROMISC_ENTRY);
699 if (is_cgx_vf(rvu, pcifunc))
700 index = npc_get_nixlf_mcam_index(mcam,
701 pcifunc & ~RVU_PFVF_FUNC_MASK,
702 nixlf, NIXLF_PROMISC_ENTRY);
704 /* If the corresponding PF's ucast action is RSS,
705 * use the same action for promisc also
707 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
708 nixlf, NIXLF_UCAST_ENTRY);
709 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
710 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
713 if (action.op != NIX_RX_ACTIONOP_RSS) {
714 *(u64 *)&action = 0x00;
715 action.op = NIX_RX_ACTIONOP_UCAST;
718 /* RX_ACTION set to MCAST for CGX PF's */
719 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
720 is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
721 *(u64 *)&action = 0x00;
722 action.op = NIX_RX_ACTIONOP_MCAST;
723 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
724 action.index = pfvf->promisc_mce_idx;
727 req.chan_mask = 0xFFFU;
729 if (!is_power_of_2(chan_cnt)) {
731 "%s: channel count more than 1, must be power of 2\n", __func__);
734 relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
736 req.chan_mask &= relaxed_mask;
740 req.intf = pfvf->nix_rx_intf;
743 req.hdr.pcifunc = 0; /* AF is requester */
745 req.index = action.index;
746 req.match_id = action.match_id;
747 req.flow_key_alg = action.flow_key_alg;
749 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
752 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
753 int nixlf, bool enable)
755 struct npc_mcam *mcam = &rvu->hw->mcam;
758 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
762 /* Get 'pcifunc' of PF device */
763 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
765 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
766 nixlf, NIXLF_PROMISC_ENTRY);
767 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
770 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
773 struct rvu_pfvf *pfvf;
774 struct npc_install_flow_req req = { 0 };
775 struct npc_install_flow_rsp rsp = { 0 };
776 struct npc_mcam *mcam = &rvu->hw->mcam;
777 struct rvu_hwinfo *hw = rvu->hw;
780 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
785 if (is_afvf(pcifunc))
788 /* If pkt replication is not supported,
789 * then only PF is allowed to add a bcast match entry.
791 if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
794 /* Get 'pcifunc' of PF device */
795 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
796 pfvf = rvu_get_pfvf(rvu, pcifunc);
797 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
798 nixlf, NIXLF_BCAST_ENTRY);
800 if (!hw->cap.nix_rx_multicast) {
801 /* Early silicon doesn't support pkt replication,
802 * so install entry with UCAST action, so that PF
803 * receives all broadcast packets.
805 req.op = NIX_RX_ACTIONOP_UCAST;
807 req.op = NIX_RX_ACTIONOP_MCAST;
808 req.index = pfvf->bcast_mce_idx;
811 eth_broadcast_addr((u8 *)&req.packet.dmac);
812 eth_broadcast_addr((u8 *)&req.mask.dmac);
813 req.features = BIT_ULL(NPC_DMAC);
815 req.chan_mask = 0xFFFU;
816 req.intf = pfvf->nix_rx_intf;
818 req.hdr.pcifunc = 0; /* AF is requester */
821 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
824 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
827 struct npc_mcam *mcam = &rvu->hw->mcam;
830 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
834 /* Get 'pcifunc' of PF device */
835 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
837 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
839 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
842 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
845 struct npc_install_flow_req req = { 0 };
846 struct npc_install_flow_rsp rsp = { 0 };
847 struct npc_mcam *mcam = &rvu->hw->mcam;
848 struct rvu_hwinfo *hw = rvu->hw;
849 int blkaddr, ucast_idx, index;
850 u8 mac_addr[ETH_ALEN] = { 0 };
851 struct nix_rx_action action;
852 struct rvu_pfvf *pfvf;
855 /* Only CGX PF/VF can add allmulticast entry */
856 if (is_afvf(pcifunc))
859 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
863 /* Get 'pcifunc' of PF device */
864 vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
865 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
866 pfvf = rvu_get_pfvf(rvu, pcifunc);
867 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
868 nixlf, NIXLF_ALLMULTI_ENTRY);
870 /* If the corresponding PF's ucast action is RSS,
871 * use the same action for multicast entry also
873 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
874 nixlf, NIXLF_UCAST_ENTRY);
875 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
876 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
879 if (action.op != NIX_RX_ACTIONOP_RSS) {
880 *(u64 *)&action = 0x00;
881 action.op = NIX_RX_ACTIONOP_UCAST;
882 action.pf_func = pcifunc;
885 /* RX_ACTION set to MCAST for CGX PF's */
886 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
887 *(u64 *)&action = 0x00;
888 action.op = NIX_RX_ACTIONOP_MCAST;
889 action.index = pfvf->mcast_mce_idx;
892 mac_addr[0] = 0x01; /* LSB bit of 1st byte in DMAC */
893 ether_addr_copy(req.packet.dmac, mac_addr);
894 ether_addr_copy(req.mask.dmac, mac_addr);
895 req.features = BIT_ULL(NPC_DMAC);
897 /* For cn10k the upper two bits of the channel number are
898 * cpt channel number. with masking out these bits in the
899 * mcam entry, same entry used for NIX will allow packets
900 * received from cpt for parsing.
902 if (!is_rvu_otx2(rvu))
903 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
905 req.chan_mask = 0xFFFU;
908 req.intf = pfvf->nix_rx_intf;
911 req.hdr.pcifunc = 0; /* AF is requester */
912 req.vf = pcifunc | vf_func;
913 req.index = action.index;
914 req.match_id = action.match_id;
915 req.flow_key_alg = action.flow_key_alg;
917 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
920 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
923 struct npc_mcam *mcam = &rvu->hw->mcam;
926 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
930 /* Get 'pcifunc' of PF device */
931 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
933 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
934 NIXLF_ALLMULTI_ENTRY);
935 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
938 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
939 int blkaddr, u16 pcifunc, u64 rx_action)
941 int actindex, index, bank;
944 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
947 mutex_lock(&mcam->lock);
948 for (index = 0; index < mcam->bmap_entries; index++) {
949 if (mcam->entry2target_pffunc[index] == pcifunc) {
950 bank = npc_get_bank(mcam, index);
952 index &= (mcam->banksize - 1);
954 /* read vf flow entry enable status */
955 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
957 /* disable before mcam entry update */
958 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
960 /* update 'action' */
961 rvu_write64(rvu, blkaddr,
962 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
965 npc_enable_mcam_entry(rvu, mcam, blkaddr,
969 mutex_unlock(&mcam->lock);
972 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
973 int group, int alg_idx, int mcam_index)
975 struct npc_mcam *mcam = &rvu->hw->mcam;
976 struct rvu_hwinfo *hw = rvu->hw;
977 struct nix_rx_action action;
978 int blkaddr, index, bank;
979 struct rvu_pfvf *pfvf;
981 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
985 /* Check if this is for reserved default entry */
986 if (mcam_index < 0) {
987 if (group != DEFAULT_RSS_CONTEXT_GROUP)
989 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
990 nixlf, NIXLF_UCAST_ENTRY);
992 /* TODO: validate this mcam index */
996 if (index >= mcam->total_entries)
999 bank = npc_get_bank(mcam, index);
1000 index &= (mcam->banksize - 1);
1002 *(u64 *)&action = rvu_read64(rvu, blkaddr,
1003 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
1004 /* Ignore if no action was set earlier */
1005 if (!*(u64 *)&action)
1008 action.op = NIX_RX_ACTIONOP_RSS;
1009 action.pf_func = pcifunc;
1010 action.index = group;
1011 action.flow_key_alg = alg_idx;
1013 rvu_write64(rvu, blkaddr,
1014 NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
1016 /* update the VF flow rule action with the VF default entry action */
1018 npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
1021 /* update the action change in default rule */
1022 pfvf = rvu_get_pfvf(rvu, pcifunc);
1023 if (pfvf->def_ucast_rule)
1024 pfvf->def_ucast_rule->rx_action = action;
1026 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1027 nixlf, NIXLF_PROMISC_ENTRY);
1029 /* If PF's promiscuous entry is enabled,
1030 * Set RSS action for that entry as well
1032 if ((!hw->cap.nix_rx_multicast || !pfvf->use_mce_list) &&
1033 is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
1034 bank = npc_get_bank(mcam, index);
1035 index &= (mcam->banksize - 1);
1037 rvu_write64(rvu, blkaddr,
1038 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
1043 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1044 int nixlf, int type, bool enable)
1046 struct npc_mcam *mcam = &rvu->hw->mcam;
1047 struct rvu_hwinfo *hw = rvu->hw;
1048 struct nix_mce_list *mce_list;
1049 int index, blkaddr, mce_idx;
1050 struct rvu_pfvf *pfvf;
1052 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1056 index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1059 /* disable MCAM entry when packet replication is not supported by hw */
1060 if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1061 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1065 /* return incase mce list is not enabled */
1066 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1067 if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1068 type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1071 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1073 nix_update_mce_list(rvu, pcifunc, mce_list,
1074 mce_idx, index, enable);
1076 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1079 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1080 int nixlf, bool enable)
1082 struct npc_mcam *mcam = &rvu->hw->mcam;
1085 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1089 /* Ucast MCAM match entry of this PF/VF */
1090 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1091 nixlf, NIXLF_UCAST_ENTRY);
1092 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1094 /* Nothing to do for VFs, on platforms where pkt replication
1097 if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1100 /* add/delete pf_func to broadcast MCE list */
1101 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1102 NIXLF_BCAST_ENTRY, enable);
1105 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1107 npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1109 /* Delete multicast and promisc MCAM entries */
1110 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1111 NIXLF_ALLMULTI_ENTRY, false);
1112 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1113 NIXLF_PROMISC_ENTRY, false);
1116 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1118 /* Enables only broadcast match entry. Promisc/Allmulti are enabled
1119 * in set_rx_mode mbox handler.
1121 npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1124 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1126 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1127 struct npc_mcam *mcam = &rvu->hw->mcam;
1128 struct rvu_npc_mcam_rule *rule, *tmp;
1131 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1135 mutex_lock(&mcam->lock);
1137 /* Disable MCAM entries directing traffic to this 'pcifunc' */
1138 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1139 if (is_npc_intf_rx(rule->intf) &&
1140 rule->rx_action.pf_func == pcifunc &&
1141 rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1142 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1143 rule->entry, false);
1144 rule->enable = false;
1145 /* Indicate that default rule is disabled */
1146 if (rule->default_rule) {
1147 pfvf->def_ucast_rule = NULL;
1148 list_del(&rule->list);
1154 mutex_unlock(&mcam->lock);
1156 npc_mcam_disable_flows(rvu, pcifunc);
1158 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1161 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1163 struct npc_mcam *mcam = &rvu->hw->mcam;
1164 struct rvu_npc_mcam_rule *rule, *tmp;
1167 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1171 mutex_lock(&mcam->lock);
1173 /* Free all MCAM entries owned by this 'pcifunc' */
1174 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1176 /* Free all MCAM counters owned by this 'pcifunc' */
1177 npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1179 /* Delete MCAM entries owned by this 'pcifunc' */
1180 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1181 if (rule->owner == pcifunc && !rule->default_rule) {
1182 list_del(&rule->list);
1187 mutex_unlock(&mcam->lock);
1189 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1192 #define SET_KEX_LD(intf, lid, ltype, ld, cfg) \
1193 rvu_write64(rvu, blkaddr, \
1194 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, ltype, ld), cfg)
1196 #define SET_KEX_LDFLAGS(intf, ld, flags, cfg) \
1197 rvu_write64(rvu, blkaddr, \
1198 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, flags), cfg)
1200 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1201 struct npc_mcam_kex *mkex, u8 intf)
1203 int lid, lt, ld, fl;
1205 if (is_npc_intf_tx(intf))
1208 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1209 mkex->keyx_cfg[NIX_INTF_RX]);
1212 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1213 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1214 for (ld = 0; ld < NPC_MAX_LD; ld++)
1215 SET_KEX_LD(intf, lid, lt, ld,
1216 mkex->intf_lid_lt_ld[NIX_INTF_RX]
1220 /* Program LFLAGS */
1221 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1222 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1223 SET_KEX_LDFLAGS(intf, ld, fl,
1224 mkex->intf_ld_flags[NIX_INTF_RX]
1229 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1230 struct npc_mcam_kex *mkex, u8 intf)
1232 int lid, lt, ld, fl;
1234 if (is_npc_intf_rx(intf))
1237 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1238 mkex->keyx_cfg[NIX_INTF_TX]);
1241 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1242 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1243 for (ld = 0; ld < NPC_MAX_LD; ld++)
1244 SET_KEX_LD(intf, lid, lt, ld,
1245 mkex->intf_lid_lt_ld[NIX_INTF_TX]
1249 /* Program LFLAGS */
1250 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1251 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1252 SET_KEX_LDFLAGS(intf, ld, fl,
1253 mkex->intf_ld_flags[NIX_INTF_TX]
1258 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1259 struct npc_mcam_kex *mkex)
1261 struct rvu_hwinfo *hw = rvu->hw;
1265 for (ld = 0; ld < NPC_MAX_LD; ld++)
1266 rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1267 mkex->kex_ld_flags[ld]);
1269 for (intf = 0; intf < hw->npc_intfs; intf++) {
1270 npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1271 npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1275 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1278 u64 prfl_addr, prfl_sz;
1283 prfl_addr = rvu->fwdata->mcam_addr;
1284 prfl_sz = rvu->fwdata->mcam_sz;
1286 if (!prfl_addr || !prfl_sz)
1289 *prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1290 if (!(*prfl_img_addr))
1298 /* strtoull of "mkexprof" with base:36 */
1299 #define MKEX_END_SIGN 0xdeadbeef
1301 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1302 const char *mkex_profile)
1304 struct device *dev = &rvu->pdev->dev;
1305 struct npc_mcam_kex *mcam_kex;
1306 void __iomem *mkex_prfl_addr = NULL;
1310 /* If user not selected mkex profile */
1311 if (rvu->kpu_fwdata_sz ||
1312 !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1315 /* Setting up the mapping for mkex profile image */
1316 ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1320 mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1322 while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1323 /* Compare with mkex mod_param name string */
1324 if (mcam_kex->mkex_sign == MKEX_SIGN &&
1325 !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1326 /* Due to an errata (35786) in A0/B0 pass silicon,
1327 * parse nibble enable configuration has to be
1328 * identical for both Rx and Tx interfaces.
1330 if (!is_rvu_96xx_B0(rvu) ||
1331 mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1332 rvu->kpu.mkex = mcam_kex;
1337 prfl_sz -= sizeof(struct npc_mcam_kex);
1339 dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1342 dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1343 /* Program selected mkex profile */
1344 npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1346 iounmap(mkex_prfl_addr);
1349 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1350 const struct npc_kpu_profile_action *kpuaction,
1351 int kpu, int entry, bool pkind)
1353 struct npc_kpu_action0 action0 = {0};
1354 struct npc_kpu_action1 action1 = {0};
1357 action1.errlev = kpuaction->errlev;
1358 action1.errcode = kpuaction->errcode;
1359 action1.dp0_offset = kpuaction->dp0_offset;
1360 action1.dp1_offset = kpuaction->dp1_offset;
1361 action1.dp2_offset = kpuaction->dp2_offset;
1364 reg = NPC_AF_PKINDX_ACTION1(entry);
1366 reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1368 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1370 action0.byp_count = kpuaction->bypass_count;
1371 action0.capture_ena = kpuaction->cap_ena;
1372 action0.parse_done = kpuaction->parse_done;
1373 action0.next_state = kpuaction->next_state;
1374 action0.capture_lid = kpuaction->lid;
1375 action0.capture_ltype = kpuaction->ltype;
1376 action0.capture_flags = kpuaction->flags;
1377 action0.ptr_advance = kpuaction->ptr_advance;
1378 action0.var_len_offset = kpuaction->offset;
1379 action0.var_len_mask = kpuaction->mask;
1380 action0.var_len_right = kpuaction->right;
1381 action0.var_len_shift = kpuaction->shift;
1384 reg = NPC_AF_PKINDX_ACTION0(entry);
1386 reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1388 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1391 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1392 const struct npc_kpu_profile_cam *kpucam,
1395 struct npc_kpu_cam cam0 = {0};
1396 struct npc_kpu_cam cam1 = {0};
1398 cam1.state = kpucam->state & kpucam->state_mask;
1399 cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1400 cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1401 cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1403 cam0.state = ~kpucam->state & kpucam->state_mask;
1404 cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1405 cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1406 cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1408 rvu_write64(rvu, blkaddr,
1409 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1410 rvu_write64(rvu, blkaddr,
1411 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1414 static inline u64 enable_mask(int count)
1416 return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1419 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1420 const struct npc_kpu_profile *profile)
1422 int entry, num_entries, max_entries;
1425 if (profile->cam_entries != profile->action_entries) {
1427 "KPU%d: CAM and action entries [%d != %d] not equal\n",
1428 kpu, profile->cam_entries, profile->action_entries);
1431 max_entries = rvu->hw->npc_kpu_entries;
1433 /* Program CAM match entries for previous KPU extracted data */
1434 num_entries = min_t(int, profile->cam_entries, max_entries);
1435 for (entry = 0; entry < num_entries; entry++)
1436 npc_config_kpucam(rvu, blkaddr,
1437 &profile->cam[entry], kpu, entry);
1439 /* Program this KPU's actions */
1440 num_entries = min_t(int, profile->action_entries, max_entries);
1441 for (entry = 0; entry < num_entries; entry++)
1442 npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1445 /* Enable all programmed entries */
1446 num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1447 entry_mask = enable_mask(num_entries);
1448 /* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1449 if (!rvu->kpu.custom)
1450 entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1451 rvu_write64(rvu, blkaddr,
1452 NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1453 if (num_entries > 64) {
1454 rvu_write64(rvu, blkaddr,
1455 NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1456 enable_mask(num_entries - 64));
1459 /* Enable this KPU */
1460 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1463 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1465 profile->custom = 0;
1466 profile->name = def_pfl_name;
1467 profile->version = NPC_KPU_PROFILE_VER;
1468 profile->ikpu = ikpu_action_entries;
1469 profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1470 profile->kpu = npc_kpu_profiles;
1471 profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1472 profile->lt_def = &npc_lt_defaults;
1473 profile->mkex = &npc_mkex_default;
1478 static int npc_apply_custom_kpu(struct rvu *rvu,
1479 struct npc_kpu_profile_adapter *profile)
1481 size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1482 struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1483 struct npc_kpu_profile_action *action;
1484 struct npc_kpu_profile_cam *cam;
1485 struct npc_kpu_fwdata *fw_kpu;
1489 if (rvu->kpu_fwdata_sz < hdr_sz) {
1490 dev_warn(rvu->dev, "Invalid KPU profile size\n");
1493 if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1494 dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1498 /* Verify if the using known profile structure */
1499 if (NPC_KPU_VER_MAJ(profile->version) >
1500 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1501 dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1502 NPC_KPU_VER_MAJ(profile->version),
1503 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1506 /* Verify if profile is aligned with the required kernel changes */
1507 if (NPC_KPU_VER_MIN(profile->version) <
1508 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1510 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1511 NPC_KPU_VER_MAJ(profile->version),
1512 NPC_KPU_VER_MIN(profile->version),
1513 NPC_KPU_VER_PATCH(profile->version),
1514 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1515 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1516 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1519 /* Verify if profile fits the HW */
1520 if (fw->kpus > profile->kpus) {
1521 dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1526 profile->custom = 1;
1527 profile->name = fw->name;
1528 profile->version = le64_to_cpu(fw->version);
1529 profile->mkex = &fw->mkex;
1530 profile->lt_def = &fw->lt_def;
1532 for (kpu = 0; kpu < fw->kpus; kpu++) {
1533 fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1534 if (fw_kpu->entries > KPU_MAX_CST_ENT)
1536 "Too many custom entries on KPU%d: %d > %d\n",
1537 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1538 entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1539 cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1540 offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1541 action = (struct npc_kpu_profile_action *)(fw->data + offset);
1542 offset += fw_kpu->entries * sizeof(*action);
1543 if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1545 "Profile size mismatch on KPU%i parsing.\n",
1549 for (entry = 0; entry < entries; entry++) {
1550 profile->kpu[kpu].cam[entry] = cam[entry];
1551 profile->kpu[kpu].action[entry] = action[entry];
1558 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1559 u64 prfl_sz, const char *kpu_profile)
1561 struct npc_kpu_profile_fwdata *kpu_data = NULL;
1564 kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1565 if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1566 !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1567 dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1569 rvu->kpu_fwdata = kpu_data;
1570 rvu->kpu_fwdata_sz = prfl_sz;
1571 rvu->kpu_prfl_addr = prfl_addr;
1578 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1579 const char *kpu_profile)
1581 struct npc_coalesced_kpu_prfl *img_data = NULL;
1582 int i = 0, rc = -EINVAL;
1583 void __iomem *kpu_prfl_addr;
1586 img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1587 if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1588 !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1589 /* Loaded profile is a single KPU profile. */
1590 rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1591 prfl_sz, kpu_profile);
1595 /* Loaded profile is coalesced image, offset of first KPU profile.*/
1596 offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1597 (img_data->num_prfl * sizeof(uint16_t));
1598 /* Check if mapped image is coalesced image. */
1599 while (i < img_data->num_prfl) {
1600 /* Profile image offsets are rounded up to next 8 multiple.*/
1601 offset = ALIGN_8B_CEIL(offset);
1602 kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1604 rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1605 img_data->prfl_sz[i], kpu_profile);
1608 /* Calculating offset of profile image based on profile size.*/
1609 offset += img_data->prfl_sz[i];
1616 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1621 /* Setting up the mapping for NPC profile image */
1622 ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1626 /* Detect if profile is coalesced or single KPU profile and load */
1627 ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1631 /* Cleaning up if KPU profile image from fwdata is not valid. */
1632 if (rvu->kpu_prfl_addr) {
1633 iounmap(rvu->kpu_prfl_addr);
1634 rvu->kpu_prfl_addr = NULL;
1635 rvu->kpu_fwdata_sz = 0;
1636 rvu->kpu_fwdata = NULL;
1643 static void npc_load_kpu_profile(struct rvu *rvu)
1645 struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1646 const char *kpu_profile = rvu->kpu_pfl_name;
1647 const struct firmware *fw = NULL;
1648 bool retry_fwdb = false;
1650 /* If user not specified profile customization */
1651 if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1652 goto revert_to_default;
1653 /* First prepare default KPU, then we'll customize top entries. */
1654 npc_prepare_default_kpu(profile);
1656 /* Order of preceedence for load loading NPC profile (high to low)
1657 * Firmware binary in filesystem.
1658 * Firmware database method.
1659 * Default KPU profile.
1661 if (!request_firmware(&fw, kpu_profile, rvu->dev)) {
1662 dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1664 rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1665 if (rvu->kpu_fwdata) {
1666 memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1667 rvu->kpu_fwdata_sz = fw->size;
1669 release_firmware(fw);
1675 /* Loading the KPU profile using firmware database */
1676 if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1677 goto revert_to_default;
1680 /* Apply profile customization if firmware was loaded. */
1681 if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1682 /* If image from firmware filesystem fails to load or invalid
1683 * retry with firmware database method.
1685 if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1686 /* Loading image from firmware database failed. */
1687 if (rvu->kpu_prfl_addr) {
1688 iounmap(rvu->kpu_prfl_addr);
1689 rvu->kpu_prfl_addr = NULL;
1691 kfree(rvu->kpu_fwdata);
1693 rvu->kpu_fwdata = NULL;
1694 rvu->kpu_fwdata_sz = 0;
1697 goto load_image_fwdb;
1702 "Can't load KPU profile %s. Using default.\n",
1704 kfree(rvu->kpu_fwdata);
1705 rvu->kpu_fwdata = NULL;
1706 goto revert_to_default;
1709 dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1710 profile->name, NPC_KPU_VER_MAJ(profile->version),
1711 NPC_KPU_VER_MIN(profile->version),
1712 NPC_KPU_VER_PATCH(profile->version));
1717 npc_prepare_default_kpu(profile);
1720 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1722 struct rvu_hwinfo *hw = rvu->hw;
1723 int num_pkinds, num_kpus, idx;
1725 /* Disable all KPUs and their entries */
1726 for (idx = 0; idx < hw->npc_kpus; idx++) {
1727 rvu_write64(rvu, blkaddr,
1728 NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1729 rvu_write64(rvu, blkaddr,
1730 NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1731 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1734 /* Load and customize KPU profile. */
1735 npc_load_kpu_profile(rvu);
1737 /* First program IKPU profile i.e PKIND configs.
1738 * Check HW max count to avoid configuring junk or
1739 * writing to unsupported CSR addresses.
1741 num_pkinds = rvu->kpu.pkinds;
1742 num_pkinds = min_t(int, hw->npc_pkinds, num_pkinds);
1744 for (idx = 0; idx < num_pkinds; idx++)
1745 npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1747 /* Program KPU CAM and Action profiles */
1748 num_kpus = rvu->kpu.kpus;
1749 num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1751 for (idx = 0; idx < num_kpus; idx++)
1752 npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1755 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1757 int nixlf_count = rvu_get_nixlf_count(rvu);
1758 struct npc_mcam *mcam = &rvu->hw->mcam;
1764 /* Actual number of MCAM entries vary by entry size */
1765 cfg = (rvu_read64(rvu, blkaddr,
1766 NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1767 mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1768 mcam->keysize = cfg;
1770 /* Number of banks combined per MCAM entry */
1771 if (cfg == NPC_MCAM_KEY_X4)
1772 mcam->banks_per_entry = 4;
1773 else if (cfg == NPC_MCAM_KEY_X2)
1774 mcam->banks_per_entry = 2;
1776 mcam->banks_per_entry = 1;
1778 /* Reserve one MCAM entry for each of the NIX LF to
1779 * guarantee space to install default matching DMAC rule.
1780 * Also reserve 2 MCAM entries for each PF for default
1781 * channel based matching or 'bcast & promisc' matching to
1782 * support BCAST and PROMISC modes of operation for PFs.
1785 rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1786 ((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1787 if (mcam->total_entries <= rsvd) {
1789 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1790 mcam->total_entries);
1794 mcam->bmap_entries = mcam->total_entries - rsvd;
1795 mcam->nixlf_offset = mcam->bmap_entries;
1796 mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1798 /* Allocate bitmaps for managing MCAM entries */
1799 mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1800 sizeof(long), GFP_KERNEL);
1804 mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1805 BITS_TO_LONGS(mcam->bmap_entries),
1806 sizeof(long), GFP_KERNEL);
1807 if (!mcam->bmap_reverse)
1810 mcam->bmap_fcnt = mcam->bmap_entries;
1812 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
1813 mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1814 sizeof(u16), GFP_KERNEL);
1815 if (!mcam->entry2pfvf_map)
1818 /* Reserve 1/8th of MCAM entries at the bottom for low priority
1819 * allocations and another 1/8th at the top for high priority
1822 mcam->lprio_count = mcam->bmap_entries / 8;
1823 if (mcam->lprio_count > BITS_PER_LONG)
1824 mcam->lprio_count = round_down(mcam->lprio_count,
1826 mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1827 mcam->hprio_count = mcam->lprio_count;
1828 mcam->hprio_end = mcam->hprio_count;
1831 /* Allocate bitmap for managing MCAM counters and memory
1832 * for saving counter to RVU PFFUNC allocation mapping.
1834 err = rvu_alloc_bitmap(&mcam->counters);
1838 mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1839 sizeof(u16), GFP_KERNEL);
1840 if (!mcam->cntr2pfvf_map)
1843 /* Alloc memory for MCAM entry to counter mapping and for tracking
1844 * counter's reference count.
1846 mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1847 sizeof(u16), GFP_KERNEL);
1848 if (!mcam->entry2cntr_map)
1851 mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1852 sizeof(u16), GFP_KERNEL);
1853 if (!mcam->cntr_refcnt)
1856 /* Alloc memory for saving target device of mcam rule */
1857 mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1858 sizeof(u16), GFP_KERNEL);
1859 if (!mcam->entry2target_pffunc)
1862 for (index = 0; index < mcam->bmap_entries; index++) {
1863 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
1864 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
1867 for (cntr = 0; cntr < mcam->counters.max; cntr++)
1868 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
1870 mutex_init(&mcam->lock);
1875 kfree(mcam->counters.bmap);
1879 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1881 struct npc_pkind *pkind = &rvu->hw->pkind;
1882 struct npc_mcam *mcam = &rvu->hw->mcam;
1883 struct rvu_hwinfo *hw = rvu->hw;
1884 u64 npc_const, npc_const1;
1887 npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1888 npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1889 if (npc_const1 & BIT_ULL(63))
1890 npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1892 pkind->rsrc.max = NPC_UNRESERVED_PKIND_COUNT;
1893 hw->npc_pkinds = (npc_const1 >> 12) & 0xFFULL;
1894 hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
1895 hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
1896 hw->npc_intfs = npc_const & 0xFULL;
1897 hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
1899 mcam->banks = (npc_const >> 44) & 0xFULL;
1900 mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
1903 hw->npc_ext_set = true;
1904 hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1905 mcam->banksize = npc_const2 & 0xFFFFULL;
1908 mcam->counters.max = hw->npc_counters;
1911 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1913 struct npc_mcam *mcam = &rvu->hw->mcam;
1914 struct rvu_hwinfo *hw = rvu->hw;
1915 u64 nibble_ena, rx_kex, tx_kex;
1918 /* Reserve last counter for MCAM RX miss action which is set to
1919 * drop packet. This way we will know how many pkts didn't match
1922 mcam->counters.max--;
1923 mcam->rx_miss_act_cntr = mcam->counters.max;
1925 rx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_RX];
1926 tx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_TX];
1927 nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1929 nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1931 tx_kex &= ~NPC_PARSE_NIBBLE;
1932 tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1933 npc_mkex_default.keyx_cfg[NIX_INTF_TX] = tx_kex;
1936 /* Configure RX interfaces */
1937 for (intf = 0; intf < hw->npc_intfs; intf++) {
1938 if (is_npc_intf_tx(intf))
1941 /* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
1942 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1945 /* If MCAM lookup doesn't result in a match, drop the received
1946 * packet. And map this action to a counter to count dropped
1949 rvu_write64(rvu, blkaddr,
1950 NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1952 /* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
1953 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
1955 rvu_write64(rvu, blkaddr,
1956 NPC_AF_INTFX_MISS_STAT_ACT(intf),
1957 ((mcam->rx_miss_act_cntr >> 9) << 12) |
1958 BIT_ULL(9) | mcam->rx_miss_act_cntr);
1961 /* Configure TX interfaces */
1962 for (intf = 0; intf < hw->npc_intfs; intf++) {
1963 if (is_npc_intf_rx(intf))
1966 /* Extract Ltypes LID_LA to LID_LE */
1967 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1970 /* Set TX miss action to UCAST_DEFAULT i.e
1971 * transmit the packet on NIX LF SQ's default channel.
1973 rvu_write64(rvu, blkaddr,
1974 NPC_AF_INTFX_MISS_ACT(intf),
1975 NIX_TX_ACTIONOP_UCAST_DEFAULT);
1979 int rvu_npc_init(struct rvu *rvu)
1981 struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
1982 struct npc_pkind *pkind = &rvu->hw->pkind;
1983 struct npc_mcam *mcam = &rvu->hw->mcam;
1984 int blkaddr, entry, bank, err;
1986 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1988 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1992 rvu_npc_hw_init(rvu, blkaddr);
1994 /* First disable all MCAM entries, to stop traffic towards NIXLFs */
1995 for (bank = 0; bank < mcam->banks; bank++) {
1996 for (entry = 0; entry < mcam->banksize; entry++)
1997 rvu_write64(rvu, blkaddr,
1998 NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
2001 err = rvu_alloc_bitmap(&pkind->rsrc);
2004 /* Reserve PKIND#0 for LBKs. Power reset value of LBK_CH_PKIND is '0',
2005 * no need to configure PKIND for all LBKs separately.
2007 rvu_alloc_rsrc(&pkind->rsrc);
2009 /* Allocate mem for pkind to PF and channel mapping info */
2010 pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
2011 sizeof(u32), GFP_KERNEL);
2012 if (!pkind->pfchan_map)
2015 /* Configure KPU profile */
2016 npc_parser_profile_init(rvu, blkaddr);
2018 /* Config Outer L2, IPv4's NPC layer info */
2019 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
2020 (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
2021 kpu->lt_def->pck_ol2.ltype_mask);
2022 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
2023 (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
2024 kpu->lt_def->pck_oip4.ltype_mask);
2026 /* Config Inner IPV4 NPC layer info */
2027 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2028 (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2029 kpu->lt_def->pck_iip4.ltype_mask);
2031 /* Enable below for Rx pkts.
2032 * - Outer IPv4 header checksum validation.
2033 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2M].
2034 * - Inner IPv4 header checksum validation.
2035 * - Set non zero checksum error code value
2037 rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2038 rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2039 BIT_ULL(32) | BIT_ULL(24) | BIT_ULL(6) |
2040 BIT_ULL(2) | BIT_ULL(1));
2042 rvu_npc_setup_interfaces(rvu, blkaddr);
2044 /* Configure MKEX profile */
2045 npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2047 err = npc_mcam_rsrcs_init(rvu, blkaddr);
2051 err = npc_flow_steering_init(rvu, blkaddr);
2054 "Incorrect mkex profile loaded using default mkex\n");
2055 npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2061 void rvu_npc_freemem(struct rvu *rvu)
2063 struct npc_pkind *pkind = &rvu->hw->pkind;
2064 struct npc_mcam *mcam = &rvu->hw->mcam;
2066 kfree(pkind->rsrc.bmap);
2067 kfree(mcam->counters.bmap);
2068 if (rvu->kpu_prfl_addr)
2069 iounmap(rvu->kpu_prfl_addr);
2071 kfree(rvu->kpu_fwdata);
2072 mutex_destroy(&mcam->lock);
2075 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2076 int blkaddr, int *alloc_cnt,
2079 struct npc_mcam *mcam = &rvu->hw->mcam;
2085 for (entry = 0; entry < mcam->bmap_entries; entry++) {
2086 if (mcam->entry2pfvf_map[entry] == pcifunc) {
2088 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2094 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2095 int blkaddr, int *alloc_cnt,
2098 struct npc_mcam *mcam = &rvu->hw->mcam;
2104 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2105 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2107 if (mcam->cntr_refcnt[cntr])
2113 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
2114 u16 pcifunc, int entry)
2116 /* verify AF installed entries */
2117 if (is_pffunc_af(pcifunc))
2119 /* Verify if entry is valid and if it is indeed
2120 * allocated to the requesting PFFUNC.
2122 if (entry >= mcam->bmap_entries)
2123 return NPC_MCAM_INVALID_REQ;
2125 if (pcifunc != mcam->entry2pfvf_map[entry])
2126 return NPC_MCAM_PERM_DENIED;
2131 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2132 u16 pcifunc, int cntr)
2134 /* Verify if counter is valid and if it is indeed
2135 * allocated to the requesting PFFUNC.
2137 if (cntr >= mcam->counters.max)
2138 return NPC_MCAM_INVALID_REQ;
2140 if (pcifunc != mcam->cntr2pfvf_map[cntr])
2141 return NPC_MCAM_PERM_DENIED;
2146 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2147 int blkaddr, u16 entry, u16 cntr)
2149 u16 index = entry & (mcam->banksize - 1);
2150 u16 bank = npc_get_bank(mcam, entry);
2152 /* Set mapping and increment counter's refcnt */
2153 mcam->entry2cntr_map[entry] = cntr;
2154 mcam->cntr_refcnt[cntr]++;
2156 * NPC_AF_MCAMEX_BANKX_STAT_ACT[14:12] - counter[11:9]
2157 * NPC_AF_MCAMEX_BANKX_STAT_ACT[8:0] - counter[8:0]
2159 rvu_write64(rvu, blkaddr,
2160 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2161 ((cntr >> 9) << 12) | BIT_ULL(9) | cntr);
2164 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2165 struct npc_mcam *mcam,
2166 int blkaddr, u16 entry, u16 cntr)
2168 u16 index = entry & (mcam->banksize - 1);
2169 u16 bank = npc_get_bank(mcam, entry);
2171 /* Remove mapping and reduce counter's refcnt */
2172 mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2173 mcam->cntr_refcnt[cntr]--;
2175 rvu_write64(rvu, blkaddr,
2176 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2179 /* Sets MCAM entry in bitmap as used. Update
2180 * reverse bitmap too. Should be called with
2181 * 'mcam->lock' held.
2183 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2188 rentry = mcam->bmap_entries - index - 1;
2190 __set_bit(entry, mcam->bmap);
2191 __set_bit(rentry, mcam->bmap_reverse);
2195 /* Sets MCAM entry in bitmap as free. Update
2196 * reverse bitmap too. Should be called with
2197 * 'mcam->lock' held.
2199 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2204 rentry = mcam->bmap_entries - index - 1;
2206 __clear_bit(entry, mcam->bmap);
2207 __clear_bit(rentry, mcam->bmap_reverse);
2211 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2212 int blkaddr, u16 pcifunc)
2216 /* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2217 for (index = 0; index < mcam->bmap_entries; index++) {
2218 if (mcam->entry2pfvf_map[index] == pcifunc) {
2219 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2220 /* Free the entry in bitmap */
2221 npc_mcam_clear_bit(mcam, index);
2222 /* Disable the entry */
2223 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2225 /* Update entry2counter mapping */
2226 cntr = mcam->entry2cntr_map[index];
2227 if (cntr != NPC_MCAM_INVALID_MAP)
2228 npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2231 mcam->entry2target_pffunc[index] = 0x0;
2236 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2241 /* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2242 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2243 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2244 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2245 mcam->cntr_refcnt[cntr] = 0;
2246 rvu_free_rsrc(&mcam->counters, cntr);
2247 /* This API is expected to be called after freeing
2248 * MCAM entries, which inturn will remove
2249 * 'entry to counter' mapping.
2250 * No need to do it again.
2256 /* Find area of contiguous free entries of size 'nr'.
2257 * If not found return max contiguous free entries available.
2259 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2260 u16 nr, u16 *max_area)
2262 u16 max_area_start = 0;
2263 u16 index, next, end;
2268 index = find_next_zero_bit(map, size, start);
2270 return max_area_start;
2272 end = ((index + nr) >= size) ? size : index + nr;
2273 next = find_next_bit(map, end, index);
2274 if (*max_area < (next - index)) {
2275 *max_area = next - index;
2276 max_area_start = index;
2284 return max_area_start;
2287 /* Find number of free MCAM entries available
2288 * within range i.e in between 'start' and 'end'.
2290 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2299 index = find_next_zero_bit(map, end, start);
2303 next = find_next_bit(map, end, index);
2305 fcnt += next - index;
2310 fcnt += end - index;
2315 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2316 struct npc_mcam_alloc_entry_req *req,
2317 u16 *start, u16 *end, bool *reverse)
2321 if (req->priority == NPC_MCAM_HIGHER_PRIO)
2324 /* For a low priority entry allocation
2325 * - If reference entry is not in hprio zone then
2326 * search range: ref_entry to end.
2327 * - If reference entry is in hprio zone and if
2328 * request can be accomodated in non-hprio zone then
2329 * search range: 'start of middle zone' to 'end'
2330 * - else search in reverse, so that less number of hprio
2331 * zone entries are allocated.
2335 *start = req->ref_entry + 1;
2336 *end = mcam->bmap_entries;
2338 if (req->ref_entry >= mcam->hprio_end)
2341 fcnt = npc_mcam_get_free_count(mcam->bmap,
2342 mcam->hprio_end, mcam->bmap_entries);
2343 if (fcnt > req->count)
2344 *start = mcam->hprio_end;
2350 /* For a high priority entry allocation, search is always
2351 * in reverse to preserve hprio zone entries.
2352 * - If reference entry is not in lprio zone then
2353 * search range: 0 to ref_entry.
2354 * - If reference entry is in lprio zone and if
2355 * request can be accomodated in middle zone then
2356 * search range: 'hprio_end' to 'lprio_start'
2361 *end = req->ref_entry;
2363 if (req->ref_entry <= mcam->lprio_start)
2366 fcnt = npc_mcam_get_free_count(mcam->bmap,
2367 mcam->hprio_end, mcam->lprio_start);
2368 if (fcnt < req->count)
2370 *start = mcam->hprio_end;
2371 *end = mcam->lprio_start;
2374 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2375 struct npc_mcam_alloc_entry_req *req,
2376 struct npc_mcam_alloc_entry_rsp *rsp)
2378 u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2379 u16 fcnt, hp_fcnt, lp_fcnt;
2380 u16 start, end, index;
2381 int entry, next_start;
2382 bool reverse = false;
2383 unsigned long *bmap;
2386 mutex_lock(&mcam->lock);
2388 /* Check if there are any free entries */
2389 if (!mcam->bmap_fcnt) {
2390 mutex_unlock(&mcam->lock);
2391 return NPC_MCAM_ALLOC_FAILED;
2394 /* MCAM entries are divided into high priority, middle and
2395 * low priority zones. Idea is to not allocate top and lower
2396 * most entries as much as possible, this is to increase
2397 * probability of honouring priority allocation requests.
2399 * Two bitmaps are used for mcam entry management,
2400 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
2401 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
2403 * Reverse bitmap is used to allocate entries
2404 * - when a higher priority entry is requested
2405 * - when available free entries are less.
2406 * Lower priority ones out of avaialble free entries are always
2407 * chosen when 'high vs low' question arises.
2410 /* Get the search range for priority allocation request */
2411 if (req->priority) {
2412 npc_get_mcam_search_range_priority(mcam, req,
2413 &start, &end, &reverse);
2417 /* Find out the search range for non-priority allocation request
2419 * Get MCAM free entry count in middle zone.
2421 lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2423 mcam->bmap_entries);
2424 hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2425 fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2427 /* Check if request can be accomodated in the middle zone */
2428 if (fcnt > req->count) {
2429 start = mcam->hprio_end;
2430 end = mcam->lprio_start;
2431 } else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2432 /* Expand search zone from half of hprio zone to
2433 * half of lprio zone.
2435 start = mcam->hprio_end / 2;
2436 end = mcam->bmap_entries - (mcam->lprio_count / 2);
2439 /* Not enough free entries, search all entries in reverse,
2440 * 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;
2676 u16 channel, chan_mask;
2680 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2682 return NPC_MCAM_INVALID_REQ;
2684 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
2685 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
2686 channel &= chan_mask;
2688 mutex_lock(&mcam->lock);
2689 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2693 if (req->set_cntr &&
2694 npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2695 rc = NPC_MCAM_INVALID_REQ;
2699 if (!is_npc_interface_valid(rvu, req->intf)) {
2700 rc = NPC_MCAM_INVALID_REQ;
2704 if (is_npc_intf_tx(req->intf))
2705 nix_intf = pfvf->nix_tx_intf;
2707 nix_intf = pfvf->nix_rx_intf;
2709 if (!is_pffunc_af(pcifunc) &&
2710 npc_mcam_verify_channel(rvu, pcifunc, req->intf, channel)) {
2711 rc = NPC_MCAM_INVALID_REQ;
2715 if (!is_pffunc_af(pcifunc) &&
2716 npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf, pcifunc)) {
2717 rc = NPC_MCAM_INVALID_REQ;
2721 /* For AF installed rules, the nix_intf should be set to target NIX */
2722 if (is_pffunc_af(req->hdr.pcifunc))
2723 nix_intf = req->intf;
2725 npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2726 &req->entry_data, req->enable_entry);
2729 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2730 req->entry, req->cntr);
2734 mutex_unlock(&mcam->lock);
2738 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2739 struct npc_mcam_ena_dis_entry_req *req,
2740 struct msg_rsp *rsp)
2742 struct npc_mcam *mcam = &rvu->hw->mcam;
2743 u16 pcifunc = req->hdr.pcifunc;
2746 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2748 return NPC_MCAM_INVALID_REQ;
2750 mutex_lock(&mcam->lock);
2751 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2752 mutex_unlock(&mcam->lock);
2756 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2761 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2762 struct npc_mcam_ena_dis_entry_req *req,
2763 struct msg_rsp *rsp)
2765 struct npc_mcam *mcam = &rvu->hw->mcam;
2766 u16 pcifunc = req->hdr.pcifunc;
2769 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2771 return NPC_MCAM_INVALID_REQ;
2773 mutex_lock(&mcam->lock);
2774 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2775 mutex_unlock(&mcam->lock);
2779 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2784 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2785 struct npc_mcam_shift_entry_req *req,
2786 struct npc_mcam_shift_entry_rsp *rsp)
2788 struct npc_mcam *mcam = &rvu->hw->mcam;
2789 u16 pcifunc = req->hdr.pcifunc;
2790 u16 old_entry, new_entry;
2794 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2796 return NPC_MCAM_INVALID_REQ;
2798 if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2799 return NPC_MCAM_INVALID_REQ;
2801 mutex_lock(&mcam->lock);
2802 for (index = 0; index < req->shift_count; index++) {
2803 old_entry = req->curr_entry[index];
2804 new_entry = req->new_entry[index];
2806 /* Check if both old and new entries are valid and
2807 * does belong to this PFFUNC or not.
2809 rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2813 rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2817 /* new_entry should not have a counter mapped */
2818 if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2819 rc = NPC_MCAM_PERM_DENIED;
2823 /* Disable the new_entry */
2824 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2826 /* Copy rule from old entry to new entry */
2827 npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2829 /* Copy counter mapping, if any */
2830 cntr = mcam->entry2cntr_map[old_entry];
2831 if (cntr != NPC_MCAM_INVALID_MAP) {
2832 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2834 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2838 /* Enable new_entry and disable old_entry */
2839 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2840 npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2843 /* If shift has failed then report the failed index */
2844 if (index != req->shift_count) {
2845 rc = NPC_MCAM_PERM_DENIED;
2846 rsp->failed_entry_idx = index;
2849 mutex_unlock(&mcam->lock);
2853 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2854 struct npc_mcam_alloc_counter_req *req,
2855 struct npc_mcam_alloc_counter_rsp *rsp)
2857 struct npc_mcam *mcam = &rvu->hw->mcam;
2858 u16 pcifunc = req->hdr.pcifunc;
2859 u16 max_contig, cntr;
2862 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2864 return NPC_MCAM_INVALID_REQ;
2866 /* If the request is from a PFFUNC with no NIXLF attached, ignore */
2867 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2868 return NPC_MCAM_INVALID_REQ;
2870 /* Since list of allocated counter IDs needs to be sent to requester,
2871 * max number of non-contiguous counters per mbox msg is limited.
2873 if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2874 return NPC_MCAM_INVALID_REQ;
2876 mutex_lock(&mcam->lock);
2878 /* Check if unused counters are available or not */
2879 if (!rvu_rsrc_free_count(&mcam->counters)) {
2880 mutex_unlock(&mcam->lock);
2881 return NPC_MCAM_ALLOC_FAILED;
2887 /* Allocate requested number of contiguous counters, if
2888 * unsuccessful find max contiguous entries available.
2890 index = npc_mcam_find_zero_area(mcam->counters.bmap,
2891 mcam->counters.max, 0,
2892 req->count, &max_contig);
2893 rsp->count = max_contig;
2895 for (cntr = index; cntr < (index + max_contig); cntr++) {
2896 __set_bit(cntr, mcam->counters.bmap);
2897 mcam->cntr2pfvf_map[cntr] = pcifunc;
2900 /* Allocate requested number of non-contiguous counters,
2901 * if unsuccessful allocate as many as possible.
2903 for (cntr = 0; cntr < req->count; cntr++) {
2904 index = rvu_alloc_rsrc(&mcam->counters);
2907 rsp->cntr_list[cntr] = index;
2909 mcam->cntr2pfvf_map[index] = pcifunc;
2913 mutex_unlock(&mcam->lock);
2917 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2918 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2920 struct npc_mcam *mcam = &rvu->hw->mcam;
2921 u16 index, entry = 0;
2924 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2926 return NPC_MCAM_INVALID_REQ;
2928 mutex_lock(&mcam->lock);
2929 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2931 mutex_unlock(&mcam->lock);
2935 /* Mark counter as free/unused */
2936 mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2937 rvu_free_rsrc(&mcam->counters, req->cntr);
2939 /* Disable all MCAM entry's stats which are using this counter */
2940 while (entry < mcam->bmap_entries) {
2941 if (!mcam->cntr_refcnt[req->cntr])
2944 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2945 if (index >= mcam->bmap_entries)
2948 if (mcam->entry2cntr_map[index] != req->cntr)
2951 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2955 mutex_unlock(&mcam->lock);
2959 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
2960 struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
2962 struct npc_mcam *mcam = &rvu->hw->mcam;
2963 u16 index, entry = 0;
2966 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2968 return NPC_MCAM_INVALID_REQ;
2970 mutex_lock(&mcam->lock);
2971 rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2975 /* Unmap the MCAM entry and counter */
2977 rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
2980 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2981 req->entry, req->cntr);
2985 /* Disable all MCAM entry's stats which are using this counter */
2986 while (entry < mcam->bmap_entries) {
2987 if (!mcam->cntr_refcnt[req->cntr])
2990 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2991 if (index >= mcam->bmap_entries)
2993 if (mcam->entry2cntr_map[index] != req->cntr)
2997 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3001 mutex_unlock(&mcam->lock);
3005 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
3006 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
3008 struct npc_mcam *mcam = &rvu->hw->mcam;
3011 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3013 return NPC_MCAM_INVALID_REQ;
3015 mutex_lock(&mcam->lock);
3016 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3017 mutex_unlock(&mcam->lock);
3021 rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
3026 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
3027 struct npc_mcam_oper_counter_req *req,
3028 struct npc_mcam_oper_counter_rsp *rsp)
3030 struct npc_mcam *mcam = &rvu->hw->mcam;
3033 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3035 return NPC_MCAM_INVALID_REQ;
3037 mutex_lock(&mcam->lock);
3038 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3039 mutex_unlock(&mcam->lock);
3043 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
3044 rsp->stat &= BIT_ULL(48) - 1;
3049 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
3050 struct npc_mcam_alloc_and_write_entry_req *req,
3051 struct npc_mcam_alloc_and_write_entry_rsp *rsp)
3053 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3054 struct npc_mcam_alloc_counter_req cntr_req;
3055 struct npc_mcam_alloc_counter_rsp cntr_rsp;
3056 struct npc_mcam_alloc_entry_req entry_req;
3057 struct npc_mcam_alloc_entry_rsp entry_rsp;
3058 struct npc_mcam *mcam = &rvu->hw->mcam;
3059 u16 entry = NPC_MCAM_ENTRY_INVALID;
3060 u16 cntr = NPC_MCAM_ENTRY_INVALID;
3061 u16 channel, chan_mask;
3065 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3067 return NPC_MCAM_INVALID_REQ;
3069 if (!is_npc_interface_valid(rvu, req->intf))
3070 return NPC_MCAM_INVALID_REQ;
3072 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
3073 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
3074 channel &= chan_mask;
3076 if (npc_mcam_verify_channel(rvu, req->hdr.pcifunc, req->intf, channel))
3077 return NPC_MCAM_INVALID_REQ;
3079 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
3081 return NPC_MCAM_INVALID_REQ;
3083 /* Try to allocate a MCAM entry */
3084 entry_req.hdr.pcifunc = req->hdr.pcifunc;
3085 entry_req.contig = true;
3086 entry_req.priority = req->priority;
3087 entry_req.ref_entry = req->ref_entry;
3088 entry_req.count = 1;
3090 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
3091 &entry_req, &entry_rsp);
3095 if (!entry_rsp.count)
3096 return NPC_MCAM_ALLOC_FAILED;
3098 entry = entry_rsp.entry;
3100 if (!req->alloc_cntr)
3103 /* Now allocate counter */
3104 cntr_req.hdr.pcifunc = req->hdr.pcifunc;
3105 cntr_req.contig = true;
3108 rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3110 /* Free allocated MCAM entry */
3111 mutex_lock(&mcam->lock);
3112 mcam->entry2pfvf_map[entry] = NPC_MCAM_INVALID_MAP;
3113 npc_mcam_clear_bit(mcam, entry);
3114 mutex_unlock(&mcam->lock);
3118 cntr = cntr_rsp.cntr;
3121 mutex_lock(&mcam->lock);
3123 if (is_npc_intf_tx(req->intf))
3124 nix_intf = pfvf->nix_tx_intf;
3126 nix_intf = pfvf->nix_rx_intf;
3128 npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
3129 &req->entry_data, req->enable_entry);
3131 if (req->alloc_cntr)
3132 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
3133 mutex_unlock(&mcam->lock);
3141 #define GET_KEX_CFG(intf) \
3142 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
3144 #define GET_KEX_FLAGS(ld) \
3145 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
3147 #define GET_KEX_LD(intf, lid, lt, ld) \
3148 rvu_read64(rvu, BLKADDR_NPC, \
3149 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
3151 #define GET_KEX_LDFLAGS(intf, ld, fl) \
3152 rvu_read64(rvu, BLKADDR_NPC, \
3153 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
3155 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
3156 struct npc_get_kex_cfg_rsp *rsp)
3158 int lid, lt, ld, fl;
3160 rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3161 rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3162 for (lid = 0; lid < NPC_MAX_LID; lid++) {
3163 for (lt = 0; lt < NPC_MAX_LT; lt++) {
3164 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3165 rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3166 GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3167 rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3168 GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3172 for (ld = 0; ld < NPC_MAX_LD; ld++)
3173 rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3175 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3176 for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3177 rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3178 GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3179 rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3180 GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3183 memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3187 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3188 struct msg_req *req,
3189 struct npc_mcam_read_base_rule_rsp *rsp)
3191 struct npc_mcam *mcam = &rvu->hw->mcam;
3192 int index, blkaddr, nixlf, rc = 0;
3193 u16 pcifunc = req->hdr.pcifunc;
3194 struct rvu_pfvf *pfvf;
3197 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3199 return NPC_MCAM_INVALID_REQ;
3201 /* Return the channel number in case of PF */
3202 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3203 pfvf = rvu_get_pfvf(rvu, pcifunc);
3204 rsp->entry.kw[0] = pfvf->rx_chan_base;
3205 rsp->entry.kw_mask[0] = 0xFFFULL;
3209 /* Find the pkt steering rule installed by PF to this VF */
3210 mutex_lock(&mcam->lock);
3211 for (index = 0; index < mcam->bmap_entries; index++) {
3212 if (mcam->entry2target_pffunc[index] == pcifunc)
3216 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3218 mutex_unlock(&mcam->lock);
3221 /* Read the default ucast entry if there is no pkt steering rule */
3222 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3225 /* Read the mcam entry */
3226 npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3228 mutex_unlock(&mcam->lock);
3233 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3234 struct npc_mcam_get_stats_req *req,
3235 struct npc_mcam_get_stats_rsp *rsp)
3237 struct npc_mcam *mcam = &rvu->hw->mcam;
3243 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3245 return NPC_MCAM_INVALID_REQ;
3247 mutex_lock(&mcam->lock);
3249 index = req->entry & (mcam->banksize - 1);
3250 bank = npc_get_bank(mcam, req->entry);
3252 /* read MCAM entry STAT_ACT register */
3253 regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3255 if (!(regval & BIT_ULL(9))) {
3257 mutex_unlock(&mcam->lock);
3261 cntr = regval & 0x1FF;
3264 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3265 rsp->stat &= BIT_ULL(48) - 1;
3267 mutex_unlock(&mcam->lock);