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 2 /* Bcast & Promisc */
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_PROMISC_ENTRY)
221 return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
224 int npc_get_bank(struct npc_mcam *mcam, int index)
226 int bank = index / mcam->banksize;
228 /* 0,1 & 2,3 banks are combined for this keysize */
229 if (mcam->keysize == NPC_MCAM_KEY_X2)
235 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
236 int blkaddr, int index)
238 int bank = npc_get_bank(mcam, index);
241 index &= (mcam->banksize - 1);
242 cfg = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_CFG(index, bank));
246 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
247 int blkaddr, int index, bool enable)
249 int bank = npc_get_bank(mcam, index);
252 index &= (mcam->banksize - 1);
253 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
254 rvu_write64(rvu, blkaddr,
255 NPC_AF_MCAMEX_BANKX_CFG(index, bank),
260 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
261 int blkaddr, int index)
263 int bank = npc_get_bank(mcam, index);
266 index &= (mcam->banksize - 1);
267 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
268 rvu_write64(rvu, blkaddr,
269 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
270 rvu_write64(rvu, blkaddr,
271 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
273 rvu_write64(rvu, blkaddr,
274 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
275 rvu_write64(rvu, blkaddr,
276 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
278 rvu_write64(rvu, blkaddr,
279 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
280 rvu_write64(rvu, blkaddr,
281 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
285 static void npc_get_keyword(struct mcam_entry *entry, int idx,
286 u64 *cam0, u64 *cam1)
290 #define CAM_MASK(n) (BIT_ULL(n) - 1)
292 /* 0, 2, 4, 6 indices refer to BANKX_CAMX_W0 and
293 * 1, 3, 5, 7 indices refer to BANKX_CAMX_W1.
295 * Also, only 48 bits of BANKX_CAMX_W1 are valid.
299 /* BANK(X)_CAM_W0<63:0> = MCAM_KEY[KW0]<63:0> */
300 *cam1 = entry->kw[0];
301 kw_mask = entry->kw_mask[0];
304 /* BANK(X)_CAM_W1<47:0> = MCAM_KEY[KW1]<47:0> */
305 *cam1 = entry->kw[1] & CAM_MASK(48);
306 kw_mask = entry->kw_mask[1] & CAM_MASK(48);
309 /* BANK(X + 1)_CAM_W0<15:0> = MCAM_KEY[KW1]<63:48>
310 * BANK(X + 1)_CAM_W0<63:16> = MCAM_KEY[KW2]<47:0>
312 *cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
313 *cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
314 kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
315 kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
318 /* BANK(X + 1)_CAM_W1<15:0> = MCAM_KEY[KW2]<63:48>
319 * BANK(X + 1)_CAM_W1<47:16> = MCAM_KEY[KW3]<31:0>
321 *cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
322 *cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
323 kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
324 kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
327 /* BANK(X + 2)_CAM_W0<31:0> = MCAM_KEY[KW3]<63:32>
328 * BANK(X + 2)_CAM_W0<63:32> = MCAM_KEY[KW4]<31:0>
330 *cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
331 *cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
332 kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
333 kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
336 /* BANK(X + 2)_CAM_W1<31:0> = MCAM_KEY[KW4]<63:32>
337 * BANK(X + 2)_CAM_W1<47:32> = MCAM_KEY[KW5]<15:0>
339 *cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
340 *cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
341 kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
342 kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
345 /* BANK(X + 3)_CAM_W0<47:0> = MCAM_KEY[KW5]<63:16>
346 * BANK(X + 3)_CAM_W0<63:48> = MCAM_KEY[KW6]<15:0>
348 *cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
349 *cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
350 kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
351 kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
354 /* BANK(X + 3)_CAM_W1<47:0> = MCAM_KEY[KW6]<63:16> */
355 *cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
356 kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
361 *cam0 = ~*cam1 & kw_mask;
364 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
367 /* Similar to npc_get_keyword, but fills mcam_entry structure from
373 entry->kw_mask[0] = cam1 ^ cam0;
377 entry->kw_mask[1] = cam1 ^ cam0;
380 entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
381 entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
382 entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
383 entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
386 entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
387 entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
388 entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
389 entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
392 entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
393 entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
394 entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
395 entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
398 entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
399 entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
400 entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
401 entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
404 entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
405 entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
406 entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
407 entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
410 entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
411 entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
416 static void npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
417 int blkaddr, int index,
418 struct mcam_entry *entry)
420 u16 owner, target_func;
421 struct rvu_pfvf *pfvf;
425 owner = mcam->entry2pfvf_map[index];
426 target_func = (entry->action >> 4) & 0xffff;
427 /* return incase target is PF or LBK or rule owner is not PF */
428 if (is_afvf(target_func) || (owner & RVU_PFVF_FUNC_MASK) ||
429 !(target_func & RVU_PFVF_FUNC_MASK))
432 pfvf = rvu_get_pfvf(rvu, target_func);
433 mcam->entry2target_pffunc[index] = target_func;
434 /* return if nixlf is not attached or initialized */
435 if (!is_nixlf_attached(rvu, target_func) || !pfvf->def_ucast_rule)
438 /* get VF ucast entry rule */
439 nix_get_nixlf(rvu, target_func, &nixlf, NULL);
440 index = npc_get_nixlf_mcam_index(mcam, target_func,
441 nixlf, NIXLF_UCAST_ENTRY);
442 bank = npc_get_bank(mcam, index);
443 index &= (mcam->banksize - 1);
445 rx_action = rvu_read64(rvu, blkaddr,
446 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
448 entry->action = rx_action;
451 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
452 int blkaddr, int index, u8 intf,
453 struct mcam_entry *entry, bool enable)
455 int bank = npc_get_bank(mcam, index);
456 int kw = 0, actbank, actindex;
459 actbank = bank; /* Save bank id, to set action later on */
461 index &= (mcam->banksize - 1);
463 /* Disable before mcam entry update */
464 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
466 /* Clear mcam entry to avoid writes being suppressed by NPC */
467 npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
469 /* CAM1 takes the comparison value and
470 * CAM0 specifies match for a bit in key being '0' or '1' or 'dontcare'.
471 * CAM1<n> = 0 & CAM0<n> = 1 => match if key<n> = 0
472 * CAM1<n> = 1 & CAM0<n> = 0 => match if key<n> = 1
473 * CAM1<n> = 0 & CAM0<n> = 0 => always match i.e dontcare.
475 for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
476 /* Interface should be set in all banks */
477 rvu_write64(rvu, blkaddr,
478 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
480 rvu_write64(rvu, blkaddr,
481 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
484 /* Set the match key */
485 npc_get_keyword(entry, kw, &cam0, &cam1);
486 rvu_write64(rvu, blkaddr,
487 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
488 rvu_write64(rvu, blkaddr,
489 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
491 npc_get_keyword(entry, kw + 1, &cam0, &cam1);
492 rvu_write64(rvu, blkaddr,
493 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
494 rvu_write64(rvu, blkaddr,
495 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
498 /* copy VF default entry action to the VF mcam entry */
499 if (intf == NIX_INTF_RX && actindex < mcam->bmap_entries)
500 npc_get_default_entry_action(rvu, mcam, blkaddr, actindex,
504 rvu_write64(rvu, blkaddr,
505 NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
507 /* Set TAG 'action' */
508 rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
511 /* Enable the entry */
513 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
516 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
517 int blkaddr, u16 src,
518 struct mcam_entry *entry, u8 *intf, u8 *ena)
520 int sbank = npc_get_bank(mcam, src);
524 src &= (mcam->banksize - 1);
527 for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
528 cam1 = rvu_read64(rvu, blkaddr,
529 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
530 cam0 = rvu_read64(rvu, blkaddr,
531 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
532 npc_fill_entryword(entry, kw, cam0, cam1);
534 cam1 = rvu_read64(rvu, blkaddr,
535 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
536 cam0 = rvu_read64(rvu, blkaddr,
537 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
538 npc_fill_entryword(entry, kw + 1, cam0, cam1);
541 entry->action = rvu_read64(rvu, blkaddr,
542 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
544 rvu_read64(rvu, blkaddr,
545 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
546 *intf = rvu_read64(rvu, blkaddr,
547 NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
548 *ena = rvu_read64(rvu, blkaddr,
549 NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
552 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
553 int blkaddr, u16 src, u16 dest)
555 int dbank = npc_get_bank(mcam, dest);
556 int sbank = npc_get_bank(mcam, src);
560 src &= (mcam->banksize - 1);
561 dest &= (mcam->banksize - 1);
563 /* Copy INTF's, W0's, W1's CAM0 and CAM1 configuration */
564 for (bank = 0; bank < mcam->banks_per_entry; bank++) {
565 sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
566 dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
567 for (i = 0; i < 6; i++) {
568 cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
569 rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
574 cfg = rvu_read64(rvu, blkaddr,
575 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
576 rvu_write64(rvu, blkaddr,
577 NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
579 /* Copy TAG action */
580 cfg = rvu_read64(rvu, blkaddr,
581 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
582 rvu_write64(rvu, blkaddr,
583 NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
585 /* Enable or disable */
586 cfg = rvu_read64(rvu, blkaddr,
587 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
588 rvu_write64(rvu, blkaddr,
589 NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
592 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
593 int blkaddr, int index)
595 int bank = npc_get_bank(mcam, index);
597 index &= (mcam->banksize - 1);
598 return rvu_read64(rvu, blkaddr,
599 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
602 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
603 int nixlf, u64 chan, u8 *mac_addr)
605 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
606 struct npc_install_flow_req req = { 0 };
607 struct npc_install_flow_rsp rsp = { 0 };
608 struct npc_mcam *mcam = &rvu->hw->mcam;
609 struct nix_rx_action action;
612 /* AF's VFs work in promiscuous mode */
613 if (is_afvf(pcifunc))
616 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
620 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
621 nixlf, NIXLF_UCAST_ENTRY);
623 /* Don't change the action if entry is already enabled
624 * Otherwise RSS action may get overwritten.
626 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
627 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
630 *(u64 *)&action = 0x00;
631 action.op = NIX_RX_ACTIONOP_UCAST;
632 action.pf_func = pcifunc;
635 req.default_rule = 1;
636 ether_addr_copy(req.packet.dmac, mac_addr);
637 eth_broadcast_addr((u8 *)&req.mask.dmac);
638 req.features = BIT_ULL(NPC_DMAC);
640 req.intf = pfvf->nix_rx_intf;
642 req.hdr.pcifunc = 0; /* AF is requester */
643 req.vf = action.pf_func;
644 req.index = action.index;
645 req.match_id = action.match_id;
646 req.flow_key_alg = action.flow_key_alg;
648 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
651 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
652 int nixlf, u64 chan, u8 chan_cnt,
655 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
656 struct npc_install_flow_req req = { 0 };
657 struct npc_install_flow_rsp rsp = { 0 };
658 struct npc_mcam *mcam = &rvu->hw->mcam;
659 int blkaddr, ucast_idx, index;
660 u8 mac_addr[ETH_ALEN] = { 0 };
661 struct nix_rx_action action;
664 /* Only PF or AF VF can add a promiscuous entry */
665 if ((pcifunc & RVU_PFVF_FUNC_MASK) && !is_afvf(pcifunc))
668 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
672 *(u64 *)&action = 0x00;
673 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
674 nixlf, NIXLF_PROMISC_ENTRY);
676 /* If the corresponding PF's ucast action is RSS,
677 * use the same action for promisc also
679 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
680 nixlf, NIXLF_UCAST_ENTRY);
681 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
682 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
685 if (action.op != NIX_RX_ACTIONOP_RSS) {
686 *(u64 *)&action = 0x00;
687 action.op = NIX_RX_ACTIONOP_UCAST;
688 action.pf_func = pcifunc;
692 mac_addr[0] = 0x01; /* LSB bit of 1st byte in DMAC */
693 ether_addr_copy(req.packet.dmac, mac_addr);
694 ether_addr_copy(req.mask.dmac, mac_addr);
695 req.features = BIT_ULL(NPC_DMAC);
698 req.chan_mask = 0xFFFU;
700 if (!is_power_of_2(chan_cnt)) {
702 "%s: channel count more than 1, must be power of 2\n", __func__);
705 relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
707 req.chan_mask &= relaxed_mask;
711 req.intf = pfvf->nix_rx_intf;
714 req.hdr.pcifunc = 0; /* AF is requester */
716 req.index = action.index;
717 req.match_id = action.match_id;
718 req.flow_key_alg = action.flow_key_alg;
720 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
723 static void npc_enadis_promisc_entry(struct rvu *rvu, u16 pcifunc,
724 int nixlf, bool enable)
726 struct npc_mcam *mcam = &rvu->hw->mcam;
729 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
733 /* Only PF's have a promiscuous entry */
734 if (pcifunc & RVU_PFVF_FUNC_MASK)
737 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
738 nixlf, NIXLF_PROMISC_ENTRY);
739 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
742 void rvu_npc_disable_promisc_entry(struct rvu *rvu, u16 pcifunc, int nixlf)
744 npc_enadis_promisc_entry(rvu, pcifunc, nixlf, false);
747 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc, int nixlf)
749 npc_enadis_promisc_entry(rvu, pcifunc, nixlf, true);
752 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
755 struct rvu_pfvf *pfvf;
756 struct npc_install_flow_req req = { 0 };
757 struct npc_install_flow_rsp rsp = { 0 };
758 struct npc_mcam *mcam = &rvu->hw->mcam;
759 struct rvu_hwinfo *hw = rvu->hw;
764 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
769 if (is_afvf(pcifunc))
772 /* If pkt replication is not supported,
773 * then only PF is allowed to add a bcast match entry.
775 if (!hw->cap.nix_rx_multicast && pcifunc & RVU_PFVF_FUNC_MASK)
778 /* Get 'pcifunc' of PF device */
779 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
780 pfvf = rvu_get_pfvf(rvu, pcifunc);
781 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
782 nixlf, NIXLF_BCAST_ENTRY);
784 if (!hw->cap.nix_rx_multicast) {
785 /* Early silicon doesn't support pkt replication,
786 * so install entry with UCAST action, so that PF
787 * receives all broadcast packets.
789 op = NIX_RX_ACTIONOP_UCAST;
791 op = NIX_RX_ACTIONOP_MCAST;
792 req_index = pfvf->bcast_mce_idx;
795 eth_broadcast_addr((u8 *)&req.packet.dmac);
796 eth_broadcast_addr((u8 *)&req.mask.dmac);
797 req.features = BIT_ULL(NPC_DMAC);
799 req.intf = pfvf->nix_rx_intf;
802 req.hdr.pcifunc = 0; /* AF is requester */
804 req.index = req_index;
806 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
809 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, bool enable)
811 struct npc_mcam *mcam = &rvu->hw->mcam;
814 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
818 /* Get 'pcifunc' of PF device */
819 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
821 index = npc_get_nixlf_mcam_index(mcam, pcifunc, 0, NIXLF_BCAST_ENTRY);
822 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
825 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
826 int blkaddr, u16 pcifunc, u64 rx_action)
828 int actindex, index, bank;
831 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
834 mutex_lock(&mcam->lock);
835 for (index = 0; index < mcam->bmap_entries; index++) {
836 if (mcam->entry2target_pffunc[index] == pcifunc) {
837 bank = npc_get_bank(mcam, index);
839 index &= (mcam->banksize - 1);
841 /* read vf flow entry enable status */
842 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
844 /* disable before mcam entry update */
845 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
847 /* update 'action' */
848 rvu_write64(rvu, blkaddr,
849 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
852 npc_enable_mcam_entry(rvu, mcam, blkaddr,
856 mutex_unlock(&mcam->lock);
859 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
860 int group, int alg_idx, int mcam_index)
862 struct npc_mcam *mcam = &rvu->hw->mcam;
863 struct nix_rx_action action;
864 int blkaddr, index, bank;
865 struct rvu_pfvf *pfvf;
867 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
871 /* Check if this is for reserved default entry */
872 if (mcam_index < 0) {
873 if (group != DEFAULT_RSS_CONTEXT_GROUP)
875 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
876 nixlf, NIXLF_UCAST_ENTRY);
878 /* TODO: validate this mcam index */
882 if (index >= mcam->total_entries)
885 bank = npc_get_bank(mcam, index);
886 index &= (mcam->banksize - 1);
888 *(u64 *)&action = rvu_read64(rvu, blkaddr,
889 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
890 /* Ignore if no action was set earlier */
891 if (!*(u64 *)&action)
894 action.op = NIX_RX_ACTIONOP_RSS;
895 action.pf_func = pcifunc;
896 action.index = group;
897 action.flow_key_alg = alg_idx;
899 rvu_write64(rvu, blkaddr,
900 NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
902 /* update the VF flow rule action with the VF default entry action */
904 npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
907 /* update the action change in default rule */
908 pfvf = rvu_get_pfvf(rvu, pcifunc);
909 if (pfvf->def_ucast_rule)
910 pfvf->def_ucast_rule->rx_action = action;
912 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
913 nixlf, NIXLF_PROMISC_ENTRY);
915 /* If PF's promiscuous entry is enabled,
916 * Set RSS action for that entry as well
918 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
919 bank = npc_get_bank(mcam, index);
920 index &= (mcam->banksize - 1);
922 rvu_write64(rvu, blkaddr,
923 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
928 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
929 int nixlf, bool enable)
931 struct npc_mcam *mcam = &rvu->hw->mcam;
932 struct nix_rx_action action;
933 int index, bank, blkaddr;
935 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
939 /* Ucast MCAM match entry of this PF/VF */
940 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
941 nixlf, NIXLF_UCAST_ENTRY);
942 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
944 /* For PF, ena/dis promisc and bcast MCAM match entries.
945 * For VFs add/delete from bcast list when RX multicast
946 * feature is present.
948 if (pcifunc & RVU_PFVF_FUNC_MASK && !rvu->hw->cap.nix_rx_multicast)
951 /* For bcast, enable/disable only if it's action is not
952 * packet replication, incase if action is replication
953 * then this PF/VF's nixlf is removed from bcast replication
956 index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
957 nixlf, NIXLF_BCAST_ENTRY);
958 bank = npc_get_bank(mcam, index);
959 *(u64 *)&action = rvu_read64(rvu, blkaddr,
960 NPC_AF_MCAMEX_BANKX_ACTION(index & (mcam->banksize - 1), bank));
962 /* VFs will not have BCAST entry */
963 if (action.op != NIX_RX_ACTIONOP_MCAST &&
964 !(pcifunc & RVU_PFVF_FUNC_MASK)) {
965 npc_enable_mcam_entry(rvu, mcam,
966 blkaddr, index, enable);
968 nix_update_bcast_mce_list(rvu, pcifunc, enable);
969 /* Enable PF's BCAST entry for packet replication */
970 rvu_npc_enable_bcast_entry(rvu, pcifunc, enable);
974 rvu_npc_enable_promisc_entry(rvu, pcifunc, nixlf);
976 rvu_npc_disable_promisc_entry(rvu, pcifunc, nixlf);
979 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
981 npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
984 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
986 npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
989 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
991 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
992 struct npc_mcam *mcam = &rvu->hw->mcam;
993 struct rvu_npc_mcam_rule *rule, *tmp;
996 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1000 mutex_lock(&mcam->lock);
1002 /* Disable MCAM entries directing traffic to this 'pcifunc' */
1003 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1004 if (is_npc_intf_rx(rule->intf) &&
1005 rule->rx_action.pf_func == pcifunc) {
1006 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1007 rule->entry, false);
1008 rule->enable = false;
1009 /* Indicate that default rule is disabled */
1010 if (rule->default_rule) {
1011 pfvf->def_ucast_rule = NULL;
1012 list_del(&rule->list);
1018 mutex_unlock(&mcam->lock);
1020 npc_mcam_disable_flows(rvu, pcifunc);
1022 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1025 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1027 struct npc_mcam *mcam = &rvu->hw->mcam;
1028 struct rvu_npc_mcam_rule *rule, *tmp;
1031 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1035 mutex_lock(&mcam->lock);
1037 /* Free all MCAM entries owned by this 'pcifunc' */
1038 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1040 /* Free all MCAM counters owned by this 'pcifunc' */
1041 npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1043 /* Delete MCAM entries owned by this 'pcifunc' */
1044 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1045 if (rule->owner == pcifunc && !rule->default_rule) {
1046 list_del(&rule->list);
1051 mutex_unlock(&mcam->lock);
1053 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1056 #define SET_KEX_LD(intf, lid, ltype, ld, cfg) \
1057 rvu_write64(rvu, blkaddr, \
1058 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, ltype, ld), cfg)
1060 #define SET_KEX_LDFLAGS(intf, ld, flags, cfg) \
1061 rvu_write64(rvu, blkaddr, \
1062 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, flags), cfg)
1064 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1065 struct npc_mcam_kex *mkex, u8 intf)
1067 int lid, lt, ld, fl;
1069 if (is_npc_intf_tx(intf))
1072 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1073 mkex->keyx_cfg[NIX_INTF_RX]);
1076 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1077 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1078 for (ld = 0; ld < NPC_MAX_LD; ld++)
1079 SET_KEX_LD(intf, lid, lt, ld,
1080 mkex->intf_lid_lt_ld[NIX_INTF_RX]
1084 /* Program LFLAGS */
1085 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1086 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1087 SET_KEX_LDFLAGS(intf, ld, fl,
1088 mkex->intf_ld_flags[NIX_INTF_RX]
1093 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1094 struct npc_mcam_kex *mkex, u8 intf)
1096 int lid, lt, ld, fl;
1098 if (is_npc_intf_rx(intf))
1101 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1102 mkex->keyx_cfg[NIX_INTF_TX]);
1105 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1106 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1107 for (ld = 0; ld < NPC_MAX_LD; ld++)
1108 SET_KEX_LD(intf, lid, lt, ld,
1109 mkex->intf_lid_lt_ld[NIX_INTF_TX]
1113 /* Program LFLAGS */
1114 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1115 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1116 SET_KEX_LDFLAGS(intf, ld, fl,
1117 mkex->intf_ld_flags[NIX_INTF_TX]
1122 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1123 struct npc_mcam_kex *mkex)
1125 struct rvu_hwinfo *hw = rvu->hw;
1129 for (ld = 0; ld < NPC_MAX_LD; ld++)
1130 rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1131 mkex->kex_ld_flags[ld]);
1133 for (intf = 0; intf < hw->npc_intfs; intf++) {
1134 npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1135 npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1139 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1142 u64 prfl_addr, prfl_sz;
1147 prfl_addr = rvu->fwdata->mcam_addr;
1148 prfl_sz = rvu->fwdata->mcam_sz;
1150 if (!prfl_addr || !prfl_sz)
1153 *prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1154 if (!(*prfl_img_addr))
1162 /* strtoull of "mkexprof" with base:36 */
1163 #define MKEX_END_SIGN 0xdeadbeef
1165 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1166 const char *mkex_profile)
1168 struct device *dev = &rvu->pdev->dev;
1169 struct npc_mcam_kex *mcam_kex;
1170 void __iomem *mkex_prfl_addr = NULL;
1174 /* If user not selected mkex profile */
1175 if (rvu->kpu_fwdata_sz ||
1176 !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1179 /* Setting up the mapping for mkex profile image */
1180 ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1184 mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1186 while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1187 /* Compare with mkex mod_param name string */
1188 if (mcam_kex->mkex_sign == MKEX_SIGN &&
1189 !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1190 /* Due to an errata (35786) in A0/B0 pass silicon,
1191 * parse nibble enable configuration has to be
1192 * identical for both Rx and Tx interfaces.
1194 if (!is_rvu_96xx_B0(rvu) ||
1195 mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1196 rvu->kpu.mkex = mcam_kex;
1201 prfl_sz -= sizeof(struct npc_mcam_kex);
1203 dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1206 dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1207 /* Program selected mkex profile */
1208 npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1210 iounmap(mkex_prfl_addr);
1213 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1214 const struct npc_kpu_profile_action *kpuaction,
1215 int kpu, int entry, bool pkind)
1217 struct npc_kpu_action0 action0 = {0};
1218 struct npc_kpu_action1 action1 = {0};
1221 action1.errlev = kpuaction->errlev;
1222 action1.errcode = kpuaction->errcode;
1223 action1.dp0_offset = kpuaction->dp0_offset;
1224 action1.dp1_offset = kpuaction->dp1_offset;
1225 action1.dp2_offset = kpuaction->dp2_offset;
1228 reg = NPC_AF_PKINDX_ACTION1(entry);
1230 reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1232 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1234 action0.byp_count = kpuaction->bypass_count;
1235 action0.capture_ena = kpuaction->cap_ena;
1236 action0.parse_done = kpuaction->parse_done;
1237 action0.next_state = kpuaction->next_state;
1238 action0.capture_lid = kpuaction->lid;
1239 action0.capture_ltype = kpuaction->ltype;
1240 action0.capture_flags = kpuaction->flags;
1241 action0.ptr_advance = kpuaction->ptr_advance;
1242 action0.var_len_offset = kpuaction->offset;
1243 action0.var_len_mask = kpuaction->mask;
1244 action0.var_len_right = kpuaction->right;
1245 action0.var_len_shift = kpuaction->shift;
1248 reg = NPC_AF_PKINDX_ACTION0(entry);
1250 reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1252 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1255 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1256 const struct npc_kpu_profile_cam *kpucam,
1259 struct npc_kpu_cam cam0 = {0};
1260 struct npc_kpu_cam cam1 = {0};
1262 cam1.state = kpucam->state & kpucam->state_mask;
1263 cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1264 cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1265 cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1267 cam0.state = ~kpucam->state & kpucam->state_mask;
1268 cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1269 cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1270 cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1272 rvu_write64(rvu, blkaddr,
1273 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1274 rvu_write64(rvu, blkaddr,
1275 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1278 static inline u64 enable_mask(int count)
1280 return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1283 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1284 const struct npc_kpu_profile *profile)
1286 int entry, num_entries, max_entries;
1289 if (profile->cam_entries != profile->action_entries) {
1291 "KPU%d: CAM and action entries [%d != %d] not equal\n",
1292 kpu, profile->cam_entries, profile->action_entries);
1295 max_entries = rvu->hw->npc_kpu_entries;
1297 /* Program CAM match entries for previous KPU extracted data */
1298 num_entries = min_t(int, profile->cam_entries, max_entries);
1299 for (entry = 0; entry < num_entries; entry++)
1300 npc_config_kpucam(rvu, blkaddr,
1301 &profile->cam[entry], kpu, entry);
1303 /* Program this KPU's actions */
1304 num_entries = min_t(int, profile->action_entries, max_entries);
1305 for (entry = 0; entry < num_entries; entry++)
1306 npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1309 /* Enable all programmed entries */
1310 num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1311 entry_mask = enable_mask(num_entries);
1312 /* Disable first KPU_MAX_CST_ENT entries for built-in profile */
1313 if (!rvu->kpu.custom)
1314 entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1315 rvu_write64(rvu, blkaddr,
1316 NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1317 if (num_entries > 64) {
1318 rvu_write64(rvu, blkaddr,
1319 NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1320 enable_mask(num_entries - 64));
1323 /* Enable this KPU */
1324 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1327 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1329 profile->custom = 0;
1330 profile->name = def_pfl_name;
1331 profile->version = NPC_KPU_PROFILE_VER;
1332 profile->ikpu = ikpu_action_entries;
1333 profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1334 profile->kpu = npc_kpu_profiles;
1335 profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1336 profile->lt_def = &npc_lt_defaults;
1337 profile->mkex = &npc_mkex_default;
1342 static int npc_apply_custom_kpu(struct rvu *rvu,
1343 struct npc_kpu_profile_adapter *profile)
1345 size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1346 struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1347 struct npc_kpu_profile_action *action;
1348 struct npc_kpu_profile_cam *cam;
1349 struct npc_kpu_fwdata *fw_kpu;
1353 if (rvu->kpu_fwdata_sz < hdr_sz) {
1354 dev_warn(rvu->dev, "Invalid KPU profile size\n");
1357 if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1358 dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1362 /* Verify if the using known profile structure */
1363 if (NPC_KPU_VER_MAJ(profile->version) >
1364 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1365 dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1366 NPC_KPU_VER_MAJ(profile->version),
1367 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1370 /* Verify if profile is aligned with the required kernel changes */
1371 if (NPC_KPU_VER_MIN(profile->version) <
1372 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1374 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1375 NPC_KPU_VER_MAJ(profile->version),
1376 NPC_KPU_VER_MIN(profile->version),
1377 NPC_KPU_VER_PATCH(profile->version),
1378 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1379 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1380 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1383 /* Verify if profile fits the HW */
1384 if (fw->kpus > profile->kpus) {
1385 dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1390 profile->custom = 1;
1391 profile->name = fw->name;
1392 profile->version = le64_to_cpu(fw->version);
1393 profile->mkex = &fw->mkex;
1394 profile->lt_def = &fw->lt_def;
1396 for (kpu = 0; kpu < fw->kpus; kpu++) {
1397 fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1398 if (fw_kpu->entries > KPU_MAX_CST_ENT)
1400 "Too many custom entries on KPU%d: %d > %d\n",
1401 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1402 entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1403 cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1404 offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1405 action = (struct npc_kpu_profile_action *)(fw->data + offset);
1406 offset += fw_kpu->entries * sizeof(*action);
1407 if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1409 "Profile size mismatch on KPU%i parsing.\n",
1413 for (entry = 0; entry < entries; entry++) {
1414 profile->kpu[kpu].cam[entry] = cam[entry];
1415 profile->kpu[kpu].action[entry] = action[entry];
1422 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1423 u64 prfl_sz, const char *kpu_profile)
1425 struct npc_kpu_profile_fwdata *kpu_data = NULL;
1428 kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1429 if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1430 !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1431 dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1433 rvu->kpu_fwdata = kpu_data;
1434 rvu->kpu_fwdata_sz = prfl_sz;
1435 rvu->kpu_prfl_addr = prfl_addr;
1442 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1443 const char *kpu_profile)
1445 struct npc_coalesced_kpu_prfl *img_data = NULL;
1446 int i = 0, rc = -EINVAL;
1447 void __iomem *kpu_prfl_addr;
1450 img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1451 if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1452 !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1453 /* Loaded profile is a single KPU profile. */
1454 rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1455 prfl_sz, kpu_profile);
1459 /* Loaded profile is coalesced image, offset of first KPU profile.*/
1460 offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1461 (img_data->num_prfl * sizeof(uint16_t));
1462 /* Check if mapped image is coalesced image. */
1463 while (i < img_data->num_prfl) {
1464 /* Profile image offsets are rounded up to next 8 multiple.*/
1465 offset = ALIGN_8B_CEIL(offset);
1466 kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1468 rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1469 img_data->prfl_sz[i], kpu_profile);
1472 /* Calculating offset of profile image based on profile size.*/
1473 offset += img_data->prfl_sz[i];
1480 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1485 /* Setting up the mapping for NPC profile image */
1486 ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1490 /* Detect if profile is coalesced or single KPU profile and load */
1491 ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1495 /* Cleaning up if KPU profile image from fwdata is not valid. */
1496 if (rvu->kpu_prfl_addr) {
1497 iounmap(rvu->kpu_prfl_addr);
1498 rvu->kpu_prfl_addr = NULL;
1499 rvu->kpu_fwdata_sz = 0;
1500 rvu->kpu_fwdata = NULL;
1507 static void npc_load_kpu_profile(struct rvu *rvu)
1509 struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1510 const char *kpu_profile = rvu->kpu_pfl_name;
1511 const struct firmware *fw = NULL;
1512 bool retry_fwdb = false;
1514 /* If user not specified profile customization */
1515 if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1516 goto revert_to_default;
1517 /* First prepare default KPU, then we'll customize top entries. */
1518 npc_prepare_default_kpu(profile);
1520 /* Order of preceedence for load loading NPC profile (high to low)
1521 * Firmware binary in filesystem.
1522 * Firmware database method.
1523 * Default KPU profile.
1525 if (!request_firmware(&fw, kpu_profile, rvu->dev)) {
1526 dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1528 rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1529 if (rvu->kpu_fwdata) {
1530 memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1531 rvu->kpu_fwdata_sz = fw->size;
1533 release_firmware(fw);
1539 /* Loading the KPU profile using firmware database */
1540 if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1541 goto revert_to_default;
1544 /* Apply profile customization if firmware was loaded. */
1545 if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1546 /* If image from firmware filesystem fails to load or invalid
1547 * retry with firmware database method.
1549 if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1550 /* Loading image from firmware database failed. */
1551 if (rvu->kpu_prfl_addr) {
1552 iounmap(rvu->kpu_prfl_addr);
1553 rvu->kpu_prfl_addr = NULL;
1555 kfree(rvu->kpu_fwdata);
1557 rvu->kpu_fwdata = NULL;
1558 rvu->kpu_fwdata_sz = 0;
1561 goto load_image_fwdb;
1566 "Can't load KPU profile %s. Using default.\n",
1568 kfree(rvu->kpu_fwdata);
1569 rvu->kpu_fwdata = NULL;
1570 goto revert_to_default;
1573 dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1574 profile->name, NPC_KPU_VER_MAJ(profile->version),
1575 NPC_KPU_VER_MIN(profile->version),
1576 NPC_KPU_VER_PATCH(profile->version));
1581 npc_prepare_default_kpu(profile);
1584 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1586 struct rvu_hwinfo *hw = rvu->hw;
1587 int num_pkinds, num_kpus, idx;
1588 struct npc_pkind *pkind;
1590 /* Disable all KPUs and their entries */
1591 for (idx = 0; idx < hw->npc_kpus; idx++) {
1592 rvu_write64(rvu, blkaddr,
1593 NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1594 rvu_write64(rvu, blkaddr,
1595 NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1596 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1599 /* Load and customize KPU profile. */
1600 npc_load_kpu_profile(rvu);
1602 /* First program IKPU profile i.e PKIND configs.
1603 * Check HW max count to avoid configuring junk or
1604 * writing to unsupported CSR addresses.
1607 num_pkinds = rvu->kpu.pkinds;
1608 num_pkinds = min_t(int, pkind->rsrc.max, num_pkinds);
1610 for (idx = 0; idx < num_pkinds; idx++)
1611 npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1613 /* Program KPU CAM and Action profiles */
1614 num_kpus = rvu->kpu.kpus;
1615 num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1617 for (idx = 0; idx < num_kpus; idx++)
1618 npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1621 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1623 int nixlf_count = rvu_get_nixlf_count(rvu);
1624 struct npc_mcam *mcam = &rvu->hw->mcam;
1628 /* Actual number of MCAM entries vary by entry size */
1629 cfg = (rvu_read64(rvu, blkaddr,
1630 NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1631 mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1632 mcam->keysize = cfg;
1634 /* Number of banks combined per MCAM entry */
1635 if (cfg == NPC_MCAM_KEY_X4)
1636 mcam->banks_per_entry = 4;
1637 else if (cfg == NPC_MCAM_KEY_X2)
1638 mcam->banks_per_entry = 2;
1640 mcam->banks_per_entry = 1;
1642 /* Reserve one MCAM entry for each of the NIX LF to
1643 * guarantee space to install default matching DMAC rule.
1644 * Also reserve 2 MCAM entries for each PF for default
1645 * channel based matching or 'bcast & promisc' matching to
1646 * support BCAST and PROMISC modes of operation for PFs.
1649 rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1650 ((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1651 if (mcam->total_entries <= rsvd) {
1653 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1654 mcam->total_entries);
1658 mcam->bmap_entries = mcam->total_entries - rsvd;
1659 mcam->nixlf_offset = mcam->bmap_entries;
1660 mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1662 /* Allocate bitmaps for managing MCAM entries */
1663 mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1664 sizeof(long), GFP_KERNEL);
1668 mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1669 BITS_TO_LONGS(mcam->bmap_entries),
1670 sizeof(long), GFP_KERNEL);
1671 if (!mcam->bmap_reverse)
1674 mcam->bmap_fcnt = mcam->bmap_entries;
1676 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
1677 mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1678 sizeof(u16), GFP_KERNEL);
1679 if (!mcam->entry2pfvf_map)
1682 /* Reserve 1/8th of MCAM entries at the bottom for low priority
1683 * allocations and another 1/8th at the top for high priority
1686 mcam->lprio_count = mcam->bmap_entries / 8;
1687 if (mcam->lprio_count > BITS_PER_LONG)
1688 mcam->lprio_count = round_down(mcam->lprio_count,
1690 mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1691 mcam->hprio_count = mcam->lprio_count;
1692 mcam->hprio_end = mcam->hprio_count;
1695 /* Allocate bitmap for managing MCAM counters and memory
1696 * for saving counter to RVU PFFUNC allocation mapping.
1698 err = rvu_alloc_bitmap(&mcam->counters);
1702 mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1703 sizeof(u16), GFP_KERNEL);
1704 if (!mcam->cntr2pfvf_map)
1707 /* Alloc memory for MCAM entry to counter mapping and for tracking
1708 * counter's reference count.
1710 mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1711 sizeof(u16), GFP_KERNEL);
1712 if (!mcam->entry2cntr_map)
1715 mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1716 sizeof(u16), GFP_KERNEL);
1717 if (!mcam->cntr_refcnt)
1720 /* Alloc memory for saving target device of mcam rule */
1721 mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1722 sizeof(u16), GFP_KERNEL);
1723 if (!mcam->entry2target_pffunc)
1726 mutex_init(&mcam->lock);
1731 kfree(mcam->counters.bmap);
1735 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1737 struct npc_pkind *pkind = &rvu->hw->pkind;
1738 struct npc_mcam *mcam = &rvu->hw->mcam;
1739 struct rvu_hwinfo *hw = rvu->hw;
1740 u64 npc_const, npc_const1;
1743 npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1744 npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1745 if (npc_const1 & BIT_ULL(63))
1746 npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1748 pkind->rsrc.max = (npc_const1 >> 12) & 0xFFULL;
1749 hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
1750 hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
1751 hw->npc_intfs = npc_const & 0xFULL;
1752 hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
1754 mcam->banks = (npc_const >> 44) & 0xFULL;
1755 mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
1758 hw->npc_ext_set = true;
1759 hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1760 mcam->banksize = npc_const2 & 0xFFFFULL;
1763 mcam->counters.max = hw->npc_counters;
1766 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1768 struct npc_mcam *mcam = &rvu->hw->mcam;
1769 struct rvu_hwinfo *hw = rvu->hw;
1770 u64 nibble_ena, rx_kex, tx_kex;
1773 /* Reserve last counter for MCAM RX miss action which is set to
1774 * drop packet. This way we will know how many pkts didn't match
1777 mcam->counters.max--;
1778 mcam->rx_miss_act_cntr = mcam->counters.max;
1780 rx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_RX];
1781 tx_kex = npc_mkex_default.keyx_cfg[NIX_INTF_TX];
1782 nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1784 nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1786 tx_kex &= ~NPC_PARSE_NIBBLE;
1787 tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1788 npc_mkex_default.keyx_cfg[NIX_INTF_TX] = tx_kex;
1791 /* Configure RX interfaces */
1792 for (intf = 0; intf < hw->npc_intfs; intf++) {
1793 if (is_npc_intf_tx(intf))
1796 /* Set RX MCAM search key size. LA..LE (ltype only) + Channel */
1797 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1800 /* If MCAM lookup doesn't result in a match, drop the received
1801 * packet. And map this action to a counter to count dropped
1804 rvu_write64(rvu, blkaddr,
1805 NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1807 /* NPC_AF_INTFX_MISS_STAT_ACT[14:12] - counter[11:9]
1808 * NPC_AF_INTFX_MISS_STAT_ACT[8:0] - counter[8:0]
1810 rvu_write64(rvu, blkaddr,
1811 NPC_AF_INTFX_MISS_STAT_ACT(intf),
1812 ((mcam->rx_miss_act_cntr >> 9) << 12) |
1813 BIT_ULL(9) | mcam->rx_miss_act_cntr);
1816 /* Configure TX interfaces */
1817 for (intf = 0; intf < hw->npc_intfs; intf++) {
1818 if (is_npc_intf_rx(intf))
1821 /* Extract Ltypes LID_LA to LID_LE */
1822 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1825 /* Set TX miss action to UCAST_DEFAULT i.e
1826 * transmit the packet on NIX LF SQ's default channel.
1828 rvu_write64(rvu, blkaddr,
1829 NPC_AF_INTFX_MISS_ACT(intf),
1830 NIX_TX_ACTIONOP_UCAST_DEFAULT);
1834 int rvu_npc_init(struct rvu *rvu)
1836 struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
1837 struct npc_pkind *pkind = &rvu->hw->pkind;
1838 struct npc_mcam *mcam = &rvu->hw->mcam;
1839 int blkaddr, entry, bank, err;
1841 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1843 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1847 rvu_npc_hw_init(rvu, blkaddr);
1849 /* First disable all MCAM entries, to stop traffic towards NIXLFs */
1850 for (bank = 0; bank < mcam->banks; bank++) {
1851 for (entry = 0; entry < mcam->banksize; entry++)
1852 rvu_write64(rvu, blkaddr,
1853 NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
1856 err = rvu_alloc_bitmap(&pkind->rsrc);
1860 /* Allocate mem for pkind to PF and channel mapping info */
1861 pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
1862 sizeof(u32), GFP_KERNEL);
1863 if (!pkind->pfchan_map)
1866 /* Configure KPU profile */
1867 npc_parser_profile_init(rvu, blkaddr);
1869 /* Config Outer L2, IPv4's NPC layer info */
1870 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
1871 (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
1872 kpu->lt_def->pck_ol2.ltype_mask);
1873 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
1874 (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
1875 kpu->lt_def->pck_oip4.ltype_mask);
1877 /* Config Inner IPV4 NPC layer info */
1878 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
1879 (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
1880 kpu->lt_def->pck_iip4.ltype_mask);
1882 /* Enable below for Rx pkts.
1883 * - Outer IPv4 header checksum validation.
1884 * - Detect outer L2 broadcast address and set NPC_RESULT_S[L2M].
1885 * - Inner IPv4 header checksum validation.
1886 * - Set non zero checksum error code value
1888 rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
1889 rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
1890 BIT_ULL(32) | BIT_ULL(24) | BIT_ULL(6) |
1891 BIT_ULL(2) | BIT_ULL(1));
1893 rvu_npc_setup_interfaces(rvu, blkaddr);
1895 /* Configure MKEX profile */
1896 npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
1898 err = npc_mcam_rsrcs_init(rvu, blkaddr);
1902 err = npc_flow_steering_init(rvu, blkaddr);
1905 "Incorrect mkex profile loaded using default mkex\n");
1906 npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
1912 void rvu_npc_freemem(struct rvu *rvu)
1914 struct npc_pkind *pkind = &rvu->hw->pkind;
1915 struct npc_mcam *mcam = &rvu->hw->mcam;
1917 kfree(pkind->rsrc.bmap);
1918 kfree(mcam->counters.bmap);
1919 if (rvu->kpu_prfl_addr)
1920 iounmap(rvu->kpu_prfl_addr);
1922 kfree(rvu->kpu_fwdata);
1923 mutex_destroy(&mcam->lock);
1926 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
1927 int blkaddr, int *alloc_cnt,
1930 struct npc_mcam *mcam = &rvu->hw->mcam;
1936 for (entry = 0; entry < mcam->bmap_entries; entry++) {
1937 if (mcam->entry2pfvf_map[entry] == pcifunc) {
1939 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
1945 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
1946 int blkaddr, int *alloc_cnt,
1949 struct npc_mcam *mcam = &rvu->hw->mcam;
1955 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
1956 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
1958 if (mcam->cntr_refcnt[cntr])
1964 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
1965 u16 pcifunc, int entry)
1967 /* verify AF installed entries */
1968 if (is_pffunc_af(pcifunc))
1970 /* Verify if entry is valid and if it is indeed
1971 * allocated to the requesting PFFUNC.
1973 if (entry >= mcam->bmap_entries)
1974 return NPC_MCAM_INVALID_REQ;
1976 if (pcifunc != mcam->entry2pfvf_map[entry])
1977 return NPC_MCAM_PERM_DENIED;
1982 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
1983 u16 pcifunc, int cntr)
1985 /* Verify if counter is valid and if it is indeed
1986 * allocated to the requesting PFFUNC.
1988 if (cntr >= mcam->counters.max)
1989 return NPC_MCAM_INVALID_REQ;
1991 if (pcifunc != mcam->cntr2pfvf_map[cntr])
1992 return NPC_MCAM_PERM_DENIED;
1997 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
1998 int blkaddr, u16 entry, u16 cntr)
2000 u16 index = entry & (mcam->banksize - 1);
2001 u16 bank = npc_get_bank(mcam, entry);
2003 /* Set mapping and increment counter's refcnt */
2004 mcam->entry2cntr_map[entry] = cntr;
2005 mcam->cntr_refcnt[cntr]++;
2007 * NPC_AF_MCAMEX_BANKX_STAT_ACT[14:12] - counter[11:9]
2008 * NPC_AF_MCAMEX_BANKX_STAT_ACT[8:0] - counter[8:0]
2010 rvu_write64(rvu, blkaddr,
2011 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2012 ((cntr >> 9) << 12) | BIT_ULL(9) | cntr);
2015 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2016 struct npc_mcam *mcam,
2017 int blkaddr, u16 entry, u16 cntr)
2019 u16 index = entry & (mcam->banksize - 1);
2020 u16 bank = npc_get_bank(mcam, entry);
2022 /* Remove mapping and reduce counter's refcnt */
2023 mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2024 mcam->cntr_refcnt[cntr]--;
2026 rvu_write64(rvu, blkaddr,
2027 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2030 /* Sets MCAM entry in bitmap as used. Update
2031 * reverse bitmap too. Should be called with
2032 * 'mcam->lock' held.
2034 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2039 rentry = mcam->bmap_entries - index - 1;
2041 __set_bit(entry, mcam->bmap);
2042 __set_bit(rentry, mcam->bmap_reverse);
2046 /* Sets MCAM entry in bitmap as free. Update
2047 * reverse bitmap too. Should be called with
2048 * 'mcam->lock' held.
2050 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2055 rentry = mcam->bmap_entries - index - 1;
2057 __clear_bit(entry, mcam->bmap);
2058 __clear_bit(rentry, mcam->bmap_reverse);
2062 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2063 int blkaddr, u16 pcifunc)
2067 /* Scan all MCAM entries and free the ones mapped to 'pcifunc' */
2068 for (index = 0; index < mcam->bmap_entries; index++) {
2069 if (mcam->entry2pfvf_map[index] == pcifunc) {
2070 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2071 /* Free the entry in bitmap */
2072 npc_mcam_clear_bit(mcam, index);
2073 /* Disable the entry */
2074 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2076 /* Update entry2counter mapping */
2077 cntr = mcam->entry2cntr_map[index];
2078 if (cntr != NPC_MCAM_INVALID_MAP)
2079 npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2082 mcam->entry2target_pffunc[index] = 0x0;
2087 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2092 /* Scan all MCAM counters and free the ones mapped to 'pcifunc' */
2093 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2094 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2095 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2096 mcam->cntr_refcnt[cntr] = 0;
2097 rvu_free_rsrc(&mcam->counters, cntr);
2098 /* This API is expected to be called after freeing
2099 * MCAM entries, which inturn will remove
2100 * 'entry to counter' mapping.
2101 * No need to do it again.
2107 /* Find area of contiguous free entries of size 'nr'.
2108 * If not found return max contiguous free entries available.
2110 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2111 u16 nr, u16 *max_area)
2113 u16 max_area_start = 0;
2114 u16 index, next, end;
2119 index = find_next_zero_bit(map, size, start);
2121 return max_area_start;
2123 end = ((index + nr) >= size) ? size : index + nr;
2124 next = find_next_bit(map, end, index);
2125 if (*max_area < (next - index)) {
2126 *max_area = next - index;
2127 max_area_start = index;
2135 return max_area_start;
2138 /* Find number of free MCAM entries available
2139 * within range i.e in between 'start' and 'end'.
2141 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2150 index = find_next_zero_bit(map, end, start);
2154 next = find_next_bit(map, end, index);
2156 fcnt += next - index;
2161 fcnt += end - index;
2166 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2167 struct npc_mcam_alloc_entry_req *req,
2168 u16 *start, u16 *end, bool *reverse)
2172 if (req->priority == NPC_MCAM_HIGHER_PRIO)
2175 /* For a low priority entry allocation
2176 * - If reference entry is not in hprio zone then
2177 * search range: ref_entry to end.
2178 * - If reference entry is in hprio zone and if
2179 * request can be accomodated in non-hprio zone then
2180 * search range: 'start of middle zone' to 'end'
2181 * - else search in reverse, so that less number of hprio
2182 * zone entries are allocated.
2186 *start = req->ref_entry + 1;
2187 *end = mcam->bmap_entries;
2189 if (req->ref_entry >= mcam->hprio_end)
2192 fcnt = npc_mcam_get_free_count(mcam->bmap,
2193 mcam->hprio_end, mcam->bmap_entries);
2194 if (fcnt > req->count)
2195 *start = mcam->hprio_end;
2201 /* For a high priority entry allocation, search is always
2202 * in reverse to preserve hprio zone entries.
2203 * - If reference entry is not in lprio zone then
2204 * search range: 0 to ref_entry.
2205 * - If reference entry is in lprio zone and if
2206 * request can be accomodated in middle zone then
2207 * search range: 'hprio_end' to 'lprio_start'
2212 *end = req->ref_entry;
2214 if (req->ref_entry <= mcam->lprio_start)
2217 fcnt = npc_mcam_get_free_count(mcam->bmap,
2218 mcam->hprio_end, mcam->lprio_start);
2219 if (fcnt < req->count)
2221 *start = mcam->hprio_end;
2222 *end = mcam->lprio_start;
2225 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2226 struct npc_mcam_alloc_entry_req *req,
2227 struct npc_mcam_alloc_entry_rsp *rsp)
2229 u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2230 u16 fcnt, hp_fcnt, lp_fcnt;
2231 u16 start, end, index;
2232 int entry, next_start;
2233 bool reverse = false;
2234 unsigned long *bmap;
2237 mutex_lock(&mcam->lock);
2239 /* Check if there are any free entries */
2240 if (!mcam->bmap_fcnt) {
2241 mutex_unlock(&mcam->lock);
2242 return NPC_MCAM_ALLOC_FAILED;
2245 /* MCAM entries are divided into high priority, middle and
2246 * low priority zones. Idea is to not allocate top and lower
2247 * most entries as much as possible, this is to increase
2248 * probability of honouring priority allocation requests.
2250 * Two bitmaps are used for mcam entry management,
2251 * mcam->bmap for forward search i.e '0 to mcam->bmap_entries'.
2252 * mcam->bmap_reverse for reverse search i.e 'mcam->bmap_entries to 0'.
2254 * Reverse bitmap is used to allocate entries
2255 * - when a higher priority entry is requested
2256 * - when available free entries are less.
2257 * Lower priority ones out of avaialble free entries are always
2258 * chosen when 'high vs low' question arises.
2261 /* Get the search range for priority allocation request */
2262 if (req->priority) {
2263 npc_get_mcam_search_range_priority(mcam, req,
2264 &start, &end, &reverse);
2268 /* Find out the search range for non-priority allocation request
2270 * Get MCAM free entry count in middle zone.
2272 lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2274 mcam->bmap_entries);
2275 hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2276 fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2278 /* Check if request can be accomodated in the middle zone */
2279 if (fcnt > req->count) {
2280 start = mcam->hprio_end;
2281 end = mcam->lprio_start;
2282 } else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2283 /* Expand search zone from half of hprio zone to
2284 * half of lprio zone.
2286 start = mcam->hprio_end / 2;
2287 end = mcam->bmap_entries - (mcam->lprio_count / 2);
2290 /* Not enough free entries, search all entries in reverse,
2291 * so that low priority ones will get used up.
2295 end = mcam->bmap_entries;
2300 bmap = mcam->bmap_reverse;
2301 start = mcam->bmap_entries - start;
2302 end = mcam->bmap_entries - end;
2311 /* Allocate requested number of contiguous entries, if
2312 * unsuccessful find max contiguous entries available.
2314 index = npc_mcam_find_zero_area(bmap, end, start,
2315 req->count, &max_contig);
2316 rsp->count = max_contig;
2318 rsp->entry = mcam->bmap_entries - index - max_contig;
2322 /* Allocate requested number of non-contiguous entries,
2323 * if unsuccessful allocate as many as possible.
2327 for (entry = 0; entry < req->count; entry++) {
2328 index = find_next_zero_bit(bmap, end, next_start);
2332 next_start = start + (index - start) + 1;
2334 /* Save the entry's index */
2336 index = mcam->bmap_entries - index - 1;
2337 entry_list[entry] = index;
2342 /* If allocating requested no of entries is unsucessful,
2343 * expand the search range to full bitmap length and retry.
2345 if (!req->priority && (rsp->count < req->count) &&
2346 ((end - start) != mcam->bmap_entries)) {
2349 end = mcam->bmap_entries;
2353 /* For priority entry allocation requests, if allocation is
2354 * failed then expand search to max possible range and retry.
2356 if (req->priority && rsp->count < req->count) {
2357 if (req->priority == NPC_MCAM_LOWER_PRIO &&
2358 (start != (req->ref_entry + 1))) {
2359 start = req->ref_entry + 1;
2360 end = mcam->bmap_entries;
2363 } else if ((req->priority == NPC_MCAM_HIGHER_PRIO) &&
2364 ((end - start) != req->ref_entry)) {
2366 end = req->ref_entry;
2372 /* Copy MCAM entry indices into mbox response entry_list.
2373 * Requester always expects indices in ascending order, so
2374 * so reverse the list if reverse bitmap is used for allocation.
2376 if (!req->contig && rsp->count) {
2378 for (entry = rsp->count - 1; entry >= 0; entry--) {
2380 rsp->entry_list[index++] = entry_list[entry];
2382 rsp->entry_list[entry] = entry_list[entry];
2386 /* Mark the allocated entries as used and set nixlf mapping */
2387 for (entry = 0; entry < rsp->count; entry++) {
2388 index = req->contig ?
2389 (rsp->entry + entry) : rsp->entry_list[entry];
2390 npc_mcam_set_bit(mcam, index);
2391 mcam->entry2pfvf_map[index] = pcifunc;
2392 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2395 /* Update available free count in mbox response */
2396 rsp->free_count = mcam->bmap_fcnt;
2398 mutex_unlock(&mcam->lock);
2402 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
2403 struct npc_mcam_alloc_entry_req *req,
2404 struct npc_mcam_alloc_entry_rsp *rsp)
2406 struct npc_mcam *mcam = &rvu->hw->mcam;
2407 u16 pcifunc = req->hdr.pcifunc;
2410 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2412 return NPC_MCAM_INVALID_REQ;
2414 rsp->entry = NPC_MCAM_ENTRY_INVALID;
2415 rsp->free_count = 0;
2417 /* Check if ref_entry is within range */
2418 if (req->priority && req->ref_entry >= mcam->bmap_entries)
2419 return NPC_MCAM_INVALID_REQ;
2421 /* ref_entry can't be '0' if requested priority is high.
2422 * Can't be last entry if requested priority is low.
2424 if ((!req->ref_entry && req->priority == NPC_MCAM_HIGHER_PRIO) ||
2425 ((req->ref_entry == (mcam->bmap_entries - 1)) &&
2426 req->priority == NPC_MCAM_LOWER_PRIO))
2427 return NPC_MCAM_INVALID_REQ;
2429 /* Since list of allocated indices needs to be sent to requester,
2430 * max number of non-contiguous entries per mbox msg is limited.
2432 if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES)
2433 return NPC_MCAM_INVALID_REQ;
2435 /* Alloc request from PFFUNC with no NIXLF attached should be denied */
2436 if (!is_nixlf_attached(rvu, pcifunc))
2437 return NPC_MCAM_ALLOC_DENIED;
2439 return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
2442 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
2443 struct npc_mcam_free_entry_req *req,
2444 struct msg_rsp *rsp)
2446 struct npc_mcam *mcam = &rvu->hw->mcam;
2447 u16 pcifunc = req->hdr.pcifunc;
2448 int blkaddr, rc = 0;
2451 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2453 return NPC_MCAM_INVALID_REQ;
2455 /* Free request from PFFUNC with no NIXLF attached, ignore */
2456 if (!is_nixlf_attached(rvu, pcifunc))
2457 return NPC_MCAM_INVALID_REQ;
2459 mutex_lock(&mcam->lock);
2464 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2468 mcam->entry2pfvf_map[req->entry] = 0;
2469 mcam->entry2target_pffunc[req->entry] = 0x0;
2470 npc_mcam_clear_bit(mcam, req->entry);
2471 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2473 /* Update entry2counter mapping */
2474 cntr = mcam->entry2cntr_map[req->entry];
2475 if (cntr != NPC_MCAM_INVALID_MAP)
2476 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2482 /* Free up all entries allocated to requesting PFFUNC */
2483 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
2485 mutex_unlock(&mcam->lock);
2489 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
2490 struct npc_mcam_read_entry_req *req,
2491 struct npc_mcam_read_entry_rsp *rsp)
2493 struct npc_mcam *mcam = &rvu->hw->mcam;
2494 u16 pcifunc = req->hdr.pcifunc;
2497 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2499 return NPC_MCAM_INVALID_REQ;
2501 mutex_lock(&mcam->lock);
2502 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2504 npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
2506 &rsp->intf, &rsp->enable);
2509 mutex_unlock(&mcam->lock);
2513 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
2514 struct npc_mcam_write_entry_req *req,
2515 struct msg_rsp *rsp)
2517 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2518 struct npc_mcam *mcam = &rvu->hw->mcam;
2519 u16 pcifunc = req->hdr.pcifunc;
2520 u16 channel, chan_mask;
2524 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2526 return NPC_MCAM_INVALID_REQ;
2528 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
2529 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
2530 channel &= chan_mask;
2532 mutex_lock(&mcam->lock);
2533 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2537 if (req->set_cntr &&
2538 npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2539 rc = NPC_MCAM_INVALID_REQ;
2543 if (!is_npc_interface_valid(rvu, req->intf)) {
2544 rc = NPC_MCAM_INVALID_REQ;
2548 if (is_npc_intf_tx(req->intf))
2549 nix_intf = pfvf->nix_tx_intf;
2551 nix_intf = pfvf->nix_rx_intf;
2553 if (npc_mcam_verify_channel(rvu, pcifunc, req->intf, channel)) {
2554 rc = NPC_MCAM_INVALID_REQ;
2558 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
2560 rc = NPC_MCAM_INVALID_REQ;
2564 /* For AF installed rules, the nix_intf should be set to target NIX */
2565 if (is_pffunc_af(req->hdr.pcifunc))
2566 nix_intf = req->intf;
2568 npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2569 &req->entry_data, req->enable_entry);
2572 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2573 req->entry, req->cntr);
2577 mutex_unlock(&mcam->lock);
2581 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2582 struct npc_mcam_ena_dis_entry_req *req,
2583 struct msg_rsp *rsp)
2585 struct npc_mcam *mcam = &rvu->hw->mcam;
2586 u16 pcifunc = req->hdr.pcifunc;
2589 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2591 return NPC_MCAM_INVALID_REQ;
2593 mutex_lock(&mcam->lock);
2594 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2595 mutex_unlock(&mcam->lock);
2599 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2604 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2605 struct npc_mcam_ena_dis_entry_req *req,
2606 struct msg_rsp *rsp)
2608 struct npc_mcam *mcam = &rvu->hw->mcam;
2609 u16 pcifunc = req->hdr.pcifunc;
2612 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2614 return NPC_MCAM_INVALID_REQ;
2616 mutex_lock(&mcam->lock);
2617 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2618 mutex_unlock(&mcam->lock);
2622 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2627 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2628 struct npc_mcam_shift_entry_req *req,
2629 struct npc_mcam_shift_entry_rsp *rsp)
2631 struct npc_mcam *mcam = &rvu->hw->mcam;
2632 u16 pcifunc = req->hdr.pcifunc;
2633 u16 old_entry, new_entry;
2637 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2639 return NPC_MCAM_INVALID_REQ;
2641 if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2642 return NPC_MCAM_INVALID_REQ;
2644 mutex_lock(&mcam->lock);
2645 for (index = 0; index < req->shift_count; index++) {
2646 old_entry = req->curr_entry[index];
2647 new_entry = req->new_entry[index];
2649 /* Check if both old and new entries are valid and
2650 * does belong to this PFFUNC or not.
2652 rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2656 rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2660 /* new_entry should not have a counter mapped */
2661 if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2662 rc = NPC_MCAM_PERM_DENIED;
2666 /* Disable the new_entry */
2667 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2669 /* Copy rule from old entry to new entry */
2670 npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2672 /* Copy counter mapping, if any */
2673 cntr = mcam->entry2cntr_map[old_entry];
2674 if (cntr != NPC_MCAM_INVALID_MAP) {
2675 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2677 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2681 /* Enable new_entry and disable old_entry */
2682 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2683 npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2686 /* If shift has failed then report the failed index */
2687 if (index != req->shift_count) {
2688 rc = NPC_MCAM_PERM_DENIED;
2689 rsp->failed_entry_idx = index;
2692 mutex_unlock(&mcam->lock);
2696 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2697 struct npc_mcam_alloc_counter_req *req,
2698 struct npc_mcam_alloc_counter_rsp *rsp)
2700 struct npc_mcam *mcam = &rvu->hw->mcam;
2701 u16 pcifunc = req->hdr.pcifunc;
2702 u16 max_contig, cntr;
2705 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2707 return NPC_MCAM_INVALID_REQ;
2709 /* If the request is from a PFFUNC with no NIXLF attached, ignore */
2710 if (!is_nixlf_attached(rvu, pcifunc))
2711 return NPC_MCAM_INVALID_REQ;
2713 /* Since list of allocated counter IDs needs to be sent to requester,
2714 * max number of non-contiguous counters per mbox msg is limited.
2716 if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2717 return NPC_MCAM_INVALID_REQ;
2719 mutex_lock(&mcam->lock);
2721 /* Check if unused counters are available or not */
2722 if (!rvu_rsrc_free_count(&mcam->counters)) {
2723 mutex_unlock(&mcam->lock);
2724 return NPC_MCAM_ALLOC_FAILED;
2730 /* Allocate requested number of contiguous counters, if
2731 * unsuccessful find max contiguous entries available.
2733 index = npc_mcam_find_zero_area(mcam->counters.bmap,
2734 mcam->counters.max, 0,
2735 req->count, &max_contig);
2736 rsp->count = max_contig;
2738 for (cntr = index; cntr < (index + max_contig); cntr++) {
2739 __set_bit(cntr, mcam->counters.bmap);
2740 mcam->cntr2pfvf_map[cntr] = pcifunc;
2743 /* Allocate requested number of non-contiguous counters,
2744 * if unsuccessful allocate as many as possible.
2746 for (cntr = 0; cntr < req->count; cntr++) {
2747 index = rvu_alloc_rsrc(&mcam->counters);
2750 rsp->cntr_list[cntr] = index;
2752 mcam->cntr2pfvf_map[index] = pcifunc;
2756 mutex_unlock(&mcam->lock);
2760 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2761 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2763 struct npc_mcam *mcam = &rvu->hw->mcam;
2764 u16 index, entry = 0;
2767 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2769 return NPC_MCAM_INVALID_REQ;
2771 mutex_lock(&mcam->lock);
2772 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2774 mutex_unlock(&mcam->lock);
2778 /* Mark counter as free/unused */
2779 mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2780 rvu_free_rsrc(&mcam->counters, req->cntr);
2782 /* Disable all MCAM entry's stats which are using this counter */
2783 while (entry < mcam->bmap_entries) {
2784 if (!mcam->cntr_refcnt[req->cntr])
2787 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2788 if (index >= mcam->bmap_entries)
2791 if (mcam->entry2cntr_map[index] != req->cntr)
2794 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2798 mutex_unlock(&mcam->lock);
2802 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
2803 struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
2805 struct npc_mcam *mcam = &rvu->hw->mcam;
2806 u16 index, entry = 0;
2809 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2811 return NPC_MCAM_INVALID_REQ;
2813 mutex_lock(&mcam->lock);
2814 rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2818 /* Unmap the MCAM entry and counter */
2820 rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
2823 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2824 req->entry, req->cntr);
2828 /* Disable all MCAM entry's stats which are using this counter */
2829 while (entry < mcam->bmap_entries) {
2830 if (!mcam->cntr_refcnt[req->cntr])
2833 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2834 if (index >= mcam->bmap_entries)
2836 if (mcam->entry2cntr_map[index] != req->cntr)
2840 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2844 mutex_unlock(&mcam->lock);
2848 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
2849 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2851 struct npc_mcam *mcam = &rvu->hw->mcam;
2854 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2856 return NPC_MCAM_INVALID_REQ;
2858 mutex_lock(&mcam->lock);
2859 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2860 mutex_unlock(&mcam->lock);
2864 rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
2869 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
2870 struct npc_mcam_oper_counter_req *req,
2871 struct npc_mcam_oper_counter_rsp *rsp)
2873 struct npc_mcam *mcam = &rvu->hw->mcam;
2876 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2878 return NPC_MCAM_INVALID_REQ;
2880 mutex_lock(&mcam->lock);
2881 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2882 mutex_unlock(&mcam->lock);
2886 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
2887 rsp->stat &= BIT_ULL(48) - 1;
2892 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
2893 struct npc_mcam_alloc_and_write_entry_req *req,
2894 struct npc_mcam_alloc_and_write_entry_rsp *rsp)
2896 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2897 struct npc_mcam_alloc_counter_req cntr_req;
2898 struct npc_mcam_alloc_counter_rsp cntr_rsp;
2899 struct npc_mcam_alloc_entry_req entry_req;
2900 struct npc_mcam_alloc_entry_rsp entry_rsp;
2901 struct npc_mcam *mcam = &rvu->hw->mcam;
2902 u16 entry = NPC_MCAM_ENTRY_INVALID;
2903 u16 cntr = NPC_MCAM_ENTRY_INVALID;
2904 u16 channel, chan_mask;
2908 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2910 return NPC_MCAM_INVALID_REQ;
2912 if (!is_npc_interface_valid(rvu, req->intf))
2913 return NPC_MCAM_INVALID_REQ;
2915 chan_mask = req->entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
2916 channel = req->entry_data.kw[0] & NPC_KEX_CHAN_MASK;
2917 channel &= chan_mask;
2919 if (npc_mcam_verify_channel(rvu, req->hdr.pcifunc, req->intf, channel))
2920 return NPC_MCAM_INVALID_REQ;
2922 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
2924 return NPC_MCAM_INVALID_REQ;
2926 /* Try to allocate a MCAM entry */
2927 entry_req.hdr.pcifunc = req->hdr.pcifunc;
2928 entry_req.contig = true;
2929 entry_req.priority = req->priority;
2930 entry_req.ref_entry = req->ref_entry;
2931 entry_req.count = 1;
2933 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
2934 &entry_req, &entry_rsp);
2938 if (!entry_rsp.count)
2939 return NPC_MCAM_ALLOC_FAILED;
2941 entry = entry_rsp.entry;
2943 if (!req->alloc_cntr)
2946 /* Now allocate counter */
2947 cntr_req.hdr.pcifunc = req->hdr.pcifunc;
2948 cntr_req.contig = true;
2951 rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
2953 /* Free allocated MCAM entry */
2954 mutex_lock(&mcam->lock);
2955 mcam->entry2pfvf_map[entry] = 0;
2956 npc_mcam_clear_bit(mcam, entry);
2957 mutex_unlock(&mcam->lock);
2961 cntr = cntr_rsp.cntr;
2964 mutex_lock(&mcam->lock);
2966 if (is_npc_intf_tx(req->intf))
2967 nix_intf = pfvf->nix_tx_intf;
2969 nix_intf = pfvf->nix_rx_intf;
2971 npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
2972 &req->entry_data, req->enable_entry);
2974 if (req->alloc_cntr)
2975 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
2976 mutex_unlock(&mcam->lock);
2984 #define GET_KEX_CFG(intf) \
2985 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
2987 #define GET_KEX_FLAGS(ld) \
2988 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
2990 #define GET_KEX_LD(intf, lid, lt, ld) \
2991 rvu_read64(rvu, BLKADDR_NPC, \
2992 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
2994 #define GET_KEX_LDFLAGS(intf, ld, fl) \
2995 rvu_read64(rvu, BLKADDR_NPC, \
2996 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
2998 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
2999 struct npc_get_kex_cfg_rsp *rsp)
3001 int lid, lt, ld, fl;
3003 rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3004 rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3005 for (lid = 0; lid < NPC_MAX_LID; lid++) {
3006 for (lt = 0; lt < NPC_MAX_LT; lt++) {
3007 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3008 rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3009 GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3010 rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3011 GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3015 for (ld = 0; ld < NPC_MAX_LD; ld++)
3016 rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3018 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3019 for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3020 rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3021 GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3022 rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3023 GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3026 memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3030 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3031 struct msg_req *req,
3032 struct npc_mcam_read_base_rule_rsp *rsp)
3034 struct npc_mcam *mcam = &rvu->hw->mcam;
3035 int index, blkaddr, nixlf, rc = 0;
3036 u16 pcifunc = req->hdr.pcifunc;
3037 struct rvu_pfvf *pfvf;
3040 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3042 return NPC_MCAM_INVALID_REQ;
3044 /* Return the channel number in case of PF */
3045 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3046 pfvf = rvu_get_pfvf(rvu, pcifunc);
3047 rsp->entry.kw[0] = pfvf->rx_chan_base;
3048 rsp->entry.kw_mask[0] = 0xFFFULL;
3052 /* Find the pkt steering rule installed by PF to this VF */
3053 mutex_lock(&mcam->lock);
3054 for (index = 0; index < mcam->bmap_entries; index++) {
3055 if (mcam->entry2target_pffunc[index] == pcifunc)
3059 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3061 mutex_unlock(&mcam->lock);
3064 /* Read the default ucast entry if there is no pkt steering rule */
3065 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3068 /* Read the mcam entry */
3069 npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3071 mutex_unlock(&mcam->lock);
3076 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3077 struct npc_mcam_get_stats_req *req,
3078 struct npc_mcam_get_stats_rsp *rsp)
3080 struct npc_mcam *mcam = &rvu->hw->mcam;
3086 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3088 return NPC_MCAM_INVALID_REQ;
3090 mutex_lock(&mcam->lock);
3092 index = req->entry & (mcam->banksize - 1);
3093 bank = npc_get_bank(mcam, req->entry);
3095 /* read MCAM entry STAT_ACT register */
3096 regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3098 if (!(regval & BIT_ULL(9))) {
3100 mutex_unlock(&mcam->lock);
3104 cntr = regval & 0x1FF;
3107 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3108 rsp->stat &= BIT_ULL(48) - 1;
3110 mutex_unlock(&mcam->lock);